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.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
17 * This work was initially developed by the Howard Chu for inclusion
18 * in OpenLDAP Software and subsequently enhanced by Pierangelo
21 /* This is an altered version */
23 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
25 * Permission is granted to anyone to use this software for any purpose
26 * on any computer system, and to alter it and redistribute it, subject
27 * to the following restrictions:
29 * 1. The author is not responsible for the consequences of use of this
30 * software, no matter how awful, even if they arise from flaws in it.
32 * 2. The origin of this software must not be misrepresented, either by
33 * explicit claim or by omission. Since few users ever read sources,
34 * credits should appear in the documentation.
36 * 3. Altered versions must be plainly marked as such, and must not be
37 * misrepresented as being the original software. Since few users
38 * ever read sources, credits should appear in the documentation.
40 * 4. This notice may not be removed or altered.
44 * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
46 * This software is being modified by Pierangelo Masarati.
47 * The previously reported conditions apply to the modified code as well.
48 * Changes in the original code are highlighted where required.
49 * Credits for the original code go to the author, Howard Chu.
56 #include <ac/string.h>
57 #include <ac/socket.h>
60 #include "back-ldap.h"
63 static SLAP_EXTOP_MAIN_FN ldap_back_exop_whoami;
74 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
77 fprintf( stderr, "%s: line %d: ldap backend info is null!\n",
82 /* server address to query (depricated, use "uri" directive) */
83 if ( strcasecmp( argv[0], "server" ) == 0 ) {
86 "%s: line %d: missing address in \"server <address>\" line\n",
92 li->url = ch_calloc(strlen(argv[1]) + 9, sizeof(char));
93 if (li->url != NULL) {
94 strcpy(li->url, "ldap://");
95 strcat(li->url, argv[1]);
99 /* URI of server to query (preferred over "server" directive) */
100 } else if ( strcasecmp( argv[0], "uri" ) == 0 ) {
103 "%s: line %d: missing address in \"uri <address>\" line\n",
109 li->url = ch_strdup(argv[1]);
111 /* name to use for ldap_back_group */
112 } else if ( strcasecmp( argv[0], "binddn" ) == 0 ) {
115 "%s: line %d: missing name in \"binddn <name>\" line\n",
119 ber_str2bv( argv[1], 0, 1, &li->binddn );
121 /* password to use for ldap_back_group */
122 } else if ( strcasecmp( argv[0], "bindpw" ) == 0 ) {
125 "%s: line %d: missing password in \"bindpw <password>\" line\n",
129 ber_str2bv( argv[1], 0, 1, &li->bindpw );
131 /* save bind creds for referral rebinds? */
132 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
135 "%s: line %d: rebind-as-user takes no arguments\n",
141 /* intercept exop_who_am_i? */
142 } else if ( strcasecmp( argv[0], "proxy-whoami" ) == 0 ) {
145 "%s: line %d: proxy-whoami takes no arguments\n",
149 load_extop( (struct berval *)&slap_EXOP_WHOAMI,
150 0, ldap_back_exop_whoami );
153 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
155 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
156 #ifdef ENABLE_REWRITE
158 #endif /* ENABLE_REWRITE */
163 * suffixmassage <suffix> <massaged suffix>
165 * the <suffix> field must be defined as a valid suffix
166 * (or suffixAlias?) for the current database;
167 * the <massaged suffix> shouldn't have already been
168 * defined as a valid suffix or suffixAlias for the
172 fprintf( stderr, "%s: line %d: syntax is"
173 " \"suffixMassage <suffix>"
174 " <massaged suffix>\"\n",
179 ber_str2bv( argv[1], 0, 0, &bvnc );
180 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
181 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
182 fname, lineno, bvnc.bv_val );
185 tmp_be = select_backend( &nvnc, 0, 0 );
186 if ( tmp_be != NULL && tmp_be != be ) {
187 fprintf( stderr, "%s: line %d: suffix already in use"
188 " by another backend in"
189 " \"suffixMassage <suffix>"
190 " <massaged suffix>\"\n",
197 ber_str2bv( argv[2], 0, 0, &brnc );
198 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
199 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
200 fname, lineno, brnc.bv_val );
207 tmp_be = select_backend( &nrnc, 0, 0 );
208 if ( tmp_be != NULL ) {
209 fprintf( stderr, "%s: line %d: massaged suffix"
210 " already in use by another backend in"
211 " \"suffixMassage <suffix>"
212 " <massaged suffix>\"\n",
222 #ifdef ENABLE_REWRITE
224 * The suffix massaging is emulated by means of the
225 * rewrite capabilities
226 * FIXME: no extra rewrite capabilities should be added
229 rc = suffix_massage_config( li->rwmap.rwm_rw,
230 &pvnc, &nvnc, &prnc, &nrnc );
238 #else /* !ENABLE_REWRITE */
239 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &pvnc );
240 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &nvnc );
242 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &prnc );
243 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &nrnc );
244 #endif /* !ENABLE_REWRITE */
246 /* rewrite stuff ... */
247 } else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
248 #ifdef ENABLE_REWRITE
249 return rewrite_parse( li->rwmap.rwm_rw,
250 fname, lineno, argc, argv );
252 #else /* !ENABLE_REWRITE */
253 fprintf( stderr, "%s: line %d: rewrite capabilities "
254 "are not enabled\n", fname, lineno );
255 #endif /* !ENABLE_REWRITE */
257 /* objectclass/attribute mapping */
258 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
259 return ldap_back_map_config( &li->rwmap.rwm_oc,
261 fname, lineno, argc, argv );
265 fprintf( stderr, "%s: line %d: unknown directive \"%s\" "
266 "in ldap database definition (ignored)\n",
267 fname, lineno, argv[0] );
273 ldap_back_map_config(
274 struct ldapmap *oc_map,
275 struct ldapmap *at_map,
282 struct ldapmapping *mapping;
286 if ( argc < 3 || argc > 4 ) {
288 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
293 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
297 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
301 fprintf( stderr, "%s: line %d: syntax is "
302 "\"map {objectclass | attribute} [<local> | *] "
303 "{<foreign> | *}\"\n",
308 if ( strcmp( argv[2], "*" ) == 0 ) {
309 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
310 map->drop_missing = ( argc < 4 );
315 } else if ( argc < 4 ) {
321 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
324 if ( ( map == at_map )
325 && ( strcasecmp( src, "objectclass" ) == 0
326 || strcasecmp( dst, "objectclass" ) == 0 ) )
329 "%s: line %d: objectclass attribute cannot be mapped\n",
333 mapping = (struct ldapmapping *)ch_calloc( 2,
334 sizeof(struct ldapmapping) );
335 if ( mapping == NULL ) {
337 "%s: line %d: out of memory\n",
341 ber_str2bv( src, 0, 1, &mapping->src );
342 ber_str2bv( dst, 0, 1, &mapping->dst );
343 mapping[1].src = mapping->dst;
344 mapping[1].dst = mapping->src;
350 if ( src[0] != '\0' ) {
351 if ( oc_bvfind( &mapping->src ) == NULL ) {
353 "%s: line %d: warning, source objectClass '%s' "
354 "should be defined in schema\n",
355 fname, lineno, src );
358 * FIXME: this should become an err
363 if ( oc_bvfind( &mapping->dst ) == NULL ) {
365 "%s: line %d: warning, destination objectClass '%s' "
366 "is not defined in schema\n",
367 fname, lineno, dst );
371 const char *text = NULL;
372 AttributeDescription *ad = NULL;
374 if ( src[0] != '\0' ) {
375 rc = slap_bv2ad( &mapping->src, &ad, &text );
376 if ( rc != LDAP_SUCCESS ) {
378 "%s: line %d: warning, source attributeType '%s' "
379 "should be defined in schema\n",
380 fname, lineno, src );
383 * FIXME: this should become an err
390 rc = slap_bv2ad( &mapping->dst, &ad, &text );
391 if ( rc != LDAP_SUCCESS ) {
393 "%s: line %d: warning, destination attributeType '%s' "
394 "is not defined in schema\n",
395 fname, lineno, dst );
399 if ( (src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL)
400 || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
403 "%s: line %d: duplicate mapping found (ignored)\n",
405 /* FIXME: free stuff */
409 if ( src[0] != '\0' ) {
410 avl_insert( &map->map, (caddr_t)mapping,
411 mapping_cmp, mapping_dup );
413 avl_insert( &map->remap, (caddr_t)&mapping[1],
414 mapping_cmp, mapping_dup );
420 ch_free( mapping->src.bv_val );
421 ch_free( mapping->dst.bv_val );
429 ldap_back_exop_whoami(
433 struct berval *bv = NULL;
435 if ( op->oq_extended.rs_reqdata != NULL ) {
436 /* no request data should be provided */
437 rs->sr_text = "no request data expected";
438 return rs->sr_err = LDAP_PROTOCOL_ERROR;
441 rs->sr_err = backend_check_restrictions( op, rs,
442 (struct berval *)&slap_EXOP_WHOAMI );
443 if( rs->sr_err != LDAP_SUCCESS ) return rs->sr_err;
445 /* if auth'd by back-ldap and request is proxied, forward it */
446 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)) {
449 LDAPControl c, *ctrls[2] = {NULL, NULL};
455 op2.o_ndn = op->o_conn->c_ndn;
456 lc = ldap_back_getconn(&op2, rs);
457 if (!lc || !ldap_back_dobind( lc, op, rs )) {
460 c.ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
461 c.ldctl_iscritical = 1;
462 c.ldctl_value.bv_val = ch_malloc(op->o_ndn.bv_len+4);
463 c.ldctl_value.bv_len = op->o_ndn.bv_len + 3;
464 strcpy(c.ldctl_value.bv_val, "dn:");
465 strcpy(c.ldctl_value.bv_val+3, op->o_ndn.bv_val);
467 rs->sr_err = ldap_whoami(lc->ld, ctrls, NULL, &msgid);
468 if (rs->sr_err == LDAP_SUCCESS) {
469 if (ldap_result(lc->ld, msgid, 1, NULL, &res) == -1) {
470 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER,
473 rs->sr_err = ldap_parse_whoami(lc->ld, res, &bv);
477 ch_free(c.ldctl_value.bv_val);
478 if (rs->sr_err != LDAP_SUCCESS) {
479 rs->sr_err = ldap_back_map_result(rs);
482 /* else just do the same as before */
483 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
484 if( op->o_dn.bv_len ) {
485 bv->bv_len = op->o_dn.bv_len + sizeof("dn:")-1;
486 bv->bv_val = ch_malloc( bv->bv_len + 1 );
487 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:")-1 );
488 AC_MEMCPY( &bv->bv_val[sizeof("dn:")-1], op->o_dn.bv_val,
490 bv->bv_val[bv->bv_len] = '\0';
502 #ifdef ENABLE_REWRITE
504 suffix_massage_regexize( const char *s )
511 ( r = strchr( p, ',' ) ) != NULL;
515 res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
517 ptr = lutil_strcopy( res, "(.*)" );
519 ( r = strchr( p, ',' ) ) != NULL;
521 ptr = lutil_strncopy( ptr, p, r - p + 1 );
522 ptr = lutil_strcopy( ptr, "[ ]?" );
524 if ( r[ 1 ] == ' ' ) {
528 lutil_strcopy( ptr, p );
534 suffix_massage_patternize( const char *s )
541 res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
547 strcpy( res + sizeof( "%1" ) - 1, s );
553 suffix_massage_config(
554 struct rewrite_info *info,
564 rargv[ 0 ] = "rewriteEngine";
567 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
569 rargv[ 0 ] = "rewriteContext";
570 rargv[ 1 ] = "default";
572 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
574 rargv[ 0 ] = "rewriteRule";
575 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
576 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
579 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
580 ch_free( rargv[ 1 ] );
581 ch_free( rargv[ 2 ] );
583 rargv[ 0 ] = "rewriteContext";
584 rargv[ 1 ] = "searchResult";
586 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
588 rargv[ 0 ] = "rewriteRule";
589 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
590 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
593 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
594 ch_free( rargv[ 1 ] );
595 ch_free( rargv[ 2 ] );
599 * FIXME: this is no longer required since now we map filters
600 * based on the parsed filter structure, so we can deal directly
601 * with attribute types and values. The rewriteContext
602 * "searchFilter" now refers to the value of attrbutes
607 * the filter should be rewritten as
610 * "(.*)member=([^)]+),o=Foo Bar,[ ]?c=US(.*)"
611 * "%1member=%2,dc=example,dc=com%3"
613 * where "o=Foo Bar, c=US" is the virtual naming context,
614 * and "dc=example, dc=com" is the real naming context
616 rargv[ 0 ] = "rewriteContext";
617 rargv[ 1 ] = "searchFilter";
619 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
621 #if 1 /* rewrite filters */
624 * Note: this is far more optimistic than desirable:
625 * for any AVA value ending with the virtual naming
626 * context the terminal part will be replaced by the
627 * real naming context; a better solution would be to
628 * walk the filter looking for DN-valued attributes,
629 * and only rewrite those that require rewriting
631 char vbuf_[BUFSIZ], *vbuf = vbuf_,
632 rbuf_[BUFSIZ], *rbuf = rbuf_;
635 len = snprintf( vbuf, sizeof( vbuf_ ),
636 "(.*)%s\\)(.*)", nvnc->bv_val );
639 * traditional behavior: snprintf returns -1
640 * if buffer is insufficient
644 } else if ( len >= (int)sizeof( vbuf_ ) ) {
646 * C99: snprintf returns the required size
648 vbuf = ch_malloc( len + 1 );
649 len = snprintf( vbuf, len,
650 "(.*)%s\\)(.*)", nvnc->bv_val );
654 len = snprintf( rbuf, sizeof( rbuf_ ), "%%1%s)%%2",
659 } else if ( len >= (int)sizeof( rbuf_ ) ) {
660 rbuf = ch_malloc( len + 1 );
661 len = snprintf( rbuf, sizeof( rbuf_ ), "%%1%s)%%2",
666 rargv[ 0 ] = "rewriteRule";
671 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
673 if ( vbuf != vbuf_ ) {
677 if ( rbuf != rbuf_ ) {
681 #endif /* rewrite filters */
684 #if 0 /* "matched" is not normalized */
685 rargv[ 0 ] = "rewriteContext";
686 rargv[ 1 ] = "matchedDn";
687 rargv[ 2 ] = "alias";
688 rargv[ 3 ] = "searchResult";
690 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
691 #else /* normalize "matched" */
693 rargv[ 0 ] = "rewriteContext";
694 rargv[ 1 ] = "matchedDN";
695 rargv[ 2 ] = "alias";
696 rargv[ 3 ] = "searchResult";
698 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
700 rargv[ 0 ] = "rewriteContext";
701 rargv[ 1 ] = "searchAttrDN";
702 rargv[ 2 ] = "alias";
703 rargv[ 3 ] = "searchResult";
705 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
708 rargv[ 0 ] = "rewriteRule";
709 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
710 rargv[ 2 ] = suffix_massage_patternize( nvnc->bv_val );
713 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
714 ch_free( rargv[ 1 ] );
715 ch_free( rargv[ 2 ] );
717 #endif /* normalize "matched" */
721 #endif /* ENABLE_REWRITE */