Commit | Line | Data |
---|---|---|
9da480be MW |
1 | /* -*-c-*- |
2 | * | |
3 | * Main daemon | |
4 | * | |
5 | * (c) 2012 Straylight/Edgeware | |
6 | */ | |
7 | ||
8 | /*----- Licensing notice --------------------------------------------------* | |
9 | * | |
10 | * This file is part of Yet Another Ident Daemon (YAID). | |
11 | * | |
12 | * YAID is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or | |
15 | * (at your option) any later version. | |
16 | * | |
17 | * YAID is distributed in the hope that it will be useful, | |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License | |
23 | * along with YAID; if not, write to the Free Software Foundation, | |
24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
25 | */ | |
26 | ||
27 | /*----- Header files ------------------------------------------------------*/ | |
28 | ||
29 | #include "yaid.h" | |
30 | ||
31 | /*----- Data structures ---------------------------------------------------*/ | |
32 | ||
c3794524 MW |
33 | /* A write buffer is the gadget which keeps track of our output and writes |
34 | * portions of it out as and when connections are ready for it. | |
35 | */ | |
9da480be MW |
36 | #define WRBUFSZ 1024 |
37 | struct writebuf { | |
c3794524 MW |
38 | size_t o; /* Offset of remaining data */ |
39 | size_t n; /* Length of remaining data */ | |
40 | sel_file wr; /* Write selector */ | |
41 | void (*func)(int /*err*/, void *); /* Function to call on completion */ | |
42 | void *p; /* Context for `func' */ | |
43 | unsigned char buf[WRBUFSZ]; /* Output buffer */ | |
9da480be MW |
44 | }; |
45 | ||
c3794524 MW |
46 | /* Structure for a listening socket. There's one of these for each address |
47 | * family we're looking after. | |
48 | */ | |
49 | struct listen { | |
50 | const struct addrops *ao; /* Address family operations */ | |
51 | sel_file f; /* Watch for incoming connections */ | |
9da480be MW |
52 | }; |
53 | ||
c3794524 | 54 | /* The main structure for a client. */ |
9da480be | 55 | struct client { |
c3794524 MW |
56 | int fd; /* The connection to the client */ |
57 | selbuf b; /* Accumulate lines of input */ | |
223e3e2b | 58 | union addr raddr; /* Remote address */ |
c3794524 | 59 | struct query q; /* The clients query and our reply */ |
4f8fdcc1 | 60 | struct sel_timer t; /* Timeout for idle or doomed conn */ |
c3794524 MW |
61 | struct listen *l; /* Back to the listener (and ops) */ |
62 | struct writebuf wb; /* Write buffer for our reply */ | |
63 | struct proxy *px; /* Proxy if conn goes via NAT */ | |
cbdfc91e | 64 | struct client *next; /* Next in a chain of clients */ |
c3794524 MW |
65 | }; |
66 | ||
67 | /* A proxy connection. */ | |
68 | struct proxy { | |
69 | int fd; /* Connection; -1 if in progress */ | |
70 | struct client *c; /* Back to the client */ | |
71 | conn cn; /* Nonblocking connection */ | |
72 | selbuf b; /* Accumulate the response line */ | |
73 | struct writebuf wb; /* Write buffer for query */ | |
74 | char nat[ADDRLEN]; /* Server address, as text */ | |
cbdfc91e | 75 | struct proxy *next; /* Next in a chain of proxies */ |
9da480be MW |
76 | }; |
77 | ||
78 | /*----- Static variables --------------------------------------------------*/ | |
79 | ||
c3794524 | 80 | static sel_state sel; /* I/O multiplexer state */ |
9da480be | 81 | |
74716d82 MW |
82 | static const char *pidfile = 0; /* Where to write daemon's pid */ |
83 | ||
84 | static const char *policyfile = POLICYFILE; /* Filename for global policy */ | |
c3794524 MW |
85 | static const struct policy default_policy = POLICY_INIT(A_NAME); |
86 | static policy_v policy = DA_INIT; /* Vector of global policy rules */ | |
87 | static fwatch polfw; /* Watch policy file for changes */ | |
9da480be | 88 | |
c3794524 MW |
89 | static unsigned char tokenbuf[4096]; /* Random-ish data for tokens */ |
90 | static size_t tokenptr = sizeof(tokenbuf); /* Current read position */ | |
9da480be | 91 | |
cbdfc91e MW |
92 | static struct client *dead_clients = 0; /* List of defunct clients */ |
93 | static struct proxy *dead_proxies = 0; /* List of defunct proxies */ | |
94 | ||
74716d82 MW |
95 | static unsigned flags = 0; /* Various interesting flags */ |
96 | #define F_SYSLOG 1u /* Use syslog for logging */ | |
97 | #define F_RUNNING 2u /* Running properly now */ | |
98 | ||
c3794524 | 99 | /*----- Ident protocol parsing --------------------------------------------*/ |
9da480be | 100 | |
c3794524 MW |
101 | /* Advance *PP over whitespace characters. */ |
102 | static void skipws(const char **pp) | |
103 | { while (isspace((unsigned char )**pp)) (*pp)++; } | |
104 | ||
105 | /* Copy a token of no more than N bytes starting at *PP into Q, advancing *PP | |
106 | * over it. | |
107 | */ | |
108 | static int idtoken(const char **pp, char *q, size_t n) | |
9da480be | 109 | { |
c3794524 | 110 | const char *p = *pp; |
9da480be | 111 | |
c3794524 MW |
112 | skipws(&p); |
113 | n--; | |
114 | for (;;) { | |
115 | if (*p == ':' || *p <= 32 || *p >= 127) break; | |
116 | if (!n) return (-1); | |
117 | *q++ = *p++; | |
118 | n--; | |
9da480be | 119 | } |
c3794524 MW |
120 | *q++ = 0; |
121 | *pp = p; | |
122 | return (0); | |
123 | } | |
124 | ||
125 | /* Read an unsigned decimal number from *PP, and store it in *II. Check that | |
126 | * it's between MIN and MAX, and advance *PP over it. Return zero for | |
127 | * success, or nonzero if something goes wrong. | |
128 | */ | |
129 | static int unum(const char **pp, unsigned *ii, unsigned min, unsigned max) | |
130 | { | |
131 | char *q; | |
132 | unsigned long i; | |
133 | int e; | |
134 | ||
135 | skipws(pp); | |
136 | if (!isdigit((unsigned char)**pp)) return (-1); | |
137 | e = errno; errno = 0; | |
138 | i = strtoul(*pp, &q, 10); | |
139 | if (errno) return (-1); | |
140 | *pp = q; | |
141 | errno = e; | |
142 | if (i < min || i > max) return (-1); | |
143 | *ii = i; | |
144 | return (0); | |
9da480be MW |
145 | } |
146 | ||
c3794524 MW |
147 | /*----- Asynchronous writing ----------------------------------------------*/ |
148 | ||
149 | /* Callback for actually writing stuff from a `writebuf'. */ | |
9da480be MW |
150 | static void write_out(int fd, unsigned mode, void *p) |
151 | { | |
152 | ssize_t n; | |
153 | struct writebuf *wb = p; | |
154 | ||
c3794524 | 155 | /* Try to write something. */ |
9da480be MW |
156 | if ((n = write(fd, wb->buf + wb->o, wb->n)) < 0) { |
157 | if (errno == EAGAIN || errno == EWOULDBLOCK) return; | |
158 | wb->n = 0; | |
159 | sel_rmfile(&wb->wr); | |
160 | wb->func(errno, wb->p); | |
161 | } | |
162 | wb->o += n; | |
163 | wb->n -= n; | |
c3794524 MW |
164 | |
165 | /* If there's nothing left then restore the buffer to its empty state. */ | |
9da480be MW |
166 | if (!wb->n) { |
167 | wb->o = 0; | |
168 | sel_rmfile(&wb->wr); | |
169 | wb->func(0, wb->p); | |
170 | } | |
171 | } | |
172 | ||
c3794524 | 173 | /* Queue N bytes starting at P to be written. */ |
9da480be MW |
174 | static int queue_write(struct writebuf *wb, const void *p, size_t n) |
175 | { | |
c3794524 | 176 | /* Maybe there's nothing to actually do. */ |
9da480be | 177 | if (!n) return (0); |
c3794524 MW |
178 | |
179 | /* Make sure it'll fit. */ | |
9da480be | 180 | if (wb->n - wb->o + n > WRBUFSZ) return (-1); |
c3794524 MW |
181 | |
182 | /* If there's anything there already, then make sure it's at the start of | |
183 | * the available space. | |
184 | */ | |
9da480be MW |
185 | if (wb->o) { |
186 | memmove(wb->buf, wb->buf + wb->o, wb->n); | |
187 | wb->o = 0; | |
188 | } | |
c3794524 MW |
189 | |
190 | /* If there's nothing currently there, then we're not requesting write | |
191 | * notifications, so set that up, and force an initial wake-up. | |
192 | */ | |
9da480be MW |
193 | if (!wb->n) { |
194 | sel_addfile(&wb->wr); | |
195 | sel_force(&wb->wr); | |
196 | } | |
c3794524 MW |
197 | |
198 | /* Copy the new material over. */ | |
199 | memcpy(wb->buf + wb->n, p, n); | |
9da480be | 200 | wb->n += n; |
c3794524 MW |
201 | |
202 | /* Done. */ | |
9da480be MW |
203 | return (0); |
204 | } | |
205 | ||
c3794524 | 206 | /* Release resources allocated to WB. */ |
9da480be MW |
207 | static void free_writebuf(struct writebuf *wb) |
208 | { if (wb->n) sel_rmfile(&wb->wr); } | |
209 | ||
c3794524 MW |
210 | /* Initialize a writebuf in *WB, writing to file descriptor FD. On |
211 | * completion, call FUNC, passing it P and an error indicator: either 0 for | |
212 | * success or an `errno' value on failure. | |
213 | */ | |
9da480be MW |
214 | static void init_writebuf(struct writebuf *wb, |
215 | int fd, void (*func)(int, void *), void *p) | |
216 | { | |
217 | sel_initfile(&sel, &wb->wr, fd, SEL_WRITE, write_out, wb); | |
218 | wb->func = func; | |
219 | wb->p = p; | |
220 | wb->n = wb->o = 0; | |
221 | } | |
222 | ||
c3794524 | 223 | /*----- General utilities -------------------------------------------------*/ |
9da480be | 224 | |
a8eb4066 MW |
225 | static void vlogmsg(const struct query *q, int prio, |
226 | const char *msg, va_list *ap) | |
9da480be | 227 | { |
c3794524 | 228 | dstr d = DSTR_INIT; |
74716d82 MW |
229 | time_t t; |
230 | struct tm *tm; | |
231 | char buf[64]; | |
c3794524 | 232 | |
c3794524 MW |
233 | if (q) { |
234 | dputsock(&d, q->ao, &q->s[L]); | |
235 | dstr_puts(&d, " <-> "); | |
236 | dputsock(&d, q->ao, &q->s[R]); | |
237 | dstr_puts(&d, ": "); | |
238 | } | |
a8eb4066 | 239 | dstr_vputf(&d, msg, ap); |
74716d82 MW |
240 | |
241 | if (!(flags & F_RUNNING)) | |
242 | moan("%s", d.buf); | |
243 | else if (flags & F_SYSLOG) | |
244 | syslog(prio, "%s", d.buf); | |
245 | else { | |
246 | t = time(0); | |
247 | tm = localtime(&t); | |
248 | strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S %z", tm); | |
249 | fprintf(stderr, "%s %s: %s\n", buf, QUIS, d.buf); | |
250 | } | |
251 | ||
c3794524 | 252 | dstr_destroy(&d); |
9da480be MW |
253 | } |
254 | ||
a8eb4066 MW |
255 | /* Format and log MSG somewhere sensible, at the syslog(3) priority PRIO. |
256 | * Prefix it with a description of the query Q, if non-null. | |
257 | */ | |
258 | void logmsg(const struct query *q, int prio, const char *msg, ...) | |
259 | { | |
260 | va_list ap; | |
261 | ||
262 | va_start(ap, msg); | |
263 | vlogmsg(q, prio, msg, &ap); | |
264 | va_end(ap); | |
265 | } | |
266 | ||
429dcd8d MW |
267 | /* Format and report MSG as a fatal error, and exit. */ |
268 | void fatal(const char *msg, ...) | |
269 | { | |
270 | va_list ap; | |
271 | ||
272 | va_start(ap, msg); | |
273 | vlogmsg(0, LOG_CRIT, msg, &ap); | |
274 | va_end(ap); | |
275 | exit(1); | |
276 | } | |
277 | ||
c3794524 MW |
278 | /* Fix up a socket FD so that it won't bite us. Returns zero on success, or |
279 | * nonzero on error. | |
280 | */ | |
95df134c MW |
281 | static int fix_up_socket(int fd, const char *what) |
282 | { | |
283 | int yes = 1; | |
284 | ||
285 | if (fdflags(fd, O_NONBLOCK, O_NONBLOCK, 0, 0)) { | |
286 | logmsg(0, LOG_ERR, "failed to set %s connection nonblocking: %s", | |
287 | what, strerror(errno)); | |
288 | return (-1); | |
289 | } | |
290 | ||
291 | if (setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &yes, sizeof(yes))) { | |
292 | logmsg(0, LOG_ERR, | |
293 | "failed to disable `out-of-band' data on %s connection: %s", | |
294 | what, strerror(errno)); | |
295 | return (-1); | |
296 | } | |
297 | ||
298 | return (0); | |
299 | } | |
300 | ||
c3794524 MW |
301 | /*----- Client output functions -------------------------------------------*/ |
302 | ||
303 | static void disconnect_client(struct client *c); | |
304 | ||
305 | /* Notification that output has been written. If successful, re-enable the | |
306 | * input buffer and prepare for another query. | |
307 | */ | |
9da480be MW |
308 | static void done_client_write(int err, void *p) |
309 | { | |
310 | struct client *c = p; | |
311 | ||
312 | if (!err) | |
313 | selbuf_enable(&c->b); | |
314 | else { | |
315 | logmsg(&c->q, LOG_ERR, "failed to send reply: %s", strerror(err)); | |
316 | disconnect_client(c); | |
317 | } | |
318 | } | |
319 | ||
c3794524 MW |
320 | /* Format the message FMT and queue it to be sent to the client. Client |
321 | * input will be disabled until the write completes. | |
322 | */ | |
bc23d2c7 MW |
323 | static void PRINTF_LIKE(2, 3) |
324 | write_to_client(struct client *c, const char *fmt, ...) | |
9da480be MW |
325 | { |
326 | va_list ap; | |
327 | char buf[WRBUFSZ]; | |
328 | ssize_t n; | |
329 | ||
330 | va_start(ap, fmt); | |
331 | n = vsnprintf(buf, sizeof(buf), fmt, ap); | |
332 | if (n < 0) { | |
333 | logmsg(&c->q, LOG_ERR, "failed to format output: %s", strerror(errno)); | |
334 | disconnect_client(c); | |
335 | return; | |
336 | } else if (n > sizeof(buf)) { | |
337 | logmsg(&c->q, LOG_ERR, "output too long for client send buffer"); | |
338 | disconnect_client(c); | |
339 | return; | |
340 | } | |
341 | ||
342 | selbuf_disable(&c->b); | |
343 | if (queue_write(&c->wb, buf, n)) { | |
344 | logmsg(&c->q, LOG_ERR, "write buffer overflow"); | |
345 | disconnect_client(c); | |
346 | } | |
347 | } | |
348 | ||
c3794524 MW |
349 | /* Format a reply to the client, with the form LPORT:RPORT:TY:TOK0[:TOK1]. |
350 | * Typically, TY will be `ERROR' or `USERID'. In the former case, TOK0 will | |
351 | * be the error token and TOK1 will be null; in the latter case, TOK0 will be | |
352 | * the operating system and TOK1 the user name. | |
353 | */ | |
c809f908 MW |
354 | static void reply(struct client *c, const char *ty, |
355 | const char *tok0, const char *tok1) | |
9da480be | 356 | { |
c809f908 MW |
357 | write_to_client(c, "%u,%u:%s:%s%s%s\r\n", |
358 | c->q.s[L].port, c->q.s[R].port, ty, | |
359 | tok0, tok1 ? ":" : "", tok1 ? tok1 : ""); | |
9da480be MW |
360 | } |
361 | ||
c3794524 | 362 | /* Mapping from error codes to their protocol tokens. */ |
bf4d9761 MW |
363 | const char *const errtok[] = { |
364 | #define DEFTOK(err, tok) tok, | |
365 | ERROR(DEFTOK) | |
366 | #undef DEFTOK | |
367 | }; | |
368 | ||
c3794524 | 369 | /* Report an error with code ERR to the client. */ |
9da480be MW |
370 | static void reply_error(struct client *c, unsigned err) |
371 | { | |
372 | assert(err < E_LIMIT); | |
c809f908 | 373 | reply(c, "ERROR", errtok[err], 0); |
9da480be MW |
374 | } |
375 | ||
c3794524 | 376 | /*----- NAT proxy functions -----------------------------------------------*/ |
9da480be | 377 | |
c3794524 MW |
378 | /* Cancel the proxy operation PX, closing the connection and releasing |
379 | * resources. This is used for both normal and unexpected closures. | |
380 | */ | |
381 | static void cancel_proxy(struct proxy *px) | |
9da480be | 382 | { |
c3794524 MW |
383 | if (px->fd == -1) |
384 | conn_kill(&px->cn); | |
385 | else { | |
386 | close(px->fd); | |
cbdfc91e | 387 | selbuf_disable(&px->b); |
9da480be | 388 | } |
c3794524 | 389 | px->c->px = 0; |
152ca59d | 390 | selbuf_enable(&px->c->b); |
cbdfc91e MW |
391 | px->next = dead_proxies; |
392 | dead_proxies = px; | |
393 | } | |
394 | ||
395 | /* Delayed destruction of unsafe parts of proxies. */ | |
396 | static void reap_dead_proxies(void) | |
397 | { | |
398 | struct proxy *px, *pp; | |
399 | ||
400 | for (px = dead_proxies; px; px = pp) { | |
401 | pp = px->next; | |
402 | if (px->fd != -1) { | |
403 | selbuf_destroy(&px->b); | |
404 | free_writebuf(&px->wb); | |
405 | } | |
406 | xfree(px); | |
407 | } | |
408 | dead_proxies = 0; | |
9da480be MW |
409 | } |
410 | ||
c3794524 MW |
411 | /* Notification that a line (presumably a reply) has been received from the |
412 | * server. We should check it, log it, and propagate the answer back. | |
413 | * Whatever happens, this proxy operation is now complete. | |
414 | */ | |
9da480be MW |
415 | static void proxy_line(char *line, size_t sz, void *p) |
416 | { | |
417 | struct proxy *px = p; | |
418 | char buf[1024]; | |
419 | const char *q = line; | |
420 | unsigned lp, rp; | |
421 | ||
c3794524 | 422 | /* Trim trailing space. */ |
9da480be | 423 | while (sz && isspace((unsigned char)line[sz - 1])) sz--; |
9da480be | 424 | |
c3794524 | 425 | /* Parse the port numbers. These should match the request. */ |
9da480be MW |
426 | if (unum(&q, &lp, 1, 65535)) goto syntax; |
427 | skipws(&q); if (*q != ',') goto syntax; q++; | |
428 | if (unum(&q, &rp, 1, 65535)) goto syntax; | |
429 | skipws(&q); if (*q != ':') goto syntax; q++; | |
430 | if (lp != px->c->q.u.nat.port || rp != px->c->q.s[R].port) goto syntax; | |
c3794524 MW |
431 | |
432 | /* Find out what kind of reply this is. */ | |
9da480be MW |
433 | if (idtoken(&q, buf, sizeof(buf))) goto syntax; |
434 | skipws(&q); if (*q != ':') goto syntax; q++; | |
c3794524 | 435 | |
9da480be | 436 | if (strcmp(buf, "ERROR") == 0) { |
c3794524 MW |
437 | |
438 | /* Report the error without interpreting it. It might be meaningful to | |
439 | * the client. | |
440 | */ | |
9da480be MW |
441 | skipws(&q); |
442 | logmsg(&px->c->q, LOG_ERR, "proxy error from %s: %s", px->nat, q); | |
c809f908 | 443 | reply(px->c, "ERROR", q, 0); |
c3794524 | 444 | |
9da480be | 445 | } else if (strcmp(buf, "USERID") == 0) { |
c3794524 MW |
446 | |
447 | /* Parse out the operating system and user name, and pass them on. */ | |
9da480be MW |
448 | if (idtoken(&q, buf, sizeof(buf))) goto syntax; |
449 | skipws(&q); if (*q != ':') goto syntax; q++; | |
450 | skipws(&q); | |
451 | logmsg(&px->c->q, LOG_ERR, "user `%s'; proxy = %s, os = %s", | |
452 | q, px->nat, buf); | |
c809f908 | 453 | reply(px->c, "USERID", buf, q); |
c3794524 | 454 | |
9da480be MW |
455 | } else |
456 | goto syntax; | |
457 | goto done; | |
458 | ||
459 | syntax: | |
c3794524 | 460 | /* We didn't understand the message from the client. */ |
9da480be MW |
461 | logmsg(&px->c->q, LOG_ERR, "failed to parse response from %s", px->nat); |
462 | reply_error(px->c, E_UNKNOWN); | |
463 | done: | |
c3794524 | 464 | /* All finished, no matter what. */ |
9da480be MW |
465 | cancel_proxy(px); |
466 | } | |
467 | ||
c3794524 MW |
468 | /* Notification that we have written the query to the server. Await a |
469 | * response if successful. | |
470 | */ | |
9da480be MW |
471 | static void done_proxy_write(int err, void *p) |
472 | { | |
473 | struct proxy *px = p; | |
474 | ||
475 | if (err) { | |
476 | logmsg(&px->c->q, LOG_ERR, "failed to proxy query to %s: %s", | |
477 | px->nat, strerror(errno)); | |
478 | reply_error(px->c, E_UNKNOWN); | |
479 | cancel_proxy(px); | |
480 | return; | |
481 | } | |
482 | selbuf_enable(&px->b); | |
483 | } | |
484 | ||
c3794524 MW |
485 | /* Notification that the connection to the server is either established or |
486 | * failed. In the former case, queue the right query. | |
487 | */ | |
9da480be MW |
488 | static void proxy_connected(int fd, void *p) |
489 | { | |
490 | struct proxy *px = p; | |
491 | char buf[16]; | |
492 | int n; | |
493 | ||
c3794524 | 494 | /* If the connection failed then report the problem and give up. */ |
9da480be MW |
495 | if (fd < 0) { |
496 | logmsg(&px->c->q, LOG_ERR, | |
497 | "failed to make %s proxy connection to %s: %s", | |
bf4d9761 | 498 | px->c->l->ao->name, px->nat, strerror(errno)); |
9da480be MW |
499 | reply_error(px->c, E_UNKNOWN); |
500 | cancel_proxy(px); | |
501 | return; | |
502 | } | |
503 | ||
c3794524 | 504 | /* We're now ready to go, so set things up. */ |
9da480be MW |
505 | px->fd = fd; |
506 | selbuf_init(&px->b, &sel, fd, proxy_line, px); | |
507 | selbuf_setsize(&px->b, 1024); | |
508 | selbuf_disable(&px->b); | |
509 | init_writebuf(&px->wb, fd, done_proxy_write, px); | |
510 | ||
c3794524 MW |
511 | /* Write the query. This buffer is large enough because we've already |
512 | * range-checked the remote the port number and the local one came from the | |
513 | * kernel, which we trust not to do anything stupid. | |
514 | */ | |
9da480be MW |
515 | n = sprintf(buf, "%u,%u\r\n", px->c->q.u.nat.port, px->c->q.s[R].port); |
516 | queue_write(&px->wb, buf, n); | |
517 | } | |
518 | ||
c3794524 MW |
519 | /* Proxy the query through to a client machine for which we're providing NAT |
520 | * disservice. | |
521 | */ | |
9da480be MW |
522 | static void proxy_query(struct client *c) |
523 | { | |
524 | struct socket s; | |
525 | struct sockaddr_storage ss; | |
526 | size_t ssz; | |
527 | struct proxy *px; | |
9da480be MW |
528 | int fd; |
529 | ||
c3794524 | 530 | /* Allocate the context structure for the NAT. */ |
9da480be | 531 | px = xmalloc(sizeof(*px)); |
c3794524 MW |
532 | |
533 | /* We'll use the client host's address in lots of log messages, so we may | |
534 | * as well format it once and use it over and over. | |
535 | */ | |
bf4d9761 | 536 | inet_ntop(c->q.ao->af, &c->q.u.nat.addr, px->nat, sizeof(px->nat)); |
9da480be | 537 | |
c3794524 | 538 | /* Create the socket for the connection. */ |
bf4d9761 | 539 | if ((fd = socket(c->q.ao->af, SOCK_STREAM, 0)) < 0) { |
9da480be | 540 | logmsg(&c->q, LOG_ERR, "failed to make %s socket for proxy: %s", |
bf4d9761 | 541 | c->l->ao->name, strerror(errno)); |
9da480be MW |
542 | goto err_0; |
543 | } | |
95df134c | 544 | if (fix_up_socket(fd, "proxy")) goto err_1; |
9da480be | 545 | |
c3794524 MW |
546 | /* Set up the connection to the client host. The connection interface is a |
547 | * bit broken: if the connection completes immediately, then the callback | |
548 | * function is called synchronously, and that might decide to shut | |
549 | * everything down. So we must have fully initialized our context before | |
550 | * calling `conn_init', and mustn't touch it again afterwards -- since the | |
551 | * block may have been freed. | |
552 | */ | |
9da480be MW |
553 | s = c->q.u.nat; |
554 | s.port = 113; | |
bf4d9761 | 555 | c->l->ao->socket_to_sockaddr(&s, &ss, &ssz); |
9da480be | 556 | selbuf_disable(&c->b); |
79805e61 MW |
557 | c->px = px; px->c = c; |
558 | px->fd = -1; | |
9da480be MW |
559 | if (conn_init(&px->cn, &sel, fd, (struct sockaddr *)&ss, ssz, |
560 | proxy_connected, px)) { | |
561 | logmsg(&c->q, LOG_ERR, "failed to make %s proxy connection to %s: %s", | |
bf4d9761 | 562 | c->l->ao->name, px->nat, strerror(errno)); |
9da480be MW |
563 | goto err_2; |
564 | } | |
565 | ||
c3794524 | 566 | /* All ready to go. */ |
9da480be MW |
567 | return; |
568 | ||
c3794524 | 569 | /* Tidy up after various kinds of failures. */ |
9da480be MW |
570 | err_2: |
571 | selbuf_enable(&c->b); | |
572 | err_1: | |
573 | close(px->fd); | |
574 | err_0: | |
575 | xfree(px); | |
576 | reply_error(c, E_UNKNOWN); | |
577 | } | |
578 | ||
c3794524 MW |
579 | /*----- Client connection functions ---------------------------------------*/ |
580 | ||
581 | /* Disconnect a client, freeing up any associated resources. */ | |
582 | static void disconnect_client(struct client *c) | |
583 | { | |
cbdfc91e | 584 | selbuf_disable(&c->b); |
c3794524 | 585 | close(c->fd); |
4f8fdcc1 | 586 | sel_rmtimer(&c->t); |
c3794524 MW |
587 | free_writebuf(&c->wb); |
588 | if (c->px) cancel_proxy(c->px); | |
cbdfc91e MW |
589 | c->next = dead_clients; |
590 | dead_clients = c; | |
591 | } | |
592 | ||
593 | /* Throw away dead clients now that we've reached a safe point in the | |
594 | * program. | |
595 | */ | |
596 | static void reap_dead_clients(void) | |
597 | { | |
598 | struct client *c, *cc; | |
599 | for (c = dead_clients; c; c = cc) { | |
600 | cc = c->next; | |
601 | selbuf_destroy(&c->b); | |
602 | xfree(c); | |
603 | } | |
604 | dead_clients = 0; | |
c3794524 | 605 | } |
9da480be | 606 | |
4f8fdcc1 MW |
607 | /* Time out a client because it's been idle for too long. */ |
608 | static void timeout_client(struct timeval *tv, void *p) | |
609 | { | |
610 | struct client *c = p; | |
611 | logmsg(&c->q, LOG_NOTICE, "timing out idle or stuck client"); | |
612 | sel_addtimer(&sel, &c->t, tv, timeout_client, 0); | |
613 | disconnect_client(c); | |
614 | } | |
615 | ||
616 | /* Reset the client idle timer, as a result of activity. Set EXISTP if | |
617 | * there is an existing timer which needs to be removed. | |
618 | */ | |
619 | static void reset_client_timer(struct client *c, int existp) | |
620 | { | |
621 | struct timeval tv; | |
622 | ||
623 | gettimeofday(&tv, 0); | |
624 | tv.tv_sec += 30; | |
625 | if (existp) sel_rmtimer(&c->t); | |
626 | sel_addtimer(&sel, &c->t, &tv, timeout_client, c); | |
627 | } | |
628 | ||
c3794524 MW |
629 | /* Write a pseudorandom token into the buffer at P, which must have space for |
630 | * at least TOKENSZ bytes. | |
631 | */ | |
632 | #define TOKENRANDSZ 8 | |
633 | #define TOKENSZ ((4*TOKENRANDSZ + 5)/3) | |
9da480be MW |
634 | static void user_token(char *p) |
635 | { | |
9da480be MW |
636 | unsigned a = 0; |
637 | unsigned b = 0; | |
638 | int i; | |
c3794524 MW |
639 | static const char tokmap[64] = |
640 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-"; | |
9da480be | 641 | |
c3794524 MW |
642 | /* If there's not enough pseudorandom stuff lying around, then read more |
643 | * from the kernel. | |
644 | */ | |
645 | if (tokenptr + TOKENRANDSZ >= sizeof(tokenbuf)) { | |
56e93c83 | 646 | fill_random(tokenbuf, sizeof(tokenbuf)); |
9da480be MW |
647 | tokenptr = 0; |
648 | } | |
649 | ||
c3794524 MW |
650 | /* Now encode the bytes using a slightly tweaked base-64 encoding. Read |
651 | * bytes into the accumulator and write out characters while there's | |
652 | * enough material. | |
653 | */ | |
654 | for (i = 0; i < TOKENRANDSZ; i++) { | |
9da480be MW |
655 | a = (a << 8) | tokenbuf[tokenptr++]; b += 8; |
656 | while (b >= 6) { | |
657 | b -= 6; | |
658 | *p++ = tokmap[(a >> b) & 0x3f]; | |
659 | } | |
660 | } | |
c3794524 MW |
661 | |
662 | /* If there's anything left in the accumulator then flush it out. */ | |
9da480be MW |
663 | if (b) |
664 | *p++ = tokmap[(a << (6 - b)) & 0x3f]; | |
c3794524 MW |
665 | |
666 | /* Null-terminate the token. */ | |
9da480be MW |
667 | *p++ = 0; |
668 | } | |
669 | ||
c3794524 MW |
670 | /* Notification that a line has been received from the client. Parse it, |
671 | * find out about the connection it's referring to, apply the relevant | |
672 | * policy rules, and produce a response. This is where almost everything | |
673 | * interesting happens. | |
674 | */ | |
9da480be MW |
675 | static void client_line(char *line, size_t len, void *p) |
676 | { | |
677 | struct client *c = p; | |
678 | const char *q; | |
679 | struct passwd *pw = 0; | |
680 | const struct policy *pol; | |
681 | dstr d = DSTR_INIT; | |
682 | struct policy upol = POLICY_INIT(A_LIMIT); | |
683 | struct policy_file pf; | |
684 | char buf[16]; | |
b9eb1a36 | 685 | int i, t; |
9da480be | 686 | |
c3794524 | 687 | /* If the connection has closed, then tidy stuff away. */ |
a4f539e7 | 688 | c->q.s[R].addr = c->raddr; |
9da480be MW |
689 | c->q.s[L].port = c->q.s[R].port = 0; |
690 | if (!line) { | |
691 | disconnect_client(c); | |
692 | return; | |
693 | } | |
694 | ||
4f8fdcc1 MW |
695 | /* Client activity, so update the timer. */ |
696 | reset_client_timer(c, 1); | |
697 | ||
c3794524 MW |
698 | /* See if the policy file has changed since we last looked. If so, try to |
699 | * read the new version. | |
700 | */ | |
74716d82 MW |
701 | if (fwatch_update(&polfw, policyfile)) { |
702 | logmsg(0, LOG_INFO, "reload master policy file `%s'", policyfile); | |
703 | load_policy_file(policyfile, &policy); | |
9da480be MW |
704 | } |
705 | ||
c3794524 | 706 | /* Read the local and remote port numbers into the query structure. */ |
9da480be MW |
707 | q = line; |
708 | if (unum(&q, &c->q.s[L].port, 1, 65535)) goto bad; | |
709 | skipws(&q); if (*q != ',') goto bad; q++; | |
710 | if (unum(&q, &c->q.s[R].port, 1, 65535)) goto bad; | |
711 | skipws(&q); if (*q) goto bad; | |
712 | ||
c3794524 | 713 | /* Identify the connection. Act on the result. */ |
9da480be MW |
714 | identify(&c->q); |
715 | switch (c->q.resp) { | |
c3794524 | 716 | |
9da480be | 717 | case R_UID: |
c3794524 MW |
718 | /* We found a user. Track down the user's password entry, because |
719 | * we'll want that later. Most of the processing for this case is | |
720 | * below. | |
721 | */ | |
9da480be MW |
722 | if ((pw = getpwuid(c->q.u.uid)) == 0) { |
723 | logmsg(&c->q, LOG_ERR, "no passwd entry for user %d", c->q.u.uid); | |
724 | reply_error(c, E_NOUSER); | |
725 | return; | |
726 | } | |
727 | break; | |
c3794524 | 728 | |
9da480be | 729 | case R_NAT: |
c3794524 MW |
730 | /* We've acted as a NAT for this connection. Proxy the query through |
731 | * to the actal client host. | |
732 | */ | |
9da480be MW |
733 | proxy_query(c); |
734 | return; | |
c3794524 | 735 | |
9da480be | 736 | case R_ERROR: |
c3794524 MW |
737 | /* We failed to identify the connection for some reason. We should |
738 | * already have logged an error, so there's not much to do here. | |
739 | */ | |
9da480be MW |
740 | reply_error(c, c->q.u.error); |
741 | return; | |
c3794524 | 742 | |
9da480be | 743 | default: |
c3794524 | 744 | /* Something happened that we don't understand. */ |
9da480be MW |
745 | abort(); |
746 | } | |
747 | ||
c3794524 | 748 | /* Search the table of policy rules to find a match. */ |
9da480be MW |
749 | for (i = 0; i < DA_LEN(&policy); i++) { |
750 | pol = &DA(&policy)[i]; | |
751 | if (!match_policy(pol, &c->q)) continue; | |
c3794524 MW |
752 | |
753 | /* If this is something simple, then apply the resulting policy rule. */ | |
754 | if (pol->act.act != A_USER) goto match; | |
755 | ||
756 | /* The global policy has decided to let the user have a say, so we must | |
757 | * parse the user file. | |
758 | */ | |
9da480be MW |
759 | DRESET(&d); |
760 | dstr_putf(&d, "%s/.yaid.policy", pw->pw_dir); | |
17272ab8 | 761 | if (open_policy_file(&pf, d.buf, "user policy file", &c->q, OPF_NOENTOK)) |
9da480be | 762 | continue; |
b9eb1a36 | 763 | while ((t = read_policy_file(&pf)) < T_ERROR) { |
c3794524 | 764 | |
b9eb1a36 MW |
765 | /* Give up after 100 lines or if there's an error. If the user's |
766 | * policy is that complicated, something's gone very wrong. Or there's | |
767 | * too much commentary or something. | |
c3794524 | 768 | */ |
9da480be MW |
769 | if (pf.lno > 100) { |
770 | logmsg(&c->q, LOG_ERR, "%s:%d: user policy file too long", | |
771 | pf.name, pf.lno); | |
772 | break; | |
773 | } | |
c3794524 | 774 | |
b9eb1a36 MW |
775 | /* If this was a blank line, just go around again. */ |
776 | if (t != T_OK) continue; | |
777 | ||
c3794524 | 778 | /* If this isn't a match, go around for the next rule. */ |
9da480be | 779 | if (!match_policy(&pf.p, &c->q)) continue; |
c3794524 MW |
780 | |
781 | /* Check that the user is allowed to request this action. If not, see | |
782 | * if there's a more acceptable action later on. | |
783 | */ | |
9da480be MW |
784 | if (!(pol->act.u.user & (1 << pf.p.act.act))) { |
785 | logmsg(&c->q, LOG_ERR, | |
786 | "%s:%d: user action forbidden by global policy", | |
787 | pf.name, pf.lno); | |
788 | continue; | |
789 | } | |
c3794524 MW |
790 | |
791 | /* We've found a match, so grab it, close the file, and say we're | |
792 | * done. | |
793 | */ | |
9da480be MW |
794 | upol = pf.p; pol = &upol; |
795 | init_policy(&pf.p); | |
796 | close_policy_file(&pf); | |
c3794524 | 797 | DDESTROY(&d); |
9da480be MW |
798 | goto match; |
799 | } | |
800 | close_policy_file(&pf); | |
c3794524 | 801 | DDESTROY(&d); |
9da480be | 802 | } |
c3794524 MW |
803 | |
804 | /* No match: apply the built-in default policy. */ | |
9da480be MW |
805 | pol = &default_policy; |
806 | ||
807 | match: | |
9da480be | 808 | switch (pol->act.act) { |
c3794524 | 809 | |
9da480be | 810 | case A_NAME: |
c3794524 | 811 | /* Report the actual user's name. */ |
9da480be | 812 | logmsg(&c->q, LOG_INFO, "user `%s' (%d)", pw->pw_name, c->q.u.uid); |
c809f908 | 813 | reply(c, "USERID", "UNIX", pw->pw_name); |
9da480be | 814 | break; |
c3794524 | 815 | |
9da480be | 816 | case A_TOKEN: |
c3794524 | 817 | /* Report an arbitrary token which we can look up in our log file. */ |
9da480be MW |
818 | user_token(buf); |
819 | logmsg(&c->q, LOG_INFO, "user `%s' (%d); token = %s", | |
820 | pw->pw_name, c->q.u.uid, buf); | |
c809f908 | 821 | reply(c, "USERID", "OTHER", buf); |
9da480be | 822 | break; |
c3794524 | 823 | |
9da480be | 824 | case A_DENY: |
c3794524 | 825 | /* Deny that there's anyone there at all. */ |
9da480be MW |
826 | logmsg(&c->q, LOG_INFO, "user `%s' (%d); denying", |
827 | pw->pw_name, c->q.u.uid); | |
828 | break; | |
c3794524 | 829 | |
9da480be | 830 | case A_HIDE: |
c3794524 | 831 | /* Report the user as being hidden. */ |
9da480be MW |
832 | logmsg(&c->q, LOG_INFO, "user `%s' (%d); hiding", |
833 | pw->pw_name, c->q.u.uid); | |
834 | reply_error(c, E_HIDDEN); | |
835 | break; | |
c3794524 | 836 | |
9da480be | 837 | case A_LIE: |
c3794524 | 838 | /* Tell an egregious lie about who the user is. */ |
9da480be MW |
839 | logmsg(&c->q, LOG_INFO, "user `%s' (%d); lie = `%s'", |
840 | pw->pw_name, c->q.u.uid, pol->act.u.lie); | |
c809f908 | 841 | reply(c, "USERID", "UNIX", pol->act.u.lie); |
9da480be | 842 | break; |
c3794524 | 843 | |
9da480be | 844 | default: |
c3794524 | 845 | /* Something has gone very wrong. */ |
9da480be MW |
846 | abort(); |
847 | } | |
848 | ||
c3794524 | 849 | /* All done. */ |
9da480be MW |
850 | free_policy(&upol); |
851 | return; | |
852 | ||
853 | bad: | |
854 | logmsg(&c->q, LOG_ERR, "failed to parse query from client"); | |
855 | disconnect_client(c); | |
856 | } | |
857 | ||
c3794524 | 858 | /* Notification that a new client has connected. Prepare to read a query. */ |
9da480be MW |
859 | static void accept_client(int fd, unsigned mode, void *p) |
860 | { | |
861 | struct listen *l = p; | |
862 | struct client *c; | |
863 | struct sockaddr_storage ssr, ssl; | |
c372d20e | 864 | socklen_t ssz = sizeof(ssr); |
9da480be MW |
865 | int sk; |
866 | ||
c3794524 | 867 | /* Accept the new connection. */ |
9da480be MW |
868 | if ((sk = accept(fd, (struct sockaddr *)&ssr, &ssz)) < 0) { |
869 | if (errno != EAGAIN && errno == EWOULDBLOCK) { | |
870 | logmsg(0, LOG_ERR, "failed to accept incoming %s connection: %s", | |
bf4d9761 | 871 | l->ao->name, strerror(errno)); |
9da480be MW |
872 | } |
873 | return; | |
874 | } | |
95df134c | 875 | if (fix_up_socket(sk, "incoming client")) { close(sk); return; } |
9da480be | 876 | |
c3794524 | 877 | /* Build a client block and fill it in. */ |
9da480be MW |
878 | c = xmalloc(sizeof(*c)); |
879 | c->l = l; | |
bf4d9761 | 880 | c->q.ao = l->ao; |
c3794524 MW |
881 | |
882 | /* Collect the local and remote addresses. */ | |
223e3e2b | 883 | l->ao->sockaddr_to_addr(&ssr, &c->raddr); |
9da480be MW |
884 | ssz = sizeof(ssl); |
885 | if (getsockname(sk, (struct sockaddr *)&ssl, &ssz)) { | |
886 | logmsg(0, LOG_ERR, | |
887 | "failed to read local address for incoming %s connection: %s", | |
bf4d9761 | 888 | l->ao->name, strerror(errno)); |
9da480be MW |
889 | close(sk); |
890 | xfree(c); | |
891 | return; | |
892 | } | |
bf4d9761 | 893 | l->ao->sockaddr_to_addr(&ssl, &c->q.s[L].addr); |
9da480be MW |
894 | c->q.s[L].port = c->q.s[R].port = 0; |
895 | ||
c3794524 | 896 | /* Set stuff up for reading the query and sending responses. */ |
9da480be MW |
897 | selbuf_init(&c->b, &sel, sk, client_line, c); |
898 | selbuf_setsize(&c->b, 1024); | |
4f8fdcc1 | 899 | reset_client_timer(c, 0); |
9da480be MW |
900 | c->fd = sk; |
901 | c->px = 0; | |
902 | init_writebuf(&c->wb, sk, done_client_write, c); | |
903 | } | |
904 | ||
c3794524 MW |
905 | /*----- Main code ---------------------------------------------------------*/ |
906 | ||
907 | /* Set up a listening socket for the address family described by AO, | |
908 | * listening on PORT. | |
909 | */ | |
bf4d9761 | 910 | static int make_listening_socket(const struct addrops *ao, int port) |
9da480be MW |
911 | { |
912 | int fd; | |
bf4d9761 MW |
913 | int yes = 1; |
914 | struct socket s; | |
9da480be MW |
915 | struct sockaddr_storage ss; |
916 | struct listen *l; | |
917 | size_t ssz; | |
918 | ||
c3794524 | 919 | /* Make the socket. */ |
bf4d9761 | 920 | if ((fd = socket(ao->af, SOCK_STREAM, 0)) < 0) { |
a20696ca | 921 | if (errno == EAFNOSUPPORT) return (-1); |
9da480be | 922 | die(1, "failed to create %s listening socket: %s", |
bf4d9761 | 923 | ao->name, strerror(errno)); |
9da480be | 924 | } |
c3794524 MW |
925 | |
926 | /* Build the appropriate local address. */ | |
bf4d9761 MW |
927 | s.addr = *ao->any; |
928 | s.port = port; | |
929 | ao->socket_to_sockaddr(&s, &ss, &ssz); | |
c3794524 MW |
930 | |
931 | /* Perform any initialization specific to the address type. */ | |
bf4d9761 MW |
932 | if (ao->init_listen_socket(fd)) { |
933 | die(1, "failed to initialize %s listening socket: %s", | |
934 | ao->name, strerror(errno)); | |
935 | } | |
c3794524 MW |
936 | |
937 | /* Bind to the address. */ | |
938 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)); | |
bf4d9761 MW |
939 | if (bind(fd, (struct sockaddr *)&ss, ssz)) { |
940 | die(1, "failed to bind %s listening socket: %s", | |
941 | ao->name, strerror(errno)); | |
9da480be | 942 | } |
c3794524 MW |
943 | |
944 | /* Avoid unpleasant race conditions. */ | |
bf4d9761 | 945 | if (fdflags(fd, O_NONBLOCK, O_NONBLOCK, 0, 0)) { |
9da480be | 946 | die(1, "failed to set %s listening socket nonblocking: %s", |
bf4d9761 | 947 | ao->name, strerror(errno)); |
9da480be | 948 | } |
c3794524 MW |
949 | |
950 | /* Prepare to listen. */ | |
9da480be | 951 | if (listen(fd, 5)) |
bf4d9761 | 952 | die(1, "failed to listen for %s: %s", ao->name, strerror(errno)); |
9da480be | 953 | |
c3794524 | 954 | /* Make a record of all of this. */ |
9da480be | 955 | l = xmalloc(sizeof(*l)); |
bf4d9761 | 956 | l->ao = ao; |
9da480be MW |
957 | sel_initfile(&sel, &l->f, fd, SEL_READ, accept_client, l); |
958 | sel_addfile(&l->f); | |
959 | ||
c3794524 | 960 | /* Done. */ |
a20696ca | 961 | return (0); |
9da480be MW |
962 | } |
963 | ||
74716d82 | 964 | /* Quit because of a fatal signal. */ |
bc23d2c7 | 965 | static void NORETURN quit(int sig, void *p) |
74716d82 MW |
966 | { |
967 | const char *signame = p; | |
968 | ||
969 | logmsg(0, LOG_NOTICE, "shutting down on %s", signame); | |
970 | if (pidfile) unlink(pidfile); | |
971 | exit(0); | |
972 | } | |
973 | ||
974 | /* Answer whether the string pointed to by P consists entirely of digits. */ | |
975 | static int numericp(const char *p) | |
976 | { | |
977 | while (*p) | |
978 | if (!isdigit((unsigned char)*p++)) return (0); | |
979 | return (1); | |
980 | } | |
981 | ||
982 | static void usage(FILE *fp) | |
983 | { | |
984 | pquis(fp, "Usage: $ [-Dl] [-G GROUP] [-U USER] [-P FILE] " | |
985 | "[-c FILE] [-p PORT]\n"); | |
986 | } | |
987 | ||
988 | static void version(FILE *fp) | |
989 | { pquis(fp, "$, version " VERSION "\n"); } | |
990 | ||
991 | static void help(FILE *fp) | |
992 | { | |
993 | version(fp); fputc('\n', fp); | |
994 | usage(fp); | |
995 | fputs("\n\ | |
996 | Yet Another Ident Daemon. Really, the world doesn't need such a thing.\n\ | |
997 | It's just a shame none of the others do the right things.\n\ | |
998 | \n\ | |
999 | Options:\n\ | |
1000 | \n\ | |
1001 | -h, --help Show this help message.\n\ | |
1002 | -v, --version Show the version number.\n\ | |
1003 | -u, --usage Show a very short usage summary.\n\ | |
1004 | \n\ | |
1005 | -D, --daemon Become a daemon, running in the background.\n\ | |
1006 | -G, --group=GROUP Set group after initialization.\n\ | |
1007 | -P, --pidfile=FILE Write process id to FILE.\n\ | |
1008 | -U, --user=USER Set user after initialization.\n\ | |
1009 | -c, --config=FILE Read global policy from FILE.\n\ | |
1010 | -l, --syslog Write log messages using syslog(3).\n\ | |
1011 | -p, --port=PORT Listen for connections on this port.\n", | |
1012 | fp); | |
1013 | } | |
1014 | ||
9da480be MW |
1015 | int main(int argc, char *argv[]) |
1016 | { | |
1017 | int port = 113; | |
74716d82 MW |
1018 | uid_t u = -1; |
1019 | gid_t g = -1; | |
1020 | struct passwd *pw = 0; | |
1021 | struct group *gr; | |
1022 | struct servent *s; | |
1023 | sig sigint, sigterm; | |
1024 | FILE *fp = 0; | |
1025 | int i; | |
1026 | unsigned f = 0; | |
1027 | #define f_bogus 1u | |
1028 | #define f_daemon 2u | |
bf4d9761 MW |
1029 | const struct addrops *ao; |
1030 | int any = 0; | |
9da480be MW |
1031 | |
1032 | ego(argv[0]); | |
1033 | ||
74716d82 MW |
1034 | /* Parse command-line options. */ |
1035 | for (;;) { | |
1036 | const struct option opts[] = { | |
1037 | { "help", 0, 0, 'h' }, | |
1038 | { "version", 0, 0, 'v' }, | |
1039 | { "usage", 0, 0, 'u' }, | |
1040 | { "daemon", 0, 0, 'D' }, | |
1041 | { "group", OPTF_ARGREQ, 0, 'G' }, | |
1042 | { "pidfile", OPTF_ARGREQ, 0, 'P' }, | |
1043 | { "user", OPTF_ARGREQ, 0, 'U' }, | |
1044 | { "config", OPTF_ARGREQ, 0, 'c' }, | |
1045 | { "syslog", 0, 0, 'l' }, | |
1046 | { "port", OPTF_ARGREQ, 0, 'p' }, | |
1047 | { 0, 0, 0, 0 } | |
1048 | }; | |
1049 | ||
1050 | if ((i = mdwopt(argc, argv, "hvuDG:P:U:c:lp:", opts, 0, 0, 0)) < 0) | |
1051 | break; | |
1052 | switch (i) { | |
1053 | case 'h': help(stdout); exit(0); | |
1054 | case 'v': version(stdout); exit(0); | |
1055 | case 'u': usage(stdout); exit(0); | |
1056 | case 'D': f |= f_daemon; break; | |
1057 | case 'P': pidfile = optarg; break; | |
1058 | case 'c': policyfile = optarg; break; | |
1059 | case 'l': flags |= F_SYSLOG; break; | |
1060 | case 'G': | |
1061 | if (numericp(optarg)) | |
1062 | g = atoi(optarg); | |
1063 | else if ((gr = getgrnam(optarg)) == 0) | |
1064 | die(1, "unknown group `%s'", optarg); | |
1065 | else | |
1066 | g = gr->gr_gid; | |
1067 | break; | |
1068 | case 'U': | |
1069 | if (numericp(optarg)) | |
1070 | u = atoi(optarg); | |
1071 | else if ((pw = getpwnam(optarg)) == 0) | |
1072 | die(1, "unknown user `%s'", optarg); | |
1073 | else | |
1074 | u = pw->pw_uid; | |
1075 | break; | |
1076 | case 'p': | |
1077 | if (numericp(optarg)) | |
1078 | port = atoi(optarg); | |
1079 | else if ((s = getservbyname(optarg, "tcp")) == 0) | |
1080 | die(1, "unknown service name `%s'", optarg); | |
1081 | else | |
1082 | port = ntohs(s->s_port); | |
1083 | break; | |
1084 | default: f |= f_bogus; break; | |
1085 | } | |
1086 | } | |
1087 | if (optind < argc) f |= f_bogus; | |
1088 | if (f & f_bogus) { usage(stderr); exit(1); } | |
1089 | ||
1090 | /* If a user has been requested, but no group, then find the user's primary | |
1091 | * group. If the user was given by name, then we already have a password | |
1092 | * entry and should use that, in case two differently-named users have the | |
1093 | * same uid but distinct gids. | |
1094 | */ | |
1095 | if (u != -1 && g == -1) { | |
1096 | if (!pw && (pw = getpwuid(u)) == 0) { | |
1097 | die(1, "failed to find password entry for user %d: " | |
1098 | "request group explicitly", u); | |
1099 | } | |
1100 | g = pw->pw_gid; | |
1101 | } | |
1102 | ||
1103 | /* Initialize system-specific machinery. */ | |
b093b41d | 1104 | init_sys(); |
74716d82 MW |
1105 | |
1106 | /* Load the global policy rules. */ | |
1107 | fwatch_init(&polfw, policyfile); | |
1108 | if (load_policy_file(policyfile, &policy)) | |
9da480be | 1109 | exit(1); |
9da480be | 1110 | |
74716d82 | 1111 | /* Set up the I/O event system. */ |
9da480be | 1112 | sel_init(&sel); |
74716d82 MW |
1113 | |
1114 | /* Watch for some interesting signals. */ | |
1115 | sig_init(&sel); | |
1116 | sig_add(&sigint, SIGINT, quit, "SIGINT"); | |
1117 | sig_add(&sigterm, SIGTERM, quit, "SIGTERM"); | |
1118 | ||
1119 | /* Listen for incoming connections. */ | |
bf4d9761 MW |
1120 | for (ao = addroptab; ao->name; ao++) |
1121 | if (!make_listening_socket(ao, port)) any = 1; | |
74716d82 MW |
1122 | if (!any) die(1, "no IP protocols supported"); |
1123 | ||
1124 | /* Open the pidfile now, in case it's somewhere we can't write. */ | |
1125 | if (pidfile && (fp = fopen(pidfile, "w")) == 0) { | |
1126 | die(1, "failed to open pidfile `%s' for writing: %s", | |
1127 | pidfile, strerror(errno)); | |
1128 | } | |
1129 | ||
1130 | /* If we're meant to use syslog, then open the log. */ | |
1131 | if (flags & F_SYSLOG) | |
1132 | openlog(QUIS, 0, LOG_DAEMON); | |
1133 | ||
1134 | /* Drop privileges. */ | |
1135 | if ((g != -1 && (setegid(g) || setgid(g) || | |
1136 | (getuid() == 0 && setgroups(1, &g)))) || | |
1137 | (u != -1 && setuid(u))) | |
1138 | die(1, "failed to drop privileges: %s", strerror(errno)); | |
9da480be | 1139 | |
74716d82 MW |
1140 | /* Become a background process, if requested. */ |
1141 | if ((f & f_daemon) && daemonize()) | |
1142 | die(1, "failed to become daemon: %s", strerror(errno)); | |
1143 | ||
1144 | /* Write the process id to the pidfile. */ | |
1145 | if (fp) { | |
1146 | fprintf(fp, "%d\n", getpid()); | |
1147 | fclose(fp); | |
1148 | } | |
1149 | ||
1150 | /* And now we're going. */ | |
1151 | flags |= F_RUNNING; | |
1152 | ||
1153 | /* Read events and process them. */ | |
1154 | for (;;) { | |
1155 | if (sel_select(&sel) && errno != EINTR) | |
1156 | die(1, "select failed: %s", strerror(errno)); | |
cbdfc91e MW |
1157 | reap_dead_proxies(); |
1158 | reap_dead_clients(); | |
74716d82 | 1159 | } |
9da480be | 1160 | |
74716d82 | 1161 | /* This just keeps the compiler happy. */ |
9da480be MW |
1162 | return (0); |
1163 | } | |
1164 | ||
1165 | /*----- That's all, folks -------------------------------------------------*/ |