1 /* config.c - ldap backend configuration file routine */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 /* This is an altered version */
9 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
11 * Permission is granted to anyone to use this software for any purpose
12 * on any computer system, and to alter it and redistribute it, subject
13 * to the following restrictions:
15 * 1. The author is not responsible for the consequences of use of this
16 * software, no matter how awful, even if they arise from flaws in it.
18 * 2. The origin of this software must not be misrepresented, either by
19 * explicit claim or by omission. Since few users ever read sources,
20 * credits should appear in the documentation.
22 * 3. Altered versions must be plainly marked as such, and must not be
23 * misrepresented as being the original software. Since few users
24 * ever read sources, credits should appear in the documentation.
26 * 4. This notice may not be removed or altered.
30 * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
32 * This software is being modified by Pierangelo Masarati.
33 * The previously reported conditions apply to the modified code as well.
34 * Changes in the original code are highlighted where required.
35 * Credits for the original code go to the author, Howard Chu.
42 #include <ac/string.h>
43 #include <ac/socket.h>
46 #include "back-ldap.h"
57 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
60 fprintf( stderr, "%s: line %d: ldap backend info is null!\n",
65 /* server address to query (depricated, use "uri" directive) */
66 if ( strcasecmp( argv[0], "server" ) == 0 ) {
69 "%s: line %d: missing address in \"server <address>\" line\n",
75 li->url = ch_calloc(strlen(argv[1]) + 9, sizeof(char));
76 if (li->url != NULL) {
77 strcpy(li->url, "ldap://");
78 strcat(li->url, argv[1]);
82 /* URI of server to query (preferred over "server" directive) */
83 } else if ( strcasecmp( argv[0], "uri" ) == 0 ) {
86 "%s: line %d: missing address in \"uri <address>\" line\n",
92 li->url = ch_strdup(argv[1]);
94 /* name to use for ldap_back_group */
95 } else if ( strcasecmp( argv[0], "binddn" ) == 0 ) {
98 "%s: line %d: missing name in \"binddn <name>\" line\n",
102 li->binddn = ch_strdup(argv[1]);
104 /* password to use for ldap_back_group */
105 } else if ( strcasecmp( argv[0], "bindpw" ) == 0 ) {
108 "%s: line %d: missing password in \"bindpw <password>\" line\n",
112 li->bindpw = ch_strdup(argv[1]);
115 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
116 #ifndef ENABLE_REWRITE
117 struct berval *bd2, *nd2;
118 #endif /* ENABLE_REWRITE */
120 struct berval bdn, ndn;
125 * suffixmassage <suffix> <massaged suffix>
127 * the <suffix> field must be defined as a valid suffix
128 * (or suffixAlias?) for the current database;
129 * the <massaged suffix> shouldn't have already been
130 * defined as a valid suffix or suffixAlias for the
134 fprintf( stderr, "%s: line %d: syntax is"
135 " \"suffixMassage <suffix>"
136 " <massaged suffix>\"\n",
141 bdn.bv_val = argv[1];
142 bdn.bv_len = strlen(bdn.bv_val);
143 if ( dnNormalize2( NULL, &bdn, &ndn ) != LDAP_SUCCESS ) {
144 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
145 fname, lineno, bdn.bv_val );
148 tmp_be = select_backend( &ndn, 0, 0 );
150 if ( tmp_be != NULL && tmp_be != be ) {
151 fprintf( stderr, "%s: line %d: suffix already in use"
152 " by another backend in"
153 " \"suffixMassage <suffix>"
154 " <massaged suffix>\"\n",
159 bdn.bv_val = argv[2];
160 bdn.bv_len = strlen(bdn.bv_val);
161 if ( dnNormalize2( NULL, &bdn, &ndn ) != LDAP_SUCCESS ) {
162 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
163 fname, lineno, bdn.bv_val );
166 tmp_be = select_backend( &ndn, 0, 0 );
168 if ( tmp_be != NULL ) {
169 fprintf( stderr, "%s: line %d: massaged suffix"
170 " already in use by another backend in"
171 " \"suffixMassage <suffix>"
172 " <massaged suffix>\"\n",
177 #ifdef ENABLE_REWRITE
179 * The suffix massaging is emulated by means of the
180 * rewrite capabilities
181 * FIXME: no extra rewrite capabilities should be added
184 return suffix_massage_config( li->rwinfo, argc, argv );
185 #else /* !ENABLE_REWRITE */
186 bd2 = ber_bvstrdup( argv[1] );
187 ber_bvecadd( &li->suffix_massage, bd2 );
189 dnNormalize( NULL, bd2, &nd2 );
190 ber_bvecadd( &li->suffix_massage, nd2 );
192 bd2 = ber_bvstrdup( argv[2] );
193 ber_bvecadd( &li->suffix_massage, bd2 );
195 dnNormalize( NULL, bd2, &nd2 );
196 ber_bvecadd( &li->suffix_massage, nd2 );
197 #endif /* !ENABLE_REWRITE */
199 #ifdef ENABLE_REWRITE
200 /* rewrite stuff ... */
201 } else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
202 return rewrite_parse( li->rwinfo, fname, lineno, argc, argv );
203 #endif /* ENABLE_REWRITE */
205 /* objectclass/attribute mapping */
206 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
208 struct ldapmapping *mapping;
211 if ( argc < 3 || argc > 4 ) {
213 "%s: line %d: syntax is \"map {objectclass | attribute} {<source> | *} [<dest> | *]\"\n",
218 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
220 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
223 fprintf( stderr, "%s: line %d: syntax is "
224 "\"map {objectclass | attribute} {<source> | *} "
230 if ( strcasecmp( argv[2], "*" ) != 0 ) {
234 else if ( strcasecmp( argv[3], "*" ) == 0 )
240 map->drop_missing = 1;
243 if ( strcasecmp( argv[3], "*" ) == 0 ) {
244 map->drop_missing = 0;
252 if ( ( map == &li->at_map )
253 && ( strcasecmp( src, "objectclass" ) == 0
254 || strcasecmp( dst, "objectclass" ) == 0 ) )
257 "%s: line %d: objectclass attribute cannot be mapped\n",
261 mapping = (struct ldapmapping *)ch_calloc( 2,
262 sizeof(struct ldapmapping) );
263 if ( mapping == NULL ) {
265 "%s: line %d: out of memory\n",
269 ber_str2bv( src, 0, 1, &mapping->src );
270 ber_str2bv( dst, 0, 1, &mapping->dst );
272 mapping[1].src = mapping->dst;
273 mapping[1].dst = mapping->src;
275 mapping[1].src = mapping->src;
276 mapping[1].dst = mapping->dst;
279 if ( avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL ||
280 avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
283 "%s: line %d: duplicate mapping found (ignored)\n",
288 avl_insert( &map->map, (caddr_t)mapping,
289 mapping_cmp, mapping_dup );
290 avl_insert( &map->remap, (caddr_t)&mapping[1],
291 mapping_cmp, mapping_dup );
295 fprintf( stderr, "%s: line %d: unknown directive \"%s\" "
296 "in ldap database definition (ignored)\n",
297 fname, lineno, argv[0] );
303 mapping_cmp ( const void *c1, const void *c2 )
305 struct ldapmapping *map1 = (struct ldapmapping *)c1;
306 struct ldapmapping *map2 = (struct ldapmapping *)c2;
307 int rc = map1->src.bv_len - map2->src.bv_len;
309 return ( strcasecmp(map1->src.bv_val, map2->src.bv_val) );
313 mapping_dup ( void *c1, void *c2 )
315 struct ldapmapping *map1 = (struct ldapmapping *)c1;
316 struct ldapmapping *map2 = (struct ldapmapping *)c2;
318 return( ( strcasecmp(map1->src.bv_val, map2->src.bv_val) == 0 ) ? -1 : 0 );
322 ldap_back_map ( struct ldapmap *map, struct berval *s, struct berval *bv,
326 struct ldapmapping *mapping, fmapping;
336 mapping = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, mapping_cmp );
337 if (mapping != NULL) {
338 if ( mapping->dst.bv_val )
343 if (!map->drop_missing)
350 ldap_back_map_filter(
351 struct ldapmap *at_map,
352 struct ldapmap *oc_map,
357 char *nf, *p, *q, *s, c;
358 int len, extra, plen, in_quote;
359 struct berval m, tmp;
367 nf = ch_malloc( len + 1 );
371 /* this loop assumes the filter ends with one
372 * of the delimiter chars -- probably ')'.
378 for (p = f->bv_val; (c = *p); p++) {
380 in_quote = !in_quote;
383 AC_MEMCPY(s, q, plen);
388 } else if (in_quote) {
389 /* ignore everything in quotes --
390 * what about attrs in DNs?
393 } else if (c != '(' && c != ')'
394 && c != '=' && c != '>' && c != '<'
395 && c != '|' && c != '&')
404 ldap_back_map(at_map, &tmp, &m, remap);
405 if (m.bv_val == NULL)
406 ldap_back_map(oc_map, &tmp, &m, remap);
407 if (m.bv_val == NULL) {
419 nf = ch_realloc(nf, len + 1);
426 AC_MEMCPY(s, m.bv_val, plen);
440 struct ldapmap *at_map,
447 struct berval mapped;
452 for (i = 0; an[i].an_name.bv_val; i++) {
456 na = (char **)ch_calloc( i + 1, sizeof(char *) );
460 for (i = 0; an[i].an_name.bv_val; i++) {
461 ldap_back_map(at_map, &an[i].an_name, &mapped, remap);
462 if (mapped.bv_val != NULL) {
463 na[i] = mapped.bv_val;
470 #ifdef ENABLE_REWRITE
472 suffix_massage_regexize( const char *s )
477 for ( i = 0, p = ( char * )s;
478 ( r = strchr( p, ',' ) ) != NULL;
482 res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
484 strcpy( res, "(.*)" );
485 for ( i = 0, p = ( char * )s;
486 ( r = strchr( p, ',' ) ) != NULL;
488 strncat( res, p, r - p + 1 );
489 strcat( res, "[ ]?" );
491 if ( r[ 1 ] == ' ' ) {
501 suffix_massage_patternize( const char *s, int normalize )
505 res = ch_calloc( sizeof( char ), strlen( s ) + sizeof("%1") );
507 sprintf( res, "%%1%s", s );
510 char *out = dn_normalize( res + (sizeof("%1")-1) );
511 if ( out != res + 2 ) {
512 strcpy( res + 2, out );
521 suffix_massage_config(
522 struct rewrite_info *info,
529 rargv[ 0 ] = "rewriteEngine";
532 rewrite_parse( info, "<suffix massage>", 1, 2, rargv );
534 rargv[ 0 ] = "rewriteContext";
535 rargv[ 1 ] = "default";
537 rewrite_parse( info, "<suffix massage>", 2, 2, rargv );
539 rargv[ 0 ] = "rewriteRule";
540 rargv[ 1 ] = suffix_massage_regexize( argv[ 1 ] );
541 rargv[ 2 ] = suffix_massage_patternize( argv[ 2 ], 0 );
544 rewrite_parse( info, "<suffix massage>", 3, 4, rargv );
545 ch_free( rargv[ 1 ] );
546 ch_free( rargv[ 2 ] );
548 rargv[ 0 ] = "rewriteContext";
549 rargv[ 1 ] = "searchResult";
551 rewrite_parse( info, "<suffix massage>", 4, 2, rargv );
553 rargv[ 0 ] = "rewriteRule";
554 rargv[ 1 ] = suffix_massage_regexize( argv[ 2 ] );
555 rargv[ 2 ] = suffix_massage_patternize( argv[ 1 ], 0 );
558 rewrite_parse( info, "<suffix massage>", 5, 4, rargv );
559 ch_free( rargv[ 1 ] );
560 ch_free( rargv[ 2 ] );
563 * the filter should be rewritten as
566 * "(.*)member=([^)]+),o=Foo Bar,[ ]?c=US(.*)"
567 * "%1member=%2,dc=example,dc=com%3"
569 * where "o=Foo Bar, c=US" is the virtual naming context,
570 * and "dc=example, dc=com" is the real naming context
572 rargv[ 0 ] = "rewriteContext";
573 rargv[ 1 ] = "searchFilter";
575 rewrite_parse( info, "<suffix massage>", 6, 2, rargv );
577 #if 0 /* matched is not normalized */
578 rargv[ 0 ] = "rewriteContext";
579 rargv[ 1 ] = "matchedDn";
580 rargv[ 2 ] = "alias";
581 rargv[ 3 ] = "searchResult";
583 rewrite_parse( info, "<suffix massage>", 7, 4, rargv );
584 #else /* normalize matched */
585 rargv[ 0 ] = "rewriteContext";
586 rargv[ 1 ] = "matchedDn";
588 rewrite_parse( info, "<suffix massage>", 7, 2, rargv );
590 rargv[ 0 ] = "rewriteRule";
591 rargv[ 1 ] = suffix_massage_regexize( argv[ 2 ] );
592 rargv[ 2 ] = suffix_massage_patternize( argv[ 1 ], 1 );
595 rewrite_parse( info, "<suffix massage>", 8, 4, rargv );
596 ch_free( rargv[ 1 ] );
597 ch_free( rargv[ 2 ] );
598 #endif /* normalize matched */
602 #endif /* ENABLE_REWRITE */