source: code/icbd.c@ 863edf1

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

Zero out the input buffer after icb_input and on drops

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