2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2003 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
16 * This work was initially developed by the Howard Chu for inclusion
17 * in OpenLDAP Software and subsequently enhanced by Pierangelo
20 /* This is an altered version */
22 * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
24 * This work has been developed to fulfill the requirements
25 * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
26 * to the OpenLDAP Foundation in the hope that it may be useful
27 * to the Open Source community, but WITHOUT ANY WARRANTY.
29 * Permission is granted to anyone to use this software for any purpose
30 * on any computer system, and to alter it and redistribute it, subject
31 * to the following restrictions:
33 * 1. The author and SysNet s.n.c. are not responsible for the consequences
34 * of use of this software, no matter how awful, even if they arise from
37 * 2. The origin of this software must not be misrepresented, either by
38 * explicit claim or by omission. Since few users ever read sources,
39 * credits should appear in the documentation.
41 * 3. Altered versions must be plainly marked as such, and must not be
42 * misrepresented as being the original software. Since few users
43 * ever read sources, credits should appear in the documentation.
44 * SysNet s.n.c. cannot be responsible for the consequences of the
47 * 4. This notice may not be removed or altered.
50 * This software is based on the backend back-ldap, implemented
51 * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
52 * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
53 * contributors. The contribution of the original software to the present
54 * implementation is acknowledged in this copyright statement.
56 * A special acknowledgement goes to Howard for the overall architecture
57 * (and for borrowing large pieces of code), and to Mark, who implemented
58 * from scratch the attribute/objectclass mapping.
60 * The original copyright statement follows.
62 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
64 * Permission is granted to anyone to use this software for any purpose
65 * on any computer system, and to alter it and redistribute it, subject
66 * to the following restrictions:
68 * 1. The author is not responsible for the consequences of use of this
69 * software, no matter how awful, even if they arise from flaws in it.
71 * 2. The origin of this software must not be misrepresented, either by
72 * explicit claim or by omission. Since few users ever read sources,
73 * credits should appear in the documentation.
75 * 3. Altered versions must be plainly marked as such, and must not be
76 * misrepresented as being the original software. Since few users
77 * ever read sources, credits should appear in the
80 * 4. This notice may not be removed or altered.
88 #include <ac/string.h>
89 #include <ac/socket.h>
92 #include "../back-ldap/back-ldap.h"
93 #undef ldap_debug /* silence a warning in ldap-int.h */
94 #include "../../../libraries/libldap/ldap-int.h"
95 #include "back-meta.h"
97 static struct metatarget *
100 struct metatarget *lt;
101 struct ldapmapping *mapping;
103 lt = ch_calloc( sizeof( struct metatarget ), 1 );
108 lt->rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
109 if ( lt->rwmap.rwm_rw == NULL ) {
114 ldap_back_map_init( <->rwmap.rwm_at, &mapping );
128 struct metainfo *li = ( struct metainfo * )be->be_private;
132 "%s: line %d: meta backend info is null!\n",
137 /* URI of server to query */
138 if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
139 int i = li->ntargets;
142 #endif /* uncomment if uri MUST be a branch of suffix */
143 LDAPURLDesc *ludp, *tmpludp;
149 "%s: line %d: missing address"
150 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
157 li->targets = ch_realloc( li->targets,
158 sizeof( struct metatarget *)*li->ntargets );
159 if ( li->targets == NULL ) {
161 "%s: line %d: out of memory while storing server name"
162 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
167 if ( ( li->targets[ i ] = new_target() ) == NULL ) {
169 "%s: line %d: unable to init server"
170 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
178 if ( ldap_url_parselist_ext( &ludp, argv[ 1 ], "\t" ) != LDAP_SUCCESS ) {
180 "%s: line %d: unable to parse URI"
181 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
187 * uri MUST have the <dn> part!
189 if ( ludp->lud_dn == NULL || ludp->lud_dn[ 0 ] == '\0' ) {
191 "%s: line %d: missing <naming context> "
192 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
198 * copies and stores uri and suffix
200 dn.bv_val = ludp->lud_dn;
201 dn.bv_len = strlen( ludp->lud_dn );
203 rc = dnPrettyNormal( NULL, &dn, &li->targets[ i ]->psuffix,
204 &li->targets[ i ]->suffix, NULL );
205 if( rc != LDAP_SUCCESS ) {
206 fprintf( stderr, "%s: line %d: "
207 "target '%s' DN is invalid\n",
208 fname, lineno, argv[ 1 ] );
212 ludp->lud_dn[ 0 ] = '\0';
214 for ( tmpludp = ludp->lud_next; tmpludp; tmpludp = tmpludp->lud_next ) {
215 if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
216 fprintf( stderr, "%s: line %d: "
217 "multiple URIs must have "
225 li->targets[ i ]->uri = ldap_url_list2urls( ludp );
226 ldap_free_urllist( ludp );
227 if ( li->targets[ i ]->uri == NULL) {
228 fprintf( stderr, "%s: line %d: no memory?\n",
234 * uri MUST be a branch of suffix!
236 #if 0 /* too strict a constraint */
237 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) != be ) {
239 "%s: line %d: <naming context> of URI does not refer to current backend"
240 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
246 * uri MUST be a branch of a suffix!
248 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) == NULL ) {
250 "%s: line %d: <naming context> of URI does not resolve to a backend"
251 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
259 * uri MUST not be used by other URIs!
261 * FIXME: this limitation may be removed,
262 * or worked out, at least, in some manner
264 for ( j = 0; j < i-1; j++ ) {
265 if ( dn_match( &li->targets[ i ]->suffix,
266 &li->targets[ j ]->suffix ) ) {
268 "%s: line %d: naming context \"%s\" already used"
269 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
270 fname, lineno, last+1 );
277 fprintf(stderr, "%s: line %d: URI \"%s\", suffix \"%s\"\n",
278 fname, lineno, li->targets[ i ]->uri,
279 li->targets[ i ]->psuffix.bv_val );
282 /* default target directive */
283 } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
284 int i = li->ntargets-1;
289 "%s: line %d: \"default-target\" alone need be"
290 " inside a \"uri\" directive\n",
294 li->defaulttarget = i;
296 if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
299 "%s: line %d: \"default-target none\""
300 " should go before uri definitions\n",
303 li->defaulttarget = META_DEFAULT_TARGET_NONE;
305 int n = atoi( argv[ 1 ] );
306 if ( n < 1 || n >= i ) {
308 "%s: line %d: illegal target number %d\n",
312 li->defaulttarget = n-1;
316 /* ttl of dn cache */
317 } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
320 "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
325 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
326 li->cache.ttl = META_DNCACHE_FOREVER;
327 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
328 li->cache.ttl = META_DNCACHE_DISABLED;
330 li->cache.ttl = atol( argv[ 1 ] );
333 /* network timeout when connecting to ldap servers */
334 } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
337 "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
341 li->network_timeout = atol(argv[ 1 ]);
343 /* name to use for meta_back_group */
344 } else if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
345 int i = li->ntargets-1;
350 "%s: line %d: need \"uri\" directive first\n",
357 "%s: line %d: missing name in \"binddn <name>\" line\n",
362 dn.bv_val = argv[ 1 ];
363 dn.bv_len = strlen( argv[ 1 ] );
364 if ( dnNormalize( 0, NULL, NULL, &dn, &li->targets[ i ]->binddn,
365 NULL ) != LDAP_SUCCESS )
367 fprintf( stderr, "%s: line %d: "
368 "bind DN '%s' is invalid\n",
369 fname, lineno, argv[ 1 ] );
373 /* password to use for meta_back_group */
374 } else if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
375 int i = li->ntargets-1;
379 "%s: line %d: need \"uri\" directive first\n",
386 "%s: line %d: missing password in \"bindpw <password>\" line\n",
390 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->bindpw );
392 /* save bind creds for referral rebinds? */
393 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
396 "%s: line %d: rebind-as-user takes no arguments\n",
402 /* name to use as pseudo-root dn */
403 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
404 int i = li->ntargets-1;
409 "%s: line %d: need \"uri\" directive first\n",
416 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
421 dn.bv_val = argv[ 1 ];
422 dn.bv_len = strlen( argv[ 1 ] );
423 if ( dnNormalize( 0, NULL, NULL, &dn,
424 &li->targets[ i ]->pseudorootdn, NULL ) != LDAP_SUCCESS )
426 fprintf( stderr, "%s: line %d: "
427 "pseudoroot DN '%s' is invalid\n",
428 fname, lineno, argv[ 1 ] );
432 /* password to use as pseudo-root */
433 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
434 int i = li->ntargets-1;
438 "%s: line %d: need \"uri\" directive first\n",
445 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
449 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->pseudorootpw );
452 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
454 int i = li->ntargets-1;
455 struct berval dn, nvnc, pvnc, nrnc, prnc;
459 "%s: line %d: need \"uri\" directive first\n",
467 * suffixmassage <suffix> <massaged suffix>
469 * the <suffix> field must be defined as a valid suffix
470 * (or suffixAlias?) for the current database;
471 * the <massaged suffix> shouldn't have already been
472 * defined as a valid suffix or suffixAlias for the
477 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
482 dn.bv_val = argv[ 1 ];
483 dn.bv_len = strlen( argv[ 1 ] );
484 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
485 fprintf( stderr, "%s: line %d: "
486 "suffix '%s' is invalid\n",
487 fname, lineno, argv[ 1 ] );
491 tmp_be = select_backend( &nvnc, 0, 0 );
492 if ( tmp_be != NULL && tmp_be != be ) {
494 "%s: line %d: suffix already in use by another backend in"
495 " \"suffixMassage <suffix> <massaged suffix>\"\n",
502 dn.bv_val = argv[ 2 ];
503 dn.bv_len = strlen( argv[ 2 ] );
504 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
505 fprintf( stderr, "%s: line %d: "
506 "massaged suffix '%s' is invalid\n",
507 fname, lineno, argv[ 2 ] );
514 tmp_be = select_backend( &nrnc, 0, 0 );
515 if ( tmp_be != NULL ) {
517 "%s: line %d: massaged suffix already in use by another backend in"
518 " \"suffixMassage <suffix> <massaged suffix>\"\n",
529 * The suffix massaging is emulated by means of the
530 * rewrite capabilities
531 * FIXME: no extra rewrite capabilities should be added
534 return suffix_massage_config( li->targets[ i ]->rwmap.rwm_rw,
535 &pvnc, &nvnc, &prnc, &nrnc );
537 /* rewrite stuff ... */
538 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
539 int i = li->ntargets-1;
542 if ( strcasecmp( argv[0], "rewriteEngine" ) == 0 ) {
543 li->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
545 return rewrite_parse(li->rwinfo, fname, lineno,
549 return rewrite_parse( li->targets[ i ]->rwmap.rwm_rw, fname, lineno,
552 /* objectclass/attribute mapping */
553 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
554 int i = li->ntargets-1;
558 "%s: line %d: need \"uri\" directive first\n",
563 return ldap_back_map_config( &li->targets[ i ]->rwmap.rwm_oc,
564 &li->targets[ i ]->rwmap.rwm_at,
565 fname, lineno, argc, argv );
568 if ( meta_back_cache_config( be, fname, lineno, argc, argv ) == 0 ) {
573 "%s: line %d: unknown directive \"%s\" in meta database definition"
575 fname, lineno, argv[0] );