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 * ));
43 static char *getline LDAP_P(( FILE * ));
44 static char *strtok_quote LDAP_P(( char *, char * ));
46 int cargc = 0, cargv_size = 0;
48 /* current config file line # */
54 * Read the slapd config file, looking only for config options we're
55 * interested in. Since we haven't detached from the controlling
56 * terminal yet, we just perror() and fprintf here.
66 cargv = ch_calloc( ARGS_STEP + 1, sizeof(*cargv) );
67 cargv_size = ARGS_STEP + 1;
70 LDAP_LOG ( CONFIG, ARGS,
71 "slurpd_read_config: Config: opening config file \"%s\"\n",
74 Debug( LDAP_DEBUG_CONFIG, "Config: opening config file \"%s\"\n",
78 if ( (fp = fopen( fname, "r" )) == NULL ) {
84 while ( (line = getline( fp )) != NULL ) {
85 /* skip comments and blank lines */
86 if ( line[0] == '#' || line[0] == '\0' ) {
91 LDAP_LOG ( CONFIG, DETAIL1,
92 "slurpd_read_config: Config: (%s)\n", line, 0, 0 );
94 Debug( LDAP_DEBUG_CONFIG, "Config: (%s)\n", line, 0, 0 );
100 fprintf( stderr, "line %d: bad config line (ignored)\n", lineno );
104 /* replication log file to which changes are appended */
105 if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
107 * if slapd_replogfile has a value, the -r option was given,
108 * so use that value. If slapd_replogfile has length == 0,
109 * then we should use the value in the config file we're reading.
111 if ( sglob->slapd_replogfile[ 0 ] == '\0' ) {
114 "line %d: missing filename in \"replogfile ",
116 fprintf( stderr, "<filename>\" line\n" );
117 exit( EXIT_FAILURE );
118 } else if ( cargc > 2 && *cargv[2] != '#' ) {
120 "line %d: extra cruft at the end of \"replogfile %s\"",
122 fprintf( stderr, "line (ignored)\n" );
124 strcpy( sglob->slapd_replogfile, cargv[1] );
126 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
127 add_replica( cargv, cargc );
129 /* include another config file */
130 } else if ( strcasecmp( cargv[0], "include" ) == 0 ) {
136 LDAP_LOG( CONFIG, CRIT,
137 "%s: line %d: missing filename in \"include "
138 "<filename>\" line.\n", fname, lineno , 0 );
140 Debug( LDAP_DEBUG_ANY,
141 "%s: line %d: missing filename in \"include <filename>\" line\n",
147 savefname = strdup( cargv[1] );
150 if ( slurpd_read_config( savefname ) != 0 ) {
155 lineno = savelineno - 1;
160 LDAP_LOG ( CONFIG, RESULTS,
161 "slurpd_read_config: Config: "
162 "** configuration file successfully read and parsed\n", 0, 0, 0 );
164 Debug( LDAP_DEBUG_CONFIG,
165 "Config: ** configuration file successfully read and parsed\n",
175 * Parse one line of input.
185 for ( token = strtok_quote( line, " \t" ); token != NULL;
186 token = strtok_quote( NULL, " \t" ) )
188 if ( cargc == cargv_size - 1 ) {
190 tmp = ch_realloc( cargv, (cargv_size + ARGS_STEP) *
197 cargv_size += ARGS_STEP;
200 cargv[cargc++] = token;
218 if ( line != NULL ) {
221 while ( *next && strchr( sep, *next ) ) {
225 if ( *next == '\0' ) {
231 for ( inquote = 0; *next; ) {
239 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
244 AC_MEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
245 next++; /* dont parse the escaped character */
250 if ( strchr( sep, *next ) != NULL ) {
263 #define CATLINE( buf ) { \
265 len = strlen( buf ); \
266 while ( lcur + len + 1 > lmax ) { \
268 line = (char *) ch_realloc( line, lmax ); \
270 strcpy( line + lcur, buf ); \
277 * Get a line of input.
285 static char buf[BUFSIZ];
287 static int lmax, lcur;
291 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
292 if ( (p = strchr( buf, '\n' )) != NULL ) {
293 if( p > buf && p[-1] == '\r' ) --p;
297 if ( ! isspace( (unsigned char) buf[0] ) ) {
301 /* change leading whitespace to space */
308 return( line[0] ? line : NULL );
313 * Add a node to the array of replicas.
323 nr = ++sglob->num_replicas;
324 sglob->replicas = (Ri **) ch_realloc( sglob->replicas,
325 ( nr + 1 ) * sizeof( Re * ));
326 if ( sglob->replicas == NULL ) {
327 fprintf( stderr, "out of memory, add_replica\n" );
328 exit( EXIT_FAILURE );
330 sglob->replicas[ nr ] = NULL;
332 if ( Ri_init( &(sglob->replicas[ nr - 1 ])) < 0 ) {
333 fprintf( stderr, "out of memory, Ri_init\n" );
334 exit( EXIT_FAILURE );
336 if ( parse_replica_line( cargv, cargc,
337 sglob->replicas[ nr - 1] ) < 0 ) {
338 /* Something bad happened - back out */
340 "Warning: failed to add replica \"%s:%d - ignoring replica\n",
341 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
342 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
343 sglob->replicas[ nr - 1 ]->ri_port );
344 sglob->replicas[ nr - 1] = NULL;
345 sglob->num_replicas--;
348 LDAP_LOG ( CONFIG, RESULTS,
349 "add_replica: Config: ** successfully added replica \"%s%d\"\n",
350 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
351 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
352 sglob->replicas[ nr - 1 ]->ri_port, 0 );
354 Debug( LDAP_DEBUG_CONFIG,
355 "Config: ** successfully added replica \"%s:%d\"\n",
356 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
357 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
358 sglob->replicas[ nr - 1 ]->ri_port, 0 );
360 sglob->replicas[ nr - 1]->ri_stel =
361 sglob->st->st_add( sglob->st,
362 sglob->replicas[ nr - 1 ] );
363 if ( sglob->replicas[ nr - 1]->ri_stel == NULL ) {
364 fprintf( stderr, "Failed to add status element structure\n" );
365 exit( EXIT_FAILURE );
373 * Parse a "replica" line from the config file. replica lines should be
374 * in the following format:
375 * replica host=<hostname:portnumber> binddn=<binddn>
376 * bindmethod="simple" credentials=<creds>
379 * <hostname:portnumber> describes the host name and port number where the
380 * replica is running,
382 * <binddn> is the DN to bind to the replica slapd as,
384 * bindmethod is "simple", and
386 * <creds> are the credentials (e.g. password) for binddn. <creds> are
387 * only used for bindmethod=simple.
389 * The "replica" config file line may be split across multiple lines. If
390 * a line begins with whitespace, it is considered a continuation of the
396 #define GOT_ALL ( GOT_HOST | GOT_DN | GOT_METHOD )
410 for ( i = 1; i < cargc; i++ ) {
411 if ( !strncasecmp( cargv[ i ], HOSTSTR, sizeof( HOSTSTR ) - 1 ) ) {
412 val = cargv[ i ] + sizeof( HOSTSTR ); /* '\0' string terminator accounts for '=' */
413 if (( hp = strchr( val, ':' )) != NULL ) {
416 ri->ri_port = atoi( hp );
418 if ( ri->ri_port <= 0 ) {
421 ri->ri_hostname = strdup( val );
423 } else if ( !strncasecmp( cargv[ i ],
424 ATTRSTR, sizeof( ATTRSTR ) - 1 ) ) {
426 } else if ( !strncasecmp( cargv[ i ],
427 SUFFIXSTR, sizeof( SUFFIXSTR ) - 1 ) ) {
429 } else if ( !strncasecmp( cargv[ i ], TLSSTR, sizeof( TLSSTR ) - 1 ) ) {
430 val = cargv[ i ] + sizeof( TLSSTR );
431 if( !strcasecmp( val, TLSCRITICALSTR ) ) {
432 ri->ri_tls = TLS_CRITICAL;
436 } else if ( !strncasecmp( cargv[ i ],
437 BINDDNSTR, sizeof( BINDDNSTR ) - 1 ) ) {
438 val = cargv[ i ] + sizeof( BINDDNSTR );
439 ri->ri_bind_dn = strdup( val );
441 } else if ( !strncasecmp( cargv[ i ], BINDMETHSTR,
442 sizeof( BINDMETHSTR ) - 1 ) ) {
443 val = cargv[ i ] + sizeof( BINDMETHSTR );
444 if ( !strcasecmp( val, KERBEROSSTR )) {
445 fprintf( stderr, "Error: a bind method of \"kerberos\" was\n" );
446 fprintf( stderr, "specified in the slapd configuration file.\n" );
447 fprintf( stderr, "slurpd no longer supports Kerberos.\n" );
448 exit( EXIT_FAILURE );
449 } else if ( !strcasecmp( val, SIMPLESTR )) {
450 ri->ri_bind_method = AUTH_SIMPLE;
452 } else if ( !strcasecmp( val, SASLSTR )) {
453 ri->ri_bind_method = AUTH_SASL;
456 ri->ri_bind_method = -1;
458 } else if ( !strncasecmp( cargv[ i ],
459 SASLMECHSTR, sizeof( SASLMECHSTR ) - 1 ) ) {
460 val = cargv[ i ] + sizeof( SASLMECHSTR );
462 ri->ri_saslmech = strdup( val );
463 } else if ( !strncasecmp( cargv[ i ],
464 CREDSTR, sizeof( CREDSTR ) - 1 ) ) {
465 val = cargv[ i ] + sizeof( CREDSTR );
466 ri->ri_password = strdup( val );
467 } else if ( !strncasecmp( cargv[ i ],
468 SECPROPSSTR, sizeof( SECPROPSSTR ) - 1 ) ) {
469 val = cargv[ i ] + sizeof( SECPROPSSTR );
470 ri->ri_secprops = strdup( val );
471 } else if ( !strncasecmp( cargv[ i ],
472 REALMSTR, sizeof( REALMSTR ) - 1 ) ) {
473 val = cargv[ i ] + sizeof( REALMSTR );
474 ri->ri_realm = strdup( val );
475 } else if ( !strncasecmp( cargv[ i ],
476 AUTHCSTR, sizeof( AUTHCSTR ) - 1 ) ) {
477 val = cargv[ i ] + sizeof( AUTHCSTR );
478 ri->ri_authcId = strdup( val );
479 } else if ( !strncasecmp( cargv[ i ],
480 OLDAUTHCSTR, sizeof( OLDAUTHCSTR ) - 1 ) ) {
481 /* Old authcID is provided for some backwards compatibility */
482 val = cargv[ i ] + sizeof( OLDAUTHCSTR );
483 ri->ri_authcId = strdup( val );
484 } else if ( !strncasecmp( cargv[ i ],
485 AUTHZSTR, sizeof( AUTHZSTR ) - 1 ) ) {
486 val = cargv[ i ] + sizeof( AUTHZSTR );
487 ri->ri_authzId = strdup( val );
488 } else if ( !strncasecmp( cargv[ i ],
489 SRVTABSTR, sizeof( SRVTABSTR ) - 1 ) ) {
490 val = cargv[ i ] + sizeof( SRVTABSTR );
491 if ( ri->ri_srvtab != NULL ) {
492 free( ri->ri_srvtab );
494 ri->ri_srvtab = strdup( val );
497 "Error: parse_replica_line: unknown keyword \"%s\"\n",
502 if ( ri->ri_bind_method == AUTH_SASL) {
503 if ((gots & GOT_MECH) == 0) {
504 fprintf( stderr, "Error: \"replica\" line needs SASLmech flag in " );
505 fprintf( stderr, "slapd config file, line %d\n", lineno );
509 else if ( gots != GOT_ALL ) {
510 fprintf( stderr, "Error: Malformed \"replica\" line in slapd " );
511 fprintf( stderr, "config file, line %d\n", lineno );