From: mdw Date: Fri, 17 Oct 2003 16:30:22 +0000 (+0000) Subject: Reload keys and config files automatically. X-Git-Url: https://git.distorted.org.uk/~mdw/become/commitdiff_plain/030c25b77f357ef0657460d7590cc662b6f7a9b3 Reload keys and config files automatically. --- diff --git a/.cvsignore b/.cvsignore index 91bc682..ac43b98 100644 --- a/.cvsignore +++ b/.cvsignore @@ -16,3 +16,4 @@ patches sgi stamp-h.in sun +test diff --git a/src/daemon.c b/src/daemon.c index 6c515f4..fbaf26b 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -1,6 +1,6 @@ /* -*-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 * @@ -29,6 +29,9 @@ /*----- 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. * @@ -135,17 +138,18 @@ /*----- 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 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 const char *daemon__keyfile; /* Keyring file for daemon */ 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); + T( trace(TRACE_DAEMON, "daemon: key file error: %s: %d: %s", + f, line, msg); ) } /* --- @daemon_readKey@ --- * @@ -198,8 +204,9 @@ void daemon_readKey(const char *kf) key *k; int err; - if (daemon__readKey) + if (daemon__keyfile) 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); @@ -218,6 +225,7 @@ void daemon_readKey(const char *kf) } key_fetchdone(kp); key_close(&f); + daemon__keyfile = kf; } /* --- @daemon__readConfig@ --- * @@ -233,13 +241,13 @@ static int daemon__readConfig(const char *cf) { FILE *fp; - daemon__readKey = 0; + daemon__keyfile = 0; 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); @@ -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); ) + /* --- 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; @@ -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(); - fwatch_update(&daemon__watch, daemon__config); + fwatch_update(&daemon__cwatch, daemon__config); + fwatch_update(&daemon__kwatch, daemon__keyfile); } /* --- @daemon__wakeUp@ --- * @@ -437,9 +454,16 @@ static void daemon__rescan(int n, void *p) static void daemon__wakeUp(struct timeval *tv, void *p) { + T( trace(TRACE_DAEMON, "daemon: interval timer"); ) 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); + else if (fwatch_update(&daemon__kwatch, daemon__keyfile)) { + const char *kf = daemon__keyfile; + daemon__keyfile = 0; + daemon_readKey(kf); + } } /* --- @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"); - fwatch_init(&daemon__watch, daemon__config); + fwatch_init(&daemon__cwatch, daemon__config); + fwatch_init(&daemon__kwatch, daemon__keyfile); /* --- Decide on a port to use --- * *