]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldap/attribute.c
Updated attribute routine to pass back bervals.
[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;
35         Attribute *attr;
36         struct berval **abv, **v;
37         char **vs;
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                 if ((attr = attr_find(target->e_attrs, entry_at)) == NULL)
46                         return(1);
47
48                 for ( count = 0; attr->a_vals[count] != NULL; count++ ) { }
49                 v = (struct berval **) ch_calloc( (count + 1), sizeof(struct berval *) );
50                 if (v != NULL) {
51                         for ( j = 0, abv = attr->a_vals; --count >= 0; abv++ ) {
52                                 if ( (*abv)->bv_len > 0 ) {
53                                         v[j] = ber_bvdup( *abv );
54                                         if( v[j] == NULL )
55                                                 break;
56                                 }
57                         }
58                         v[j] = NULL;
59                         *vals = v;
60                         rc = 0;
61                 }
62
63         } else {
64                 if (ldap_initialize(&ld, li->url) != LDAP_SUCCESS) {
65                         return(1);
66                 }
67
68                 if (ldap_bind_s(ld, li->binddn, li->bindpw, LDAP_AUTH_SIMPLE) == LDAP_SUCCESS) {
69                         gattr[0] = entry_at->ad_cname->bv_val;
70                         gattr[1] = NULL;
71                         if (ldap_search_ext_s(ld, e_ndn, LDAP_SCOPE_BASE, "(objectclass=*)",
72                                                                         gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
73                                                                         LDAP_NO_LIMIT, &result) == LDAP_SUCCESS)
74                         {
75                                 if ((e = ldap_first_entry(ld, result)) != NULL) {
76                                         vs = ldap_get_values(ld, e, entry_at->ad_cname->bv_val);
77                                         if (vs != NULL) {
78                                                 for ( count = 0; vs[count] != NULL; count++ ) { }
79                                                 v = (struct berval **) ch_calloc( (count + 1), sizeof(struct berval *) );
80                                                 if (v == NULL) {
81                                                         ldap_value_free(vs);
82                                                 } else {
83                                                         for ( i = 0, j = 0; i < count; i++) {
84                                                                 v[j] = ber_bvstr( vs[i] );
85                                                                 if( v[j] == NULL )
86                                                                         ch_free(vs[i]);
87                                                                 else
88                                                                         j++;
89                                                         }
90                                                         v[j] = NULL;
91                                                         *vals = v;
92                                                         rc = 0;
93                                                         ch_free(vs);
94                                                 }
95                                         }
96                                 }
97                                 ldap_msgfree(result);
98                         }
99                 }
100                 ldap_unbind(ld);
101     }
102
103         return(rc);
104 }
105