source: code/icbd.c@ 4a74b5b

Last change on this file since 4a74b5b was 4a74b5b, checked in by Izuru Yakumo <eternal-servant@…>, 7 weeks ago

Nitori Engineering

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