1 | /*
|
---|
2 | * Copyright (c) 2014 Mike Belopuhov
|
---|
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>
|
---|
26 | #include <string.h>
|
---|
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 *);
|
---|
39 | void dns_done(int, short, void *);
|
---|
40 |
|
---|
41 | int dns_pipe;
|
---|
42 |
|
---|
43 | struct icbd_dnsquery {
|
---|
44 | uint64_t sid;
|
---|
45 | union {
|
---|
46 | struct sockaddr_storage req;
|
---|
47 | char rep[MAXHOSTNAMELEN];
|
---|
48 | } u;
|
---|
49 | };
|
---|
50 |
|
---|
51 | int
|
---|
52 | dns_init(void)
|
---|
53 | {
|
---|
54 | static struct event ev;
|
---|
55 | struct passwd *pw;
|
---|
56 | int pipes[2];
|
---|
57 |
|
---|
58 | if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipes) == -1) {
|
---|
59 | syslog(LOG_ERR, "socketpair: %m");
|
---|
60 | exit(EX_OSERR);
|
---|
61 | }
|
---|
62 |
|
---|
63 | switch (fork()) {
|
---|
64 | case -1:
|
---|
65 | syslog(LOG_ERR, "fork: %m");
|
---|
66 | exit(EX_OSERR);
|
---|
67 | case 0:
|
---|
68 | break;
|
---|
69 |
|
---|
70 | default:
|
---|
71 | close(pipes[1]);
|
---|
72 | dns_pipe = pipes[0];
|
---|
73 |
|
---|
74 | /* event for the reply */
|
---|
75 | event_set(&ev, dns_pipe, EV_READ | EV_PERSIST,
|
---|
76 | dns_done, NULL);
|
---|
77 | if (event_add(&ev, NULL) < 0) {
|
---|
78 | syslog(LOG_ERR, "event_add: %m");
|
---|
79 | exit (EX_UNAVAILABLE);
|
---|
80 | }
|
---|
81 | return (0);
|
---|
82 | }
|
---|
83 |
|
---|
84 | setproctitle("dns resolver");
|
---|
85 | close(pipes[0]);
|
---|
86 |
|
---|
87 | if ((pw = getpwnam(ICBD_USER)) == NULL) {
|
---|
88 | syslog(LOG_ERR, "No passwd entry for %s", ICBD_USER);
|
---|
89 | exit(EX_NOUSER);
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (chdir("/") < 0) {
|
---|
93 | syslog(LOG_ERR, "chdir: %m");
|
---|
94 | exit(EX_UNAVAILABLE);
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (setusercontext(NULL, pw, pw->pw_uid,
|
---|
98 | LOGIN_SETALL & ~LOGIN_SETUSER) < 0)
|
---|
99 | exit(EX_NOPERM);
|
---|
100 |
|
---|
101 | if (setuid(pw->pw_uid) < 0) {
|
---|
102 | syslog(LOG_ERR, "%d: %m", pw->pw_uid);
|
---|
103 | exit(EX_NOPERM);
|
---|
104 | }
|
---|
105 |
|
---|
106 | event_init();
|
---|
107 |
|
---|
108 | /* event for the request */
|
---|
109 | event_set(&ev, pipes[1], EV_READ | EV_PERSIST, dns_dispatch, NULL);
|
---|
110 | if (event_add(&ev, NULL) < 0) {
|
---|
111 | syslog(LOG_ERR, "event_add: %m");
|
---|
112 | exit (EX_UNAVAILABLE);
|
---|
113 | }
|
---|
114 |
|
---|
115 | return event_dispatch();
|
---|
116 | }
|
---|
117 |
|
---|
118 | void
|
---|
119 | dns_dispatch(int fd, short event, void *arg __attribute__((unused)))
|
---|
120 | {
|
---|
121 | char host[NI_MAXHOST];
|
---|
122 | struct sockaddr *sa;
|
---|
123 | struct icbd_dnsquery q;
|
---|
124 | int gerr;
|
---|
125 |
|
---|
126 | if (event != EV_READ)
|
---|
127 | return;
|
---|
128 |
|
---|
129 | if (read(fd, &q, sizeof q) != sizeof q) {
|
---|
130 | syslog(LOG_ERR, "dns read: %m");
|
---|
131 | exit(EX_DATAERR);
|
---|
132 | }
|
---|
133 |
|
---|
134 | sa = (struct sockaddr *)&q.u.req;
|
---|
135 | if ((gerr = getnameinfo(sa, sa->sa_len,
|
---|
136 | host, sizeof host, NULL, 0, NI_NOFQDN))) {
|
---|
137 | syslog(LOG_ERR, "getnameinfo: %s", gai_strerror(gerr));
|
---|
138 | return;
|
---|
139 | }
|
---|
140 |
|
---|
141 | if (verbose)
|
---|
142 | syslog(LOG_DEBUG, "dns_dispatch: resolved %s", host);
|
---|
143 |
|
---|
144 | memcpy(&q.u.rep, host, sizeof host);
|
---|
145 | if (write(fd, &q, sizeof q) != sizeof q)
|
---|
146 | syslog(LOG_ERR, "dns write: %m");
|
---|
147 | }
|
---|
148 |
|
---|
149 | void
|
---|
150 | dns_done(int fd, short event, void *arg __attribute__((unused)))
|
---|
151 | {
|
---|
152 | struct icb_session *is;
|
---|
153 | struct icbd_dnsquery q;
|
---|
154 |
|
---|
155 | if (event != EV_READ)
|
---|
156 | return;
|
---|
157 |
|
---|
158 | if (read(fd, &q, sizeof q) != sizeof q) {
|
---|
159 | syslog(LOG_ERR, "read: %m");
|
---|
160 | return;
|
---|
161 | }
|
---|
162 |
|
---|
163 | if ((is = icbd_session_lookup(q.sid)) == NULL) {
|
---|
164 | syslog(LOG_ERR, "failed to find session %llu", q.sid);
|
---|
165 | return;
|
---|
166 | }
|
---|
167 |
|
---|
168 | memcpy(is->host, q.u.rep, MAXHOSTNAMELEN);
|
---|
169 | is->host[sizeof is->host - 1] = '\0';
|
---|
170 |
|
---|
171 | if (verbose)
|
---|
172 | syslog(LOG_DEBUG, "icbd_dns: resolved %s", is->host);
|
---|
173 | }
|
---|
174 |
|
---|
175 | int
|
---|
176 | dns_rresolv(struct icb_session *is, struct sockaddr_storage *ss)
|
---|
177 | {
|
---|
178 | struct icbd_dnsquery q;
|
---|
179 |
|
---|
180 | if (verbose)
|
---|
181 | syslog(LOG_DEBUG, "resolving: %s", is->host);
|
---|
182 |
|
---|
183 | memset(&q, 0, sizeof q);
|
---|
184 | q.sid = is->id;
|
---|
185 | memcpy(&q.u.req, ss, sizeof *ss);
|
---|
186 | if (write(dns_pipe, &q, sizeof q) != sizeof q) {
|
---|
187 | syslog(LOG_ERR, "write: %m");
|
---|
188 | exit (EX_OSERR);
|
---|
189 | }
|
---|
190 |
|
---|
191 | return 0;
|
---|
192 | }
|
---|