Reload keys and config files automatically.
authormdw <mdw>
Fri, 17 Oct 2003 16:30:22 +0000 (16:30 +0000)
committermdw <mdw>
Fri, 17 Oct 2003 16:30:22 +0000 (16:30 +0000)
.cvsignore
src/daemon.c

index 91bc682..ac43b98 100644 (file)
@@ -16,3 +16,4 @@ patches
 sgi
 stamp-h.in
 sun
 sgi
 stamp-h.in
 sun
+test
index 6c515f4..fbaf26b 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
 /* -*-c-*-
  *
- * $Id: daemon.c,v 1.13 2003/10/12 10:00:06 mdw Exp $
+ * $Id: daemon.c,v 1.14 2003/10/17 16:30:22 mdw Exp $
  *
  * Running a `become' daemon
  *
  *
  * Running a `become' daemon
  *
@@ -29,6 +29,9 @@
 /*----- Revision history --------------------------------------------------*
  *
  * $Log: daemon.c,v $
 /*----- Revision history --------------------------------------------------*
  *
  * $Log: daemon.c,v $
+ * Revision 1.14  2003/10/17 16:30:22  mdw
+ * Reload keys and config files automatically.
+ *
  * Revision 1.13  2003/10/12 10:00:06  mdw
  * Fix for daemon mode.  Oops.
  *
  * Revision 1.13  2003/10/12 10:00:06  mdw
  * Fix for daemon mode.  Oops.
  *
 
 /*----- Arbitrary constants -----------------------------------------------*/
 
 
 /*----- Arbitrary constants -----------------------------------------------*/
 
-#define daemon__awakeEvery (5 * 60)    /* Awaken this often to rescan */
+/* #define daemon__awakeEvery (5 * 60) /\* Awaken this often to rescan *\/ */
+#define daemon__awakeEvery (10)                /* Awaken this often to rescan */
 
 /*----- Static variables --------------------------------------------------*/
 
 static int daemon__port = -1;          /* No particular port yet */
 
 /*----- Static variables --------------------------------------------------*/
 
 static int daemon__port = -1;          /* No particular port yet */
-static int daemon__readKey = 0;                /* Have I read a key? */
-static fwatch daemon__watch;
+static fwatch daemon__cwatch, daemon__kwatch; /* Watching key / config files */
 static sel_timer daemon__timer;                /* Timer for reading */
 static sel_state daemon__sel;          /* Select context */
 static sel_file daemon__listen;                /* Listening socket selector */
 static const char *daemon__config;     /* Configuration file for daemon */
 static sel_timer daemon__timer;                /* Timer for reading */
 static sel_state daemon__sel;          /* Select context */
 static sel_file daemon__listen;                /* Listening socket selector */
 static const char *daemon__config;     /* Configuration file for daemon */
+static const char *daemon__keyfile;    /* Keyring file for daemon */
 static dsa_priv daemon__key;           /* The key data */
 
 /*----- Main code ---------------------------------------------------------*/
 static dsa_priv daemon__key;           /* The key data */
 
 /*----- Main code ---------------------------------------------------------*/
