]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/attribute.c
#unifdef paged results, remove lint
[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         } else {
95 dn2entry_retry:
96                 /* can we find entry */
97                 rc = bdb_dn2entry_r( be, NULL, entry_ndn, &e, NULL, 0, locker, &lock );
98                 switch( rc ) {
99                 case DB_NOTFOUND:
100                 case 0:
101                         break;
102                 case DB_LOCK_DEADLOCK:
103                 case DB_LOCK_NOTGRANTED:
104                         goto dn2entry_retry;
105                 default:
106                         if( txn != NULL ) {
107                                 boi->boi_err = rc;
108                         }
109                         else {
110                                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
111                         }
112                         return (rc != LDAP_BUSY) ? LDAP_OTHER : LDAP_BUSY;
113                 }
114                 if (e == NULL) {
115 #ifdef NEW_LOGGING
116                         LDAP_LOG( BACK_BDB, INFO, 
117                                 "bdb_attribute: cannot find entry (%s)\n", 
118                                 entry_ndn->bv_val, 0, 0 );
119 #else
120                         Debug( LDAP_DEBUG_ACL,
121                                 "=> bdb_attribute: cannot find entry: \"%s\"\n",
122                                         entry_ndn->bv_val, 0, 0 ); 
123 #endif
124                         if ( txn == NULL ) {
125                                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
126                         }
127                         return LDAP_NO_SUCH_OBJECT; 
128                 }
129                 
130 #ifdef NEW_LOGGING
131                 LDAP_LOG( BACK_BDB, DETAIL1, "bdb_attribute: found entry (%s)\n",
132                         entry_ndn->bv_val, 0, 0 );
133 #else
134                 Debug( LDAP_DEBUG_ACL,
135                         "=> bdb_attribute: found entry: \"%s\"\n",
136                         entry_ndn->bv_val, 0, 0 ); 
137 #endif
138         }
139
140 #ifdef BDB_ALIASES
141         /* find attribute values */
142         if( is_entry_alias( e ) ) {
143 #ifdef NEW_LOGGING
144                 LDAP_LOG( BACK_BDB, INFO, 
145                         "bdb_attribute: entry (%s) is an alias\n", e->e_dn, 0, 0 );
146 #else
147                 Debug( LDAP_DEBUG_ACL,
148                         "<= bdb_attribute: entry is an alias\n", 0, 0, 0 );
149 #endif
150                 rc = LDAP_ALIAS_PROBLEM;
151                 goto return_results;
152         }
153 #endif
154
155         if( is_entry_referral( e ) ) {
156 #ifdef NEW_LOGGING
157                 LDAP_LOG( BACK_BDB, INFO, 
158                         "bdb_attribute: entry (%s) is a referral.\n", e->e_dn, 0, 0);
159 #else
160                 Debug( LDAP_DEBUG_ACL,
161                         "<= bdb_attribute: entry is a referral\n", 0, 0, 0 );
162 #endif
163                 rc = LDAP_REFERRAL;
164                 goto return_results;
165         }
166
167         if ((attr = attr_find(e->e_attrs, entry_at)) == NULL) {
168 #ifdef NEW_LOGGING
169                 LDAP_LOG( BACK_BDB, INFO, 
170                         "bdb_attribute: failed to find %s.\n", entry_at_name, 0, 0 );
171 #else
172                 Debug( LDAP_DEBUG_ACL,
173                         "<= bdb_attribute: failed to find %s\n",
174                         entry_at_name, 0, 0 ); 
175 #endif
176                 rc = LDAP_NO_SUCH_ATTRIBUTE;
177                 goto return_results;
178         }
179
180         if (conn != NULL && op != NULL
181                 && access_allowed( be, conn, op, e, entry_at, NULL,
182                         ACL_AUTH, &acl_state ) == 0 )
183         {
184                 rc = LDAP_INSUFFICIENT_ACCESS;
185                 goto return_results;
186         }
187
188         for ( i = 0; attr->a_vals[i].bv_val != NULL; i++ ) {
189                 /* count them */
190         }
191
192         v = (BerVarray) ch_malloc( sizeof(struct berval) * (i+1) );
193
194         for ( i=0, j=0; attr->a_vals[i].bv_val != NULL; i++ ) {
195                 if( conn != NULL
196                         && op != NULL
197                         && access_allowed(be, conn, op, e, entry_at,
198                                 &attr->a_vals[i], ACL_AUTH, &acl_state ) == 0)
199                 {
200                         continue;
201                 }
202                 ber_dupbv( &v[j], &attr->a_vals[i] );
203
204                 if( v[j].bv_val != NULL ) j++;
205         }
206
207         if( j == 0 ) {
208                 ch_free( v );
209                 *vals = NULL;
210                 rc = LDAP_INSUFFICIENT_ACCESS;
211         } else {
212                 v[j].bv_val = NULL;
213                 v[j].bv_len = 0;
214                 *vals = v;
215                 rc = LDAP_SUCCESS;
216         }
217
218 return_results:
219         if( target != e ) {
220                 /* free entry */
221                 bdb_cache_return_entry_r(bdb->bi_dbenv, &bdb->bi_cache, e, &lock);
222         }
223
224         if ( txn == NULL ) {
225                 LOCK_ID_FREE( bdb->bi_dbenv, locker );
226         }
227
228 #ifdef NEW_LOGGING
229         LDAP_LOG( BACK_BDB, ENTRY, "bdb_attribute: rc=%d nvals=%d.\n", rc, j, 0 );
230 #else
231         Debug( LDAP_DEBUG_TRACE,
232                 "bdb_attribute: rc=%d nvals=%d\n",
233                 rc, j, 0 ); 
234 #endif
235         return(rc);
236 }