source: code/dns.c@ bacf9da

Last change on this file since bacf9da was a76f866, checked in by Mike Belopuhov <mike@…>, 11 years ago

Improve the read loop in dns_dispatch before we switch to bufferevents

  • Property mode set to 100644
File size: 4.4 KB
Line 
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
38void dns_dispatch(int, short, void *);
39void dns_done(int, short, void *);
40
41struct icbd_dnsquery {
42 uint64_t sid;
43 union {
44 struct sockaddr_storage req;
45 char rep[NI_MAXHOST];
46 } u;
47};
48
49int dns_pipe;
50
51extern int dodns;
52
53int
54dns_init(void)
55{
56 static struct event ev;
57 struct passwd *pw;
58 int pipes[2];
59
60 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipes) == -1) {
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:
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 }
83 return (0);
84 }
85
86 setproctitle("dns resolver");
87 close(pipes[0]);
88
89 if ((pw = getpwnam(ICBD_USER)) == NULL) {
90 syslog(LOG_ERR, "No passwd entry for %s", ICBD_USER);
91 exit(EX_NOUSER);
92 }
93
94 if (chdir("/") < 0) {
95 syslog(LOG_ERR, "chdir: %m");
96 exit(EX_UNAVAILABLE);
97 }
98
99 if (setusercontext(NULL, pw, pw->pw_uid,
100 LOGIN_SETALL & ~LOGIN_SETUSER) < 0)
101 exit(EX_NOPERM);
102
103 if (setuid(pw->pw_uid) < 0) {
104 syslog(LOG_ERR, "%d: %m", pw->pw_uid);
105 exit(EX_NOPERM);
106 }
107
108 event_init();
109
110 /* event for the request */
111 event_set(&ev, pipes[1], EV_READ | EV_PERSIST, dns_dispatch, NULL);
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
120void
121dns_dispatch(int fd, short event, void *arg __attribute__((unused)))
122{
123 char host[NI_MAXHOST];
124 struct sockaddr *sa;
125 struct icbd_dnsquery q;
126 ssize_t res;
127 int gerr;
128
129 if (event != EV_READ)
130 return;
131
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) {
138 syslog(LOG_ERR, "dns read: %m");
139 /* disable dns resolver */
140 dodns = 0;
141 return;
142 }
143
144 sa = (struct sockaddr *)&q.u.req;
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
154 memcpy(&q.u.rep, host, sizeof host);
155 if (write(fd, &q, sizeof q) != sizeof q)
156 syslog(LOG_ERR, "dns write: %m");
157}
158
159void
160dns_done(int fd, short event, void *arg __attribute__((unused)))
161{
162 struct icb_session *is;
163 struct icbd_dnsquery q;
164
165 if (event != EV_READ)
166 return;
167
168 if (read(fd, &q, sizeof q) != sizeof q) {
169 syslog(LOG_ERR, "read: %m");
170 return;
171 }
172
173 if ((is = icbd_session_lookup(q.sid)) == NULL) {
174 syslog(LOG_ERR, "failed to find session %llu", q.sid);
175 return;
176 }
177
178 if (verbose)
179 syslog(LOG_DEBUG, "icbd_dns: resolved %s to %s",
180 is->host, q.u.rep);
181
182 /* XXX */
183 if (strcmp(q.u.rep, "localhost") == 0)
184 strlcpy(is->host, "unknown", ICB_MAXHOSTLEN);
185 else if (strlen(q.u.rep) < ICB_MAXHOSTLEN)
186 strlcpy(is->host, q.u.rep, ICB_MAXHOSTLEN);
187}
188
189void
190dns_rresolv(struct icb_session *is, struct sockaddr_storage *ss)
191{
192 struct icbd_dnsquery q;
193
194 if (!dodns)
195 return;
196
197 if (verbose)
198 syslog(LOG_DEBUG, "resolving: %s", is->host);
199
200 memset(&q, 0, sizeof q);
201 q.sid = is->id;
202 memcpy(&q.u.req, ss, sizeof *ss);
203 if (write(dns_pipe, &q, sizeof q) != sizeof q) {
204 syslog(LOG_ERR, "write: %m");
205 exit(EX_OSERR);
206 }
207}
Note: See TracBrowser for help on using the repository browser.