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