]> git.sur5r.net Git - openldap/blob - servers/slapd/config.c
s/SAFEMEMCPY/AC_MEMCPY/
[openldap] / servers / slapd / config.c
1 /* config.c - configuration file handling routines */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/ctype.h>
14 #include <ac/socket.h>
15
16 #include "ldap_pvt.h"
17 #include "slap.h"
18
19 #define MAXARGS 100
20
21 /*
22  * defaults for various global variables
23  */
24 int             defsize = SLAPD_DEFAULT_SIZELIMIT;
25 int             deftime = SLAPD_DEFAULT_TIMELIMIT;
26 AccessControl   *global_acl = NULL;
27 slap_access_t           global_default_access = ACL_READ;
28 int             global_readonly = 0;
29 char            *replogfile;
30 int             global_lastmod = ON;
31 int             global_idletimeout = 0;
32 char    *global_realm = NULL;
33 char            *ldap_srvtab = "";
34 char            *default_passwd_hash;
35
36 char   *slapd_pid_file  = NULL;
37 char   *slapd_args_file = NULL;
38
39 static char     *fp_getline(FILE *fp, int *lineno);
40 static void     fp_getline_init(int *lineno);
41 static int      fp_parse_line(char *line, int *argcp, char **argv);
42
43 static char     *strtok_quote(char *line, char *sep);
44
45 int
46 read_config( const char *fname )
47 {
48         FILE    *fp;
49         char    *line, *savefname, *saveline;
50         int     cargc, savelineno;
51         char    *cargv[MAXARGS+1];
52         int     lineno, i;
53 #ifdef HAVE_TLS
54         int rc;
55 #endif
56         struct berval *vals[2];
57         struct berval val;
58
59         static BackendInfo *bi = NULL;
60         static BackendDB        *be = NULL;
61
62         vals[0] = &val;
63         vals[1] = NULL;
64
65         if ( (fp = fopen( fname, "r" )) == NULL ) {
66                 ldap_syslog = 1;
67                 Debug( LDAP_DEBUG_ANY,
68                     "could not open config file \"%s\" - absolute path?\n",
69                     fname, 0, 0 );
70                 perror( fname );
71                 return 1;
72         }
73
74         Debug( LDAP_DEBUG_CONFIG, "reading config file %s\n", fname, 0, 0 );
75
76         fp_getline_init( &lineno );
77
78         while ( (line = fp_getline( fp, &lineno )) != NULL ) {
79                 /* skip comments and blank lines */
80                 if ( line[0] == '#' || line[0] == '\0' ) {
81                         continue;
82                 }
83
84                 Debug( LDAP_DEBUG_CONFIG, "line %d (%s)\n", lineno, line, 0 );
85
86                 /* fp_parse_line is destructive, we save a copy */
87                 saveline = ch_strdup( line );
88
89                 if ( fp_parse_line( line, &cargc, cargv ) != 0 ) {
90                         return( 1 );
91                 }
92
93                 if ( cargc < 1 ) {
94                         Debug( LDAP_DEBUG_ANY,
95                             "%s: line %d: bad config line (ignored)\n",
96                             fname, lineno, 0 );
97                         continue;
98                 }
99
100                 if ( strcasecmp( cargv[0], "backend" ) == 0 ) {
101                         if ( cargc < 2 ) {
102                                 Debug( LDAP_DEBUG_ANY,
103                 "%s: line %d: missing type in \"backend <type>\" line\n",
104                                     fname, lineno, 0 );
105                                 return( 1 );
106                         }
107
108                         if( be != NULL ) {
109                                 Debug( LDAP_DEBUG_ANY,
110 "%s: line %d: backend line must appear before any database definition\n",
111                                     fname, lineno, 0 );
112                                 return( 1 );
113                         }
114
115                         bi = backend_info( cargv[1] );
116
117                         if( bi == NULL ) {
118                                 Debug( LDAP_DEBUG_ANY,
119                                         "backend %s initialization failed.\n",
120                                     cargv[1], 0, 0 );
121                                 return( 1 );
122                         }
123
124                 /* start of a new database definition */
125                 } else if ( strcasecmp( cargv[0], "database" ) == 0 ) {
126                         if ( cargc < 2 ) {
127                                 Debug( LDAP_DEBUG_ANY,
128                 "%s: line %d: missing type in \"database <type>\" line\n",
129                                     fname, lineno, 0 );
130                                 return( 1 );
131                         }
132
133                         bi = NULL;
134                         be = backend_db_init( cargv[1] );
135
136                         if( be == NULL ) {
137                                 Debug( LDAP_DEBUG_ANY,
138                                         "database %s initialization failed.\n",
139                                     cargv[1], 0, 0 );
140                                 return( 1 );
141                         }
142
143                 /* set thread concurrency */
144                 } else if ( strcasecmp( cargv[0], "concurrency" ) == 0 ) {
145                         int c;
146                         if ( cargc < 2 ) {
147                                 Debug( LDAP_DEBUG_ANY,
148             "%s: line %d: missing level in \"concurrency <level>\" line\n",
149                                     fname, lineno, 0 );
150                                 return( 1 );
151                         }
152
153                         c = atoi( cargv[1] );
154
155                         if( c < 1 ) {
156                                 Debug( LDAP_DEBUG_ANY,
157             "%s: line %d: invalid level (%d) in \"concurrency <level>\" line\n",
158                                     fname, lineno, c );
159                                 return( 1 );
160                         }
161
162                         ldap_pvt_thread_set_concurrency( c );
163
164                 /* set maximum threads in thread pool */
165                 } else if ( strcasecmp( cargv[0], "threads" ) == 0 ) {
166                         int c;
167                         if ( cargc < 2 ) {
168                                 Debug( LDAP_DEBUG_ANY,
169             "%s: line %d: missing count in \"threads <count>\" line\n",
170                                     fname, lineno, 0 );
171                                 return( 1 );
172                         }
173
174                         c = atoi( cargv[1] );
175
176                         if( c < 0 ) {
177                                 Debug( LDAP_DEBUG_ANY,
178             "%s: line %d: invalid level (%d) in \"threads <count>\" line\n",
179                                     fname, lineno, c );
180                                 return( 1 );
181                         }
182
183                         ldap_pvt_thread_pool_maxthreads( &connection_pool, c );
184
185                 /* get pid file name */
186                 } else if ( strcasecmp( cargv[0], "pidfile" ) == 0 ) {
187                         if ( cargc < 2 ) {
188                                 Debug( LDAP_DEBUG_ANY,
189             "%s: line %d: missing file name in \"pidfile <file>\" line\n",
190                                     fname, lineno, 0 );
191                                 return( 1 );
192                         }
193
194                         slapd_pid_file = ch_strdup( cargv[1] );
195
196                 /* get args file name */
197                 } else if ( strcasecmp( cargv[0], "argsfile" ) == 0 ) {
198                         if ( cargc < 2 ) {
199                                 Debug( LDAP_DEBUG_ANY,
200             "%s: line %d: missing file name in \"argsfile <file>\" line\n",
201                                     fname, lineno, 0 );
202                                 return( 1 );
203                         }
204
205                         slapd_args_file = ch_strdup( cargv[1] );
206
207                 /* default password hash */
208                 } else if ( strcasecmp( cargv[0], "password-hash" ) == 0 ) {
209                         if ( cargc < 2 ) {
210                                 Debug( LDAP_DEBUG_ANY,
211             "%s: line %d: missing hash in \"password-hash <hash>\" line\n",
212                                     fname, lineno, 0 );
213                                 return( 1 );
214                         }
215                         if ( default_passwd_hash != NULL ) {
216                                 Debug( LDAP_DEBUG_ANY,
217                                         "%s: line %d: already set default password_hash!\n",
218                                         fname, lineno, 0 );
219                                 return 1;
220
221                         } else {
222                                 default_passwd_hash = ch_strdup( cargv[1] );
223                         }
224
225                 /* set SASL realm */
226                 } else if ( strcasecmp( cargv[0], "sasl-realm" ) == 0 ) {
227                         if ( cargc < 2 ) {
228                                 Debug( LDAP_DEBUG_ANY,
229             "%s: line %d: missing realm in \"sasl-realm <realm>\" line\n",
230                                     fname, lineno, 0 );
231                                 return( 1 );
232                         }
233                         if ( be != NULL ) {
234                                 be->be_realm = ch_strdup( cargv[1] );
235
236                         } else if ( global_realm != NULL ) {
237                                 Debug( LDAP_DEBUG_ANY,
238                                         "%s: line %d: already set global realm!\n",
239                                         fname, lineno, 0 );
240                                 return 1;
241
242                         } else {
243                                 global_realm = ch_strdup( cargv[1] );
244                         }
245
246                 /* SASL security properties */
247                 } else if ( strcasecmp( cargv[0], "sasl-secprops" ) == 0 ) {
248                         char *txt;
249
250                         if ( cargc < 2 ) {
251                                 Debug( LDAP_DEBUG_ANY,
252             "%s: line %d: missing flags in \"sasl-secprops <properties>\" line\n",
253                                     fname, lineno, 0 );
254                                 return 1;
255                         }
256
257                         txt = slap_sasl_secprops( cargv[1] );
258                         if ( txt != NULL ) {
259                                 Debug( LDAP_DEBUG_ANY,
260             "%s: line %d: sasl-secprops: %s\n",
261                                     fname, lineno, txt );
262                                 return 1;
263                         }
264
265                 /* set time limit */
266                 } else if ( strcasecmp( cargv[0], "sizelimit" ) == 0 ) {
267                         if ( cargc < 2 ) {
268                                 Debug( LDAP_DEBUG_ANY,
269             "%s: line %d: missing limit in \"sizelimit <limit>\" line\n",
270                                     fname, lineno, 0 );
271                                 return( 1 );
272                         }
273                         if ( be == NULL ) {
274                                 defsize = atoi( cargv[1] );
275                         } else {
276                                 be->be_sizelimit = atoi( cargv[1] );
277                         }
278
279                 /* set time limit */
280                 } else if ( strcasecmp( cargv[0], "timelimit" ) == 0 ) {
281                         if ( cargc < 2 ) {
282                                 Debug( LDAP_DEBUG_ANY,
283             "%s: line %d: missing limit in \"timelimit <limit>\" line\n",
284                                     fname, lineno, 0 );
285                                 return( 1 );
286                         }
287                         if ( be == NULL ) {
288                                 deftime = atoi( cargv[1] );
289                         } else {
290                                 be->be_timelimit = atoi( cargv[1] );
291                         }
292
293                 /* set database suffix */
294                 } else if ( strcasecmp( cargv[0], "suffix" ) == 0 ) {
295                         Backend *tmp_be;
296                         if ( cargc < 2 ) {
297                                 Debug( LDAP_DEBUG_ANY,
298                     "%s: line %d: missing dn in \"suffix <dn>\" line\n",
299                                     fname, lineno, 0 );
300                                 return( 1 );
301                         } else if ( cargc > 2 ) {
302                                 Debug( LDAP_DEBUG_ANY,
303     "%s: line %d: extra cruft after <dn> in \"suffix %s\" line (ignored)\n",
304                                     fname, lineno, cargv[1] );
305                         }
306                         if ( be == NULL ) {
307                                 Debug( LDAP_DEBUG_ANY,
308 "%s: line %d: suffix line must appear inside a database definition (ignored)\n",
309                                     fname, lineno, 0 );
310                         } else if ( ( tmp_be = select_backend( cargv[1] ) ) == be ) {
311                                 Debug( LDAP_DEBUG_ANY,
312 "%s: line %d: suffix already served by this backend (ignored)\n",
313                                     fname, lineno, 0 );
314                         } else if ( tmp_be  != NULL ) {
315                                 Debug( LDAP_DEBUG_ANY,
316 "%s: line %d: suffix already served by a preceeding backend \"%s\" (ignored)\n",
317                                     fname, lineno, tmp_be->be_suffix[0] );
318                         } else {
319                                 char *dn = ch_strdup( cargv[1] );
320                                 (void) dn_validate( dn );
321                                 charray_add( &be->be_suffix, dn );
322                                 (void) ldap_pvt_str2upper( dn );
323                                 charray_add( &be->be_nsuffix, dn );
324                                 free( dn );
325                         }
326
327                 /* set database suffixAlias */
328                 } else if ( strcasecmp( cargv[0], "suffixAlias" ) == 0 ) {
329                         Backend *tmp_be;
330                         if ( cargc < 2 ) {
331                                 Debug( LDAP_DEBUG_ANY,
332 "%s: line %d: missing alias and aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
333                                         fname, lineno, 0 );
334                                 return( 1 );
335                         } else if ( cargc < 3 ) {
336                                 Debug( LDAP_DEBUG_ANY,
337 "%s: line %d: missing aliased_dn in \"suffixAlias <alias> <aliased_dn>\" line\n",
338                                 fname, lineno, 0 );
339                                 return( 1 );
340                         } else if ( cargc > 3 ) {
341                                 Debug( LDAP_DEBUG_ANY,
342                                         "%s: line %d: extra cruft in suffixAlias line (ignored)\n",
343                                 fname, lineno, 0 );
344                         }
345
346                         if ( be == NULL ) {
347                                 Debug( LDAP_DEBUG_ANY,
348                                         "%s: line %d: suffixAlias line"
349                                         " must appear inside a database definition (ignored)\n",
350                                         fname, lineno, 0 );
351                         } else if ( (tmp_be = select_backend( cargv[1] )) != NULL ) {
352                                 Debug( LDAP_DEBUG_ANY,
353                                         "%s: line %d: suffixAlias served by"
354                                         "  a preceeding backend \"%s\" (ignored)\n",
355                                         fname, lineno, tmp_be->be_suffix[0] );
356
357                         } else if ( (tmp_be = select_backend( cargv[2] )) != NULL ) {
358                                 Debug( LDAP_DEBUG_ANY,
359                                         "%s: line %d: suffixAlias derefs to differnet backend"
360                                         "  a preceeding backend \"%s\" (ignored)\n",
361                                         fname, lineno, tmp_be->be_suffix[0] );
362
363                         } else {
364                                 char *alias, *aliased_dn;
365
366                                 alias = ch_strdup( cargv[1] );
367                                 (void) dn_normalize( alias );
368
369                                 aliased_dn = ch_strdup( cargv[2] );
370                                 (void) dn_normalize( aliased_dn );
371
372                                 charray_add( &be->be_suffixAlias, alias );
373                                 charray_add( &be->be_suffixAlias, aliased_dn );
374
375                                 free(alias);
376                                 free(aliased_dn);
377                         }
378
379                /* set max deref depth */
380                } else if ( strcasecmp( cargv[0], "maxDerefDepth" ) == 0 ) {
381                                         int i;
382                        if ( cargc < 2 ) {
383                                Debug( LDAP_DEBUG_ANY,
384                    "%s: line %d: missing depth in \"maxDerefDepth <depth>\" line\n",
385                                    fname, lineno, 0 );
386                                return( 1 );
387                        }
388                        if ( be == NULL ) {
389                                Debug( LDAP_DEBUG_ANY,
390 "%s: line %d: depth line must appear inside a database definition (ignored)\n",
391                                    fname, lineno, 0 );
392                        } else if ((i = atoi(cargv[1])) < 0) {
393                                Debug( LDAP_DEBUG_ANY,
394 "%s: line %d: depth must be positive (ignored)\n",
395                                    fname, lineno, 0 );
396
397                        } else {
398                            be->be_max_deref_depth = i;
399                                            }
400
401
402                 /* set magic "root" dn for this database */
403                 } else if ( strcasecmp( cargv[0], "rootdn" ) == 0 ) {
404                         if ( cargc < 2 ) {
405                                 Debug( LDAP_DEBUG_ANY,
406                     "%s: line %d: missing dn in \"rootdn <dn>\" line\n",
407                                     fname, lineno, 0 );
408                                 return( 1 );
409                         }
410                         if ( be == NULL ) {
411                                 Debug( LDAP_DEBUG_ANY,
412 "%s: line %d: rootdn line must appear inside a database definition (ignored)\n",
413                                     fname, lineno, 0 );
414                         } else {
415                                 be->be_root_dn = ch_strdup( cargv[1] );
416                                 be->be_root_ndn = ch_strdup( cargv[1] );
417
418                                 if( dn_normalize( be->be_root_ndn ) == NULL ) {
419                                         free( be->be_root_dn );
420                                         free( be->be_root_ndn );
421                                         Debug( LDAP_DEBUG_ANY,
422 "%s: line %d: rootdn DN is invalid\n",
423                                            fname, lineno, 0 );
424                                         return( 1 );
425                                 }
426                         }
427
428                 /* set super-secret magic database password */
429                 } else if ( strcasecmp( cargv[0], "rootpw" ) == 0 ) {
430                         if ( cargc < 2 ) {
431                                 Debug( LDAP_DEBUG_ANY,
432             "%s: line %d: missing passwd in \"rootpw <passwd>\" line\n",
433                                     fname, lineno, 0 );
434                                 return( 1 );
435                         }
436                         if ( be == NULL ) {
437                                 Debug( LDAP_DEBUG_ANY,
438 "%s: line %d: rootpw line must appear inside a database definition (ignored)\n",
439                                     fname, lineno, 0 );
440                         } else {
441                                 be->be_root_pw.bv_val = ch_strdup( cargv[1] );
442                                 be->be_root_pw.bv_len = strlen( be->be_root_pw.bv_val );
443                         }
444
445                 /* make this database read-only */
446                 } else if ( strcasecmp( cargv[0], "readonly" ) == 0 ) {
447                         if ( cargc < 2 ) {
448                                 Debug( LDAP_DEBUG_ANY,
449             "%s: line %d: missing on|off in \"readonly <on|off>\" line\n",
450                                     fname, lineno, 0 );
451                                 return( 1 );
452                         }
453                         if ( be == NULL ) {
454                                 global_readonly = (strcasecmp( cargv[1], "on" ) == 0);
455                         } else {
456                                 if ( strcasecmp( cargv[1], "on" ) == 0 ) {
457                                         be->be_readonly = 1;
458                                 } else {
459                                         be->be_readonly = 0;
460                                 }
461                         }
462
463                 /* where to send clients when we don't hold it */
464                 } else if ( strcasecmp( cargv[0], "referral" ) == 0 ) {
465                         if ( cargc < 2 ) {
466                                 Debug( LDAP_DEBUG_ANY,
467                     "%s: line %d: missing URL in \"referral <URL>\" line\n",
468                                     fname, lineno, 0 );
469                                 return( 1 );
470                         }
471
472                         vals[0]->bv_val = cargv[1];
473                         vals[0]->bv_len = strlen( vals[0]->bv_val );
474                         value_add( &default_referral, vals );
475
476                 /* specify an Object Identifier macro */
477                 } else if ( strcasecmp( cargv[0], "objectidentifier" ) == 0 ) {
478                         parse_oidm( fname, lineno, cargc, cargv );
479
480                 /* specify an objectclass */
481                 } else if ( strcasecmp( cargv[0], "objectclass" ) == 0 ) {
482                         if ( *cargv[1] == '(' ) {
483                                 char * p;
484                                 p = strchr(saveline,'(');
485                                 parse_oc( fname, lineno, p, cargv );
486                         } else {
487                                 Debug( LDAP_DEBUG_ANY,
488     "%s: line %d: old objectclass format not supported.\n",
489                                     fname, lineno, 0 );
490                         }
491
492                 /* specify an attribute type */
493                 } else if (( strcasecmp( cargv[0], "attributetype" ) == 0 )
494                         || ( strcasecmp( cargv[0], "attribute" ) == 0 ))
495                 {
496                         if ( *cargv[1] == '(' ) {
497                                 char * p;
498                                 p = strchr(saveline,'(');
499                                 parse_at( fname, lineno, p, cargv );
500                         } else {
501                                 Debug( LDAP_DEBUG_ANY,
502     "%s: line %d: old attribute type format not supported.\n",
503                                     fname, lineno, 0 );
504                         }
505
506                 /* turn on/off schema checking */
507                 } else if ( strcasecmp( cargv[0], "schemacheck" ) == 0 ) {
508                         if ( cargc < 2 ) {
509                                 Debug( LDAP_DEBUG_ANY,
510     "%s: line %d: missing on|off in \"schemacheck <on|off>\" line\n",
511                                     fname, lineno, 0 );
512                                 return( 1 );
513                         }
514                         if ( strcasecmp( cargv[1], "off" ) == 0 ) {
515                                 global_schemacheck = 0;
516                         } else {
517                                 global_schemacheck = 1;
518                         }
519
520                 /* specify access control info */
521                 } else if ( strcasecmp( cargv[0], "access" ) == 0 ) {
522                         parse_acl( be, fname, lineno, cargc, cargv );
523
524                 /* specify default access control info */
525                 } else if ( strcasecmp( cargv[0], "defaultaccess" ) == 0 ) {
526                         slap_access_t access;
527
528                         if ( cargc < 2 ) {
529                                 Debug( LDAP_DEBUG_ANY,
530             "%s: line %d: missing limit in \"defaultaccess <access>\" line\n",
531                                     fname, lineno, 0 );
532                                 return( 1 );
533                         }
534
535                         access = str2access( cargv[1] );
536
537                         if ( access == ACL_INVALID_ACCESS ) {
538                                 Debug( LDAP_DEBUG_ANY,
539                                         "%s: line %d: bad access level \"%s\", "
540                                         "expecting none|auth|compare|search|read|write\n",
541                                     fname, lineno, cargv[1] );
542                                 return( 1 );
543                         }
544
545                         if ( be == NULL ) {
546                                 global_default_access = access;
547                         } else {
548                                 be->be_dfltaccess = access;
549                         }
550
551                 /* debug level to log things to syslog */
552                 } else if ( strcasecmp( cargv[0], "loglevel" ) == 0 ) {
553                         if ( cargc < 2 ) {
554                                 Debug( LDAP_DEBUG_ANY,
555                     "%s: line %d: missing level in \"loglevel <level>\" line\n",
556                                     fname, lineno, 0 );
557                                 return( 1 );
558                         }
559                         ldap_syslog = atoi( cargv[1] );
560
561                 /* list of replicas of the data in this backend (master only) */
562                 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
563                         if ( cargc < 2 ) {
564                                 Debug( LDAP_DEBUG_ANY,
565             "%s: line %d: missing host in \"replica <host[:port]>\" line\n",
566                                     fname, lineno, 0 );
567                                 return( 1 );
568                         }
569                         if ( be == NULL ) {
570                                 Debug( LDAP_DEBUG_ANY,
571 "%s: line %d: replica line must appear inside a database definition (ignored)\n",
572                                     fname, lineno, 0 );
573                         } else {
574                                 for ( i = 1; i < cargc; i++ ) {
575                                         if ( strncasecmp( cargv[i], "host=", 5 )
576                                             == 0 ) {
577                                                 charray_add( &be->be_replica,
578                                                              cargv[i] + 5 );
579                                                 break;
580                                         }
581                                 }
582                                 if ( i == cargc ) {
583                                         Debug( LDAP_DEBUG_ANY,
584                     "%s: line %d: missing host in \"replica\" line (ignored)\n",
585                                             fname, lineno, 0 );
586                                 }
587                         }
588
589                 /* dn of master entity allowed to write to replica */
590                 } else if ( strcasecmp( cargv[0], "updatedn" ) == 0 ) {
591                         if ( cargc < 2 ) {
592                                 Debug( LDAP_DEBUG_ANY,
593                     "%s: line %d: missing dn in \"updatedn <dn>\" line\n",
594                                     fname, lineno, 0 );
595                                 return( 1 );
596                         }
597                         if ( be == NULL ) {
598                                 Debug( LDAP_DEBUG_ANY,
599 "%s: line %d: updatedn line must appear inside a database definition (ignored)\n",
600                                     fname, lineno, 0 );
601                         } else {
602                                 be->be_update_ndn = ch_strdup( cargv[1] );
603                                 if( dn_normalize( be->be_update_ndn ) == NULL ) {
604                                         Debug( LDAP_DEBUG_ANY,
605 "%s: line %d: updatedn DN is invalid\n",
606                                             fname, lineno, 0 );
607                                         return 1;
608                                 }
609                         }
610
611                 } else if ( strcasecmp( cargv[0], "updateref" ) == 0 ) {
612                         if ( cargc < 2 ) {
613                                 Debug( LDAP_DEBUG_ANY,
614                     "%s: line %d: missing dn in \"updateref <ldapurl>\" line\n",
615                                     fname, lineno, 0 );
616                                 return( 1 );
617                         }
618                         if ( be == NULL ) {
619                                 Debug( LDAP_DEBUG_ANY,
620 "%s: line %d: updateref line must appear inside a database definition (ignored)\n",
621                                     fname, lineno, 0 );
622                         } else if ( be->be_update_ndn == NULL ) {
623                                 Debug( LDAP_DEBUG_ANY,
624 "%s: line %d: updateref line must after updatedn (ignored)\n",
625                                     fname, lineno, 0 );
626                         } else {
627                                 vals[0]->bv_val = cargv[1];
628                                 vals[0]->bv_len = strlen( vals[0]->bv_val );
629                                 value_add( &be->be_update_refs, vals );
630                         }
631
632                 /* replication log file to which changes are appended */
633                 } else if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
634                         if ( cargc < 2 ) {
635                                 Debug( LDAP_DEBUG_ANY,
636             "%s: line %d: missing dn in \"replogfile <filename>\" line\n",
637                                     fname, lineno, 0 );
638                                 return( 1 );
639                         }
640                         if ( be ) {
641                                 be->be_replogfile = ch_strdup( cargv[1] );
642                         } else {
643                                 replogfile = ch_strdup( cargv[1] );
644                         }
645
646                 /* maintain lastmodified{by,time} attributes */
647                 } else if ( strcasecmp( cargv[0], "lastmod" ) == 0 ) {
648                         if ( cargc < 2 ) {
649                                 Debug( LDAP_DEBUG_ANY,
650             "%s: line %d: missing on|off in \"lastmod <on|off>\" line\n",
651                                     fname, lineno, 0 );
652                                 return( 1 );
653                         }
654                         if ( strcasecmp( cargv[1], "on" ) == 0 ) {
655                                 if ( be )
656                                         be->be_lastmod = ON;
657                                 else
658                                         global_lastmod = ON;
659                         } else {
660                                 if ( be )
661                                         be->be_lastmod = OFF;
662                                 else
663                                         global_lastmod = OFF;
664                         }
665
666                 /* set idle timeout value */
667                 } else if ( strcasecmp( cargv[0], "idletimeout" ) == 0 ) {
668                         int i;
669                         if ( cargc < 2 ) {
670                                 Debug( LDAP_DEBUG_ANY,
671             "%s: line %d: missing timeout value in \"idletimeout <seconds>\" line\n",
672                                     fname, lineno, 0 );
673                                 return( 1 );
674                         }
675
676                         i = atoi( cargv[1] );
677
678                         if( i < 0 ) {
679                                 Debug( LDAP_DEBUG_ANY,
680             "%s: line %d: timeout value (%d) invalid \"idletimeout <seconds>\" line\n",
681                                     fname, lineno, i );
682                                 return( 1 );
683                         }
684
685                         global_idletimeout = i;
686
687                 /* include another config file */
688                 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
689                         if ( cargc < 2 ) {
690                                 Debug( LDAP_DEBUG_ANY,
691     "%s: line %d: missing filename in \"include <filename>\" line\n",
692                                     fname, lineno, 0 );
693                                 return( 1 );
694                         }
695                         savefname = ch_strdup( cargv[1] );
696                         savelineno = lineno;
697
698                         if ( read_config( savefname ) != 0 ) {
699                                 return( 1 );
700                         }
701
702                         free( savefname );
703                         lineno = savelineno - 1;
704
705                 /* location of kerberos srvtab file */
706                 } else if ( strcasecmp( cargv[0], "srvtab" ) == 0 ) {
707                         if ( cargc < 2 ) {
708                                 Debug( LDAP_DEBUG_ANY,
709             "%s: line %d: missing filename in \"srvtab <filename>\" line\n",
710                                     fname, lineno, 0 );
711                                 return( 1 );
712                         }
713                         ldap_srvtab = ch_strdup( cargv[1] );
714
715 #ifdef SLAPD_MODULES
716                 } else if (strcasecmp( cargv[0], "moduleload") == 0 ) {
717                    if ( cargc < 2 ) {
718                       Debug( LDAP_DEBUG_ANY,
719                              "%s: line %d: missing filename in \"moduleload <filename>\" line\n",
720                              fname, lineno, 0 );
721                       exit( EXIT_FAILURE );
722                    }
723                    if (module_load(cargv[1], cargc - 2, (cargc > 2) ? cargv + 2 : NULL)) {
724                       Debug( LDAP_DEBUG_ANY,
725                              "%s: line %d: failed to load or initialize module %s\n",
726                              fname, lineno, cargv[1]);
727                       exit( EXIT_FAILURE );
728                    }
729                 } else if (strcasecmp( cargv[0], "modulepath") == 0 ) {
730                    if ( cargc != 2 ) {
731                       Debug( LDAP_DEBUG_ANY,
732                              "%s: line %d: missing path in \"modulepath <path>\" line\n",
733                              fname, lineno, 0 );
734                       exit( EXIT_FAILURE );
735                    }
736                    if (module_path( cargv[1] )) {
737                       Debug( LDAP_DEBUG_ANY,
738                              "%s: line %d: failed to set module search path to %s\n",
739                              fname, lineno, cargv[1]);
740                       exit( EXIT_FAILURE );
741                    }
742                    
743 #endif /*SLAPD_MODULES*/
744
745 #ifdef HAVE_TLS
746                 } else if ( !strcasecmp( cargv[0], "TLSProtocol" ) ) {
747                         rc = ldap_pvt_tls_set_option( NULL,
748                                                       LDAP_OPT_X_TLS_PROTOCOL,
749                                                       cargv[1] );
750                         if ( rc )
751                                 return rc;
752
753                 } else if ( !strcasecmp( cargv[0], "TLSCipherSuite" ) ) {
754                         rc = ldap_pvt_tls_set_option( NULL,
755                                                       LDAP_OPT_X_TLS_CIPHER_SUITE,
756                                                       cargv[1] );
757                         if ( rc )
758                                 return rc;
759
760                 } else if ( !strcasecmp( cargv[0], "TLSCertificateFile" ) ) {
761                         rc = ldap_pvt_tls_set_option( NULL,
762                                                       LDAP_OPT_X_TLS_CERTFILE,
763                                                       cargv[1] );
764                         if ( rc )
765                                 return rc;
766
767                 } else if ( !strcasecmp( cargv[0], "TLSCertificateKeyFile" ) ) {
768                         rc = ldap_pvt_tls_set_option( NULL,
769                                                       LDAP_OPT_X_TLS_KEYFILE,
770                                                       cargv[1] );
771                         if ( rc )
772                                 return rc;
773
774                 } else if ( !strcasecmp( cargv[0], "TLSCACertificatePath" ) ) {
775                         rc = ldap_pvt_tls_set_option( NULL,
776                                                       LDAP_OPT_X_TLS_CACERTDIR,
777                                                       cargv[1] );
778                         if ( rc )
779                                 return rc;
780
781                 } else if ( !strcasecmp( cargv[0], "TLSCACertificateFile" ) ) {
782                         rc = ldap_pvt_tls_set_option( NULL,
783                                                       LDAP_OPT_X_TLS_CACERTFILE,
784                                                       cargv[1] );
785                         if ( rc )
786                                 return rc;
787                 } else if ( !strcasecmp( cargv[0], "TLSVerifyClient" ) ) {
788                         rc = ldap_pvt_tls_set_option( NULL,
789                                                       LDAP_OPT_X_TLS_REQUIRE_CERT,
790                                                       cargv[1] );
791                         if ( rc )
792                                 return rc;
793
794 #endif
795
796                 /* pass anything else to the current backend info/db config routine */
797                 } else {
798                         if ( bi != NULL ) {
799                                 if ( bi->bi_config == 0 ) {
800                                         Debug( LDAP_DEBUG_ANY,
801 "%s: line %d: unknown directive \"%s\" inside backend info definition (ignored)\n",
802                                                 fname, lineno, cargv[0] );
803                                 } else {
804                                         if ( (*bi->bi_config)( bi, fname, lineno, cargc, cargv )
805                                                 != 0 )
806                                         {
807                                                 return( 1 );
808                                         }
809                                 }
810                         } else if ( be != NULL ) {
811                                 if ( be->be_config == 0 ) {
812                                         Debug( LDAP_DEBUG_ANY,
813 "%s: line %d: unknown directive \"%s\" inside backend database definition (ignored)\n",
814                                         fname, lineno, cargv[0] );
815                                 } else {
816                                         if ( (*be->be_config)( be, fname, lineno, cargc, cargv )
817                                                 != 0 )
818                                         {
819                                                 return( 1 );
820                                         }
821                                 }
822                         } else {
823                                 Debug( LDAP_DEBUG_ANY,
824 "%s: line %d: unknown directive \"%s\" outside backend info and database definitions (ignored)\n",
825                                     fname, lineno, cargv[0] );
826                         }
827                 }
828                 free( saveline );
829         }
830         fclose( fp );
831         return( 0 );
832 }
833
834 static int
835 fp_parse_line(
836     char        *line,
837     int         *argcp,
838     char        **argv
839 )
840 {
841         char *  token;
842
843         *argcp = 0;
844         for ( token = strtok_quote( line, " \t" ); token != NULL;
845             token = strtok_quote( NULL, " \t" ) ) {
846                 if ( *argcp == MAXARGS ) {
847                         Debug( LDAP_DEBUG_ANY, "Too many tokens (max %d)\n",
848                             MAXARGS, 0, 0 );
849                         return( 1 );
850                 }
851                 argv[(*argcp)++] = token;
852         }
853         argv[*argcp] = NULL;
854         return 0;
855 }
856
857 static char *
858 strtok_quote( char *line, char *sep )
859 {
860         int             inquote;
861         char            *tmp;
862         static char     *next;
863
864         if ( line != NULL ) {
865                 next = line;
866         }
867         while ( *next && strchr( sep, *next ) ) {
868                 next++;
869         }
870
871         if ( *next == '\0' ) {
872                 next = NULL;
873                 return( NULL );
874         }
875         tmp = next;
876
877         for ( inquote = 0; *next; ) {
878                 switch ( *next ) {
879                 case '"':
880                         if ( inquote ) {
881                                 inquote = 0;
882                         } else {
883                                 inquote = 1;
884                         }
885                         AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
886                         break;
887
888                 case '\\':
889                         if ( next[1] )
890                                 AC_MEMCPY( next,
891                                             next + 1, strlen( next + 1 ) + 1 );
892                         next++;         /* dont parse the escaped character */
893                         break;
894
895                 default:
896                         if ( ! inquote ) {
897                                 if ( strchr( sep, *next ) != NULL ) {
898                                         *next++ = '\0';
899                                         return( tmp );
900                                 }
901                         }
902                         next++;
903                         break;
904                 }
905         }
906
907         return( tmp );
908 }
909
910 static char     buf[BUFSIZ];
911 static char     *line;
912 static int      lmax, lcur;
913
914 #define CATLINE( buf )  { \
915         int     len; \
916         len = strlen( buf ); \
917         while ( lcur + len + 1 > lmax ) { \
918                 lmax += BUFSIZ; \
919                 line = (char *) ch_realloc( line, lmax ); \
920         } \
921         strcpy( line + lcur, buf ); \
922         lcur += len; \
923 }
924
925 static char *
926 fp_getline( FILE *fp, int *lineno )
927 {
928         char            *p;
929
930         lcur = 0;
931         CATLINE( buf );
932         (*lineno)++;
933
934         /* hack attack - keeps us from having to keep a stack of bufs... */
935         if ( strncasecmp( line, "include", 7 ) == 0 ) {
936                 buf[0] = '\0';
937                 return( line );
938         }
939
940         while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
941                 if ( (p = strchr( buf, '\n' )) != NULL ) {
942                         *p = '\0';
943                 }
944                 if ( ! isspace( (unsigned char) buf[0] ) ) {
945                         return( line );
946                 }
947
948                 CATLINE( buf );
949                 (*lineno)++;
950         }
951         buf[0] = '\0';
952
953         return( line[0] ? line : NULL );
954 }
955
956 static void
957 fp_getline_init( int *lineno )
958 {
959         *lineno = -1;
960         buf[0] = '\0';
961 }