~case/dbns

ref: 0f9985edf4d2b9e98aaac388d30fd4ab8c4722cf dbns/src/dns.c -rw-r--r-- 8.8 KiB
0f9985edcase rm extern 10 months ago
                                                                                
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/****************************************************************************
 *                   ^     +----- |  / ^     ^ |     | +-\                  *
 *                  / \    |      | /  |\   /| |     | |  \                 *
 *                 /   \   +---   |<   | \ / | |     | |  |                 *
 *                /-----\  |      | \  |  v  | |     | |  /                 *
 *               /       \ |      |  \ |     | +-----+ +-/                  *
 ****************************************************************************
 * AFKMud (c)1997-2002 Alsherok. Contributors: Samson, Dwip, Whir,          *
 * Cyberfox, Karangi, Rathian, Cam, Raine, and Tarl.                        *
 *                                                                          *
 * Original SMAUG 1.4a written by Thoric (Derek Snider) with Altrag,        *
 * Blodkai, Haus, Narn, Scryn, Swordbearer, Tricops, Gorog, Rennard,        *
 * Grishnakh, Fireblade, and Nivek.                                         *
 *                                                                          *
 * Original MERC 2.1 code by Hatchet, Furey, and Kahn.                      *
 *                                                                          *
 * Original DikuMUD code by: Hans Staerfeldt, Katja Nyboe, Tom Madsen,      *
 * Michael Seifert, and Sebastian Hammer.                                   *
 ****************************************************************************
 *                          DNS Resolver Module                             *
 ****************************************************************************/

#include <sys/types.h>
#include <sys/wait.h>
#include <stdbool.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include "mud.h"

DNS_DATA *first_cache;
DNS_DATA *last_cache;

void	save_dns(void);

void 
prune_dns(void)
{
	DNS_DATA *cache, *cache_next;

	for (cache = first_cache; cache; cache = cache_next) {
		cache_next = cache->next;

		/* Stay in cache for 14 days */
		if (current_time - cache->time >= 1209600 || !str_cmp(cache->ip, "Unknown??") || !str_cmp(cache->name, "Unknown??")) {
			STRFREE(cache->ip);
			STRFREE(cache->name);
			UNLINK(cache, first_cache, last_cache, next, prev);
			DISPOSE(cache);
		}
	}
	save_dns();
	return;
}

void 
check_dns(void)
{
	if (current_time >= new_boot_time_t)
		prune_dns();
	return;
}

void 
add_dns(char *dhost, char *address)
{
	DNS_DATA *cache;

	CREATE(cache, DNS_DATA, 1);
	cache->ip = STRALLOC(dhost);
	cache->name = STRALLOC(address);
	cache->time = current_time;
	LINK(cache, first_cache, last_cache, next, prev);

	save_dns();
	return;
}

char   *
in_dns_cache(char *ip)
{
	DNS_DATA *cache;
	static char dnsbuf[MSL];

	dnsbuf[0] = '\0';

	for (cache = first_cache; cache; cache = cache->next) {
		if (!str_cmp(ip, cache->ip)) {
			strcpy(dnsbuf, cache->name);
			break;
		}
	}
	return dnsbuf;
}

#if defined(KEY)
#undef KEY
#endif

#define KEY( literal, field, value )					\
				if ( !str_cmp( word, literal ) )	\
				{					\
				      field = value;			\
				      fMatch = true;			\
				      break;				\
				}

void 
fread_dns(DNS_DATA * cache, FILE *fp)
{
	char *word;
	bool fMatch;

	for (;;) {
		word = feof(fp) ? "End" : fread_word(fp);
		fMatch = false;

		switch (UPPER(word[0])) {
		case '*':
			fMatch = true;
			fread_to_eol(fp);
			break;

		case 'E':
			if (!str_cmp(word, "End")) {
				if (!cache->ip)
					cache->ip = STRALLOC("Unknown??");
				if (!cache->name)
					cache->name = STRALLOC("Unknown??");
				return;
			}
			break;

		case 'I':
			KEY("IP", cache->ip, fread_string(fp));
			break;

		case 'N':
			KEY("Name", cache->name, fread_string(fp));
			break;

		case 'T':
			KEY("Time", cache->time, fread_number(fp));
			break;
		}

		if (!fMatch)
			bug("fread_dns: no match: %s", word);
	}
}

void 
load_dns(void)
{
	char filename[256];
	DNS_DATA *cache;
	FILE *fp;

	first_cache = NULL;
	last_cache = NULL;

	sprintf(filename, "%s", DNS_FILE);

	if ((fp = fopen(filename, "r")) != NULL) {
		for (;;) {
			char letter;
			char *word;

			letter = fread_letter(fp);
			if (letter == '*') {
				fread_to_eol(fp);
				continue;
			}
			if (letter != '#') {
				bug("load_dns: # not found.", 0);
				break;
			}
			word = fread_word(fp);
			if (!str_cmp(word, "CACHE")) {
				CREATE(cache, DNS_DATA, 1);
				fread_dns(cache, fp);
				LINK(cache, first_cache, last_cache, next, prev);
				continue;
			} else if (!str_cmp(word, "END"))
				break;
			else {
				bug("load_dns: bad section: %s.", word);
				continue;
			}
		}
		FCLOSE(fp);
	}
	prune_dns();			/* Clean out entries beyond 14 days */
	return;
}

