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 ) {
76 fprintf( stderr, "%s: line %d: "
78 "in \"uri <uri>\" line\n",
82 if ( li->url != NULL ) {
85 if ( li->lud != NULL ) {
86 ldap_free_urldesc( li->lud );
89 if ( ldap_url_parse( argv[ 1 ], &li->lud ) != LDAP_URL_SUCCESS ) {
90 fprintf( stderr, "%s: line %d: "
91 "unable to parse uri \"%s\" "
92 "in \"uri <uri>\" line\n",
93 fname, lineno, argv[ 1 ] );
97 if ( ( li->lud->lud_dn != NULL && li->lud->lud_dn[0] != '\0' )
98 || li->lud->lud_attrs != NULL
99 || li->lud->lud_filter != NULL
100 || li->lud->lud_exts != NULL )
102 fprintf( stderr, "%s: line %d: "
103 "warning, only protocol, "
104 "host and port allowed "
105 "in \"uri <uri>\" line\n",
112 tmplud.lud_attrs = NULL;
113 tmplud.lud_filter = NULL;
114 if ( !ldap_is_ldapi_url( argv[ 1 ] ) ) {
115 tmplud.lud_exts = NULL;
116 tmplud.lud_crit_exts = 0;
119 li->url = ldap_url_desc2str( &tmplud );
120 if ( li->url == NULL ) {
121 fprintf( stderr, "%s: line %d: "
122 "unable to rebuild uri \"%s\" "
123 "in \"uri <uri>\" line\n",
124 fname, lineno, argv[ 1 ] );
128 li->url = ch_strdup( argv[ 1 ] );
131 /* name to use for ldap_back_group */
132 } else if ( strcasecmp( argv[0], "binddn" ) == 0 ) {
135 "%s: line %d: missing name in \"binddn <name>\" line\n",
139 ber_str2bv( argv[1], 0, 1, &li->binddn );
141 /* password to use for ldap_back_group */
142 } else if ( strcasecmp( argv[0], "bindpw" ) == 0 ) {
145 "%s: line %d: missing password in \"bindpw <password>\" line\n",
149 ber_str2bv( argv[1], 0, 1, &li->bindpw );
151 #ifdef LDAP_BACK_PROXY_AUTHZ
152 /* name to use for proxyAuthz propagation */
153 } else if ( strcasecmp( argv[0], "proxyauthzdn" ) == 0 ) {
156 "%s: line %d: missing name in \"proxyauthzdn <name>\" line\n",
160 ber_str2bv( argv[1], 0, 1, &li->proxyauthzdn );
162 /* password to use for proxyAuthz propagation */
163 } else if ( strcasecmp( argv[0], "proxyauthzpw" ) == 0 ) {
166 "%s: line %d: missing password in \"proxyauthzpw <password>\" line\n",
170 ber_str2bv( argv[1], 0, 1, &li->proxyauthzpw );
171 #endif /* LDAP_BACK_PROXY_AUTHZ */
173 /* save bind creds for referral rebinds? */
174 } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
177 "%s: line %d: rebind-as-user takes no arguments\n",
183 /* intercept exop_who_am_i? */
184 } else if ( strcasecmp( argv[0], "proxy-whoami" ) == 0 ) {
187 "%s: line %d: proxy-whoami takes no arguments\n",
191 load_extop( (struct berval *)&slap_EXOP_WHOAMI,
192 0, ldap_back_exop_whoami );
195 } else if ( strcasecmp( argv[0], "suffixmassage" ) == 0 ) {
197 struct berval bvnc, nvnc, pvnc, brnc, nrnc, prnc;
198 #ifdef ENABLE_REWRITE
200 #endif /* ENABLE_REWRITE */
205 * suffixmassage <suffix> <massaged suffix>
207 * the <suffix> field must be defined as a valid suffix
208 * (or suffixAlias?) for the current database;
209 * the <massaged suffix> shouldn't have already been
210 * defined as a valid suffix or suffixAlias for the
214 fprintf( stderr, "%s: line %d: syntax is"
215 " \"suffixMassage <suffix>"
216 " <massaged suffix>\"\n",
221 ber_str2bv( argv[1], 0, 0, &bvnc );
222 if ( dnPrettyNormal( NULL, &bvnc, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
223 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
224 fname, lineno, bvnc.bv_val );
227 tmp_be = select_backend( &nvnc, 0, 0 );
228 if ( tmp_be != NULL && tmp_be != be ) {
229 fprintf( stderr, "%s: line %d: suffix already in use"
230 " by another backend in"
231 " \"suffixMassage <suffix>"
232 " <massaged suffix>\"\n",
239 ber_str2bv( argv[2], 0, 0, &brnc );
240 if ( dnPrettyNormal( NULL, &brnc, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
241 fprintf( stderr, "%s: line %d: suffix DN %s is invalid\n",
242 fname, lineno, brnc.bv_val );
249 tmp_be = select_backend( &nrnc, 0, 0 );
250 if ( tmp_be != NULL ) {
251 fprintf( stderr, "%s: line %d: massaged suffix"
252 " already in use by another backend in"
253 " \"suffixMassage <suffix>"
254 " <massaged suffix>\"\n",
264 #ifdef ENABLE_REWRITE
266 * The suffix massaging is emulated by means of the
267 * rewrite capabilities
268 * FIXME: no extra rewrite capabilities should be added
271 rc = suffix_massage_config( li->rwmap.rwm_rw,
272 &pvnc, &nvnc, &prnc, &nrnc );
280 #else /* !ENABLE_REWRITE */
281 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &pvnc );
282 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &nvnc );
284 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &prnc );
285 ber_bvarray_add( &li->rwmap.rwm_suffix_massage, &nrnc );
286 #endif /* !ENABLE_REWRITE */
288 /* rewrite stuff ... */
289 } else if ( strncasecmp( argv[0], "rewrite", 7 ) == 0 ) {
290 #ifdef ENABLE_REWRITE
291 return rewrite_parse( li->rwmap.rwm_rw,
292 fname, lineno, argc, argv );
294 #else /* !ENABLE_REWRITE */
295 fprintf( stderr, "%s: line %d: rewrite capabilities "
296 "are not enabled\n", fname, lineno );
297 #endif /* !ENABLE_REWRITE */
299 /* objectclass/attribute mapping */
300 } else if ( strcasecmp( argv[0], "map" ) == 0 ) {
301 return ldap_back_map_config( &li->rwmap.rwm_oc,
303 fname, lineno, argc, argv );
307 return SLAP_CONF_UNKNOWN;
313 ldap_back_map_config(
314 struct ldapmap *oc_map,
315 struct ldapmap *at_map,
322 struct ldapmapping *mapping;
326 if ( argc < 3 || argc > 4 ) {
328 "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
333 if ( strcasecmp( argv[1], "objectclass" ) == 0 ) {
337 } else if ( strcasecmp( argv[1], "attribute" ) == 0 ) {
341 fprintf( stderr, "%s: line %d: syntax is "
342 "\"map {objectclass | attribute} [<local> | *] "
343 "{<foreign> | *}\"\n",
348 if ( strcmp( argv[2], "*" ) == 0 ) {
349 if ( argc < 4 || strcmp( argv[3], "*" ) == 0 ) {
350 map->drop_missing = ( argc < 4 );
355 } else if ( argc < 4 ) {
361 dst = ( strcmp( argv[3], "*" ) == 0 ? src : argv[3] );
364 if ( ( map == at_map )
365 && ( strcasecmp( src, "objectclass" ) == 0
366 || strcasecmp( dst, "objectclass" ) == 0 ) )
369 "%s: line %d: objectclass attribute cannot be mapped\n",
373 mapping = (struct ldapmapping *)ch_calloc( 2,
374 sizeof(struct ldapmapping) );
375 if ( mapping == NULL ) {
377 "%s: line %d: out of memory\n",
381 ber_str2bv( src, 0, 1, &mapping->src );
382 ber_str2bv( dst, 0, 1, &mapping->dst );
383 mapping[1].src = mapping->dst;
384 mapping[1].dst = mapping->src;
390 if ( src[0] != '\0' ) {
391 if ( oc_bvfind( &mapping->src ) == NULL ) {
393 "%s: line %d: warning, source objectClass '%s' "
394 "should be defined in schema\n",
395 fname, lineno, src );
398 * FIXME: this should become an err
404 if ( oc_bvfind( &mapping->dst ) == NULL ) {
406 "%s: line %d: warning, destination objectClass '%s' "
407 "is not defined in schema\n",
408 fname, lineno, dst );
412 const char *text = NULL;
413 AttributeDescription *ad = NULL;
415 if ( src[0] != '\0' ) {
416 rc = slap_bv2ad( &mapping->src, &ad, &text );
417 if ( rc != LDAP_SUCCESS ) {
419 "%s: line %d: warning, source attributeType '%s' "
420 "should be defined in schema\n",
421 fname, lineno, src );
424 * FIXME: this should become an err
432 rc = slap_bv2ad( &mapping->dst, &ad, &text );
433 if ( rc != LDAP_SUCCESS ) {
435 "%s: line %d: warning, destination attributeType '%s' "
436 "is not defined in schema\n",
437 fname, lineno, dst );
441 if ( (src[0] != '\0' && avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL)
442 || avl_find( map->remap, (caddr_t)&mapping[1], mapping_cmp ) != NULL)
445 "%s: line %d: duplicate mapping found (ignored)\n",
450 if ( src[0] != '\0' ) {
451 avl_insert( &map->map, (caddr_t)mapping,
452 mapping_cmp, mapping_dup );
454 avl_insert( &map->remap, (caddr_t)&mapping[1],
455 mapping_cmp, mapping_dup );
461 ch_free( mapping->src.bv_val );
462 ch_free( mapping->dst.bv_val );
470 ldap_back_exop_whoami(
474 struct berval *bv = NULL;
476 if ( op->oq_extended.rs_reqdata != NULL ) {
477 /* no request data should be provided */
478 rs->sr_text = "no request data expected";
479 return rs->sr_err = LDAP_PROTOCOL_ERROR;
482 rs->sr_err = backend_check_restrictions( op, rs,
483 (struct berval *)&slap_EXOP_WHOAMI );
484 if( rs->sr_err != LDAP_SUCCESS ) return rs->sr_err;
486 /* if auth'd by back-ldap and request is proxied, forward it */
487 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)) {
490 LDAPControl c, *ctrls[2] = {NULL, NULL};
496 op2.o_ndn = op->o_conn->c_ndn;
497 lc = ldap_back_getconn(&op2, rs);
498 if (!lc || !ldap_back_dobind( lc, op, rs )) {
501 c.ldctl_oid = LDAP_CONTROL_PROXY_AUTHZ;
502 c.ldctl_iscritical = 1;
503 c.ldctl_value.bv_val = ch_malloc(op->o_ndn.bv_len+4);
504 c.ldctl_value.bv_len = op->o_ndn.bv_len + 3;
505 strcpy(c.ldctl_value.bv_val, "dn:");
506 strcpy(c.ldctl_value.bv_val+3, op->o_ndn.bv_val);
508 rs->sr_err = ldap_whoami(lc->ld, ctrls, NULL, &msgid);
509 if (rs->sr_err == LDAP_SUCCESS) {
510 if (ldap_result(lc->ld, msgid, 1, NULL, &res) == -1) {
511 ldap_get_option(lc->ld, LDAP_OPT_ERROR_NUMBER,
514 rs->sr_err = ldap_parse_whoami(lc->ld, res, &bv);
518 ch_free(c.ldctl_value.bv_val);
519 if (rs->sr_err != LDAP_SUCCESS) {
520 rs->sr_err = slap_map_api2result( rs );
523 /* else just do the same as before */
524 bv = (struct berval *) ch_malloc( sizeof(struct berval) );
525 if( op->o_dn.bv_len ) {
526 bv->bv_len = op->o_dn.bv_len + sizeof("dn:") - 1;
527 bv->bv_val = ch_malloc( bv->bv_len + 1 );
528 AC_MEMCPY( bv->bv_val, "dn:", sizeof("dn:") - 1 );
529 AC_MEMCPY( &bv->bv_val[sizeof("dn:") - 1], op->o_dn.bv_val,
531 bv->bv_val[bv->bv_len] = '\0';
543 #ifdef ENABLE_REWRITE
545 suffix_massage_regexize( const char *s )
552 ( r = strchr( p, ',' ) ) != NULL;
556 res = ch_calloc( sizeof( char ), strlen( s ) + 4 + 4*i + 1 );
558 ptr = lutil_strcopy( res, "(.*)" );
560 ( r = strchr( p, ',' ) ) != NULL;
562 ptr = lutil_strncopy( ptr, p, r - p + 1 );
563 ptr = lutil_strcopy( ptr, "[ ]?" );
565 if ( r[ 1 ] == ' ' ) {
569 lutil_strcopy( ptr, p );
575 suffix_massage_patternize( const char *s )
582 res = ch_calloc( sizeof( char ), len + sizeof( "%1" ) );
588 strcpy( res + sizeof( "%1" ) - 1, s );
594 suffix_massage_config(
595 struct rewrite_info *info,
605 rargv[ 0 ] = "rewriteEngine";
608 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
610 rargv[ 0 ] = "rewriteContext";
611 rargv[ 1 ] = "default";
613 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
615 rargv[ 0 ] = "rewriteRule";
616 rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
617 rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val );
620 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
621 ch_free( rargv[ 1 ] );
622 ch_free( rargv[ 2 ] );
624 rargv[ 0 ] = "rewriteContext";
625 rargv[ 1 ] = "searchResult";
627 rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
629 rargv[ 0 ] = "rewriteRule";
630 rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
631 rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val );
634 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
635 ch_free( rargv[ 1 ] );
636 ch_free( rargv[ 2 ] );
638 rargv[ 0 ] = "rewriteContext";
639 rargv[ 1 ] = "matchedDN";
640 rargv[ 2 ] = "alias";
641 rargv[ 3 ] = "searchResult";
643 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
645 rargv[ 0 ] = "rewriteContext";
646 rargv[ 1 ] = "searchAttrDN";
647 rargv[ 2 ] = "alias";
648 rargv[ 3 ] = "searchResult";
650 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
654 #endif /* ENABLE_REWRITE */