]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/attribute.c
Moved AttributeDescription caching into main code:
[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 e_ndn
21  */
22 int
23 ldap_back_attribute(
24         Backend *be,
25         Connection *conn,
26         Operation *op,
27         Entry   *target,
28         const char      *e_ndn,
29         AttributeDescription *entry_at,
30         struct berval ***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         struct berval **abv, **v;
37         char **vs, *mapped;
38         LDAPMessage     *result, *e;
39         char *gattr[2];
40         LDAP *ld;
41
42         *vals = NULL;
43         if (target != NULL && strcmp(target->e_ndn, e_ndn) == 0) {
44                 /* we already have a copy of the entry */
45                 /* attribute and objectclass mapping has already been done */
46                 if ((attr = attr_find(target->e_attrs, entry_at)) == NULL)
47                         return(1);
48
49                 for ( count = 0; attr->a_vals[count] != NULL; count++ ) { }
50                 v = (struct berval **) ch_calloc( (count + 1), sizeof(struct berval *) );
51                 if (v != NULL) {
52                         for ( j = 0, abv = attr->a_vals; --count >= 0; abv++ ) {
53                                 if ( (*abv)->bv_len > 0 ) {
54                                         v[j] = ber_bvdup( *abv );
55                                         if( v[j] == NULL )
56                                                 break;
57                                 }
58                         }
59                         v[j] = NULL;
60                         *vals = v;
61                         rc = 0;
62                 }
63
64         } else {
65                 mapped = ldap_back_map(&li->at_map, entry_at->ad_cname.bv_val, 0);
66                 if (mapped == NULL)
67                         return(1);
68
69                 if (ldap_initialize(&ld, li->url) != LDAP_SUCCESS) {
70                         return(1);
71                 }
72
73                 if (ldap_bind_s(ld, li->binddn, li->bindpw, LDAP_AUTH_SIMPLE) == LDAP_SUCCESS) {
74                         gattr[0] = mapped;
75                         gattr[1] = NULL;
76                         if (ldap_search_ext_s(ld, e_ndn, LDAP_SCOPE_BASE, "(objectclass=*)",
77                                                                         gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
78                                                                         LDAP_NO_LIMIT, &result) == LDAP_SUCCESS)
79                         {
80                                 if ((e = ldap_first_entry(ld, result)) != NULL) {
81                                         vs = ldap_get_values(ld, e, mapped);
82                                         if (vs != NULL) {
83                                                 for ( count = 0; vs[count] != NULL; count++ ) { }
84                                                 v = (struct berval **) ch_calloc( (count + 1), sizeof(struct berval *) );
85                                                 if (v == NULL) {
86                                                         ldap_value_free(vs);
87                                                 } else {
88                                                         is_oc = (strcasecmp("objectclass", mapped) == 0);
89                                                         for ( i = 0, j = 0; i < count; i++) {
90                                                                 if (!is_oc) {
91                                                                         v[j] = ber_bvstr( vs[i] );
92                                                                         if( v[j] == NULL )
93                                                                                 ch_free(vs[i]);
94                                                                         else
95                                                                                 j++;
96                                                                 } else {
97                                                                         mapped = ldap_back_map(&li->oc_map, vs[i], 1);
98                                                                         if (mapped) {
99                                                                                 mapped = ch_strdup( mapped );
100                                                                                 if (mapped) {
101                                                                                         v[j] = ber_bvstr( mapped );
102                                                                                         if (v[j])
103                                                                                                 j++;
104                                                                                 }
105                                                                         }
106                                                                         ch_free(vs[i]);
107                                                                 }
108                                                         }
109                                                         v[j] = NULL;
110                                                         *vals = v;
111                                                         rc = 0;
112                                                         ch_free(vs);
113                                                 }
114                                         }
115                                 }
116                                 ldap_msgfree(result);
117                         }
118                 }
119                 ldap_unbind(ld);
120     }
121
122         return(rc);
123 }
124