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