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 #undef ldap_debug /* silence a warning in ldap-int.h */
77 #include "../../../libraries/libldap/ldap-int.h"
78 #include "back-meta.h"
80 static struct metatarget *
83 struct metatarget *lt;
84 struct ldapmapping *mapping;
86 lt = ch_calloc( sizeof( struct metatarget ), 1 );
91 lt->rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
92 if ( lt->rwmap.rwm_rw == NULL ) {
97 ldap_back_map_init( <->rwmap.rwm_at, &mapping );
111 struct metainfo *li = ( struct metainfo * )be->be_private;
115 "%s: line %d: meta backend info is null!\n",
120 /* URI of server to query */
121 if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
122 int i = li->ntargets;
125 #endif /* uncomment if uri MUST be a branch of suffix */
126 LDAPURLDesc *ludp, *tmpludp;
132 "%s: line %d: missing address"
133 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
140 li->targets = ch_realloc( li->targets,
141 sizeof( struct metatarget *)*li->ntargets );
142 if ( li->targets == NULL ) {
144 "%s: line %d: out of memory while storing server name"
145 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
150 if ( ( li->targets[ i ] = new_target() ) == NULL ) {
152 "%s: line %d: unable to init server"
153 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
161 if ( ldap_url_parselist_ext( &ludp, argv[ 1 ], "\t" ) != LDAP_SUCCESS ) {
163 "%s: line %d: unable to parse URI"
164 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
170 * uri MUST have the <dn> part!
172 if ( ludp->lud_dn == NULL || ludp->lud_dn[ 0 ] == '\0' ) {
174 "%s: line %d: missing <naming context> "
175 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
181 * copies and stores uri and suffix
183 dn.bv_val = ludp->lud_dn;
184 dn.bv_len = strlen( ludp->lud_dn );
186 rc = dnPrettyNormal( NULL, &dn, &li->targets[ i ]->psuffix,
187 &li->targets[ i ]->suffix, NULL );
188 if( rc != LDAP_SUCCESS ) {
189 fprintf( stderr, "%s: line %d: "
190 "target '%s' DN is invalid\n",
191 fname, lineno, argv[ 1 ] );
195 ludp->lud_dn[ 0 ] = '\0';
197 for ( tmpludp = ludp->lud_next; tmpludp; tmpludp = tmpludp->lud_next ) {
198 if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
199 fprintf( stderr, "%s: line %d: "
200 "multiple URIs must have no DN part\n",
201 fname, lineno, argv[ 1 ] );
207 li->targets[ i ]->uri = ldap_url_list2urls( ludp );
208 ldap_free_urllist( ludp );
209 if ( li->targets[ i ]->uri == NULL) {
210 fprintf( stderr, "%s: line %d: no memory?\n",
216 * uri MUST be a branch of suffix!
218 #if 0 /* too strict a constraint */
219 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) != be ) {
221 "%s: line %d: <naming context> of URI does not refer to current backend"
222 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
228 * uri MUST be a branch of a suffix!
230 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) == NULL ) {
232 "%s: line %d: <naming context> of URI does not resolve to a backend"
233 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
241 * uri MUST not be used by other URIs!
243 * FIXME: this limitation may be removed,
244 * or worked out, at least, in some manner
246 for ( j = 0; j < i-1; j++ ) {
247 if ( dn_match( &li->targets[ i ]->suffix,
248 &li->targets[ j ]->suffix ) ) {
250 "%s: line %d: naming context \"%s\" already used"
251 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
252 fname, lineno, last+1 );
259 fprintf(stderr, "%s: line %d: URI \"%s\", suffix \"%s\"\n",
260 fname, lineno, li->targets[ i ]->uri,
261 li->targets[ i ]->psuffix.bv_val );
264 /* default target directive */
265 } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
266 int i = li->ntargets-1;
271 "%s: line %d: \"default-target\" alone need be"
272 " inside a \"uri\" directive\n",
276 li->defaulttarget = i;
278 if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
281 "%s: line %d: \"default-target none\""
282 " should go before uri definitions\n",
285 li->defaulttarget = META_DEFAULT_TARGET_NONE;
287 int n = atoi( argv[ 1 ] );
288 if ( n < 1 || n >= i ) {
290 "%s: line %d: illegal target number %d\n",
294 li->defaulttarget = n-1;
298 /* ttl of dn cache */
299 } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
302 "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
307 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
308 li->cache.ttl = META_DNCACHE_FOREVER;
309 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
310 li->cache.ttl = META_DNCACHE_DISABLED;
312 li->cache.ttl = atol( argv[ 1 ] );
315 /* name to use for meta_back_group */
316 } else if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
317 int i = li->ntargets-1;
322 "%s: line %d: need \"uri\" directive first\n",
329 "%s: line %d: missing name in \"binddn <name>\" line\n",
334 dn.bv_val = argv[ 1 ];
335 dn.bv_len = strlen( argv[ 1 ] );
336 if ( dnNormalize2( NULL, &dn, &li->targets[ i ]->binddn, NULL ) != LDAP_SUCCESS ) {
337 fprintf( stderr, "%s: line %d: "
338 "bind DN '%s' is invalid\n",
339 fname, lineno, argv[ 1 ] );
343 /* password to use for meta_back_group */
344 } else if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
345 int i = li->ntargets-1;
349 "%s: line %d: need \"uri\" directive first\n",
356 "%s: line %d: missing password in \"bindpw <password>\" line\n",
360 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->bindpw );
362 /* save bind creds for referral rebinds? */
363 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
366 "%s: line %d: rebind-as-user takes no arguments\n",
372 /* name to use as pseudo-root dn */
373 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
374 int i = li->ntargets-1;
379 "%s: line %d: need \"uri\" directive first\n",
386 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
391 dn.bv_val = argv[ 1 ];
392 dn.bv_len = strlen( argv[ 1 ] );
393 if ( dnNormalize2( NULL, &dn, &li->targets[ i ]->pseudorootdn, NULL ) != LDAP_SUCCESS ) {
394 fprintf( stderr, "%s: line %d: "
395 "pseudoroot DN '%s' is invalid\n",
396 fname, lineno, argv[ 1 ] );
400 /* password to use as pseudo-root */
401 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
402 int i = li->ntargets-1;
406 "%s: line %d: need \"uri\" directive first\n",
413 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
417 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->pseudorootpw );
420 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
422 int i = li->ntargets-1;
423 struct berval dn, nvnc, pvnc, nrnc, prnc;
427 "%s: line %d: need \"uri\" directive first\n",
435 * suffixmassage <suffix> <massaged suffix>
437 * the <suffix> field must be defined as a valid suffix
438 * (or suffixAlias?) for the current database;
439 * the <massaged suffix> shouldn't have already been
440 * defined as a valid suffix or suffixAlias for the
445 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
450 dn.bv_val = argv[ 1 ];
451 dn.bv_len = strlen( argv[ 1 ] );
452 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
453 fprintf( stderr, "%s: line %d: "
454 "suffix '%s' is invalid\n",
455 fname, lineno, argv[ 1 ] );
459 tmp_be = select_backend( &nvnc, 0, 0 );
460 if ( tmp_be != NULL && tmp_be != be ) {
462 "%s: line %d: suffix already in use by another backend in"
463 " \"suffixMassage <suffix> <massaged suffix>\"\n",
470 dn.bv_val = argv[ 2 ];
471 dn.bv_len = strlen( argv[ 2 ] );
472 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
473 fprintf( stderr, "%s: line %d: "
474 "massaged suffix '%s' is invalid\n",
475 fname, lineno, argv[ 2 ] );
482 tmp_be = select_backend( &nrnc, 0, 0 );
483 if ( tmp_be != NULL ) {
485 "%s: line %d: massaged suffix already in use by another backend in"
486 " \"suffixMassage <suffix> <massaged suffix>\"\n",
497 * The suffix massaging is emulated by means of the
498 * rewrite capabilities
499 * FIXME: no extra rewrite capabilities should be added
502 return suffix_massage_config( li->targets[ i ]->rwmap.rwm_rw,
503 &pvnc, &nvnc, &prnc, &nrnc );
505 /* rewrite stuff ... */
506 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
507 int i = li->ntargets-1;
512 "%s: line %d: need \"uri\" directive first\n",
514 #else /* LDAP_CACHING */
515 if ( strcasecmp( argv[0], "rewriteEngine" ) == 0 ) {
516 li->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
518 return rewrite_parse(li->rwinfo, fname, lineno,
520 #endif /* LDAP_CACHING */
523 return rewrite_parse( li->targets[ i ]->rwmap.rwm_rw, fname, lineno,
526 /* objectclass/attribute mapping */
527 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
528 int i = li->ntargets-1;
532 "%s: line %d: need \"uri\" directive first\n",
537 return ldap_back_map_config( &li->targets[ i ]->rwmap.rwm_oc,
538 &li->targets[ i ]->rwmap.rwm_at,
539 fname, lineno, argc, argv );
543 if ( meta_back_cache_config( be, fname, lineno, argc, argv ) == 0 ) {
546 #endif /* LDAP_CACHING */
549 "%s: line %d: unknown directive \"%s\" in meta database definition"
551 fname, lineno, argv[0] );