]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/attribute.c
Commit of the Proxy Cache contribution (ITS#2062)
[openldap] / servers / slapd / back-ldap / attribute.c
1 /* group.c - ldap backend acl group routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 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-ldap.h"
17
18
19 /* return 0 IFF we can retrieve the attributes
20  * of entry with ndn
21  */
22 int
23 ldap_back_attribute(
24         Backend *be,
25         Connection *conn,
26         Operation *op,
27         Entry   *target,
28         struct berval   *ndn,
29         AttributeDescription *entry_at,
30         BerVarray *vals
31 )
32 {
33         struct ldapinfo *li = (struct ldapinfo *) be->be_private;    
34         struct ldapconn *lc;
35         int rc = 1, i, j, count, is_oc;
36         Attribute *attr = NULL;
37         BerVarray abv, v;
38         struct berval mapped = { 0, NULL };
39         char **vs = NULL;
40         LDAPMessage     *result = NULL, *e = NULL;
41         char *gattr[2];
42
43         *vals = NULL;
44         if (target != NULL && dn_match( &target->e_nname, ndn )) {
45                 /* we already have a copy of the entry */
46                 /* attribute and objectclass mapping has already been done */
47                 if ((attr = attr_find(target->e_attrs, entry_at)) == NULL)
48                         return(1);
49
50                 for ( count = 0; attr->a_vals[count].bv_val != NULL; count++ ) { }
51                 v = (BerVarray) ch_calloc( (count + 1), sizeof(struct berval) );
52                 if (v != NULL) {
53                         for ( j = 0, abv = attr->a_vals; --count >= 0; abv++ ) {
54                                 if ( abv->bv_len > 0 ) {
55                                         ber_dupbv( &v[j], abv );
56                                         if( v[j].bv_val == NULL )
57                                                 break;
58                                 }
59                         }
60                         v[j].bv_val = NULL;
61                         *vals = v;
62                         return 0;
63                 }
64
65         }
66         ldap_back_map(&li->at_map, &entry_at->ad_cname, &mapped, BACKLDAP_MAP);
67         if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
68                 return 1;
69         }
70
71         /* Tell getconn this is a privileged op */
72         is_oc = op->o_do_not_cache;
73         op->o_do_not_cache = 1;
74         lc = ldap_back_getconn(li, conn, op);
75         if ( !lc || !ldap_back_dobind(li, lc, NULL, op) ) {
76                 op->o_do_not_cache = is_oc;
77                 return 1;
78         }
79         op->o_do_not_cache = is_oc;
80
81         gattr[0] = mapped.bv_val;
82         gattr[1] = NULL;
83         if (ldap_search_ext_s(lc->ld, ndn->bv_val, LDAP_SCOPE_BASE, "(objectclass=*)",
84                                 gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
85                                 LDAP_NO_LIMIT, &result) != LDAP_SUCCESS)
86         {
87                 goto cleanup;
88         }
89
90         if ((e = ldap_first_entry(lc->ld, result)) == NULL) {
91                 goto cleanup;
92         }
93                 
94         vs = ldap_get_values(lc->ld, e, mapped.bv_val);
95         if (vs == NULL) {
96                 goto cleanup;
97         }
98
99         for ( count = 0; vs[count] != NULL; count++ ) { }
100         v = (BerVarray) ch_calloc( (count + 1), sizeof(struct berval) );
101         if (v == NULL) {
102                 goto cleanup;
103         }
104
105         is_oc = (strcasecmp("objectclass", mapped.bv_val) == 0);
106         for ( i = 0, j = 0; i < count; i++) {
107                 ber_str2bv(vs[i], 0, 0, &v[j] );
108                 if (!is_oc) {
109                         if( v[j].bv_val == NULL )
110                                 ch_free(vs[i]);
111                         else
112                                 j++;
113                 } else {
114                         ldap_back_map(&li->oc_map, &v[j], &mapped,
115                                         BACKLDAP_REMAP);
116                         if (mapped.bv_val && mapped.bv_val[0] != '\0') {
117                                 ber_dupbv( &v[j], &mapped );
118                                 if (v[j].bv_val)
119                                         j++;
120                         }
121                         ch_free(vs[i]);
122                 }
123         }
124         v[j].bv_val = NULL;
125         *vals = v;
126         rc = 0;
127         ch_free(vs);
128         vs = NULL;
129
130 cleanup:
131         if (vs) {
132                 ldap_value_free(vs);
133         }
134         if (result) {
135                 ldap_msgfree(result);
136         }
137
138         return(rc);
139 }
140