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"
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]);
114 /* save bind creds for referral rebinds? */
115 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
118 "%s: line %d: rebind-as-user takes no arguments\n",
125 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
127 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
128 #ifdef ENABLE_REWRITE
130 #endif /* ENABLE_REWRITE */
135 * suffixmassage <suffix> <massaged suffix>
137 * the <suffix> field must be defined as a valid suffix
138 * (or suffixAlias?) for the current database;
139 * the <massaged suffix> shouldn't have already been
140 * defined as a valid suffix or suffixAlias for the
144 fprintf( stderr, "%s: line %d: syntax is"
145 " \"suffixMassage <suffix>"
146 " <massaged suffix>\"\n",
151 ber_str2bv( argv[1], 0, 0, &bvnc );
152 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc ) != LDAP_SUCCESS ) {
153 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
154 fname, lineno, bvnc.bv_val );
157 tmp_be = select_backend( &nvnc, 0, 0 );
158 if ( tmp_be != NULL && tmp_be != be ) {
159 fprintf( stderr, "%s: line %d: suffix already in use"
160 " by another backend in"
161 " \"suffixMassage <suffix>"
162 " <massaged suffix>\"\n",
169 ber_str2bv( argv[2], 0, 0, &brnc );
170 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc ) != LDAP_SUCCESS ) {
171 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
172 fname, lineno, brnc.bv_val );
179 tmp_be = select_backend( &nrnc, 0, 0 );
180 if ( tmp_be != NULL ) {
181 fprintf( stderr, "%s: line %d: massaged suffix"
182 " already in use by another backend in"
183 " \"suffixMassage <suffix>"
184 " <massaged suffix>\"\n",
194 #ifdef ENABLE_REWRITE
196 * The suffix massaging is emulated by means of the
197 * rewrite capabilities
198 * FIXME: no extra rewrite capabilities should be added
201 rc = suffix_massage_config( li->rwinfo, &pvnc, &nvnc, &prnc, &nrnc );
209 #else /* !ENABLE_REWRITE */
210 ber_bvarray_add( &li->suffix_massage, &pvnc );
211 ber_bvarray_add( &li->suffix_massage, &nvnc );
213 ber_bvarray_add( &li->suffix_massage, &prnc );
214 ber_bvarray_add( &li->suffix_massage, &nrnc );
215 #endif /* !ENABLE_REWRITE */
217 /* rewrite stuff ... */
218 } else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
219 #ifdef ENABLE_REWRITE
220 return rewrite_parse( li->rwinfo, fname, lineno, argc, argv );
222 #else /* !ENABLE_REWRITE */
223 fprintf( stderr, "%s: line %d: rewrite capabilities "
224 "are not enabled\n", fname, lineno );
225 #endif /* !ENABLE_REWRITE */
227 /* objectclass/attribute mapping */
228 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
230 struct ldapmapping *mapping;
233 if ( argc < 3 || argc > 4 ) {
235 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
240 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
242 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
245 fprintf( stderr, "%s: line %d: syntax is "
246 "\"map {objectclass | attribute} [<local> | *] "
247 "{<foreign> | *}\"\n",
252 if ( strcmp( argv[2], "*" ) == 0 ) {
253 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
254 map->drop_missing = ( argc < 4 );
258 } else if ( argc < 4 ) {
263 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
266 if ( ( map == &li->at_map )
267 && ( strcasecmp( src, "objectclass" ) == 0
268 || strcasecmp( dst, "objectclass" ) == 0 ) )
271 "%s: line %d: objectclass attribute cannot be mapped\n",
275 mapping = (struct ldapmapping *)ch_calloc( 2,
276 sizeof(struct ldapmapping) );
277 if ( mapping == NULL ) {
279 "%s: line %d: out of memory\n",
283 ber_str2bv( src, 0, 1, &mapping->src );
284 ber_str2bv( dst, 0, 1, &mapping->dst );
285 mapping[1].src = mapping->dst;
286 mapping[1].dst = mapping->src;
288 if ( (*src != '\0' &&
289 avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL) ||
290 avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
293 "%s: line %d: duplicate mapping found (ignored)\n",
299 avl_insert( &map->map, (caddr_t)mapping,
300 mapping_cmp, mapping_dup );
301 avl_insert( &map->remap, (caddr_t)&mapping[1],
302 mapping_cmp, mapping_dup );
306 fprintf( stderr, "%s: line %d: unknown directive \"%s\" "
307 "in ldap database definition (ignored)\n",
308 fname, lineno, argv[0] );
313 #ifdef ENABLE_REWRITE
315 suffix_massage_regexize( const char *s )
322 ( r = strchr( p, ',' ) ) != NULL;
326 res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
328 ptr = slap_strcopy( res, "(.*)" );
330 ( r = strchr( p, ',' ) ) != NULL;
332 ptr = slap_strncopy( ptr, p, r - p + 1 );
333 ptr = slap_strcopy( ptr, "[ ]?" );
335 if ( r[ 1 ] == ' ' ) {
339 slap_strcopy( ptr, p );
345 suffix_massage_patternize( const char *s )
352 res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
358 strcpy( res + sizeof( "%1" ) - 1, s );
364 suffix_massage_config(
365 struct rewrite_info *info,
375 rargv[ 0 ] = "rewriteEngine";
378 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
380 rargv[ 0 ] = "rewriteContext";
381 rargv[ 1 ] = "default";
383 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
385 rargv[ 0 ] = "rewriteRule";
386 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
387 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
390 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
391 ch_free( rargv[ 1 ] );
392 ch_free( rargv[ 2 ] );
394 rargv[ 0 ] = "rewriteContext";
395 rargv[ 1 ] = "searchResult";
397 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
399 rargv[ 0 ] = "rewriteRule";
400 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
401 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
404 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
405 ch_free( rargv[ 1 ] );
406 ch_free( rargv[ 2 ] );
409 * the filter should be rewritten as
412 * "(.*)member=([^)]+),o=Foo Bar,[ ]?c=US(.*)"
413 * "%1member=%2,dc=example,dc=com%3"
415 * where "o=Foo Bar, c=US" is the virtual naming context,
416 * and "dc=example, dc=com" is the real naming context
418 rargv[ 0 ] = "rewriteContext";
419 rargv[ 1 ] = "searchFilter";
421 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
423 #if 1 /* rewrite filters */
426 * Note: this is far more optimistic than desirable:
427 * for any AVA value ending with the virtual naming
428 * context the terminal part will be replaced by the
429 * real naming context; a better solution would be to
430 * walk the filter looking for DN-valued attributes,
431 * and only rewrite those that require rewriting
433 char vbuf[LDAP_FILT_MAXSIZ], rbuf[LDAP_FILT_MAXSIZ];
435 snprintf( vbuf, sizeof( vbuf ), "(.*)%s\\)(.*)", nvnc->bv_val );
436 snprintf( rbuf, sizeof( rbuf ), "%%1%s)%%2", nrnc->bv_val );
438 rargv[ 0 ] = "rewriteRule";
443 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
445 #endif /* rewrite filters */
447 #if 0 /* "matched" is not normalized */
448 rargv[ 0 ] = "rewriteContext";
449 rargv[ 1 ] = "matchedDn";
450 rargv[ 2 ] = "alias";
451 rargv[ 3 ] = "searchResult";
453 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
454 #else /* normalize "matched" */
455 rargv[ 0 ] = "rewriteContext";
456 rargv[ 1 ] = "matchedDn";
458 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
460 rargv[ 0 ] = "rewriteRule";
461 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
462 rargv[ 2 ] = suffix_massage_patternize( nvnc->bv_val );
465 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
466 ch_free( rargv[ 1 ] );
467 ch_free( rargv[ 2 ] );
468 #endif /* normalize "matched" */
472 #endif /* ENABLE_REWRITE */