[cd7b81d] | 1 | /*
|
---|
[626f420] | 2 | * Copyright (c) 2014 Mike Belopuhov
|
---|
[cd7b81d] | 3 | * Copyright (c) 2009 Michael Shalayeff
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Permission to use, copy, modify, and distribute this software for any
|
---|
| 7 | * purpose with or without fee is hereby granted, provided that the above
|
---|
| 8 | * copyright notice and this permission notice appear in all copies.
|
---|
| 9 | *
|
---|
| 10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
---|
| 11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
---|
| 12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
---|
| 13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
---|
| 14 | * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
|
---|
| 15 | * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
---|
| 16 | * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
---|
| 17 | */
|
---|
| 18 |
|
---|
| 19 | #include <sys/param.h>
|
---|
| 20 | #include <sys/socket.h>
|
---|
| 21 | #include <sys/time.h>
|
---|
| 22 | #include <netinet/in.h>
|
---|
| 23 | #include <arpa/inet.h>
|
---|
| 24 | #include <errno.h>
|
---|
| 25 | #include <stdlib.h>
|
---|
[b4049f9] | 26 | #include <string.h>
|
---|
[cd7b81d] | 27 | #include <unistd.h>
|
---|
| 28 | #include <syslog.h>
|
---|
| 29 | #include <sysexits.h>
|
---|
| 30 | #include <login_cap.h>
|
---|
| 31 | #include <event.h>
|
---|
| 32 | #include <pwd.h>
|
---|
| 33 | #include <netdb.h>
|
---|
| 34 |
|
---|
| 35 | #include "icb.h"
|
---|
| 36 | #include "icbd.h"
|
---|
| 37 |
|
---|
| 38 | void dns_dispatch(int, short, void *);
|
---|
[b4049f9] | 39 | void dns_done(int, short, void *);
|
---|
[55923b7] | 40 |
|
---|
[b4049f9] | 41 | struct icbd_dnsquery {
|
---|
| 42 | uint64_t sid;
|
---|
| 43 | union {
|
---|
| 44 | struct sockaddr_storage req;
|
---|
[7289823] | 45 | char rep[NI_MAXHOST];
|
---|
[b4049f9] | 46 | } u;
|
---|
| 47 | };
|
---|
| 48 |
|
---|
[460786f] | 49 | int dns_pipe;
|
---|
| 50 |
|
---|
| 51 | extern int dodns;
|
---|
| 52 |
|
---|
[cd7b81d] | 53 | int
|
---|
[b4049f9] | 54 | dns_init(void)
|
---|
[cd7b81d] | 55 | {
|
---|
[b4049f9] | 56 | static struct event ev;
|
---|
| 57 | struct passwd *pw;
|
---|
| 58 | int pipes[2];
|
---|
[cd7b81d] | 59 |
|
---|
[b4049f9] | 60 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipes) == -1) {
|
---|
[cd7b81d] | 61 | syslog(LOG_ERR, "socketpair: %m");
|
---|
| 62 | exit(EX_OSERR);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | switch (fork()) {
|
---|
| 66 | case -1:
|
---|
| 67 | syslog(LOG_ERR, "fork: %m");
|
---|
| 68 | exit(EX_OSERR);
|
---|
| 69 | case 0:
|
---|
| 70 | break;
|
---|
| 71 |
|
---|
| 72 | default:
|
---|
[b4049f9] | 73 | close(pipes[1]);
|
---|
| 74 | dns_pipe = pipes[0];
|
---|
| 75 |
|
---|
| 76 | /* event for the reply */
|
---|
| 77 | event_set(&ev, dns_pipe, EV_READ | EV_PERSIST,
|
---|
| 78 | dns_done, NULL);
|
---|
| 79 | if (event_add(&ev, NULL) < 0) {
|
---|
| 80 | syslog(LOG_ERR, "event_add: %m");
|
---|
| 81 | exit (EX_UNAVAILABLE);
|
---|
| 82 | }
|
---|
[cd7b81d] | 83 | return (0);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | setproctitle("dns resolver");
|
---|
[b4049f9] | 87 | close(pipes[0]);
|
---|
[cd7b81d] | 88 |
|
---|
| 89 | if ((pw = getpwnam(ICBD_USER)) == NULL) {
|
---|
| 90 | syslog(LOG_ERR, "No passwd entry for %s", ICBD_USER);
|
---|
| 91 | exit(EX_NOUSER);
|
---|
| 92 | }
|
---|
[d554223] | 93 |
|
---|
[e54f151] | 94 | if (chdir("/") < 0) {
|
---|
| 95 | syslog(LOG_ERR, "chdir: %m");
|
---|
| 96 | exit(EX_UNAVAILABLE);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[cd7b81d] | 99 | if (setusercontext(NULL, pw, pw->pw_uid,
|
---|
[d554223] | 100 | LOGIN_SETALL & ~LOGIN_SETUSER) < 0)
|
---|
[cd7b81d] | 101 | exit(EX_NOPERM);
|
---|
[d554223] | 102 |
|
---|
[cd7b81d] | 103 | if (setuid(pw->pw_uid) < 0) {
|
---|
[d554223] | 104 | syslog(LOG_ERR, "%d: %m", pw->pw_uid);
|
---|
[cd7b81d] | 105 | exit(EX_NOPERM);
|
---|
| 106 | }
|
---|
[d554223] | 107 |
|
---|
[cd7b81d] | 108 | event_init();
|
---|
| 109 |
|
---|
[b4049f9] | 110 | /* event for the request */
|
---|
| 111 | event_set(&ev, pipes[1], EV_READ | EV_PERSIST, dns_dispatch, NULL);
|
---|
[cd7b81d] | 112 | if (event_add(&ev, NULL) < 0) {
|
---|
| 113 | syslog(LOG_ERR, "event_add: %m");
|
---|
| 114 | exit (EX_UNAVAILABLE);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | return event_dispatch();
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | void
|
---|
[b4049f9] | 121 | dns_dispatch(int fd, short event, void *arg __attribute__((unused)))
|
---|
[cd7b81d] | 122 | {
|
---|
| 123 | char host[NI_MAXHOST];
|
---|
[b4049f9] | 124 | struct sockaddr *sa;
|
---|
| 125 | struct icbd_dnsquery q;
|
---|
[a76f866] | 126 | ssize_t res;
|
---|
[b4049f9] | 127 | int gerr;
|
---|
[cd7b81d] | 128 |
|
---|
| 129 | if (event != EV_READ)
|
---|
| 130 | return;
|
---|
| 131 |
|
---|
[a76f866] | 132 | do
|
---|
| 133 | res = read(fd, &q, sizeof q);
|
---|
| 134 | while (res == -1 && errno == EINTR);
|
---|
| 135 | if (res == -1 && errno == EAGAIN)
|
---|
| 136 | return;
|
---|
| 137 | if (res < (ssize_t)sizeof q) {
|
---|
[9c04f2a] | 138 | syslog(LOG_ERR, "dns_dispatch read: %m");
|
---|
[a76f866] | 139 | /* disable dns resolver */
|
---|
| 140 | dodns = 0;
|
---|
| 141 | return;
|
---|
[cd7b81d] | 142 | }
|
---|
| 143 |
|
---|
[b4049f9] | 144 | sa = (struct sockaddr *)&q.u.req;
|
---|
[cd7b81d] | 145 | if ((gerr = getnameinfo(sa, sa->sa_len,
|
---|
| 146 | host, sizeof host, NULL, 0, NI_NOFQDN))) {
|
---|
| 147 | syslog(LOG_ERR, "getnameinfo: %s", gai_strerror(gerr));
|
---|
| 148 | return;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | if (verbose)
|
---|
| 152 | syslog(LOG_DEBUG, "dns_dispatch: resolved %s", host);
|
---|
| 153 |
|
---|
[b4049f9] | 154 | memcpy(&q.u.rep, host, sizeof host);
|
---|
| 155 | if (write(fd, &q, sizeof q) != sizeof q)
|
---|
[9c04f2a] | 156 | syslog(LOG_ERR, "dns_dispatch write: %m");
|
---|
[cd7b81d] | 157 | }
|
---|
| 158 |
|
---|
[b4049f9] | 159 | void
|
---|
| 160 | dns_done(int fd, short event, void *arg __attribute__((unused)))
|
---|
[cd7b81d] | 161 | {
|
---|
[b4049f9] | 162 | struct icb_session *is;
|
---|
| 163 | struct icbd_dnsquery q;
|
---|
[9c04f2a] | 164 | ssize_t res;
|
---|
[b4049f9] | 165 |
|
---|
| 166 | if (event != EV_READ)
|
---|
| 167 | return;
|
---|
| 168 |
|
---|
[9c04f2a] | 169 | do
|
---|
| 170 | res = read(fd, &q, sizeof q);
|
---|
| 171 | while (res == -1 && errno == EINTR);
|
---|
| 172 | if (res == -1 && errno == EAGAIN)
|
---|
| 173 | return;
|
---|
| 174 | if (res < (ssize_t)sizeof q) {
|
---|
| 175 | syslog(LOG_ERR, "dns_done read: %m");
|
---|
[b4049f9] | 176 | return;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | if ((is = icbd_session_lookup(q.sid)) == NULL) {
|
---|
[145faa6] | 180 | syslog(LOG_DEBUG, "failed to find session %llu", q.sid);
|
---|
[b4049f9] | 181 | return;
|
---|
[cd7b81d] | 182 | }
|
---|
| 183 |
|
---|
[b4049f9] | 184 | if (verbose)
|
---|
[7289823] | 185 | syslog(LOG_DEBUG, "icbd_dns: resolved %s to %s",
|
---|
| 186 | is->host, q.u.rep);
|
---|
| 187 |
|
---|
| 188 | /* XXX */
|
---|
| 189 | if (strcmp(q.u.rep, "localhost") == 0)
|
---|
| 190 | strlcpy(is->host, "unknown", ICB_MAXHOSTLEN);
|
---|
| 191 | else if (strlen(q.u.rep) < ICB_MAXHOSTLEN)
|
---|
| 192 | strlcpy(is->host, q.u.rep, ICB_MAXHOSTLEN);
|
---|
[b4049f9] | 193 | }
|
---|
| 194 |
|
---|
[460786f] | 195 | void
|
---|
[b4049f9] | 196 | dns_rresolv(struct icb_session *is, struct sockaddr_storage *ss)
|
---|
| 197 | {
|
---|
| 198 | struct icbd_dnsquery q;
|
---|
[cd7b81d] | 199 |
|
---|
[460786f] | 200 | if (!dodns)
|
---|
| 201 | return;
|
---|
| 202 |
|
---|
[cd7b81d] | 203 | if (verbose)
|
---|
| 204 | syslog(LOG_DEBUG, "resolving: %s", is->host);
|
---|
| 205 |
|
---|
[b4049f9] | 206 | memset(&q, 0, sizeof q);
|
---|
| 207 | q.sid = is->id;
|
---|
| 208 | memcpy(&q.u.req, ss, sizeof *ss);
|
---|
| 209 | if (write(dns_pipe, &q, sizeof q) != sizeof q) {
|
---|
[9c04f2a] | 210 | syslog(LOG_ERR, "dns_rresolv write: %m");
|
---|
[460786f] | 211 | exit(EX_OSERR);
|
---|
[cd7b81d] | 212 | }
|
---|
| 213 | }
|
---|