2 * Copyright 1998-2001 The OpenLDAP Foundation, All Rights Reserved.
3 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
5 * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
7 * This work has been developed to fulfill the requirements
8 * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
9 * to the OpenLDAP Foundation in the hope that it may be useful
10 * to the Open Source community, but WITHOUT ANY WARRANTY.
12 * Permission is granted to anyone to use this software for any purpose
13 * on any computer system, and to alter it and redistribute it, subject
14 * to the following restrictions:
16 * 1. The author and SysNet s.n.c. are not responsible for the consequences
17 * of use of this software, no matter how awful, even if they arise from
20 * 2. The origin of this software must not be misrepresented, either by
21 * explicit claim or by omission. Since few users ever read sources,
22 * credits should appear in the documentation.
24 * 3. Altered versions must be plainly marked as such, and must not be
25 * misrepresented as being the original software. Since few users
26 * ever read sources, credits should appear in the documentation.
27 * SysNet s.n.c. cannot be responsible for the consequences of the
30 * 4. This notice may not be removed or altered.
33 * This software is based on the backend back-ldap, implemented
34 * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
35 * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
36 * contributors. The contribution of the original software to the present
37 * implementation is acknowledged in this copyright statement.
39 * A special acknowledgement goes to Howard for the overall architecture
40 * (and for borrowing large pieces of code), and to Mark, who implemented
41 * from scratch the attribute/objectclass mapping.
43 * The original copyright statement follows.
45 * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
47 * Permission is granted to anyone to use this software for any purpose
48 * on any computer system, and to alter it and redistribute it, subject
49 * to the following restrictions:
51 * 1. The author is not responsible for the consequences of use of this
52 * software, no matter how awful, even if they arise from flaws in it.
54 * 2. The origin of this software must not be misrepresented, either by
55 * explicit claim or by omission. Since few users ever read sources,
56 * credits should appear in the documentation.
58 * 3. Altered versions must be plainly marked as such, and must not be
59 * misrepresented as being the original software. Since few users
60 * ever read sources, credits should appear in the
63 * 4. This notice may not be removed or altered.
71 #include <ac/string.h>
72 #include <ac/socket.h>
75 #include "../back-ldap/back-ldap.h"
76 #include "back-meta.h"
79 suffix_massage_config(
80 struct rewrite_info *info,
85 static struct metatarget *
88 struct metatarget *lt;
89 struct ldapmapping *mapping;
91 lt = ch_calloc( sizeof( struct metatarget ), 1 );
96 lt->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
97 if ( lt->rwinfo == NULL ) {
102 mapping = ch_calloc( 2, sizeof( struct ldapmapping ) );
103 if ( mapping == NULL ) {
107 mapping->src = ch_strdup( "objectClass" );
108 mapping->dst = ch_strdup( "objectClass" );
109 mapping[ 1 ].src = mapping->src;
110 mapping[ 1 ].dst = mapping->dst;
112 avl_insert( <->at_map.map, ( caddr_t )mapping,
113 mapping_cmp, mapping_dup );
114 avl_insert( <->at_map.remap, ( caddr_t )&mapping[ 1 ],
115 mapping_cmp, mapping_dup );
129 struct metainfo *li = ( struct metainfo * )be->be_private;
133 "%s: line %d: meta backend info is null!\n",
138 /* URI of server to query */
139 if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
140 int i = li->ntargets;
143 #endif /* uncomment if uri MUST be a branch of suffix */
146 struct berval dn, *pdn = NULL, *ndn = NULL;
151 "%s: line %d: missing address"
152 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
159 li->targets = ch_realloc( li->targets,
160 sizeof( struct metatarget *)*li->ntargets );
161 if ( li->targets == NULL ) {
163 "%s: line %d: out of memory while storing server name"
164 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
169 if ( ( li->targets[ i ] = new_target() ) == NULL ) {
171 "%s: line %d: unable to init server"
172 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
180 if ( ldap_url_parse( argv[ 1 ], &ludp ) != LDAP_SUCCESS ) {
182 "%s: line %d: unable to parse URI"
183 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
189 * uri MUST have the <dn> part!
191 if ( ludp->lud_dn == NULL || ludp->lud_dn[ 0 ] == '\0' ) {
193 "%s: line %d: missing <naming context> "
194 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
200 * copies and stores uri and suffix
202 dn.bv_val = ludp->lud_dn;
203 dn.bv_len = strlen( ludp->lud_dn );
205 rc = dnPretty( NULL, &dn, &pdn );
206 if( rc != LDAP_SUCCESS ) {
207 fprintf( stderr, "%s: line %d: "
208 "target '%s' DN is invalid\n",
209 fname, lineno, argv[ 1 ] );
213 rc = dnNormalize( NULL, &dn, &ndn );
214 if( rc != LDAP_SUCCESS ) {
215 fprintf( stderr, "%s: line %d: "
216 "target '%s' DN is invalid\n",
217 fname, lineno, argv[ 1 ] );
222 li->targets[ i ]->psuffix = pdn;
223 li->targets[ i ]->suffix = ndn;
225 li->targets[ i ]->uri = ch_strdup( argv[ 1 ] );
226 last = strstr( li->targets[ i ]->uri, ludp->lud_dn );
227 assert( last != NULL );
231 * uri MUST be a branch of suffix!
233 #if 0 /* too strict a constraint */
234 if ( select_backend( li->targets[ i ]->suffix, 0, 0 ) != be ) {
236 "%s: line %d: <naming context> of URI does not refer to current backend"
237 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
243 * uri MUST be a branch of a suffix!
245 if ( select_backend( li->targets[ i ]->suffix, 0, 0 ) == NULL ) {
247 "%s: line %d: <naming context> of URI does not resolve to a backend"
248 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
256 * uri MUST not be used by other URIs!
258 * FIXME: this limitation may be removed,
259 * or worked out, at least, in some manner
261 for ( j = 0; j < i-1; j++ ) {
262 if ( strcmp( li->targets[ i ]->suffix->bv_val,
263 li->targets[ j ]->suffix->bv_val ) == 0 ) {
265 "%s: line %d: naming context \"%s\" already used"
266 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
267 fname, lineno, last+1 );
273 ldap_free_urldesc( ludp );
275 fprintf(stderr, "%s: line %d: URI \"%s\", suffix \"%s\"\n",
276 fname, lineno, li->targets[ i ]->uri,
277 li->targets[ i ]->psuffix->bv_val );
279 /* default target directive */
280 } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
281 int i = li->ntargets-1;
286 "%s: line %d: \"default-target\" alone need be"
287 " inside a \"uri\" directive\n",
291 li->defaulttarget = i;
293 if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
296 "%s: line %d: \"default-target none\""
297 " should go before uri definitions\n",
300 li->defaulttarget = META_DEFAULT_TARGET_NONE;
302 int n = atoi( argv[ 1 ] );
303 if ( n < 1 || n >= i ) {
305 "%s: line %d: illegal target number %d\n",
309 li->defaulttarget = n-1;
313 /* ttl of dn cache */
314 } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
317 "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
322 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
323 li->cache.ttl = META_DNCACHE_FOREVER;
324 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
325 li->cache.ttl = META_DNCACHE_DISABLED;
327 li->cache.ttl = atol( argv[ 1 ] );
330 /* name to use for meta_back_group */
331 } else if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
332 int i = li->ntargets-1;
333 struct berval dn, *ndn = NULL;
337 "%s: line %d: need \"uri\" directive first\n",
343 "%s: line %d: missing name in \"binddn <name>\" line\n",
348 dn.bv_val = argv[ 1 ];
349 dn.bv_len = strlen( argv[ 1 ] );
350 if ( dnNormalize( NULL, &dn, &ndn ) != LDAP_SUCCESS ) {
351 fprintf( stderr, "%s: line %d: "
352 "bind DN '%s' is invalid\n",
353 fname, lineno, argv[ 1 ] );
357 li->targets[ i ]->binddn = ndn;
359 /* password to use for meta_back_group */
360 } else if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
361 int i = li->ntargets-1;
365 "%s: line %d: need \"uri\" directive first\n",
371 "%s: line %d: missing password in \"bindpw <password>\" line\n",
375 li->targets[ i ]->bindpw = ber_bvstrdup( argv[ 1 ] );
377 /* name to use as pseudo-root dn */
378 } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
379 int i = li->ntargets-1;
380 struct berval dn, *ndn = NULL;
384 "%s: line %d: need \"uri\" directive first\n",
390 "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
395 dn.bv_val = argv[ 1 ];
396 dn.bv_len = strlen( argv[ 1 ] );
397 if ( dnNormalize( NULL, &dn, &ndn ) != LDAP_SUCCESS ) {
398 fprintf( stderr, "%s: line %d: "
399 "pseudoroot DN '%s' is invalid\n",
400 fname, lineno, argv[ 1 ] );
404 li->targets[ i ]->pseudorootdn = ndn;
406 /* password to use as pseudo-root */
407 } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
408 int i = li->ntargets-1;
412 "%s: line %d: need \"uri\" directive first\n",
418 "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
422 li->targets[ i ]->pseudorootpw = ber_bvstrdup( argv[ 1 ] );
425 } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
427 int i = li->ntargets-1;
428 struct berval dn, *ndn = NULL;
432 "%s: line %d: need \"uri\" directive first\n",
440 * suffixmassage <suffix> <massaged suffix>
442 * the <suffix> field must be defined as a valid suffix
443 * (or suffixAlias?) for the current database;
444 * the <massaged suffix> shouldn't have already been
445 * defined as a valid suffix or suffixAlias for the
450 "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
455 dn.bv_val = argv[ 1 ];
456 dn.bv_len = strlen( argv[ 1 ] );
457 if ( dnNormalize( NULL, &dn, &ndn ) != LDAP_SUCCESS ) {
458 fprintf( stderr, "%s: line %d: "
459 "suffix '%s' is invalid\n",
460 fname, lineno, argv[ 1 ] );
464 tmp_be = select_backend( ndn, 0, 0 );
466 if ( tmp_be != NULL && tmp_be != be ) {
468 "%s: line %d: suffix already in use by another backend in"
469 " \"suffixMassage <suffix> <massaged suffix>\"\n",
474 dn.bv_val = argv[ 2 ];
475 dn.bv_len = strlen( argv[ 2 ] );
477 if ( dnNormalize( NULL, &dn, &ndn ) != LDAP_SUCCESS ) {
478 fprintf( stderr, "%s: line %d: "
479 "massaged suffix '%s' is invalid\n",
480 fname, lineno, argv[ 2 ] );
484 tmp_be = select_backend( ndn, 0, 0 );
486 if ( tmp_be != NULL ) {
488 "%s: line %d: massaged suffix already in use by another backend in"
489 " \"suffixMassage <suffix> <massaged suffix>\"\n",
495 * The suffix massaging is emulated by means of the
496 * rewrite capabilities
497 * FIXME: no extra rewrite capabilities should be added
500 return suffix_massage_config( li->targets[ i ]->rwinfo,
503 /* rewrite stuff ... */
504 } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
505 int i = li->ntargets-1;
509 "%s: line %d: need \"uri\" directive first\n",
513 return rewrite_parse( li->targets[ i ]->rwinfo, fname, lineno,
516 /* objectclass/attribute mapping */
517 } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
519 struct ldapmapping *mapping;
521 int i = li->ntargets-1;
525 "%s: line %d: need \"uri\" directive first\n",
530 if ( argc < 3 || argc > 4 ) {
532 "%s: line %d: syntax is \"map {objectclass | attribute} {<source> | *} [<dest> | *]\"\n",
537 if ( strcasecmp( argv[ 1 ], "objectClass" ) == 0 ) {
538 map = &li->targets[ i ]->oc_map;
539 } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
540 map = &li->targets[ i ]->at_map;
543 "%s: line %d: syntax is \"map {objectclass | attribute} {<source> | *} [<dest> | *]\"\n",
548 if ( strcasecmp( argv[ 2 ], "*" ) != 0 ) {
552 } else if ( strcasecmp( argv[ 3 ], "*" ) == 0 ) {
559 map->drop_missing = 1;
562 if ( strcasecmp( argv[ 3 ], "*" ) == 0 ) {
563 map->drop_missing = 0;
571 if ( ( map == &li->targets[ i ]->at_map )
572 && ( strcasecmp( src, "objectclass" ) == 0
573 || strcasecmp( dst, "objectclass" ) == 0 ) ) {
575 "%s: line %d: objectclass attribute cannot be mapped\n",
579 mapping = ch_calloc( 2, sizeof( struct ldapmapping ) );
580 if ( mapping == NULL ) {
582 "%s: line %d: out of memory\n",
586 mapping->src = ch_strdup( src );
587 mapping->dst = ch_strdup( dst );
589 mapping[ 1 ].src = mapping->dst;
590 mapping[ 1 ].dst = mapping->src;
592 mapping[ 1 ].src = mapping->src;
593 mapping[ 1 ].dst = mapping->dst;
596 if ( avl_find( map->map, ( caddr_t )mapping,
597 mapping_cmp ) != NULL
598 || avl_find( map->remap, ( caddr_t )&mapping[ 1 ],
599 mapping_cmp ) != NULL) {
601 "%s: line %d: duplicate mapping found (ignored)\n",
606 avl_insert( &map->map, ( caddr_t )mapping,
607 mapping_cmp, mapping_dup );
608 avl_insert( &map->remap, ( caddr_t )&mapping[ 1 ],
609 mapping_cmp, mapping_dup );
614 "%s: line %d: unknown directive \"%s\" in meta database definition"
616 fname, lineno, argv[0] );