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
|
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "str.h"
#include "array.h"
#include "smtp-parser.h"
#include "smtp-syntax.h"
#include <ctype.h>
/*
* String
*/
int smtp_string_parse(const char *string, const char **value_r,
const char **error_r)
{
struct smtp_parser parser;
*value_r = NULL;
*error_r = NULL;
if (string == NULL || *string == '\0') {
*value_r = "";
return 0;
}
smtp_parser_init(&parser, pool_datastack_create(), string);
if (smtp_parser_parse_string(&parser, value_r) < 0) {
*error_r = parser.error;
return -1;
}
if (parser.cur < parser.end) {
*error_r = "Invalid character in string";
return -1;
}
return 1;
}
void smtp_string_write(string_t *out, const char *value)
{
bool quoted = FALSE;
const unsigned char *p, *pend, *pblock;
size_t begin = str_len(out);
if (value == NULL)
return;
p = (const unsigned char *)value;
pend = p + strlen(value);
while (p < pend) {
pblock = p;
while (p < pend && smtp_char_is_atext(*p))
p++;
if (!quoted && p < pend) {
quoted = TRUE;
str_insert(out, begin, "\"");
}
str_append_data(out, pblock, p-pblock);
if (p >= pend)
break;
i_assert(quoted);
i_assert(smtp_char_is_qpair(*p));
if (!smtp_char_is_qtext(*p))
str_append_c(out, '\\');
str_append_c(out, *p);
p++;
}
if (quoted)
str_append_c(out, '\"');
}
/*
* Xtext encoding
*/
int smtp_xtext_decode(string_t *out, const char *xtext, bool allow_nul,
const char **error_r)
{
struct smtp_parser parser;
if (xtext == NULL || *xtext == '\0')
return 1;
smtp_parser_init(&parser, pool_datastack_create(), xtext);
if (smtp_parser_parse_xtext(&parser, out) < 0) {
*error_r = parser.error;
return -1;
}
if (parser.cur < parser.end) {
*error_r = "Invalid character in xtext";
return -1;
}
if (!allow_nul && strlen(str_c(out)) != str_len(out)) {
*error_r = "Encountered NUL character in xtext";
return -1;
}
return 1;
}
int smtp_xtext_parse(const char *xtext, const char **value_r,
const char **error_r)
{
string_t *value;
int ret;
*value_r = NULL;
*error_r = NULL;
if (xtext == NULL || *xtext == '\0') {
*value_r = "";
return 1;
}
value = t_str_new(256);
ret = smtp_xtext_decode(value, xtext, FALSE, error_r);
if (ret <= 0)
return ret;
*value_r = str_c(value);
return 1;
}
void smtp_xtext_encode(string_t *out, const unsigned char *data, size_t size)
{
const unsigned char *p, *pbegin, *pend;
p = data;
pend = p + size;
while (p < pend) {
pbegin = p;
while (p < pend && smtp_char_is_xtext(*p))
p++;
str_append_data(out, pbegin, p-pbegin);
if (p >= pend)
break;
str_printfa(out, "+%02X", (unsigned int)*p);
p++;
}
}
/*
* HELO domain
*/
int smtp_helo_domain_parse(const char *helo, bool allow_literal,
const char **domain_r)
{
struct smtp_parser parser;
int ret;
smtp_parser_init(&parser, pool_datastack_create(), helo);
ret = smtp_parser_parse_domain(&parser, domain_r);
if (ret == 0) {
if (allow_literal) {
ret = smtp_parser_parse_address_literal(
&parser, domain_r, NULL);
}
}
if (ret <= 0 || (parser.cur < parser.end && *parser.cur != ' '))
return -1;
return 0;
}
/*
* EHLO reply
*/
bool smtp_ehlo_keyword_is_valid(const char *keyword)
{
const char *p;
for (p = keyword; *p != '\0'; p++) {
if (!i_isalnum(*p))
return FALSE;
}
return TRUE;
}
bool smtp_ehlo_param_is_valid(const char *param)
{
const char *p;
for (p = param; *p != '\0'; p++) {
if (!smtp_char_is_ehlo_param(*p))
return FALSE;
}
return TRUE;
}
bool smtp_ehlo_params_are_valid(const char *const *params)
{
if (params == NULL)
return TRUE;
while (*params != NULL) {
if (!smtp_ehlo_param_is_valid(*params))
return FALSE;
params++;
}
return TRUE;
}
bool smtp_ehlo_params_str_is_valid(const char *params)
{
const char *p;
bool space = FALSE;
for (p = params; *p != '\0'; p++) {
if (*p == ' ') {
if (space)
return FALSE;
space = TRUE;
continue;
}
space = FALSE;
if (!smtp_char_is_ehlo_param(*p))
return FALSE;
}
return TRUE;
}
static int
smtp_parse_ehlo_line(struct smtp_parser *parser, const char **key_r,
const char *const **params_r)
{
const unsigned char *pbegin = parser->cur;
ARRAY_TYPE(const_string) params = ARRAY_INIT;
const char *param;
/* ehlo-line = ehlo-keyword *( SP ehlo-param )
ehlo-keyword = (ALPHA / DIGIT) *(ALPHA / DIGIT / "-")
; additional syntax of ehlo-params depends on
; ehlo-keyword
ehlo-param = 1*(%d33-126)
; any CHAR excluding <SP> and all
; control characters (US-ASCII 0-31 and 127
; inclusive)
*/
if (parser->cur >= parser->end || !i_isalnum(*parser->cur)) {
parser->error = "Unexpected character in EHLO keyword";
return -1;
}
parser->cur++;
while (parser->cur < parser->end &&
(i_isalnum(*parser->cur) || *parser->cur == '-'))
parser->cur++;
*key_r = p_strdup_until(parser->pool, pbegin, parser->cur);
if (parser->cur >= parser->end) {
*params_r = p_new(parser->pool, const char *, 1);
return 1;
}
if (*parser->cur != ' ') {
parser->error = "Unexpected character in EHLO keyword";
return -1;
}
parser->cur++;
pbegin = parser->cur;
p_array_init(¶ms, parser->pool, 32);
while (parser->cur < parser->end) {
if (*parser->cur == ' ') {
if (parser->cur+1 >= parser->end ||
*(parser->cur+1) == ' ') {
parser->error =
"Missing EHLO parameter after ' '";
return -1;
}
param = p_strdup_until(parser->pool, pbegin,
parser->cur);
array_push_back(¶ms, ¶m);
pbegin = parser->cur + 1;
} else if (!smtp_char_is_ehlo_param(*parser->cur)) {
parser->error =
"Unexpected character in EHLO parameter";
return -1;
}
parser->cur++;
}
param = p_strdup_until(parser->pool, pbegin, parser->cur);
array_push_back(¶ms, ¶m);
array_append_zero(¶ms);
*params_r = array_front(¶ms);
return 1;
}
int smtp_ehlo_line_parse(const char *ehlo_line, const char **key_r,
const char *const **params_r, const char **error_r)
{
struct smtp_parser parser;
*key_r = NULL;
*params_r = NULL;
*error_r = NULL;
if (ehlo_line == NULL || *ehlo_line == '\0') {
*error_r = "Parameter is empty";
return -1;
}
smtp_parser_init(&parser, pool_datastack_create(), ehlo_line);
if (smtp_parse_ehlo_line(&parser, key_r, params_r) <= 0) {
*error_r = parser.error;
return -1;
}
return 1;
}
|