source: code/dns.c@ fa271b8

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

setusercontext(3) does it's own syslogging

  • Property mode set to 100644
File size: 3.5 KB
RevLine 
[cd7b81d]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#include <sys/param.h>
19#include <sys/socket.h>
20#include <sys/time.h>
21#include <netinet/in.h>
22#include <arpa/inet.h>
23#include <errno.h>
24#include <stdlib.h>
25#include <unistd.h>
26#include <syslog.h>
27#include <sysexits.h>
28#include <login_cap.h>
29#include <event.h>
30#include <pwd.h>
31#include <netdb.h>
32
33#include "icb.h"
34#include "icbd.h"
35
36void dns_dispatch(int, short, void *);
37int dns_pipe;
38
39int
40icbd_dns_init(void)
41{
42 struct event ev;
43 int pipe[2];
44 struct passwd *pw;
45
46 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe) == -1) {
47 syslog(LOG_ERR, "socketpair: %m");
48 exit(EX_OSERR);
49 }
50
51 switch (fork()) {
52 case -1:
53 syslog(LOG_ERR, "fork: %m");
54 exit(EX_OSERR);
55 case 0:
56 break;
57
58 default:
59 close(pipe[1]);
60 dns_pipe = pipe[0];
61 return (0);
62 }
63
64 setproctitle("dns resolver");
65 close(pipe[0]);
66
67 if ((pw = getpwnam(ICBD_USER)) == NULL) {
68 syslog(LOG_ERR, "No passwd entry for %s", ICBD_USER);
69 exit(EX_NOUSER);
70 }
[d554223]71
[cd7b81d]72 if (setusercontext(NULL, pw, pw->pw_uid,
[d554223]73 LOGIN_SETALL & ~LOGIN_SETUSER) < 0)
[cd7b81d]74 exit(EX_NOPERM);
[d554223]75
[cd7b81d]76 if (setuid(pw->pw_uid) < 0) {
[d554223]77 syslog(LOG_ERR, "%d: %m", pw->pw_uid);
[cd7b81d]78 exit(EX_NOPERM);
79 }
[d554223]80
[cd7b81d]81 if (chdir("/") < 0) {
82 syslog(LOG_ERR, "chdir: %m");
83 exit(EX_UNAVAILABLE);
84 }
85
86 event_init();
87
88 event_set(&ev, pipe[1], EV_READ | EV_PERSIST, dns_dispatch, NULL);
89 if (event_add(&ev, NULL) < 0) {
90 syslog(LOG_ERR, "event_add: %m");
91 exit (EX_UNAVAILABLE);
92 }
93
94 return event_dispatch();
95}
96
97void
98dns_dispatch(int fd, short event, void *arg)
99{
100 char host[NI_MAXHOST];
101 struct sockaddr_storage ss;
102 struct sockaddr *sa = (struct sockaddr *)&ss;
103 int gerr, ss_len = sizeof ss;
104
105 arg = NULL;
106 if (event != EV_READ)
107 return;
108
109 if (read(fd, &ss, ss_len) != ss_len) {
110 syslog(LOG_ERR, "dns read: %m");
111 exit(1);
112 }
113
114 if ((gerr = getnameinfo(sa, sa->sa_len,
115 host, sizeof host, NULL, 0, NI_NOFQDN))) {
116 syslog(LOG_ERR, "getnameinfo: %s", gai_strerror(gerr));
117 write(fd, host, sizeof host);
118 return;
119 }
120
121 if (verbose)
122 syslog(LOG_DEBUG, "dns_dispatch: resolved %s", host);
123
124 if (write(fd, host, sizeof host) != sizeof host)
125 syslog(LOG_ERR, "dns write: %m");
126}
127
128int
129dns_rresolv(struct icb_session *is, struct sockaddr_storage *ss)
130{
131 /* one-shot event for the reply */
132 event_set(&is->ev, dns_pipe, EV_READ, icbd_dns, is);
133 if (event_add(&is->ev, NULL) < 0) {
134 syslog(LOG_ERR, "event_add: %m");
135 exit (EX_UNAVAILABLE);
136 }
137
138 inet_ntop(ss->ss_family, ss->ss_family == AF_INET ?
139 (void *)&((struct sockaddr_in *)ss)->sin_addr :
140 (void *)&((struct sockaddr_in6 *)ss)->sin6_addr,
141 is->host, sizeof is->host);
142
143 if (verbose)
144 syslog(LOG_DEBUG, "resolving: %s", is->host);
145
146 if (write(dns_pipe, ss, sizeof *ss) != sizeof *ss) {
147 syslog(LOG_ERR, "write: %m");
148 exit (EX_OSERR);
149 }
150
151 return 0;
152}
Note: See TracBrowser for help on using the repository browser.