source: code/icbd.c@ f73b386

Last change on this file since f73b386 was d27dc1e, checked in by Mike Belopuhov <mike@…>, 10 years ago

fixup signal and MAXPATHLEN include fallout

  • Property mode set to 100644
File size: 13.1 KB
RevLine 
[cd7b81d]1/*
2 * Copyright (c) 2009 Mike Belopuhov
3 * Copyright (c) 2007 Oleg Safiullin
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <sys/types.h>
19#include <sys/queue.h>
20#include <sys/socket.h>
21#include <sys/stat.h>
[c8c9ccf]22#include <netinet/in_systm.h>
[cd7b81d]23#include <netinet/in.h>
[c8c9ccf]24#include <netinet/ip.h>
[cd7b81d]25#include <arpa/inet.h>
26#include <fcntl.h>
[d27dc1e]27#include <limits.h>
28#include <signal.h>
[cd7b81d]29#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <string.h>
33#include <sysexits.h>
34#include <syslog.h>
35#include <pwd.h>
36#include <login_cap.h>
37#include <locale.h>
38#include <netdb.h>
39#include <event.h>
40#include <errno.h>
41#include <err.h>
[e87ab6d]42#include <resolv.h>
[cd7b81d]43
44#include "icb.h"
45#include "icbd.h"
46
[82d3c1f]47struct stat modtabst;
[d27dc1e]48char modtabpath[PATH_MAX];
[1d2125a]49char modtab[ICB_MTABLEN][ICB_MAXNICKLEN];
50int modtabcnt;
[d27dc1e]51char srvname[NI_MAXHOST];
[a785c27]52int creategroups;
53int foreground;
[d27dc1e]54char logprefix[PATH_MAX/2];
[460786f]55int dodns = 1;
[3dba97d]56int dologging;
[a785c27]57int verbose;
[cd7b81d]58
59void usage(void);
60void getpeerinfo(struct icb_session *);
61void icbd_accept(int, short, void *);
[6e89d69]62void icbd_paused(int, short, void *);
[cd7b81d]63void icbd_drop(struct icb_session *, char *);
64void icbd_ioerr(struct bufferevent *, short, void *);
65void icbd_dispatch(struct bufferevent *, void *);
66void icbd_log(struct icb_session *, int, const char *, ...);
67void icbd_grplist(char *);
68void icbd_restrict(void);
[7882a6f]69void icbd_send(struct icb_session *, char *, ssize_t);
[cd7b81d]70
[6e89d69]71struct icbd_listener {
72 struct event ev, pause;
73};
74
[cd7b81d]75int
76main(int argc, char *argv[])
77{
78 const char *cause = NULL;
79 int ch, nsocks = 0, save_errno = 0;
80 int inet4 = 0, inet6 = 0;
81
82 /* init group lists before calling icb_addgroup */
[7882a6f]83 icb_init();
[cd7b81d]84
[460786f]85 while ((ch = getopt(argc, argv, "46CdG:M:nL:S:v")) != -1)
[cd7b81d]86 switch (ch) {
87 case '4':
88 inet4++;
89 break;
90 case '6':
91 inet6++;
92 break;
93 case 'C':
94 creategroups++;
95 break;
96 case 'd':
97 foreground++;
98 break;
99 case 'G':
100 icbd_grplist(optarg);
101 break;
[3dba97d]102 case 'L':
103 strlcpy(logprefix, optarg, sizeof logprefix);
104 dologging++;
105 break;
[1d2125a]106 case 'M':
[270fd23]107 strlcpy(modtabpath, optarg, sizeof modtabpath);
[1d2125a]108 break;
[460786f]109 case 'n':
110 dodns = 0;
111 break;
[a785c27]112 case 'S':
113 strlcpy(srvname, optarg, sizeof srvname);
114 break;
[cd7b81d]115 case 'v':
116 verbose++;
117 break;
118 default:
119 usage();
120 /* NOTREACHED */
121 }
122 argc -= optind;
123 argv += optind;
124
125 /* add group "1" as it's a login group for most of the clients */
126 if (icb_addgroup(NULL, "1", NULL) == NULL)
127 err(EX_UNAVAILABLE, NULL);
128
129 if (argc == 0)
130 argc++;
131
132 if (inet4 && inet6)
[2e37e9f]133 errx(EX_USAGE, "Can't specify both -4 and -6");
[cd7b81d]134
135 tzset();
136 (void)setlocale(LC_ALL, "C");
137
138 if (foreground)
139 openlog("icbd", LOG_PID | LOG_PERROR, LOG_DAEMON);
140 else
141 openlog("icbd", LOG_PID | LOG_NDELAY, LOG_DAEMON);
142
143 if (!foreground && daemon(0, 0) < 0)
144 err(EX_OSERR, NULL);
145
146 (void)event_init();
147
148 for (ch = 0; ch < argc; ch++) {
149 struct addrinfo hints, *res, *res0;
[6e89d69]150 struct icbd_listener *l;
[cd7b81d]151 char *addr, *port;
152 int error, s, on = 1;
153
154 addr = port = NULL;
155 if (argv[ch] != NULL) {
156 if (argv[ch][0] != ':')
157 addr = argv[ch];
158 if ((port = strrchr(argv[ch], ':')) != NULL)
159 *port++ = '\0';
160 }
161
162 bzero(&hints, sizeof hints);
163 if (inet4 || inet6)
164 hints.ai_family = inet4 ? PF_INET : PF_INET6;
165 else
166 hints.ai_family = PF_UNSPEC;
167 hints.ai_socktype = SOCK_STREAM;
168 hints.ai_flags = AI_PASSIVE;
169 if ((error = getaddrinfo(addr, port ? port : "icb", &hints,
170 &res0)) != 0) {
171 syslog(LOG_ERR, "%s", gai_strerror(error));
172 return (EX_UNAVAILABLE);
173 }
174
175 for (res = res0; res != NULL; res = res->ai_next) {
176 if ((s = socket(res->ai_family, res->ai_socktype,
177 res->ai_protocol)) < 0) {
178 cause = "socket";
179 save_errno = errno;
180 continue;
181 }
182
183 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &on,
184 sizeof on) < 0) {
185 cause = "SO_REUSEADDR";
186 save_errno = errno;
187 (void)close(s);
188 continue;
189 }
190
191 if (bind(s, res->ai_addr, res->ai_addrlen) < 0) {
192 cause = "bind";
193 save_errno = errno;
194 (void)close(s);
195 continue;
196 }
197
198 (void)listen(s, TCP_BACKLOG);
199
[6e89d69]200 if ((l = calloc(1, sizeof *l)) == NULL)
[cd7b81d]201 err(EX_UNAVAILABLE, NULL);
[bacf9da]202 event_set(&l->ev, s, EV_READ | EV_PERSIST,
203 icbd_accept, l);
[6e89d69]204 if (event_add(&l->ev, NULL) < 0) {
[cd7b81d]205 syslog(LOG_ERR, "event_add: %m");
206 return (EX_UNAVAILABLE);
207 }
[6e89d69]208 evtimer_set(&l->pause, icbd_paused, l);
[cd7b81d]209
210 nsocks++;
211 }
212
213 freeaddrinfo(res0);
214 }
215
216 if (nsocks == 0) {
217 errno = save_errno;
218 syslog(LOG_ERR, "%s: %m", cause);
219 return (EX_UNAVAILABLE);
220 }
221
[55923b7]222 /* start the logger service */
223 logger_init();
224
[e87ab6d]225 /* initialize resolver */
226 res_init();
[cd7b81d]227
228 if (!foreground)
229 icbd_restrict();
230
[82d3c1f]231 icbd_modupdate();
232
[cd7b81d]233 (void)signal(SIGPIPE, SIG_IGN);
234
235 (void)event_dispatch();
236
237 syslog(LOG_ERR, "event_dispatch: %m");
238
239 return (EX_UNAVAILABLE);
240}
241
242void
243icbd_accept(int fd, short event __attribute__((__unused__)),
[6e89d69]244 void *arg)
[cd7b81d]245{
[6e89d69]246 struct icbd_listener *l = arg;
[cd7b81d]247 struct sockaddr_storage ss;
[6e89d69]248 struct timeval p = { 1, 0 };
[cd7b81d]249 struct icb_session *is;
250 socklen_t ss_len = sizeof ss;
[8ef8c4e]251 int s, on = 1, tos = IPTOS_LOWDELAY;
[cd7b81d]252
253 ss.ss_len = ss_len;
[6e89d69]254 s = accept(fd, (struct sockaddr *)&ss, &ss_len);
255 if (s == -1) {
256 switch (errno) {
257 case EINTR:
258 case EWOULDBLOCK:
259 case ECONNABORTED:
260 return;
261 case EMFILE:
262 case ENFILE:
263 event_del(&l->ev);
264 evtimer_add(&l->pause, &p);
265 return;
266 default:
267 syslog(LOG_ERR, "accept: %m");
268 return;
269 }
[cd7b81d]270 }
[6e89d69]271
[c8c9ccf]272 if (ss.ss_family == AF_INET)
273 if (setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof tos) < 0)
274 syslog(LOG_WARNING, "IP_TOS: %m");
[8ef8c4e]275 if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof on) < 0)
276 syslog(LOG_WARNING, "SO_KEEPALIVE: %m");
[cd7b81d]277 if ((is = calloc(1, sizeof *is)) == NULL) {
278 syslog(LOG_ERR, "calloc: %m");
279 (void)close(s);
280 return;
281 }
282 if ((is->bev = bufferevent_new(s, icbd_dispatch, NULL, icbd_ioerr,
283 is)) == NULL) {
284 syslog(LOG_ERR, "bufferevent_new: %m");
285 (void)close(s);
286 free(is);
287 return;
288 }
289 if (bufferevent_enable(is->bev, EV_READ)) {
290 syslog(LOG_ERR, "bufferevent_enable: %m");
291 (void)close(s);
292 bufferevent_free(is->bev);
293 free(is);
294 return;
295 }
296
297 /* save host information */
298 getpeerinfo(is);
299
300 /* start icb conversation */
301 icb_start(is);
302}
303
[6e89d69]304void
305icbd_paused(int fd __attribute__((__unused__)),
306 short events __attribute__((__unused__)), void *arg)
307{
308 struct icbd_listener *l = arg;
309 event_add(&l->ev, NULL);
310}
311
[cd7b81d]312__dead void
313usage(void)
314{
[3dba97d]315 extern char *__progname;
316
[cd7b81d]317 (void)fprintf(stderr, "usage: %s [-46Cdv] [-G group1[,group2,...]] "
[3dba97d]318 "[-L prefix] [-M modtab]\n\t[-S name] [[addr][:port] ...]\n",
319 __progname);
[cd7b81d]320 exit(EX_USAGE);
321}
322
323/*
324 * bufferevent functions
325 */
[e13307d]326void
327icbd_ioerr(struct bufferevent *bev __attribute__((__unused__)), short what,
328 void *arg)
329{
330 struct icb_session *is = (struct icb_session *)arg;
331
332 if (what & EVBUFFER_TIMEOUT)
333 icbd_drop(is, "timeout");
334 else if (what & EVBUFFER_EOF)
335 icbd_drop(is, NULL);
336 else if (what & EVBUFFER_ERROR)
337 icbd_drop(is, (what & EVBUFFER_READ) ? "read error" :
338 "write error");
339}
[cd7b81d]340
341void
342icbd_dispatch(struct bufferevent *bev, void *arg)
343{
344 struct icb_session *is = (struct icb_session *)arg;
[a2fadb4]345 unsigned char length;
[de86230]346 size_t res;
[cd7b81d]347
[c9402c3]348 while (EVBUFFER_LENGTH(EVBUFFER_INPUT(bev)) > 0) {
[cd7b81d]349 if (is->length == 0) {
[c9402c3]350 /* read length */
[a2fadb4]351 bufferevent_read(bev, &length, 1);
352 if (length == 0) {
353 /*
354 * An extension has been proposed:
355 * if length is 0, the packet is part of an
356 * "extended packet". The packet should be
357 * treated as if length was 255 and the next
358 * packet received from the sender should be
359 * appended to this packet.
360 *
361 * This server doesn't support this yet.
362 */
[c9402c3]363 icbd_drop(is, "invalid packet");
364 return;
365 }
[a2fadb4]366 is->length = (size_t)length;
367 is->rlen = 0;
[cd7b81d]368 }
[c9402c3]369 /* read as much as we can */
[de86230]370 res = bufferevent_read(bev, &is->buffer[is->rlen],
371 is->length - is->rlen);
372 is->rlen += res;
[cd7b81d]373#ifdef DEBUG
[c9402c3]374 {
375 int i;
376
377 printf("-> read %lu out of %lu from %s:%d:\n",
378 is->rlen, is->length, is->host, is->port);
379 for (i = 0; i < (int)is->rlen; i++)
380 printf(" %02x", (unsigned char)is->buffer[i]);
381 printf("\n");
382 }
[cd7b81d]383#endif
[c9402c3]384 /* see you next time around */
385 if (is->rlen < is->length)
386 return;
[a2fadb4]387 /* null-terminate the data */
388 is->buffer[MIN(is->rlen, ICB_MSGSIZE - 1)] = '\0';
[c9402c3]389 /* process the message in full */
[d45051e]390 if (icb_input(is))
391 return;
[c9402c3]392 is->rlen = is->length = 0;
393 }
[cd7b81d]394}
395
396void
[7882a6f]397icbd_send(struct icb_session *is, char *buf, ssize_t size)
[cd7b81d]398{
399 if (bufferevent_write(is->bev, buf, size) == -1)
400 syslog(LOG_ERR, "bufferevent_write: %m");
401#ifdef DEBUG
402 {
403 int i;
404
[c9402c3]405 printf("-> wrote %lu to %s:%d:\n", size, is->host, is->port);
[cd7b81d]406 for (i = 0; i < size; i++)
407 printf(" %02x", (unsigned char)buf[i]);
408 printf("\n");
409 }
410#endif
411}
412
413void
414icbd_drop(struct icb_session *is, char *reason)
415{
416 if (reason) {
417 icb_remove(is, reason);
418 icbd_log(is, LOG_DEBUG, reason);
419 } else
420 icb_remove(is, NULL);
421 (void)evbuffer_write(EVBUFFER_OUTPUT(is->bev), EVBUFFER_FD(is->bev));
422 (void)close(EVBUFFER_FD(is->bev));
423 bufferevent_free(is->bev);
[120eedd]424 if (!ISSETF(is->flags, ICB_SF_DNSINPROGRESS))
425 free(is);
426 else
427 SETF(is->flags, ICB_SF_PENDINGDROP);
[cd7b81d]428}
429
430void
431icbd_log(struct icb_session *is, int level, const char *fmt, ...)
432{
433 char buf[512];
434 va_list ap;
435
436 if (!verbose && level == LOG_DEBUG)
437 return;
438
439 va_start(ap, fmt);
440 (void)vsnprintf(buf, sizeof buf, fmt, ap);
441 va_end(ap);
442 if (is)
443 syslog(level, "%s:%u: %s", is->host, is->port, buf);
444 else
445 syslog(level, "%s", buf);
446}
447
448void
449icbd_restrict(void)
450{
451 struct stat sb;
452 struct passwd *pw;
453
454 if ((pw = getpwnam(ICBD_USER)) == NULL) {
455 syslog(LOG_ERR, "No passwd entry for %s", ICBD_USER);
456 exit(EX_NOUSER);
457 }
458
459 if (setusercontext(NULL, pw, pw->pw_uid,
[d554223]460 LOGIN_SETALL & ~LOGIN_SETUSER) < 0)
[cd7b81d]461 exit(EX_NOPERM);
462
463 if (stat(pw->pw_dir, &sb) == -1) {
464 syslog(LOG_ERR, "%s: %m", pw->pw_name);
465 exit(EX_NOPERM);
466 }
467
468 if (sb.st_uid != 0 || (sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
469 syslog(LOG_ERR, "bad directory permissions");
470 exit(EX_NOPERM);
471 }
472
473 if (chroot(pw->pw_dir) < 0) {
474 syslog(LOG_ERR, "%s: %m", pw->pw_dir);
475 exit(EX_UNAVAILABLE);
476 }
477
[cdd2ff5]478 if (chdir("/") < 0) {
[ad8b08d]479 syslog(LOG_ERR, "/" ICBD_HOME ": %m");
[cd7b81d]480 exit(EX_UNAVAILABLE);
481 }
482
[cdd2ff5]483 chdir(ICBD_HOME);
484
[e54f151]485 if (setuid(pw->pw_uid) < 0) {
486 syslog(LOG_ERR, "%d: %m", pw->pw_uid);
487 exit(EX_NOPERM);
488 }
489
[cd7b81d]490 (void)setproctitle("icbd");
491}
492
493void
494icbd_grplist(char *list)
495{
496 char *s, *s1, *s2;
497 int last = 0;
498
499 if (!list || strlen(list) == 0)
500 return;
501
502 /* "group1[:pass1][,group2[:pass2],...]" */
503 s = list;
504 s1 = s2 = NULL;
505 while (!last && s) {
506 if ((s1 = strchr(s, ',')) != NULL)
507 *s1 = '\0';
508 else {
509 last = 1;
510 s1 = s;
511 }
512 if ((s2 = strchr(s, ':')) != NULL)
513 *s2 = '\0';
514 if (icb_addgroup(NULL, s, s2 ? ++s2 : NULL) == NULL)
515 err(EX_UNAVAILABLE, NULL);
516 s = ++s1;
517 s1 = s2 = NULL;
518 }
519}
520
[1d2125a]521void
[82d3c1f]522icbd_modupdate(void)
[1d2125a]523{
[82d3c1f]524 struct stat st;
[1d2125a]525 FILE *fp;
526 char *buf, *lbuf;
527 size_t len;
528
[82d3c1f]529 if (strlen(modtabpath) == 0)
530 return;
531 if (stat(modtabpath, &st)) {
[709589d]532 syslog(LOG_ERR, "stat %s: %m", modtabpath);
[82d3c1f]533 return;
534 }
535 /* see if there are any changes */
536 if (timespeccmp(&st.st_mtim, &modtabst.st_mtim, ==) ||
537 st.st_size == 0)
538 return;
539
[709589d]540 if ((fp = fopen(modtabpath, "r")) == NULL) {
541 syslog(LOG_ERR, "open %s: %m", modtabpath);
542 return;
543 }
[1d2125a]544
[2cdab10]545 modtabcnt = 0;
[1d2125a]546 bzero(modtab, ICB_MTABLEN * ICB_MAXNICKLEN);
547 lbuf = NULL;
548 while ((buf = fgetln(fp, &len)) && modtabcnt < ICB_MTABLEN) {
549 if (buf[len - 1] == '\n')
550 buf[len - 1] = '\0';
551 else {
552 /* EOF without EOL, copy and add the NUL */
553 if ((lbuf = malloc(len + 1)) == NULL)
554 err(1, NULL);
555 memcpy(lbuf, buf, len);
556 lbuf[len] = '\0';
557 buf = lbuf;
558 }
559 while (buf[0] == ' ' || buf[0] == '\t')
560 buf++;
561 if (buf[0] == '#' || buf[0] == '\0')
562 continue;
563 strlcpy(modtab[modtabcnt++], buf, ICB_MAXNICKLEN);
564 }
565 free(lbuf);
566
567 qsort(modtab, modtabcnt, ICB_MAXNICKLEN,
568 (int (*)(const void *, const void *))strcmp);
569
570 fclose(fp);
[709589d]571
572 memcpy(&modtabst, &st, sizeof modtabst);
[1d2125a]573}
574
[cd7b81d]575time_t
576getmonotime(void)
577{
578 struct timespec ts;
579
580 if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
581 syslog(LOG_ERR, "%m");
582 exit(EX_OSERR);
583 }
584 return (ts.tv_sec);
585}
586
587void
588getpeerinfo(struct icb_session *is)
589{
[e68221b]590 struct sockaddr_in *sin = (struct sockaddr_in *)&is->ss;
591 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&is->ss;
592 socklen_t ss_len = sizeof is->ss;
[cd7b81d]593
[e68221b]594 bzero(&is->ss, sizeof is->ss);
595 if (getpeername(EVBUFFER_FD(is->bev), (struct sockaddr *)&is->ss,
[cd7b81d]596 &ss_len) != 0)
597 return;
598
599 is->port = 0;
[e68221b]600 switch (is->ss.ss_family) {
[cd7b81d]601 case AF_INET:
[b4049f9]602 is->port = ntohs(sin->sin_port);
[cd7b81d]603 break;
604
605 case AF_INET6:
[b4049f9]606 is->port = ntohs(sin6->sin6_port);
[cd7b81d]607 break;
608 }
609
[e68221b]610 inet_ntop(is->ss.ss_family, is->ss.ss_family == AF_INET ?
[b4049f9]611 (void *)&sin->sin_addr : (void *)&sin6->sin6_addr,
612 is->host, sizeof is->host);
613
[e68221b]614 dns_resolve(is);
[cd7b81d]615}
Note: See TracBrowser for help on using the repository browser.