Mercurial > hg > nginx-quic
annotate src/mail/ngx_mail_parse.c @ 1369:6254b90eea4b
block reading for level events
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 06 Aug 2007 15:31:00 +0000 |
parents | c4b2c893989d |
children | fdd064faf26a |
rev | line source |
---|---|
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
1 |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
2 /* |
444
42d11f017717
nginx-0.1.0-2004-09-29-20:00:49 import; remove years from copyright
Igor Sysoev <igor@sysoev.ru>
parents:
441
diff
changeset
|
3 * Copyright (C) Igor Sysoev |
441
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
4 */ |
da8c5707af39
nginx-0.1.0-2004-09-28-12:34:51 import; set copyright and remove unused files
Igor Sysoev <igor@sysoev.ru>
parents:
422
diff
changeset
|
5 |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
6 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
7 #include <ngx_config.h> |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
8 #include <ngx_core.h> |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
9 #include <ngx_event.h> |
1136 | 10 #include <ngx_mail.h> |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
11 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
12 |
1136 | 13 ngx_int_t ngx_pop3_parse_command(ngx_mail_session_t *s) |
14 { | |
15 u_char ch, *p, *c, c0, c1, c2, c3; | |
16 ngx_str_t *arg; | |
17 enum { | |
18 sw_start = 0, | |
19 sw_spaces_before_argument, | |
20 sw_argument, | |
21 sw_almost_done | |
22 } state; | |
23 | |
24 state = s->state; | |
25 | |
26 for (p = s->buffer->pos; p < s->buffer->last; p++) { | |
27 ch = *p; | |
28 | |
29 switch (state) { | |
30 | |
31 /* POP3 command */ | |
32 case sw_start: | |
33 if (ch == ' ' || ch == CR || ch == LF) { | |
34 c = s->buffer->start; | |
35 | |
36 if (p - c == 4) { | |
37 | |
38 c0 = ngx_toupper(c[0]); | |
39 c1 = ngx_toupper(c[1]); | |
40 c2 = ngx_toupper(c[2]); | |
41 c3 = ngx_toupper(c[3]); | |
42 | |
43 if (c0 == 'U' && c1 == 'S' && c2 == 'E' && c3 == 'R') | |
44 { | |
45 s->command = NGX_POP3_USER; | |
46 | |
47 } else if (c0 == 'P' && c1 == 'A' && c2 == 'S' && c3 == 'S') | |
48 { | |
49 s->command = NGX_POP3_PASS; | |
50 | |
51 } else if (c0 == 'A' && c1 == 'P' && c2 == 'O' && c3 == 'P') | |
52 { | |
53 s->command = NGX_POP3_APOP; | |
54 | |
55 } else if (c0 == 'Q' && c1 == 'U' && c2 == 'I' && c3 == 'T') | |
56 { | |
57 s->command = NGX_POP3_QUIT; | |
58 | |
59 } else if (c0 == 'C' && c1 == 'A' && c2 == 'P' && c3 == 'A') | |
60 { | |
61 s->command = NGX_POP3_CAPA; | |
62 | |
63 } else if (c0 == 'A' && c1 == 'U' && c2 == 'T' && c3 == 'H') | |
64 { | |
65 s->command = NGX_POP3_AUTH; | |
66 | |
67 } else if (c0 == 'N' && c1 == 'O' && c2 == 'O' && c3 == 'P') | |
68 { | |
69 s->command = NGX_POP3_NOOP; | |
70 #if (NGX_MAIL_SSL) | |
71 } else if (c0 == 'S' && c1 == 'T' && c2 == 'L' && c3 == 'S') | |
72 { | |
73 s->command = NGX_POP3_STLS; | |
74 #endif | |
75 } else { | |
76 goto invalid; | |
77 } | |
78 | |
79 } else { | |
80 goto invalid; | |
81 } | |
82 | |
83 switch (ch) { | |
84 case ' ': | |
85 state = sw_spaces_before_argument; | |
86 break; | |
87 case CR: | |
88 state = sw_almost_done; | |
89 break; | |
90 case LF: | |
91 goto done; | |
92 } | |
93 break; | |
94 } | |
95 | |
96 if ((ch < 'A' || ch > 'Z') && (ch < 'a' || ch > 'z')) { | |
97 goto invalid; | |
98 } | |
99 | |
100 break; | |
101 | |
102 case sw_spaces_before_argument: | |
103 switch (ch) { | |
104 case ' ': | |
105 break; | |
106 case CR: | |
107 state = sw_almost_done; | |
108 s->arg_end = p; | |
109 break; | |
110 case LF: | |
111 s->arg_end = p; | |
112 goto done; | |
113 default: | |
114 if (s->args.nelts <= 2) { | |
115 state = sw_argument; | |
116 s->arg_start = p; | |
117 break; | |
118 } | |
119 goto invalid; | |
120 } | |
121 break; | |
122 | |
123 case sw_argument: | |
124 switch (ch) { | |
125 | |
126 case ' ': | |
127 | |
128 /* | |
129 * the space should be considered as part of the at username | |
130 * or password, but not of argument in other commands | |
131 */ | |
132 | |
133 if (s->command == NGX_POP3_USER | |
134 || s->command == NGX_POP3_PASS) | |
135 { | |
136 break; | |
137 } | |
138 | |
139 /* fall through */ | |
140 | |
141 case CR: | |
142 case LF: | |
143 arg = ngx_array_push(&s->args); | |
144 if (arg == NULL) { | |
145 return NGX_ERROR; | |
146 } | |
147 arg->len = p - s->arg_start; | |
148 arg->data = s->arg_start; | |
149 s->arg_start = NULL; | |
150 | |
151 switch (ch) { | |
152 case ' ': | |
153 state = sw_spaces_before_argument; | |
154 break; | |
155 case CR: | |
156 state = sw_almost_done; | |
157 break; | |
158 case LF: | |
159 goto done; | |
160 } | |
161 break; | |
162 | |
163 default: | |
164 break; | |
165 } | |
166 break; | |
167 | |
168 case sw_almost_done: | |
169 switch (ch) { | |
170 case LF: | |
171 goto done; | |
172 default: | |
173 goto invalid; | |
174 } | |
175 } | |
176 } | |
177 | |
178 s->buffer->pos = p; | |
179 s->state = state; | |
180 | |
181 return NGX_AGAIN; | |
182 | |
183 done: | |
184 | |
185 s->buffer->pos = p + 1; | |
186 | |
187 if (s->arg_start) { | |
188 arg = ngx_array_push(&s->args); | |
189 if (arg == NULL) { | |
190 return NGX_ERROR; | |
191 } | |
192 arg->len = s->arg_end - s->arg_start; | |
193 arg->data = s->arg_start; | |
194 s->arg_start = NULL; | |
195 } | |
196 | |
197 s->state = (s->command != NGX_POP3_AUTH) ? sw_start : sw_argument; | |
198 | |
199 return NGX_OK; | |
200 | |
201 invalid: | |
202 | |
203 s->state = sw_start; | |
204 s->arg_start = NULL; | |
205 | |
206 return NGX_MAIL_PARSE_INVALID_COMMAND; | |
207 } | |
208 | |
209 | |
210 ngx_int_t ngx_imap_parse_command(ngx_mail_session_t *s) | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
211 { |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
212 u_char ch, *p, *c; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
213 ngx_str_t *arg; |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
214 enum { |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
215 sw_start = 0, |
527 | 216 sw_spaces_before_command, |
217 sw_command, | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
218 sw_spaces_before_argument, |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
219 sw_argument, |
543 | 220 sw_backslash, |
527 | 221 sw_literal, |
543 | 222 sw_no_sync_literal_argument, |
527 | 223 sw_start_literal_argument, |
224 sw_literal_argument, | |
225 sw_end_literal_argument, | |
226 sw_almost_done | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
227 } state; |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
228 |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
229 state = s->state; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
230 |
527 | 231 for (p = s->buffer->pos; p < s->buffer->last; p++) { |
232 ch = *p; | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
233 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
234 switch (state) { |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
235 |
527 | 236 /* IMAP tag */ |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
237 case sw_start: |
527 | 238 switch (ch) { |
239 case ' ': | |
240 s->tag.len = p - s->buffer->start + 1; | |
241 s->tag.data = s->buffer->start; | |
242 state = sw_spaces_before_command; | |
243 break; | |
244 case CR: | |
245 s->state = sw_start; | |
1136 | 246 return NGX_MAIL_PARSE_INVALID_COMMAND; |
527 | 247 case LF: |
248 s->state = sw_start; | |
1136 | 249 return NGX_MAIL_PARSE_INVALID_COMMAND; |
527 | 250 } |
251 break; | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
252 |
527 | 253 case sw_spaces_before_command: |
254 switch (ch) { | |
255 case ' ': | |
256 break; | |
257 case CR: | |
258 s->state = sw_start; | |
1136 | 259 return NGX_MAIL_PARSE_INVALID_COMMAND; |
527 | 260 case LF: |
261 s->state = sw_start; | |
1136 | 262 return NGX_MAIL_PARSE_INVALID_COMMAND; |
527 | 263 default: |
264 s->cmd_start = p; | |
265 state = sw_command; | |
266 break; | |
267 } | |
268 break; | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
269 |
527 | 270 case sw_command: |
271 if (ch == ' ' || ch == CR || ch == LF) { | |
272 | |
273 c = s->cmd_start; | |
274 | |
275 switch (p - c) { | |
276 | |
277 case 4: | |
278 if ((c[0] == 'N' || c[0] == 'n') | |
279 && (c[1] == 'O'|| c[1] == 'o') | |
280 && (c[2] == 'O'|| c[2] == 'o') | |
281 && (c[3] == 'P'|| c[3] == 'p')) | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
282 { |
527 | 283 s->command = NGX_IMAP_NOOP; |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
284 |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
285 } else { |
527 | 286 goto invalid; |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
287 } |
527 | 288 break; |
289 | |
290 case 5: | |
291 if ((c[0] == 'L'|| c[0] == 'l') | |
292 && (c[1] == 'O'|| c[1] == 'o') | |
293 && (c[2] == 'G'|| c[2] == 'g') | |
294 && (c[3] == 'I'|| c[3] == 'i') | |
295 && (c[4] == 'N'|| c[4] == 'n')) | |
296 { | |
297 s->command = NGX_IMAP_LOGIN; | |
298 | |
299 } else { | |
300 goto invalid; | |
301 } | |
302 break; | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
303 |
527 | 304 case 6: |
305 if ((c[0] == 'L'|| c[0] == 'l') | |
306 && (c[1] == 'O'|| c[1] == 'o') | |
307 && (c[2] == 'G'|| c[2] == 'g') | |
308 && (c[3] == 'O'|| c[3] == 'o') | |
309 && (c[4] == 'U'|| c[4] == 'u') | |
310 && (c[5] == 'T'|| c[5] == 't')) | |
311 { | |
312 s->command = NGX_IMAP_LOGOUT; | |
313 | |
314 } else { | |
315 goto invalid; | |
316 } | |
317 break; | |
318 | |
1136 | 319 #if (NGX_MAIL_SSL) |
583 | 320 case 8: |
321 if ((c[0] == 'S'|| c[0] == 's') | |
322 && (c[1] == 'T'|| c[1] == 't') | |
323 && (c[2] == 'A'|| c[2] == 'a') | |
324 && (c[3] == 'R'|| c[3] == 'r') | |
325 && (c[4] == 'T'|| c[4] == 't') | |
326 && (c[5] == 'T'|| c[5] == 't') | |
327 && (c[6] == 'L'|| c[6] == 'l') | |
328 && (c[7] == 'S'|| c[7] == 's')) | |
329 { | |
330 s->command = NGX_IMAP_STARTTLS; | |
331 | |
332 } else { | |
333 goto invalid; | |
334 } | |
335 break; | |
336 #endif | |
337 | |
527 | 338 case 10: |
339 if ((c[0] == 'C'|| c[0] == 'c') | |
340 && (c[1] == 'A'|| c[1] == 'a') | |
341 && (c[2] == 'P'|| c[2] == 'p') | |
342 && (c[3] == 'A'|| c[3] == 'a') | |
343 && (c[4] == 'B'|| c[4] == 'b') | |
344 && (c[5] == 'I'|| c[5] == 'i') | |
345 && (c[6] == 'L'|| c[6] == 'l') | |
346 && (c[7] == 'I'|| c[7] == 'i') | |
347 && (c[8] == 'T'|| c[8] == 't') | |
348 && (c[9] == 'Y'|| c[9] == 'y')) | |
349 { | |
350 s->command = NGX_IMAP_CAPABILITY; | |
351 | |
352 } else { | |
353 goto invalid; | |
354 } | |
355 break; | |
356 | |
1323 | 357 case 12: |
358 if ((c[0] == 'A'|| c[0] == 'a') | |
359 && (c[1] == 'U'|| c[1] == 'u') | |
360 && (c[2] == 'T'|| c[2] == 't') | |
361 && (c[3] == 'H'|| c[3] == 'h') | |
362 && (c[4] == 'E'|| c[4] == 'e') | |
363 && (c[5] == 'N'|| c[5] == 'n') | |
364 && (c[6] == 'T'|| c[6] == 't') | |
365 && (c[7] == 'I'|| c[7] == 'i') | |
366 && (c[8] == 'C'|| c[8] == 'c') | |
367 && (c[9] == 'A'|| c[9] == 'a') | |
368 && (c[10] == 'T'|| c[10] == 't') | |
369 && (c[11] == 'E'|| c[11] == 'e')) | |
370 { | |
371 s->command = NGX_IMAP_AUTHENTICATE; | |
372 | |
373 } else { | |
374 goto invalid; | |
375 } | |
376 break; | |
377 | |
527 | 378 default: |
379 goto invalid; | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
380 } |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
381 |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
382 switch (ch) { |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
383 case ' ': |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
384 state = sw_spaces_before_argument; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
385 break; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
386 case CR: |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
387 state = sw_almost_done; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
388 break; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
389 case LF: |
527 | 390 goto done; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
391 } |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
392 break; |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
393 } |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
394 |
527 | 395 if ((ch < 'A' || ch > 'Z') && (ch < 'a' || ch > 'z')) { |
396 goto invalid; | |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
397 } |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
398 |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
399 break; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
400 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
401 case sw_spaces_before_argument: |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
402 switch (ch) { |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
403 case ' ': |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
404 break; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
405 case CR: |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
406 state = sw_almost_done; |
527 | 407 s->arg_end = p; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
408 break; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
409 case LF: |
527 | 410 s->arg_end = p; |
411 goto done; | |
412 case '"': | |
413 if (s->args.nelts <= 2) { | |
414 s->quoted = 1; | |
415 s->arg_start = p + 1; | |
416 state = sw_argument; | |
417 break; | |
418 } | |
419 goto invalid; | |
420 case '{': | |
421 if (s->args.nelts <= 2) { | |
422 state = sw_literal; | |
423 break; | |
424 } | |
425 goto invalid; | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
426 default: |
527 | 427 if (s->args.nelts <= 2) { |
428 s->arg_start = p; | |
429 state = sw_argument; | |
430 break; | |
431 } | |
432 goto invalid; | |
433 } | |
434 break; | |
435 | |
436 case sw_argument: | |
437 switch (ch) { | |
438 case '"': | |
439 if (!s->quoted) { | |
440 break; | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
441 } |
527 | 442 s->quoted = 0; |
443 /* fall through */ | |
444 case ' ': | |
445 case CR: | |
446 case LF: | |
447 arg = ngx_array_push(&s->args); | |
448 if (arg == NULL) { | |
449 return NGX_ERROR; | |
450 } | |
451 arg->len = p - s->arg_start; | |
452 arg->data = s->arg_start; | |
453 s->arg_start = NULL; | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
454 |
527 | 455 switch (ch) { |
456 case '"': | |
457 case ' ': | |
458 state = sw_spaces_before_argument; | |
459 break; | |
460 case CR: | |
461 state = sw_almost_done; | |
462 break; | |
463 case LF: | |
464 goto done; | |
465 } | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
466 break; |
543 | 467 case '\\': |
468 if (s->quoted) { | |
469 s->backslash = 1; | |
470 state = sw_backslash; | |
471 } | |
472 break; | |
473 } | |
474 break; | |
475 | |
476 case sw_backslash: | |
477 switch (ch) { | |
478 case CR: | |
479 case LF: | |
480 goto invalid; | |
481 default: | |
482 state = sw_argument; | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
483 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
484 break; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
485 |
527 | 486 case sw_literal: |
487 if (ch >= '0' && ch <= '9') { | |
488 s->literal_len = s->literal_len * 10 + (ch - '0'); | |
489 break; | |
490 } | |
491 if (ch == '}') { | |
492 state = sw_start_literal_argument; | |
493 break; | |
494 } | |
543 | 495 if (ch == '+') { |
496 state = sw_no_sync_literal_argument; | |
497 break; | |
498 } | |
499 goto invalid; | |
500 | |
501 case sw_no_sync_literal_argument: | |
502 if (ch == '}') { | |
503 s->no_sync_literal = 1; | |
504 state = sw_start_literal_argument; | |
505 break; | |
506 } | |
527 | 507 goto invalid; |
508 | |
509 case sw_start_literal_argument: | |
510 switch (ch) { | |
511 case CR: | |
512 break; | |
513 case LF: | |
514 s->buffer->pos = p + 1; | |
515 s->arg_start = p + 1; | |
543 | 516 if (s->no_sync_literal == 0) { |
517 s->state = sw_literal_argument; | |
518 return NGX_IMAP_NEXT; | |
519 } | |
520 state = sw_literal_argument; | |
521 s->no_sync_literal = 0; | |
522 break; | |
523 default: | |
524 goto invalid; | |
527 | 525 } |
543 | 526 break; |
527 | 527 |
528 case sw_literal_argument: | |
529 | 529 if (s->literal_len && --s->literal_len) { |
527 | 530 break; |
531 } | |
532 | |
533 arg = ngx_array_push(&s->args); | |
534 if (arg == NULL) { | |
535 return NGX_ERROR; | |
536 } | |
537 arg->len = p + 1 - s->arg_start; | |
538 arg->data = s->arg_start; | |
539 s->arg_start = NULL; | |
540 state = sw_end_literal_argument; | |
541 | |
542 break; | |
543 | |
544 case sw_end_literal_argument: | |
545 switch (ch) { | |
546 case '{': | |
547 if (s->args.nelts <= 2) { | |
548 state = sw_literal; | |
549 break; | |
550 } | |
551 goto invalid; | |
552 case CR: | |
553 state = sw_almost_done; | |
554 break; | |
555 case LF: | |
556 goto done; | |
557 default: | |
529 | 558 state = sw_spaces_before_argument; |
559 break; | |
527 | 560 } |
561 break; | |
562 | |
563 case sw_almost_done: | |
564 switch (ch) { | |
565 case LF: | |
566 goto done; | |
567 default: | |
568 goto invalid; | |
569 } | |
570 } | |
571 } | |
572 | |
573 s->buffer->pos = p; | |
574 s->state = state; | |
575 | |
576 return NGX_AGAIN; | |
577 | |
578 done: | |
579 | |
580 s->buffer->pos = p + 1; | |
581 | |
582 if (s->arg_start) { | |
583 arg = ngx_array_push(&s->args); | |
584 if (arg == NULL) { | |
585 return NGX_ERROR; | |
586 } | |
587 arg->len = s->arg_end - s->arg_start; | |
588 arg->data = s->arg_start; | |
543 | 589 |
527 | 590 s->arg_start = NULL; |
591 s->cmd_start = NULL; | |
592 s->quoted = 0; | |
543 | 593 s->no_sync_literal = 0; |
527 | 594 s->literal_len = 0; |
595 } | |
596 | |
1323 | 597 s->state = (s->command != NGX_IMAP_AUTHENTICATE) ? sw_start : sw_argument; |
527 | 598 |
599 return NGX_OK; | |
600 | |
601 invalid: | |
602 | |
603 s->state = sw_start; | |
604 s->quoted = 0; | |
543 | 605 s->no_sync_literal = 0; |
527 | 606 s->literal_len = 0; |
607 | |
1136 | 608 return NGX_MAIL_PARSE_INVALID_COMMAND; |
527 | 609 } |
610 | |
611 | |
1136 | 612 ngx_int_t ngx_smtp_parse_command(ngx_mail_session_t *s) |
527 | 613 { |
614 u_char ch, *p, *c, c0, c1, c2, c3; | |
615 ngx_str_t *arg; | |
616 enum { | |
617 sw_start = 0, | |
618 sw_spaces_before_argument, | |
619 sw_argument, | |
620 sw_almost_done | |
621 } state; | |
622 | |
623 state = s->state; | |
624 | |
625 for (p = s->buffer->pos; p < s->buffer->last; p++) { | |
626 ch = *p; | |
627 | |
628 switch (state) { | |
629 | |
1136 | 630 /* SMTP command */ |
527 | 631 case sw_start: |
632 if (ch == ' ' || ch == CR || ch == LF) { | |
633 c = s->buffer->start; | |
634 | |
635 if (p - c == 4) { | |
636 | |
637 c0 = ngx_toupper(c[0]); | |
638 c1 = ngx_toupper(c[1]); | |
639 c2 = ngx_toupper(c[2]); | |
640 c3 = ngx_toupper(c[3]); | |
641 | |
1136 | 642 if (c0 == 'H' && c1 == 'E' && c2 == 'L' && c3 == 'O') |
527 | 643 { |
1136 | 644 s->command = NGX_SMTP_HELO; |
527 | 645 |
1136 | 646 } else if (c0 == 'E' && c1 == 'H' && c2 == 'L' && c3 == 'O') |
800 | 647 { |
1136 | 648 s->command = NGX_SMTP_EHLO; |
800 | 649 |
527 | 650 } else if (c0 == 'Q' && c1 == 'U' && c2 == 'I' && c3 == 'T') |
651 { | |
1136 | 652 s->command = NGX_SMTP_QUIT; |
527 | 653 |
809 | 654 } else if (c0 == 'A' && c1 == 'U' && c2 == 'T' && c3 == 'H') |
655 { | |
1136 | 656 s->command = NGX_SMTP_AUTH; |
809 | 657 |
527 | 658 } else if (c0 == 'N' && c1 == 'O' && c2 == 'O' && c3 == 'P') |
659 { | |
1136 | 660 s->command = NGX_SMTP_NOOP; |
661 | |
662 } else if (c0 == 'M' && c1 == 'A' && c2 == 'I' && c3 == 'L') | |
583 | 663 { |
1136 | 664 s->command = NGX_SMTP_MAIL; |
665 | |
666 } else if (c0 == 'R' && c1 == 'S' && c2 == 'E' && c3 == 'T') | |
667 { | |
668 s->command = NGX_SMTP_RSET; | |
669 | |
1322 | 670 } else if (c0 == 'R' && c1 == 'C' && c2 == 'P' && c3 == 'T') |
671 { | |
672 s->command = NGX_SMTP_RCPT; | |
673 | |
674 } else if (c0 == 'V' && c1 == 'R' && c2 == 'F' && c3 == 'Y') | |
675 { | |
676 s->command = NGX_SMTP_VRFY; | |
677 | |
678 } else if (c0 == 'E' && c1 == 'X' && c2 == 'P' && c3 == 'N') | |
679 { | |
680 s->command = NGX_SMTP_EXPN; | |
681 | |
682 } else if (c0 == 'H' && c1 == 'E' && c2 == 'L' && c3 == 'P') | |
683 { | |
684 s->command = NGX_SMTP_HELP; | |
685 | |
527 | 686 } else { |
687 goto invalid; | |
688 } | |
1322 | 689 #if (NGX_MAIL_SSL) |
690 } else if (p - c == 8) { | |
527 | 691 |
1322 | 692 if ((c[0] == 'S'|| c[0] == 's') |
693 && (c[1] == 'T'|| c[1] == 't') | |
694 && (c[2] == 'A'|| c[2] == 'a') | |
695 && (c[3] == 'R'|| c[3] == 'r') | |
696 && (c[4] == 'T'|| c[4] == 't') | |
697 && (c[5] == 'T'|| c[5] == 't') | |
698 && (c[6] == 'L'|| c[6] == 'l') | |
699 && (c[7] == 'S'|| c[7] == 's')) | |
700 { | |
701 s->command = NGX_SMTP_STARTTLS; | |
702 | |
703 } else { | |
704 goto invalid; | |
705 } | |
706 #endif | |
527 | 707 } else { |
708 goto invalid; | |
709 } | |
710 | |
711 switch (ch) { | |
712 case ' ': | |
713 state = sw_spaces_before_argument; | |
714 break; | |
715 case CR: | |
716 state = sw_almost_done; | |
717 break; | |
718 case LF: | |
719 goto done; | |
720 } | |
721 break; | |
722 } | |
723 | |
724 if ((ch < 'A' || ch > 'Z') && (ch < 'a' || ch > 'z')) { | |
725 goto invalid; | |
726 } | |
727 | |
728 break; | |
729 | |
730 case sw_spaces_before_argument: | |
731 switch (ch) { | |
732 case ' ': | |
733 break; | |
734 case CR: | |
735 state = sw_almost_done; | |
736 s->arg_end = p; | |
737 break; | |
738 case LF: | |
739 s->arg_end = p; | |
740 goto done; | |
741 default: | |
742 if (s->args.nelts <= 2) { | |
743 state = sw_argument; | |
744 s->arg_start = p; | |
745 break; | |
746 } | |
747 goto invalid; | |
748 } | |
749 break; | |
750 | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
751 case sw_argument: |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
752 switch (ch) { |
800 | 753 case ' ': |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
754 case CR: |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
755 case LF: |
501 | 756 arg = ngx_array_push(&s->args); |
757 if (arg == NULL) { | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
758 return NGX_ERROR; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
759 } |
527 | 760 arg->len = p - s->arg_start; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
761 arg->data = s->arg_start; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
762 s->arg_start = NULL; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
763 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
764 switch (ch) { |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
765 case ' ': |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
766 state = sw_spaces_before_argument; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
767 break; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
768 case CR: |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
769 state = sw_almost_done; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
770 break; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
771 case LF: |
527 | 772 goto done; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
773 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
774 break; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
775 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
776 default: |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
777 break; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
778 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
779 break; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
780 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
781 case sw_almost_done: |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
782 switch (ch) { |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
783 case LF: |
527 | 784 goto done; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
785 default: |
527 | 786 goto invalid; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
787 } |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
788 } |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
789 } |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
790 |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
791 s->buffer->pos = p; |
527 | 792 s->state = state; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
793 |
527 | 794 return NGX_AGAIN; |
795 | |
796 done: | |
797 | |
798 s->buffer->pos = p + 1; | |
799 | |
800 if (s->arg_start) { | |
801 arg = ngx_array_push(&s->args); | |
802 if (arg == NULL) { | |
803 return NGX_ERROR; | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
804 } |
527 | 805 arg->len = s->arg_end - s->arg_start; |
806 arg->data = s->arg_start; | |
807 s->arg_start = NULL; | |
808 } | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
809 |
1136 | 810 s->state = (s->command != NGX_SMTP_AUTH) ? sw_start : sw_argument; |
811 | |
527 | 812 return NGX_OK; |
813 | |
814 invalid: | |
815 | |
816 s->state = sw_start; | |
810 | 817 s->arg_start = NULL; |
527 | 818 |
1136 | 819 return NGX_MAIL_PARSE_INVALID_COMMAND; |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
820 } |