]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/config.c
Date: Thu, 2 May 2002 08:54:59 GMT
[openldap] / servers / slapd / back-meta / config.c
1 /*
2  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
3  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
4  *
5  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
6  *
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.
11  *
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:
15  *
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 
18  *    flaws in it.
19  *
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.
23  *
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
28  *    alterations.
29  *
30  * 4. This notice may not be removed or altered.
31  *
32  *
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.
38  *
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.
42  *
43  * The original copyright statement follows.
44  *
45  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
46  *
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:
50  *
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.
53  *
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.
57  *
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
61  *    documentation.
62  *
63  * 4. This notice may not be removed or altered.
64  *
65  */
66
67 #include "portable.h"
68
69 #include <stdio.h>
70
71 #include <ac/string.h>
72 #include <ac/socket.h>
73
74 #include "slap.h"
75 #include "../back-ldap/back-ldap.h"
76 #include "back-meta.h"
77
78 static struct metatarget *
79 new_target( void )
80 {
81         struct metatarget *lt;
82         struct ldapmapping *mapping;
83
84         lt = ch_calloc( sizeof( struct metatarget ), 1 );
85         if ( lt == NULL ) {
86                 return NULL;
87         }
88
89         lt->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
90         if ( lt->rwinfo == NULL ) {
91                 free( lt );
92                 return NULL;
93         }
94
95         ldap_back_map_init( &lt->at_map, &mapping );
96
97         return lt;
98 }
99
100 int
101 meta_back_db_config(
102                 BackendDB       *be,
103                 const char      *fname,
104                 int             lineno,
105                 int             argc,
106                 char            **argv
107 )
108 {
109         struct metainfo *li = ( struct metainfo * )be->be_private;
110
111         if ( li == NULL ) {
112                 fprintf( stderr, 
113         "%s: line %d: meta backend info is null!\n",
114                     fname, lineno );
115                 return 1;
116         }
117
118         /* URI of server to query */
119         if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
120                 int             i = li->ntargets;
121 #if 0
122                 int             j;
123 #endif /* uncomment if uri MUST be a branch of suffix */
124                 LDAPURLDesc     *ludp;
125                 char            *last;
126                 struct berval   dn;
127                 int             rc;
128                 
129                 if ( argc != 2 ) {
130                         fprintf( stderr,
131         "%s: line %d: missing address"
132         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
133                                 fname, lineno );
134                         return 1;
135                 }
136                 
137                 ++li->ntargets;
138
139                 li->targets = ch_realloc( li->targets, 
140                         sizeof( struct metatarget *)*li->ntargets );
141                 if ( li->targets == NULL ) {
142                         fprintf( stderr,
143         "%s: line %d: out of memory while storing server name"
144         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
145                                 fname, lineno );
146                         return 1;
147                 }
148
149                 if ( ( li->targets[ i ] = new_target() ) == NULL ) {
150                         fprintf( stderr,
151         "%s: line %d: unable to init server"
152         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
153                                 fname, lineno );
154                         return 1;
155                 }
156
157                 /*
158                  * uri MUST be legal!
159                  */
160                 if ( ldap_url_parse( argv[ 1 ], &ludp ) != LDAP_SUCCESS ) {
161                         fprintf( stderr,
162         "%s: line %d: unable to parse URI"
163         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
164                                 fname, lineno );
165                         return 1;
166                 }
167
168                 /*
169                  * uri MUST have the <dn> part!
170                  */
171                 if ( ludp->lud_dn == NULL || ludp->lud_dn[ 0 ] == '\0' ) {
172                         fprintf( stderr,
173         "%s: line %d: missing <naming context> "
174         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
175                                 fname, lineno );
176                         return 1;
177                 }
178
179                 /*
180                  * copies and stores uri and suffix
181                  */
182                 dn.bv_val = ludp->lud_dn;
183                 dn.bv_len = strlen( ludp->lud_dn );
184
185                 rc = dnPrettyNormal( NULL, &dn, &li->targets[ i ]->psuffix,
186                         &li->targets[ i ]->suffix );
187                 if( rc != LDAP_SUCCESS ) {
188                         fprintf( stderr, "%s: line %d: "
189                                         "target '%s' DN is invalid\n",
190                                         fname, lineno, argv[ 1 ] );
191                         return( 1 );
192                 }
193
194                 li->targets[ i ]->uri = ch_strdup( argv[ 1 ] );
195                 last = strstr( li->targets[ i ]->uri, ludp->lud_dn );
196                 assert( last != NULL );
197                 last[ 0 ] = '\0';
198                 
199                 /*
200                  * uri MUST be a branch of suffix!
201                  */
202 #if 0 /* too strict a constraint */
203                 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) != be ) {
204                         fprintf( stderr,
205         "%s: line %d: <naming context> of URI does not refer to current backend"
206         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
207                                 fname, lineno );
208                         return 1;
209                 }
210 #else
211                 /*
212                  * uri MUST be a branch of a suffix!
213                  */
214                 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) == NULL ) {
215                         fprintf( stderr,
216         "%s: line %d: <naming context> of URI does not resolve to a backend"
217         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
218                                 fname, lineno );
219                         return 1;
220                 }
221 #endif
222
223 #if 0
224                 /*
225                  * uri MUST not be used by other URIs!
226                  *
227                  * FIXME: this limitation may be removed,
228                  * or worked out, at least, in some manner
229                  */
230                 for ( j = 0; j < i-1; j++ ) {
231                         if ( dn_match( &li->targets[ i ]->suffix,
232                                         &li->targets[ j ]->suffix ) ) {
233                                 fprintf( stderr,
234         "%s: line %d: naming context \"%s\" already used"
235         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
236                                         fname, lineno, last+1 );
237                                 return 1;
238                         }
239                 }
240 #endif
241                 
242                 ldap_free_urldesc( ludp );
243
244 #if 0
245                 fprintf(stderr, "%s: line %d: URI \"%s\", suffix \"%s\"\n",
246                         fname, lineno, li->targets[ i ]->uri, 
247                         li->targets[ i ]->psuffix.bv_val );
248 #endif
249                 
250         /* default target directive */
251         } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
252                 int             i = li->ntargets-1;
253                 
254                 if ( argc == 1 ) {
255                         if ( i < 0 ) {
256                                 fprintf( stderr,
257         "%s: line %d: \"default-target\" alone need be"
258         " inside a \"uri\" directive\n",
259                                         fname, lineno );
260                                 return 1;
261                         }
262                         li->defaulttarget = i;
263                 } else {
264                         if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
265                                 if ( i >= 0 ) {
266                                         fprintf( stderr,
267         "%s: line %d: \"default-target none\""
268         " should go before uri definitions\n",
269                                                 fname, lineno );
270                                 }
271                                 li->defaulttarget = META_DEFAULT_TARGET_NONE;
272                         } else {
273                                 int n = atoi( argv[ 1 ] );
274                                 if ( n < 1 || n >= i ) {
275                                         fprintf( stderr,
276         "%s: line %d: illegal target number %d\n",
277                                                 fname, lineno, n );
278                                         return 1;
279                                 }
280                                 li->defaulttarget = n-1;
281                         }
282                 }
283                 
284         /* ttl of dn cache */
285         } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
286                 if ( argc != 2 ) {
287                         fprintf( stderr,
288         "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
289                                 fname, lineno );
290                         return 1;
291                 }
292                 
293                 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
294                         li->cache.ttl = META_DNCACHE_FOREVER;
295                 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
296                         li->cache.ttl = META_DNCACHE_DISABLED;
297                 } else {
298                         li->cache.ttl = atol( argv[ 1 ] );
299                 }
300
301         /* name to use for meta_back_group */
302         } else if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
303                 int             i = li->ntargets-1;
304                 struct berval   dn;
305
306                 if ( i < 0 ) {
307                         fprintf( stderr,
308         "%s: line %d: need \"uri\" directive first\n",
309                                 fname, lineno );
310                 }
311                 
312                 if ( argc != 2 ) {
313                         fprintf( stderr,
314         "%s: line %d: missing name in \"binddn <name>\" line\n",
315                                 fname, lineno );
316                         return 1;
317                 }
318
319                 dn.bv_val = argv[ 1 ];
320                 dn.bv_len = strlen( argv[ 1 ] );
321                 if ( dnNormalize2( NULL, &dn, &li->targets[ i ]->binddn ) != LDAP_SUCCESS ) {
322                         fprintf( stderr, "%s: line %d: "
323                                         "bind DN '%s' is invalid\n",
324                                         fname, lineno, argv[ 1 ] );
325                         return( 1 );
326                 }
327
328         /* password to use for meta_back_group */
329         } else if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
330                 int             i = li->ntargets-1;
331
332                 if ( i < 0 ) {
333                         fprintf( stderr,
334         "%s: line %d: need \"uri\" directive first\n",
335                                 fname, lineno );
336                 }
337                 
338                 if ( argc != 2 ) {
339                         fprintf( stderr,
340         "%s: line %d: missing password in \"bindpw <password>\" line\n",
341                             fname, lineno );
342                         return 1;
343                 }
344                 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->bindpw );
345                 
346         /* name to use as pseudo-root dn */
347         } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
348                 int             i = li->ntargets-1;
349                 struct berval   dn;
350
351                 if ( i < 0 ) {
352                         fprintf( stderr,
353         "%s: line %d: need \"uri\" directive first\n",
354                                 fname, lineno );
355                 }
356                 
357                 if ( argc != 2 ) {
358                         fprintf( stderr,
359         "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
360                                 fname, lineno );
361                         return 1;
362                 }
363
364                 dn.bv_val = argv[ 1 ];
365                 dn.bv_len = strlen( argv[ 1 ] );
366                 if ( dnNormalize2( NULL, &dn, &li->targets[ i ]->pseudorootdn ) != LDAP_SUCCESS ) {
367                         fprintf( stderr, "%s: line %d: "
368                                         "pseudoroot DN '%s' is invalid\n",
369                                         fname, lineno, argv[ 1 ] );
370                         return( 1 );
371                 }
372
373         /* password to use as pseudo-root */
374         } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
375                 int             i = li->ntargets-1;
376
377                 if ( i < 0 ) {
378                         fprintf( stderr,
379         "%s: line %d: need \"uri\" directive first\n",
380                                 fname, lineno );
381                 }
382                 
383                 if ( argc != 2 ) {
384                         fprintf( stderr,
385         "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
386                             fname, lineno );
387                         return 1;
388                 }
389                 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->pseudorootpw );
390         
391         /* dn massaging */
392         } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
393                 BackendDB       *tmp_be;
394                 int             i = li->ntargets-1;
395                 struct berval   dn, nvnc, pvnc, nrnc, prnc;
396
397                 if ( i < 0 ) {
398                         fprintf( stderr,
399         "%s: line %d: need \"uri\" directive first\n",
400                                 fname, lineno );
401                         return 1;
402                 }
403                 
404                 /*
405                  * syntax:
406                  * 
407                  *      suffixmassage <suffix> <massaged suffix>
408                  *
409                  * the <suffix> field must be defined as a valid suffix
410                  * (or suffixAlias?) for the current database;
411                  * the <massaged suffix> shouldn't have already been
412                  * defined as a valid suffix or suffixAlias for the 
413                  * current server
414                  */
415                 if ( argc != 3 ) {
416                         fprintf( stderr,
417         "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
418                                 fname, lineno );
419                         return 1;
420                 }
421
422                 dn.bv_val = argv[ 1 ];
423                 dn.bv_len = strlen( argv[ 1 ] );
424                 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc ) != LDAP_SUCCESS ) {
425                         fprintf( stderr, "%s: line %d: "
426                                         "suffix '%s' is invalid\n",
427                                         fname, lineno, argv[ 1 ] );
428                         return 1;
429                 }
430                 
431                 tmp_be = select_backend( &nvnc, 0, 0 );
432                 if ( tmp_be != NULL && tmp_be != be ) {
433                         fprintf( stderr, 
434         "%s: line %d: suffix already in use by another backend in"
435         " \"suffixMassage <suffix> <massaged suffix>\"\n",
436                                 fname, lineno );
437                         free( pvnc.bv_val );
438                         free( nvnc.bv_val );
439                         return 1;                                               
440                 }
441
442                 dn.bv_val = argv[ 2 ];
443                 dn.bv_len = strlen( argv[ 2 ] );
444                 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc ) != LDAP_SUCCESS ) {
445                         fprintf( stderr, "%s: line %d: "
446                                         "massaged suffix '%s' is invalid\n",
447                                         fname, lineno, argv[ 2 ] );
448                         free( pvnc.bv_val );
449                         free( nvnc.bv_val );
450                         return 1;
451                 }
452         
453 #if 0   
454                 tmp_be = select_backend( &nrnc, 0, 0 );
455                 if ( tmp_be != NULL ) {
456                         fprintf( stderr,
457         "%s: line %d: massaged suffix already in use by another backend in" 
458         " \"suffixMassage <suffix> <massaged suffix>\"\n",
459                                 fname, lineno );
460                         free( pvnc.bv_val );
461                         free( nvnc.bv_val );
462                         free( prnc.bv_val );
463                         free( nrnc.bv_val );
464                         return 1;
465                 }
466 #endif
467                 
468                 /*
469                  * The suffix massaging is emulated by means of the
470                  * rewrite capabilities
471                  * FIXME: no extra rewrite capabilities should be added
472                  * to the database
473                  */
474                 return suffix_massage_config( li->targets[ i ]->rwinfo,
475                                 &pvnc, &nvnc, &prnc, &nrnc );
476                 
477         /* rewrite stuff ... */
478         } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
479                 int             i = li->ntargets-1;
480
481                 if ( i < 0 ) {
482                         fprintf( stderr,
483         "%s: line %d: need \"uri\" directive first\n",
484                                 fname, lineno );
485                 }
486                 
487                 return rewrite_parse( li->targets[ i ]->rwinfo, fname, lineno,
488                                 argc, argv );
489
490         /* objectclass/attribute mapping */
491         } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
492                 struct ldapmap *map;
493                 struct ldapmapping *mapping;
494                 char *src, *dst;
495                 int             i = li->ntargets-1;
496
497                 if ( i < 0 ) {
498                         fprintf( stderr,
499         "%s: line %d: need \"uri\" directive first\n",
500                                 fname, lineno );
501                 }
502                 
503
504                 if ( argc < 3 || argc > 4 ) {
505                         fprintf( stderr,
506         "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
507                                 fname, lineno );
508                         return 1;
509                 }
510
511                 if ( strcasecmp( argv[ 1 ], "objectClass" ) == 0 ) {
512                         map = &li->targets[ i ]->oc_map;
513                 } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
514                         map = &li->targets[ i ]->at_map;
515                 } else {
516                         fprintf( stderr,
517         "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
518                                 fname, lineno );
519                         return 1;
520                 }
521
522                 if ( strcmp( argv[ 2 ], "*" ) == 0 ) {
523                         if ( argc < 4 || strcmp( argv[ 3 ], "*" ) == 0 ) {
524                                 map->drop_missing = ( argc < 4 );
525                                 return 0;
526                         }
527                         src = dst = argv[ 3 ];
528                 } else if ( argc < 4 ) {
529                         src = "";
530                         dst = argv[ 2 ];
531                 } else {
532                         src = argv[ 2 ];
533                         dst = ( strcmp( argv[ 3 ], "*" ) == 0 ? src : argv[ 3 ] );
534                 }
535
536                 if ( ( map == &li->targets[ i ]->at_map )
537                         && ( strcasecmp( src, "objectclass" ) == 0
538                                 || strcasecmp( dst, "objectclass" ) == 0 ) ) {
539                         fprintf( stderr,
540         "%s: line %d: objectclass attribute cannot be mapped\n",
541                                 fname, lineno );
542                 }
543
544                 mapping = ch_calloc( 2, sizeof( struct ldapmapping ) );
545                 if ( mapping == NULL ) {
546                         fprintf( stderr,
547                                 "%s: line %d: out of memory\n",
548                                 fname, lineno );
549                         return 1;
550                 }
551                 ber_str2bv( src, 0, 1, &mapping->src );
552                 ber_str2bv( dst, 0, 1, &mapping->dst );
553                 mapping[ 1 ].src = mapping->dst;
554                 mapping[ 1 ].dst = mapping->src;
555
556                 if ( (*src != '\0' &&
557                           avl_find( map->map, ( caddr_t )mapping,
558                                 mapping_cmp ) != NULL)
559                         || avl_find( map->remap, ( caddr_t )&mapping[ 1 ],
560                                 mapping_cmp ) != NULL) {
561                         fprintf( stderr,
562         "%s: line %d: duplicate mapping found (ignored)\n",
563                                 fname, lineno );
564                         return 0;
565                 }
566
567                 if ( *src != '\0' )
568                         avl_insert( &map->map, ( caddr_t )mapping,
569                                                 mapping_cmp, mapping_dup );
570                 avl_insert( &map->remap, ( caddr_t )&mapping[ 1 ],
571                                         mapping_cmp, mapping_dup );
572
573         /* anything else */
574         } else {
575                 fprintf( stderr,
576         "%s: line %d: unknown directive \"%s\" in meta database definition"
577         " (ignored)\n",
578                     fname, lineno, argv[0] );
579         }
580         return 0;
581 }
582