2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 The OpenLDAP Foundation.
5 * Portions Copyright 2003 Mark Benson.
6 * Portions Copyright 2002 John Morrissey.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
17 /* Portions Copyright (c) 1996 Regents of the University of Michigan.
18 * All rights reserved.
20 * Redistribution and use in source and binary forms are permitted
21 * provided that this notice is preserved and that due credit is given
22 * to the University of Michigan at Ann Arbor. The name of the University
23 * may not be used to endorse or promote products derived from this
24 * software without specific prior written permission. This software
25 * is provided ``as is'' without express or implied warranty.
28 * This work was originally developed by the University of Michigan
29 * (as part of U-MICH LDAP). Additional signficant contributors
37 * config.c - configuration file handling routines
44 #include <ac/stdlib.h>
45 #include <ac/string.h>
46 #include <ac/socket.h>
57 /* Forward declarations */
58 static void add_replica LDAP_P(( char **, int ));
59 static int parse_replica_line LDAP_P(( char **, int, Ri *));
60 static void parse_line LDAP_P(( char * ));
61 static char *getline LDAP_P(( FILE * ));
62 static char *strtok_quote LDAP_P(( char *, char * ));
64 int cargc = 0, cargv_size = 0;
66 /* current config file line # */
69 char *slurpd_pid_file = NULL;
70 char *slurpd_args_file = NULL;
73 * Read the slapd config file, looking only for config options we're
74 * interested in. Since we haven't detached from the controlling
75 * terminal yet, we just perror() and fprintf here.
85 if ( cargv == NULL ) {
86 cargv = ch_calloc( ARGS_STEP + 1, sizeof(*cargv) );
87 cargv_size = ARGS_STEP + 1;
91 LDAP_LOG ( CONFIG, ARGS,
92 "slurpd_read_config: Config: opening config file \"%s\"\n",
95 Debug( LDAP_DEBUG_CONFIG, "Config: opening config file \"%s\"\n",
99 if ( (fp = fopen( fname, "r" )) == NULL ) {
101 exit( EXIT_FAILURE );
105 while ( (line = getline( fp )) != NULL ) {
106 /* skip comments and blank lines */
107 if ( line[0] == '#' || line[0] == '\0' ) {
112 LDAP_LOG ( CONFIG, DETAIL1,
113 "slurpd_read_config: Config: (%s)\n", line, 0, 0 );
115 Debug( LDAP_DEBUG_CONFIG, "Config: (%s)\n", line, 0, 0 );
121 fprintf( stderr, "line %d: bad config line (ignored)\n", lineno );
125 /* replication log file to which changes are appended */
126 if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
128 * if slapd_replogfile has a value, the -r option was given,
129 * so use that value. If slapd_replogfile has length == 0,
130 * then we should use the value in the config file we're reading.
132 if ( sglob->slapd_replogfile[ 0 ] == '\0' ) {
135 "line %d: missing filename in \"replogfile ",
137 fprintf( stderr, "<filename>\" line\n" );
138 exit( EXIT_FAILURE );
139 } else if ( cargc > 2 && *cargv[2] != '#' ) {
141 "line %d: extra cruft at the end of \"replogfile %s\"",
143 fprintf( stderr, "line (ignored)\n" );
145 LUTIL_SLASHPATH( cargv[1] );
146 strcpy( sglob->slapd_replogfile, cargv[1] );
148 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
149 add_replica( cargv, cargc );
151 /* include another config file */
152 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
158 LDAP_LOG( CONFIG, CRIT,
159 "%s: line %d: missing filename in \"include "
160 "<filename>\" line.\n", fname, lineno , 0 );
162 Debug( LDAP_DEBUG_ANY,
163 "%s: line %d: missing filename in \"include <filename>\" line\n",
169 LUTIL_SLASHPATH( cargv[1] );
170 savefname = strdup( cargv[1] );
173 if ( slurpd_read_config( savefname ) != 0 ) {
178 lineno = savelineno - 1;
180 } else if ( strcasecmp( cargv[0], "replica-pidfile" ) == 0 ) {
183 LDAP_LOG( CONFIG, CRIT,
184 "%s: line %d missing file name in \"replica-pidfile <file>\" "
185 "line.\n", fname, lineno, 0 );
187 Debug( LDAP_DEBUG_ANY,
188 "%s: line %d: missing file name in \"replica-pidfile <file>\" line\n",
195 LUTIL_SLASHPATH( cargv[1] );
196 slurpd_pid_file = ch_strdup( cargv[1] );
198 } else if ( strcasecmp( cargv[0], "replica-argsfile" ) == 0 ) {
201 LDAP_LOG( CONFIG, CRIT,
202 "%s: %d: missing file name in "
203 "\"argsfile <file>\" line.\n",
206 Debug( LDAP_DEBUG_ANY,
207 "%s: line %d: missing file name in \"argsfile <file>\" line\n",
214 LUTIL_SLASHPATH( cargv[1] );
215 slurpd_args_file = ch_strdup( cargv[1] );
217 } else if ( strcasecmp( cargv[0], "replicationinterval" ) == 0 ) {
221 LDAP_LOG( CONFIG, CRIT, "%s: %d: missing interval in "
222 "\"replicationinterval <seconds>\" line.\n",
225 Debug( LDAP_DEBUG_ANY, "%s: line %d: missing interval in "
226 "\"replicationinterval <seconds>\" line\n",
232 c = atoi( cargv[1] );
235 LDAP_LOG( CONFIG, CRIT, "%s: line %d: invalid interval "
236 "(%d) in \"replicationinterval <seconds>\" line\n",
239 Debug( LDAP_DEBUG_ANY, "%s: line %d: invalid interval "
240 "(%d) in \"replicationinterval <seconds>\" line\n",
247 sglob->no_work_interval = c;
252 LDAP_LOG ( CONFIG, RESULTS,
253 "slurpd_read_config: Config: "
254 "** configuration file successfully read and parsed\n", 0, 0, 0 );
256 Debug( LDAP_DEBUG_CONFIG,
257 "Config: ** configuration file successfully read and parsed\n",
267 * Parse one line of input.
277 for ( token = strtok_quote( line, " \t" ); token != NULL;
278 token = strtok_quote( NULL, " \t" ) )
280 if ( cargc == cargv_size - 1 ) {
282 tmp = ch_realloc( cargv, (cargv_size + ARGS_STEP) *
289 cargv_size += ARGS_STEP;
292 cargv[cargc++] = token;
310 if ( line != NULL ) {
313 while ( *next && strchr( sep, *next ) ) {
317 if ( *next == '\0' ) {
323 for ( inquote = 0; *next; ) {
331 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
336 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
337 next++; /* dont parse the escaped character */
342 if ( strchr( sep, *next ) != NULL ) {
355 #define CATLINE( buf ) { \
357 len = strlen( buf ); \
358 while ( lcur + len + 1 > lmax ) { \
360 line = (char *) ch_realloc( line, lmax ); \
362 strcpy( line + lcur, buf ); \
369 * Get a line of input.
377 static char buf[BUFSIZ];
379 static int lmax, lcur;
383 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
384 if ( (p = strchr( buf, '\n' )) != NULL ) {
385 if( p > buf && p[-1] == '\r' ) --p;
389 if ( ! isspace( (unsigned char) buf[0] ) ) {
393 /* change leading whitespace to space */
400 return( line[0] ? line : NULL );
405 * Add a node to the array of replicas.
415 nr = ++sglob->num_replicas;
416 sglob->replicas = (Ri **) ch_realloc( sglob->replicas,
417 ( nr + 1 ) * sizeof( Re * ));
418 if ( sglob->replicas == NULL ) {
419 fprintf( stderr, "out of memory, add_replica\n" );
420 exit( EXIT_FAILURE );
422 sglob->replicas[ nr ] = NULL;
424 if ( Ri_init( &(sglob->replicas[ nr - 1 ])) < 0 ) {
425 fprintf( stderr, "out of memory, Ri_init\n" );
426 exit( EXIT_FAILURE );
428 if ( parse_replica_line( cargv, cargc,
429 sglob->replicas[ nr - 1] ) < 0 ) {
430 /* Something bad happened - back out */
432 "Warning: failed to add replica \"%s:%d - ignoring replica\n",
433 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
434 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
435 sglob->replicas[ nr - 1 ]->ri_port );
436 sglob->replicas[ nr - 1] = NULL;
437 sglob->num_replicas--;
440 LDAP_LOG ( CONFIG, RESULTS,
441 "add_replica: Config: ** successfully added replica \"%s%d\"\n",
442 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
443 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
444 sglob->replicas[ nr - 1 ]->ri_port, 0 );
446 Debug( LDAP_DEBUG_CONFIG,
447 "Config: ** successfully added replica \"%s:%d\"\n",
448 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
449 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
450 sglob->replicas[ nr - 1 ]->ri_port, 0 );
452 sglob->replicas[ nr - 1]->ri_stel =
453 sglob->st->st_add( sglob->st,
454 sglob->replicas[ nr - 1 ] );
455 if ( sglob->replicas[ nr - 1]->ri_stel == NULL ) {
456 fprintf( stderr, "Failed to add status element structure\n" );
457 exit( EXIT_FAILURE );
465 * Parse a "replica" line from the config file. replica lines should be
466 * in the following format:
467 * replica host=<hostname:portnumber> binddn=<binddn>
468 * bindmethod="simple" credentials=<creds>
471 * <hostname:portnumber> describes the host name and port number where the
472 * replica is running,
474 * <binddn> is the DN to bind to the replica slapd as,
476 * bindmethod is "simple", and
478 * <creds> are the credentials (e.g. password) for binddn. <creds> are
479 * only used for bindmethod=simple.
481 * The "replica" config file line may be split across multiple lines. If
482 * a line begins with whitespace, it is considered a continuation of the
488 #define GOT_ALL ( GOT_HOST | GOT_DN | GOT_METHOD )
503 for ( i = 1; i < cargc; i++ ) {
504 if ( !strncasecmp( cargv[ i ], HOSTSTR, sizeof( HOSTSTR ) - 1 ) ) {
505 if ( gots & GOT_HOST ) {
506 fprintf( stderr, "Error: Malformed \"replica\" line in slapd config " );
507 fprintf( stderr, "file, too many host or uri names specified, line %d\n",
511 val = cargv[ i ] + sizeof( HOSTSTR ); /* '\0' string terminator accounts for '=' */
512 if (( hp = strchr( val, ':' )) != NULL ) {
515 ri->ri_port = atoi( hp );
517 if ( ri->ri_port <= 0 ) {
520 ri->ri_hostname = strdup( val );
522 } else if ( !strncasecmp( cargv[ i ], URISTR, sizeof( URISTR ) - 1 ) ) {
523 if ( gots & GOT_HOST ) {
524 fprintf( stderr, "Error: Malformed \"replica\" line in slapd config " );
525 fprintf( stderr, "file, too many host or uri names specified, line %d\n",
529 if ( ldap_url_parse( cargv[ i ] + sizeof( URISTR ), &ludp ) != LDAP_SUCCESS ) {
530 fprintf( stderr, "Error: Malformed \"replica\" line in slapd config " );
531 fprintf( stderr, "file, bad uri format specified, line %d\n",
535 if (ludp->lud_host == NULL) {
536 fprintf( stderr, "Error: Malformed \"replica\" line in slapd config " );
537 fprintf( stderr, "file, missing uri hostname, line %d\n",
541 ri->ri_hostname = strdup ( ludp->lud_host );
542 ri->ri_port = ludp->lud_port;
543 ri->ri_uri = strdup ( cargv[ i ] + sizeof( URISTR ) );
544 ldap_free_urldesc( ludp );
546 } else if ( !strncasecmp( cargv[ i ],
547 ATTRSTR, sizeof( ATTRSTR ) - 1 ) ) {
549 } else if ( !strncasecmp( cargv[ i ],
550 SUFFIXSTR, sizeof( SUFFIXSTR ) - 1 ) ) {
552 } else if ( !strncasecmp( cargv[i], STARTTLSSTR, sizeof(STARTTLSSTR)-1 )) {
553 val = cargv[ i ] + sizeof( STARTTLSSTR );
554 if( !strcasecmp( val, CRITICALSTR ) ) {
555 ri->ri_tls = TLS_CRITICAL;
559 } else if ( !strncasecmp( cargv[ i ], TLSSTR, sizeof( TLSSTR ) - 1 ) ) {
560 val = cargv[ i ] + sizeof( TLSSTR );
561 if( !strcasecmp( val, CRITICALSTR ) ) {
562 ri->ri_tls = TLS_CRITICAL;
566 } else if ( !strncasecmp( cargv[ i ],
567 BINDDNSTR, sizeof( BINDDNSTR ) - 1 ) ) {
568 val = cargv[ i ] + sizeof( BINDDNSTR );
569 ri->ri_bind_dn = strdup( val );
571 } else if ( !strncasecmp( cargv[ i ], BINDMETHSTR,
572 sizeof( BINDMETHSTR ) - 1 ) ) {
573 val = cargv[ i ] + sizeof( BINDMETHSTR );
574 if ( !strcasecmp( val, KERBEROSSTR )) {
575 fprintf( stderr, "Error: a bind method of \"kerberos\" was\n" );
576 fprintf( stderr, "specified in the slapd configuration file.\n" );
577 fprintf( stderr, "slurpd no longer supports Kerberos.\n" );
578 exit( EXIT_FAILURE );
579 } else if ( !strcasecmp( val, SIMPLESTR )) {
580 ri->ri_bind_method = LDAP_AUTH_SIMPLE;
582 } else if ( !strcasecmp( val, SASLSTR )) {
583 ri->ri_bind_method = LDAP_AUTH_SASL;
586 ri->ri_bind_method = -1;
588 } else if ( !strncasecmp( cargv[ i ],
589 SASLMECHSTR, sizeof( SASLMECHSTR ) - 1 ) ) {
590 val = cargv[ i ] + sizeof( SASLMECHSTR );
592 ri->ri_saslmech = strdup( val );
593 } else if ( !strncasecmp( cargv[ i ],
594 CREDSTR, sizeof( CREDSTR ) - 1 ) ) {
595 val = cargv[ i ] + sizeof( CREDSTR );
596 ri->ri_password = strdup( val );
597 } else if ( !strncasecmp( cargv[ i ],
598 SECPROPSSTR, sizeof( SECPROPSSTR ) - 1 ) ) {
599 val = cargv[ i ] + sizeof( SECPROPSSTR );
600 ri->ri_secprops = strdup( val );
601 } else if ( !strncasecmp( cargv[ i ],
602 REALMSTR, sizeof( REALMSTR ) - 1 ) ) {
603 val = cargv[ i ] + sizeof( REALMSTR );
604 ri->ri_realm = strdup( val );
605 } else if ( !strncasecmp( cargv[ i ],
606 AUTHCSTR, sizeof( AUTHCSTR ) - 1 ) ) {
607 val = cargv[ i ] + sizeof( AUTHCSTR );
608 ri->ri_authcId = strdup( val );
609 } else if ( !strncasecmp( cargv[ i ],
610 OLDAUTHCSTR, sizeof( OLDAUTHCSTR ) - 1 ) ) {
611 /* Old authcID is provided for some backwards compatibility */
612 val = cargv[ i ] + sizeof( OLDAUTHCSTR );
613 ri->ri_authcId = strdup( val );
614 } else if ( !strncasecmp( cargv[ i ],
615 AUTHZSTR, sizeof( AUTHZSTR ) - 1 ) ) {
616 val = cargv[ i ] + sizeof( AUTHZSTR );
617 ri->ri_authzId = strdup( val );
618 } else if ( !strncasecmp( cargv[ i ],
619 SRVTABSTR, sizeof( SRVTABSTR ) - 1 ) ) {
620 val = cargv[ i ] + sizeof( SRVTABSTR );
621 if ( ri->ri_srvtab != NULL ) {
622 free( ri->ri_srvtab );
624 ri->ri_srvtab = strdup( val );
627 "Error: parse_replica_line: unknown keyword \"%s\"\n",
632 if ( ri->ri_bind_method == LDAP_AUTH_SASL) {
633 if ((gots & GOT_MECH) == 0) {
634 fprintf( stderr, "Error: \"replica\" line needs SASLmech flag in " );
635 fprintf( stderr, "slapd config file, line %d\n", lineno );
638 } else if ( gots != GOT_ALL ) {
639 fprintf( stderr, "Error: Malformed \"replica\" line in slapd " );
640 fprintf( stderr, "config file, line %d\n", lineno );