Mercurial > hg > nginx-quic
annotate src/mail/ngx_mail_parse.c @ 1609:7bdf156fc852 stable-0.5
r1491 merge:
add rpath for Solaris
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 29 Oct 2007 14:32:18 +0000 |
parents | b19709ee1f52 |
children | 59e1caf2be94 ed9cd1702f7a |
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 | |
1502 | 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: | |
1512 | 437 if (ch == ' ' && s->quoted) { |
438 break; | |
439 } | |
440 | |
527 | 441 switch (ch) { |
442 case '"': | |
443 if (!s->quoted) { | |
444 break; | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
445 } |
527 | 446 s->quoted = 0; |
447 /* fall through */ | |
448 case ' ': | |
449 case CR: | |
450 case LF: | |
451 arg = ngx_array_push(&s->args); | |
452 if (arg == NULL) { | |
453 return NGX_ERROR; | |
454 } | |
455 arg->len = p - s->arg_start; | |
456 arg->data = s->arg_start; | |
457 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
|
458 |
527 | 459 switch (ch) { |
460 case '"': | |
461 case ' ': | |
462 state = sw_spaces_before_argument; | |
463 break; | |
464 case CR: | |
465 state = sw_almost_done; | |
466 break; | |
467 case LF: | |
468 goto done; | |
469 } | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
470 break; |
543 | 471 case '\\': |
472 if (s->quoted) { | |
473 s->backslash = 1; | |
474 state = sw_backslash; | |
475 } | |
476 break; | |
477 } | |
478 break; | |
479 | |
480 case sw_backslash: | |
481 switch (ch) { | |
482 case CR: | |
483 case LF: | |
484 goto invalid; | |
485 default: | |
486 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
|
487 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
488 break; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
489 |
527 | 490 case sw_literal: |
491 if (ch >= '0' && ch <= '9') { | |
492 s->literal_len = s->literal_len * 10 + (ch - '0'); | |
493 break; | |
494 } | |
495 if (ch == '}') { | |
496 state = sw_start_literal_argument; | |
497 break; | |
498 } | |
543 | 499 if (ch == '+') { |
500 state = sw_no_sync_literal_argument; | |
501 break; | |
502 } | |
503 goto invalid; | |
504 | |
505 case sw_no_sync_literal_argument: | |
506 if (ch == '}') { | |
507 s->no_sync_literal = 1; | |
508 state = sw_start_literal_argument; | |
509 break; | |
510 } | |
527 | 511 goto invalid; |
512 | |
513 case sw_start_literal_argument: | |
514 switch (ch) { | |
515 case CR: | |
516 break; | |
517 case LF: | |
518 s->buffer->pos = p + 1; | |
519 s->arg_start = p + 1; | |
543 | 520 if (s->no_sync_literal == 0) { |
521 s->state = sw_literal_argument; | |
522 return NGX_IMAP_NEXT; | |
523 } | |
524 state = sw_literal_argument; | |
525 s->no_sync_literal = 0; | |
526 break; | |
527 default: | |
528 goto invalid; | |
527 | 529 } |
543 | 530 break; |
527 | 531 |
532 case sw_literal_argument: | |
529 | 533 if (s->literal_len && --s->literal_len) { |
527 | 534 break; |
535 } | |
536 | |
537 arg = ngx_array_push(&s->args); | |
538 if (arg == NULL) { | |
539 return NGX_ERROR; | |
540 } | |
541 arg->len = p + 1 - s->arg_start; | |
542 arg->data = s->arg_start; | |
543 s->arg_start = NULL; | |
544 state = sw_end_literal_argument; | |
545 | |
546 break; | |
547 | |
548 case sw_end_literal_argument: | |
549 switch (ch) { | |
550 case '{': | |
551 if (s->args.nelts <= 2) { | |
552 state = sw_literal; | |
553 break; | |
554 } | |
555 goto invalid; | |
556 case CR: | |
557 state = sw_almost_done; | |
558 break; | |
559 case LF: | |
560 goto done; | |
561 default: | |
529 | 562 state = sw_spaces_before_argument; |
563 break; | |
527 | 564 } |
565 break; | |
566 | |
567 case sw_almost_done: | |
568 switch (ch) { | |
569 case LF: | |
570 goto done; | |
571 default: | |
572 goto invalid; | |
573 } | |
574 } | |
575 } | |
576 | |
577 s->buffer->pos = p; | |
578 s->state = state; | |
579 | |
580 return NGX_AGAIN; | |
581 | |
582 done: | |
583 | |
584 s->buffer->pos = p + 1; | |
585 | |
586 if (s->arg_start) { | |
587 arg = ngx_array_push(&s->args); | |
588 if (arg == NULL) { | |
589 return NGX_ERROR; | |
590 } | |
591 arg->len = s->arg_end - s->arg_start; | |
592 arg->data = s->arg_start; | |
543 | 593 |
527 | 594 s->arg_start = NULL; |
595 s->cmd_start = NULL; | |
596 s->quoted = 0; | |
543 | 597 s->no_sync_literal = 0; |
527 | 598 s->literal_len = 0; |
599 } | |
600 | |
1502 | 601 s->state = (s->command != NGX_IMAP_AUTHENTICATE) ? sw_start : sw_argument; |
527 | 602 |
603 return NGX_OK; | |
604 | |
605 invalid: | |
606 | |
607 s->state = sw_start; | |
608 s->quoted = 0; | |
543 | 609 s->no_sync_literal = 0; |
527 | 610 s->literal_len = 0; |
611 | |
1136 | 612 return NGX_MAIL_PARSE_INVALID_COMMAND; |
527 | 613 } |
614 | |
615 | |
1136 | 616 ngx_int_t ngx_smtp_parse_command(ngx_mail_session_t *s) |
527 | 617 { |
618 u_char ch, *p, *c, c0, c1, c2, c3; | |
619 ngx_str_t *arg; | |
620 enum { | |
621 sw_start = 0, | |
622 sw_spaces_before_argument, | |
623 sw_argument, | |
624 sw_almost_done | |
625 } state; | |
626 | |
627 state = s->state; | |
628 | |
629 for (p = s->buffer->pos; p < s->buffer->last; p++) { | |
630 ch = *p; | |
631 | |
632 switch (state) { | |
633 | |
1136 | 634 /* SMTP command */ |
527 | 635 case sw_start: |
636 if (ch == ' ' || ch == CR || ch == LF) { | |
637 c = s->buffer->start; | |
638 | |
639 if (p - c == 4) { | |
640 | |
641 c0 = ngx_toupper(c[0]); | |
642 c1 = ngx_toupper(c[1]); | |
643 c2 = ngx_toupper(c[2]); | |
644 c3 = ngx_toupper(c[3]); | |
645 | |
1136 | 646 if (c0 == 'H' && c1 == 'E' && c2 == 'L' && c3 == 'O') |
527 | 647 { |
1136 | 648 s->command = NGX_SMTP_HELO; |
527 | 649 |
1136 | 650 } else if (c0 == 'E' && c1 == 'H' && c2 == 'L' && c3 == 'O') |
800 | 651 { |
1136 | 652 s->command = NGX_SMTP_EHLO; |
800 | 653 |
527 | 654 } else if (c0 == 'Q' && c1 == 'U' && c2 == 'I' && c3 == 'T') |
655 { | |
1136 | 656 s->command = NGX_SMTP_QUIT; |
527 | 657 |
809 | 658 } else if (c0 == 'A' && c1 == 'U' && c2 == 'T' && c3 == 'H') |
659 { | |
1136 | 660 s->command = NGX_SMTP_AUTH; |
809 | 661 |
527 | 662 } else if (c0 == 'N' && c1 == 'O' && c2 == 'O' && c3 == 'P') |
663 { | |
1136 | 664 s->command = NGX_SMTP_NOOP; |
665 | |
666 } else if (c0 == 'M' && c1 == 'A' && c2 == 'I' && c3 == 'L') | |
583 | 667 { |
1136 | 668 s->command = NGX_SMTP_MAIL; |
669 | |
670 } else if (c0 == 'R' && c1 == 'S' && c2 == 'E' && c3 == 'T') | |
671 { | |
672 s->command = NGX_SMTP_RSET; | |
673 | |
1501 | 674 } else if (c0 == 'R' && c1 == 'C' && c2 == 'P' && c3 == 'T') |
675 { | |
676 s->command = NGX_SMTP_RCPT; | |
677 | |
678 } else if (c0 == 'V' && c1 == 'R' && c2 == 'F' && c3 == 'Y') | |
679 { | |
680 s->command = NGX_SMTP_VRFY; | |
681 | |
682 } else if (c0 == 'E' && c1 == 'X' && c2 == 'P' && c3 == 'N') | |
683 { | |
684 s->command = NGX_SMTP_EXPN; | |
685 | |
686 } else if (c0 == 'H' && c1 == 'E' && c2 == 'L' && c3 == 'P') | |
687 { | |
688 s->command = NGX_SMTP_HELP; | |
689 | |
527 | 690 } else { |
691 goto invalid; | |
692 } | |
1501 | 693 #if (NGX_MAIL_SSL) |
694 } else if (p - c == 8) { | |
527 | 695 |
1501 | 696 if ((c[0] == 'S'|| c[0] == 's') |
697 && (c[1] == 'T'|| c[1] == 't') | |
698 && (c[2] == 'A'|| c[2] == 'a') | |
699 && (c[3] == 'R'|| c[3] == 'r') | |
700 && (c[4] == 'T'|| c[4] == 't') | |
701 && (c[5] == 'T'|| c[5] == 't') | |
702 && (c[6] == 'L'|| c[6] == 'l') | |
703 && (c[7] == 'S'|| c[7] == 's')) | |
704 { | |
705 s->command = NGX_SMTP_STARTTLS; | |
706 | |
707 } else { | |
708 goto invalid; | |
709 } | |
710 #endif | |
527 | 711 } else { |
712 goto invalid; | |
713 } | |
714 | |
715 switch (ch) { | |
716 case ' ': | |
717 state = sw_spaces_before_argument; | |
718 break; | |
719 case CR: | |
720 state = sw_almost_done; | |
721 break; | |
722 case LF: | |
723 goto done; | |
724 } | |
725 break; | |
726 } | |
727 | |
728 if ((ch < 'A' || ch > 'Z') && (ch < 'a' || ch > 'z')) { | |
729 goto invalid; | |
730 } | |
731 | |
732 break; | |
733 | |
734 case sw_spaces_before_argument: | |
735 switch (ch) { | |
736 case ' ': | |
737 break; | |
738 case CR: | |
739 state = sw_almost_done; | |
740 s->arg_end = p; | |
741 break; | |
742 case LF: | |
743 s->arg_end = p; | |
744 goto done; | |
745 default: | |
746 if (s->args.nelts <= 2) { | |
747 state = sw_argument; | |
748 s->arg_start = p; | |
749 break; | |
750 } | |
751 goto invalid; | |
752 } | |
753 break; | |
754 | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
755 case sw_argument: |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
756 switch (ch) { |
800 | 757 case ' ': |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
758 case CR: |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
759 case LF: |
501 | 760 arg = ngx_array_push(&s->args); |
761 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
|
762 return NGX_ERROR; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
763 } |
527 | 764 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
|
765 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
|
766 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
|
767 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
768 switch (ch) { |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
769 case ' ': |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
770 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
|
771 break; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
772 case CR: |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
773 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
|
774 break; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
775 case LF: |
527 | 776 goto done; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
777 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
778 break; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
779 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
780 default: |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
781 break; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
782 } |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
783 break; |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
784 |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
785 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
|
786 switch (ch) { |
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
787 case LF: |
527 | 788 goto done; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
789 default: |
527 | 790 goto invalid; |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
791 } |
417
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
792 } |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
793 } |
0526206251f6
nginx-0.0.10-2004-09-07-19:29:22 import
Igor Sysoev <igor@sysoev.ru>
parents:
diff
changeset
|
794 |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
795 s->buffer->pos = p; |
527 | 796 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
|
797 |
527 | 798 return NGX_AGAIN; |
799 | |
800 done: | |
801 | |
802 s->buffer->pos = p + 1; | |
803 | |
804 if (s->arg_start) { | |
805 arg = ngx_array_push(&s->args); | |
806 if (arg == NULL) { | |
807 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
|
808 } |
527 | 809 arg->len = s->arg_end - s->arg_start; |
810 arg->data = s->arg_start; | |
811 s->arg_start = NULL; | |
812 } | |
419
47709bff4468
nginx-0.0.10-2004-09-09-19:40:48 import
Igor Sysoev <igor@sysoev.ru>
parents:
417
diff
changeset
|
813 |
1136 | 814 s->state = (s->command != NGX_SMTP_AUTH) ? sw_start : sw_argument; |
815 | |
527 | 816 return NGX_OK; |
817 | |
818 invalid: | |
819 | |
820 s->state = sw_start; | |
810 | 821 s->arg_start = NULL; |
527 | 822 |
1136 | 823 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
|
824 } |