3 * Copyright (c) 1996 Regents of the University of Michigan.
6 * Redistribution and use in source and binary forms are permitted
7 * provided that this notice is preserved and that due credit is given
8 * to the University of Michigan at Ann Arbor. The name of the University
9 * may not be used to endorse or promote products derived from this
10 * software without specific prior written permission. This software
11 * is provided ``as is'' without express or implied warranty.
16 * config.c - configuration file handling routines
23 #include <ac/stdlib.h>
24 #include <ac/string.h>
25 #include <ac/socket.h>
35 /* Forward declarations */
36 static void add_replica LDAP_P(( char **, int ));
37 static int parse_replica_line LDAP_P(( char **, int, Ri *));
38 static void parse_line LDAP_P(( char *, int *, char ** ));
39 static char *getline LDAP_P(( FILE * ));
40 static char *strtok_quote LDAP_P(( char *, char * ));
42 /* current config file line # */
48 * Read the slapd config file, looking only for config options we're
49 * interested in. Since we haven't detached from the controlling
50 * terminal yet, we just perror() and fprintf here.
62 Debug( LDAP_DEBUG_CONFIG, "Config: opening config file \"%s\"\n",
65 if ( (fp = fopen( fname, "r" )) == NULL ) {
71 while ( (line = getline( fp )) != NULL ) {
72 /* skip comments and blank lines */
73 if ( line[0] == '#' || line[0] == '\0' ) {
77 Debug( LDAP_DEBUG_CONFIG, "Config: (%s)\n", line, 0, 0 );
79 parse_line( line, &cargc, cargv );
82 fprintf( stderr, "line %d: bad config line (ignored)\n", lineno );
86 /* replication log file to which changes are appended */
87 if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
89 * if slapd_replogfile has a value, the -r option was given,
90 * so use that value. If slapd_replogfile has length == 0,
91 * then we should use the value in the config file we're reading.
93 if ( sglob->slapd_replogfile[ 0 ] == '\0' ) {
96 "line %d: missing filename in \"replogfile ",
98 fprintf( stderr, "<filename>\" line\n" );
100 } else if ( cargc > 2 && *cargv[2] != '#' ) {
102 "line %d: extra cruft at the end of \"replogfile %s\"",
104 fprintf( stderr, "line (ignored)\n" );
106 strcpy( sglob->slapd_replogfile, cargv[1] );
108 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
109 add_replica( cargv, cargc );
113 Debug( LDAP_DEBUG_CONFIG,
114 "Config: ** configuration file successfully read and parsed\n",
123 * Parse one line of input.
135 for ( token = strtok_quote( line, " \t" ); token != NULL;
136 token = strtok_quote( NULL, " \t" ) ) {
137 argv[(*argcp)++] = token;
155 if ( line != NULL ) {
158 while ( *next && strchr( sep, *next ) ) {
162 if ( *next == '\0' ) {
168 for ( inquote = 0; *next; ) {
176 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
181 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
182 next++; /* dont parse the escaped character */
187 if ( strchr( sep, *next ) != NULL ) {
200 #define CATLINE( buf ) { \
202 len = strlen( buf ); \
203 while ( lcur + len + 1 > lmax ) { \
205 line = (char *) ch_realloc( line, lmax ); \
207 strcpy( line + lcur, buf ); \
214 * Get a line of input.
222 static char buf[BUFSIZ];
224 static int lmax, lcur;
228 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
229 if ( (p = strchr( buf, '\n' )) != NULL ) {
233 if ( ! isspace( (unsigned char) buf[0] ) ) {
241 return( line[0] ? line : NULL );
246 * Add a node to the array of replicas.
256 nr = ++sglob->num_replicas;
257 sglob->replicas = (Ri **) ch_realloc( sglob->replicas,
258 ( nr + 1 ) * sizeof( Re * ));
259 if ( sglob->replicas == NULL ) {
260 fprintf( stderr, "out of memory, add_replica\n" );
261 exit( EXIT_FAILURE );
263 sglob->replicas[ nr ] = NULL;
265 if ( Ri_init( &(sglob->replicas[ nr - 1 ])) < 0 ) {
266 fprintf( stderr, "out of memory, Ri_init\n" );
267 exit( EXIT_FAILURE );
269 if ( parse_replica_line( cargv, cargc,
270 sglob->replicas[ nr - 1] ) < 0 ) {
271 /* Something bad happened - back out */
273 "Warning: failed to add replica \"%s:%d - ignoring replica\n",
274 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
275 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
276 sglob->replicas[ nr - 1 ]->ri_port );
277 sglob->replicas[ nr - 1] = NULL;
278 sglob->num_replicas--;
280 Debug( LDAP_DEBUG_CONFIG,
281 "Config: ** successfully added replica \"%s:%d\"\n",
282 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
283 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
284 sglob->replicas[ nr - 1 ]->ri_port, 0 );
285 sglob->replicas[ nr - 1]->ri_stel =
286 sglob->st->st_add( sglob->st,
287 sglob->replicas[ nr - 1 ] );
288 if ( sglob->replicas[ nr - 1]->ri_stel == NULL ) {
289 fprintf( stderr, "Failed to add status element structure\n" );
290 exit( EXIT_FAILURE );
298 * Parse a "replica" line from the config file. replica lines should be
299 * in the following format:
300 * replica host=<hostname:portnumber> binddn=<binddn>
301 * bindmethod="simple" credentials=<creds>
304 * <hostname:portnumber> describes the host name and port number where the
305 * replica is running,
307 * <binddn> is the DN to bind to the replica slapd as,
309 * bindmethod is "simple", and
311 * <creds> are the credentials (e.g. password) for binddn. <creds> are
312 * only used for bindmethod=simple.
314 * The "replica" config file line may be split across multiple lines. If
315 * a line begins with whitespace, it is considered a continuation of the
321 #define GOT_ALL ( GOT_HOST | GOT_DN | GOT_METHOD )
335 for ( i = 1; i < cargc; i++ ) {
336 if ( !strncasecmp( cargv[ i ], HOSTSTR, strlen( HOSTSTR ))) {
337 val = cargv[ i ] + strlen( HOSTSTR ) + 1;
338 if (( hp = strchr( val, ':' )) != NULL ) {
341 ri->ri_port = atoi( hp );
343 if ( ri->ri_port <= 0 ) {
346 ri->ri_hostname = strdup( val );
348 } else if ( !strncasecmp( cargv[ i ], TLSSTR, strlen( TLSSTR ))) {
349 val = cargv[ i ] + strlen( TLSSTR ) + 1;
350 if( !strcasecmp( val, TLSCRITICALSTR ) ) {
351 ri->ri_tls = TLS_CRITICAL;
355 } else if ( !strncasecmp( cargv[ i ],
356 BINDDNSTR, strlen( BINDDNSTR ))) {
357 val = cargv[ i ] + strlen( BINDDNSTR ) + 1;
358 ri->ri_bind_dn = strdup( val );
360 } else if ( !strncasecmp( cargv[ i ], BINDMETHSTR,
361 strlen( BINDMETHSTR ))) {
362 val = cargv[ i ] + strlen( BINDMETHSTR ) + 1;
363 if ( !strcasecmp( val, KERBEROSSTR )) {
364 fprintf( stderr, "Error: a bind method of \"kerberos\" was\n" );
365 fprintf( stderr, "specified in the slapd configuration file.\n" );
366 fprintf( stderr, "slurpd no longer supports Kerberos.\n" );
367 exit( EXIT_FAILURE );
368 } else if ( !strcasecmp( val, SIMPLESTR )) {
369 ri->ri_bind_method = AUTH_SIMPLE;
371 } else if ( !strcasecmp( val, SASLSTR )) {
372 ri->ri_bind_method = AUTH_SASL;
375 ri->ri_bind_method = -1;
377 } else if ( !strncasecmp( cargv[ i ], SASLMECHSTR, strlen( SASLMECHSTR ))) {
378 val = cargv[ i ] + strlen( SASLMECHSTR ) + 1;
380 ri->ri_saslmech = strdup( val );
381 } else if ( !strncasecmp( cargv[ i ], CREDSTR, strlen( CREDSTR ))) {
382 val = cargv[ i ] + strlen( CREDSTR ) + 1;
383 ri->ri_password = strdup( val );
384 } else if ( !strncasecmp( cargv[ i ], SECPROPSSTR, strlen( SECPROPSSTR ))) {
385 val = cargv[ i ] + strlen( SECPROPSSTR ) + 1;
386 ri->ri_secprops = strdup( val );
387 } else if ( !strncasecmp( cargv[ i ], REALMSTR, strlen( REALMSTR ))) {
388 val = cargv[ i ] + strlen( REALMSTR ) + 1;
389 ri->ri_realm = strdup( val );
390 } else if ( !strncasecmp( cargv[ i ], AUTHCSTR, strlen( AUTHCSTR ))) {
391 val = cargv[ i ] + strlen( AUTHCSTR ) + 1;
392 ri->ri_authcId = strdup( val );
393 } else if ( !strncasecmp( cargv[ i ], OLDAUTHCSTR, strlen( OLDAUTHCSTR ))) {
394 /* Old authcID is provided for some backwards compatibility */
395 val = cargv[ i ] + strlen( OLDAUTHCSTR ) + 1;
396 ri->ri_authcId = strdup( val );
397 } else if ( !strncasecmp( cargv[ i ], AUTHZSTR, strlen( AUTHZSTR ))) {
398 val = cargv[ i ] + strlen( AUTHZSTR ) + 1;
399 ri->ri_authzId = strdup( val );
400 } else if ( !strncasecmp( cargv[ i ], SRVTABSTR, strlen( SRVTABSTR ))) {
401 val = cargv[ i ] + strlen( SRVTABSTR ) + 1;
402 if ( ri->ri_srvtab != NULL ) {
403 free( ri->ri_srvtab );
405 ri->ri_srvtab = strdup( val );
408 "Error: parse_replica_line: unknown keyword \"%s\"\n",
413 if ( ri->ri_bind_method == AUTH_SASL) {
414 if ((gots & GOT_MECH) == 0) {
415 fprintf( stderr, "Error: \"replica\" line needs SASLmech flag in " );
416 fprintf( stderr, "slapd config file, line %d\n", lineno );
420 else if ( gots != GOT_ALL ) {
421 fprintf( stderr, "Error: Malformed \"replica\" line in slapd " );
422 fprintf( stderr, "config file, line %d\n", lineno );