]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/attribute.c
improved support for NOOP; add is fine, the other write funcs still need work
[openldap] / servers / slapd / back-bdb / attribute.c
1 /* attribute.c - bdb backend acl attribute 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 /* 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;
43         DB_LOCK         lock;
44
45 #ifdef NEW_LOGGING
46         LDAP_LOG( BACK_BDB, ARGS, 
47                 "bdb_attribute: gr dn: \"%s\"\n", entry_ndn->bv_val, 0, 0 );
48         LDAP_LOG( BACK_BDB, ARGS, 
49                 "bdb_attribute: at: \"%s\"\n", entry_at_name, 0, 0);
50         LDAP_LOG( BACK_BDB, ARGS, "bdb_attribute: tr dn: \"%s\"\n",
51                 target ? target->e_ndn : "", 0, 0 );
52 #else
53         Debug( LDAP_DEBUG_ARGS,
54                 "=> bdb_attribute: gr dn: \"%s\"\n",
55                 entry_ndn->bv_val, 0, 0 ); 
56         Debug( LDAP_DEBUG_ARGS,
57                 "=> bdb_attribute: at: \"%s\"\n", 
58                 entry_at_name, 0, 0 ); 
59
60         Debug( LDAP_DEBUG_ARGS,
61                 "=> bdb_attribute: tr dn: \"%s\"\n",
62                 target ? target->e_ndn : "", 0, 0 ); 
63 #endif
64
65         if( op ) boi = (struct bdb_op_info *) op->o_private;
66         if( boi != NULL && be == boi->boi_bdb ) {
67                 txn = boi->boi_txn;
68         }
69
70         if ( txn != NULL ) {
71                 locker = TXN_ID ( txn );
72         } else {
73                 rc = LOCK_ID ( bdb->bi_dbenv, &locker );
74                 switch(rc) {
75                 case 0:
76                         break;
77                 default:
78                         return LDAP_OTHER;
79                 }
80         }
81
82         if (target != NULL && dn_match(&target->e_nname, entry_ndn)) {
83                 /* we already have a LOCKED copy of the entry */
84                 e = target;
85 #ifdef NEW_LOGGING
86                 LDAP_LOG( BACK_BDB, DETAIL1, 
87                         "bdb_attribute: target is LOCKED (%s)\n", entry_ndn->bv_val, 0, 0 );
88 #else
89                 Debug( LDAP_DEBUG_ARGS,
90                         "=> bdb_attribute: target is entry: \"%s\"\n",
91                         entry_ndn->bv_val, 0, 0 );
92 #endif
93
94
95         } else {
96 dn2entry_retry:
97                 /* can we find entry */
98                 rc = bdb_dn2entry_r( be, NULL, entry_ndn, &e, NULL, 0, locker, &lock );
99                 switch( rc ) {
100                 case DB_NOTFOUND:
101                 case 0:
102                         break;
103                 case DB_LOCK_DEADLOCK:
104                 case DB_LOCK_NOTGRANTED:
105                         goto dn2entry_retry;
106                 default:
107                         if( txn != NULL ) {
108                                 boi->boi_err = rc;
109                         }
110                         else {
111                                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
112                         }
113                         return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
114                 }
115                 if (e == NULL) {
116 #ifdef NEW_LOGGING
117                         LDAP_LOG( BACK_BDB, INFO, 
118                                 "bdb_attribute: cannot find entry (%s)\n", 
119                                 entry_ndn->bv_val, 0, 0 );
120 #else
121                         Debug( LDAP_DEBUG_ACL,
122                                 "=> bdb_attribute: cannot find entry: \"%s\"\n",
123                                         entry_ndn->bv_val, 0, 0 ); 
124 #endif
125                         if ( txn == NULL ) {
126                                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
127                         }
128                         return LDAP_NO_SUCH_OBJECT; 
129                 }
130                 
131 #ifdef NEW_LOGGING
132                 LDAP_LOG( BACK_BDB, DETAIL1, "bdb_attribute: found entry (%s)\n",
133                         entry_ndn->bv_val, 0, 0 );
134 #else
135                 Debug( LDAP_DEBUG_ACL,
136                         "=> bdb_attribute: found entry: \"%s\"\n",
137                         entry_ndn->bv_val, 0, 0 ); 
138 #endif
139         }
140
141 #ifdef BDB_ALIASES
142         /* find attribute values */
143         if( is_entry_alias( e ) ) {
144 #ifdef NEW_LOGGING
145                 LDAP_LOG( BACK_BDB, INFO, 
146                         "bdb_attribute: entry (%s) is an alias\n", e->e_dn, 0, 0 );
147 #else
148                 Debug( LDAP_DEBUG_ACL,
149                         "<= bdb_attribute: entry is an alias\n", 0, 0, 0 );
150 #endif
151                 rc = LDAP_ALIAS_PROBLEM;
152                 goto return_results;
153         }
154 #endif
155
156         if( is_entry_referral( e ) ) {
157 #ifdef NEW_LOGGING
158                 LDAP_LOG( BACK_BDB, INFO, 
159                         "bdb_attribute: entry (%s) is a referral.\n", e->e_dn, 0, 0);
160 #else
161                 Debug( LDAP_DEBUG_ACL,
162                         "<= bdb_attribute: entry is a referral\n", 0, 0, 0 );
163 #endif
164                 rc = LDAP_REFERRAL;
165                 goto return_results;
166         }
167
168         if (conn != NULL && op != NULL
169                 && access_allowed( be, conn, op, e, slap_schema.si_ad_entry,
170                         NULL, ACL_READ, &acl_state ) == 0 )
171         {
172                 rc = LDAP_INSUFFICIENT_ACCESS;
173                 goto return_results;
174         }
175
176         if ((attr = attr_find(e->e_attrs, entry_at)) == NULL) {
177 #ifdef NEW_LOGGING
178                 LDAP_LOG( BACK_BDB, INFO, 
179                         "bdb_attribute: failed to find %s.\n", entry_at_name, 0, 0 );
180 #else
181                 Debug( LDAP_DEBUG_ACL,
182                         "<= bdb_attribute: failed to find %s\n",
183                         entry_at_name, 0, 0 ); 
184 #endif
185                 rc = LDAP_NO_SUCH_ATTRIBUTE;
186                 goto return_results;
187         }
188
189         if (conn != NULL && op != NULL
190                 && access_allowed( be, conn, op, e, entry_at, NULL, ACL_READ, 
191                            &acl_state ) == 0 )
192         {
193                 rc = LDAP_INSUFFICIENT_ACCESS;
194                 goto return_results;
195         }
196
197         for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
198                 /* count them */
199         }
200
201         v = (BerVarray) ch_malloc( sizeof(struct berval) * (i+1) );
202
203         for ( i=0, j=0; attr->a_vals[i].bv_val != NULL; i++ ) {
204                 if( conn != NULL
205                         && op != NULL
206                         && access_allowed(be, conn, op, e, entry_at,
207                                 &attr->a_vals[i], ACL_READ, &acl_state ) == 0)
208                 {
209                         continue;
210                 }
211                 ber_dupbv( &v[j], &attr->a_vals[i] );
212
213                 if( v[j].bv_val != NULL ) j++;
214         }
215
216         if( j == 0 ) {
217                 ch_free( v );
218                 *vals = NULL;
219                 rc = LDAP_INSUFFICIENT_ACCESS;
220         } else {
221                 v[j].bv_val = NULL;
222                 v[j].bv_len = 0;
223                 *vals = v;
224                 rc = LDAP_SUCCESS;
225         }
226
227 return_results:
228         if( target != e ) {
229                 /* free entry */
230                 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
231         }
232
233         if ( txn == NULL ) {
234                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
235         }
236
237 #ifdef NEW_LOGGING
238         LDAP_LOG( BACK_BDB, ENTRY, "bdb_attribute: rc=%d nvals=%d.\n", rc, j, 0 );
239 #else
240         Debug( LDAP_DEBUG_TRACE,
241                 "bdb_attribute: rc=%d nvals=%d\n",
242                 rc, j, 0 ); 
243 #endif
244         return(rc);
245 }