]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/attribute.c
NVALUES: fix a couple of value_find_ex() calls
[openldap] / servers / slapd / back-bdb / attribute.c
1 /* attribute.c - bdb backend acl attribute 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-bdb.h"
17 #include "proto-bdb.h"
18
19 /* return LDAP_SUCCESS IFF we can retrieve the attributes
20  * of entry with e_ndn
21  */
22 int
23 bdb_attribute(
24         Backend *be,
25         Connection *conn,
26         Operation *op,
27         Entry *target,
28         struct berval *entry_ndn,
29         AttributeDescription *entry_at,
30         BerVarray *vals )
31 {
32         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
33         struct bdb_op_info *boi = NULL;
34         DB_TXN *txn = NULL;
35         Entry *e;
36         int     i, j = 0, rc;
37         Attribute *attr;
38         BerVarray v;
39         const char *entry_at_name = entry_at->ad_cname.bv_val;
40         AccessControlState acl_state = ACL_STATE_INIT;
41
42         u_int32_t       locker = 0;
43         DB_LOCK         lock;
44         int             free_lock_id = 0;
45
46 #ifdef NEW_LOGGING
47         LDAP_LOG( BACK_BDB, ARGS, 
48                 "bdb_attribute: gr ndn: \"%s\"\n", entry_ndn->bv_val, 0, 0 );
49         LDAP_LOG( BACK_BDB, ARGS, 
50                 "bdb_attribute: at: \"%s\"\n", entry_at_name, 0, 0);
51         LDAP_LOG( BACK_BDB, ARGS, "bdb_attribute: tr ndn: \"%s\"\n",
52                 target ? target->e_ndn : "", 0, 0 );
53 #else
54         Debug( LDAP_DEBUG_ARGS,
55                 "=> bdb_attribute: gr ndn: \"%s\"\n",
56                 entry_ndn->bv_val, 0, 0 ); 
57         Debug( LDAP_DEBUG_ARGS,
58                 "=> bdb_attribute: at: \"%s\"\n", 
59                 entry_at_name, 0, 0 ); 
60
61         Debug( LDAP_DEBUG_ARGS,
62                 "=> bdb_attribute: tr ndn: \"%s\"\n",
63                 target ? target->e_ndn : "", 0, 0 ); 
64 #endif
65
66         if( op ) boi = (struct bdb_op_info *) op->o_private;
67         if( boi != NULL && be == boi->boi_bdb ) {
68                 txn = boi->boi_txn;
69                 locker = boi->boi_locker;
70         }
71
72         if ( txn != NULL ) {
73                 locker = TXN_ID ( txn );
74         } else if ( !locker ) {
75                 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
76                 free_lock_id = 1;
77                 switch(rc) {
78                 case 0:
79                         break;
80                 default:
81                         return LDAP_OTHER;
82                 }
83         }
84
85         if (target != NULL && dn_match(&target->e_nname, entry_ndn)) {
86                 /* we already have a LOCKED copy of the entry */
87                 e = target;
88 #ifdef NEW_LOGGING
89                 LDAP_LOG( BACK_BDB, DETAIL1, 
90                         "bdb_attribute: target is LOCKED (%s)\n", entry_ndn->bv_val, 0, 0 );
91 #else
92                 Debug( LDAP_DEBUG_ARGS,
93                         "=> bdb_attribute: target is entry: \"%s\"\n",
94                         entry_ndn->bv_val, 0, 0 );
95 #endif
96
97         } else {
98 dn2entry_retry:
99                 /* can we find entry */
100                 rc = bdb_dn2entry_r( be, txn, entry_ndn, &e, NULL, 0, locker, &lock );
101                 switch( rc ) {
102                 case DB_NOTFOUND:
103                 case 0:
104                         break;
105                 case DB_LOCK_DEADLOCK:
106                 case DB_LOCK_NOTGRANTED:
107                         /* the txn must abort and retry */
108                         if ( txn ) {
109                                 boi->boi_err = rc;
110                                 return LDAP_BUSY;
111                         }
112                         ldap_pvt_thread_yield();
113                         goto dn2entry_retry;
114                 default:
115                         boi->boi_err = rc;
116                         if ( free_lock_id ) {
117                                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
118                         }
119                         return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
120                 }
121                 if (e == NULL) {
122 #ifdef NEW_LOGGING
123                         LDAP_LOG( BACK_BDB, INFO, 
124                                 "bdb_attribute: cannot find entry (%s)\n", 
125                                 entry_ndn->bv_val, 0, 0 );
126 #else
127                         Debug( LDAP_DEBUG_ACL,
128                                 "=> bdb_attribute: cannot find entry: \"%s\"\n",
129                                         entry_ndn->bv_val, 0, 0 ); 
130 #endif
131                         if ( free_lock_id ) {
132                                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
133                         }
134                         return LDAP_NO_SUCH_OBJECT; 
135                 }
136                 
137 #ifdef NEW_LOGGING
138                 LDAP_LOG( BACK_BDB, DETAIL1, "bdb_attribute: found entry (%s)\n",
139                         entry_ndn->bv_val, 0, 0 );
140 #else
141                 Debug( LDAP_DEBUG_ACL,
142                         "=> bdb_attribute: found entry: \"%s\"\n",
143                         entry_ndn->bv_val, 0, 0 ); 
144 #endif
145         }
146
147 #ifdef BDB_ALIASES
148         /* find attribute values */
149         if( is_entry_alias( e ) ) {
150 #ifdef NEW_LOGGING
151                 LDAP_LOG( BACK_BDB, INFO, 
152                         "bdb_attribute: entry (%s) is an alias\n", e->e_dn, 0, 0 );
153 #else
154                 Debug( LDAP_DEBUG_ACL,
155                         "<= bdb_attribute: entry is an alias\n", 0, 0, 0 );
156 #endif
157                 rc = LDAP_ALIAS_PROBLEM;
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_attribute: entry (%s) is a referral.\n", e->e_dn, 0, 0);
166 #else
167                 Debug( LDAP_DEBUG_ACL,
168                         "<= bdb_attribute: entry is a referral\n", 0, 0, 0 );
169 #endif
170                 rc = LDAP_REFERRAL;
171                 goto return_results;
172         }
173
174         if ((attr = attr_find(e->e_attrs, entry_at)) == NULL) {
175 #ifdef NEW_LOGGING
176                 LDAP_LOG( BACK_BDB, INFO, 
177                         "bdb_attribute: failed to find %s.\n", entry_at_name, 0, 0 );
178 #else
179                 Debug( LDAP_DEBUG_ACL,
180                         "<= bdb_attribute: failed to find %s\n",
181                         entry_at_name, 0, 0 ); 
182 #endif
183                 rc = LDAP_NO_SUCH_ATTRIBUTE;
184                 goto return_results;
185         }
186
187         if (conn != NULL && op != NULL
188                 && access_allowed( be, conn, op, e, entry_at, NULL,
189                         ACL_AUTH, &acl_state ) == 0 )
190         {
191                 rc = LDAP_INSUFFICIENT_ACCESS;
192                 goto return_results;
193         }
194
195         for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
196                 /* count them */
197         }
198
199         v = (BerVarray) ch_malloc( sizeof(struct berval) * (i+1) );
200
201         for ( i=0, j=0; attr->a_vals[i].bv_val != NULL; i++ ) {
202                 if( conn != NULL
203                         && op != NULL
204                         && access_allowed(be, conn, op, e, entry_at,
205                                 &attr->a_vals[i], ACL_AUTH, &acl_state ) == 0)
206                 {
207                         continue;
208                 }
209                 ber_dupbv( &v[j], &attr->a_vals[i] );
210
211                 if( v[j].bv_val != NULL ) j++;
212         }
213
214         if( j == 0 ) {
215                 ch_free( v );
216                 *vals = NULL;
217                 rc = LDAP_INSUFFICIENT_ACCESS;
218         } else {
219                 v[j].bv_val = NULL;
220                 v[j].bv_len = 0;
221                 *vals = v;
222                 rc = LDAP_SUCCESS;
223         }
224
225 return_results:
226         if( target != e ) {
227                 /* free entry */
228                 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
229         }
230
231         if ( free_lock_id ) {
232                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
233         }
234
235 #ifdef NEW_LOGGING
236         LDAP_LOG( BACK_BDB, ENTRY, "bdb_attribute: rc=%d nvals=%d.\n", rc, j, 0 );
237 #else
238         Debug( LDAP_DEBUG_TRACE,
239                 "bdb_attribute: rc=%d nvals=%d\n",
240                 rc, j, 0 ); 
241 #endif
242         return(rc);
243 }