]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/config.c
notices and acknowledges
[openldap] / servers / slapd / back-meta / config.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2003 The OpenLDAP Foundation.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted only as authorized by the OpenLDAP
9  * Public License.
10  *
11  * A copy of this license is available in the file LICENSE in the
12  * top-level directory of the distribution or, alternatively, at
13  * <http://www.OpenLDAP.org/license.html>.
14  */
15 /* ACKNOWLEDGEMENTS:
16  * This work was initially developed by the Howard Chu for inclusion
17  * in OpenLDAP Software and subsequently enhanced by Pierangelo
18  * Masarati.
19  */
20 /* This is an altered version */
21 /*
22  * Copyright 2001, Pierangelo Masarati, All rights reserved. <ando@sys-net.it>
23  *
24  * This work has been developed to fulfill the requirements
25  * of SysNet s.n.c. <http:www.sys-net.it> and it has been donated
26  * to the OpenLDAP Foundation in the hope that it may be useful
27  * to the Open Source community, but WITHOUT ANY WARRANTY.
28  *
29  * Permission is granted to anyone to use this software for any purpose
30  * on any computer system, and to alter it and redistribute it, subject
31  * to the following restrictions:
32  *
33  * 1. The author and SysNet s.n.c. are not responsible for the consequences
34  *    of use of this software, no matter how awful, even if they arise from 
35  *    flaws in it.
36  *
37  * 2. The origin of this software must not be misrepresented, either by
38  *    explicit claim or by omission.  Since few users ever read sources,
39  *    credits should appear in the documentation.
40  *
41  * 3. Altered versions must be plainly marked as such, and must not be
42  *    misrepresented as being the original software.  Since few users
43  *    ever read sources, credits should appear in the documentation.
44  *    SysNet s.n.c. cannot be responsible for the consequences of the
45  *    alterations.
46  *
47  * 4. This notice may not be removed or altered.
48  *
49  *
50  * This software is based on the backend back-ldap, implemented
51  * by Howard Chu <hyc@highlandsun.com>, and modified by Mark Valence
52  * <kurash@sassafras.com>, Pierangelo Masarati <ando@sys-net.it> and other
53  * contributors. The contribution of the original software to the present
54  * implementation is acknowledged in this copyright statement.
55  *
56  * A special acknowledgement goes to Howard for the overall architecture
57  * (and for borrowing large pieces of code), and to Mark, who implemented
58  * from scratch the attribute/objectclass mapping.
59  *
60  * The original copyright statement follows.
61  *
62  * Copyright 1999, Howard Chu, All rights reserved. <hyc@highlandsun.com>
63  *
64  * Permission is granted to anyone to use this software for any purpose
65  * on any computer system, and to alter it and redistribute it, subject
66  * to the following restrictions:
67  *
68  * 1. The author is not responsible for the consequences of use of this
69  *    software, no matter how awful, even if they arise from flaws in it.
70  *
71  * 2. The origin of this software must not be misrepresented, either by
72  *    explicit claim or by omission.  Since few users ever read sources,
73  *    credits should appear in the documentation.
74  *
75  * 3. Altered versions must be plainly marked as such, and must not be
76  *    misrepresented as being the original software.  Since few users
77  *    ever read sources, credits should appear in the
78  *    documentation.
79  *
80  * 4. This notice may not be removed or altered.
81  *
82  */
83
84 #include "portable.h"
85
86 #include <stdio.h>
87
88 #include <ac/string.h>
89 #include <ac/socket.h>
90
91 #include "slap.h"
92 #include "../back-ldap/back-ldap.h"
93 #undef ldap_debug       /* silence a warning in ldap-int.h */
94 #include "../../../libraries/libldap/ldap-int.h"
95 #include "back-meta.h"
96
97 static struct metatarget *
98 new_target( void )
99 {
100         struct metatarget *lt;
101         struct ldapmapping *mapping;
102
103         lt = ch_calloc( sizeof( struct metatarget ), 1 );
104         if ( lt == NULL ) {
105                 return NULL;
106         }
107
108         lt->rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
109         if ( lt->rwmap.rwm_rw == NULL ) {
110                 free( lt );
111                 return NULL;
112         }
113
114         ldap_back_map_init( &lt->rwmap.rwm_at, &mapping );
115
116         return lt;
117 }
118
119 int
120 meta_back_db_config(
121                 BackendDB       *be,
122                 const char      *fname,
123                 int             lineno,
124                 int             argc,
125                 char            **argv
126 )
127 {
128         struct metainfo *li = ( struct metainfo * )be->be_private;
129
130         if ( li == NULL ) {
131                 fprintf( stderr, 
132         "%s: line %d: meta backend info is null!\n",
133                     fname, lineno );
134                 return 1;
135         }
136
137         /* URI of server to query */
138         if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
139                 int             i = li->ntargets;
140 #if 0
141                 int             j;
142 #endif /* uncomment if uri MUST be a branch of suffix */
143                 LDAPURLDesc     *ludp, *tmpludp;
144                 struct berval   dn;
145                 int             rc;
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_parselist_ext( &ludp, argv[ 1 ], "\t" ) != 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                 dn.bv_val = ludp->lud_dn;
201                 dn.bv_len = strlen( ludp->lud_dn );
202
203                 rc = dnPrettyNormal( NULL, &dn, &li->targets[ i ]->psuffix,
204                         &li->targets[ i ]->suffix, NULL );
205                 if( rc != LDAP_SUCCESS ) {
206                         fprintf( stderr, "%s: line %d: "
207                                         "target '%s' DN is invalid\n",
208                                         fname, lineno, argv[ 1 ] );
209                         return( 1 );
210                 }
211
212                 ludp->lud_dn[ 0 ] = '\0';
213
214                 for ( tmpludp = ludp->lud_next; tmpludp; tmpludp = tmpludp->lud_next ) {
215                         if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
216                                 fprintf( stderr, "%s: line %d: "
217                                                 "multiple URIs must have "
218                                                 "no DN part\n",
219                                         fname, lineno );
220                                 return( 1 );
221
222                         }
223                 }
224
225                 li->targets[ i ]->uri = ldap_url_list2urls( ludp );
226                 ldap_free_urllist( ludp );
227                 if ( li->targets[ i ]->uri == NULL) {
228                         fprintf( stderr, "%s: line %d: no memory?\n",
229                                         fname, lineno );
230                         return( 1 );
231                 }
232                 
233                 /*
234                  * uri MUST be a branch of suffix!
235                  */
236 #if 0 /* too strict a constraint */
237                 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) != be ) {
238                         fprintf( stderr,
239         "%s: line %d: <naming context> of URI does not refer to current backend"
240         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
241                                 fname, lineno );
242                         return 1;
243                 }
244 #else
245                 /*
246                  * uri MUST be a branch of a suffix!
247                  */
248                 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) == NULL ) {
249                         fprintf( stderr,
250         "%s: line %d: <naming context> of URI does not resolve to a backend"
251         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
252                                 fname, lineno );
253                         return 1;
254                 }
255 #endif
256
257 #if 0
258                 /*
259                  * uri MUST not be used by other URIs!
260                  *
261                  * FIXME: this limitation may be removed,
262                  * or worked out, at least, in some manner
263                  */
264                 for ( j = 0; j < i-1; j++ ) {
265                         if ( dn_match( &li->targets[ i ]->suffix,
266                                         &li->targets[ j ]->suffix ) ) {
267                                 fprintf( stderr,
268         "%s: line %d: naming context \"%s\" already used"
269         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
270                                         fname, lineno, last+1 );
271                                 return 1;
272                         }
273                 }
274 #endif
275
276 #if 0
277                 fprintf(stderr, "%s: line %d: URI \"%s\", suffix \"%s\"\n",
278                         fname, lineno, li->targets[ i ]->uri, 
279                         li->targets[ i ]->psuffix.bv_val );
280 #endif
281                 
282         /* default target directive */
283         } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
284                 int             i = li->ntargets-1;
285                 
286                 if ( argc == 1 ) {
287                         if ( i < 0 ) {
288                                 fprintf( stderr,
289         "%s: line %d: \"default-target\" alone need be"
290         " inside a \"uri\" directive\n",
291                                         fname, lineno );
292                                 return 1;
293                         }
294                         li->defaulttarget = i;
295                 } else {
296                         if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
297                                 if ( i >= 0 ) {
298                                         fprintf( stderr,
299         "%s: line %d: \"default-target none\""
300         " should go before uri definitions\n",
301                                                 fname, lineno );
302                                 }
303                                 li->defaulttarget = META_DEFAULT_TARGET_NONE;
304                         } else {
305                                 int n = atoi( argv[ 1 ] );
306                                 if ( n < 1 || n >= i ) {
307                                         fprintf( stderr,
308         "%s: line %d: illegal target number %d\n",
309                                                 fname, lineno, n );
310                                         return 1;
311                                 }
312                                 li->defaulttarget = n-1;
313                         }
314                 }
315                 
316         /* ttl of dn cache */
317         } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
318                 if ( argc != 2 ) {
319                         fprintf( stderr,
320         "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
321                                 fname, lineno );
322                         return 1;
323                 }
324                 
325                 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
326                         li->cache.ttl = META_DNCACHE_FOREVER;
327                 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
328                         li->cache.ttl = META_DNCACHE_DISABLED;
329                 } else {
330                         li->cache.ttl = atol( argv[ 1 ] );
331                 }
332
333         /* network timeout when connecting to ldap servers */
334         } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
335                 if ( argc != 2 ) {
336                         fprintf( stderr,
337         "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
338                                 fname, lineno );
339                         return 1;
340                 }
341                 li->network_timeout = atol(argv[ 1 ]);
342
343         /* name to use for meta_back_group */
344         } else if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
345                 int             i = li->ntargets-1;
346                 struct berval   dn;
347
348                 if ( i < 0 ) {
349                         fprintf( stderr,
350         "%s: line %d: need \"uri\" directive first\n",
351                                 fname, lineno );
352                         return 1;
353                 }
354                 
355                 if ( argc != 2 ) {
356                         fprintf( stderr,
357         "%s: line %d: missing name in \"binddn <name>\" line\n",
358                                 fname, lineno );
359                         return 1;
360                 }
361
362                 dn.bv_val = argv[ 1 ];
363                 dn.bv_len = strlen( argv[ 1 ] );
364                 if ( dnNormalize( 0, NULL, NULL, &dn, &li->targets[ i ]->binddn,
365                         NULL ) != LDAP_SUCCESS )
366                 {
367                         fprintf( stderr, "%s: line %d: "
368                                         "bind DN '%s' is invalid\n",
369                                         fname, lineno, argv[ 1 ] );
370                         return( 1 );
371                 }
372
373         /* password to use for meta_back_group */
374         } else if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
375                 int             i = li->ntargets-1;
376
377                 if ( i < 0 ) {
378                         fprintf( stderr,
379         "%s: line %d: need \"uri\" directive first\n",
380                                 fname, lineno );
381                         return 1;
382                 }
383                 
384                 if ( argc != 2 ) {
385                         fprintf( stderr,
386         "%s: line %d: missing password in \"bindpw <password>\" line\n",
387                             fname, lineno );
388                         return 1;
389                 }
390                 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->bindpw );
391                 
392         /* save bind creds for referral rebinds? */
393         } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
394                 if (argc != 1) {
395                         fprintf( stderr,
396         "%s: line %d: rebind-as-user takes no arguments\n",
397                             fname, lineno );
398                         return( 1 );
399                 }
400                 li->savecred = 1;
401         
402         /* name to use as pseudo-root dn */
403         } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
404                 int             i = li->ntargets-1;
405                 struct berval   dn;
406
407                 if ( i < 0 ) {
408                         fprintf( stderr,
409         "%s: line %d: need \"uri\" directive first\n",
410                                 fname, lineno );
411                         return 1;
412                 }
413                 
414                 if ( argc != 2 ) {
415                         fprintf( stderr,
416         "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
417                                 fname, lineno );
418                         return 1;
419                 }
420
421                 dn.bv_val = argv[ 1 ];
422                 dn.bv_len = strlen( argv[ 1 ] );
423                 if ( dnNormalize( 0, NULL, NULL, &dn,
424                         &li->targets[ i ]->pseudorootdn, NULL ) != LDAP_SUCCESS )
425                 {
426                         fprintf( stderr, "%s: line %d: "
427                                         "pseudoroot DN '%s' is invalid\n",
428                                         fname, lineno, argv[ 1 ] );
429                         return( 1 );
430                 }
431
432         /* password to use as pseudo-root */
433         } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
434                 int             i = li->ntargets-1;
435
436                 if ( i < 0 ) {
437                         fprintf( stderr,
438         "%s: line %d: need \"uri\" directive first\n",
439                                 fname, lineno );
440                         return 1;
441                 }
442                 
443                 if ( argc != 2 ) {
444                         fprintf( stderr,
445         "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
446                             fname, lineno );
447                         return 1;
448                 }
449                 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->pseudorootpw );
450         
451         /* dn massaging */
452         } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
453                 BackendDB       *tmp_be;
454                 int             i = li->ntargets-1;
455                 struct berval   dn, nvnc, pvnc, nrnc, prnc;
456
457                 if ( i < 0 ) {
458                         fprintf( stderr,
459         "%s: line %d: need \"uri\" directive first\n",
460                                 fname, lineno );
461                         return 1;
462                 }
463                 
464                 /*
465                  * syntax:
466                  * 
467                  *      suffixmassage <suffix> <massaged suffix>
468                  *
469                  * the <suffix> field must be defined as a valid suffix
470                  * (or suffixAlias?) for the current database;
471                  * the <massaged suffix> shouldn't have already been
472                  * defined as a valid suffix or suffixAlias for the 
473                  * current server
474                  */
475                 if ( argc != 3 ) {
476                         fprintf( stderr,
477         "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
478                                 fname, lineno );
479                         return 1;
480                 }
481
482                 dn.bv_val = argv[ 1 ];
483                 dn.bv_len = strlen( argv[ 1 ] );
484                 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
485                         fprintf( stderr, "%s: line %d: "
486                                         "suffix '%s' is invalid\n",
487                                         fname, lineno, argv[ 1 ] );
488                         return 1;
489                 }
490                 
491                 tmp_be = select_backend( &nvnc, 0, 0 );
492                 if ( tmp_be != NULL && tmp_be != be ) {
493                         fprintf( stderr, 
494         "%s: line %d: suffix already in use by another backend in"
495         " \"suffixMassage <suffix> <massaged suffix>\"\n",
496                                 fname, lineno );
497                         free( pvnc.bv_val );
498                         free( nvnc.bv_val );
499                         return 1;                                               
500                 }
501
502                 dn.bv_val = argv[ 2 ];
503                 dn.bv_len = strlen( argv[ 2 ] );
504                 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
505                         fprintf( stderr, "%s: line %d: "
506                                         "massaged suffix '%s' is invalid\n",
507                                         fname, lineno, argv[ 2 ] );
508                         free( pvnc.bv_val );
509                         free( nvnc.bv_val );
510                         return 1;
511                 }
512         
513 #if 0   
514                 tmp_be = select_backend( &nrnc, 0, 0 );
515                 if ( tmp_be != NULL ) {
516                         fprintf( stderr,
517         "%s: line %d: massaged suffix already in use by another backend in" 
518         " \"suffixMassage <suffix> <massaged suffix>\"\n",
519                                 fname, lineno );
520                         free( pvnc.bv_val );
521                         free( nvnc.bv_val );
522                         free( prnc.bv_val );
523                         free( nrnc.bv_val );
524                         return 1;
525                 }
526 #endif
527                 
528                 /*
529                  * The suffix massaging is emulated by means of the
530                  * rewrite capabilities
531                  * FIXME: no extra rewrite capabilities should be added
532                  * to the database
533                  */
534                 return suffix_massage_config( li->targets[ i ]->rwmap.rwm_rw,
535                                 &pvnc, &nvnc, &prnc, &nrnc );
536                 
537         /* rewrite stuff ... */
538         } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
539                 int             i = li->ntargets-1;
540
541                 if ( i < 0 ) {
542                         if ( strcasecmp( argv[0], "rewriteEngine" ) == 0 ) {
543                                 li->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
544                         }
545                         return rewrite_parse(li->rwinfo, fname, lineno,
546                                         argc, argv); 
547                 }
548                 
549                 return rewrite_parse( li->targets[ i ]->rwmap.rwm_rw, fname, lineno,
550                                 argc, argv );
551
552         /* objectclass/attribute mapping */
553         } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
554                 int             i = li->ntargets-1;
555
556                 if ( i < 0 ) {
557                         fprintf( stderr,
558         "%s: line %d: need \"uri\" directive first\n",
559                                 fname, lineno );
560                         return 1;
561                 }
562
563                 return ldap_back_map_config( &li->targets[ i ]->rwmap.rwm_oc, 
564                                 &li->targets[ i ]->rwmap.rwm_at,
565                                 fname, lineno, argc, argv );
566         /* anything else */
567         } else {
568                 if ( meta_back_cache_config( be, fname, lineno, argc, argv ) == 0 ) {
569                         return 0;
570                 }
571
572                 fprintf( stderr,
573         "%s: line %d: unknown directive \"%s\" in meta database definition"
574         " (ignored)\n",
575                     fname, lineno, argv[0] );
576         }
577         return 0;
578 }
579