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"
26 #ifndef strstr /* glibc-2.1 declares this as a macro */
27 extern char *strstr(); /* needed on sysV68 R3V7.1. */
31 #define PHASE_GETAUTH 0
32 #define PHASE_GETRANGE 1
33 #define PHASE_GETSIZES 2
35 #define PHASE_LOGOUT 4
39 static char lastok[POPBUFSIZE+1];
42 int pop3_ok (int sock, char *argbuf)
43 /* parse command response */
46 char buf [POPBUFSIZE+1];
49 if ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
52 if (*bufp == '+' || *bufp == '-')
57 while (isalpha(*bufp))
63 if (strcmp(buf,"+OK") == 0)
70 else if (strcmp(buf,"-ERR") == 0)
72 if (phase > PHASE_GETAUTH)
75 * We're checking for "lock busy", "unable to lock",
76 * "already locked" etc. here. This indicates that we
77 * have to wait for the server to clean up before we
80 * PS_LOCKBUSY check empirically verified with two recent
81 * versions the Berkeley popper; QPOP (version 2.2) and
82 * QUALCOMM Pop server derived from UCB (version 2.1.4-R3)
84 else if (strstr(bufp,"lock")||strstr(bufp,"Lock")||strstr(bufp,"LOCK"))
101 int pop3_getauth(int sock, struct query *ctl, char *greeting)
102 /* apply for connection authorization */
111 phase = PHASE_GETAUTH;
113 switch (ctl->server.protocol) {
115 ok = gen_transact(sock, "USER %s", ctl->remotename);
119 * CompuServe has changed its RPA behavior. Used to be they didn't
120 * accept PASS, but I'm told this changed in mid-November 1997.
122 if (strstr(greeting, "csi.com"))
124 /* temporary fix to get back out of cleartext authentication */
125 gen_transact(sock, "PASS %s", "dummypass");
127 /* AUTH command should return a list of available mechanisms */
128 if (gen_transact(sock, "AUTH") == 0)
131 flag has_rpa = FALSE;
133 while ((ok = gen_recv(sock, buffer, sizeof(buffer))) == 0)
135 if (buffer[0] == '.')
137 if (strncasecmp(buffer, "rpa", 3) == 0)
140 if (has_rpa && !POP3_auth_rpa(ctl->remotename,
141 ctl->password, sock))
147 #endif /* RPA_ENABLE */
150 /* see RFC1938: A One-Time Password System */
151 if (challenge = strstr(lastok, "otp-")) {
152 char response[OPIE_RESPONSE_MAX+1];
155 i = opiegenerator(challenge, !strcmp(ctl->password, "opie") ? "" : ctl->password, response);
156 if ((i == -2) && (cmd_daemon == -1)) {
157 char secret[OPIE_SECRET_MAX+1];
158 fprintf(stderr, "Secret pass phrase: ");
159 if (opiereadpass(secret, sizeof(secret), 0))
160 i = opiegenerator(challenge, secret, response);
161 memset(secret, 0, sizeof(secret));
169 ok = gen_transact(sock, "PASS %s", response);
174 /* ordinary validation, no one-time password or RPA */
175 ok = gen_transact(sock, "PASS %s", ctl->password);
179 /* build MD5 digest from greeting timestamp + password */
180 /* find start of timestamp */
181 for (start = greeting; *start != 0 && *start != '<'; start++)
184 error(0, -1, "Required APOP timestamp not found in greeting");
188 /* find end of timestamp */
189 for (end = start; *end != 0 && *end != '>'; end++)
191 if (*end == 0 || end == start + 1) {
192 error(0, -1, "Timestamp syntax error in greeting");
198 /* copy timestamp and password into digestion buffer */
199 msg = (char *)xmalloc((end-start+1) + strlen(ctl->password) + 1);
201 strcat(msg,ctl->password);
203 strcpy(ctl->digest, MD5Digest(msg));
206 ok = gen_transact(sock, "APOP %s %s", ctl->remotename, ctl->digest);
210 if ((ok = gen_transact(sock,"USER %s", ctl->remotename)) == 0)
211 ok = gen_transact(sock, "RPOP %s", ctl->password);
215 error(0, 0, "Undefined protocol request in POP3_auth");
221 /* maybe we detected a lock-busy condition? */
222 if (ok == PS_LOCKBUSY)
223 error(0, 0, "lock busy! Is another session active?");
229 * Empirical experience shows some server/OS combinations
230 * may need a brief pause even after any lockfiles on the
231 * server are released, to give the server time to finish
232 * copying back very large mailfolders from the temp-file...
233 * this is only ever an issue with extremely large mailboxes.
235 sleep(3); /* to be _really_ safe, probably need sleep(5)! */
242 pop3_gettopid( int sock, int num , char *id)
246 char buf [POPBUFSIZE+1];
247 sprintf( buf, "TOP %d 1", num );
248 if( (ok = gen_transact(sock, buf ) ) != 0 )
251 while((ok = gen_recv(sock, buf, sizeof(buf))) == 0) {
254 if( ! got_it && ! strncasecmp("Message-Id:", buf, 11 )) {
256 /* prevent stack overflows */
258 sscanf( buf+12, "%s", id);
265 pop3_slowuidl( int sock, struct query *ctl, int *countp, int *newp)
267 /* This approach tries to get the message headers from the
268 * remote hosts and compares the message-id to the already known
270 * + if the first message containes a new id, all messages on
271 * the server will be new
272 * + if the first is known, try to estimate the last known message
273 * on the server and check. If this works you know the total number
274 * of messages to get.
275 * + Otherwise run a binary search to determine the last known message
277 int ok, nolinear = 0;
278 int first_nr, list_len, try_id, try_nr, add_id;
282 if( (ok = pop3_gettopid( sock, 1, id )) != 0 )
285 if( ( first_nr = str_nr_in_list(&ctl->oldsaved, id) ) == -1 ) {
286 /* the first message is unknown -> all messages are new */
291 /* check where we expect the latest known message */
292 list_len = count_list( &ctl->oldsaved );
293 try_id = list_len - first_nr; /* -1 + 1 */
295 if( try_id <= *countp ) {
296 if( (ok = pop3_gettopid( sock, try_id, id )) != 0 )
299 try_nr = str_nr_last_in_list(&ctl->oldsaved, id);
304 if( try_nr != list_len -1 ) {
305 /* some messages inbetween have been deleted... */
309 for( add_id = 1<<30; add_id > try_id-1; add_id >>= 1 )
311 for( ; add_id; add_id >>= 1 ) {
313 if( try_id - add_id <= 1 ) {
320 if( (ok = pop3_gettopid( sock, try_id, id )) != 0 )
322 try_nr = str_nr_in_list(&ctl->oldsaved, id);
328 error(0,0,"Messages inserted into list on server. "
329 "Cannot handle this.");
334 /* the first try_id messages are known -> copy them to the newsaved list */
335 for( num = first_nr; num < list_len; num++ )
337 struct idlist *new = save_str(&ctl->newsaved,
338 str_from_nr_list(&ctl->oldsaved, num),
340 new->val.status.num = num - first_nr + 1;
344 free_str_list(&ctl->oldsaved);
349 *newp = *countp - try_id;
353 static int pop3_getrange(int sock,
356 int *countp, int *newp)
357 /* get range of messages to be fetched */
360 char buf [POPBUFSIZE+1];
362 phase = PHASE_GETRANGE;
364 /* Ensure that the new list is properly empty */
365 ctl->newsaved = (struct idlist *)NULL;
368 /* Alain Knaff suggests this, but it's not RFC standard */
370 if ((ok = gen_transact(sock, "MBOX %s", folder)))
374 /* get the total message count */
375 gen_send(sock, "STAT");
376 ok = pop3_ok(sock, buf);
378 sscanf(buf,"%d %*d", countp);
383 * Newer, RFC-1725-conformant POP servers may not have the LAST command.
384 * We work as hard as possible to hide this ugliness, but it makes
385 * counting new messages intrinsically quadratic in the worst case.
389 if (*countp > 0 && !ctl->fetchall)
393 if (!ctl->server.uidl) {
394 gen_send(sock, "LAST");
395 ok = pop3_ok(sock, buf);
400 if (sscanf(buf, "%d", &last) == 0)
402 error(0, 0, "protocol error");
405 *newp = (*countp - last);
409 /* grab the mailbox's UID list */
410 if ((ok = gen_transact(sock, "UIDL")) != 0)
412 /* don't worry, yet! do it the slow way */
413 if((ok = pop3_slowuidl( sock, ctl, countp, newp))!=0)
415 error(0, 0, "protocol error while fetching UIDLs");
424 while ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
428 else if (sscanf(buf, "%d %s", &num, id) == 2)
432 new = save_str(&ctl->newsaved, id, UID_UNSEEN);
433 new->val.status.num = num;
435 /* note: ID comparison is caseblind */
436 if (str_in_list(&ctl->oldsaved, id)) {
437 new->val.status.mark = UID_SEEN;
438 str_set_mark(&ctl->oldsaved, id, UID_SEEN);
451 static int pop3_getsizes(int sock, int count, int *sizes)
452 /* capture the sizes of all messages */
456 /* phase = PHASE_GETSIZES */
458 if ((ok = gen_transact(sock, "LIST")) != 0)
462 char buf [POPBUFSIZE+1];
464 while ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
470 else if (sscanf(buf, "%d %d", &num, &size) == 2)
471 sizes[num - 1] = size;
478 static int pop3_is_old(int sock, struct query *ctl, int num)
479 /* is the given message old? */
482 return (num <= last);
484 /* note: ID comparison is caseblind */
485 return (str_in_list(&ctl->oldsaved,
486 str_find (&ctl->newsaved, num)));
489 static int pop3_fetch(int sock, struct query *ctl, int number, int *lenp)
490 /* request nth message */
493 char buf [POPBUFSIZE+1];
495 /* phase = PHASE_FETCH */
497 gen_send(sock, "RETR %d", number);
498 if ((ok = pop3_ok(sock, buf)) != 0)
501 *lenp = -1; /* we got sizes from the LIST response */
506 static int pop3_delete(int sock, struct query *ctl, int number)
507 /* delete a given message */
509 /* actually, mark for deletion -- doesn't happen until QUIT time */
510 return(gen_transact(sock, "DELE %d", number));
513 static int pop3_logout(int sock, struct query *ctl)
514 /* send logout command */
518 /* phase = PHASE_LOGOUT */
520 ok = gen_transact(sock, "QUIT");
527 const static struct method pop3 =
529 "POP3", /* Post Office Protocol v3 */
531 "pop3", /* standard POP3 port */
533 110, /* standard POP3 port */
535 FALSE, /* this is not a tagged protocol */
536 TRUE, /* this uses a message delimiter */
537 pop3_ok, /* parse command response */
538 NULL, /* no password canonicalization */
539 pop3_getauth, /* get authorization */
540 pop3_getrange, /* query range of messages */
541 pop3_getsizes, /* we can get a list of sizes */
542 pop3_is_old, /* how do we tell a message is old? */
543 pop3_fetch, /* request given message */
544 NULL, /* no way to fetch body alone */
545 NULL, /* no message trailer */
546 pop3_delete, /* how to delete a message */
547 pop3_logout, /* log out, we're done */
548 FALSE, /* no, we can't re-poll */
551 int doPOP3 (struct query *ctl)
552 /* retrieve messages using POP3 */
555 if (ctl->mailboxes->id) {
556 fprintf(stderr,"Option --remote is not supported with POP3\n");
560 peek_capable = FALSE;
561 return(do_protocol(ctl, &pop3));
563 #endif /* POP3_ENABLE */
565 /* pop3.c ends here */