3 |
#include <string.h> |
#include <string.h> |
4 |
#include <strings.h> |
#include <strings.h> |
5 |
#include <ctype.h> |
#include <ctype.h> |
6 |
#include <sys/types.h> /* IEEE Std 1003.1-2001 non-conformance. */ |
#include <sys/types.h> /* IEEE Std 1003.1-2001 transitional. */ |
7 |
#include <regex.h> |
#include <regex.h> |
8 |
#include <setjmp.h> |
#include <setjmp.h> |
9 |
|
|
12 |
#include "buffer.h" |
#include "buffer.h" |
13 |
|
|
14 |
|
|
15 |
extern conn_t connpri, connaux; |
extern connection_t connpri, connaux; |
16 |
extern jmp_buf acctloop; |
extern jmp_buf acctloop; |
17 |
|
|
18 |
buffer_t ibuf; /* Input buffer. */ |
buffer_t ibuf; /* Input buffer. */ |
19 |
|
|
20 |
|
|
21 |
void receive_response(conn_t * conn, char *buf); |
void receive_response(connection_t * conn, char *buf); |
22 |
void bye_response(char *buf); |
void response_bye(char *buf); |
23 |
int analyze_response(conn_t * conn, char *buf); |
int analyze_response(connection_t * conn, char *buf); |
24 |
|
|
25 |
|
|
26 |
/* |
/* |
27 |
* Read one packet of data that the server sent. |
* Read one packet of data that the server sent. |
28 |
*/ |
*/ |
29 |
void |
void |
30 |
receive_response(conn_t * conn, char *buf) |
receive_response(connection_t * conn, char *buf) |
31 |
{ |
{ |
32 |
if (socket_read(conn, buf) == ERROR_NETWORK) |
if (socket_read(conn, buf) == ERROR_NETWORK) |
33 |
longjmp(acctloop, -1); |
longjmp(acctloop, -1); |
41 |
* Get server response to client's request. |
* Get server response to client's request. |
42 |
*/ |
*/ |
43 |
int |
int |
44 |
server_response(conn_t * conn, unsigned int tag) |
response_generic(connection_t * conn, unsigned int tag) |
45 |
{ |
{ |
46 |
reset_buffer(&ibuf); |
buffer_reset(&ibuf); |
47 |
|
|
48 |
do { |
do { |
49 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
buffer_check(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
50 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
51 |
bye_response(ibuf.data); |
response_bye(ibuf.data); |
52 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
53 |
|
|
54 |
return analyze_response(conn, ibuf.data); |
return analyze_response(conn, ibuf.data); |
59 |
* Check if server sent a BYE response (connection is closed immediately). |
* Check if server sent a BYE response (connection is closed immediately). |
60 |
*/ |
*/ |
61 |
void |
void |
62 |
bye_response(char *buf) |
response_bye(char *buf) |
63 |
{ |
{ |
64 |
if (strcasestr(buf, "* BYE")) |
if (strcasestr(buf, "* BYE")) |
65 |
longjmp(acctloop, -1); |
longjmp(acctloop, -1); |
70 |
* Process the greeting that server sends during connection. |
* Process the greeting that server sends during connection. |
71 |
*/ |
*/ |
72 |
int |
int |
73 |
greeting_response(conn_t * conn) |
response_greeting(connection_t * conn) |
74 |
{ |
{ |
75 |
reset_buffer(&ibuf); |
buffer_reset(&ibuf); |
76 |
|
|
77 |
receive_response(conn, ibuf.data); |
receive_response(conn, ibuf.data); |
78 |
|
|
79 |
verbose("%s: %s", (conn == &connpri ? "S" : "s"), ibuf); |
verbose("%s: %s", (conn == &connpri ? "S" : "s"), ibuf); |
80 |
|
|
81 |
bye_response(ibuf.data); |
response_bye(ibuf.data); |
82 |
|
|
83 |
if (strcasestr(ibuf.data, "* PREAUTH")) |
if (strcasestr(ibuf.data, "* PREAUTH")) |
84 |
return RESPONSE_PREAUTH; |
return RESPONSE_PREAUTH; |
91 |
* Process the data that server sent due to IMAP LOGOUT client request. |
* Process the data that server sent due to IMAP LOGOUT client request. |
92 |
*/ |
*/ |
93 |
int |
int |
94 |
logout_response(conn_t * conn, unsigned int tag) |
response_logout(connection_t * conn, unsigned int tag) |
95 |
{ |
{ |
96 |
reset_buffer(&ibuf); |
buffer_reset(&ibuf); |
97 |
|
|
98 |
do { |
do { |
99 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
buffer_check(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
100 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
101 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
102 |
|
|
108 |
* Process the data that server sent due to IMAP CAPABILITY client request. |
* Process the data that server sent due to IMAP CAPABILITY client request. |
109 |
*/ |
*/ |
110 |
int |
int |
111 |
capability_response(conn_t * conn, unsigned int tag) |
response_capability(connection_t * conn, unsigned int tag) |
112 |
{ |
{ |
113 |
reset_buffer(&ibuf); |
buffer_reset(&ibuf); |
114 |
|
|
115 |
do { |
do { |
116 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
buffer_check(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
117 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
118 |
bye_response(ibuf.data); |
response_bye(ibuf.data); |
119 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
120 |
|
|
121 |
if (!strcasestr(ibuf.data, "IMAP4rev1")) { |
if (!strcasestr(ibuf.data, "IMAP4rev1")) { |
123 |
return -2; |
return -2; |
124 |
} |
} |
125 |
if (strcasestr(ibuf.data, "NAMESPACE")) |
if (strcasestr(ibuf.data, "NAMESPACE")) |
126 |
conn->caps |= CAPABILITY_NAMESPACE; |
conn->caps |= CAPS_NAMESPACE; |
127 |
#ifdef CRAM_MD5 |
#ifdef CRAM_MD5 |
128 |
if (strcasestr(ibuf.data, "AUTH=CRAM-MD5")) |
if (strcasestr(ibuf.data, "AUTH=CRAM-MD5")) |
129 |
conn->caps |= CAPABILITY_AUTH_CRAM_MD5; |
conn->caps |= CAPS_CRAMMD5; |
130 |
#endif |
#endif |
131 |
#ifdef SSL_TLS |
#ifdef SSL_TLS |
132 |
if (strcasestr(ibuf.data, "STARTTLS")) |
if (strcasestr(ibuf.data, "STARTTLS")) |
133 |
conn->caps |= CAPABILITY_STARTTLS; |
conn->caps |= CAPS_STARTTLS; |
134 |
#endif |
#endif |
135 |
return analyze_response(conn, ibuf.data); |
return analyze_response(conn, ibuf.data); |
136 |
} |
} |
141 |
* Process the data that server sent due to IMAP AUTHENTICATE client request. |
* Process the data that server sent due to IMAP AUTHENTICATE client request. |
142 |
*/ |
*/ |
143 |
int |
int |
144 |
authenticate_response(conn_t * conn, unsigned int tag, unsigned char **cont) |
response_authenticate(connection_t * conn, unsigned int tag, unsigned char **cont) |
145 |
{ |
{ |
146 |
int i; |
int i; |
147 |
char *c; |
char *c; |
148 |
|
|
149 |
reset_buffer(&ibuf); |
buffer_reset(&ibuf); |
150 |
|
|
151 |
do { |
do { |
152 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
buffer_check(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
153 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
154 |
bye_response(ibuf.data); |
response_bye(ibuf.data); |
155 |
} while (strlen(ibuf.data) == RESPONSE_BUF && |
} while (strlen(ibuf.data) == RESPONSE_BUF && |
156 |
!strcasestr(ibuf.data, ultostr(tag, 16))); |
!strcasestr(ibuf.data, ultostr(tag, 16))); |
157 |
|
|
172 |
* Process the data that server sent due to IMAP NAMESPACE client request. |
* Process the data that server sent due to IMAP NAMESPACE client request. |
173 |
*/ |
*/ |
174 |
int |
int |
175 |
namespace_response(conn_t * conn, unsigned int tag) |
response_namespace(connection_t * conn, unsigned int tag) |
176 |
{ |
{ |
177 |
char *c, *d; |
char *c, *d; |
178 |
|
|
179 |
reset_buffer(&ibuf); |
buffer_reset(&ibuf); |
180 |
|
|
181 |
do { |
do { |
182 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
buffer_check(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
183 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
184 |
bye_response(ibuf.data); |
response_bye(ibuf.data); |
185 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
186 |
|
|
187 |
if ((c = strcasestr(ibuf.data, "* NAMESPACE"))) { |
if ((c = strcasestr(ibuf.data, "* NAMESPACE"))) { |
190 |
c = strchr(c, '"') + 1; |
c = strchr(c, '"') + 1; |
191 |
d = strchr(c, '"') + 1; |
d = strchr(c, '"') + 1; |
192 |
|
|
193 |
strncat(conn->nsp.prefix, c, d - c - 1); |
strncat(conn->ns.prefix, c, d - c - 1); |
194 |
conn->nsp.delim = *(strchr(d, '"') + 1); |
conn->ns.delim = *(strchr(d, '"') + 1); |
195 |
} |
} |
196 |
} |
} |
197 |
debug("namespace (%s): '%s' '%c'\n", |
debug("namespace (%s): '%s' '%c'\n", |
198 |
(conn == &connpri ? "primary" : "auxiliary"), conn->nsp.prefix, |
(conn == &connpri ? "primary" : "auxiliary"), conn->ns.prefix, |
199 |
conn->nsp.delim); |
conn->ns.delim); |
200 |
return analyze_response(conn, ibuf.data); |
return analyze_response(conn, ibuf.data); |
201 |
} |
} |
202 |
|
|
205 |
* Process the data that server sent due to IMAP STATUS client request. |
* Process the data that server sent due to IMAP STATUS client request. |
206 |
*/ |
*/ |
207 |
int |
int |
208 |
status_response(conn_t * conn, unsigned int tag, char *mbox) |
response_status(connection_t * conn, unsigned int tag, char *mbox) |
209 |
{ |
{ |
210 |
int r; |
int r; |
211 |
unsigned int exist, recent, unseen; |
unsigned int exist, recent, unseen; |
213 |
|
|
214 |
exist = recent = unseen = 0; |
exist = recent = unseen = 0; |
215 |
|
|
216 |
reset_buffer(&ibuf); |
buffer_reset(&ibuf); |
217 |
|
|
218 |
do { |
do { |
219 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
buffer_check(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
220 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
221 |
bye_response(ibuf.data); |
response_bye(ibuf.data); |
222 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
223 |
|
|
224 |
r = analyze_response(conn, ibuf.data); |
r = analyze_response(conn, ibuf.data); |
253 |
* Process the data that server sent due to IMAP SELECT client request. |
* Process the data that server sent due to IMAP SELECT client request. |
254 |
*/ |
*/ |
255 |
int |
int |
256 |
select_response(conn_t * conn, unsigned int tag) |
response_select(connection_t * conn, unsigned int tag) |
257 |
{ |
{ |
258 |
reset_buffer(&ibuf); |
buffer_reset(&ibuf); |
259 |
|
|
260 |
do { |
do { |
261 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
buffer_check(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
262 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
263 |
bye_response(ibuf.data); |
response_bye(ibuf.data); |
264 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
265 |
|
|
266 |
if (strcasestr(ibuf.data, "[READ-ONLY]")) |
if (strcasestr(ibuf.data, "[READ-ONLY]")) |
274 |
* Process the data that server sent due to IMAP SEARCH client request. |
* Process the data that server sent due to IMAP SEARCH client request. |
275 |
*/ |
*/ |
276 |
int |
int |
277 |
search_response(conn_t * conn, unsigned int tag, char **mesgs) |
response_search(connection_t * conn, unsigned int tag, char **mesgs) |
278 |
{ |
{ |
279 |
char *c, *m; |
char *c, *m; |
280 |
unsigned int blen; |
unsigned int blen; |
281 |
|
|
282 |
reset_buffer(&ibuf); |
buffer_reset(&ibuf); |
283 |
|
|
284 |
do { |
do { |
285 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
buffer_check(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
286 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
287 |
bye_response(ibuf.data); |
response_bye(ibuf.data); |
288 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
289 |
|
|
290 |
if ((c = strcasestr(ibuf.data, "* SEARCH "))) { |
if ((c = strcasestr(ibuf.data, "* SEARCH "))) { |
307 |
* Process the data that server sent due to IMAP FETCH client request. |
* Process the data that server sent due to IMAP FETCH client request. |
308 |
*/ |
*/ |
309 |
int |
int |
310 |
fetch_response(conn_t * conn, unsigned int tag, int reset, char *fetch) |
response_fetch(connection_t * conn, unsigned int tag, int reset, char *fetch) |
311 |
{ |
{ |
312 |
unsigned int i; |
unsigned int i; |
313 |
static unsigned int s; |
static unsigned int s; |
319 |
} |
} |
320 |
i = 0; |
i = 0; |
321 |
|
|
322 |
reset_buffer(&ibuf); |
buffer_reset(&ibuf); |
323 |
|
|
324 |
do { |
do { |
325 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
buffer_check(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
326 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
327 |
bye_response(ibuf.data); |
response_bye(ibuf.data); |
328 |
} while (strlen(ibuf.data) < RESPONSE_BUF && |
} while (strlen(ibuf.data) < RESPONSE_BUF && |
329 |
!strcasestr(ibuf.data, ultostr(tag, 16))); |
!strcasestr(ibuf.data, ultostr(tag, 16))); |
330 |
|
|
355 |
* Process the data that server sent due to IMAP FETCH FAST client request. |
* Process the data that server sent due to IMAP FETCH FAST client request. |
356 |
*/ |
*/ |
357 |
int |
int |
358 |
fetchfast_response(conn_t * conn, char **flags, char **date, |
response_fetchfast(connection_t * conn, char **flags, char **date, |
359 |
unsigned int *size, unsigned int tag) |
unsigned int *size, unsigned int tag) |
360 |
{ |
{ |
361 |
char *c, *m; |
char *c, *m; |
363 |
|
|
364 |
*size = 0; |
*size = 0; |
365 |
|
|
366 |
reset_buffer(&ibuf); |
buffer_reset(&ibuf); |
367 |
|
|
368 |
do { |
do { |
369 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
buffer_check(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
370 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
371 |
bye_response(ibuf.data); |
response_bye(ibuf.data); |
372 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
373 |
|
|
374 |
if ((c = strcasestr(ibuf.data, "FETCH (FLAGS ("))) { |
if ((c = strcasestr(ibuf.data, "FETCH (FLAGS ("))) { |
416 |
* Process the data that server sent due to IMAP APPEND client request. |
* Process the data that server sent due to IMAP APPEND client request. |
417 |
*/ |
*/ |
418 |
int |
int |
419 |
append_response(conn_t * conn, unsigned int tag) |
response_append(connection_t * conn, unsigned int tag) |
420 |
{ |
{ |
421 |
int r; |
int r; |
422 |
|
|
423 |
r = RESPONSE_OK; |
r = RESPONSE_OK; |
424 |
|
|
425 |
reset_buffer(&ibuf); |
buffer_reset(&ibuf); |
426 |
|
|
427 |
do { |
do { |
428 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
buffer_check(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
429 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
430 |
bye_response(ibuf.data); |
response_bye(ibuf.data); |
431 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
432 |
|
|
433 |
if ((r = analyze_response(conn, ibuf.data)) == RESPONSE_NO && |
if ((r = analyze_response(conn, ibuf.data)) == RESPONSE_NO && |
442 |
* Process the data that server sent due to IMAP COPY client request. |
* Process the data that server sent due to IMAP COPY client request. |
443 |
*/ |
*/ |
444 |
int |
int |
445 |
copy_response(conn_t * conn, unsigned int tag) |
response_copy(connection_t * conn, unsigned int tag) |
446 |
{ |
{ |
447 |
int r; |
int r; |
448 |
|
|
449 |
r = RESPONSE_OK; |
r = RESPONSE_OK; |
450 |
|
|
451 |
reset_buffer(&ibuf); |
buffer_reset(&ibuf); |
452 |
|
|
453 |
do { |
do { |
454 |
check_buffer(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
buffer_check(&ibuf, strlen(ibuf.data) + RESPONSE_BUF); |
455 |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
receive_response(conn, ibuf.data + strlen(ibuf.data)); |
456 |
bye_response(ibuf.data); |
response_bye(ibuf.data); |
457 |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
} while (!strcasestr(ibuf.data, ultostr(tag, 16))); |
458 |
|
|
459 |
if ((r = analyze_response(conn, ibuf.data)) == RESPONSE_NO && |
if ((r = analyze_response(conn, ibuf.data)) == RESPONSE_NO && |
469 |
* delivered or there was some kind of error. |
* delivered or there was some kind of error. |
470 |
*/ |
*/ |
471 |
int |
int |
472 |
analyze_response(conn_t * conn, char *buf) |
analyze_response(connection_t * conn, char *buf) |
473 |
{ |
{ |
474 |
int r; |
int r; |
475 |
regex_t creg; |
regex_t creg; |