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