source: code/dns.c@ be3ad87

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

Add command line switch '-n' to disable DNS

  • Property mode set to 100644
File size: 4.2 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[MAXHOSTNAMELEN];
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 int gerr;
127
128 if (event != EV_READ)
129 return;
130
131 if (read(fd, &q, sizeof q) != sizeof q) {
132 syslog(LOG_ERR, "dns read: %m");
133 exit(EX_DATAERR);
134 }
135
136 sa = (struct sockaddr *)&q.u.req;
137 if ((gerr = getnameinfo(sa, sa->sa_len,
138 host, sizeof host, NULL, 0, NI_NOFQDN))) {
139 syslog(LOG_ERR, "getnameinfo: %s", gai_strerror(gerr));
140 return;
141 }
142
143 if (verbose)
144 syslog(LOG_DEBUG, "dns_dispatch: resolved %s", host);
145
146 memcpy(&q.u.rep, host, sizeof host);
147 if (write(fd, &q, sizeof q) != sizeof q)
148 syslog(LOG_ERR, "dns write: %m");
149}
150
151void
152dns_done(int fd, short event, void *arg __attribute__((unused)))
153{
154 struct icb_session *is;
155 struct icbd_dnsquery q;
156
157 if (event != EV_READ)
158 return;
159
160 if (read(fd, &q, sizeof q) != sizeof q) {
161 syslog(LOG_ERR, "read: %m");
162 return;
163 }
164
165 if ((is = icbd_session_lookup(q.sid)) == NULL) {
166 syslog(LOG_ERR, "failed to find session %llu", q.sid);
167 return;
168 }
169
170 memcpy(is->host, q.u.rep, MAXHOSTNAMELEN);
171 is->host[sizeof is->host - 1] = '\0';
172
173 if (verbose)
174 syslog(LOG_DEBUG, "icbd_dns: resolved %s", is->host);
175}
176
177void
178dns_rresolv(struct icb_session *is, struct sockaddr_storage *ss)
179{
180 struct icbd_dnsquery q;
181
182 if (!dodns)
183 return;
184
185 if (verbose)
186 syslog(LOG_DEBUG, "resolving: %s", is->host);
187
188 memset(&q, 0, sizeof q);
189 q.sid = is->id;
190 memcpy(&q.u.req, ss, sizeof *ss);
191 if (write(dns_pipe, &q, sizeof q) != sizeof q) {
192 syslog(LOG_ERR, "write: %m");
193 exit(EX_OSERR);
194 }
195}
Note: See TracBrowser for help on using the repository browser.