source: code/dns.c@ cd7b81d

Last change on this file since cd7b81d was cd7b81d, checked in by Mike Belopuhov <mike.belopuhov@…>, 15 years ago

move it to the github

  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*
2 * Copyright (c) 2009 Michael Shalayeff
3 * All rights reserved.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER IN
14 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#ifndef lint
19static const char rcsid[] = "$ABSD: dns.c,v 1.2 2010/01/03 01:30:00 kmerz Exp $";
20#endif /* not lint */
21
22#include <sys/param.h>
23#include <sys/socket.h>
24#include <sys/time.h>
25#include <netinet/in.h>
26#include <arpa/inet.h>
27#include <errno.h>
28#include <stdlib.h>
29#include <unistd.h>
30#include <syslog.h>
31#include <sysexits.h>
32#include <login_cap.h>
33#include <event.h>
34#include <pwd.h>
35#include <netdb.h>
36
37#include "icb.h"
38#include "icbd.h"
39
40void dns_dispatch(int, short, void *);
41int dns_pipe;
42
43int
44icbd_dns_init(void)
45{
46 struct event ev;
47 int pipe[2];
48 struct passwd *pw;
49
50 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1) {
51 syslog(LOG_ERR, "socketpair: %m");
52 exit(EX_OSERR);
53 }
54
55 switch (fork()) {
56 case -1:
57 syslog(LOG_ERR, "fork: %m");
58 exit(EX_OSERR);
59 case 0:
60 break;
61
62 default:
63 close(pipe[1]);
64 dns_pipe = pipe[0];
65 return (0);
66 }
67
68 setproctitle("dns resolver");
69 close(pipe[0]);
70
71 if ((pw = getpwnam(ICBD_USER)) == NULL) {
72 syslog(LOG_ERR, "No passwd entry for %s", ICBD_USER);
73 exit(EX_NOUSER);
74 }
75 if (setusercontext(NULL, pw, pw->pw_uid,
76 LOGIN_SETALL & ~LOGIN_SETUSER) < 0) {
77 syslog(LOG_ERR, "%s:%m", pw->pw_name);
78 exit(EX_NOPERM);
79 }
80 if (setuid(pw->pw_uid) < 0) {
81 syslog(LOG_ERR, "%d:%m", pw->pw_uid);
82 exit(EX_NOPERM);
83 }
84 if (chdir("/") < 0) {
85 syslog(LOG_ERR, "chdir: %m");
86 exit(EX_UNAVAILABLE);
87 }
88
89 event_init();
90
91 event_set(&ev, pipe[1], EV_READ | EV_PERSIST, dns_dispatch, NULL);
92 if (event_add(&ev, NULL) < 0) {
93 syslog(LOG_ERR, "event_add: %m");
94 exit (EX_UNAVAILABLE);
95 }
96
97 return event_dispatch();
98}
99
100void
101dns_dispatch(int fd, short event, void *arg)
102{
103 char host[NI_MAXHOST];
104 struct sockaddr_storage ss;
105 struct sockaddr *sa = (struct sockaddr *)&ss;
106 int gerr, ss_len = sizeof ss;
107
108 arg = NULL;
109 if (event != EV_READ)
110 return;
111
112 if (verbose)
113 syslog(LOG_DEBUG, "dns_dispatch");
114
115 if (read(fd, &ss, ss_len) != ss_len) {
116 syslog(LOG_ERR, "dns read: %m");
117 exit(1);
118 }
119
120 if ((gerr = getnameinfo(sa, sa->sa_len,
121 host, sizeof host, NULL, 0, NI_NOFQDN))) {
122 syslog(LOG_ERR, "getnameinfo: %s", gai_strerror(gerr));
123 write(fd, host, sizeof host);
124 return;
125 }
126
127 if (verbose)
128 syslog(LOG_DEBUG, "dns_dispatch: resolved %s", host);
129
130 if (write(fd, host, sizeof host) != sizeof host)
131 syslog(LOG_ERR, "dns write: %m");
132}
133
134int
135dns_rresolv(struct icb_session *is, struct sockaddr_storage *ss)
136{
137 /* one-shot event for the reply */
138 event_set(&is->ev, dns_pipe, EV_READ, icbd_dns, is);
139 if (event_add(&is->ev, NULL) < 0) {
140 syslog(LOG_ERR, "event_add: %m");
141 exit (EX_UNAVAILABLE);
142 }
143
144 inet_ntop(ss->ss_family, ss->ss_family == AF_INET ?
145 (void *)&((struct sockaddr_in *)ss)->sin_addr :
146 (void *)&((struct sockaddr_in6 *)ss)->sin6_addr,
147 is->host, sizeof is->host);
148
149 if (verbose)
150 syslog(LOG_DEBUG, "resolving: %s", is->host);
151
152 if (write(dns_pipe, ss, sizeof *ss) != sizeof *ss) {
153 syslog(LOG_ERR, "write: %m");
154 exit (EX_OSERR);
155 }
156
157 return 0;
158}
Note: See TracBrowser for help on using the repository browser.