]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/attribute.c
06c8998d955acb9b711896af6c56065c0a5aa3dd
[openldap] / servers / slapd / back-ldap / attribute.c
1 /* group.c - ldap backend acl group routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-1999 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         BVarray *vals
31 )
32 {
33         struct ldapinfo *li = (struct ldapinfo *) be->be_private;    
34         int rc = 1, i, j, count, is_oc;
35         Attribute *attr;
36         BVarray abv, v;
37         struct berval mapped;
38         char **vs;
39         LDAPMessage     *result, *e;
40         char *gattr[2];
41         LDAP *ld;
42
43         *vals = NULL;
44         if (target != NULL && target->e_nname.bv_len == ndn->bv_len &&
45                 strcmp(target->e_nname.bv_val, ndn->bv_val) == 0) {
46                 /* we already have a copy of the entry */
47                 /* attribute and objectclass mapping has already been done */
48                 if ((attr = attr_find(target->e_attrs, entry_at)) == NULL)
49                         return(1);
50
51                 for ( count = 0; attr->a_vals[count].bv_val != NULL; count++ ) { }
52                 v = (BVarray) ch_calloc( (count + 1), sizeof(struct berval) );
53                 if (v != NULL) {
54                         for ( j = 0, abv = attr->a_vals; --count >= 0; abv++ ) {
55                                 if ( abv->bv_len > 0 ) {
56                                         ber_dupbv( &v[j], abv );
57                                         if( v[j].bv_val == NULL )
58                                                 break;
59                                 }
60                         }
61                         v[j].bv_val = NULL;
62                         *vals = v;
63                         rc = 0;
64                 }
65
66         } else {
67                 ldap_back_map(&li->at_map, &entry_at->ad_cname, &mapped, 0);
68                 if (mapped.bv_val == NULL)
69                         return(1);
70
71                 if (ldap_initialize(&ld, li->url) != LDAP_SUCCESS) {
72                         return(1);
73                 }
74
75                 if (ldap_bind_s(ld, li->binddn, li->bindpw, LDAP_AUTH_SIMPLE) == LDAP_SUCCESS) {
76                         gattr[0] = mapped.bv_val;
77                         gattr[1] = NULL;
78                         if (ldap_search_ext_s(ld, ndn->bv_val, LDAP_SCOPE_BASE, "(objectclass=*)",
79                                                                         gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
80                                                                         LDAP_NO_LIMIT, &result) == LDAP_SUCCESS)
81                         {
82                                 if ((e = ldap_first_entry(ld, result)) != NULL) {
83                                         vs = ldap_get_values(ld, e, mapped.bv_val);
84                                         if (vs != NULL) {
85                                                 for ( count = 0; vs[count] != NULL; count++ ) { }
86                                                 v = (BVarray) ch_calloc( (count + 1), sizeof(struct berval) );
87                                                 if (v == NULL) {
88                                                         ldap_value_free(vs);
89                                                 } else {
90                                                         is_oc = (strcasecmp("objectclass", mapped.bv_val) == 0);
91                                                         for ( i = 0, j = 0; i < count; i++) {
92                                                                 ber_str2bv(vs[i], 0, 0, &v[j] );
93                                                                 if (!is_oc) {
94                                                                         if( v[j].bv_val == NULL )
95                                                                                 ch_free(vs[i]);
96                                                                         else
97                                                                                 j++;
98                                                                 } else {
99                                                                         ldap_back_map(&li->oc_map, &v[j], &mapped, 1);
100                                                                         if (mapped.bv_val) {
101                                                                                 ber_dupbv( &v[j], &mapped );
102                                                                                 if (v[j].bv_val)
103                                                                                         j++;
104                                                                         }
105                                                                         ch_free(vs[i]);
106                                                                 }
107                                                         }
108                                                         v[j].bv_val = NULL;
109                                                         *vals = v;
110                                                         rc = 0;
111                                                         ch_free(vs);
112                                                 }
113                                         }
114                                 }
115                                 ldap_msgfree(result);
116                         }
117                 }
118                 ldap_unbind(ld);
119     }
120
121         return(rc);
122 }
123