2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1999-2005 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>
32 #include "../back-ldap/back-ldap.h"
33 #undef ldap_debug /* silence a warning in ldap-int.h */
34 #include "../../../libraries/libldap/ldap-int.h"
35 #include "back-meta.h"
37 static struct metatarget *
40 struct metatarget *lt;
41 struct ldapmapping *mapping;
43 lt = ch_calloc( sizeof( struct metatarget ), 1 );
48 lt->mt_rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
49 if ( lt->mt_rwmap.rwm_rw == NULL ) {
58 * the filter rewrite as a string must be disabled
59 * by default; it can be re-enabled by adding rules;
60 * this creates an empty rewriteContext
62 rargv[ 0 ] = "rewriteContext";
63 rargv[ 1 ] = "searchFilter";
65 rewrite_parse( lt->mt_rwmap.rwm_rw, "<suffix massage>",
68 rargv[ 0 ] = "rewriteContext";
69 rargv[ 1 ] = "default";
71 rewrite_parse( lt->mt_rwmap.rwm_rw, "<suffix massage>",
75 ldap_back_map_init( <->mt_rwmap.rwm_at, &mapping );
89 struct metainfo *li = ( struct metainfo * )be->be_private;
93 "%s: line %d: meta backend info is null!\n",
98 /* URI of server to query */
99 if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
100 int i = li->ntargets;
103 #endif /* uncomment if uri MUST be a branch of suffix */
104 LDAPURLDesc *ludp, *tmpludp;
110 "%s: line %d: missing address"
111 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
118 li->targets = ch_realloc( li->targets,
119 sizeof( struct metatarget *)*li->ntargets );
120 if ( li->targets == NULL ) {
122 "%s: line %d: out of memory while storing server name"
123 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
128 if ( ( li->targets[ i ] = new_target() ) == NULL ) {
130 "%s: line %d: unable to init server"
131 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
139 if ( ldap_url_parselist_ext( &ludp, argv[ 1 ], "\t" ) != LDAP_SUCCESS ) {
141 "%s: line %d: unable to parse URI"
142 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
148 * uri MUST have the <dn> part!
150 if ( ludp->lud_dn == NULL || ludp->lud_dn[ 0 ] == '\0' ) {
152 "%s: line %d: missing <naming context> "
153 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
159 * copies and stores uri and suffix
161 dn.bv_val = ludp->lud_dn;
162 dn.bv_len = strlen( ludp->lud_dn );
164 rc = dnPrettyNormal( NULL, &dn, &li->targets[ i ]->mt_psuffix,
165 &li->targets[ i ]->mt_nsuffix, NULL );
166 if( rc != LDAP_SUCCESS ) {
167 fprintf( stderr, "%s: line %d: "
168 "target '%s' DN is invalid\n",
169 fname, lineno, argv[ 1 ] );
173 ludp->lud_dn[ 0 ] = '\0';
175 for ( tmpludp = ludp->lud_next; tmpludp; tmpludp = tmpludp->lud_next ) {
176 if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
177 fprintf( stderr, "%s: line %d: "
178 "multiple URIs must have "
186 li->targets[ i ]->mt_uri = ldap_url_list2urls( ludp );
187 ldap_free_urllist( ludp );
188 if ( li->targets[ i ]->mt_uri == NULL) {
189 fprintf( stderr, "%s: line %d: no memory?\n",
195 * uri MUST be a branch of suffix!
197 #if 0 /* too strict a constraint */
198 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) != be ) {
200 "%s: line %d: <naming context> of URI does not refer to current backend"
201 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
207 * uri MUST be a branch of a suffix!
209 if ( select_backend( &li->targets[ i ]->mt_nsuffix, 0, 0 ) == NULL ) {
211 "%s: line %d: <naming context> of URI does not resolve to a backend"
212 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
220 * uri MUST not be used by other URIs!
222 * FIXME: this limitation may be removed,
223 * or worked out, at least, in some manner
225 for ( j = 0; j < i-1; j++ ) {
226 if ( dn_match( &li->targets[ i ]->suffix,
227 &li->targets[ j ]->suffix ) ) {
229 "%s: line %d: naming context \"%s\" already used"
230 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
231 fname, lineno, last+1 );
238 fprintf(stderr, "%s: line %d: URI \"%s\", suffix \"%s\"\n",
239 fname, lineno, li->targets[ i ]->uri,
240 li->targets[ i ]->psuffix.bv_val );
243 /* default target directive */
244 } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
245 int i = li->ntargets-1;
250 "%s: line %d: \"default-target\" alone need be"
251 " inside a \"uri\" directive\n",
255 li->defaulttarget = i;
257 if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
260 "%s: line %d: \"default-target none\""
261 " should go before uri definitions\n",
264 li->defaulttarget = META_DEFAULT_TARGET_NONE;
266 int n = atoi( argv[ 1 ] );
267 if ( n < 1 || n >= i ) {
269 "%s: line %d: illegal target number %d\n",
273 li->defaulttarget = n-1;
277 /* ttl of dn cache */
278 } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
281 "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
286 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
287 li->cache.ttl = META_DNCACHE_FOREVER;
288 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
289 li->cache.ttl = META_DNCACHE_DISABLED;
291 li->cache.ttl = atol( argv[ 1 ] );
294 /* network timeout when connecting to ldap servers */
295 } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
298 "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
302 li->network_timeout = atol(argv[ 1 ]);
304 /* name to use for meta_back_group */
305 } else if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
306 int i = li->ntargets-1;
311 "%s: line %d: need \"uri\" directive first\n",
318 "%s: line %d: missing name in \"binddn <name>\" line\n",
323 dn.bv_val = argv[ 1 ];
324 dn.bv_len = strlen( argv[ 1 ] );
325 if ( dnNormalize( 0, NULL, NULL, &dn, &li->targets[ i ]->mt_binddn,
326 NULL ) != LDAP_SUCCESS )
328 fprintf( stderr, "%s: line %d: "
329 "bind DN '%s' is invalid\n",
330 fname, lineno, argv[ 1 ] );
334 /* password to use for meta_back_group */
335 } else if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
336 int i = li->ntargets-1;
340 "%s: line %d: need \"uri\" directive first\n",
347 "%s: line %d: missing password in \"bindpw <password>\" line\n",
351 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->mt_bindpw );
353 /* save bind creds for referral rebinds? */
354 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
357 "%s: line %d: rebind-as-user takes no arguments\n",
363 /* name to use as pseudo-root dn */
364 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
365 int i = li->ntargets-1;
370 "%s: line %d: need \"uri\" directive first\n",
377 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
382 dn.bv_val = argv[ 1 ];
383 dn.bv_len = strlen( argv[ 1 ] );
384 if ( dnNormalize( 0, NULL, NULL, &dn,
385 &li->targets[ i ]->mt_pseudorootdn, NULL ) != LDAP_SUCCESS )
387 fprintf( stderr, "%s: line %d: "
388 "pseudoroot DN '%s' is invalid\n",
389 fname, lineno, argv[ 1 ] );
393 /* password to use as pseudo-root */
394 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
395 int i = li->ntargets-1;
399 "%s: line %d: need \"uri\" directive first\n",
406 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
410 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->mt_pseudorootpw );
413 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
415 int i = li->ntargets-1;
416 struct berval dn, nvnc, pvnc, nrnc, prnc;
420 "%s: line %d: need \"uri\" directive first\n",
428 * suffixmassage <suffix> <massaged suffix>
430 * the <suffix> field must be defined as a valid suffix
431 * (or suffixAlias?) for the current database;
432 * the <massaged suffix> shouldn't have already been
433 * defined as a valid suffix or suffixAlias for the
438 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
443 dn.bv_val = argv[ 1 ];
444 dn.bv_len = strlen( argv[ 1 ] );
445 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
446 fprintf( stderr, "%s: line %d: "
447 "suffix '%s' is invalid\n",
448 fname, lineno, argv[ 1 ] );
452 tmp_be = select_backend( &nvnc, 0, 0 );
453 if ( tmp_be != NULL && tmp_be != be ) {
455 "%s: line %d: suffix already in use by another backend in"
456 " \"suffixMassage <suffix> <massaged suffix>\"\n",
463 dn.bv_val = argv[ 2 ];
464 dn.bv_len = strlen( argv[ 2 ] );
465 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
466 fprintf( stderr, "%s: line %d: "
467 "massaged suffix '%s' is invalid\n",
468 fname, lineno, argv[ 2 ] );
475 tmp_be = select_backend( &nrnc, 0, 0 );
476 if ( tmp_be != NULL ) {
478 "%s: line %d: massaged suffix already in use by another backend in"
479 " \"suffixMassage <suffix> <massaged suffix>\"\n",
490 * The suffix massaging is emulated by means of the
491 * rewrite capabilities
492 * FIXME: no extra rewrite capabilities should be added
495 return suffix_massage_config( li->targets[ i ]->mt_rwmap.rwm_rw,
496 &pvnc, &nvnc, &prnc, &nrnc );
498 /* rewrite stuff ... */
499 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
500 int i = li->ntargets-1;
503 if ( strcasecmp( argv[0], "rewriteEngine" ) == 0 ) {
504 li->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
506 return rewrite_parse( li->rwinfo, fname, lineno,
510 return rewrite_parse( li->targets[ i ]->mt_rwmap.rwm_rw,
511 fname, lineno, argc, argv );
513 /* objectclass/attribute mapping */
514 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
515 int i = li->ntargets-1;
519 "%s: line %d: need \"uri\" directive first\n",
524 return ldap_back_map_config( &li->targets[ i ]->mt_rwmap.rwm_oc,
525 &li->targets[ i ]->mt_rwmap.rwm_at,
526 fname, lineno, argc, argv );
529 return SLAP_CONF_UNKNOWN;
535 ldap_back_map_config(
536 struct ldapmap *oc_map,
537 struct ldapmap *at_map,
544 struct ldapmapping *mapping;
548 if ( argc < 3 || argc > 4 ) {
550 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
555 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
559 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
563 fprintf( stderr, "%s: line %d: syntax is "
564 "\"map {objectclass | attribute} [<local> | *] "
565 "{<foreign> | *}\"\n",
570 if ( strcmp( argv[2], "*" ) == 0 ) {
571 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
572 map->drop_missing = ( argc < 4 );
577 } else if ( argc < 4 ) {
583 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
586 if ( ( map == at_map )
587 && ( strcasecmp( src, "objectclass" ) == 0
588 || strcasecmp( dst, "objectclass" ) == 0 ) )
591 "%s: line %d: objectclass attribute cannot be mapped\n",
595 mapping = (struct ldapmapping *)ch_calloc( 2,
596 sizeof(struct ldapmapping) );
597 if ( mapping == NULL ) {
599 "%s: line %d: out of memory\n",
603 ber_str2bv( src, 0, 1, &mapping->src );
604 ber_str2bv( dst, 0, 1, &mapping->dst );
605 mapping[1].src = mapping->dst;
606 mapping[1].dst = mapping->src;
612 if ( src[0] != '\0' ) {
613 if ( oc_bvfind( &mapping->src ) == NULL ) {
615 "%s: line %d: warning, source objectClass '%s' "
616 "should be defined in schema\n",
617 fname, lineno, src );
620 * FIXME: this should become an err
626 if ( oc_bvfind( &mapping->dst ) == NULL ) {
628 "%s: line %d: warning, destination objectClass '%s' "
629 "is not defined in schema\n",
630 fname, lineno, dst );
634 const char *text = NULL;
635 AttributeDescription *ad = NULL;
637 if ( src[0] != '\0' ) {
638 rc = slap_bv2ad( &mapping->src, &ad, &text );
639 if ( rc != LDAP_SUCCESS ) {
641 "%s: line %d: warning, source attributeType '%s' "
642 "should be defined in schema\n",
643 fname, lineno, src );
646 * FIXME: this should become an err
654 rc = slap_bv2ad( &mapping->dst, &ad, &text );
655 if ( rc != LDAP_SUCCESS ) {
657 "%s: line %d: warning, destination attributeType '%s' "
658 "is not defined in schema\n",
659 fname, lineno, dst );
663 if ( (src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL)
664 || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
667 "%s: line %d: duplicate mapping found (ignored)\n",
672 if ( src[0] != '\0' ) {
673 avl_insert( &map->map, (caddr_t)mapping,
674 mapping_cmp, mapping_dup );
676 avl_insert( &map->remap, (caddr_t)&mapping[1],
677 mapping_cmp, mapping_dup );
683 ch_free( mapping->src.bv_val );
684 ch_free( mapping->dst.bv_val );
692 #ifdef ENABLE_REWRITE
694 suffix_massage_regexize( const char *s )
701 ( r = strchr( p, ',' ) ) != NULL;
705 res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
707 ptr = lutil_strcopy( res, "(.*)" );
709 ( r = strchr( p, ',' ) ) != NULL;
711 ptr = lutil_strncopy( ptr, p, r - p + 1 );
712 ptr = lutil_strcopy( ptr, "[ ]?" );
714 if ( r[ 1 ] == ' ' ) {
718 lutil_strcopy( ptr, p );
724 suffix_massage_patternize( const char *s )
731 res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
737 strcpy( res + sizeof( "%1" ) - 1, s );
743 suffix_massage_config(
744 struct rewrite_info *info,
754 rargv[ 0 ] = "rewriteEngine";
757 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
759 rargv[ 0 ] = "rewriteContext";
760 rargv[ 1 ] = "default";
762 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
764 rargv[ 0 ] = "rewriteRule";
765 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
766 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
769 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
770 ch_free( rargv[ 1 ] );
771 ch_free( rargv[ 2 ] );
773 rargv[ 0 ] = "rewriteContext";
774 rargv[ 1 ] = "searchEntryDN";
776 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
778 rargv[ 0 ] = "rewriteRule";
779 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
780 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
783 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
784 ch_free( rargv[ 1 ] );
785 ch_free( rargv[ 2 ] );
787 /* backward compatibility */
788 rargv[ 0 ] = "rewriteContext";
789 rargv[ 1 ] = "searchResult";
790 rargv[ 2 ] = "alias";
791 rargv[ 3 ] = "searchEntryDN";
793 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
795 rargv[ 0 ] = "rewriteContext";
796 rargv[ 1 ] = "matchedDN";
797 rargv[ 2 ] = "alias";
798 rargv[ 3 ] = "searchEntryDN";
800 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
802 rargv[ 0 ] = "rewriteContext";
803 rargv[ 1 ] = "searchAttrDN";
804 rargv[ 2 ] = "alias";
805 rargv[ 3 ] = "searchEntryDN";
807 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
809 /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
810 * see servers/slapd/overlays/rwm.h for details */
811 rargv[ 0 ] = "rewriteContext";
812 rargv[ 1 ] = "referralAttrDN";
814 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
816 rargv[ 0 ] = "rewriteContext";
817 rargv[ 1 ] = "referralDN";
819 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
823 #endif /* ENABLE_REWRITE */