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
23 #include <ac/string.h>
24 #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 sprintf( 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 strcpy( next, next + 1 );
180 strcpy( next, next + 1 );
185 if ( strchr( sep, *next ) != NULL ) {
198 #define CATLINE( buf ) { \
200 len = strlen( buf ); \
201 while ( lcur + len + 1 > lmax ) { \
203 line = (char *) ch_realloc( line, lmax ); \
205 strcpy( line + lcur, buf ); \
212 * Get a line of input.
220 static char buf[BUFSIZ];
222 static int lmax, lcur;
226 while ( fgets( buf, sizeof(buf), fp ) != NULL ) {
227 if ( (p = strchr( buf, '\n' )) != NULL ) {
231 if ( ! isspace( (unsigned char) buf[0] ) ) {
239 return( line[0] ? line : NULL );
244 * Add a node to the array of replicas.
254 nr = ++sglob->num_replicas;
255 sglob->replicas = (Ri **) ch_realloc( sglob->replicas,
256 ( nr + 1 ) * sizeof( Re * ));
257 if ( sglob->replicas == NULL ) {
258 fprintf( stderr, "out of memory, add_replica\n" );
261 sglob->replicas[ nr ] = NULL;
263 if ( Ri_init( &(sglob->replicas[ nr - 1 ])) < 0 ) {
264 fprintf( stderr, "out of memory, Ri_init\n" );
267 if ( parse_replica_line( cargv, cargc,
268 sglob->replicas[ nr - 1] ) < 0 ) {
269 /* Something bad happened - back out */
271 "Warning: failed to add replica \"%s:%d - ignoring replica\n",
272 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
273 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
274 sglob->replicas[ nr - 1 ]->ri_port );
275 sglob->replicas[ nr - 1] = NULL;
276 sglob->num_replicas--;
278 Debug( LDAP_DEBUG_CONFIG,
279 "Config: ** successfully added replica \"%s:%d\"\n",
280 sglob->replicas[ nr - 1 ]->ri_hostname == NULL ?
281 "(null)" : sglob->replicas[ nr - 1 ]->ri_hostname,
282 sglob->replicas[ nr - 1 ]->ri_port, 0 );
283 sglob->replicas[ nr - 1]->ri_stel =
284 sglob->st->st_add( sglob->st,
285 sglob->replicas[ nr - 1 ] );
286 if ( sglob->replicas[ nr - 1]->ri_stel == NULL ) {
287 fprintf( stderr, "Failed to add status element structure\n" );
296 * Parse a "replica" line from the config file. replica lines should be
297 * in the following format:
298 * replica host=<hostname:portnumber> binddn=<binddn>
299 * bindmethod="simple|kerberos" credentials=<creds>
302 * <hostname:portnumber> describes the host name and port number where the
303 * replica is running,
305 * <binddn> is the DN to bind to the replica slapd as,
307 * bindmethod is either "simple" or "kerberos", and
309 * <creds> are the credentials (e.g. password) for binddn. <creds> are
310 * only used for bindmethod=simple. For bindmethod=kerberos, the
311 * credentials= option should be omitted. Credentials for kerberos
312 * authentication are in the system srvtab file.
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 )
333 for ( i = 1; i < cargc; i++ ) {
334 if ( !strncasecmp( cargv[ i ], HOSTSTR, strlen( HOSTSTR ))) {
335 val = cargv[ i ] + strlen( HOSTSTR ) + 1;
336 if (( hp = strchr( val, ':' )) != NULL ) {
339 ri->ri_port = atoi( hp );
341 if ( ri->ri_port <= 0 ) {
344 ri->ri_hostname = strdup( val );
346 } else if ( !strncasecmp( cargv[ i ],
347 BINDDNSTR, strlen( BINDDNSTR ))) {
348 val = cargv[ i ] + strlen( BINDDNSTR ) + 1;
349 ri->ri_bind_dn = strdup( val );
351 } else if ( !strncasecmp( cargv[ i ], BINDMETHSTR,
352 strlen( BINDMETHSTR ))) {
353 val = cargv[ i ] + strlen( BINDMETHSTR ) + 1;
354 if ( !strcasecmp( val, KERBEROSSTR )) {
356 ri->ri_bind_method = AUTH_KERBEROS;
357 if ( ri->ri_srvtab == NULL ) {
358 ri->ri_srvtab = strdup( sglob->default_srvtab );
361 #else /* HAVE_KERBEROS */
362 fprintf( stderr, "Error: a bind method of \"kerberos\" was\n" );
363 fprintf( stderr, "specified in the slapd configuration file,\n" );
364 fprintf( stderr, "but slurpd was not built with kerberos.\n" );
365 fprintf( stderr, "You must rebuild the LDAP release with\n" );
366 fprintf( stderr, "kerberos support if you wish to use\n" );
367 fprintf( stderr, "bindmethod=kerberos\n" );
369 #endif /* HAVE_KERBEROS */
370 } else if ( !strcasecmp( val, SIMPLESTR )) {
371 ri->ri_bind_method = AUTH_SIMPLE;
374 ri->ri_bind_method = -1;
376 } else if ( !strncasecmp( cargv[ i ], CREDSTR, strlen( CREDSTR ))) {
377 val = cargv[ i ] + strlen( CREDSTR ) + 1;
378 ri->ri_password = strdup( val );
379 } else if ( !strncasecmp( cargv[ i ], BINDPSTR, strlen( BINDPSTR ))) {
380 val = cargv[ i ] + strlen( BINDPSTR ) + 1;
381 ri->ri_principal = strdup( val );
382 } else if ( !strncasecmp( cargv[ i ], SRVTABSTR, strlen( SRVTABSTR ))) {
383 val = cargv[ i ] + strlen( SRVTABSTR ) + 1;
384 if ( ri->ri_srvtab != NULL ) {
385 free( ri->ri_srvtab );
387 ri->ri_srvtab = strdup( val );
390 "Error: parse_replica_line: unknown keyword \"%s\"\n",
394 if ( gots != GOT_ALL ) {
395 fprintf( stderr, "Error: Malformed \"replica\" line in slapd " );
396 fprintf( stderr, "config file, line %d\n", lineno );