2 * pop3.c -- POP3 protocol methods
4 * For license terms, see the file COPYING in this directory.
12 #if defined(HAVE_UNISTD_H)
15 #if defined(STDC_HEADERS)
19 #include "fetchmail.h"
24 #endif /* HAVE_LIBOPIE */
26 #define PROTOCOL_ERROR {error(0, 0, "protocol error"); return(PS_ERROR);}
28 #define LOCKBUSY_ERROR {error(0, 0, "lock busy! Is another session active?"); return(PS_LOCKBUSY);}
30 extern char *strstr(); /* needed on sysV68 R3V7.1. */
35 static char lastok[POPBUFSIZE+1];
36 #endif /* HAVE_LIBOPIE */
38 int pop3_ok (int sock, char *argbuf)
39 /* parse command response */
42 char buf [POPBUFSIZE+1];
45 if ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
48 if (*bufp == '+' || *bufp == '-')
53 while (isalpha(*bufp))
57 if (strcmp(buf,"+OK") == 0)
61 #endif /* HAVE_LIBOPIE */
64 else if (strcmp(buf,"-ERR") == 0)
67 * We're checking for "lock busy", "unable to lock",
68 * "already locked" etc. here. This indicates that we
69 * have to wait for the server to clean up before we
72 * PS_LOCKBUSY check empirically verified with two recent
73 * versions the Berkeley popper; QPOP (version 2.2) and
74 * QUALCOMM Pop server derived from UCB (version 2.1.4-R3)
76 if (strstr(bufp,"lock")||strstr(bufp,"Lock")||strstr(bufp,"LOCK"))
91 int pop3_getauth(int sock, struct query *ctl, char *greeting)
92 /* apply for connection authorization */
97 #endif /* HAVE_LIBOPIE */
99 /* build MD5 digest from greeting timestamp + password */
100 if (ctl->server.protocol == P_APOP)
105 /* find start of timestamp */
106 for (start = greeting; *start != 0 && *start != '<'; start++)
109 error(0, -1, "Required APOP timestamp not found in greeting");
113 /* find end of timestamp */
114 for (end = start; *end != 0 && *end != '>'; end++)
116 if (*end == 0 || end == start + 1) {
117 error(0, -1, "Timestamp syntax error in greeting");
123 /* copy timestamp and password into digestion buffer */
124 msg = (char *)xmalloc((end-start+1) + strlen(ctl->password) + 1);
126 strcat(msg,ctl->password);
128 strcpy(ctl->digest, MD5Digest(msg));
132 switch (ctl->server.protocol) {
134 if ((gen_transact(sock, "USER %s", ctl->remotename)) != 0)
138 /* see RFC1938: A One-Time Password System */
139 if (challenge = strstr(lastok, "otp-"))
141 char response[OPIE_RESPONSE_MAX+1];
144 * Special case in case we're running Craig Metz's
145 * OPIE daemon. Code in opiegenerator() will detect this.
147 if (ctl->password && !strcmp(ctl->password, "opie"))
149 if (ok = opiegenerator(challenge, "", response))
153 else if (opiegenerator(challenge, ctl->password, response))
156 ok = gen_transact(sock, "PASS %s", response);
160 /* ordinary validation, no one-time password */
161 ok = gen_transact(sock, "PASS %s", ctl->password);
162 #endif /* HAVE_LIBOPIE */
166 if (ok == PS_LOCKBUSY)
172 * Empirical experience shows some server/OS combinations
173 * may need a brief pause even after any lockfiles on the
174 * server are released, to give the server time to finish
175 * copying back very large mailfolders from the temp-file...
176 * this is only ever an issue with extremely large mailboxes.
178 sleep(3); /* to be _really_ safe, probably need sleep(5)! */
182 if ((gen_transact(sock, "APOP %s %s",
183 ctl->remotename, ctl->digest)) != 0)
188 if ((gen_transact(sock,"USER %s", ctl->remotename)) != 0)
191 if ((gen_transact(sock, "RPOP %s", ctl->password)) != 0)
196 error(0, 0, "Undefined protocol request in POP3_auth");
203 static int pop3_getrange(int sock,
206 int *countp, int *newp)
207 /* get range of messages to be fetched */
210 char buf [POPBUFSIZE+1];
212 /* Ensure that the new list is properly empty */
213 ctl->newsaved = (struct idlist *)NULL;
215 /* get the total message count */
216 gen_send(sock, "STAT");
217 ok = pop3_ok(sock, buf);
219 sscanf(buf,"%d %*d", countp);
224 * Newer, RFC-1725-conformant POP servers may not have the LAST command.
225 * We work as hard as possible to hide this ugliness, but it makes
226 * counting new messages intrinsically quadratic in the worst case.
230 if (*countp > 0 && !ctl->fetchall)
234 if (!ctl->server.uidl) {
235 gen_send(sock, "LAST");
236 ok = pop3_ok(sock, buf);
241 if (sscanf(buf, "%d", &last) == 0)
243 *newp = (*countp - last);
247 /* grab the mailbox's UID list */
248 if ((ok = gen_transact(sock, "UIDL")) != 0)
255 while ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
259 else if (sscanf(buf, "%d %s", &num, id) == 2)
261 save_str(&ctl->newsaved, num, id);
263 /* note: ID comparison is caseblind */
264 if (!str_in_list(&ctl->oldsaved, id))
275 static int pop3_getsizes(int sock, int count, int *sizes)
276 /* capture the sizes of all messages */
280 if ((ok = gen_transact(sock, "LIST")) != 0)
284 char buf [POPBUFSIZE+1];
286 while ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
292 else if (sscanf(buf, "%d %d", &num, &size) == 2)
293 sizes[num - 1] = size;
302 static int pop3_is_old(int sock, struct query *ctl, int num)
303 /* is the given message old? */
306 return (num <= last);
308 /* note: ID comparison is caseblind */
309 return (str_in_list(&ctl->oldsaved,
310 str_find (&ctl->newsaved, num)));
313 static int pop3_fetch(int sock, struct query *ctl, int number, int *lenp)
314 /* request nth message */
317 char buf [POPBUFSIZE+1], *cp;
319 gen_send(sock, "RETR %d", number);
320 if ((ok = pop3_ok(sock, buf)) != 0)
324 * Look for "nnn octets" -- there may or may not be preceding cruft.
325 * It's OK to punt and pass back -1 as a failure indication here, as
326 * long as the force_getsizes flag has forced sizes to be preloaded.
328 if ((cp = strstr(buf, " octets")) == (char *)NULL)
332 while (--cp >= buf && isdigit(*cp))
340 static int pop3_delete(int sock, struct query *ctl, int number)
341 /* delete a given message */
343 return(gen_transact(sock, "DELE %d", number));
346 const static struct method pop3 =
348 "POP3", /* Post Office Protocol v3 */
349 110, /* standard POP3 port */
350 FALSE, /* this is not a tagged protocol */
351 TRUE, /* this uses a message delimiter */
352 TRUE, /* RFC 1725 doesn't require a size field in fetch */
353 pop3_ok, /* parse command response */
354 pop3_getauth, /* get authorization */
355 pop3_getrange, /* query range of messages */
356 pop3_getsizes, /* we can get a list of sizes */
357 pop3_is_old, /* how do we tell a message is old? */
358 pop3_fetch, /* request given message */
359 NULL, /* no way to fetch body alone */
360 NULL, /* no message trailer */
361 pop3_delete, /* how to delete a message */
362 "QUIT", /* the POP3 exit command */
365 int doPOP3 (struct query *ctl)
366 /* retrieve messages using POP3 */
368 if (ctl->mailboxes->id) {
369 fprintf(stderr,"Option --remote is not supported with POP3\n");
372 peek_capable = FALSE;
373 return(do_protocol(ctl, &pop3));
376 /* pop3.c ends here */