5 * (c) 2012 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Yet Another Ident Daemon (YAID).
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
31 /*----- Data structures ---------------------------------------------------*/
34 const struct addrops *ao;
42 void (*func)(int, void *);
44 unsigned char buf[WRBUFSZ];
65 /*----- Static variables --------------------------------------------------*/
69 static policy_v policy = DA_INIT;
72 static unsigned char tokenbuf[4096];
73 static size_t tokenptr = sizeof(tokenbuf);
76 /*----- Main code ---------------------------------------------------------*/
78 void logmsg(const struct query *q, int prio, const char *msg, ...)
85 dputsock(&d, q->ao, &q->s[L]);
86 dstr_puts(&d, " <-> ");
87 dputsock(&d, q->ao, &q->s[R]);
90 dstr_vputf(&d, msg, &ap);
92 fprintf(stderr, "yaid: %s\n", d.buf);
96 static void write_out(int fd, unsigned mode, void *p)
99 struct writebuf *wb = p;
101 if ((n = write(fd, wb->buf + wb->o, wb->n)) < 0) {
102 if (errno == EAGAIN || errno == EWOULDBLOCK) return;
105 wb->func(errno, wb->p);
116 static int queue_write(struct writebuf *wb, const void *p, size_t n)
119 if (wb->n - wb->o + n > WRBUFSZ) return (-1);
121 memmove(wb->buf, wb->buf + wb->o, wb->n);
124 memcpy(wb->buf + wb->n, p, n);
126 sel_addfile(&wb->wr);
133 static void free_writebuf(struct writebuf *wb)
134 { if (wb->n) sel_rmfile(&wb->wr); }
136 static void init_writebuf(struct writebuf *wb,
137 int fd, void (*func)(int, void *), void *p)
139 sel_initfile(&sel, &wb->wr, fd, SEL_WRITE, write_out, wb);
145 static void cancel_proxy(struct proxy *px)
151 selbuf_destroy(&px->b);
152 free_writebuf(&px->wb);
154 selbuf_enable(&px->c->b);
159 static void disconnect_client(struct client *c)
162 selbuf_destroy(&c->b);
163 free_writebuf(&c->wb);
164 if (c->px) cancel_proxy(c->px);
168 static void done_client_write(int err, void *p)
170 struct client *c = p;
173 selbuf_enable(&c->b);
175 logmsg(&c->q, LOG_ERR, "failed to send reply: %s", strerror(err));
176 disconnect_client(c);
180 static void write_to_client(struct client *c, const char *fmt, ...)
187 n = vsnprintf(buf, sizeof(buf), fmt, ap);
189 logmsg(&c->q, LOG_ERR, "failed to format output: %s", strerror(errno));
190 disconnect_client(c);
192 } else if (n > sizeof(buf)) {
193 logmsg(&c->q, LOG_ERR, "output too long for client send buffer");
194 disconnect_client(c);
198 selbuf_disable(&c->b);
199 if (queue_write(&c->wb, buf, n)) {
200 logmsg(&c->q, LOG_ERR, "write buffer overflow");
201 disconnect_client(c);
205 static void reply(struct client *c, const char *ty,
206 const char *tok0, const char *tok1)
208 write_to_client(c, "%u,%u:%s:%s%s%s\r\n",
209 c->q.s[L].port, c->q.s[R].port, ty,
210 tok0, tok1 ? ":" : "", tok1 ? tok1 : "");
213 const char *const errtok[] = {
214 #define DEFTOK(err, tok) tok,
219 static void reply_error(struct client *c, unsigned err)
221 assert(err < E_LIMIT);
222 reply(c, "ERROR", errtok[err], 0);
225 static void skipws(const char **pp)
226 { while (isspace((unsigned char )**pp)) (*pp)++; }
228 static int idtoken(const char **pp, char *q, size_t n)
235 if (*p == ':' || *p <= 32 || *p >= 127) break;
245 static int unum(const char **pp, unsigned *ii, unsigned min, unsigned max)
252 if (!isdigit((unsigned char)**pp)) return (-1);
253 e = errno; errno = 0;
254 i = strtoul(*pp, &q, 10);
255 if (errno) return (-1);
258 if (i < min || i > max) return (-1);
263 static void proxy_line(char *line, size_t sz, void *p)
265 struct proxy *px = p;
267 const char *q = line;
270 while (sz && isspace((unsigned char)line[sz - 1])) sz--;
271 printf("received proxy line from %s: %s\n", px->nat, line);
273 if (unum(&q, &lp, 1, 65535)) goto syntax;
274 skipws(&q); if (*q != ',') goto syntax; q++;
275 if (unum(&q, &rp, 1, 65535)) goto syntax;
276 skipws(&q); if (*q != ':') goto syntax; q++;
277 if (lp != px->c->q.u.nat.port || rp != px->c->q.s[R].port) goto syntax;
278 if (idtoken(&q, buf, sizeof(buf))) goto syntax;
279 skipws(&q); if (*q != ':') goto syntax; q++;
280 if (strcmp(buf, "ERROR") == 0) {
282 logmsg(&px->c->q, LOG_ERR, "proxy error from %s: %s", px->nat, q);
283 reply(px->c, "ERROR", q, 0);
284 } else if (strcmp(buf, "USERID") == 0) {
285 if (idtoken(&q, buf, sizeof(buf))) goto syntax;
286 skipws(&q); if (*q != ':') goto syntax; q++;
288 logmsg(&px->c->q, LOG_ERR, "user `%s'; proxy = %s, os = %s",
290 reply(px->c, "USERID", buf, q);
296 logmsg(&px->c->q, LOG_ERR, "failed to parse response from %s", px->nat);
297 reply_error(px->c, E_UNKNOWN);
302 static void done_proxy_write(int err, void *p)
304 struct proxy *px = p;
307 logmsg(&px->c->q, LOG_ERR, "failed to proxy query to %s: %s",
308 px->nat, strerror(errno));
309 reply_error(px->c, E_UNKNOWN);
313 selbuf_enable(&px->b);
316 static void proxy_connected(int fd, void *p)
318 struct proxy *px = p;
323 logmsg(&px->c->q, LOG_ERR,
324 "failed to make %s proxy connection to %s: %s",
325 px->c->l->ao->name, px->nat, strerror(errno));
326 reply_error(px->c, E_UNKNOWN);
332 selbuf_init(&px->b, &sel, fd, proxy_line, px);
333 selbuf_setsize(&px->b, 1024);
334 selbuf_disable(&px->b);
335 init_writebuf(&px->wb, fd, done_proxy_write, px);
337 n = sprintf(buf, "%u,%u\r\n", px->c->q.u.nat.port, px->c->q.s[R].port);
338 queue_write(&px->wb, buf, n);
341 static void proxy_query(struct client *c)
344 struct sockaddr_storage ss;
349 px = xmalloc(sizeof(*px));
350 inet_ntop(c->q.ao->af, &c->q.u.nat.addr, px->nat, sizeof(px->nat));
352 if ((fd = socket(c->q.ao->af, SOCK_STREAM, 0)) < 0) {
353 logmsg(&c->q, LOG_ERR, "failed to make %s socket for proxy: %s",
354 c->l->ao->name, strerror(errno));
358 if (fdflags(fd, O_NONBLOCK, O_NONBLOCK, 0, 0)) {
359 logmsg(&c->q, LOG_ERR, "failed to set %s proxy socket nonblocking: %s",
360 c->l->ao->name, strerror(errno));
366 c->l->ao->socket_to_sockaddr(&s, &ss, &ssz);
367 selbuf_disable(&c->b);
368 if (conn_init(&px->cn, &sel, fd, (struct sockaddr *)&ss, ssz,
369 proxy_connected, px)) {
370 logmsg(&c->q, LOG_ERR, "failed to make %s proxy connection to %s: %s",
371 c->l->ao->name, px->nat, strerror(errno));
375 c->px = px; px->c = c;
380 selbuf_enable(&c->b);
385 reply_error(c, E_UNKNOWN);
388 static const struct policy default_policy = POLICY_INIT(A_NAME);
390 static void user_token(char *p)
392 static const char tokmap[64] =
393 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-";
399 if (tokenptr + TOKENSZ >= sizeof(tokenbuf)) {
400 if (read(randfd, tokenbuf, sizeof(tokenbuf)) < sizeof(tokenbuf))
401 die(1, "unexpected short read or error from `/dev/urandom'");
405 for (i = 0; i < TOKENSZ; i++) {
406 a = (a << 8) | tokenbuf[tokenptr++]; b += 8;
409 *p++ = tokmap[(a >> b) & 0x3f];
413 *p++ = tokmap[(a << (6 - b)) & 0x3f];
417 static void client_line(char *line, size_t len, void *p)
419 struct client *c = p;
421 struct passwd *pw = 0;
422 const struct policy *pol;
424 struct policy upol = POLICY_INIT(A_LIMIT);
425 struct policy_file pf;
429 c->q.s[L].port = c->q.s[R].port = 0;
431 disconnect_client(c);
435 if (fwatch_update(&polfw, "yaid.policy")) {
436 logmsg(0, LOG_INFO, "reload master policy file `%s'", "yaid.policy");
437 load_policy_file("yaid.policy", &policy);
441 if (unum(&q, &c->q.s[L].port, 1, 65535)) goto bad;
442 skipws(&q); if (*q != ',') goto bad; q++;
443 if (unum(&q, &c->q.s[R].port, 1, 65535)) goto bad;
444 skipws(&q); if (*q) goto bad;
449 if ((pw = getpwuid(c->q.u.uid)) == 0) {
450 logmsg(&c->q, LOG_ERR, "no passwd entry for user %d", c->q.u.uid);
451 reply_error(c, E_NOUSER);
459 /* Should already be logged. */
460 reply_error(c, c->q.u.error);
466 for (i = 0; i < DA_LEN(&policy); i++) {
467 pol = &DA(&policy)[i];
468 if (!match_policy(pol, &c->q)) continue;
469 if (pol->act.act != A_USER)
472 dstr_putf(&d, "%s/.yaid.policy", pw->pw_dir);
473 if (open_policy_file(&pf, d.buf, "user policy file", &c->q))
475 while (!read_policy_file(&pf)) {
477 logmsg(&c->q, LOG_ERR, "%s:%d: user policy file too long",
481 if (!match_policy(&pf.p, &c->q)) continue;
482 if (!(pol->act.u.user & (1 << pf.p.act.act))) {
483 logmsg(&c->q, LOG_ERR,
484 "%s:%d: user action forbidden by global policy",
488 upol = pf.p; pol = &upol;
490 close_policy_file(&pf);
493 close_policy_file(&pf);
495 pol = &default_policy;
499 switch (pol->act.act) {
501 logmsg(&c->q, LOG_INFO, "user `%s' (%d)", pw->pw_name, c->q.u.uid);
502 reply(c, "USERID", "UNIX", pw->pw_name);
506 logmsg(&c->q, LOG_INFO, "user `%s' (%d); token = %s",
507 pw->pw_name, c->q.u.uid, buf);
508 reply(c, "USERID", "OTHER", buf);
511 logmsg(&c->q, LOG_INFO, "user `%s' (%d); denying",
512 pw->pw_name, c->q.u.uid);
515 logmsg(&c->q, LOG_INFO, "user `%s' (%d); hiding",
516 pw->pw_name, c->q.u.uid);
517 reply_error(c, E_HIDDEN);
520 logmsg(&c->q, LOG_INFO, "user `%s' (%d); lie = `%s'",
521 pw->pw_name, c->q.u.uid, pol->act.u.lie);
522 reply(c, "USERID", "UNIX", pol->act.u.lie);
532 logmsg(&c->q, LOG_ERR, "failed to parse query from client");
533 disconnect_client(c);
536 static void accept_client(int fd, unsigned mode, void *p)
538 struct listen *l = p;
540 struct sockaddr_storage ssr, ssl;
541 size_t ssz = sizeof(ssr);
544 if ((sk = accept(fd, (struct sockaddr *)&ssr, &ssz)) < 0) {
545 if (errno != EAGAIN && errno == EWOULDBLOCK) {
546 logmsg(0, LOG_ERR, "failed to accept incoming %s connection: %s",
547 l->ao->name, strerror(errno));
552 c = xmalloc(sizeof(*c));
555 l->ao->sockaddr_to_addr(&ssr, &c->q.s[R].addr);
557 if (getsockname(sk, (struct sockaddr *)&ssl, &ssz)) {
559 "failed to read local address for incoming %s connection: %s",
560 l->ao->name, strerror(errno));
565 l->ao->sockaddr_to_addr(&ssl, &c->q.s[L].addr);
566 c->q.s[L].port = c->q.s[R].port = 0;
568 /* logmsg(&c->q, LOG_INFO, "accepted %s connection", l->ao->name); */
570 selbuf_init(&c->b, &sel, sk, client_line, c);
571 selbuf_setsize(&c->b, 1024);
574 init_writebuf(&c->wb, sk, done_client_write, c);
577 static int make_listening_socket(const struct addrops *ao, int port)
582 struct sockaddr_storage ss;
586 if ((fd = socket(ao->af, SOCK_STREAM, 0)) < 0) {
587 if (errno == EAFNOSUPPORT) return (-1);
588 die(1, "failed to create %s listening socket: %s",
589 ao->name, strerror(errno));
591 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
594 ao->socket_to_sockaddr(&s, &ss, &ssz);
595 if (ao->init_listen_socket(fd)) {
596 die(1, "failed to initialize %s listening socket: %s",
597 ao->name, strerror(errno));
599 if (bind(fd, (struct sockaddr *)&ss, ssz)) {
600 die(1, "failed to bind %s listening socket: %s",
601 ao->name, strerror(errno));
603 if (fdflags(fd, O_NONBLOCK, O_NONBLOCK, 0, 0)) {
604 die(1, "failed to set %s listening socket nonblocking: %s",
605 ao->name, strerror(errno));
608 die(1, "failed to listen for %s: %s", ao->name, strerror(errno));
610 l = xmalloc(sizeof(*l));
612 sel_initfile(&sel, &l->f, fd, SEL_READ, accept_client, l);
618 int main(int argc, char *argv[])
621 const struct addrops *ao;
626 fwatch_init(&polfw, "yaid.policy");
628 if (load_policy_file("yaid.policy", &policy))
631 for (i = 0; i < DA_LEN(&policy); i++)
632 print_policy(&DA(&policy)[i]);
635 if ((randfd = open("/dev/urandom", O_RDONLY)) < 0) {
636 die(1, "failed to open `/dev/urandom' for reading: %s",
641 for (ao = addroptab; ao->name; ao++)
642 if (!make_listening_socket(ao, port)) any = 1;
644 die(1, "no IP protocols supported");
647 if (sel_select(&sel)) die(1, "select failed: %s", strerror(errno));
652 /*----- That's all, folks -------------------------------------------------*/