]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-ldap/attribute.c
fix for select_backend suggested G. Gombas (ITS 1090)
[openldap] / servers / slapd / back-ldap / attribute.c
index 8588a291209045010b2d01ef2af01154440f4cad..e8b49e052adcbdf5d03e78c9d3f3d6075e506d30 100644 (file)
@@ -27,14 +27,14 @@ ldap_back_attribute(
        Entry   *target,
        const char      *e_ndn,
        AttributeDescription *entry_at,
-       const char ***vals
+       struct berval ***vals
 )
 {
        struct ldapinfo *li = (struct ldapinfo *) be->be_private;    
-       int rc = 1, i, j;
+       int rc = 1, i, j, count, is_oc;
        Attribute *attr;
-       struct berval **abv;
-       char *s, **v;
+       struct berval **abv, **v;
+       char **vs, *mapped;
        LDAPMessage     *result, *e;
        char *gattr[2];
        LDAP *ld;
@@ -42,20 +42,18 @@ ldap_back_attribute(
        *vals = NULL;
        if (target != NULL && strcmp(target->e_ndn, e_ndn) == 0) {
                /* we already have a copy of the entry */
+               /* attribute and objectclass mapping has already been done */
                if ((attr = attr_find(target->e_attrs, entry_at)) == NULL)
                        return(1);
 
-               for ( i = 0; attr->a_vals[i] != NULL; i++ ) { }
-               v = (char **) ch_calloc( (i + 1), sizeof(char *) );
+               for ( count = 0; attr->a_vals[count] != NULL; count++ ) { }
+               v = (struct berval **) ch_calloc( (count + 1), sizeof(struct berval *) );
                if (v != NULL) {
-                       for ( j = 0, abv = attr->a_vals; --i >= 0; abv++ ) {
+                       for ( j = 0, abv = attr->a_vals; --count >= 0; abv++ ) {
                                if ( (*abv)->bv_len > 0 ) {
-                                       s = ch_malloc( (*abv)->bv_len + 1 );
-                                       if( s == NULL )
+                                       v[j] = ber_bvdup( *abv );
+                                       if( v[j] == NULL )
                                                break;
-                                       memcpy(s, (*abv)->bv_val, (*abv)->bv_len);
-                                       s[(*abv)->bv_len] = 0;
-                                       v[j++] = s;
                                }
                        }
                        v[j] = NULL;
@@ -64,21 +62,56 @@ ldap_back_attribute(
                }
 
        } else {
+               mapped = ldap_back_map(&li->at_map, entry_at->ad_cname->bv_val, 0);
+               if (mapped == NULL)
+                       return(1);
+
                if (ldap_initialize(&ld, li->url) != LDAP_SUCCESS) {
                        return(1);
                }
 
                if (ldap_bind_s(ld, li->binddn, li->bindpw, LDAP_AUTH_SIMPLE) == LDAP_SUCCESS) {
-                       gattr[0] = entry_at->ad_cname->bv_val;
+                       gattr[0] = mapped;
                        gattr[1] = NULL;
                        if (ldap_search_ext_s(ld, e_ndn, LDAP_SCOPE_BASE, "(objectclass=*)",
                                                                        gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
                                                                        LDAP_NO_LIMIT, &result) == LDAP_SUCCESS)
                        {
                                if ((e = ldap_first_entry(ld, result)) != NULL) {
-                                       *vals = ldap_get_values(ld, e, entry_at->ad_cname->bv_val);
-                                       if (*vals != NULL)
-                                               rc = 0;
+                                       vs = ldap_get_values(ld, e, mapped);
+                                       if (vs != NULL) {
+                                               for ( count = 0; vs[count] != NULL; count++ ) { }
+                                               v = (struct berval **) ch_calloc( (count + 1), sizeof(struct berval *) );
+                                               if (v == NULL) {
+                                                       ldap_value_free(vs);
+                                               } else {
+                                                       is_oc = (strcasecmp("objectclass", mapped) == 0);
+                                                       for ( i = 0, j = 0; i < count; i++) {
+                                                               if (!is_oc) {
+                                                                       v[j] = ber_bvstr( vs[i] );
+                                                                       if( v[j] == NULL )
+                                                                               ch_free(vs[i]);
+                                                                       else
+                                                                               j++;
+                                                               } else {
+                                                                       mapped = ldap_back_map(&li->oc_map, vs[i], 1);
+                                                                       if (mapped) {
+                                                                               mapped = ch_strdup( mapped );
+                                                                               if (mapped) {
+                                                                                       v[j] = ber_bvstr( mapped );
+                                                                                       if (v[j])
+                                                                                               j++;
+                                                                               }
+                                                                       }
+                                                                       ch_free(vs[i]);
+                                                               }
+                                                       }
+                                                       v[j] = NULL;
+                                                       *vals = v;
+                                                       rc = 0;
+                                                       ch_free(vs);
+                                               }
+                                       }
                                }
                                ldap_msgfree(result);
                        }