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