]> git.sur5r.net Git - openldap/blob - servers/slapd/back-meta/config.c
better fix: in case of error during bind, just bail out (very conservative, though)
[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-2005 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 "lutil.h"
32 #include "../back-ldap/back-ldap.h"
33 #undef ldap_debug       /* silence a warning in ldap-int.h */
34 #include "../../../libraries/libldap/ldap-int.h"
35 #include "back-meta.h"
36
37 static int
38 new_target( 
39         metatarget_t    *mt )
40 {
41         struct ldapmapping      *mapping;
42         char                    *rargv[ 3 ];
43
44         memset( mt, 0, sizeof( metatarget_t ) );
45
46         mt->mt_rwmap.rwm_rw = rewrite_info_init( REWRITE_MODE_USE_DEFAULT );
47         if ( mt->mt_rwmap.rwm_rw == NULL ) {
48                 return -1;
49         }
50
51
52         /*
53          * the filter rewrite as a string must be disabled
54          * by default; it can be re-enabled by adding rules;
55          * this creates an empty rewriteContext
56          */
57         rargv[ 0 ] = "rewriteContext";
58         rargv[ 1 ] = "searchFilter";
59         rargv[ 2 ] = NULL;
60         rewrite_parse( mt->mt_rwmap.rwm_rw, "<suffix massage>", 1, 2, rargv );
61
62         rargv[ 0 ] = "rewriteContext";
63         rargv[ 1 ] = "default";
64         rargv[ 2 ] = NULL;
65         rewrite_parse( mt->mt_rwmap.rwm_rw, "<suffix massage>", 1, 2, rargv );
66
67         ldap_back_map_init( &mt->mt_rwmap.rwm_at, &mapping );
68
69         return 0;
70 }
71
72 static int
73 check_true_false( char *str )
74 {
75         if ( strcasecmp( str, "true" ) == 0 || strcasecmp( str, "yes" ) == 0 ) {
76                 return 1;
77         }
78
79         if ( strcasecmp( str, "false" ) == 0 || strcasecmp( str, "no" ) == 0 ) {
80                 return 0;
81         }
82
83         return -1;
84 }
85
86
87 int
88 meta_back_db_config(
89                 BackendDB       *be,
90                 const char      *fname,
91                 int             lineno,
92                 int             argc,
93                 char            **argv
94 )
95 {
96         metainfo_t      *mi = ( metainfo_t * )be->be_private;
97
98         if ( mi == NULL ) {
99                 fprintf( stderr, 
100         "%s: line %d: meta backend info is null!\n",
101                     fname, lineno );
102                 return 1;
103         }
104
105         /* URI of server to query */
106         if ( strcasecmp( argv[ 0 ], "uri" ) == 0 ) {
107                 int             i = mi->mi_ntargets;
108 #if 0
109                 int             j;
110 #endif /* uncomment if uri MUST be a branch of suffix */
111                 LDAPURLDesc     *ludp, *tmpludp;
112                 struct berval   dn;
113                 int             rc;
114                 int             c;
115                 
116                 switch ( argc ) {
117                 case 1:
118                         fprintf( stderr,
119         "%s: line %d: missing URI "
120         "in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
121                                 fname, lineno );
122                         return 1;
123
124                 case 2:
125                         break;
126
127                 default:
128                         fprintf( stderr,
129         "%s: line %d: too many args "
130         "in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
131                                 fname, lineno );
132                         return 1;
133                 }
134
135                 if ( be->be_nsuffix == NULL ) {
136                         fprintf( stderr,
137         "%s: line %d: the suffix must be defined before any target.\n",
138                                 fname, lineno );
139                         return 1;
140                 }
141                 
142                 ++mi->mi_ntargets;
143
144                 mi->mi_targets = ( metatarget_t * )ch_realloc( mi->mi_targets, 
145                         sizeof( metatarget_t ) * mi->mi_ntargets );
146                 if ( mi->mi_targets == NULL ) {
147                         fprintf( stderr,
148         "%s: line %d: out of memory while storing server name"
149         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
150                                 fname, lineno );
151                         return 1;
152                 }
153
154                 if ( new_target( &mi->mi_targets[ i ] ) != 0 ) {
155                         fprintf( stderr,
156         "%s: line %d: unable to init server"
157         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
158                                 fname, lineno );
159                         return 1;
160                 }
161
162                 mi->mi_targets[ i ].mt_nretries = mi->mi_nretries;
163                 mi->mi_targets[ i ].mt_flags = mi->flags;
164                 mi->mi_targets[ i ].mt_version = mi->mi_version;
165
166                 for ( c = 0; c < META_OP_LAST; c++ ) {
167                         mi->mi_targets[ i ].mt_timeout[ c ] = mi->mi_timeout[ c ];
168                 }
169
170                 /*
171                  * uri MUST be legal!
172                  */
173                 if ( ldap_url_parselist_ext( &ludp, argv[ 1 ], "\t" ) != LDAP_SUCCESS ) {
174                         fprintf( stderr,
175         "%s: line %d: unable to parse URI"
176         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
177                                 fname, lineno );
178                         return 1;
179                 }
180
181                 /*
182                  * uri MUST have the <dn> part!
183                  */
184                 if ( ludp->lud_dn == NULL ) {
185                         fprintf( stderr,
186         "%s: line %d: missing <naming context> "
187         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
188                                 fname, lineno );
189                         return 1;
190
191                 } else if ( ludp->lud_dn[ 0 ] == '\0' ) {
192                         int     j = -1;
193
194                         for ( j = 0; !BER_BVISNULL( &be->be_nsuffix[ j ] ); j++ ) {
195                                 if ( BER_BVISEMPTY( &be->be_nsuffix[ j ] ) ) {
196                                         break;
197                                 }
198                         }
199
200                         if ( BER_BVISNULL( &be->be_nsuffix[ j ] ) ) {
201                                 fprintf( stderr,
202                 "%s: line %d: missing <naming context> "
203                 " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
204                                         fname, lineno );
205                                 return 1;
206                         }
207                 }
208
209                 /*
210                  * copies and stores uri and suffix
211                  */
212                 ber_str2bv( ludp->lud_dn, 0, 0, &dn );
213                 rc = dnPrettyNormal( NULL, &dn, &mi->mi_targets[ i ].mt_psuffix,
214                         &mi->mi_targets[ i ].mt_nsuffix, NULL );
215                 if( rc != LDAP_SUCCESS ) {
216                         fprintf( stderr, "%s: line %d: "
217                                         "target '%s' DN is invalid\n",
218                                         fname, lineno, argv[ 1 ] );
219                         return( 1 );
220                 }
221
222                 ludp->lud_dn[ 0 ] = '\0';
223
224                 switch ( ludp->lud_scope ) {
225                 case LDAP_SCOPE_DEFAULT:
226                         mi->mi_targets[ i ].mt_scope = LDAP_SCOPE_SUBTREE;
227                         break;
228
229                 case LDAP_SCOPE_SUBTREE:
230 #ifdef LDAP_SCOPE_SUBORDINATE
231                 case LDAP_SCOPE_SUBORDINATE:
232 #endif /* LDAP_SCOPE_SUBORDINATE */
233                         mi->mi_targets[ i ].mt_scope = ludp->lud_scope;
234                         break;
235
236                 default:
237                         fprintf( stderr, "%s: line %d: "
238                                         "invalid scope for target '%s'\n",
239                                         fname, lineno, argv[ 1 ] );
240                         return( 1 );
241                 }
242
243                 /* check all, to apply the scope check on the first one */
244                 for ( tmpludp = ludp; tmpludp; tmpludp = tmpludp->lud_next ) {
245                         if ( tmpludp->lud_dn != NULL && tmpludp->lud_dn[ 0 ] != '\0' ) {
246                                 fprintf( stderr, "%s: line %d: "
247                                                 "multiple URIs must have "
248                                                 "no DN part\n",
249                                         fname, lineno );
250                                 return( 1 );
251
252                         }
253                 }
254
255                 mi->mi_targets[ i ].mt_uri = ldap_url_list2urls( ludp );
256                 ldap_free_urllist( ludp );
257                 if ( mi->mi_targets[ i ].mt_uri == NULL) {
258                         fprintf( stderr, "%s: line %d: no memory?\n",
259                                         fname, lineno );
260                         return( 1 );
261                 }
262                 
263                 /*
264                  * uri MUST be a branch of suffix!
265                  */
266 #if 0 /* too strict a constraint */
267                 if ( select_backend( &mi->mi_targets[ i ].suffix, 0, 0 ) != be ) {
268                         fprintf( stderr,
269         "%s: line %d: <naming context> of URI does not refer to current backend"
270         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
271                                 fname, lineno );
272                         return 1;
273                 }
274 #else
275                 /*
276                  * uri MUST be a branch of a suffix!
277                  */
278                 if ( select_backend( &mi->mi_targets[ i ].mt_nsuffix, 0, 0 ) == NULL ) {
279                         fprintf( stderr,
280         "%s: line %d: <naming context> of URI does not resolve to a backend"
281         " in \"uri <protocol>://<server>[:port]/<naming context>\" line\n",
282                                 fname, lineno );
283                         return 1;
284                 }
285 #endif
286
287         /* default target directive */
288         } else if ( strcasecmp( argv[ 0 ], "default-target" ) == 0 ) {
289                 int             i = mi->mi_ntargets - 1;
290                 
291                 if ( argc == 1 ) {
292                         if ( i < 0 ) {
293                                 fprintf( stderr,
294         "%s: line %d: \"default-target\" alone need be"
295         " inside a \"uri\" directive\n",
296                                         fname, lineno );
297                                 return 1;
298                         }
299                         mi->mi_defaulttarget = i;
300                 } else {
301                         if ( strcasecmp( argv[ 1 ], "none" ) == 0 ) {
302                                 if ( i >= 0 ) {
303                                         fprintf( stderr,
304         "%s: line %d: \"default-target none\""
305         " should go before uri definitions\n",
306                                                 fname, lineno );
307                                 }
308                                 mi->mi_defaulttarget = META_DEFAULT_TARGET_NONE;
309
310                         } else {
311                                 char    *next;
312                                 int     n = strtol( argv[ 1 ], &next, 10 );
313                                 if ( n < 0 || n >= i - 1 ) {
314                                         fprintf( stderr,
315         "%s: line %d: illegal target number %d\n",
316                                                 fname, lineno, n );
317                                         return 1;
318                                 }
319                                 mi->mi_defaulttarget = n;
320                         }
321                 }
322                 
323         /* ttl of dn cache */
324         } else if ( strcasecmp( argv[ 0 ], "dncache-ttl" ) == 0 ) {
325                 if ( argc != 2 ) {
326                         fprintf( stderr,
327         "%s: line %d: missing ttl in \"dncache-ttl <ttl>\" line\n",
328                                 fname, lineno );
329                         return 1;
330                 }
331                 
332                 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
333                         mi->mi_cache.ttl = META_DNCACHE_FOREVER;
334                 } else if ( strcasecmp( argv[ 1 ], "disabled" ) == 0 ) {
335                         mi->mi_cache.ttl = META_DNCACHE_DISABLED;
336                 } else {
337                         mi->mi_cache.ttl = atol( argv[ 1 ] );
338                 }
339
340         /* network timeout when connecting to ldap servers */
341         } else if ( strcasecmp( argv[ 0 ], "network-timeout" ) == 0 ) {
342                 if ( argc != 2 ) {
343                         fprintf( stderr,
344         "%s: line %d: missing network timeout in \"network-timeout <seconds>\" line\n",
345                                 fname, lineno );
346                         return 1;
347                 }
348                 mi->mi_network_timeout = atol(argv[ 1 ]);
349
350         /* name to use for meta_back_group */
351         } else if ( strcasecmp( argv[ 0 ], "acl-authcDN" ) == 0
352                         || strcasecmp( argv[ 0 ], "binddn" ) == 0 )
353         {
354                 int             i = mi->mi_ntargets - 1;
355                 struct berval   dn;
356
357                 if ( i < 0 ) {
358                         fprintf( stderr,
359         "%s: line %d: need \"uri\" directive first\n",
360                                 fname, lineno );
361                         return 1;
362                 }
363                 
364                 if ( argc != 2 ) {
365                         fprintf( stderr,
366         "%s: line %d: missing name in \"binddn <name>\" line\n",
367                                 fname, lineno );
368                         return 1;
369                 }
370
371                 if ( strcasecmp( argv[ 0 ], "binddn" ) == 0 ) {
372                         fprintf( stderr, "%s: line %d: "
373                                 "\"binddn\" statement is deprecated; "
374                                 "use \"acl-authcDN\" instead\n",
375                                 fname, lineno );
376                         /* FIXME: some day we'll need to throw an error */
377                 }
378
379                 dn.bv_val = argv[ 1 ];
380                 dn.bv_len = strlen( argv[ 1 ] );
381                 if ( dnNormalize( 0, NULL, NULL, &dn, &mi->mi_targets[ i ].mt_binddn,
382                         NULL ) != LDAP_SUCCESS )
383                 {
384                         fprintf( stderr, "%s: line %d: "
385                                         "bind DN '%s' is invalid\n",
386                                         fname, lineno, argv[ 1 ] );
387                         return( 1 );
388                 }
389
390         /* password to use for meta_back_group */
391         } else if ( strcasecmp( argv[ 0 ], "acl-passwd" ) == 0
392                         || strcasecmp( argv[ 0 ], "bindpw" ) == 0 )
393         {
394                 int             i = mi->mi_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 \"bindpw <password>\" line\n",
406                             fname, lineno );
407                         return 1;
408                 }
409
410                 if ( strcasecmp( argv[ 0 ], "bindpw" ) == 0 ) {
411                         fprintf( stderr, "%s: line %d: "
412                                 "\"bindpw\" statement is deprecated; "
413                                 "use \"acl-passwd\" instead\n",
414                                 fname, lineno );
415                         /* FIXME: some day we'll need to throw an error */
416                 }
417
418                 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_bindpw );
419                 
420         /* save bind creds for referral rebinds? */
421         } else if ( strcasecmp( argv[ 0 ], "rebind-as-user" ) == 0 ) {
422                 if ( argc > 2 ) {
423                         fprintf( stderr,
424         "%s: line %d: \"rebind-as-user {NO|yes}\" takes 1 argument.\n",
425                             fname, lineno );
426                         return( 1 );
427                 }
428
429                 if ( argc == 1 ) {
430                         fprintf( stderr,
431         "%s: line %d: deprecated use of \"rebind-as-user {FALSE|true}\" with no arguments.\n",
432                             fname, lineno );
433                         mi->flags |= LDAP_BACK_F_SAVECRED;
434
435                 } else {
436                         switch ( check_true_false( argv[ 1 ] ) ) {
437                         case 0:
438                                 mi->flags &= ~LDAP_BACK_F_SAVECRED;
439                                 break;
440
441                         case 1:
442                                 mi->flags |= LDAP_BACK_F_SAVECRED;
443                                 break;
444
445                         default:
446                                 fprintf( stderr,
447         "%s: line %d: \"rebind-as-user {FALSE|true}\" unknown argument \"%s\".\n",
448                                     fname, lineno, argv[ 1 ] );
449                                 return 1;
450                         }
451                 }
452
453         } else if ( strcasecmp( argv[ 0 ], "chase-referrals" ) == 0 ) {
454                 unsigned        *flagsp = mi->mi_ntargets ?
455                                 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
456                                 : &mi->flags;
457
458                 if ( argc != 2 ) {
459                         fprintf( stderr,
460         "%s: line %d: \"chase-referrals {TRUE|false}\" needs 1 argument.\n",
461                                         fname, lineno );
462                         return( 1 );
463                 }
464
465                 /* this is the default; we add it because the default might change... */
466                 switch ( check_true_false( argv[ 1 ] ) ) {
467                 case 1:
468                         *flagsp |= LDAP_BACK_F_CHASE_REFERRALS;
469                         break;
470
471                 case 0:
472                         *flagsp &= ~LDAP_BACK_F_CHASE_REFERRALS;
473                         break;
474
475                 default:
476                         fprintf( stderr,
477                 "%s: line %d: \"chase-referrals {TRUE|false}\": unknown argument \"%s\".\n",
478                                         fname, lineno, argv[ 1 ] );
479                         return( 1 );
480                 }
481         
482         } else if ( strcasecmp( argv[ 0 ], "tls" ) == 0 ) {
483                 unsigned        *flagsp = mi->mi_ntargets ?
484                                 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
485                                 : &mi->flags;
486
487                 if ( argc != 2 ) {
488                         fprintf( stderr,
489                 "%s: line %d: \"tls <what>\" needs 1 argument.\n",
490                                         fname, lineno );
491                         return( 1 );
492                 }
493
494                 /* start */
495                 if ( strcasecmp( argv[ 1 ], "start" ) == 0 ) {
496                         *flagsp |= ( LDAP_BACK_F_USE_TLS | LDAP_BACK_F_TLS_CRITICAL );
497         
498                 /* try start tls */
499                 } else if ( strcasecmp( argv[ 1 ], "try-start" ) == 0 ) {
500                         *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
501                         *flagsp |= LDAP_BACK_F_USE_TLS;
502         
503                 /* propagate start tls */
504                 } else if ( strcasecmp( argv[ 1 ], "propagate" ) == 0 ) {
505                         *flagsp |= ( LDAP_BACK_F_PROPAGATE_TLS | LDAP_BACK_F_TLS_CRITICAL );
506                 
507                 /* try start tls */
508                 } else if ( strcasecmp( argv[ 1 ], "try-propagate" ) == 0 ) {
509                         *flagsp &= ~LDAP_BACK_F_TLS_CRITICAL;
510                         *flagsp |= LDAP_BACK_F_PROPAGATE_TLS;
511
512                 } else {
513                         fprintf( stderr,
514                 "%s: line %d: \"tls <what>\": unknown argument \"%s\".\n",
515                                         fname, lineno, argv[ 1 ] );
516                         return( 1 );
517                 }
518
519         } else if ( strcasecmp( argv[ 0 ], "t-f-support" ) == 0 ) {
520                 unsigned        *flagsp = mi->mi_ntargets ?
521                                 &mi->mi_targets[ mi->mi_ntargets - 1 ].mt_flags
522                                 : &mi->flags;
523
524                 if ( argc != 2 ) {
525                         fprintf( stderr,
526                 "%s: line %d: \"t-f-support {FALSE|true|discover}\" needs 1 argument.\n",
527                                         fname, lineno );
528                         return( 1 );
529                 }
530
531                 switch ( check_true_false( argv[ 1 ] ) ) {
532                 case 0:
533                         *flagsp &= ~(LDAP_BACK_F_SUPPORT_T_F|LDAP_BACK_F_SUPPORT_T_F_DISCOVER);
534                         break;
535
536                 case 1:
537                         *flagsp |= LDAP_BACK_F_SUPPORT_T_F;
538                         break;
539
540                 default:
541                         if ( strcasecmp( argv[ 1 ], "discover" ) == 0 ) {
542                                 *flagsp |= LDAP_BACK_F_SUPPORT_T_F_DISCOVER;
543
544                         } else {
545                                 fprintf( stderr,
546         "%s: line %d: unknown value \"%s\" for \"t-f-support {no|yes|discover}\".\n",
547                                         fname, lineno, argv[ 1 ] );
548                                 return 1;
549                         }
550                         break;
551                 }
552
553         /* onerr? */
554         } else if ( strcasecmp( argv[ 0 ], "onerr" ) == 0 ) {
555                 if ( argc != 2 ) {
556                         fprintf( stderr,
557         "%s: line %d: \"onerr {CONTINUE|stop}\" takes 1 argument\n",
558                             fname, lineno );
559                         return( 1 );
560                 }
561
562                 if ( strcasecmp( argv[ 1 ], "continue" ) == 0 ) {
563                         mi->flags &= ~META_BACK_F_ONERR_STOP;
564
565                 } else if ( strcasecmp( argv[ 1 ], "stop" ) == 0 ) {
566                         mi->flags |= META_BACK_F_ONERR_STOP;
567
568                 } else {
569                         fprintf( stderr,
570         "%s: line %d: \"onerr {CONTINUE|stop}\": invalid arg \"%s\".\n",
571                                 fname, lineno, argv[ 1 ] );
572                         return 1;
573                 }
574
575         /* bind-defer? */
576         } else if ( strcasecmp( argv[ 0 ], "pseudoroot-bind-defer" ) == 0 ) {
577                 if ( argc != 2 ) {
578                         fprintf( stderr,
579         "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\" takes 1 argument\n",
580                             fname, lineno );
581                         return( 1 );
582                 }
583
584                 switch ( check_true_false( argv[ 1 ] ) ) {
585                 case 0:
586                         mi->flags &= ~META_BACK_F_DEFER_ROOTDN_BIND;
587                         break;
588
589                 case 1:
590                         mi->flags |= META_BACK_F_DEFER_ROOTDN_BIND;
591                         break;
592
593                 default:
594                         fprintf( stderr,
595         "%s: line %d: \"pseudoroot-bind-defer {FALSE|true}\": invalid arg \"%s\".\n",
596                                 fname, lineno, argv[ 1 ] );
597                         return 1;
598                 }
599
600         } else if ( strcasecmp( argv[ 0 ], "timeout" ) == 0 ) {
601                 char    *sep, *next;
602                 time_t  *tv = mi->mi_ntargets ?
603                                 mi->mi_targets[ mi->mi_ntargets - 1 ].mt_timeout
604                                 : mi->mi_timeout;
605                 int     c;
606
607                 if ( argc < 2 ) {
608                         fprintf( stderr,
609         "%s: line %d: \"timeout [{add|delete|modify|modrdn}=]<val> [...]\" takes at least 1 argument\n",
610                             fname, lineno );
611                         return( 1 );
612                 }
613
614                 for ( c = 1; c < argc; c++ ) {
615                         time_t  *t = NULL, val;
616
617                         sep = strchr( argv[ c ], '=' );
618                         if ( sep != NULL ) {
619                                 size_t  len = sep - argv[ c ];
620
621                                 if ( strncasecmp( argv[ c ], "add", len ) == 0 ) {
622                                         t = &tv[ META_OP_ADD ];
623                                 } else if ( strncasecmp( argv[ c ], "delete", len ) == 0 ) {
624                                         t = &tv[ META_OP_DELETE ];
625                                 } else if ( strncasecmp( argv[ c ], "modify", len ) == 0 ) {
626                                         t = &tv[ META_OP_MODIFY ];
627                                 } else if ( strncasecmp( argv[ c ], "modrdn", len ) == 0 ) {
628                                         t = &tv[ META_OP_MODRDN ];
629                                 } else {
630                                         fprintf( stderr,
631                 "%s: line %d: unknown operation \"%s\" for timeout #%d.\n",
632                                                 fname, lineno, argv[ c ], c );
633                                         return 1;
634                                 }
635                                 sep++;
636         
637                         } else {
638                                 sep = argv[ c ];
639                         }
640         
641                         val = strtoul( sep, &next, 10 );
642                         if ( next == sep || next[ 0 ] != '\0' ) {
643                                 fprintf( stderr,
644                 "%s: line %d: unable to parse value \"%s\" for timeout.\n",
645                                         fname, lineno, sep );
646                                 return 1;
647                         }
648                 
649                         if ( t ) {
650                                 *t = val;
651         
652                         } else {
653                                 int     i;
654         
655                                 for ( i = 0; i < META_OP_LAST; i++ ) {
656                                         tv[ i ] = val;
657                                 }
658                         }
659                 }
660         
661         /* name to use as pseudo-root dn */
662         } else if ( strcasecmp( argv[ 0 ], "pseudorootdn" ) == 0 ) {
663                 int             i = mi->mi_ntargets - 1;
664                 struct berval   dn;
665
666                 if ( i < 0 ) {
667                         fprintf( stderr,
668         "%s: line %d: need \"uri\" directive first\n",
669                                 fname, lineno );
670                         return 1;
671                 }
672                 
673                 if ( argc != 2 ) {
674                         fprintf( stderr,
675         "%s: line %d: missing name in \"pseudorootdn <name>\" line\n",
676                                 fname, lineno );
677                         return 1;
678                 }
679
680                 dn.bv_val = argv[ 1 ];
681                 dn.bv_len = strlen( argv[ 1 ] );
682                 if ( dnNormalize( 0, NULL, NULL, &dn,
683                         &mi->mi_targets[ i ].mt_pseudorootdn, NULL ) != LDAP_SUCCESS )
684                 {
685                         fprintf( stderr, "%s: line %d: "
686                                         "pseudoroot DN '%s' is invalid\n",
687                                         fname, lineno, argv[ 1 ] );
688                         return( 1 );
689                 }
690
691         /* password to use as pseudo-root */
692         } else if ( strcasecmp( argv[ 0 ], "pseudorootpw" ) == 0 ) {
693                 int             i = mi->mi_ntargets - 1;
694
695                 if ( i < 0 ) {
696                         fprintf( stderr,
697         "%s: line %d: need \"uri\" directive first\n",
698                                 fname, lineno );
699                         return 1;
700                 }
701                 
702                 if ( argc != 2 ) {
703                         fprintf( stderr,
704         "%s: line %d: missing password in \"pseudorootpw <password>\" line\n",
705                             fname, lineno );
706                         return 1;
707                 }
708                 ber_str2bv( argv[ 1 ], 0L, 1, &mi->mi_targets[ i ].mt_pseudorootpw );
709         
710         /* dn massaging */
711         } else if ( strcasecmp( argv[ 0 ], "suffixmassage" ) == 0 ) {
712                 BackendDB       *tmp_be;
713                 int             i = mi->mi_ntargets - 1, rc;
714                 struct berval   dn, nvnc, pvnc, nrnc, prnc;
715
716                 if ( i < 0 ) {
717                         fprintf( stderr,
718         "%s: line %d: need \"uri\" directive first\n",
719                                 fname, lineno );
720                         return 1;
721                 }
722                 
723                 /*
724                  * syntax:
725                  * 
726                  *      suffixmassage <suffix> <massaged suffix>
727                  *
728                  * the <suffix> field must be defined as a valid suffix
729                  * (or suffixAlias?) for the current database;
730                  * the <massaged suffix> shouldn't have already been
731                  * defined as a valid suffix or suffixAlias for the 
732                  * current server
733                  */
734                 if ( argc != 3 ) {
735                         fprintf( stderr,
736         "%s: line %d: syntax is \"suffixMassage <suffix> <massaged suffix>\"\n",
737                                 fname, lineno );
738                         return 1;
739                 }
740
741                 ber_str2bv( argv[ 1 ], 0, 0, &dn );
742                 if ( dnPrettyNormal( NULL, &dn, &pvnc, &nvnc, NULL ) != LDAP_SUCCESS ) {
743                         fprintf( stderr, "%s: line %d: "
744                                         "suffix '%s' is invalid\n",
745                                         fname, lineno, argv[ 1 ] );
746                         return 1;
747                 }
748                 
749                 tmp_be = select_backend( &nvnc, 0, 0 );
750                 if ( tmp_be != NULL && tmp_be != be ) {
751                         fprintf( stderr, 
752         "%s: line %d: suffix already in use by another backend in"
753         " \"suffixMassage <suffix> <massaged suffix>\"\n",
754                                 fname, lineno );
755                         free( pvnc.bv_val );
756                         free( nvnc.bv_val );
757                         return 1;                                               
758                 }
759
760                 ber_str2bv( argv[ 2 ], 0, 0, &dn );
761                 if ( dnPrettyNormal( NULL, &dn, &prnc, &nrnc, NULL ) != LDAP_SUCCESS ) {
762                         fprintf( stderr, "%s: line %d: "
763                                         "massaged suffix '%s' is invalid\n",
764                                         fname, lineno, argv[ 2 ] );
765                         free( pvnc.bv_val );
766                         free( nvnc.bv_val );
767                         return 1;
768                 }
769         
770 #if 0   
771                 tmp_be = select_backend( &nrnc, 0, 0 );
772                 if ( tmp_be != NULL ) {
773                         fprintf( stderr,
774         "%s: line %d: massaged suffix already in use by another backend in" 
775         " \"suffixMassage <suffix> <massaged suffix>\"\n",
776                                 fname, lineno );
777                         free( pvnc.bv_val );
778                         free( nvnc.bv_val );
779                         free( prnc.bv_val );
780                         free( nrnc.bv_val );
781                         return 1;
782                 }
783 #endif
784                 
785                 /*
786                  * The suffix massaging is emulated by means of the
787                  * rewrite capabilities
788                  * FIXME: no extra rewrite capabilities should be added
789                  * to the database
790                  */
791                 rc = suffix_massage_config( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
792                                 &pvnc, &nvnc, &prnc, &nrnc );
793
794                 free( pvnc.bv_val );
795                 free( nvnc.bv_val );
796                 free( prnc.bv_val );
797                 free( nrnc.bv_val );
798
799                 return rc;
800                 
801         /* rewrite stuff ... */
802         } else if ( strncasecmp( argv[ 0 ], "rewrite", 7 ) == 0 ) {
803                 int             i = mi->mi_ntargets - 1;
804
805                 if ( i < 0 ) {
806                         fprintf( stderr, "%s: line %d: \"rewrite\" "
807                                 "statement outside target definition.\n",
808                                 fname, lineno );
809                         return 1;
810                 }
811                 
812                 return rewrite_parse( mi->mi_targets[ i ].mt_rwmap.rwm_rw,
813                                 fname, lineno, argc, argv );
814
815         /* objectclass/attribute mapping */
816         } else if ( strcasecmp( argv[ 0 ], "map" ) == 0 ) {
817                 int             i = mi->mi_ntargets - 1;
818
819                 if ( i < 0 ) {
820                         fprintf( stderr,
821         "%s: line %d: need \"uri\" directive first\n",
822                                 fname, lineno );
823                         return 1;
824                 }
825
826                 return ldap_back_map_config( &mi->mi_targets[ i ].mt_rwmap.rwm_oc, 
827                                 &mi->mi_targets[ i ].mt_rwmap.rwm_at,
828                                 fname, lineno, argc, argv );
829
830         } else if ( strcasecmp( argv[ 0 ], "nretries" ) == 0 ) {
831                 int             i = mi->mi_ntargets - 1;
832                 int             nretries = META_RETRY_UNDEFINED;
833
834                 if ( argc != 2 ) {
835                         fprintf( stderr,
836         "%s: line %d: need value in \"nretries <value>\"\n",
837                                 fname, lineno );
838                         return 1;
839                 }
840
841                 if ( strcasecmp( argv[ 1 ], "forever" ) == 0 ) {
842                         nretries = META_RETRY_FOREVER;
843
844                 } else if ( strcasecmp( argv[ 1 ], "never" ) == 0 ) {
845                         nretries = META_RETRY_NEVER;
846
847                 } else {
848                         char    *next;
849
850                         nretries = strtol( argv[ 1 ], &next, 10 );
851                         if ( next == argv[ 1 ] || next[ 0 ] != '\0' ) {
852                                 fprintf( stderr,
853         "%s: line %d: unable to parse value \"%s\" in \"nretries <value>\"\n",
854                                         fname, lineno, argv[ 1 ] );
855                                 return 1;
856                         }
857                 }
858
859                 if ( i < 0 ) {
860                         mi->mi_nretries = nretries;
861
862                 } else {
863                         mi->mi_targets[ i ].mt_nretries = nretries;
864                 }
865
866         /* anything else */
867         } else {
868                 return SLAP_CONF_UNKNOWN;
869         }
870         return 0;
871 }
872
873 int
874 ldap_back_map_config(
875                 struct ldapmap  *oc_map,
876                 struct ldapmap  *at_map,
877                 const char      *fname,
878                 int             lineno,
879                 int             argc,
880                 char            **argv )
881 {
882         struct ldapmap          *map;
883         struct ldapmapping      *mapping;
884         char                    *src, *dst;
885         int                     is_oc = 0;
886
887         if ( argc < 3 || argc > 4 ) {
888                 fprintf( stderr,
889         "%s: line %d: syntax is \"map {objectclass | attribute} [<local> | *] {<foreign> | *}\"\n",
890                         fname, lineno );
891                 return 1;
892         }
893
894         if ( strcasecmp( argv[ 1 ], "objectclass" ) == 0 ) {
895                 map = oc_map;
896                 is_oc = 1;
897
898         } else if ( strcasecmp( argv[ 1 ], "attribute" ) == 0 ) {
899                 map = at_map;
900
901         } else {
902                 fprintf( stderr, "%s: line %d: syntax is "
903                         "\"map {objectclass | attribute} [<local> | *] "
904                         "{<foreign> | *}\"\n",
905                         fname, lineno );
906                 return 1;
907         }
908
909         if ( strcmp( argv[ 2 ], "*" ) == 0 ) {
910                 if ( argc < 4 || strcmp( argv[ 3 ], "*" ) == 0 ) {
911                         map->drop_missing = ( argc < 4 );
912                         return 0;
913                 }
914                 src = dst = argv[ 3 ];
915
916         } else if ( argc < 4 ) {
917                 src = "";
918                 dst = argv[ 2 ];
919
920         } else {
921                 src = argv[ 2 ];
922                 dst = ( strcmp( argv[ 3 ], "*" ) == 0 ? src : argv[ 3 ] );
923         }
924
925         if ( ( map == at_map )
926                         && ( strcasecmp( src, "objectclass" ) == 0
927                         || strcasecmp( dst, "objectclass" ) == 0 ) )
928         {
929                 fprintf( stderr,
930                         "%s: line %d: objectclass attribute cannot be mapped\n",
931                         fname, lineno );
932         }
933
934         mapping = (struct ldapmapping *)ch_calloc( 2,
935                 sizeof(struct ldapmapping) );
936         if ( mapping == NULL ) {
937                 fprintf( stderr,
938                         "%s: line %d: out of memory\n",
939                         fname, lineno );
940                 return 1;
941         }
942         ber_str2bv( src, 0, 1, &mapping->src );
943         ber_str2bv( dst, 0, 1, &mapping->dst );
944         mapping[ 1 ].src = mapping->dst;
945         mapping[ 1 ].dst = mapping->src;
946
947         /*
948          * schema check
949          */
950         if ( is_oc ) {
951                 if ( src[ 0 ] != '\0' ) {
952                         if ( oc_bvfind( &mapping->src ) == NULL ) {
953                                 fprintf( stderr,
954         "%s: line %d: warning, source objectClass '%s' "
955         "should be defined in schema\n",
956                                         fname, lineno, src );
957
958                                 /*
959                                  * FIXME: this should become an err
960                                  */
961                                 goto error_return;
962                         }
963                 }
964
965                 if ( oc_bvfind( &mapping->dst ) == NULL ) {
966                         fprintf( stderr,
967         "%s: line %d: warning, destination objectClass '%s' "
968         "is not defined in schema\n",
969                                 fname, lineno, dst );
970                 }
971         } else {
972                 int                     rc;
973                 const char              *text = NULL;
974                 AttributeDescription    *ad = NULL;
975
976                 if ( src[ 0 ] != '\0' ) {
977                         rc = slap_bv2ad( &mapping->src, &ad, &text );
978                         if ( rc != LDAP_SUCCESS ) {
979                                 fprintf( stderr,
980         "%s: line %d: warning, source attributeType '%s' "
981         "should be defined in schema\n",
982                                         fname, lineno, src );
983
984                                 /*
985                                  * FIXME: this should become an err
986                                  */
987                                 /*
988                                  * we create a fake "proxied" ad 
989                                  * and add it here.
990                                  */
991
992                                 rc = slap_bv2undef_ad( &mapping->src,
993                                                 &ad, &text, SLAP_AD_PROXIED );
994                                 if ( rc != LDAP_SUCCESS ) {
995                                         fprintf( stderr,
996         "%s: line %d: source attributeType '%s': %d (%s)\n",
997                                                 fname, lineno, src,
998                                                 rc, text ? text : "" );
999                                         goto error_return;
1000                                 }
1001                         }
1002
1003                         ad = NULL;
1004                 }
1005
1006                 rc = slap_bv2ad( &mapping->dst, &ad, &text );
1007                 if ( rc != LDAP_SUCCESS ) {
1008                         fprintf( stderr,
1009         "%s: line %d: warning, destination attributeType '%s' "
1010         "is not defined in schema\n",
1011                                 fname, lineno, dst );
1012
1013                         /*
1014                          * we create a fake "proxied" ad 
1015                          * and add it here.
1016                          */
1017
1018                         rc = slap_bv2undef_ad( &mapping->dst,
1019                                         &ad, &text, SLAP_AD_PROXIED );
1020                         if ( rc != LDAP_SUCCESS ) {
1021                                 fprintf( stderr,
1022         "%s: line %d: source attributeType '%s': %d (%s)\n",
1023                                         fname, lineno, dst,
1024                                         rc, text ? text : "" );
1025                                 return 1;
1026                         }
1027                 }
1028         }
1029
1030         if ( (src[ 0 ] != '\0' && avl_find( map->map, (caddr_t)mapping, mapping_cmp ) != NULL)
1031                         || avl_find( map->remap, (caddr_t)&mapping[ 1 ], mapping_cmp ) != NULL)
1032         {
1033                 fprintf( stderr,
1034                         "%s: line %d: duplicate mapping found" SLAPD_CONF_UNKNOWN_IGNORED ".\n",
1035                         fname, lineno );
1036                 goto error_return;
1037         }
1038
1039         if ( src[ 0 ] != '\0' ) {
1040                 avl_insert( &map->map, (caddr_t)mapping,
1041                                         mapping_cmp, mapping_dup );
1042         }
1043         avl_insert( &map->remap, (caddr_t)&mapping[ 1 ],
1044                                 mapping_cmp, mapping_dup );
1045
1046         return 0;
1047
1048 error_return:;
1049         if ( mapping ) {
1050                 ch_free( mapping->src.bv_val );
1051                 ch_free( mapping->dst.bv_val );
1052                 ch_free( mapping );
1053         }
1054
1055         return 1;
1056 }
1057
1058
1059 #ifdef ENABLE_REWRITE
1060 static char *
1061 suffix_massage_regexize( const char *s )
1062 {
1063         char *res, *ptr;
1064         const char *p, *r;
1065         int i;
1066
1067         if ( s[ 0 ] == '\0' ) {
1068                 return ch_strdup( "^(.+)$" );
1069         }
1070
1071         for ( i = 0, p = s; 
1072                         ( r = strchr( p, ',' ) ) != NULL; 
1073                         p = r + 1, i++ )
1074                 ;
1075
1076         res = ch_calloc( sizeof( char ),
1077                         strlen( s )
1078                         + STRLENOF( "((.+),)?" )
1079                         + STRLENOF( "[ ]?" ) * i
1080                         + STRLENOF( "$" ) + 1 );
1081
1082         ptr = lutil_strcopy( res, "((.+),)?" );
1083         for ( i = 0, p = s;
1084                         ( r = strchr( p, ',' ) ) != NULL;
1085                         p = r + 1 , i++ ) {
1086                 ptr = lutil_strncopy( ptr, p, r - p + 1 );
1087                 ptr = lutil_strcopy( ptr, "[ ]?" );
1088
1089                 if ( r[ 1 ] == ' ' ) {
1090                         r++;
1091                 }
1092         }
1093         ptr = lutil_strcopy( ptr, p );
1094         ptr[ 0 ] = '$';
1095         ptr++;
1096         ptr[ 0 ] = '\0';
1097
1098         return res;
1099 }
1100
1101 static char *
1102 suffix_massage_patternize( const char *s, const char *p )
1103 {
1104         ber_len_t       len;
1105         char            *res, *ptr;
1106
1107         len = strlen( p );
1108
1109         if ( s[ 0 ] == '\0' ) {
1110                 len++;
1111         }
1112
1113         res = ch_calloc( sizeof( char ), len + STRLENOF( "%1" ) + 1 );
1114         if ( res == NULL ) {
1115                 return NULL;
1116         }
1117
1118         ptr = lutil_strcopy( res, ( p[ 0 ] == '\0' ? "%2" : "%1" ) );
1119         if ( s[ 0 ] == '\0' ) {
1120                 ptr[ 0 ] = ',';
1121                 ptr++;
1122         }
1123         lutil_strcopy( ptr, p );
1124
1125         return res;
1126 }
1127
1128 int
1129 suffix_massage_config( 
1130                 struct rewrite_info *info,
1131                 struct berval *pvnc,
1132                 struct berval *nvnc,
1133                 struct berval *prnc,
1134                 struct berval *nrnc
1135 )
1136 {
1137         char *rargv[ 5 ];
1138         int line = 0;
1139
1140         rargv[ 0 ] = "rewriteEngine";
1141         rargv[ 1 ] = "on";
1142         rargv[ 2 ] = NULL;
1143         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1144
1145         rargv[ 0 ] = "rewriteContext";
1146         rargv[ 1 ] = "default";
1147         rargv[ 2 ] = NULL;
1148         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1149
1150         rargv[ 0 ] = "rewriteRule";
1151         rargv[ 1 ] = suffix_massage_regexize( pvnc->bv_val );
1152         rargv[ 2 ] = suffix_massage_patternize( pvnc->bv_val, prnc->bv_val );
1153         rargv[ 3 ] = ":";
1154         rargv[ 4 ] = NULL;
1155         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1156         ch_free( rargv[ 1 ] );
1157         ch_free( rargv[ 2 ] );
1158
1159         if ( BER_BVISEMPTY( pvnc ) ) {
1160                 rargv[ 0 ] = "rewriteRule";
1161                 rargv[ 1 ] = "^$";
1162                 rargv[ 2 ] = prnc->bv_val;
1163                 rargv[ 3 ] = ":";
1164                 rargv[ 4 ] = NULL;
1165                 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1166         }
1167         
1168         rargv[ 0 ] = "rewriteContext";
1169         rargv[ 1 ] = "searchEntryDN";
1170         rargv[ 2 ] = NULL;
1171         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1172
1173         rargv[ 0 ] = "rewriteRule";
1174         rargv[ 1 ] = suffix_massage_regexize( prnc->bv_val );
1175         rargv[ 2 ] = suffix_massage_patternize( prnc->bv_val, pvnc->bv_val );
1176         rargv[ 3 ] = ":";
1177         rargv[ 4 ] = NULL;
1178         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1179         ch_free( rargv[ 1 ] );
1180         ch_free( rargv[ 2 ] );
1181
1182         if ( BER_BVISEMPTY( prnc ) ) {
1183                 rargv[ 0 ] = "rewriteRule";
1184                 rargv[ 1 ] = "^$";
1185                 rargv[ 2 ] = pvnc->bv_val;
1186                 rargv[ 3 ] = ":";
1187                 rargv[ 4 ] = NULL;
1188                 rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1189         }
1190         
1191         /* backward compatibility */
1192         rargv[ 0 ] = "rewriteContext";
1193         rargv[ 1 ] = "searchResult";
1194         rargv[ 2 ] = "alias";
1195         rargv[ 3 ] = "searchEntryDN";
1196         rargv[ 4 ] = NULL;
1197         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1198         
1199         rargv[ 0 ] = "rewriteContext";
1200         rargv[ 1 ] = "matchedDN";
1201         rargv[ 2 ] = "alias";
1202         rargv[ 3 ] = "searchEntryDN";
1203         rargv[ 4 ] = NULL;
1204         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1205
1206         rargv[ 0 ] = "rewriteContext";
1207         rargv[ 1 ] = "searchAttrDN";
1208         rargv[ 2 ] = "alias";
1209         rargv[ 3 ] = "searchEntryDN";
1210         rargv[ 4 ] = NULL;
1211         rewrite_parse( info, "<suffix massage>", ++line, 4, rargv );
1212
1213         /* NOTE: this corresponds to #undef'ining RWM_REFERRAL_REWRITE;
1214          * see servers/slapd/overlays/rwm.h for details */
1215         rargv[ 0 ] = "rewriteContext";
1216         rargv[ 1 ] = "referralAttrDN";
1217         rargv[ 2 ] = NULL;
1218         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1219
1220         rargv[ 0 ] = "rewriteContext";
1221         rargv[ 1 ] = "referralDN";
1222         rargv[ 2 ] = NULL;
1223         rewrite_parse( info, "<suffix massage>", ++line, 2, rargv );
1224         
1225         return 0;
1226 }
1227 #endif /* ENABLE_REWRITE */
1228