1 /* map.c - ldap backend mapping routines */
3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6 /* This is an altered version */
8 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
10 * Permission is granted to anyone to use this software for any purpose
11 * on any computer system, and to alter it and redistribute it, subject
12 * to the following restrictions:
14 * 1. The author is not responsible for the consequences of use of this
15 * software, no matter how awful, even if they arise from flaws in it.
17 * 2. The origin of this software must not be misrepresented, either by
18 * explicit claim or by omission. Since few users ever read sources,
19 * credits should appear in the documentation.
21 * 3. Altered versions must be plainly marked as such, and must not be
22 * misrepresented as being the original software. Since few users
23 * ever read sources, credits should appear in the documentation.
25 * 4. This notice may not be removed or altered.
29 * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
31 * This software is being modified by Pierangelo Masarati.
32 * The previously reported conditions apply to the modified code as well.
33 * Changes in the original code are highlighted where required.
34 * Credits for the original code go to the author, Howard Chu.
41 #include <ac/string.h>
42 #include <ac/socket.h>
45 #include "back-ldap.h"
48 mapping_cmp ( const void *c1, const void *c2 )
50 struct ldapmapping *map1 = (struct ldapmapping *)c1;
51 struct ldapmapping *map2 = (struct ldapmapping *)c2;
52 int rc = map1->src.bv_len - map2->src.bv_len;
54 return ( strcasecmp(map1->src.bv_val, map2->src.bv_val) );
58 mapping_dup ( void *c1, void *c2 )
60 struct ldapmapping *map1 = (struct ldapmapping *)c1;
61 struct ldapmapping *map2 = (struct ldapmapping *)c2;
63 return( ( strcasecmp(map1->src.bv_val, map2->src.bv_val) == 0 ) ? -1 : 0 );
67 ldap_back_map_init ( struct ldapmap *lm, struct ldapmapping **m )
69 struct ldapmapping *mapping;
75 mapping = (struct ldapmapping *)ch_calloc( 2,
76 sizeof( struct ldapmapping ) );
77 if ( mapping == NULL ) {
81 ber_str2bv( "objectclass", sizeof("objectclass")-1, 1, &mapping->src);
82 ber_dupbv( &mapping->dst, &mapping->src );
83 mapping[1].src = mapping->src;
84 mapping[1].dst = mapping->dst;
86 avl_insert( &lm->map, (caddr_t)mapping,
87 mapping_cmp, mapping_dup );
88 avl_insert( &lm->remap, (caddr_t)&mapping[1],
89 mapping_cmp, mapping_dup );
94 ldap_back_map ( struct ldapmap *map, struct berval *s, struct berval *bv,
98 struct ldapmapping *mapping, fmapping;
108 mapping = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, mapping_cmp );
109 if (mapping != NULL) {
110 if ( mapping->dst.bv_val )
115 if (!map->drop_missing)
122 ldap_back_map_filter(
123 struct ldapmap *at_map,
124 struct ldapmap *oc_map,
129 char *nf, *p, *q, *s, c;
130 int len, extra, plen, in_quote;
131 struct berval m, tmp;
139 nf = ch_malloc( len + 1 );
143 /* this loop assumes the filter ends with one
144 * of the delimiter chars -- probably ')'.
150 for (p = f->bv_val; (c = *p); p++) {
152 in_quote = !in_quote;
155 AC_MEMCPY(s, q, plen);
160 } else if (in_quote) {
161 /* ignore everything in quotes --
162 * what about attrs in DNs?
165 } else if (c != '(' && c != ')'
166 && c != '=' && c != '>' && c != '<'
167 && c != '|' && c != '&')
176 ldap_back_map(at_map, &tmp, &m, remap);
177 if (m.bv_val == NULL)
178 ldap_back_map(oc_map, &tmp, &m, remap);
179 if (m.bv_val == NULL) {
192 tmpnf = ch_realloc(nf, len + 1);
200 AC_MEMCPY(s, m.bv_val, plen);
214 struct ldapmap *at_map,
221 struct berval mapped;
226 for (i = 0; an[i].an_name.bv_val; i++) {
230 na = (char **)ch_calloc( i + 1, sizeof(char *) );
234 for (i = j = 0; an[i].an_name.bv_val; i++) {
235 ldap_back_map(at_map, &an[i].an_name, &mapped, remap);
236 if (mapped.bv_val != NULL)
237 na[j++] = mapped.bv_val;
239 if (j == 0 && i != 0)
240 na[j++] = LDAP_NO_ATTRS;