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