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