2 * Copyright 1998-2003 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 "../../../libraries/libldap/ldap-int.h"
77 #include "back-meta.h"
79 static struct metatarget *
82 struct metatarget *lt;
83 struct ldapmapping *mapping;
85 lt = ch_calloc( sizeof( struct metatarget ), 1 );
90 lt->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
91 if ( lt->rwinfo == NULL ) {
96 ldap_back_map_init( <->at_map, &mapping );
110 struct metainfo *li = ( struct metainfo * )be->be_private;
114 "%s: line %d: meta backend info is null!\n",
119 /* URI of server to query */
120 if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
121 int i = li->ntargets;
124 #endif /* uncomment if uri MUST be a branch of suffix */
125 LDAPURLDesc *ludp, *tmpludp;
131 "%s: line %d: missing address"
132 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
139 li->targets = ch_realloc( li->targets,
140 sizeof( struct metatarget *)*li->ntargets );
141 if ( li->targets == NULL ) {
143 "%s: line %d: out of memory while storing server name"
144 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
149 if ( ( li->targets[ i ] = new_target() ) == NULL ) {
151 "%s: line %d: unable to init server"
152 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
160 if ( ldap_url_parselist_ext( &ludp, argv[ 1 ], "\t" ) != LDAP_SUCCESS ) {
162 "%s: line %d: unable to parse URI"
163 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
169 * uri MUST have the <dn> part!
171 if ( ludp->lud_dn == NULL || ludp->lud_dn[ 0 ] == '\0' ) {
173 "%s: line %d: missing <naming context> "
174 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
180 * copies and stores uri and suffix
182 dn.bv_val = ludp->lud_dn;
183 dn.bv_len = strlen( ludp->lud_dn );
185 rc = dnPrettyNormal( NULL, &dn, &li->targets[ i ]->psuffix,
186 &li->targets[ i ]->suffix );
187 if( rc != LDAP_SUCCESS ) {
188 fprintf( stderr, "%s: line %d: "
189 "target '%s' DN is invalid\n",
190 fname, lineno, argv[ 1 ] );
194 ludp->lud_dn[ 0 ] = '\0';
196 for ( tmpludp = ludp->lud_next; tmpludp; tmpludp = tmpludp->lud_next ) {
197 if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
198 fprintf( stderr, "%s: line %d: "
199 "multiple URIs must have no DN part\n",
200 fname, lineno, argv[ 1 ] );
206 li->targets[ i ]->uri = ldap_url_list2urls( ludp );
207 ldap_free_urllist( ludp );
208 if ( li->targets[ i ]->uri == NULL) {
209 fprintf( stderr, "%s: line %d: no memory?\n",
215 * uri MUST be a branch of suffix!
217 #if 0 /* too strict a constraint */
218 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) != be ) {
220 "%s: line %d: <naming context> of URI does not refer to current backend"
221 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
227 * uri MUST be a branch of a suffix!
229 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) == NULL ) {
231 "%s: line %d: <naming context> of URI does not resolve to a backend"
232 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
240 * uri MUST not be used by other URIs!
242 * FIXME: this limitation may be removed,
243 * or worked out, at least, in some manner
245 for ( j = 0; j < i-1; j++ ) {
246 if ( dn_match( &li->targets[ i ]->suffix,
247 &li->targets[ j ]->suffix ) ) {
249 "%s: line %d: naming context \"%s\" already used"
250 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
251 fname, lineno, last+1 );
258 fprintf(stderr, "%s: line %d: URI \"%s\", suffix \"%s\"\n",
259 fname, lineno, li->targets[ i ]->uri,
260 li->targets[ i ]->psuffix.bv_val );
263 /* default target directive */
264 } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
265 int i = li->ntargets-1;
270 "%s: line %d: \"default-target\" alone need be"
271 " inside a \"uri\" directive\n",
275 li->defaulttarget = i;
277 if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
280 "%s: line %d: \"default-target none\""
281 " should go before uri definitions\n",
284 li->defaulttarget = META_DEFAULT_TARGET_NONE;
286 int n = atoi( argv[ 1 ] );
287 if ( n < 1 || n >= i ) {
289 "%s: line %d: illegal target number %d\n",
293 li->defaulttarget = n-1;
297 /* ttl of dn cache */
298 } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
301 "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
306 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
307 li->cache.ttl = META_DNCACHE_FOREVER;
308 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
309 li->cache.ttl = META_DNCACHE_DISABLED;
311 li->cache.ttl = atol( argv[ 1 ] );
314 /* name to use for meta_back_group */
315 } else if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
316 int i = li->ntargets-1;
321 "%s: line %d: need \"uri\" directive first\n",
327 "%s: line %d: missing name in \"binddn <name>\" line\n",
332 dn.bv_val = argv[ 1 ];
333 dn.bv_len = strlen( argv[ 1 ] );
334 if ( dnNormalize2( NULL, &dn, &li->targets[ i ]->binddn ) != LDAP_SUCCESS ) {
335 fprintf( stderr, "%s: line %d: "
336 "bind DN '%s' is invalid\n",
337 fname, lineno, argv[ 1 ] );
341 /* password to use for meta_back_group */
342 } else if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
343 int i = li->ntargets-1;
347 "%s: line %d: need \"uri\" directive first\n",
353 "%s: line %d: missing password in \"bindpw <password>\" line\n",
357 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->bindpw );
359 /* save bind creds for referral rebinds? */
360 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
363 "%s: line %d: rebind-as-user takes no arguments\n",
369 /* name to use as pseudo-root dn */
370 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
371 int i = li->ntargets-1;
376 "%s: line %d: need \"uri\" directive first\n",
382 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
387 dn.bv_val = argv[ 1 ];
388 dn.bv_len = strlen( argv[ 1 ] );
389 if ( dnNormalize2( NULL, &dn, &li->targets[ i ]->pseudorootdn ) != LDAP_SUCCESS ) {
390 fprintf( stderr, "%s: line %d: "
391 "pseudoroot DN '%s' is invalid\n",
392 fname, lineno, argv[ 1 ] );
396 /* password to use as pseudo-root */
397 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
398 int i = li->ntargets-1;
402 "%s: line %d: need \"uri\" directive first\n",
408 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
412 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->pseudorootpw );
415 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
417 int i = li->ntargets-1;
418 struct berval dn, nvnc, pvnc, nrnc, prnc;
422 "%s: line %d: need \"uri\" directive first\n",
430 * suffixmassage <suffix> <massaged suffix>
432 * the <suffix> field must be defined as a valid suffix
433 * (or suffixAlias?) for the current database;
434 * the <massaged suffix> shouldn't have already been
435 * defined as a valid suffix or suffixAlias for the
440 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
445 dn.bv_val = argv[ 1 ];
446 dn.bv_len = strlen( argv[ 1 ] );
447 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc ) != LDAP_SUCCESS ) {
448 fprintf( stderr, "%s: line %d: "
449 "suffix '%s' is invalid\n",
450 fname, lineno, argv[ 1 ] );
454 tmp_be = select_backend( &nvnc, 0, 0 );
455 if ( tmp_be != NULL && tmp_be != be ) {
457 "%s: line %d: suffix already in use by another backend in"
458 " \"suffixMassage <suffix> <massaged suffix>\"\n",
465 dn.bv_val = argv[ 2 ];
466 dn.bv_len = strlen( argv[ 2 ] );
467 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc ) != LDAP_SUCCESS ) {
468 fprintf( stderr, "%s: line %d: "
469 "massaged suffix '%s' is invalid\n",
470 fname, lineno, argv[ 2 ] );
477 tmp_be = select_backend( &nrnc, 0, 0 );
478 if ( tmp_be != NULL ) {
480 "%s: line %d: massaged suffix already in use by another backend in"
481 " \"suffixMassage <suffix> <massaged suffix>\"\n",
492 * The suffix massaging is emulated by means of the
493 * rewrite capabilities
494 * FIXME: no extra rewrite capabilities should be added
497 return suffix_massage_config( li->targets[ i ]->rwinfo,
498 &pvnc, &nvnc, &prnc, &nrnc );
500 /* rewrite stuff ... */
501 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
502 int i = li->ntargets-1;
506 "%s: line %d: need \"uri\" directive first\n",
510 return rewrite_parse( li->targets[ i ]->rwinfo, fname, lineno,
513 /* objectclass/attribute mapping */
514 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
516 struct ldapmapping *mapping;
518 int i = li->ntargets-1;
522 "%s: line %d: need \"uri\" directive first\n",
527 if ( argc < 3 || argc > 4 ) {
529 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
534 if ( strcasecmp( argv[ 1 ], "objectClass" ) == 0 ) {
535 map = &li->targets[ i ]->oc_map;
536 } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
537 map = &li->targets[ i ]->at_map;
540 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
545 if ( strcmp( argv[ 2 ], "*" ) == 0 ) {
546 if ( argc < 4 || strcmp( argv[ 3 ], "*" ) == 0 ) {
547 map->drop_missing = ( argc < 4 );
550 src = dst = argv[ 3 ];
551 } else if ( argc < 4 ) {
556 dst = ( strcmp( argv[ 3 ], "*" ) == 0 ? src : argv[ 3 ] );
559 if ( ( map == &li->targets[ i ]->at_map )
560 && ( strcasecmp( src, "objectclass" ) == 0
561 || strcasecmp( dst, "objectclass" ) == 0 ) ) {
563 "%s: line %d: objectclass attribute cannot be mapped\n",
567 mapping = ch_calloc( 2, sizeof( struct ldapmapping ) );
568 if ( mapping == NULL ) {
570 "%s: line %d: out of memory\n",
574 ber_str2bv( src, 0, 1, &mapping->src );
575 ber_str2bv( dst, 0, 1, &mapping->dst );
576 mapping[ 1 ].src = mapping->dst;
577 mapping[ 1 ].dst = mapping->src;
579 if ( (*src != '\0' &&
580 avl_find( map->map, ( caddr_t )mapping,
581 mapping_cmp ) != NULL)
582 || avl_find( map->remap, ( caddr_t )&mapping[ 1 ],
583 mapping_cmp ) != NULL) {
585 "%s: line %d: duplicate mapping found (ignored)\n",
591 avl_insert( &map->map, ( caddr_t )mapping,
592 mapping_cmp, mapping_dup );
593 avl_insert( &map->remap, ( caddr_t )&mapping[ 1 ],
594 mapping_cmp, mapping_dup );
599 "%s: line %d: unknown directive \"%s\" in meta database definition"
601 fname, lineno, argv[0] );