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)
15 #include "fetchmail.h"
21 #endif /* OPIE_ENABLE */
23 #ifndef strstr /* glibc-2.1 declares this as a macro */
24 extern char *strstr(); /* needed on sysV68 R3V7.1. */
27 /* imap_version values */
28 #define IMAP2 -1 /* IMAP2 or IMAP2BIS, RFC1176 */
29 #define IMAP4 0 /* IMAP4 rev 0, RFC1730 */
30 #define IMAP4rev1 1 /* IMAP4 rev 1, RFC2060 */
32 static int count = 0, recentcount = 0, unseen = 0, deletions = 0;
33 static int expunged, expunge_period, saved_timeout = 0;
34 static int imap_version, preauth;
36 static char capabilities[MSGBUFSIZE+1];
37 static unsigned int *unseen_messages;
39 static int imap_ok(int sock, char *argbuf)
40 /* parse command response */
42 char buf[MSGBUFSIZE+1];
48 if ((ok = gen_recv(sock, buf, sizeof(buf))))
51 /* all tokens in responses are caseblind */
52 for (cp = buf; *cp; cp++)
56 /* interpret untagged status responses */
57 if (strstr(buf, "* CAPABILITY"))
58 strncpy(capabilities, buf + 12, sizeof(capabilities));
59 else if (strstr(buf, "EXISTS"))
63 * Nasty kluge to handle RFC2177 IDLE. If we know we're idling
64 * we can't wait for the tag matching the IDLE; we have to tell the
65 * server the IDLE is finished by shipping back a DONE when we
66 * see an EXISTS. Only after that will a tagged response be
67 * shipped. The idling flag also gets cleared on a timeout.
69 if (stage == STAGE_IDLE)
71 /* we do our own write and report here to disable tagging */
72 SockWrite(sock, "DONE\r\n", 6);
73 if (outlevel >= O_MONITOR)
74 report(stdout, "IMAP> DONE\n");
76 mytimeout = saved_timeout;
80 else if (strstr(buf, "RECENT"))
82 recentcount = atoi(buf+2);
84 else if (strstr(buf, "PREAUTH"))
87 * The server may decide to make the mailbox read-only,
88 * which causes fetchmail to go into a endless loop
89 * fetching the same message over and over again.
91 * However, for check_only, we use EXAMINE which will
92 * mark the mailbox read-only as per the RFC.
94 * This checks for the condition and aborts if
95 * the mailbox is read-only.
97 * See RFC 2060 section 6.3.1 (SELECT).
98 * See RFC 2060 section 6.3.2 (EXAMINE).
100 else if (!check_only && strstr(buf, "[READ-ONLY]"))
103 (tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
116 for (cp = buf; !isspace(*cp); cp++)
121 if (strncmp(cp, "OK", 2) == 0)
127 else if (strncmp(cp, "BAD", 3) == 0)
129 else if (strncmp(cp, "NO", 2) == 0)
131 if (stage == STAGE_GETAUTH)
132 return(PS_AUTHFAIL); /* RFC2060, 6.2.2 */
144 static tSmbNtlmAuthRequest request;
145 static tSmbNtlmAuthChallenge challenge;
146 static tSmbNtlmAuthResponse response;
149 * NTLM support by Grant Edwards.
151 * Handle MS-Exchange NTLM authentication method. This is the same
152 * as the NTLM auth used by Samba for SMB related services. We just
153 * encode the packets in base64 instead of sending them out via a
156 * Much source (ntlm.h, smb*.c smb*.h) was borrowed from Samba.
159 static int do_imap_ntlm(int sock, struct query *ctl)
164 gen_send(sock, "AUTHENTICATE NTLM");
166 if ((result = gen_recv(sock, msgbuf, sizeof msgbuf)))
169 if (msgbuf[0] != '+')
172 buildSmbNtlmAuthRequest(&request,ctl->remotename,NULL);
174 if (outlevel >= O_DEBUG)
175 dumpSmbNtlmAuthRequest(stdout, &request);
177 memset(msgbuf,0,sizeof msgbuf);
178 to64frombits (msgbuf, (unsigned char*)&request, SmbLength(&request));
180 if (outlevel >= O_MONITOR)
181 report(stdout, "IMAP> %s\n", msgbuf);
183 strcat(msgbuf,"\r\n");
184 SockWrite (sock, msgbuf, strlen (msgbuf));
186 if ((gen_recv(sock, msgbuf, sizeof msgbuf)))
189 len = from64tobits ((unsigned char*)&challenge, msgbuf, sizeof(msgbuf));
191 if (outlevel >= O_DEBUG)
192 dumpSmbNtlmAuthChallenge(stdout, &challenge);
194 buildSmbNtlmAuthResponse(&challenge, &response,ctl->remotename,ctl->password);
196 if (outlevel >= O_DEBUG)
197 dumpSmbNtlmAuthResponse(stdout, &response);
199 memset(msgbuf,0,sizeof msgbuf);
200 to64frombits (msgbuf, (unsigned char*)&response, SmbLength(&response));
202 if (outlevel >= O_MONITOR)
203 report(stdout, "IMAP> %s\n", msgbuf);
205 strcat(msgbuf,"\r\n");
206 SockWrite (sock, msgbuf, strlen (msgbuf));
208 if ((result = gen_recv (sock, msgbuf, sizeof msgbuf)))
211 if (strstr (msgbuf, "OK"))
218 static int imap_canonicalize(char *result, char *raw, int maxlen)
219 /* encode an IMAP password as per RFC1730's quoting conventions */
224 for (i = 0; i < strlen(raw) && i < maxlen; i++)
226 if ((raw[i] == '\\') || (raw[i] == '"'))
228 result[j++] = raw[i];
235 static int imap_getauth(int sock, struct query *ctl, char *greeting)
236 /* apply for connection authorization */
240 /* probe to see if we're running IMAP4 and can use RFC822.PEEK */
241 capabilities[0] = '\0';
242 if ((ok = gen_transact(sock, "CAPABILITY")) == PS_SUCCESS)
246 /* capability checks are supposed to be caseblind */
247 for (cp = capabilities; *cp; cp++)
250 /* UW-IMAP server 10.173 notifies in all caps, but RFC2060 says we
251 should expect a response in mixed-case */
252 if (strstr(capabilities, "IMAP4REV1"))
254 imap_version = IMAP4rev1;
255 if (outlevel >= O_DEBUG)
256 report(stdout, GT_("Protocol identified as IMAP4 rev 1\n"));
260 imap_version = IMAP4;
261 if (outlevel >= O_DEBUG)
262 report(stdout, GT_("Protocol identified as IMAP4 rev 0\n"));
265 else if (ok == PS_ERROR)
267 imap_version = IMAP2;
268 if (outlevel >= O_DEBUG)
269 report(stdout, GT_("Protocol identified as IMAP2 or IMAP2BIS\n"));
274 peek_capable = (imap_version >= IMAP4);
277 * Assumption: expunges are cheap, so we want to do them
278 * after every message unless user said otherwise.
280 if (NUM_SPECIFIED(ctl->expunge))
281 expunge_period = NUM_VALUE_OUT(ctl->expunge);
286 * Handle idling. We depend on coming through here on startup
287 * and after each timeout (including timeouts during idles).
289 if (strstr(capabilities, "IDLE") && ctl->idle)
292 if (outlevel >= O_VERBOSE)
293 report(stdout, GT_("will idle after poll\n"));
297 * If either (a) we saw a PREAUTH token in the greeting, or
298 * (b) the user specified ssh preauthentication, then we're done.
300 if (preauth || ctl->server.authenticate == A_SSH)
302 preauth = FALSE; /* reset for the next session */
307 * Time to authenticate the user.
308 * Try the protocol variants that don't require passwords first.
313 if ((ctl->server.authenticate == A_ANY
314 || ctl->server.authenticate == A_GSSAPI)
315 && strstr(capabilities, "AUTH=GSSAPI"))
316 if(ok = do_gssauth(sock, "AUTHENTICATE", ctl->server.truename, ctl->remotename))
318 /* SASL cancellation of authentication */
320 if(ctl->server.authenticate != A_ANY)
328 if ((ctl->server.authenticate == A_ANY
329 || ctl->server.authenticate == A_KERBEROS_V4
330 || ctl->server.authenticate == A_KERBEROS_V5)
331 && strstr(capabilities, "AUTH=KERBEROS_V4"))
333 if ((ok = do_rfc1731(sock, "AUTHENTICATE", ctl->server.truename)))
335 /* SASL cancellation of authentication */
337 if(ctl->server.authenticate != A_ANY)
343 #endif /* KERBEROS_V4 */
346 * No such luck. OK, now try the variants that mask your password
347 * in a challenge-response.
350 if ((ctl->server.authenticate == A_ANY
351 || ctl->server.authenticate == A_CRAM_MD5)
352 && strstr(capabilities, "AUTH=CRAM-MD5"))
354 if ((ok = do_cram_md5 (sock, "AUTHENTICATE", ctl, NULL)))
356 /* SASL cancellation of authentication */
358 if(ctl->server.authenticate != A_ANY)
366 if ((ctl->server.authenticate == A_ANY
367 || ctl->server.authenticate == A_OTP)
368 && strstr(capabilities, "AUTH=X-OTP"))
369 if ((ok = do_otp(sock, "AUTHENTICATE", ctl)))
371 /* SASL cancellation of authentication */
373 if(ctl->server.authenticate != A_ANY)
379 if (ctl->server.authenticate == A_OTP)
382 GT_("Required OTP capability not compiled into fetchmail\n"));
384 #endif /* OPIE_ENABLE */
387 if ((ctl->server.authenticate == A_ANY
388 || ctl->server.authenticate == A_NTLM)
389 && strstr (capabilities, "AUTH=NTLM")) {
390 if ((ok = do_imap_ntlm(sock, ctl)))
392 /* SASL cancellation of authentication */
394 if(ctl->server.authenticate != A_ANY)
401 if (ctl->server.authenticate == A_NTLM)
404 GT_("Required NTLM capability not compiled into fetchmail\n"));
406 #endif /* NTLM_ENABLE */
408 #ifdef __UNUSED__ /* The Cyrus IMAP4rev1 server chokes on this */
409 /* this handles either AUTH=LOGIN or AUTH-LOGIN */
410 if ((imap_version >= IMAP4rev1) && (!strstr(capabilities, "LOGIN")))
413 GT_("Required LOGIN capability not supported by server\n"));
415 #endif /* __UNUSED__ */
417 /* we're stuck with sending the password en clair */
418 if ((ctl->server.authenticate == A_ANY
419 || ctl->server.authenticate == A_PASSWORD)
420 && !strstr (capabilities, "LOGINDISABLED"))
422 /* these sizes guarantee no buffer overflow */
423 char remotename[NAMELEN*2+1], password[PASSWORDLEN*2+1];
425 imap_canonicalize(remotename, ctl->remotename, NAMELEN);
426 imap_canonicalize(password, ctl->password, PASSWORDLEN);
428 strcpy(shroud, ctl->password);
429 ok = gen_transact(sock, "LOGIN \"%s\" \"%s\"", remotename, password);
433 /* SASL cancellation of authentication */
435 if(ctl->server.authenticate != A_ANY)
445 static int internal_expunge(int sock)
446 /* ship an expunge, resetting associated counters */
450 if ((ok = gen_transact(sock, "EXPUNGE")))
453 expunged += deletions;
456 #ifdef IMAP_UID /* not used */
458 #endif /* IMAP_UID */
463 static int imap_idle(int sock)
464 /* start an RFC2177 IDLE */
467 saved_timeout = mytimeout;
470 return (gen_transact(sock, "IDLE"));
473 static int imap_getrange(int sock,
476 int *countp, int *newp, int *bytes)
477 /* get range of messages to be fetched */
480 char buf[MSGBUFSIZE+1], *cp;
482 /* find out how many messages are waiting */
488 * We have to have an expunge here, otherwise the re-poll will
489 * infinite-loop picking up un-expunged messages -- unless the
490 * expunge period is one and we've been nuking each message
491 * just after deletion.
495 ok = internal_expunge(sock);
498 report(stderr, GT_("expunge failed\n"));
504 * recentcount is already set here by the last imap command which
505 * returned RECENT on detecting new mail. if recentcount is 0, wait
509 /* this is a while loop because imap_idle() might return on other
510 * mailbox changes also */
511 while (recentcount == 0 && do_idle) {
513 ok = imap_idle(sock);
516 report(stderr, GT_("re-poll failed\n"));
520 /* if recentcount is 0, return no mail */
521 if (recentcount == 0)
523 if (outlevel >= O_DEBUG)
524 report(stdout, GT_("%d messages waiting after re-poll\n"), count);
529 ok = gen_transact(sock,
530 check_only ? "EXAMINE \"%s\"" : "SELECT \"%s\"",
531 folder ? folder : "INBOX");
534 report(stderr, GT_("mailbox selection failed\n"));
537 else if (outlevel >= O_DEBUG)
538 report(stdout, GT_("%d messages waiting after first poll\n"), count);
540 /* no messages? then we may need to idle until we get some */
541 while (count == 0 && do_idle) {
542 ok = imap_idle(sock);
545 report(stderr, GT_("re-poll failed\n"));
551 * We should have an expunge here to
552 * a) avoid fetching deleted mails during 'fetchall'
553 * b) getting a wrong count of mails during 'no fetchall'
555 if (!check_only && !ctl->keep && count > 0)
557 ok = internal_expunge(sock);
560 report(stderr, GT_("expunge failed\n"));
563 if (outlevel >= O_DEBUG)
564 report(stdout, GT_("%d messages waiting after expunge\n"), count);
571 /* OK, now get a count of unseen messages and their indices */
572 if (!ctl->fetchall && count > 0)
575 free(unseen_messages);
576 unseen_messages = xmalloc(count * sizeof(unsigned int));
577 memset(unseen_messages, 0, count * sizeof(unsigned int));
580 gen_send(sock, "SEARCH UNSEEN");
582 ok = gen_recv(sock, buf, sizeof(buf));
585 report(stderr, GT_("search for unseen messages failed\n"));
588 else if ((cp = strstr(buf, "* SEARCH")))
592 cp += 8; /* skip "* SEARCH" */
594 while (*cp && unseen < count)
596 /* skip whitespace */
597 while (*cp && isspace(*cp))
602 * Message numbers are between 1 and 2^32 inclusive,
603 * so unsigned int is large enough.
605 unseen_messages[unseen]=(unsigned int)strtol(cp,&ep,10);
607 if (outlevel >= O_DEBUG)
609 GT_("%u is unseen\n"),
610 unseen_messages[unseen]);
618 (tag[0] != '\0' && strncmp(buf, tag, strlen(tag)));
630 static int imap_getsizes(int sock, int count, int *sizes)
631 /* capture the sizes of all messages */
633 char buf [MSGBUFSIZE+1];
636 * Some servers (as in, PMDF5.1-9.1 under OpenVMS 6.1)
637 * won't accept 1:1 as valid set syntax. Some implementors
638 * should be taken out and shot for excessive anality.
640 * Microsoft Exchange (brain-dead piece of crap that it is)
641 * sometimes gets its knickers in a knot about bodiless messages.
642 * You may see responses like this:
644 * fetchmail: IMAP> A0004 FETCH 1:9 RFC822.SIZE
645 * fetchmail: IMAP< * 2 FETCH (RFC822.SIZE 1187)
646 * fetchmail: IMAP< * 3 FETCH (RFC822.SIZE 3954)
647 * fetchmail: IMAP< * 4 FETCH (RFC822.SIZE 1944)
648 * fetchmail: IMAP< * 5 FETCH (RFC822.SIZE 2933)
649 * fetchmail: IMAP< * 6 FETCH (RFC822.SIZE 1854)
650 * fetchmail: IMAP< * 7 FETCH (RFC822.SIZE 34054)
651 * fetchmail: IMAP< * 8 FETCH (RFC822.SIZE 5561)
652 * fetchmail: IMAP< * 9 FETCH (RFC822.SIZE 1101)
653 * fetchmail: IMAP< A0004 NO The requested item could not be found.
655 * This means message 1 has only headers. For kicks and grins
656 * you can telnet in and look:
658 * A003 NO The requested item could not be found.
659 * A004 fetch 1 rfc822.header
660 * A004 NO The requested item could not be found.
662 * * 1 FETCH (BODY ("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 35 3))
663 * A006 OK FETCH completed.
665 * To get around this, we terminate the read loop on a NO and count
666 * on the fact that the sizes array has been preinitialized with a
667 * known-bad size value.
670 gen_send(sock, "FETCH 1 RFC822.SIZE", count);
672 gen_send(sock, "FETCH 1:%d RFC822.SIZE", count);
675 unsigned int num, size;
678 if ((ok = gen_recv(sock, buf, sizeof(buf))))
680 else if (strstr(buf, "OK") || strstr(buf, "NO"))
682 else if (sscanf(buf, "* %u FETCH (RFC822.SIZE %u)", &num, &size) == 2) {
683 if (num > 0 && num <= count)
684 sizes[num - 1] = size;
686 report(stderr, "Warning: ignoring bogus data for message sizes returned by the server.\n");
693 static int imap_is_old(int sock, struct query *ctl, int number)
694 /* is the given message old? */
700 * Expunges change the fetch numbers, but unseen_messages contains
701 * indices from before any expungees were done. So neither the
702 * argument nor the values in message_sequence need to be decremented.
706 for (i = 0; i < unseen; i++)
707 if (unseen_messages[i] == number)
716 static char *skip_token(char *ptr)
718 while(isspace(*ptr)) ptr++;
719 while(!isspace(*ptr) && !iscntrl(*ptr)) ptr++;
720 while(isspace(*ptr)) ptr++;
724 static int imap_fetch_headers(int sock, struct query *ctl,int number,int *lenp)
725 /* request headers of nth message */
727 char buf [MSGBUFSIZE+1];
730 /* expunges change the fetch numbers */
734 * This is blessed by RFC1176, RFC1730, RFC2060.
735 * According to the RFCs, it should *not* set the \Seen flag.
737 gen_send(sock, "FETCH %d RFC822.HEADER", number);
739 /* looking for FETCH response */
745 if ((ok = gen_recv(sock, buf, sizeof(buf))))
747 ptr = skip_token(buf); /* either "* " or "AXXXX " */
748 if (sscanf(ptr, "%d FETCH (%*s {%d}", &num, lenp) == 2)
750 /* try to recover from chronically fucked-up M$ Exchange servers */
751 else if (!strncmp(ptr, "NO", 2))
752 return(PS_TRANSIENT);
753 else if (!strncmp(ptr, "BAD", 3))
754 return(PS_TRANSIENT);
763 static int imap_fetch_body(int sock, struct query *ctl, int number, int *lenp)
764 /* request body of nth message */
766 char buf [MSGBUFSIZE+1], *cp;
769 /* expunges change the fetch numbers */
773 * If we're using IMAP4, we can fetch the message without setting its
774 * seen flag. This is good! It means that if the protocol exchange
775 * craps out during the message, it will still be marked `unseen' on
778 * However...*don't* do this if we're using keep to suppress deletion!
779 * In that case, marking the seen flag is the only way to prevent the
780 * message from being re-fetched on subsequent runs (and according
781 * to RFC2060 p.43 this fetch should set Seen as a side effect).
783 * According to RFC2060, and Mark Crispin the IMAP maintainer,
784 * FETCH %d BODY[TEXT] and RFC822.TEXT are "functionally
785 * equivalent". However, we know of at least one server that
786 * treats them differently in the presence of MIME attachments;
787 * the latter form downloads the attachment, the former does not.
788 * The server is InterChange, and the fool who implemented this
789 * misfeature ought to be strung up by his thumbs.
791 * When I tried working around this by disabling use of the 4rev1 form,
792 * I found that doing this breaks operation with M$ Exchange.
793 * Annoyingly enough, Exchange's refusal to cope is technically legal
794 * under RFC2062. Trust Microsoft, the Great Enemy of interoperability
795 * standards, to find a way to make standards compliance irritating....
797 switch (imap_version)
799 case IMAP4rev1: /* RFC 2060 */
801 gen_send(sock, "FETCH %d BODY.PEEK[TEXT]", number);
803 gen_send(sock, "FETCH %d BODY[TEXT]", number);
806 case IMAP4: /* RFC 1730 */
808 gen_send(sock, "FETCH %d RFC822.TEXT.PEEK", number);
810 gen_send(sock, "FETCH %d RFC822.TEXT", number);
813 default: /* RFC 1176 */
814 gen_send(sock, "FETCH %d RFC822.TEXT", number);
818 /* looking for FETCH response */
822 if ((ok = gen_recv(sock, buf, sizeof(buf))))
825 (!strstr(buf+4, "FETCH") || sscanf(buf+2, "%d", &num) != 1);
831 * Try to extract a length from the FETCH response. RFC2060 requires
832 * it to be present, but at least one IMAP server (Novell GroupWise)
835 if ((cp = strchr(buf, '{')))
836 *lenp = atoi(cp + 1);
838 *lenp = -1; /* missing length part in FETCH reponse */
843 static int imap_trail(int sock, struct query *ctl, int number)
844 /* discard tail of FETCH response after reading message text */
846 /* expunges change the fetch numbers */
847 /* number -= expunged; */
851 char buf[MSGBUFSIZE+1];
854 if ((ok = gen_recv(sock, buf, sizeof(buf))))
857 /* UW IMAP returns "OK FETCH", Cyrus returns "OK Completed" */
858 if (strstr(buf, "OK"))
863 * Any IMAP server that fails to set Seen on a BODY[TEXT]
864 * fetch violates RFC2060 p.43 (top). This becomes an issue
865 * when keep is on, because seen messages aren't deleted and
866 * get refetched on each poll. As a workaround, if keep is on
867 * we can set the Seen flag explicitly.
869 * This code isn't used yet because we don't know of any IMAP
870 * servers broken in this way.
873 if ((ok = gen_transact(sock,
874 imap_version == IMAP4
875 ? "STORE %d +FLAGS.SILENT (\\Seen)"
876 : "STORE %d +FLAGS (\\Seen)",
879 #endif /* __UNUSED__ */
885 static int imap_delete(int sock, struct query *ctl, int number)
886 /* set delete flag for given message */
890 /* expunges change the fetch numbers */
894 * Use SILENT if possible as a minor throughput optimization.
895 * Note: this has been dropped from IMAP4rev1.
897 * We set Seen because there are some IMAP servers (notably HP
898 * OpenMail) that do message-receipt DSNs, but only when the seen
899 * bit is set. This is the appropriate time -- we get here right
900 * after the local SMTP response that says delivery was
903 if ((ok = gen_transact(sock,
904 imap_version == IMAP4
905 ? "STORE %d +FLAGS.SILENT (\\Seen \\Deleted)"
906 : "STORE %d +FLAGS (\\Seen \\Deleted)",
913 * We do an expunge after expunge_period messages, rather than
914 * just before quit, so that a line hit during a long session
915 * won't result in lots of messages being fetched again during
918 if (NUM_NONZERO(expunge_period) && (deletions % expunge_period) == 0)
919 internal_expunge(sock);
924 static int imap_logout(int sock, struct query *ctl)
925 /* send logout command */
927 /* if any un-expunged deletions remain, ship an expunge now */
929 internal_expunge(sock);
932 /* Memory clean-up */
934 free(unseen_messages);
935 #endif /* USE_SEARCH */
937 return(gen_transact(sock, "LOGOUT"));
940 const static struct method imap =
942 "IMAP", /* Internet Message Access Protocol */
946 #else /* INET6_ENABLE */
947 143, /* standard IMAP2bis/IMAP4 port */
948 993, /* ssl IMAP2bis/IMAP4 port */
949 #endif /* INET6_ENABLE */
950 TRUE, /* this is a tagged protocol */
951 FALSE, /* no message delimiter */
952 imap_ok, /* parse command response */
953 imap_getauth, /* get authorization */
954 imap_getrange, /* query range of messages */
955 imap_getsizes, /* get sizes of messages (used for ESMTP SIZE option) */
956 imap_is_old, /* no UID check */
957 imap_fetch_headers, /* request given message headers */
958 imap_fetch_body, /* request given message body */
959 imap_trail, /* eat message trailer */
960 imap_delete, /* delete the message */
961 imap_logout, /* expunge and exit */
962 TRUE, /* yes, we can re-poll */
965 int doIMAP(struct query *ctl)
966 /* retrieve messages using IMAP Version 2bis or Version 4 */
968 return(do_protocol(ctl, &imap));
971 /* imap.c ends here */