

/*
 * Configuration for the server --- read from configuration file
 * in the following format:
 *
 *     database_path:		/usr/tmp/freeloaddb
 *     afs_cell:		sipb.mit.edu
 *     k_per_user:		500
 *     k_total:			100000
 *     max_lifetime:		14
 *     grace_period:		3
 *     server_partition:	ronald-ann:/vicepc
 *     server_partition:	rosebud:/vicepa
 *     server_partition:	rosebud:/vicepb
 */

#include <stdio.h>
#include <ctype.h>
#include <syslog.h>
#include <strings.h>

#include "server.h"

char *hostdb_path;                      /* Pathname to database for hosts down info */
char *database_path;			/* Pathname to database (not including .dir or .pag) */
char *mount_path;			/* Pathname to create AFS mountpoints for users */
char *afs_cell;				/* What cell are we in?  (i.e., SIPB.MIT.EDU) */
long k_per_user;			/* How much space do give each user (a la afs quota) */
long k_total;				/* What's the total amount of space we'll give out? */
long max_lifetime;			/* What's the absolute longest we'll let a locker live (in days)? */
long grace_period;			/* Days from server up to cutoff */
char *server_partitions[MAXPARTITIONS];	/* What partitions can we put these things on? */
long recheck_time;			/* Seconds between polling of downed partitions */
char *attach;				/* Pathname of attach program */
char *detach;				/* Pathname of detach program */
char *attach_opt;			/* Options to give attach */
char *aklog;				/* Pathname of aklog */
char *unlog;				/* Pathname of unlog */
char *vos;				/* Pathname of vos */
char *pts;				/* Pathname of pts */
char *fs;				/* Pathname of fs */
char *srvtab;				/* Pathname to the srvtab */                        
char *volume_base;			/* volume base name */

load_configuration (config_file)
     char *config_file;
{
  FILE *f;
  char linebuf[1024], *c, *kwd, *arg;
  int linenum = 0;
  
  if (!(f = fopen(config_file, "r"))) {
    syslog (LOG_ALERT, "Cannot read configuration file: %m");
    exit(1);
  }
  hostdb_path = NULL;
  database_path = NULL;
  afs_cell = NULL;
  mount_path = NULL;
  k_per_user = 0;
  k_total = 0;
  max_lifetime = 0;
  grace_period = 0;
  bzero(server_partitions, sizeof(server_partitions));
  while (fgets(linebuf, 1024, f)) {
    linenum++;
    if (c = index(linebuf, '#')) *c = 0;
    c = linebuf;
    while (isspace(*c)) c++;
    kwd = c;
    if (kwd) {
      while (isalnum(*c) || *c == '_') c++;
      if (!*c) {
      error:
	syslog (LOG_ERR, "Syntax error in configuration file on line %d", linenum);
	continue;
      }
      if (*c != ':') {
	*c++ = 0;
	c = index(c, ':');
	if (!c) goto error;
      }
      *c++ = 0;
      do
	c++;
      while (isspace(*c));
      arg = c;
      c = index(c, 0);
      do
	c--;
      while (isspace(*c));
      c[1] = 0;
      if (!strcmp(kwd, "hostdb_path"))
	malloc_strcpy(hostdb_path, arg);
      else if (!strcmp(kwd, "database_path"))
	malloc_strcpy(database_path, arg);
      else if (!strcmp(kwd, "mount_path"))
	malloc_strcpy(mount_path, arg);
      else if (!strcmp(kwd, "afs_cell"))
	malloc_strcpy(afs_cell, arg);
      else if (!strcmp(kwd, "attach"))
	malloc_strcpy(attach, arg);
      else if (!strcmp(kwd, "detach"))
	malloc_strcpy(detach, arg);
      else if (!strcmp(kwd, "attach_opt"))
	malloc_strcpy(attach_opt, arg);
      else if (!strcmp(kwd, "aklog"))
	malloc_strcpy(aklog, arg);
      else if (!strcmp(kwd, "unlog"))
	malloc_strcpy(unlog, arg);
      else if (!strcmp(kwd, "vos"))
	malloc_strcpy(vos, arg);
      else if (!strcmp(kwd, "pts"))
	malloc_strcpy(pts, arg);
      else if (!strcmp(kwd, "fs"))
	malloc_strcpy(fs, arg);
      else if (!strcmp(kwd, "volume_base"))
	malloc_strcpy(volume_base, arg);
      else if (!strcmp(kwd, "srvtab"))
	malloc_strcpy(srvtab, arg);
      else if (!strcmp(kwd, "k_per_user"))
	k_per_user = atoi(arg);
      else if (!strcmp(kwd, "k_total"))
	k_total = atoi(arg);
      else if (!strcmp(kwd, "max_lifetime"))
	max_lifetime = atoi(arg);
      else if (!strcmp(kwd, "grace_period"))
	grace_period = atoi(arg);
      else if (!strcmp(kwd, "recheck_time"))
	recheck_time = atoi(arg);
      else if (!strcmp(kwd, "server_partition")) {
	int pn;
	for (pn = 0; pn < MAXPARTITIONS && server_partitions[pn]; pn++);
	if (pn < MAXPARTITIONS)
	  malloc_strcpy(server_partitions[pn], arg);
	else {
	  syslog (LOG_ALERT, "Too many server partitions on line %d", linenum);
	  exit(1);
	}
      }
    }
  }
  fclose (f);
}
  
