X-Git-Url: https://git.distorted.org.uk/~mdw/become/blobdiff_plain/f60a34341fee6aafd5b878dce23b80af7c60064d..8fc4c5cd7e7c8590b55bf41e4f984a4952374b4b:/src/daemon.c diff --git a/src/daemon.c b/src/daemon.c index 8d69510..3d29735 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: daemon.c,v 1.12 2003/10/12 00:14:55 mdw Exp $ + * $Id: daemon.c,v 1.15 2003/10/26 11:57:46 mdw Exp $ * * Running a `become' daemon * @@ -29,6 +29,15 @@ /*----- Revision history --------------------------------------------------* * * $Log: daemon.c,v $ + * Revision 1.15 2003/10/26 11:57:46 mdw + * Fix key reloading core dumps. Change advice on keys. + * + * 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.12 2003/10/12 00:14:55 mdw * Major overhaul. Now uses DSA signatures rather than the bogus symmetric * encrypt-and-hope thing. Integrated with mLib and Catacomb. @@ -132,17 +141,17 @@ /*----- Arbitrary constants -----------------------------------------------*/ -#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 ---------------------------------------------------------*/ @@ -176,6 +185,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@ --- * @@ -195,8 +206,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); @@ -212,6 +224,7 @@ void daemon_readKey(const char *kf) mp_copy(daemon__key.dp.g); mp_copy(daemon__key.x); mp_copy(daemon__key.y); + daemon__keyfile = kf; } key_fetchdone(kp); key_close(&f); @@ -230,13 +243,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); @@ -294,6 +307,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; @@ -419,7 +440,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@ --- * @@ -434,22 +456,30 @@ 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@ --- * * * Arguments: @const char *cf@ = pointer to name of configuration file * @int port@ = port to listen to, or %$-1$% for default + * @unsigned f@ = various flags * * Returns: Never. * * Use: Starts `become' up in daemon mode. */ -void daemon_init(const char *cf, int port) +void daemon_init(const char *cf, int port, unsigned f) { int s; int i; @@ -496,7 +526,8 @@ void daemon_init(const char *cf, int port) 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 --- * * @@ -529,8 +560,7 @@ void daemon_init(const char *cf, int port) /* --- Fork off into the sunset --- */ -#ifdef NDEBUG - { + if (!(f & df_nofork)) { int pid = fork(); FILE *fp; @@ -553,7 +583,6 @@ void daemon_init(const char *cf, int port) } T( trace(TRACE_DAEMON, "daemon: forked to pid %li", (long)getpid()); ) } -#endif /* --- Set signal handlers --- */