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