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