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