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 ( tmpludp = li->lud; tmpludp; tmpludp = tmpludp->lud_next ) {
156 if ( ( tmpludp->lud_dn != NULL
157 && tmpludp->lud_dn[0] != '\0' )
158 || tmpludp->lud_attrs != NULL
159 || tmpludp->lud_filter != NULL
160 || tmpludp->lud_exts != NULL )
162 fprintf( stderr, "%s: line %d: "
163 "warning, only protocol, "
164 "host and port allowed "
165 "in \"uri <uri>\" statement "
167 fname, lineno, argv[1] );
172 for ( tmpludp = li->lud; tmpludp; tmpludp = tmpludp->lud_next ) {
175 ber_len_t oldlen = 0, len;
179 tmplud.lud_attrs = NULL;
180 tmplud.lud_filter = NULL;
181 if ( !ldap_is_ldapi_url( argv[ 1 ] ) ) {
182 tmplud.lud_exts = NULL;
183 tmplud.lud_crit_exts = 0;
186 tmpurl = ldap_url_desc2str( &tmplud );
188 if ( tmpurl == NULL ) {
189 fprintf( stderr, "%s: line %d: "
190 "unable to rebuild uri "
191 "in \"uri <uri>\" statement "
193 fname, lineno, argv[ 1 ] );
197 len = strlen( tmpurl );
199 oldlen = strlen( li->url ) + STRLENOF( " " );
201 li->url = ch_realloc( li->url, oldlen + len + 1);
203 li->url[oldlen - 1] = " ";
205 AC_MEMCPY( &li->url[oldlen], tmpurl, len + 1 );
209 li->url = ch_strdup( argv[ 1 ] );
212 /* name to use for ldap_back_group */
213 } else if ( strcasecmp( argv[0], "acl-authcdn" ) == 0
214 || strcasecmp( argv[0], "binddn" ) == 0 ) {
217 "%s: line %d: missing name in \"%s <name>\" line\n",
218 fname, lineno, argv[0] );
221 ber_str2bv( argv[1], 0, 1, &li->acl_authcDN );
223 /* password to use for ldap_back_group */
224 } else if ( strcasecmp( argv[0], "acl-passwd" ) == 0
225 || strcasecmp( argv[0], "bindpw" ) == 0 ) {
228 "%s: line %d: missing password in \"%s <password>\" line\n",
229 fname, lineno, argv[0] );
232 ber_str2bv( argv[1], 0, 1, &li->acl_passwd );
234 #ifdef LDAP_BACK_PROXY_AUTHZ
235 /* identity assertion stuff... */
236 } else if ( strncasecmp( argv[0], "idassert-", STRLENOF( "idassert-" ) ) == 0
237 || strncasecmp( argv[0], "proxyauthz", STRLENOF( "proxyauthz" ) ) == 0 ) {
238 return parse_idassert( be, fname, lineno, argc, argv );
239 #endif /* LDAP_BACK_PROXY_AUTHZ */
241 /* save bind creds for referral rebinds? */
242 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
245 "%s: line %d: rebind-as-user takes no arguments\n",
251 /* intercept exop_who_am_i? */
252 } else if ( strcasecmp( argv[0], "proxy-whoami" ) == 0 ) {
255 "%s: line %d: proxy-whoami takes no arguments\n",
259 load_extop( (struct berval *)&slap_EXOP_WHOAMI,
260 0, ldap_back_exop_whoami );
264 return SLAP_CONF_UNKNOWN;
271 ldap_back_exop_whoami(
275 struct berval *bv = NULL;
277 if ( op->oq_extended.rs_reqdata != NULL ) {
278 /* no request data should be provided */
279 rs->sr_text = "no request data expected";
280 return rs->sr_err = LDAP_PROTOCOL_ERROR;
283 rs->sr_err = backend_check_restrictions( op, rs,
284 (struct berval *)&slap_EXOP_WHOAMI );
285 if( rs->sr_err != LDAP_SUCCESS ) return rs->sr_err;
287 /* if auth'd by back-ldap and request is proxied, forward it */
288 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)) {
291 LDAPControl c, *ctrls[2] = {NULL, NULL};
298 op2.o_ndn = op->o_conn->c_ndn;
299 lc = ldap_back_getconn(&op2, rs);
300 if (!lc || !ldap_back_dobind( lc, op, rs )) {
303 c.ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
304 c.ldctl_iscritical = 1;
305 c.ldctl_value.bv_val = ch_malloc(op->o_ndn.bv_len+4);
306 c.ldctl_value.bv_len = op->o_ndn.bv_len + 3;
307 strcpy(c.ldctl_value.bv_val, "dn:");
308 strcpy(c.ldctl_value.bv_val+3, op->o_ndn.bv_val);
311 rs->sr_err = ldap_whoami(lc->lc_ld, ctrls, NULL, &msgid);
312 if (rs->sr_err == LDAP_SUCCESS) {
313 if (ldap_result(lc->lc_ld, msgid, 1, NULL, &res) == -1) {
314 ldap_get_option(lc->lc_ld, LDAP_OPT_ERROR_NUMBER,
316 if ( rs->sr_err == LDAP_SERVER_DOWN && do_retry ) {
318 if ( ldap_back_retry( lc, op, rs ) )
321 ldap_back_freeconn( op, lc );
325 rs->sr_err = ldap_parse_whoami(lc->lc_ld, res, &bv);
329 ch_free(c.ldctl_value.bv_val);
330 if (rs->sr_err != LDAP_SUCCESS) {
331 rs->sr_err = slap_map_api2result( rs );
334 /* else just do the same as before */
335 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
336 if ( !BER_BVISEMPTY( &op->o_dn ) ) {
337 bv->bv_len = op->o_dn.bv_len + sizeof("dn:") - 1;
338 bv->bv_val = ch_malloc( bv->bv_len + 1 );
339 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:") - 1 );
340 AC_MEMCPY( &bv->bv_val[sizeof("dn:") - 1], op->o_dn.bv_val,
342 bv->bv_val[bv->bv_len] = '\0';
354 #ifdef LDAP_BACK_PROXY_AUTHZ
364 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
366 /* identity assertion mode */
367 if ( strcasecmp( argv[0], "idassert-mode" ) == 0 ) {
369 Debug( LDAP_DEBUG_ANY,
370 "%s: line %d: illegal args number %d in \"idassert-mode <args> [<flag> [...]]\" line.\n",
371 fname, lineno, argc );
375 if ( strcasecmp( argv[1], "legacy" ) == 0 ) {
376 /* will proxyAuthz as client's identity only if bound */
377 li->idassert_mode = LDAP_BACK_IDASSERT_LEGACY;
379 } else if ( strcasecmp( argv[1], "self" ) == 0 ) {
380 /* will proxyAuthz as client's identity */
381 li->idassert_mode = LDAP_BACK_IDASSERT_SELF;
383 } else if ( strcasecmp( argv[1], "anonymous" ) == 0 ) {
384 /* will proxyAuthz as anonymous */
385 li->idassert_mode = LDAP_BACK_IDASSERT_ANONYMOUS;
387 } else if ( strcasecmp( argv[1], "none" ) == 0 ) {
388 /* will not proxyAuthz */
389 li->idassert_mode = LDAP_BACK_IDASSERT_NOASSERT;
395 /* will proxyAuthz as argv[1] */
396 ber_str2bv( argv[1], 0, 0, &id );
398 if ( strncasecmp( id.bv_val, "u:", STRLENOF( "u:" ) ) == 0 ) {
399 /* force lowercase... */
401 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERID;
402 ber_dupbv( &li->idassert_authzID, &id );
408 if ( strncasecmp( id.bv_val, "dn:", STRLENOF( "dn:" ) ) == 0 ) {
409 id.bv_val += STRLENOF( "dn:" );
410 id.bv_len -= STRLENOF( "dn:" );
413 rc = dnNormalize( 0, NULL, NULL, &id, &dn, NULL );
414 if ( rc != LDAP_SUCCESS ) {
415 Debug( LDAP_DEBUG_ANY,
416 "%s: line %d: idassert ID \"%s\" is not a valid DN\n",
417 fname, lineno, argv[1] );
421 li->idassert_authzID.bv_len = STRLENOF( "dn:" ) + dn.bv_len;
422 li->idassert_authzID.bv_val = ch_malloc( li->idassert_authzID.bv_len + 1 );
423 AC_MEMCPY( li->idassert_authzID.bv_val, "dn:", STRLENOF( "dn:" ) );
424 AC_MEMCPY( &li->idassert_authzID.bv_val[ STRLENOF( "dn:" ) ], dn.bv_val, dn.bv_len + 1 );
425 ch_free( dn.bv_val );
427 li->idassert_mode = LDAP_BACK_IDASSERT_OTHERDN;
431 for ( argc -= 2, argv += 2; argc--; argv++ ) {
432 if ( strcasecmp( argv[0], "override" ) == 0 ) {
433 li->idassert_flags |= LDAP_BACK_AUTH_OVERRIDE;
436 Debug( LDAP_DEBUG_ANY,
437 "%s: line %d: unknown flag \"%s\" "
438 "in \"idassert-mode <args> "
439 "[<flags>]\" line.\n",
440 fname, lineno, argv[0] );
445 /* name to use for proxyAuthz propagation */
446 } else if ( strcasecmp( argv[0], "idassert-authcdn" ) == 0
447 || strcasecmp( argv[0], "proxyauthzdn" ) == 0 )
452 /* FIXME: "proxyauthzdn" is no longer documented, and
453 * temporarily supported for backwards compatibility */
457 "%s: line %d: missing name in \"%s <name>\" line\n",
458 fname, lineno, argv[0] );
462 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
463 fprintf( stderr, "%s: line %d: "
464 "authcDN already defined; replacing...\n",
466 ch_free( li->idassert_authcDN.bv_val );
469 ber_str2bv( argv[1], 0, 0, &dn );
470 rc = dnNormalize( 0, NULL, NULL, &dn, &li->idassert_authcDN, NULL );
471 if ( rc != LDAP_SUCCESS ) {
472 Debug( LDAP_DEBUG_ANY,
473 "%s: line %d: idassert ID \"%s\" is not a valid DN\n",
474 fname, lineno, argv[1] );
478 /* password to use for proxyAuthz propagation */
479 } else if ( strcasecmp( argv[0], "idassert-passwd" ) == 0
480 || strcasecmp( argv[0], "proxyauthzpw" ) == 0 )
482 /* FIXME: "proxyauthzpw" is no longer documented, and
483 * temporarily supported for backwards compatibility */
487 "%s: line %d: missing password in \"%s <password>\" line\n",
488 fname, lineno, argv[0] );
492 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
493 fprintf( stderr, "%s: line %d: "
494 "passwd already defined; replacing...\n",
496 ch_free( li->idassert_passwd.bv_val );
499 ber_str2bv( argv[1], 0, 1, &li->idassert_passwd );
501 /* rules to accept identity assertion... */
502 } else if ( strcasecmp( argv[0], "idassert-authzFrom" ) == 0 ) {
505 ber_str2bv( argv[1], 0, 1, &rule );
507 ber_bvarray_add( &li->idassert_authz, &rule );
509 } else if ( strcasecmp( argv[0], "idassert-method" ) == 0 ) {
512 "%s: line %d: missing method in \"%s <method>\" line\n",
513 fname, lineno, argv[0] );
517 if ( strcasecmp( argv[1], "none" ) == 0 ) {
518 /* FIXME: is this useful? */
519 li->idassert_authmethod = LDAP_AUTH_NONE;
523 "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
524 fname, lineno, argv[0], argv[1] );
527 } else if ( strcasecmp( argv[1], "simple" ) == 0 ) {
528 li->idassert_authmethod = LDAP_AUTH_SIMPLE;
532 "%s: line %d: trailing args in \"%s %s ...\" line ignored\"\n",
533 fname, lineno, argv[0], argv[1] );
536 } else if ( strcasecmp( argv[1], "sasl" ) == 0 ) {
537 #ifdef HAVE_CYRUS_SASL
540 for ( arg = 2; arg < argc; arg++ ) {
541 if ( strncasecmp( argv[arg], "mech=", STRLENOF( "mech=" ) ) == 0 ) {
542 char *val = argv[arg] + STRLENOF( "mech=" );
544 if ( !BER_BVISNULL( &li->idassert_sasl_mech ) ) {
545 fprintf( stderr, "%s: line %d: "
546 "SASL mech already defined; replacing...\n",
548 ch_free( li->idassert_sasl_mech.bv_val );
550 ber_str2bv( val, 0, 1, &li->idassert_sasl_mech );
552 } else if ( strncasecmp( argv[arg], "realm=", STRLENOF( "realm=" ) ) == 0 ) {
553 char *val = argv[arg] + STRLENOF( "realm=" );
555 if ( !BER_BVISNULL( &li->idassert_sasl_realm ) ) {
556 fprintf( stderr, "%s: line %d: "
557 "SASL realm already defined; replacing...\n",
559 ch_free( li->idassert_sasl_realm.bv_val );
561 ber_str2bv( val, 0, 1, &li->idassert_sasl_realm );
563 } else if ( strncasecmp( argv[arg], "authcdn=", STRLENOF( "authcdn=" ) ) == 0 ) {
564 char *val = argv[arg] + STRLENOF( "authcdn=" );
568 if ( !BER_BVISNULL( &li->idassert_authcDN ) ) {
569 fprintf( stderr, "%s: line %d: "
570 "SASL authcDN already defined; replacing...\n",
572 ch_free( li->idassert_authcDN.bv_val );
574 if ( strncasecmp( argv[arg], "dn:", STRLENOF( "dn:" ) ) == 0 ) {
575 val += STRLENOF( "dn:" );
578 ber_str2bv( val, 0, 0, &dn );
579 rc = dnNormalize( 0, NULL, NULL, &dn, &li->idassert_authcDN, NULL );
580 if ( rc != LDAP_SUCCESS ) {
581 Debug( LDAP_DEBUG_ANY,
582 "%s: line %d: SASL authcdn \"%s\" is not a valid DN\n",
583 fname, lineno, val );
587 } else if ( strncasecmp( argv[arg], "authcid=", STRLENOF( "authcid=" ) ) == 0 ) {
588 char *val = argv[arg] + STRLENOF( "authcid=" );
590 if ( !BER_BVISNULL( &li->idassert_authcID ) ) {
591 fprintf( stderr, "%s: line %d: "
592 "SASL authcID already defined; replacing...\n",
594 ch_free( li->idassert_authcID.bv_val );
596 if ( strncasecmp( argv[arg], "u:", STRLENOF( "u:" ) ) == 0 ) {
597 val += STRLENOF( "u:" );
599 ber_str2bv( val, 0, 1, &li->idassert_authcID );
601 } else if ( strncasecmp( argv[arg], "cred=", STRLENOF( "cred=" ) ) == 0 ) {
602 char *val = argv[arg] + STRLENOF( "cred=" );
604 if ( !BER_BVISNULL( &li->idassert_passwd ) ) {
605 fprintf( stderr, "%s: line %d: "
606 "SASL cred already defined; replacing...\n",
608 ch_free( li->idassert_passwd.bv_val );
610 ber_str2bv( val, 0, 1, &li->idassert_passwd );
612 } else if ( strncasecmp( argv[arg], "authz=", STRLENOF( "authz=" ) ) == 0 ) {
613 char *val = argv[arg] + STRLENOF( "authz=" );
615 if ( strcasecmp( val, "proxyauthz" ) == 0 ) {
616 li->idassert_flags &= ~LDAP_BACK_AUTH_NATIVE_AUTHZ;
618 } else if ( strcasecmp( val, "native" ) == 0 ) {
619 li->idassert_flags |= LDAP_BACK_AUTH_NATIVE_AUTHZ;
622 fprintf( stderr, "%s: line %s: "
623 "unknown authz mode \"%s\"\n",
624 fname, lineno, val );
629 fprintf( stderr, "%s: line %d: "
630 "unknown SASL parameter %s\n",
631 fname, lineno, argv[arg] );
636 li->idassert_authmethod = LDAP_AUTH_SASL;
638 #else /* !HAVE_CYRUS_SASL */
639 fprintf( stderr, "%s: line %d: "
640 "compile --with-cyrus-sasl to enable SASL auth\n",
643 #endif /* !HAVE_CYRUS_SASL */
646 fprintf( stderr, "%s: line %d: "
647 "unhandled idassert-method method %s\n",
648 fname, lineno, argv[1] );
653 return SLAP_CONF_UNKNOWN;
658 #endif /* LDAP_BACK_PROXY_AUTHZ */