Light fixes for strange building.
[become] / src / daemon.c
CommitLineData
c4f2d992 1/* -*-c-*-
2 *
f60a3434 3 * $Id: daemon.c,v 1.12 2003/10/12 00:14:55 mdw Exp $
c4f2d992 4 *
5 * Running a `become' daemon
6 *
c758e654 7 * (c) 1998 EBI
c4f2d992 8 */
9
03f996bd 10/*----- Licensing notice --------------------------------------------------*
c4f2d992 11 *
12 * This file is part of `become'
13 *
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.
18 *
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.
23 *
24 * You should have received a copy of the GNU General Public License
03f996bd 25 * along with `become'; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
c4f2d992 27 */
28
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: daemon.c,v $
f60a3434 32 * Revision 1.12 2003/10/12 00:14:55 mdw
33 * Major overhaul. Now uses DSA signatures rather than the bogus symmetric
34 * encrypt-and-hope thing. Integrated with mLib and Catacomb.
35 *
79fae27d 36 * Revision 1.11 1999/05/04 16:17:12 mdw
37 * Change to header file name for parser. See log for `parse.h' for
38 * details.
39 *
40 * Revision 1.10 1998/04/23 13:23:09 mdw
8702965d 41 * Support new interface to configuration file parser.
42 *
c758e654 43 * Revision 1.9 1998/01/12 16:45:59 mdw
44 * Fix copyright date.
45 *
9e5602f0 46 * Revision 1.8 1997/09/26 09:14:58 mdw
47 * Merged blowfish branch into trunk.
48 *
49 * Revision 1.7.2.1 1997/09/26 09:08:05 mdw
50 * Use the Blowfish encryption algorithm instead of IDEA. This is partly
51 * because I prefer Blowfish (without any particularly strong evidence) but
52 * mainly because IDEA is patented and Blowfish isn't.
53 *
0c6c5a97 54 * Revision 1.7 1997/09/17 10:23:23 mdw
55 * Fix a typo. Port numbers are in network order now, so don't change them.
56 *
f3debbd8 57 * Revision 1.6 1997/09/09 18:17:06 mdw
58 * Allow default port to be given as a service name or port number.
59 *
60 * Revision 1.5 1997/08/20 16:17:10 mdw
ffdb9474 61 * More sensible restart routine: `_reinit' functions replaced by `_end' and
62 * `_init' functions.
63 *
a63eaefc 64 * Revision 1.4 1997/08/07 10:00:37 mdw
65 * (Log entry for previous version is bogus.) Read netgroups database.
66 * Give up privileges permanently on startup.
607937a4 67 *
03f996bd 68 * Revision 1.2 1997/08/04 10:24:21 mdw
69 * Sources placed under CVS control.
70 *
71 * Revision 1.1 1997/07/21 13:47:50 mdw
c4f2d992 72 * Initial revision
73 *
74 */
75
76/*----- Header files ------------------------------------------------------*/
77
78/* --- ANSI headers --- */
79
80#include <errno.h>
81#include <signal.h>
c4f2d992 82#include <stdio.h>
83#include <stdlib.h>
84#include <string.h>
85
86/* --- Unix headers --- */
87
88#include <sys/types.h>
89#include <sys/time.h>
90#include <sys/socket.h>
91
92#include <netinet/in.h>
93
94#include <arpa/inet.h>
95
96#include <netdb.h>
97#include <syslog.h>
98#include <unistd.h>
99
f60a3434 100/* --- mLib headers --- */
101
102#include <mLib/fwatch.h>
103#include <mLib/quis.h>
104#include <mLib/report.h>
105#include <mLib/sel.h>
106#include <mLib/sig.h>
107#include <mLib/sym.h>
108#include <mLib/trace.h>
109
110/* --- Catacomb headers --- */
111
112#include <catacomb/buf.h>
113#include <catacomb/dsa.h>
114#include <catacomb/key.h>
115#include <catacomb/mp.h>
116#include <catacomb/noise.h>
117#include <catacomb/paranoia.h>
118#include <catacomb/rand.h>
119#include <catacomb/sha.h>
120
c4f2d992 121/* --- Local headers --- */
122
123#include "become.h"
124#include "config.h"
c4f2d992 125#include "daemon.h"
c4f2d992 126#include "lexer.h"
127#include "name.h"
607937a4 128#include "netg.h"
79fae27d 129#include "parse.h"
c4f2d992 130#include "rule.h"
c4f2d992 131#include "userdb.h"
c4f2d992 132
133/*----- Arbitrary constants -----------------------------------------------*/
134
f60a3434 135#define daemon__awakeEvery (5 * 60) /* Awaken this often to rescan */
c4f2d992 136
137/*----- Static variables --------------------------------------------------*/
138
c4f2d992 139static int daemon__port = -1; /* No particular port yet */
c4f2d992 140static int daemon__readKey = 0; /* Have I read a key? */
f60a3434 141static fwatch daemon__watch;
142static sel_timer daemon__timer; /* Timer for reading */
143static sel_state daemon__sel; /* Select context */
144static sel_file daemon__listen; /* Listening socket selector */
145static const char *daemon__config; /* Configuration file for daemon */
146static dsa_priv daemon__key; /* The key data */
c4f2d992 147
148/*----- Main code ---------------------------------------------------------*/
149
150/* --- @daemon_usePort@ --- *
151 *
152 * Arguments: @int port@ = port to use, please
153 *
154 * Returns: ---
155 *
156 * Use: Instructs the daemon to listen to the given port.
157 */
158
159void daemon_usePort(int port)
160{
161 daemon__port = port;
162}
163
f60a3434 164/* --- @daemon__moan@ --- *
165 *
166 * Arguments: @const char *f@ = offending file name
167 * @int line@ = offending line of the file
168 * @const char *msg@ = message
169 * @void *p@ = ignored
170 *
171 * Returns: ---
172 *
173 * Use: Reports an error message about a key file.
174 */
175
176static void daemon__moan(const char *f, int line, const char *msg, void *p)
177{
178 syslog(LOG_ERR, "key file error: %s: %d: %s", f, line, msg);
179}
180
c4f2d992 181/* --- @daemon_readKey@ --- *
182 *
f60a3434 183 * Arguments: @const char *kf@ = pointer to key file name to use
c4f2d992 184 *
185 * Returns: ---
186 *
f60a3434 187 * Use: Loads the private key from the key file.
c4f2d992 188 */
189
190void daemon_readKey(const char *kf)
191{
f60a3434 192 key_packstruct kps[DSA_PRIVFETCHSZ];
193 key_packdef *kp;
194 key_file f;
195 key *k;
196 int err;
c4f2d992 197
f60a3434 198 if (daemon__readKey)
c4f2d992 199 return;
f60a3434 200 if (key_open(&f, kf, KOPEN_READ, daemon__moan, 0))
c4f2d992 201 return;
f60a3434 202 kp = key_fetchinit(dsa_privfetch, kps, &daemon__key);
203 if ((k = key_bytype(&f, "become-dsa")) == 0)
204 err = KERR_NOTFOUND;
205 else
206 err = key_fetch(kp, k);
207 if (err)
208 syslog(LOG_ERR, "couldn't load key: %s", key_strerror(err));
209 else {
210 mp_copy(daemon__key.dp.p);
211 mp_copy(daemon__key.dp.q);
212 mp_copy(daemon__key.dp.g);
213 mp_copy(daemon__key.x);
214 mp_copy(daemon__key.y);
c4f2d992 215 }
f60a3434 216 key_fetchdone(kp);
217 key_close(&f);
c4f2d992 218}
219
220/* --- @daemon__readConfig@ --- *
221 *
222 * Arguments: @const char *cf@ = pointer to configuration file to use
223 *
224 * Returns: Zero if it worked, nonzero if it hurt...
225 *
226 * Use: Reads the configuration file, and other things.
227 */
228
229static int daemon__readConfig(const char *cf)
230{
231 FILE *fp;
232
233 daemon__readKey = 0;
234 if ((fp = fopen(cf, "r")) == 0)
235 return (-1);
236 lexer_scan(fp);
8702965d 237 parse();
c4f2d992 238 fclose(fp);
239 if (!daemon__readKey)
240 daemon_readKey(file_KEY);
03f996bd 241 T( trace(TRACE_DAEMON, "daemon: read config file"); )
c4f2d992 242 return (0);
243}
244
c4f2d992 245/* --- @daemon__read@ --- *
246 *
247 * Arguments: @int fd@ = socket handle
f60a3434 248 * @unsigned mode@ = ignored
249 * @void *p@ = ignored
c4f2d992 250 *
251 * Returns: ---
252 *
253 * Use: Examines a buffer, and returns a response.
254 */
255
f60a3434 256void daemon__read(int fd, unsigned mode, void *p)
c4f2d992 257{
258 unsigned char buff[65536]; /* Buffer for incoming packets */
c4f2d992 259 struct sockaddr_in sin; /* Address of packet sender */
260 char sender[64]; /* Sender's hostname (resolved) */
f60a3434 261 octet h[SHA_HASHSZ]; /* Hash of the transmission buffer */
262 sha_ctx hc; /* Hashing context */
c4f2d992 263 request rq; /* Request buffer for verification */
f60a3434 264 ssize_t sz; /* Length of incoming message */
265 socklen_t slen; /* Length of incoming address */
266 uint32 u; /* Scratch integer */
267 uint16 ul; /* And another */
268 struct hostent *he; /* Resolve structure */
269 mp *m, *k, *r, *s; /* Integers for signing */
270 int ans; /* Answer from the check */
271 buf b; /* Buffer for parsing request */
c4f2d992 272
f60a3434 273 /* --- Kick some randomness in the pot --- */
c4f2d992 274
f60a3434 275 noise_timer(RAND_GLOBAL);
c4f2d992 276
f60a3434 277 /* --- Read the message --- */
278
279 slen = sizeof(sin);
280 if ((sz = recvfrom(fd, (char *)buff, sizeof(buff), 0,
281 (struct sockaddr *)&sin, &slen)) < 0) {
282 T( trace(TRACE_DAEMON, "daemon: error reading packet: %s",
283 strerror(errno)); )
284 syslog(LOG_INFO, "duff packet received: %e");
285 return;
c4f2d992 286 }
287
288 /* --- Resolve the host name --- */
289
f60a3434 290 he = gethostbyaddr((char *)&sin.sin_addr, sizeof(sin.sin_addr), AF_INET);
291 sender[0] = 0;
292 strncat(sender, he ? he->h_name : inet_ntoa(sin.sin_addr),
293 sizeof(sender) - 1);
294 syslog(LOG_DEBUG, "packet received from %s", sender);
295 T( trace(TRACE_DAEMON, "daemon: received request from %s", sender); )
c4f2d992 296
297 /* --- Unpack the block --- */
298
c4f2d992 299 rq.host = sin.sin_addr;
f60a3434 300 buf_init(&b, buff, sz);
301 if (buf_ensure(&b, SHA_HASHSZ)) goto fail; BSTEP(&b, SHA_HASHSZ);
302 if (buf_getu32(&b, &u)) goto fail; rq.from = u;
303 if (buf_getu32(&b, &u)) goto fail; rq.to = u;
304 if (buf_getu16(&b, &ul) || buf_ensure(&b, ul) || ul >= sizeof(rq.cmd))
305 goto fail;
306 memcpy(rq.cmd, BCUR(&b), ul);
307 rq.cmd[ul] = 0;
308 BSTEP(&b, ul);
309 if (BLEFT(&b)) goto fail;
310
311 /* --- Hash the request block --- */
312
313 sha_init(&hc);
314 sha_hash(&hc, buff, sz);
315 sha_done(&hc, h);
c4f2d992 316
317 /* --- Build a reply block --- */
318
f60a3434 319 ans = rule_check(&rq);
320 syslog(LOG_INFO, "request from %s for %i to become %i to run %s %s",
321 sender, rq.from, rq.to, rq.cmd, ans ? "granted" : "denied");
322 buf_init(&b, buff, sizeof(buff));
323 if (buf_put(&b, h, sizeof(h)) || buf_putbyte(&b, ans))
324 goto fail;
325
326 /* --- Sign the reply block --- */
327
328 sha_init(&hc);
329 sha_hash(&hc, BBASE(&b), BLEN(&b));
330 sha_done(&hc, h);
331 m = mp_loadb(MP_NEW, h, sizeof(h));
332 rand_get(RAND_GLOBAL, h, sizeof(h));
333 k = mp_loadb(MP_NEWSEC, h, sizeof(h));
334 r = s = MP_NEW;
335 dsa_mksig(&daemon__key.dp, daemon__key.x, m, k, &r, &s);
336 buf_putmp(&b, r);
337 buf_putmp(&b, s);
338 mp_drop(m);
339 mp_drop(k);
340 mp_drop(r);
341 mp_drop(s);
342 if (BBAD(&b))
343 goto fail;
c4f2d992 344
345 /* --- Send the reply off --- */
346
f60a3434 347 sendto(fd, BBASE(&b), BLEN(&b), 0, (struct sockaddr *)&sin, sizeof(sin));
03f996bd 348 T( trace(TRACE_DAEMON, "daemon: reply sent"); )
f60a3434 349 return;
350
351fail:
352 syslog(LOG_ERR, "couldn't respond to query");
353 T( trace(TRACE_DAEMON, "daemon: failed to answer query"); )
354}
355
356/* --- @daemon__die@ --- *
357 *
358 * Arguments: @int n@ = signal number
359 * @void *p@ = ignored
360 *
361 * Returns: Doesn't.
362 *
363 * Use: Exits the daemon.
364 */
365
366static void daemon__die(int n, void *p)
367{
368 T( trace(TRACE_DAEMON, "daemon: killed by signal %i", n); )
369 syslog(LOG_NOTICE, "killed by signal type %i", n);
370 remove(file_PID);
371 exit(0);
372}
373
374/* --- @daemon__setTimer@ --- *
375 *
376 * Arguments: ---
377 *
378 * Returns: ---
379 *
380 * Use: Sets the interval timer up.
381 */
382
383static void daemon__wakeUp(struct timeval *tv, void *p);
384
385static void daemon__setTimer(void)
386{
387 struct timeval tv;
388
389 gettimeofday(&tv, 0);
390 tv.tv_sec += daemon__awakeEvery;
391 sel_addtimer(&daemon__sel, &daemon__timer, &tv, daemon__wakeUp, 0);
392}
393
394/* --- @daemon__rescan@ --- *
395 *
396 * Arguments: @int n@ = signal number
397 * @void *p@ = ignored
398 *
399 * Returns: ---
400 *
401 * Use: Forces a rescan of the daemon's configuration.
402 */
403
404static void daemon__rescan(int n, void *p)
405{
406 syslog(LOG_INFO, "rescanning configuration file");
407 name_end();
408 rule_end();
409 netg_end();
410 userdb_end();
411 dsa_privfree(&daemon__key);
412 userdb_init();
413 userdb_local();
414 userdb_yp();
415 netg_init();
416 rule_init();
417 name_init();
418 if (daemon__readConfig(daemon__config))
419 syslog(LOG_ERR, "error reading configuration file");
420 sel_rmtimer(&daemon__timer);
421 daemon__setTimer();
422 fwatch_update(&daemon__watch, daemon__config);
423}
424
425/* --- @daemon__wakeUp@ --- *
426 *
427 * Arguments: @struct timeval *tv@ = ignored
428 * @void *p@ = ignored
429 *
430 * Returns: ---
431 *
432 * Use: Wakes up periodically to check the configuration file.
433 */
434
435static void daemon__wakeUp(struct timeval *tv, void *p)
436{
437 rand_seed(RAND_GLOBAL, 160);
438 if (fwatch_update(&daemon__watch, daemon__config))
439 daemon__rescan(0, 0);
c4f2d992 440}
441
442/* --- @daemon_init@ --- *
443 *
444 * Arguments: @const char *cf@ = pointer to name of configuration file
445 * @int port@ = port to listen to, or %$-1$% for default
446 *
447 * Returns: Never.
448 *
449 * Use: Starts `become' up in daemon mode.
450 */
451
452void daemon_init(const char *cf, int port)
453{
454 int s;
f60a3434 455 int i;
456
457 static struct sigvec {
458 int sig;
459 void (*proc)(int n, void *p);
460 sig s;
461 } sigs[] = {
462 { SIGHUP, daemon__rescan },
463 { SIGINT, daemon__die },
464 { SIGTERM, daemon__die },
465 { SIGQUIT, daemon__die },
466 { 0, 0 }
467 };
c4f2d992 468
469 /* --- Remove my root privileges --- *
470 *
471 * Just in case there's anything dodgy in my configuration file, or the
472 * user wants me to start on a funny port.
473 */
474
607937a4 475 setuid(getuid());
c4f2d992 476
f60a3434 477 /* --- Initialize the random number generator --- */
478
479 rand_noisesrc(RAND_GLOBAL, &noise_source);
480 rand_seed(RAND_GLOBAL, 160);
481
c4f2d992 482 /* --- Initialise bits of the program --- */
483
f60a3434 484 daemon__config = cf;
c4f2d992 485 daemon__port = port;
f60a3434 486 sel_init(&daemon__sel);
487 sig_init(&daemon__sel);
c4f2d992 488 userdb_init();
489 userdb_local();
490 userdb_yp();
607937a4 491 netg_init();
c4f2d992 492 name_init();
493 rule_init();
494 openlog(quis(), 0, LOG_DAEMON);
495 syslog(LOG_NOTICE, "starting up");
496
f60a3434 497 if (daemon__readConfig(daemon__config))
498 die(1, "couldn't read configuration file");
499 fwatch_init(&daemon__watch, daemon__config);
c4f2d992 500
501 /* --- Decide on a port to use --- *
502 *
503 * If I don't have a port yet (e.g., from the configuration file) then
504 * look it up in /etc/services under whatever name I was started as.
505 */
506
0c6c5a97 507 if (daemon__port == 0) {
c4f2d992 508 struct servent *se = getservbyname(quis(), "udp");
509 if (!se)
f60a3434 510 die(1, "no idea which port to listen to");
f3debbd8 511 daemon__port = se->s_port;
c4f2d992 512 }
513
514 /* --- Now set up a socket --- */
515
516 {
517 struct sockaddr_in sin;
518
519 if ((s = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
f60a3434 520 die(1, "couldn't create socket: %s", strerror(errno));
c4f2d992 521 sin.sin_family = AF_INET;
0c6c5a97 522 sin.sin_port = daemon__port;
c4f2d992 523 sin.sin_addr.s_addr = htonl(INADDR_ANY);
0c6c5a97 524 if (bind(s, (struct sockaddr *)&sin, sizeof(sin))) {
f60a3434 525 die(1, "couldn't bind socket to port %i: %s",
0c6c5a97 526 ntohs(daemon__port), strerror(errno));
527 }
c4f2d992 528 }
529
530 /* --- Fork off into the sunset --- */
531
532#ifdef NDEBUG
533 {
534 int pid = fork();
535 FILE *fp;
536
537 /* --- Make a background process --- */
538
539 if (pid == -1)
f60a3434 540 die(1, "couldn't fork daemon: %s", strerror(errno));
c4f2d992 541 else if (pid != 0)
542 return;
543
544 /* --- Disconnect from the terminal --- */
545
546 setsid();
547
548 /* --- Write my process id to a file --- */
549
550 if ((fp = fopen(file_PID, "w")) != 0) {
551 fprintf(fp, "%lu\n", (unsigned long)getpid());
552 fclose(fp);
553 }
03f996bd 554 T( trace(TRACE_DAEMON, "daemon: forked to pid %li", (long)getpid()); )
c4f2d992 555 }
556#endif
557
f60a3434 558 /* --- Set signal handlers --- */
c4f2d992 559
f60a3434 560 for (i = 0; sigs[i].proc; i++)
561 sig_add(&sigs[i].s, sigs[i].sig, sigs[i].proc, 0);
c4f2d992 562
f60a3434 563 /* --- Set the timer for rescanning the file --- */
c4f2d992 564
f60a3434 565 daemon__setTimer();
c4f2d992 566
f60a3434 567 /* --- Watch for input --- */
c4f2d992 568
f60a3434 569 sel_initfile(&daemon__sel, &daemon__listen, s, SEL_READ,
570 daemon__read, 0);
571 sel_addfile(&daemon__listen);
c4f2d992 572
f60a3434 573 /* --- Now wait for something exciting to happen --- */
c4f2d992 574
f60a3434 575 for (;;) {
576 if (sel_select(&daemon__sel)) {
577 if (errno == EINTR || errno == EAGAIN)
578 continue;
579 syslog(LOG_ERR, "error from select: %s", strerror(errno));
580 exit(1);
c4f2d992 581 }
582 }
583}
584
585/*----- That's all, folks -------------------------------------------------*/