source: code/icbd.c@ d45051e

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

make sure to abort bufferevent processing after icbd_drop was called

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