2 * imap.c -- IMAP2bis/IMAP4 protocol methods
4 * Copyright 1997 by Eric S. Raymond
5 * For license terms, see the file COPYING in this directory.
12 #if defined(STDC_HEADERS)
17 #include "fetchmail.h"
22 /* imap_version values */
23 #define IMAP2 -1 /* IMAP2 or IMAP2BIS, RFC1176 */
24 #define IMAP4 0 /* IMAP4 rev 0, RFC1730 */
25 #define IMAP4rev1 1 /* IMAP4 rev 1, RFC2060 */
27 /* global variables: please reinitialize them explicitly for proper
28 * working in daemon mode */
30 /* TODO: session variables to be initialized before server greeting */
31 static int preauth = FALSE;
33 /* session variables initialized in capa_probe() or imap_getauth() */
34 static char capabilities[MSGBUFSIZE+1];
35 static int imap_version = IMAP4;
36 static flag do_idle = FALSE, has_idle = FALSE;
37 static int expunge_period = 1;
39 /* mailbox variables initialized in imap_getrange() */
40 static int count = 0, oldcount = 0, recentcount = 0, unseen = 0, deletions = 0;
41 static unsigned int startcount = 1;
42 static int expunged = 0;
43 static unsigned int *unseen_messages;
45 /* for "IMAP> EXPUNGE" */
46 static int actual_deletions = 0;
48 /* for "IMAP> IDLE" */
49 static int saved_timeout = 0, idle_timeout = 0;
50 static time_t idle_start_time = 0;
52 static int imap_untagged_response(int sock, const char *buf)
53 /* interpret untagged status responses */
55 /* For each individual check, use a BLANK before the word to avoid
56 * confusion with the \Recent flag or similar */
57 if (stage == STAGE_GETAUTH
58 && !strncmp(buf, "* CAPABILITY", 12))
60 strlcpy(capabilities, buf + 12, sizeof(capabilities));
62 else if (stage == STAGE_GETAUTH
63 && !strncmp(buf, "* PREAUTH", 9))
67 else if (stage != STAGE_LOGOUT
68 && !strncmp(buf, "* BYE", 5))
70 /* log the unexpected bye from server as we expect the
71 * connection to be cut-off after this */
72 if (outlevel > O_SILENT)
73 report(stderr, GT_("Received BYE response from IMAP server: %s"), buf + 5);
75 else if (strstr(buf, " EXISTS"))
79 * Don't trust the message count passed by the server.
80 * Without this check, it might be possible to do a
81 * DNS-spoofing attack that would pass back a ridiculous
82 * count, and allocate a malloc area that would overlap
83 * a portion of the stack.
85 if ((unsigned)count > INT_MAX/sizeof(int))
87 report(stderr, GT_("bogus message count!"));
90 if ((recentcount = count - oldcount) < 0)
94 * Nasty kluge to handle RFC2177 IDLE. If we know we're idling
95 * we can't wait for the tag matching the IDLE; we have to tell the
96 * server the IDLE is finished by shipping back a DONE when we
97 * see an EXISTS. Only after that will a tagged response be
98 * shipped. The idling flag also gets cleared on a timeout.
100 if (stage == STAGE_IDLE)
102 /* If IDLE isn't supported, we were only sending NOOPs anyway. */
105 /* we do our own write and report here to disable tagging */
106 SockWrite(sock, "DONE\r\n", 6);
107 if (outlevel >= O_MONITOR)
108 report(stdout, "IMAP> DONE\n");
111 mytimeout = saved_timeout;
112 stage = STAGE_GETRANGE;
115 /* we now compute recentcount as a difference between
116 * new and old EXISTS, hence disable RECENT check */
118 else if (strstr(buf, " RECENT"))
120 recentcount = atoi(buf+2);
123 else if (strstr(buf, " EXPUNGE"))
125 /* the response "* 10 EXPUNGE" means that the currently
126 * tenth (i.e. only one) message has been deleted */
133 /* We do expect an EXISTS response immediately
134 * after this, so this updation of recentcount is
135 * just a precaution! */
136 if ((recentcount = count - oldcount) < 0)
142 * The server may decide to make the mailbox read-only,
143 * which causes fetchmail to go into a endless loop
144 * fetching the same message over and over again.
146 * However, for check_only, we use EXAMINE which will
147 * mark the mailbox read-only as per the RFC.
149 * This checks for the condition and aborts if
150 * the mailbox is read-only.
152 * See RFC 2060 section 6.3.1 (SELECT).
153 * See RFC 2060 section 6.3.2 (EXAMINE).
155 else if (stage == STAGE_GETRANGE
156 && !check_only && strstr(buf, "[READ-ONLY]"))
167 static int imap_response(int sock, char *argbuf)
168 /* parse command response */
170 char buf[MSGBUFSIZE+1];
176 if ((ok = gen_recv(sock, buf, sizeof(buf))))
179 /* all tokens in responses are caseblind */
180 for (cp = buf; *cp; cp++)
181 if (islower((unsigned char)*cp))
182 *cp = toupper((unsigned char)*cp);
184 /* untagged responses start with "* " */
185 if (buf[0] == '*' && buf[1] == ' ') {
186 ok = imap_untagged_response(sock, buf);
187 if (ok == PS_UNTAGGED)
189 if (argbuf && stage != STAGE_IDLE && tag[0] != '\0')
191 /* if there is an unmatched response, pass it back to
192 * the calling function for further analysis. The
193 * calling function should call imap_response() again
194 * to read the remaining response */
199 else if (ok != PS_SUCCESS)
203 if (stage == STAGE_IDLE)
205 /* reduce the timeout: servers may not reset their timeout
206 * when they send some information asynchronously */
207 mytimeout = idle_timeout - (time((time_t *) NULL) - idle_start_time);
209 return(PS_IDLETIMEOUT);
212 (tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
225 for (cp = buf; !isspace((unsigned char)*cp); cp++)
227 while (isspace((unsigned char)*cp))
230 if (strncasecmp(cp, "OK", 2) == 0)
236 else if (strncasecmp(cp, "BAD", 3) == 0)
238 else if (strncasecmp(cp, "NO", 2) == 0)
240 if (stage == STAGE_GETAUTH)
241 return(PS_AUTHFAIL); /* RFC2060, 6.2.2 */
242 else if (stage == STAGE_GETSIZES)
243 return(PS_SUCCESS); /* see comments in imap_getpartialsizes() */
252 static int imap_ok(int sock, char *argbuf)
253 /* parse command response */
257 while ((ok = imap_response(sock, argbuf)) == PS_UNTAGGED)
258 ; /* wait for the tagged response */
266 * NTLM support by Grant Edwards.
268 * Handle MS-Exchange NTLM authentication method. This is the same
269 * as the NTLM auth used by Samba for SMB related services. We just
270 * encode the packets in base64 instead of sending them out via a
273 * Much source (ntlm.h, smb*.c smb*.h) was borrowed from Samba.
276 static int do_imap_ntlm(int sock, struct query *ctl)
278 tSmbNtlmAuthRequest request;
279 tSmbNtlmAuthChallenge challenge;
280 tSmbNtlmAuthResponse response;
285 gen_send(sock, "AUTHENTICATE NTLM");
287 if ((result = gen_recv(sock, msgbuf, sizeof msgbuf)))
290 if (msgbuf[0] != '+')
293 buildSmbNtlmAuthRequest(&request,ctl->remotename,NULL);
295 if (outlevel >= O_DEBUG)
296 dumpSmbNtlmAuthRequest(stdout, &request);
298 memset(msgbuf,0,sizeof msgbuf);
299 to64frombits (msgbuf, &request, SmbLength(&request));
301 if (outlevel >= O_MONITOR)
302 report(stdout, "IMAP> %s\n", msgbuf);
304 strcat(msgbuf,"\r\n");
305 SockWrite (sock, msgbuf, strlen (msgbuf));
307 if ((gen_recv(sock, msgbuf, sizeof msgbuf)))
310 len = from64tobits (&challenge, msgbuf, sizeof(challenge));
312 if (outlevel >= O_DEBUG)
313 dumpSmbNtlmAuthChallenge(stdout, &challenge);
315 buildSmbNtlmAuthResponse(&challenge, &response,ctl->remotename,ctl->password);
317 if (outlevel >= O_DEBUG)
318 dumpSmbNtlmAuthResponse(stdout, &response);
320 memset(msgbuf,0,sizeof msgbuf);
321 to64frombits (msgbuf, &response, SmbLength(&response));
323 if (outlevel >= O_MONITOR)
324 report(stdout, "IMAP> %s\n", msgbuf);
326 strcat(msgbuf,"\r\n");
327 SockWrite (sock, msgbuf, strlen (msgbuf));
329 result = imap_ok (sock, NULL);
330 if (result == PS_SUCCESS)
337 static void imap_canonicalize(char *result, char *raw, size_t maxlen)
338 /* encode an IMAP password as per RFC1730's quoting conventions */
343 for (i = 0; i < strlen(raw) && i < maxlen; i++)
345 if ((raw[i] == '\\') || (raw[i] == '"'))
347 result[j++] = raw[i];
352 static void capa_probe(int sock, struct query *ctl)
353 /* set capability variables from a CAPA probe */
357 /* probe to see if we're running IMAP4 and can use RFC822.PEEK */
358 capabilities[0] = '\0';
359 if ((ok = gen_transact(sock, "CAPABILITY")) == PS_SUCCESS)
363 /* capability checks are supposed to be caseblind */
364 for (cp = capabilities; *cp; cp++)
365 *cp = toupper((unsigned char)*cp);
367 /* UW-IMAP server 10.173 notifies in all caps, but RFC2060 says we
368 should expect a response in mixed-case */
369 if (strstr(capabilities, "IMAP4REV1"))
371 imap_version = IMAP4rev1;
372 if (outlevel >= O_DEBUG)
373 report(stdout, GT_("Protocol identified as IMAP4 rev 1\n"));
377 imap_version = IMAP4;
378 if (outlevel >= O_DEBUG)
379 report(stdout, GT_("Protocol identified as IMAP4 rev 0\n"));
382 else if (ok == PS_ERROR)
384 imap_version = IMAP2;
385 if (outlevel >= O_DEBUG)
386 report(stdout, GT_("Protocol identified as IMAP2 or IMAP2BIS\n"));
390 * Handle idling. We depend on coming through here on startup
391 * and after each timeout (including timeouts during idles).
396 if (strstr(capabilities, "IDLE"))
400 if (outlevel >= O_VERBOSE)
401 report(stdout, GT_("will idle after poll\n"));
404 peek_capable = (imap_version >= IMAP4);
407 static int do_authcert (int sock, char *command, const char *name)
408 /* do authentication "external" (authentication provided by client cert) */
414 size_t len = strlen(name);
415 if ((len / 3) + ((len % 3) ? 4 : 0) < sizeof(buf))
416 to64frombits (buf, name, strlen(name));
418 return PS_AUTHFAIL; /* buffer too small. */
422 return gen_transact(sock, "%s EXTERNAL %s",command,buf);
425 static int imap_getauth(int sock, struct query *ctl, char *greeting)
426 /* apply for connection authorization */
435 * Assumption: expunges are cheap, so we want to do them
436 * after every message unless user said otherwise.
438 if (NUM_SPECIFIED(ctl->expunge))
439 expunge_period = NUM_VALUE_OUT(ctl->expunge);
443 capa_probe(sock, ctl);
446 * If either (a) we saw a PREAUTH token in the greeting, or
447 * (b) the user specified ssh preauthentication, then we're done.
449 if (preauth || ctl->server.authenticate == A_SSH)
451 preauth = FALSE; /* reset for the next session */
456 if (maybe_tls(ctl)) {
459 commonname = ctl->server.pollname;
461 commonname = ctl->server.via;
462 if (ctl->sslcommonname)
463 commonname = ctl->sslcommonname;
465 if (strstr(capabilities, "STARTTLS"))
467 /* Use "tls1" rather than ctl->sslproto because tls1 is the only
468 * protocol that will work with STARTTLS. Don't need to worry
469 * whether TLS is mandatory or opportunistic unless SSLOpen() fails
471 if (gen_transact(sock, "STARTTLS") == PS_SUCCESS
472 && SSLOpen(sock, ctl->sslcert, ctl->sslkey, "tls1", ctl->sslcertck,
473 ctl->sslcertpath, ctl->sslfingerprint, commonname,
474 ctl->server.pollname, &ctl->remotename) != -1)
477 * RFC 2595 says this:
479 * "Once TLS has been started, the client MUST discard cached
480 * information about server capabilities and SHOULD re-issue the
481 * CAPABILITY command. This is necessary to protect against
482 * man-in-the-middle attacks which alter the capabilities list prior
483 * to STARTTLS. The server MAY advertise different capabilities
486 * Now that we're confident in our TLS connection we can
487 * guarantee a secure capability re-probe.
490 capa_probe(sock, ctl);
491 if (outlevel >= O_VERBOSE)
493 report(stdout, GT_("%s: upgrade to TLS succeeded.\n"), commonname);
500 /* Config required TLS but we couldn't guarantee it, so we must
502 report(stderr, GT_("%s: upgrade to TLS failed.\n"), commonname);
505 if (outlevel >= O_VERBOSE) {
506 report(stdout, GT_("%s: opportunistic upgrade to TLS failed, trying to continue\n"), commonname);
508 /* We don't know whether the connection is in a working state, so
509 * test by issuing a NOOP. */
510 if (gen_transact(sock, "NOOP") != PS_SUCCESS) {
511 /* Not usable. Empty sslproto to force an unencrypted
512 * connection on the next attempt, and repoll. */
513 ctl->sslproto = xstrdup("");
516 /* Usable. Proceed with authenticating insecurely. */
520 #endif /* SSL_ENABLE */
523 * Time to authenticate the user.
524 * Try the protocol variants that don't require passwords first.
528 /* Yahoo hack - we'll just try ID if it was offered by the server,
529 * and IGNORE errors. */
531 char *tmp = strstr(capabilities, " ID");
532 if (tmp && !isalnum(tmp[3]) && strstr(ctl->server.via ? ctl->server.via : ctl->server.pollname, "yahoo.com")) {
533 (void)gen_transact(sock, "ID (\"guid\" \"1\")");
537 if ((ctl->server.authenticate == A_ANY
538 || ctl->server.authenticate == A_EXTERNAL)
539 && strstr(capabilities, "AUTH=EXTERNAL"))
541 ok = do_authcert(sock, "AUTHENTICATE", ctl->remotename);
544 /* SASL cancellation of authentication */
546 if (ctl->server.authenticate != A_ANY)
554 if ((ctl->server.authenticate == A_ANY
555 || ctl->server.authenticate == A_GSSAPI)
556 && strstr(capabilities, "AUTH=GSSAPI"))
558 if ((ok = do_gssauth(sock, "AUTHENTICATE", "imap",
559 ctl->server.truename, ctl->remotename)))
561 /* SASL cancellation of authentication */
563 if (ctl->server.authenticate != A_ANY)
572 if ((ctl->server.authenticate == A_ANY
573 || ctl->server.authenticate == A_KERBEROS_V4
574 || ctl->server.authenticate == A_KERBEROS_V5)
575 && strstr(capabilities, "AUTH=KERBEROS_V4"))
577 if ((ok = do_rfc1731(sock, "AUTHENTICATE", ctl->server.truename)))
579 /* SASL cancellation of authentication */
581 if(ctl->server.authenticate != A_ANY)
587 #endif /* KERBEROS_V4 */
590 * No such luck. OK, now try the variants that mask your password
591 * in a challenge-response.
594 if ((ctl->server.authenticate == A_ANY && strstr(capabilities, "AUTH=CRAM-MD5"))
595 || ctl->server.authenticate == A_CRAM_MD5)
597 if ((ok = do_cram_md5 (sock, "AUTHENTICATE", ctl, NULL)))
599 /* SASL cancellation of authentication */
601 if(ctl->server.authenticate != A_ANY)
609 if ((ctl->server.authenticate == A_ANY
610 || ctl->server.authenticate == A_OTP)
611 && strstr(capabilities, "AUTH=X-OTP")) {
612 if ((ok = do_otp(sock, "AUTHENTICATE", ctl)))
614 /* SASL cancellation of authentication */
616 if(ctl->server.authenticate != A_ANY)
623 if (ctl->server.authenticate == A_OTP)
626 GT_("Required OTP capability not compiled into fetchmail\n"));
628 #endif /* OPIE_ENABLE */
631 if ((ctl->server.authenticate == A_ANY
632 || ctl->server.authenticate == A_NTLM)
633 && strstr (capabilities, "AUTH=NTLM")) {
634 if ((ok = do_imap_ntlm(sock, ctl)))
636 /* SASL cancellation of authentication */
638 if(ctl->server.authenticate != A_ANY)
645 if (ctl->server.authenticate == A_NTLM)
648 GT_("Required NTLM capability not compiled into fetchmail\n"));
650 #endif /* NTLM_ENABLE */
652 #ifdef __UNUSED__ /* The Cyrus IMAP4rev1 server chokes on this */
653 /* this handles either AUTH=LOGIN or AUTH-LOGIN */
654 if ((imap_version >= IMAP4rev1) && (!strstr(capabilities, "LOGIN")))
657 GT_("Required LOGIN capability not supported by server\n"));
659 #endif /* __UNUSED__ */
662 * We're stuck with sending the password en clair.
663 * The reason for this odd-looking logic is that some
664 * servers return LOGINDISABLED even though login
665 * actually works. So arrange things in such a way that
666 * setting auth passwd makes it ignore this capability.
668 if((ctl->server.authenticate==A_ANY&&!strstr(capabilities,"LOGINDISABLED"))
669 || ctl->server.authenticate == A_PASSWORD)
671 /* these sizes guarantee no buffer overflow */
672 char *remotename, *password;
674 rnl = 2 * strlen(ctl->remotename) + 1;
675 pwl = 2 * strlen(ctl->password) + 1;
676 remotename = (char *)xmalloc(rnl);
677 password = (char *)xmalloc(pwl);
679 imap_canonicalize(remotename, ctl->remotename, rnl);
680 imap_canonicalize(password, ctl->password, pwl);
682 snprintf(shroud, sizeof (shroud), "\"%s\"", password);
683 ok = gen_transact(sock, "LOGIN \"%s\" \"%s\"", remotename, password);
684 memset(shroud, 0x55, sizeof(shroud));
686 memset(password, 0x55, strlen(password));
691 /* SASL cancellation of authentication */
693 if(ctl->server.authenticate != A_ANY)
703 static int internal_expunge(int sock)
704 /* ship an expunge, resetting associated counters */
708 actual_deletions = 0;
710 if ((ok = gen_transact(sock, "EXPUNGE")))
713 /* if there is a mismatch between the number of mails which should
714 * have been expunged and the number of mails actually expunged,
715 * another email client may be deleting mails. Quit here,
716 * otherwise fetchmail gets out-of-sync with the imap server,
717 * reports the wrong size to the SMTP server on MAIL FROM: and
718 * triggers a "message ... was not the expected length" error on
719 * every subsequent mail */
720 if (deletions > 0 && deletions != actual_deletions)
723 GT_("mail expunge mismatch (%d actual != %d expected)\n"),
724 actual_deletions, deletions);
729 expunged += deletions;
732 #ifdef IMAP_UID /* not used */
734 #endif /* IMAP_UID */
739 static int imap_idle(int sock)
740 /* start an RFC2177 IDLE, or fake one if unsupported */
744 saved_timeout = mytimeout;
747 /* special timeout to terminate the IDLE and re-issue it
748 * at least every 28 minutes:
749 * (the server may have an inactivity timeout) */
750 mytimeout = idle_timeout = 1680; /* 28 min */
751 time(&idle_start_time);
753 /* enter IDLE mode */
754 ok = gen_transact(sock, "IDLE");
756 if (ok == PS_IDLETIMEOUT) {
757 /* send "DONE" continuation */
758 SockWrite(sock, "DONE\r\n", 6);
759 if (outlevel >= O_MONITOR)
760 report(stdout, "IMAP> DONE\n");
761 /* reset stage and timeout here: we are not idling any more */
762 mytimeout = saved_timeout;
763 stage = STAGE_GETRANGE;
764 /* get OK IDLE message */
765 ok = imap_ok(sock, NULL);
767 } else { /* no idle support, fake it */
768 /* Note: stage and timeout have not been changed here as NOOP
770 ok = gen_transact(sock, "NOOP");
772 /* no error, but no new mail either */
773 if (ok == PS_SUCCESS && recentcount == 0)
775 /* There are some servers who do send new mail
776 * notification out of the blue. This is in compliance
777 * with RFC 2060 section 5.3. Wait for that with a low
779 mytimeout = idle_timeout = 28;
780 time(&idle_start_time);
782 /* We are waiting for notification; no tag needed */
784 /* wait (briefly) for an unsolicited status update */
785 ok = imap_ok(sock, NULL);
786 if (ok == PS_IDLETIMEOUT) {
787 /* no notification came; ok */
793 /* restore normal timeout value */
795 mytimeout = saved_timeout;
796 stage = STAGE_GETRANGE;
801 static int imap_getrange(int sock,
804 int *countp, int *newp, int *bytes)
805 /* get range of messages to be fetched */
808 char buf[MSGBUFSIZE+1], *cp;
810 /* find out how many messages are waiting */
815 /* deleted mails have already been expunged by
816 * end_mailbox_poll().
818 * recentcount is already set here by the last imap command which
819 * returned EXISTS on detecting new mail. if recentcount is 0, wait
822 * this is a while loop because imap_idle() might return on other
823 * mailbox changes also */
824 while (recentcount == 0 && do_idle) {
826 ok = imap_idle(sock);
829 report(stderr, GT_("re-poll failed\n"));
833 /* if recentcount is 0, return no mail */
834 if (recentcount == 0)
836 if (outlevel >= O_DEBUG)
837 report(stdout, ngettext("%d message waiting after re-poll\n",
838 "%d messages waiting after re-poll\n",
843 oldcount = count = 0;
844 ok = gen_transact(sock,
845 check_only ? "EXAMINE \"%s\"" : "SELECT \"%s\"",
846 folder ? folder : "INBOX");
847 /* imap_ok returns PS_LOCKBUSY for READ-ONLY folders,
848 * which we can safely use in fetchall keep only */
849 if (ok == PS_LOCKBUSY && ctl->fetchall && ctl-> keep)
854 report(stderr, GT_("mailbox selection failed\n"));
857 else if (outlevel >= O_DEBUG)
858 report(stdout, ngettext("%d message waiting after first poll\n",
859 "%d messages waiting after first poll\n",
863 * We should have an expunge here to
864 * a) avoid fetching deleted mails during 'fetchall'
865 * b) getting a wrong count of mails during 'no fetchall'
867 if (!check_only && !ctl->keep && count > 0)
869 ok = internal_expunge(sock);
872 report(stderr, GT_("expunge failed\n"));
875 if (outlevel >= O_DEBUG)
876 report(stdout, ngettext("%d message waiting after expunge\n",
877 "%d messages waiting after expunge\n",
881 if (count == 0 && do_idle)
883 /* no messages? then we may need to idle until we get some */
885 ok = imap_idle(sock);
888 report(stderr, GT_("re-poll failed\n"));
892 if (outlevel >= O_DEBUG)
893 report(stdout, ngettext("%d message waiting after re-poll\n",
894 "%d messages waiting after re-poll\n",
899 *countp = oldcount = count;
903 /* OK, now get a count of unseen messages and their indices */
904 if (!ctl->fetchall && count > 0)
907 free(unseen_messages);
908 unseen_messages = (unsigned int *)xmalloc(count * sizeof(unsigned int));
909 memset(unseen_messages, 0, count * sizeof(unsigned int));
912 /* don't count deleted messages, in case user enabled keep last time */
913 gen_send(sock, "SEARCH UNSEEN NOT DELETED");
914 while ((ok = imap_response(sock, buf)) == PS_UNTAGGED)
916 if ((cp = strstr(buf, "* SEARCH")))
920 cp += 8; /* skip "* SEARCH" */
921 /* startcount is higher than count so that if there are no
922 * unseen messages, imap_getsizes() will not need to do
924 startcount = count + 1;
926 while (*cp && unseen < count)
928 /* skip whitespace */
929 while (*cp && isspace((unsigned char)*cp))
936 um = strtoul(cp,&ep,10);
937 if (errno == 0 && um <= UINT_MAX && um <= (unsigned)count)
939 unseen_messages[unseen++] = um;
940 if (outlevel >= O_DEBUG)
941 report(stdout, GT_("%lu is unseen\n"), um);
952 report(stderr, GT_("search for unseen messages failed\n"));
956 if (outlevel >= O_DEBUG && unseen > 0)
957 report(stdout, GT_("%u is first unseen\n"), startcount);
968 static int imap_getpartialsizes(int sock, int first, int last, int *sizes)
969 /* capture the sizes of messages #first-#last */
971 char buf [MSGBUFSIZE+1];
975 * Some servers (as in, PMDF5.1-9.1 under OpenVMS 6.1)
976 * won't accept 1:1 as valid set syntax. Some implementors
977 * should be taken out and shot for excessive anality.
979 * Microsoft Exchange (brain-dead piece of crap that it is)
980 * sometimes gets its knickers in a knot about bodiless messages.
981 * You may see responses like this:
983 * fetchmail: IMAP> A0004 FETCH 1:9 RFC822.SIZE
984 * fetchmail: IMAP< * 2 FETCH (RFC822.SIZE 1187)
985 * fetchmail: IMAP< * 3 FETCH (RFC822.SIZE 3954)
986 * fetchmail: IMAP< * 4 FETCH (RFC822.SIZE 1944)
987 * fetchmail: IMAP< * 5 FETCH (RFC822.SIZE 2933)
988 * fetchmail: IMAP< * 6 FETCH (RFC822.SIZE 1854)
989 * fetchmail: IMAP< * 7 FETCH (RFC822.SIZE 34054)
990 * fetchmail: IMAP< * 8 FETCH (RFC822.SIZE 5561)
991 * fetchmail: IMAP< * 9 FETCH (RFC822.SIZE 1101)
992 * fetchmail: IMAP< A0004 NO The requested item could not be found.
994 * This means message 1 has only headers. For kicks and grins
995 * you can telnet in and look:
997 * A003 NO The requested item could not be found.
998 * A004 fetch 1 rfc822.header
999 * A004 NO The requested item could not be found.
1001 * * 1 FETCH (BODY ("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 35 3))
1002 * A006 OK FETCH completed.
1004 * To get around this, we treat the final NO as success and count
1005 * on the fact that the sizes array has been preinitialized with a
1006 * known-bad size value.
1009 /* expunges change the fetch numbers */
1014 gen_send(sock, "FETCH %d RFC822.SIZE", last);
1015 else if (last > first)
1016 gen_send(sock, "FETCH %d:%d RFC822.SIZE", first, last);
1017 else /* no unseen messages! */
1019 while ((ok = imap_response(sock, buf)) == PS_UNTAGGED)
1024 if (sscanf(buf, "* %d FETCH (RFC822.SIZE %u)", &num, &size) == 2
1025 /* some servers (like mail.internode.on.net bld-mail04) return UID information here
1027 * IMAP> A0005 FETCH 1 RFC822.SIZE
1028 * IMAP< * 1 FETCH (UID 16 RFC822.SIZE 1447)
1029 * IMAP< A0005 OK FETCH completed
1032 || sscanf(buf, "* %d FETCH (UID %*s RFC822.SIZE %u)", &num, &size) == 2)
1034 if (num >= first && num <= last)
1035 sizes[num - first] = size;
1038 GT_("Warning: ignoring bogus data for message sizes returned by the server.\n"));
1044 static int imap_getsizes(int sock, int count, int *sizes)
1045 /* capture the sizes of all messages */
1047 return imap_getpartialsizes(sock, 1, count, sizes);
1050 static int imap_is_old(int sock, struct query *ctl, int number)
1051 /* is the given message old? */
1059 * Expunges change the fetch numbers, but unseen_messages contains
1060 * indices from before any expungees were done. So neither the
1061 * argument nor the values in message_sequence need to be decremented.
1065 for (i = 0; i < unseen; i++)
1066 if (unseen_messages[i] == (unsigned)number)
1075 static char *skip_token(char *ptr)
1077 while(isspace((unsigned char)*ptr)) ptr++;
1078 while(!isspace((unsigned char)*ptr) && !iscntrl((unsigned char)*ptr)) ptr++;
1079 while(isspace((unsigned char)*ptr)) ptr++;
1083 static int imap_fetch_headers(int sock, struct query *ctl,int number,int *lenp)
1084 /* request headers of nth message */
1086 char buf [MSGBUFSIZE+1];
1090 /* expunges change the fetch numbers */
1094 * This is blessed by RFC1176, RFC1730, RFC2060.
1095 * According to the RFCs, it should *not* set the \Seen flag.
1097 gen_send(sock, "FETCH %d RFC822.HEADER", number);
1099 /* looking for FETCH response */
1105 if ((ok = gen_recv(sock, buf, sizeof(buf))))
1107 ptr = skip_token(buf); /* either "* " or "AXXXX " */
1108 if (sscanf(ptr, "%d FETCH (RFC822.HEADER {%d}", &num, lenp) == 2
1109 /* some servers (like mail.internode.on.net bld-mail04) return UID information here
1111 * IMAP> A0006 FETCH 1 RFC822.HEADER
1112 * IMAP< * 1 FETCH (UID 16 RFC822.HEADER {1360}
1115 * IMAP< A0006 OK FETCH completed
1118 || sscanf(ptr, "%d FETCH (UID %*s RFC822.HEADER {%d}", &num, lenp) == 2)
1120 /* try to recover from chronically fucked-up M$ Exchange servers */
1121 else if (!strncmp(ptr, "NO", 2))
1123 /* wait for a tagged response */
1124 if (strstr (buf, "* NO"))
1126 return(PS_TRANSIENT);
1128 else if (!strncmp(ptr, "BAD", 3))
1130 /* wait for a tagged response */
1131 if (strstr (buf, "* BAD"))
1133 return(PS_TRANSIENT);
1143 static int imap_fetch_body(int sock, struct query *ctl, int number, int *lenp)
1144 /* request body of nth message */
1146 char buf [MSGBUFSIZE+1], *cp;
1150 /* expunges change the fetch numbers */
1154 * If we're using IMAP4, we can fetch the message without setting its
1155 * seen flag. This is good! It means that if the protocol exchange
1156 * craps out during the message, it will still be marked `unseen' on
1159 * According to RFC2060, and Mark Crispin the IMAP maintainer,
1160 * FETCH %d BODY[TEXT] and RFC822.TEXT are "functionally
1161 * equivalent". However, we know of at least one server that
1162 * treats them differently in the presence of MIME attachments;
1163 * the latter form downloads the attachment, the former does not.
1164 * The server is InterChange, and the fool who implemented this
1165 * misfeature ought to be strung up by his thumbs.
1167 * When I tried working around this by disabling use of the 4rev1 form,
1168 * I found that doing this breaks operation with M$ Exchange.
1169 * Annoyingly enough, Exchange's refusal to cope is technically legal
1170 * under RFC2062. Trust Microsoft, the Great Enemy of interoperability
1171 * standards, to find a way to make standards compliance irritating....
1173 switch (imap_version)
1175 case IMAP4rev1: /* RFC 2060 */
1176 gen_send(sock, "FETCH %d BODY.PEEK[TEXT]", number);
1179 case IMAP4: /* RFC 1730 */
1180 gen_send(sock, "FETCH %d RFC822.TEXT.PEEK", number);
1183 default: /* RFC 1176 */
1184 gen_send(sock, "FETCH %d RFC822.TEXT", number);
1188 /* looking for FETCH response */
1192 if ((ok = gen_recv(sock, buf, sizeof(buf))))
1195 (!strstr(buf+4, "FETCH") || sscanf(buf+2, "%d", &num) != 1);
1200 /* Understand "NIL" as length => no body present
1201 * (MS Exchange, BerliOS Bug #11980) */
1202 if (strstr(buf+10, "NIL)")) {
1208 * Try to extract a length from the FETCH response. RFC2060 requires
1209 * it to be present, but at least one IMAP server (Novell GroupWise)
1210 * botches this. The overflow check is needed because of a broken
1211 * server called dbmail that returns huge garbage lengths.
1213 if ((cp = strchr(buf, '{'))) {
1215 *lenp = (int)strtol(cp + 1, (char **)NULL, 10);
1216 if (errno == ERANGE || *lenp < 0)
1217 *lenp = -1; /* length is too big/small for us to handle */
1220 *lenp = -1; /* missing length part in FETCH reponse */
1225 static int imap_trail(int sock, struct query *ctl, const char *tag)
1226 /* discard tail of FETCH response after reading message text */
1228 /* expunges change the fetch numbers */
1229 /* number -= expunged; */
1234 return imap_ok(sock, NULL);
1237 static int imap_delete(int sock, struct query *ctl, int number)
1238 /* set delete flag for given message */
1243 /* expunges change the fetch numbers */
1247 * Use SILENT if possible as a minor throughput optimization.
1248 * Note: this has been dropped from IMAP4rev1.
1250 * We set Seen because there are some IMAP servers (notably HP
1251 * OpenMail) that do message-receipt DSNs, but only when the seen
1252 * bit is set. This is the appropriate time -- we get here right
1253 * after the local SMTP response that says delivery was
1256 if ((ok = gen_transact(sock,
1257 imap_version == IMAP4
1258 ? "STORE %d +FLAGS.SILENT (\\Seen \\Deleted)"
1259 : "STORE %d +FLAGS (\\Seen \\Deleted)",
1266 * We do an expunge after expunge_period messages, rather than
1267 * just before quit, so that a line hit during a long session
1268 * won't result in lots of messages being fetched again during
1271 if (NUM_NONZERO(expunge_period) && (deletions % expunge_period) == 0)
1273 if ((ok = internal_expunge(sock)))
1280 static int imap_mark_seen(int sock, struct query *ctl, int number)
1281 /* mark the given message as seen */
1285 /* expunges change the message numbers */
1288 return(gen_transact(sock,
1289 imap_version == IMAP4
1290 ? "STORE %d +FLAGS.SILENT (\\Seen)"
1291 : "STORE %d +FLAGS (\\Seen)",
1295 static int imap_end_mailbox_poll(int sock, struct query *ctl)
1296 /* cleanup mailbox before we idle or switch to another one */
1300 internal_expunge(sock);
1304 static int imap_logout(int sock, struct query *ctl)
1305 /* send logout command */
1308 /* if any un-expunged deletions remain, ship an expunge now */
1310 internal_expunge(sock);
1313 /* Memory clean-up */
1314 if (unseen_messages)
1315 free(unseen_messages);
1316 #endif /* USE_SEARCH */
1318 return(gen_transact(sock, "LOGOUT"));
1321 static const struct method imap =
1323 "IMAP", /* Internet Message Access Protocol */
1324 "imap", /* service (plain and TLS) */
1325 "imaps", /* service (SSL) */
1326 TRUE, /* this is a tagged protocol */
1327 FALSE, /* no message delimiter */
1328 imap_ok, /* parse command response */
1329 imap_getauth, /* get authorization */
1330 imap_getrange, /* query range of messages */
1331 imap_getsizes, /* get sizes of messages (used for ESMTP SIZE option) */
1332 imap_getpartialsizes, /* get sizes of subset of messages (used for ESMTP SIZE option) */
1333 imap_is_old, /* no UID check */
1334 imap_fetch_headers, /* request given message headers */
1335 imap_fetch_body, /* request given message body */
1336 imap_trail, /* eat message trailer */
1337 imap_delete, /* delete the message */
1338 imap_mark_seen, /* how to mark a message as seen */
1339 imap_end_mailbox_poll, /* end-of-mailbox processing */
1340 imap_logout, /* expunge and exit */
1341 TRUE, /* yes, we can re-poll */
1344 int doIMAP(struct query *ctl)
1345 /* retrieve messages using IMAP Version 2bis or Version 4 */
1347 return(do_protocol(ctl, &imap));
1350 /* imap.c ends here */