source: code/icbd.c@ 270fd23

Last change on this file since 270fd23 was 270fd23, checked in by Florian Obser <florian@…>, 11 years ago

Move modtab into the chroot and make it reloadable by SIGHUB
input mikeb, manpage bits benno

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