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