5 * Running a `become' daemon
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of `become'
14 * `Become' is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * `Become' is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with `become'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
31 /* --- ANSI headers --- */
39 /* --- Unix headers --- */
41 #include <sys/types.h>
43 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
53 /* --- mLib headers --- */
55 #include <mLib/fwatch.h>
56 #include <mLib/quis.h>
57 #include <mLib/report.h>
61 #include <mLib/trace.h>
63 /* --- Catacomb headers --- */
65 #include <catacomb/buf.h>
66 #include <catacomb/gdsa.h>
67 #include <catacomb/key.h>
68 #include <catacomb/dh.h>
69 #include <catacomb/ec-keys.h>
70 #include <catacomb/mp.h>
71 #include <catacomb/noise.h>
72 #include <catacomb/paranoia.h>
73 #include <catacomb/rand.h>
75 /* --- Local headers --- */
87 /*----- Arbitrary constants -----------------------------------------------*/
89 #define daemon__awakeEvery (10) /* Awaken this often to rescan */
91 /*----- Static variables --------------------------------------------------*/
93 static int daemon__port = -1; /* No particular port yet */
94 static fwatch daemon__cwatch, daemon__kwatch; /* Watching key/config files */
95 static sel_timer daemon__timer; /* Timer for reading */
96 static sel_state daemon__sel; /* Select context */
97 static sel_file daemon__listen; /* Listening socket selector */
98 static const char *daemon__config; /* Configuration file for daemon */
99 static const char *daemon__keyfile; /* Keyring file for daemon */
100 static gdsa daemon__key; /* The key data */
102 /*----- Main code ---------------------------------------------------------*/
104 /* --- @daemon_usePort@ --- *
106 * Arguments: @int port@ = port to use, please
110 * Use: Instructs the daemon to listen to the given port.
113 void daemon_usePort(int port)
118 /* --- @daemon__moan@ --- *
120 * Arguments: @const char *f@ = offending file name
121 * @int line@ = offending line of the file
122 * @const char *msg@ = message
123 * @void *p@ = ignored
127 * Use: Reports an error message about a key file.
130 static void daemon__moan(const char *f, int line, const char *msg, void *p)
132 syslog(LOG_ERR, "key file error: %s: %d: %s", f, line, msg);
133 T( trace(TRACE_DAEMON, "daemon: key file error: %s: %d: %s",
137 /* --- @daemon_readKey@ --- *
139 * Arguments: @const char *kf@ = pointer to key file name to use
143 * Use: Loads the private key from the key file.
146 void daemon_readKey(const char *kf)
159 T( trace(TRACE_DAEMON, "daemon: reading key from `%s'", kf); )
160 if (key_open(&f, kf, KOPEN_READ, daemon__moan, 0))
162 if ((k = key_bytype(&f, "become")) == 0) {
163 syslog(LOG_ERR, "no key of type `become' found");
166 if ((hn = key_getattr(&f, k, "hash")) == 0)
168 sn = key_getattr(&f, k, "sig");
170 if ((g.h = ghash_byname(hn)) == 0) {
171 syslog(LOG_ERR, "key uses unknown hash algorithm `%s'", hn);
174 if (!sn || strcmp(sn, "dsa") == 0) {
176 kp = key_fetchinit(dh_privfetch, 0, &dp);
177 if ((err = key_fetch(kp, k)) != 0) {
178 syslog(LOG_ERR, "error loading key: %s", key_strerror(err));
181 if ((g.g = group_prime(&dp.dp)) == 0) {
182 syslog(LOG_ERR, "bad prime group in key");
186 if (G_FROMINT(g.g, g.p, dp.y)) {
187 syslog(LOG_ERR, "bad public key");
191 } else if (strcmp(sn, "ecdsa") == 0) {
194 kp = key_fetchinit(ec_privfetch, 0, &ep);
195 if ((err = key_fetch(kp, k)) != 0) {
196 syslog(LOG_ERR, "error loading key: %s", key_strerror(err));
199 if ((errmsg = ec_getinfo(&ei, ep.cstr)) != 0) {
200 syslog(LOG_ERR, "bad curve in key: %s", errmsg);
205 if (G_FROMEC(g.g, g.p, &ep.p)) {
206 syslog(LOG_ERR, "bad public point");
211 syslog(LOG_ERR, "key uses unknown signature scheme `%s'", sn);
215 daemon__keyfile = kf;
218 mp_drop(daemon__key.u);
219 G_DESTROY(daemon__key.g, daemon__key.p);
220 G_DESTROYGROUP(daemon__key.g);
223 T( trace(TRACE_DAEMON, "daemon: key read ok"); )
235 T( trace(TRACE_DAEMON, "daemon: failed to read key"); )
239 /* --- @daemon__readConfig@ --- *
241 * Arguments: @const char *cf@ = pointer to configuration file to use
243 * Returns: Zero if it worked, nonzero if it hurt...
245 * Use: Reads the configuration file, and other things.
248 static int daemon__readConfig(const char *cf)
253 if ((fp = fopen(cf, "r")) == 0)
258 if (!daemon__keyfile)
259 daemon_readKey(file_KEY);
260 T( trace(TRACE_DAEMON, "daemon: read config file"); )
264 /* --- @daemon__read@ --- *
266 * Arguments: @int fd@ = socket handle
267 * @unsigned mode@ = ignored
268 * @void *p@ = ignored
272 * Use: Examines a buffer, and returns a response.
275 void daemon__read(int fd, unsigned mode, void *p)
277 unsigned char buff[65536]; /* Buffer for incoming packets */
278 struct sockaddr_in sin; /* Address of packet sender */
279 char sender[64]; /* Sender's hostname (resolved) */
280 ghash *h; /* Hash context */
281 request rq; /* Request buffer for verification */
282 ssize_t sz; /* Length of incoming message */
283 socklen_t slen; /* Length of incoming address */
284 uint32 u; /* Scratch integer */
285 uint16 ul; /* And another */
286 struct hostent *he; /* Resolve structure */
287 gdsa_sig s = GDSA_SIG_INIT; /* Signature block */
288 int ans; /* Answer from the check */
289 buf b; /* Buffer for parsing request */
291 /* --- Kick some randomness in the pot --- */
293 noise_timer(RAND_GLOBAL);
295 /* --- Read the message --- */
298 if ((sz = recvfrom(fd, (char *)buff, sizeof(buff), 0,
299 (struct sockaddr *)&sin, &slen)) < 0) {
300 T( trace(TRACE_DAEMON, "daemon: error reading packet: %s",
302 syslog(LOG_INFO, "duff packet received: %e");
306 /* --- Resolve the host name --- */
308 he = gethostbyaddr((char *)&sin.sin_addr, sizeof(sin.sin_addr), AF_INET);
310 strncat(sender, he ? he->h_name : inet_ntoa(sin.sin_addr),
312 syslog(LOG_DEBUG, "packet received from %s", sender);
313 T( trace(TRACE_DAEMON, "daemon: received request from %s", sender); )
315 /* --- Sanity check --- */
317 if (!daemon__keyfile) {
318 syslog(LOG_NOTICE, "no key file: ignoring request");
319 T( trace(TRACE_DAEMON, "daemon: no key file: ignoring request"); )
323 /* --- Unpack the block --- */
325 rq.host = sin.sin_addr;
326 buf_init(&b, buff, sz);
327 if (buf_ensure(&b, 64)) goto fail; BSTEP(&b, 64);
328 if (buf_getu32(&b, &u)) goto fail; rq.from = u;
329 if (buf_getu32(&b, &u)) goto fail; rq.to = u;
330 if (buf_getu16(&b, &ul) || buf_ensure(&b, ul) || ul >= sizeof(rq.cmd))
332 memcpy(rq.cmd, BCUR(&b), ul);
335 if (BLEFT(&b)) goto fail;
337 /* --- Hash the request block --- */
339 h = GH_INIT(daemon__key.h);
340 GH_HASH(h, buff, sz);
342 /* --- Build a reply block --- */
344 ans = rule_check(&rq);
345 syslog(LOG_INFO, "request from %s for %i to become %i to run %s %s",
346 sender, rq.from, rq.to, rq.cmd, ans ? "granted" : "denied");
347 buf_init(&b, buff, sizeof(buff));
348 buf_put(&b, GH_DONE(h, 0), GH_CLASS(h)->hashsz);
349 buf_putbyte(&b, ans);
350 if (BBAD(&b)) goto fail;
352 /* --- Sign the reply block --- */
354 h = gdsa_beginhash(&daemon__key);
355 GH_HASH(h, BBASE(&b), BLEN(&b));
356 gdsa_endhash(&daemon__key, h);
357 gdsa_sign(&daemon__key, &s, GH_DONE(h, 0), 0);
359 buf_putmp(&b, s.r); buf_putmp(&b, s.s);
360 mp_drop(s.r); mp_drop(s.s);
361 if (BBAD(&b)) goto fail;
363 /* --- Send the reply off --- */
365 sendto(fd, BBASE(&b), BLEN(&b), 0, (struct sockaddr *)&sin, sizeof(sin));
366 T( trace(TRACE_DAEMON, "daemon: reply sent"); )
370 syslog(LOG_ERR, "couldn't respond to query");
371 T( trace(TRACE_DAEMON, "daemon: failed to answer query"); )
374 /* --- @daemon__die@ --- *
376 * Arguments: @int n@ = signal number
377 * @void *p@ = ignored
381 * Use: Exits the daemon.
384 static void daemon__die(int n, void *p)
386 T( trace(TRACE_DAEMON, "daemon: killed by signal %i", n); )
387 syslog(LOG_NOTICE, "killed by signal type %i", n);
392 /* --- @daemon__setTimer@ --- *
398 * Use: Sets the interval timer up.
401 static void daemon__wakeUp(struct timeval *tv, void *p);
403 static void daemon__setTimer(void)
407 gettimeofday(&tv, 0);
408 tv.tv_sec += daemon__awakeEvery;
409 sel_addtimer(&daemon__sel, &daemon__timer, &tv, daemon__wakeUp, 0);
412 /* --- @daemon__rescan@ --- *
414 * Arguments: @int n@ = signal number
415 * @void *p@ = ignored
419 * Use: Forces a rescan of the daemon's configuration.
422 static void daemon__rescan(int n, void *p)
424 syslog(LOG_INFO, "rescanning configuration file");
435 if (daemon__readConfig(daemon__config))
436 syslog(LOG_ERR, "error reading configuration file");
437 sel_rmtimer(&daemon__timer);
439 fwatch_update(&daemon__cwatch, daemon__config);
440 fwatch_update(&daemon__kwatch, daemon__keyfile);
443 /* --- @daemon__wakeUp@ --- *
445 * Arguments: @struct timeval *tv@ = ignored
446 * @void *p@ = ignored
450 * Use: Wakes up periodically to check the configuration file.
453 static void daemon__wakeUp(struct timeval *tv, void *p)
455 T( trace(TRACE_DAEMON, "daemon: interval timer"); )
456 rand_seed(RAND_GLOBAL, 160);
458 if (fwatch_update(&daemon__cwatch, daemon__config))
459 daemon__rescan(0, 0);
460 else if (fwatch_update(&daemon__kwatch, daemon__keyfile)) {
461 const char *kf = daemon__keyfile;
467 /* --- @daemon_init@ --- *
469 * Arguments: @const char *cf@ = pointer to name of configuration file
470 * @int port@ = port to listen to, or %$-1$% for default
471 * @unsigned f@ = various flags
475 * Use: Starts `become' up in daemon mode.
478 void daemon_init(const char *cf, int port, unsigned f)
483 static struct sigvec {
485 void (*proc)(int n, void *p);
488 { SIGHUP, daemon__rescan },
489 { SIGINT, daemon__die },
490 { SIGTERM, daemon__die },
491 { SIGQUIT, daemon__die },
495 /* --- Remove my root privileges --- *
497 * Just in case there's anything dodgy in my configuration file, or the
498 * user wants me to start on a funny port.
503 /* --- Initialize the random number generator --- */
505 rand_noisesrc(RAND_GLOBAL, &noise_source);
506 rand_seed(RAND_GLOBAL, 160);
508 /* --- Initialise bits of the program --- */
512 sel_init(&daemon__sel);
513 sig_init(&daemon__sel);
520 openlog(quis(), 0, LOG_DAEMON);
521 syslog(LOG_NOTICE, "starting up");
523 if (daemon__readConfig(daemon__config))
524 die(1, "couldn't read configuration file");
525 fwatch_init(&daemon__cwatch, daemon__config);
526 fwatch_init(&daemon__kwatch, daemon__keyfile);
528 /* --- Decide on a port to use --- *
530 * If I don't have a port yet (e.g., from the configuration file) then
531 * look it up in /etc/services under whatever name I was started as.
534 if (daemon__port == 0) {
535 struct servent *se = getservbyname(quis(), "udp");
537 daemon__port = se->s_port;
539 daemon__port = htons(SERVER_PORT);
542 /* --- Now set up a socket --- */
545 struct sockaddr_in sin;
547 if ((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
548 die(1, "couldn't create socket: %s", strerror(errno));
549 sin.sin_family = AF_INET;
550 sin.sin_port = daemon__port;
551 sin.sin_addr.s_addr = htonl(INADDR_ANY);
552 if (bind(s, (struct sockaddr *)&sin, sizeof(sin))) {
553 die(1, "couldn't bind socket to port %i: %s",
554 ntohs(daemon__port), strerror(errno));
558 /* --- Fork off into the sunset --- */
560 if (!(f & df_nofork)) {
564 /* --- Make a background process --- */
567 die(1, "couldn't fork daemon: %s", strerror(errno));
571 /* --- Disconnect from the terminal --- */
575 /* --- Write my process id to a file --- */
577 if ((fp = fopen(file_PID, "w")) != 0) {
578 fprintf(fp, "%lu\n", (unsigned long)getpid());
581 T( trace(TRACE_DAEMON, "daemon: forked to pid %li", (long)getpid()); )
584 /* --- Set signal handlers --- */
586 for (i = 0; sigs[i].proc; i++)
587 sig_add(&sigs[i].s, sigs[i].sig, sigs[i].proc, 0);
589 /* --- Set the timer for rescanning the file --- */
593 /* --- Watch for input --- */
595 sel_initfile(&daemon__sel, &daemon__listen, s, SEL_READ,
597 sel_addfile(&daemon__listen);
599 /* --- Now wait for something exciting to happen --- */
602 if (sel_select(&daemon__sel)) {
603 if (errno == EINTR || errno == EAGAIN)
605 syslog(LOG_ERR, "error from select: %s", strerror(errno));
611 /*----- That's all, folks -------------------------------------------------*/