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;
46 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
49 fprintf( stderr, "%s: line %d: ldap backend info is null!\n",
54 /* server address to query (depricated, use "uri" directive) */
55 if ( strcasecmp( argv[0], "server" ) == 0 ) {
58 "%s: line %d: missing address in \"server <address>\" line\n",
64 li->url = ch_calloc(strlen(argv[1]) + 9, sizeof(char));
65 if (li->url != NULL) {
66 strcpy(li->url, "ldap://");
67 strcat(li->url, argv[1]);
71 /* URI of server to query (preferred over "server" directive) */
72 } else if ( strcasecmp( argv[0], "uri" ) == 0 ) {
75 "%s: line %d: missing address in \"uri <address>\" line\n",
81 li->url = ch_strdup(argv[1]);
83 /* name to use for ldap_back_group */
84 } else if ( strcasecmp( argv[0], "binddn" ) == 0 ) {
87 "%s: line %d: missing name in \"binddn <name>\" line\n",
91 ber_str2bv( argv[1], 0, 1, &li->binddn );
93 /* password to use for ldap_back_group */
94 } else if ( strcasecmp( argv[0], "bindpw" ) == 0 ) {
97 "%s: line %d: missing password in \"bindpw <password>\" line\n",
101 ber_str2bv( argv[1], 0, 1, &li->bindpw );
103 #ifdef LDAP_BACK_PROXY_AUTHZ
104 /* name to use for proxyAuthz propagation */
105 } else if ( strcasecmp( argv[0], "proxyauthzdn" ) == 0 ) {
108 "%s: line %d: missing name in \"proxyauthzdn <name>\" line\n",
112 ber_str2bv( argv[1], 0, 1, &li->proxyauthzdn );
114 /* password to use for proxyAuthz propagation */
115 } else if ( strcasecmp( argv[0], "proxyauthzpw" ) == 0 ) {
118 "%s: line %d: missing password in \"proxyauthzpw <password>\" line\n",
122 ber_str2bv( argv[1], 0, 1, &li->proxyauthzpw );
123 #endif /* LDAP_BACK_PROXY_AUTHZ */
125 /* save bind creds for referral rebinds? */
126 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
129 "%s: line %d: rebind-as-user takes no arguments\n",
135 /* intercept exop_who_am_i? */
136 } else if ( strcasecmp( argv[0], "proxy-whoami" ) == 0 ) {
139 "%s: line %d: proxy-whoami takes no arguments\n",
143 load_extop( (struct berval *)&slap_EXOP_WHOAMI,
144 0, ldap_back_exop_whoami );
147 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
149 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
150 #ifdef ENABLE_REWRITE
152 #endif /* ENABLE_REWRITE */
157 * suffixmassage <suffix> <massaged suffix>
159 * the <suffix> field must be defined as a valid suffix
160 * (or suffixAlias?) for the current database;
161 * the <massaged suffix> shouldn't have already been
162 * defined as a valid suffix or suffixAlias for the
166 fprintf( stderr, "%s: line %d: syntax is"
167 " \"suffixMassage <suffix>"
168 " <massaged suffix>\"\n",
173 ber_str2bv( argv[1], 0, 0, &bvnc );
174 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
175 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
176 fname, lineno, bvnc.bv_val );
179 tmp_be = select_backend( &nvnc, 0, 0 );
180 if ( tmp_be != NULL && tmp_be != be ) {
181 fprintf( stderr, "%s: line %d: suffix already in use"
182 " by another backend in"
183 " \"suffixMassage <suffix>"
184 " <massaged suffix>\"\n",
191 ber_str2bv( argv[2], 0, 0, &brnc );
192 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
193 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
194 fname, lineno, brnc.bv_val );
201 tmp_be = select_backend( &nrnc, 0, 0 );
202 if ( tmp_be != NULL ) {
203 fprintf( stderr, "%s: line %d: massaged suffix"
204 " already in use by another backend in"
205 " \"suffixMassage <suffix>"
206 " <massaged suffix>\"\n",
216 #ifdef ENABLE_REWRITE
218 * The suffix massaging is emulated by means of the
219 * rewrite capabilities
220 * FIXME: no extra rewrite capabilities should be added
223 rc = suffix_massage_config( li->rwmap.rwm_rw,
224 &pvnc, &nvnc, &prnc, &nrnc );
232 #else /* !ENABLE_REWRITE */
233 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &pvnc );
234 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &nvnc );
236 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &prnc );
237 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &nrnc );
238 #endif /* !ENABLE_REWRITE */
240 /* rewrite stuff ... */
241 } else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
242 #ifdef ENABLE_REWRITE
243 return rewrite_parse( li->rwmap.rwm_rw,
244 fname, lineno, argc, argv );
246 #else /* !ENABLE_REWRITE */
247 fprintf( stderr, "%s: line %d: rewrite capabilities "
248 "are not enabled\n", fname, lineno );
249 #endif /* !ENABLE_REWRITE */
251 /* objectclass/attribute mapping */
252 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
253 return ldap_back_map_config( &li->rwmap.rwm_oc,
255 fname, lineno, argc, argv );
259 return SLAP_CONF_UNKNOWN;
265 ldap_back_map_config(
266 struct ldapmap *oc_map,
267 struct ldapmap *at_map,
274 struct ldapmapping *mapping;
278 if ( argc < 3 || argc > 4 ) {
280 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
285 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
289 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
293 fprintf( stderr, "%s: line %d: syntax is "
294 "\"map {objectclass | attribute} [<local> | *] "
295 "{<foreign> | *}\"\n",
300 if ( strcmp( argv[2], "*" ) == 0 ) {
301 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
302 map->drop_missing = ( argc < 4 );
307 } else if ( argc < 4 ) {
313 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
316 if ( ( map == at_map )
317 && ( strcasecmp( src, "objectclass" ) == 0
318 || strcasecmp( dst, "objectclass" ) == 0 ) )
321 "%s: line %d: objectclass attribute cannot be mapped\n",
325 mapping = (struct ldapmapping *)ch_calloc( 2,
326 sizeof(struct ldapmapping) );
327 if ( mapping == NULL ) {
329 "%s: line %d: out of memory\n",
333 ber_str2bv( src, 0, 1, &mapping->src );
334 ber_str2bv( dst, 0, 1, &mapping->dst );
335 mapping[1].src = mapping->dst;
336 mapping[1].dst = mapping->src;
342 if ( src[0] != '\0' ) {
343 if ( oc_bvfind( &mapping->src ) == NULL ) {
345 "%s: line %d: warning, source objectClass '%s' "
346 "should be defined in schema\n",
347 fname, lineno, src );
350 * FIXME: this should become an err
355 if ( oc_bvfind( &mapping->dst ) == NULL ) {
357 "%s: line %d: warning, destination objectClass '%s' "
358 "is not defined in schema\n",
359 fname, lineno, dst );
363 const char *text = NULL;
364 AttributeDescription *ad = NULL;
366 if ( src[0] != '\0' ) {
367 rc = slap_bv2ad( &mapping->src, &ad, &text );
368 if ( rc != LDAP_SUCCESS ) {
370 "%s: line %d: warning, source attributeType '%s' "
371 "should be defined in schema\n",
372 fname, lineno, src );
375 * FIXME: this should become an err
382 rc = slap_bv2ad( &mapping->dst, &ad, &text );
383 if ( rc != LDAP_SUCCESS ) {
385 "%s: line %d: warning, destination attributeType '%s' "
386 "is not defined in schema\n",
387 fname, lineno, dst );
391 if ( (src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL)
392 || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
395 "%s: line %d: duplicate mapping found (ignored)\n",
397 /* FIXME: free stuff */
401 if ( src[0] != '\0' ) {
402 avl_insert( &map->map, (caddr_t)mapping,
403 mapping_cmp, mapping_dup );
405 avl_insert( &map->remap, (caddr_t)&mapping[1],
406 mapping_cmp, mapping_dup );
412 ch_free( mapping->src.bv_val );
413 ch_free( mapping->dst.bv_val );
421 ldap_back_exop_whoami(
425 struct berval *bv = NULL;
427 if ( op->oq_extended.rs_reqdata != NULL ) {
428 /* no request data should be provided */
429 rs->sr_text = "no request data expected";
430 return rs->sr_err = LDAP_PROTOCOL_ERROR;
433 rs->sr_err = backend_check_restrictions( op, rs,
434 (struct berval *)&slap_EXOP_WHOAMI );
435 if( rs->sr_err != LDAP_SUCCESS ) return rs->sr_err;
437 /* if auth'd by back-ldap and request is proxied, forward it */
438 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)) {
441 LDAPControl c, *ctrls[2] = {NULL, NULL};
447 op2.o_ndn = op->o_conn->c_ndn;
448 lc = ldap_back_getconn(&op2, rs);
449 if (!lc || !ldap_back_dobind( lc, op, rs )) {
452 c.ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
453 c.ldctl_iscritical = 1;
454 c.ldctl_value.bv_val = ch_malloc(op->o_ndn.bv_len+4);
455 c.ldctl_value.bv_len = op->o_ndn.bv_len + 3;
456 strcpy(c.ldctl_value.bv_val, "dn:");
457 strcpy(c.ldctl_value.bv_val+3, op->o_ndn.bv_val);
459 rs->sr_err = ldap_whoami(lc->ld, ctrls, NULL, &msgid);
460 if (rs->sr_err == LDAP_SUCCESS) {
461 if (ldap_result(lc->ld, msgid, 1, NULL, &res) == -1) {
462 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER,
465 rs->sr_err = ldap_parse_whoami(lc->ld, res, &bv);
469 ch_free(c.ldctl_value.bv_val);
470 if (rs->sr_err != LDAP_SUCCESS) {
471 rs->sr_err = ldap_back_map_result(rs);
474 /* else just do the same as before */
475 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
476 if( op->o_dn.bv_len ) {
477 bv->bv_len = op->o_dn.bv_len + sizeof("dn:")-1;
478 bv->bv_val = ch_malloc( bv->bv_len + 1 );
479 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:")-1 );
480 AC_MEMCPY( &bv->bv_val[sizeof("dn:")-1], op->o_dn.bv_val,
482 bv->bv_val[bv->bv_len] = '\0';
494 #ifdef ENABLE_REWRITE
496 suffix_massage_regexize( const char *s )
503 ( r = strchr( p, ',' ) ) != NULL;
507 res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
509 ptr = lutil_strcopy( res, "(.*)" );
511 ( r = strchr( p, ',' ) ) != NULL;
513 ptr = lutil_strncopy( ptr, p, r - p + 1 );
514 ptr = lutil_strcopy( ptr, "[ ]?" );
516 if ( r[ 1 ] == ' ' ) {
520 lutil_strcopy( ptr, p );
526 suffix_massage_patternize( const char *s )
533 res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
539 strcpy( res + sizeof( "%1" ) - 1, s );
545 suffix_massage_config(
546 struct rewrite_info *info,
556 rargv[ 0 ] = "rewriteEngine";
559 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
561 rargv[ 0 ] = "rewriteContext";
562 rargv[ 1 ] = "default";
564 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
566 rargv[ 0 ] = "rewriteRule";
567 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
568 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
571 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
572 ch_free( rargv[ 1 ] );
573 ch_free( rargv[ 2 ] );
575 rargv[ 0 ] = "rewriteContext";
576 rargv[ 1 ] = "searchResult";
578 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
580 rargv[ 0 ] = "rewriteRule";
581 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
582 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
585 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
586 ch_free( rargv[ 1 ] );
587 ch_free( rargv[ 2 ] );
589 #if 0 /* "matched" is not normalized */
590 rargv[ 0 ] = "rewriteContext";
591 rargv[ 1 ] = "matchedDN";
592 rargv[ 2 ] = "alias";
593 rargv[ 3 ] = "searchResult";
595 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
596 #else /* normalize "matched" */
598 rargv[ 0 ] = "rewriteContext";
599 rargv[ 1 ] = "matchedDN";
600 rargv[ 2 ] = "alias";
601 rargv[ 3 ] = "searchResult";
603 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
605 rargv[ 0 ] = "rewriteContext";
606 rargv[ 1 ] = "searchAttrDN";
607 rargv[ 2 ] = "alias";
608 rargv[ 3 ] = "searchResult";
610 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
613 rargv[ 0 ] = "rewriteRule";
614 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
615 rargv[ 2 ] = suffix_massage_patternize( nvnc->bv_val );
618 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
619 ch_free( rargv[ 1 ] );
620 ch_free( rargv[ 2 ] );
622 #endif /* normalize "matched" */
626 #endif /* ENABLE_REWRITE */