2 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5 * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
7 * This work has been developed to fulfill the requirements
8 * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
9 * to the OpenLDAP Foundation in the hope that it may be useful
10 * to the Open Source community, but WITHOUT ANY WARRANTY.
12 * Permission is granted to anyone to use this software for any purpose
13 * on any computer system, and to alter it and redistribute it, subject
14 * to the following restrictions:
16 * 1. The author and SysNet s.n.c. are not responsible for the consequences
17 * of use of this software, no matter how awful, even if they arise from
20 * 2. The origin of this software must not be misrepresented, either by
21 * explicit claim or by omission. Since few users ever read sources,
22 * credits should appear in the documentation.
24 * 3. Altered versions must be plainly marked as such, and must not be
25 * misrepresented as being the original software. Since few users
26 * ever read sources, credits should appear in the documentation.
27 * SysNet s.n.c. cannot be responsible for the consequences of the
30 * 4. This notice may not be removed or altered.
33 * This software is based on the backend back-ldap, implemented
34 * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
35 * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
36 * contributors. The contribution of the original software to the present
37 * implementation is acknowledged in this copyright statement.
39 * A special acknowledgement goes to Howard for the overall architecture
40 * (and for borrowing large pieces of code), and to Mark, who implemented
41 * from scratch the attribute/objectclass mapping.
43 * The original copyright statement follows.
45 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
47 * Permission is granted to anyone to use this software for any purpose
48 * on any computer system, and to alter it and redistribute it, subject
49 * to the following restrictions:
51 * 1. The author is not responsible for the consequences of use of this
52 * software, no matter how awful, even if they arise from flaws in it.
54 * 2. The origin of this software must not be misrepresented, either by
55 * explicit claim or by omission. Since few users ever read sources,
56 * credits should appear in the documentation.
58 * 3. Altered versions must be plainly marked as such, and must not be
59 * misrepresented as being the original software. Since few users
60 * ever read sources, credits should appear in the
63 * 4. This notice may not be removed or altered.
71 #include <ac/string.h>
72 #include <ac/socket.h>
75 #include "../back-ldap/back-ldap.h"
76 #include "back-meta.h"
79 suffix_massage_config(
80 struct rewrite_info *info,
85 static struct metatarget *
88 struct metatarget *lt;
89 struct ldapmapping *mapping;
91 lt = ch_calloc( sizeof( struct metatarget ), 1 );
96 lt->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
97 if ( lt->rwinfo == NULL ) {
102 ldap_back_map_init( <->at_map, &mapping );
116 struct metainfo *li = ( struct metainfo * )be->be_private;
120 "%s: line %d: meta backend info is null!\n",
125 /* URI of server to query */
126 if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
127 int i = li->ntargets;
130 #endif /* uncomment if uri MUST be a branch of suffix */
138 "%s: line %d: missing address"
139 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
146 li->targets = ch_realloc( li->targets,
147 sizeof( struct metatarget *)*li->ntargets );
148 if ( li->targets == NULL ) {
150 "%s: line %d: out of memory while storing server name"
151 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
156 if ( ( li->targets[ i ] = new_target() ) == NULL ) {
158 "%s: line %d: unable to init server"
159 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
167 if ( ldap_url_parse( argv[ 1 ], &ludp ) != LDAP_SUCCESS ) {
169 "%s: line %d: unable to parse URI"
170 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
176 * uri MUST have the <dn> part!
178 if ( ludp->lud_dn == NULL || ludp->lud_dn[ 0 ] == '\0' ) {
180 "%s: line %d: missing <naming context> "
181 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
187 * copies and stores uri and suffix
189 dn.bv_val = ludp->lud_dn;
190 dn.bv_len = strlen( ludp->lud_dn );
192 rc = dnPrettyNormal( NULL, &dn, &li->targets[ i ]->psuffix,
193 &li->targets[ i ]->suffix );
194 if( rc != LDAP_SUCCESS ) {
195 fprintf( stderr, "%s: line %d: "
196 "target '%s' DN is invalid\n",
197 fname, lineno, argv[ 1 ] );
201 li->targets[ i ]->uri = ch_strdup( argv[ 1 ] );
202 last = strstr( li->targets[ i ]->uri, ludp->lud_dn );
203 assert( last != NULL );
207 * uri MUST be a branch of suffix!
209 #if 0 /* too strict a constraint */
210 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) != be ) {
212 "%s: line %d: <naming context> of URI does not refer to current backend"
213 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
219 * uri MUST be a branch of a suffix!
221 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) == NULL ) {
223 "%s: line %d: <naming context> of URI does not resolve to a backend"
224 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
232 * uri MUST not be used by other URIs!
234 * FIXME: this limitation may be removed,
235 * or worked out, at least, in some manner
237 for ( j = 0; j < i-1; j++ ) {
238 if ( dn_match( &li->targets[ i ]->suffix,
239 &li->targets[ j ]->suffix ) ) {
241 "%s: line %d: naming context \"%s\" already used"
242 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
243 fname, lineno, last+1 );
249 ldap_free_urldesc( ludp );
252 fprintf(stderr, "%s: line %d: URI \"%s\", suffix \"%s\"\n",
253 fname, lineno, li->targets[ i ]->uri,
254 li->targets[ i ]->psuffix.bv_val );
257 /* default target directive */
258 } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
259 int i = li->ntargets-1;
264 "%s: line %d: \"default-target\" alone need be"
265 " inside a \"uri\" directive\n",
269 li->defaulttarget = i;
271 if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
274 "%s: line %d: \"default-target none\""
275 " should go before uri definitions\n",
278 li->defaulttarget = META_DEFAULT_TARGET_NONE;
280 int n = atoi( argv[ 1 ] );
281 if ( n < 1 || n >= i ) {
283 "%s: line %d: illegal target number %d\n",
287 li->defaulttarget = n-1;
291 /* ttl of dn cache */
292 } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
295 "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
300 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
301 li->cache.ttl = META_DNCACHE_FOREVER;
302 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
303 li->cache.ttl = META_DNCACHE_DISABLED;
305 li->cache.ttl = atol( argv[ 1 ] );
308 /* name to use for meta_back_group */
309 } else if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
310 int i = li->ntargets-1;
315 "%s: line %d: need \"uri\" directive first\n",
321 "%s: line %d: missing name in \"binddn <name>\" line\n",
326 dn.bv_val = argv[ 1 ];
327 dn.bv_len = strlen( argv[ 1 ] );
328 if ( dnNormalize2( NULL, &dn, &li->targets[ i ]->binddn ) != LDAP_SUCCESS ) {
329 fprintf( stderr, "%s: line %d: "
330 "bind DN '%s' is invalid\n",
331 fname, lineno, argv[ 1 ] );
335 /* password to use for meta_back_group */
336 } else if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
337 int i = li->ntargets-1;
341 "%s: line %d: need \"uri\" directive first\n",
347 "%s: line %d: missing password in \"bindpw <password>\" line\n",
351 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->bindpw );
353 /* name to use as pseudo-root dn */
354 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
355 int i = li->ntargets-1;
360 "%s: line %d: need \"uri\" directive first\n",
366 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
371 dn.bv_val = argv[ 1 ];
372 dn.bv_len = strlen( argv[ 1 ] );
373 if ( dnNormalize2( NULL, &dn, &li->targets[ i ]->pseudorootdn ) != LDAP_SUCCESS ) {
374 fprintf( stderr, "%s: line %d: "
375 "pseudoroot DN '%s' is invalid\n",
376 fname, lineno, argv[ 1 ] );
380 /* password to use as pseudo-root */
381 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
382 int i = li->ntargets-1;
386 "%s: line %d: need \"uri\" directive first\n",
392 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
396 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->pseudorootpw );
399 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
401 int i = li->ntargets-1;
402 struct berval dn, ndn;
406 "%s: line %d: need \"uri\" directive first\n",
414 * suffixmassage <suffix> <massaged suffix>
416 * the <suffix> field must be defined as a valid suffix
417 * (or suffixAlias?) for the current database;
418 * the <massaged suffix> shouldn't have already been
419 * defined as a valid suffix or suffixAlias for the
424 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
429 dn.bv_val = argv[ 1 ];
430 dn.bv_len = strlen( argv[ 1 ] );
431 if ( dnNormalize2( NULL, &dn, &ndn ) != LDAP_SUCCESS ) {
432 fprintf( stderr, "%s: line %d: "
433 "suffix '%s' is invalid\n",
434 fname, lineno, argv[ 1 ] );
438 tmp_be = select_backend( &ndn, 0, 0 );
440 if ( tmp_be != NULL && tmp_be != be ) {
442 "%s: line %d: suffix already in use by another backend in"
443 " \"suffixMassage <suffix> <massaged suffix>\"\n",
448 dn.bv_val = argv[ 2 ];
449 dn.bv_len = strlen( argv[ 2 ] );
450 if ( dnNormalize2( NULL, &dn, &ndn ) != LDAP_SUCCESS ) {
451 fprintf( stderr, "%s: line %d: "
452 "massaged suffix '%s' is invalid\n",
453 fname, lineno, argv[ 2 ] );
457 tmp_be = select_backend( &ndn, 0, 0 );
459 if ( tmp_be != NULL ) {
461 "%s: line %d: massaged suffix already in use by another backend in"
462 " \"suffixMassage <suffix> <massaged suffix>\"\n",
468 * The suffix massaging is emulated by means of the
469 * rewrite capabilities
470 * FIXME: no extra rewrite capabilities should be added
473 return suffix_massage_config( li->targets[ i ]->rwinfo,
476 /* rewrite stuff ... */
477 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
478 int i = li->ntargets-1;
482 "%s: line %d: need \"uri\" directive first\n",
486 return rewrite_parse( li->targets[ i ]->rwinfo, fname, lineno,
489 /* objectclass/attribute mapping */
490 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
492 struct ldapmapping *mapping;
494 int i = li->ntargets-1;
498 "%s: line %d: need \"uri\" directive first\n",
503 if ( argc < 3 || argc > 4 ) {
505 "%s: line %d: syntax is \"map {objectclass | attribute} {<source> | *} [<dest> | *]\"\n",
510 if ( strcasecmp( argv[ 1 ], "objectClass" ) == 0 ) {
511 map = &li->targets[ i ]->oc_map;
512 } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
513 map = &li->targets[ i ]->at_map;
516 "%s: line %d: syntax is \"map {objectclass | attribute} {<source> | *} [<dest> | *]\"\n",
521 if ( strcasecmp( argv[ 2 ], "*" ) != 0 ) {
525 } else if ( strcasecmp( argv[ 3 ], "*" ) == 0 ) {
532 map->drop_missing = 1;
535 if ( strcasecmp( argv[ 3 ], "*" ) == 0 ) {
536 map->drop_missing = 0;
544 if ( ( map == &li->targets[ i ]->at_map )
545 && ( strcasecmp( src, "objectclass" ) == 0
546 || strcasecmp( dst, "objectclass" ) == 0 ) ) {
548 "%s: line %d: objectclass attribute cannot be mapped\n",
552 mapping = ch_calloc( 2, sizeof( struct ldapmapping ) );
553 if ( mapping == NULL ) {
555 "%s: line %d: out of memory\n",
559 ber_str2bv( src, 0, 1, &mapping->src );
560 ber_str2bv( dst, 0, 1, &mapping->dst );
562 mapping[ 1 ].src = mapping->dst;
563 mapping[ 1 ].dst = mapping->src;
565 mapping[ 1 ].src = mapping->src;
566 mapping[ 1 ].dst = mapping->dst;
569 if ( avl_find( map->map, ( caddr_t )mapping,
570 mapping_cmp ) != NULL
571 || avl_find( map->remap, ( caddr_t )&mapping[ 1 ],
572 mapping_cmp ) != NULL) {
574 "%s: line %d: duplicate mapping found (ignored)\n",
579 avl_insert( &map->map, ( caddr_t )mapping,
580 mapping_cmp, mapping_dup );
581 avl_insert( &map->remap, ( caddr_t )&mapping[ 1 ],
582 mapping_cmp, mapping_dup );
587 "%s: line %d: unknown directive \"%s\" in meta database definition"
589 fname, lineno, argv[0] );