3 * Copyright 1998-2000 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.
66 Debug( LDAP_DEBUG_CONFIG, "Config: opening config file \"%s\"\n",
69 if ( (fp = fopen( fname, "r" )) == NULL ) {
75 while ( (line = getline( fp )) != NULL ) {
76 /* skip comments and blank lines */
77 if ( line[0] == '#' || line[0] == '\0' ) {
81 Debug( LDAP_DEBUG_CONFIG, "Config: (%s)\n", line, 0, 0 );
83 parse_line( line, &cargc, cargv );
86 fprintf( stderr, "line %d: bad config line (ignored)\n", lineno );
90 /* replication log file to which changes are appended */
91 if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
93 * if slapd_replogfile has a value, the -r option was given,
94 * so use that value. If slapd_replogfile has length == 0,
95 * then we should use the value in the config file we're reading.
97 if ( sglob->slapd_replogfile[ 0 ] == '\0' ) {
100 "line %d: missing filename in \"replogfile ",
102 fprintf( stderr, "<filename>\" line\n" );
103 exit( EXIT_FAILURE );
104 } else if ( cargc > 2 && *cargv[2] != '#' ) {
106 "line %d: extra cruft at the end of \"replogfile %s\"",
108 fprintf( stderr, "line (ignored)\n" );
110 strcpy( sglob->slapd_replogfile, cargv[1] );
112 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
113 add_replica( cargv, cargc );
115 /* include another config file */
116 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
122 LDAP_LOG(( "config", LDAP_LEVEL_CRIT,
123 "%s: line %d: missing filename in \"include "
124 "<filename>\" line.\n", fname, lineno ));
126 Debug( LDAP_DEBUG_ANY,
127 "%s: line %d: missing filename in \"include <filename>\" line\n",
133 savefname = strdup( cargv[1] );
136 if ( slurpd_read_config( savefname ) != 0 ) {
141 lineno = savelineno - 1;
145 Debug( LDAP_DEBUG_CONFIG,
146 "Config: ** configuration file successfully read and parsed\n",
155 * Parse one line of input.
167 for ( token = strtok_quote( line, " \t" ); token != NULL;
168 token = strtok_quote( NULL, " \t" ) ) {
169 argv[(*argcp)++] = token;
187 if ( line != NULL ) {
190 while ( *next && strchr( sep, *next ) ) {
194 if ( *next == '\0' ) {
200 for ( inquote = 0; *next; ) {
208 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
213 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
214 next++; /* dont parse the escaped character */
219 if ( strchr( sep, *next ) != NULL ) {
232 #define CATLINE( buf ) { \
234 len = strlen( buf ); \
235 while ( lcur + len + 1 > lmax ) { \
237 line = (char *) ch_realloc( line, lmax ); \
239 strcpy( line + lcur, buf ); \
246 * Get a line of input.
254 static char buf[BUFSIZ];
256 static int lmax, lcur;
260 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
261 if ( (p = strchr( buf, '\n' )) != NULL ) {
262 if( p > buf && p[-1] == '\r' ) --p;
266 if ( ! isspace( (unsigned char) buf[0] ) ) {
270 /* change leading whitespace to space */
277 return( line[0] ? line : NULL );
282 * Add a node to the array of replicas.
292 nr = ++sglob->num_replicas;
293 sglob->replicas = (Ri **) ch_realloc( sglob->replicas,
294 ( nr + 1 ) * sizeof( Re * ));
295 if ( sglob->replicas == NULL ) {
296 fprintf( stderr, "out of memory, add_replica\n" );
297 exit( EXIT_FAILURE );
299 sglob->replicas[ nr ] = NULL;
301 if ( Ri_init( &(sglob->replicas[ nr - 1 ])) < 0 ) {
302 fprintf( stderr, "out of memory, Ri_init\n" );
303 exit( EXIT_FAILURE );
305 if ( parse_replica_line( cargv, cargc,
306 sglob->replicas[ nr - 1] ) < 0 ) {
307 /* Something bad happened - back out */
309 "Warning: failed to add replica \"%s:%d - ignoring replica\n",
310 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
311 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
312 sglob->replicas[ nr - 1 ]->ri_port );
313 sglob->replicas[ nr - 1] = NULL;
314 sglob->num_replicas--;
316 Debug( LDAP_DEBUG_CONFIG,
317 "Config: ** successfully added replica \"%s:%d\"\n",
318 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
319 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
320 sglob->replicas[ nr - 1 ]->ri_port, 0 );
321 sglob->replicas[ nr - 1]->ri_stel =
322 sglob->st->st_add( sglob->st,
323 sglob->replicas[ nr - 1 ] );
324 if ( sglob->replicas[ nr - 1]->ri_stel == NULL ) {
325 fprintf( stderr, "Failed to add status element structure\n" );
326 exit( EXIT_FAILURE );
334 * Parse a "replica" line from the config file. replica lines should be
335 * in the following format:
336 * replica host=<hostname:portnumber> binddn=<binddn>
337 * bindmethod="simple" credentials=<creds>
340 * <hostname:portnumber> describes the host name and port number where the
341 * replica is running,
343 * <binddn> is the DN to bind to the replica slapd as,
345 * bindmethod is "simple", and
347 * <creds> are the credentials (e.g. password) for binddn. <creds> are
348 * only used for bindmethod=simple.
350 * The "replica" config file line may be split across multiple lines. If
351 * a line begins with whitespace, it is considered a continuation of the
357 #define GOT_ALL ( GOT_HOST | GOT_DN | GOT_METHOD )
371 for ( i = 1; i < cargc; i++ ) {
372 if ( !strncasecmp( cargv[ i ], HOSTSTR, sizeof( HOSTSTR ) - 1 ) ) {
373 val = cargv[ i ] + sizeof( HOSTSTR ); /* '\0' string terminator accounts for '=' */
374 if (( hp = strchr( val, ':' )) != NULL ) {
377 ri->ri_port = atoi( hp );
379 if ( ri->ri_port <= 0 ) {
382 ri->ri_hostname = strdup( val );
384 } else if ( !strncasecmp( cargv[ i ],
385 SUFFIXSTR, sizeof( SUFFIXSTR ) - 1 ) ) {
387 } else if ( !strncasecmp( cargv[ i ], TLSSTR, sizeof( TLSSTR ) - 1 ) ) {
388 val = cargv[ i ] + sizeof( TLSSTR );
389 if( !strcasecmp( val, TLSCRITICALSTR ) ) {
390 ri->ri_tls = TLS_CRITICAL;
394 } else if ( !strncasecmp( cargv[ i ],
395 BINDDNSTR, sizeof( BINDDNSTR ) - 1 ) ) {
396 val = cargv[ i ] + sizeof( BINDDNSTR );
397 ri->ri_bind_dn = strdup( val );
399 } else if ( !strncasecmp( cargv[ i ], BINDMETHSTR,
400 sizeof( BINDMETHSTR ) - 1 ) ) {
401 val = cargv[ i ] + sizeof( BINDMETHSTR );
402 if ( !strcasecmp( val, KERBEROSSTR )) {
403 fprintf( stderr, "Error: a bind method of \"kerberos\" was\n" );
404 fprintf( stderr, "specified in the slapd configuration file.\n" );
405 fprintf( stderr, "slurpd no longer supports Kerberos.\n" );
406 exit( EXIT_FAILURE );
407 } else if ( !strcasecmp( val, SIMPLESTR )) {
408 ri->ri_bind_method = AUTH_SIMPLE;
410 } else if ( !strcasecmp( val, SASLSTR )) {
411 ri->ri_bind_method = AUTH_SASL;
414 ri->ri_bind_method = -1;
416 } else if ( !strncasecmp( cargv[ i ],
417 SASLMECHSTR, sizeof( SASLMECHSTR ) - 1 ) ) {
418 val = cargv[ i ] + sizeof( SASLMECHSTR );
420 ri->ri_saslmech = strdup( val );
421 } else if ( !strncasecmp( cargv[ i ],
422 CREDSTR, sizeof( CREDSTR ) - 1 ) ) {
423 val = cargv[ i ] + sizeof( CREDSTR );
424 ri->ri_password = strdup( val );
425 } else if ( !strncasecmp( cargv[ i ],
426 SECPROPSSTR, sizeof( SECPROPSSTR ) - 1 ) ) {
427 val = cargv[ i ] + sizeof( SECPROPSSTR );
428 ri->ri_secprops = strdup( val );
429 } else if ( !strncasecmp( cargv[ i ],
430 REALMSTR, sizeof( REALMSTR ) - 1 ) ) {
431 val = cargv[ i ] + sizeof( REALMSTR );
432 ri->ri_realm = strdup( val );
433 } else if ( !strncasecmp( cargv[ i ],
434 AUTHCSTR, sizeof( AUTHCSTR ) - 1 ) ) {
435 val = cargv[ i ] + sizeof( AUTHCSTR );
436 ri->ri_authcId = strdup( val );
437 } else if ( !strncasecmp( cargv[ i ],
438 OLDAUTHCSTR, sizeof( OLDAUTHCSTR ) - 1 ) ) {
439 /* Old authcID is provided for some backwards compatibility */
440 val = cargv[ i ] + sizeof( OLDAUTHCSTR );
441 ri->ri_authcId = strdup( val );
442 } else if ( !strncasecmp( cargv[ i ],
443 AUTHZSTR, sizeof( AUTHZSTR ) - 1 ) ) {
444 val = cargv[ i ] + sizeof( AUTHZSTR );
445 ri->ri_authzId = strdup( val );
446 } else if ( !strncasecmp( cargv[ i ],
447 SRVTABSTR, sizeof( SRVTABSTR ) - 1 ) ) {
448 val = cargv[ i ] + sizeof( SRVTABSTR );
449 if ( ri->ri_srvtab != NULL ) {
450 free( ri->ri_srvtab );
452 ri->ri_srvtab = strdup( val );
455 "Error: parse_replica_line: unknown keyword \"%s\"\n",
460 if ( ri->ri_bind_method == AUTH_SASL) {
461 if ((gots & GOT_MECH) == 0) {
462 fprintf( stderr, "Error: \"replica\" line needs SASLmech flag in " );
463 fprintf( stderr, "slapd config file, line %d\n", lineno );
467 else if ( gots != GOT_ALL ) {
468 fprintf( stderr, "Error: Malformed \"replica\" line in slapd " );
469 fprintf( stderr, "config file, line %d\n", lineno );