@@ -179,6 +183,8 @@ void daemon_usePort(int port)
 static void daemon__moan(const char *f, int line, const char *msg, void *p)
 {
   syslog(LOG_ERR, "key file error: %s: %d: %s", f, line, msg);
 static void daemon__moan(const char *f, int line, const char *msg, void *p)
 {
   syslog(LOG_ERR, "key file error: %s: %d: %s", f, line, msg);
+  T( trace(TRACE_DAEMON, "daemon: key file error: %s: %d: %s",
+          f, line, msg); )
 }
  
 /* --- @daemon_readKey@ --- *
 }
  
 /* --- @daemon_readKey@ --- *
@@ -198,8 +204,9 @@ void daemon_readKey(const char *kf)
   key *k;
   int err;
 
   key *k;
   int err;
 
-  if (daemon__readKey)
+  if (daemon__keyfile)
     return;
     return;
+  T( trace(TRACE_DAEMON, "daemon: reading key from `%s'", kf); )
   if (key_open(&f, kf, KOPEN_READ, daemon__moan, 0))
     return;
   kp = key_fetchinit(dsa_privfetch, kps, &daemon__key);
   if (key_open(&f, kf, KOPEN_READ, daemon__moan, 0))
     return;
   kp = key_fetchinit(dsa_privfetch, kps, &daemon__key);
@@ -218,6 +225,7 @@ void daemon_readKey(const char *kf)
   }
   key_fetchdone(kp);
   key_close(&f);
   }
   key_fetchdone(kp);
   key_close(&f);
+  daemon__keyfile = kf;
 }
 
 /* --- @daemon__readConfig@ --- *
 }
 
 /* --- @daemon__readConfig@ --- *
@@ -233,13 +241,13 @@ static int daemon__readConfig(const char *cf)
 {
   FILE *fp;
 
 {
   FILE *fp;
 
-  daemon__readKey = 0;
+  daemon__keyfile = 0;
   if ((fp = fopen(cf, "r")) == 0)
     return (-1);
   lexer_scan(fp);
   parse();
   fclose(fp);
   if ((fp = fopen(cf, "r")) == 0)
     return (-1);
   lexer_scan(fp);
   parse();
   fclose(fp);
-  if (!daemon__readKey)
+  if (!daemon__keyfile)
     daemon_readKey(file_KEY);
   T( trace(TRACE_DAEMON, "daemon: read config file"); )
   return (0);
     daemon_readKey(file_KEY);
   T( trace(TRACE_DAEMON, "daemon: read config file"); )
   return (0);
@@ -297,6 +305,14 @@ void daemon__read(int fd, unsigned mode, void *p)
   syslog(LOG_DEBUG, "packet received from %s", sender);
   T( trace(TRACE_DAEMON, "daemon: received request from %s", sender); )
 
   syslog(LOG_DEBUG, "packet received from %s", sender);
   T( trace(TRACE_DAEMON, "daemon: received request from %s", sender); )
 
+  /* --- Sanity check --- */
+
+  if (!daemon__keyfile) {
+    syslog(LOG_NOTICE, "no key file: ignoring request");
+    T( trace(TRACE_DAEMON, "daemon: no key file: ignoring request"); )
+    return;
+  }
+
   /* --- Unpack the block --- */
 
   rq.host = sin.sin_addr;
   /* --- Unpack the block --- */
 
   rq.host = sin.sin_addr;
@@ -422,7 +438,8 @@ static void daemon__rescan(int n, void *p)
     syslog(LOG_ERR, "error reading configuration file");
   sel_rmtimer(&daemon__timer);
   daemon__setTimer();
     syslog(LOG_ERR, "error reading configuration file");
   sel_rmtimer(&daemon__timer);
   daemon__setTimer();
-  fwatch_update(&daemon__watch, daemon__config);
+  fwatch_update(&daemon__cwatch, daemon__config);
+  fwatch_update(&daemon__kwatch, daemon__keyfile);
 }
 
 /* --- @daemon__wakeUp@ --- *
 }
 
 /* --- @daemon__wakeUp@ --- *
@@ -437,9 +454,16 @@ static void daemon__rescan(int n, void *p)
 
 static void daemon__wakeUp(struct timeval *tv, void *p)
 {
 
 static void daemon__wakeUp(struct timeval *tv, void *p)
 {
+  T( trace(TRACE_DAEMON, "daemon: interval timer"); )
   rand_seed(RAND_GLOBAL, 160);
   rand_seed(RAND_GLOBAL, 160);
-  if (fwatch_update(&daemon__watch, daemon__config))
+  daemon__setTimer();
+  if (fwatch_update(&daemon__cwatch, daemon__config))
     daemon__rescan(0, 0);
     daemon__rescan(0, 0);
+  else if (fwatch_update(&daemon__kwatch, daemon__keyfile)) {
+    const char *kf = daemon__keyfile;
+    daemon__keyfile = 0;
+    daemon_readKey(kf);
+  }
 }
 
 /* --- @daemon_init@ --- *
 }
 
 /* --- @daemon_init@ --- *
@@ -500,7 +524,8 @@ void daemon_init(const char *cf, int port, unsigned f)
 
   if (daemon__readConfig(daemon__config))
     die(1, "couldn't read configuration file");
 
   if (daemon__readConfig(daemon__config))
     die(1, "couldn't read configuration file");
-  fwatch_init(&daemon__watch, daemon__config);
+  fwatch_init(&daemon__cwatch, daemon__config);
+  fwatch_init(&daemon__kwatch, daemon__keyfile);
 
   /* --- Decide on a port to use --- *
    *
 
   /* --- Decide on a port to use --- *
    *