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