]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/group.c
800b873e8d8c85f6f4661139a795ebd22a88e527
[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                 Debug( LDAP_DEBUG_ARGS,
104                         "rw> bindDn (op ndn in group): \"%s\" -> \"%s\"\n%s",
105                         op_ndn, mop_ndn, "" );
106                 break;
107         
108         case REWRITE_REGEXEC_UNWILLING:
109         
110         case REWRITE_REGEXEC_ERR:
111                 goto cleanup;
112         }
113
114         /*
115          * Rewrite the gr ndn if needed
116          */
117         switch ( rewrite_session( li->rwinfo, "searchBase",
118                                 gr_ndn, conn, &mgr_ndn ) ) {
119         case REWRITE_REGEXEC_OK:
120                 if ( mgr_ndn == NULL ) {
121                         mgr_ndn = ( char * )gr_ndn;
122                 }
123                 Debug( LDAP_DEBUG_ARGS,
124                         "rw> searchBase (gr ndn in group):"
125                         " \"%s\" -> \"%s\"\n%s",
126                         gr_ndn, mgr_ndn, "" );
127                 break;
128         
129         case REWRITE_REGEXEC_UNWILLING:
130         
131         case REWRITE_REGEXEC_ERR:
132                 goto cleanup;
133         }
134 #else /* !ENABLE_REWRITE */
135         mop_ndn = ldap_back_dn_massage( li, ch_strdup( op_ndn ), 1 );
136         if ( mop_ndn == NULL ) {
137                 goto cleanup;
138         }
139         mgr_ndn = ldap_back_dn_massage( li, ch_strdup( gr_ndn ), 1 );
140         if ( mgr_ndn == NULL ) {
141                 goto cleanup;
142         }
143 #endif /* !ENABLE_REWRITE */
144
145         group_oc_name = ldap_back_map(&li->oc_map, group_oc_name, 0);
146         if (group_oc_name == NULL)
147                 goto cleanup;
148         group_at_name = ldap_back_map(&li->at_map, group_at_name, 0);
149         if (group_at_name == NULL)
150                 goto cleanup;
151
152         filter = ch_malloc(sizeof("(&(objectclass=)(=))")
153                                                 + strlen(group_oc_name)
154                                                 + strlen(group_at_name)
155                                                 + strlen(mop_ndn) + 1);
156         if (filter == NULL)
157                 goto cleanup;
158
159         if (ldap_initialize(&ld, li->url) != LDAP_SUCCESS) {
160                 goto cleanup;
161         }
162
163         if (ldap_bind_s(ld, li->binddn, li->bindpw, LDAP_AUTH_SIMPLE)
164                         != LDAP_SUCCESS) {
165                 goto cleanup;
166         }
167
168         strcpy(filter, "(&(objectclass=");
169         strcat(filter, group_oc_name);
170         strcat(filter, ")(");
171         strcat(filter, group_at_name);
172         strcat(filter, "=");
173         strcat(filter, mop_ndn);
174         strcat(filter, "))");
175
176         gattr[0] = "objectclass";
177         gattr[1] = NULL;
178         if (ldap_search_ext_s(ld, mgr_ndn, LDAP_SCOPE_BASE, filter,
179                 gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
180                 LDAP_NO_LIMIT, &result) == LDAP_SUCCESS) {
181                 if (ldap_first_entry(ld, result) != NULL)
182                         rc = 0;
183                 ldap_msgfree(result);
184         }
185
186 cleanup:
187         if ( ld != NULL ) {
188                 ldap_unbind(ld);
189         }
190         ch_free(filter);
191 #ifdef ENABLE_REWRITE
192         if ( mop_ndn != op_ndn ) {
193 #endif /* ENABLE_REWRITE */
194                 free( mop_ndn );
195 #ifdef ENABLE_REWRITE
196         }
197         if ( mgr_ndn != gr_ndn ) {
198 #endif /* ENABLE_REWRITE */
199                 free( mgr_ndn );
200 #ifdef ENABLE_REWRITE
201         }
202 #endif /* ENABLE_REWRITE */
203         return(rc);
204 }
205