2 * Copyright (c) 1996 Regents of the University of Michigan.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of Michigan at Ann Arbor. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific prior written permission. This software
10 * is provided ``as is'' without express or implied warranty.
15 * config.c - configuration file handling routines
22 #include <ac/string.h>
23 #include <ac/socket.h>
33 /* Forward declarations */
34 static void add_replica LDAP_P(( char **, int ));
35 static int parse_replica_line LDAP_P(( char **, int, Ri *));
36 static void parse_line LDAP_P(( char *, int *, char ** ));
37 static char *getline LDAP_P(( FILE * ));
38 static char *strtok_quote LDAP_P(( char *, char * ));
40 /* current config file line # */
46 * Read the slapd config file, looking only for config options we're
47 * interested in. Since we haven't detached from the controlling
48 * terminal yet, we just perror() and fprintf here.
61 Debug( LDAP_DEBUG_CONFIG, "Config: opening config file \"%s\"\n",
64 if ( (fp = fopen( fname, "r" )) == NULL ) {
70 while ( (line = getline( fp )) != NULL ) {
71 /* skip comments and blank lines */
72 if ( line[0] == '#' || line[0] == '\0' ) {
76 Debug( LDAP_DEBUG_CONFIG, "Config: (%s)\n", line, 0, 0 );
78 parse_line( line, &cargc, cargv );
81 fprintf( stderr, "line %d: bad config line (ignored)\n", lineno );
85 /* replication log file to which changes are appended */
86 if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
88 * if slapd_replogfile has a value, the -r option was given,
89 * so use that value. If slapd_replogfile has length == 0,
90 * then we should use the value in the config file we're reading.
92 if ( sglob->slapd_replogfile[ 0 ] == '\0' ) {
95 "line %d: missing filename in \"replogfile ",
97 fprintf( stderr, "<filename>\" line\n" );
99 } else if ( cargc > 2 && *cargv[2] != '#' ) {
101 "line %d: extra cruft at the end of \"replogfile %s\"",
103 fprintf( stderr, "line (ignored)\n" );
105 sprintf( sglob->slapd_replogfile, cargv[1] );
107 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
108 add_replica( cargv, cargc );
112 Debug( LDAP_DEBUG_CONFIG,
113 "Config: ** configuration file successfully read and parsed\n",
122 * Parse one line of input.
134 for ( token = strtok_quote( line, " \t" ); token != NULL;
135 token = strtok_quote( NULL, " \t" ) ) {
136 argv[(*argcp)++] = token;
154 if ( line != NULL ) {
157 while ( *next && strchr( sep, *next ) ) {
161 if ( *next == '\0' ) {
167 for ( inquote = 0; *next; ) {
175 strcpy( next, next + 1 );
179 strcpy( next, next + 1 );
184 if ( strchr( sep, *next ) != NULL ) {
197 #define CATLINE( buf ) { \
199 len = strlen( buf ); \
200 while ( lcur + len + 1 > lmax ) { \
202 line = (char *) ch_realloc( line, lmax ); \
204 strcpy( line + lcur, buf ); \
211 * Get a line of input.
219 static char buf[BUFSIZ];
221 static int lmax, lcur;
225 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
226 if ( (p = strchr( buf, '\n' )) != NULL ) {
230 if ( ! isspace( buf[0] ) ) {
238 return( line[0] ? line : NULL );
243 * Add a node to the array of replicas.
253 nr = ++sglob->num_replicas;
254 sglob->replicas = (Ri **) ch_realloc( sglob->replicas,
255 ( nr + 1 ) * sizeof( Re * ));
256 if ( sglob->replicas == NULL ) {
257 fprintf( stderr, "out of memory, add_replica\n" );
260 sglob->replicas[ nr ] = NULL;
262 if ( Ri_init( &(sglob->replicas[ nr - 1 ])) < 0 ) {
263 fprintf( stderr, "out of memory, Ri_init\n" );
266 if ( parse_replica_line( cargv, cargc,
267 sglob->replicas[ nr - 1] ) < 0 ) {
268 /* Something bad happened - back out */
270 "Warning: failed to add replica \"%s:%d - ignoring replica\n",
271 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
272 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
273 sglob->replicas[ nr - 1 ]->ri_port );
274 sglob->replicas[ nr - 1] = NULL;
275 sglob->num_replicas--;
277 Debug( LDAP_DEBUG_CONFIG,
278 "Config: ** successfully added replica \"%s:%d\"\n",
279 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
280 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
281 sglob->replicas[ nr - 1 ]->ri_port, 0 );
282 sglob->replicas[ nr - 1]->ri_stel =
283 sglob->st->st_add( sglob->st,
284 sglob->replicas[ nr - 1 ] );
285 if ( sglob->replicas[ nr - 1]->ri_stel == NULL ) {
286 fprintf( stderr, "Failed to add status element structure\n" );
295 * Parse a "replica" line from the config file. replica lines should be
296 * in the following format:
297 * replica host=<hostname:portnumber> binddn=<binddn>
298 * bindmethod="simple|kerberos" credentials=<creds>
301 * <hostname:portnumber> describes the host name and port number where the
302 * replica is running,
304 * <binddn> is the DN to bind to the replica slapd as,
306 * bindmethod is either "simple" or "kerberos", and
308 * <creds> are the credentials (e.g. password) for binddn. <creds> are
309 * only used for bindmethod=simple. For bindmethod=kerberos, the
310 * credentials= option should be omitted. Credentials for kerberos
311 * authentication are in the system srvtab file.
313 * The "replica" config file line may be split across multiple lines. If
314 * a line begins with whitespace, it is considered a continuation of the
320 #define GOT_ALL ( GOT_HOST | GOT_DN | GOT_METHOD )
332 for ( i = 1; i < cargc; i++ ) {
333 if ( !strncasecmp( cargv[ i ], HOSTSTR, strlen( HOSTSTR ))) {
334 val = cargv[ i ] + strlen( HOSTSTR ) + 1;
335 if (( hp = strchr( val, ':' )) != NULL ) {
338 ri->ri_port = atoi( hp );
340 if ( ri->ri_port <= 0 ) {
341 ri->ri_port = LDAP_PORT;
343 ri->ri_hostname = strdup( val );
345 } else if ( !strncasecmp( cargv[ i ],
346 BINDDNSTR, strlen( BINDDNSTR ))) {
347 val = cargv[ i ] + strlen( BINDDNSTR ) + 1;
348 ri->ri_bind_dn = strdup( val );
350 } else if ( !strncasecmp( cargv[ i ], BINDMETHSTR,
351 strlen( BINDMETHSTR ))) {
352 val = cargv[ i ] + strlen( BINDMETHSTR ) + 1;
353 if ( !strcasecmp( val, KERBEROSSTR )) {
355 ri->ri_bind_method = AUTH_KERBEROS;
356 if ( ri->ri_srvtab == NULL ) {
357 ri->ri_srvtab = strdup( sglob->default_srvtab );
360 #else /* HAVE_KERBEROS */
361 fprintf( stderr, "Error: a bind method of \"kerberos\" was\n" );
362 fprintf( stderr, "specified in the slapd configuration file,\n" );
363 fprintf( stderr, "but slurpd was not built with kerberos.\n" );
364 fprintf( stderr, "You must rebuild the LDAP release with\n" );
365 fprintf( stderr, "kerberos support if you wish to use\n" );
366 fprintf( stderr, "bindmethod=kerberos\n" );
368 #endif /* HAVE_KERBEROS */
369 } else if ( !strcasecmp( val, SIMPLESTR )) {
370 ri->ri_bind_method = AUTH_SIMPLE;
373 ri->ri_bind_method = -1;
375 } else if ( !strncasecmp( cargv[ i ], CREDSTR, strlen( CREDSTR ))) {
376 val = cargv[ i ] + strlen( CREDSTR ) + 1;
377 ri->ri_password = strdup( val );
378 } else if ( !strncasecmp( cargv[ i ], BINDPSTR, strlen( BINDPSTR ))) {
379 val = cargv[ i ] + strlen( BINDPSTR ) + 1;
380 ri->ri_principal = strdup( val );
381 } else if ( !strncasecmp( cargv[ i ], SRVTABSTR, strlen( SRVTABSTR ))) {
382 val = cargv[ i ] + strlen( SRVTABSTR ) + 1;
383 if ( ri->ri_srvtab != NULL ) {
384 free( ri->ri_srvtab );
386 ri->ri_srvtab = strdup( val );
389 "Error: parse_replica_line: unknown keyword \"%s\"\n",
393 if ( gots != GOT_ALL ) {
394 fprintf( stderr, "Error: Malformed \"replica\" line in slapd " );
395 fprintf( stderr, "config file, line %d\n", lineno );