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;
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 ) {
78 l = strlen( argv[1] ) + STRLENOF( "ldap:///") + 1;
79 li->url = ch_calloc( l, sizeof( char ) );
80 if ( li->url == NULL ) {
81 fprintf( stderr, "%s: line %d: malloc failed\n" );
85 snprintf( li->url, l, "ldap://%s/", argv[1] );
87 /* URI of server to query (preferred over "server" directive) */
88 } else if ( strcasecmp( argv[0], "uri" ) == 0 ) {
93 fprintf( stderr, "%s: line %d: "
95 "in \"uri <uri>\" line\n",
99 if ( li->url != NULL ) {
102 if ( li->lud != NULL ) {
103 ldap_free_urllist( li->lud );
107 /* PARANOID: DN and more are not required nor allowed */
108 urlrc = ldap_url_parselist_ext( &li->lud, argv[ 1 ], "\t" );
110 urlrc = ldap_url_parselist( &li->lud, argv[ 1 ] );
112 if ( urlrc != LDAP_URL_SUCCESS ) {
116 case LDAP_URL_ERR_MEM:
119 case LDAP_URL_ERR_PARAM:
120 why = "parameter is bad";
122 case LDAP_URL_ERR_BADSCHEME:
123 why = "URL doesn't begin with \"[c]ldap[si]://\"";
125 case LDAP_URL_ERR_BADENCLOSURE:
126 why = "URL is missing trailing \">\"";
128 case LDAP_URL_ERR_BADURL:
130 case LDAP_URL_ERR_BADHOST:
131 why = "host/port is bad";
133 case LDAP_URL_ERR_BADATTRS:
134 why = "bad (or missing) attributes";
136 case LDAP_URL_ERR_BADSCOPE:
137 why = "scope string is invalid (or missing)";
139 case LDAP_URL_ERR_BADFILTER:
140 why = "bad or missing filter";
142 case LDAP_URL_ERR_BADEXTS:
143 why = "bad or missing extensions";
146 why = "unknown reason";
149 fprintf( stderr, "%s: line %d: "
150 "unable to parse uri \"%s\" "
151 "in \"uri <uri>\" line: %s\n",
152 fname, lineno, argv[ 1 ], why );
156 for ( i = 0, tmpludp = li->lud;
158 i++, tmpludp = tmpludp->lud_next )
160 if ( ( tmpludp->lud_dn != NULL
161 && tmpludp->lud_dn[0] != '\0' )
162 || tmpludp->lud_attrs != NULL
163 || tmpludp->lud_filter != NULL
164 || tmpludp->lud_exts != NULL )
166 fprintf( stderr, "%s: line %d: "
167 "warning, only protocol, "
168 "host and port allowed "
169 "in \"uri <uri>\" statement "
170 "for uri #%d of \"%s\"\n",
171 fname, lineno, i, argv[1] );
176 for ( tmpludp = li->lud; tmpludp; tmpludp = tmpludp->lud_next ) {
179 ber_len_t oldlen = 0, len;
183 tmplud.lud_attrs = NULL;
184 tmplud.lud_filter = NULL;
185 if ( !ldap_is_ldapi_url( argv[ 1 ] ) ) {
186 tmplud.lud_exts = NULL;
187 tmplud.lud_crit_exts = 0;
190 tmpurl = ldap_url_desc2str( &tmplud );
192 if ( tmpurl == NULL ) {
193 fprintf( stderr, "%s: line %d: "
194 "unable to rebuild uri "
195 "in \"uri <uri>\" statement "
197 fname, lineno, argv[ 1 ] );
201 len = strlen( tmpurl );
203 oldlen = strlen( li->url ) + STRLENOF( " " );
205 li->url = ch_realloc( li->url, oldlen + len + 1);
207 li->url[oldlen - 1] = " ";
209 AC_MEMCPY( &li->url[oldlen], tmpurl, len + 1 );
213 li->url = ch_strdup( argv[ 1 ] );
217 } else if ( strcasecmp( argv[0], "start-tls" ) == 0 ) {
220 "%s: line %d: start-tls takes no arguments\n",
224 li->flags |= LDAP_BACK_F_TLS_CRITICAL;
227 } else if ( strcasecmp( argv[0], "try-start-tls" ) == 0 ) {
230 "%s: line %d: try-start-tls takes no arguments\n",
234 li->flags &= ~LDAP_BACK_F_TLS_CRITICAL;
235 li->flags |= LDAP_BACK_F_USE_TLS;
237 /* name to use for ldap_back_group */
238 } else if ( strcasecmp( argv[0], "acl-authcdn" ) == 0
239 || strcasecmp( argv[0], "binddn" ) == 0 )
243 "%s: line %d: missing name in \"%s <name>\" line\n",
244 fname, lineno, argv[0] );
248 if ( strcasecmp( argv[0], "binddn" ) == 0 ) {
249 fprintf( stderr, "%s: line %d: "
250 "\"binddn\" statement is deprecated; "
251 "use \"acl-authcDN\" instead\n",
253 /* FIXME: some day we'll need to throw an error */
256 ber_str2bv( argv[1], 0, 1, &li->acl_authcDN );
258 /* password to use for ldap_back_group */
259 } else if ( strcasecmp( argv[0], "acl-passwd" ) == 0
260 || strcasecmp( argv[0], "bindpw" ) == 0 )
264 "%s: line %d: missing password in \"%s <password>\" line\n",
265 fname, lineno, argv[0] );
269 if ( strcasecmp( argv[0], "bindpw" ) == 0 ) {
270 fprintf( stderr, "%s: line %d: "
271 "\"bindpw\" statement is deprecated; "
272 "use \"acl-passwd\" instead\n",
274 /* FIXME: some day we'll need to throw an error */
277 ber_str2bv( argv[1], 0, 1, &li->acl_passwd );
279 /* identity assertion stuff... */
280 } else if ( strncasecmp( argv[0], "idassert-", STRLENOF( "idassert-" ) ) == 0
281 || strncasecmp( argv[0], "proxyauthz", STRLENOF( "proxyauthz" ) ) == 0 )
283 /* NOTE: "proxyauthz{DN,pw}" was initially used; it's now
284 * deprected and undocumented, it can be dropped at some
285 * point, since nobody should be really using it */
286 return parse_idassert( be, fname, lineno, argc, argv );
288 /* save bind creds for referral rebinds? */
289 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
292 "%s: line %d: \"rebind-as-user\" takes no arguments\n",
296 li->flags |= LDAP_BACK_F_SAVECRED;
298 } else if ( strcasecmp( argv[0], "chase-referrals" ) == 0 ) {
301 "%s: line %d: \"chase-referrals\" takes no arguments\n",
306 li->flags |= LDAP_BACK_F_CHASE_REFERRALS;
308 } else if ( strcasecmp( argv[0], "dont-chase-referrals" ) == 0 ) {
311 "%s: line %d: \"dont-chase-referrals\" takes no arguments\n",
316 li->flags &= ~LDAP_BACK_F_CHASE_REFERRALS;
318 /* intercept exop_who_am_i? */
319 } else if ( strcasecmp( argv[0], "proxy-whoami" ) == 0 ) {
322 "%s: line %d: proxy-whoami takes no arguments\n",
326 load_extop( (struct berval *)&slap_EXOP_WHOAMI,
327 0, ldap_back_exop_whoami );
329 /* FIXME: legacy: intercept old rewrite/remap directives
330 * and try to start the rwm overlay */
331 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0
332 || strcasecmp( argv[0], "map" ) == 0
333 || strncasecmp( argv[0], "rewrite", STRLENOF( "rewrite" ) ) == 0 )
335 fprintf( stderr, "%s: line %d: "
336 "rewrite/remap capabilities have been moved "
337 "to the \"rwm\" overlay; see slapo-rwm(5) "
338 "for details. I'm trying to do my best "
339 "to preserve backwards compatibility...\n",
342 if ( li->rwm_started == 0 ) {
343 if ( overlay_config( be, "rwm" ) ) {
344 fprintf( stderr, "%s: line %d: "
345 "unable to configure the \"rwm\" "
346 "overlay, required by directive "
348 fname, lineno, argv[0] );
349 #if SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC
350 fprintf( stderr, "\thint: try loading the \"rwm.la\" dynamic module.\n" );
351 #endif /* SLAPD_OVER_RWM == SLAPD_MOD_DYNAMIC */
355 fprintf( stderr, "%s: line %d: back-ldap: "
356 "automatically starting \"rwm\" overlay, "
357 "triggered by \"%s\" directive.\n",
358 fname, lineno, argv[ 0 ] );
362 return ( *be->bd_info->bi_db_config )( be, fname, lineno, argc, argv );
365 return SLAP_CONF_UNKNOWN;
369 return SLAP_CONF_UNKNOWN;
376 ldap_back_exop_whoami(
380 struct berval *bv = NULL;
382 if ( op->oq_extended.rs_reqdata != NULL ) {
383 /* no request data should be provided */
384 rs->sr_text = "no request data expected";
385 return rs->sr_err = LDAP_PROTOCOL_ERROR;
388 rs->sr_err = backend_check_restrictions( op, rs,
389 (struct berval *)&slap_EXOP_WHOAMI );
390 if( rs->sr_err != LDAP_SUCCESS ) return rs->sr_err;
392 /* if auth'd by back-ldap and request is proxied, forward it */
393 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)) {
396 LDAPControl c, *ctrls[2] = {NULL, NULL};
403 op2.o_ndn = op->o_conn->c_ndn;
404 lc = ldap_back_getconn(&op2, rs, LDAP_BACK_SENDERR);
405 if (!lc || !ldap_back_dobind( lc, op, rs, LDAP_BACK_SENDERR )) {
408 c.ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
409 c.ldctl_iscritical = 1;
410 c.ldctl_value.bv_val = ch_malloc(op->o_ndn.bv_len+4);
411 c.ldctl_value.bv_len = op->o_ndn.bv_len + 3;
412 strcpy(c.ldctl_value.bv_val, "dn:");
413 strcpy(c.ldctl_value.bv_val+3, op->o_ndn.bv_val);
416 rs->sr_err = ldap_whoami(lc->lc_ld, ctrls, NULL, &msgid);
417 if (rs->sr_err == LDAP_SUCCESS) {
418 if (ldap_result(lc->lc_ld, msgid, 1, NULL, &res) == -1) {
419 ldap_get_option(lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
421 if ( rs->sr_err == LDAP_SERVER_DOWN && do_retry ) {
423 if ( ldap_back_retry( lc, op, rs, LDAP_BACK_SENDERR ) )
426 ldap_back_freeconn( op, lc );
430 rs->sr_err = ldap_parse_whoami(lc->lc_ld, res, &bv);
434 ch_free(c.ldctl_value.bv_val);
435 if (rs->sr_err != LDAP_SUCCESS) {
436 rs->sr_err = slap_map_api2result( rs );
439 /* else just do the same as before */
440 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
441 if ( !BER_BVISEMPTY( &op->o_dn ) ) {
442 bv->bv_len = op->o_dn.bv_len + sizeof("dn:") - 1;
443 bv->bv_val = ch_malloc( bv->bv_len + 1 );
444 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:") - 1 );
445 AC_MEMCPY( &bv->bv_val[sizeof("dn:") - 1], op->o_dn.bv_val,
447 bv->bv_val[bv->bv_len] = '\0';
468 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
470 /* identity assertion mode */
471 if ( strcasecmp( argv[0], "idassert-mode" ) == 0 ) {
473 Debug( LDAP_DEBUG_ANY,
474 "%s: line %d: illegal args number %d in \"idassert-mode <args> [<flag> [...]]\" line.\n",
475 fname, lineno, argc );
479 if ( strcasecmp( argv[1], "legacy" ) == 0 ) {
480 /* will proxyAuthz as client's identity only if bound */
481 li->idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
483 } else if ( strcasecmp( argv[1], "self" ) == 0 ) {
484 /* will proxyAuthz as client's identity */
485 li->idassert_mode = LDAP_BACK_IDASSERT_SELF;
487 } else if ( strcasecmp( argv[1], "anonymous" ) == 0 ) {
488 /* will proxyAuthz as anonymous */
489 li->idassert_mode = LDAP_BACK_IDASSERT_ANONYMOUS;
491 } else if ( strcasecmp( argv[1], "none" ) == 0 ) {
492 /* will not proxyAuthz */
493 li->idassert_mode = LDAP_BACK_IDASSERT_NOASSERT;
499 /* will proxyAuthz as argv[1] */
500 ber_str2bv( argv[1], 0, 0, &id );
502 if ( strncasecmp( id.bv_val, "u:", STRLENOF( "u:" ) ) == 0 ) {
503 /* force lowercase... */
505 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERID;
506 ber_dupbv( &li->idassert_authzID, &id );
512 if ( strncasecmp( id.bv_val, "dn:", STRLENOF( "dn:" ) ) == 0 ) {
513 id.bv_val += STRLENOF( "dn:" );
514 id.bv_len -= STRLENOF( "dn:" );
517 rc = dnNormalize( 0, NULL, NULL, &id, &dn, NULL );
518 if ( rc != LDAP_SUCCESS ) {
519 Debug( LDAP_DEBUG_ANY,
520 "%s: line %d: idassert ID \"%s\" is not a valid DN\n",
521 fname, lineno, argv[1] );
525 li->idassert_authzID.bv_len = STRLENOF( "dn:" ) + dn.bv_len;
526 li->idassert_authzID.bv_val = ch_malloc( li->idassert_authzID.bv_len + 1 );
527 AC_MEMCPY( li->idassert_authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
528 AC_MEMCPY( &li->idassert_authzID.bv_val[ STRLENOF( "dn:" ) ], dn.bv_val, dn.bv_len + 1 );
529 ch_free( dn.bv_val );
531 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERDN;
535 for ( argc -= 2, argv += 2; argc--; argv++ ) {
536 if ( strcasecmp( argv[0], "override" ) == 0 ) {
537 li->idassert_flags |= LDAP_BACK_AUTH_OVERRIDE;
540 Debug( LDAP_DEBUG_ANY,
541 "%s: line %d: unknown flag \"%s\" "
542 "in \"idassert-mode <args> "
543 "[<flags>]\" line.\n",
544 fname, lineno, argv[0] );
549 /* name to use for proxyAuthz propagation */
550 } else if ( strcasecmp( argv[0], "idassert-authcdn" ) == 0
551 || strcasecmp( argv[0], "proxyauthzdn" ) == 0 )
556 /* FIXME: "proxyauthzdn" is no longer documented, and
557 * temporarily supported for backwards compatibility */
561 "%s: line %d: missing name in \"%s <name>\" line\n",
562 fname, lineno, argv[0] );
566 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
567 fprintf( stderr, "%s: line %d: "
568 "authcDN already defined; replacing...\n",
570 ch_free( li->idassert_authcDN.bv_val );
573 ber_str2bv( argv[1], 0, 0, &dn );
574 rc = dnNormalize( 0, NULL, NULL, &dn, &li->idassert_authcDN, NULL );
575 if ( rc != LDAP_SUCCESS ) {
576 Debug( LDAP_DEBUG_ANY,
577 "%s: line %d: idassert ID \"%s\" is not a valid DN\n",
578 fname, lineno, argv[1] );
582 /* password to use for proxyAuthz propagation */
583 } else if ( strcasecmp( argv[0], "idassert-passwd" ) == 0
584 || strcasecmp( argv[0], "proxyauthzpw" ) == 0 )
586 /* FIXME: "proxyauthzpw" is no longer documented, and
587 * temporarily supported for backwards compatibility */
591 "%s: line %d: missing password in \"%s <password>\" line\n",
592 fname, lineno, argv[0] );
596 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
597 fprintf( stderr, "%s: line %d: "
598 "passwd already defined; replacing...\n",
600 ch_free( li->idassert_passwd.bv_val );
603 ber_str2bv( argv[1], 0, 1, &li->idassert_passwd );
605 /* rules to accept identity assertion... */
606 } else if ( strcasecmp( argv[0], "idassert-authzFrom" ) == 0 ) {
609 ber_str2bv( argv[1], 0, 1, &rule );
611 ber_bvarray_add( &li->idassert_authz, &rule );
613 } else if ( strcasecmp( argv[0], "idassert-method" ) == 0 ) {
616 "%s: line %d: missing method in \"%s <method>\" line\n",
617 fname, lineno, argv[0] );
621 if ( strcasecmp( argv[1], "none" ) == 0 ) {
622 /* FIXME: is this at all useful? */
623 li->idassert_authmethod = LDAP_AUTH_NONE;
627 "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
628 fname, lineno, argv[0], argv[1] );
631 } else if ( strcasecmp( argv[1], "simple" ) == 0 ) {
632 li->idassert_authmethod = LDAP_AUTH_SIMPLE;
636 "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
637 fname, lineno, argv[0], argv[1] );
640 } else if ( strcasecmp( argv[1], "sasl" ) == 0 ) {
641 #ifdef HAVE_CYRUS_SASL
644 for ( arg = 2; arg < argc; arg++ ) {
645 if ( strncasecmp( argv[arg], "mech=", STRLENOF( "mech=" ) ) == 0 ) {
646 char *val = argv[arg] + STRLENOF( "mech=" );
648 if ( !BER_BVISNULL( &li->idassert_sasl_mech ) ) {
649 fprintf( stderr, "%s: line %d: "
650 "SASL mech already defined; replacing...\n",
652 ch_free( li->idassert_sasl_mech.bv_val );
654 ber_str2bv( val, 0, 1, &li->idassert_sasl_mech );
656 } else if ( strncasecmp( argv[arg], "realm=", STRLENOF( "realm=" ) ) == 0 ) {
657 char *val = argv[arg] + STRLENOF( "realm=" );
659 if ( !BER_BVISNULL( &li->idassert_sasl_realm ) ) {
660 fprintf( stderr, "%s: line %d: "
661 "SASL realm already defined; replacing...\n",
663 ch_free( li->idassert_sasl_realm.bv_val );
665 ber_str2bv( val, 0, 1, &li->idassert_sasl_realm );
667 } else if ( strncasecmp( argv[arg], "authcdn=", STRLENOF( "authcdn=" ) ) == 0 ) {
668 char *val = argv[arg] + STRLENOF( "authcdn=" );
672 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
673 fprintf( stderr, "%s: line %d: "
674 "SASL authcDN already defined; replacing...\n",
676 ch_free( li->idassert_authcDN.bv_val );
678 if ( strncasecmp( argv[arg], "dn:", STRLENOF( "dn:" ) ) == 0 ) {
679 val += STRLENOF( "dn:" );
682 ber_str2bv( val, 0, 0, &dn );
683 rc = dnNormalize( 0, NULL, NULL, &dn, &li->idassert_authcDN, NULL );
684 if ( rc != LDAP_SUCCESS ) {
685 Debug( LDAP_DEBUG_ANY,
686 "%s: line %d: SASL authcdn \"%s\" is not a valid DN\n",
687 fname, lineno, val );
691 } else if ( strncasecmp( argv[arg], "authcid=", STRLENOF( "authcid=" ) ) == 0 ) {
692 char *val = argv[arg] + STRLENOF( "authcid=" );
694 if ( !BER_BVISNULL( &li->idassert_authcID ) ) {
695 fprintf( stderr, "%s: line %d: "
696 "SASL authcID already defined; replacing...\n",
698 ch_free( li->idassert_authcID.bv_val );
700 if ( strncasecmp( argv[arg], "u:", STRLENOF( "u:" ) ) == 0 ) {
701 val += STRLENOF( "u:" );
703 ber_str2bv( val, 0, 1, &li->idassert_authcID );
705 } else if ( strncasecmp( argv[arg], "cred=", STRLENOF( "cred=" ) ) == 0 ) {
706 char *val = argv[arg] + STRLENOF( "cred=" );
708 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
709 fprintf( stderr, "%s: line %d: "
710 "SASL cred already defined; replacing...\n",
712 ch_free( li->idassert_passwd.bv_val );
714 ber_str2bv( val, 0, 1, &li->idassert_passwd );
716 } else if ( strncasecmp( argv[arg], "authz=", STRLENOF( "authz=" ) ) == 0 ) {
717 char *val = argv[arg] + STRLENOF( "authz=" );
719 if ( strcasecmp( val, "proxyauthz" ) == 0 ) {
720 li->idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
722 } else if ( strcasecmp( val, "native" ) == 0 ) {
723 li->idassert_flags |= LDAP_BACK_AUTH_NATIVE_AUTHZ;
726 fprintf( stderr, "%s: line %s: "
727 "unknown authz mode \"%s\"\n",
728 fname, lineno, val );
733 fprintf( stderr, "%s: line %d: "
734 "unknown SASL parameter %s\n",
735 fname, lineno, argv[arg] );
740 li->idassert_authmethod = LDAP_AUTH_SASL;
742 #else /* !HAVE_CYRUS_SASL */
743 fprintf( stderr, "%s: line %d: "
744 "compile --with-cyrus-sasl to enable SASL auth\n",
747 #endif /* !HAVE_CYRUS_SASL */
750 fprintf( stderr, "%s: line %d: "
751 "unhandled idassert-method method %s\n",
752 fname, lineno, argv[1] );
757 return SLAP_CONF_UNKNOWN;