[cd7b81d] | 1 | /*
|
---|
| 2 | * Copyright (c) 2009 Michael Shalayeff
|
---|
| 3 | * All rights reserved.
|
---|
| 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 MIND, USE, DATA OR PROFITS, WHETHER IN
|
---|
| 14 | * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
---|
| 15 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
---|
| 16 | */
|
---|
| 17 |
|
---|
| 18 | #include <sys/param.h>
|
---|
| 19 | #include <sys/socket.h>
|
---|
| 20 | #include <sys/time.h>
|
---|
| 21 | #include <netinet/in.h>
|
---|
| 22 | #include <arpa/inet.h>
|
---|
| 23 | #include <errno.h>
|
---|
| 24 | #include <stdlib.h>
|
---|
| 25 | #include <unistd.h>
|
---|
| 26 | #include <syslog.h>
|
---|
| 27 | #include <sysexits.h>
|
---|
| 28 | #include <login_cap.h>
|
---|
| 29 | #include <event.h>
|
---|
| 30 | #include <pwd.h>
|
---|
| 31 | #include <netdb.h>
|
---|
| 32 |
|
---|
| 33 | #include "icb.h"
|
---|
| 34 | #include "icbd.h"
|
---|
| 35 |
|
---|
| 36 | void dns_dispatch(int, short, void *);
|
---|
| 37 | int dns_pipe;
|
---|
| 38 |
|
---|
| 39 | int
|
---|
| 40 | icbd_dns_init(void)
|
---|
| 41 | {
|
---|
| 42 | struct event ev;
|
---|
| 43 | int pipe[2];
|
---|
| 44 | struct passwd *pw;
|
---|
| 45 |
|
---|
| 46 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1) {
|
---|
| 47 | syslog(LOG_ERR, "socketpair: %m");
|
---|
| 48 | exit(EX_OSERR);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | switch (fork()) {
|
---|
| 52 | case -1:
|
---|
| 53 | syslog(LOG_ERR, "fork: %m");
|
---|
| 54 | exit(EX_OSERR);
|
---|
| 55 | case 0:
|
---|
| 56 | break;
|
---|
| 57 |
|
---|
| 58 | default:
|
---|
| 59 | close(pipe[1]);
|
---|
| 60 | dns_pipe = pipe[0];
|
---|
| 61 | return (0);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | setproctitle("dns resolver");
|
---|
| 65 | close(pipe[0]);
|
---|
| 66 |
|
---|
| 67 | if ((pw = getpwnam(ICBD_USER)) == NULL) {
|
---|
| 68 | syslog(LOG_ERR, "No passwd entry for %s", ICBD_USER);
|
---|
| 69 | exit(EX_NOUSER);
|
---|
| 70 | }
|
---|
| 71 | if (setusercontext(NULL, pw, pw->pw_uid,
|
---|
| 72 | LOGIN_SETALL & ~LOGIN_SETUSER) < 0) {
|
---|
| 73 | syslog(LOG_ERR, "%s:%m", pw->pw_name);
|
---|
| 74 | exit(EX_NOPERM);
|
---|
| 75 | }
|
---|
| 76 | if (setuid(pw->pw_uid) < 0) {
|
---|
| 77 | syslog(LOG_ERR, "%d:%m", pw->pw_uid);
|
---|
| 78 | exit(EX_NOPERM);
|
---|
| 79 | }
|
---|
| 80 | if (chdir("/") < 0) {
|
---|
| 81 | syslog(LOG_ERR, "chdir: %m");
|
---|
| 82 | exit(EX_UNAVAILABLE);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | event_init();
|
---|
| 86 |
|
---|
| 87 | event_set(&ev, pipe[1], EV_READ | EV_PERSIST, dns_dispatch, NULL);
|
---|
| 88 | if (event_add(&ev, NULL) < 0) {
|
---|
| 89 | syslog(LOG_ERR, "event_add: %m");
|
---|
| 90 | exit (EX_UNAVAILABLE);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | return event_dispatch();
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | void
|
---|
| 97 | dns_dispatch(int fd, short event, void *arg)
|
---|
| 98 | {
|
---|
| 99 | char host[NI_MAXHOST];
|
---|
| 100 | struct sockaddr_storage ss;
|
---|
| 101 | struct sockaddr *sa = (struct sockaddr *)&ss;
|
---|
| 102 | int gerr, ss_len = sizeof ss;
|
---|
| 103 |
|
---|
| 104 | arg = NULL;
|
---|
| 105 | if (event != EV_READ)
|
---|
| 106 | return;
|
---|
| 107 |
|
---|
| 108 | if (read(fd, &ss, ss_len) != ss_len) {
|
---|
| 109 | syslog(LOG_ERR, "dns read: %m");
|
---|
| 110 | exit(1);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | if ((gerr = getnameinfo(sa, sa->sa_len,
|
---|
| 114 | host, sizeof host, NULL, 0, NI_NOFQDN))) {
|
---|
| 115 | syslog(LOG_ERR, "getnameinfo: %s", gai_strerror(gerr));
|
---|
| 116 | write(fd, host, sizeof host);
|
---|
| 117 | return;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | if (verbose)
|
---|
| 121 | syslog(LOG_DEBUG, "dns_dispatch: resolved %s", host);
|
---|
| 122 |
|
---|
| 123 | if (write(fd, host, sizeof host) != sizeof host)
|
---|
| 124 | syslog(LOG_ERR, "dns write: %m");
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | int
|
---|
| 128 | dns_rresolv(struct icb_session *is, struct sockaddr_storage *ss)
|
---|
| 129 | {
|
---|
| 130 | /* one-shot event for the reply */
|
---|
| 131 | event_set(&is->ev, dns_pipe, EV_READ, icbd_dns, is);
|
---|
| 132 | if (event_add(&is->ev, NULL) < 0) {
|
---|
| 133 | syslog(LOG_ERR, "event_add: %m");
|
---|
| 134 | exit (EX_UNAVAILABLE);
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | inet_ntop(ss->ss_family, ss->ss_family == AF_INET ?
|
---|
| 138 | (void *)&((struct sockaddr_in *)ss)->sin_addr :
|
---|
| 139 | (void *)&((struct sockaddr_in6 *)ss)->sin6_addr,
|
---|
| 140 | is->host, sizeof is->host);
|
---|
| 141 |
|
---|
| 142 | if (verbose)
|
---|
| 143 | syslog(LOG_DEBUG, "resolving: %s", is->host);
|
---|
| 144 |
|
---|
| 145 | if (write(dns_pipe, ss, sizeof *ss) != sizeof *ss) {
|
---|
| 146 | syslog(LOG_ERR, "write: %m");
|
---|
| 147 | exit (EX_OSERR);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | return 0;
|
---|
| 151 | }
|
---|