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
20 #include <sys/types.h>
21 #include <sys/socket.h>
31 /* Forward declarations */
33 static void add_replica( char **, int );
34 static int parse_replica_line( char **, int, Ri *);
35 static void parse_line( char *, int *, char ** );
36 static char *getline( FILE * );
37 static char *strtok_quote( char *, char * );
38 #else /* NEEDPROTOS */
39 static void add_replica();
40 static int parse_replica_line();
41 static void parse_line();
42 static char *getline();
43 static char *strtok_quote();
44 #endif /* NEEDPROTOS */
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.
67 Debug( LDAP_DEBUG_CONFIG, "Config: opening config file \"%s\"\n",
70 if ( (fp = fopen( fname, "r" )) == NULL ) {
76 while ( (line = getline( fp )) != NULL ) {
77 /* skip comments and blank lines */
78 if ( line[0] == '#' || line[0] == '\0' ) {
82 Debug( LDAP_DEBUG_CONFIG, "Config: (%s)\n", line, 0, 0 );
84 parse_line( line, &cargc, cargv );
87 fprintf( stderr, "line %d: bad config line (ignored)\n", lineno );
91 /* replication log file to which changes are appended */
92 if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
94 * if slapd_replogfile has a value, the -r option was given,
95 * so use that value. If slapd_replogfile has length == 0,
96 * then we should use the value in the config file we're reading.
98 if ( sglob->slapd_replogfile[ 0 ] == '\0' ) {
101 "line %d: missing filename in \"replogfile ",
103 fprintf( stderr, "<filename>\" line\n" );
105 } else if ( cargc > 2 && *cargv[2] != '#' ) {
107 "line %d: extra cruft at the end of \"replogfile %s\"",
109 fprintf( stderr, "line (ignored)\n" );
111 sprintf( sglob->slapd_replogfile, cargv[1] );
113 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
114 add_replica( cargv, cargc );
118 Debug( LDAP_DEBUG_CONFIG,
119 "Config: ** configuration file successfully read and parsed\n",
128 * Parse one line of input.
140 for ( token = strtok_quote( line, " \t" ); token != NULL;
141 token = strtok_quote( NULL, " \t" ) ) {
142 argv[(*argcp)++] = token;
160 if ( line != NULL ) {
163 while ( *next && strchr( sep, *next ) ) {
167 if ( *next == '\0' ) {
173 for ( inquote = 0; *next; ) {
181 strcpy( next, next + 1 );
185 strcpy( next, next + 1 );
190 if ( strchr( sep, *next ) != NULL ) {
203 #define CATLINE( buf ) { \
205 len = strlen( buf ); \
206 while ( lcur + len + 1 > lmax ) { \
208 line = (char *) ch_realloc( line, lmax ); \
210 strcpy( line + lcur, buf ); \
217 * Get a line of input.
225 static char buf[BUFSIZ];
227 static int lmax, lcur;
231 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
232 if ( (p = strchr( buf, '\n' )) != NULL ) {
236 if ( ! isspace( buf[0] ) ) {
244 return( line[0] ? line : NULL );
249 * Add a node to the array of replicas.
259 nr = ++sglob->num_replicas;
260 sglob->replicas = (Ri **) ch_realloc( sglob->replicas,
261 ( nr + 1 ) * sizeof( Re * ));
262 if ( sglob->replicas == NULL ) {
263 fprintf( stderr, "out of memory, add_replica\n" );
266 sglob->replicas[ nr ] = NULL;
268 if ( Ri_init( &(sglob->replicas[ nr - 1 ])) < 0 ) {
269 fprintf( stderr, "out of memory, Ri_init\n" );
272 if ( parse_replica_line( cargv, cargc,
273 sglob->replicas[ nr - 1] ) < 0 ) {
274 /* Something bad happened - back out */
276 "Warning: failed to add replica \"%s:%d - ignoring replica\n",
277 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
278 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
279 sglob->replicas[ nr - 1 ]->ri_port );
280 sglob->replicas[ nr - 1] = NULL;
281 sglob->num_replicas--;
283 Debug( LDAP_DEBUG_CONFIG,
284 "Config: ** successfully added replica \"%s:%d\"\n",
285 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
286 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
287 sglob->replicas[ nr - 1 ]->ri_port, 0 );
288 sglob->replicas[ nr - 1]->ri_stel =
289 sglob->st->st_add( sglob->st,
290 sglob->replicas[ nr - 1 ] );
291 if ( sglob->replicas[ nr - 1]->ri_stel == NULL ) {
292 fprintf( stderr, "Failed to add status element structure\n" );
301 * Parse a "replica" line from the config file. replica lines should be
302 * in the following format:
303 * replica host=<hostname:portnumber> binddn=<binddn>
304 * bindmethod="simple|kerberos" credentials=<creds>
307 * <hostname:portnumber> describes the host name and port number where the
308 * replica is running,
310 * <binddn> is the DN to bind to the replica slapd as,
312 * bindmethod is either "simple" or "kerberos", and
314 * <creds> are the credentials (e.g. password) for binddn. <creds> are
315 * only used for bindmethod=simple. For bindmethod=kerberos, the
316 * credentials= option should be omitted. Credentials for kerberos
317 * authentication are in the system srvtab file.
319 * The "replica" config file line may be split across multiple lines. If
320 * a line begins with whitespace, it is considered a continuation of the
326 #define GOT_ALL ( GOT_HOST | GOT_DN | GOT_METHOD )
338 for ( i = 1; i < cargc; i++ ) {
339 if ( !strncasecmp( cargv[ i ], HOSTSTR, strlen( HOSTSTR ))) {
340 val = cargv[ i ] + strlen( HOSTSTR ) + 1;
341 if (( hp = strchr( val, ':' )) != NULL ) {
344 ri->ri_port = atoi( hp );
346 if ( ri->ri_port <= 0 ) {
347 ri->ri_port = LDAP_PORT;
349 ri->ri_hostname = strdup( val );
351 } else if ( !strncasecmp( cargv[ i ],
352 BINDDNSTR, strlen( BINDDNSTR ))) {
353 val = cargv[ i ] + strlen( BINDDNSTR ) + 1;
354 ri->ri_bind_dn = strdup( val );
356 } else if ( !strncasecmp( cargv[ i ], BINDMETHSTR,
357 strlen( BINDMETHSTR ))) {
358 val = cargv[ i ] + strlen( BINDMETHSTR ) + 1;
359 if ( !strcasecmp( val, KERBEROSSTR )) {
361 ri->ri_bind_method = AUTH_KERBEROS;
362 if ( ri->ri_srvtab == NULL ) {
363 ri->ri_srvtab = strdup( sglob->default_srvtab );
367 fprintf( stderr, "Error: a bind method of \"kerberos\" was\n" );
368 fprintf( stderr, "specified in the slapd configuration file,\n" );
369 fprintf( stderr, "but slurpd was not built with kerberos.\n" );
370 fprintf( stderr, "You must rebuild the LDAP release with\n" );
371 fprintf( stderr, "kerberos support if you wish to use\n" );
372 fprintf( stderr, "bindmethod=kerberos\n" );
374 #endif /* KERBEROS */
375 } else if ( !strcasecmp( val, SIMPLESTR )) {
376 ri->ri_bind_method = AUTH_SIMPLE;
379 ri->ri_bind_method = -1;
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 ], BINDPSTR, strlen( BINDPSTR ))) {
385 val = cargv[ i ] + strlen( BINDPSTR ) + 1;
386 ri->ri_principal = strdup( val );
387 } else if ( !strncasecmp( cargv[ i ], SRVTABSTR, strlen( SRVTABSTR ))) {
388 val = cargv[ i ] + strlen( SRVTABSTR ) + 1;
389 if ( ri->ri_srvtab != NULL ) {
390 free( ri->ri_srvtab );
392 ri->ri_srvtab = strdup( val );
395 "Error: parse_replica_line: unknown keyword \"%s\"\n",
399 if ( gots != GOT_ALL ) {
400 fprintf( stderr, "Error: Malformed \"replica\" line in slapd " );
401 fprintf( stderr, "config file, line %d\n", lineno );