3 * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1996 Regents of the University of Michigan.
10 * Redistribution and use in source and binary forms are permitted
11 * provided that this notice is preserved and that due credit is given
12 * to the University of Michigan at Ann Arbor. The name of the University
13 * may not be used to endorse or promote products derived from this
14 * software without specific prior written permission. This software
15 * is provided ``as is'' without express or implied warranty.
20 * config.c - configuration file handling routines
27 #include <ac/stdlib.h>
28 #include <ac/string.h>
29 #include <ac/socket.h>
39 /* Forward declarations */
40 static void add_replica LDAP_P(( char **, int ));
41 static int parse_replica_line LDAP_P(( char **, int, Ri *));
42 static void parse_line LDAP_P(( char *, int *, char ** ));
43 static char *getline LDAP_P(( FILE * ));
44 static char *strtok_quote LDAP_P(( char *, char * ));
46 /* current config file line # */
52 * Read the slapd config file, looking only for config options we're
53 * interested in. Since we haven't detached from the controlling
54 * terminal yet, we just perror() and fprintf here.
67 LDAP_LOG (( "config", LDAP_LEVEL_ARGS,
68 "slurpd_read_config: Config: opening config file \"%s\"\n", fname ));
70 Debug( LDAP_DEBUG_CONFIG, "Config: opening config file \"%s\"\n",
74 if ( (fp = fopen( fname, "r" )) == NULL ) {
80 while ( (line = getline( fp )) != NULL ) {
81 /* skip comments and blank lines */
82 if ( line[0] == '#' || line[0] == '\0' ) {
87 LDAP_LOG (( "config", LDAP_LEVEL_DETAIL1,
88 "slurpd_read_config: Config: (%s)\n", line ));
90 Debug( LDAP_DEBUG_CONFIG, "Config: (%s)\n", line, 0, 0 );
93 parse_line( line, &cargc, cargv );
96 fprintf( stderr, "line %d: bad config line (ignored)\n", lineno );
100 /* replication log file to which changes are appended */
101 if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
103 * if slapd_replogfile has a value, the -r option was given,
104 * so use that value. If slapd_replogfile has length == 0,
105 * then we should use the value in the config file we're reading.
107 if ( sglob->slapd_replogfile[ 0 ] == '\0' ) {
110 "line %d: missing filename in \"replogfile ",
112 fprintf( stderr, "<filename>\" line\n" );
113 exit( EXIT_FAILURE );
114 } else if ( cargc > 2 && *cargv[2] != '#' ) {
116 "line %d: extra cruft at the end of \"replogfile %s\"",
118 fprintf( stderr, "line (ignored)\n" );
120 strcpy( sglob->slapd_replogfile, cargv[1] );
122 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
123 add_replica( cargv, cargc );
125 /* include another config file */
126 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
132 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
133 "%s: line %d: missing filename in \"include "
134 "<filename>\" line.\n", fname, lineno ));
136 Debug( LDAP_DEBUG_ANY,
137 "%s: line %d: missing filename in \"include <filename>\" line\n",
143 savefname = strdup( cargv[1] );
146 if ( slurpd_read_config( savefname ) != 0 ) {
151 lineno = savelineno - 1;
156 LDAP_LOG (( "config", LDAP_LEVEL_RESULTS,
157 "slurpd_read_config: Config: "
158 "** configuration file successfully read and parsed\n" ));
160 Debug( LDAP_DEBUG_CONFIG,
161 "Config: ** configuration file successfully read and parsed\n",
171 * Parse one line of input.
183 for ( token = strtok_quote( line, " \t" ); token != NULL;
184 token = strtok_quote( NULL, " \t" ) ) {
185 argv[(*argcp)++] = token;
203 if ( line != NULL ) {
206 while ( *next && strchr( sep, *next ) ) {
210 if ( *next == '\0' ) {
216 for ( inquote = 0; *next; ) {
224 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
229 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
230 next++; /* dont parse the escaped character */
235 if ( strchr( sep, *next ) != NULL ) {
248 #define CATLINE( buf ) { \
250 len = strlen( buf ); \
251 while ( lcur + len + 1 > lmax ) { \
253 line = (char *) ch_realloc( line, lmax ); \
255 strcpy( line + lcur, buf ); \
262 * Get a line of input.
270 static char buf[BUFSIZ];
272 static int lmax, lcur;
276 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
277 if ( (p = strchr( buf, '\n' )) != NULL ) {
278 if( p > buf && p[-1] == '\r' ) --p;
282 if ( ! isspace( (unsigned char) buf[0] ) ) {
286 /* change leading whitespace to space */
293 return( line[0] ? line : NULL );
298 * Add a node to the array of replicas.
308 nr = ++sglob->num_replicas;
309 sglob->replicas = (Ri **) ch_realloc( sglob->replicas,
310 ( nr + 1 ) * sizeof( Re * ));
311 if ( sglob->replicas == NULL ) {
312 fprintf( stderr, "out of memory, add_replica\n" );
313 exit( EXIT_FAILURE );
315 sglob->replicas[ nr ] = NULL;
317 if ( Ri_init( &(sglob->replicas[ nr - 1 ])) < 0 ) {
318 fprintf( stderr, "out of memory, Ri_init\n" );
319 exit( EXIT_FAILURE );
321 if ( parse_replica_line( cargv, cargc,
322 sglob->replicas[ nr - 1] ) < 0 ) {
323 /* Something bad happened - back out */
325 "Warning: failed to add replica \"%s:%d - ignoring replica\n",
326 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
327 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
328 sglob->replicas[ nr - 1 ]->ri_port );
329 sglob->replicas[ nr - 1] = NULL;
330 sglob->num_replicas--;
333 LDAP_LOG (( "config", LDAP_LEVEL_RESULTS,
334 "add_replica: Config: ** successfully added replica \"%s%d\"\n",
335 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
336 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
337 sglob->replicas[ nr - 1 ]->ri_port, 0 ));
339 Debug( LDAP_DEBUG_CONFIG,
340 "Config: ** successfully added replica \"%s:%d\"\n",
341 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
342 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
343 sglob->replicas[ nr - 1 ]->ri_port, 0 );
345 sglob->replicas[ nr - 1]->ri_stel =
346 sglob->st->st_add( sglob->st,
347 sglob->replicas[ nr - 1 ] );
348 if ( sglob->replicas[ nr - 1]->ri_stel == NULL ) {
349 fprintf( stderr, "Failed to add status element structure\n" );
350 exit( EXIT_FAILURE );
358 * Parse a "replica" line from the config file. replica lines should be
359 * in the following format:
360 * replica host=<hostname:portnumber> binddn=<binddn>
361 * bindmethod="simple" credentials=<creds>
364 * <hostname:portnumber> describes the host name and port number where the
365 * replica is running,
367 * <binddn> is the DN to bind to the replica slapd as,
369 * bindmethod is "simple", and
371 * <creds> are the credentials (e.g. password) for binddn. <creds> are
372 * only used for bindmethod=simple.
374 * The "replica" config file line may be split across multiple lines. If
375 * a line begins with whitespace, it is considered a continuation of the
381 #define GOT_ALL ( GOT_HOST | GOT_DN | GOT_METHOD )
395 for ( i = 1; i < cargc; i++ ) {
396 if ( !strncasecmp( cargv[ i ], HOSTSTR, sizeof( HOSTSTR ) - 1 ) ) {
397 val = cargv[ i ] + sizeof( HOSTSTR ); /* '\0' string terminator accounts for '=' */
398 if (( hp = strchr( val, ':' )) != NULL ) {
401 ri->ri_port = atoi( hp );
403 if ( ri->ri_port <= 0 ) {
406 ri->ri_hostname = strdup( val );
408 } else if ( !strncasecmp( cargv[ i ],
409 ATTRSTR, sizeof( ATTRSTR ) - 1 ) ) {
411 } else if ( !strncasecmp( cargv[ i ],
412 SUFFIXSTR, sizeof( SUFFIXSTR ) - 1 ) ) {
414 } else if ( !strncasecmp( cargv[ i ], TLSSTR, sizeof( TLSSTR ) - 1 ) ) {
415 val = cargv[ i ] + sizeof( TLSSTR );
416 if( !strcasecmp( val, TLSCRITICALSTR ) ) {
417 ri->ri_tls = TLS_CRITICAL;
421 } else if ( !strncasecmp( cargv[ i ],
422 BINDDNSTR, sizeof( BINDDNSTR ) - 1 ) ) {
423 val = cargv[ i ] + sizeof( BINDDNSTR );
424 ri->ri_bind_dn = strdup( val );
426 } else if ( !strncasecmp( cargv[ i ], BINDMETHSTR,
427 sizeof( BINDMETHSTR ) - 1 ) ) {
428 val = cargv[ i ] + sizeof( BINDMETHSTR );
429 if ( !strcasecmp( val, KERBEROSSTR )) {
430 fprintf( stderr, "Error: a bind method of \"kerberos\" was\n" );
431 fprintf( stderr, "specified in the slapd configuration file.\n" );
432 fprintf( stderr, "slurpd no longer supports Kerberos.\n" );
433 exit( EXIT_FAILURE );
434 } else if ( !strcasecmp( val, SIMPLESTR )) {
435 ri->ri_bind_method = AUTH_SIMPLE;
437 } else if ( !strcasecmp( val, SASLSTR )) {
438 ri->ri_bind_method = AUTH_SASL;
441 ri->ri_bind_method = -1;
443 } else if ( !strncasecmp( cargv[ i ],
444 SASLMECHSTR, sizeof( SASLMECHSTR ) - 1 ) ) {
445 val = cargv[ i ] + sizeof( SASLMECHSTR );
447 ri->ri_saslmech = strdup( val );
448 } else if ( !strncasecmp( cargv[ i ],
449 CREDSTR, sizeof( CREDSTR ) - 1 ) ) {
450 val = cargv[ i ] + sizeof( CREDSTR );
451 ri->ri_password = strdup( val );
452 } else if ( !strncasecmp( cargv[ i ],
453 SECPROPSSTR, sizeof( SECPROPSSTR ) - 1 ) ) {
454 val = cargv[ i ] + sizeof( SECPROPSSTR );
455 ri->ri_secprops = strdup( val );
456 } else if ( !strncasecmp( cargv[ i ],
457 REALMSTR, sizeof( REALMSTR ) - 1 ) ) {
458 val = cargv[ i ] + sizeof( REALMSTR );
459 ri->ri_realm = strdup( val );
460 } else if ( !strncasecmp( cargv[ i ],
461 AUTHCSTR, sizeof( AUTHCSTR ) - 1 ) ) {
462 val = cargv[ i ] + sizeof( AUTHCSTR );
463 ri->ri_authcId = strdup( val );
464 } else if ( !strncasecmp( cargv[ i ],
465 OLDAUTHCSTR, sizeof( OLDAUTHCSTR ) - 1 ) ) {
466 /* Old authcID is provided for some backwards compatibility */
467 val = cargv[ i ] + sizeof( OLDAUTHCSTR );
468 ri->ri_authcId = strdup( val );
469 } else if ( !strncasecmp( cargv[ i ],
470 AUTHZSTR, sizeof( AUTHZSTR ) - 1 ) ) {
471 val = cargv[ i ] + sizeof( AUTHZSTR );
472 ri->ri_authzId = strdup( val );
473 } else if ( !strncasecmp( cargv[ i ],
474 SRVTABSTR, sizeof( SRVTABSTR ) - 1 ) ) {
475 val = cargv[ i ] + sizeof( SRVTABSTR );
476 if ( ri->ri_srvtab != NULL ) {
477 free( ri->ri_srvtab );
479 ri->ri_srvtab = strdup( val );
482 "Error: parse_replica_line: unknown keyword \"%s\"\n",
487 if ( ri->ri_bind_method == AUTH_SASL) {
488 if ((gots & GOT_MECH) == 0) {
489 fprintf( stderr, "Error: \"replica\" line needs SASLmech flag in " );
490 fprintf( stderr, "slapd config file, line %d\n", lineno );
494 else if ( gots != GOT_ALL ) {
495 fprintf( stderr, "Error: Malformed \"replica\" line in slapd " );
496 fprintf( stderr, "config file, line %d\n", lineno );