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 char *dn, *massaged_dn;
118 #endif /* ENABLE_REWRITE */
124 * suffixmassage <suffix> <massaged suffix>
126 * the <suffix> field must be defined as a valid suffix
127 * (or suffixAlias?) for the current database;
128 * the <massaged suffix> shouldn't have already been
129 * defined as a valid suffix or suffixAlias for the
133 fprintf( stderr, "%s: line %d: syntax is"
134 " \"suffixMassage <suffix>"
135 " <massaged suffix>\"\n",
140 tmp_be = select_backend( argv[1], 0 );
141 if ( tmp_be != NULL && tmp_be != be ) {
142 fprintf( stderr, "%s: line %d: suffix already in use"
143 " by another backend in"
144 " \"suffixMassage <suffix>"
145 " <massaged suffix>\"\n",
150 tmp_be = select_backend( argv[2], 0 );
151 if ( tmp_be != NULL ) {
152 fprintf( stderr, "%s: line %d: massaged suffix"
153 " already in use by another backend in"
154 " \"suffixMassage <suffix>"
155 " <massaged suffix>\"\n",
160 #ifdef ENABLE_REWRITE
162 * The suffix massaging is emulated by means of the
163 * rewrite capabilities
164 * FIXME: no extra rewrite capabilities should be added
167 return suffix_massage_config( li->rwinfo, argc, argv );
168 #else /* !ENABLE_REWRITE */
169 dn = ch_strdup( argv[1] );
170 charray_add( &li->suffix_massage, dn );
171 (void) dn_normalize( dn );
172 charray_add( &li->suffix_massage, dn );
174 massaged_dn = ch_strdup( argv[2] );
175 charray_add( &li->suffix_massage, massaged_dn );
176 (void) dn_normalize( massaged_dn );
177 charray_add( &li->suffix_massage, massaged_dn );
181 #endif /* !ENABLE_REWRITE */
183 #ifdef ENABLE_REWRITE
184 /* rewrite stuff ... */
185 } else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
186 return rewrite_parse( li->rwinfo, fname, lineno, argc, argv );
187 #endif /* ENABLE_REWRITE */
189 /* objectclass/attribute mapping */
190 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
192 struct ldapmapping *mapping;
195 if ( argc < 3 || argc > 4 ) {
197 "%s: line %d: syntax is \"map {objectclass | attribute} {<source> | *} [<dest> | *]\"\n",
202 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
204 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
208 "%s: line %d: syntax is \"map {objectclass | attribute} {<source> | *} [<dest> | *]\"\n",
213 if ( strcasecmp( argv[2], "*" ) != 0 ) {
217 else if ( strcasecmp( argv[3], "*" ) == 0 )
223 map->drop_missing = 1;
226 if ( strcasecmp( argv[3], "*" ) == 0 ) {
227 map->drop_missing = 0;
235 if ( ( map == &li->at_map )
236 && ( strcasecmp( src, "objectclass" ) == 0
237 || strcasecmp( dst, "objectclass" ) == 0 ) )
240 "%s: line %d: objectclass attribute cannot be mapped\n",
244 mapping = (struct ldapmapping *)ch_calloc( 2, sizeof(struct ldapmapping) );
245 if ( mapping == NULL ) {
247 "%s: line %d: out of memory\n",
251 mapping->src = ch_strdup(src);
252 mapping->dst = ch_strdup(dst);
254 mapping[1].src = mapping->dst;
255 mapping[1].dst = mapping->src;
257 mapping[1].src = mapping->src;
258 mapping[1].dst = mapping->dst;
261 if ( avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL
262 || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
265 "%s: line %d: duplicate mapping found (ignored)\n",
270 avl_insert( &map->map, (caddr_t)mapping,
271 mapping_cmp, mapping_dup );
272 avl_insert( &map->remap, (caddr_t)&mapping[1],
273 mapping_cmp, mapping_dup );
278 "%s: line %d: unknown directive \"%s\" in ldap database definition (ignored)\n",
279 fname, lineno, argv[0] );
285 mapping_cmp ( const void *c1, const void *c2 )
287 struct ldapmapping *map1 = (struct ldapmapping *)c1;
288 struct ldapmapping *map2 = (struct ldapmapping *)c2;
290 return ( strcasecmp(map1->src, map2->src) );
294 mapping_dup ( void *c1, void *c2 )
296 struct ldapmapping *map1 = (struct ldapmapping *)c1;
297 struct ldapmapping *map2 = (struct ldapmapping *)c2;
299 return( ( strcasecmp(map1->src, map2->src) == 0 ) ? -1 : 0 );
303 ldap_back_map ( struct ldapmap *map, char *s, int remap )
306 struct ldapmapping *mapping, fmapping;
314 mapping = (struct ldapmapping *)avl_find( tree, (caddr_t)&fmapping, mapping_cmp );
315 if (mapping != NULL) {
316 if ( *mapping->dst == 0 )
318 return(mapping->dst);
321 if (map->drop_missing)
328 ldap_back_map_filter(
329 struct ldapmap *at_map,
330 struct ldapmap *oc_map,
335 char *nf, *m, *p, *q, *s, c;
336 int len, extra, plen, in_quote;
344 nf = ch_malloc( len + 1 );
348 /* this loop assumes the filter ends with one
349 * of the delimiter chars -- probably ')'.
355 for (p = f; (c = *p); p++) {
357 in_quote = !in_quote;
365 } else if (in_quote) {
366 /* ignore everything in quotes --
367 * what about attrs in DNs?
370 } else if (c != '(' && c != ')'
371 && c != '=' && c != '>' && c != '<'
372 && c != '|' && c != '&')
379 m = ldap_back_map(at_map, q, remap);
381 m = ldap_back_map(oc_map, q, remap);
394 nf = ch_realloc(nf, len + 1);
415 struct ldapmap *at_map,
426 for (count = 0; a[count] != NULL; count++) {
430 na = (char **)ch_calloc( count + 1, sizeof(char *) );
434 for (i = 0, j = 0; i < count; i++) {
435 mapped = ldap_back_map(at_map, a[i], remap);
436 if (mapped != NULL) {
437 mapped = ch_strdup(mapped);
438 if (mapped == NULL) {
449 #ifdef ENABLE_REWRITE
451 suffix_massage_regexize( const char *s )
456 for ( i = 0, p = ( char * )s;
457 ( r = strchr( p, ',' ) ) != NULL;
461 res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
463 strcpy( res, "(.*)" );
464 for ( i = 0, p = ( char * )s;
465 ( r = strchr( p, ',' ) ) != NULL;
467 strncat( res, p, r - p + 1 );
468 strcat( res, "[ ]?" );
470 if ( r[ 1 ] == ' ' ) {
480 suffix_massage_patternize( const char *s, int normalize )
484 res = ch_calloc( sizeof( char ), strlen( s ) + 3 );
486 sprintf( res, "%%1%s", s );
489 char *out = dn_normalize( res + 2 );
490 if ( out != res + 2 ) {
491 strcpy( res + 2, out );
500 suffix_massage_config(
501 struct rewrite_info *info,
508 rargv[ 0 ] = "rewriteEngine";
511 rewrite_parse( info, "<suffix massage>", 1, 2, rargv );
513 rargv[ 0 ] = "rewriteContext";
514 rargv[ 1 ] = "default";
516 rewrite_parse( info, "<suffix massage>", 2, 2, rargv );
518 rargv[ 0 ] = "rewriteRule";
519 rargv[ 1 ] = suffix_massage_regexize( argv[ 1 ] );
520 rargv[ 2 ] = suffix_massage_patternize( argv[ 2 ], 0 );
523 rewrite_parse( info, "<suffix massage>", 3, 4, rargv );
524 ch_free( rargv[ 1 ] );
525 ch_free( rargv[ 2 ] );
527 rargv[ 0 ] = "rewriteContext";
528 rargv[ 1 ] = "searchResult";
530 rewrite_parse( info, "<suffix massage>", 4, 2, rargv );
532 rargv[ 0 ] = "rewriteRule";
533 rargv[ 1 ] = suffix_massage_regexize( argv[ 2 ] );
534 rargv[ 2 ] = suffix_massage_patternize( argv[ 1 ], 0 );
537 rewrite_parse( info, "<suffix massage>", 5, 4, rargv );
538 ch_free( rargv[ 1 ] );
539 ch_free( rargv[ 2 ] );
542 * the filter should be rewritten as
545 * "(.*)member=([^)]+),o=Foo Bar,[ ]?c=US(.*)"
546 * "%1member=%2,dc=example,dc=com%3"
548 * where "o=Foo Bar, c=US" is the virtual naming context,
549 * and "dc=example, dc=com" is the real naming context
551 rargv[ 0 ] = "rewriteContext";
552 rargv[ 1 ] = "searchFilter";
554 rewrite_parse( info, "<suffix massage>", 6, 2, rargv );
556 #if 0 /* matched is not normalized */
557 rargv[ 0 ] = "rewriteContext";
558 rargv[ 1 ] = "matchedDn";
559 rargv[ 2 ] = "alias";
560 rargv[ 3 ] = "searchResult";
562 rewrite_parse( info, "<suffix massage>", 7, 4, rargv );
563 #else /* normalize matched */
564 rargv[ 0 ] = "rewriteContext";
565 rargv[ 1 ] = "matchedDn";
567 rewrite_parse( info, "<suffix massage>", 7, 2, rargv );
569 rargv[ 0 ] = "rewriteRule";
570 rargv[ 1 ] = suffix_massage_regexize( argv[ 2 ] );
571 rargv[ 2 ] = suffix_massage_patternize( argv[ 1 ], 1 );
574 rewrite_parse( info, "<suffix massage>", 8, 4, rargv );
575 ch_free( rargv[ 1 ] );
576 ch_free( rargv[ 2 ] );
577 #endif /* normalize matched */
581 #endif /* ENABLE_REWRITE */