1 /* config.c - ldap backend configuration file routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003-2004 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 static SLAP_EXTOP_MAIN_FN ldap_back_exop_whoami;
38 parse_idassert( BackendDB *be, const char *fname, int lineno,
39 int argc, char **argv );
50 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
53 fprintf( stderr, "%s: line %d: ldap backend info is null!\n",
58 /* server address to query (depricated, use "uri" directive) */
59 if ( strcasecmp( argv[0], "server" ) == 0 ) {
62 "%s: line %d: missing address in \"server <address>\" line\n",
68 li->url = ch_calloc(strlen(argv[1]) + 9, sizeof(char));
69 if (li->url != NULL) {
70 strcpy(li->url, "ldap://");
71 strcat(li->url, argv[1]);
75 /* URI of server to query (preferred over "server" directive) */
76 } 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 if ( ldap_url_parse( argv[ 1 ], &li->lud ) != LDAP_URL_SUCCESS ) {
94 fprintf( stderr, "%s: line %d: "
95 "unable to parse uri \"%s\" "
96 "in \"uri <uri>\" line\n",
97 fname, lineno, argv[ 1 ] );
101 if ( ( li->lud->lud_dn != NULL && li->lud->lud_dn[0] != '\0' )
102 || li->lud->lud_attrs != NULL
103 || li->lud->lud_filter != NULL
104 || li->lud->lud_exts != NULL )
106 fprintf( stderr, "%s: line %d: "
107 "warning, only protocol, "
108 "host and port allowed "
109 "in \"uri <uri>\" line\n",
116 tmplud.lud_attrs = NULL;
117 tmplud.lud_filter = NULL;
118 if ( !ldap_is_ldapi_url( argv[ 1 ] ) ) {
119 tmplud.lud_exts = NULL;
120 tmplud.lud_crit_exts = 0;
123 li->url = ldap_url_desc2str( &tmplud );
124 if ( li->url == NULL ) {
125 fprintf( stderr, "%s: line %d: "
126 "unable to rebuild uri \"%s\" "
127 "in \"uri <uri>\" line\n",
128 fname, lineno, argv[ 1 ] );
132 li->url = ch_strdup( argv[ 1 ] );
135 /* name to use for ldap_back_group */
136 } else if ( strcasecmp( argv[0], "binddn" ) == 0 ) {
139 "%s: line %d: missing name in \"binddn <name>\" line\n",
143 ber_str2bv( argv[1], 0, 1, &li->binddn );
145 /* password to use for ldap_back_group */
146 } else if ( strcasecmp( argv[0], "bindpw" ) == 0 ) {
149 "%s: line %d: missing password in \"bindpw <password>\" line\n",
153 ber_str2bv( argv[1], 0, 1, &li->bindpw );
155 #ifdef LDAP_BACK_PROXY_AUTHZ
156 /* name to use for proxyAuthz propagation */
157 } else if ( strcasecmp( argv[0], "proxyauthzdn" ) == 0 ) {
160 "%s: line %d: missing name in \"proxyauthzdn <name>\" line\n",
164 ber_str2bv( argv[1], 0, 1, &li->proxyauthzdn );
166 /* password to use for proxyAuthz propagation */
167 } else if ( strcasecmp( argv[0], "proxyauthzpw" ) == 0 ) {
170 "%s: line %d: missing password in \"proxyauthzpw <password>\" line\n",
174 ber_str2bv( argv[1], 0, 1, &li->proxyauthzpw );
176 /* identity assertion stuff... */
177 } else if ( strncasecmp( argv[0], "idassert-", STRLENOF( "idassert-" ) ) == 0 ) {
178 return parse_idassert( be, fname, lineno, argc, argv );
179 #endif /* LDAP_BACK_PROXY_AUTHZ */
181 /* save bind creds for referral rebinds? */
182 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
185 "%s: line %d: rebind-as-user takes no arguments\n",
191 /* intercept exop_who_am_i? */
192 } else if ( strcasecmp( argv[0], "proxy-whoami" ) == 0 ) {
195 "%s: line %d: proxy-whoami takes no arguments\n",
199 load_extop( (struct berval *)&slap_EXOP_WHOAMI,
200 0, ldap_back_exop_whoami );
203 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
205 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
206 #ifdef ENABLE_REWRITE
208 #endif /* ENABLE_REWRITE */
213 * suffixmassage <suffix> <massaged suffix>
215 * the <suffix> field must be defined as a valid suffix
216 * (or suffixAlias?) for the current database;
217 * the <massaged suffix> shouldn't have already been
218 * defined as a valid suffix or suffixAlias for the
222 fprintf( stderr, "%s: line %d: syntax is"
223 " \"suffixMassage <suffix>"
224 " <massaged suffix>\"\n",
229 ber_str2bv( argv[1], 0, 0, &bvnc );
230 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
231 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
232 fname, lineno, bvnc.bv_val );
235 tmp_be = select_backend( &nvnc, 0, 0 );
236 if ( tmp_be != NULL && tmp_be != be ) {
237 fprintf( stderr, "%s: line %d: suffix already in use"
238 " by another backend in"
239 " \"suffixMassage <suffix>"
240 " <massaged suffix>\"\n",
247 ber_str2bv( argv[2], 0, 0, &brnc );
248 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
249 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
250 fname, lineno, brnc.bv_val );
257 tmp_be = select_backend( &nrnc, 0, 0 );
258 if ( tmp_be != NULL ) {
259 fprintf( stderr, "%s: line %d: massaged suffix"
260 " already in use by another backend in"
261 " \"suffixMassage <suffix>"
262 " <massaged suffix>\"\n",
272 #ifdef ENABLE_REWRITE
274 * The suffix massaging is emulated by means of the
275 * rewrite capabilities
276 * FIXME: no extra rewrite capabilities should be added
279 rc = suffix_massage_config( li->rwmap.rwm_rw,
280 &pvnc, &nvnc, &prnc, &nrnc );
288 #else /* !ENABLE_REWRITE */
289 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &pvnc );
290 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &nvnc );
292 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &prnc );
293 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &nrnc );
294 #endif /* !ENABLE_REWRITE */
296 /* rewrite stuff ... */
297 } else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
298 #ifdef ENABLE_REWRITE
299 return rewrite_parse( li->rwmap.rwm_rw,
300 fname, lineno, argc, argv );
302 #else /* !ENABLE_REWRITE */
303 fprintf( stderr, "%s: line %d: rewrite capabilities "
304 "are not enabled\n", fname, lineno );
305 #endif /* !ENABLE_REWRITE */
307 /* objectclass/attribute mapping */
308 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
309 return ldap_back_map_config( &li->rwmap.rwm_oc,
311 fname, lineno, argc, argv );
315 return SLAP_CONF_UNKNOWN;
321 ldap_back_map_config(
322 struct ldapmap *oc_map,
323 struct ldapmap *at_map,
330 struct ldapmapping *mapping;
334 if ( argc < 3 || argc > 4 ) {
336 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
341 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
345 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
349 fprintf( stderr, "%s: line %d: syntax is "
350 "\"map {objectclass | attribute} [<local> | *] "
351 "{<foreign> | *}\"\n",
356 if ( strcmp( argv[2], "*" ) == 0 ) {
357 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
358 map->drop_missing = ( argc < 4 );
363 } else if ( argc < 4 ) {
369 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
372 if ( ( map == at_map )
373 && ( strcasecmp( src, "objectclass" ) == 0
374 || strcasecmp( dst, "objectclass" ) == 0 ) )
377 "%s: line %d: objectclass attribute cannot be mapped\n",
381 mapping = (struct ldapmapping *)ch_calloc( 2,
382 sizeof(struct ldapmapping) );
383 if ( mapping == NULL ) {
385 "%s: line %d: out of memory\n",
389 ber_str2bv( src, 0, 1, &mapping->src );
390 ber_str2bv( dst, 0, 1, &mapping->dst );
391 mapping[1].src = mapping->dst;
392 mapping[1].dst = mapping->src;
398 if ( src[0] != '\0' ) {
399 if ( oc_bvfind( &mapping->src ) == NULL ) {
401 "%s: line %d: warning, source objectClass '%s' "
402 "should be defined in schema\n",
403 fname, lineno, src );
406 * FIXME: this should become an err
412 if ( oc_bvfind( &mapping->dst ) == NULL ) {
414 "%s: line %d: warning, destination objectClass '%s' "
415 "is not defined in schema\n",
416 fname, lineno, dst );
420 const char *text = NULL;
421 AttributeDescription *ad = NULL;
423 if ( src[0] != '\0' ) {
424 rc = slap_bv2ad( &mapping->src, &ad, &text );
425 if ( rc != LDAP_SUCCESS ) {
427 "%s: line %d: warning, source attributeType '%s' "
428 "should be defined in schema\n",
429 fname, lineno, src );
432 * FIXME: this should become an err
440 rc = slap_bv2ad( &mapping->dst, &ad, &text );
441 if ( rc != LDAP_SUCCESS ) {
443 "%s: line %d: warning, destination attributeType '%s' "
444 "is not defined in schema\n",
445 fname, lineno, dst );
449 if ( (src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL)
450 || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
453 "%s: line %d: duplicate mapping found (ignored)\n",
458 if ( src[0] != '\0' ) {
459 avl_insert( &map->map, (caddr_t)mapping,
460 mapping_cmp, mapping_dup );
462 avl_insert( &map->remap, (caddr_t)&mapping[1],
463 mapping_cmp, mapping_dup );
469 ch_free( mapping->src.bv_val );
470 ch_free( mapping->dst.bv_val );
478 ldap_back_exop_whoami(
482 struct berval *bv = NULL;
484 if ( op->oq_extended.rs_reqdata != NULL ) {
485 /* no request data should be provided */
486 rs->sr_text = "no request data expected";
487 return rs->sr_err = LDAP_PROTOCOL_ERROR;
490 rs->sr_err = backend_check_restrictions( op, rs,
491 (struct berval *)&slap_EXOP_WHOAMI );
492 if( rs->sr_err != LDAP_SUCCESS ) return rs->sr_err;
494 /* if auth'd by back-ldap and request is proxied, forward it */
495 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)) {
498 LDAPControl c, *ctrls[2] = {NULL, NULL};
504 op2.o_ndn = op->o_conn->c_ndn;
505 lc = ldap_back_getconn(&op2, rs);
506 if (!lc || !ldap_back_dobind( lc, op, rs )) {
509 c.ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
510 c.ldctl_iscritical = 1;
511 c.ldctl_value.bv_val = ch_malloc(op->o_ndn.bv_len+4);
512 c.ldctl_value.bv_len = op->o_ndn.bv_len + 3;
513 strcpy(c.ldctl_value.bv_val, "dn:");
514 strcpy(c.ldctl_value.bv_val+3, op->o_ndn.bv_val);
516 rs->sr_err = ldap_whoami(lc->ld, ctrls, NULL, &msgid);
517 if (rs->sr_err == LDAP_SUCCESS) {
518 if (ldap_result(lc->ld, msgid, 1, NULL, &res) == -1) {
519 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER,
522 rs->sr_err = ldap_parse_whoami(lc->ld, res, &bv);
526 ch_free(c.ldctl_value.bv_val);
527 if (rs->sr_err != LDAP_SUCCESS) {
528 rs->sr_err = slap_map_api2result( rs );
531 /* else just do the same as before */
532 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
533 if( op->o_dn.bv_len ) {
534 bv->bv_len = op->o_dn.bv_len + sizeof("dn:") - 1;
535 bv->bv_val = ch_malloc( bv->bv_len + 1 );
536 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:") - 1 );
537 AC_MEMCPY( &bv->bv_val[sizeof("dn:") - 1], op->o_dn.bv_val,
539 bv->bv_val[bv->bv_len] = '\0';
551 #ifdef ENABLE_REWRITE
553 suffix_massage_regexize( const char *s )
560 ( r = strchr( p, ',' ) ) != NULL;
564 res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
566 ptr = lutil_strcopy( res, "(.*)" );
568 ( r = strchr( p, ',' ) ) != NULL;
570 ptr = lutil_strncopy( ptr, p, r - p + 1 );
571 ptr = lutil_strcopy( ptr, "[ ]?" );
573 if ( r[ 1 ] == ' ' ) {
577 lutil_strcopy( ptr, p );
583 suffix_massage_patternize( const char *s )
590 res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
596 strcpy( res + sizeof( "%1" ) - 1, s );
602 suffix_massage_config(
603 struct rewrite_info *info,
613 rargv[ 0 ] = "rewriteEngine";
616 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
618 rargv[ 0 ] = "rewriteContext";
619 rargv[ 1 ] = "default";
621 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
623 rargv[ 0 ] = "rewriteRule";
624 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
625 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
628 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
629 ch_free( rargv[ 1 ] );
630 ch_free( rargv[ 2 ] );
632 rargv[ 0 ] = "rewriteContext";
633 rargv[ 1 ] = "searchResult";
635 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
637 rargv[ 0 ] = "rewriteRule";
638 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
639 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
642 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
643 ch_free( rargv[ 1 ] );
644 ch_free( rargv[ 2 ] );
646 rargv[ 0 ] = "rewriteContext";
647 rargv[ 1 ] = "matchedDN";
648 rargv[ 2 ] = "alias";
649 rargv[ 3 ] = "searchResult";
651 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
653 rargv[ 0 ] = "rewriteContext";
654 rargv[ 1 ] = "searchAttrDN";
655 rargv[ 2 ] = "alias";
656 rargv[ 3 ] = "searchResult";
658 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
662 #endif /* ENABLE_REWRITE */
664 #ifdef LDAP_BACK_PROXY_AUTHZ
674 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
676 if ( strcasecmp( argv[0], "idassert-mode" ) == 0 ) {
679 LDAP_LOG( CONFIG, CRIT,
680 "%s: line %d: illegal args number %d in \"idassert-mode <args>\" line.\n",
681 fname, lineno, argc );
683 Debug( LDAP_DEBUG_ANY,
684 "%s: line %d: illegal args number %d in \"idassert-mode <args>\" line.\n",
685 fname, lineno, argc );
690 if ( strcasecmp( argv[1], "none" ) == 0 ) {
691 /* will proxyAuthz as client's identity only if bound */
692 li->idassert_mode = LDAP_BACK_IDASSERT_NONE;
694 } else if ( strcasecmp( argv[1], "self" ) == 0 ) {
695 /* will proxyAuthz as client's identity */
696 li->idassert_mode = LDAP_BACK_IDASSERT_SELF;
698 } else if ( strcasecmp( argv[1], "anonymous" ) == 0 ) {
699 /* will proxyAuthz as anonymous */
700 li->idassert_mode = LDAP_BACK_IDASSERT_ANONYMOUS;
702 } else if ( strcasecmp( argv[1], "proxyid" ) == 0 ) {
703 /* will not proxyAuthz */
704 li->idassert_mode = LDAP_BACK_IDASSERT_PROXYID;
710 /* will proxyAuthz as argv[1] */
711 li->idassert_mode = LDAP_BACK_IDASSERT_OTHER;
713 ber_str2bv( argv[1], 0, 0, &dn );
715 rc = dnNormalize( 0, NULL, NULL, &dn, &li->idassert_dn, NULL );
716 if ( rc != LDAP_SUCCESS ) {
718 LDAP_LOG( CONFIG, CRIT,
719 "%s: line %d: idassert DN \"%s\" is invalid.\n",
720 fname, lineno, argv[1] );
722 Debug( LDAP_DEBUG_ANY,
723 "%s: line %d: idassert DN \"%s\" is invalid\n",
724 fname, lineno, argv[1] );
730 } else if ( strcasecmp( argv[0], "idassert-authz" ) == 0 ) {
733 ber_str2bv( argv[1], 0, 1, &rule );
735 ber_bvarray_add( &li->idassert_authz, &rule );
738 return SLAP_CONF_UNKNOWN;
743 #endif /* LDAP_BACK_PROXY_AUTHZ */