]> Pileus Git - ~andy/fetchmail/blob - pop3.c
Try to fix the build code.
[~andy/fetchmail] / pop3.c
1 /*
2  * pop3.c -- POP3 protocol methods
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6
7 #include  "config.h"
8
9 #include  <stdio.h>
10 #include  <string.h>
11 #include  <ctype.h>
12 #if defined(HAVE_UNISTD_H)
13 #include <unistd.h>
14 #endif
15 #if defined(STDC_HEADERS)
16 #include  <stdlib.h>
17 #endif
18  
19 #include  "fetchmail.h"
20 #include  "socket.h"
21
22 #if HAVE_LIBOPIE
23 #include  <opie.h>
24 #endif /* HAVE_LIBOPIE */
25
26 #define PROTOCOL_ERROR  {error(0, 0, "protocol error"); return(PS_ERROR);}
27
28 #define LOCKBUSY_ERROR  {error(0, 0, "lock busy!  Is another session active?"); return(PS_LOCKBUSY);}
29
30 extern char *strstr();  /* needed on sysV68 R3V7.1. */
31
32 static int last;
33
34 #if HAVE_LIBOPIE
35 static char lastok[POPBUFSIZE+1];
36 #endif /* HAVE_LIBOPIE */
37
38 int pop3_ok (int sock, char *argbuf)
39 /* parse command response */
40 {
41     int ok;
42     char buf [POPBUFSIZE+1];
43     char *bufp;
44
45     if ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
46     {
47         bufp = buf;
48         if (*bufp == '+' || *bufp == '-')
49             bufp++;
50         else
51             return(PS_PROTOCOL);
52
53         while (isalpha(*bufp))
54             bufp++;
55         *(bufp++) = '\0';
56
57         if (strcmp(buf,"+OK") == 0)
58         {
59 #if HAVE_LIBOPIE
60             strcpy(lastok, bufp);
61 #endif /* HAVE_LIBOPIE */
62             ok = 0;
63         }
64         else if (strcmp(buf,"-ERR") == 0)
65         {
66             /*
67              * We're checking for "lock busy", "unable to lock", 
68              * "already locked" etc. here.  This indicates that we
69              * have to wait for the server to clean up before we
70              * can poll again.
71              *
72              * PS_LOCKBUSY check empirically verified with two recent
73              * versions the Berkeley popper;    QPOP (version 2.2)  and
74              * QUALCOMM Pop server derived from UCB (version 2.1.4-R3)
75              */
76             if (strstr(bufp,"lock")||strstr(bufp,"Lock")||strstr(bufp,"LOCK"))
77                 ok = PS_LOCKBUSY;
78             else
79                 ok = PS_ERROR;
80         }
81         else
82             ok = PS_PROTOCOL;
83
84         if (argbuf != NULL)
85             strcpy(argbuf,bufp);
86     }
87
88     return(ok);
89 }
90
91 int pop3_getauth(int sock, struct query *ctl, char *greeting)
92 /* apply for connection authorization */
93 {
94     int ok;
95 #if HAVE_LIBOPIE
96     char *challenge;
97 #endif /* HAVE_LIBOPIE */
98
99     /* build MD5 digest from greeting timestamp + password */
100     if (ctl->server.protocol == P_APOP) 
101     {
102         char *start,*end;
103         char *msg;
104
105         /* find start of timestamp */
106         for (start = greeting;  *start != 0 && *start != '<';  start++)
107             continue;
108         if (*start == 0) {
109             error(0, -1, "Required APOP timestamp not found in greeting");
110             return(PS_AUTHFAIL);
111         }
112
113         /* find end of timestamp */
114         for (end = start;  *end != 0  && *end != '>';  end++)
115             continue;
116         if (*end == 0 || end == start + 1) {
117             error(0, -1, "Timestamp syntax error in greeting");
118             return(PS_AUTHFAIL);
119         }
120         else
121             *++end = '\0';
122
123         /* copy timestamp and password into digestion buffer */
124         msg = (char *)xmalloc((end-start+1) + strlen(ctl->password) + 1);
125         strcpy(msg,start);
126         strcat(msg,ctl->password);
127
128         strcpy(ctl->digest, MD5Digest(msg));
129         free(msg);
130     }
131
132     switch (ctl->server.protocol) {
133     case P_POP3:
134         if ((gen_transact(sock, "USER %s", ctl->remotename)) != 0)
135             PROTOCOL_ERROR
136
137 #ifdef HAVE_LIBOPIE
138         /* see RFC1938: A One-Time Password System */
139         if (challenge = strstr(lastok, "otp-"))
140         {
141             char response[OPIE_RESPONSE_MAX+1];
142
143             /*
144              * Special case in case we're running Craig Metz's
145              * OPIE daemon.  Code in opiegenerator() will detect this.
146              */
147             if (ctl->password && !strcmp(ctl->password, "opie"))
148             {
149                 if (ok = opiegenerator(challenge, "", response))
150                     if (ok != 2)
151                         PROTOCOL_ERROR
152             }
153             else if (opiegenerator(challenge, ctl->password, response))
154                  PROTOCOL_ERROR
155
156             ok = gen_transact(sock, "PASS %s", response);
157         }
158         else
159 #else
160             /* ordinary validation, no one-time password */ 
161             ok = gen_transact(sock, "PASS %s", ctl->password);
162 #endif /* HAVE_LIBOPIE */
163
164         if (ok != 0)
165         {
166             if (ok == PS_LOCKBUSY)
167                 LOCKBUSY_ERROR
168             PROTOCOL_ERROR
169         }
170
171         /*
172          * Empirical experience shows some server/OS combinations
173          * may need a brief pause even after any lockfiles on the
174          * server are released, to give the server time to finish
175          * copying back very large mailfolders from the temp-file...
176          * this is only ever an issue with extremely large mailboxes.
177          */
178         sleep(3); /* to be _really_ safe, probably need sleep(5)! */
179         break;
180
181     case P_APOP:
182         if ((gen_transact(sock, "APOP %s %s",
183                           ctl->remotename, ctl->digest)) != 0)
184             PROTOCOL_ERROR
185         break;
186
187     case P_RPOP:
188         if ((gen_transact(sock,"USER %s", ctl->remotename)) != 0)
189             PROTOCOL_ERROR
190
191         if ((gen_transact(sock, "RPOP %s", ctl->password)) != 0)
192             PROTOCOL_ERROR
193         break;
194
195     default:
196         error(0, 0, "Undefined protocol request in POP3_auth");
197     }
198
199     /* we're approved */
200     return(0);
201 }
202
203 static int pop3_getrange(int sock, 
204                          struct query *ctl,
205                          const char *folder,
206                          int *countp, int *newp)
207 /* get range of messages to be fetched */
208 {
209     int ok;
210     char buf [POPBUFSIZE+1];
211
212     /* Ensure that the new list is properly empty */
213     ctl->newsaved = (struct idlist *)NULL;
214
215     /* get the total message count */
216     gen_send(sock, "STAT");
217     ok = pop3_ok(sock, buf);
218     if (ok == 0)
219         sscanf(buf,"%d %*d", countp);
220     else
221         return(ok);
222
223     /*
224      * Newer, RFC-1725-conformant POP servers may not have the LAST command.
225      * We work as hard as possible to hide this ugliness, but it makes 
226      * counting new messages intrinsically quadratic in the worst case.
227      */
228     last = 0;
229     *newp = -1;
230     if (*countp > 0 && !ctl->fetchall)
231     {
232         char id [IDLEN+1];
233
234         if (!ctl->server.uidl) {
235             gen_send(sock, "LAST");
236             ok = pop3_ok(sock, buf);
237         } else
238             ok = 1;
239         if (ok == 0)
240         {
241             if (sscanf(buf, "%d", &last) == 0)
242                 PROTOCOL_ERROR
243             *newp = (*countp - last);
244         }
245         else
246         {
247             /* grab the mailbox's UID list */
248             if ((ok = gen_transact(sock, "UIDL")) != 0)
249                 PROTOCOL_ERROR
250             else
251             {
252                 int     num;
253
254                 *newp = 0;
255                 while ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
256                 {
257                     if (buf[0] == '.')
258                         break;
259                     else if (sscanf(buf, "%d %s", &num, id) == 2)
260                     {
261                         save_str(&ctl->newsaved, num, id);
262
263                         /* note: ID comparison is caseblind */
264                         if (!str_in_list(&ctl->oldsaved, id))
265                             (*newp)++;
266                     }
267                 }
268             }
269         }
270     }
271
272     return(0);
273 }
274
275 static int pop3_getsizes(int sock, int count, int *sizes)
276 /* capture the sizes of all messages */
277 {
278     int ok;
279
280     if ((ok = gen_transact(sock, "LIST")) != 0)
281         return(ok);
282     else
283     {
284         char buf [POPBUFSIZE+1];
285
286         while ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
287         {
288             int num, size;
289
290             if (buf[0] == '.')
291                 break;
292             else if (sscanf(buf, "%d %d", &num, &size) == 2)
293                 sizes[num - 1] = size;
294             else
295                 sizes[num - 1] = -1;
296         }
297
298         return(ok);
299     }
300 }
301
302 static int pop3_is_old(int sock, struct query *ctl, int num)
303 /* is the given message old? */
304 {
305     if (!ctl->oldsaved)
306         return (num <= last);
307     else
308         /* note: ID comparison is caseblind */
309         return (str_in_list(&ctl->oldsaved,
310                             str_find (&ctl->newsaved, num)));
311 }
312
313 static int pop3_fetch(int sock, struct query *ctl, int number, int *lenp)
314 /* request nth message */
315 {
316     int ok;
317     char buf [POPBUFSIZE+1], *cp;
318
319     gen_send(sock, "RETR %d", number);
320     if ((ok = pop3_ok(sock, buf)) != 0)
321         return(ok);
322
323     /* 
324      * Look for "nnn octets" -- there may or may not be preceding cruft.
325      * It's OK to punt and pass back -1 as a failure indication here, as 
326      * long as the force_getsizes flag has forced sizes to be preloaded.
327      */
328     if ((cp = strstr(buf, " octets")) == (char *)NULL)
329         *lenp = -1;
330     else
331     {
332         while (--cp >= buf && isdigit(*cp))
333             continue;
334         *lenp = atoi(++cp);
335     }
336
337     return(0);
338 }
339
340 static int pop3_delete(int sock, struct query *ctl, int number)
341 /* delete a given message */
342 {
343     return(gen_transact(sock, "DELE %d", number));
344 }
345
346 const static struct method pop3 =
347 {
348     "POP3",             /* Post Office Protocol v3 */
349     110,                /* standard POP3 port */
350     FALSE,              /* this is not a tagged protocol */
351     TRUE,               /* this uses a message delimiter */
352     TRUE,               /* RFC 1725 doesn't require a size field in fetch */
353     pop3_ok,            /* parse command response */
354     pop3_getauth,       /* get authorization */
355     pop3_getrange,      /* query range of messages */
356     pop3_getsizes,      /* we can get a list of sizes */
357     pop3_is_old,        /* how do we tell a message is old? */
358     pop3_fetch,         /* request given message */
359     NULL,               /* no way to fetch body alone */
360     NULL,               /* no message trailer */
361     pop3_delete,        /* how to delete a message */
362     "QUIT",             /* the POP3 exit command */
363 };
364
365 int doPOP3 (struct query *ctl)
366 /* retrieve messages using POP3 */
367 {
368     if (ctl->mailboxes->id) {
369         fprintf(stderr,"Option --remote is not supported with POP3\n");
370         return(PS_SYNTAX);
371     }
372     peek_capable = FALSE;
373     return(do_protocol(ctl, &pop3));
374 }
375
376 /* pop3.c ends here */