source: code/dns.c@ b4049f9

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

Rewrite DNS resolver to do things truly asynchronously

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[cd7b81d]1/*
[b4049f9]2 * Copyright (c) 2013 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
38void dns_dispatch(int, short, void *);
[b4049f9]39void dns_done(int, short, void *);
[cd7b81d]40int dns_pipe;
41
[b4049f9]42
43struct icbd_dnsquery {
44 uint64_t sid;
45 union {
46 struct sockaddr_storage req;
47 char rep[MAXHOSTNAMELEN];
48 } u;
49};
50
[cd7b81d]51int
[b4049f9]52dns_init(void)
[cd7b81d]53{
[b4049f9]54 static struct event ev;
55 struct passwd *pw;
56 int pipes[2];
[cd7b81d]57
[b4049f9]58 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipes) == -1) {
[cd7b81d]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:
[b4049f9]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 }
[cd7b81d]81 return (0);
82 }
83
84 setproctitle("dns resolver");
[b4049f9]85 close(pipes[0]);
[cd7b81d]86
87 if ((pw = getpwnam(ICBD_USER)) == NULL) {
88 syslog(LOG_ERR, "No passwd entry for %s", ICBD_USER);
89 exit(EX_NOUSER);
90 }
[d554223]91
[cd7b81d]92 if (setusercontext(NULL, pw, pw->pw_uid,
[d554223]93 LOGIN_SETALL & ~LOGIN_SETUSER) < 0)
[cd7b81d]94 exit(EX_NOPERM);
[d554223]95
[cd7b81d]96 if (setuid(pw->pw_uid) < 0) {
[d554223]97 syslog(LOG_ERR, "%d: %m", pw->pw_uid);
[cd7b81d]98 exit(EX_NOPERM);
99 }
[d554223]100
[cd7b81d]101 if (chdir("/") < 0) {
102 syslog(LOG_ERR, "chdir: %m");
103 exit(EX_UNAVAILABLE);
104 }
105
106 event_init();
107
[b4049f9]108 /* event for the request */
109 event_set(&ev, pipes[1], EV_READ | EV_PERSIST, dns_dispatch, NULL);
[cd7b81d]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
118void
[b4049f9]119dns_dispatch(int fd, short event, void *arg __attribute__((unused)))
[cd7b81d]120{
121 char host[NI_MAXHOST];
[b4049f9]122 struct sockaddr *sa;
123 struct icbd_dnsquery q;
124 int gerr;
[cd7b81d]125
126 arg = NULL;
127 if (event != EV_READ)
128 return;
129
[b4049f9]130 if (read(fd, &q, sizeof q) != sizeof q) {
[cd7b81d]131 syslog(LOG_ERR, "dns read: %m");
132 exit(1);
133 }
134
[b4049f9]135 sa = (struct sockaddr *)&q.u.req;
[cd7b81d]136 if ((gerr = getnameinfo(sa, sa->sa_len,
137 host, sizeof host, NULL, 0, NI_NOFQDN))) {
138 syslog(LOG_ERR, "getnameinfo: %s", gai_strerror(gerr));
139 return;
140 }
141
142 if (verbose)
143 syslog(LOG_DEBUG, "dns_dispatch: resolved %s", host);
144
[b4049f9]145 memcpy(&q.u.rep, host, sizeof host);
146 if (write(fd, &q, sizeof q) != sizeof q)
[cd7b81d]147 syslog(LOG_ERR, "dns write: %m");
148}
149
[b4049f9]150void
151dns_done(int fd, short event, void *arg __attribute__((unused)))
[cd7b81d]152{
[b4049f9]153 struct icb_session *is;
154 struct icbd_dnsquery q;
155
156 if (event != EV_READ)
157 return;
158
159 if (read(fd, &q, sizeof q) != sizeof q) {
160 syslog(LOG_ERR, "read: %m");
161 return;
162 }
163
164 if ((is = icbd_session_lookup(q.sid)) == NULL) {
165 syslog(LOG_ERR, "failed to find session %llu", q.sid);
166 return;
[cd7b81d]167 }
168
[b4049f9]169 memcpy(is->host, q.u.rep, MAXHOSTNAMELEN);
170 is->host[sizeof is->host - 1] = '\0';
171
172 if (verbose)
173 syslog(LOG_DEBUG, "icbd_dns: resolved %s", is->host);
174}
175
176int
177dns_rresolv(struct icb_session *is, struct sockaddr_storage *ss)
178{
179 struct icbd_dnsquery q;
[cd7b81d]180
181 if (verbose)
182 syslog(LOG_DEBUG, "resolving: %s", is->host);
183
[b4049f9]184 memset(&q, 0, sizeof q);
185 q.sid = is->id;
186 memcpy(&q.u.req, ss, sizeof *ss);
187 if (write(dns_pipe, &q, sizeof q) != sizeof q) {
[cd7b81d]188 syslog(LOG_ERR, "write: %m");
189 exit (EX_OSERR);
190 }
191
192 return 0;
193}
Note: See TracBrowser for help on using the repository browser.