]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/group.c
improved filter mapping/rewrite; improved result rewriting; improved attribute/object...
[openldap] / servers / slapd / back-ldap / group.c
1 /* group.c - ldap backend acl group routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 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 #include "lutil.h"
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         struct ldapconn *lc;
37         int rc = 1, oc;
38         Attribute   *attr;
39
40         LDAPMessage     *result;
41         char *gattr[2];
42         char *filter = NULL, *ptr;
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, 0 ) ) {
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_ex( group_at, SLAP_MR_ASSERTED_VALUE_NORMALIZED_MATCH,
86                                         attr->a_vals, op_ndn ) != 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->bv_val, conn, &mop_ndn.bv_val ) ) {
99         case REWRITE_REGEXEC_OK:
100                 if ( mop_ndn.bv_val == NULL ) {
101                         mop_ndn = *op_ndn;
102                 }
103 #ifdef NEW_LOGGING
104                 LDAP_LOG( BACK_LDAP, DETAIL1, 
105                         "[rw] bindDn (op ndn in group): \"%s\" -> \"%s\"\n", 
106                         op_ndn->bv_val, mop_ndn.bv_val, 0 );
107 #else /* !NEW_LOGGING */
108                 Debug( LDAP_DEBUG_ARGS,
109                         "rw> bindDn (op ndn in group): \"%s\" -> \"%s\"\n%s",
110                         op_ndn->bv_val, mop_ndn.bv_val, "" );
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->bv_val, conn, &mgr_ndn.bv_val ) ) {
125         case REWRITE_REGEXEC_OK:
126                 if ( mgr_ndn.bv_val == NULL ) {
127                         mgr_ndn = *gr_ndn;
128                 }
129 #ifdef NEW_LOGGING
130                 LDAP_LOG( BACK_LDAP, DETAIL1, 
131                         "[rw] searchBase (gr ndn in group): \"%s\" -> \"%s\"\n%s", 
132                         gr_ndn->bv_val, mgr_ndn.bv_val, "" );
133 #else /* !NEW_LOGGING */
134                 Debug( LDAP_DEBUG_ARGS,
135                         "rw> searchBase (gr ndn in group):"
136                         " \"%s\" -> \"%s\"\n%s",
137                         gr_ndn->bv_val, mgr_ndn.bv_val, "" );
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         ldap_back_dn_massage( li, op_ndn, &mop_ndn, 1, 1 );
148         if ( mop_ndn.bv_val == NULL ) {
149                 goto cleanup;
150         }
151         ldap_back_dn_massage( li, gr_ndn, &mgr_ndn, 1, 1 );
152         if ( mgr_ndn.bv_val == NULL ) {
153                 goto cleanup;
154         }
155 #endif /* !ENABLE_REWRITE */
156
157         ldap_back_map(&li->oc_map, &group_oc_name, &group_oc_name,
158                         BACKLDAP_MAP);
159         if (group_oc_name.bv_val == NULL || group_oc_name.bv_val[0] == '\0')
160                 goto cleanup;
161         ldap_back_map(&li->at_map, &group_at_name, &group_at_name,
162                         BACKLDAP_MAP);
163         if (group_at_name.bv_val == NULL || group_at_name.bv_val[0] == '\0')
164                 goto cleanup;
165
166         filter = ch_malloc(sizeof("(&(objectclass=)(=))")
167                                                 + group_oc_name.bv_len
168                                                 + group_at_name.bv_len
169                                                 + mop_ndn.bv_len + 1);
170         if (filter == NULL)
171                 goto cleanup;
172
173         /* Tell getconn this is a privileged op */
174         oc = op->o_do_not_cache;
175         op->o_do_not_cache = 1;
176         lc = ldap_back_getconn(li, conn, op);
177         if ( !lc || !ldap_back_dobind( li, lc, NULL, op ) ) {
178                 op->o_do_not_cache = oc;
179                 goto cleanup;
180         }
181         op->o_do_not_cache = oc;
182
183         ptr = lutil_strcopy(filter, "(&(objectclass=");
184         ptr = lutil_strcopy(ptr, group_oc_name.bv_val);
185         ptr = lutil_strcopy(ptr, ")(");
186         ptr = lutil_strcopy(ptr, group_at_name.bv_val);
187         ptr = lutil_strcopy(ptr, "=");
188         ptr = lutil_strcopy(ptr, mop_ndn.bv_val);
189         strcpy(ptr, "))");
190
191         gattr[0] = "objectclass";
192         gattr[1] = NULL;
193         if (ldap_search_ext_s(lc->ld, mgr_ndn.bv_val, LDAP_SCOPE_BASE, filter,
194                 gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
195                 LDAP_NO_LIMIT, &result) == LDAP_SUCCESS) {
196                 if (ldap_first_entry(lc->ld, result) != NULL)
197                         rc = 0;
198                 ldap_msgfree(result);
199         }
200
201 cleanup:;
202         ch_free(filter);
203         if ( mop_ndn.bv_val != op_ndn->bv_val ) {
204                 free( mop_ndn.bv_val );
205         }
206         if ( mgr_ndn.bv_val != gr_ndn->bv_val ) {
207                 free( mgr_ndn.bv_val );
208         }
209         return(rc);
210 }