* Improve code that determines the type of encryption that is used according
[alpine.git] / imap / src / c-client / pop3.c
blob7ca07da896268e2ec4ee750e75e22e1e0543c5a9
1 /* ========================================================================
2 * Copyright 2008-2011 Mark Crispin
3 * ========================================================================
4 */
6 /*
7 * Program: Post Office Protocol 3 (POP3) client routines
9 * Author: Mark Crispin
11 * Date: 6 June 1994
12 * Last Edited: 8 April 2011
14 * Previous versions of this file were:
16 * Copyright 1988-2007 University of Washington
18 * Licensed under the Apache License, Version 2.0 (the "License");
19 * you may not use this file except in compliance with the License.
20 * You may obtain a copy of the License at
22 * http://www.apache.org/licenses/LICENSE-2.0
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <time.h>
30 #include "c-client.h"
31 #include "flstring.h"
32 #include "netmsg.h"
34 /* Parameters */
36 #define POP3TCPPORT (long) 110 /* assigned TCP contact port */
37 #define POP3SSLPORT (long) 995 /* assigned SSL TCP contact port */
38 #define IDLETIMEOUT (long) 10 /* defined in RFC 1939 */
41 /* POP3 I/O stream local data */
43 typedef struct pop3_local {
44 NETSTREAM *netstream; /* TCP I/O stream */
45 char *response; /* last server reply */
46 char *reply; /* text of last server reply */
47 unsigned long cached; /* current cached message uid */
48 unsigned long hdrsize; /* current cached header size */
49 FILE *txt; /* current cached file descriptor */
50 struct {
51 unsigned int capa : 1; /* server has CAPA, definitely new */
52 unsigned int expire : 1; /* server has EXPIRE */
53 unsigned int logindelay : 1;/* server has LOGIN-DELAY */
54 unsigned int stls : 1; /* server has STLS */
55 unsigned int pipelining : 1;/* server has PIPELINING */
56 unsigned int respcodes : 1; /* server has RESP-CODES */
57 unsigned int top : 1; /* server has TOP */
58 unsigned int uidl : 1; /* server has UIDL */
59 unsigned int user : 1; /* server has USER */
60 char *implementation; /* server implementation string */
61 long delaysecs; /* minimum time between login (neg variable) */
62 long expiredays; /* server-guaranteed minimum retention days */
63 /* supported authenticators */
64 unsigned int sasl : MAXAUTHENTICATORS;
65 } cap;
66 unsigned int sensitive : 1; /* sensitive data in progress */
67 unsigned int loser : 1; /* server is a loser */
68 unsigned int saslcancel : 1; /* SASL cancelled by protocol */
69 } POP3LOCAL;
72 /* Convenient access to local data */
74 #define LOCAL ((POP3LOCAL *) stream->local)
76 /* Function prototypes */
78 DRIVER *pop3_valid (char *name);
79 void *pop3_parameters (long function,void *value);
80 void pop3_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents);
81 void pop3_list (MAILSTREAM *stream,char *ref,char *pat);
82 void pop3_lsub (MAILSTREAM *stream,char *ref,char *pat);
83 long pop3_subscribe (MAILSTREAM *stream,char *mailbox);
84 long pop3_unsubscribe (MAILSTREAM *stream,char *mailbox);
85 long pop3_create (MAILSTREAM *stream,char *mailbox);
86 long pop3_delete (MAILSTREAM *stream,char *mailbox);
87 long pop3_rename (MAILSTREAM *stream,char *old,char *newname);
88 long pop3_status (MAILSTREAM *stream,char *mbx,long flags);
89 MAILSTREAM *pop3_open (MAILSTREAM *stream);
90 long pop3_capa (MAILSTREAM *stream,long flags);
91 long pop3_auth (MAILSTREAM *stream,NETMBX *mb,char *pwd,char *usr);
92 void *pop3_challenge (void *stream,unsigned long *len);
93 long pop3_response (void *stream,char *s,unsigned long size);
94 void pop3_close (MAILSTREAM *stream,long options);
95 void pop3_fetchfast (MAILSTREAM *stream,char *sequence,long flags);
96 char *pop3_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *size,
97 long flags);
98 long pop3_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags);
99 unsigned long pop3_cache (MAILSTREAM *stream,MESSAGECACHE *elt);
100 long pop3_ping (MAILSTREAM *stream);
101 void pop3_check (MAILSTREAM *stream);
102 long pop3_expunge (MAILSTREAM *stream,char *sequence,long options);
103 long pop3_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options);
104 long pop3_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data);
106 long pop3_send_num (MAILSTREAM *stream,char *command,unsigned long n);
107 long pop3_send (MAILSTREAM *stream,char *command,char *args);
108 long pop3_reply (MAILSTREAM *stream);
109 long pop3_fake (MAILSTREAM *stream,char *text);
111 /* POP3 mail routines */
114 /* Driver dispatch used by MAIL */
116 DRIVER pop3driver = {
117 "pop3", /* driver name */
118 /* driver flags */
119 #ifdef INADEQUATE_MEMORY
120 DR_LOWMEM |
121 #endif
122 DR_MAIL|DR_NOFAST|DR_CRLF|DR_NOSTICKY|DR_NOINTDATE|DR_NONEWMAIL,
123 (DRIVER *) NIL, /* next driver */
124 pop3_valid, /* mailbox is valid for us */
125 pop3_parameters, /* manipulate parameters */
126 pop3_scan, /* scan mailboxes */
127 pop3_list, /* find mailboxes */
128 pop3_lsub, /* find subscribed mailboxes */
129 pop3_subscribe, /* subscribe to mailbox */
130 pop3_unsubscribe, /* unsubscribe from mailbox */
131 pop3_create, /* create mailbox */
132 pop3_delete, /* delete mailbox */
133 pop3_rename, /* rename mailbox */
134 pop3_status, /* status of mailbox */
135 pop3_open, /* open mailbox */
136 pop3_close, /* close mailbox */
137 pop3_fetchfast, /* fetch message "fast" attributes */
138 NIL, /* fetch message flags */
139 NIL, /* fetch overview */
140 NIL, /* fetch message structure */
141 pop3_header, /* fetch message header */
142 pop3_text, /* fetch message text */
143 NIL, /* fetch message */
144 NIL, /* unique identifier */
145 NIL, /* message number from UID */
146 NIL, /* modify flags */
147 NIL, /* per-message modify flags */
148 NIL, /* search for message based on criteria */
149 NIL, /* sort messages */
150 NIL, /* thread messages */
151 pop3_ping, /* ping mailbox to see if still alive */
152 pop3_check, /* check for new messages */
153 pop3_expunge, /* expunge deleted messages */
154 pop3_copy, /* copy messages to another mailbox */
155 pop3_append, /* append string message to mailbox */
156 NIL /* garbage collect stream */
159 /* prototype stream */
160 MAILSTREAM pop3proto = {&pop3driver};
162 /* driver parameters */
163 static unsigned long pop3_maxlogintrials = MAXLOGINTRIALS;
164 static long pop3_port = 0;
165 static long pop3_sslport = 0;
167 /* POP3 mail validate mailbox
168 * Accepts: mailbox name
169 * Returns: our driver if name is valid, NIL otherwise
172 DRIVER *pop3_valid (char *name)
174 NETMBX mb;
175 return (mail_valid_net_parse (name,&mb) &&
176 !strcmp (mb.service,pop3driver.name) && !mb.authuser[0] &&
177 !compare_cstring (mb.mailbox,"INBOX")) ? &pop3driver : NIL;
181 /* News manipulate driver parameters
182 * Accepts: function code
183 * function-dependent value
184 * Returns: function-dependent return value
187 void *pop3_parameters (long function,void *value)
189 switch ((int) function) {
190 case SET_MAXLOGINTRIALS:
191 pop3_maxlogintrials = (unsigned long) value;
192 break;
193 case GET_MAXLOGINTRIALS:
194 value = (void *) pop3_maxlogintrials;
195 break;
196 case SET_POP3PORT:
197 pop3_port = (long) value;
198 break;
199 case GET_POP3PORT:
200 value = (void *) pop3_port;
201 break;
202 case SET_SSLPOPPORT:
203 pop3_sslport = (long) value;
204 break;
205 case GET_SSLPOPPORT:
206 value = (void *) pop3_sslport;
207 break;
208 case GET_IDLETIMEOUT:
209 value = (void *) IDLETIMEOUT;
210 break;
211 default:
212 value = NIL; /* error case */
213 break;
215 return value;
218 /* POP3 mail scan mailboxes for string
219 * Accepts: mail stream
220 * reference
221 * pattern to search
222 * string to scan
225 void pop3_scan (MAILSTREAM *stream,char *ref,char *pat,char *contents)
227 char tmp[MAILTMPLEN];
228 if ((ref && *ref) ? /* have a reference */
229 (pop3_valid (ref) && pmatch ("INBOX",pat)) :
230 (mail_valid_net (pat,&pop3driver,NIL,tmp) && pmatch ("INBOX",tmp)))
231 mm_log ("Scan not valid for POP3 mailboxes",ERROR);
235 /* POP3 mail find list of all mailboxes
236 * Accepts: mail stream
237 * reference
238 * pattern to search
241 void pop3_list (MAILSTREAM *stream,char *ref,char *pat)
243 char tmp[MAILTMPLEN];
244 if (ref && *ref) { /* have a reference */
245 if (pop3_valid (ref) && pmatch ("INBOX",pat)) {
246 strcpy (strchr (strcpy (tmp,ref),'}')+1,"INBOX");
247 mm_list (stream,NIL,tmp,LATT_NOINFERIORS);
250 else if (mail_valid_net (pat,&pop3driver,NIL,tmp) && pmatch ("INBOX",tmp)) {
251 strcpy (strchr (strcpy (tmp,pat),'}')+1,"INBOX");
252 mm_list (stream,NIL,tmp,LATT_NOINFERIORS);
256 /* POP3 mail find list of subscribed mailboxes
257 * Accepts: mail stream
258 * reference
259 * pattern to search
262 void pop3_lsub (MAILSTREAM *stream,char *ref,char *pat)
264 void *sdb = NIL;
265 char *s,mbx[MAILTMPLEN],tmp[MAILTMPLEN];
266 if (*pat == '{') { /* if remote pattern, must be POP3 */
267 if (!pop3_valid (pat)) return;
268 ref = NIL; /* good POP3 pattern, punt reference */
270 /* if remote reference, must be valid POP3 */
271 if (ref && (*ref == '{') && !pop3_valid (ref)) return;
272 /* kludgy application of reference */
273 if (ref && *ref) sprintf (mbx,"%s%s",ref,pat);
274 else strcpy (mbx,pat);
276 if ((s = sm_read (tmp,&sdb)) != NULL) do if (pop3_valid (s) && pmatch (s,mbx))
277 mm_lsub (stream,NIL,s,NIL);
278 /* until no more subscriptions */
279 while ((s = sm_read (tmp,&sdb)) != NULL);
283 /* POP3 mail subscribe to mailbox
284 * Accepts: mail stream
285 * mailbox to add to subscription list
286 * Returns: T on success, NIL on failure
289 long pop3_subscribe (MAILSTREAM *stream,char *mailbox)
291 return sm_subscribe (mailbox);
295 /* POP3 mail unsubscribe to mailbox
296 * Accepts: mail stream
297 * mailbox to delete from subscription list
298 * Returns: T on success, NIL on failure
301 long pop3_unsubscribe (MAILSTREAM *stream,char *mailbox)
303 return sm_unsubscribe (mailbox);
306 /* POP3 mail create mailbox
307 * Accepts: mail stream
308 * mailbox name to create
309 * Returns: T on success, NIL on failure
312 long pop3_create (MAILSTREAM *stream,char *mailbox)
314 return NIL; /* never valid for POP3 */
318 /* POP3 mail delete mailbox
319 * mailbox name to delete
320 * Returns: T on success, NIL on failure
323 long pop3_delete (MAILSTREAM *stream,char *mailbox)
325 return NIL; /* never valid for POP3 */
329 /* POP3 mail rename mailbox
330 * Accepts: mail stream
331 * old mailbox name
332 * new mailbox name
333 * Returns: T on success, NIL on failure
336 long pop3_rename (MAILSTREAM *stream,char *old,char *newname)
338 return NIL; /* never valid for POP3 */
341 /* POP3 status
342 * Accepts: mail stream
343 * mailbox name
344 * status flags
345 * Returns: T on success, NIL on failure
348 long pop3_status (MAILSTREAM *stream,char *mbx,long flags)
350 MAILSTATUS status;
351 unsigned long i;
352 long ret = NIL;
353 MAILSTREAM *tstream =
354 (stream && LOCAL->netstream && mail_usable_network_stream (stream,mbx)) ?
355 stream : mail_open (NIL,mbx,OP_SILENT);
356 if (tstream) { /* have a usable stream? */
357 status.flags = flags; /* return status values */
358 status.messages = tstream->nmsgs;
359 status.recent = tstream->recent;
360 if (flags & SA_UNSEEN) /* must search to get unseen messages */
361 for (i = 1,status.unseen = 0; i <= tstream->nmsgs; i++)
362 if (!mail_elt (tstream,i)->seen) status.unseen++;
363 status.uidnext = tstream->uid_last + 1;
364 status.uidvalidity = tstream->uid_validity;
365 /* pass status to main program */
366 mm_status (tstream,mbx,&status);
367 if (stream != tstream) mail_close (tstream);
368 ret = LONGT;
370 return ret; /* success */
373 /* POP3 mail open
374 * Accepts: stream to open
375 * Returns: stream on success, NIL on failure
378 MAILSTREAM *pop3_open (MAILSTREAM *stream)
380 unsigned long i,j;
381 char *s,*t,tmp[MAILTMPLEN],usr[MAILTMPLEN];
382 NETMBX mb;
383 MESSAGECACHE *elt;
384 /* return prototype for OP_PROTOTYPE call */
385 if (!stream) return &pop3proto;
386 mail_valid_net_parse (stream->mailbox,&mb);
387 usr[0] = '\0'; /* initially no user name */
388 if (stream->local) fatal ("pop3 recycle stream");
389 /* /anonymous not supported */
390 if (mb.anoflag || stream->anonymous) {
391 mm_log ("Anonymous POP3 login not available",ERROR);
392 return NIL;
394 /* /readonly not supported either */
395 if (mb.readonlyflag || stream->rdonly) {
396 mm_log ("Read-only POP3 access not available",ERROR);
397 return NIL;
399 /* copy other switches */
400 if (mb.dbgflag) stream->debug = T;
401 if (mb.secflag) stream->secure = T;
402 mb.trysslflag = stream->tryssl = (mb.trysslflag || stream->tryssl) ? T : NIL;
403 stream->local = /* instantiate localdata */
404 (void *) memset (fs_get (sizeof (POP3LOCAL)),0,sizeof (POP3LOCAL));
405 stream->sequence++; /* bump sequence number */
406 stream->perm_deleted = T; /* deleted is only valid flag */
408 if ((LOCAL->netstream = /* try to open connection */
409 net_open (&mb,NIL,pop3_port ? pop3_port : POP3TCPPORT,
410 (NETDRIVER *) mail_parameters (NIL,GET_SSLDRIVER,NIL),
411 "*pop3s",pop3_sslport ? pop3_sslport : POP3SSLPORT)) &&
412 pop3_reply (stream)) {
413 mm_log (LOCAL->reply,NIL); /* give greeting */
414 if (!pop3_auth (stream,&mb,tmp,usr)) pop3_close (stream,NIL);
415 else if (pop3_send (stream,"STAT",NIL)) {
416 int silent = stream->silent;
417 stream->silent = T;
418 sprintf (tmp,"{%.200s:%lu/pop3",
419 (long) mail_parameters (NIL,GET_TRUSTDNS,NIL) ?
420 net_host (LOCAL->netstream) : mb.host,
421 net_port (LOCAL->netstream));
422 if (mb.tlsflag) strcat (tmp,"/tls");
423 if (mb.tlssslv23) strcat (tmp,"/tls-sslv23");
424 if (mb.tls1) strcat (tmp,"/tls1");
425 if (mb.tls1_1) strcat (tmp,"/tls1_1");
426 if (mb.tls1_2) strcat (tmp,"/tls1_2");
427 if (mb.tls1_3) strcat (tmp,"/tls1_3");
428 if (mb.notlsflag) strcat (tmp,"/notls");
429 if (mb.sslflag) strcat (tmp,"/ssl");
430 if (mb.novalidate) strcat (tmp,"/novalidate-cert");
431 if ((LOCAL->loser = mb.loser) != 0) strcat (tmp,"/loser");
432 if (stream->secure) strcat (tmp,"/secure");
433 sprintf (tmp + strlen (tmp),"/user=\"%s\"}%s",usr,mb.mailbox);
434 stream->inbox = T; /* always INBOX */
435 fs_give ((void **) &stream->mailbox);
436 stream->mailbox = cpystr (tmp);
437 /* notify upper level */
438 mail_exists (stream,stream->uid_last = strtoul (LOCAL->reply,NIL,10));
439 mail_recent (stream,stream->nmsgs);
440 /* instantiate elt */
441 for (i = 0; i < stream->nmsgs;) {
442 elt = mail_elt (stream,++i);
443 elt->valid = elt->recent = T;
444 elt->private.uid = i;
447 /* trust LIST output if new server */
448 if (!LOCAL->loser && LOCAL->cap.capa && pop3_send (stream,"LIST",NIL)) {
449 while ((s = net_getline (LOCAL->netstream)) && (*s != '.')) {
450 if ((i = strtoul (s,&t,10)) && (i <= stream->nmsgs) &&
451 (j = strtoul (t,NIL,10))) mail_elt (stream,i)->rfc822_size = j;
452 fs_give ((void **) &s);
454 /* flush final dot */
455 if (s) fs_give ((void **) &s);
456 else { /* lost connection */
457 mm_log ("POP3 connection broken while itemizing messages",ERROR);
458 pop3_close (stream,NIL);
459 return NIL;
462 stream->silent = silent; /* notify main program */
463 mail_exists (stream,stream->nmsgs);
464 /* notify if empty */
465 if (!(stream->nmsgs || stream->silent)) mm_log ("Mailbox is empty",WARN);
467 else { /* error in STAT */
468 mm_log (LOCAL->reply,ERROR);
469 pop3_close (stream,NIL); /* too bad */
472 else { /* connection failed */
473 if (LOCAL->reply) mm_log (LOCAL->reply,ERROR);
474 pop3_close (stream,NIL); /* failed, clean up */
476 return LOCAL ? stream : NIL; /* if stream is alive, return to caller */
479 /* POP3 capabilities
480 * Accepts: stream
481 * authenticator flags
482 * Returns: T on success, NIL on failure
485 long pop3_capa (MAILSTREAM *stream,long flags)
487 unsigned long i;
488 char *s,*t,*r,*args;
489 if (LOCAL->cap.implementation)/* zap all old capabilities */
490 fs_give ((void **) &LOCAL->cap.implementation);
491 memset (&LOCAL->cap,0,sizeof (LOCAL->cap));
492 /* get server capabilities */
493 if (pop3_send (stream,"CAPA",NIL)) LOCAL->cap.capa = T;
494 else {
495 LOCAL->cap.user = T; /* guess worst-case old server */
496 return NIL; /* no CAPA on this server */
498 while ((t = net_getline (LOCAL->netstream)) && (t[1] || (*t != '.'))) {
499 if (stream->debug) mm_dlog (t);
500 /* get optional capability arguments */
501 if ((args = strchr (t,' ')) != NULL) *args++ = '\0';
502 if (!compare_cstring (t,"STLS")) LOCAL->cap.stls = T;
503 else if (!compare_cstring (t,"PIPELINING")) LOCAL->cap.pipelining = T;
504 else if (!compare_cstring (t,"RESP-CODES")) LOCAL->cap.respcodes = T;
505 else if (!compare_cstring (t,"TOP")) LOCAL->cap.top = T;
506 else if (!compare_cstring (t,"UIDL")) LOCAL->cap.uidl = T;
507 else if (!compare_cstring (t,"USER")) LOCAL->cap.user = T;
508 else if (!compare_cstring (t,"IMPLEMENTATION") && args)
509 LOCAL->cap.implementation = cpystr (args);
510 else if (!compare_cstring (t,"EXPIRE") && args) {
511 LOCAL->cap.expire = T; /* note that it is present */
512 if ((s = strchr(args,' ')) != NULL){/* separate time from possible USER */
513 *s++ = '\0';
514 /* in case they add something after USER */
515 if ((strlen (s) > 4) && (s[4] == ' ')) s[4] = '\0';
517 LOCAL->cap.expire = /* get expiration time */
518 (!compare_cstring (args,"NEVER")) ? 65535 :
519 ((s && !compare_cstring (s,"USER")) ? -atoi (args) : atoi (args));
521 else if (!compare_cstring (t,"LOGIN-DELAY") && args) {
522 LOCAL->cap.logindelay = T;/* note that it is present */
523 if ((s = strchr(args,' ')) != NULL){/* separate time from possible USER */
524 *s++ = '\0';
525 /* in case they add something after USER */
526 if ((strlen (s) > 4) && (s[4] == ' ')) s[4] = '\0';
528 /* get delay time */
529 LOCAL->cap.delaysecs = (s && !compare_cstring (s,"USER")) ?
530 -atoi (args) : atoi (args);
532 else if (!compare_cstring (t,"SASL") && args)
533 for (args = strtok_r (args," ",&r); args; args = strtok_r (NIL," ",&r))
534 if ((i = mail_lookup_auth_name (args,flags)) &&
535 (--i < MAXAUTHENTICATORS))
536 LOCAL->cap.sasl |= (1 << i);
537 fs_give ((void **) &t);
539 if (t) { /* flush end of text indicator */
540 if (stream->debug) mm_dlog (t);
541 fs_give ((void **) &t);
543 return LONGT;
546 /* POP3 authenticate
547 * Accepts: stream to login
548 * parsed network mailbox structure
549 * scratch buffer of length MAILTMPLEN
550 * place to return user name
551 * Returns: T on success, NIL on failure
554 long pop3_auth (MAILSTREAM *stream,NETMBX *mb,char *pwd,char *usr)
556 unsigned long i,trial,auths = 0, authsaved;
557 char *t, *app_pwd = NIL;
558 AUTHENTICATOR *at, *atsaved;
559 long ret = NIL;
560 long flags = (stream->secure ? AU_SECURE : NIL) |
561 (mb->authuser[0] ? AU_AUTHUSER : NIL);
562 long capaok = pop3_capa (stream,flags);
563 NETDRIVER *ssld = (NETDRIVER *) mail_parameters (NIL,GET_SSLDRIVER,NIL);
564 sslstart_t stls = (sslstart_t) mail_parameters (NIL,GET_SSLSTART,NIL);
565 /* server has TLS? */
566 if (stls && LOCAL->cap.stls && !mb->sslflag && !mb->notlsflag &&
567 pop3_send (stream,"STLS",NIL)) {
568 mb->tlsflag = T; /* TLS OK, get into TLS at this end */
569 LOCAL->netstream->dtb = ssld;
570 if (!(LOCAL->netstream->stream =
571 (*stls) (LOCAL->netstream->stream,mb->host,
572 SSL_MTHD(*mb) | (mb->novalidate ? NET_NOVALIDATECERT : NIL)))) {
573 /* drat, drop this connection */
574 if (LOCAL->netstream) net_close (LOCAL->netstream);
575 LOCAL->netstream= NIL;
576 return NIL; /* TLS negotiation failed */
578 pop3_capa (stream,flags); /* get capabilities now that TLS in effect */
580 else if (mb->tlsflag) { /* user specified /tls but can't do it */
581 mm_log ("Unable to negotiate TLS with this server",ERROR);
582 return NIL;
584 /* get authenticators from capabilities */
585 if (capaok) auths = LOCAL->cap.sasl;
586 /* get list of authenticators */
587 else if (pop3_send (stream,"AUTH",NIL)) {
588 while ((t = net_getline (LOCAL->netstream)) && (t[1] || (*t != '.'))) {
589 if (stream->debug) mm_dlog (t);
590 if ((i = mail_lookup_auth_name (t,flags)) && (--i < MAXAUTHENTICATORS))
591 auths |= (1 << i);
592 fs_give ((void **) &t);
594 if (t) { /* flush end of text indicator */
595 if (stream->debug) mm_dlog (t);
596 fs_give ((void **) &t);
599 /* disable LOGIN if PLAIN also advertised */
600 if ((i = mail_lookup_auth_name ("PLAIN",NIL)) && (--i < MAXAUTHENTICATORS) &&
601 (auths & (1 << i)) &&
602 (i = mail_lookup_auth_name ("LOGIN",NIL)) && (--i < MAXAUTHENTICATORS))
603 auths &= ~(1 << i);
605 if (auths) { /* got any authenticators? */
606 if ((long) mail_parameters (NIL,GET_TRUSTDNS,NIL)) {
607 /* remote name for authentication */
608 strncpy (mb->host,(long) mail_parameters (NIL,GET_SASLUSESPTRNAME,NIL) ?
609 net_remotehost (LOCAL->netstream) : net_host (LOCAL->netstream),
610 NETMAXHOST-1);
611 mb->host[NETMAXHOST-1] = '\0';
613 for (t = NIL, LOCAL->saslcancel = NIL; !ret && LOCAL->netstream && auths &&
614 (at = mail_lookup_auth (find_rightmost_bit (&auths)+1)); ) {
615 if(mb && *mb->auth){
616 if(!compare_cstring(at->name, mb->auth))
617 atsaved = at;
618 else{
619 authsaved = auths;
620 continue;
624 if (t) { /* previous authenticator failed? */
625 sprintf (pwd,"Retrying using %.80s authentication after %.80s",
626 at->name,t);
627 mm_log (pwd,NIL);
628 fs_give ((void **) &t);
630 trial = 0; /* initial trial count */
631 do {
632 if (t) {
633 sprintf (pwd,"Retrying %s authentication after %.80s",at->name,t);
634 mm_log (pwd,WARN);
635 fs_give ((void **) &t);
637 LOCAL->saslcancel = NIL;
638 if (pop3_send (stream,"AUTH",at->name)) {
639 /* hide client authentication responses */
640 if (!(at->flags & AU_SECURE)) LOCAL->sensitive = T;
641 if ((*at->client) (pop3_challenge,pop3_response,"pop",mb,stream,
642 &trial,usr) && LOCAL->response) {
643 if (*LOCAL->response == '+') ret = LONGT;
644 /* if main program requested cancellation */
645 else if (!trial) mm_log ("POP3 Authentication cancelled",ERROR);
647 LOCAL->sensitive=NIL; /* unhide */
649 /* remember response if error and no cancel */
650 if (!ret && trial) t = cpystr (LOCAL->reply);
651 } while (!ret && trial && (trial < pop3_maxlogintrials) &&
652 LOCAL->netstream);
654 if (t) { /* previous authenticator failed? */
655 if (!LOCAL->saslcancel) { /* don't do this if a cancel */
656 sprintf (pwd,"Can not authenticate to POP3 server: %.80s",t);
657 mm_log (pwd,ERROR);
659 fs_give ((void **) &t);
661 if(mb && *mb->auth){
662 if(!authsaved) sprintf (pwd,"Client does not support AUTH=%.80s authenticator",mb->auth);
663 else if (!atsaved) sprintf (pwd,"POP server does not support AUTH=%.80s authenticator",mb->auth);
664 if (!authsaved || !atsaved) mm_log (pwd,ERROR);
669 else if (stream->secure)
670 mm_log ("Can't do secure authentication with this server",ERROR);
671 else if (mb->authuser[0])
672 mm_log ("Can't do /authuser with this server",ERROR);
673 else if (!LOCAL->cap.user) mm_log ("Can't login to this server",ERROR);
674 else { /* traditional login */
675 trial = 0; /* initial trial count */
676 do {
677 pwd[0] = 0; /* prompt user for password */
678 if(app_pwd) fs_give((void **) &app_pwd);
679 mm_login (mb,usr, &app_pwd,trial++);
680 if(app_pwd){
681 strncpy(pwd, app_pwd, MAILTMPLEN);
682 pwd[MAILTMPLEN-1] = '\0';
684 if (pwd[0]) { /* send login sequence if have password */
685 if (pop3_send (stream,"USER",usr)) {
686 LOCAL->sensitive = T; /* hide this command */
687 if (pop3_send (stream,"PASS",pwd)) ret = LONGT;
688 LOCAL->sensitive=NIL; /* unhide */
690 if (!ret) { /* failure */
691 mm_log (LOCAL->reply,WARN);
692 if (trial == pop3_maxlogintrials)
693 mm_log ("Too many login failures",ERROR);
696 /* user refused to give password */
697 else mm_log ("Login aborted",ERROR);
698 } while (!ret && pwd[0] && (trial < pop3_maxlogintrials) &&
699 LOCAL->netstream);
701 if(app_pwd) fs_give((void **) &app_pwd);
702 memset (pwd,0,MAILTMPLEN); /* erase password */
703 /* get capabilities if logged in */
704 if (ret && capaok) pop3_capa (stream,flags);
705 return ret;
708 /* Get challenge to authenticator in binary
709 * Accepts: stream
710 * pointer to returned size
711 * Returns: challenge or NIL if not challenge
714 void *pop3_challenge (void *s,unsigned long *len)
716 char tmp[MAILTMPLEN];
717 void *ret = NIL;
718 MAILSTREAM *stream = (MAILSTREAM *) s;
719 if (stream && LOCAL->response &&
720 (*LOCAL->response == '+') && (LOCAL->response[1] == ' ') &&
721 !(ret = rfc822_base64 ((unsigned char *) LOCAL->reply,
722 strlen (LOCAL->reply),len))) {
723 sprintf (tmp,"POP3 SERVER BUG (invalid challenge): %.80s",LOCAL->reply);
724 mm_log (tmp,ERROR);
726 return ret;
730 /* Send authenticator response in BASE64
731 * Accepts: MAIL stream
732 * string to send
733 * length of string
734 * Returns: T if successful, else NIL
737 long pop3_response (void *s,char *response,unsigned long size)
739 MAILSTREAM *stream = (MAILSTREAM *) s;
740 unsigned long i,j,ret;
741 char *t,*u;
742 if (response) { /* make CRLFless BASE64 string */
743 if (size) {
744 for (t = (char *) rfc822_binary ((void *) response,size,&i),u = t,j = 0;
745 j < i; j++) if (t[j] > ' ') *u++ = t[j];
746 *u = '\0'; /* tie off string for mm_dlog() */
747 if (stream->debug) mail_dlog (t,LOCAL->sensitive);
748 /* append CRLF */
749 *u++ = '\015'; *u++ = '\012'; *u = '\0';
750 ret = net_sout (LOCAL->netstream,t,u - t);
751 fs_give ((void **) &t);
753 else ret = net_sout (LOCAL->netstream,"\015\012",2);
755 else { /* abort requested */
756 ret = net_sout (LOCAL->netstream,"*\015\012",3);
757 LOCAL->saslcancel = T; /* mark protocol-requested SASL cancel */
759 pop3_reply (stream); /* get response */
760 return ret;
763 /* POP3 mail close
764 * Accepts: MAIL stream
765 * option flags
768 void pop3_close (MAILSTREAM *stream,long options)
770 int silent = stream->silent;
771 if (LOCAL) { /* only if a file is open */
772 if (LOCAL->netstream) { /* close POP3 connection */
773 stream->silent = T;
774 if (options & CL_EXPUNGE) pop3_expunge (stream,NIL,NIL);
775 stream->silent = silent;
776 pop3_send (stream,"QUIT",NIL);
777 mm_notify (stream,LOCAL->reply,BYE);
779 /* close POP3 connection */
780 if (LOCAL->netstream) net_close (LOCAL->netstream);
781 /* clean up */
782 if (LOCAL->cap.implementation)
783 fs_give ((void **) &LOCAL->cap.implementation);
784 if (LOCAL->txt) fclose (LOCAL->txt);
785 LOCAL->txt = NIL;
786 if (LOCAL->response) fs_give ((void **) &LOCAL->response);
787 /* nuke the local data */
788 fs_give ((void **) &stream->local);
789 stream->dtb = NIL; /* log out the DTB */
793 /* POP3 mail fetch fast information
794 * Accepts: MAIL stream
795 * sequence
796 * option flags
797 * This is ugly and slow
800 void pop3_fetchfast (MAILSTREAM *stream,char *sequence,long flags)
802 unsigned long i;
803 MESSAGECACHE *elt;
804 /* get sequence */
805 if (stream && LOCAL && ((flags & FT_UID) ?
806 mail_uid_sequence (stream,sequence) :
807 mail_sequence (stream,sequence)))
808 for (i = 1; i <= stream->nmsgs; i++)
809 if ((elt = mail_elt (stream,i))->sequence &&
810 !(elt->day && elt->rfc822_size)) {
811 ENVELOPE **env = NIL;
812 ENVELOPE *e = NIL;
813 if (!stream->scache) env = &elt->private.msg.env;
814 else if (stream->msgno == i) env = &stream->env;
815 else env = &e;
816 if (!*env || !elt->rfc822_size) {
817 STRING bs;
818 unsigned long hs;
819 char *ht = (*stream->dtb->header) (stream,i,&hs,NIL);
820 /* need to make an envelope? */
821 if (!*env) rfc822_parse_msg (env,NIL,ht,hs,NIL,BADHOST,
822 stream->dtb->flags);
823 /* need message size too, ugh */
824 if (!elt->rfc822_size) {
825 (*stream->dtb->text) (stream,i,&bs,FT_PEEK);
826 elt->rfc822_size = hs + SIZE (&bs) - GETPOS (&bs);
829 /* if need date, have date in envelope? */
830 if (!elt->day && *env && (*env)->date)
831 mail_parse_date (elt,(*env)->date);
832 /* sigh, fill in bogus default */
833 if (!elt->day) elt->day = elt->month = 1;
834 mail_free_envelope (&e);
838 /* POP3 fetch header as text
839 * Accepts: mail stream
840 * message number
841 * pointer to return size
842 * flags
843 * Returns: header text
846 char *pop3_header (MAILSTREAM *stream,unsigned long msgno,unsigned long *size,
847 long flags)
849 unsigned long i;
850 char tmp[MAILTMPLEN];
851 MESSAGECACHE *elt;
852 FILE *f = NIL;
853 *size = 0; /* initially no header size */
854 if ((flags & FT_UID) && !(msgno = mail_msgno (stream,msgno))) return "";
855 /* have header text already? */
856 if (!(elt = mail_elt (stream,msgno))->private.msg.header.text.data) {
857 /* if have CAPA and TOP, assume good TOP */
858 if (!LOCAL->loser && LOCAL->cap.top) {
859 sprintf (tmp,"TOP %lu 0",mail_uid (stream,msgno));
860 if (pop3_send (stream,tmp,NIL))
861 f = netmsg_slurp (LOCAL->netstream,&i,
862 &elt->private.msg.header.text.size);
864 /* otherwise load the cache with the message */
865 else if ((elt->private.msg.header.text.size = pop3_cache (stream,elt)) != 0L)
866 f = LOCAL->txt;
867 if (f) { /* got it, make sure at start of file */
868 fseek (f,(unsigned long) 0,SEEK_SET);
869 /* read header from the cache */
870 fread (elt->private.msg.header.text.data = (unsigned char *)
871 fs_get ((size_t) elt->private.msg.header.text.size + 1),
872 (size_t) 1,(size_t) elt->private.msg.header.text.size,f);
873 /* tie off header text */
874 elt->private.msg.header.text.data[elt->private.msg.header.text.size] =
875 '\0';
876 /* close if not the cache */
877 if (f != LOCAL->txt) fclose (f);
880 /* return size of text */
881 if (size) *size = elt->private.msg.header.text.size;
882 return elt->private.msg.header.text.data ?
883 (char *) elt->private.msg.header.text.data : "";
886 /* POP3 fetch body
887 * Accepts: mail stream
888 * message number
889 * pointer to stringstruct to initialize
890 * flags
891 * Returns: T if successful, else NIL
894 long pop3_text (MAILSTREAM *stream,unsigned long msgno,STRING *bs,long flags)
896 MESSAGECACHE *elt;
897 INIT (bs,mail_string,(void *) "",0);
898 if ((flags & FT_UID) && !(msgno = mail_msgno (stream,msgno))) return NIL;
899 elt = mail_elt (stream,msgno);
900 pop3_cache (stream,elt); /* make sure cache loaded */
901 if (!LOCAL->txt) return NIL; /* error if don't have a file */
902 if (!(flags & FT_PEEK)) { /* mark seen if needed */
903 elt->seen = T;
904 mm_flags (stream,elt->msgno);
906 INIT (bs,file_string,(void *) LOCAL->txt,elt->rfc822_size);
907 SETPOS (bs,LOCAL->hdrsize); /* skip past header */
908 return T;
911 /* POP3 cache message
912 * Accepts: mail stream
913 * message number
914 * Returns: header size
917 unsigned long pop3_cache (MAILSTREAM *stream,MESSAGECACHE *elt)
919 /* already cached? */
920 if (LOCAL->cached != mail_uid (stream,elt->msgno)) {
921 /* no, close current file */
922 if (LOCAL->txt) fclose (LOCAL->txt);
923 LOCAL->txt = NIL;
924 LOCAL->cached = LOCAL->hdrsize = 0;
925 if (pop3_send_num (stream,"RETR",elt->msgno) &&
926 (LOCAL->txt = netmsg_slurp (LOCAL->netstream,&elt->rfc822_size,
927 &LOCAL->hdrsize)))
928 /* set as current message number */
929 LOCAL->cached = mail_uid (stream,elt->msgno);
930 else elt->deleted = T;
932 return LOCAL->hdrsize;
935 /* POP3 mail ping mailbox
936 * Accepts: MAIL stream
937 * Returns: T if stream alive, else NIL
940 long pop3_ping (MAILSTREAM *stream)
942 return pop3_send (stream,"NOOP",NIL);
946 /* POP3 mail check mailbox
947 * Accepts: MAIL stream
950 void pop3_check (MAILSTREAM *stream)
952 if (pop3_ping (stream)) mm_log ("Check completed",NIL);
956 /* POP3 mail expunge mailbox
957 * Accepts: MAIL stream
958 * sequence to expunge if non-NIL
959 * expunge options
960 * Returns: T if success, NIL if failure
963 long pop3_expunge (MAILSTREAM *stream,char *sequence,long options)
965 char tmp[MAILTMPLEN];
966 MESSAGECACHE *elt;
967 unsigned long i = 1,n = 0;
968 long ret;
969 if ((ret = sequence ? ((options & EX_UID) ?
970 mail_uid_sequence (stream,sequence) :
971 mail_sequence (stream,sequence)) :
972 LONGT) != 0L) { /* build selected sequence if needed */
973 while (i <= stream->nmsgs) {
974 elt = mail_elt (stream,i);
975 if (elt->deleted && (sequence ? elt->sequence : T) &&
976 pop3_send_num (stream,"DELE",i)) {
977 /* expunging currently cached message? */
978 if (LOCAL->cached == mail_uid (stream,i)) {
979 /* yes, close current file */
980 if (LOCAL->txt) fclose (LOCAL->txt);
981 LOCAL->txt = NIL;
982 LOCAL->cached = LOCAL->hdrsize = 0;
984 mail_expunged (stream,i);
985 n++;
987 else i++; /* try next message */
989 if (!stream->silent) { /* only if not silent */
990 if (n) { /* did we expunge anything? */
991 sprintf (tmp,"Expunged %lu messages",n);
992 mm_log (tmp,(long) NIL);
994 else mm_log ("No messages deleted, so no update needed",(long) NIL);
997 return ret;
1000 /* POP3 mail copy message(s)
1001 * Accepts: MAIL stream
1002 * sequence
1003 * destination mailbox
1004 * option flags
1005 * Returns: T if copy successful, else NIL
1008 long pop3_copy (MAILSTREAM *stream,char *sequence,char *mailbox,long options)
1010 mailproxycopy_t pc =
1011 (mailproxycopy_t) mail_parameters (stream,GET_MAILPROXYCOPY,NIL);
1012 if (pc) return (*pc) (stream,sequence,mailbox,options);
1013 mm_log ("Copy not valid for POP3",ERROR);
1014 return NIL;
1018 /* POP3 mail append message from stringstruct
1019 * Accepts: MAIL stream
1020 * destination mailbox
1021 * append callback
1022 * data for callback
1023 * Returns: T if append successful, else NIL
1026 long pop3_append (MAILSTREAM *stream,char *mailbox,append_t af,void *data)
1028 mm_log ("Append not valid for POP3",ERROR);
1029 return NIL;
1032 /* Internal routines */
1035 /* Post Office Protocol 3 send command with number argument
1036 * Accepts: MAIL stream
1037 * command
1038 * number
1039 * Returns: T if successful, NIL if failure
1042 long pop3_send_num (MAILSTREAM *stream,char *command,unsigned long n)
1044 char tmp[MAILTMPLEN];
1045 sprintf (tmp,"%lu",mail_uid (stream,n));
1046 return pop3_send (stream,command,tmp);
1050 /* Post Office Protocol 3 send command
1051 * Accepts: MAIL stream
1052 * command
1053 * command argument
1054 * Returns: T if successful, NIL if failure
1057 long pop3_send (MAILSTREAM *stream,char *command,char *args)
1059 long ret;
1060 char *s = (char *) fs_get (strlen (command) + (args ? strlen (args) + 1: 0)
1061 + 3);
1062 mail_lock (stream); /* lock up the stream */
1063 if (!LOCAL->netstream) ret = pop3_fake (stream,"POP3 connection lost");
1064 else { /* build the complete command */
1065 if (args) sprintf (s,"%s %s",command,args);
1066 else strcpy (s,command);
1067 if (stream->debug) mail_dlog (s,LOCAL->sensitive);
1068 strcat (s,"\015\012");
1069 /* send the command */
1070 ret = net_soutr (LOCAL->netstream,s) ? pop3_reply (stream) :
1071 pop3_fake (stream,"POP3 connection broken in command");
1073 fs_give ((void **) &s);
1074 mail_unlock (stream); /* unlock stream */
1075 return ret;
1078 /* Post Office Protocol 3 get reply
1079 * Accepts: MAIL stream
1080 * Returns: T if success reply, NIL if error reply
1083 long pop3_reply (MAILSTREAM *stream)
1085 char *s;
1086 /* flush old reply */
1087 if (LOCAL->response) fs_give ((void **) &LOCAL->response);
1088 /* get reply */
1089 if (!(LOCAL->response = net_getline (LOCAL->netstream)))
1090 return pop3_fake (stream,"POP3 connection broken in response");
1091 if (stream->debug) mm_dlog (LOCAL->response);
1092 LOCAL->reply = (s = strchr (LOCAL->response,' ')) ? s + 1 : LOCAL->response;
1093 /* return success or failure */
1094 return (*LOCAL->response =='+') ? T : NIL;
1098 /* Post Office Protocol 3 set fake error
1099 * Accepts: MAIL stream
1100 * error text
1101 * Returns: NIL, always
1104 long pop3_fake (MAILSTREAM *stream,char *text)
1106 mm_notify (stream,text,BYE); /* send bye alert */
1107 if (LOCAL->netstream) net_close (LOCAL->netstream);
1108 LOCAL->netstream = NIL; /* farewell, dear TCP stream */
1109 /* flush any old reply */
1110 if (LOCAL->response) fs_give ((void **) &LOCAL->response);
1111 LOCAL->reply = text; /* set up pseudo-reply string */
1112 return NIL; /* return error code */