-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnss-dnspq.c
362 lines (331 loc) · 9.04 KB
/
nss-dnspq.c
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* This file is part of dnspq.
*
* dnspq is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* dnspq is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with dnspq. If not, see <http://www.gnu.org/licenses/>.
*/
/* GLIBC nss module */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <nss.h>
#include <netdb.h>
#include <arpa/inet.h>
#ifdef LOGGING
#include <syslog.h>
#endif
#include "dnspq.h"
#ifndef RESOLV_CONF
#define RESOLV_CONF "/etc/resolv-dnspq.conf"
#endif
typedef struct _domaingroup {
char *domain;
struct _domaingroup *next;
size_t poolcount;
struct sockaddr_in **dnsservers;
} domaingroup;
static domaingroup *rpool = NULL;
#ifdef DEBUG
void debugconfig(void) {
domaingroup *walk;
struct sockaddr_in *swalk;
int i;
for (walk = rpool; walk != NULL; walk = walk->next) {
printf("\"%s\": %zd\n",
walk->domain ? walk->domain : "(cont)", walk->poolcount);
for (i = 0, swalk = walk->dnsservers[i]; swalk != NULL; swalk = walk->dnsservers[++i]) {
printf(" %s:%d\n", inet_ntoa(swalk->sin_addr), htons(swalk->sin_port));
}
}
}
#endif
/* library init */
/* read the config file and build up the structure per domain */
#ifndef DEBUG
__attribute__((constructor))
#endif
void readconfig(void) {
FILE *resolvconf = NULL;
int j, k;
char buf[1024];
domaingroup *tdg = NULL;
domaingroup *ndg = NULL;
char *p = NULL;
struct sockaddr_in *dnsservers[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
struct sockaddr_in *dnsserver = NULL;
int dnsi = 0;
char *fps[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
int port;
#ifdef LOGGING
openlog("dnspq", LOG_PID, LOG_USER);
syslog(LOG_INFO, "nss-dnspq.so.2 v" VERSION " (" GIT_VERSION ") has been invoked");
#endif
/* don't use time to avoid same sequence when multiple processes
* start at the same time */
srand(getpid());
/* .domain ip:port ip:port ...
* or
* nameserver ip
*
* The first form creates a group of DNS servers to query for the
* domain. The leading . is mandatory here (to distinguish easily).
* The second form is to facilitate traditional /etc/resolv.conf
* files. Interleaving both forms is NOT supported.
*/
if ((resolvconf = fopen(RESOLV_CONF, "r")) == NULL)
return;
while (fgets(buf, sizeof(buf), resolvconf) != NULL)
if (
buf[0] == 'n' &&
buf[1] == 'a' &&
buf[2] == 'm' &&
buf[3] == 'e' &&
buf[4] == 's' &&
buf[5] == 'e' &&
buf[6] == 'r' &&
buf[7] == 'v' &&
buf[8] == 'e' &&
buf[9] == 'r' &&
buf[10] == ' ')
{ /* traditional /etc/resolv.conf mode */
if (dnsi == sizeof(dnsservers) - 1)
continue;
if ((p = strchr(buf + 11, '\n')) != NULL)
*p = '\0';
dnsserver = dnsservers[dnsi++] = malloc(sizeof(*dnsserver));
if (inet_pton(AF_INET, buf + 11, &(dnsserver->sin_addr)) <= 0) {
free(dnsserver);
dnsserver = dnsservers[dnsi--] = NULL;
continue;
}
dnsserver->sin_family = AF_INET;
dnsserver->sin_port = htons(53);
} else if (buf[0] == '.') { /* group mode */
p = buf + 1;
dnsi = 0;
while (dnsi < sizeof(fps) && (p = strchr(p, ' ')) != NULL) {
*p++ = '\0';
fps[dnsi] = p;
dnsi++;
}
if (dnsi == 0)
continue;
if ((p = strchr(fps[dnsi - 1], '\n')) != NULL)
*p = '\0';
k = -1;
if (rpool == NULL) {
tdg = rpool = malloc(sizeof(domaingroup));
tdg->next = NULL;
} else {
ndg = NULL;
for (tdg = rpool; ; tdg = tdg->next) {
if (tdg->domain != NULL &&
strcmp(tdg->domain, buf + 1) == 0)
{
/* randomise insertion */
tdg->poolcount++;
k = rand() % tdg->poolcount;
if (k == 0) {
tdg = ndg;
} else {
for (j = 1; j < k; j++)
tdg = tdg->next;
}
break;
}
if (tdg->next == NULL)
break;
ndg = tdg;
}
ndg = malloc(sizeof(domaingroup));
if (tdg == NULL) {
ndg->next = rpool;
tdg = rpool = ndg;
} else {
ndg->next = tdg->next;
tdg = tdg->next = ndg;
}
}
if (k == -1) {
tdg->domain = strdup(buf + 1);
tdg->poolcount = 1;
} else if (k == 0) {
tdg->domain = tdg->next->domain;
tdg->poolcount = tdg->next->poolcount;
tdg->next->domain = NULL;
tdg->next->poolcount = 0;
} else {
tdg->domain = NULL;
tdg->poolcount = 0;
}
tdg->dnsservers = malloc(sizeof(*dnsserver) * (dnsi + 1));
for (j = 0, k = 0; j < dnsi; j++) {
dnsserver = tdg->dnsservers[k++] = malloc(sizeof(*dnsserver));
port = 0;
if ((p = strchr(fps[j], ':')) != NULL) {
*p++ = '\0';
port = atoi(p);
}
if (inet_pton(AF_INET, fps[j], &(dnsserver->sin_addr)) <= 0) {
free(dnsserver);
dnsserver = tdg->dnsservers[--k] = NULL;
continue;
}
dnsserver->sin_family = AF_INET;
dnsserver->sin_port = htons(port == 0 ? 53 : port);
}
tdg->dnsservers[k] = NULL;
dnsi = 0;
}
fclose(resolvconf);
if (dnsi > 0) {
/* create fallback group for traditional mode */
if (rpool == NULL) {
tdg = rpool = malloc(sizeof(domaingroup));
} else {
for (tdg = rpool; tdg->next != NULL; tdg = tdg->next)
;
tdg = tdg->next = malloc(sizeof(domaingroup));
}
tdg->domain = NULL;
tdg->next = NULL;
tdg->dnsservers = malloc(sizeof(*dnsserver) * (dnsi + 1));
memcpy(tdg->dnsservers, dnsservers, sizeof(*dnsserver) * (dnsi + 1));
}
}
/* strcmp at the tail of a string, either start, or from a dot */
static inline int tailcmp(const char *haystack, const char *needle) {
size_t nl = strlen(needle);
size_t hl = strlen(haystack);
const char *p;
if (nl < hl) {
p = haystack + hl - nl - 1;
if (*p++ == '.') {
for (; *p != '\0' && *p == *needle; p++, needle++)
;
if (*p == '\0')
return 0;
}
}
return 1;
}
/* helper function to locate the set of nameservers for the given domain */
static inline char get_dnss_for_domain(
struct sockaddr_in ***dnsservers,
const char *name)
{
domaingroup *w = rpool;
int i;
while (w != NULL) {
if (w->domain == NULL) {
*dnsservers = w->dnsservers;
return 1;
} else if (tailcmp(name, w->domain) == 0) {
if (w->poolcount > 1) {
i = w->next->poolcount;
w->next->poolcount = (w->next->poolcount + 1) % w->poolcount;
for (; i > 0; i--)
w = w->next;
}
*dnsservers = w->dnsservers;
return 1;
} else {
/* skip over entire pool */
for (i = w->poolcount; i > 0; i--)
w = w->next;
}
}
return 0;
}
enum nss_status _nss_dnspq_gethostbyname3_r(const char *name, int af,
struct hostent *host, char *buf, size_t buflen,
int *errnop, int *h_errnop, int32_t *ttlp, char **canonp)
{
unsigned int ttl;
char sid;
struct sockaddr_in **dnsservers = NULL;
size_t nlen = 0;
if (af == AF_INET &&
(nlen = strlen(name)) > 0 &&
buflen >= nlen + 1 + 2 * sizeof(void *) + sizeof(struct in_addr) + sizeof(void *) &&
get_dnss_for_domain(&dnsservers, name) &&
dnsq(dnsservers, name, (struct in_addr *)buf, &ttl, &sid) == 0)
{
host->h_addrtype = af;
host->h_length = sizeof(struct in_addr);
host->h_addr_list = (char **)buf + sizeof(struct in_addr);
host->h_addr_list[0] = buf;
host->h_addr_list[1] = NULL;
host->h_aliases = (char **)&host->h_addr_list[2];
host->h_aliases[0] = NULL;
host->h_name = (char *)&host->h_aliases[1];
memcpy(host->h_name, name, nlen + 1);
if (ttlp != NULL)
*ttlp = (int32_t)ttl;
if (canonp != NULL)
*canonp = host->h_name;
*errnop = 0;
*h_errnop = 0;
return NSS_STATUS_SUCCESS;
}
*errnop = EINVAL;
*h_errnop = NO_RECOVERY;
return NSS_STATUS_UNAVAIL;
}
enum nss_status _nss_dnspq_gethostbyname2_r(const char *name, int af,
struct hostent *host, char *buffer, size_t buflen,
int *errnop, int *h_errnop)
{
return _nss_dnspq_gethostbyname3_r(name, af, host, buffer, buflen,
errnop, h_errnop, NULL, NULL);
}
enum nss_status _nss_dnspq_gethostbyname_r(const char *name,
struct hostent *host, char *buffer, size_t buflen,
int *errnop, int *h_errnop)
{
return _nss_dnspq_gethostbyname3_r(name, AF_INET, host, buffer, buflen,
errnop, h_errnop, NULL, NULL);
}
enum nss_status _nss_dnspq_gethostbyaddr2_r(const void* addr, socklen_t len,
int af, struct hostent *host, char *buffer, size_t buflen,
int *errnop, int *h_errnop, int32_t *ttlp)
{
/* pacify compiler */
(void) addr;
(void) len;
(void) af;
(void) host;
(void) buffer;
(void) buflen;
(void) ttlp;
*errnop = EINVAL;
*h_errnop = NO_RECOVERY;
return NSS_STATUS_UNAVAIL;
}
enum nss_status _nss_dnspq_gethostbyaddr_r(const void* addr, socklen_t len,
int af, struct hostent *host, char *buffer, size_t buflen,
int *errnop, int *h_errnop)
{
/* pacify compiler */
(void) addr;
(void) len;
(void) af;
(void) host;
(void) buffer;
(void) buflen;
*errnop = EINVAL;
*h_errnop = NO_RECOVERY;
return NSS_STATUS_UNAVAIL;
}