1 |
#include <stdio.h> |
2 |
#include <stdlib.h> |
3 |
#include <string.h> |
4 |
#include <strings.h> |
5 |
#include <ctype.h> |
6 |
#include <sys/types.h> |
7 |
#include <regex.h> |
8 |
#include <setjmp.h> |
9 |
|
10 |
#include "config.h" |
11 |
#include "imapfilter.h" |
12 |
#include "buffer.h" |
13 |
|
14 |
|
15 |
extern conn_t connpri, connaux; |
16 |
extern jmp_buf acctloop; |
17 |
|
18 |
buffer_t ibuf; /* Input buffer. */ |
19 |
|
20 |
|
21 |
void receive_response(conn_t * conn, char *buf); |
22 |
void bye_response(char *buf); |
23 |
int analyze_response(conn_t * conn, char *buf); |
24 |
|
25 |
|
26 |
/* |
27 |
* Read one packet of data that the server sent. |
28 |
*/ |
29 |
void |
30 |
receive_response(conn_t * conn, char *buf) |
31 |
{ |
32 |
if (socket_read(conn, buf) == ERROR_NETWORK) |
33 |
longjmp(acctloop, -1); |
34 |
|
35 |
#ifdef DEBUG |
36 |
fprintf(stderr, "debug: getting response (%s):\n\n%s\n", |
37 |
(conn == &connpri ? "primary" : "auxiliary"), buf); |
38 |
#endif |
39 |
} |
40 |
|
41 |
|
42 |
/* |
43 |
* Get server response to client's request. |
44 |
*/ |
45 |
int |
46 |
server_response(conn_t * conn, unsigned int tag) |
47 |
{ |
48 |
reset_buffer(&ibuf); |
49 |
|
50 |
do { |
51 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
52 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
53 |
bye_response(ibuf.data); |
54 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
55 |
|
56 |
return analyze_response(conn, ibuf.data); |
57 |
} |
58 |
|
59 |
|
60 |
/* |
61 |
* Check if server sent a BYE response (connection is closed immediately). |
62 |
*/ |
63 |
void |
64 |
bye_response(char *buf) |
65 |
{ |
66 |
if (strcasestr(buf, "* BYE")) |
67 |
longjmp(acctloop, -1); |
68 |
} |
69 |
|
70 |
|
71 |
/* |
72 |
* Process the greeting that server sends during connection. |
73 |
*/ |
74 |
int |
75 |
greeting_response(conn_t * conn) |
76 |
{ |
77 |
reset_buffer(&ibuf); |
78 |
|
79 |
receive_response(conn, ibuf.data); |
80 |
|
81 |
verbose("%s: %s", (conn == &connpri ? "S" : "s"), ibuf); |
82 |
|
83 |
bye_response(ibuf.data); |
84 |
|
85 |
if (strcasestr(ibuf.data, "* PREAUTH")) |
86 |
return RESPONSE_PREAUTH; |
87 |
|
88 |
return RESPONSE_OK; |
89 |
} |
90 |
|
91 |
|
92 |
/* |
93 |
* Process the data that server sent due to IMAP LOGOUT client request. |
94 |
*/ |
95 |
int |
96 |
logout_response(conn_t * conn, unsigned int tag) |
97 |
{ |
98 |
reset_buffer(&ibuf); |
99 |
|
100 |
do { |
101 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
102 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
103 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
104 |
|
105 |
return analyze_response(conn, ibuf.data); |
106 |
} |
107 |
|
108 |
|
109 |
/* |
110 |
* Process the data that server sent due to IMAP CAPABILITY client request. |
111 |
*/ |
112 |
int |
113 |
capability_response(conn_t * conn, unsigned int tag) |
114 |
{ |
115 |
reset_buffer(&ibuf); |
116 |
|
117 |
do { |
118 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
119 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
120 |
bye_response(ibuf.data); |
121 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
122 |
|
123 |
if (!strcasestr(ibuf.data, "IMAP4rev1")) { |
124 |
error("server does not support IMAP4rev1 protocol\n"); |
125 |
return -2; |
126 |
} |
127 |
if (strcasestr(ibuf.data, "NAMESPACE")) |
128 |
conn->caps |= CAPABILITY_NAMESPACE; |
129 |
#ifdef CRAM_MD5 |
130 |
if (strcasestr(ibuf.data, "AUTH=CRAM-MD5")) |
131 |
conn->caps |= CAPABILITY_AUTH_CRAM_MD5; |
132 |
#endif |
133 |
#ifdef SSL_TLS |
134 |
if (strcasestr(ibuf.data, "STARTTLS")) |
135 |
conn->caps |= CAPABILITY_STARTTLS; |
136 |
#endif |
137 |
return analyze_response(conn, ibuf.data); |
138 |
} |
139 |
|
140 |
|
141 |
#ifdef CRAM_MD5 |
142 |
/* |
143 |
* Process the data that server sent due to IMAP AUTHENTICATE client request. |
144 |
*/ |
145 |
int |
146 |
authenticate_response(conn_t * conn, unsigned int tag, unsigned char **cont) |
147 |
{ |
148 |
int i; |
149 |
char *c; |
150 |
|
151 |
reset_buffer(&ibuf); |
152 |
|
153 |
do { |
154 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
155 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
156 |
bye_response(ibuf.data); |
157 |
} while (strlen(ibuf.data) == RESPONSE_BUF && |
158 |
!strcasestr(ibuf.data, ultostr(tag, 16))); |
159 |
|
160 |
if (cont != NULL && ibuf.data[0] == '+' && ibuf.data[1] == ' ') { |
161 |
c = *cont = (unsigned char *)xmalloc(strlen(ibuf.data) + 1); |
162 |
|
163 |
for (i = 2; ibuf.data[i] != '\r'; i++) |
164 |
*c++ = ibuf.data[i]; |
165 |
|
166 |
*c = '\0'; |
167 |
} |
168 |
return analyze_response(conn, ibuf.data); |
169 |
} |
170 |
#endif |
171 |
|
172 |
|
173 |
/* |
174 |
* Process the data that server sent due to IMAP NAMESPACE client request. |
175 |
*/ |
176 |
int |
177 |
namespace_response(conn_t * conn, unsigned int tag) |
178 |
{ |
179 |
char *c, *d; |
180 |
|
181 |
reset_buffer(&ibuf); |
182 |
|
183 |
do { |
184 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
185 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
186 |
bye_response(ibuf.data); |
187 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
188 |
|
189 |
if ((c = strcasestr(ibuf.data, "* NAMESPACE"))) { |
190 |
c += 12; |
191 |
if (strncasecmp(c, "NIL", 3)) { |
192 |
c = strchr(c, '"') + 1; |
193 |
d = strchr(c, '"') + 1; |
194 |
|
195 |
strncat(conn->nsp.prefix, c, d - c - 1); |
196 |
conn->nsp.delim = *(strchr(d, '"') + 1); |
197 |
} |
198 |
} |
199 |
#ifdef DEBUG |
200 |
fprintf(stderr, "debug: namespace (%s): '%s' '%c'\n", |
201 |
(conn == &connpri ? "primary" : "auxiliary"), conn->nsp.prefix, |
202 |
conn->nsp.delim); |
203 |
#endif |
204 |
return analyze_response(conn, ibuf.data); |
205 |
} |
206 |
|
207 |
|
208 |
/* |
209 |
* Process the data that server sent due to IMAP STATUS client request. |
210 |
*/ |
211 |
int |
212 |
status_response(conn_t * conn, unsigned int tag, char *mbox) |
213 |
{ |
214 |
int r; |
215 |
unsigned int exist, recent, unseen; |
216 |
char *c; |
217 |
|
218 |
exist = recent = unseen = 0; |
219 |
|
220 |
reset_buffer(&ibuf); |
221 |
|
222 |
do { |
223 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
224 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
225 |
bye_response(ibuf.data); |
226 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
227 |
|
228 |
r = analyze_response(conn, ibuf.data); |
229 |
|
230 |
if (r == RESPONSE_NO) |
231 |
return -2; |
232 |
|
233 |
if ((c = strcasestr(ibuf.data, "MESSAGES"))) { |
234 |
c += 9; |
235 |
exist = strtoul(c, NULL, 10); |
236 |
} |
237 |
if ((c = strcasestr(ibuf.data, "RECENT"))) { |
238 |
c += 7; |
239 |
recent = strtoul(c, NULL, 10); |
240 |
} |
241 |
if ((c = strcasestr(ibuf.data, "UNSEEN"))) { |
242 |
c += 7; |
243 |
unseen = strtoul(c, NULL, 10); |
244 |
} |
245 |
if (exist == 0) { |
246 |
info("No messages in mailbox \"%s\".\n", mbox); |
247 |
return -2; |
248 |
} |
249 |
info("%d message%s, %d recent, %d unseen, in mailbox \"%s\".\n", exist, |
250 |
plural(exist), recent, unseen, mbox); |
251 |
|
252 |
return r; |
253 |
} |
254 |
|
255 |
|
256 |
/* |
257 |
* Process the data that server sent due to IMAP SELECT client request. |
258 |
*/ |
259 |
int |
260 |
select_response(conn_t * conn, unsigned int tag) |
261 |
{ |
262 |
reset_buffer(&ibuf); |
263 |
|
264 |
do { |
265 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
266 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
267 |
bye_response(ibuf.data); |
268 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
269 |
|
270 |
if (strcasestr(ibuf.data, "[READ-ONLY]")) |
271 |
return RESPONSE_READONLY; |
272 |
|
273 |
return analyze_response(conn, ibuf.data); |
274 |
} |
275 |
|
276 |
|
277 |
/* |
278 |
* Process the data that server sent due to IMAP SEARCH client request. |
279 |
*/ |
280 |
int |
281 |
search_response(conn_t * conn, unsigned int tag, char **mesgs) |
282 |
{ |
283 |
char *c, *m; |
284 |
unsigned int blen; |
285 |
|
286 |
reset_buffer(&ibuf); |
287 |
|
288 |
do { |
289 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
290 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
291 |
bye_response(ibuf.data); |
292 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
293 |
|
294 |
if ((c = strcasestr(ibuf.data, "* SEARCH "))) { |
295 |
blen = strlen(ibuf.data); |
296 |
|
297 |
m = *mesgs = (char *)xmalloc(blen + 1); |
298 |
|
299 |
c += 9; |
300 |
|
301 |
while (*c != '\0' && (isdigit(*c) || *c == ' ')) |
302 |
*(m++) = *(c++); |
303 |
|
304 |
*m = 0; |
305 |
} |
306 |
return analyze_response(conn, ibuf.data); |
307 |
} |
308 |
|
309 |
|
310 |
/* |
311 |
* Process the data that server sent due to IMAP FETCH client request. |
312 |
*/ |
313 |
int |
314 |
fetch_response(conn_t * conn, unsigned int tag, int reset, char *fetch) |
315 |
{ |
316 |
unsigned int i; |
317 |
static unsigned int s; |
318 |
char *b; |
319 |
|
320 |
if (reset) { |
321 |
s = 0; |
322 |
return 0; |
323 |
} |
324 |
i = 0; |
325 |
|
326 |
reset_buffer(&ibuf); |
327 |
|
328 |
do { |
329 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
330 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
331 |
bye_response(ibuf.data); |
332 |
} while (strlen(ibuf.data) < RESPONSE_BUF && |
333 |
!strcasestr(ibuf.data, ultostr(tag, 16))); |
334 |
|
335 |
b = ibuf.data; |
336 |
|
337 |
if (s == 0) { |
338 |
if ((b = strstr(b, "}\r\n"))) { |
339 |
while (b - ibuf.data > 0) |
340 |
if (*--b == '{') |
341 |
break; |
342 |
s = atoi(++b) - 2; |
343 |
b = strchr(b, '}'); |
344 |
b += 3; |
345 |
} else { |
346 |
return RESPONSE_NULLBODY; /* Null body. */ |
347 |
} |
348 |
} |
349 |
while (*b != '\0' && s-- != 0) |
350 |
fetch[i++] = *(b++); |
351 |
|
352 |
fetch[i] = '\0'; |
353 |
|
354 |
return analyze_response(conn, ibuf.data); |
355 |
} |
356 |
|
357 |
|
358 |
/* |
359 |
* Process the data that server sent due to IMAP FETCH RFC822.SIZE client |
360 |
* request. |
361 |
*/ |
362 |
int |
363 |
fetchsize_response(conn_t * conn, unsigned int *size, unsigned int tag) |
364 |
{ |
365 |
char *c; |
366 |
|
367 |
*size = 0; |
368 |
|
369 |
reset_buffer(&ibuf); |
370 |
|
371 |
do { |
372 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
373 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
374 |
bye_response(ibuf.data); |
375 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
376 |
|
377 |
if ((c = strcasestr(ibuf.data, "FETCH (RFC822.SIZE "))) { |
378 |
c += 19; |
379 |
*size = strtoul(c, NULL, 10); |
380 |
} |
381 |
return analyze_response(conn, ibuf.data); |
382 |
} |
383 |
|
384 |
|
385 |
/* |
386 |
* Process the data that server sent due to IMAP APPEND client request. |
387 |
*/ |
388 |
int |
389 |
append_response(conn_t * conn, unsigned int tag) |
390 |
{ |
391 |
int r; |
392 |
|
393 |
r = RESPONSE_OK; |
394 |
|
395 |
reset_buffer(&ibuf); |
396 |
|
397 |
do { |
398 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
399 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
400 |
bye_response(ibuf.data); |
401 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
402 |
|
403 |
if ((r = analyze_response(conn, ibuf.data)) == RESPONSE_NO && |
404 |
strcasestr(ibuf.data, "[TRYCREATE]")) |
405 |
return RESPONSE_TRYCREATE; |
406 |
|
407 |
return r; |
408 |
} |
409 |
|
410 |
|
411 |
/* |
412 |
* Process the data that server sent due to IMAP COPY client request. |
413 |
*/ |
414 |
int |
415 |
copy_response(conn_t * conn, unsigned int tag) |
416 |
{ |
417 |
int r; |
418 |
|
419 |
r = RESPONSE_OK; |
420 |
|
421 |
reset_buffer(&ibuf); |
422 |
|
423 |
do { |
424 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
425 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
426 |
bye_response(ibuf.data); |
427 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
428 |
|
429 |
if ((r = analyze_response(conn, ibuf.data)) == RESPONSE_NO && |
430 |
strcasestr(ibuf.data, "[TRYCREATE]")) |
431 |
return RESPONSE_TRYCREATE; |
432 |
|
433 |
return r; |
434 |
} |
435 |
|
436 |
|
437 |
/* |
438 |
* Check if response of server to client's request was succesfully |
439 |
* delivered or there was some kind of error. |
440 |
*/ |
441 |
int |
442 |
analyze_response(conn_t * conn, char *buf) |
443 |
{ |
444 |
int r; |
445 |
regex_t creg; |
446 |
regmatch_t match[3]; |
447 |
const char *reg; |
448 |
char result[RESULT_BUF]; |
449 |
|
450 |
r = RESPONSE_OK; |
451 |
reg = "[[:xdigit:]]{8,8} ((OK|NO|BAD)[[:print:]]*)\r\n"; |
452 |
result[0] = '\0'; |
453 |
|
454 |
regcomp(&creg, reg, REG_EXTENDED | REG_ICASE); |
455 |
|
456 |
if (!regexec(&creg, buf, 3, match, 0)) { |
457 |
strncat(result, buf + match[1].rm_so, |
458 |
min(match[1].rm_eo - match[1].rm_so, RESULT_BUF - 1)); |
459 |
|
460 |
if (!strncasecmp(buf + match[2].rm_so, "NO", 2)) |
461 |
r = RESPONSE_NO; |
462 |
else if (!strncasecmp(buf + match[2].rm_so, "BAD", 3)) |
463 |
r = RESPONSE_BAD; |
464 |
|
465 |
verbose("%s: %s", (conn == &connpri ? "S" : "s"), |
466 |
buf + match[0].rm_so); |
467 |
} else |
468 |
r = RESPONSE_NONE; |
469 |
|
470 |
regfree(&creg); |
471 |
|
472 |
return r; |
473 |
} |