source: code/dns.c@ efa8586

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

Add icb_vis to escape '%' chars and do some other sanitizing

  • Property mode set to 100644
File size: 4.1 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 *);
40int dns_pipe;
41
42struct icbd_dnsquery {
43 uint64_t sid;
44 union {
45 struct sockaddr_storage req;
46 char rep[MAXHOSTNAMELEN];
47 } u;
48};
49
50int
51dns_init(void)
52{
53 static struct event ev;
54 struct passwd *pw;
55 int pipes[2];
56
57 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipes) == -1) {
58 syslog(LOG_ERR, "socketpair: %m");
59 exit(EX_OSERR);
60 }
61
62 switch (fork()) {
63 case -1:
64 syslog(LOG_ERR, "fork: %m");
65 exit(EX_OSERR);
66 case 0:
67 break;
68
69 default:
70 close(pipes[1]);
71 dns_pipe = pipes[0];
72
73 /* event for the reply */
74 event_set(&ev, dns_pipe, EV_READ | EV_PERSIST,
75 dns_done, NULL);
76 if (event_add(&ev, NULL) < 0) {
77 syslog(LOG_ERR, "event_add: %m");
78 exit (EX_UNAVAILABLE);
79 }
80 return (0);
81 }
82
83 setproctitle("dns resolver");
84 close(pipes[0]);
85
86 if ((pw = getpwnam(ICBD_USER)) == NULL) {
87 syslog(LOG_ERR, "No passwd entry for %s", ICBD_USER);
88 exit(EX_NOUSER);
89 }
90
91 if (setusercontext(NULL, pw, pw->pw_uid,
92 LOGIN_SETALL & ~LOGIN_SETUSER) < 0)
93 exit(EX_NOPERM);
94
95 if (setuid(pw->pw_uid) < 0) {
96 syslog(LOG_ERR, "%d: %m", pw->pw_uid);
97 exit(EX_NOPERM);
98 }
99
100 if (chdir("/") < 0) {
101 syslog(LOG_ERR, "chdir: %m");
102 exit(EX_UNAVAILABLE);
103 }
104
105 event_init();
106
107 /* event for the request */
108 event_set(&ev, pipes[1], EV_READ | EV_PERSIST, dns_dispatch, NULL);
109 if (event_add(&ev, NULL) < 0) {
110 syslog(LOG_ERR, "event_add: %m");
111 exit (EX_UNAVAILABLE);
112 }
113
114 return event_dispatch();
115}
116
117void
118dns_dispatch(int fd, short event, void *arg __attribute__((unused)))
119{
120 char host[NI_MAXHOST];
121 struct sockaddr *sa;
122 struct icbd_dnsquery q;
123 int gerr;
124
125 arg = NULL;
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(1);
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
149void
150dns_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
175int
176dns_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}
Note: See TracBrowser for help on using the repository browser.