source: code/icbd.c@ 058b664

Last change on this file since 058b664 was e13307d, checked in by Mike Belopuhov <mike@…>, 11 years ago

Move icbd_ioerr before the dispatch

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