]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/group.c
Plug memory leak
[openldap] / servers / slapd / back-bdb / group.c
1 /* group.c - bdb 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-bdb.h"
17 #include "proto-bdb.h"
18
19
20 /* return 0 IFF op_dn is a value in member attribute
21  * of entry with gr_dn AND that entry has an objectClass
22  * value of groupOfNames
23  */
24 int
25 bdb_group(
26         Backend *be,
27         Connection *conn,
28         Operation *op,
29         Entry   *target,
30         struct berval   *gr_ndn,
31         struct berval   *op_ndn,
32         ObjectClass *group_oc,
33         AttributeDescription *group_at )
34 {
35         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
36         struct bdb_op_info *boi = NULL;
37         DB_TXN *txn = NULL;
38         Entry *e;
39         int     rc = 1;
40         Attribute *attr;
41
42         const char *group_oc_name = NULL;
43         const char *group_at_name = group_at->ad_cname.bv_val;
44
45         u_int32_t       locker;
46         DB_LOCK         lock;
47
48         if( group_oc->soc_names && group_oc->soc_names[0] ) {
49                 group_oc_name = group_oc->soc_names[0];
50         } else {
51                 group_oc_name = group_oc->soc_oid;
52         }
53
54 #ifdef NEW_LOGGING
55         LDAP_LOG( BACK_BDB, ENTRY, 
56                 "bdb_group: check (%s) member of (%s), oc %s\n",
57                 op_ndn->bv_val, gr_ndn->bv_val, group_oc_name );
58 #else
59         Debug( LDAP_DEBUG_ARGS,
60                 "=> bdb_group: group ndn: \"%s\"\n",
61                 gr_ndn->bv_val, 0, 0 ); 
62
63         Debug( LDAP_DEBUG_ARGS,
64                 "=> bdb_group: op ndn: \"%s\"\n",
65                 op_ndn->bv_val, 0, 0 ); 
66
67         Debug( LDAP_DEBUG_ARGS,
68                 "=> bdb_group: oc: \"%s\" at: \"%s\"\n", 
69                 group_oc_name, group_at_name, 0 ); 
70
71         Debug( LDAP_DEBUG_ARGS,
72                 "=> bdb_group: tr ndn: \"%s\"\n",
73                 target->e_ndn, 0, 0 ); 
74 #endif
75
76         if( op ) boi = (struct bdb_op_info *) op->o_private;
77         if( boi != NULL && be == boi->boi_bdb ) {
78                 txn = boi->boi_txn;
79         }
80
81         if ( txn ) {
82                 locker = TXN_ID( txn );
83         } else {
84                 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
85                 switch(rc) {
86                 case 0:
87                         break;
88                 default:
89                         return 1;
90                 }
91         }
92
93         if (dn_match(&target->e_name, gr_ndn)) {
94                 /* we already have a LOCKED copy of the entry */
95                 e = target;
96 #ifdef NEW_LOGGING
97                 LDAP_LOG( BACK_BDB, DETAIL1, 
98                         "bdb_group: target is group (%s)\n", gr_ndn->bv_val, 0, 0 );
99 #else
100                 Debug( LDAP_DEBUG_ARGS,
101                         "=> bdb_group: target is group: \"%s\"\n",
102                         gr_ndn->bv_val, 0, 0 );
103 #endif
104         } else {
105 dn2entry_retry:
106                 /* can we find group entry */
107                 rc = bdb_dn2entry_r( be, NULL, gr_ndn, &e, NULL, 0, locker, &lock ); 
108                 if( rc ) {
109                         if ( rc == DB_LOCK_DEADLOCK || rc == DB_LOCK_NOTGRANTED )
110                                 goto dn2entry_retry;
111                         if( txn ) {
112                                 boi->boi_err = rc;
113                         }
114                         else {
115                                 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
116                         }
117                         return( 1 );
118                 }
119                 if (e == NULL) {
120 #ifdef NEW_LOGGING
121                         LDAP_LOG( BACK_BDB, DETAIL1, 
122                                 "bdb_group: cannot find group (%s)\n", gr_ndn->bv_val, 0, 0 );
123 #else
124                         Debug( LDAP_DEBUG_ACL,
125                                 "=> bdb_group: cannot find group: \"%s\"\n",
126                                         gr_ndn->bv_val, 0, 0 ); 
127 #endif
128                         if ( txn == NULL ) {
129                                 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
130                         }
131                         return( 1 );
132                 }
133 #ifdef NEW_LOGGING
134                 LDAP_LOG( BACK_BDB, DETAIL1, 
135                         "bdb_group: found group (%s)\n", gr_ndn->bv_val, 0, 0 );
136 #else
137                 Debug( LDAP_DEBUG_ACL,
138                         "=> bdb_group: found group: \"%s\"\n",
139                         gr_ndn->bv_val, 0, 0 ); 
140 #endif
141         }
142
143         /* find it's objectClass and member attribute values
144          * make sure this is a group entry
145          * finally test if we can find op_dn in the member attribute value list
146          */
147         rc = 1;
148
149 #ifdef BDB_ALIASES
150         if( is_entry_alias( e ) ) {
151 #ifdef NEW_LOGGING
152                 LDAP_LOG( BACK_BDB, INFO, 
153                         "bdb_group: group (%s) is an alias\n", gr_ndn->bv_val, 0, 0);
154 #else
155                 Debug( LDAP_DEBUG_ACL,
156                         "<= bdb_group: group is an alias\n", 0, 0, 0 );
157 #endif
158                 goto return_results;
159         }
160 #endif
161
162         if( is_entry_referral( e ) ) {
163 #ifdef NEW_LOGGING
164                 LDAP_LOG( BACK_BDB, INFO, 
165                         "bdb_group: group (%s) is a referral.\n", gr_ndn->bv_val, 0, 0 );
166 #else
167                 Debug( LDAP_DEBUG_ACL,
168                         "<= bdb_group: group is a referral\n", 0, 0, 0 );
169 #endif
170                 goto return_results;
171         }
172
173         if( !is_entry_objectclass( e, group_oc, 0 ) ) {
174 #ifdef NEW_LOGGING
175                 LDAP_LOG( BACK_BDB, ERR, 
176                         "bdb_group: failed to find %s in objectClass.\n", 
177                         group_oc_name, 0, 0 );
178 #else
179                 Debug( LDAP_DEBUG_ACL,
180                         "<= bdb_group: failed to find %s in objectClass\n", 
181                                 group_oc_name, 0, 0 ); 
182 #endif
183                 goto return_results;
184         }
185
186         if ((attr = attr_find(e->e_attrs, group_at)) == NULL) {
187 #ifdef NEW_LOGGING
188                 LDAP_LOG( BACK_BDB, INFO, 
189                         "bdb_group: failed to find %s\n", group_at_name, 0, 0 );
190 #else
191                 Debug( LDAP_DEBUG_ACL,
192                         "<= bdb_group: failed to find %s\n",
193                         group_at_name, 0, 0 ); 
194 #endif
195                 goto return_results;
196         }
197
198 #ifdef NEW_LOGGING
199         LDAP_LOG( BACK_BDB, ENTRY, 
200                 "bdb_group: found objectClass %s and %s\n",
201                 group_oc_name, group_at_name, 0 );
202 #else
203         Debug( LDAP_DEBUG_ACL,
204                 "<= bdb_group: found objectClass %s and %s\n",
205                 group_oc_name, group_at_name, 0 ); 
206 #endif
207
208         if( value_find_ex( group_at, SLAP_MR_VALUE_NORMALIZED_MATCH,
209                 attr->a_vals, op_ndn ) != LDAP_SUCCESS ) {
210 #ifdef NEW_LOGGING
211                 LDAP_LOG( BACK_BDB, DETAIL1, 
212                         "bdb_group: \"%s\" not in \"%s\": %s\n",
213                         op_ndn->bv_val, gr_ndn->bv_val, group_at_name );
214 #else
215                 Debug( LDAP_DEBUG_ACL,
216                         "<= bdb_group: \"%s\" not in \"%s\": %s\n", 
217                         op_ndn->bv_val, gr_ndn->bv_val, group_at_name ); 
218 #endif
219                 goto return_results;
220         }
221
222 #ifdef NEW_LOGGING
223         LDAP_LOG( BACK_BDB, DETAIL1, "bdb_group: %s is in %s: %s\n",
224                 op_ndn->bv_val, gr_ndn->bv_val, group_at_name );
225 #else
226         Debug( LDAP_DEBUG_ACL,
227                 "<= bdb_group: \"%s\" is in \"%s\": %s\n", 
228                 op_ndn->bv_val, gr_ndn->bv_val, group_at_name ); 
229 #endif
230
231         rc = 0;
232
233 return_results:
234         if( target != e ) {
235                 /* free entry */
236                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
237         }
238
239         if ( txn == NULL ) {
240                 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
241         }
242
243 #ifdef NEW_LOGGING
244         LDAP_LOG( BACK_BDB, ENTRY, "bdb_group: rc=%d\n", rc, 0, 0 );
245 #else
246         Debug( LDAP_DEBUG_TRACE, "bdb_group: rc=%d\n", rc, 0, 0 ); 
247 #endif
248
249         return(rc);
250 }
251