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>
36 /* Forward declarations */
37 static void add_replica LDAP_P(( char **, int ));
38 static int parse_replica_line LDAP_P(( char **, int, Ri *));
39 static void parse_line LDAP_P(( char *, int *, char ** ));
40 static char *getline LDAP_P(( FILE * ));
41 static char *strtok_quote LDAP_P(( char *, char * ));
43 /* current config file line # */
49 * Read the slapd config file, looking only for config options we're
50 * interested in. Since we haven't detached from the controlling
51 * terminal yet, we just perror() and fprintf here.
63 Debug( LDAP_DEBUG_CONFIG, "Config: opening config file \"%s\"\n",
66 if ( (fp = fopen( fname, "r" )) == NULL ) {
72 while ( (line = getline( fp )) != NULL ) {
73 /* skip comments and blank lines */
74 if ( line[0] == '#' || line[0] == '\0' ) {
78 Debug( LDAP_DEBUG_CONFIG, "Config: (%s)\n", line, 0, 0 );
80 parse_line( line, &cargc, cargv );
83 fprintf( stderr, "line %d: bad config line (ignored)\n", lineno );
87 /* replication log file to which changes are appended */
88 if ( strcasecmp( cargv[0], "replogfile" ) == 0 ) {
90 * if slapd_replogfile has a value, the -r option was given,
91 * so use that value. If slapd_replogfile has length == 0,
92 * then we should use the value in the config file we're reading.
94 if ( sglob->slapd_replogfile[ 0 ] == '\0' ) {
97 "line %d: missing filename in \"replogfile ",
99 fprintf( stderr, "<filename>\" line\n" );
100 exit( EXIT_FAILURE );
101 } else if ( cargc > 2 && *cargv[2] != '#' ) {
103 "line %d: extra cruft at the end of \"replogfile %s\"",
105 fprintf( stderr, "line (ignored)\n" );
107 strcpy( sglob->slapd_replogfile, cargv[1] );
109 } else if ( strcasecmp( cargv[0], "replica" ) == 0 ) {
110 add_replica( cargv, cargc );
114 Debug( LDAP_DEBUG_CONFIG,
115 "Config: ** configuration file successfully read and parsed\n",
124 * Parse one line of input.
136 for ( token = strtok_quote( line, " \t" ); token != NULL;
137 token = strtok_quote( NULL, " \t" ) ) {
138 argv[(*argcp)++] = token;
156 if ( line != NULL ) {
159 while ( *next && strchr( sep, *next ) ) {
163 if ( *next == '\0' ) {
169 for ( inquote = 0; *next; ) {
177 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
182 SAFEMEMCPY( next, next + 1, strlen( next + 1 ) + 1 );
183 next++; /* dont parse the escaped character */
188 if ( strchr( sep, *next ) != NULL ) {
201 #define CATLINE( buf ) { \
203 len = strlen( buf ); \
204 while ( lcur + len + 1 > lmax ) { \
206 line = (char *) ch_realloc( line, lmax ); \
208 strcpy( line + lcur, buf ); \
215 * Get a line of input.
223 static char buf[BUFSIZ];
225 static int lmax, lcur;
229 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
230 if ( (p = strchr( buf, '\n' )) != NULL ) {
234 if ( ! isspace( (unsigned char) buf[0] ) ) {
242 return( line[0] ? line : NULL );
247 * Add a node to the array of replicas.
257 nr = ++sglob->num_replicas;
258 sglob->replicas = (Ri **) ch_realloc( sglob->replicas,
259 ( nr + 1 ) * sizeof( Re * ));
260 if ( sglob->replicas == NULL ) {
261 fprintf( stderr, "out of memory, add_replica\n" );
262 exit( EXIT_FAILURE );
264 sglob->replicas[ nr ] = NULL;
266 if ( Ri_init( &(sglob->replicas[ nr - 1 ])) < 0 ) {
267 fprintf( stderr, "out of memory, Ri_init\n" );
268 exit( EXIT_FAILURE );
270 if ( parse_replica_line( cargv, cargc,
271 sglob->replicas[ nr - 1] ) < 0 ) {
272 /* Something bad happened - back out */
274 "Warning: failed to add replica \"%s:%d - ignoring replica\n",
275 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
276 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
277 sglob->replicas[ nr - 1 ]->ri_port );
278 sglob->replicas[ nr - 1] = NULL;
279 sglob->num_replicas--;
281 Debug( LDAP_DEBUG_CONFIG,
282 "Config: ** successfully added replica \"%s:%d\"\n",
283 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
284 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
285 sglob->replicas[ nr - 1 ]->ri_port, 0 );
286 sglob->replicas[ nr - 1]->ri_stel =
287 sglob->st->st_add( sglob->st,
288 sglob->replicas[ nr - 1 ] );
289 if ( sglob->replicas[ nr - 1]->ri_stel == NULL ) {
290 fprintf( stderr, "Failed to add status element structure\n" );
291 exit( EXIT_FAILURE );
299 * Parse a "replica" line from the config file. replica lines should be
300 * in the following format:
301 * replica host=<hostname:portnumber> binddn=<binddn>
302 * bindmethod="simple|kerberos" credentials=<creds>
305 * <hostname:portnumber> describes the host name and port number where the
306 * replica is running,
308 * <binddn> is the DN to bind to the replica slapd as,
310 * bindmethod is either "simple" or "kerberos", and
312 * <creds> are the credentials (e.g. password) for binddn. <creds> are
313 * only used for bindmethod=simple. For bindmethod=kerberos, the
314 * credentials= option should be omitted. Credentials for kerberos
315 * authentication are in the system srvtab file.
317 * The "replica" config file line may be split across multiple lines. If
318 * a line begins with whitespace, it is considered a continuation of the
324 #define GOT_ALL ( GOT_HOST | GOT_DN | GOT_METHOD )
336 for ( i = 1; i < cargc; i++ ) {
337 if ( !strncasecmp( cargv[ i ], HOSTSTR, strlen( HOSTSTR ))) {
338 val = cargv[ i ] + strlen( HOSTSTR ) + 1;
339 if (( hp = strchr( val, ':' )) != NULL ) {
342 ri->ri_port = atoi( hp );
344 if ( ri->ri_port <= 0 ) {
347 ri->ri_hostname = strdup( val );
349 } else if ( !strncasecmp( cargv[ i ],
350 BINDDNSTR, strlen( BINDDNSTR ))) {
351 val = cargv[ i ] + strlen( BINDDNSTR ) + 1;
352 ri->ri_bind_dn = strdup( val );
354 } else if ( !strncasecmp( cargv[ i ], BINDMETHSTR,
355 strlen( BINDMETHSTR ))) {
356 val = cargv[ i ] + strlen( BINDMETHSTR ) + 1;
357 if ( !strcasecmp( val, KERBEROSSTR )) {
358 #ifdef LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND
359 ri->ri_bind_method = AUTH_KERBEROS;
360 if ( ri->ri_srvtab == NULL ) {
361 ri->ri_srvtab = strdup( sglob->default_srvtab );
364 #else /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
365 fprintf( stderr, "Error: a bind method of \"kerberos\" was\n" );
366 fprintf( stderr, "specified in the slapd configuration file,\n" );
367 fprintf( stderr, "but slurpd was not built with kerberos.\n" );
368 fprintf( stderr, "You must rebuild the LDAP release with\n" );
369 fprintf( stderr, "kerberos support if you wish to use\n" );
370 fprintf( stderr, "bindmethod=kerberos\n" );
371 exit( EXIT_FAILURE );
372 #endif /* LDAP_API_FEATURE_X_OPENLDAP_V2_KBIND */
373 } else if ( !strcasecmp( val, SIMPLESTR )) {
374 ri->ri_bind_method = AUTH_SIMPLE;
377 ri->ri_bind_method = -1;
379 } else if ( !strncasecmp( cargv[ i ], CREDSTR, strlen( CREDSTR ))) {
380 val = cargv[ i ] + strlen( CREDSTR ) + 1;
381 ri->ri_password = strdup( val );
382 } else if ( !strncasecmp( cargv[ i ], BINDPSTR, strlen( BINDPSTR ))) {
383 val = cargv[ i ] + strlen( BINDPSTR ) + 1;
384 ri->ri_principal = strdup( val );
385 } else if ( !strncasecmp( cargv[ i ], SRVTABSTR, strlen( SRVTABSTR ))) {
386 val = cargv[ i ] + strlen( SRVTABSTR ) + 1;
387 if ( ri->ri_srvtab != NULL ) {
388 free( ri->ri_srvtab );
390 ri->ri_srvtab = strdup( val );
393 "Error: parse_replica_line: unknown keyword \"%s\"\n",
397 if ( gots != GOT_ALL ) {
398 fprintf( stderr, "Error: Malformed \"replica\" line in slapd " );
399 fprintf( stderr, "config file, line %d\n", lineno );