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 /* check all, to apply the scope check on the first one */
176 for ( tmpludp = ludp; tmpludp; tmpludp = tmpludp->lud_next ) {
177 if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
178 fprintf( stderr, "%s: line %d: "
179 "multiple URIs must have "
186 if ( tmpludp->lud_scope == LDAP_SCOPE_BASE ) {
187 tmpludp->lud_scope = LDAP_SCOPE_DEFAULT;
191 li->targets[ i ]->mt_uri = ldap_url_list2urls( ludp );
192 ldap_free_urllist( ludp );
193 if ( li->targets[ i ]->mt_uri == NULL) {
194 fprintf( stderr, "%s: line %d: no memory?\n",
200 * uri MUST be a branch of suffix!
202 #if 0 /* too strict a constraint */
203 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) != be ) {
205 "%s: line %d: <naming context> of URI does not refer to current backend"
206 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
212 * uri MUST be a branch of a suffix!
214 if ( select_backend( &li->targets[ i ]->mt_nsuffix, 0, 0 ) == NULL ) {
216 "%s: line %d: <naming context> of URI does not resolve to a backend"
217 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
225 * uri MUST not be used by other URIs!
227 * FIXME: this limitation may be removed,
228 * or worked out, at least, in some manner
230 for ( j = 0; j < i-1; j++ ) {
231 if ( dn_match( &li->targets[ i ]->suffix,
232 &li->targets[ j ]->suffix ) ) {
234 "%s: line %d: naming context \"%s\" already used"
235 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
236 fname, lineno, last+1 );
243 fprintf(stderr, "%s: line %d: URI \"%s\", suffix \"%s\"\n",
244 fname, lineno, li->targets[ i ]->uri,
245 li->targets[ i ]->psuffix.bv_val );
248 /* default target directive */
249 } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
250 int i = li->ntargets-1;
255 "%s: line %d: \"default-target\" alone need be"
256 " inside a \"uri\" directive\n",
260 li->defaulttarget = i;
262 if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
265 "%s: line %d: \"default-target none\""
266 " should go before uri definitions\n",
269 li->defaulttarget = META_DEFAULT_TARGET_NONE;
271 int n = atoi( argv[ 1 ] );
272 if ( n < 1 || n >= i ) {
274 "%s: line %d: illegal target number %d\n",
278 li->defaulttarget = n-1;
282 /* ttl of dn cache */
283 } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
286 "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
291 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
292 li->cache.ttl = META_DNCACHE_FOREVER;
293 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
294 li->cache.ttl = META_DNCACHE_DISABLED;
296 li->cache.ttl = atol( argv[ 1 ] );
299 /* network timeout when connecting to ldap servers */
300 } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
303 "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
307 li->network_timeout = atol(argv[ 1 ]);
309 /* name to use for meta_back_group */
310 } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
311 || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
313 int i = li->ntargets-1;
318 "%s: line %d: need \"uri\" directive first\n",
325 "%s: line %d: missing name in \"binddn <name>\" line\n",
330 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
331 fprintf( stderr, "%s: line %d: "
332 "\"binddn\" statement is deprecated; "
333 "use \"acl-authcDN\" instead\n",
335 /* FIXME: some day we'll need to throw an error */
338 dn.bv_val = argv[ 1 ];
339 dn.bv_len = strlen( argv[ 1 ] );
340 if ( dnNormalize( 0, NULL, NULL, &dn, &li->targets[ i ]->mt_binddn,
341 NULL ) != LDAP_SUCCESS )
343 fprintf( stderr, "%s: line %d: "
344 "bind DN '%s' is invalid\n",
345 fname, lineno, argv[ 1 ] );
349 /* password to use for meta_back_group */
350 } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
351 || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
353 int i = li->ntargets-1;
357 "%s: line %d: need \"uri\" directive first\n",
364 "%s: line %d: missing password in \"bindpw <password>\" line\n",
369 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
370 fprintf( stderr, "%s: line %d: "
371 "\"bindpw\" statement is deprecated; "
372 "use \"acl-passwd\" instead\n",
374 /* FIXME: some day we'll need to throw an error */
377 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->mt_bindpw );
379 /* save bind creds for referral rebinds? */
380 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
383 "%s: line %d: rebind-as-user takes no arguments\n",
388 li->flags |= LDAP_BACK_F_SAVECRED;
390 } else if ( strcasecmp( argv[0], "chase-referrals" ) == 0 ) {
393 "%s: line %d: \"chase-referrals\" takes no arguments\n",
398 li->flags |= LDAP_BACK_F_CHASE_REFERRALS;
400 } else if ( strcasecmp( argv[0], "dont-chase-referrals" ) == 0 ) {
403 "%s: line %d: \"dont-chase-referrals\" takes no arguments\n",
408 li->flags &= ~LDAP_BACK_F_CHASE_REFERRALS;
410 /* name to use as pseudo-root dn */
411 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
412 int i = li->ntargets-1;
417 "%s: line %d: need \"uri\" directive first\n",
424 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
429 dn.bv_val = argv[ 1 ];
430 dn.bv_len = strlen( argv[ 1 ] );
431 if ( dnNormalize( 0, NULL, NULL, &dn,
432 &li->targets[ i ]->mt_pseudorootdn, NULL ) != LDAP_SUCCESS )
434 fprintf( stderr, "%s: line %d: "
435 "pseudoroot DN '%s' is invalid\n",
436 fname, lineno, argv[ 1 ] );
440 /* password to use as pseudo-root */
441 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
442 int i = li->ntargets-1;
446 "%s: line %d: need \"uri\" directive first\n",
453 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
457 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->mt_pseudorootpw );
460 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
462 int i = li->ntargets-1;
463 struct berval dn, nvnc, pvnc, nrnc, prnc;
467 "%s: line %d: need \"uri\" directive first\n",
475 * suffixmassage <suffix> <massaged suffix>
477 * the <suffix> field must be defined as a valid suffix
478 * (or suffixAlias?) for the current database;
479 * the <massaged suffix> shouldn't have already been
480 * defined as a valid suffix or suffixAlias for the
485 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
490 dn.bv_val = argv[ 1 ];
491 dn.bv_len = strlen( argv[ 1 ] );
492 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
493 fprintf( stderr, "%s: line %d: "
494 "suffix '%s' is invalid\n",
495 fname, lineno, argv[ 1 ] );
499 tmp_be = select_backend( &nvnc, 0, 0 );
500 if ( tmp_be != NULL && tmp_be != be ) {
502 "%s: line %d: suffix already in use by another backend in"
503 " \"suffixMassage <suffix> <massaged suffix>\"\n",
510 dn.bv_val = argv[ 2 ];
511 dn.bv_len = strlen( argv[ 2 ] );
512 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
513 fprintf( stderr, "%s: line %d: "
514 "massaged suffix '%s' is invalid\n",
515 fname, lineno, argv[ 2 ] );
522 tmp_be = select_backend( &nrnc, 0, 0 );
523 if ( tmp_be != NULL ) {
525 "%s: line %d: massaged suffix already in use by another backend in"
526 " \"suffixMassage <suffix> <massaged suffix>\"\n",
537 * The suffix massaging is emulated by means of the
538 * rewrite capabilities
539 * FIXME: no extra rewrite capabilities should be added
542 return suffix_massage_config( li->targets[ i ]->mt_rwmap.rwm_rw,
543 &pvnc, &nvnc, &prnc, &nrnc );
545 /* rewrite stuff ... */
546 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
547 int i = li->ntargets-1;
550 if ( strcasecmp( argv[0], "rewriteEngine" ) == 0 ) {
551 li->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
553 return rewrite_parse( li->rwinfo, fname, lineno,
557 return rewrite_parse( li->targets[ i ]->mt_rwmap.rwm_rw,
558 fname, lineno, argc, argv );
560 /* objectclass/attribute mapping */
561 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
562 int i = li->ntargets-1;
566 "%s: line %d: need \"uri\" directive first\n",
571 return ldap_back_map_config( &li->targets[ i ]->mt_rwmap.rwm_oc,
572 &li->targets[ i ]->mt_rwmap.rwm_at,
573 fname, lineno, argc, argv );
576 return SLAP_CONF_UNKNOWN;
582 ldap_back_map_config(
583 struct ldapmap *oc_map,
584 struct ldapmap *at_map,
591 struct ldapmapping *mapping;
595 if ( argc < 3 || argc > 4 ) {
597 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
602 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
606 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
610 fprintf( stderr, "%s: line %d: syntax is "
611 "\"map {objectclass | attribute} [<local> | *] "
612 "{<foreign> | *}\"\n",
617 if ( strcmp( argv[2], "*" ) == 0 ) {
618 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
619 map->drop_missing = ( argc < 4 );
624 } else if ( argc < 4 ) {
630 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
633 if ( ( map == at_map )
634 && ( strcasecmp( src, "objectclass" ) == 0
635 || strcasecmp( dst, "objectclass" ) == 0 ) )
638 "%s: line %d: objectclass attribute cannot be mapped\n",
642 mapping = (struct ldapmapping *)ch_calloc( 2,
643 sizeof(struct ldapmapping) );
644 if ( mapping == NULL ) {
646 "%s: line %d: out of memory\n",
650 ber_str2bv( src, 0, 1, &mapping->src );
651 ber_str2bv( dst, 0, 1, &mapping->dst );
652 mapping[1].src = mapping->dst;
653 mapping[1].dst = mapping->src;
659 if ( src[0] != '\0' ) {
660 if ( oc_bvfind( &mapping->src ) == NULL ) {
662 "%s: line %d: warning, source objectClass '%s' "
663 "should be defined in schema\n",
664 fname, lineno, src );
667 * FIXME: this should become an err
673 if ( oc_bvfind( &mapping->dst ) == NULL ) {
675 "%s: line %d: warning, destination objectClass '%s' "
676 "is not defined in schema\n",
677 fname, lineno, dst );
681 const char *text = NULL;
682 AttributeDescription *ad = NULL;
684 if ( src[0] != '\0' ) {
685 rc = slap_bv2ad( &mapping->src, &ad, &text );
686 if ( rc != LDAP_SUCCESS ) {
688 "%s: line %d: warning, source attributeType '%s' "
689 "should be defined in schema\n",
690 fname, lineno, src );
693 * FIXME: this should become an err
701 rc = slap_bv2ad( &mapping->dst, &ad, &text );
702 if ( rc != LDAP_SUCCESS ) {
704 "%s: line %d: warning, destination attributeType '%s' "
705 "is not defined in schema\n",
706 fname, lineno, dst );
710 if ( (src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL)
711 || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
714 "%s: line %d: duplicate mapping found (ignored)\n",
719 if ( src[0] != '\0' ) {
720 avl_insert( &map->map, (caddr_t)mapping,
721 mapping_cmp, mapping_dup );
723 avl_insert( &map->remap, (caddr_t)&mapping[1],
724 mapping_cmp, mapping_dup );
730 ch_free( mapping->src.bv_val );
731 ch_free( mapping->dst.bv_val );
739 #ifdef ENABLE_REWRITE
741 suffix_massage_regexize( const char *s )
748 ( r = strchr( p, ',' ) ) != NULL;
752 res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
754 ptr = lutil_strcopy( res, "(.*)" );
756 ( r = strchr( p, ',' ) ) != NULL;
758 ptr = lutil_strncopy( ptr, p, r - p + 1 );
759 ptr = lutil_strcopy( ptr, "[ ]?" );
761 if ( r[ 1 ] == ' ' ) {
765 lutil_strcopy( ptr, p );
771 suffix_massage_patternize( const char *s )
778 res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
784 strcpy( res + sizeof( "%1" ) - 1, s );
790 suffix_massage_config(
791 struct rewrite_info *info,
801 rargv[ 0 ] = "rewriteEngine";
804 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
806 rargv[ 0 ] = "rewriteContext";
807 rargv[ 1 ] = "default";
809 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
811 rargv[ 0 ] = "rewriteRule";
812 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
813 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
816 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
817 ch_free( rargv[ 1 ] );
818 ch_free( rargv[ 2 ] );
820 rargv[ 0 ] = "rewriteContext";
821 rargv[ 1 ] = "searchEntryDN";
823 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
825 rargv[ 0 ] = "rewriteRule";
826 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
827 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
830 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
831 ch_free( rargv[ 1 ] );
832 ch_free( rargv[ 2 ] );
834 /* backward compatibility */
835 rargv[ 0 ] = "rewriteContext";
836 rargv[ 1 ] = "searchResult";
837 rargv[ 2 ] = "alias";
838 rargv[ 3 ] = "searchEntryDN";
840 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
842 rargv[ 0 ] = "rewriteContext";
843 rargv[ 1 ] = "matchedDN";
844 rargv[ 2 ] = "alias";
845 rargv[ 3 ] = "searchEntryDN";
847 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
849 rargv[ 0 ] = "rewriteContext";
850 rargv[ 1 ] = "searchAttrDN";
851 rargv[ 2 ] = "alias";
852 rargv[ 3 ] = "searchEntryDN";
854 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
856 /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
857 * see servers/slapd/overlays/rwm.h for details */
858 rargv[ 0 ] = "rewriteContext";
859 rargv[ 1 ] = "referralAttrDN";
861 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
863 rargv[ 0 ] = "rewriteContext";
864 rargv[ 1 ] = "referralDN";
866 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
870 #endif /* ENABLE_REWRITE */