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