File: attribute.c

package info (click to toggle)
openldap2 2.1.30-8
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 13,320 kB
  • ctags: 9,329
  • sloc: ansic: 127,414; sh: 18,958; cpp: 3,897; sql: 1,490; makefile: 1,221; perl: 843
file content (139 lines) | stat: -rw-r--r-- 3,059 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* group.c - ldap backend acl group routine */
/* $OpenLDAP: pkg/ldap/servers/slapd/back-ldap/attribute.c,v 1.12.2.1 2003/02/09 16:31:38 kurt Exp $ */
/*
 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
 */

#include "portable.h"

#include <stdio.h>

#include <ac/socket.h>
#include <ac/string.h>

#include "slap.h"
#include "back-ldap.h"


/* return 0 IFF we can retrieve the attributes
 * of entry with ndn
 */
int
ldap_back_attribute(
	Backend	*be,
	Connection *conn,
	Operation *op,
	Entry	*target,
	struct berval	*ndn,
	AttributeDescription *entry_at,
	BerVarray *vals
)
{
	struct ldapinfo *li = (struct ldapinfo *) be->be_private;    
	int rc = 1, i, j, count, is_oc;
	Attribute *attr = NULL;
	BerVarray abv, v;
	struct berval mapped = { 0, NULL };
	char **vs = NULL;
	LDAPMessage	*result = NULL, *e = NULL;
	char *gattr[2];
	LDAP *ld = NULL;

	*vals = NULL;
	if (target != NULL && dn_match( &target->e_nname, ndn )) {
		/* 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 ( count = 0; attr->a_vals[count].bv_val != NULL; count++ ) { }
		v = (BerVarray) ch_calloc( (count + 1), sizeof(struct berval) );
		if (v != NULL) {
			for ( j = 0, abv = attr->a_vals; --count >= 0; abv++ ) {
				if ( abv->bv_len > 0 ) {
					ber_dupbv( &v[j], abv );
					if( v[j].bv_val == NULL )
						break;
				}
			}
			v[j].bv_val = NULL;
			*vals = v;
			return 0;
		}

	}
	ldap_back_map(&li->at_map, &entry_at->ad_cname, &mapped, BACKLDAP_MAP);
	if (mapped.bv_val == NULL || mapped.bv_val[0] == '\0') {
		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) {
		goto cleanup;
	}

	gattr[0] = mapped.bv_val;
	gattr[1] = NULL;
	if (ldap_search_ext_s(ld, ndn->bv_val, LDAP_SCOPE_BASE, "(objectclass=*)",
				gattr, 0, NULL, NULL, LDAP_NO_LIMIT,
				LDAP_NO_LIMIT, &result) != LDAP_SUCCESS)
	{
		goto cleanup;
	}

	if ((e = ldap_first_entry(ld, result)) == NULL) {
		goto cleanup;
	}
		
	vs = ldap_get_values(ld, e, mapped.bv_val);
	if (vs == NULL) {
		goto cleanup;
	}

	for ( count = 0; vs[count] != NULL; count++ ) { }
	v = (BerVarray) ch_calloc( (count + 1), sizeof(struct berval) );
	if (v == NULL) {
		goto cleanup;
	}

	is_oc = (strcasecmp("objectclass", mapped.bv_val) == 0);
	for ( i = 0, j = 0; i < count; i++) {
		ber_str2bv(vs[i], 0, 0, &v[j] );
		if (!is_oc) {
			if( v[j].bv_val == NULL )
				ch_free(vs[i]);
			else
				j++;
		} else {
			ldap_back_map(&li->oc_map, &v[j], &mapped,
					BACKLDAP_REMAP);
			if (mapped.bv_val && mapped.bv_val[0] != '\0') {
				ber_dupbv( &v[j], &mapped );
				if (v[j].bv_val)
					j++;
			}
			ch_free(vs[i]);
		}
	}
	v[j].bv_val = NULL;
	*vals = v;
	rc = 0;
	ch_free(vs);
	vs = NULL;

cleanup:
	if (vs) {
		ldap_value_free(vs);
	}
	if (result) {
		ldap_msgfree(result);
	}
	ldap_unbind(ld);

	return(rc);
}