[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] | 45 | struct stat modtabst;
|
---|
[270fd23] | 46 | char modtabpath[MAXPATHLEN];
|
---|
[1d2125a] | 47 | char modtab[ICB_MTABLEN][ICB_MAXNICKLEN];
|
---|
| 48 | int modtabcnt;
|
---|
[a785c27] | 49 | char srvname[MAXHOSTNAMELEN];
|
---|
| 50 | int creategroups;
|
---|
| 51 | int foreground;
|
---|
[3dba97d] | 52 | char logprefix[MAXPATHLEN/2];
|
---|
[460786f] | 53 | int dodns = 1;
|
---|
[3dba97d] | 54 | int dologging;
|
---|
[a785c27] | 55 | int verbose;
|
---|
[cd7b81d] | 56 |
|
---|
| 57 | void usage(void);
|
---|
| 58 | void getpeerinfo(struct icb_session *);
|
---|
| 59 | void icbd_accept(int, short, void *);
|
---|
[6e89d69] | 60 | void icbd_paused(int, short, void *);
|
---|
[cd7b81d] | 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);
|
---|
[7882a6f] | 67 | void icbd_send(struct icb_session *, char *, ssize_t);
|
---|
[cd7b81d] | 68 |
|
---|
[6e89d69] | 69 | struct icbd_listener {
|
---|
| 70 | struct event ev, pause;
|
---|
| 71 | };
|
---|
| 72 |
|
---|
[cd7b81d] | 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 */
|
---|
[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 |
|
---|
| 240 | void
|
---|
| 241 | icbd_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] | 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 |
|
---|
[cd7b81d] | 310 | __dead void
|
---|
| 311 | usage(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] | 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 | }
|
---|
[cd7b81d] | 338 |
|
---|
| 339 | void
|
---|
| 340 | icbd_dispatch(struct bufferevent *bev, void *arg)
|
---|
| 341 | {
|
---|
| 342 | struct icb_session *is = (struct icb_session *)arg;
|
---|
[a2fadb4] | 343 | unsigned char length;
|
---|
[cd7b81d] | 344 |
|
---|
[c9402c3] | 345 | while (EVBUFFER_LENGTH(EVBUFFER_INPUT(bev)) > 0) {
|
---|
[cd7b81d] | 346 | if (is->length == 0) {
|
---|
[c9402c3] | 347 | /* read length */
|
---|
[a2fadb4] | 348 | bufferevent_read(bev, &length, 1);
|
---|
| 349 | if (length == 0) {
|
---|
| 350 | /*
|
---|
| 351 | * An extension has been proposed:
|
---|
| 352 | * if length is 0, the packet is part of an
|
---|
| 353 | * "extended packet". The packet should be
|
---|
| 354 | * treated as if length was 255 and the next
|
---|
| 355 | * packet received from the sender should be
|
---|
| 356 | * appended to this packet.
|
---|
| 357 | *
|
---|
| 358 | * This server doesn't support this yet.
|
---|
| 359 | */
|
---|
[c9402c3] | 360 | icbd_drop(is, "invalid packet");
|
---|
| 361 | return;
|
---|
| 362 | }
|
---|
[a2fadb4] | 363 | is->length = (size_t)length;
|
---|
| 364 | is->rlen = 0;
|
---|
[cd7b81d] | 365 | }
|
---|
[c9402c3] | 366 | /* read as much as we can */
|
---|
| 367 | is->rlen += bufferevent_read(bev, &is->buffer[is->rlen],
|
---|
| 368 | is->length);
|
---|
[cd7b81d] | 369 | #ifdef DEBUG
|
---|
[c9402c3] | 370 | {
|
---|
| 371 | int i;
|
---|
| 372 |
|
---|
| 373 | printf("-> read %lu out of %lu from %s:%d:\n",
|
---|
| 374 | is->rlen, is->length, is->host, is->port);
|
---|
| 375 | for (i = 0; i < (int)is->rlen; i++)
|
---|
| 376 | printf(" %02x", (unsigned char)is->buffer[i]);
|
---|
| 377 | printf("\n");
|
---|
| 378 | }
|
---|
[cd7b81d] | 379 | #endif
|
---|
[c9402c3] | 380 | /* see you next time around */
|
---|
| 381 | if (is->rlen < is->length)
|
---|
| 382 | return;
|
---|
[a2fadb4] | 383 | /* null-terminate the data */
|
---|
| 384 | is->buffer[MIN(is->rlen, ICB_MSGSIZE - 1)] = '\0';
|
---|
[c9402c3] | 385 | /* process the message in full */
|
---|
| 386 | icb_input(is);
|
---|
| 387 | is->rlen = is->length = 0;
|
---|
| 388 | }
|
---|
[cd7b81d] | 389 | }
|
---|
| 390 |
|
---|
| 391 | void
|
---|
[7882a6f] | 392 | icbd_send(struct icb_session *is, char *buf, ssize_t size)
|
---|
[cd7b81d] | 393 | {
|
---|
| 394 | if (bufferevent_write(is->bev, buf, size) == -1)
|
---|
| 395 | syslog(LOG_ERR, "bufferevent_write: %m");
|
---|
| 396 | #ifdef DEBUG
|
---|
| 397 | {
|
---|
| 398 | int i;
|
---|
| 399 |
|
---|
[c9402c3] | 400 | printf("-> wrote %lu to %s:%d:\n", size, is->host, is->port);
|
---|
[cd7b81d] | 401 | for (i = 0; i < size; i++)
|
---|
| 402 | printf(" %02x", (unsigned char)buf[i]);
|
---|
| 403 | printf("\n");
|
---|
| 404 | }
|
---|
| 405 | #endif
|
---|
| 406 | }
|
---|
| 407 |
|
---|
| 408 | void
|
---|
| 409 | icbd_drop(struct icb_session *is, char *reason)
|
---|
| 410 | {
|
---|
| 411 | if (reason) {
|
---|
| 412 | icb_remove(is, reason);
|
---|
| 413 | icbd_log(is, LOG_DEBUG, reason);
|
---|
| 414 | } else
|
---|
| 415 | icb_remove(is, NULL);
|
---|
| 416 | (void)evbuffer_write(EVBUFFER_OUTPUT(is->bev), EVBUFFER_FD(is->bev));
|
---|
| 417 | (void)close(EVBUFFER_FD(is->bev));
|
---|
| 418 | bufferevent_free(is->bev);
|
---|
| 419 | free(is);
|
---|
| 420 | }
|
---|
| 421 |
|
---|
| 422 | void
|
---|
| 423 | icbd_log(struct icb_session *is, int level, const char *fmt, ...)
|
---|
| 424 | {
|
---|
| 425 | char buf[512];
|
---|
| 426 | va_list ap;
|
---|
| 427 |
|
---|
| 428 | if (!verbose && level == LOG_DEBUG)
|
---|
| 429 | return;
|
---|
| 430 |
|
---|
| 431 | va_start(ap, fmt);
|
---|
| 432 | (void)vsnprintf(buf, sizeof buf, fmt, ap);
|
---|
| 433 | va_end(ap);
|
---|
| 434 | if (is)
|
---|
| 435 | syslog(level, "%s:%u: %s", is->host, is->port, buf);
|
---|
| 436 | else
|
---|
| 437 | syslog(level, "%s", buf);
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 | void
|
---|
| 441 | icbd_restrict(void)
|
---|
| 442 | {
|
---|
| 443 | struct stat sb;
|
---|
| 444 | struct passwd *pw;
|
---|
| 445 |
|
---|
| 446 | if ((pw = getpwnam(ICBD_USER)) == NULL) {
|
---|
| 447 | syslog(LOG_ERR, "No passwd entry for %s", ICBD_USER);
|
---|
| 448 | exit(EX_NOUSER);
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | if (setusercontext(NULL, pw, pw->pw_uid,
|
---|
[d554223] | 452 | LOGIN_SETALL & ~LOGIN_SETUSER) < 0)
|
---|
[cd7b81d] | 453 | exit(EX_NOPERM);
|
---|
| 454 |
|
---|
| 455 | if (stat(pw->pw_dir, &sb) == -1) {
|
---|
| 456 | syslog(LOG_ERR, "%s: %m", pw->pw_name);
|
---|
| 457 | exit(EX_NOPERM);
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | if (sb.st_uid != 0 || (sb.st_mode & (S_IWGRP|S_IWOTH)) != 0) {
|
---|
| 461 | syslog(LOG_ERR, "bad directory permissions");
|
---|
| 462 | exit(EX_NOPERM);
|
---|
| 463 | }
|
---|
| 464 |
|
---|
| 465 | if (chroot(pw->pw_dir) < 0) {
|
---|
| 466 | syslog(LOG_ERR, "%s: %m", pw->pw_dir);
|
---|
| 467 | exit(EX_UNAVAILABLE);
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 | if (chdir("/") < 0) {
|
---|
| 471 | syslog(LOG_ERR, "/: %m");
|
---|
| 472 | exit(EX_UNAVAILABLE);
|
---|
| 473 | }
|
---|
| 474 |
|
---|
[e54f151] | 475 | if (setuid(pw->pw_uid) < 0) {
|
---|
| 476 | syslog(LOG_ERR, "%d: %m", pw->pw_uid);
|
---|
| 477 | exit(EX_NOPERM);
|
---|
| 478 | }
|
---|
| 479 |
|
---|
[cd7b81d] | 480 | (void)setproctitle("icbd");
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 | void
|
---|
| 484 | icbd_grplist(char *list)
|
---|
| 485 | {
|
---|
| 486 | char *s, *s1, *s2;
|
---|
| 487 | int last = 0;
|
---|
| 488 |
|
---|
| 489 | if (!list || strlen(list) == 0)
|
---|
| 490 | return;
|
---|
| 491 |
|
---|
| 492 | /* "group1[:pass1][,group2[:pass2],...]" */
|
---|
| 493 | s = list;
|
---|
| 494 | s1 = s2 = NULL;
|
---|
| 495 | while (!last && s) {
|
---|
| 496 | if ((s1 = strchr(s, ',')) != NULL)
|
---|
| 497 | *s1 = '\0';
|
---|
| 498 | else {
|
---|
| 499 | last = 1;
|
---|
| 500 | s1 = s;
|
---|
| 501 | }
|
---|
| 502 | if ((s2 = strchr(s, ':')) != NULL)
|
---|
| 503 | *s2 = '\0';
|
---|
| 504 | if (icb_addgroup(NULL, s, s2 ? ++s2 : NULL) == NULL)
|
---|
| 505 | err(EX_UNAVAILABLE, NULL);
|
---|
| 506 | s = ++s1;
|
---|
| 507 | s1 = s2 = NULL;
|
---|
| 508 | }
|
---|
| 509 | }
|
---|
| 510 |
|
---|
[1d2125a] | 511 | void
|
---|
[82d3c1f] | 512 | icbd_modupdate(void)
|
---|
[1d2125a] | 513 | {
|
---|
[82d3c1f] | 514 | struct stat st;
|
---|
[1d2125a] | 515 | FILE *fp;
|
---|
| 516 | char *buf, *lbuf;
|
---|
| 517 | size_t len;
|
---|
| 518 |
|
---|
[82d3c1f] | 519 | if (strlen(modtabpath) == 0)
|
---|
| 520 | return;
|
---|
| 521 | if (stat(modtabpath, &st)) {
|
---|
[709589d] | 522 | syslog(LOG_ERR, "stat %s: %m", modtabpath);
|
---|
[82d3c1f] | 523 | return;
|
---|
| 524 | }
|
---|
| 525 | /* see if there are any changes */
|
---|
| 526 | if (timespeccmp(&st.st_mtim, &modtabst.st_mtim, ==) ||
|
---|
| 527 | st.st_size == 0)
|
---|
| 528 | return;
|
---|
| 529 |
|
---|
[709589d] | 530 | if ((fp = fopen(modtabpath, "r")) == NULL) {
|
---|
| 531 | syslog(LOG_ERR, "open %s: %m", modtabpath);
|
---|
| 532 | return;
|
---|
| 533 | }
|
---|
[1d2125a] | 534 |
|
---|
[2cdab10] | 535 | modtabcnt = 0;
|
---|
[1d2125a] | 536 | bzero(modtab, ICB_MTABLEN * ICB_MAXNICKLEN);
|
---|
| 537 | lbuf = NULL;
|
---|
| 538 | while ((buf = fgetln(fp, &len)) && modtabcnt < ICB_MTABLEN) {
|
---|
| 539 | if (buf[len - 1] == '\n')
|
---|
| 540 | buf[len - 1] = '\0';
|
---|
| 541 | else {
|
---|
| 542 | /* EOF without EOL, copy and add the NUL */
|
---|
| 543 | if ((lbuf = malloc(len + 1)) == NULL)
|
---|
| 544 | err(1, NULL);
|
---|
| 545 | memcpy(lbuf, buf, len);
|
---|
| 546 | lbuf[len] = '\0';
|
---|
| 547 | buf = lbuf;
|
---|
| 548 | }
|
---|
| 549 | while (buf[0] == ' ' || buf[0] == '\t')
|
---|
| 550 | buf++;
|
---|
| 551 | if (buf[0] == '#' || buf[0] == '\0')
|
---|
| 552 | continue;
|
---|
| 553 | strlcpy(modtab[modtabcnt++], buf, ICB_MAXNICKLEN);
|
---|
| 554 | }
|
---|
| 555 | free(lbuf);
|
---|
| 556 |
|
---|
| 557 | qsort(modtab, modtabcnt, ICB_MAXNICKLEN,
|
---|
| 558 | (int (*)(const void *, const void *))strcmp);
|
---|
| 559 |
|
---|
| 560 | fclose(fp);
|
---|
[709589d] | 561 |
|
---|
| 562 | memcpy(&modtabst, &st, sizeof modtabst);
|
---|
[1d2125a] | 563 | }
|
---|
| 564 |
|
---|
[cd7b81d] | 565 | time_t
|
---|
| 566 | getmonotime(void)
|
---|
| 567 | {
|
---|
| 568 | struct timespec ts;
|
---|
| 569 |
|
---|
| 570 | if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
|
---|
| 571 | syslog(LOG_ERR, "%m");
|
---|
| 572 | exit(EX_OSERR);
|
---|
| 573 | }
|
---|
| 574 | return (ts.tv_sec);
|
---|
| 575 | }
|
---|
| 576 |
|
---|
| 577 | void
|
---|
| 578 | getpeerinfo(struct icb_session *is)
|
---|
| 579 | {
|
---|
| 580 | struct sockaddr_storage ss;
|
---|
[b4049f9] | 581 | struct sockaddr_in *sin = (struct sockaddr_in *)&ss;
|
---|
| 582 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&ss;
|
---|
[cd7b81d] | 583 | socklen_t ss_len = sizeof ss;
|
---|
| 584 |
|
---|
| 585 | bzero(&ss, sizeof ss);
|
---|
| 586 | if (getpeername(EVBUFFER_FD(is->bev), (struct sockaddr *)&ss,
|
---|
| 587 | &ss_len) != 0)
|
---|
| 588 | return;
|
---|
| 589 |
|
---|
| 590 | is->port = 0;
|
---|
| 591 | switch (ss.ss_family) {
|
---|
| 592 | case AF_INET:
|
---|
[b4049f9] | 593 | is->port = ntohs(sin->sin_port);
|
---|
[cd7b81d] | 594 | break;
|
---|
| 595 |
|
---|
| 596 | case AF_INET6:
|
---|
[b4049f9] | 597 | is->port = ntohs(sin6->sin6_port);
|
---|
[cd7b81d] | 598 | break;
|
---|
| 599 | }
|
---|
| 600 |
|
---|
[b4049f9] | 601 | inet_ntop(ss.ss_family, ss.ss_family == AF_INET ?
|
---|
| 602 | (void *)&sin->sin_addr : (void *)&sin6->sin6_addr,
|
---|
| 603 | is->host, sizeof is->host);
|
---|
| 604 |
|
---|
[e87ab6d] | 605 | dns_rresolv(is, (struct sockaddr *)&ss);
|
---|
[cd7b81d] | 606 | }
|
---|