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 /* for advanced URL parsing */
36 #include "../../../libraries/libldap/ldap-int.h"
38 static SLAP_EXTOP_MAIN_FN ldap_back_exop_whoami;
41 parse_idassert( BackendDB *be, const char *fname, int lineno,
42 int argc, char **argv );
52 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
55 fprintf( stderr, "%s: line %d: ldap backend info is null!\n",
60 /* server address to query (depricated, use "uri" directive) */
61 if ( strcasecmp( argv[0], "server" ) == 0 ) {
65 "%s: line %d: \"server <address>\" directive is deprecated\n",
70 "%s: line %d: missing address in \"server <address>\" line\n",
74 if ( li->url != NULL ) {
77 l = strlen( argv[1] ) + STRLENOF( "ldap:///") + 1;
78 li->url = ch_calloc( l, sizeof( char ) );
79 if ( li->url == NULL ) {
80 fprintf( stderr, "%s: line %d: malloc failed\n" );
84 snprintf( li->url, l, "ldap://%s/", argv[1] );
86 /* URI of server to query (preferred over "server" directive) */
87 } else if ( strcasecmp( argv[0], "uri" ) == 0 ) {
92 fprintf( stderr, "%s: line %d: "
94 "in \"uri <uri>\" line\n",
98 if ( li->url != NULL ) {
101 if ( li->lud != NULL ) {
102 ldap_free_urllist( li->lud );
106 /* PARANOID: DN and more are not required nor allowed */
107 urlrc = ldap_url_parselist_ext( &li->lud, argv[ 1 ], "\t" );
109 urlrc = ldap_url_parselist( &li->lud, argv[ 1 ] );
111 if ( urlrc != LDAP_URL_SUCCESS ) {
115 case LDAP_URL_ERR_MEM:
118 case LDAP_URL_ERR_PARAM:
119 why = "parameter is bad";
121 case LDAP_URL_ERR_BADSCHEME:
122 why = "URL doesn't begin with \"[c]ldap[si]://\"";
124 case LDAP_URL_ERR_BADENCLOSURE:
125 why = "URL is missing trailing \">\"";
127 case LDAP_URL_ERR_BADURL:
129 case LDAP_URL_ERR_BADHOST:
130 why = "host/port is bad";
132 case LDAP_URL_ERR_BADATTRS:
133 why = "bad (or missing) attributes";
135 case LDAP_URL_ERR_BADSCOPE:
136 why = "scope string is invalid (or missing)";
138 case LDAP_URL_ERR_BADFILTER:
139 why = "bad or missing filter";
141 case LDAP_URL_ERR_BADEXTS:
142 why = "bad or missing extensions";
145 why = "unknown reason";
148 fprintf( stderr, "%s: line %d: "
149 "unable to parse uri \"%s\" "
150 "in \"uri <uri>\" line: %s\n",
151 fname, lineno, argv[ 1 ], why );
155 for ( i = 0, tmpludp = li->lud;
157 i++, tmpludp = tmpludp->lud_next )
159 if ( ( tmpludp->lud_dn != NULL
160 && tmpludp->lud_dn[0] != '\0' )
161 || tmpludp->lud_attrs != NULL
162 || tmpludp->lud_filter != NULL
163 || tmpludp->lud_exts != NULL )
165 fprintf( stderr, "%s: line %d: "
166 "warning, only protocol, "
167 "host and port allowed "
168 "in \"uri <uri>\" statement "
169 "for uri #%d of \"%s\"\n",
170 fname, lineno, i, argv[1] );
175 for ( tmpludp = li->lud; tmpludp; tmpludp = tmpludp->lud_next ) {
178 ber_len_t oldlen = 0, len;
182 tmplud.lud_attrs = NULL;
183 tmplud.lud_filter = NULL;
184 if ( !ldap_is_ldapi_url( argv[ 1 ] ) ) {
185 tmplud.lud_exts = NULL;
186 tmplud.lud_crit_exts = 0;
189 tmpurl = ldap_url_desc2str( &tmplud );
191 if ( tmpurl == NULL ) {
192 fprintf( stderr, "%s: line %d: "
193 "unable to rebuild uri "
194 "in \"uri <uri>\" statement "
196 fname, lineno, argv[ 1 ] );
200 len = strlen( tmpurl );
202 oldlen = strlen( li->url ) + STRLENOF( " " );
204 li->url = ch_realloc( li->url, oldlen + len + 1);
206 li->url[oldlen - 1] = " ";
208 AC_MEMCPY( &li->url[oldlen], tmpurl, len + 1 );
212 li->url = ch_strdup( argv[ 1 ] );
215 /* name to use for ldap_back_group */
216 } else if ( strcasecmp( argv[0], "acl-authcdn" ) == 0
217 || strcasecmp( argv[0], "binddn" ) == 0 ) {
220 "%s: line %d: missing name in \"%s <name>\" line\n",
221 fname, lineno, argv[0] );
224 ber_str2bv( argv[1], 0, 1, &li->acl_authcDN );
226 /* password to use for ldap_back_group */
227 } else if ( strcasecmp( argv[0], "acl-passwd" ) == 0
228 || strcasecmp( argv[0], "bindpw" ) == 0 ) {
231 "%s: line %d: missing password in \"%s <password>\" line\n",
232 fname, lineno, argv[0] );
235 ber_str2bv( argv[1], 0, 1, &li->acl_passwd );
237 #ifdef LDAP_BACK_PROXY_AUTHZ
238 /* identity assertion stuff... */
239 } else if ( strncasecmp( argv[0], "idassert-", STRLENOF( "idassert-" ) ) == 0
240 || strncasecmp( argv[0], "proxyauthz", STRLENOF( "proxyauthz" ) ) == 0 ) {
241 return parse_idassert( be, fname, lineno, argc, argv );
242 #endif /* LDAP_BACK_PROXY_AUTHZ */
244 /* save bind creds for referral rebinds? */
245 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
248 "%s: line %d: rebind-as-user takes no arguments\n",
254 /* intercept exop_who_am_i? */
255 } else if ( strcasecmp( argv[0], "proxy-whoami" ) == 0 ) {
258 "%s: line %d: proxy-whoami takes no arguments\n",
262 load_extop( (struct berval *)&slap_EXOP_WHOAMI,
263 0, ldap_back_exop_whoami );
265 /* FIXME: legacy: intercept old rewrite/remap directives
266 * and try to start the rwm overlay */
267 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0
268 || strcasecmp( argv[0], "map" ) == 0
269 || strncasecmp( argv[0], "rewrite", STRLENOF( "rewrite" ) ) == 0 )
271 if ( li->rwm_started == 0 ) {
272 if ( overlay_config( be, "rwm" ) ) {
273 fprintf( stderr, "%s: line %d: "
274 "unable to configure the \"rwm\" "
275 "overlay, required by directive "
277 fname, lineno, argv[0] );
278 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
279 fprintf( stderr, "\thint: try loading the \"rwm.la\" dynamic module.\n" );
280 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
284 fprintf( stderr, "%s: line %d: back-ldap: "
285 "automatically starting \"rwm\" overlay, "
286 "triggered by \"%s\" directive.\n",
287 fname, lineno, argv[ 0 ] );
291 return ( *be->bd_info->bi_db_config )( be, fname, lineno, argc, argv );
294 return SLAP_CONF_UNKNOWN;
298 return SLAP_CONF_UNKNOWN;
305 ldap_back_exop_whoami(
309 struct berval *bv = NULL;
311 if ( op->oq_extended.rs_reqdata != NULL ) {
312 /* no request data should be provided */
313 rs->sr_text = "no request data expected";
314 return rs->sr_err = LDAP_PROTOCOL_ERROR;
317 rs->sr_err = backend_check_restrictions( op, rs,
318 (struct berval *)&slap_EXOP_WHOAMI );
319 if( rs->sr_err != LDAP_SUCCESS ) return rs->sr_err;
321 /* if auth'd by back-ldap and request is proxied, forward it */
322 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)) {
325 LDAPControl c, *ctrls[2] = {NULL, NULL};
332 op2.o_ndn = op->o_conn->c_ndn;
333 lc = ldap_back_getconn(&op2, rs);
334 if (!lc || !ldap_back_dobind( lc, op, rs )) {
337 c.ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
338 c.ldctl_iscritical = 1;
339 c.ldctl_value.bv_val = ch_malloc(op->o_ndn.bv_len+4);
340 c.ldctl_value.bv_len = op->o_ndn.bv_len + 3;
341 strcpy(c.ldctl_value.bv_val, "dn:");
342 strcpy(c.ldctl_value.bv_val+3, op->o_ndn.bv_val);
345 rs->sr_err = ldap_whoami(lc->lc_ld, ctrls, NULL, &msgid);
346 if (rs->sr_err == LDAP_SUCCESS) {
347 if (ldap_result(lc->lc_ld, msgid, 1, NULL, &res) == -1) {
348 ldap_get_option(lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
350 if ( rs->sr_err == LDAP_SERVER_DOWN && do_retry ) {
352 if ( ldap_back_retry( lc, op, rs ) )
355 ldap_back_freeconn( op, lc );
359 rs->sr_err = ldap_parse_whoami(lc->lc_ld, res, &bv);
363 ch_free(c.ldctl_value.bv_val);
364 if (rs->sr_err != LDAP_SUCCESS) {
365 rs->sr_err = slap_map_api2result( rs );
368 /* else just do the same as before */
369 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
370 if ( !BER_BVISEMPTY( &op->o_dn ) ) {
371 bv->bv_len = op->o_dn.bv_len + sizeof("dn:") - 1;
372 bv->bv_val = ch_malloc( bv->bv_len + 1 );
373 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:") - 1 );
374 AC_MEMCPY( &bv->bv_val[sizeof("dn:") - 1], op->o_dn.bv_val,
376 bv->bv_val[bv->bv_len] = '\0';
388 #ifdef LDAP_BACK_PROXY_AUTHZ
398 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
400 /* identity assertion mode */
401 if ( strcasecmp( argv[0], "idassert-mode" ) == 0 ) {
403 Debug( LDAP_DEBUG_ANY,
404 "%s: line %d: illegal args number %d in \"idassert-mode <args> [<flag> [...]]\" line.\n",
405 fname, lineno, argc );
409 if ( strcasecmp( argv[1], "legacy" ) == 0 ) {
410 /* will proxyAuthz as client's identity only if bound */
411 li->idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
413 } else if ( strcasecmp( argv[1], "self" ) == 0 ) {
414 /* will proxyAuthz as client's identity */
415 li->idassert_mode = LDAP_BACK_IDASSERT_SELF;
417 } else if ( strcasecmp( argv[1], "anonymous" ) == 0 ) {
418 /* will proxyAuthz as anonymous */
419 li->idassert_mode = LDAP_BACK_IDASSERT_ANONYMOUS;
421 } else if ( strcasecmp( argv[1], "none" ) == 0 ) {
422 /* will not proxyAuthz */
423 li->idassert_mode = LDAP_BACK_IDASSERT_NOASSERT;
429 /* will proxyAuthz as argv[1] */
430 ber_str2bv( argv[1], 0, 0, &id );
432 if ( strncasecmp( id.bv_val, "u:", STRLENOF( "u:" ) ) == 0 ) {
433 /* force lowercase... */
435 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERID;
436 ber_dupbv( &li->idassert_authzID, &id );
442 if ( strncasecmp( id.bv_val, "dn:", STRLENOF( "dn:" ) ) == 0 ) {
443 id.bv_val += STRLENOF( "dn:" );
444 id.bv_len -= STRLENOF( "dn:" );
447 rc = dnNormalize( 0, NULL, NULL, &id, &dn, NULL );
448 if ( rc != LDAP_SUCCESS ) {
449 Debug( LDAP_DEBUG_ANY,
450 "%s: line %d: idassert ID \"%s\" is not a valid DN\n",
451 fname, lineno, argv[1] );
455 li->idassert_authzID.bv_len = STRLENOF( "dn:" ) + dn.bv_len;
456 li->idassert_authzID.bv_val = ch_malloc( li->idassert_authzID.bv_len + 1 );
457 AC_MEMCPY( li->idassert_authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
458 AC_MEMCPY( &li->idassert_authzID.bv_val[ STRLENOF( "dn:" ) ], dn.bv_val, dn.bv_len + 1 );
459 ch_free( dn.bv_val );
461 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERDN;
465 for ( argc -= 2, argv += 2; argc--; argv++ ) {
466 if ( strcasecmp( argv[0], "override" ) == 0 ) {
467 li->idassert_flags |= LDAP_BACK_AUTH_OVERRIDE;
470 Debug( LDAP_DEBUG_ANY,
471 "%s: line %d: unknown flag \"%s\" "
472 "in \"idassert-mode <args> "
473 "[<flags>]\" line.\n",
474 fname, lineno, argv[0] );
479 /* name to use for proxyAuthz propagation */
480 } else if ( strcasecmp( argv[0], "idassert-authcdn" ) == 0
481 || strcasecmp( argv[0], "proxyauthzdn" ) == 0 )
486 /* FIXME: "proxyauthzdn" is no longer documented, and
487 * temporarily supported for backwards compatibility */
491 "%s: line %d: missing name in \"%s <name>\" line\n",
492 fname, lineno, argv[0] );
496 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
497 fprintf( stderr, "%s: line %d: "
498 "authcDN already defined; replacing...\n",
500 ch_free( li->idassert_authcDN.bv_val );
503 ber_str2bv( argv[1], 0, 0, &dn );
504 rc = dnNormalize( 0, NULL, NULL, &dn, &li->idassert_authcDN, NULL );
505 if ( rc != LDAP_SUCCESS ) {
506 Debug( LDAP_DEBUG_ANY,
507 "%s: line %d: idassert ID \"%s\" is not a valid DN\n",
508 fname, lineno, argv[1] );
512 /* password to use for proxyAuthz propagation */
513 } else if ( strcasecmp( argv[0], "idassert-passwd" ) == 0
514 || strcasecmp( argv[0], "proxyauthzpw" ) == 0 )
516 /* FIXME: "proxyauthzpw" is no longer documented, and
517 * temporarily supported for backwards compatibility */
521 "%s: line %d: missing password in \"%s <password>\" line\n",
522 fname, lineno, argv[0] );
526 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
527 fprintf( stderr, "%s: line %d: "
528 "passwd already defined; replacing...\n",
530 ch_free( li->idassert_passwd.bv_val );
533 ber_str2bv( argv[1], 0, 1, &li->idassert_passwd );
535 /* rules to accept identity assertion... */
536 } else if ( strcasecmp( argv[0], "idassert-authzFrom" ) == 0 ) {
539 ber_str2bv( argv[1], 0, 1, &rule );
541 ber_bvarray_add( &li->idassert_authz, &rule );
543 } else if ( strcasecmp( argv[0], "idassert-method" ) == 0 ) {
546 "%s: line %d: missing method in \"%s <method>\" line\n",
547 fname, lineno, argv[0] );
551 if ( strcasecmp( argv[1], "none" ) == 0 ) {
552 /* FIXME: is this useful? */
553 li->idassert_authmethod = LDAP_AUTH_NONE;
557 "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
558 fname, lineno, argv[0], argv[1] );
561 } else if ( strcasecmp( argv[1], "simple" ) == 0 ) {
562 li->idassert_authmethod = LDAP_AUTH_SIMPLE;
566 "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
567 fname, lineno, argv[0], argv[1] );
570 } else if ( strcasecmp( argv[1], "sasl" ) == 0 ) {
571 #ifdef HAVE_CYRUS_SASL
574 for ( arg = 2; arg < argc; arg++ ) {
575 if ( strncasecmp( argv[arg], "mech=", STRLENOF( "mech=" ) ) == 0 ) {
576 char *val = argv[arg] + STRLENOF( "mech=" );
578 if ( !BER_BVISNULL( &li->idassert_sasl_mech ) ) {
579 fprintf( stderr, "%s: line %d: "
580 "SASL mech already defined; replacing...\n",
582 ch_free( li->idassert_sasl_mech.bv_val );
584 ber_str2bv( val, 0, 1, &li->idassert_sasl_mech );
586 } else if ( strncasecmp( argv[arg], "realm=", STRLENOF( "realm=" ) ) == 0 ) {
587 char *val = argv[arg] + STRLENOF( "realm=" );
589 if ( !BER_BVISNULL( &li->idassert_sasl_realm ) ) {
590 fprintf( stderr, "%s: line %d: "
591 "SASL realm already defined; replacing...\n",
593 ch_free( li->idassert_sasl_realm.bv_val );
595 ber_str2bv( val, 0, 1, &li->idassert_sasl_realm );
597 } else if ( strncasecmp( argv[arg], "authcdn=", STRLENOF( "authcdn=" ) ) == 0 ) {
598 char *val = argv[arg] + STRLENOF( "authcdn=" );
602 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
603 fprintf( stderr, "%s: line %d: "
604 "SASL authcDN already defined; replacing...\n",
606 ch_free( li->idassert_authcDN.bv_val );
608 if ( strncasecmp( argv[arg], "dn:", STRLENOF( "dn:" ) ) == 0 ) {
609 val += STRLENOF( "dn:" );
612 ber_str2bv( val, 0, 0, &dn );
613 rc = dnNormalize( 0, NULL, NULL, &dn, &li->idassert_authcDN, NULL );
614 if ( rc != LDAP_SUCCESS ) {
615 Debug( LDAP_DEBUG_ANY,
616 "%s: line %d: SASL authcdn \"%s\" is not a valid DN\n",
617 fname, lineno, val );
621 } else if ( strncasecmp( argv[arg], "authcid=", STRLENOF( "authcid=" ) ) == 0 ) {
622 char *val = argv[arg] + STRLENOF( "authcid=" );
624 if ( !BER_BVISNULL( &li->idassert_authcID ) ) {
625 fprintf( stderr, "%s: line %d: "
626 "SASL authcID already defined; replacing...\n",
628 ch_free( li->idassert_authcID.bv_val );
630 if ( strncasecmp( argv[arg], "u:", STRLENOF( "u:" ) ) == 0 ) {
631 val += STRLENOF( "u:" );
633 ber_str2bv( val, 0, 1, &li->idassert_authcID );
635 } else if ( strncasecmp( argv[arg], "cred=", STRLENOF( "cred=" ) ) == 0 ) {
636 char *val = argv[arg] + STRLENOF( "cred=" );
638 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
639 fprintf( stderr, "%s: line %d: "
640 "SASL cred already defined; replacing...\n",
642 ch_free( li->idassert_passwd.bv_val );
644 ber_str2bv( val, 0, 1, &li->idassert_passwd );
646 } else if ( strncasecmp( argv[arg], "authz=", STRLENOF( "authz=" ) ) == 0 ) {
647 char *val = argv[arg] + STRLENOF( "authz=" );
649 if ( strcasecmp( val, "proxyauthz" ) == 0 ) {
650 li->idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
652 } else if ( strcasecmp( val, "native" ) == 0 ) {
653 li->idassert_flags |= LDAP_BACK_AUTH_NATIVE_AUTHZ;
656 fprintf( stderr, "%s: line %s: "
657 "unknown authz mode \"%s\"\n",
658 fname, lineno, val );
663 fprintf( stderr, "%s: line %d: "
664 "unknown SASL parameter %s\n",
665 fname, lineno, argv[arg] );
670 li->idassert_authmethod = LDAP_AUTH_SASL;
672 #else /* !HAVE_CYRUS_SASL */
673 fprintf( stderr, "%s: line %d: "
674 "compile --with-cyrus-sasl to enable SASL auth\n",
677 #endif /* !HAVE_CYRUS_SASL */
680 fprintf( stderr, "%s: line %d: "
681 "unhandled idassert-method method %s\n",
682 fname, lineno, argv[1] );
687 return SLAP_CONF_UNKNOWN;
692 #endif /* LDAP_BACK_PROXY_AUTHZ */