]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/group.c
Import LOCK_ID rc fix from HEAD
[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 {
36         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
37         struct bdb_op_info *boi = NULL;
38         DB_TXN *txn = NULL;
39         Entry *e;
40         int     rc = 1;
41         Attribute *attr;
42
43         const char *group_oc_name = NULL;
44         const char *group_at_name = group_at->ad_cname.bv_val;
45
46         u_int32_t       locker;
47         DB_LOCK         lock;
48
49         if( group_oc->soc_names && group_oc->soc_names[0] ) {
50                 group_oc_name = group_oc->soc_names[0];
51         } else {
52                 group_oc_name = group_oc->soc_oid;
53         }
54
55 #ifdef NEW_LOGGING
56         LDAP_LOG( BACK_BDB, ENTRY, 
57                 "bdb_group: check (%s) member of (%s), oc %s\n",
58                 op_ndn->bv_val, gr_ndn->bv_val, group_oc_name );
59 #else
60         Debug( LDAP_DEBUG_ARGS,
61                 "=> bdb_group: gr dn: \"%s\"\n",
62                 gr_ndn->bv_val, 0, 0 ); 
63
64         Debug( LDAP_DEBUG_ARGS,
65                 "=> bdb_group: op dn: \"%s\"\n",
66                 op_ndn->bv_val, 0, 0 ); 
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 dn: \"%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( group_at, attr->a_vals, op_ndn ) != LDAP_SUCCESS ) {
209 #ifdef NEW_LOGGING
210                 LDAP_LOG( BACK_BDB, DETAIL1, 
211                         "bdb_group: \"%s\" not in \"%s\": %s\n",
212                         op_ndn->bv_val, gr_ndn->bv_val, group_at_name );
213 #else
214                 Debug( LDAP_DEBUG_ACL,
215                         "<= bdb_group: \"%s\" not in \"%s\": %s\n", 
216                         op_ndn->bv_val, gr_ndn->bv_val, group_at_name ); 
217 #endif
218                 goto return_results;
219         }
220
221 #ifdef NEW_LOGGING
222         LDAP_LOG( BACK_BDB, DETAIL1, "bdb_group: %s is in %s: %s\n",
223                 op_ndn->bv_val, gr_ndn->bv_val, group_at_name );
224 #else
225         Debug( LDAP_DEBUG_ACL,
226                 "<= bdb_group: \"%s\" is in \"%s\": %s\n", 
227                 op_ndn->bv_val, gr_ndn->bv_val, group_at_name ); 
228 #endif
229
230         rc = 0;
231
232 return_results:
233         if( target != e ) {
234                 /* free entry */
235                 bdb_cache_return_entry_r( bdb->bi_dbenv, &bdb->bi_cache, e, &lock );
236         }
237
238         if ( txn == NULL ) {
239                 LOCK_ID_FREE ( bdb->bi_dbenv, locker );
240         }
241
242 #ifdef NEW_LOGGING
243         LDAP_LOG( BACK_BDB, ENTRY, "bdb_group: rc=%d\n", rc, 0, 0 );
244 #else
245         Debug( LDAP_DEBUG_TRACE, "bdb_group: rc=%d\n", rc, 0, 0 ); 
246 #endif
247
248         return(rc);
249 }
250