~samiam/MaraDNS

ref: 3.5.0002 MaraDNS/update/1.3.03/maradns-1.3.02-txt_multichunk.patch -rw-r--r-- 7.7 KiB View raw
db09084f — Sam Trenholme MaraDNS release 3.5.0002 5 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
--- maradns-1.3.03/parse/Csv2_rr_txt.c.orig	2007-02-12 10:05:48.000000000 -0800
+++ maradns-1.3.03/parse/Csv2_rr_txt.c	2007-02-12 10:26:58.000000000 -0800
@@ -62,6 +62,10 @@
                    leave the state machine */
 #define TXT_EXIT_STATE 9
 
+/* TXT_BETWEEN_CHUNKS_STATE: We're using ~ to separate records, and have
+ * just ended a chunk in a TXT record */
+#define TXT_BETWEEN_CHUNKS_STATE 10
+
 /* This returns true on [0-9a-zA-Z\-\_\+\%\!\^\=\;\\\'], false otherwise */
 int csv2_is_txtchar(int32 in) {
         return (in >= '0' && in <= '9') ||
@@ -176,7 +180,38 @@
 	} else { printf("Whoo!  Unicode is huge!\n"); exit(1); }
 	return 1;
 }
+
+/* Finalize a TXT record.  This is called at the end of TXT record to
+ * set the last chunk and to make sure we have the correct number of
+ * TXT chunks for this RR */
+js_string *csv2_finalize_txt(csv2_read *stream,int numchunks,
+                                       int chunkcount,int txt_len_place,
+                                       int txt_len, js_string *out) {
 	
+	/* TXT length */
+	if(numchunks != -1) {
+		if(txt_len_place < 0 || txt_len_place > out->max_count) {
+			js_destroy(out);
+			return 0;
+		}
+		if(txt_len > 255) {
+			csv2_error(stream, "Single TXT chunk too long");
+			js_destroy(out);
+			return 0;
+		}
+	    	*(out->string + txt_len_place) = txt_len;
+                chunkcount++;
+	}
+        /* This if only affects HINFO and other obscure RRs */
+        if(numchunks > 0 && chunkcount != numchunks) {
+                csv2_error(stream, "Incorrect number of chunks for this RR");
+                js_destroy(out);
+                return 0;
+        }
+        /* End of TXT record */
+	return out;
+}
+
 /* Get a string from the stream; we have this so that processing
  * RAW data and processing TXT records can use the same code
  * Input: A pointer to a stream we are reading; number of chunks (0 if any
@@ -192,7 +227,11 @@
 	int x; /* counter used to make sure a txt record is not too long */
 	int32 look;
         js_string *out;
-        
+       
+	if(stream->tilde_handling == 103) {
+		csv2_allow_tilde(stream);
+	}
+ 
         if(csv2_get_1st(stream,csv2_is_txtchar,0) != JS_SUCCESS) {
                 return 0;
         }
@@ -287,7 +326,8 @@
 		look = csv2_read_unicode(stream);
 
 		/* EOF outside */
-		if(look == -2 && state != TXT_GET_STATE) {
+		if(look == -2 && state != TXT_GET_STATE &&
+                   state != TXT_BETWEEN_CHUNKS_STATE) {
 			csv2_error(stream,"Unterminated RR");
 			js_destroy(out);
 			return 0;
@@ -300,33 +340,14 @@
 					return 0;
 				}
 			}
