2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2004 The OpenLDAP Foundation.
5 * Portions Copyright 2001-2003 Pierangelo Masarati.
6 * Portions Copyright 1999-2003 Howard Chu.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
18 * This work was initially developed by the Howard Chu for inclusion
19 * in OpenLDAP Software and subsequently enhanced by Pierangelo
27 #include <ac/string.h>
28 #include <ac/socket.h>
31 #include "../back-ldap/back-ldap.h"
32 #undef ldap_debug /* silence a warning in ldap-int.h */
33 #include "../../../libraries/libldap/ldap-int.h"
34 #include "back-meta.h"
36 static struct metatarget *
39 struct metatarget *lt;
40 struct ldapmapping *mapping;
42 lt = ch_calloc( sizeof( struct metatarget ), 1 );
47 lt->rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
48 if ( lt->rwmap.rwm_rw == NULL ) {
53 ldap_back_map_init( <->rwmap.rwm_at, &mapping );
67 struct metainfo *li = ( struct metainfo * )be->be_private;
71 "%s: line %d: meta backend info is null!\n",
76 /* URI of server to query */
77 if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
81 #endif /* uncomment if uri MUST be a branch of suffix */
82 LDAPURLDesc *ludp, *tmpludp;
88 "%s: line %d: missing address"
89 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
96 li->targets = ch_realloc( li->targets,
97 sizeof( struct metatarget *)*li->ntargets );
98 if ( li->targets == NULL ) {
100 "%s: line %d: out of memory while storing server name"
101 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
106 if ( ( li->targets[ i ] = new_target() ) == NULL ) {
108 "%s: line %d: unable to init server"
109 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
117 if ( ldap_url_parselist_ext( &ludp, argv[ 1 ], "\t" ) != LDAP_SUCCESS ) {
119 "%s: line %d: unable to parse URI"
120 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
126 * uri MUST have the <dn> part!
128 if ( ludp->lud_dn == NULL || ludp->lud_dn[ 0 ] == '\0' ) {
130 "%s: line %d: missing <naming context> "
131 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
137 * copies and stores uri and suffix
139 dn.bv_val = ludp->lud_dn;
140 dn.bv_len = strlen( ludp->lud_dn );
142 rc = dnPrettyNormal( NULL, &dn, &li->targets[ i ]->psuffix,
143 &li->targets[ i ]->suffix, NULL );
144 if( rc != LDAP_SUCCESS ) {
145 fprintf( stderr, "%s: line %d: "
146 "target '%s' DN is invalid\n",
147 fname, lineno, argv[ 1 ] );
151 ludp->lud_dn[ 0 ] = '\0';
153 for ( tmpludp = ludp->lud_next; tmpludp; tmpludp = tmpludp->lud_next ) {
154 if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
155 fprintf( stderr, "%s: line %d: "
156 "multiple URIs must have "
164 li->targets[ i ]->uri = ldap_url_list2urls( ludp );
165 ldap_free_urllist( ludp );
166 if ( li->targets[ i ]->uri == NULL) {
167 fprintf( stderr, "%s: line %d: no memory?\n",
173 * uri MUST be a branch of suffix!
175 #if 0 /* too strict a constraint */
176 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) != be ) {
178 "%s: line %d: <naming context> of URI does not refer to current backend"
179 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
185 * uri MUST be a branch of a suffix!
187 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) == NULL ) {
189 "%s: line %d: <naming context> of URI does not resolve to a backend"
190 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
198 * uri MUST not be used by other URIs!
200 * FIXME: this limitation may be removed,
201 * or worked out, at least, in some manner
203 for ( j = 0; j < i-1; j++ ) {
204 if ( dn_match( &li->targets[ i ]->suffix,
205 &li->targets[ j ]->suffix ) ) {
207 "%s: line %d: naming context \"%s\" already used"
208 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
209 fname, lineno, last+1 );
216 fprintf(stderr, "%s: line %d: URI \"%s\", suffix \"%s\"\n",
217 fname, lineno, li->targets[ i ]->uri,
218 li->targets[ i ]->psuffix.bv_val );
221 /* default target directive */
222 } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
223 int i = li->ntargets-1;
228 "%s: line %d: \"default-target\" alone need be"
229 " inside a \"uri\" directive\n",
233 li->defaulttarget = i;
235 if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
238 "%s: line %d: \"default-target none\""
239 " should go before uri definitions\n",
242 li->defaulttarget = META_DEFAULT_TARGET_NONE;
244 int n = atoi( argv[ 1 ] );
245 if ( n < 1 || n >= i ) {
247 "%s: line %d: illegal target number %d\n",
251 li->defaulttarget = n-1;
255 /* ttl of dn cache */
256 } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
259 "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
264 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
265 li->cache.ttl = META_DNCACHE_FOREVER;
266 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
267 li->cache.ttl = META_DNCACHE_DISABLED;
269 li->cache.ttl = atol( argv[ 1 ] );
272 /* network timeout when connecting to ldap servers */
273 } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
276 "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
280 li->network_timeout = atol(argv[ 1 ]);
282 /* name to use for meta_back_group */
283 } else if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
284 int i = li->ntargets-1;
289 "%s: line %d: need \"uri\" directive first\n",
296 "%s: line %d: missing name in \"binddn <name>\" line\n",
301 dn.bv_val = argv[ 1 ];
302 dn.bv_len = strlen( argv[ 1 ] );
303 if ( dnNormalize( 0, NULL, NULL, &dn, &li->targets[ i ]->binddn,
304 NULL ) != LDAP_SUCCESS )
306 fprintf( stderr, "%s: line %d: "
307 "bind DN '%s' is invalid\n",
308 fname, lineno, argv[ 1 ] );
312 /* password to use for meta_back_group */
313 } else if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
314 int i = li->ntargets-1;
318 "%s: line %d: need \"uri\" directive first\n",
325 "%s: line %d: missing password in \"bindpw <password>\" line\n",
329 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->bindpw );
331 /* save bind creds for referral rebinds? */
332 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
335 "%s: line %d: rebind-as-user takes no arguments\n",
341 /* name to use as pseudo-root dn */
342 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
343 int i = li->ntargets-1;
348 "%s: line %d: need \"uri\" directive first\n",
355 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
360 dn.bv_val = argv[ 1 ];
361 dn.bv_len = strlen( argv[ 1 ] );
362 if ( dnNormalize( 0, NULL, NULL, &dn,
363 &li->targets[ i ]->pseudorootdn, NULL ) != LDAP_SUCCESS )
365 fprintf( stderr, "%s: line %d: "
366 "pseudoroot DN '%s' is invalid\n",
367 fname, lineno, argv[ 1 ] );
371 /* password to use as pseudo-root */
372 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
373 int i = li->ntargets-1;
377 "%s: line %d: need \"uri\" directive first\n",
384 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
388 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->pseudorootpw );
391 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
393 int i = li->ntargets-1;
394 struct berval dn, nvnc, pvnc, nrnc, prnc;
398 "%s: line %d: need \"uri\" directive first\n",
406 * suffixmassage <suffix> <massaged suffix>
408 * the <suffix> field must be defined as a valid suffix
409 * (or suffixAlias?) for the current database;
410 * the <massaged suffix> shouldn't have already been
411 * defined as a valid suffix or suffixAlias for the
416 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
421 dn.bv_val = argv[ 1 ];
422 dn.bv_len = strlen( argv[ 1 ] );
423 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
424 fprintf( stderr, "%s: line %d: "
425 "suffix '%s' is invalid\n",
426 fname, lineno, argv[ 1 ] );
430 tmp_be = select_backend( &nvnc, 0, 0 );
431 if ( tmp_be != NULL && tmp_be != be ) {
433 "%s: line %d: suffix already in use by another backend in"
434 " \"suffixMassage <suffix> <massaged suffix>\"\n",
441 dn.bv_val = argv[ 2 ];
442 dn.bv_len = strlen( argv[ 2 ] );
443 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
444 fprintf( stderr, "%s: line %d: "
445 "massaged suffix '%s' is invalid\n",
446 fname, lineno, argv[ 2 ] );
453 tmp_be = select_backend( &nrnc, 0, 0 );
454 if ( tmp_be != NULL ) {
456 "%s: line %d: massaged suffix already in use by another backend in"
457 " \"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 ]->rwmap.rwm_rw,
474 &pvnc, &nvnc, &prnc, &nrnc );
476 /* rewrite stuff ... */
477 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
478 int i = li->ntargets-1;
481 if ( strcasecmp( argv[0], "rewriteEngine" ) == 0 ) {
482 li->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
484 return rewrite_parse(li->rwinfo, fname, lineno,
488 return rewrite_parse( li->targets[ i ]->rwmap.rwm_rw, fname, lineno,
491 /* objectclass/attribute mapping */
492 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
493 int i = li->ntargets-1;
497 "%s: line %d: need \"uri\" directive first\n",
502 return ldap_back_map_config( &li->targets[ i ]->rwmap.rwm_oc,
503 &li->targets[ i ]->rwmap.rwm_at,
504 fname, lineno, argc, argv );
507 return SLAP_CONF_UNKNOWN;