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