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 <netinet/in_systm.h>
|
---|
23 | #include <netinet/in.h>
|
---|
24 | #include <netinet/ip.h>
|
---|
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>
|
---|
40 | #include <resolv.h>
|
---|
41 |
|
---|
42 | #include "icb.h"
|
---|
43 | #include "icbd.h"
|
---|
44 |
|
---|
45 | struct stat modtabst;
|
---|
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_restrict(void);
|
---|
67 | void icbd_send(struct icb_session *, char *, ssize_t);
|
---|
68 |
|
---|
69 | struct icbd_listener {
|
---|
70 | struct event ev, pause;
|
---|
71 | };
|
---|
72 |
|
---|
73 | int
|
---|
74 | main(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 */
|
---|
81 | icb_init();
|
---|
82 |
|
---|
83 | while ((ch = getopt(argc, argv, "46CdG:M:nL:S:v")) != -1)
|
---|
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;
|
---|
100 | case 'L':
|
---|
101 | strlcpy(logprefix, optarg, sizeof logprefix);
|
---|
102 | dologging++;
|
---|
103 | break;
|
---|
104 | case 'M':
|
---|
105 | strlcpy(modtabpath, optarg, sizeof modtabpath);
|
---|
106 | break;
|
---|
107 | case 'n':
|
---|
108 | dodns = 0;
|
---|
109 | break;
|
---|
110 | case 'S':
|
---|
111 | strlcpy(srvname, optarg, sizeof srvname);
|
---|
112 | break;
|
---|
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)
|
---|
131 | errx(EX_USAGE, "Can't specify both -4 and -6");
|
---|
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;
|
---|
148 | struct icbd_listener *l;
|
---|
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 |
|
---|
198 | if ((l = calloc(1, sizeof *l)) == NULL)
|
---|
199 | err(EX_UNAVAILABLE, NULL);
|
---|
200 | event_set(&l->ev, s, EV_READ | EV_PERSIST,
|
---|
201 | icbd_accept, l);
|
---|
202 | if (event_add(&l->ev, NULL) < 0) {
|
---|
203 | syslog(LOG_ERR, "event_add: %m");
|
---|
204 | return (EX_UNAVAILABLE);
|
---|
205 | }
|
---|
206 | evtimer_set(&l->pause, icbd_paused, l);
|
---|
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 |
|
---|
220 | /* start the logger service */
|
---|
221 | logger_init();
|
---|
222 |
|
---|
223 | /* initialize resolver */
|
---|
224 | res_init();
|
---|
225 |
|
---|
226 | if (!foreground)
|
---|
227 | icbd_restrict();
|
---|
228 |
|
---|
229 | icbd_modupdate();
|
---|
230 |
|
---|
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 |
|
---|
240 | void
|
---|
241 | icbd_accept(int fd, short event __attribute__((__unused__)),
|
---|
242 | void *arg)
|
---|
243 | {
|
---|
244 | struct icbd_listener *l = arg;
|
---|
245 | struct sockaddr_storage ss;
|
---|
246 | struct timeval p = { 1, 0 };
|
---|
247 | struct icb_session *is;
|
---|
248 | socklen_t ss_len = sizeof ss;
|
---|
249 | int s, on = 1, tos = IPTOS_LOWDELAY;
|
---|
250 |
|
---|
251 | ss.ss_len = ss_len;
|
---|
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 | }
|
---|
268 | }
|
---|
269 |
|
---|
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");
|
---|
273 | if (setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, &on, sizeof on) < 0)
|
---|
274 | syslog(LOG_WARNING, "SO_KEEPALIVE: %m");
|
---|
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 |
|
---|
302 | void
|
---|
303 | icbd_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 |
|
---|
310 | __dead void
|
---|
311 | usage(void)
|
---|
312 | {
|
---|
313 | extern char *__progname;
|
---|
314 |
|
---|
315 | (void)fprintf(stderr, "usage: %s [-46Cdv] [-G group1[,group2,...]] "
|
---|
316 | "[-L prefix] [-M modtab]\n\t[-S name] [[addr][:port] ...]\n",
|
---|
317 | __progname);
|
---|
318 | exit(EX_USAGE);
|
---|
319 | }
|
---|
320 |
|
---|
321 | /*
|
---|
322 | * bufferevent functions
|
---|
323 | */
|
---|
324 | void
|
---|
325 | icbd_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 | }
|
---|
338 |
|
---|
339 | void
|
---|
340 | icbd_dispatch(struct bufferevent *bev, void *arg)
|
---|
341 | {
|
---|
342 | struct icb_session *is = (struct icb_session *)arg;
|
---|
343 | unsigned char length;
|
---|
344 | size_t res;
|
---|
345 |
|
---|
346 | while (EVBUFFER_LENGTH(EVBUFFER_INPUT(bev)) > 0) {
|
---|
347 | if (is->length == 0) {
|
---|
348 | /* read length */
|
---|
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 | */
|
---|
361 | icbd_drop(is, "invalid packet");
|
---|
362 | return;
|
---|
363 | }
|
---|
364 | is->length = (size_t)length;
|
---|
365 | is->rlen = 0;
|
---|
366 | }
|
---|
367 | /* read as much as we can */
|
---|
368 | res = bufferevent_read(bev, &is->buffer[is->rlen],
|
---|
369 | is->length - is->rlen);
|
---|
370 | is->rlen += res;
|
---|
371 | #ifdef DEBUG
|
---|
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 | }
|
---|
381 | #endif
|
---|
382 | /* see you next time around */
|
---|
383 | if (is->rlen < is->length)
|
---|
384 | return;
|
---|
385 | /* null-terminate the data */
|
---|
386 | is->buffer[MIN(is->rlen, ICB_MSGSIZE - 1)] = '\0';
|
---|
387 | /* process the message in full */
|
---|
388 | if (icb_input(is))
|
---|
389 | return;
|
---|
390 | is->rlen = is->length = 0;
|
---|
391 | }
|
---|
392 | }
|
---|
393 |
|
---|
394 | void
|
---|
395 | icbd_send(struct icb_session *is, char *buf, ssize_t size)
|
---|
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 |
|
---|
403 | printf("-> wrote %lu to %s:%d:\n", size, is->host, is->port);
|
---|
404 | for (i = 0; i < size; i++)
|
---|
405 | printf(" %02x", (unsigned char)buf[i]);
|
---|
406 | printf("\n");
|
---|
407 | }
|
---|
408 | #endif
|
---|
409 | }
|
---|
410 |
|
---|
411 | void
|
---|
412 | icbd_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 | if (!ISSETF(is->flags, ICB_SF_DNSINPROGRESS))
|
---|
423 | free(is);
|
---|
424 | else
|
---|
425 | SETF(is->flags, ICB_SF_PENDINGDROP);
|
---|
426 | }
|
---|
427 |
|
---|
428 | void
|
---|
429 | icbd_log(struct icb_session *is, int level, const char *fmt, ...)
|
---|
430 | {
|
---|
431 | char buf[512];
|
---|
432 | va_list ap;
|
---|
433 |
|
---|
434 | if (!verbose && level == LOG_DEBUG)
|
---|
435 | return;
|
---|
436 |
|
---|
437 | va_start(ap, fmt);
|
---|
438 | (void)vsnprintf(buf, sizeof buf, fmt, ap);
|
---|
439 | va_end(ap);
|
---|
440 | if (is)
|
---|
441 | syslog(level, "%s:%u: %s", is->host, is->port, buf);
|
---|
442 | else
|
---|
443 | syslog(level, "%s", buf);
|
---|
444 | }
|
---|
445 |
|
---|
446 | void
|
---|
447 | icbd_restrict(void)
|
---|
448 | {
|
---|
449 | struct stat sb;
|
---|
450 | struct passwd *pw;
|
---|
451 |
|
---|
452 | if ((pw = getpwnam(ICBD_USER)) == NULL) {
|
---|
453 | syslog(LOG_ERR, "No passwd entry for %s", ICBD_USER);
|
---|
454 | exit(EX_NOUSER);
|
---|
455 | }
|
---|
456 |
|
---|
457 | if (setusercontext(NULL, pw, pw->pw_uid,
|
---|
458 | LOGIN_SETALL & ~LOGIN_SETUSER) < 0)
|
---|
459 | exit(EX_NOPERM);
|
---|
460 |
|
---|
461 | if (stat(pw->pw_dir, &sb) == -1) {
|
---|
462 | syslog(LOG_ERR, "%s: %m", pw->pw_name);
|
---|
463 | exit(EX_NOPERM);
|
---|
464 | }
|
---|
465 |
|
---|
466 | if (sb.st_uid != 0 || (sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
|
---|
467 | syslog(LOG_ERR, "bad directory permissions");
|
---|
468 | exit(EX_NOPERM);
|
---|
469 | }
|
---|
470 |
|
---|
471 | if (chroot(pw->pw_dir) < 0) {
|
---|
472 | syslog(LOG_ERR, "%s: %m", pw->pw_dir);
|
---|
473 | exit(EX_UNAVAILABLE);
|
---|
474 | }
|
---|
475 |
|
---|
476 | if (chdir("/") < 0) {
|
---|
477 | syslog(LOG_ERR, "/" ICBD_HOME ": %m");
|
---|
478 | exit(EX_UNAVAILABLE);
|
---|
479 | }
|
---|
480 |
|
---|
481 | chdir(ICBD_HOME);
|
---|
482 |
|
---|
483 | if (setuid(pw->pw_uid) < 0) {
|
---|
484 | syslog(LOG_ERR, "%d: %m", pw->pw_uid);
|
---|
485 | exit(EX_NOPERM);
|
---|
486 | }
|
---|
487 |
|
---|
488 | (void)setproctitle("icbd");
|
---|
489 | }
|
---|
490 |
|
---|
491 | void
|
---|
492 | icbd_grplist(char *list)
|
---|
493 | {
|
---|
494 | char *s, *s1, *s2;
|
---|
495 | int last = 0;
|
---|
496 |
|
---|
497 | if (!list || strlen(list) == 0)
|
---|
498 | return;
|
---|
499 |
|
---|
500 | /* "group1[:pass1][,group2[:pass2],...]" */
|
---|
501 | s = list;
|
---|
502 | s1 = s2 = NULL;
|
---|
503 | while (!last && s) {
|
---|
504 | if ((s1 = strchr(s, ',')) != NULL)
|
---|
505 | *s1 = '\0';
|
---|
506 | else {
|
---|
507 | last = 1;
|
---|
508 | s1 = s;
|
---|
509 | }
|
---|
510 | if ((s2 = strchr(s, ':')) != NULL)
|
---|
511 | *s2 = '\0';
|
---|
512 | if (icb_addgroup(NULL, s, s2 ? ++s2 : NULL) == NULL)
|
---|
513 | err(EX_UNAVAILABLE, NULL);
|
---|
514 | s = ++s1;
|
---|
515 | s1 = s2 = NULL;
|
---|
516 | }
|
---|
517 | }
|
---|
518 |
|
---|
519 | void
|
---|
520 | icbd_modupdate(void)
|
---|
521 | {
|
---|
522 | struct stat st;
|
---|
523 | FILE *fp;
|
---|
524 | char *buf, *lbuf;
|
---|
525 | size_t len;
|
---|
526 |
|
---|
527 | if (strlen(modtabpath) == 0)
|
---|
528 | return;
|
---|
529 | if (stat(modtabpath, &st)) {
|
---|
530 | syslog(LOG_ERR, "stat %s: %m", modtabpath);
|
---|
531 | return;
|
---|
532 | }
|
---|
533 | /* see if there are any changes */
|
---|
534 | if (timespeccmp(&st.st_mtim, &modtabst.st_mtim, ==) ||
|
---|
535 | st.st_size == 0)
|
---|
536 | return;
|
---|
537 |
|
---|
538 | if ((fp = fopen(modtabpath, "r")) == NULL) {
|
---|
539 | syslog(LOG_ERR, "open %s: %m", modtabpath);
|
---|
540 | return;
|
---|
541 | }
|
---|
542 |
|
---|
543 | modtabcnt = 0;
|
---|
544 | bzero(modtab, ICB_MTABLEN * ICB_MAXNICKLEN);
|
---|
545 | lbuf = NULL;
|
---|
546 | while ((buf = fgetln(fp, &len)) && modtabcnt < ICB_MTABLEN) {
|
---|
547 | if (buf[len - 1] == '\n')
|
---|
548 | buf[len - 1] = '\0';
|
---|
549 | else {
|
---|
550 | /* EOF without EOL, copy and add the NUL */
|
---|
551 | if ((lbuf = malloc(len + 1)) == NULL)
|
---|
552 | err(1, NULL);
|
---|
553 | memcpy(lbuf, buf, len);
|
---|
554 | lbuf[len] = '\0';
|
---|
555 | buf = lbuf;
|
---|
556 | }
|
---|
557 | while (buf[0] == ' ' || buf[0] == '\t')
|
---|
558 | buf++;
|
---|
559 | if (buf[0] == '#' || buf[0] == '\0')
|
---|
560 | continue;
|
---|
561 | strlcpy(modtab[modtabcnt++], buf, ICB_MAXNICKLEN);
|
---|
562 | }
|
---|
563 | free(lbuf);
|
---|
564 |
|
---|
565 | qsort(modtab, modtabcnt, ICB_MAXNICKLEN,
|
---|
566 | (int (*)(const void *, const void *))strcmp);
|
---|
567 |
|
---|
568 | fclose(fp);
|
---|
569 |
|
---|
570 | memcpy(&modtabst, &st, sizeof modtabst);
|
---|
571 | }
|
---|
572 |
|
---|
573 | time_t
|
---|
574 | getmonotime(void)
|
---|
575 | {
|
---|
576 | struct timespec ts;
|
---|
577 |
|
---|
578 | if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
|
---|
579 | syslog(LOG_ERR, "%m");
|
---|
580 | exit(EX_OSERR);
|
---|
581 | }
|
---|
582 | return (ts.tv_sec);
|
---|
583 | }
|
---|
584 |
|
---|
585 | void
|
---|
586 | getpeerinfo(struct icb_session *is)
|
---|
587 | {
|
---|
588 | struct sockaddr_storage ss;
|
---|
589 | struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
|
---|
590 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
|
---|
591 | socklen_t ss_len = sizeof ss;
|
---|
592 |
|
---|
593 | bzero(&ss, sizeof ss);
|
---|
594 | if (getpeername(EVBUFFER_FD(is->bev), (struct sockaddr *)&ss,
|
---|
595 | &ss_len) != 0)
|
---|
596 | return;
|
---|
597 |
|
---|
598 | is->port = 0;
|
---|
599 | switch (ss.ss_family) {
|
---|
600 | case AF_INET:
|
---|
601 | is->port = ntohs(sin->sin_port);
|
---|
602 | break;
|
---|
603 |
|
---|
604 | case AF_INET6:
|
---|
605 | is->port = ntohs(sin6->sin6_port);
|
---|
606 | break;
|
---|
607 | }
|
---|
608 |
|
---|
609 | inet_ntop(ss.ss_family, ss.ss_family == AF_INET ?
|
---|
610 | (void *)&sin->sin_addr : (void *)&sin6->sin6_addr,
|
---|
611 | is->host, sizeof is->host);
|
---|
612 |
|
---|
613 | dns_resolve(is, (struct sockaddr *)&ss);
|
---|
614 | }
|
---|