]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/attribute.c
More struct beral conversion
[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         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 && target->e_nname.bv_len == ndn->bv_len &&
44                 strcmp(target->e_nname.bv_val, ndn->bv_val) == 0) {
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] != NULL; count++ ) { }
51                 v = (struct berval **) 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                                         v[j] = ber_bvdup( *abv );
56                                         if( v[j] == NULL )
57                                                 break;
58                                 }
59                         }
60                         v[j] = NULL;
61                         *vals = v;
62                         rc = 0;
63                 }
64
65         } else {
66                 mapped = ldap_back_map(&li->at_map, entry_at->ad_cname.bv_val, 0);
67                 if (mapped == NULL)
68                         return(1);
69
70                 if (ldap_initialize(&ld, li->url) != LDAP_SUCCESS) {
71                         return(1);
72                 }
73
74                 if (ldap_bind_s(ld, li->binddn, li->bindpw, LDAP_AUTH_SIMPLE) == LDAP_SUCCESS) {
75                         gattr[0] = mapped;
76                         gattr[1] = NULL;
77                         if (ldap_search_ext_s(ld, ndn->bv_val, LDAP_SCOPE_BASE, "(objectclass=*)",
78                                                                         gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
79                                                                         LDAP_NO_LIMIT, &result) == LDAP_SUCCESS)
80                         {
81                                 if ((e = ldap_first_entry(ld, result)) != NULL) {
82                                         vs = ldap_get_values(ld, e, mapped);
83                                         if (vs != NULL) {
84                                                 for ( count = 0; vs[count] != NULL; count++ ) { }
85                                                 v = (struct berval **) ch_calloc( (count + 1), sizeof(struct berval *) );
86                                                 if (v == NULL) {
87                                                         ldap_value_free(vs);
88                                                 } else {
89                                                         is_oc = (strcasecmp("objectclass", mapped) == 0);
90                                                         for ( i = 0, j = 0; i < count; i++) {
91                                                                 if (!is_oc) {
92                                                                         v[j] = ber_bvstr( vs[i] );
93                                                                         if( v[j] == NULL )
94                                                                                 ch_free(vs[i]);
95                                                                         else
96                                                                                 j++;
97                                                                 } else {
98                                                                         mapped = ldap_back_map(&li->oc_map, vs[i], 1);
99                                                                         if (mapped) {
100                                                                                 mapped = ch_strdup( mapped );
101                                                                                 if (mapped) {
102                                                                                         v[j] = ber_bvstr( mapped );
103                                                                                         if (v[j])
104                                                                                                 j++;
105                                                                                 }
106                                                                         }
107                                                                         ch_free(vs[i]);
108                                                                 }
109                                                         }
110                                                         v[j] = NULL;
111                                                         *vals = v;
112                                                         rc = 0;
113                                                         ch_free(vs);
114                                                 }
115                                         }
116                                 }
117                                 ldap_msgfree(result);
118                         }
119                 }
120                 ldap_unbind(ld);
121     }
122
123         return(rc);
124 }
125