-			else if(csv2_is_delimiter(look) || 
+			else if((csv2_is_delimiter(look) && 
+                                 stream->tilde_handling != 103) || 
+                             (look == '~' && stream->tilde_handling == 103) ||
+                             (csv2_is_delimiter(look) && 
+                                 numchunks == -1) || 
 					look == -2 /* EOF */) {
-				/* TXT length */
-				if(numchunks != -1) {
-				    if(txt_len_place < 0 || 
-			                txt_len_place > out->max_count) {
-						js_destroy(out);
-						return 0;
-					}
-				    if(txt_len > 255) {
-					csv2_error(stream,
-					   "Single TXT chunk too long");
-					js_destroy(out);
-					return 0;
-				    }
-				    *(out->string + txt_len_place) = txt_len;
-                                    chunkcount++;
-				}
-                                /* This if only affects HINFO and other obscure
-                                   RRs */
-                                if(numchunks > 0 && chunkcount != numchunks) {
-                                    csv2_error(stream,
-                                    "Incorrect number of chunks for this RR");
-                                    js_destroy(out);
-                                    return 0;
-                                    }
-				return out;
+				return csv2_finalize_txt(stream,numchunks,
+                                       chunkcount,txt_len_place,txt_len,out);
 			}
 			/* [\\] */
 			else if(csv2_is_bslash(look)) {
@@ -337,7 +358,7 @@
 				state = TXT_QUOTE_STATE;
 			}
 			/* [\;]: New TXT field, only if non-RAW RR */
-			else if(csv2_is_semicolon(look) && numchunks != -1) {
+			else if(look == ';' && numchunks != -1) {
 				if(txt_len_place < 0 || 
 				    txt_len_place > out->max_count) {
 					js_destroy(out);
@@ -353,34 +374,17 @@
 				}
 				state = TXT_GET_STATE;
 			}
+			else if(stream->tilde_handling == 103 &&
+				csv2_is_delimiter(look) && look != '|') {
+				state = TXT_BETWEEN_CHUNKS_STATE;
+			}
 			/* [\#]: Comment */
 			else if(csv2_is_hash(look)) {
 				process_comment(stream);
-				/* TXT length */
-				if(numchunks != -1) {
-				    if(txt_len_place < 0 || 
-			                txt_len_place > out->max_count) {
-						js_destroy(out);
-						return 0;
-					}
-				    if(txt_len > 255) {
-					csv2_error(stream,
-					   "Single TXT chunk too long");
-					js_destroy(out);
-					return 0;
-				    }
-				    *(out->string + txt_len_place) = txt_len;
-                                    chunkcount++;
-				}
-                                /* This if only affects HINFO and other obscure
-                                   RRs */
-                                if(numchunks > 0 && chunkcount != numchunks) {
-                                    csv2_error(stream,
-                                    "Incorrect number of chunks for this RR");
-                                    js_destroy(out);
-                                    return 0;
+				if(stream->tilde_handling != 103) {
+				    return csv2_finalize_txt(stream,numchunks,
+                                       chunkcount,txt_len_place,txt_len,out);
                                     }
-				return out;
 			}
 			/* Syntax error */
 			else {
@@ -420,6 +424,11 @@
                            "The | character is not allowed in TXT records\n"
                            "Please use the '\\x7c' escape sequence instead.");
                            		return 0;
+				} else if(look == '~') {
+					csv2_error(stream,
+                           "The ~ character is not allowed in TXT records\n"
+                           "Please use the '\\x7e' escape sequence instead.");
+                           		return 0;
 				} else if(look == 127) {
 					csv2_error(stream,
                            "The DEL character is not allowed in TXT records\n"
@@ -605,6 +614,53 @@
 				return (0);
 			}
 		}
+		else if(state == TXT_BETWEEN_CHUNKS_STATE) { 
+			/* [\#] */
+			if(csv2_is_hash(look)) {
+				process_comment(stream);
+			/* [\~] or EOF */
+			} else if(look == '~' || look == -2 /* EOF */) {
+				return csv2_finalize_txt(stream,numchunks,
+				chunkcount,txt_len_place,txt_len,out);
+			/* [\\\'] */
+			} else if(look == '\'' || look == '\\') {
+				if(txt_len_place < 0 || 
+				    txt_len_place > out->max_count) {
+					js_destroy(out);
+					return 0;
+				}
+				*(out->string + txt_len_place) = txt_len;
+				chunkcount++;
+				txt_len_place = out->unit_count; /* 1 */
+				txt_len = 0;
+				if(csv2_append_utf8(out, 0) == JS_ERROR) {
+					js_destroy(out);
+					return 0;
+				}
+				if(look == '\'') {
+					state = TXT_QUOTE_STATE;
+				} else if(look == '\\') {
+					state = TXT_BSLASH_STATE1;
+				} else {
+					printf(
+				"Fatal error in BETWEEN_CHUNK state!\n");
+					exit(1);
+				}	
+			/* [\|] */
+			} else if(look == '|') {
+				csv2_error(stream,
+                                "Pipes are not allowed between TXT chunks");
+				js_destroy(out);
+				return 0;
+			} else if(!csv2_is_delimiter(look)) {
+				csv2_error(stream,
+"Invalid character between chunks; this might be caused by a TXT RR not\n"
+"terminated by a ~ character");
+				js_destroy(out);
+				return 0;
+			}
+			
+		}
 	}
 
 	/* We should never get here */