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 extern char *strstr(); /* needed on sysV68 R3V7.1. */
31 static char lastok[POPBUFSIZE+1];
32 #endif /* HAVE_LIBOPIE */
34 int pop3_ok (int sock, char *argbuf)
35 /* parse command response */
38 char buf [POPBUFSIZE+1];
41 if ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
44 if (*bufp == '+' || *bufp == '-')
49 while (isalpha(*bufp))
55 if (strcmp(buf,"+OK") == 0)
59 #endif /* HAVE_LIBOPIE */
62 else if (strcmp(buf,"-ERR") == 0)
65 * We're checking for "lock busy", "unable to lock",
66 * "already locked" etc. here. This indicates that we
67 * have to wait for the server to clean up before we
70 * PS_LOCKBUSY check empirically verified with two recent
71 * versions the Berkeley popper; QPOP (version 2.2) and
72 * QUALCOMM Pop server derived from UCB (version 2.1.4-R3)
74 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 */
99 #endif /* HAVE_LIBOPIE */
101 switch (ctl->server.protocol) {
103 if ((ok = gen_transact(sock, "USER %s", ctl->remotename)) != 0)
106 #if defined(HAVE_LIBOPIE) && defined(OPIE_ENABLE)
107 /* see RFC1938: A One-Time Password System */
108 if (challenge = strstr(greeting, "otp-"))
110 char response[OPIE_RESPONSE_MAX+1];
112 if (opiegenerator(challenge, ctl->password, response))
118 ok = gen_transact(sock, "PASS %s", response);
121 #endif /* defined(HAVE_LIBOPIE) && defined(OPIE_ENABLE) */
124 /* if we're talking to CompuServe, try RPA */
125 if (strstr(greeting, "csi.com"))
127 /* AUTH command should return a list of available mechanisms */
128 if (gen_transact(sock, "AUTH") == 0)
131 flag authenticated = FALSE;
133 while ((ok = gen_recv(sock, buffer, sizeof(buffer))) == 0)
135 if (buffer[0] == '.')
137 if (strncasecmp(buffer, "rpa", 3) == 0)
139 if (POP3_auth_rpa(ctl->remotename,
140 ctl->password, sock) == PS_SUCCEED)
142 authenticated = TRUE;
152 #endif /* ENABLE_RPA */
154 /* ordinary validation, no one-time password or RPA */
155 ok = gen_transact(sock, "PASS %s", ctl->password);
159 /* build MD5 digest from greeting timestamp + password */
160 /* find start of timestamp */
161 for (start = greeting; *start != 0 && *start != '<'; start++)
164 error(0, -1, "Required APOP timestamp not found in greeting");
168 /* find end of timestamp */
169 for (end = start; *end != 0 && *end != '>'; end++)
171 if (*end == 0 || end == start + 1) {
172 error(0, -1, "Timestamp syntax error in greeting");
178 /* copy timestamp and password into digestion buffer */
179 msg = (char *)xmalloc((end-start+1) + strlen(ctl->password) + 1);
181 strcat(msg,ctl->password);
183 strcpy(ctl->digest, MD5Digest(msg));
186 ok = gen_transact(sock, "APOP %s %s", ctl->remotename, ctl->digest);
190 if ((ok = gen_transact(sock,"USER %s", ctl->remotename)) == 0)
191 ok = gen_transact(sock, "RPOP %s", ctl->password);
195 error(0, 0, "Undefined protocol request in POP3_auth");
199 /* maybe we detected a lock-busy condition? */
202 if (ok == PS_LOCKBUSY)
204 error(0, 0, "lock busy! Is another session active?");
210 * Empirical experience shows some server/OS combinations
211 * may need a brief pause even after any lockfiles on the
212 * server are released, to give the server time to finish
213 * copying back very large mailfolders from the temp-file...
214 * this is only ever an issue with extremely large mailboxes.
216 sleep(3); /* to be _really_ safe, probably need sleep(5)! */
223 pop3_gettopid( int sock, int num , char *id)
227 char buf [POPBUFSIZE+1];
228 sprintf( buf, "TOP %d 1", num );
229 if( (ok = gen_transact(sock, buf ) ) != 0 )
232 while((ok = gen_recv(sock, buf, sizeof(buf))) == 0) {
235 if( ! got_it && ! strncasecmp("Message-Id:", buf, 11 )) {
237 sscanf( buf+12, "%s", id);
244 pop3_slowuidl( int sock, struct query *ctl, int *countp, int *newp)
246 /* This approach tries to get the message headers from the
247 * remote hosts and compares the message-id to the already known
249 * + if the first message containes a new id, all messages on
250 * the server will be new
251 * + if the first is known, try to estimate the last known message
252 * on the server and check. If this works you know the total number
253 * of messages to get.
254 * + Otherwise run a binary search to determine the last known message
256 int ok, nolinear = 0;
257 int first_nr, list_len, try_id, try_nr, hop_id, add_id;
261 if( (ok = pop3_gettopid( sock, 1, id )) != 0 )
264 if( ( first_nr = str_nr_in_list(&ctl->oldsaved, id) ) == -1 ) {
265 /* the first message is unknown -> all messages are new */
270 /* check where we expect the latest known message */
271 list_len = count_list( &ctl->oldsaved );
272 try_id = list_len - first_nr; /* -1 + 1 */
274 if( try_id <= *countp ) {
275 if( (ok = pop3_gettopid( sock, try_id, id )) != 0 )
278 try_nr = str_nr_in_list(&ctl->oldsaved, id);
283 if( try_nr != list_len -1 ) {
284 /* some messages inbetween have been deleted... */
288 for( add_id = 1<<30; add_id > try_id-1; add_id >>= 1 )
290 for( ; add_id; add_id >>= 1 ) {
292 if( try_id - add_id <= 1 ) {
299 if( (ok = pop3_gettopid( sock, try_id, id )) != 0 )
301 try_nr = str_nr_in_list(&ctl->oldsaved, id);
307 error(0,0,"Messages inserted into list on server. "
308 "Cannot handle this.");
313 /* The first try_id messages are known -> copy them to
315 for( num = first_nr; num < list_len; num++ )
316 save_str(&ctl->newsaved, num-first_nr + 1,
317 str_from_nr_list( &ctl->oldsaved, num ));
320 free_str_list(&ctl->oldsaved);
325 *newp = *countp - try_id;
329 static int pop3_getrange(int sock,
332 int *countp, int *newp)
333 /* get range of messages to be fetched */
336 char buf [POPBUFSIZE+1];
338 /* Ensure that the new list is properly empty */
339 ctl->newsaved = (struct idlist *)NULL;
342 /* Alain Knaff suggests this, but it's not RFC standard */
344 if ((ok = gen_transact(sock, "MBOX %s", folder)))
348 /* get the total message count */
349 gen_send(sock, "STAT");
350 ok = pop3_ok(sock, buf);
352 sscanf(buf,"%d %*d", countp);
357 * Newer, RFC-1725-conformant POP servers may not have the LAST command.
358 * We work as hard as possible to hide this ugliness, but it makes
359 * counting new messages intrinsically quadratic in the worst case.
363 if (*countp > 0 && !ctl->fetchall)
367 if (!ctl->server.uidl) {
368 gen_send(sock, "LAST");
369 ok = pop3_ok(sock, buf);
374 if (sscanf(buf, "%d", &last) == 0)
376 error(0, 0, "protocol error");
379 *newp = (*countp - last);
383 /* grab the mailbox's UID list */
384 if ((ok = gen_transact(sock, "UIDL")) != 0)
386 /* don't worry, yet! do it the slow way */
387 if((ok = pop3_slowuidl( sock, ctl, countp, newp))!=0)
389 error(0, 0, "protocol error while fetching UIDLs");
398 while ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
402 else if (sscanf(buf, "%d %s", &num, id) == 2)
404 save_str(&ctl->newsaved, num, id);
406 /* note: ID comparison is caseblind */
407 if (!str_in_list(&ctl->oldsaved, id))
418 static int pop3_getsizes(int sock, int count, int *sizes)
419 /* capture the sizes of all messages */
423 if ((ok = gen_transact(sock, "LIST")) != 0)
427 char buf [POPBUFSIZE+1];
429 while ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
435 else if (sscanf(buf, "%d %d", &num, &size) == 2)
436 sizes[num - 1] = size;
443 static int pop3_is_old(int sock, struct query *ctl, int num)
444 /* is the given message old? */
447 return (num <= last);
449 /* note: ID comparison is caseblind */
450 return (str_in_list(&ctl->oldsaved,
451 str_find (&ctl->newsaved, num)));
454 static int pop3_fetch(int sock, struct query *ctl, int number, int *lenp)
455 /* request nth message */
458 char buf [POPBUFSIZE+1], *cp;
460 gen_send(sock, "RETR %d", number);
461 if ((ok = pop3_ok(sock, buf)) != 0)
464 *lenp = -1; /* we got sizes from the LIST response */
469 static int pop3_delete(int sock, struct query *ctl, int number)
470 /* delete a given message */
472 /* actually, mark for deletion -- doesn't happen until QUIT time */
473 return(gen_transact(sock, "DELE %d", number));
476 static int pop3_logout(int sock, struct query *ctl)
477 /* send logout command */
479 return(gen_transact(sock, "QUIT"));
482 const static struct method pop3 =
484 "POP3", /* Post Office Protocol v3 */
485 110, /* standard POP3 port */
486 FALSE, /* this is not a tagged protocol */
487 TRUE, /* this uses a message delimiter */
488 pop3_ok, /* parse command response */
489 pop3_getauth, /* get authorization */
490 pop3_getrange, /* query range of messages */
491 pop3_getsizes, /* we can get a list of sizes */
492 pop3_is_old, /* how do we tell a message is old? */
493 pop3_fetch, /* request given message */
494 NULL, /* no way to fetch body alone */
495 NULL, /* no message trailer */
496 pop3_delete, /* how to delete a message */
497 pop3_logout, /* log out, we're done */
498 FALSE, /* no, we can't re-poll */
501 int doPOP3 (struct query *ctl)
502 /* retrieve messages using POP3 */
505 if (ctl->mailboxes->id) {
506 fprintf(stderr,"Option --remote is not supported with POP3\n");
510 peek_capable = FALSE;
511 return(do_protocol(ctl, &pop3));
514 /* pop3.c ends here */