-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
291 lines (253 loc) · 6.02 KB
/
main.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
/* porkbind by Derek Callaway <[email protected]> */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<netinet/in.h>
#include<arpa/nameser.h>
#include<resolv.h>
#include<errno.h>
#include<netdb.h>
#include<unistd.h>
#include<signal.h>
#include<pthread.h>
#include"dnsqry.h"
static pthread_t **threads;
static void handler(int);
int
main(int argc, char **argv)
{
struct funcargs da = {0}, **fargs;
struct servers *start, *llp = 0;
struct hostent *host;
/*
* Using char as variable type to perform bitwise operations upon
* because sizeof(char)==1 always according to standard.
*/
register char *p;
char *server = NULL, *config = NULL, *domain, *op, recursion =
0, indiv = 0;
int count, membs;
unsigned int c;
union {
HEADER hdr;
unsigned char buf[PACKETSZ];
} response;
char buf [8096];
if (argc == 1)
usage(argv);
signal(SIGINT, handler);
signal(SIGSEGV, handler);
setvbuf(stdout, buf, _IOFBF, (size_t) 8096);
spin();
while ((c = getopt(argc, argv, "s:c:ri")) != EOF) {
switch (c) {
case 0:
puts(optarg);
break;
case '?':
usage(argv);
case 's':
server = optarg;
break;
case 'c':
config = optarg;
break;
case 'r':
recursion = 1;
break;
case 'i':
indiv = 1;
break;
}
}
res_init();
_res.options &= ~(RES_DNSRCH | RES_DEFNAMES);
_res.options |= RES_RECURSE;
_res.retry = 2;
if (server) {
spin();
#ifdef DEBUG
dputs("gethostbyname");
#endif
if (!(host = gethostbyname(server))) {
printf("Unable to resolve: %s\n", server);
exit(EXIT_FAILURE);
}
spin();
_res.nscount = 1;
memcpy((void *)&_res.nsaddr_list[0].sin_addr,
(void *)host->h_addr_list[0], (size_t) host->h_length);
}
if (argv[optind])
domain = argv[optind];
else {
if (!(domain = (char *)malloc(MAXDNAME + 1)))
vexit("malloc");
if (getdomainname(domain, MAXDNAME) < 0)
vexit("getdomainname");
if (!strlen(domain))
usage(argv);
}
puts("\n-*- PorkBind v1.3 (http://innu.org/~super) -*-\n");
op = da.dom = domain;
if (!(start = llp = (struct servers *)calloc(1, sizeof(*start))))
vexit("calloc");
llp->next = NULL, llp->ns = llp->ver = NULL, membs = 1;
do {
if (indiv)
break;
spin();
#ifdef DEBUG
dputs("res_query");
#endif
if (
(c =
res_query(op, C_IN, T_NS, (unsigned char *)&response,
sizeof(response))) < 0) {
printf("Unable to retrieve nameserver records for: %s\n",
domain);
exit(EXIT_FAILURE);
}
spin();
da.eom = response.buf + c;
da.cp = response.buf + sizeof(HEADER);
da.cp += skipname(response.buf, da.cp, da.eom) + QFIXEDSZ;
count = ntohs(response.hdr.ancount) + ntohs(response.hdr.nscount);
spin();
while ((--count >= 0)) {
if (da.cp < da.eom) {
da.bufp = (char*)response.buf;
da.cp += skiptodata(&da);
if (da.type == T_NS) {
if (!(llp->ns = (char *)malloc(MAXDNAME + 1)))
vexit("malloc");
#ifdef DEBUG
dputs("dn_expand");
#endif
if (
(count =
dn_expand(response.buf, da.eom, da.cp, llp->ns,
MAXDNAME)) < 0)
continue;
if (!
(llp->next =
(struct servers *)calloc(1, sizeof(*(llp->next)))))
vexit("calloc");
llp = llp->next;
membs++;
}
da.cp += da.dlen;
if (!
(llp->next =
(struct servers *)calloc(1, sizeof(*(llp->next)))))
vexit("calloc");
}
}
spin();
if (recursion) {
p = op;
while (*p && *p != '.')
p++;
if (*p)
p++;
op = p;
if (*p) {
while (*p && *p != '.')
p++;
if (!*p)
recursion = 0;
} else
recursion = 0;
}
} while (recursion);
spin();
/* -i flag was used; only allocating for the one individual nameserver
* that was provided on the command line.. */
if (indiv) {
if (!(start = llp = (struct servers *)calloc(1, sizeof(*start))))
vexit("calloc");
if (!(llp->ns = (char *)malloc((c = strlen(domain)) + 1)))
vexit("malloc");
strncpy(llp->ns, domain, c);
*(llp->ns + c) = 0;
if (!(llp->next = (struct servers *)malloc(sizeof(*(llp->next)))))
vexit("malloc");
llp = llp->next;
membs++;
}
spin();
llp->next = 0, llp->ns = llp->ver = 0;
if (!(threads = (pthread_t **) malloc((membs + 1) * sizeof(*threads))))
vexit("malloc");
if (!(fargs = (struct funcargs **)malloc((membs + 1) * sizeof(*fargs))))
vexit("malloc");
for (llp = start, c = 0; llp->ns; llp = llp->next, c++) {
if (!(threads[c] = (pthread_t *) malloc(sizeof(*threads[c]))))
vexit("malloc");
if (!(fargs[c] = (struct funcargs *)malloc(sizeof(*fargs[c]))))
vexit("malloc");
da.reserved = (void *)llp;
memcpy((void *)fargs[c], (void *)&da,
(size_t) sizeof(struct funcargs));
spin();
}
for (llp = start, c = 0; llp->ns; llp = llp->next, c++) {
if(!c)
spin();
#ifdef DEBUG
dputs("pthread_create");
#endif
pthread_create(threads[c], NULL, vcheck, fargs[c]);
}
threads[c] = 0;
signal(SIGALRM, handler);
alarm(TOTAL_TIMEOUT);
for (c = 0; threads[c]; c++) {
#ifdef DEBUG
dputs("pthread_join");
#endif
spin();
pthread_join(*threads[c], NULL);
}
spin();
if (alarm(0))
for (llp = start; llp->ns; llp = llp->next)
if (!llp->ver)
printf("%s timed out.\n", llp->ns);
if(!start||!start->ns)
printf("Unable to retrieve nameserver records for: %s\n",domain);
else
for (llp = start; llp->ns; llp = llp->next)
if(!llp->ver)
printf("%s timed out.\n",llp->ns);
else
report(llp, config);
exit(EXIT_SUCCESS);
}
static void
handler(int signum)
{
switch (signum) {
case SIGALRM:
/*
* Other threads will pay attention to the signal if it's not
* ignored.
*/
signal(SIGALRM, SIG_IGN);
for (signum = 0; threads[signum]; signum++)
pthread_cancel(*threads[signum]);
return;
case SIGSEGV:
signal(SIGSEGV, SIG_IGN);
puts("ACK! Caught a SIGSEGV. :-(");
puts("Please mail bugs to [email protected]!");
exit(EXIT_FAILURE);
case SIGINT:
signal(SIGINT, SIG_IGN);
puts("Cancelling unexpended threads...");
for (signum = 0; threads[signum]; signum++)
pthread_cancel(*threads[signum]);
puts("Quitting!");
exit(EXIT_SUCCESS);
}
}