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>
|
---|
22 | #include <sys/tree.h>
|
---|
23 | #include <netinet/in_systm.h>
|
---|
24 | #include <netinet/in.h>
|
---|
25 | #include <netinet/ip.h>
|
---|
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 |
|
---|
45 | uint64_t sessionid;
|
---|
46 | char modtabpath[MAXPATHLEN];
|
---|
47 | char modtab[ICB_MTABLEN][ICB_MAXNICKLEN];
|
---|
48 | int modtabcnt;
|
---|
49 | char srvname[MAXHOSTNAMELEN];
|
---|
50 | int creategroups;
|
---|
51 | int foreground;
|
---|
52 | char logprefix[MAXPATHLEN/2];
|
---|
53 | int dodns = 1;
|
---|
54 | int dologging;
|
---|
55 | int verbose;
|
---|
56 |
|
---|
57 | void usage(void);
|
---|
58 | void getpeerinfo(struct icb_session *);
|
---|
59 | void icbd_accept(int, short, void *);
|
---|
60 | void icbd_paused(int, short, void *);
|
---|
61 | void icbd_drop(struct icb_session *, char *);
|
---|
62 | void icbd_ioerr(struct bufferevent *, short, void *);
|
---|
63 | void icbd_dispatch(struct bufferevent *, void *);
|
---|
64 | void icbd_log(struct icb_session *, int, const char *, ...);
|
---|
65 | void icbd_grplist(char *);
|
---|
66 | void icbd_modtab(char *);
|
---|
67 | void icbd_restrict(void);
|
---|
68 | void icbd_write(struct icb_session *, char *, ssize_t);
|
---|
69 | void icbd_signal(int);
|
---|
70 |
|
---|
71 | static inline int icbd_session_cmp(struct icb_session *, struct icb_session *);
|
---|
72 |
|
---|
73 | RB_HEAD(icbd_sessions, icb_session) icbd_sessions;
|
---|
74 | RB_PROTOTYPE(icbd_sessions, icb_session, node, icbd_session_cmp);
|
---|
75 | RB_GENERATE(icbd_sessions, icb_session, node, icbd_session_cmp);
|
---|
76 |
|
---|
77 | struct icbd_listener {
|
---|
78 | struct event ev, pause;
|
---|
79 | };
|
---|
80 |
|
---|
81 | int
|
---|
82 | main(int argc, char *argv[])
|
---|
83 | {
|
---|
84 | struct event ev_sig;
|
---|
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 |
|
---|
90 | RB_INIT(&icbd_sessions);
|
---|
91 |
|
---|
92 | /* init group lists before calling icb_addgroup */
|
---|
93 | icb_init(&ic);
|
---|
94 |
|
---|
95 | while ((ch = getopt(argc, argv, "46CdG:M:nL:S:v")) != -1)
|
---|
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;
|
---|
112 | case 'L':
|
---|
113 | strlcpy(logprefix, optarg, sizeof logprefix);
|
---|
114 | dologging++;
|
---|
115 | break;
|
---|
116 | case 'M':
|
---|
117 | strlcpy(modtabpath, optarg, sizeof modtabpath);
|
---|
118 | break;
|
---|
119 | case 'n':
|
---|
120 | dodns = 0;
|
---|
121 | break;
|
---|
122 | case 'S':
|
---|
123 | strlcpy(srvname, optarg, sizeof srvname);
|
---|
124 | break;
|
---|
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)
|
---|
143 | errx(EX_USAGE, "Can't specify both -4 and -6");
|
---|
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;
|
---|
160 | struct icbd_listener *l;
|
---|
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 |
|
---|
210 | if ((l = calloc(1, sizeof *l)) == NULL)
|
---|
211 | err(EX_UNAVAILABLE, NULL);
|
---|
212 | event_set(&l->ev, s, EV_READ | EV_PERSIST, icbd_accept, l);
|
---|
213 | if (event_add(&l->ev, NULL) < 0) {
|
---|
214 | syslog(LOG_ERR, "event_add: %m");
|
---|
215 | return (EX_UNAVAILABLE);
|
---|
216 | }
|
---|
217 | evtimer_set(&l->pause, icbd_paused, l);
|
---|
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 |
|
---|
231 | /* start the logger service */
|
---|
232 | logger_init();
|
---|
233 |
|
---|
234 | /* start a dns resolver thread */
|
---|
235 | dns_init();
|
---|
236 |
|
---|
237 | if (!foreground)
|
---|
238 | icbd_restrict();
|
---|
239 |
|
---|
240 | (void)signal(SIGPIPE, SIG_IGN);
|
---|
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 | }
|
---|
247 |
|
---|
248 | (void)event_dispatch();
|
---|
249 |
|
---|
250 | syslog(LOG_ERR, "event_dispatch: %m");
|
---|
251 |
|
---|
252 | return (EX_UNAVAILABLE);
|
---|
253 | }
|
---|
254 |
|
---|
255 | static inline int
|
---|
256 | icbd_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 |
|
---|
265 | inline struct icb_session *
|
---|
266 | icbd_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 |
|
---|
274 | void
|
---|
275 | icbd_accept(int fd, short event __attribute__((__unused__)),
|
---|
276 | void *arg)
|
---|
277 | {
|
---|
278 | struct icbd_listener *l = arg;
|
---|
279 | struct sockaddr_storage ss;
|
---|
280 | struct timeval p = { 1, 0 };
|
---|
281 | struct icb_session *is;
|
---|
282 | socklen_t ss_len = sizeof ss;
|
---|
283 | int s, on = 1, tos = IPTOS_LOWDELAY;
|
---|
284 |
|
---|
285 | ss.ss_len = ss_len;
|
---|
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 | }
|
---|
302 | }
|
---|
303 |
|
---|
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");
|
---|
307 | if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof on) < 0)
|
---|
308 | syslog(LOG_WARNING, "SO_KEEPALIVE: %m");
|
---|
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 |
|
---|
329 | is->id = sessionid++;
|
---|
330 | RB_INSERT(icbd_sessions, &icbd_sessions, is);
|
---|
331 |
|
---|
332 | /* save host information */
|
---|
333 | getpeerinfo(is);
|
---|
334 |
|
---|
335 | /* start icb conversation */
|
---|
336 | icb_start(is);
|
---|
337 | }
|
---|
338 |
|
---|
339 | void
|
---|
340 | icbd_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 |
|
---|
347 | __dead void
|
---|
348 | usage(void)
|
---|
349 | {
|
---|
350 | extern char *__progname;
|
---|
351 |
|
---|
352 | (void)fprintf(stderr, "usage: %s [-46Cdv] [-G group1[,group2,...]] "
|
---|
353 | "[-L prefix] [-M modtab]\n\t[-S name] [[addr][:port] ...]\n",
|
---|
354 | __progname);
|
---|
355 | exit(EX_USAGE);
|
---|
356 | }
|
---|
357 |
|
---|
358 | /*
|
---|
359 | * bufferevent functions
|
---|
360 | */
|
---|
361 | void
|
---|
362 | icbd_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 | }
|
---|
375 |
|
---|
376 | void
|
---|
377 | icbd_dispatch(struct bufferevent *bev, void *arg)
|
---|
378 | {
|
---|
379 | struct icb_session *is = (struct icb_session *)arg;
|
---|
380 |
|
---|
381 | while (EVBUFFER_LENGTH(EVBUFFER_INPUT(bev)) > 0) {
|
---|
382 | if (is->length == 0) {
|
---|
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 | }
|
---|
390 | }
|
---|
391 | /* read as much as we can */
|
---|
392 | is->rlen += bufferevent_read(bev, &is->buffer[is->rlen],
|
---|
393 | is->length);
|
---|
394 | #ifdef DEBUG
|
---|
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 | }
|
---|
404 | #endif
|
---|
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 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | void
|
---|
415 | icbd_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 |
|
---|
423 | printf("-> wrote %lu to %s:%d:\n", size, is->host, is->port);
|
---|
424 | for (i = 0; i < size; i++)
|
---|
425 | printf(" %02x", (unsigned char)buf[i]);
|
---|
426 | printf("\n");
|
---|
427 | }
|
---|
428 | #endif
|
---|
429 | }
|
---|
430 |
|
---|
431 | void
|
---|
432 | icbd_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);
|
---|
442 | RB_REMOVE(icbd_sessions, &icbd_sessions, is);
|
---|
443 | free(is);
|
---|
444 | }
|
---|
445 |
|
---|
446 | void
|
---|
447 | icbd_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 |
|
---|
464 | void
|
---|
465 | icbd_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,
|
---|
476 | LOGIN_SETALL & ~LOGIN_SETUSER) < 0)
|
---|
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 |
|
---|
499 | if (setuid(pw->pw_uid) < 0) {
|
---|
500 | syslog(LOG_ERR, "%d: %m", pw->pw_uid);
|
---|
501 | exit(EX_NOPERM);
|
---|
502 | }
|
---|
503 |
|
---|
504 | (void)setproctitle("icbd");
|
---|
505 | }
|
---|
506 |
|
---|
507 | void
|
---|
508 | icbd_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 |
|
---|
535 | void
|
---|
536 | icbd_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 |
|
---|
572 | void
|
---|
573 | icbd_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 |
|
---|
586 | time_t
|
---|
587 | getmonotime(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 |
|
---|
598 | void
|
---|
599 | getpeerinfo(struct icb_session *is)
|
---|
600 | {
|
---|
601 | struct sockaddr_storage ss;
|
---|
602 | struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
|
---|
603 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
|
---|
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:
|
---|
614 | is->port = ntohs(sin->sin_port);
|
---|
615 | break;
|
---|
616 |
|
---|
617 | case AF_INET6:
|
---|
618 | is->port = ntohs(sin6->sin6_port);
|
---|
619 | break;
|
---|
620 | }
|
---|
621 |
|
---|
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 |
|
---|
626 | dns_rresolv(is, &ss);
|
---|
627 | }
|
---|