]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/group.c
c1410d3879b1bf0e8d368b62bfde7cd9b4c758b7
[openldap] / servers / slapd / back-ldap / group.c
1 /* group.c - ldap backend acl group routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/socket.h>
13 #include <ac/string.h>
14
15 #include "slap.h"
16 #include "back-ldap.h"
17
18
19 /* return 0 IFF op_dn is a value in group_at (member) attribute
20  * of entry with gr_dn AND that entry has an objectClass
21  * value of group_oc (groupOfNames)
22  */
23 int
24 ldap_back_group(
25         Backend         *be,
26         Connection      *conn,
27         Operation       *op,
28         Entry           *target,
29         struct berval   *gr_ndn,
30         struct berval   *op_ndn,
31         ObjectClass     *group_oc,
32         AttributeDescription* group_at
33 )
34 {
35         struct ldapinfo *li = (struct ldapinfo *) be->be_private;    
36         int rc = 1;
37         Attribute   *attr;
38
39         LDAPMessage     *result;
40         char *gattr[2];
41         char *filter = NULL, *ptr;
42         LDAP *ld;
43         struct berval mop_ndn = { 0, NULL }, mgr_ndn = { 0, NULL };
44
45         AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
46         struct berval group_oc_name = {0, NULL};
47         struct berval group_at_name = group_at->ad_cname;
48
49         if( group_oc->soc_names && group_oc->soc_names[0] ) {
50                 group_oc_name.bv_val = group_oc->soc_names[0];
51         } else {
52                 group_oc_name.bv_val = group_oc->soc_oid;
53         }
54         if (group_oc_name.bv_val)
55                 group_oc_name.bv_len = strlen(group_oc_name.bv_val);
56
57         if (target != NULL && dn_match( &target->e_nname, gr_ndn ) ) {
58                 /* we already have a copy of the entry */
59                 /* attribute and objectclass mapping has already been done */
60
61                 /*
62                  * first we need to check if the objectClass attribute
63                  * has been retieved; otherwise we need to repeat the search
64                  */
65                 attr = attr_find( target->e_attrs, ad_objectClass );
66                 if ( attr != NULL ) {
67
68                         /*
69                          * Now we can check for the group objectClass value
70                          */
71                         if( !is_entry_objectclass( target, group_oc ) ) {
72                                 return(1);
73                         }
74
75                         /*
76                          * This part has been reworked: the group attr compare
77                          * fails only if the attribute is PRESENT but the value
78                          * is NOT PRESENT; if the attribute is NOT PRESENT, the
79                          * search must be repeated as well.
80                          * This may happen if a search for an entry has already
81                          * been performed (target is not null) but the group
82                          * attribute has not been required
83                          */
84                         if ((attr = attr_find(target->e_attrs, group_at)) != NULL) {
85                                 if( value_find( group_at, attr->a_vals, op_ndn ) != LDAP_SUCCESS  )
86                                         return(1);
87                                 return(0);
88                         } /* else: repeat the search */
89                 } /* else: repeat the search */
90         } /* else: do the search */
91
92         /*
93          * Rewrite the op ndn if needed
94          */
95 #ifdef ENABLE_REWRITE
96         switch ( rewrite_session( li->rwinfo, "bindDn",
97                                 op_ndn->bv_val, conn, &mop_ndn.bv_val ) ) {
98         case REWRITE_REGEXEC_OK:
99                 if ( mop_ndn.bv_val == NULL ) {
100                         mop_ndn = *op_ndn;
101                 }
102 #ifdef NEW_LOGGING
103                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
104                                 "[rw] bindDn (op ndn in group):"
105                                 " \"%s\" -> \"%s\"\n", op_ndn->bv_val, mop_ndn.bv_val ));
106 #else /* !NEW_LOGGING */
107                 Debug( LDAP_DEBUG_ARGS,
108                         "rw> bindDn (op ndn in group): \"%s\" -> \"%s\"\n%s",
109                         op_ndn->bv_val, mop_ndn.bv_val, "" );
110 #endif /* !NEW_LOGGING */
111                 break;
112         
113         case REWRITE_REGEXEC_UNWILLING:
114         
115         case REWRITE_REGEXEC_ERR:
116                 goto cleanup;
117         }
118
119         /*
120          * Rewrite the gr ndn if needed
121          */
122         switch ( rewrite_session( li->rwinfo, "searchBase",
123                                 gr_ndn->bv_val, conn, &mgr_ndn.bv_val ) ) {
124         case REWRITE_REGEXEC_OK:
125                 if ( mgr_ndn.bv_val == NULL ) {
126                         mgr_ndn = *gr_ndn;
127                 }
128 #ifdef NEW_LOGGING
129                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
130                                 "[rw] searchBase (gr ndn in group):"
131                                 " \"%s\" -> \"%s\"\n%s", gr_ndn->bv_val, mgr_ndn.bv_val ));
132 #else /* !NEW_LOGGING */
133                 Debug( LDAP_DEBUG_ARGS,
134                         "rw> searchBase (gr ndn in group):"
135                         " \"%s\" -> \"%s\"\n%s",
136                         gr_ndn->bv_val, mgr_ndn.bv_val, "" );
137 #endif /* !NEW_LOGGING */
138                 break;
139         
140         case REWRITE_REGEXEC_UNWILLING:
141         
142         case REWRITE_REGEXEC_ERR:
143                 goto cleanup;
144         }
145 #else /* !ENABLE_REWRITE */
146         ldap_back_dn_massage( li, op_ndn, &mop_ndn, 1, 1 );
147         if ( mop_ndn.bv_val == NULL ) {
148                 goto cleanup;
149         }
150         ldap_back_dn_massage( li, gr_ndn, &mgr_ndn, 1, 1 );
151         if ( mgr_ndn.bv_val == NULL ) {
152                 goto cleanup;
153         }
154 #endif /* !ENABLE_REWRITE */
155
156         ldap_back_map(&li->oc_map, &group_oc_name, &group_oc_name, 0);
157         if (group_oc_name.bv_val == NULL)
158                 goto cleanup;
159         ldap_back_map(&li->at_map, &group_at_name, &group_at_name, 0);
160         if (group_at_name.bv_val == NULL)
161                 goto cleanup;
162
163         filter = ch_malloc(sizeof("(&(objectclass=)(=))")
164                                                 + group_oc_name.bv_len
165                                                 + group_at_name.bv_len
166                                                 + mop_ndn.bv_len + 1);
167         if (filter == NULL)
168                 goto cleanup;
169
170         if (ldap_initialize(&ld, li->url) != LDAP_SUCCESS) {
171                 goto cleanup;
172         }
173
174         if (ldap_bind_s(ld, li->binddn, li->bindpw, LDAP_AUTH_SIMPLE)
175                         != LDAP_SUCCESS) {
176                 goto cleanup;
177         }
178
179         ptr = slap_strcopy(filter, "(&(objectclass=");
180         ptr = slap_strcopy(ptr, group_oc_name.bv_val);
181         ptr = slap_strcopy(ptr, ")(");
182         ptr = slap_strcopy(ptr, group_at_name.bv_val);
183         ptr = slap_strcopy(ptr, "=");
184         ptr = slap_strcopy(ptr, mop_ndn.bv_val);
185         strcpy(ptr, "))");
186
187         gattr[0] = "objectclass";
188         gattr[1] = NULL;
189         if (ldap_search_ext_s(ld, mgr_ndn.bv_val, LDAP_SCOPE_BASE, filter,
190                 gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
191                 LDAP_NO_LIMIT, &result) == LDAP_SUCCESS) {
192                 if (ldap_first_entry(ld, result) != NULL)
193                         rc = 0;
194                 ldap_msgfree(result);
195         }
196
197 cleanup:;
198         if ( ld != NULL ) {
199                 ldap_unbind(ld);
200         }
201         ch_free(filter);
202         if ( mop_ndn.bv_val != op_ndn->bv_val ) {
203                 free( mop_ndn.bv_val );
204         }
205         if ( mgr_ndn.bv_val != gr_ndn->bv_val ) {
206                 free( mgr_ndn.bv_val );
207         }
208         return(rc);
209 }