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