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 #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];
40 #endif /* HAVE_LIBOPIE */
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)
67 #endif /* HAVE_LIBOPIE */
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 */
109 #endif /* HAVE_LIBOPIE */
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.
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 */
149 #if defined(HAVE_LIBOPIE) && defined(OPIE_ENABLE)
150 /* see RFC1938: A One-Time Password System */
151 if (challenge = strstr(lastok, "otp-"))
153 char response[OPIE_RESPONSE_MAX+1];
155 if (opiegenerator(challenge, ctl->password, response))
161 ok = gen_transact(sock, "PASS %s", response);
164 #endif /* defined(HAVE_LIBOPIE) && defined(OPIE_ENABLE) */
166 /* ordinary validation, no one-time password or RPA */
167 ok = gen_transact(sock, "PASS %s", ctl->password);
171 /* build MD5 digest from greeting timestamp + password */
172 /* find start of timestamp */
173 for (start = greeting; *start != 0 && *start != '<'; start++)
176 error(0, -1, "Required APOP timestamp not found in greeting");
180 /* find end of timestamp */
181 for (end = start; *end != 0 && *end != '>'; end++)
183 if (*end == 0 || end == start + 1) {
184 error(0, -1, "Timestamp syntax error in greeting");
190 /* copy timestamp and password into digestion buffer */
191 msg = (char *)xmalloc((end-start+1) + strlen(ctl->password) + 1);
193 strcat(msg,ctl->password);
195 strcpy(ctl->digest, MD5Digest(msg));
198 ok = gen_transact(sock, "APOP %s %s", ctl->remotename, ctl->digest);
202 if ((ok = gen_transact(sock,"USER %s", ctl->remotename)) == 0)
203 ok = gen_transact(sock, "RPOP %s", ctl->password);
207 error(0, 0, "Undefined protocol request in POP3_auth");
211 /* maybe we detected a lock-busy condition? */
214 if (ok == PS_LOCKBUSY)
216 error(0, 0, "lock busy! Is another session active?");
222 * Empirical experience shows some server/OS combinations
223 * may need a brief pause even after any lockfiles on the
224 * server are released, to give the server time to finish
225 * copying back very large mailfolders from the temp-file...
226 * this is only ever an issue with extremely large mailboxes.
228 sleep(3); /* to be _really_ safe, probably need sleep(5)! */
235 pop3_gettopid( int sock, int num , char *id)
239 char buf [POPBUFSIZE+1];
240 sprintf( buf, "TOP %d 1", num );
241 if( (ok = gen_transact(sock, buf ) ) != 0 )
244 while((ok = gen_recv(sock, buf, sizeof(buf))) == 0) {
247 if( ! got_it && ! strncasecmp("Message-Id:", buf, 11 )) {
249 /* prevent stack overflows */
251 sscanf( buf+12, "%s", id);
258 pop3_slowuidl( int sock, struct query *ctl, int *countp, int *newp)
260 /* This approach tries to get the message headers from the
261 * remote hosts and compares the message-id to the already known
263 * + if the first message containes a new id, all messages on
264 * the server will be new
265 * + if the first is known, try to estimate the last known message
266 * on the server and check. If this works you know the total number
267 * of messages to get.
268 * + Otherwise run a binary search to determine the last known message
270 int ok, nolinear = 0;
271 int first_nr, list_len, try_id, try_nr, add_id;
275 if( (ok = pop3_gettopid( sock, 1, id )) != 0 )
278 if( ( first_nr = str_nr_in_list(&ctl->oldsaved, id) ) == -1 ) {
279 /* the first message is unknown -> all messages are new */
284 /* check where we expect the latest known message */
285 list_len = count_list( &ctl->oldsaved );
286 try_id = list_len - first_nr; /* -1 + 1 */
288 if( try_id <= *countp ) {
289 if( (ok = pop3_gettopid( sock, try_id, id )) != 0 )
292 try_nr = str_nr_last_in_list(&ctl->oldsaved, id);
297 if( try_nr != list_len -1 ) {
298 /* some messages inbetween have been deleted... */
302 for( add_id = 1<<30; add_id > try_id-1; add_id >>= 1 )
304 for( ; add_id; add_id >>= 1 ) {
306 if( try_id - add_id <= 1 ) {
313 if( (ok = pop3_gettopid( sock, try_id, id )) != 0 )
315 try_nr = str_nr_in_list(&ctl->oldsaved, id);
321 error(0,0,"Messages inserted into list on server. "
322 "Cannot handle this.");
327 /* The first try_id messages are known -> copy them to
329 for( num = first_nr; num < list_len; num++ )
330 save_str(&ctl->newsaved, num-first_nr + 1,
331 str_from_nr_list( &ctl->oldsaved, num ));
334 free_str_list(&ctl->oldsaved);
339 *newp = *countp - try_id;
343 static int pop3_getrange(int sock,
346 int *countp, int *newp)
347 /* get range of messages to be fetched */
350 char buf [POPBUFSIZE+1];
352 phase = PHASE_GETRANGE;
354 /* Ensure that the new list is properly empty */
355 ctl->newsaved = (struct idlist *)NULL;
358 /* Alain Knaff suggests this, but it's not RFC standard */
360 if ((ok = gen_transact(sock, "MBOX %s", folder)))
364 /* get the total message count */
365 gen_send(sock, "STAT");
366 ok = pop3_ok(sock, buf);
368 sscanf(buf,"%d %*d", countp);
373 * Newer, RFC-1725-conformant POP servers may not have the LAST command.
374 * We work as hard as possible to hide this ugliness, but it makes
375 * counting new messages intrinsically quadratic in the worst case.
379 if (*countp > 0 && !ctl->fetchall)
383 if (!ctl->server.uidl) {
384 gen_send(sock, "LAST");
385 ok = pop3_ok(sock, buf);
390 if (sscanf(buf, "%d", &last) == 0)
392 error(0, 0, "protocol error");
395 *newp = (*countp - last);
399 /* grab the mailbox's UID list */
400 if ((ok = gen_transact(sock, "UIDL")) != 0)
402 /* don't worry, yet! do it the slow way */
403 if((ok = pop3_slowuidl( sock, ctl, countp, newp))!=0)
405 error(0, 0, "protocol error while fetching UIDLs");
414 while ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
418 else if (sscanf(buf, "%d %s", &num, id) == 2)
420 save_str(&ctl->newsaved, num, id);
422 /* note: ID comparison is caseblind */
423 if (!str_in_list(&ctl->oldsaved, id))
434 static int pop3_getsizes(int sock, int count, int *sizes)
435 /* capture the sizes of all messages */
439 /* phase = PHASE_GETSIZES */
441 if ((ok = gen_transact(sock, "LIST")) != 0)
445 char buf [POPBUFSIZE+1];
447 while ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
453 else if (sscanf(buf, "%d %d", &num, &size) == 2)
454 sizes[num - 1] = size;
461 static int pop3_is_old(int sock, struct query *ctl, int num)
462 /* is the given message old? */
465 return (num <= last);
467 /* note: ID comparison is caseblind */
468 return (str_in_list(&ctl->oldsaved,
469 str_find (&ctl->newsaved, num)));
472 static int pop3_fetch(int sock, struct query *ctl, int number, int *lenp)
473 /* request nth message */
476 char buf [POPBUFSIZE+1];
478 /* phase = PHASE_FETCH */
480 gen_send(sock, "RETR %d", number);
481 if ((ok = pop3_ok(sock, buf)) != 0)
484 *lenp = -1; /* we got sizes from the LIST response */
489 static int pop3_delete(int sock, struct query *ctl, int number)
490 /* delete a given message */
492 /* actually, mark for deletion -- doesn't happen until QUIT time */
493 return(gen_transact(sock, "DELE %d", number));
496 static int pop3_logout(int sock, struct query *ctl)
497 /* send logout command */
501 /* phase = PHASE_LOGOUT */
503 ok = gen_transact(sock, "QUIT");
510 const static struct method pop3 =
512 "POP3", /* Post Office Protocol v3 */
513 110, /* standard POP3 port */
514 FALSE, /* this is not a tagged protocol */
515 TRUE, /* this uses a message delimiter */
516 pop3_ok, /* parse command response */
517 pop3_getauth, /* get authorization */
518 pop3_getrange, /* query range of messages */
519 pop3_getsizes, /* we can get a list of sizes */
520 pop3_is_old, /* how do we tell a message is old? */
521 pop3_fetch, /* request given message */
522 NULL, /* no way to fetch body alone */
523 NULL, /* no message trailer */
524 pop3_delete, /* how to delete a message */
525 pop3_logout, /* log out, we're done */
526 FALSE, /* no, we can't re-poll */
529 int doPOP3 (struct query *ctl)
530 /* retrieve messages using POP3 */
533 if (ctl->mailboxes->id) {
534 fprintf(stderr,"Option --remote is not supported with POP3\n");
538 peek_capable = FALSE;
539 return(do_protocol(ctl, &pop3));
541 #endif /* POP3_ENABLE */
543 /* pop3.c ends here */