/[imapfilter]/imapfilter/response.c
ViewVC logotype

Annotation of /imapfilter/response.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.36 - (hide annotations)
Sat Jul 26 19:42:13 2003 UTC (20 years, 8 months ago) by lefcha
Branch: MAIN
Changes since 1.35: +129 -155 lines
File MIME type: text/plain
Added CRAM-MD5 support and replaced virtual buffer with more clean input buffer.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26