]> Pileus Git - ~andy/fetchmail/blob - pop3.c
b9b675078b044edff60476068f083f68de2060b8
[~andy/fetchmail] / pop3.c
1 /*
2  * pop3.c -- POP3 protocol methods
3  *
4  * Copyright 1998 by Eric S. Raymond.
5  * For license terms, see the file COPYING in this directory.
6  */
7
8 #include  "config.h"
9 #ifdef POP3_ENABLE
10 #include  <stdio.h>
11 #include  <string.h>
12 #include  <ctype.h>
13 #if defined(HAVE_UNISTD_H)
14 #include <unistd.h>
15 #endif
16 #if defined(STDC_HEADERS)
17 #include  <stdlib.h>
18 #endif
19 #include  <errno.h>
20
21 #include  "fetchmail.h"
22 #include  "socket.h"
23 #include  "i18n.h"
24
25 #ifdef OPIE_ENABLE
26 #include <opie.h>
27 #endif /* OPIE_ENABLE */
28
29 static int last;
30 #ifdef SDPS_ENABLE
31 char *sdps_envfrom;
32 char *sdps_envto;
33 #endif /* SDPS_ENABLE */
34
35 #ifdef OPIE_ENABLE
36 static char lastok[POPBUFSIZE+1];
37 #endif /* OPIE_ENABLE */
38
39 /* these variables are shared between the CAPA probe and the authenticator */
40 #if defined(GSSAPI)
41     flag has_gssapi = FALSE;
42 #endif /* defined(GSSAPI) */
43 #if defined(KERBEROS_V4) || defined(KERBEROS_V5)
44     flag has_kerberos = FALSE;
45 #endif /* defined(KERBEROS_V4) || defined(KERBEROS_V5) */
46     static flag has_cram = FALSE;
47 #ifdef OPIE_ENABLE
48     flag has_otp = FALSE;
49 #endif /* OPIE_ENABLE */
50 #ifdef SSL_ENABLE
51     static flag has_ssl = FALSE;
52 #endif /* SSL_ENABLE */
53
54 #ifdef NTLM_ENABLE
55 #include "ntlm.h"
56
57 static tSmbNtlmAuthRequest   request;              
58 static tSmbNtlmAuthChallenge challenge;
59 static tSmbNtlmAuthResponse  response;
60
61 /*
62  * NTLM support by Grant Edwards.
63  *
64  * Handle MS-Exchange NTLM authentication method.  This is the same
65  * as the NTLM auth used by Samba for SMB related services. We just
66  * encode the packets in base64 instead of sending them out via a
67  * network interface.
68  * 
69  * Much source (ntlm.h, smb*.c smb*.h) was borrowed from Samba.
70  */
71
72 static int do_pop3_ntlm(int sock, struct query *ctl)
73 {
74     char msgbuf[2048];
75     int result,len;
76   
77     gen_send(sock, "AUTH MSN");
78
79     if ((result = gen_recv(sock, msgbuf, sizeof msgbuf)))
80         return result;
81   
82     if (msgbuf[0] != '+')
83         return PS_AUTHFAIL;
84   
85     buildSmbNtlmAuthRequest(&request,ctl->remotename,NULL);
86
87     if (outlevel >= O_DEBUG)
88         dumpSmbNtlmAuthRequest(stdout, &request);
89
90     memset(msgbuf,0,sizeof msgbuf);
91     to64frombits (msgbuf, (unsigned char*)&request, SmbLength(&request));
92   
93     if (outlevel >= O_MONITOR)
94         report(stdout, "POP3> %s\n", msgbuf);
95   
96     strcat(msgbuf,"\r\n");
97     SockWrite (sock, msgbuf, strlen (msgbuf));
98
99     if ((gen_recv(sock, msgbuf, sizeof msgbuf)))
100         return result;
101   
102     len = from64tobits ((unsigned char*)&challenge, msgbuf, sizeof(msgbuf));
103     
104     if (outlevel >= O_DEBUG)
105         dumpSmbNtlmAuthChallenge(stdout, &challenge);
106     
107     buildSmbNtlmAuthResponse(&challenge, &response,ctl->remotename,ctl->password);
108   
109     if (outlevel >= O_DEBUG)
110         dumpSmbNtlmAuthResponse(stdout, &response);
111   
112     memset(msgbuf,0,sizeof msgbuf);
113     to64frombits (msgbuf, (unsigned char*)&response, SmbLength(&response));
114
115     if (outlevel >= O_MONITOR)
116         report(stdout, "POP3> %s\n", msgbuf);
117       
118     strcat(msgbuf,"\r\n");
119     SockWrite (sock, msgbuf, strlen (msgbuf));
120   
121     if ((result = gen_recv (sock, msgbuf, sizeof msgbuf)))
122         return result;
123   
124     if (strstr (msgbuf, "OK"))
125         return PS_SUCCESS;
126     else
127         return PS_AUTHFAIL;
128 }
129 #endif /* NTLM */
130
131
132 #define DOTLINE(s)      (s[0] == '.' && (s[1]=='\r'||s[1]=='\n'||s[1]=='\0'))
133
134 static int pop3_ok (int sock, char *argbuf)
135 /* parse command response */
136 {
137     int ok;
138     char buf [POPBUFSIZE+1];
139     char *bufp;
140
141     if ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
142     {   bufp = buf;
143         if (*bufp == '+' || *bufp == '-')
144             bufp++;
145         else
146             return(PS_PROTOCOL);
147
148         while (isalpha((unsigned char)*bufp))
149             bufp++;
150
151         if (*bufp)
152           *(bufp++) = '\0';
153
154         if (strcmp(buf,"+OK") == 0)
155         {
156 #ifdef OPIE_ENABLE
157             strcpy(lastok, bufp);
158 #endif /* OPIE_ENABLE */
159             ok = 0;
160         }
161         else if (strncmp(buf,"-ERR", 4) == 0)
162         {
163             if (stage == STAGE_FETCH)
164                 ok = PS_TRANSIENT;
165             else if (stage > STAGE_GETAUTH)
166                 ok = PS_PROTOCOL;
167             /*
168              * We're checking for "lock busy", "unable to lock", 
169              * "already locked", "wait a few minutes" etc. here. 
170              * This indicates that we have to wait for the server to
171              * unwedge itself before we can poll again.
172              *
173              * PS_LOCKBUSY check empirically verified with two recent
174              * versions of the Berkeley popper; QPOP (version 2.2)  and
175              * QUALCOMM Pop server derived from UCB (version 2.1.4-R3)
176              * These are caught by the case-indifferent "lock" check.
177              * The "wait" catches "mail storage services unavailable,
178              * wait a few minutes and try again" on the InterMail server.
179              *
180              * If these aren't picked up on correctly, fetchmail will 
181              * think there is an authentication failure and wedge the
182              * connection in order to prevent futile polls.
183              *
184              * Gad, what a kluge.
185              */
186             else if (strstr(bufp,"lock")
187                      || strstr(bufp,"Lock")
188                      || strstr(bufp,"LOCK")
189                      || strstr(bufp,"wait")
190                      /* these are blessed by RFC 2449 */
191                      || strstr(bufp,"[IN-USE]")||strstr(bufp,"[LOGIN-DELAY]"))
192                 ok = PS_LOCKBUSY;
193             else if ((strstr(bufp,"Service")
194                      || strstr(bufp,"service"))
195                          && (strstr(bufp,"unavailable")))
196                 ok = PS_SERVBUSY;
197             else
198                 ok = PS_AUTHFAIL;
199             /*
200              * We always want to pass the user lock-busy messages, because
201              * they're red flags.  Other stuff (like AUTH failures on non-
202              * RFC1734 servers) only if we're debugging.
203              */
204             if (*bufp && (ok == PS_LOCKBUSY || outlevel >= O_MONITOR))
205               report(stderr, "%s\n", bufp);
206         }
207         else
208             ok = PS_PROTOCOL;
209
210 #if POPBUFSIZE > MSGBUFSIZE
211 #error "POPBUFSIZE must not be larger than MSGBUFSIZE"
212 #endif
213         if (argbuf != NULL)
214             strcpy(argbuf,bufp);
215     }
216
217     return(ok);
218 }
219
220
221
222 static int capa_probe(int sock)
223 /* probe the capabilities of the remote server */
224 {
225     int ok;
226
227 #if defined(GSSAPI)
228     has_gssapi = FALSE;
229 #endif /* defined(GSSAPI) */
230 #if defined(KERBEROS_V4) || defined(KERBEROS_V5)
231     has_kerberos = FALSE;
232 #endif /* defined(KERBEROS_V4) || defined(KERBEROS_V5) */
233     has_cram = FALSE;
234 #ifdef OPIE_ENABLE
235     has_otp = FALSE;
236 #endif /* OPIE_ENABLE */
237
238     ok = gen_transact(sock, "CAPA");
239     if (ok == PS_SUCCESS)
240     {
241         char buffer[64];
242
243         /* determine what authentication methods we have available */
244         while ((ok = gen_recv(sock, buffer, sizeof(buffer))) == 0)
245         {
246             if (DOTLINE(buffer))
247                 break;
248 #ifdef SSL_ENABLE
249             if (strstr(buffer, "STLS"))
250                 has_ssl = TRUE;
251 #endif /* SSL_ENABLE */
252 #if defined(GSSAPI)
253             if (strstr(buffer, "GSSAPI"))
254                 has_gssapi = TRUE;
255 #endif /* defined(GSSAPI) */
256 #if defined(KERBEROS_V4)
257             if (strstr(buffer, "KERBEROS_V4"))
258                 has_kerberos = TRUE;
259 #endif /* defined(KERBEROS_V4)  */
260 #ifdef OPIE_ENABLE
261             if (strstr(buffer, "X-OTP"))
262                 has_otp = TRUE;
263 #endif /* OPIE_ENABLE */
264             if (strstr(buffer, "CRAM-MD5"))
265                 has_cram = TRUE;
266         }
267     }
268     return(ok);
269 }
270
271 static void set_peek_capable(struct query *ctl)
272 {
273     /* we're peek-capable means that the use of TOP is enabled,
274      * see pop3_fetch for details - short story, we can use TOP if
275      * we have a means of reliably tracking which mail we need to
276      * refetch should the connection abort in the middle.
277      * fetchall forces RETR, as does keep without UIDL */
278     peek_capable = !ctl->fetchall && (!ctl->keep || ctl->server.uidl);
279 }
280
281 static int pop3_getauth(int sock, struct query *ctl, char *greeting)
282 /* apply for connection authorization */
283 {
284     int ok;
285     char *start,*end;
286     char *msg;
287 #ifdef OPIE_ENABLE
288     char *challenge;
289 #endif /* OPIE_ENABLE */
290 #ifdef SSL_ENABLE
291     flag did_stls = FALSE;
292 #endif /* SSL_ENABLE */
293
294 #if defined(GSSAPI)
295     has_gssapi = FALSE;
296 #endif /* defined(GSSAPI) */
297 #if defined(KERBEROS_V4) || defined(KERBEROS_V5)
298     has_kerberos = FALSE;
299 #endif /* defined(KERBEROS_V4) || defined(KERBEROS_V5) */
300     has_cram = FALSE;
301 #ifdef OPIE_ENABLE
302     has_otp = FALSE;
303 #endif /* OPIE_ENABLE */
304 #ifdef SSL_ENABLE
305     has_ssl = FALSE;
306 #endif /* SSL_ENABLE */
307
308     if (ctl->server.authenticate == A_SSH) {
309         return PS_SUCCESS;
310     }
311
312 #ifdef SDPS_ENABLE
313     /*
314      * This needs to catch both demon.co.uk and demon.net.
315      * If we see either, and we're in multidrop mode, try to use
316      * the SDPS *ENV extension.
317      */
318     if (!(ctl->server.sdps) && MULTIDROP(ctl) && strstr(greeting, "demon."))
319         ctl->server.sdps = TRUE;
320 #endif /* SDPS_ENABLE */
321 #ifdef NTLM_ENABLE
322         /* MSN servers require the use of NTLM (MSN) authentication */
323         if (!strcasecmp(ctl->server.pollname, "pop3.email.msn.com") ||
324             ctl->server.authenticate == A_NTLM)
325         {
326             if (!do_pop3_ntlm(sock, ctl))
327             {
328                 return(PS_SUCCESS);
329             }
330             else
331             {
332                 return(PS_AUTHFAIL);
333             }
334         }
335 #endif
336
337     switch (ctl->server.protocol) {
338     case P_POP3:
339 #ifdef RPA_ENABLE
340         /* CompuServe POP3 Servers as of 990730 want AUTH first for RPA */
341         if (strstr(ctl->remotename, "@compuserve.com"))
342         {
343             /* AUTH command should return a list of available mechanisms */
344             if (gen_transact(sock, "AUTH") == 0)
345             {
346                 char buffer[10];
347                 flag has_rpa = FALSE;
348
349                 while ((ok = gen_recv(sock, buffer, sizeof(buffer))) == 0)
350                 {
351                     if (DOTLINE(buffer))
352                         break;
353                     if (strncasecmp(buffer, "rpa", 3) == 0)
354                         has_rpa = TRUE;
355                 }
356                 if (has_rpa && !POP3_auth_rpa(ctl->remotename, 
357                                               ctl->password, sock))
358                     return(PS_SUCCESS);
359             }
360
361             return(PS_AUTHFAIL);
362         }
363 #endif /* RPA_ENABLE */
364
365         /*
366          * CAPA command may return a list including available
367          * authentication mechanisms.  if it doesn't, no harm done, we
368          * just fall back to a plain login.  Note that this code 
369          * latches the server's authentication type, so that in daemon mode
370          * the CAPA check only needs to be done once at start of run.
371          *
372          * If CAPA fails, then force the authentication method to PASSORD
373          * and repoll immediately.
374          *
375          * These authentication methods are blessed by RFC1734,
376          * describing the POP3 AUTHentication command.
377          */
378         if ((ctl->server.authenticate == A_ANY) ||
379             (ctl->server.authenticate == A_GSSAPI) ||
380             (ctl->server.authenticate == A_KERBEROS_V4) ||
381             (ctl->server.authenticate == A_OTP) ||
382             (ctl->server.authenticate == A_CRAM_MD5))
383         {
384             if ((ok = capa_probe(sock)) != PS_SUCCESS)
385             /* we are in STAGE_GETAUTH! */
386                 if (ok == PS_AUTHFAIL ||
387                     /* Some servers directly close the socket. However, if we
388                      * have already authenticated before, then a previous CAPA
389                      * must have succeeded. In that case, treat this as a
390                      * genuine socket error and do not change the auth method.
391                      */
392                     (ok == PS_SOCKET && !ctl->wehaveauthed))
393                 {
394                     ctl->server.authenticate = A_PASSWORD;
395                     /* repoll immediately */
396                     ok = PS_REPOLL;
397                     break;
398                 }
399         }
400
401 #ifdef SSL_ENABLE
402         if (has_ssl
403             && !ctl->use_ssl
404             && (!ctl->sslproto || !strcmp(ctl->sslproto,"tls1")))
405         {
406             char *realhost;
407
408            realhost = ctl->server.via ? ctl->server.via : ctl->server.pollname;
409            ok = gen_transact(sock, "STLS");
410
411            /* We use "tls1" instead of ctl->sslproto, as we want STLS,
412             * not other SSL protocols
413             */
414            if (ok == PS_SUCCESS &&
415                SSLOpen(sock,ctl->sslcert,ctl->sslkey,"tls1",ctl->sslcertck, ctl->sslcertpath,ctl->sslfingerprint,realhost,ctl->server.pollname) == -1)
416            {
417                if (!ctl->sslproto && !ctl->wehaveauthed)
418                {
419                    ctl->sslproto = xstrdup("");
420                    /* repoll immediately */
421                    return(PS_REPOLL);
422                }
423                report(stderr,
424                        GT_("SSL connection failed.\n"));
425                 return(PS_AUTHFAIL);
426             }
427            did_stls = TRUE;
428
429            /*
430             * RFC 2595 says this:
431             *
432             * "Once TLS has been started, the client MUST discard cached
433             * information about server capabilities and SHOULD re-issue the
434             * CAPABILITY command.  This is necessary to protect against
435             * man-in-the-middle attacks which alter the capabilities list prior
436             * to STARTTLS.  The server MAY advertise different capabilities
437             * after STARTTLS."
438             */
439            capa_probe(sock);
440         }
441 #endif /* SSL_ENABLE */
442
443         /*
444          * OK, we have an authentication type now.
445          */
446 #if defined(KERBEROS_V4)
447         /* 
448          * Servers doing KPOP have to go through a dummy login sequence
449          * rather than doing SASL.
450          */
451         if (has_kerberos &&
452 #ifdef INET6_ENABLE
453             ctl->server.service && (strcmp(ctl->server.service, KPOP_PORT)!=0)
454 #else /* INET6_ENABLE */
455             ctl->server.port != KPOP_PORT
456 #endif /* INET6_ENABLE */
457             && (ctl->server.authenticate == A_KERBEROS_V4
458              || ctl->server.authenticate == A_KERBEROS_V5
459              || ctl->server.authenticate == A_ANY))
460         {
461             ok = do_rfc1731(sock, "AUTH", ctl->server.truename);
462             if (ok == PS_SUCCESS || ctl->server.authenticate != A_ANY)
463                 break;
464         }
465 #endif /* defined(KERBEROS_V4) || defined(KERBEROS_V5) */
466
467 #if defined(GSSAPI)
468         if (has_gssapi &&
469             (ctl->server.authenticate == A_GSSAPI ||
470              ctl->server.authenticate == A_ANY))
471         {
472             ok = do_gssauth(sock,"AUTH","pop",ctl->server.truename,ctl->remotename);
473             if (ok == PS_SUCCESS || ctl->server.authenticate != A_ANY)
474                 break;
475         }
476 #endif /* defined(GSSAPI) */
477
478 #ifdef OPIE_ENABLE
479         if (has_otp &&
480             (ctl->server.authenticate == A_OTP ||
481              ctl->server.authenticate == A_ANY))
482         {
483             ok = do_otp(sock, "AUTH", ctl);
484             if (ok == PS_SUCCESS || ctl->server.authenticate != A_ANY)
485                 break;
486         }
487 #endif /* OPIE_ENABLE */
488
489         if (ctl->server.authenticate == A_CRAM_MD5 || 
490             (has_cram && ctl->server.authenticate == A_ANY))
491         {
492             ok = do_cram_md5(sock, "AUTH", ctl, NULL);
493             if (ok == PS_SUCCESS || ctl->server.authenticate != A_ANY)
494                 break;
495         }
496
497         /* ordinary validation, no one-time password or RPA */ 
498         if ((ok = gen_transact(sock, "USER %s", ctl->remotename)))
499             break;
500
501 #ifdef OPIE_ENABLE
502         /* see RFC1938: A One-Time Password System */
503         if ((challenge = strstr(lastok, "otp-"))) {
504           char response[OPIE_RESPONSE_MAX+1];
505           int i;
506
507           i = opiegenerator(challenge, !strcmp(ctl->password, "opie") ? "" : ctl->password, response);
508           if ((i == -2) && !run.poll_interval) {
509             char secret[OPIE_SECRET_MAX+1];
510             fprintf(stderr, GT_("Secret pass phrase: "));
511             if (opiereadpass(secret, sizeof(secret), 0))
512               i = opiegenerator(challenge,  secret, response);
513             memset(secret, 0, sizeof(secret));
514           };
515
516           if (i) {
517             ok = PS_ERROR;
518             break;
519           };
520
521           ok = gen_transact(sock, "PASS %s", response);
522           break;
523         }
524 #endif /* OPIE_ENABLE */
525
526         strcpy(shroud, ctl->password);
527         ok = gen_transact(sock, "PASS %s", ctl->password);
528         shroud[0] = '\0';
529 #ifdef SSL_ENABLE
530         /* this is for servers which claim to support TLS, but actually
531          * don't! */
532         if (did_stls && ok == PS_SOCKET && !ctl->sslproto && !ctl->wehaveauthed)
533         {
534             ctl->sslproto = xstrdup("");
535             /* repoll immediately */
536             ok = PS_REPOLL;
537         }
538 #endif
539         break;
540
541     case P_APOP:
542         /* build MD5 digest from greeting timestamp + password */
543         /* find start of timestamp */
544         for (start = greeting;  *start != 0 && *start != '<';  start++)
545             continue;
546         if (*start == 0) {
547             report(stderr,
548                    GT_("Required APOP timestamp not found in greeting\n"));
549             return(PS_AUTHFAIL);
550         }
551
552         /* find end of timestamp */
553         for (end = start;  *end != 0  && *end != '>';  end++)
554             continue;
555         if (*end == 0 || end == start + 1) {
556             report(stderr, 
557                    GT_("Timestamp syntax error in greeting\n"));
558             return(PS_AUTHFAIL);
559         }
560         else
561             *++end = '\0';
562
563         /* copy timestamp and password into digestion buffer */
564         xalloca(msg, char *, (end-start+1) + strlen(ctl->password) + 1);
565         strcpy(msg,start);
566         strcat(msg,ctl->password);
567
568         strcpy(ctl->digest, MD5Digest((unsigned char *)msg));
569
570         ok = gen_transact(sock, "APOP %s %s", ctl->remotename, ctl->digest);
571         break;
572
573     case P_RPOP:
574         if ((ok = gen_transact(sock,"USER %s", ctl->remotename)) == 0)
575             ok = gen_transact(sock, "RPOP %s", ctl->password);
576         break;
577
578     default:
579         report(stderr, GT_("Undefined protocol request in POP3_auth\n"));
580         ok = PS_ERROR;
581     }
582
583     if (ok != 0)
584     {
585         /* maybe we detected a lock-busy condition? */
586         if (ok == PS_LOCKBUSY)
587             report(stderr, GT_("lock busy!  Is another session active?\n")); 
588
589         return(ok);
590     }
591
592 /* Disable the sleep. Based on patch by Brian Candler 2004-04-19/2004-11-08,
593  * accepted by Matthias Andree.
594  *
595  * Rationale: the server must have locked the spool before returning +OK;
596  * this sleep just wastes time and hence, for modem and GSM CSD users, money. */
597 #ifdef WANT_BOGUS
598     /*
599      * Empirical experience shows some server/OS combinations
600      * may need a brief pause even after any lockfiles on the
601      * server are released, to give the server time to finish
602      * copying back very large mailfolders from the temp-file...
603      * this is only ever an issue with extremely large mailboxes.
604      */
605     sleep(3); /* to be _really_ safe, probably need sleep(5)! */
606 #endif
607
608     set_peek_capable(ctl);
609
610     /* we're approved */
611     return(PS_SUCCESS);
612 }
613
614 /* cut off C string at first POSIX space */
615 static void trim(char *s) {
616     s += strcspn(s, POSIX_space);
617     s[0] = '\0';
618 }
619
620 static int pop3_gettopid(int sock, int num , char *id, size_t idsize)
621 {
622     int ok;
623     int got_it;
624     char buf [POPBUFSIZE+1];
625     snprintf(buf, sizeof(buf), "TOP %d 1", num);
626     if ((ok = gen_transact(sock, buf )) != 0)
627        return ok;
628     got_it = 0;
629     while ((ok = gen_recv(sock, buf, sizeof(buf))) == 0) 
630     {
631         if (DOTLINE(buf))
632             break;
633         if (!got_it && 0 == strncasecmp("Message-Id:", buf, 11)) {
634             char *p = buf + 11;
635             got_it = 1;
636             p += strspn(p, POSIX_space);
637             strlcpy(id, p, idsize);
638             trim(id);
639         }
640     }
641     return 0;
642 }
643
644 /** Parse the UID response (leading +OK must have been
645  * stripped off) in buf, store the number in gotnum, and store the ID
646  * into the caller-provided buffer "id" of size "idsize".
647  * Returns PS_SUCCESS or PS_PROTOCOL for failure. */
648 static int parseuid(const char *buf, unsigned long *gotnum, char *id, size_t idsize)
649 {
650     const char *i;
651     char *j;
652
653     /* skip leading blanks ourselves */
654     i = buf;
655     i += strspn(i, POSIX_space);
656     errno = 0;
657     *gotnum = strtoul(i, &j, 10);
658     if (j == i || !*j || errno || NULL == strchr(POSIX_space, *j)) {
659         report(stderr, GT_("Cannot handle UIDL response from upstream server.\n"));
660         return PS_PROTOCOL;
661     }
662     j += strspn(j, POSIX_space);
663     strlcpy(id, j, idsize);
664     trim(id);
665     return PS_SUCCESS;
666 }
667
668 static int pop3_getuidl(int sock, int num , char *id, size_t idsize)
669 {
670     int ok;
671     char buf [POPBUFSIZE+1];
672     unsigned long gotnum;
673
674     gen_send(sock, "UIDL %d", num);
675     if ((ok = pop3_ok(sock, buf)) != 0)
676         return(ok);
677     if ((ok = parseuid(buf, &gotnum, id, idsize)))
678         return ok;
679     if (gotnum != num) {
680         report(stderr, GT_("Server responded with UID for wrong message.\n"));
681         return PS_PROTOCOL;
682     }
683     return(PS_SUCCESS);
684 }
685
686 static int pop3_fastuidl( int sock,  struct query *ctl, unsigned int count, int *newp)
687 {
688     int ok;
689     unsigned int first_nr, last_nr, try_nr;
690     char id [IDLEN+1];
691
692     first_nr = 0;
693     last_nr = count + 1;
694     while (first_nr < last_nr - 1)
695     {
696         struct idlist   *new;
697
698         try_nr = (first_nr + last_nr) / 2;
699         if ((ok = pop3_getuidl(sock, try_nr, id, sizeof(id))) != 0)
700             return ok;
701         if ((new = str_in_list(&ctl->oldsaved, id, FALSE)))
702         {
703             flag mark = new->val.status.mark;
704             if (mark == UID_DELETED || mark == UID_EXPUNGED)
705             {
706                 if (outlevel >= O_VERBOSE)
707                     report(stderr, GT_("id=%s (num=%d) was deleted, but is still present!\n"), id, try_nr);
708                 /* just mark it as seen now! */
709                 new->val.status.mark = mark = UID_SEEN;
710             }
711
712             /* narrow the search region! */
713             if (mark == UID_UNSEEN)
714             {
715                 if (outlevel >= O_DEBUG)
716                     report(stdout, GT_("%u is unseen\n"), try_nr);
717                 last_nr = try_nr;
718             }
719             else
720                 first_nr = try_nr;
721
722             /* save the number */
723             new->val.status.num = try_nr;
724         }
725         else
726         {
727             if (outlevel >= O_DEBUG)
728                 report(stdout, GT_("%u is unseen\n"), try_nr);
729             last_nr = try_nr;
730
731             /* save it */
732             new = save_str(&ctl->oldsaved, id, UID_UNSEEN);
733             new->val.status.num = try_nr;
734         }
735     }
736     if (outlevel >= O_DEBUG && last_nr <= count)
737         report(stdout, GT_("%u is first unseen\n"), last_nr);
738
739     /* update last! */
740     *newp = count - first_nr;
741     last = first_nr;
742     return 0;
743 }
744
745 static int pop3_slowuidl( int sock,  struct query *ctl, int *countp, int *newp)
746 {
747     /* This approach tries to get the message headers from the
748      * remote hosts and compares the message-id to the already known
749      * ones:
750      *  + if the first message containes a new id, all messages on
751      *    the server will be new
752      *  + if the first is known, try to estimate the last known message
753      *    on the server and check. If this works you know the total number
754      *    of messages to get.
755      *  + Otherwise run a binary search to determine the last known message
756      */
757     int ok, nolinear = 0;
758     int first_nr, list_len, try_id, try_nr, add_id;
759     int num;
760     char id [IDLEN+1];
761
762     if ((ok = pop3_gettopid(sock, 1, id, sizeof(id))) != 0)
763         return ok;
764
765     if( ( first_nr = str_nr_in_list(&ctl->oldsaved, id) ) == -1 ) {
766         /* the first message is unknown -> all messages are new */
767         *newp = *countp;        
768         return 0;
769     }
770
771     /* check where we expect the latest known message */
772     list_len = count_list( &ctl->oldsaved );
773     try_id = list_len  - first_nr; /* -1 + 1 */
774     if( try_id > 1 ) {
775         if( try_id <= *countp ) {
776             if ((ok = pop3_gettopid(sock, try_id, id, sizeof(id))) != 0)
777                 return ok;
778     
779             try_nr = str_nr_last_in_list(&ctl->oldsaved, id);
780         } else {
781             try_id = *countp+1;
782             try_nr = -1;
783         }
784         if( try_nr != list_len -1 ) {
785             /* some messages inbetween have been deleted... */
786             if( try_nr == -1 ) {
787                 nolinear = 1;
788
789                 for( add_id = 1<<30; add_id > try_id-1; add_id >>= 1 )
790                     ;
791                 for( ; add_id; add_id >>= 1 ) {
792                     if( try_nr == -1 ) {
793                         if( try_id - add_id <= 1 ) {
794                             continue;
795                         }
796                         try_id -= add_id;
797                     } else 
798                         try_id += add_id;
799                     
800                     if ((ok = pop3_gettopid(sock, try_id, id, sizeof(id))) != 0)
801                         return ok;
802                     try_nr = str_nr_in_list(&ctl->oldsaved, id);
803                 }
804                 if( try_nr == -1 ) {
805                     try_id--;
806                 }
807             } else {
808                 report(stderr, 
809                        GT_("Messages inserted into list on server. Cannot handle this.\n"));
810                 return -1;
811             }
812         } 
813     }
814     /* the first try_id messages are known -> copy them to the newsaved list */
815     for( num = first_nr; num < list_len; num++ )
816     {
817         struct idlist   *new = save_str(&ctl->newsaved, 
818                                 str_from_nr_list(&ctl->oldsaved, num),
819                                 UID_UNSEEN);
820         new->val.status.num = num - first_nr + 1;
821     }
822
823     if( nolinear ) {
824         free_str_list(&ctl->oldsaved);
825         ctl->oldsaved = 0;
826         last = try_id;
827     }
828
829     *newp = *countp - try_id;
830     return 0;
831 }
832
833 static int pop3_getrange(int sock, 
834                          struct query *ctl,
835                          const char *folder,
836                          int *countp, int *newp, int *bytes)
837 /* get range of messages to be fetched */
838 {
839     int ok;
840     char buf [POPBUFSIZE+1];
841
842     /* Ensure that the new list is properly empty */
843     ctl->newsaved = (struct idlist *)NULL;
844
845 #ifdef MBOX
846     /* Alain Knaff suggests this, but it's not RFC standard */
847     if (folder)
848         if ((ok = gen_transact(sock, "MBOX %s", folder)))
849             return ok;
850 #endif /* MBOX */
851
852     /* get the total message count */
853     gen_send(sock, "STAT");
854     ok = pop3_ok(sock, buf);
855     if (ok == 0)
856         sscanf(buf,"%d %d", countp, bytes);
857     else
858         return(ok);
859
860     /*
861      * Newer, RFC-1725-conformant POP servers may not have the LAST command.
862      * We work as hard as possible to hide this ugliness, but it makes
863      * counting new messages intrinsically quadratic in the worst case.
864      */
865     last = 0;
866     *newp = -1;
867     if (*countp > 0 && !ctl->fetchall)
868     {
869         int fastuidl;
870         char id [IDLEN+1];
871
872         /* should we do fast uidl this time? */
873         fastuidl = ctl->fastuidl;
874         if (*countp > 7 &&              /* linear search is better if there are few mails! */
875             !ctl->flush &&              /* with flush, it is safer to disable fastuidl */
876             NUM_NONZERO (fastuidl))
877         {
878             if (fastuidl == 1)
879                 dofastuidl = 1;
880             else
881                 dofastuidl = ctl->fastuidlcount != 0;
882         }
883         else
884             dofastuidl = 0;
885
886         if (!ctl->server.uidl) {
887             gen_send(sock, "LAST");
888             ok = pop3_ok(sock, buf);
889         } else
890             ok = 1;
891         if (ok == 0)
892         {
893             if (sscanf(buf, "%d", &last) == 0)
894             {
895                 report(stderr, GT_("protocol error\n"));
896                 return(PS_ERROR);
897             }
898             *newp = (*countp - last);
899         }
900         else
901         {
902             if (dofastuidl)
903                 return(pop3_fastuidl( sock, ctl, *countp, newp));
904             /* grab the mailbox's UID list */
905             if ((ok = gen_transact(sock, "UIDL")) != 0)
906             {
907                 /* don't worry, yet! do it the slow way */
908                 if ((ok = pop3_slowuidl(sock, ctl, countp, newp)))
909                 {
910                     report(stderr, GT_("protocol error while fetching UIDLs\n"));
911                     return(PS_ERROR);
912                 }
913             }
914             else
915             {
916                 unsigned long unum;
917
918                 *newp = 0;
919                 while ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
920                 {
921                     if (DOTLINE(buf))
922                         break;
923
924                     if (parseuid(buf, &unum, id, sizeof(id)) == PS_SUCCESS)
925                     {
926                         struct idlist   *old, *new;
927
928                         new = save_str(&ctl->newsaved, id, UID_UNSEEN);
929                         new->val.status.num = unum;
930
931                         if ((old = str_in_list(&ctl->oldsaved, id, FALSE)))
932                         {
933                             flag mark = old->val.status.mark;
934                             if (mark == UID_DELETED || mark == UID_EXPUNGED)
935                             {
936                                 /* XXX FIXME: switch 3 occurrences from
937                                  * (int)unum or (unsigned int)unum to
938                                  * remove the cast and use %lu - not now
939                                  * though, time for new release */
940                                 if (outlevel >= O_VERBOSE)
941                                     report(stderr, GT_("id=%s (num=%d) was deleted, but is still present!\n"), id, (int)unum);
942                                 /* just mark it as seen now! */
943                                 old->val.status.mark = mark = UID_SEEN;
944                             }
945                             new->val.status.mark = mark;
946                             if (mark == UID_UNSEEN)
947                             {
948                                 (*newp)++;
949                                 if (outlevel >= O_DEBUG)
950                                     report(stdout, GT_("%u is unseen\n"), (unsigned int)unum);
951                             }
952                         }
953                         else
954                         {
955                             (*newp)++;
956                             if (outlevel >= O_DEBUG)
957                                 report(stdout, GT_("%u is unseen\n"), (unsigned int)unum);
958                             /* add it to oldsaved also! In case, we do not
959                              * swap the lists (say, due to socket error),
960                              * the same mail will not be downloaded again.
961                              */
962                             old = save_str(&ctl->oldsaved, id, UID_UNSEEN);
963                             old->val.status.num = unum;
964                         }
965                     }
966                 }
967             }
968         }
969     }
970
971     return(PS_SUCCESS);
972 }
973
974 static int pop3_getpartialsizes(int sock, int first, int last, int *sizes)
975 /* capture the size of message #first */
976 {
977     int ok = 0, i;
978     char buf [POPBUFSIZE+1];
979     unsigned int num, size;
980
981     for (i = first; i <= last; i++) {
982         gen_send(sock, "LIST %d", i);
983         if ((ok = pop3_ok(sock, buf)) != 0)
984             return(ok);
985         if (sscanf(buf, "%u %u", &num, &size) == 2) {
986             if (num == i)
987                 sizes[i - first] = size;
988             else
989                 /* warn about possible attempt to induce buffer overrun
990                  *
991                  * we expect server reply message number and requested
992                  * message number to match */
993                 report(stderr, "Warning: ignoring bogus data for message sizes returned by server.\n");
994         }
995     }
996     return(ok);
997 }
998
999 static int pop3_getsizes(int sock, int count, int *sizes)
1000 /* capture the sizes of all messages */
1001 {
1002     int ok;
1003
1004     if ((ok = gen_transact(sock, "LIST")) != 0)
1005         return(ok);
1006     else
1007     {
1008         char buf [POPBUFSIZE+1];
1009
1010         while ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
1011         {
1012             unsigned int num, size;
1013
1014             if (DOTLINE(buf))
1015                 break;
1016             else if (sscanf(buf, "%u %u", &num, &size) == 2) {
1017                 if (num > 0 && num <= count)
1018                     sizes[num - 1] = size;
1019                 else
1020                     /* warn about possible attempt to induce buffer overrun */
1021                     report(stderr, "Warning: ignoring bogus data for message sizes returned by server.\n");
1022             }
1023         }
1024
1025         return(ok);
1026     }
1027 }
1028
1029 static int pop3_is_old(int sock, struct query *ctl, int num)
1030 /* is the given message old? */
1031 {
1032     struct idlist *new;
1033     if (!ctl->oldsaved)
1034         return (num <= last);
1035     else if (dofastuidl)
1036     {
1037         char id [IDLEN+1];
1038
1039         if (num <= last)
1040             return(TRUE);
1041
1042         /* in fast uidl, we manipulate the old list only! */
1043
1044         if ((new = id_find(&ctl->oldsaved, num)))
1045         {
1046             /* we already have the id! */
1047             return(new->val.status.mark != UID_UNSEEN);
1048         }
1049
1050         /* get the uidl first! */
1051         if (pop3_getuidl(sock, num, id, sizeof(id)) != PS_SUCCESS)
1052             return(TRUE);
1053
1054         if ((new = str_in_list(&ctl->oldsaved, id, FALSE))) {
1055             /* we already have the id! */
1056             new->val.status.num = num;
1057             return(new->val.status.mark != UID_UNSEEN);
1058         }
1059
1060         /* save it */
1061         new = save_str(&ctl->oldsaved, id, UID_UNSEEN);
1062         new->val.status.num = num;
1063         return(FALSE);
1064     }
1065     else
1066         return ((new = id_find(&ctl->newsaved, num)) != NULL &&
1067             new->val.status.mark != UID_UNSEEN);
1068 }
1069
1070 #ifdef UNUSED
1071 /*
1072  * We could use this to fetch headers only as we do for IMAP.  The trouble 
1073  * is that there's no way to fetch the body only.  So the following RETR 
1074  * would have to re-fetch the header.  Enough messages have longer headers
1075  * than bodies to make this a net loss.
1076  */
1077 static int pop_fetch_headers(int sock, struct query *ctl,int number,int *lenp)
1078 /* request headers of nth message */
1079 {
1080     int ok;
1081     char buf[POPBUFSIZE+1];
1082
1083     gen_send(sock, "TOP %d 0", number);
1084     if ((ok = pop3_ok(sock, buf)) != 0)
1085         return(ok);
1086
1087     *lenp = -1;         /* we got sizes from the LIST response */
1088
1089     return(PS_SUCCESS);
1090 }
1091 #endif /* UNUSED */
1092
1093 static int pop3_fetch(int sock, struct query *ctl, int number, int *lenp)
1094 /* request nth message */
1095 {
1096     int ok;
1097     char buf[POPBUFSIZE+1];
1098
1099 #ifdef SDPS_ENABLE
1100     /*
1101      * See http://www.demon.net/services/mail/sdps-tech.html
1102      * for a description of what we're parsing here.
1103      */
1104     if (ctl->server.sdps)
1105     {
1106         int     linecount = 0;
1107
1108         sdps_envfrom = (char *)NULL;
1109         sdps_envto = (char *)NULL;
1110         gen_send(sock, "*ENV %d", number);
1111         do {
1112             if (gen_recv(sock, buf, sizeof(buf)))
1113             {
1114                 break;
1115             }
1116             linecount++;
1117             switch (linecount) {
1118             case 4:
1119                 /* No need to wrap envelope from address */
1120                 sdps_envfrom = xmalloc(strlen(buf)+1);
1121                 strcpy(sdps_envfrom,buf);
1122                 break;
1123             case 5:
1124                 /* Wrap address with To: <> so nxtaddr() likes it */
1125                 sdps_envto = xmalloc(strlen(buf)+7);
1126                 sprintf(sdps_envto,"To: <%s>",buf);
1127                 break;
1128             }
1129         } while
1130             (!(buf[0] == '.' && (buf[1] == '\r' || buf[1] == '\n' || buf[1] == '\0')));
1131     }
1132 #endif /* SDPS_ENABLE */
1133
1134     /*
1135      * Though the POP RFCs don't document this fact, on almost every
1136      * POP3 server I know of messages are marked "seen" only at the
1137      * time the OK response to a RETR is issued.
1138      *
1139      * This means we can use TOP to fetch the message without setting its
1140      * seen flag.  This is good!  It means that if the protocol exchange
1141      * craps out during the message, it will still be marked `unseen' on
1142      * the server.  (Exception: in early 1999 SpryNet's POP3 servers were
1143      * reported to mark messages seen on a TOP fetch.)
1144      *
1145      * However...*don't* do this if we're using keep to suppress deletion!
1146      * In that case, marking the seen flag is the only way to prevent the
1147      * message from being re-fetched on subsequent runs.
1148      *
1149      * Also use RETR (that means no TOP, no peek) if fetchall is on.
1150      * This gives us a workaround for servers like usa.net's that bungle
1151      * TOP.  It's pretty harmless because fetchall guarantees that any
1152      * message dropped by an interrupted RETR will be picked up on the
1153      * next poll of the site.
1154      *
1155      * We take advantage here of the fact that, according to all the
1156      * POP RFCs, "if the number of lines requested by the POP3 client
1157      * is greater than than the number of lines in the body, then the
1158      * POP3 server sends the entire message.").
1159      *
1160      * The line count passed (99999999) is the maximum value CompuServe will
1161      * accept; it's much lower than the natural value 2147483646 (the maximum
1162      * twos-complement signed 32-bit integer minus 1) */
1163     if (!peek_capable)
1164         gen_send(sock, "RETR %d", number);
1165     else
1166         gen_send(sock, "TOP %d 99999999", number);
1167     if ((ok = pop3_ok(sock, buf)) != 0)
1168         return(ok);
1169
1170     *lenp = -1;         /* we got sizes from the LIST response */
1171
1172     return(PS_SUCCESS);
1173 }
1174
1175 static void mark_uid_seen(struct query *ctl, int number)
1176 /* Tell the UID code we've seen this. */
1177 {
1178     struct idlist       *sdp;
1179
1180     if ((sdp = id_find(&ctl->newsaved, number)))
1181         sdp->val.status.mark = UID_SEEN;
1182     /* mark it as seen in oldsaved also! In case, we do not swap the lists
1183      * (say, due to socket error), the same mail will not be downloaded
1184      * again.
1185      */
1186     if ((sdp = id_find(&ctl->oldsaved, number)))
1187         sdp->val.status.mark = UID_SEEN;
1188 }
1189
1190 static int pop3_delete(int sock, struct query *ctl, int number)
1191 /* delete a given message */
1192 {
1193     int ok;
1194     mark_uid_seen(ctl, number);
1195     /* actually, mark for deletion -- doesn't happen until QUIT time */
1196     ok = gen_transact(sock, "DELE %d", number);
1197     if (ok != PS_SUCCESS)
1198         return(ok);
1199     delete_str(dofastuidl ? &ctl->oldsaved : &ctl->newsaved, number);
1200     return(PS_SUCCESS);
1201 }
1202
1203 static int pop3_mark_seen(int sock, struct query *ctl, int number)
1204 /* mark a given message as seen */
1205 {
1206     mark_uid_seen(ctl, number);
1207     return(PS_SUCCESS);
1208 }
1209
1210 static int pop3_logout(int sock, struct query *ctl)
1211 /* send logout command */
1212 {
1213     int ok;
1214
1215 #ifdef __UNUSED__
1216     /*
1217      * We used to do this in case the server marks messages deleted when seen.
1218      * (Yes, this has been reported, in the MercuryP/NLM server.
1219      * It's even legal under RFC 1939 (section 8) as a site policy.)
1220      * It interacted badly with UIDL, though.  Thomas Zajic wrote:
1221      * "Running 'fetchmail -F -v' and checking the logs, I found out
1222      * that fetchmail did in fact flush my mailbox properly, but sent
1223      * a RSET just before sending QUIT to log off.  This caused the
1224      * POP3 server to undo/forget about the previous DELEs, resetting
1225      * my mailbox to its original (ie.  unflushed) state. The
1226      * ~/.fetchids file did get flushed though, so the next time
1227      * fetchmail was run it saw all the old messages as new ones ..."
1228      */
1229      if (ctl->keep)
1230         gen_transact(sock, "RSET");
1231 #endif /* __UNUSED__ */
1232
1233     ok = gen_transact(sock, "QUIT");
1234     if (!ok)
1235         expunge_uids(ctl);
1236
1237     return(ok);
1238 }
1239
1240 static const struct method pop3 =
1241 {
1242     "POP3",             /* Post Office Protocol v3 */
1243 #ifdef INET6_ENABLE
1244     "pop3",             /* standard POP3 port */
1245     "pop3s",            /* ssl POP3 port */
1246 #else /* INET6_ENABLE */
1247     110,                /* standard POP3 port */
1248     995,                /* ssl POP3 port */
1249 #endif /* INET6_ENABLE */
1250     FALSE,              /* this is not a tagged protocol */
1251     TRUE,               /* this uses a message delimiter */
1252     pop3_ok,            /* parse command response */
1253     pop3_getauth,       /* get authorization */
1254     pop3_getrange,      /* query range of messages */
1255     pop3_getsizes,      /* we can get a list of sizes */
1256     pop3_getpartialsizes,       /* we can get the size of 1 mail */
1257     pop3_is_old,        /* how do we tell a message is old? */
1258     pop3_fetch,         /* request given message */
1259     NULL,               /* no way to fetch body alone */
1260     NULL,               /* no message trailer */
1261     pop3_delete,        /* how to delete a message */
1262     pop3_mark_seen,     /* how to mark a message as seen */
1263     pop3_logout,        /* log out, we're done */
1264     FALSE,              /* no, we can't re-poll */
1265 };
1266
1267 int doPOP3 (struct query *ctl)
1268 /* retrieve messages using POP3 */
1269 {
1270 #ifndef MBOX
1271     if (ctl->mailboxes->id) {
1272         fprintf(stderr,GT_("Option --remote is not supported with POP3\n"));
1273         return(PS_SYNTAX);
1274     }
1275 #endif /* MBOX */
1276     set_peek_capable(ctl); /* XXX FIXME: is this needed or do we always
1277                               call this from pop3_getauth anyways? */
1278     return(do_protocol(ctl, &pop3));
1279 }
1280 #endif /* POP3_ENABLE */
1281
1282 /* pop3.c ends here */