1 /* config.c - ldap backend configuration file routine */
4 * Copyright 1998-2002 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"
58 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
61 fprintf( stderr, "%s: line %d: ldap backend info is null!\n",
66 /* server address to query (depricated, use "uri" directive) */
67 if ( strcasecmp( argv[0], "server" ) == 0 ) {
70 "%s: line %d: missing address in \"server <address>\" line\n",
76 li->url = ch_calloc(strlen(argv[1]) + 9, sizeof(char));
77 if (li->url != NULL) {
78 strcpy(li->url, "ldap://");
79 strcat(li->url, argv[1]);
83 /* URI of server to query (preferred over "server" directive) */
84 } else if ( strcasecmp( argv[0], "uri" ) == 0 ) {
87 "%s: line %d: missing address in \"uri <address>\" line\n",
93 li->url = ch_strdup(argv[1]);
95 /* name to use for ldap_back_group */
96 } else if ( strcasecmp( argv[0], "binddn" ) == 0 ) {
99 "%s: line %d: missing name in \"binddn <name>\" line\n",
103 li->binddn = ch_strdup(argv[1]);
105 /* password to use for ldap_back_group */
106 } else if ( strcasecmp( argv[0], "bindpw" ) == 0 ) {
109 "%s: line %d: missing password in \"bindpw <password>\" line\n",
113 li->bindpw = ch_strdup(argv[1]);
115 /* save bind creds for referral rebinds? */
116 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
119 "%s: line %d: rebind-as-user takes no arguments\n",
126 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
128 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
129 #ifdef ENABLE_REWRITE
131 #endif /* ENABLE_REWRITE */
136 * suffixmassage <suffix> <massaged suffix>
138 * the <suffix> field must be defined as a valid suffix
139 * (or suffixAlias?) for the current database;
140 * the <massaged suffix> shouldn't have already been
141 * defined as a valid suffix or suffixAlias for the
145 fprintf( stderr, "%s: line %d: syntax is"
146 " \"suffixMassage <suffix>"
147 " <massaged suffix>\"\n",
152 ber_str2bv( argv[1], 0, 0, &bvnc );
153 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc ) != LDAP_SUCCESS ) {
154 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
155 fname, lineno, bvnc.bv_val );
158 tmp_be = select_backend( &nvnc, 0, 0 );
159 if ( tmp_be != NULL && tmp_be != be ) {
160 fprintf( stderr, "%s: line %d: suffix already in use"
161 " by another backend in"
162 " \"suffixMassage <suffix>"
163 " <massaged suffix>\"\n",
170 ber_str2bv( argv[2], 0, 0, &brnc );
171 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc ) != LDAP_SUCCESS ) {
172 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
173 fname, lineno, brnc.bv_val );
180 tmp_be = select_backend( &nrnc, 0, 0 );
181 if ( tmp_be != NULL ) {
182 fprintf( stderr, "%s: line %d: massaged suffix"
183 " already in use by another backend in"
184 " \"suffixMassage <suffix>"
185 " <massaged suffix>\"\n",
195 #ifdef ENABLE_REWRITE
197 * The suffix massaging is emulated by means of the
198 * rewrite capabilities
199 * FIXME: no extra rewrite capabilities should be added
202 rc = suffix_massage_config( li->rwinfo, &pvnc, &nvnc, &prnc, &nrnc );
210 #else /* !ENABLE_REWRITE */
211 ber_bvarray_add( &li->suffix_massage, &pvnc );
212 ber_bvarray_add( &li->suffix_massage, &nvnc );
214 ber_bvarray_add( &li->suffix_massage, &prnc );
215 ber_bvarray_add( &li->suffix_massage, &nrnc );
216 #endif /* !ENABLE_REWRITE */
218 /* rewrite stuff ... */
219 } else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
220 #ifdef ENABLE_REWRITE
221 return rewrite_parse( li->rwinfo, fname, lineno, argc, argv );
223 #else /* !ENABLE_REWRITE */
224 fprintf( stderr, "%s: line %d: rewrite capabilities "
225 "are not enabled\n", fname, lineno );
226 #endif /* !ENABLE_REWRITE */
228 /* objectclass/attribute mapping */
229 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
231 struct ldapmapping *mapping;
234 if ( argc < 3 || argc > 4 ) {
236 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
241 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
243 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
246 fprintf( stderr, "%s: line %d: syntax is "
247 "\"map {objectclass | attribute} [<local> | *] "
248 "{<foreign> | *}\"\n",
253 if ( strcmp( argv[2], "*" ) == 0 ) {
254 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
255 map->drop_missing = ( argc < 4 );
259 } else if ( argc < 4 ) {
264 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
267 if ( ( map == &li->at_map )
268 && ( strcasecmp( src, "objectclass" ) == 0
269 || strcasecmp( dst, "objectclass" ) == 0 ) )
272 "%s: line %d: objectclass attribute cannot be mapped\n",
276 mapping = (struct ldapmapping *)ch_calloc( 2,
277 sizeof(struct ldapmapping) );
278 if ( mapping == NULL ) {
280 "%s: line %d: out of memory\n",
284 ber_str2bv( src, 0, 1, &mapping->src );
285 ber_str2bv( dst, 0, 1, &mapping->dst );
286 mapping[1].src = mapping->dst;
287 mapping[1].dst = mapping->src;
289 if ( (*src != '\0' &&
290 avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL) ||
291 avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
294 "%s: line %d: duplicate mapping found (ignored)\n",
300 avl_insert( &map->map, (caddr_t)mapping,
301 mapping_cmp, mapping_dup );
302 avl_insert( &map->remap, (caddr_t)&mapping[1],
303 mapping_cmp, mapping_dup );
307 fprintf( stderr, "%s: line %d: unknown directive \"%s\" "
308 "in ldap database definition (ignored)\n",
309 fname, lineno, argv[0] );
314 #ifdef ENABLE_REWRITE
316 suffix_massage_regexize( const char *s )
323 ( r = strchr( p, ',' ) ) != NULL;
327 res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
329 ptr = lutil_strcopy( res, "(.*)" );
331 ( r = strchr( p, ',' ) ) != NULL;
333 ptr = lutil_strncopy( ptr, p, r - p + 1 );
334 ptr = lutil_strcopy( ptr, "[ ]?" );
336 if ( r[ 1 ] == ' ' ) {
340 lutil_strcopy( ptr, p );
346 suffix_massage_patternize( const char *s )
353 res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
359 strcpy( res + sizeof( "%1" ) - 1, s );
365 suffix_massage_config(
366 struct rewrite_info *info,
376 rargv[ 0 ] = "rewriteEngine";
379 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
381 rargv[ 0 ] = "rewriteContext";
382 rargv[ 1 ] = "default";
384 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
386 rargv[ 0 ] = "rewriteRule";
387 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
388 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
391 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
392 ch_free( rargv[ 1 ] );
393 ch_free( rargv[ 2 ] );
395 rargv[ 0 ] = "rewriteContext";
396 rargv[ 1 ] = "searchResult";
398 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
400 rargv[ 0 ] = "rewriteRule";
401 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
402 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
405 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
406 ch_free( rargv[ 1 ] );
407 ch_free( rargv[ 2 ] );
410 * the filter should be rewritten as
413 * "(.*)member=([^)]+),o=Foo Bar,[ ]?c=US(.*)"
414 * "%1member=%2,dc=example,dc=com%3"
416 * where "o=Foo Bar, c=US" is the virtual naming context,
417 * and "dc=example, dc=com" is the real naming context
419 rargv[ 0 ] = "rewriteContext";
420 rargv[ 1 ] = "searchFilter";
422 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
424 #if 1 /* rewrite filters */
427 * Note: this is far more optimistic than desirable:
428 * for any AVA value ending with the virtual naming
429 * context the terminal part will be replaced by the
430 * real naming context; a better solution would be to
431 * walk the filter looking for DN-valued attributes,
432 * and only rewrite those that require rewriting
434 char vbuf[LDAP_FILT_MAXSIZ], rbuf[LDAP_FILT_MAXSIZ];
436 snprintf( vbuf, sizeof( vbuf ), "(.*)%s\\)(.*)", nvnc->bv_val );
437 snprintf( rbuf, sizeof( rbuf ), "%%1%s)%%2", nrnc->bv_val );
439 rargv[ 0 ] = "rewriteRule";
444 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
446 #endif /* rewrite filters */
448 #if 0 /* "matched" is not normalized */
449 rargv[ 0 ] = "rewriteContext";
450 rargv[ 1 ] = "matchedDn";
451 rargv[ 2 ] = "alias";
452 rargv[ 3 ] = "searchResult";
454 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
455 #else /* normalize "matched" */
456 rargv[ 0 ] = "rewriteContext";
457 rargv[ 1 ] = "matchedDn";
459 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
461 rargv[ 0 ] = "rewriteRule";
462 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
463 rargv[ 2 ] = suffix_massage_patternize( nvnc->bv_val );
466 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
467 ch_free( rargv[ 1 ] );
468 ch_free( rargv[ 2 ] );
469 #endif /* normalize "matched" */
473 #endif /* ENABLE_REWRITE */