]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/group.c
Remove global_backendsyncfreq code (code has been pushed down into back-ldbm).
[openldap] / servers / slapd / back-ldap / group.c
1 /* group.c - ldap backend acl group routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 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         const char      *gr_ndn,
30         const char      *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         struct berval bv;
39
40         LDAPMessage     *result;
41         char *gattr[2];
42         char *filter;
43         LDAP *ld;
44         char *mop_ndn, *mgr_ndn;
45
46         AttributeDescription *ad_objectClass = slap_schema.si_ad_objectClass;
47         char *group_oc_name = NULL;
48         char *group_at_name = group_at->ad_cname->bv_val;
49
50         if( group_oc->soc_names && group_oc->soc_names[0] ) {
51                 group_oc_name = group_oc->soc_names[0];
52         } else {
53                 group_oc_name = group_oc->soc_oid;
54         }
55
56         if (target != NULL && strcmp(target->e_ndn, gr_ndn) == 0) {
57                 /* we already have a copy of the entry */
58                 /* attribute and objectclass mapping has already been done */
59
60                 /*
61                  * first we need to check if the objectClass attribute
62                  * has been retieved; otherwise we need to repeat the search
63                  */
64                 attr = attr_find( target->e_attrs, ad_objectClass );
65                 if ( attr != NULL ) {
66
67                         /*
68                          * Now we can check for the group objectClass value
69                          */
70                         if( !is_entry_objectclass( target, group_oc ) ) {
71                                 return(1);
72                         }
73
74                         /*
75                          * This part has been reworked: the group attr compare
76                          * fails only if the attribute is PRESENT but the value
77                          * is NOT PRESENT; if the attribute is NOT PRESENT, the
78                          * search must be repeated as well.
79                          * This may happen if a search for an entry has already
80                          * been performed (target is not null) but the group
81                          * attribute has not been required
82                          */
83                         if ((attr = attr_find(target->e_attrs, group_at)) != NULL) {
84                                 bv.bv_val = (char *) op_ndn;
85                                 bv.bv_len = strlen( op_ndn );         
86                                 if( value_find( group_at, attr->a_vals, &bv ) != LDAP_SUCCESS  )
87                                         return(1);
88                                 return(0);
89                         } /* else: repeat the search */
90                 } /* else: repeat the search */
91         } /* else: do the search */
92
93         /*
94          * Rewrite the op ndn if needed
95          */
96 #ifdef ENABLE_REWRITE
97         switch ( rewrite_session( li->rwinfo, "bindDn",
98                                 op_ndn, conn, &mop_ndn ) ) {
99         case REWRITE_REGEXEC_OK:
100                 if ( mop_ndn == NULL ) {
101                         mop_ndn = ( char * )op_ndn;
102                 }
103 #ifdef NEW_LOGGING
104                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
105                                 "[rw] bindDn (op ndn in group):"
106                                 " \"%s\" -> \"%s\"\n", op_ndn, mop_ndn ));
107 #else /* !NEW_LOGGING */
108                 Debug( LDAP_DEBUG_ARGS,
109                         "rw> bindDn (op ndn in group): \"%s\" -> \"%s\"\n%s",
110                         op_ndn, mop_ndn, "" );
111 #endif /* !NEW_LOGGING */
112                 break;
113         
114         case REWRITE_REGEXEC_UNWILLING:
115         
116         case REWRITE_REGEXEC_ERR:
117                 goto cleanup;
118         }
119
120         /*
121          * Rewrite the gr ndn if needed
122          */
123         switch ( rewrite_session( li->rwinfo, "searchBase",
124                                 gr_ndn, conn, &mgr_ndn ) ) {
125         case REWRITE_REGEXEC_OK:
126                 if ( mgr_ndn == NULL ) {
127                         mgr_ndn = ( char * )gr_ndn;
128                 }
129 #ifdef NEW_LOGGING
130                 LDAP_LOG(( "backend", LDAP_LEVEL_DETAIL1,
131                                 "[rw] searchBase (gr ndn in group):"
132                                 " \"%s\" -> \"%s\"\n%s", gr_ndn, mgr_ndn ));
133 #else /* !NEW_LOGGING */
134                 Debug( LDAP_DEBUG_ARGS,
135                         "rw> searchBase (gr ndn in group):"
136                         " \"%s\" -> \"%s\"\n%s",
137                         gr_ndn, mgr_ndn, "" );
138 #endif /* !NEW_LOGGING */
139                 break;
140         
141         case REWRITE_REGEXEC_UNWILLING:
142         
143         case REWRITE_REGEXEC_ERR:
144                 goto cleanup;
145         }
146 #else /* !ENABLE_REWRITE */
147         mop_ndn = ldap_back_dn_massage( li, ch_strdup( op_ndn ), 1 );
148         if ( mop_ndn == NULL ) {
149                 goto cleanup;
150         }
151         mgr_ndn = ldap_back_dn_massage( li, ch_strdup( gr_ndn ), 1 );
152         if ( mgr_ndn == NULL ) {
153                 goto cleanup;
154         }
155 #endif /* !ENABLE_REWRITE */
156
157         group_oc_name = ldap_back_map(&li->oc_map, group_oc_name, 0);
158         if (group_oc_name == NULL)
159                 goto cleanup;
160         group_at_name = ldap_back_map(&li->at_map, group_at_name, 0);
161         if (group_at_name == NULL)
162                 goto cleanup;
163
164         filter = ch_malloc(sizeof("(&(objectclass=)(=))")
165                                                 + strlen(group_oc_name)
166                                                 + strlen(group_at_name)
167                                                 + strlen(mop_ndn) + 1);
168         if (filter == NULL)
169                 goto cleanup;
170
171         if (ldap_initialize(&ld, li->url) != LDAP_SUCCESS) {
172                 goto cleanup;
173         }
174
175         if (ldap_bind_s(ld, li->binddn, li->bindpw, LDAP_AUTH_SIMPLE)
176                         != LDAP_SUCCESS) {
177                 goto cleanup;
178         }
179
180         strcpy(filter, "(&(objectclass=");
181         strcat(filter, group_oc_name);
182         strcat(filter, ")(");
183         strcat(filter, group_at_name);
184         strcat(filter, "=");
185         strcat(filter, mop_ndn);
186         strcat(filter, "))");
187
188         gattr[0] = "objectclass";
189         gattr[1] = NULL;
190         if (ldap_search_ext_s(ld, mgr_ndn, LDAP_SCOPE_BASE, filter,
191                 gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
192                 LDAP_NO_LIMIT, &result) == LDAP_SUCCESS) {
193                 if (ldap_first_entry(ld, result) != NULL)
194                         rc = 0;
195                 ldap_msgfree(result);
196         }
197
198 cleanup:;
199         if ( ld != NULL ) {
200                 ldap_unbind(ld);
201         }
202         ch_free(filter);
203 #ifdef ENABLE_REWRITE
204         if ( mop_ndn != op_ndn ) {
205 #endif /* ENABLE_REWRITE */
206                 free( mop_ndn );
207 #ifdef ENABLE_REWRITE
208         }
209         if ( mgr_ndn != gr_ndn ) {
210 #endif /* ENABLE_REWRITE */
211                 free( mgr_ndn );
212 #ifdef ENABLE_REWRITE
213         }
214 #endif /* ENABLE_REWRITE */
215         return(rc);
216 }
217