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