void 
save_dns(void)
{
	DNS_DATA *cache;
	FILE *fp;
	char filename[256];

	sprintf(filename, "%s", DNS_FILE);

	if ((fp = fopen(filename, "w")) == NULL) {
		bug("save_dns: fopen", 0);
		perror(filename);
	} else {
		for (cache = first_cache; cache; cache = cache->next) {
			fprintf(fp, "#CACHE\n");
			fprintf(fp, "IP		%s~\n", cache->ip);
			fprintf(fp, "Name		%s~\n", cache->name);
			fprintf(fp, "Time		%ld\n", cache->time);
			fprintf(fp, "End\n\n");
		}
		fprintf(fp, "#END\n");
		FCLOSE(fp);
	}
	return;
}

/* DNS Resolver code by Trax of Forever's End */
/*
 * Almost the same as read_from_buffer...
 */
bool 
read_from_dns(int fd, char *buffer)
{
	static char inbuf[MAX_STRING_LENGTH * 2];
	int iStart, i, j, k;

	/* Check for overflow. */
	iStart = strlen(inbuf);
	if (iStart >= sizeof(inbuf) - 10) {
		bug("DNS input overflow!!!", 0);
		return false;
	}
	/* Snarf input. */
	for (;;) {
		int nRead;

		nRead = read(fd, inbuf + iStart, sizeof(inbuf) - 10 - iStart);
		if (nRead > 0) {
			iStart += nRead;
			if (inbuf[iStart - 2] == '\n' || inbuf[iStart - 2] == '\r')
				break;
		} else if (nRead == 0) {
			return false;
		} else if (errno == EWOULDBLOCK)
			break;
		else {
			perror("Read_from_dns");
			return false;
		}
	}

	inbuf[iStart] = '\0';

	/*
         * Look for at least one new line.
         */
	for (i = 0; inbuf[i] != '\n' && inbuf[i] != '\r'; i++) {
		if (inbuf[i] == '\0')
			return false;
	}

	/*
         * Canonical input processing.
         */
	for (i = 0, k = 0; inbuf[i] != '\n' && inbuf[i] != '\r'; i++) {
		if (inbuf[i] == '\b' && k > 0)
			--k;
		else if (isascii(inbuf[i]) && isprint(inbuf[i]))
			buffer[k++] = inbuf[i];
	}

	/*
         * Finish off the line.
         */
	if (k == 0)
		buffer[k++] = ' ';
	buffer[k] = '\0';

	/*
         * Shift the input buffer.
         */
	while (inbuf[i] == '\n' || inbuf[i] == '\r')
		i++;
	for (j = 0; (inbuf[j] = inbuf[i + j]) != '\0'; j++);

	return true;
}

/* DNS Resolver code by Trax of Forever's End */
/*
 * Process input that we got from resolve_dns.
 */
void 
process_dns(DESCRIPTOR_DATA * d)
{
	char address[MIL];
	int status;

	address[0] = '\0';

	if (!read_from_dns(d->ifd, address) || address[0] == '\0')
		return;

	if (address[0] != '\0') {
		add_dns(d->host, address);	/* Add entry to DNS cache */
		STRFREE(d->host);
		d->host = STRALLOC(address);
		if (d->character) {
			STRFREE(d->character->pcdata->lasthost);
			d->character->pcdata->lasthost = STRALLOC(address);
		}
	}
	/* close descriptor and kill dns process */
	if (d->ifd != -1) {
		close(d->ifd);
		d->ifd = -1;
	}
	/*
	 * we don't have to check here, cos the child is probably dead
	 * already. (but out of safety we do)
	 * 
	 * (later) I found this not to be true. The call to waitpid( ) is
	 * necessary, because otherwise the child processes become zombie
	 * and keep lingering around... The waitpid( ) removes them.
	 */
	if (d->ipid != -1) {
		waitpid(d->ipid, &status, 0);
		d->ipid = -1;
	}
	return;
}

/* DNS Resolver hook. Code written by Trax of Forever's End */
void 
resolve_dns(DESCRIPTOR_DATA * d, long ip)
{
	int fds[2];
	pid_t pid;

	/* create pipe first */
	if (pipe(fds) != 0) {
		perror("resolve_dns: pipe: ");
		return;
	}
	if (dup2(fds[1], STDOUT_FILENO) != STDOUT_FILENO) {
		perror("resolve_dns: dup2(stdout): ");
		return;
	}
	if ((pid = fork()) > 0) {
		/* parent process */
		d->ifd = fds[0];
		d->ipid = pid;
		close(fds[1]);
	} else if (pid == 0) {
		/* child process */
		char str_ip[64];
		int i;

		d->ifd = fds[0];
		d->ipid = pid;

		for (i = 2; i < 255; ++i)
			close(i);

		sprintf(str_ip, "%ld", ip);
		execl("../src/resolver", "AFKMud Resolver", str_ip, NULL);
		/* Still here --> hmm. An error. */
		bug("resolve_dns: Exec failed; Closing child.", 0);
		d->ifd = -1;
		d->ipid = -1;
		exit(0);
	} else {
		/* error */
		perror("resolve_dns: failed fork");
		close(fds[0]);
		close(fds[1]);
	}
}

void 
do_cache(CHAR_DATA * ch, char *argument)
{
	DNS_DATA *cache;
	int ip = 0;

	send_to_pager("&YCached DNS Information\n\r", ch);
	send_to_pager("IP               | Address\n\r", ch);
	send_to_pager("------------------------------------------------------------------------------\n\r", ch);
	for (cache = first_cache; cache; cache = cache->next) {
		pager_printf(ch, "&W%16.16s  &Y%s\n\r", cache->ip, cache->name);
		ip++;
	}
	pager_printf(ch, "\n\r&W%d IPs in the cache.\n\r", ip);
	return;
}