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 ) {
57 * the filter rewrite as a string must be disabled
58 * by default; it can be re-enabled by adding rules;
59 * this creates an empty rewriteContext
61 rargv[ 0 ] = "rewriteContext";
62 rargv[ 1 ] = "searchFilter";
64 rewrite_parse( lt->rwmap.rwm_rw, "<suffix massage>",
67 rargv[ 0 ] = "rewriteContext";
68 rargv[ 1 ] = "default";
70 rewrite_parse( lt->rwmap.rwm_rw, "<suffix massage>",
74 ldap_back_map_init( <->rwmap.rwm_at, &mapping );
88 struct metainfo *li = ( struct metainfo * )be->be_private;
92 "%s: line %d: meta backend info is null!\n",
97 /* URI of server to query */
98 if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
102 #endif /* uncomment if uri MUST be a branch of suffix */
103 LDAPURLDesc *ludp, *tmpludp;
109 "%s: line %d: missing address"
110 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
117 li->targets = ch_realloc( li->targets,
118 sizeof( struct metatarget *)*li->ntargets );
119 if ( li->targets == NULL ) {
121 "%s: line %d: out of memory while storing server name"
122 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
127 if ( ( li->targets[ i ] = new_target() ) == NULL ) {
129 "%s: line %d: unable to init server"
130 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
138 if ( ldap_url_parselist_ext( &ludp, argv[ 1 ], "\t" ) != LDAP_SUCCESS ) {
140 "%s: line %d: unable to parse URI"
141 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
147 * uri MUST have the <dn> part!
149 if ( ludp->lud_dn == NULL || ludp->lud_dn[ 0 ] == '\0' ) {
151 "%s: line %d: missing <naming context> "
152 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
158 * copies and stores uri and suffix
160 dn.bv_val = ludp->lud_dn;
161 dn.bv_len = strlen( ludp->lud_dn );
163 rc = dnPrettyNormal( NULL, &dn, &li->targets[ i ]->psuffix,
164 &li->targets[ i ]->suffix, NULL );
165 if( rc != LDAP_SUCCESS ) {
166 fprintf( stderr, "%s: line %d: "
167 "target '%s' DN is invalid\n",
168 fname, lineno, argv[ 1 ] );
172 ludp->lud_dn[ 0 ] = '\0';
174 for ( tmpludp = ludp->lud_next; tmpludp; tmpludp = tmpludp->lud_next ) {
175 if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
176 fprintf( stderr, "%s: line %d: "
177 "multiple URIs must have "
185 li->targets[ i ]->uri = ldap_url_list2urls( ludp );
186 ldap_free_urllist( ludp );
187 if ( li->targets[ i ]->uri == NULL) {
188 fprintf( stderr, "%s: line %d: no memory?\n",
194 * uri MUST be a branch of suffix!
196 #if 0 /* too strict a constraint */
197 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) != be ) {
199 "%s: line %d: <naming context> of URI does not refer to current backend"
200 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
206 * uri MUST be a branch of a suffix!
208 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) == NULL ) {
210 "%s: line %d: <naming context> of URI does not resolve to a backend"
211 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
219 * uri MUST not be used by other URIs!
221 * FIXME: this limitation may be removed,
222 * or worked out, at least, in some manner
224 for ( j = 0; j < i-1; j++ ) {
225 if ( dn_match( &li->targets[ i ]->suffix,
226 &li->targets[ j ]->suffix ) ) {
228 "%s: line %d: naming context \"%s\" already used"
229 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
230 fname, lineno, last+1 );
237 fprintf(stderr, "%s: line %d: URI \"%s\", suffix \"%s\"\n",
238 fname, lineno, li->targets[ i ]->uri,
239 li->targets[ i ]->psuffix.bv_val );
242 /* default target directive */
243 } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
244 int i = li->ntargets-1;
249 "%s: line %d: \"default-target\" alone need be"
250 " inside a \"uri\" directive\n",
254 li->defaulttarget = i;
256 if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
259 "%s: line %d: \"default-target none\""
260 " should go before uri definitions\n",
263 li->defaulttarget = META_DEFAULT_TARGET_NONE;
265 int n = atoi( argv[ 1 ] );
266 if ( n < 1 || n >= i ) {
268 "%s: line %d: illegal target number %d\n",
272 li->defaulttarget = n-1;
276 /* ttl of dn cache */
277 } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
280 "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
285 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
286 li->cache.ttl = META_DNCACHE_FOREVER;
287 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
288 li->cache.ttl = META_DNCACHE_DISABLED;
290 li->cache.ttl = atol( argv[ 1 ] );
293 /* network timeout when connecting to ldap servers */
294 } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
297 "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
301 li->network_timeout = atol(argv[ 1 ]);
303 /* name to use for meta_back_group */
304 } else if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
305 int i = li->ntargets-1;
310 "%s: line %d: need \"uri\" directive first\n",
317 "%s: line %d: missing name in \"binddn <name>\" line\n",
322 dn.bv_val = argv[ 1 ];
323 dn.bv_len = strlen( argv[ 1 ] );
324 if ( dnNormalize( 0, NULL, NULL, &dn, &li->targets[ i ]->binddn,
325 NULL ) != LDAP_SUCCESS )
327 fprintf( stderr, "%s: line %d: "
328 "bind DN '%s' is invalid\n",
329 fname, lineno, argv[ 1 ] );
333 /* password to use for meta_back_group */
334 } else if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
335 int i = li->ntargets-1;
339 "%s: line %d: need \"uri\" directive first\n",
346 "%s: line %d: missing password in \"bindpw <password>\" line\n",
350 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->bindpw );
352 /* save bind creds for referral rebinds? */
353 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
356 "%s: line %d: rebind-as-user takes no arguments\n",
362 /* name to use as pseudo-root dn */
363 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
364 int i = li->ntargets-1;
369 "%s: line %d: need \"uri\" directive first\n",
376 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
381 dn.bv_val = argv[ 1 ];
382 dn.bv_len = strlen( argv[ 1 ] );
383 if ( dnNormalize( 0, NULL, NULL, &dn,
384 &li->targets[ i ]->pseudorootdn, NULL ) != LDAP_SUCCESS )
386 fprintf( stderr, "%s: line %d: "
387 "pseudoroot DN '%s' is invalid\n",
388 fname, lineno, argv[ 1 ] );
392 /* password to use as pseudo-root */
393 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
394 int i = li->ntargets-1;
398 "%s: line %d: need \"uri\" directive first\n",
405 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
409 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->pseudorootpw );
412 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
414 int i = li->ntargets-1;
415 struct berval dn, nvnc, pvnc, nrnc, prnc;
419 "%s: line %d: need \"uri\" directive first\n",
427 * suffixmassage <suffix> <massaged suffix>
429 * the <suffix> field must be defined as a valid suffix
430 * (or suffixAlias?) for the current database;
431 * the <massaged suffix> shouldn't have already been
432 * defined as a valid suffix or suffixAlias for the
437 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
442 dn.bv_val = argv[ 1 ];
443 dn.bv_len = strlen( argv[ 1 ] );
444 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
445 fprintf( stderr, "%s: line %d: "
446 "suffix '%s' is invalid\n",
447 fname, lineno, argv[ 1 ] );
451 tmp_be = select_backend( &nvnc, 0, 0 );
452 if ( tmp_be != NULL && tmp_be != be ) {
454 "%s: line %d: suffix already in use by another backend in"
455 " \"suffixMassage <suffix> <massaged suffix>\"\n",
462 dn.bv_val = argv[ 2 ];
463 dn.bv_len = strlen( argv[ 2 ] );
464 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
465 fprintf( stderr, "%s: line %d: "
466 "massaged suffix '%s' is invalid\n",
467 fname, lineno, argv[ 2 ] );
474 tmp_be = select_backend( &nrnc, 0, 0 );
475 if ( tmp_be != NULL ) {
477 "%s: line %d: massaged suffix already in use by another backend in"
478 " \"suffixMassage <suffix> <massaged suffix>\"\n",
489 * The suffix massaging is emulated by means of the
490 * rewrite capabilities
491 * FIXME: no extra rewrite capabilities should be added
494 return suffix_massage_config( li->targets[ i ]->rwmap.rwm_rw,
495 &pvnc, &nvnc, &prnc, &nrnc );
497 /* rewrite stuff ... */
498 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
499 int i = li->ntargets-1;
502 if ( strcasecmp( argv[0], "rewriteEngine" ) == 0 ) {
503 li->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
505 return rewrite_parse(li->rwinfo, fname, lineno,
509 return rewrite_parse( li->targets[ i ]->rwmap.rwm_rw, fname, lineno,
512 /* objectclass/attribute mapping */
513 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
514 int i = li->ntargets-1;
518 "%s: line %d: need \"uri\" directive first\n",
523 return ldap_back_map_config( &li->targets[ i ]->rwmap.rwm_oc,
524 &li->targets[ i ]->rwmap.rwm_at,
525 fname, lineno, argc, argv );
528 return SLAP_CONF_UNKNOWN;