1 /* config.c - ldap backend configuration file routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2005 The OpenLDAP Foundation.
6 * Portions Copyright 1999-2003 Howard Chu.
7 * Portions Copyright 2000-2003 Pierangelo Masarati.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted only as authorized by the OpenLDAP
14 * A copy of this license is available in the file LICENSE in the
15 * top-level directory of the distribution or, alternatively, at
16 * <http://www.OpenLDAP.org/license.html>.
19 * This work was initially developed by the Howard Chu for inclusion
20 * in OpenLDAP Software and subsequently enhanced by Pierangelo
28 #include <ac/string.h>
29 #include <ac/socket.h>
32 #include "back-ldap.h"
35 /* for advanced URL parsing */
36 #include "../../../libraries/libldap/ldap-int.h"
38 static SLAP_EXTOP_MAIN_FN ldap_back_exop_whoami;
49 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
52 fprintf( stderr, "%s: line %d: ldap backend info is null!\n",
57 /* server address to query (depricated, use "uri" directive) */
58 if ( strcasecmp( argv[0], "server" ) == 0 ) {
61 "%s: line %d: missing address in \"server <address>\" line\n",
67 li->url = ch_calloc(strlen(argv[1]) + 9, sizeof(char));
68 if (li->url != NULL) {
69 strcpy(li->url, "ldap://");
70 strcat(li->url, argv[1]);
74 /* URI of server to query (preferred over "server" directive) */
75 } else if ( strcasecmp( argv[0], "uri" ) == 0 ) {
80 fprintf( stderr, "%s: line %d: "
82 "in \"uri <uri>\" line\n",
86 if ( li->url != NULL ) {
89 if ( li->lud != NULL ) {
90 ldap_free_urldesc( li->lud );
93 urlrc = ldap_url_parselist( &li->lud, argv[ 1 ] );
94 if ( urlrc != LDAP_URL_SUCCESS ) {
98 case LDAP_URL_ERR_MEM:
101 case LDAP_URL_ERR_PARAM:
102 why = "parameter is bad";
104 case LDAP_URL_ERR_BADSCHEME:
105 why = "URL doesn't begin with \"[c]ldap[si]://\"";
107 case LDAP_URL_ERR_BADENCLOSURE:
108 why = "URL is missing trailing \">\"";
110 case LDAP_URL_ERR_BADURL:
112 case LDAP_URL_ERR_BADHOST:
113 why = "host/port is bad";
115 case LDAP_URL_ERR_BADATTRS:
116 why = "bad (or missing) attributes";
118 case LDAP_URL_ERR_BADSCOPE:
119 why = "scope string is invalid (or missing)";
121 case LDAP_URL_ERR_BADFILTER:
122 why = "bad or missing filter";
124 case LDAP_URL_ERR_BADEXTS:
125 why = "bad or missing extensions";
128 why = "unknown reason";
131 fprintf( stderr, "%s: line %d: "
132 "unable to parse uri \"%s\" "
133 "in \"uri <uri>\" line: %s\n",
134 fname, lineno, argv[ 1 ], why );
138 for ( i = 0, tmpludp = li->lud;
140 i++, tmpludp = tmpludp->lud_next )
142 if ( ( tmpludp->lud_dn != NULL
143 && tmpludp->lud_dn[0] != '\0' )
144 || tmpludp->lud_attrs != NULL
145 || tmpludp->lud_filter != NULL
146 || tmpludp->lud_exts != NULL )
148 fprintf( stderr, "%s: line %d: "
149 "warning, only protocol, "
150 "host and port allowed "
151 "in \"uri <uri>\" statement "
152 "for uri #%d of \"%s\"\n",
153 fname, lineno, i, argv[1] );
157 li->url = ch_strdup( argv[ 1 ] );
159 /* name to use for ldap_back_group */
160 } else if ( strcasecmp( argv[0], "binddn" ) == 0 ) {
163 "%s: line %d: missing name in \"binddn <name>\" line\n",
167 ber_str2bv( argv[1], 0, 1, &li->binddn );
169 /* password to use for ldap_back_group */
170 } else if ( strcasecmp( argv[0], "bindpw" ) == 0 ) {
173 "%s: line %d: missing password in \"bindpw <password>\" line\n",
177 ber_str2bv( argv[1], 0, 1, &li->bindpw );
179 #ifdef LDAP_BACK_PROXY_AUTHZ
180 /* name to use for proxyAuthz propagation */
181 } else if ( strcasecmp( argv[0], "proxyauthzdn" ) == 0 ) {
184 "%s: line %d: missing name in \"proxyauthzdn <name>\" line\n",
188 ber_str2bv( argv[1], 0, 1, &li->proxyauthzdn );
190 /* password to use for proxyAuthz propagation */
191 } else if ( strcasecmp( argv[0], "proxyauthzpw" ) == 0 ) {
194 "%s: line %d: missing password in \"proxyauthzpw <password>\" line\n",
198 ber_str2bv( argv[1], 0, 1, &li->proxyauthzpw );
199 #endif /* LDAP_BACK_PROXY_AUTHZ */
201 /* save bind creds for referral rebinds? */
202 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
205 "%s: line %d: rebind-as-user takes no arguments\n",
211 /* intercept exop_who_am_i? */
212 } else if ( strcasecmp( argv[0], "proxy-whoami" ) == 0 ) {
215 "%s: line %d: proxy-whoami takes no arguments\n",
219 load_extop( (struct berval *)&slap_EXOP_WHOAMI,
220 0, ldap_back_exop_whoami );
223 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
225 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
226 #ifdef ENABLE_REWRITE
228 #endif /* ENABLE_REWRITE */
233 * suffixmassage <suffix> <massaged suffix>
235 * the <suffix> field must be defined as a valid suffix
236 * (or suffixAlias?) for the current database;
237 * the <massaged suffix> shouldn't have already been
238 * defined as a valid suffix or suffixAlias for the
242 fprintf( stderr, "%s: line %d: syntax is"
243 " \"suffixMassage <suffix>"
244 " <massaged suffix>\"\n",
249 ber_str2bv( argv[1], 0, 0, &bvnc );
250 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
251 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
252 fname, lineno, bvnc.bv_val );
255 tmp_be = select_backend( &nvnc, 0, 0 );
256 if ( tmp_be != NULL && tmp_be != be ) {
257 fprintf( stderr, "%s: line %d: suffix already in use"
258 " by another backend in"
259 " \"suffixMassage <suffix>"
260 " <massaged suffix>\"\n",
267 ber_str2bv( argv[2], 0, 0, &brnc );
268 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
269 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
270 fname, lineno, brnc.bv_val );
277 tmp_be = select_backend( &nrnc, 0, 0 );
278 if ( tmp_be != NULL ) {
279 fprintf( stderr, "%s: line %d: massaged suffix"
280 " already in use by another backend in"
281 " \"suffixMassage <suffix>"
282 " <massaged suffix>\"\n",
292 #ifdef ENABLE_REWRITE
294 * The suffix massaging is emulated by means of the
295 * rewrite capabilities
296 * FIXME: no extra rewrite capabilities should be added
299 rc = suffix_massage_config( li->rwmap.rwm_rw,
300 &pvnc, &nvnc, &prnc, &nrnc );
308 #else /* !ENABLE_REWRITE */
309 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &pvnc );
310 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &nvnc );
312 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &prnc );
313 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &nrnc );
314 #endif /* !ENABLE_REWRITE */
316 /* rewrite stuff ... */
317 } else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
318 #ifdef ENABLE_REWRITE
319 return rewrite_parse( li->rwmap.rwm_rw,
320 fname, lineno, argc, argv );
322 #else /* !ENABLE_REWRITE */
323 fprintf( stderr, "%s: line %d: rewrite capabilities "
324 "are not enabled\n", fname, lineno );
325 #endif /* !ENABLE_REWRITE */
327 /* objectclass/attribute mapping */
328 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
329 return ldap_back_map_config( &li->rwmap.rwm_oc,
331 fname, lineno, argc, argv );
335 return SLAP_CONF_UNKNOWN;
341 ldap_back_map_config(
342 struct ldapmap *oc_map,
343 struct ldapmap *at_map,
350 struct ldapmapping *mapping;
354 if ( argc < 3 || argc > 4 ) {
356 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
361 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
365 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
369 fprintf( stderr, "%s: line %d: syntax is "
370 "\"map {objectclass | attribute} [<local> | *] "
371 "{<foreign> | *}\"\n",
376 if ( strcmp( argv[2], "*" ) == 0 ) {
377 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
378 map->drop_missing = ( argc < 4 );
383 } else if ( argc < 4 ) {
389 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
392 if ( ( map == at_map )
393 && ( strcasecmp( src, "objectclass" ) == 0
394 || strcasecmp( dst, "objectclass" ) == 0 ) )
397 "%s: line %d: objectclass attribute cannot be mapped\n",
401 mapping = (struct ldapmapping *)ch_calloc( 2,
402 sizeof(struct ldapmapping) );
403 if ( mapping == NULL ) {
405 "%s: line %d: out of memory\n",
409 ber_str2bv( src, 0, 1, &mapping->src );
410 ber_str2bv( dst, 0, 1, &mapping->dst );
411 mapping[1].src = mapping->dst;
412 mapping[1].dst = mapping->src;
418 if ( src[0] != '\0' ) {
419 if ( oc_bvfind( &mapping->src ) == NULL ) {
421 "%s: line %d: warning, source objectClass '%s' "
422 "should be defined in schema\n",
423 fname, lineno, src );
426 * FIXME: this should become an err
432 if ( oc_bvfind( &mapping->dst ) == NULL ) {
434 "%s: line %d: warning, destination objectClass '%s' "
435 "is not defined in schema\n",
436 fname, lineno, dst );
440 const char *text = NULL;
441 AttributeDescription *ad = NULL;
443 if ( src[0] != '\0' ) {
444 rc = slap_bv2ad( &mapping->src, &ad, &text );
445 if ( rc != LDAP_SUCCESS ) {
447 "%s: line %d: warning, source attributeType '%s' "
448 "should be defined in schema\n",
449 fname, lineno, src );
452 * FIXME: this should become an err
460 rc = slap_bv2ad( &mapping->dst, &ad, &text );
461 if ( rc != LDAP_SUCCESS ) {
463 "%s: line %d: warning, destination attributeType '%s' "
464 "is not defined in schema\n",
465 fname, lineno, dst );
469 if ( (src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL)
470 || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
473 "%s: line %d: duplicate mapping found (ignored)\n",
478 if ( src[0] != '\0' ) {
479 avl_insert( &map->map, (caddr_t)mapping,
480 mapping_cmp, mapping_dup );
482 avl_insert( &map->remap, (caddr_t)&mapping[1],
483 mapping_cmp, mapping_dup );
489 ch_free( mapping->src.bv_val );
490 ch_free( mapping->dst.bv_val );
498 ldap_back_exop_whoami(
502 struct berval *bv = NULL;
504 if ( op->oq_extended.rs_reqdata != NULL ) {
505 /* no request data should be provided */
506 rs->sr_text = "no request data expected";
507 return rs->sr_err = LDAP_PROTOCOL_ERROR;
510 rs->sr_err = backend_check_restrictions( op, rs,
511 (struct berval *)&slap_EXOP_WHOAMI );
512 if( rs->sr_err != LDAP_SUCCESS ) return rs->sr_err;
514 /* if auth'd by back-ldap and request is proxied, forward it */
515 if ( op->o_conn->c_authz_backend && !strcmp(op->o_conn->c_authz_backend->be_type, "ldap" ) && !dn_match(&op->o_ndn, &op->o_conn->c_ndn)) {
518 LDAPControl c, *ctrls[2] = {NULL, NULL};
525 op2.o_ndn = op->o_conn->c_ndn;
526 lc = ldap_back_getconn(&op2, rs);
527 if (!lc || !ldap_back_dobind( lc, op, rs )) {
530 c.ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
531 c.ldctl_iscritical = 1;
532 c.ldctl_value.bv_val = ch_malloc(op->o_ndn.bv_len+4);
533 c.ldctl_value.bv_len = op->o_ndn.bv_len + 3;
534 strcpy(c.ldctl_value.bv_val, "dn:");
535 strcpy(c.ldctl_value.bv_val+3, op->o_ndn.bv_val);
538 rs->sr_err = ldap_whoami(lc->ld, ctrls, NULL, &msgid);
539 if (rs->sr_err == LDAP_SUCCESS) {
540 if (ldap_result(lc->ld, msgid, 1, NULL, &res) == -1) {
541 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER,
543 if ( rs->sr_err = LDAP_SERVER_DOWN && do_retry ) {
545 if ( ldap_back_retry( lc, op, rs ))
548 ldap_back_freeconn( op, lc );
552 rs->sr_err = ldap_parse_whoami(lc->ld, res, &bv);
556 ch_free(c.ldctl_value.bv_val);
557 if (rs->sr_err != LDAP_SUCCESS) {
558 rs->sr_err = slap_map_api2result( rs );
561 /* else just do the same as before */
562 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
563 if( op->o_dn.bv_len ) {
564 bv->bv_len = op->o_dn.bv_len + sizeof("dn:") - 1;
565 bv->bv_val = ch_malloc( bv->bv_len + 1 );
566 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:") - 1 );
567 AC_MEMCPY( &bv->bv_val[sizeof("dn:") - 1], op->o_dn.bv_val,
569 bv->bv_val[bv->bv_len] = '\0';
581 #ifdef ENABLE_REWRITE
583 suffix_massage_regexize( const char *s )
590 ( r = strchr( p, ',' ) ) != NULL;
594 res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
596 ptr = lutil_strcopy( res, "(.*)" );
598 ( r = strchr( p, ',' ) ) != NULL;
600 ptr = lutil_strncopy( ptr, p, r - p + 1 );
601 ptr = lutil_strcopy( ptr, "[ ]?" );
603 if ( r[ 1 ] == ' ' ) {
607 lutil_strcopy( ptr, p );
613 suffix_massage_patternize( const char *s )
620 res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
626 strcpy( res + sizeof( "%1" ) - 1, s );
632 suffix_massage_config(
633 struct rewrite_info *info,
643 rargv[ 0 ] = "rewriteEngine";
646 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
648 rargv[ 0 ] = "rewriteContext";
649 rargv[ 1 ] = "default";
651 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
653 rargv[ 0 ] = "rewriteRule";
654 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
655 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
658 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
659 ch_free( rargv[ 1 ] );
660 ch_free( rargv[ 2 ] );
662 rargv[ 0 ] = "rewriteContext";
663 rargv[ 1 ] = "searchResult";
665 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
667 rargv[ 0 ] = "rewriteRule";
668 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
669 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
672 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
673 ch_free( rargv[ 1 ] );
674 ch_free( rargv[ 2 ] );
676 rargv[ 0 ] = "rewriteContext";
677 rargv[ 1 ] = "matchedDN";
678 rargv[ 2 ] = "alias";
679 rargv[ 3 ] = "searchResult";
681 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
683 rargv[ 0 ] = "rewriteContext";
684 rargv[ 1 ] = "searchAttrDN";
685 rargv[ 2 ] = "alias";
686 rargv[ 3 ] = "searchResult";
688 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
692 #endif /* ENABLE_REWRITE */