1 /* config.c - ldap backend configuration file routine */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 2003 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 /* save bind creds for referral rebinds? */
104 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
107 "%s: line %d: rebind-as-user takes no arguments\n",
113 /* intercept exop_who_am_i? */
114 } else if ( strcasecmp( argv[0], "proxy-whoami" ) == 0 ) {
117 "%s: line %d: proxy-whoami takes no arguments\n",
121 load_extop( (struct berval *)&slap_EXOP_WHOAMI,
122 0, ldap_back_exop_whoami );
125 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
127 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
128 #ifdef ENABLE_REWRITE
130 #endif /* ENABLE_REWRITE */
135 * suffixmassage <suffix> <massaged suffix>
137 * the <suffix> field must be defined as a valid suffix
138 * (or suffixAlias?) for the current database;
139 * the <massaged suffix> shouldn't have already been
140 * defined as a valid suffix or suffixAlias for the
144 fprintf( stderr, "%s: line %d: syntax is"
145 " \"suffixMassage <suffix>"
146 " <massaged suffix>\"\n",
151 ber_str2bv( argv[1], 0, 0, &bvnc );
152 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
153 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
154 fname, lineno, bvnc.bv_val );
157 tmp_be = select_backend( &nvnc, 0, 0 );
158 if ( tmp_be != NULL && tmp_be != be ) {
159 fprintf( stderr, "%s: line %d: suffix already in use"
160 " by another backend in"
161 " \"suffixMassage <suffix>"
162 " <massaged suffix>\"\n",
169 ber_str2bv( argv[2], 0, 0, &brnc );
170 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
171 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
172 fname, lineno, brnc.bv_val );
179 tmp_be = select_backend( &nrnc, 0, 0 );
180 if ( tmp_be != NULL ) {
181 fprintf( stderr, "%s: line %d: massaged suffix"
182 " already in use by another backend in"
183 " \"suffixMassage <suffix>"
184 " <massaged suffix>\"\n",
194 #ifdef ENABLE_REWRITE
196 * The suffix massaging is emulated by means of the
197 * rewrite capabilities
198 * FIXME: no extra rewrite capabilities should be added
201 rc = suffix_massage_config( li->rwmap.rwm_rw,
202 &pvnc, &nvnc, &prnc, &nrnc );
210 #else /* !ENABLE_REWRITE */
211 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &pvnc );
212 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &nvnc );
214 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &prnc );
215 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &nrnc );
216 #endif /* !ENABLE_REWRITE */
218 /* rewrite stuff ... */
219 } else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
220 #ifdef ENABLE_REWRITE
221 return rewrite_parse( li->rwmap.rwm_rw,
222 fname, lineno, argc, argv );
224 #else /* !ENABLE_REWRITE */
225 fprintf( stderr, "%s: line %d: rewrite capabilities "
226 "are not enabled\n", fname, lineno );
227 #endif /* !ENABLE_REWRITE */
229 /* objectclass/attribute mapping */
230 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
231 return ldap_back_map_config( &li->rwmap.rwm_oc,
233 fname, lineno, argc, argv );
237 return SLAP_CONF_UNKNOWN;
243 ldap_back_map_config(
244 struct ldapmap *oc_map,
245 struct ldapmap *at_map,
252 struct ldapmapping *mapping;
256 if ( argc < 3 || argc > 4 ) {
258 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
263 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
267 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
271 fprintf( stderr, "%s: line %d: syntax is "
272 "\"map {objectclass | attribute} [<local> | *] "
273 "{<foreign> | *}\"\n",
278 if ( strcmp( argv[2], "*" ) == 0 ) {
279 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
280 map->drop_missing = ( argc < 4 );
285 } else if ( argc < 4 ) {
291 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
294 if ( ( map == at_map )
295 && ( strcasecmp( src, "objectclass" ) == 0
296 || strcasecmp( dst, "objectclass" ) == 0 ) )
299 "%s: line %d: objectclass attribute cannot be mapped\n",
303 mapping = (struct ldapmapping *)ch_calloc( 2,
304 sizeof(struct ldapmapping) );
305 if ( mapping == NULL ) {
307 "%s: line %d: out of memory\n",
311 ber_str2bv( src, 0, 1, &mapping->src );
312 ber_str2bv( dst, 0, 1, &mapping->dst );
313 mapping[1].src = mapping->dst;
314 mapping[1].dst = mapping->src;
320 if ( src[0] != '\0' ) {
321 if ( oc_bvfind( &mapping->src ) == NULL ) {
323 "%s: line %d: warning, source objectClass '%s' "
324 "should be defined in schema\n",
325 fname, lineno, src );
328 * FIXME: this should become an err
333 if ( oc_bvfind( &mapping->dst ) == NULL ) {
335 "%s: line %d: warning, destination objectClass '%s' "
336 "is not defined in schema\n",
337 fname, lineno, dst );
341 const char *text = NULL;
342 AttributeDescription *ad = NULL;
344 if ( src[0] != '\0' ) {
345 rc = slap_bv2ad( &mapping->src, &ad, &text );
346 if ( rc != LDAP_SUCCESS ) {
348 "%s: line %d: warning, source attributeType '%s' "
349 "should be defined in schema\n",
350 fname, lineno, src );
353 * FIXME: this should become an err
360 rc = slap_bv2ad( &mapping->dst, &ad, &text );
361 if ( rc != LDAP_SUCCESS ) {
363 "%s: line %d: warning, destination attributeType '%s' "
364 "is not defined in schema\n",
365 fname, lineno, dst );
369 if ( (src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL)
370 || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
373 "%s: line %d: duplicate mapping found (ignored)\n",
375 /* FIXME: free stuff */
379 if ( src[0] != '\0' ) {
380 avl_insert( &map->map, (caddr_t)mapping,
381 mapping_cmp, mapping_dup );
383 avl_insert( &map->remap, (caddr_t)&mapping[1],
384 mapping_cmp, mapping_dup );
390 ch_free( mapping->src.bv_val );
391 ch_free( mapping->dst.bv_val );
399 ldap_back_exop_whoami(
403 struct berval *bv = NULL;
405 if ( op->oq_extended.rs_reqdata != NULL ) {
406 /* no request data should be provided */
407 rs->sr_text = "no request data expected";
408 return rs->sr_err = LDAP_PROTOCOL_ERROR;
411 rs->sr_err = backend_check_restrictions( op, rs,
412 (struct berval *)&slap_EXOP_WHOAMI );
413 if( rs->sr_err != LDAP_SUCCESS ) return rs->sr_err;
415 /* if auth'd by back-ldap and request is proxied, forward it */
416 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)) {
419 LDAPControl c, *ctrls[2] = {NULL, NULL};
425 op2.o_ndn = op->o_conn->c_ndn;
426 lc = ldap_back_getconn(&op2, rs);
427 if (!lc || !ldap_back_dobind( lc, op, rs )) {
430 c.ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
431 c.ldctl_iscritical = 1;
432 c.ldctl_value.bv_val = ch_malloc(op->o_ndn.bv_len+4);
433 c.ldctl_value.bv_len = op->o_ndn.bv_len + 3;
434 strcpy(c.ldctl_value.bv_val, "dn:");
435 strcpy(c.ldctl_value.bv_val+3, op->o_ndn.bv_val);
437 rs->sr_err = ldap_whoami(lc->ld, ctrls, NULL, &msgid);
438 if (rs->sr_err == LDAP_SUCCESS) {
439 if (ldap_result(lc->ld, msgid, 1, NULL, &res) == -1) {
440 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER,
443 rs->sr_err = ldap_parse_whoami(lc->ld, res, &bv);
447 ch_free(c.ldctl_value.bv_val);
448 if (rs->sr_err != LDAP_SUCCESS) {
449 rs->sr_err = ldap_back_map_result(rs);
452 /* else just do the same as before */
453 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
454 if( op->o_dn.bv_len ) {
455 bv->bv_len = op->o_dn.bv_len + sizeof("dn:")-1;
456 bv->bv_val = ch_malloc( bv->bv_len + 1 );
457 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:")-1 );
458 AC_MEMCPY( &bv->bv_val[sizeof("dn:")-1], op->o_dn.bv_val,
460 bv->bv_val[bv->bv_len] = '\0';
472 #ifdef ENABLE_REWRITE
474 suffix_massage_regexize( const char *s )
481 ( r = strchr( p, ',' ) ) != NULL;
485 res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
487 ptr = lutil_strcopy( res, "(.*)" );
489 ( r = strchr( p, ',' ) ) != NULL;
491 ptr = lutil_strncopy( ptr, p, r - p + 1 );
492 ptr = lutil_strcopy( ptr, "[ ]?" );
494 if ( r[ 1 ] == ' ' ) {
498 lutil_strcopy( ptr, p );
504 suffix_massage_patternize( const char *s )
511 res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
517 strcpy( res + sizeof( "%1" ) - 1, s );
523 suffix_massage_config(
524 struct rewrite_info *info,
534 rargv[ 0 ] = "rewriteEngine";
537 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
539 rargv[ 0 ] = "rewriteContext";
540 rargv[ 1 ] = "default";
542 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
544 rargv[ 0 ] = "rewriteRule";
545 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
546 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
549 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
550 ch_free( rargv[ 1 ] );
551 ch_free( rargv[ 2 ] );
553 rargv[ 0 ] = "rewriteContext";
554 rargv[ 1 ] = "searchResult";
556 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
558 rargv[ 0 ] = "rewriteRule";
559 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
560 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
563 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
564 ch_free( rargv[ 1 ] );
565 ch_free( rargv[ 2 ] );
569 * FIXME: this is no longer required since now we map filters
570 * based on the parsed filter structure, so we can deal directly
571 * with attribute types and values. The rewriteContext
572 * "searchFilter" now refers to the value of attrbutes
577 * the filter should be rewritten as
580 * "(.*)member=([^)]+),o=Foo Bar,[ ]?c=US(.*)"
581 * "%1member=%2,dc=example,dc=com%3"
583 * where "o=Foo Bar, c=US" is the virtual naming context,
584 * and "dc=example, dc=com" is the real naming context
586 rargv[ 0 ] = "rewriteContext";
587 rargv[ 1 ] = "searchFilter";
589 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
591 #if 1 /* rewrite filters */
594 * Note: this is far more optimistic than desirable:
595 * for any AVA value ending with the virtual naming
596 * context the terminal part will be replaced by the
597 * real naming context; a better solution would be to
598 * walk the filter looking for DN-valued attributes,
599 * and only rewrite those that require rewriting
601 char vbuf_[BUFSIZ], *vbuf = vbuf_,
602 rbuf_[BUFSIZ], *rbuf = rbuf_;
605 len = snprintf( vbuf, sizeof( vbuf_ ),
606 "(.*)%s\\)(.*)", nvnc->bv_val );
609 * traditional behavior: snprintf returns -1
610 * if buffer is insufficient
614 } else if ( len >= (int)sizeof( vbuf_ ) ) {
616 * C99: snprintf returns the required size
618 vbuf = ch_malloc( len + 1 );
619 len = snprintf( vbuf, len,
620 "(.*)%s\\)(.*)", nvnc->bv_val );
624 len = snprintf( rbuf, sizeof( rbuf_ ), "%%1%s)%%2",
629 } else if ( len >= (int)sizeof( rbuf_ ) ) {
630 rbuf = ch_malloc( len + 1 );
631 len = snprintf( rbuf, sizeof( rbuf_ ), "%%1%s)%%2",
636 rargv[ 0 ] = "rewriteRule";
641 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
643 if ( vbuf != vbuf_ ) {
647 if ( rbuf != rbuf_ ) {
651 #endif /* rewrite filters */
654 #if 0 /* "matched" is not normalized */
655 rargv[ 0 ] = "rewriteContext";
656 rargv[ 1 ] = "matchedDn";
657 rargv[ 2 ] = "alias";
658 rargv[ 3 ] = "searchResult";
660 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
661 #else /* normalize "matched" */
663 rargv[ 0 ] = "rewriteContext";
664 rargv[ 1 ] = "matchedDN";
665 rargv[ 2 ] = "alias";
666 rargv[ 3 ] = "searchResult";
668 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
670 rargv[ 0 ] = "rewriteContext";
671 rargv[ 1 ] = "searchAttrDN";
672 rargv[ 2 ] = "alias";
673 rargv[ 3 ] = "searchResult";
675 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
678 rargv[ 0 ] = "rewriteRule";
679 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
680 rargv[ 2 ] = suffix_massage_patternize( nvnc->bv_val );
683 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
684 ch_free( rargv[ 1 ] );
685 ch_free( rargv[ 2 ] );
687 #endif /* normalize "matched" */
691 #endif /* ENABLE_REWRITE */