X-Git-Url: https://git.distorted.org.uk/~mdw/become/blobdiff_plain/4c37c6af624a9d4dabadac32a5e566d388ee78d7..b0f66028a9a17a845590a4fe737a4f5e46f6b778:/src/daemon.c diff --git a/src/daemon.c b/src/daemon.c index 6c515f4..20698ec 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.16 2003/11/29 23:39:16 mdw Exp $ * * Running a `become' daemon * @@ -29,6 +29,15 @@ /*----- Revision history --------------------------------------------------* * * $Log: daemon.c,v $ + * Revision 1.16 2003/11/29 23:39:16 mdw + * Debianization. + * + * 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. * @@ -135,17 +144,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 ---------------------------------------------------------*/ @@ -179,6 +188,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 +209,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); @@ -215,6 +227,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); @@ -233,13 +246,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 +310,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 +443,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 +459,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 +529,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 --- * * @@ -510,9 +540,10 @@ void daemon_init(const char *cf, int port, unsigned f) if (daemon__port == 0) { struct servent *se = getservbyname(quis(), "udp"); - if (!se) - die(1, "no idea which port to listen to"); - daemon__port = se->s_port; + if (se) + daemon__port = se->s_port; + else + daemon__port = htons(SERVER_PORT); } /* --- Now set up a socket --- */