1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
/* $OpenBSD: ntp_dns.c,v 1.16 2015/12/19 17:55:29 reyk Exp $ */
/*
* Copyright (c) 2003-2008 Henning Brauer <henning@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <err.h>
#include <errno.h>
#include <poll.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include "ntpd.h"
volatile sig_atomic_t quit_dns = 0;
struct imsgbuf *ibuf_dns;
void sighdlr_dns(int);
int dns_dispatch_imsg(void);
void
sighdlr_dns(int sig)
{
switch (sig) {
case SIGTERM:
case SIGINT:
quit_dns = 1;
break;
}
}
pid_t
ntp_dns(int pipe_ntp[2], struct ntpd_conf *nconf, struct passwd *pw)
{
pid_t pid;
struct pollfd pfd[1];
int nfds, nullfd;
switch (pid = fork()) {
case -1:
fatal("cannot fork");
break;
case 0:
break;
default:
return (pid);
}
if (setpriority(PRIO_PROCESS, 0, 0) == -1)
log_warn("could not set priority");
/* in this case the parent didn't init logging and didn't daemonize */
if (nconf->settime && !nconf->debug) {
log_init(nconf->debug, LOG_DAEMON);
if (setsid() == -1)
fatal("setsid");
}
log_procinit("dns");
if ((nullfd = open("/dev/null", O_RDWR, 0)) == -1)
fatal(NULL);
if (!nconf->debug) {
dup2(nullfd, STDIN_FILENO);
dup2(nullfd, STDOUT_FILENO);
dup2(nullfd, STDERR_FILENO);
}
close(nullfd);
setproctitle("dns engine");
close(pipe_ntp[0]);
if (setgroups(1, &pw->pw_gid) ||
setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) ||
setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
fatal("can't drop privileges");
signal(SIGTERM, sighdlr_dns);
signal(SIGINT, sighdlr_dns);
signal(SIGHUP, SIG_IGN);
if ((ibuf_dns = malloc(sizeof(struct imsgbuf))) == NULL)
fatal(NULL);
imsg_init(ibuf_dns, pipe_ntp[1]);
if (pledge("stdio dns", NULL) == -1)
err(1, "pledge");
while (quit_dns == 0) {
pfd[0].fd = ibuf_dns->fd;
pfd[0].events = POLLIN;
if (ibuf_dns->w.queued)
pfd[0].events |= POLLOUT;
if ((nfds = poll(pfd, 1, INFTIM)) == -1)
if (errno != EINTR) {
log_warn("poll error");
quit_dns = 1;
}
if (nfds > 0 && (pfd[0].revents & POLLOUT))
if (msgbuf_write(&ibuf_dns->w) <= 0 &&
errno != EAGAIN) {
log_warn("pipe write error (to ntp engine)");
quit_dns = 1;
}
if (nfds > 0 && pfd[0].revents & POLLIN) {
nfds--;
if (dns_dispatch_imsg() == -1)
quit_dns = 1;
}
}
msgbuf_clear(&ibuf_dns->w);
free(ibuf_dns);
_exit(0);
}
int
dns_dispatch_imsg(void)
{
struct imsg imsg;
int n, cnt;
char *name;
struct ntp_addr *h, *hn;
struct ibuf *buf;
const char *str;
if ((n = imsg_read(ibuf_dns)) == -1 && errno != EAGAIN)
return (-1);
if (n == 0) { /* connection closed */
log_warnx("dispatch_imsg in main: pipe closed");
return (-1);
}
for (;;) {
if ((n = imsg_get(ibuf_dns, &imsg)) == -1)
return (-1);
if (n == 0)
break;
switch (imsg.hdr.type) {
case IMSG_HOST_DNS:
case IMSG_CONSTRAINT_DNS:
if (imsg.hdr.type == IMSG_HOST_DNS)
str = "IMSG_HOST_DNS";
else
str = "IMSG_CONSTRAINT_DNS";
name = imsg.data;
if (imsg.hdr.len < 1 + IMSG_HEADER_SIZE)
fatalx("invalid %s received", str);
imsg.hdr.len -= 1 + IMSG_HEADER_SIZE;
if (name[imsg.hdr.len] != '\0' ||
strlen(name) != imsg.hdr.len)
fatalx("invalid %s received", str);
if ((cnt = host_dns(name, &hn)) == -1)
break;
buf = imsg_create(ibuf_dns, imsg.hdr.type,
imsg.hdr.peerid, 0,
cnt * sizeof(struct sockaddr_storage));
if (cnt > 0) {
if (buf) {
for (h = hn; h != NULL; h = h->next)
if (imsg_add(buf, &h->ss,
sizeof(h->ss)) == -1) {
buf = NULL;
break;
}
}
host_dns_free(hn);
hn = NULL;
}
if (buf)
imsg_close(ibuf_dns, buf);
break;
default:
break;
}
imsg_free(&imsg);
}
return (0);
}
|