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