]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
A follow-on commit to the previous @oc support change to use anlist
[openldap] / servers / slapd / config.c
1 /* config.c - configuration file handling routines */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 1998-2004 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
17  * All rights reserved.
18  *
19  * Redistribution and use in source and binary forms are permitted
20  * provided that this notice is preserved and that due credit is given
21  * to the University of Michigan at Ann Arbor. The name of the University
22  * may not be used to endorse or promote products derived from this
23  * software without specific prior written permission. This software
24  * is provided ``as is'' without express or implied warranty.
25  */
26
27 #include "portable.h"
28
29 #include <stdio.h>
30
31 #include <ac/string.h>
32 #include <ac/ctype.h>
33 #include <ac/signal.h>
34 #include <ac/socket.h>
35 #include <ac/errno.h>
36
37 #include "ldap_pvt.h"
38 #include "slap.h"
39 #ifdef LDAP_SLAPI
40 #include "slapi/slapi.h"
41 #endif
42 #include "lutil.h"
43
44 #define ARGS_STEP       512
45
46 /*
47  * defaults for various global variables
48  */
49 slap_mask_t             global_allows = 0;
50 slap_mask_t             global_disallows = 0;
51 char            *replogfile;
52 int             global_gentlehup = 0;
53 int             global_idletimeout = 0;
54 char    *global_host = NULL;
55 char    *global_realm = NULL;
56 char            *ldap_srvtab = "";
57 char            **default_passwd_hash = NULL;
58 int             cargc = 0, cargv_size = 0;
59 char    **cargv;
60 struct berval default_search_base = BER_BVNULL;
61 struct berval default_search_nbase = BER_BVNULL;
62 unsigned                num_subordinates = 0;
63
64 ber_len_t sockbuf_max_incoming = SLAP_SB_MAX_INCOMING_DEFAULT;
65 ber_len_t sockbuf_max_incoming_auth= SLAP_SB_MAX_INCOMING_AUTH;
66
67 int     slap_conn_max_pending = SLAP_CONN_MAX_PENDING_DEFAULT;
68 int     slap_conn_max_pending_auth = SLAP_CONN_MAX_PENDING_AUTH;
69
70 char   *slapd_pid_file  = NULL;
71 char   *slapd_args_file = NULL;
72
73 char   *strtok_quote_ptr;
74
75 int use_reverse_lookup = 0;
76
77 #ifdef LDAP_SLAPI
78 int slapi_plugins_used = 0;
79 #endif
80
81 static char *fp_getline(FILE *fp, int *lineno);
82 static void fp_getline_init(int *lineno);
83 static int fp_parse_line(int lineno, char *line);
84
85 static char     *strtok_quote(char *line, char *sep);
86 static int load_ucdata(char *path);
87
88 static int add_syncrepl LDAP_P(( Backend *, char **, int ));
89 static int parse_syncrepl_line LDAP_P(( char **, int, syncinfo_t *));
90
91 int
92 read_config( const char *fname, int depth )
93 {
94         FILE    *fp;
95         char    *line, *savefname, *saveline;
96         int savelineno;
97         int     lineno, i;
98         int rc;
99         struct berval vals[2];
100         char *replicahost;
101         LDAPURLDesc *ludp;
102         static BackendInfo *bi = NULL;
103         static BackendDB        *be = NULL;
104         char    *next;
105
106         vals[1].bv_val = NULL;
107
108         if ( depth == 0 ) {
109                 cargv = ch_calloc( ARGS_STEP + 1, sizeof(*cargv) );
110                 cargv_size = ARGS_STEP + 1;
111         }
112
113         if ( (fp = fopen( fname, "r" )) == NULL ) {
114                 ldap_syslog = 1;
115                 Debug( LDAP_DEBUG_ANY,
116                     "could not open config file \"%s\": %s (%d)\n",
117                     fname, strerror(errno), errno );
118                 return 1;
119         }
120
121         Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
122
123
124         fp_getline_init( &lineno );
125
126         while ( (line = fp_getline( fp, &lineno )) != NULL ) {
127                 /* skip comments and blank lines */
128                 if ( line[0] == '#' || line[0] == '\0' ) {
129                         continue;
130                 }
131
132                 /* fp_parse_line is destructive, we save a copy */
133                 saveline = ch_strdup( line );
134
135                 if ( fp_parse_line( lineno, line ) != 0 ) {
136                         return( 1 );
137                 }
138
139                 if ( cargc < 1 ) {
140                         Debug( LDAP_DEBUG_ANY,
141                             "%s: line %d: bad config line (ignored)\n",
142                             fname, lineno, 0 );
143
144                         continue;
145                 }
146
147                 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
148                         if ( cargc < 2 ) {
149                                 Debug( LDAP_DEBUG_ANY,
150                 "%s: line %d: missing type in \"backend <type>\" line\n",
151                                     fname, lineno, 0 );
152
153                                 return( 1 );
154                         }
155
156                         if( be != NULL ) {
157                                 Debug( LDAP_DEBUG_ANY,
158 "%s: line %d: backend line must appear before any database definition\n",
159                                     fname, lineno, 0 );
160
161                                 return( 1 );
162                         }
163
164                         bi = backend_info( cargv[1] );
165
166                         if( bi == NULL ) {
167                                 Debug( LDAP_DEBUG_ANY,
168                                         "backend %s initialization failed.\n",
169                                     cargv[1], 0, 0 );
170
171                                 return( 1 );
172                         }
173                 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
174                         if ( cargc < 2 ) {
175                                 Debug( LDAP_DEBUG_ANY,
176                 "%s: line %d: missing type in \"database <type>\" line\n",
177                                     fname, lineno, 0 );
178
179                                 return( 1 );
180                         }
181
182                         bi = NULL;
183                         be = backend_db_init( cargv[1] );
184
185                         if( be == NULL ) {
186                                 Debug( LDAP_DEBUG_ANY,
187                                         "database %s initialization failed.\n",
188                                     cargv[1], 0, 0 );
189
190                                 return( 1 );
191                         }
192
193                 /* set local security factor */
194                 } else if ( strcasecmp( cargv[0], "localSSF" ) == 0 ) {
195                         long ssf;
196                         if ( cargc < 2 ) {
197                                 Debug( LDAP_DEBUG_ANY,
198                                    "%s: line %d: missing ssf in \"localSSF <ssf>\" line\n",
199                                     fname, lineno, 0 );
200                                 return( 1 );
201                         }
202
203                         ssf = atol( cargv[1] );
204
205                         if( ssf < 0 ) {
206                                 Debug( LDAP_DEBUG_ANY,
207                                         "%s: line %d: invalid ssf value (%ld) in "
208                                         "\"localSSF <ssf>\" line.\n",
209                                     fname, lineno, ssf );
210                                 return( 1 );
211                         }
212
213                         local_ssf = ssf;
214
215                 /* set thread concurrency */
216                 } else if ( strcasecmp( cargv[0], "concurrency" ) == 0 ) {
217                         int c;
218                         if ( cargc < 2 ) {
219                                 Debug( LDAP_DEBUG_ANY,
220             "%s: line %d: missing level in \"concurrency <level>\" line\n",
221                                     fname, lineno, 0 );
222
223                                 return( 1 );
224                         }
225
226                         c = strtol( cargv[1], &next, 10 );
227                         if ( next == NULL || next[0] != '\0' ) {
228                                 Debug( LDAP_DEBUG_ANY,
229             "%s: line %d: unable to parse level \"%s\" in \"concurrency <level>\" line\n",
230                                     fname, lineno, cargv[1] );
231                                 return( 1 );
232                         }
233
234                         if( c < 1 ) {
235                                 Debug( LDAP_DEBUG_ANY,
236             "%s: line %d: invalid level (%d) in \"concurrency <level>\" line\n",
237                                     fname, lineno, c );
238
239                                 return( 1 );
240                         }
241
242                         ldap_pvt_thread_set_concurrency( c );
243
244                 /* set sockbuf max */
245                 } else if ( strcasecmp( cargv[0], "sockbuf_max_incoming" ) == 0 ) {
246                         long max;
247                         if ( cargc < 2 ) {
248                                 Debug( LDAP_DEBUG_ANY,
249                                            "%s: line %d: missing max in \"sockbuf_max_incoming <bytes>\" line\n",
250                                     fname, lineno, 0 );
251
252                                 return( 1 );
253                         }
254
255                         max = atol( cargv[1] );
256
257                         if( max < 0 ) {
258                                 Debug( LDAP_DEBUG_ANY,
259                                         "%s: line %d: invalid max value (%ld) in "
260                                         "\"sockbuf_max_incoming <bytes>\" line.\n",
261                                     fname, lineno, max );
262
263                                 return( 1 );
264                         }
265
266                         sockbuf_max_incoming = max;
267
268                 /* set sockbuf max authenticated */
269                 } else if ( strcasecmp( cargv[0], "sockbuf_max_incoming_auth" ) == 0 ) {
270                         long max;
271                         if ( cargc < 2 ) {
272                                 Debug( LDAP_DEBUG_ANY,
273                                            "%s: line %d: missing max in \"sockbuf_max_incoming_auth <bytes>\" line\n",
274                                     fname, lineno, 0 );
275
276                                 return( 1 );
277                         }
278
279                         max = atol( cargv[1] );
280
281                         if( max < 0 ) {
282                                 Debug( LDAP_DEBUG_ANY,
283                                         "%s: line %d: invalid max value (%ld) in "
284                                         "\"sockbuf_max_incoming_auth <bytes>\" line.\n",
285                                     fname, lineno, max );
286
287                                 return( 1 );
288                         }
289
290                         sockbuf_max_incoming_auth = max;
291
292                 /* set conn pending max */
293                 } else if ( strcasecmp( cargv[0], "conn_max_pending" ) == 0 ) {
294                         long max;
295                         if ( cargc < 2 ) {
296                                 Debug( LDAP_DEBUG_ANY,
297                                            "%s: line %d: missing max in \"conn_max_pending <requests>\" line\n",
298                                     fname, lineno, 0 );
299
300                                 return( 1 );
301                         }
302
303                         max = atol( cargv[1] );
304
305                         if( max < 0 ) {
306                                 Debug( LDAP_DEBUG_ANY,
307                                         "%s: line %d: invalid max value (%ld) in "
308                                         "\"conn_max_pending <requests>\" line.\n",
309                                     fname, lineno, max );
310
311                                 return( 1 );
312                         }
313
314                         slap_conn_max_pending = max;
315
316                 /* set conn pending max authenticated */
317                 } else if ( strcasecmp( cargv[0], "conn_max_pending_auth" ) == 0 ) {
318                         long max;
319                         if ( cargc < 2 ) {
320                                 Debug( LDAP_DEBUG_ANY,
321                                            "%s: line %d: missing max in \"conn_max_pending_auth <requests>\" line\n",
322                                     fname, lineno, 0 );
323
324                                 return( 1 );
325                         }
326
327                         max = atol( cargv[1] );
328
329                         if( max < 0 ) {
330                                 Debug( LDAP_DEBUG_ANY,
331                                         "%s: line %d: invalid max value (%ld) in "
332                                         "\"conn_max_pending_auth <requests>\" line.\n",
333                                     fname, lineno, max );
334
335                                 return( 1 );
336                         }
337
338                         slap_conn_max_pending_auth = max;
339
340                 /* default search base */
341                 } else if ( strcasecmp( cargv[0], "defaultSearchBase" ) == 0 ) {
342                         if ( cargc < 2 ) {
343                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
344                                         "missing dn in \"defaultSearchBase <dn>\" line\n",
345                                         fname, lineno, 0 );
346
347                                 return 1;
348
349                         } else if ( cargc > 2 ) {
350                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
351                                         "extra cruft after <dn> in \"defaultSearchBase %s\", "
352                                         "line (ignored)\n",
353                                         fname, lineno, cargv[1] );
354                         }
355
356                         if ( bi != NULL || be != NULL ) {
357                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
358                                         "defaultSearchBaase line must appear prior to "
359                                         "any backend or database definition\n",
360                                     fname, lineno, 0 );
361
362                                 return 1;
363                         }
364
365                         if ( default_search_nbase.bv_len ) {
366                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
367                                         "default search base \"%s\" already defined "
368                                         "(discarding old)\n",
369                                         fname, lineno, default_search_base.bv_val );
370
371                                 free( default_search_base.bv_val );
372                                 free( default_search_nbase.bv_val );
373                         }
374
375                         if ( load_ucdata( NULL ) < 0 ) return 1;
376
377                         {
378                                 struct berval dn;
379
380                                 dn.bv_val = cargv[1];
381                                 dn.bv_len = strlen( dn.bv_val );
382
383                                 rc = dnPrettyNormal( NULL, &dn,
384                                         &default_search_base,
385                                         &default_search_nbase, NULL );
386
387                                 if( rc != LDAP_SUCCESS ) {
388                                         Debug( LDAP_DEBUG_ANY,
389                                                 "%s: line %d: defaultSearchBase DN is invalid\n",
390                                            fname, lineno, 0 );
391                                         return( 1 );
392                                 }
393                         }
394
395                 /* set maximum threads in thread pool */
396                 } else if ( strcasecmp( cargv[0], "threads" ) == 0 ) {
397                         int c;
398                         if ( cargc < 2 ) {
399                                 Debug( LDAP_DEBUG_ANY,
400             "%s: line %d: missing count in \"threads <count>\" line\n",
401                                     fname, lineno, 0 );
402
403                                 return( 1 );
404                         }
405
406                         c = strtol( cargv[1], &next, 10 );
407                         if (next == NULL || next[0] != '\0' ) {
408                                 Debug( LDAP_DEBUG_ANY,
409             "%s: line %d: unable to parse count \"%s\" in \"threads <count>\" line\n",
410                                     fname, lineno, cargv[1] );
411                                 return( 1 );
412                         }
413
414                         if( c < 0 ) {
415                                 Debug( LDAP_DEBUG_ANY,
416             "%s: line %d: invalid level (%d) in \"threads <count>\" line\n",
417                                     fname, lineno, c );
418
419                                 return( 1 );
420                         }
421
422                         ldap_pvt_thread_pool_maxthreads( &connection_pool, c );
423
424                         /* save for later use */
425                         connection_pool_max = c;
426
427                 /* get pid file name */
428                 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
429                         if ( cargc < 2 ) {
430                                 Debug( LDAP_DEBUG_ANY,
431             "%s: line %d: missing file name in \"pidfile <file>\" line\n",
432                                     fname, lineno, 0 );
433
434                                 return( 1 );
435                         }
436
437                         slapd_pid_file = ch_strdup( cargv[1] );
438
439                 /* get args file name */
440                 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
441                         if ( cargc < 2 ) {
442                                 Debug( LDAP_DEBUG_ANY,
443             "%s: line %d: missing file name in \"argsfile <file>\" line\n",
444                                     fname, lineno, 0 );
445
446                                 return( 1 );
447                         }
448
449                         slapd_args_file = ch_strdup( cargv[1] );
450
451                 } else if ( strcasecmp( cargv[0], "replica-pidfile" ) == 0 ) {
452                         /* ignore */ ;
453
454                 } else if ( strcasecmp( cargv[0], "replica-argsfile" ) == 0 ) {
455                         /* ignore */ ;
456
457                 /* default password hash */
458                 } else if ( strcasecmp( cargv[0], "password-hash" ) == 0 ) {
459                         if ( cargc < 2 ) {
460                                 Debug( LDAP_DEBUG_ANY,
461             "%s: line %d: missing hash in \"password-hash <hash>\" line\n",
462                                     fname, lineno, 0 );
463
464                                 return( 1 );
465                         }
466                         if ( default_passwd_hash != NULL ) {
467                                 Debug( LDAP_DEBUG_ANY,
468                                         "%s: line %d: already set default password_hash!\n",
469                                         fname, lineno, 0 );
470
471                                 return 1;
472
473                         }
474                         for(i = 1; i < cargc; i++) {
475                                 if ( lutil_passwd_scheme( cargv[i] ) == 0 ) {
476                                         Debug( LDAP_DEBUG_ANY,
477                                                 "%s: line %d: password scheme \"%s\" not available\n",
478                                                 fname, lineno, cargv[i] );
479                                 } else {
480                                         ldap_charray_add( &default_passwd_hash, cargv[i] );
481                                 }
482                         }
483                         if( !default_passwd_hash ) {
484                                 Debug( LDAP_DEBUG_ANY,
485                                         "%s: line %d: no valid hashes found\n",
486                                         fname, lineno, 0 );
487                                 return 1;
488                         }
489
490                 } else if ( strcasecmp( cargv[0], "password-crypt-salt-format" ) == 0 ) 
491                 {
492                         if ( cargc < 2 ) {
493                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: missing format in "
494                                         "\"password-crypt-salt-format <format>\" line\n",
495                                     fname, lineno, 0 );
496
497                                 return 1;
498                         }
499
500                         lutil_salt_format( cargv[1] );
501
502 #ifdef SLAP_AUTH_REWRITE
503                 /* use authid rewrite instead of sasl regexp */
504                 } else if ( strncasecmp( cargv[0], "auth-rewrite",
505                         STRLENOF("auth-rewrite") ) == 0 )
506                 {
507                         int rc = slap_sasl_rewrite_config( fname, lineno,
508                                         cargc, cargv );
509                         if ( rc ) {
510                                 return rc;
511                         }
512 #endif /* SLAP_AUTH_REWRITE */
513
514                 /* Auth + SASL config options */
515                 } else if ( !strncasecmp( cargv[0], "auth", STRLENOF("auth") ) ||
516                         !strncasecmp( cargv[0], "sasl", STRLENOF("sasl") ))
517                 {
518                         if ( slap_sasl_config( cargc, cargv, line, fname, lineno ) )
519                                 return 1;
520
521
522                 } else if ( strcasecmp( cargv[0], "schemadn" ) == 0 ) {
523                         struct berval dn;
524                         if ( cargc < 2 ) {
525                                 Debug( LDAP_DEBUG_ANY,
526             "%s: line %d: missing dn in \"schemadn <dn>\" line\n",
527                                     fname, lineno, 0 );
528                                 return 1 ;
529                         }
530                         ber_str2bv( cargv[1], 0, 0, &dn );
531                         if ( be ) {
532                                 rc = dnPrettyNormal( NULL, &dn, &be->be_schemadn,
533                                         &be->be_schemandn, NULL );
534                         } else {
535                                 rc = dnPrettyNormal( NULL, &dn, &frontendDB->be_schemadn,
536                                         &frontendDB->be_schemandn, NULL );
537                         }
538                         if ( rc != LDAP_SUCCESS ) {
539                                 Debug( LDAP_DEBUG_ANY,
540                                         "%s: line %d: schemadn DN is invalid\n",
541                                         fname, lineno, 0 );
542                                 return 1;
543                         }
544
545                 /* set UCDATA path */
546                 } else if ( strcasecmp( cargv[0], "ucdata-path" ) == 0 ) {
547                         int err;
548                         if ( cargc < 2 ) {
549                                 Debug( LDAP_DEBUG_ANY,
550             "%s: line %d: missing path in \"ucdata-path <path>\" line\n",
551                                     fname, lineno, 0 );
552
553                                 return( 1 );
554                         }
555
556                         err = load_ucdata( cargv[1] );
557                         if ( err <= 0 ) {
558                                 if ( err == 0 ) {
559                                         Debug( LDAP_DEBUG_ANY,
560                                                "%s: line %d: ucdata already loaded, ucdata-path must be set earlier in the file and/or be specified only once!\n",
561                                                fname, lineno, 0 );
562
563                                 }
564                                 return( 1 );
565                         }
566
567                 /* set size limit */
568                 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
569                         int rc = 0, i;
570                         struct slap_limits_set *lim;
571                         
572                         if ( cargc < 2 ) {
573                                 Debug( LDAP_DEBUG_ANY,
574             "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
575                                     fname, lineno, 0 );
576
577                                 return( 1 );
578                         }
579
580                         if ( be == NULL ) {
581                                 lim = &frontendDB->be_def_limit;
582                         } else {
583                                 lim = &be->be_def_limit;
584                         }
585
586                         for ( i = 1; i < cargc; i++ ) {
587                                 if ( strncasecmp( cargv[i], "size", 4 ) == 0 ) {
588                                         rc = limits_parse_one( cargv[i], lim );
589                                         if ( rc ) {
590                                                 Debug( LDAP_DEBUG_ANY,
591                                                         "%s: line %d: unable "
592                                                         "to parse value \"%s\" "
593                                                         "in \"sizelimit "
594                                                         "<limit>\" line\n",
595                                                         fname, lineno, cargv[i] );
596                                                 return( 1 );
597                                         }
598
599                                 } else {
600                                         if ( strcasecmp( cargv[i], "unlimited" ) == 0 ) {
601                                                 lim->lms_s_soft = -1;
602                                         } else {
603                                                 lim->lms_s_soft = strtol( cargv[i] , &next, 0 );
604                                                 if ( next == cargv[i] ) {
605                                                         Debug( LDAP_DEBUG_ANY,
606                                                             "%s: line %d: unable to parse limit \"%s\" in \"sizelimit <limit>\" line\n",
607                                                             fname, lineno, cargv[i] );
608                                                         return( 1 );
609
610                                                 } else if ( next[0] != '\0' ) {
611                                                         Debug( LDAP_DEBUG_ANY,
612                                                             "%s: line %d: trailing chars \"%s\" in \"sizelimit <limit>\" line ignored\n",
613                                                             fname, lineno, next );
614                                                 }
615                                         }
616                                         lim->lms_s_hard = 0;
617                                 }
618                         }
619
620                 /* set time limit */
621                 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
622                         int rc = 0, i;
623                         struct slap_limits_set *lim;
624                         
625                         if ( cargc < 2 ) {
626                                 Debug( LDAP_DEBUG_ANY,
627             "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
628                                     fname, lineno, 0 );
629
630                                 return( 1 );
631                         }
632                         
633                         if ( be == NULL ) {
634                                 lim = &frontendDB->be_def_limit;
635                         } else {
636                                 lim = &be->be_def_limit;
637                         }
638
639                         for ( i = 1; i < cargc; i++ ) {
640                                 if ( strncasecmp( cargv[i], "time", 4 ) == 0 ) {
641                                         rc = limits_parse_one( cargv[i], lim );
642                                         if ( rc ) {
643                                                 Debug( LDAP_DEBUG_ANY,
644                                                         "%s: line %d: unable "
645                                                         "to parse value \"%s\" "
646                                                         "in \"timelimit "
647                                                         "<limit>\" line\n",
648                                                         fname, lineno, cargv[i] );
649                                                 return( 1 );
650                                         }
651
652                                 } else {
653                                         if ( strcasecmp( cargv[i], "unlimited" ) == 0 ) {
654                                                 lim->lms_t_soft = -1;
655                                         } else {
656                                                 lim->lms_t_soft = strtol( cargv[i] , &next, 0 );
657                                                 if ( next == cargv[i] ) {
658                                                         Debug( LDAP_DEBUG_ANY,
659                                                             "%s: line %d: unable to parse limit \"%s\" in \"timelimit <limit>\" line\n",
660                                                             fname, lineno, cargv[i] );
661                                                         return( 1 );
662
663                                                 } else if ( next[0] != '\0' ) {
664                                                         Debug( LDAP_DEBUG_ANY,
665                                                             "%s: line %d: trailing chars \"%s\" in \"timelimit <limit>\" line ignored\n",
666                                                             fname, lineno, next );
667                                                 }
668                                         }
669                                         lim->lms_t_hard = 0;
670                                 }
671                         }
672
673                 /* set regex-based limits */
674                 } else if ( strcasecmp( cargv[0], "limits" ) == 0 ) {
675                         if ( be == NULL ) {
676                                 Debug( LDAP_DEBUG_ANY,
677         "%s: line %d \"limits\" allowed only in database environment.\n%s",
678                                         fname, lineno, "" );
679                                 return( 1 );
680                         }
681
682                         if ( limits_parse( be, fname, lineno, cargc, cargv ) ) {
683                                 return( 1 );
684                         }
685
686                 /* mark this as a subordinate database */
687                 } else if ( strcasecmp( cargv[0], "subordinate" ) == 0 ) {
688                         if ( be == NULL ) {
689                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: subordinate keyword "
690                                         "must appear inside a database definition.\n",
691                                     fname, lineno, 0 );
692                                 return 1;
693
694                         } else {
695                                 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_GLUE_SUBORDINATE;
696                                 num_subordinates++;
697                         }
698
699                 /* add an overlay to this backend */
700                 } else if ( strcasecmp( cargv[0], "overlay" ) == 0 ) {
701                         if ( be == NULL ) {
702                                 if ( cargv[1][0] == '-' && overlay_config( frontendDB, &cargv[1][1] ) ) {
703                                         /* log error */
704                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
705                                                 "(optional) global overlay \"%s\" configuration "
706                                                 "failed (ignored)\n", fname, lineno, &cargv[1][1] );
707                                 } else if ( overlay_config( frontendDB, cargv[1] ) ) {
708                                         return 1;
709                                 }
710
711                         } else {
712                                 if ( cargv[1][0] == '-' && overlay_config( be, &cargv[1][1] ) ) {
713                                         /* log error */
714                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
715                                                 "(optional) overlay \"%s\" configuration "
716                                                 "failed (ignored)\n", fname, lineno, &cargv[1][1] );
717                                 } else if ( overlay_config( be, cargv[1] ) ) {
718                                         return 1;
719                                 }
720                         }
721
722                 /* set database suffix */
723                 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
724                         Backend *tmp_be;
725                         struct berval dn, pdn, ndn;
726
727                         if ( cargc < 2 ) {
728                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
729                                         "missing dn in \"suffix <dn>\" line\n",
730                                     fname, lineno, 0 );
731
732                                 return( 1 );
733
734                         } else if ( cargc > 2 ) {
735                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: extra cruft "
736                                         "after <dn> in \"suffix %s\" line (ignored)\n",
737                                     fname, lineno, cargv[1] );
738                         }
739
740                         if ( be == NULL ) {
741                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: suffix line "
742                                         "must appear inside a database definition\n",
743                                     fname, lineno, 0 );
744                                 return( 1 );
745
746 #if defined(SLAPD_MONITOR_DN)
747                         /* "cn=Monitor" is reserved for monitoring slap */
748                         } else if ( strcasecmp( cargv[1], SLAPD_MONITOR_DN ) == 0 ) {
749                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: \""
750                                         "%s\" is reserved for monitoring slapd\n", 
751                                         fname, lineno, SLAPD_MONITOR_DN );
752                                 return( 1 );
753 #endif /* SLAPD_MONITOR_DN */
754                         }
755
756                         if ( load_ucdata( NULL ) < 0 ) return 1;
757
758                         dn.bv_val = cargv[1];
759                         dn.bv_len = strlen( cargv[1] );
760
761                         rc = dnPrettyNormal( NULL, &dn, &pdn, &ndn, NULL );
762                         if( rc != LDAP_SUCCESS ) {
763                                 Debug( LDAP_DEBUG_ANY,
764                                         "%s: line %d: suffix DN is invalid\n",
765                                    fname, lineno, 0 );
766                                 return( 1 );
767                         }
768
769                         tmp_be = select_backend( &ndn, 0, 0 );
770                         if ( tmp_be == be ) {
771                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: suffix "
772                                         "already served by this backend (ignored)\n",
773                                     fname, lineno, 0 );
774                                 free( pdn.bv_val );
775                                 free( ndn.bv_val );
776
777                         } else if ( tmp_be  != NULL ) {
778                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: suffix "
779                                         "already served by a preceeding backend \"%s\"\n",
780                                     fname, lineno, tmp_be->be_suffix[0].bv_val );
781                                 free( pdn.bv_val );
782                                 free( ndn.bv_val );
783                                 return( 1 );
784
785                         } else if( pdn.bv_len == 0 && default_search_nbase.bv_len ) {
786                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
787                                                 "suffix DN empty and default "
788                                                 "search base provided \"%s\" (assuming okay)\n",
789                                         fname, lineno, default_search_base.bv_val );
790                         }
791
792                         ber_bvarray_add( &be->be_suffix, &pdn );
793                         ber_bvarray_add( &be->be_nsuffix, &ndn );
794
795                /* set max deref depth */
796                } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
797                                         int i;
798                        if ( cargc < 2 ) {
799                                Debug( LDAP_DEBUG_ANY,
800                    "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
801                                    fname, lineno, 0 );
802
803                                return( 1 );
804                        }
805                        if ( be == NULL ) {
806                                Debug( LDAP_DEBUG_ANY,
807 "%s: line %d: depth line must appear inside a database definition.\n",
808                                    fname, lineno, 0 );
809                                 return 1;
810                        }
811
812                        i = strtol( cargv[1], &next, 10 );
813                        if ( next == NULL || next[0] != '\0' ) {
814                                Debug( LDAP_DEBUG_ANY,
815                                           "%s: line %d: unable to parse depth \"%s\" in \"maxDerefDepth <depth>\" "
816                                           "line.\n", fname, lineno, cargv[1] );
817                                 return 1;
818                        }
819
820                        if (i < 0) {
821                                Debug( LDAP_DEBUG_ANY,
822 "%s: line %d: depth must be positive.\n",
823                                    fname, lineno, 0 );
824                                 return 1;
825
826
827                        }
828                        be->be_max_deref_depth = i;
829
830                 /* set magic "root" dn for this database */
831                 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
832                         if ( cargc < 2 ) {
833                                 Debug( LDAP_DEBUG_ANY,
834                     "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
835                                     fname, lineno, 0 );
836
837                                 return( 1 );
838                         }
839
840                         if ( be == NULL ) {
841                                 Debug( LDAP_DEBUG_ANY,
842 "%s: line %d: rootdn line must appear inside a database definition.\n",
843                                     fname, lineno, 0 );
844                                 return 1;
845
846                         } else {
847                                 struct berval dn;
848                                 
849                                 if ( load_ucdata( NULL ) < 0 ) return 1;
850
851                                 dn.bv_val = cargv[1];
852                                 dn.bv_len = strlen( cargv[1] );
853
854                                 rc = dnPrettyNormal( NULL, &dn,
855                                         &be->be_rootdn,
856                                         &be->be_rootndn, NULL );
857
858                                 if( rc != LDAP_SUCCESS ) {
859                                         Debug( LDAP_DEBUG_ANY,
860                                                 "%s: line %d: rootdn DN is invalid\n",
861                                            fname, lineno, 0 );
862                                         return( 1 );
863                                 }
864                         }
865
866                 /* set super-secret magic database password */
867                 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
868                         if ( cargc < 2 ) {
869                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
870                                         "missing passwd in \"rootpw <passwd>\" line\n",
871                                     fname, lineno, 0 );
872
873                                 return( 1 );
874                         }
875
876                         if ( be == NULL ) {
877                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
878                                         "rootpw line must appear inside a database "
879                                         "definition.\n",
880                                     fname, lineno, 0 );
881                                 return 1;
882
883                         } else {
884                                 Backend *tmp_be = select_backend( &be->be_rootndn, 0, 0 );
885
886                                 if( tmp_be != be ) {
887                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
888                                                 "rootpw can only be set when rootdn is under suffix\n",
889                                         fname, lineno, 0 );
890                                         return 1;
891                                 }
892
893                                 be->be_rootpw.bv_val = ch_strdup( cargv[1] );
894                                 be->be_rootpw.bv_len = strlen( be->be_rootpw.bv_val );
895                         }
896
897                 /* make this database read-only */
898                 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
899                         if ( cargc < 2 ) {
900                                 Debug( LDAP_DEBUG_ANY,
901             "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
902                                     fname, lineno, 0 );
903
904                                 return( 1 );
905                         }
906                         if ( be == NULL ) {
907                                 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
908                                         frontendDB->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
909                                 } else {
910                                         frontendDB->be_restrictops &= ~SLAP_RESTRICT_OP_WRITES;
911                                 }
912
913                         } else {
914                                 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
915                                         be->be_restrictops |= SLAP_RESTRICT_OP_WRITES;
916                                 } else {
917                                         be->be_restrictops &= ~SLAP_RESTRICT_OP_WRITES;
918                                 }
919                         }
920
921                 /* restricts specific operations */
922                 } else if ( strcasecmp( cargv[0], "restrict" ) == 0 ) {
923                         slap_mask_t     restrictops = 0;
924                         struct restrictable_exops_t {
925                                 char    *name;
926                                 int     flag;
927                         } restrictable_exops[] = {
928                                 { LDAP_EXOP_START_TLS,          SLAP_RESTRICT_EXOP_START_TLS },
929                                 { LDAP_EXOP_MODIFY_PASSWD,      SLAP_RESTRICT_EXOP_MODIFY_PASSWD },
930                                 { LDAP_EXOP_X_WHO_AM_I,         SLAP_RESTRICT_EXOP_WHOAMI },
931                                 { LDAP_EXOP_X_CANCEL,           SLAP_RESTRICT_EXOP_CANCEL },
932                                 { NULL,                         0 }
933                         };
934                         int i;
935
936                         if ( cargc < 2 ) {
937                                 Debug( LDAP_DEBUG_ANY,
938                                         "%s: line %d: missing <op_list> in \"restrict <op_list>\" "
939                                         "line.\n", fname, lineno, 0 );
940                                 return 1;
941                         }
942
943                         for ( i = 1; i < cargc; i++ ) {
944                                 if ( strcasecmp( cargv[ i ], "read" ) == 0 ) {
945                                         restrictops |= SLAP_RESTRICT_OP_READS;
946
947                                 } else if ( strcasecmp( cargv[ i ], "write" ) == 0 ) {
948                                         restrictops |= SLAP_RESTRICT_OP_WRITES;
949
950                                 } else if ( strcasecmp( cargv[ i ], "add" ) == 0 ) {
951                                         restrictops |= SLAP_RESTRICT_OP_ADD;
952
953                                 } else if ( strcasecmp( cargv[ i ], "bind" ) == 0 ) {
954                                         restrictops |= SLAP_RESTRICT_OP_BIND;
955
956                                 } else if ( strcasecmp( cargv[ i ], "compare" ) == 0 ) {
957                                         restrictops |= SLAP_RESTRICT_OP_COMPARE;
958
959                                 } else if ( strcasecmp( cargv[ i ], "delete" ) == 0 ) {
960                                         restrictops |= SLAP_RESTRICT_OP_DELETE;
961
962                                 } else if ( strncasecmp( cargv[ i ], "extended",
963                                         STRLENOF( "extended" ) ) == 0 )
964                                 {
965                                         char    *e = cargv[ i ] + STRLENOF( "extended" );
966
967                                         if ( e[0] == '=' ) {
968                                                 int     j;
969
970                                                 e++;
971                                                 for ( j = 0; restrictable_exops[ j ].name; j++ ) {
972                                                         if ( strcmp( e, restrictable_exops[j].name ) == 0 )
973                                                         {
974                                                                 restrictops |= restrictable_exops[ j ].flag;
975                                                                 break;
976                                                         }
977                                                 }
978
979                                                 if ( restrictable_exops[ j ].name == NULL ) {
980                                                         goto restrict_unknown;
981                                                 }
982
983                                                 restrictops &= ~SLAP_RESTRICT_OP_EXTENDED;
984
985                                         } else if ( e[0] == '\0' ) {
986                                                 restrictops &= ~SLAP_RESTRICT_EXOP_MASK;
987                                                 restrictops |= SLAP_RESTRICT_OP_EXTENDED;
988                                                 
989                                         } else {
990                                                 goto restrict_unknown;
991                                         }
992
993                                 } else if ( strcasecmp( cargv[ i ], "modify" ) == 0 ) {
994                                         restrictops |= SLAP_RESTRICT_OP_MODIFY;
995
996                                 } else if ( strcasecmp( cargv[ i ], "rename" ) == 0
997                                         || strcasecmp( cargv[ i ], "modrdn" ) == 0 )
998                                 {
999                                         restrictops |= SLAP_RESTRICT_OP_RENAME;
1000
1001                                 } else if ( strcasecmp( cargv[ i ], "search" ) == 0 ) {
1002                                         restrictops |= SLAP_RESTRICT_OP_SEARCH;
1003
1004                                 } else {
1005 restrict_unknown:;
1006
1007                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1008                                                 "unknown operation %s in \"allow <features>\" line\n",
1009                                                 fname, lineno, cargv[i] );
1010                                         return 1;
1011                                 }
1012                         }
1013
1014                         if ( be == NULL ) {
1015                                 frontendDB->be_restrictops |= restrictops;
1016                         } else {
1017                                 be->be_restrictops |= restrictops;
1018                         }
1019
1020                 /* allow these features */
1021                 } else if ( strcasecmp( cargv[0], "allows" ) == 0 ||
1022                         strcasecmp( cargv[0], "allow" ) == 0 )
1023                 {
1024                         slap_mask_t     allows = 0;
1025
1026                         if ( be != NULL ) {
1027                                 Debug( LDAP_DEBUG_ANY,
1028 "%s: line %d: allow line must appear prior to database definitions\n",
1029                                     fname, lineno, 0 );
1030
1031                         }
1032
1033                         if ( cargc < 2 ) {
1034                                 Debug( LDAP_DEBUG_ANY,
1035             "%s: line %d: missing feature(s) in \"allow <features>\" line\n",
1036                                     fname, lineno, 0 );
1037
1038                                 return( 1 );
1039                         }
1040
1041                         for( i=1; i < cargc; i++ ) {
1042                                 if( strcasecmp( cargv[i], "bind_v2" ) == 0 ) {
1043                                         allows |= SLAP_ALLOW_BIND_V2;
1044
1045                                 } else if( strcasecmp( cargv[i], "bind_anon_cred" ) == 0 ) {
1046                                         allows |= SLAP_ALLOW_BIND_ANON_CRED;
1047
1048                                 } else if( strcasecmp( cargv[i], "bind_anon_dn" ) == 0 ) {
1049                                         allows |= SLAP_ALLOW_BIND_ANON_DN;
1050
1051                                 } else if( strcasecmp( cargv[i], "update_anon" ) == 0 ) {
1052                                         allows |= SLAP_ALLOW_UPDATE_ANON;
1053
1054                                 } else {
1055                                         Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1056                                                 "unknown feature %s in \"allow <features>\" line\n",
1057                                                 fname, lineno, cargv[i] );
1058
1059                                         return 1;
1060                                 }
1061                         }
1062
1063                         global_allows |= allows;
1064
1065                 /* disallow these features */
1066                 } else if ( strcasecmp( cargv[0], "disallows" ) == 0 ||
1067                         strcasecmp( cargv[0], "disallow" ) == 0 )
1068                 {
1069                         slap_mask_t     disallows = 0; 
1070
1071                         if ( be != NULL ) {
1072                                 Debug( LDAP_DEBUG_ANY,
1073 "%s: line %d: disallow line must appear prior to database definitions\n",
1074                                     fname, lineno, 0 );
1075
1076                         }
1077
1078                         if ( cargc < 2 ) {
1079                                 Debug( LDAP_DEBUG_ANY,
1080             "%s: line %d: missing feature(s) in \"disallow <features>\" line\n",
1081                                     fname, lineno, 0 );
1082
1083                                 return( 1 );
1084                         }
1085
1086                         for( i=1; i < cargc; i++ ) {
1087                                 if( strcasecmp( cargv[i], "bind_anon" ) == 0 ) {
1088                                         disallows |= SLAP_DISALLOW_BIND_ANON;
1089
1090                                 } else if( strcasecmp( cargv[i], "bind_simple" ) == 0 ) {
1091                                         disallows |= SLAP_DISALLOW_BIND_SIMPLE;
1092
1093                                 } else if( strcasecmp( cargv[i], "bind_krbv4" ) == 0 ) {
1094                                         disallows |= SLAP_DISALLOW_BIND_KRBV4;
1095
1096                                 } else if( strcasecmp( cargv[i], "tls_2_anon" ) == 0 ) {
1097                                         disallows |= SLAP_DISALLOW_TLS_2_ANON;
1098
1099                                 } else if( strcasecmp( cargv[i], "tls_authc" ) == 0 ) {
1100                                         disallows |= SLAP_DISALLOW_TLS_AUTHC;
1101
1102                                 } else {
1103                                         Debug( LDAP_DEBUG_ANY,
1104                     "%s: line %d: unknown feature %s in \"disallow <features>\" line\n",
1105                                             fname, lineno, cargv[i] );
1106
1107                                         return 1;
1108                                 }
1109                         }
1110
1111                         global_disallows |= disallows;
1112
1113                 /* require these features */
1114                 } else if ( strcasecmp( cargv[0], "requires" ) == 0 ||
1115                         strcasecmp( cargv[0], "require" ) == 0 )
1116                 {
1117                         slap_mask_t     requires = 0; 
1118
1119                         if ( cargc < 2 ) {
1120                                 Debug( LDAP_DEBUG_ANY,
1121             "%s: line %d: missing feature(s) in \"require <features>\" line\n",
1122                                     fname, lineno, 0 );
1123
1124                                 return( 1 );
1125                         }
1126
1127                         for( i=1; i < cargc; i++ ) {
1128                                 if( strcasecmp( cargv[i], "bind" ) == 0 ) {
1129                                         requires |= SLAP_REQUIRE_BIND;
1130
1131                                 } else if( strcasecmp( cargv[i], "LDAPv3" ) == 0 ) {
1132                                         requires |= SLAP_REQUIRE_LDAP_V3;
1133
1134                                 } else if( strcasecmp( cargv[i], "authc" ) == 0 ) {
1135                                         requires |= SLAP_REQUIRE_AUTHC;
1136
1137                                 } else if( strcasecmp( cargv[i], "SASL" ) == 0 ) {
1138                                         requires |= SLAP_REQUIRE_SASL;
1139
1140                                 } else if( strcasecmp( cargv[i], "strong" ) == 0 ) {
1141                                         requires |= SLAP_REQUIRE_STRONG;
1142
1143                                 } else if( strcasecmp( cargv[i], "none" ) != 0 ) {
1144                                         Debug( LDAP_DEBUG_ANY,
1145                     "%s: line %d: unknown feature %s in \"require <features>\" line\n",
1146                                             fname, lineno, cargv[i] );
1147
1148                                         return( 1 );
1149                                 }
1150                         }
1151
1152                         if ( be == NULL ) {
1153                                 frontendDB->be_requires = requires;
1154                         } else {
1155                                 be->be_requires = requires;
1156                         }
1157
1158                 } else if ( strcasecmp( cargv[0], "security" ) == 0 ) {
1159                         slap_ssf_set_t *set;
1160
1161                         if ( cargc < 2 ) {
1162                                 Debug( LDAP_DEBUG_ANY,
1163             "%s: line %d: missing factor(s) in \"security <factors>\" line\n",
1164                                     fname, lineno, 0 );
1165
1166                                 return( 1 );
1167                         }
1168
1169                         if ( be == NULL ) {
1170                                 set = &frontendDB->be_ssf_set;
1171                         } else {
1172                                 set = &be->be_ssf_set;
1173                         }
1174
1175                         for( i=1; i < cargc; i++ ) {
1176                                 slap_ssf_t      *tgt;
1177                                 char            *src;
1178
1179                                 if ( strncasecmp( cargv[i], "ssf=",
1180                                                 STRLENOF("ssf=") ) == 0 )
1181                                 {
1182                                         tgt = &set->sss_ssf;
1183                                         src = &cargv[i][STRLENOF("ssf=")];
1184
1185                                 } else if ( strncasecmp( cargv[i], "transport=",
1186                                                 STRLENOF("transport=") ) == 0 )
1187                                 {
1188                                         tgt = &set->sss_transport;
1189                                         src = &cargv[i][STRLENOF("transport=")];
1190
1191                                 } else if ( strncasecmp( cargv[i], "tls=",
1192                                                 STRLENOF("tls=") ) == 0 )
1193                                 {
1194                                         tgt = &set->sss_tls;
1195                                         src = &cargv[i][STRLENOF("tls=")];
1196
1197                                 } else if ( strncasecmp( cargv[i], "sasl=",
1198                                                 STRLENOF("sasl=") ) == 0 )
1199                                 {
1200                                         tgt = &set->sss_sasl;
1201                                         src = &cargv[i][STRLENOF("sasl=")];
1202
1203                                 } else if ( strncasecmp( cargv[i], "update_ssf=",
1204                                                 STRLENOF("update_ssf=") ) == 0 )
1205                                 {
1206                                         tgt = &set->sss_update_ssf;
1207                                         src = &cargv[i][STRLENOF("update_ssf=")];
1208
1209                                 } else if ( strncasecmp( cargv[i], "update_transport=",
1210                                                 STRLENOF("update_transport=") ) == 0 )
1211                                 {
1212                                         tgt = &set->sss_update_transport;
1213                                         src = &cargv[i][STRLENOF("update_transport=")];
1214
1215                                 } else if ( strncasecmp( cargv[i], "update_tls=",
1216                                                 STRLENOF("update_tls=") ) == 0 )
1217                                 {
1218                                         tgt = &set->sss_update_tls;
1219                                         src = &cargv[i][STRLENOF("update_tls=")];
1220
1221                                 } else if ( strncasecmp( cargv[i], "update_sasl=",
1222                                                 STRLENOF("update_sasl=") ) == 0 )
1223                                 {
1224                                         tgt = &set->sss_update_sasl;
1225                                         src = &cargv[i][STRLENOF("update_sasl=")];
1226
1227                                 } else if ( strncasecmp( cargv[i], "simple_bind=",
1228                                                 STRLENOF("simple_bind=") ) == 0 )
1229                                 {
1230                                         tgt = &set->sss_simple_bind;
1231                                         src = &cargv[i][STRLENOF("simple_bind=")];
1232
1233                                 } else {
1234                                         Debug( LDAP_DEBUG_ANY,
1235                     "%s: line %d: unknown factor %s in \"security <factors>\" line\n",
1236                                             fname, lineno, cargv[i] );
1237
1238                                         return( 1 );
1239                                 }
1240
1241                                 *tgt = strtol( src, &next, 10 );
1242                                 if ( next == NULL || next[0] != '\0' ) {
1243                                         Debug( LDAP_DEBUG_ANY,
1244                     "%s: line %d: unable to parse factor \"%s\" in \"security <factors>\" line\n",
1245                                             fname, lineno, cargv[i] );
1246
1247                                         return( 1 );
1248                                 }
1249                         }
1250
1251                 /* where to send clients when we don't hold it */
1252                 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
1253                         if ( cargc < 2 ) {
1254                                 Debug( LDAP_DEBUG_ANY,
1255                     "%s: line %d: missing URL in \"referral <URL>\" line\n",
1256                                     fname, lineno, 0 );
1257
1258                                 return( 1 );
1259                         }
1260
1261                         if( validate_global_referral( cargv[1] ) ) {
1262                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1263                                         "invalid URL (%s) in \"referral\" line.\n",
1264                                     fname, lineno, cargv[1] );
1265                                 return 1;
1266                         }
1267
1268                         vals[0].bv_val = cargv[1];
1269                         vals[0].bv_len = strlen( vals[0].bv_val );
1270                         if( value_add( &default_referral, vals ) )
1271                                 return LDAP_OTHER;
1272
1273                 /* start of a new database definition */
1274                 } else if ( strcasecmp( cargv[0], "debug" ) == 0 ) {
1275                         int level;
1276                         if ( cargc < 3 ) {
1277                                 Debug( LDAP_DEBUG_ANY,
1278                                         "%s: line %d: Error in debug directive, \"debug subsys level\"\n",
1279                                         fname, lineno, 0 );
1280                                 return( 1 );
1281                         }
1282                         level = strtol( cargv[2], &next, 10 );
1283                         if ( next == NULL || next[0] != '\0' ){
1284                                 Debug( LDAP_DEBUG_ANY,
1285                                            "%s: line %d: unable to parse level \"%s\" in debug directive, "
1286                                            "\"debug <subsys> <level>\"\n", fname, lineno , cargv[2] );
1287                                 return( 1 );
1288                         }
1289
1290                         if ( level <= 0 ) level = lutil_mnem2level( cargv[2] );
1291                         lutil_set_debug_level( cargv[1], level );
1292                 /* specify an Object Identifier macro */
1293                 } else if ( strcasecmp( cargv[0], "objectidentifier" ) == 0 ) {
1294                         rc = parse_oidm( fname, lineno, cargc, cargv );
1295                         if( rc ) return rc;
1296
1297                 /* specify an objectclass */
1298                 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
1299                         if ( cargc < 2 ) {
1300                                 Debug( LDAP_DEBUG_ANY,
1301                                        "%s: line %d: illegal objectclass format.\n",
1302                                        fname, lineno, 0 );
1303                                 return( 1 );
1304
1305                         } else if ( *cargv[1] == '('  /*')'*/) {
1306                                 char * p;
1307                                 p = strchr(saveline,'(' /*')'*/);
1308                                 rc = parse_oc( fname, lineno, p, cargv );
1309                                 if( rc ) return rc;
1310
1311                         } else {
1312                                 Debug( LDAP_DEBUG_ANY,
1313                                        "%s: line %d: old objectclass format not supported.\n",
1314                                        fname, lineno, 0 );
1315                         }
1316
1317                 } else if ( strcasecmp( cargv[0], "ditcontentrule" ) == 0 ) {
1318                         char * p;
1319                         p = strchr(saveline,'(' /*')'*/);
1320                         rc = parse_cr( fname, lineno, p, cargv );
1321                         if( rc ) return rc;
1322
1323                 /* specify an attribute type */
1324                 } else if (( strcasecmp( cargv[0], "attributetype" ) == 0 )
1325                         || ( strcasecmp( cargv[0], "attribute" ) == 0 ))
1326                 {
1327                         if ( cargc < 2 ) {
1328                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1329                                         "illegal attribute type format.\n",
1330                                         fname, lineno, 0 );
1331                                 return( 1 );
1332
1333                         } else if ( *cargv[1] == '(' /*')'*/) {
1334                                 char * p;
1335                                 p = strchr(saveline,'(' /*')'*/);
1336                                 rc = parse_at( fname, lineno, p, cargv );
1337                                 if( rc ) return rc;
1338
1339                         } else {
1340                                 Debug( LDAP_DEBUG_ANY,
1341     "%s: line %d: old attribute type format not supported.\n",
1342                                     fname, lineno, 0 );
1343
1344                         }
1345
1346                 /* define attribute option(s) */
1347                 } else if ( strcasecmp( cargv[0], "attributeoptions" ) == 0 ) {
1348                         ad_define_option( NULL, NULL, 0 );
1349                         for ( i = 1; i < cargc; i++ )
1350                                 if ( ad_define_option( cargv[i], fname, lineno ) != 0 )
1351                                         return 1;
1352
1353                 /* turn on/off schema checking */
1354                 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
1355                         if ( cargc < 2 ) {
1356                                 Debug( LDAP_DEBUG_ANY,
1357     "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
1358                                     fname, lineno, 0 );
1359
1360                                 return( 1 );
1361                         }
1362                         if ( strcasecmp( cargv[1], "off" ) == 0 ) {
1363                                 Debug( LDAP_DEBUG_ANY,
1364                                         "%s: line %d: schema checking disabled! your mileage may vary!\n",
1365                                     fname, lineno, 0 );
1366                                 global_schemacheck = 0;
1367                         } else {
1368                                 global_schemacheck = 1;
1369                         }
1370
1371                 /* specify access control info */
1372                 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
1373                         parse_acl( be, fname, lineno, cargc, cargv );
1374
1375                 /* debug level to log things to syslog */
1376                 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
1377                         if ( cargc < 2 ) {
1378                                 Debug( LDAP_DEBUG_ANY,
1379                     "%s: line %d: missing level(s) in \"loglevel <level> [...]\" line\n",
1380                                     fname, lineno, 0 );
1381
1382                                 return( 1 );
1383                         }
1384
1385                         ldap_syslog = 0;
1386
1387                         for( i=1; i < cargc; i++ ) {
1388                                 int     level;
1389
1390                                 if ( isdigit( cargv[i][0] ) ) {
1391                                         level = strtol( cargv[i], &next, 10 );
1392                                         if ( next == NULL || next[0] != '\0' ) {
1393                                                 Debug( LDAP_DEBUG_ANY,
1394                                                         "%s: line %d: unable to parse level \"%s\" "
1395                                                         "in \"loglevel <level> [...]\" line.\n",
1396                                                         fname, lineno , cargv[i] );
1397                                                 return( 1 );
1398                                         }
1399                                         
1400                                 } else {
1401                                         static struct {
1402                                                 int     i;
1403                                                 char    *s;
1404                                         } int_2_level[] = {
1405                                                 { LDAP_DEBUG_TRACE,     "Trace"         },
1406                                                 { LDAP_DEBUG_PACKETS,   "Packets"       },
1407                                                 { LDAP_DEBUG_ARGS,      "Args"          },
1408                                                 { LDAP_DEBUG_CONNS,     "Conns"         },
1409                                                 { LDAP_DEBUG_BER,       "BER"           },
1410                                                 { LDAP_DEBUG_FILTER,    "Filter"        },
1411                                                 { LDAP_DEBUG_CONFIG,    "Config"        },
1412                                                 { LDAP_DEBUG_ACL,       "ACL"           },
1413                                                 { LDAP_DEBUG_STATS,     "Stats"         },
1414                                                 { LDAP_DEBUG_STATS2,    "Stats2"        },
1415                                                 { LDAP_DEBUG_SHELL,     "Shell"         },
1416                                                 { LDAP_DEBUG_PARSE,     "Parse"         },
1417                                                 { LDAP_DEBUG_CACHE,     "Cache"         },
1418                                                 { LDAP_DEBUG_INDEX,     "Index"         },
1419                                                 { -1,                   "Any"           },
1420                                                 { 0,                    NULL            }
1421                                         };
1422                                         int     j;
1423
1424                                         for ( j = 0; int_2_level[j].s; j++ ) {
1425                                                 if ( strcasecmp( cargv[i], int_2_level[j].s ) == 0 ) {
1426                                                         level = int_2_level[j].i;
1427                                                         break;
1428                                                 }
1429                                         }
1430
1431                                         if ( int_2_level[j].s == NULL ) {
1432                                                 Debug( LDAP_DEBUG_ANY,
1433                                                         "%s: line %d: unknown level \"%s\" "
1434                                                         "in \"loglevel <level> [...]\" line.\n",
1435                                                         fname, lineno , cargv[i] );
1436                                                 return( 1 );
1437                                         }
1438                                 }
1439
1440                                 ldap_syslog |= level;
1441                         }
1442
1443                 /* list of sync replication information in this backend (slave only) */
1444                 } else if ( strcasecmp( cargv[0], "syncrepl" ) == 0 ) {
1445
1446                         if ( be == NULL ) {
1447                                 Debug( LDAP_DEBUG_ANY,
1448                                             "%s: line %d: syncrepl line must appear inside "
1449                                             "a database definition.\n", fname, lineno, 0);
1450                                 return 1;
1451
1452                         } else if ( SLAP_SHADOW( be )) {
1453                                 Debug( LDAP_DEBUG_ANY,
1454                                         "%s: line %d: syncrepl: database already shadowed.\n",
1455                                         fname, lineno, 0);
1456                                 return 1;
1457
1458                         } else if ( add_syncrepl( be, cargv, cargc )) {
1459                                 return 1;
1460                         }
1461
1462                         SLAP_DBFLAGS(be) |= ( SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SYNC_SHADOW );
1463
1464                 /* list of replicas of the data in this backend (master only) */
1465                 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
1466                         if ( cargc < 2 ) {
1467                                 Debug( LDAP_DEBUG_ANY,
1468             "%s: line %d: missing host or uri in \"replica <host[:port]>\" line\n",
1469                                     fname, lineno, 0 );
1470
1471                                 return( 1 );
1472                         }
1473                         if ( be == NULL ) {
1474                                 Debug( LDAP_DEBUG_ANY,
1475 "%s: line %d: replica line must appear inside a database definition\n",
1476                                     fname, lineno, 0 );
1477                                 return 1;
1478
1479                         } else {
1480                                 int nr = -1;
1481
1482                                 for ( i = 1; i < cargc; i++ ) {
1483                                         if ( strncasecmp( cargv[i], "host=", 5 )
1484                                             == 0 ) {
1485                                                 nr = add_replica_info( be, 
1486                                                         cargv[i] + 5 );
1487                                                 break;
1488                                         } else if (strncasecmp( cargv[i], "uri=", 4 )
1489                                             == 0 ) {
1490                                             if ( ldap_url_parse( cargv[ i ] + 4, &ludp )
1491                                                 != LDAP_SUCCESS ) {
1492                                                         Debug( LDAP_DEBUG_ANY,
1493                                                         "%s: line %d: replica line contains invalid "
1494                                                         "uri definition.\n", fname, lineno, 0);
1495                                                         return 1;
1496                                                 }
1497                                                 if (ludp->lud_host == NULL ) {
1498                                                         Debug( LDAP_DEBUG_ANY,
1499                                                         "%s: line %d: replica line contains invalid "
1500                                                         "uri definition - missing hostname.\n", fname, lineno, 0);
1501                                                         return 1;
1502                                                 }
1503                                         replicahost = ch_malloc( strlen( cargv[ i ] ) );
1504                                                 if ( replicahost == NULL ) {
1505                                                         Debug( LDAP_DEBUG_ANY, 
1506                                                         "out of memory in read_config\n", 0, 0, 0 );
1507                                                         ldap_free_urldesc( ludp );                              
1508                                                         exit( EXIT_FAILURE );
1509                                                 }
1510                                                 sprintf(replicahost, "%s:%d", 
1511                                                         ludp->lud_host, ludp->lud_port);
1512                                                 nr = add_replica_info( be, replicahost );
1513                                                 ldap_free_urldesc( ludp );                              
1514                                                 ch_free(replicahost);
1515                                                 break;
1516                                         }
1517                                 }
1518                                 if ( i == cargc ) {
1519                                         Debug( LDAP_DEBUG_ANY,
1520                     "%s: line %d: missing host or uri in \"replica\" line\n",
1521                                             fname, lineno, 0 );
1522                                         return 1;
1523
1524                                 } else if ( nr == -1 ) {
1525                                         Debug( LDAP_DEBUG_ANY,
1526                 "%s: line %d: unable to add replica \"%s\"\n",
1527                                                 fname, lineno, cargv[i] + 5 );
1528                                         return 1;
1529                                 } else {
1530                                         for ( i = 1; i < cargc; i++ ) {
1531                                                 if ( strncasecmp( cargv[i], "suffix=", 7 ) == 0 ) {
1532
1533                                                         switch ( add_replica_suffix( be, nr, cargv[i] + 7 ) ) {
1534                                                         case 1:
1535                                                                 Debug( LDAP_DEBUG_ANY,
1536                                                                                 "%s: line %d: suffix \"%s\" in \"replica\" line is not valid for backend (ignored)\n",
1537                                                                                 fname, lineno, cargv[i] + 7 );
1538                                                                 break;
1539
1540                                                         case 2:
1541                                                                 Debug( LDAP_DEBUG_ANY,
1542                                                                                  "%s: line %d: unable to normalize suffix in \"replica\" line (ignored)\n",
1543                                                                                  fname, lineno, 0 );
1544                                                                 break;
1545                                                         }
1546
1547                                                 } else if ( strncasecmp( cargv[i], "attr", 4 ) == 0 ) {
1548                                                         int exclude = 0;
1549                                                         char *arg = cargv[i] + 4;
1550
1551                                                         if ( arg[0] == '!' ) {
1552                                                                 arg++;
1553                                                                 exclude = 1;
1554                                                         }
1555
1556                                                         if ( arg[0] != '=' ) {
1557                                                                 continue;
1558                                                         }
1559
1560                                                         if ( add_replica_attrs( be, nr, arg + 1, exclude ) ) {
1561                                                                 Debug( LDAP_DEBUG_ANY,
1562                                                                                 "%s: line %d: attribute \"%s\" in \"replica\" line is unknown\n",
1563                                                                                 fname, lineno, arg + 1 );
1564                                                                 return( 1 );
1565                                                         }
1566                                                 }
1567                                         }
1568                                 }
1569                         }
1570
1571                 } else if ( strcasecmp( cargv[0], "replicationInterval" ) == 0 ) {
1572                         /* ignore */
1573
1574                 /* dn of slave entity allowed to write to replica */
1575                 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
1576                         if ( cargc < 2 ) {
1577                                 Debug( LDAP_DEBUG_ANY,
1578                     "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
1579                                     fname, lineno, 0 );
1580
1581                                 return( 1 );
1582                         }
1583                         if ( be == NULL ) {
1584                                 Debug( LDAP_DEBUG_ANY,
1585 "%s: line %d: updatedn line must appear inside a database definition\n",
1586                                     fname, lineno, 0 );
1587                                 return 1;
1588
1589                         } else if ( SLAP_SHADOW(be) ) {
1590                                 Debug( LDAP_DEBUG_ANY,
1591                                         "%s: line %d: updatedn: database already shadowed.\n",
1592                                         fname, lineno, 0);
1593                                 return 1;
1594
1595                         } else {
1596                                 struct berval dn;
1597
1598                                 if ( load_ucdata( NULL ) < 0 ) return 1;
1599
1600                                 dn.bv_val = cargv[1];
1601                                 dn.bv_len = strlen( cargv[1] );
1602
1603                                 rc = dnNormalize( 0, NULL, NULL, &dn, &be->be_update_ndn, NULL );
1604                                 if( rc != LDAP_SUCCESS ) {
1605                                         Debug( LDAP_DEBUG_ANY,
1606                                                 "%s: line %d: updatedn DN is invalid\n",
1607                                             fname, lineno, 0 );
1608                                         return 1;
1609                                 }
1610
1611                         }
1612                         SLAP_DBFLAGS(be) |= ( SLAP_DBFLAG_SHADOW | SLAP_DBFLAG_SLURP_SHADOW );
1613
1614                 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
1615                         if ( cargc < 2 ) {
1616                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1617                                         "missing url in \"updateref <ldapurl>\" line\n",
1618                                     fname, lineno, 0 );
1619
1620                                 return( 1 );
1621                         }
1622                         if ( be == NULL ) {
1623                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: updateref"
1624                                         " line must appear inside a database definition\n",
1625                                         fname, lineno, 0 );
1626                                 return 1;
1627
1628                         } else if ( !SLAP_SHADOW(be) ) {
1629                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1630                                         "updateref line must after syncrepl or updatedn.\n",
1631                                     fname, lineno, 0 );
1632                                 return 1;
1633                         }
1634
1635                         if( validate_global_referral( cargv[1] ) ) {
1636                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1637                                         "invalid URL (%s) in \"updateref\" line.\n",
1638                                     fname, lineno, cargv[1] );
1639                                 return 1;
1640                         }
1641
1642                         vals[0].bv_val = cargv[1];
1643                         vals[0].bv_len = strlen( vals[0].bv_val );
1644                         if( value_add( &be->be_update_refs, vals ) ) {
1645                                 return LDAP_OTHER;
1646                         }
1647
1648                 /* replication log file to which changes are appended */
1649                 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
1650                         if ( cargc < 2 ) {
1651                                 Debug( LDAP_DEBUG_ANY,
1652             "%s: line %d: missing filename in \"replogfile <filename>\" line\n",
1653                                     fname, lineno, 0 );
1654
1655                                 return( 1 );
1656                         }
1657                         if ( be ) {
1658                                 be->be_replogfile = ch_strdup( cargv[1] );
1659                         } else {
1660                                 replogfile = ch_strdup( cargv[1] );
1661                         }
1662
1663                 /* file from which to read additional rootdse attrs */
1664                 } else if ( strcasecmp( cargv[0], "rootDSE" ) == 0) {
1665                         if ( cargc < 2 ) {
1666                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1667                                         "missing filename in \"rootDSE <filename>\" line.\n",
1668                                     fname, lineno, 0 );
1669                                 return 1;
1670                         }
1671
1672                         if( read_root_dse_file( cargv[1] ) ) {
1673                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: "
1674                                         "could not read \"rootDSE <filename>\" line\n",
1675                                     fname, lineno, 0 );
1676                                 return 1;
1677                         }
1678
1679                 /* maintain lastmodified{by,time} attributes */
1680                 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
1681                         if ( cargc < 2 ) {
1682                                 Debug( LDAP_DEBUG_ANY,
1683             "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
1684                                     fname, lineno, 0 );
1685
1686                                 return( 1 );
1687                         }
1688
1689                         if ( be == NULL ) {
1690                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: lastmod"
1691                                         " line must appear inside a database definition\n",
1692                                         fname, lineno, 0 );
1693                                 return 1;
1694
1695                         } else if ( SLAP_NOLASTMODCMD(be) ) {
1696                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: lastmod"
1697                                         " not available for %s databases\n",
1698                                         fname, lineno, be->bd_info->bi_type );
1699                                 return 1;
1700                         }
1701
1702                         if ( strcasecmp( cargv[1], "on" ) == 0 ) {
1703                                 SLAP_DBFLAGS(be) &= ~SLAP_DBFLAG_NOLASTMOD;
1704                         } else {
1705                                 SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NOLASTMOD;
1706                         }
1707
1708 #ifdef SIGHUP
1709                 /* turn on/off gentle SIGHUP handling */
1710                 } else if ( strcasecmp( cargv[0], "gentlehup" ) == 0 ) {
1711                         if ( cargc < 2 ) {
1712                                 Debug( LDAP_DEBUG_ANY,
1713     "%s: line %d: missing on|off in \"gentlehup <on|off>\" line\n",
1714                                     fname, lineno, 0 );
1715                                 return( 1 );
1716                         }
1717                         if ( strcasecmp( cargv[1], "off" ) == 0 ) {
1718                                 global_gentlehup = 0;
1719                         } else {
1720                                 global_gentlehup = 1;
1721                         }
1722 #endif
1723
1724                 /* set idle timeout value */
1725                 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
1726                         int i;
1727                         if ( cargc < 2 ) {
1728                                 Debug( LDAP_DEBUG_ANY,
1729             "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
1730                                     fname, lineno, 0 );
1731
1732                                 return( 1 );
1733                         }
1734
1735                         i = atoi( cargv[1] );
1736
1737                         if( i < 0 ) {
1738                                 Debug( LDAP_DEBUG_ANY,
1739             "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
1740                                     fname, lineno, i );
1741
1742                                 return( 1 );
1743                         }
1744
1745                         global_idletimeout = i;
1746
1747                 /* include another config file */
1748                 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
1749                         if ( cargc < 2 ) {
1750                                 Debug( LDAP_DEBUG_ANY,
1751     "%s: line %d: missing filename in \"include <filename>\" line\n",
1752                                     fname, lineno, 0 );
1753
1754                                 return( 1 );
1755                         }
1756                         savefname = ch_strdup( cargv[1] );
1757                         savelineno = lineno;
1758
1759                         if ( read_config( savefname, depth+1 ) != 0 ) {
1760                                 return( 1 );
1761                         }
1762
1763                         free( savefname );
1764                         lineno = savelineno - 1;
1765
1766                 /* location of kerberos srvtab file */
1767                 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
1768                         if ( cargc < 2 ) {
1769                                 Debug( LDAP_DEBUG_ANY,
1770             "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
1771                                     fname, lineno, 0 );
1772
1773                                 return( 1 );
1774                         }
1775                         ldap_srvtab = ch_strdup( cargv[1] );
1776
1777 #ifdef SLAPD_MODULES
1778                 } else if (strcasecmp( cargv[0], "moduleload") == 0 ) {
1779                    if ( cargc < 2 ) {
1780                       Debug( LDAP_DEBUG_ANY,
1781                              "%s: line %d: missing filename in \"moduleload <filename>\" line\n",
1782                              fname, lineno, 0 );
1783
1784                       exit( EXIT_FAILURE );
1785                    }
1786                    if (module_load(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
1787                       Debug( LDAP_DEBUG_ANY,
1788                              "%s: line %d: failed to load or initialize module %s\n",
1789                              fname, lineno, cargv[1]);
1790
1791                       exit( EXIT_FAILURE );
1792                    }
1793                 } else if (strcasecmp( cargv[0], "modulepath") == 0 ) {
1794                    if ( cargc != 2 ) {
1795                       Debug( LDAP_DEBUG_ANY,
1796                              "%s: line %d: missing path in \"modulepath <path>\" line\n",
1797                              fname, lineno, 0 );
1798
1799                       exit( EXIT_FAILURE );
1800                    }
1801                    if (module_path( cargv[1] )) {
1802                            Debug( LDAP_DEBUG_ANY,
1803                                   "%s: line %d: failed to set module search path to %s\n",
1804                                   fname, lineno, cargv[1]);
1805
1806                       exit( EXIT_FAILURE );
1807                    }
1808                    
1809 #endif /*SLAPD_MODULES*/
1810
1811 #ifdef HAVE_TLS
1812                 } else if ( !strcasecmp( cargv[0], "TLSRandFile" ) ) {
1813                         rc = ldap_pvt_tls_set_option( NULL,
1814                                                       LDAP_OPT_X_TLS_RANDOM_FILE,
1815                                                       cargv[1] );
1816                         if ( rc )
1817                                 return rc;
1818
1819                 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
1820                         rc = ldap_pvt_tls_set_option( NULL,
1821                                                       LDAP_OPT_X_TLS_CIPHER_SUITE,
1822                                                       cargv[1] );
1823                         if ( rc )
1824                                 return rc;
1825
1826                 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
1827                         rc = ldap_pvt_tls_set_option( NULL,
1828                                                       LDAP_OPT_X_TLS_CERTFILE,
1829                                                       cargv[1] );
1830                         if ( rc )
1831                                 return rc;
1832
1833                 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
1834                         rc = ldap_pvt_tls_set_option( NULL,
1835                                                       LDAP_OPT_X_TLS_KEYFILE,
1836                                                       cargv[1] );
1837                         if ( rc )
1838                                 return rc;
1839
1840                 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
1841                         rc = ldap_pvt_tls_set_option( NULL,
1842                                                       LDAP_OPT_X_TLS_CACERTDIR,
1843                                                       cargv[1] );
1844                         if ( rc )
1845                                 return rc;
1846
1847                 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
1848                         rc = ldap_pvt_tls_set_option( NULL,
1849                                                       LDAP_OPT_X_TLS_CACERTFILE,
1850                                                       cargv[1] );
1851                         if ( rc )
1852                                 return rc;
1853                 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
1854                         if ( isdigit( (unsigned char) cargv[1][0] ) ) {
1855                                 i = atoi(cargv[1]);
1856                                 rc = ldap_pvt_tls_set_option( NULL,
1857                                                       LDAP_OPT_X_TLS_REQUIRE_CERT,
1858                                                       &i );
1859                         } else {
1860                                 rc = ldap_int_tls_config( NULL,
1861                                                       LDAP_OPT_X_TLS_REQUIRE_CERT,
1862                                                       cargv[1] );
1863                         }
1864
1865                         if ( rc )
1866                                 return rc;
1867
1868 #endif
1869
1870                 } else if ( !strcasecmp( cargv[0], "reverse-lookup" ) ) {
1871 #ifdef SLAPD_RLOOKUPS
1872                         if ( cargc < 2 ) {
1873                                 Debug( LDAP_DEBUG_ANY,
1874 "%s: line %d: reverse-lookup: missing \"on\" or \"off\"\n",
1875                                         fname, lineno, 0 );
1876                                 return( 1 );
1877                         }
1878
1879                         if ( !strcasecmp( cargv[1], "on" ) ) {
1880                                 use_reverse_lookup = 1;
1881                         } else if ( !strcasecmp( cargv[1], "off" ) ) {
1882                                 use_reverse_lookup = 0;
1883                         } else {
1884                                 Debug( LDAP_DEBUG_ANY,
1885 "%s: line %d: reverse-lookup: must be \"on\" (default) or \"off\"\n",
1886                                         fname, lineno, 0 );
1887                                 return( 1 );
1888                         }
1889
1890 #else /* !SLAPD_RLOOKUPS */
1891                         Debug( LDAP_DEBUG_ANY,
1892 "%s: line %d: reverse lookups are not configured (ignored).\n",
1893                                 fname, lineno, 0 );
1894 #endif /* !SLAPD_RLOOKUPS */
1895
1896                 /* Netscape plugins */
1897                 } else if ( strcasecmp( cargv[0], "plugin" ) == 0 ) {
1898 #if defined( LDAP_SLAPI )
1899
1900 #ifdef notdef /* allow global plugins, too */
1901                         /*
1902                          * a "plugin" line must be inside a database
1903                          * definition, since we implement pre-,post- 
1904                          * and extended operation plugins
1905                          */
1906                         if ( be == NULL ) {
1907                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: plugin "
1908                                     "line must appear inside a database "
1909                                     "definition\n", fname, lineno, 0 );
1910                                 return( 1 );
1911                         }
1912 #endif /* notdef */
1913
1914                         if ( slapi_int_read_config( be, fname, lineno, cargc, cargv ) 
1915                                         != LDAP_SUCCESS )
1916                         {
1917                                 Debug( LDAP_DEBUG_ANY, "%s: line %d: SLAPI "
1918                                                 "config read failed.\n", fname, lineno, 0 );
1919                                 return( 1 );
1920                         }
1921                         slapi_plugins_used++;
1922
1923 #else /* !defined( LDAP_SLAPI ) */
1924                         Debug( LDAP_DEBUG_ANY, "%s: line %d: SLAPI "
1925                             "not supported.\n", fname, lineno, 0 );
1926                         return( 1 );
1927                         
1928 #endif /* !defined( LDAP_SLAPI ) */
1929
1930                 /* Netscape plugins */
1931                 } else if ( strcasecmp( cargv[0], "pluginlog" ) == 0 ) {
1932 #if defined( LDAP_SLAPI )
1933                         if ( cargc < 2 ) {
1934                                 Debug( LDAP_DEBUG_ANY, 
1935                                         "%s: line %d: missing file name "
1936                                         "in pluginlog <filename> line.\n",
1937                                         fname, lineno, 0 );
1938                                 return( 1 );
1939                         }
1940
1941                         if ( slapi_log_file != NULL ) {
1942                                 ch_free( slapi_log_file );
1943                         }
1944
1945                         slapi_log_file = ch_strdup( cargv[1] );
1946 #endif /* !defined( LDAP_SLAPI ) */
1947
1948                 /* pass anything else to the current backend info/db config routine */
1949                 } else {
1950                         if ( bi != NULL ) {
1951                                 if ( bi->bi_config ) {
1952                                         rc = (*bi->bi_config)( bi, fname, lineno, cargc, cargv );
1953
1954                                         switch ( rc ) {
1955                                         case 0:
1956                                                 break;
1957
1958                                         case SLAP_CONF_UNKNOWN:
1959                                                 Debug( LDAP_DEBUG_ANY,
1960 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
1961                                                         fname, lineno, cargv[0] );
1962                                                 break;
1963
1964                                         default:
1965                                                 return 1;
1966                                         }
1967                                 }
1968
1969                         } else if ( be != NULL ) {
1970                                 if ( be->be_config ) {
1971                                         rc = (*be->be_config)( be, fname, lineno, cargc, cargv );
1972
1973                                         switch ( rc ) {
1974                                         case 0:
1975                                                 break;
1976
1977                                         case SLAP_CONF_UNKNOWN:
1978                                                 Debug( LDAP_DEBUG_ANY,
1979 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
1980                                                         fname, lineno, cargv[0] );
1981                                                 break;
1982
1983                                         default:
1984                                                 return 1;
1985                                         }
1986                                 }
1987
1988                         } else {
1989                                 if ( frontendDB->be_config ) {
1990                                         rc = (*frontendDB->be_config)( frontendDB, fname, lineno, cargc, cargv );
1991
1992                                         switch ( rc ) {
1993                                         case 0:
1994                                                 break;
1995
1996                                         case SLAP_CONF_UNKNOWN:
1997                                                 Debug( LDAP_DEBUG_ANY,
1998 "%s: line %d: unknown directive \"%s\" inside global database definition (ignored)\n",
1999                                                         fname, lineno, cargv[0] );
2000                                                 break;
2001
2002                                         default:
2003                                                 return 1;
2004                                         }
2005                                 }
2006                         }
2007                 }
2008                 free( saveline );
2009         }
2010         fclose( fp );
2011
2012         if ( depth == 0 ) ch_free( cargv );
2013
2014         if ( BER_BVISNULL( &frontendDB->be_schemadn ) ) {
2015                 ber_str2bv( SLAPD_SCHEMA_DN, sizeof(SLAPD_SCHEMA_DN)-1, 1,
2016                         &frontendDB->be_schemadn );
2017                 dnNormalize( 0, NULL, NULL, &frontendDB->be_schemadn, &frontendDB->be_schemandn, NULL );
2018         }
2019
2020         if ( load_ucdata( NULL ) < 0 ) return 1;
2021         return( 0 );
2022 }
2023
2024 static int
2025 fp_parse_line(
2026     int         lineno,
2027     char        *line
2028 )
2029 {
2030         char *  token;
2031         char *  logline;
2032         char    logbuf[sizeof("pseudorootpw ***")];
2033
2034         cargc = 0;
2035         token = strtok_quote( line, " \t" );
2036
2037         logline = line;
2038
2039         if ( token && ( strcasecmp( token, "rootpw" ) == 0 ||
2040                 strcasecmp( token, "replica" ) == 0 ||          /* contains "credentials" */
2041                 strcasecmp( token, "bindpw" ) == 0 ||           /* used in back-ldap */
2042                 strcasecmp( token, "pseudorootpw" ) == 0 ||     /* used in back-meta */
2043                 strcasecmp( token, "dbpasswd" ) == 0 ) )        /* used in back-sql */
2044         {
2045                 snprintf( logline = logbuf, sizeof logbuf, "%s ***", token );
2046         }
2047
2048         if ( strtok_quote_ptr ) {
2049                 *strtok_quote_ptr = ' ';
2050         }
2051
2052         Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, logline, 0 );
2053
2054         if ( strtok_quote_ptr ) {
2055                 *strtok_quote_ptr = '\0';
2056         }
2057
2058         for ( ; token != NULL; token = strtok_quote( NULL, " \t" ) ) {
2059                 if ( cargc == cargv_size - 1 ) {
2060                         char **tmp;
2061                         tmp = ch_realloc( cargv, (cargv_size + ARGS_STEP) *
2062                                             sizeof(*cargv) );
2063                         if ( tmp == NULL ) {
2064                                 Debug( LDAP_DEBUG_ANY, 
2065                                                 "line %d: out of memory\n", 
2066                                                 lineno, 0, 0 );
2067                                 return -1;
2068                         }
2069                         cargv = tmp;
2070                         cargv_size += ARGS_STEP;
2071                 }
2072                 cargv[cargc++] = token;
2073         }
2074         cargv[cargc] = NULL;
2075         return 0;
2076 }
2077
2078 static char *
2079 strtok_quote( char *line, char *sep )
2080 {
2081         int             inquote;
2082         char            *tmp;
2083         static char     *next;
2084
2085         strtok_quote_ptr = NULL;
2086         if ( line != NULL ) {
2087                 next = line;
2088         }
2089         while ( *next && strchr( sep, *next ) ) {
2090                 next++;
2091         }
2092
2093         if ( *next == '\0' ) {
2094                 next = NULL;
2095                 return( NULL );
2096         }
2097         tmp = next;
2098
2099         for ( inquote = 0; *next; ) {
2100                 switch ( *next ) {
2101                 case '"':
2102                         if ( inquote ) {
2103                                 inquote = 0;
2104                         } else {
2105                                 inquote = 1;
2106                         }
2107                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
2108                         break;
2109
2110                 case '\\':
2111                         if ( next[1] )
2112                                 AC_MEMCPY( next,
2113                                             next + 1, strlen( next + 1 ) + 1 );
2114                         next++;         /* dont parse the escaped character */
2115                         break;
2116
2117                 default:
2118                         if ( ! inquote ) {
2119                                 if ( strchr( sep, *next ) != NULL ) {
2120                                         strtok_quote_ptr = next;
2121                                         *next++ = '\0';
2122                                         return( tmp );
2123                                 }
2124                         }
2125                         next++;
2126                         break;
2127                 }
2128         }
2129
2130         return( tmp );
2131 }
2132
2133 static char     buf[BUFSIZ];
2134 static char     *line;
2135 static size_t lmax, lcur;
2136
2137 #define CATLINE( buf ) \
2138         do { \
2139                 size_t len = strlen( buf ); \
2140                 while ( lcur + len + 1 > lmax ) { \
2141                         lmax += BUFSIZ; \
2142                         line = (char *) ch_realloc( line, lmax ); \
2143                 } \
2144                 strcpy( line + lcur, buf ); \
2145                 lcur += len; \
2146         } while( 0 )
2147
2148 static char *
2149 fp_getline( FILE *fp, int *lineno )
2150 {
2151         char            *p;
2152
2153         lcur = 0;
2154         CATLINE( buf );
2155         (*lineno)++;
2156
2157         /* hack attack - keeps us from having to keep a stack of bufs... */
2158         if ( strncasecmp( line, "include", 7 ) == 0 ) {
2159                 buf[0] = '\0';
2160                 return( line );
2161         }
2162
2163         while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
2164                 /* trim off \r\n or \n */
2165                 if ( (p = strchr( buf, '\n' )) != NULL ) {
2166                         if( p > buf && p[-1] == '\r' ) --p;
2167                         *p = '\0';
2168                 }
2169                 
2170                 /* trim off trailing \ and append the next line */
2171                 if ( line[ 0 ] != '\0' 
2172                                 && (p = line + strlen( line ) - 1)[ 0 ] == '\\'
2173                                 && p[ -1 ] != '\\' ) {
2174                         p[ 0 ] = '\0';
2175                         lcur--;
2176
2177                 } else {
2178                         if ( ! isspace( (unsigned char) buf[0] ) ) {
2179                                 return( line );
2180                         }
2181
2182                         /* change leading whitespace to a space */
2183                         buf[0] = ' ';
2184                 }
2185
2186                 CATLINE( buf );
2187                 (*lineno)++;
2188         }
2189         buf[0] = '\0';
2190
2191         return( line[0] ? line : NULL );
2192 }
2193
2194 static void
2195 fp_getline_init( int *lineno )
2196 {
2197         *lineno = -1;
2198         buf[0] = '\0';
2199 }
2200
2201 /* Loads ucdata, returns 1 if loading, 0 if already loaded, -1 on error */
2202 static int
2203 load_ucdata( char *path )
2204 {
2205 #if 0
2206         static int loaded = 0;
2207         int err;
2208         
2209         if ( loaded ) {
2210                 return( 0 );
2211         }
2212         err = ucdata_load( path ? path : SLAPD_DEFAULT_UCDATA, UCDATA_ALL );
2213         if ( err ) {
2214                 Debug( LDAP_DEBUG_ANY, "error loading ucdata (error %d)\n",
2215                        err, 0, 0 );
2216
2217                 return( -1 );
2218         }
2219         loaded = 1;
2220         return( 1 );
2221 #else
2222         /* ucdata is now hardcoded */
2223         return( 0 );
2224 #endif
2225 }
2226
2227 void
2228 config_destroy( )
2229 {
2230         ucdata_unload( UCDATA_ALL );
2231         if ( frontendDB ) {
2232                 /* NOTE: in case of early exit, frontendDB can be NULL */
2233                 if ( frontendDB->be_schemandn.bv_val )
2234                         free( frontendDB->be_schemandn.bv_val );
2235                 if ( frontendDB->be_schemadn.bv_val )
2236                         free( frontendDB->be_schemadn.bv_val );
2237                 if ( frontendDB->be_acl )
2238                         acl_destroy( frontendDB->be_acl, NULL );
2239         }
2240         free( line );
2241         if ( slapd_args_file )
2242                 free ( slapd_args_file );
2243         if ( slapd_pid_file )
2244                 free ( slapd_pid_file );
2245         if ( default_passwd_hash )
2246                 ldap_charray_free( default_passwd_hash );
2247 }
2248
2249 static int
2250 add_syncrepl(
2251         Backend *be,
2252         char    **cargv,
2253         int     cargc
2254 )
2255 {
2256         syncinfo_t *si;
2257         syncinfo_t *si_entry;
2258         int     rc = 0;
2259         int duplicated_replica_id = 0;
2260
2261         si = (syncinfo_t *) ch_calloc( 1, sizeof( syncinfo_t ) );
2262
2263         if ( si == NULL ) {
2264                 Debug( LDAP_DEBUG_ANY, "out of memory in add_syncrepl\n", 0, 0, 0 );
2265                 return 1;
2266         }
2267
2268         si->si_tls = SYNCINFO_TLS_OFF;
2269         if ( be->be_rootndn.bv_val ) {
2270                 ber_dupbv( &si->si_updatedn, &be->be_rootndn );
2271         }
2272         si->si_bindmethod = LDAP_AUTH_SIMPLE;
2273         si->si_schemachecking = 0;
2274         ber_str2bv( "(objectclass=*)", STRLENOF("(objectclass=*)"), 1,
2275                 &si->si_filterstr );
2276         si->si_base.bv_val = NULL;
2277         si->si_scope = LDAP_SCOPE_SUBTREE;
2278         si->si_attrsonly = 0;
2279         si->si_anlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
2280         si->si_exanlist = (AttributeName *) ch_calloc( 1, sizeof( AttributeName ));
2281         si->si_attrs = NULL;
2282         si->si_allattrs = 0;
2283         si->si_allopattrs = 0;
2284         si->si_exattrs = NULL;
2285         si->si_type = LDAP_SYNC_REFRESH_ONLY;
2286         si->si_interval = 86400;
2287         si->si_retryinterval = NULL;
2288         si->si_retrynum_init = NULL;
2289         si->si_retrynum = NULL;
2290         si->si_syncCookie.ctxcsn = NULL;
2291         si->si_syncCookie.octet_str = NULL;
2292         si->si_syncCookie.sid = -1;
2293         si->si_manageDSAit = 0;
2294         si->si_tlimit = 0;
2295         si->si_slimit = 0;
2296         si->si_syncUUID_ndn.bv_val = NULL;
2297         si->si_syncUUID_ndn.bv_len = 0;
2298
2299         si->si_presentlist = NULL;
2300         LDAP_LIST_INIT( &si->si_nonpresentlist );
2301
2302         rc = parse_syncrepl_line( cargv, cargc, si );
2303
2304         LDAP_STAILQ_FOREACH( si_entry, &be->be_syncinfo, si_next ) {
2305                 if ( si->si_rid == si_entry->si_rid ) {
2306                         Debug( LDAP_DEBUG_ANY,
2307                                 "add_syncrepl: duplicated replica id\n",0, 0, 0 );
2308                         duplicated_replica_id = 1;
2309                         break;
2310                 }
2311         }
2312
2313         if ( rc < 0 || duplicated_replica_id ) {
2314                 Debug( LDAP_DEBUG_ANY, "failed to add syncinfo\n", 0, 0, 0 );
2315                 syncinfo_free( si );    
2316                 return 1;
2317         } else {
2318                 Debug( LDAP_DEBUG_CONFIG,
2319                         "Config: ** successfully added syncrepl \"%s\"\n",
2320                         si->si_provideruri == NULL ? "(null)" : si->si_provideruri, 0, 0 );
2321                 if ( !si->si_schemachecking ) {
2322                         SLAP_DBFLAGS(be) |= SLAP_DBFLAG_NO_SCHEMA_CHECK;
2323                 }
2324                 si->si_be = be;
2325                 LDAP_STAILQ_INSERT_TAIL( &be->be_syncinfo, si, si_next );
2326                 return 0;
2327         }
2328 }
2329
2330 #define IDSTR                   "rid"
2331 #define PROVIDERSTR             "provider"
2332 #define SUFFIXSTR               "suffix"
2333 #define UPDATEDNSTR             "updatedn"
2334 #define BINDMETHSTR             "bindmethod"
2335 #define SIMPLESTR               "simple"
2336 #define SASLSTR                 "sasl"
2337 #define BINDDNSTR               "binddn"
2338 #define CREDSTR                 "credentials"
2339 #define OLDAUTHCSTR             "bindprincipal"
2340 #define AUTHCSTR                "authcID"
2341 #define AUTHZSTR                "authzID"
2342 #define SRVTABSTR               "srvtab"
2343 #define SASLMECHSTR             "saslmech"
2344 #define REALMSTR                "realm"
2345 #define SECPROPSSTR             "secprops"
2346 #define STARTTLSSTR             "starttls"
2347 #define CRITICALSTR             "critical"
2348
2349 #define SCHEMASTR               "schemachecking"
2350 #define FILTERSTR               "filter"
2351 #define SEARCHBASESTR   "searchbase"
2352 #define SCOPESTR                "scope"
2353 #define ATTRSSTR                "attrs"
2354 #define EXATTRSSTR              "exattrs"
2355 #define ATTRSONLYSTR    "attrsonly"
2356 #define TYPESTR                 "type"
2357 #define INTERVALSTR             "interval"
2358 #define LASTMODSTR              "lastmod"
2359 #define LMREQSTR                "req"
2360 #define LMGENSTR                "gen"
2361 #define LMNOSTR                 "no"
2362 #define MANAGEDSAITSTR  "manageDSAit"
2363 #define SLIMITSTR               "sizelimit"
2364 #define TLIMITSTR               "timelimit"
2365
2366 #define RETRYSTR                "retry"
2367
2368 #define GOT_ID                  0x0001
2369 #define GOT_PROVIDER    0x0002
2370 #define GOT_METHOD              0x0004
2371 #define GOT_ALL                 0x0007
2372
2373 static int
2374 parse_syncrepl_line(
2375         char            **cargv,
2376         int             cargc,
2377         syncinfo_t      *si
2378 )
2379 {
2380         int     gots = 0;
2381         int     i, j;
2382         char    *hp, *val;
2383         int     nr_attr = 0;
2384
2385         for ( i = 1; i < cargc; i++ ) {
2386                 if ( !strncasecmp( cargv[ i ], IDSTR, sizeof( IDSTR ) - 1 )) {
2387                         int tmp;
2388                         /* '\0' string terminator accounts for '=' */
2389                         val = cargv[ i ] + sizeof( IDSTR );
2390                         tmp= atoi( val );
2391                         if ( tmp >= 1000 || tmp < 0 ) {
2392                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2393                                          "syncrepl id %d is out of range [0..999]\n", tmp );
2394                                 return -1;
2395                         }
2396                         si->si_rid = tmp;
2397                         gots |= GOT_ID;
2398                 } else if ( !strncasecmp( cargv[ i ], PROVIDERSTR,
2399                                         sizeof( PROVIDERSTR ) - 1 )) {
2400                         val = cargv[ i ] + sizeof( PROVIDERSTR );
2401                         si->si_provideruri = ch_strdup( val );
2402                         si->si_provideruri_bv = (BerVarray)
2403                                 ch_calloc( 2, sizeof( struct berval ));
2404                         ber_str2bv( si->si_provideruri, strlen( si->si_provideruri ),
2405                                 1, &si->si_provideruri_bv[0] );
2406                         si->si_provideruri_bv[1].bv_len = 0;
2407                         si->si_provideruri_bv[1].bv_val = NULL;
2408                         gots |= GOT_PROVIDER;
2409                 } else if ( !strncasecmp( cargv[ i ], STARTTLSSTR,
2410                         sizeof(STARTTLSSTR) - 1 ) )
2411                 {
2412                         val = cargv[ i ] + sizeof( STARTTLSSTR );
2413                         if( !strcasecmp( val, CRITICALSTR ) ) {
2414                                 si->si_tls = SYNCINFO_TLS_CRITICAL;
2415                         } else {
2416                                 si->si_tls = SYNCINFO_TLS_ON;
2417                         }
2418                 } else if ( !strncasecmp( cargv[ i ],
2419                         UPDATEDNSTR, sizeof( UPDATEDNSTR ) - 1 ) )
2420                 {
2421                         struct berval updatedn = {0, NULL};
2422                         val = cargv[ i ] + sizeof( UPDATEDNSTR );
2423                         ber_str2bv( val, 0, 0, &updatedn );
2424                         ch_free( si->si_updatedn.bv_val );
2425                         dnNormalize( 0, NULL, NULL, &updatedn, &si->si_updatedn, NULL );
2426                 } else if ( !strncasecmp( cargv[ i ], BINDMETHSTR,
2427                                 sizeof( BINDMETHSTR ) - 1 ) )
2428                 {
2429                         val = cargv[ i ] + sizeof( BINDMETHSTR );
2430                         if ( !strcasecmp( val, SIMPLESTR )) {
2431                                 si->si_bindmethod = LDAP_AUTH_SIMPLE;
2432                                 gots |= GOT_METHOD;
2433                         } else if ( !strcasecmp( val, SASLSTR )) {
2434 #ifdef HAVE_CYRUS_SASL
2435                                 si->si_bindmethod = LDAP_AUTH_SASL;
2436                                 gots |= GOT_METHOD;
2437 #else /* HAVE_CYRUS_SASL */
2438                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2439                                         "not compiled with SASL support\n" );
2440                                 return 1;
2441 #endif /* HAVE_CYRUS_SASL */
2442                         } else {
2443                                 si->si_bindmethod = -1;
2444                         }
2445                 } else if ( !strncasecmp( cargv[ i ],
2446                                 BINDDNSTR, sizeof( BINDDNSTR ) - 1 ) ) {
2447                         val = cargv[ i ] + sizeof( BINDDNSTR );
2448                         si->si_binddn = ch_strdup( val );
2449                 } else if ( !strncasecmp( cargv[ i ],
2450                                 CREDSTR, sizeof( CREDSTR ) - 1 ) ) {
2451                         val = cargv[ i ] + sizeof( CREDSTR );
2452                         si->si_passwd = ch_strdup( val );
2453                 } else if ( !strncasecmp( cargv[ i ],
2454                                 SASLMECHSTR, sizeof( SASLMECHSTR ) - 1 ) ) {
2455                         val = cargv[ i ] + sizeof( SASLMECHSTR );
2456                         si->si_saslmech = ch_strdup( val );
2457                 } else if ( !strncasecmp( cargv[ i ],
2458                                 SECPROPSSTR, sizeof( SECPROPSSTR ) - 1 ) ) {
2459                         val = cargv[ i ] + sizeof( SECPROPSSTR );
2460                         si->si_secprops = ch_strdup( val );
2461                 } else if ( !strncasecmp( cargv[ i ],
2462                                 REALMSTR, sizeof( REALMSTR ) - 1 ) ) {
2463                         val = cargv[ i ] + sizeof( REALMSTR );
2464                         si->si_realm = ch_strdup( val );
2465                 } else if ( !strncasecmp( cargv[ i ],
2466                                 AUTHCSTR, sizeof( AUTHCSTR ) - 1 ) ) {
2467                         val = cargv[ i ] + sizeof( AUTHCSTR );
2468                         if ( si->si_authcId )
2469                                 ch_free( si->si_authcId );
2470                         si->si_authcId = ch_strdup( val );
2471                 } else if ( !strncasecmp( cargv[ i ],
2472                                 OLDAUTHCSTR, sizeof( OLDAUTHCSTR ) - 1 ) ) {
2473                         /* Old authcID is provided for some backwards compatibility */
2474                         val = cargv[ i ] + sizeof( OLDAUTHCSTR );
2475                         if ( si->si_authcId )
2476                                 ch_free( si->si_authcId );
2477                         si->si_authcId = ch_strdup( val );
2478                 } else if ( !strncasecmp( cargv[ i ],
2479                                 AUTHZSTR, sizeof( AUTHZSTR ) - 1 ) ) {
2480                         val = cargv[ i ] + sizeof( AUTHZSTR );
2481                         si->si_authzId = ch_strdup( val );
2482                 } else if ( !strncasecmp( cargv[ i ],
2483                                 SCHEMASTR, sizeof( SCHEMASTR ) - 1 ) )
2484                 {
2485                         val = cargv[ i ] + sizeof( SCHEMASTR );
2486                         if ( !strncasecmp( val, "on", STRLENOF( "on" ) )) {
2487                                 si->si_schemachecking = 1;
2488                         } else if ( !strncasecmp( val, "off", STRLENOF( "off" ) ) ) {
2489                                 si->si_schemachecking = 0;
2490                         } else {
2491                                 si->si_schemachecking = 1;
2492                         }
2493                 } else if ( !strncasecmp( cargv[ i ],
2494                         FILTERSTR, sizeof( FILTERSTR ) - 1 ) )
2495                 {
2496                         val = cargv[ i ] + sizeof( FILTERSTR );
2497                         ber_str2bv( val, 0, 1, &si->si_filterstr );
2498                 } else if ( !strncasecmp( cargv[ i ],
2499                         SEARCHBASESTR, sizeof( SEARCHBASESTR ) - 1 ) )
2500                 {
2501                         struct berval bv;
2502                         val = cargv[ i ] + sizeof( SEARCHBASESTR );
2503                         if ( si->si_base.bv_val ) {
2504                                 ch_free( si->si_base.bv_val );
2505                         }
2506                         ber_str2bv( val, 0, 0, &bv );
2507                         if ( dnNormalize( 0, NULL, NULL, &bv, &si->si_base, NULL )) {
2508                                 fprintf( stderr, "Invalid base DN \"%s\"\n", val );
2509                                 return 1;
2510                         }
2511                 } else if ( !strncasecmp( cargv[ i ],
2512                         SCOPESTR, sizeof( SCOPESTR ) - 1 ) )
2513                 {
2514                         val = cargv[ i ] + sizeof( SCOPESTR );
2515                         if ( !strncasecmp( val, "base", STRLENOF( "base" ) )) {
2516                                 si->si_scope = LDAP_SCOPE_BASE;
2517                         } else if ( !strncasecmp( val, "one", STRLENOF( "one" ) )) {
2518                                 si->si_scope = LDAP_SCOPE_ONELEVEL;
2519 #ifdef LDAP_SCOPE_SUBORDINATE
2520                         } else if ( !strcasecmp( val, "subordinate" ) ||
2521                                 !strcasecmp( val, "children" ))
2522                         {
2523                                 si->si_scope = LDAP_SCOPE_SUBORDINATE;
2524 #endif
2525                         } else if ( !strncasecmp( val, "sub", STRLENOF( "sub" ) )) {
2526                                 si->si_scope = LDAP_SCOPE_SUBTREE;
2527                         } else {
2528                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2529                                         "unknown scope \"%s\"\n", val);
2530                                 return 1;
2531                         }
2532                 } else if ( !strncasecmp( cargv[ i ],
2533                         ATTRSONLYSTR, sizeof( ATTRSONLYSTR ) - 1 ) )
2534                 {
2535                         si->si_attrsonly = 1;
2536                 } else if ( !strncasecmp( cargv[ i ],
2537                         ATTRSSTR, sizeof( ATTRSSTR ) - 1 ) )
2538                 {
2539                         val = cargv[ i ] + sizeof( ATTRSSTR );
2540                         if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2541                                 char *attr_fname;
2542                                 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2543                                 si->si_anlist = file2anlist(
2544                                                                 si->si_anlist, attr_fname, " ,\t" );
2545                                 if ( si->si_anlist == NULL ) {
2546                                         ch_free( attr_fname );
2547                                         return -1;
2548                                 }
2549                                 ch_free( attr_fname );
2550                         } else {
2551                                 char *str, *s, *next;
2552                                 char delimstr[] = " ,\t";
2553                                 str = ch_strdup( val );
2554                                 for ( s = ldap_pvt_strtok( str, delimstr, &next );
2555                                           s != NULL;
2556                                           s = ldap_pvt_strtok( NULL, delimstr, &next )) {
2557                                         if ( strlen(s) == 1 && *s == '*' ) {
2558                                                 si->si_allattrs = 1;
2559                                                 *(val + ( s - str )) = delimstr[0];
2560                                         }
2561                                         if ( strlen(s) == 1 && *s == '+' ) {
2562                                                 si->si_allopattrs = 1;
2563                                                 *(val + ( s - str )) = delimstr[0];
2564                                         }
2565                                 }
2566                                 ch_free( str );
2567                                 si->si_anlist = str2anlist( si->si_anlist, val, " ,\t" );
2568                                 if ( si->si_anlist == NULL ) {
2569                                         return -1;
2570                                 }
2571                         }
2572                 } else if ( !strncasecmp( cargv[ i ],
2573                         EXATTRSSTR, sizeof( EXATTRSSTR ) - 1 ) )
2574                 {
2575                         val = cargv[ i ] + sizeof( EXATTRSSTR );
2576                         if ( !strncasecmp( val, ":include:", STRLENOF(":include:") )) {
2577                                 char *attr_fname;
2578                                 attr_fname = ch_strdup( val + STRLENOF(":include:") );
2579                                 si->si_exanlist = file2anlist(
2580                                                                         si->si_exanlist, attr_fname, " ,\t" );
2581                                 if ( si->si_exanlist == NULL ) {
2582                                         ch_free( attr_fname );
2583                                         return -1;
2584                                 }
2585                                 ch_free( attr_fname );
2586                         } else {
2587                                 int j;
2588                                 si->si_exanlist = str2anlist( si->si_exanlist, val, " ,\t" );
2589                                 if ( si->si_exanlist == NULL ) {
2590                                         return -1;
2591                                 }
2592                         }
2593                 } else if ( !strncasecmp( cargv[ i ],
2594                         TYPESTR, sizeof( TYPESTR ) - 1 ) )
2595                 {
2596                         val = cargv[ i ] + sizeof( TYPESTR );
2597                         if ( !strncasecmp( val, "refreshOnly", STRLENOF("refreshOnly") )) {
2598                                 si->si_type = LDAP_SYNC_REFRESH_ONLY;
2599                         } else if ( !strncasecmp( val, "refreshAndPersist",
2600                                 STRLENOF("refreshAndPersist") ))
2601                         {
2602                                 si->si_type = LDAP_SYNC_REFRESH_AND_PERSIST;
2603                                 si->si_interval = 60;
2604                         } else {
2605                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2606                                         "unknown sync type \"%s\"\n", val);
2607                                 return 1;
2608                         }
2609                 } else if ( !strncasecmp( cargv[ i ],
2610                         INTERVALSTR, sizeof( INTERVALSTR ) - 1 ) )
2611                 {
2612                         val = cargv[ i ] + sizeof( INTERVALSTR );
2613                         if ( si->si_type == LDAP_SYNC_REFRESH_AND_PERSIST ) {
2614                                 si->si_interval = 0;
2615                         } else {
2616                                 char *hstr;
2617                                 char *mstr;
2618                                 char *dstr;
2619                                 char *sstr;
2620                                 int dd, hh, mm, ss;
2621                                 dstr = val;
2622                                 hstr = strchr( dstr, ':' );
2623                                 if ( hstr == NULL ) {
2624                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2625                                                 "invalid interval \"%s\"\n", val );
2626                                         return 1;
2627                                 }
2628                                 *hstr++ = '\0';
2629                                 mstr = strchr( hstr, ':' );
2630                                 if ( mstr == NULL ) {
2631                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2632                                                 "invalid interval \"%s\"\n", val );
2633                                         return 1;
2634                                 }
2635                                 *mstr++ = '\0';
2636                                 sstr = strchr( mstr, ':' );
2637                                 if ( sstr == NULL ) {
2638                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2639                                                 "invalid interval \"%s\"\n", val );
2640                                         return 1;
2641                                 }
2642                                 *sstr++ = '\0';
2643
2644                                 dd = atoi( dstr );
2645                                 hh = atoi( hstr );
2646                                 mm = atoi( mstr );
2647                                 ss = atoi( sstr );
2648                                 if (( hh > 24 ) || ( hh < 0 ) ||
2649                                         ( mm > 60 ) || ( mm < 0 ) ||
2650                                         ( ss > 60 ) || ( ss < 0 ) || ( dd < 0 )) {
2651                                         fprintf( stderr, "Error: parse_syncrepl_line: "
2652                                                 "invalid interval \"%s\"\n", val );
2653                                         return 1;
2654                                 }
2655                                 si->si_interval = (( dd * 24 + hh ) * 60 + mm ) * 60 + ss;
2656                         }
2657                         if ( si->si_interval < 0 ) {
2658                                 fprintf( stderr, "Error: parse_syncrepl_line: "
2659                                         "invalid interval \"%ld\"\n",
2660                                         (long) si->si_interval);
2661                                 return 1;
2662                         }
2663                 } else if ( !strncasecmp( cargv[ i ],
2664                         RETRYSTR, sizeof( RETRYSTR ) - 1 ) )
2665                 {
2666                         char *str;
2667                         char **retry_list;
2668                         int j, k, n;
2669
2670                         val = cargv[ i ] + sizeof( RETRYSTR );
2671                         retry_list = (char **) ch_calloc( 1, sizeof( char * ));
2672                         retry_list[0] = NULL;
2673
2674                         slap_str2clist( &retry_list, val, " ,\t" );
2675
2676                         for ( k = 0; retry_list && retry_list[k]; k++ ) ;
2677                         n = k / 2;
2678                         if ( k % 2 ) {
2679                                 fprintf( stderr,
2680                                                 "Error: incomplete syncrepl retry list\n" );
2681                                 for ( k = 0; retry_list && retry_list[k]; k++ ) {
2682                                         ch_free( retry_list[k] );
2683                                 }
2684                                 ch_free( retry_list );
2685                                 exit( EXIT_FAILURE );
2686                         }
2687                         si->si_retryinterval = (time_t *) ch_calloc( n + 1, sizeof( time_t ));
2688                         si->si_retrynum = (int *) ch_calloc( n + 1, sizeof( int ));
2689                         si->si_retrynum_init = (int *) ch_calloc( n + 1, sizeof( int ));
2690                         for ( j = 0; j < n; j++ ) {
2691                                 si->si_retryinterval[j] = atoi( retry_list[j*2] );
2692                                 if ( *retry_list[j*2+1] == '+' ) {
2693                                         si->si_retrynum_init[j] = -1;
2694                                         si->si_retrynum[j] = -1;
2695                                         j++;
2696                                         break;
2697                                 } else {
2698                                         si->si_retrynum_init[j] = atoi( retry_list[j*2+1] );
2699                                         si->si_retrynum[j] = atoi( retry_list[j*2+1] );
2700                                 }
2701                         }
2702                         si->si_retrynum_init[j] = -2;
2703                         si->si_retrynum[j] = -2;
2704                         si->si_retryinterval[j] = 0;
2705                         
2706                         for ( k = 0; retry_list && retry_list[k]; k++ ) {
2707                                 ch_free( retry_list[k] );
2708                         }
2709                         ch_free( retry_list );
2710                 } else if ( !strncasecmp( cargv[ i ],
2711                         MANAGEDSAITSTR, sizeof( MANAGEDSAITSTR ) - 1 ) )
2712                 {
2713                         val = cargv[ i ] + sizeof( MANAGEDSAITSTR );
2714                         si->si_manageDSAit = atoi( val );
2715                 } else if ( !strncasecmp( cargv[ i ],
2716                         SLIMITSTR, sizeof( SLIMITSTR ) - 1 ) )
2717                 {
2718                         val = cargv[ i ] + sizeof( SLIMITSTR );
2719                         si->si_slimit = atoi( val );
2720                 } else if ( !strncasecmp( cargv[ i ],
2721                         TLIMITSTR, sizeof( TLIMITSTR ) - 1 ) )
2722                 {
2723                         val = cargv[ i ] + sizeof( TLIMITSTR );
2724                         si->si_tlimit = atoi( val );
2725                 } else {
2726                         fprintf( stderr, "Error: parse_syncrepl_line: "
2727                                 "unknown keyword \"%s\"\n", cargv[ i ] );
2728                 }
2729         }
2730
2731         if ( gots != GOT_ALL ) {
2732                 fprintf( stderr,
2733                         "Error: Malformed \"syncrepl\" line in slapd config file" );
2734                 return -1;
2735         }
2736
2737         return 0;
2738 }
2739
2740 char **
2741 slap_str2clist( char ***out, char *in, const char *brkstr )
2742 {
2743         char    *str;
2744         char    *s;
2745         char    *lasts;
2746         int     i, j;
2747         const char *text;
2748         char    **new;
2749
2750         /* find last element in list */
2751         for (i = 0; *out && (*out)[i]; i++);
2752
2753         /* protect the input string from strtok */
2754         str = ch_strdup( in );
2755
2756         if ( *str == '\0' ) {
2757                 free( str );
2758                 return( *out );
2759         }
2760
2761         /* Count words in string */
2762         j=1;
2763         for ( s = str; *s; s++ ) {
2764                 if ( strchr( brkstr, *s ) != NULL ) {
2765                         j++;
2766                 }
2767         }
2768
2769         *out = ch_realloc( *out, ( i + j + 1 ) * sizeof( char * ) );
2770         new = *out + i;
2771         for ( s = ldap_pvt_strtok( str, brkstr, &lasts );
2772                 s != NULL;
2773                 s = ldap_pvt_strtok( NULL, brkstr, &lasts ) )
2774         {
2775                 *new = ch_strdup( s );
2776                 new++;
2777         }
2778
2779         *new = NULL;
2780         free( str );
2781         return( *out );
2782 }
2783