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 ) {
265 if ( ! isspace( (unsigned char) buf[0] ) ) {
273 return( line[0] ? line : NULL );
278 * Add a node to the array of replicas.
288 nr = ++sglob->num_replicas;
289 sglob->replicas = (Ri **) ch_realloc( sglob->replicas,
290 ( nr + 1 ) * sizeof( Re * ));
291 if ( sglob->replicas == NULL ) {
292 fprintf( stderr, "out of memory, add_replica\n" );
293 exit( EXIT_FAILURE );
295 sglob->replicas[ nr ] = NULL;
297 if ( Ri_init( &(sglob->replicas[ nr - 1 ])) < 0 ) {
298 fprintf( stderr, "out of memory, Ri_init\n" );
299 exit( EXIT_FAILURE );
301 if ( parse_replica_line( cargv, cargc,
302 sglob->replicas[ nr - 1] ) < 0 ) {
303 /* Something bad happened - back out */
305 "Warning: failed to add replica \"%s:%d - ignoring replica\n",
306 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
307 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
308 sglob->replicas[ nr - 1 ]->ri_port );
309 sglob->replicas[ nr - 1] = NULL;
310 sglob->num_replicas--;
312 Debug( LDAP_DEBUG_CONFIG,
313 "Config: ** successfully added replica \"%s:%d\"\n",
314 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
315 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
316 sglob->replicas[ nr - 1 ]->ri_port, 0 );
317 sglob->replicas[ nr - 1]->ri_stel =
318 sglob->st->st_add( sglob->st,
319 sglob->replicas[ nr - 1 ] );
320 if ( sglob->replicas[ nr - 1]->ri_stel == NULL ) {
321 fprintf( stderr, "Failed to add status element structure\n" );
322 exit( EXIT_FAILURE );
330 * Parse a "replica" line from the config file. replica lines should be
331 * in the following format:
332 * replica host=<hostname:portnumber> binddn=<binddn>
333 * bindmethod="simple" credentials=<creds>
336 * <hostname:portnumber> describes the host name and port number where the
337 * replica is running,
339 * <binddn> is the DN to bind to the replica slapd as,
341 * bindmethod is "simple", and
343 * <creds> are the credentials (e.g. password) for binddn. <creds> are
344 * only used for bindmethod=simple.
346 * The "replica" config file line may be split across multiple lines. If
347 * a line begins with whitespace, it is considered a continuation of the
353 #define GOT_ALL ( GOT_HOST | GOT_DN | GOT_METHOD )
367 for ( i = 1; i < cargc; i++ ) {
368 if ( !strncasecmp( cargv[ i ], HOSTSTR, strlen( HOSTSTR ))) {
369 val = cargv[ i ] + strlen( HOSTSTR ) + 1;
370 if (( hp = strchr( val, ':' )) != NULL ) {
373 ri->ri_port = atoi( hp );
375 if ( ri->ri_port <= 0 ) {
378 ri->ri_hostname = strdup( val );
380 } else if ( !strncasecmp( cargv[ i ], TLSSTR, strlen( TLSSTR ))) {
381 val = cargv[ i ] + strlen( TLSSTR ) + 1;
382 if( !strcasecmp( val, TLSCRITICALSTR ) ) {
383 ri->ri_tls = TLS_CRITICAL;
387 } else if ( !strncasecmp( cargv[ i ],
388 BINDDNSTR, strlen( BINDDNSTR ))) {
389 val = cargv[ i ] + strlen( BINDDNSTR ) + 1;
390 ri->ri_bind_dn = strdup( val );
392 } else if ( !strncasecmp( cargv[ i ], BINDMETHSTR,
393 strlen( BINDMETHSTR ))) {
394 val = cargv[ i ] + strlen( BINDMETHSTR ) + 1;
395 if ( !strcasecmp( val, KERBEROSSTR )) {
396 fprintf( stderr, "Error: a bind method of \"kerberos\" was\n" );
397 fprintf( stderr, "specified in the slapd configuration file.\n" );
398 fprintf( stderr, "slurpd no longer supports Kerberos.\n" );
399 exit( EXIT_FAILURE );
400 } else if ( !strcasecmp( val, SIMPLESTR )) {
401 ri->ri_bind_method = AUTH_SIMPLE;
403 } else if ( !strcasecmp( val, SASLSTR )) {
404 ri->ri_bind_method = AUTH_SASL;
407 ri->ri_bind_method = -1;
409 } else if ( !strncasecmp( cargv[ i ], SASLMECHSTR, strlen( SASLMECHSTR ))) {
410 val = cargv[ i ] + strlen( SASLMECHSTR ) + 1;
412 ri->ri_saslmech = strdup( val );
413 } else if ( !strncasecmp( cargv[ i ], CREDSTR, strlen( CREDSTR ))) {
414 val = cargv[ i ] + strlen( CREDSTR ) + 1;
415 ri->ri_password = strdup( val );
416 } else if ( !strncasecmp( cargv[ i ], SECPROPSSTR, strlen( SECPROPSSTR ))) {
417 val = cargv[ i ] + strlen( SECPROPSSTR ) + 1;
418 ri->ri_secprops = strdup( val );
419 } else if ( !strncasecmp( cargv[ i ], REALMSTR, strlen( REALMSTR ))) {
420 val = cargv[ i ] + strlen( REALMSTR ) + 1;
421 ri->ri_realm = strdup( val );
422 } else if ( !strncasecmp( cargv[ i ], AUTHCSTR, strlen( AUTHCSTR ))) {
423 val = cargv[ i ] + strlen( AUTHCSTR ) + 1;
424 ri->ri_authcId = strdup( val );
425 } else if ( !strncasecmp( cargv[ i ], OLDAUTHCSTR, strlen( OLDAUTHCSTR ))) {
426 /* Old authcID is provided for some backwards compatibility */
427 val = cargv[ i ] + strlen( OLDAUTHCSTR ) + 1;
428 ri->ri_authcId = strdup( val );
429 } else if ( !strncasecmp( cargv[ i ], AUTHZSTR, strlen( AUTHZSTR ))) {
430 val = cargv[ i ] + strlen( AUTHZSTR ) + 1;
431 ri->ri_authzId = strdup( val );
432 } else if ( !strncasecmp( cargv[ i ], SRVTABSTR, strlen( SRVTABSTR ))) {
433 val = cargv[ i ] + strlen( SRVTABSTR ) + 1;
434 if ( ri->ri_srvtab != NULL ) {
435 free( ri->ri_srvtab );
437 ri->ri_srvtab = strdup( val );
440 "Error: parse_replica_line: unknown keyword \"%s\"\n",
445 if ( ri->ri_bind_method == AUTH_SASL) {
446 if ((gots & GOT_MECH) == 0) {
447 fprintf( stderr, "Error: \"replica\" line needs SASLmech flag in " );
448 fprintf( stderr, "slapd config file, line %d\n", lineno );
452 else if ( gots != GOT_ALL ) {
453 fprintf( stderr, "Error: Malformed \"replica\" line in slapd " );
454 fprintf( stderr, "config file, line %d\n", lineno );