~ubuntu-branches/ubuntu/utopic/maradns/utopic

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
/* Copyright (c) 2004 Sam Trenholme
 *
 * TERMS
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * This software is provided 'as is' with no guarantees of correctness or
 * fitness for purpose.
 */

#include "../libs/JsStr.h"
#include <stdio.h>

/* Generate Csv2-compatible output for TXT record from raw js_string
 * Quote ASCII sequences printed to standard output
 * Make listeral ' \'
 * Make \xXX (hex number) sequences of anything else
 */
int escape_stdout_csv2(js_string *js) {
    unsigned char this;
    int inquote = 0, counter = 0;

    /* Sanity checks */
    if(js_has_sanity(js) < 0)
        return -1;
    if(js->unit_size != 1)
        return -1;

    inquote = 0;

    while(counter < js->unit_count) {
        this = *(js->string + counter);
	if(this < 32 || this > 122) { /* 122 == 'z'; {|}~ are escaped since
					 we currently don't allow the {
					 character in csv2 zone files (to
					 allow for future macro processing) */
	    if(inquote == 1) {
	        printf("\'");
	        inquote = 0;
	    }
	    printf("\\x%02x",this);
	}
        else if(this == '\'') {
	    if(inquote == 1) {
	    	printf("\'\\\'\'");
	    }
	    else {
		printf("\\\'");
	    }
	}
        else {
	    if(inquote == 0) {
                inquote = 1;
		printf("\'");
	    }
	    printf("%c",this);
	}
	counter++;
    }

    if(inquote == 1) {
        printf("\'");
    }

    return 1;
    }

/* Given a raw DNS query, show them the query in qtype:qname format, where
 * qname is a human-readable dns query */

void human_readable_dns_query(js_string *query, int hide_qtype) {
	unsigned char this;
	int counter = 0, dlen = -1, qtype, x;
	unsigned char *that;
	/* Sanity checks */
	if(js_has_sanity(query) < 0) {
		printf(":ERROR:\n");
		return;
	}
	if(query->unit_size != 1) {
		printf(":ERROR:\n");
		return;
	}

	if(query->unit_count < 2) {
		printf(":ERROR:\n");
	}

	that = query->string;
	qtype = *(that + query->unit_count - 1);
	qtype += *(that + query->unit_count - 2) << 8;
	if(hide_qtype != 1)
		printf("%d:",qtype);

	/* Print out the actual query */
	for(x=0;x<10000;x++) {
		if(counter > query->unit_count || counter < 0) {
			printf(":ERROR:105:\n");
			return;
		}
		dlen = *(query->string + counter);
		if(dlen == '_') {
			printf("{STAR}");
			if(counter == 0 &&
				query->unit_count != 3) { 
				/* ('*' at beginning of hostname) */
			    counter++;
			    continue;
			} else { /* '*' at end of hostname */
			    break;
			}
		}
		if(dlen > 64 || dlen < 0) {
			printf(":ERROR:110:%d:\n",dlen);
			return;
		}
		if(dlen == 0) {
			break;
		}
		while(dlen > 0) {
			counter++;
			if(counter > query->unit_count || counter < 0) {
				printf(":ERROR:119:\n");
				return;
			}
			this = *(query->string + counter);
			if(this < 32 || this > 127 || this == '.' || 
					this ==':') {
				printf("\\x%02x",this);
			} else {
				printf("%c",this);
			}
			dlen--;
		}
		counter++;
		printf(".");
	}
	
	if(counter != query->unit_count - 3) {
		printf(":ERROR:%d:%d:\n",counter,query->unit_count);	
	}
	
}