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)
137 #if defined(HAVE_LIBOPIE) && defined(OPIE_ENABLE)
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 (strcmp(ctl->password, "opie") == 0)
149 if (ok = opiegenerator(challenge, "", response))
155 * Otherwise, generate a challenge from whatever secret we
156 * have previously collected.
158 else if (opiegenerator(challenge, ctl->password, response))
161 ok = gen_transact(sock, "PASS %s", response);
164 #endif /* defined(HAVE_LIBOPIE) && defined(OPIE_ENABLE) */
165 /* ordinary validation, no one-time password */
166 ok = gen_transact(sock, "PASS %s", ctl->password);
170 if (ok == PS_LOCKBUSY)
176 * Empirical experience shows some server/OS combinations
177 * may need a brief pause even after any lockfiles on the
178 * server are released, to give the server time to finish
179 * copying back very large mailfolders from the temp-file...
180 * this is only ever an issue with extremely large mailboxes.
182 sleep(3); /* to be _really_ safe, probably need sleep(5)! */
186 if ((gen_transact(sock, "APOP %s %s",
187 ctl->remotename, ctl->digest)) != 0)
192 if ((gen_transact(sock,"USER %s", ctl->remotename)) != 0)
195 if ((gen_transact(sock, "RPOP %s", ctl->password)) != 0)
200 error(0, 0, "Undefined protocol request in POP3_auth");
208 pop3_gettopid( int sock, int num , char *id)
212 char buf [POPBUFSIZE+1];
213 sprintf( buf, "TOP %d 1", num );
214 if( (ok = gen_transact(sock, buf ) ) != 0 )
217 while((ok = gen_recv(sock, buf, sizeof(buf))) == 0) {
220 if( ! got_it && ! strncasecmp("Message-Id:", buf, 11 )) {
222 sscanf( buf+12, "%s", id);
229 pop3_slowuidl( int sock, struct query *ctl, int *countp, int *newp)
231 /* This approach tries to get the message headers from the
232 * remote hosts and compares the message-id to the already known
234 * + if the first message containes a new id, all messages on
235 * the server will be new
236 * + if the first is known, try to estimate the last known message
237 * on the server and check. If this works you know the total number
238 * of messages to get.
239 * + Otherwise run a binary search to determine the last known message
241 int ok, nolinear = 0;
242 int first_nr, list_len, try_id, try_nr, hop_id, add_id;
246 if( (ok = pop3_gettopid( sock, 1, id )) != 0 )
249 if( ( first_nr = str_nr_in_list(&ctl->oldsaved, id) ) == -1 ) {
250 /* the first message is unknown -> all messages are new */
255 /* check where we expect the latest known message */
256 list_len = count_list( &ctl->oldsaved );
257 try_id = list_len - first_nr; /* -1 + 1 */
259 if( try_id <= *countp ) {
260 if( (ok = pop3_gettopid( sock, try_id, id )) != 0 )
263 try_nr = str_nr_in_list(&ctl->oldsaved, id);
268 if( try_nr != list_len -1 ) {
269 /* some messages inbetween have been deleted... */
273 for( add_id = 1<<30; add_id > try_id-1; add_id >>= 1 )
275 for( ; add_id; add_id >>= 1 ) {
277 if( try_id - add_id <= 1 ) {
284 if( (ok = pop3_gettopid( sock, try_id, id )) != 0 )
286 try_nr = str_nr_in_list(&ctl->oldsaved, id);
292 error(0,0,"Messages inserted into list on server. "
293 "Cannot handle this.");
298 /* The first try_id messages are known -> copy them to
300 for( num = first_nr; num < list_len; num++ )
301 save_str(&ctl->newsaved, num-first_nr + 1,
302 str_from_nr_list( &ctl->oldsaved, num ));
305 free_str_list(&ctl->oldsaved);
310 *newp = *countp - try_id;
314 static int pop3_getrange(int sock,
317 int *countp, int *newp)
318 /* get range of messages to be fetched */
321 char buf [POPBUFSIZE+1];
323 /* Ensure that the new list is properly empty */
324 ctl->newsaved = (struct idlist *)NULL;
327 /* Alain Knaff suggests this, but it's not RFC standard */
329 if ((ok = gen_transact(sock, "MBOX %s", folder)))
333 /* get the total message count */
334 gen_send(sock, "STAT");
335 ok = pop3_ok(sock, buf);
337 sscanf(buf,"%d %*d", countp);
342 * Newer, RFC-1725-conformant POP servers may not have the LAST command.
343 * We work as hard as possible to hide this ugliness, but it makes
344 * counting new messages intrinsically quadratic in the worst case.
348 if (*countp > 0 && !ctl->fetchall)
352 if (!ctl->server.uidl) {
353 gen_send(sock, "LAST");
354 ok = pop3_ok(sock, buf);
359 if (sscanf(buf, "%d", &last) == 0)
361 *newp = (*countp - last);
365 /* grab the mailbox's UID list */
366 if ((ok = gen_transact(sock, "UIDL")) != 0)
368 /* don't worry, yet! do it the slow way */
369 if((ok = pop3_slowuidl( sock, ctl, countp, newp))!=0)
377 while ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
381 else if (sscanf(buf, "%d %s", &num, id) == 2)
383 save_str(&ctl->newsaved, num, id);
385 /* note: ID comparison is caseblind */
386 if (!str_in_list(&ctl->oldsaved, id))
397 static int pop3_getsizes(int sock, int count, int *sizes)
398 /* capture the sizes of all messages */
402 if ((ok = gen_transact(sock, "LIST")) != 0)
406 char buf [POPBUFSIZE+1];
408 while ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
414 else if (sscanf(buf, "%d %d", &num, &size) == 2)
415 sizes[num - 1] = size;
424 static int pop3_is_old(int sock, struct query *ctl, int num)
425 /* is the given message old? */
428 return (num <= last);
430 /* note: ID comparison is caseblind */
431 return (str_in_list(&ctl->oldsaved,
432 str_find (&ctl->newsaved, num)));
435 static int pop3_fetch(int sock, struct query *ctl, int number, int *lenp)
436 /* request nth message */
439 char buf [POPBUFSIZE+1], *cp;
441 gen_send(sock, "RETR %d", number);
442 if ((ok = pop3_ok(sock, buf)) != 0)
445 *lenp = -1; /* we got sizes from the LIST response */
450 static int pop3_delete(int sock, struct query *ctl, int number)
451 /* delete a given message */
453 /* actually, mark for deletion -- doesn't happen until QUIT time */
454 return(gen_transact(sock, "DELE %d", number));
457 const static struct method pop3 =
459 "POP3", /* Post Office Protocol v3 */
460 110, /* standard POP3 port */
461 FALSE, /* this is not a tagged protocol */
462 TRUE, /* this uses a message delimiter */
463 pop3_ok, /* parse command response */
464 pop3_getauth, /* get authorization */
465 pop3_getrange, /* query range of messages */
466 pop3_getsizes, /* we can get a list of sizes */
467 pop3_is_old, /* how do we tell a message is old? */
468 pop3_fetch, /* request given message */
469 NULL, /* no way to fetch body alone */
470 NULL, /* no message trailer */
471 pop3_delete, /* how to delete a message */
472 "QUIT", /* the POP3 exit command */
475 int doPOP3 (struct query *ctl)
476 /* retrieve messages using POP3 */
479 if (ctl->mailboxes->id) {
480 fprintf(stderr,"Option --remote is not supported with POP3\n");
484 peek_capable = FALSE;
485 return(do_protocol(ctl, &pop3));
488 /* pop3.c ends here */