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