]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/config.c
unifdef LDAP_CACHING
[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->rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
92         if ( lt->rwmap.rwm_rw == NULL ) {
93                 free( lt );
94                 return NULL;
95         }
96
97         ldap_back_map_init( &lt->rwmap.rwm_at, &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, NULL );
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 "
201                                                 "no DN part\n",
202                                         fname, lineno );
203                                 return( 1 );
204
205                         }
206                 }
207
208                 li->targets[ i ]->uri = ldap_url_list2urls( ludp );
209                 ldap_free_urllist( ludp );
210                 if ( li->targets[ i ]->uri == NULL) {
211                         fprintf( stderr, "%s: line %d: no memory?\n",
212                                         fname, lineno );
213                         return( 1 );
214                 }
215                 
216                 /*
217                  * uri MUST be a branch of suffix!
218                  */
219 #if 0 /* too strict a constraint */
220                 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) != be ) {
221                         fprintf( stderr,
222         "%s: line %d: <naming context> of URI does not refer to current backend"
223         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
224                                 fname, lineno );
225                         return 1;
226                 }
227 #else
228                 /*
229                  * uri MUST be a branch of a suffix!
230                  */
231                 if ( select_backend( &li->targets[ i ]->suffix, 0, 0 ) == NULL ) {
232                         fprintf( stderr,
233         "%s: line %d: <naming context> of URI does not resolve to a backend"
234         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
235                                 fname, lineno );
236                         return 1;
237                 }
238 #endif
239
240 #if 0
241                 /*
242                  * uri MUST not be used by other URIs!
243                  *
244                  * FIXME: this limitation may be removed,
245                  * or worked out, at least, in some manner
246                  */
247                 for ( j = 0; j < i-1; j++ ) {
248                         if ( dn_match( &li->targets[ i ]->suffix,
249                                         &li->targets[ j ]->suffix ) ) {
250                                 fprintf( stderr,
251         "%s: line %d: naming context \"%s\" already used"
252         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
253                                         fname, lineno, last+1 );
254                                 return 1;
255                         }
256                 }
257 #endif
258
259 #if 0
260                 fprintf(stderr, "%s: line %d: URI \"%s\", suffix \"%s\"\n",
261                         fname, lineno, li->targets[ i ]->uri, 
262                         li->targets[ i ]->psuffix.bv_val );
263 #endif
264                 
265         /* default target directive */
266         } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
267                 int             i = li->ntargets-1;
268                 
269                 if ( argc == 1 ) {
270                         if ( i < 0 ) {
271                                 fprintf( stderr,
272         "%s: line %d: \"default-target\" alone need be"
273         " inside a \"uri\" directive\n",
274                                         fname, lineno );
275                                 return 1;
276                         }
277                         li->defaulttarget = i;
278                 } else {
279                         if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
280                                 if ( i >= 0 ) {
281                                         fprintf( stderr,
282         "%s: line %d: \"default-target none\""
283         " should go before uri definitions\n",
284                                                 fname, lineno );
285                                 }
286                                 li->defaulttarget = META_DEFAULT_TARGET_NONE;
287                         } else {
288                                 int n = atoi( argv[ 1 ] );
289                                 if ( n < 1 || n >= i ) {
290                                         fprintf( stderr,
291         "%s: line %d: illegal target number %d\n",
292                                                 fname, lineno, n );
293                                         return 1;
294                                 }
295                                 li->defaulttarget = n-1;
296                         }
297                 }
298                 
299         /* ttl of dn cache */
300         } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
301                 if ( argc != 2 ) {
302                         fprintf( stderr,
303         "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
304                                 fname, lineno );
305                         return 1;
306                 }
307                 
308                 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
309                         li->cache.ttl = META_DNCACHE_FOREVER;
310                 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
311                         li->cache.ttl = META_DNCACHE_DISABLED;
312                 } else {
313                         li->cache.ttl = atol( argv[ 1 ] );
314                 }
315
316         /* network timeout when connecting to ldap servers */
317         } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
318                 if ( argc != 2 ) {
319                         fprintf( stderr,
320         "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
321                                 fname, lineno );
322                         return 1;
323                 }
324                 li->network_timeout = atol(argv[ 1 ]);
325
326         /* name to use for meta_back_group */
327         } else if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
328                 int             i = li->ntargets-1;
329                 struct berval   dn;
330
331                 if ( i < 0 ) {
332                         fprintf( stderr,
333         "%s: line %d: need \"uri\" directive first\n",
334                                 fname, lineno );
335                         return 1;
336                 }
337                 
338                 if ( argc != 2 ) {
339                         fprintf( stderr,
340         "%s: line %d: missing name in \"binddn <name>\" line\n",
341                                 fname, lineno );
342                         return 1;
343                 }
344
345                 dn.bv_val = argv[ 1 ];
346                 dn.bv_len = strlen( argv[ 1 ] );
347                 if ( dnNormalize( 0, NULL, NULL, &dn, &li->targets[ i ]->binddn,
348                         NULL ) != LDAP_SUCCESS )
349                 {
350                         fprintf( stderr, "%s: line %d: "
351                                         "bind DN '%s' is invalid\n",
352                                         fname, lineno, argv[ 1 ] );
353                         return( 1 );
354                 }
355
356         /* password to use for meta_back_group */
357         } else if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
358                 int             i = li->ntargets-1;
359
360                 if ( i < 0 ) {
361                         fprintf( stderr,
362         "%s: line %d: need \"uri\" directive first\n",
363                                 fname, lineno );
364                         return 1;
365                 }
366                 
367                 if ( argc != 2 ) {
368                         fprintf( stderr,
369         "%s: line %d: missing password in \"bindpw <password>\" line\n",
370                             fname, lineno );
371                         return 1;
372                 }
373                 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->bindpw );
374                 
375         /* save bind creds for referral rebinds? */
376         } else if ( strcasecmp( argv[0], "rebind-as-user" ) == 0 ) {
377                 if (argc != 1) {
378                         fprintf( stderr,
379         "%s: line %d: rebind-as-user takes no arguments\n",
380                             fname, lineno );
381                         return( 1 );
382                 }
383                 li->savecred = 1;
384         
385         /* name to use as pseudo-root dn */
386         } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
387                 int             i = li->ntargets-1;
388                 struct berval   dn;
389
390                 if ( i < 0 ) {
391                         fprintf( stderr,
392         "%s: line %d: need \"uri\" directive first\n",
393                                 fname, lineno );
394                         return 1;
395                 }
396                 
397                 if ( argc != 2 ) {
398                         fprintf( stderr,
399         "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
400                                 fname, lineno );
401                         return 1;
402                 }
403
404                 dn.bv_val = argv[ 1 ];
405                 dn.bv_len = strlen( argv[ 1 ] );
406                 if ( dnNormalize( 0, NULL, NULL, &dn,
407                         &li->targets[ i ]->pseudorootdn, NULL ) != LDAP_SUCCESS )
408                 {
409                         fprintf( stderr, "%s: line %d: "
410                                         "pseudoroot DN '%s' is invalid\n",
411                                         fname, lineno, argv[ 1 ] );
412                         return( 1 );
413                 }
414
415         /* password to use as pseudo-root */
416         } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
417                 int             i = li->ntargets-1;
418
419                 if ( i < 0 ) {
420                         fprintf( stderr,
421         "%s: line %d: need \"uri\" directive first\n",
422                                 fname, lineno );
423                         return 1;
424                 }
425                 
426                 if ( argc != 2 ) {
427                         fprintf( stderr,
428         "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
429                             fname, lineno );
430                         return 1;
431                 }
432                 ber_str2bv( argv[ 1 ], 0L, 1, &li->targets[ i ]->pseudorootpw );
433         
434         /* dn massaging */
435         } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
436                 BackendDB       *tmp_be;
437                 int             i = li->ntargets-1;
438                 struct berval   dn, nvnc, pvnc, nrnc, prnc;
439
440                 if ( i < 0 ) {
441                         fprintf( stderr,
442         "%s: line %d: need \"uri\" directive first\n",
443                                 fname, lineno );
444                         return 1;
445                 }
446                 
447                 /*
448                  * syntax:
449                  * 
450                  *      suffixmassage <suffix> <massaged suffix>
451                  *
452                  * the <suffix> field must be defined as a valid suffix
453                  * (or suffixAlias?) for the current database;
454                  * the <massaged suffix> shouldn't have already been
455                  * defined as a valid suffix or suffixAlias for the 
456                  * current server
457                  */
458                 if ( argc != 3 ) {
459                         fprintf( stderr,
460         "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
461                                 fname, lineno );
462                         return 1;
463                 }
464
465                 dn.bv_val = argv[ 1 ];
466                 dn.bv_len = strlen( argv[ 1 ] );
467                 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
468                         fprintf( stderr, "%s: line %d: "
469                                         "suffix '%s' is invalid\n",
470                                         fname, lineno, argv[ 1 ] );
471                         return 1;
472                 }
473                 
474                 tmp_be = select_backend( &nvnc, 0, 0 );
475                 if ( tmp_be != NULL && tmp_be != be ) {
476                         fprintf( stderr, 
477         "%s: line %d: suffix already in use by another backend in"
478         " \"suffixMassage <suffix> <massaged suffix>\"\n",
479                                 fname, lineno );
480                         free( pvnc.bv_val );
481                         free( nvnc.bv_val );
482                         return 1;                                               
483                 }
484
485                 dn.bv_val = argv[ 2 ];
486                 dn.bv_len = strlen( argv[ 2 ] );
487                 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
488                         fprintf( stderr, "%s: line %d: "
489                                         "massaged suffix '%s' is invalid\n",
490                                         fname, lineno, argv[ 2 ] );
491                         free( pvnc.bv_val );
492                         free( nvnc.bv_val );
493                         return 1;
494                 }
495         
496 #if 0   
497                 tmp_be = select_backend( &nrnc, 0, 0 );
498                 if ( tmp_be != NULL ) {
499                         fprintf( stderr,
500         "%s: line %d: massaged suffix already in use by another backend in" 
501         " \"suffixMassage <suffix> <massaged suffix>\"\n",
502                                 fname, lineno );
503                         free( pvnc.bv_val );
504                         free( nvnc.bv_val );
505                         free( prnc.bv_val );
506                         free( nrnc.bv_val );
507                         return 1;
508                 }
509 #endif
510                 
511                 /*
512                  * The suffix massaging is emulated by means of the
513                  * rewrite capabilities
514                  * FIXME: no extra rewrite capabilities should be added
515                  * to the database
516                  */
517                 return suffix_massage_config( li->targets[ i ]->rwmap.rwm_rw,
518                                 &pvnc, &nvnc, &prnc, &nrnc );
519                 
520         /* rewrite stuff ... */
521         } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
522                 int             i = li->ntargets-1;
523
524                 if ( i < 0 ) {
525                         if ( strcasecmp( argv[0], "rewriteEngine" ) == 0 ) {
526                                 li->rwinfo = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
527                         }
528                         return rewrite_parse(li->rwinfo, fname, lineno,
529                                         argc, argv); 
530                 }
531                 
532                 return rewrite_parse( li->targets[ i ]->rwmap.rwm_rw, fname, lineno,
533                                 argc, argv );
534
535         /* objectclass/attribute mapping */
536         } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
537                 int             i = li->ntargets-1;
538
539                 if ( i < 0 ) {
540                         fprintf( stderr,
541         "%s: line %d: need \"uri\" directive first\n",
542                                 fname, lineno );
543                         return 1;
544                 }
545
546                 return ldap_back_map_config( &li->targets[ i ]->rwmap.rwm_oc, 
547                                 &li->targets[ i ]->rwmap.rwm_at,
548                                 fname, lineno, argc, argv );
549         /* anything else */
550         } else {
551                 if ( meta_back_cache_config( be, fname, lineno, argc, argv ) == 0 ) {
552                         return 0;
553                 }
554
555                 fprintf( stderr,
556         "%s: line %d: unknown directive \"%s\" in meta database definition"
557         " (ignored)\n",
558                     fname, lineno, argv[0] );
559         }
560         return 0;
561 }
562