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