1 /* config.c - ldap backend configuration file routine */
4 * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 /* This is an altered version */
9 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
11 * Permission is granted to anyone to use this software for any purpose
12 * on any computer system, and to alter it and redistribute it, subject
13 * to the following restrictions:
15 * 1. The author is not responsible for the consequences of use of this
16 * software, no matter how awful, even if they arise from flaws in it.
18 * 2. The origin of this software must not be misrepresented, either by
19 * explicit claim or by omission. Since few users ever read sources,
20 * credits should appear in the documentation.
22 * 3. Altered versions must be plainly marked as such, and must not be
23 * misrepresented as being the original software. Since few users
24 * ever read sources, credits should appear in the documentation.
26 * 4. This notice may not be removed or altered.
30 * Copyright 2000, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
32 * This software is being modified by Pierangelo Masarati.
33 * The previously reported conditions apply to the modified code as well.
34 * Changes in the original code are highlighted where required.
35 * Credits for the original code go to the author, Howard Chu.
42 #include <ac/string.h>
43 #include <ac/socket.h>
46 #include "back-ldap.h"
49 static SLAP_EXTOP_MAIN_FN ldap_back_exop_whoami;
60 struct ldapinfo *li = (struct ldapinfo *) be->be_private;
63 fprintf( stderr, "%s: line %d: ldap backend info is null!\n",
68 /* server address to query (depricated, use "uri" directive) */
69 if ( strcasecmp( argv[0], "server" ) == 0 ) {
72 "%s: line %d: missing address in \"server <address>\" line\n",
78 li->url = ch_calloc(strlen(argv[1]) + 9, sizeof(char));
79 if (li->url != NULL) {
80 strcpy(li->url, "ldap://");
81 strcat(li->url, argv[1]);
85 /* URI of server to query (preferred over "server" directive) */
86 } else if ( strcasecmp( argv[0], "uri" ) == 0 ) {
89 "%s: line %d: missing address in \"uri <address>\" line\n",
95 li->url = ch_strdup(argv[1]);
97 /* name to use for ldap_back_group */
98 } else if ( strcasecmp( argv[0], "binddn" ) == 0 ) {
101 "%s: line %d: missing name in \"binddn <name>\" line\n",
105 ber_str2bv( argv[1], 0, 1, &li->binddn );
107 /* password to use for ldap_back_group */
108 } else if ( strcasecmp( argv[0], "bindpw" ) == 0 ) {
111 "%s: line %d: missing password in \"bindpw <password>\" line\n",
115 ber_str2bv( argv[1], 0, 1, &li->bindpw );
117 /* save bind creds for referral rebinds? */
118 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
121 "%s: line %d: rebind-as-user takes no arguments\n",
127 /* intercept exop_who_am_i? */
128 } else if ( strcasecmp( argv[0], "proxy-whoami" ) == 0 ) {
131 "%s: line %d: proxy-whoami takes no arguments\n",
135 load_extop( (struct berval *)&slap_EXOP_WHOAMI, ldap_back_exop_whoami );
138 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
140 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
141 #ifdef ENABLE_REWRITE
143 #endif /* ENABLE_REWRITE */
148 * suffixmassage <suffix> <massaged suffix>
150 * the <suffix> field must be defined as a valid suffix
151 * (or suffixAlias?) for the current database;
152 * the <massaged suffix> shouldn't have already been
153 * defined as a valid suffix or suffixAlias for the
157 fprintf( stderr, "%s: line %d: syntax is"
158 " \"suffixMassage <suffix>"
159 " <massaged suffix>\"\n",
164 ber_str2bv( argv[1], 0, 0, &bvnc );
165 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
166 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
167 fname, lineno, bvnc.bv_val );
170 tmp_be = select_backend( &nvnc, 0, 0 );
171 if ( tmp_be != NULL && tmp_be != be ) {
172 fprintf( stderr, "%s: line %d: suffix already in use"
173 " by another backend in"
174 " \"suffixMassage <suffix>"
175 " <massaged suffix>\"\n",
182 ber_str2bv( argv[2], 0, 0, &brnc );
183 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
184 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
185 fname, lineno, brnc.bv_val );
192 tmp_be = select_backend( &nrnc, 0, 0 );
193 if ( tmp_be != NULL ) {
194 fprintf( stderr, "%s: line %d: massaged suffix"
195 " already in use by another backend in"
196 " \"suffixMassage <suffix>"
197 " <massaged suffix>\"\n",
207 #ifdef ENABLE_REWRITE
209 * The suffix massaging is emulated by means of the
210 * rewrite capabilities
211 * FIXME: no extra rewrite capabilities should be added
214 rc = suffix_massage_config( li->rwmap.rwm_rw,
215 &pvnc, &nvnc, &prnc, &nrnc );
223 #else /* !ENABLE_REWRITE */
224 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &pvnc );
225 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &nvnc );
227 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &prnc );
228 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &nrnc );
229 #endif /* !ENABLE_REWRITE */
231 /* rewrite stuff ... */
232 } else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
233 #ifdef ENABLE_REWRITE
234 return rewrite_parse( li->rwmap.rwm_rw,
235 fname, lineno, argc, argv );
237 #else /* !ENABLE_REWRITE */
238 fprintf( stderr, "%s: line %d: rewrite capabilities "
239 "are not enabled\n", fname, lineno );
240 #endif /* !ENABLE_REWRITE */
242 /* objectclass/attribute mapping */
243 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
244 return ldap_back_map_config( &li->rwmap.rwm_oc,
246 fname, lineno, argc, argv );
250 fprintf( stderr, "%s: line %d: unknown directive \"%s\" "
251 "in ldap database definition (ignored)\n",
252 fname, lineno, argv[0] );
258 ldap_back_map_config(
259 struct ldapmap *oc_map,
260 struct ldapmap *at_map,
267 struct ldapmapping *mapping;
271 if ( argc < 3 || argc > 4 ) {
273 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
278 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
282 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
286 fprintf( stderr, "%s: line %d: syntax is "
287 "\"map {objectclass | attribute} [<local> | *] "
288 "{<foreign> | *}\"\n",
293 if ( strcmp( argv[2], "*" ) == 0 ) {
294 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
295 map->drop_missing = ( argc < 4 );
300 } else if ( argc < 4 ) {
306 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
309 if ( ( map == at_map )
310 && ( strcasecmp( src, "objectclass" ) == 0
311 || strcasecmp( dst, "objectclass" ) == 0 ) )
314 "%s: line %d: objectclass attribute cannot be mapped\n",
318 mapping = (struct ldapmapping *)ch_calloc( 2,
319 sizeof(struct ldapmapping) );
320 if ( mapping == NULL ) {
322 "%s: line %d: out of memory\n",
326 ber_str2bv( src, 0, 1, &mapping->src );
327 ber_str2bv( dst, 0, 1, &mapping->dst );
328 mapping[1].src = mapping->dst;
329 mapping[1].dst = mapping->src;
335 if ( src[0] != '\0' ) {
336 if ( oc_bvfind( &mapping->src ) == NULL ) {
338 "%s: line %d: warning, source objectClass '%s' "
339 "should be defined in schema\n",
340 fname, lineno, src );
343 * FIXME: this should become an err
348 if ( oc_bvfind( &mapping->dst ) == NULL ) {
350 "%s: line %d: warning, destination objectClass '%s' "
351 "is not defined in schema\n",
352 fname, lineno, dst );
356 const char *text = NULL;
357 AttributeDescription *ad = NULL;
359 if ( src[0] != '\0' ) {
360 rc = slap_bv2ad( &mapping->src, &ad, &text );
361 if ( rc != LDAP_SUCCESS ) {
363 "%s: line %d: warning, source attributeType '%s' "
364 "should be defined in schema\n",
365 fname, lineno, src );
368 * FIXME: this should become an err
375 rc = slap_bv2ad( &mapping->dst, &ad, &text );
376 if ( rc != LDAP_SUCCESS ) {
378 "%s: line %d: warning, destination attributeType '%s' "
379 "is not defined in schema\n",
380 fname, lineno, dst );
384 if ( (src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL)
385 || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
388 "%s: line %d: duplicate mapping found (ignored)\n",
390 /* FIXME: free stuff */
394 if ( src[0] != '\0' ) {
395 avl_insert( &map->map, (caddr_t)mapping,
396 mapping_cmp, mapping_dup );
398 avl_insert( &map->remap, (caddr_t)&mapping[1],
399 mapping_cmp, mapping_dup );
405 ch_free( mapping->src.bv_val );
406 ch_free( mapping->dst.bv_val );
414 ldap_back_exop_whoami(
418 struct berval *bv = NULL;
420 if ( op->oq_extended.rs_reqdata != NULL ) {
421 /* no request data should be provided */
422 rs->sr_text = "no request data expected";
423 return rs->sr_err = LDAP_PROTOCOL_ERROR;
426 rs->sr_err = backend_check_restrictions( op, rs,
427 (struct berval *)&slap_EXOP_WHOAMI );
428 if( rs->sr_err != LDAP_SUCCESS ) return rs->sr_err;
430 /* if auth'd by back-ldap and request is proxied, forward it */
431 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)) {
434 LDAPControl c, *ctrls[2] = {&c, NULL};
439 op2.o_ndn = op->o_conn->c_ndn;
440 lc = ldap_back_getconn(&op2, rs);
441 if (!lc || !ldap_back_dobind( lc, op, rs )) {
444 c.ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
445 c.ldctl_iscritical = 1;
446 c.ldctl_value.bv_val = ch_malloc(op->o_ndn.bv_len+4);
447 c.ldctl_value.bv_len = op->o_ndn.bv_len + 3;
448 strcpy(c.ldctl_value.bv_val, "dn:");
449 strcpy(c.ldctl_value.bv_val+3, op->o_ndn.bv_val);
451 rs->sr_err = ldap_whoami(lc->ld, ctrls, NULL, &msgid);
452 if (rs->sr_err == LDAP_SUCCESS) {
453 if (ldap_result(lc->ld, msgid, 1, NULL, &res) == -1) {
454 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER,
457 rs->sr_err = ldap_parse_whoami(lc->ld, res, &bv);
461 ch_free(c.ldctl_value.bv_val);
462 if (rs->sr_err != LDAP_SUCCESS) {
463 rs->sr_err = ldap_back_map_result(rs);
466 /* else just do the same as before */
467 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
468 if( op->o_dn.bv_len ) {
469 bv->bv_len = op->o_dn.bv_len + sizeof("dn:")-1;
470 bv->bv_val = ch_malloc( bv->bv_len + 1 );
471 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:")-1 );
472 AC_MEMCPY( &bv->bv_val[sizeof("dn:")-1], op->o_dn.bv_val,
474 bv->bv_val[bv->bv_len] = '\0';
486 #ifdef ENABLE_REWRITE
488 suffix_massage_regexize( const char *s )
495 ( r = strchr( p, ',' ) ) != NULL;
499 res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
501 ptr = lutil_strcopy( res, "(.*)" );
503 ( r = strchr( p, ',' ) ) != NULL;
505 ptr = lutil_strncopy( ptr, p, r - p + 1 );
506 ptr = lutil_strcopy( ptr, "[ ]?" );
508 if ( r[ 1 ] == ' ' ) {
512 lutil_strcopy( ptr, p );
518 suffix_massage_patternize( const char *s )
525 res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
531 strcpy( res + sizeof( "%1" ) - 1, s );
537 suffix_massage_config(
538 struct rewrite_info *info,
548 rargv[ 0 ] = "rewriteEngine";
551 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
553 rargv[ 0 ] = "rewriteContext";
554 rargv[ 1 ] = "default";
556 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
558 rargv[ 0 ] = "rewriteRule";
559 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
560 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
563 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
564 ch_free( rargv[ 1 ] );
565 ch_free( rargv[ 2 ] );
567 rargv[ 0 ] = "rewriteContext";
568 rargv[ 1 ] = "searchResult";
570 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
572 rargv[ 0 ] = "rewriteRule";
573 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
574 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
577 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
578 ch_free( rargv[ 1 ] );
579 ch_free( rargv[ 2 ] );
583 * FIXME: this is no longer required since now we map filters
584 * based on the parsed filter structure, so we can deal directly
585 * with attribute types and values. The rewriteContext
586 * "searchFilter" now refers to the value of attrbutes
591 * the filter should be rewritten as
594 * "(.*)member=([^)]+),o=Foo Bar,[ ]?c=US(.*)"
595 * "%1member=%2,dc=example,dc=com%3"
597 * where "o=Foo Bar, c=US" is the virtual naming context,
598 * and "dc=example, dc=com" is the real naming context
600 rargv[ 0 ] = "rewriteContext";
601 rargv[ 1 ] = "searchFilter";
603 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
605 #if 1 /* rewrite filters */
608 * Note: this is far more optimistic than desirable:
609 * for any AVA value ending with the virtual naming
610 * context the terminal part will be replaced by the
611 * real naming context; a better solution would be to
612 * walk the filter looking for DN-valued attributes,
613 * and only rewrite those that require rewriting
615 char vbuf_[BUFSIZ], *vbuf = vbuf_,
616 rbuf_[BUFSIZ], *rbuf = rbuf_;
619 len = snprintf( vbuf, sizeof( vbuf_ ),
620 "(.*)%s\\)(.*)", nvnc->bv_val );
623 * traditional behavior: snprintf returns -1
624 * if buffer is insufficient
628 } else if ( len >= (int)sizeof( vbuf_ ) ) {
630 * C99: snprintf returns the required size
632 vbuf = ch_malloc( len + 1 );
633 len = snprintf( vbuf, len,
634 "(.*)%s\\)(.*)", nvnc->bv_val );
638 len = snprintf( rbuf, sizeof( rbuf_ ), "%%1%s)%%2",
643 } else if ( len >= (int)sizeof( rbuf_ ) ) {
644 rbuf = ch_malloc( len + 1 );
645 len = snprintf( rbuf, sizeof( rbuf_ ), "%%1%s)%%2",
650 rargv[ 0 ] = "rewriteRule";
655 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
657 if ( vbuf != vbuf_ ) {
661 if ( rbuf != rbuf_ ) {
665 #endif /* rewrite filters */
668 #if 0 /* "matched" is not normalized */
669 rargv[ 0 ] = "rewriteContext";
670 rargv[ 1 ] = "matchedDn";
671 rargv[ 2 ] = "alias";
672 rargv[ 3 ] = "searchResult";
674 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
675 #else /* normalize "matched" */
677 rargv[ 0 ] = "rewriteContext";
678 rargv[ 1 ] = "matchedDN";
679 rargv[ 2 ] = "alias";
680 rargv[ 3 ] = "searchResult";
682 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
684 rargv[ 0 ] = "rewriteContext";
685 rargv[ 1 ] = "searchAttrDN";
686 rargv[ 2 ] = "alias";
687 rargv[ 3 ] = "searchResult";
689 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
692 rargv[ 0 ] = "rewriteRule";
693 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
694 rargv[ 2 ] = suffix_massage_patternize( nvnc->bv_val );
697 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
698 ch_free( rargv[ 1 ] );
699 ch_free( rargv[ 2 ] );
701 #endif /* normalize "matched" */
705 #endif /* ENABLE_REWRITE */