This commit was generated by cvs2svn to compensate for changes in r4038,
[chise/xemacs-chise.git.1] / lib-src / pop.c
1 /* pop.c: client routines for talking to a POP3-protocol post-office server
2    Copyright (c) 1991, 1993, 1996 Free Software Foundation, Inc.
3    Written by Jonathan Kamens, jik@security.ov.com.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING.  If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #ifdef HAVE_CONFIG_H
23 #define NO_SHORTNAMES   /* Tell config not to load remap.h */
24 #define DONT_ENCAPSULATE
25 #include <config.h>
26 #else
27 #define MAIL_USE_POP
28 #endif
29
30 #ifdef MAIL_USE_POP
31
32 #include <sys/types.h>
33 #ifdef WIN32_NATIVE
34 #include <winsock.h>
35 #undef SOCKET_ERROR
36 #define RECV(s,buf,len,flags) recv(s,buf,len,flags)
37 #define SEND(s,buf,len,flags) send(s,buf,len,flags)
38 #define CLOSESOCKET(s) closesocket(s)
39 #else
40 #include <netinet/in.h>
41 #include <sys/socket.h>
42 #define RECV(s,buf,len,flags) read(s,buf,len)
43 #define SEND(s,buf,len,flags) write(s,buf,len)
44 #define CLOSESOCKET(s) close(s)
45 #endif
46 #include "pop.h"
47
48 #ifdef sun
49 #include <malloc.h>
50 #endif /* sun */
51
52 #ifdef HESIOD
53 #include <hesiod.h>
54 /*
55  * It really shouldn't be necessary to put this declaration here, but
56  * the version of hesiod.h that Athena has installed in release 7.2
57  * doesn't declare this function; I don't know if the 7.3 version of
58  * hesiod.h does.
59  */
60 extern struct servent *hes_getservbyname (/* char *, char * */);
61 #endif
62
63 #include "../src/syspwd.h"
64 #ifndef WIN32_NATIVE
65 #include <netdb.h>
66 #endif
67 #include <errno.h>
68 #include <stdio.h>
69
70 #ifdef HAVE_UNISTD_H
71 #include <unistd.h>
72 #endif
73 #include <sys/stat.h>
74 #ifndef WIN32_NATIVE
75 #include <sys/file.h>
76 #endif
77 #include "../src/syswait.h"
78 #ifndef WIN32_NATIVE
79 #include "../src/systime.h"
80 #endif
81 #include <stdlib.h>
82 #include <string.h>
83
84 #ifdef KERBEROS
85 #ifndef KRB5
86 #include <des.h>
87 #include <krb.h>
88 #else /* KRB5 */
89 #include <krb5/krb5.h>
90 #include <krb5/ext-proto.h>
91 #include <ctype.h>
92 #endif /* KRB5 */
93 #endif /* KERBEROS */
94
95 #ifdef KERBEROS
96 #ifndef KRB5
97 extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *,
98                             unsigned long, MSG_DAT *, CREDENTIALS *, Key_schedule,
99                             struct sockaddr_in *, struct sockaddr_in *,
100                             char * */);
101 extern char *krb_realmofhost (/* char * */);
102 #endif /* ! KRB5 */
103 #endif /* KERBEROS */
104
105 #ifndef WIN32_NATIVE
106 #if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H)
107 extern int h_errno;
108 #endif
109 #endif
110
111 static int socket_connection (char *, int);
112 static char *pop_getline (popserver);
113 static int sendline (popserver, char *);
114 static int fullwrite (int, char *, int);
115 static int getok (popserver);
116 #if 0
117 static int gettermination (popserver);
118 #endif
119 static void pop_trash (popserver);
120 static char *find_crlf (char *);
121
122 #define ERROR_MAX 80            /* a pretty arbitrary size */
123 #define POP_PORT 110
124 #define KPOP_PORT 1109
125 #if defined(WIN32_NATIVE) || defined(CYGWIN)
126 #define POP_SERVICE "pop3"      /* we don't want the POP2 port! */
127 #else
128 #define POP_SERVICE "pop"
129 #endif
130 #ifdef KERBEROS
131 #ifdef KRB5
132 #define KPOP_SERVICE "k5pop";
133 #else
134 #define KPOP_SERVICE "kpop"
135 #endif
136 #endif
137
138 char pop_error[ERROR_MAX];
139 int pop_debug = 0;
140
141 #ifndef min
142 #define min(a,b) (((a) < (b)) ? (a) : (b))
143 #endif
144
145 /*
146  * Function: pop_open (char *host, char *username, char *password,
147  *                     int flags)
148  *
149  * Purpose: Establishes a connection with a post-office server, and
150  *      completes the authorization portion of the session.
151  *
152  * Arguments:
153  *      host    The server host with which the connection should be
154  *              established.  Optional.  If omitted, internal
155  *              heuristics will be used to determine the server host,
156  *              if possible.
157  *      username
158  *              The username of the mail-drop to access.  Optional.
159  *              If omitted, internal heuristics will be used to
160  *              determine the username, if possible.
161  *      password
162  *              The password to use for authorization.  If omitted,
163  *              internal heuristics will be used to determine the
164  *              password, if possible.
165  *      flags   A bit mask containing flags controlling certain
166  *              functions of the routine.  Valid flags are defined in
167  *              the file pop.h
168  *
169  * Return value: Upon successful establishment of a connection, a
170  *      non-null popserver will be returned.  Otherwise, null will be
171  *      returned, and the string variable pop_error will contain an
172  *      explanation of the error.
173  */
174 popserver
175 pop_open (char *host, char *username, char *password, int flags)
176 {
177   int sock;
178   popserver server;
179
180   /* Determine the user name */
181   if (! username)
182     {
183       username = getenv ("USER");
184       if (! (username && *username))
185         {
186 #ifndef WIN32_NATIVE
187           username = getlogin ();
188           if (! (username && *username))
189             {
190               struct passwd *passwd;
191               passwd = getpwuid (getuid ());
192               if (passwd && passwd->pw_name && *passwd->pw_name)
193                 {
194                   username = passwd->pw_name;
195                 }
196               else
197                 {
198                   strcpy (pop_error, "Could not determine username");
199                   return (0);
200                 }
201             }
202 #else
203           strcpy (pop_error, "Could not determine username");
204           return (0);
205 #endif
206         }
207     }
208
209   /*
210    *  Determine the mail host.
211    */
212
213   if (! host)
214     {
215       host = getenv ("MAILHOST");
216     }
217
218 #ifdef HESIOD
219   if ((! host) && (! (flags & POP_NO_HESIOD)))
220     {
221       struct hes_postoffice *office;
222       office = hes_getmailhost (username);
223       if (office && office->po_type && (! strcmp (office->po_type, "POP"))
224           && office->po_name && *office->po_name && office->po_host
225           && *office->po_host)
226         {
227           host = office->po_host;
228           username = office->po_name;
229         }
230     }
231 #endif
232
233 #ifdef MAILHOST
234   if (! host)
235     {
236       host = MAILHOST;
237     }
238 #endif
239
240   if (! host)
241     {
242       strcpy (pop_error, "Could not determine POP server");
243       return (0);
244     }
245
246   /* Determine the password */
247 #ifdef KERBEROS
248 #define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS))
249 #else
250 #define DONT_NEED_PASSWORD 0
251 #endif
252  
253   if ((! password) && (! DONT_NEED_PASSWORD))
254     {
255 #ifndef WIN32_NATIVE
256       if (! (flags & POP_NO_GETPASS))
257         {
258           password = getpass ("Enter POP password:");
259         }
260 #endif
261       if (! password)
262         {
263           strcpy (pop_error, "Could not determine POP password");
264           return (0);
265         }
266     }
267   if (password)
268     flags |= POP_NO_KERBEROS;
269   else
270     password = username;
271
272   sock = socket_connection (host, flags);
273   if (sock == -1)
274     return (0);
275
276   server = (popserver) malloc (sizeof (struct _popserver));
277   if (! server)
278     {
279       strcpy (pop_error, "Out of memory in pop_open");
280       return (0);
281     }
282   server->buffer = (char *) malloc (GETLINE_MIN);
283   if (! server->buffer)
284     {
285       strcpy (pop_error, "Out of memory in pop_open");
286       free ((char *) server);
287       return (0);
288     }
289           
290   server->file = sock;
291   server->data = 0;
292   server->buffer_index = 0;
293   server->buffer_size = GETLINE_MIN;
294   server->in_multi = 0;
295   server->trash_started = 0;
296
297   if (getok (server))
298     return (0);
299
300   /*
301    * I really shouldn't use the pop_error variable like this, but....
302    */
303   if (strlen (username) > ERROR_MAX - 6)
304     {
305       pop_close (server);
306       strcpy (pop_error,
307               "Username too long; recompile pop.c with larger ERROR_MAX");
308       return (0);
309     }
310   sprintf (pop_error, "USER %s", username);
311
312   if (sendline (server, pop_error) || getok (server))
313     {
314       return (0);
315     }
316
317   if (strlen (password) > ERROR_MAX - 6)
318     {
319       pop_close (server);
320       strcpy (pop_error,
321               "Password too long; recompile pop.c with larger ERROR_MAX");
322       return (0);
323     }
324   sprintf (pop_error, "PASS %s", password);
325
326   if (sendline (server, pop_error) || getok (server))
327     {
328       return (0);
329     }
330
331   return (server);
332 }
333
334 /*
335  * Function: pop_stat
336  *
337  * Purpose: Issue the STAT command to the server and return (in the
338  *      value parameters) the number of messages in the maildrop and
339  *      the total size of the maildrop.
340  *
341  * Return value: 0 on success, or non-zero with an error in pop_error
342  *      in failure.
343  *
344  * Side effects: On failure, may make further operations on the
345  *      connection impossible.
346  */
347 int
348 pop_stat (popserver server, int *count, int *size)
349 {
350   char *fromserver;
351
352   if (server->in_multi)
353     {
354       strcpy (pop_error, "In multi-line query in pop_stat");
355       return (-1);
356     }
357      
358   if (sendline (server, "STAT") || (! (fromserver = pop_getline (server))))
359     return (-1);
360
361   if (strncmp (fromserver, "+OK ", 4))
362     {
363       if (0 == strncmp (fromserver, "-ERR", 4))
364         {
365           strncpy (pop_error, fromserver, ERROR_MAX);
366         }
367       else
368         {
369           strcpy (pop_error,
370                   "Unexpected response from POP server in pop_stat");
371           pop_trash (server);
372         }
373       return (-1);
374     }
375
376   *count = atoi (&fromserver[4]);
377      
378   fromserver = strchr (&fromserver[4], ' ');
379   if (! fromserver)
380     {
381       strcpy (pop_error,
382               "Badly formatted response from server in pop_stat");
383       pop_trash (server);
384       return (-1);
385     }
386
387   *size = atoi (fromserver + 1);
388
389   return (0);
390 }
391
392 /*
393  * Function: pop_list
394  *
395  * Purpose: Performs the POP "list" command and returns (in value
396  *      parameters) two malloc'd zero-terminated arrays -- one of
397  *      message IDs, and a parallel one of sizes.
398  *
399  * Arguments:
400  *      server  The pop connection to talk to.
401  *      message The number of the one message about which to get
402  *              information, or 0 to get information about all
403  *              messages.
404  *
405  * Return value: 0 on success, non-zero with error in pop_error on
406  *      failure.
407  *
408  * Side effects: On failure, may make further operations on the
409  *      connection impossible.
410  */
411 int
412 pop_list (popserver server, int message, int **IDs, int **sizes)
413 {
414   int how_many, i;
415   char *fromserver;
416
417   if (server->in_multi)
418     {
419       strcpy (pop_error, "In multi-line query in pop_list");
420       return (-1);
421     }
422
423   if (message)
424     how_many = 1;
425   else
426     {
427       int count, size;
428       if (pop_stat (server, &count, &size))
429         return (-1);
430       how_many = count;
431     }
432
433   *IDs = (int *) malloc ((how_many + 1) * sizeof (int));
434   *sizes = (int *) malloc ((how_many + 1) * sizeof (int));
435   if (! (*IDs && *sizes))
436     {
437       strcpy (pop_error, "Out of memory in pop_list");
438       return (-1);
439     }
440
441   if (message)
442     {
443       sprintf (pop_error, "LIST %d", message);
444       if (sendline (server, pop_error))
445         {
446           free ((char *) *IDs);
447           free ((char *) *sizes);
448           return (-1);
449         }
450       if (! (fromserver = pop_getline (server)))
451         {
452           free ((char *) *IDs);
453           free ((char *) *sizes);
454           return (-1);
455         }
456       if (strncmp (fromserver, "+OK ", 4))
457         {
458           if (! strncmp (fromserver, "-ERR", 4))
459             strncpy (pop_error, fromserver, ERROR_MAX);
460           else
461             {
462               strcpy (pop_error,
463                       "Unexpected response from server in pop_list");
464               pop_trash (server);
465             }
466           free ((char *) *IDs);
467           free ((char *) *sizes);
468           return (-1);
469         }
470       (*IDs)[0] = atoi (&fromserver[4]);
471       fromserver = strchr (&fromserver[4], ' ');
472       if (! fromserver)
473         {
474           strcpy (pop_error,
475                   "Badly formatted response from server in pop_list");
476           pop_trash (server);
477           free ((char *) *IDs);
478           free ((char *) *sizes);
479           return (-1);
480         }
481       (*sizes)[0] = atoi (fromserver);
482       (*IDs)[1] = (*sizes)[1] = 0;
483       return (0);
484     }
485   else
486     {
487       if (pop_multi_first (server, "LIST", &fromserver))
488         {
489           free ((char *) *IDs);
490           free ((char *) *sizes);
491           return (-1);
492         }
493       for (i = 0; i < how_many; i++)
494         {
495           if (pop_multi_next (server, &fromserver))
496             {
497               free ((char *) *IDs);
498               free ((char *) *sizes);
499               return (-1);
500             }
501           (*IDs)[i] = atoi (fromserver);
502           fromserver = strchr (fromserver, ' ');
503           if (! fromserver)
504             {
505               strcpy (pop_error,
506                       "Badly formatted response from server in pop_list");
507               free ((char *) *IDs);
508               free ((char *) *sizes);
509               pop_trash (server);
510               return (-1);
511             }
512           (*sizes)[i] = atoi (fromserver);
513         }
514       if (pop_multi_next (server, &fromserver))
515         {
516           free ((char *) *IDs);
517           free ((char *) *sizes);
518           return (-1);
519         }
520       else if (fromserver)
521         {
522           strcpy (pop_error,
523                   "Too many response lines from server in pop_list");
524           free ((char *) *IDs);
525           free ((char *) *sizes);
526           return (-1);
527         }
528       (*IDs)[i] = (*sizes)[i] = 0;
529       return (0);
530     }
531 }
532
533 /*
534  * Function: pop_retrieve
535  *
536  * Purpose: Retrieve a specified message from the maildrop.
537  *
538  * Arguments:
539  *      server  The server to retrieve from.
540  *      message The message number to retrieve.
541  *      markfrom
542  *              If true, then mark the string "From " at the beginning
543  *              of lines with '>'.
544  * 
545  * Return value: A string pointing to the message, if successful, or
546  *      null with pop_error set if not.
547  *
548  * Side effects: May kill connection on error.
549  */
550 char *
551 pop_retrieve (popserver server, int message, int markfrom)
552 {
553   int *IDs, *sizes, bufsize, fromcount = 0, cp = 0;
554   char *ptr, *fromserver;
555   int ret;
556
557   if (server->in_multi)
558     {
559       strcpy (pop_error, "In multi-line query in pop_retrieve");
560       return (0);
561     }
562
563   if (pop_list (server, message, &IDs, &sizes))
564     return (0);
565
566   if (pop_retrieve_first (server, message, &fromserver))
567     {
568       return (0);
569     }
570
571   /*
572    * The "5" below is an arbitrary constant -- I assume that if
573    * there are "From" lines in the text to be marked, there
574    * probably won't be more than 5 of them.  If there are, I
575    * allocate more space for them below.
576    */
577   bufsize = sizes[0] + (markfrom ? 5 : 0);
578   ptr = (char *)malloc (bufsize);
579   free ((char *) IDs);
580   free ((char *) sizes);
581
582   if (! ptr)
583     {
584       strcpy (pop_error, "Out of memory in pop_retrieve");
585       pop_retrieve_flush (server);
586       return (0);
587     }
588
589   while (! (ret = pop_retrieve_next (server, &fromserver)))
590     {
591       int linesize;
592
593       if (! fromserver)
594         {
595           ptr[cp] = '\0';
596           return (ptr);
597         }
598       if (markfrom && fromserver[0] == 'F' && fromserver[1] == 'r' &&
599           fromserver[2] == 'o' && fromserver[3] == 'm' &&
600           fromserver[4] == ' ')
601         {
602           if (++fromcount == 5)
603             {
604               bufsize += 5;
605               ptr = (char *)realloc (ptr, bufsize);
606               if (! ptr)
607                 {
608                   strcpy (pop_error, "Out of memory in pop_retrieve");
609                   pop_retrieve_flush (server);
610                   return (0);
611                 }
612               fromcount = 0;
613             }
614           ptr[cp++] = '>';
615         }
616       linesize = strlen (fromserver);
617       memcpy (&ptr[cp], fromserver, linesize);
618       cp += linesize;
619       ptr[cp++] = '\n';
620     }
621
622   if (ret)
623     {
624       free (ptr);
625       /* return (0); */
626     }
627   /* This function used to fall off the end, but that doesn't make any sense */
628   return (0);
629 }     
630
631 int
632 pop_retrieve_first (popserver server, int message, char **response)
633 {
634   sprintf (pop_error, "RETR %d", message);
635   return (pop_multi_first (server, pop_error, response));
636 }
637
638 int
639 pop_retrieve_next (popserver server, char **line)
640 {
641   return (pop_multi_next (server, line));
642 }
643
644 int
645 pop_retrieve_flush (popserver server)
646 {
647   return (pop_multi_flush (server));
648 }
649
650 int
651 pop_top_first (popserver server, int message, int lines, char **response)
652 {
653   sprintf (pop_error, "TOP %d %d", message, lines);
654   return (pop_multi_first (server, pop_error, response));
655 }
656
657 int
658 pop_top_next (popserver server, char **line)
659 {
660   return (pop_multi_next (server, line));
661 }
662
663 int
664 pop_top_flush (popserver server)
665 {
666   return (pop_multi_flush (server));
667 }
668
669 int
670 pop_multi_first (popserver server, char *command, char **response)
671 {
672   if (server->in_multi)
673     {
674       strcpy (pop_error,
675               "Already in multi-line query in pop_multi_first");
676       return (-1);
677     }
678
679   if (sendline (server, command) || (! (*response = pop_getline (server))))
680     {
681       return (-1);
682     }
683
684   if (0 == strncmp (*response, "-ERR", 4))
685     {
686       strncpy (pop_error, *response, ERROR_MAX);
687       return (-1);
688     }
689   else if (0 == strncmp (*response, "+OK", 3))
690     {
691       for (*response += 3; **response == ' '; (*response)++) /* empty */;
692       server->in_multi = 1;
693       return (0);
694     }
695   else
696     {
697       strcpy (pop_error,
698               "Unexpected response from server in pop_multi_first");
699       return (-1);
700     }
701 }
702
703 int
704 pop_multi_next (popserver server, char **line)
705 {
706   char *fromserver;
707
708   if (! server->in_multi)
709     {
710       strcpy (pop_error, "Not in multi-line query in pop_multi_next");
711       return (-1);
712     }
713
714   fromserver = pop_getline (server);
715   if (! fromserver)
716     {
717       return (-1);
718     }
719
720   if (fromserver[0] == '.')
721     {
722       if (! fromserver[1])
723         {
724           *line = 0;
725           server->in_multi = 0;
726           return (0);
727         }
728       else
729         {
730           *line = fromserver + 1;
731           return (0);
732         }
733     }
734   else
735     {
736       *line = fromserver;
737       return (0);
738     }
739 }
740
741 int
742 pop_multi_flush (popserver server)
743 {
744   char *line;
745
746   if (! server->in_multi)
747     {
748       return (0);
749     }
750
751   while (! pop_multi_next (server, &line))
752     {
753       if (! line)
754         {
755           return (0);
756         }
757     }
758
759   return (-1);
760 }
761
762 /* Function: pop_delete
763  *
764  * Purpose: Delete a specified message.
765  *
766  * Arguments:
767  *      server  Server from which to delete the message.
768  *      message Message to delete.
769  *
770  * Return value: 0 on success, non-zero with error in pop_error
771  *      otherwise.
772  */
773 int
774 pop_delete (popserver server, int message)
775 {
776   if (server->in_multi)
777     {
778       strcpy (pop_error, "In multi-line query in pop_delete");
779       return (-1);
780     }
781
782   sprintf (pop_error, "DELE %d", message);
783
784   if (sendline (server, pop_error) || getok (server))
785     return (-1);
786
787   return (0);
788 }
789
790 /*
791  * Function: pop_noop
792  *
793  * Purpose: Send a noop command to the server.
794  *
795  * Argument:
796  *      server  The server to send to.
797  *
798  * Return value: 0 on success, non-zero with error in pop_error
799  *      otherwise.
800  *
801  * Side effects: Closes connection on error.
802  */
803 int
804 pop_noop (popserver server)
805 {
806   if (server->in_multi)
807     {
808       strcpy (pop_error, "In multi-line query in pop_noop");
809       return (-1);
810     }
811
812   if (sendline (server, "NOOP") || getok (server))
813     return (-1);
814
815   return (0);
816 }
817
818 /*
819  * Function: pop_last
820  *
821  * Purpose: Find out the highest seen message from the server.
822  *
823  * Arguments:
824  *      server  The server.
825  *
826  * Return value: If successful, the highest seen message, which is
827  *      greater than or equal to 0.  Otherwise, a negative number with
828  *      the error explained in pop_error.
829  *
830  * Side effects: Closes the connection on error.
831  */
832 int
833 pop_last (popserver server)
834 {
835   char *fromserver;
836      
837   if (server->in_multi)
838     {
839       strcpy (pop_error, "In multi-line query in pop_last");
840       return (-1);
841     }
842
843   if (sendline (server, "LAST"))
844     return (-1);
845
846   if (! (fromserver = pop_getline (server)))
847     return (-1);
848
849   if (! strncmp (fromserver, "-ERR", 4))
850     {
851       strncpy (pop_error, fromserver, ERROR_MAX);
852       return (-1);
853     }
854   else if (strncmp (fromserver, "+OK ", 4))
855     {
856       strcpy (pop_error, "Unexpected response from server in pop_last");
857       pop_trash (server);
858       return (-1);
859     }
860   else
861     {
862       return (atoi (&fromserver[4]));
863     }
864 }
865
866 /*
867  * Function: pop_reset
868  *
869  * Purpose: Reset the server to its initial connect state
870  *
871  * Arguments:
872  *      server  The server.
873  *
874  * Return value: 0 for success, non-0 with error in pop_error
875  *      otherwise.
876  *
877  * Side effects: Closes the connection on error.
878  */
879 int
880 pop_reset (popserver server)
881 {
882   if (pop_retrieve_flush (server))
883     {
884       return (-1);
885     }
886
887   if (sendline (server, "RSET") || getok (server))
888     return (-1);
889
890   return (0);
891 }
892
893 /*
894  * Function: pop_quit
895  *
896  * Purpose: Quit the connection to the server,
897  *
898  * Arguments:
899  *      server  The server to quit.
900  *
901  * Return value: 0 for success, non-zero otherwise with error in
902  *      pop_error.
903  *
904  * Side Effects: The popserver passed in is unusable after this
905  *      function is called, even if an error occurs.
906  */
907 int
908 pop_quit (popserver server)
909 {
910   int ret = 0;
911
912   if (server->file >= 0)
913     {
914       if (pop_retrieve_flush (server))
915         {
916           ret = -1;
917         }
918
919       if (sendline (server, "QUIT") || getok (server))
920         {
921           ret = -1;
922         }
923
924       CLOSESOCKET (server->file);
925     }
926
927   if (server->buffer)
928     free (server->buffer);
929   free ((char *) server);
930
931   return (ret);
932 }
933
934 #ifdef WIN32_NATIVE
935 static int have_winsock = 0;
936 #endif
937
938 /*
939  * Function: socket_connection
940  *
941  * Purpose: Opens the network connection with the mail host, without
942  *      doing any sort of I/O with it or anything.
943  *
944  * Arguments:
945  *      host    The host to which to connect.
946  *      flags   Option flags.
947  *      
948  * Return value: A file descriptor indicating the connection, or -1
949  *      indicating failure, in which case an error has been copied
950  *      into pop_error.
951  */
952 static int
953 socket_connection (char *host, int flags)
954 {
955   struct hostent *hostent;
956   struct servent *servent;
957   struct sockaddr_in addr;
958   char found_port = 0;
959   char *service;
960   int sock;
961 #ifdef KERBEROS
962 #ifdef KRB5
963   krb5_error_code rem;
964   krb5_ccache ccdef;
965   krb5_principal client, server;
966   krb5_error *err_ret;
967   register char *cp;
968 #else
969   KTEXT ticket;
970   MSG_DAT msg_data;
971   CREDENTIALS cred;
972   Key_schedule schedule;
973   int rem;
974 #endif /* KRB5 */
975 #endif /* KERBEROS */
976
977   int try_count = 0;
978
979 #ifdef WIN32_NATIVE
980   {
981     WSADATA winsockData;
982     if (WSAStartup (0x101, &winsockData) == 0)
983       have_winsock = 1;
984   }
985 #endif
986
987   do
988     {
989       hostent = gethostbyname (host);
990       try_count++;
991       if ((! hostent) 
992 #ifndef BROKEN_CYGWIN     
993           && ((h_errno != TRY_AGAIN) || (try_count == 5))
994 #endif
995           )
996         {
997           strcpy (pop_error, "Could not determine POP server's address");
998           return (-1);
999         }
1000     } while (! hostent);
1001
1002   memset (&addr, 0, sizeof (addr));
1003   addr.sin_family = AF_INET;
1004
1005 #ifdef KERBEROS
1006   service = (flags & POP_NO_KERBEROS) ? POP_SERVICE : KPOP_SERVICE;
1007 #else
1008   service = POP_SERVICE;
1009 #endif
1010
1011 #ifdef HESIOD
1012   if (! (flags & POP_NO_HESIOD))
1013     {
1014       servent = hes_getservbyname (service, "tcp");
1015       if (servent)
1016         {
1017           addr.sin_port = servent->s_port;
1018           found_port = 1;
1019         }
1020     }
1021 #endif
1022   if (! found_port)
1023     {
1024       servent = getservbyname (service, "tcp");
1025       if (servent)
1026         {
1027           addr.sin_port = servent->s_port;
1028         }
1029       else
1030         {
1031 #ifdef KERBEROS
1032           addr.sin_port = htons ((flags & POP_NO_KERBEROS) ?
1033                                 POP_PORT : KPOP_PORT);
1034 #else
1035           addr.sin_port = htons (POP_PORT);
1036 #endif
1037         }
1038     }
1039
1040 #define SOCKET_ERROR "Could not create socket for POP connection: "
1041
1042   sock = socket (PF_INET, SOCK_STREAM, 0);
1043   if (sock < 0)
1044     {
1045       strcpy (pop_error, SOCKET_ERROR);
1046       strncat (pop_error, strerror (errno),
1047                ERROR_MAX - sizeof (SOCKET_ERROR));
1048       return (-1);
1049           
1050     }
1051
1052   while (*hostent->h_addr_list)
1053     {
1054       memcpy (&addr.sin_addr, *hostent->h_addr_list, hostent->h_length);
1055       if (! connect (sock, (struct sockaddr *) &addr, sizeof (addr)))
1056         break;
1057       hostent->h_addr_list++;
1058     }
1059
1060 #define CONNECT_ERROR "Could not connect to POP server: "
1061      
1062   if (! *hostent->h_addr_list)
1063     {
1064       CLOSESOCKET (sock);
1065       strcpy (pop_error, CONNECT_ERROR);
1066       strncat (pop_error, strerror (errno),
1067                ERROR_MAX - sizeof (CONNECT_ERROR));
1068       return (-1);
1069           
1070     }
1071
1072 #ifdef KERBEROS
1073 #define KRB_ERROR "Kerberos error connecting to POP server: "
1074   if (! (flags & POP_NO_KERBEROS))
1075     {
1076 #ifdef KRB5
1077       krb5_init_ets ();
1078
1079       if (rem = krb5_cc_default (&ccdef))
1080         {
1081         krb5error:
1082           strcpy (pop_error, KRB_ERROR);
1083           strncat (pop_error, error_message (rem),
1084                    ERROR_MAX - sizeof(KRB_ERROR));
1085           CLOSESOCKET (sock);
1086           return (-1);
1087         }
1088
1089       if (rem = krb5_cc_get_principal (ccdef, &client))
1090         {
1091           goto krb5error;
1092         }
1093
1094       for (cp = hostent->h_name; *cp; cp++)
1095         {
1096           if (isupper (*cp))
1097             {
1098               *cp = tolower (*cp);
1099             }
1100         }
1101
1102       if (rem = krb5_sname_to_principal (hostent->h_name, POP_SERVICE,
1103                                          FALSE, &server))
1104         {
1105           goto krb5error;
1106         }
1107
1108       rem = krb5_sendauth ((krb5_pointer) &sock, "KPOPV1.0", client, server,
1109                           AP_OPTS_MUTUAL_REQUIRED,
1110                           0,    /* no checksum */
1111                           0,    /* no creds, use ccache instead */
1112                           ccdef,
1113                           0,    /* don't need seq # */
1114                           0,    /* don't need subsession key */
1115                           &err_ret,
1116                           0);   /* don't need reply */
1117       krb5_free_principal (server);
1118       if (rem)
1119         {
1120           if (err_ret && err_ret->text.length)
1121             {
1122               strcpy (pop_error, KRB_ERROR);
1123               strncat (pop_error, error_message (rem),
1124                        ERROR_MAX - sizeof (KRB_ERROR));
1125               strncat (pop_error, " [server says '",
1126                        ERROR_MAX - strlen (pop_error) - 1);
1127               strncat (pop_error, err_ret->text.data,
1128                        min (ERROR_MAX - strlen (pop_error) - 1,
1129                             err_ret->text.length));
1130               strncat (pop_error, "']",
1131                        ERROR_MAX - strlen (pop_error) - 1);
1132             }
1133           else
1134             {
1135               strcpy (pop_error, KRB_ERROR);
1136               strncat (pop_error, error_message (rem),
1137                        ERROR_MAX - sizeof (KRB_ERROR));
1138             }
1139           if (err_ret)
1140             krb5_free_error (err_ret);
1141
1142           CLOSESOCKET (sock);
1143           return (-1);
1144         }
1145 #else  /* ! KRB5 */       
1146       ticket = (KTEXT) malloc (sizeof (KTEXT_ST));
1147       rem = krb_sendauth (0L, sock, ticket, "pop", hostent->h_name,
1148                           (char *) krb_realmofhost (hostent->h_name),
1149                           (unsigned long) 0, &msg_data, &cred, schedule,
1150                           (struct sockaddr_in *) 0,
1151                           (struct sockaddr_in *) 0,
1152                           "KPOPV0.1");
1153       free ((char *) ticket);
1154       if (rem != KSUCCESS)
1155         {
1156           strcpy (pop_error, KRB_ERROR);
1157           strncat (pop_error, krb_err_txt[rem],
1158                    ERROR_MAX - sizeof (KRB_ERROR));
1159           CLOSESOCKET (sock);
1160           return (-1);
1161         }
1162 #endif /* KRB5 */
1163     }
1164 #endif /* KERBEROS */
1165
1166   return (sock);
1167 } /* socket_connection */
1168
1169 /*
1170  * Function: pop_getline
1171  *
1172  * Purpose: Get a line of text from the connection and return a
1173  *      pointer to it.  The carriage return and linefeed at the end of
1174  *      the line are stripped, but periods at the beginnings of lines
1175  *      are NOT dealt with in any special way.
1176  *
1177  * Arguments:
1178  *      server  The server from which to get the line of text.
1179  *
1180  * Returns: A non-null pointer if successful, or a null pointer on any
1181  *      error, with an error message copied into pop_error.
1182  *
1183  * Notes: The line returned is overwritten with each call to pop_getline.
1184  *
1185  * Side effects: Closes the connection on error.
1186  */
1187 static char *
1188 pop_getline (popserver server)
1189 {
1190 #define GETLINE_ERROR "Error reading from server: "
1191
1192   int ret;
1193   int search_offset = 0;
1194
1195   if (server->data)
1196     {
1197       char *cp = find_crlf (server->buffer + server->buffer_index);
1198       if (cp)
1199         {
1200           int found;
1201           int data_used;
1202
1203           found = server->buffer_index;
1204           data_used = (cp + 2) - server->buffer - found;
1205                
1206           *cp = '\0';           /* terminate the string to be returned */
1207           server->data -= data_used;
1208           server->buffer_index += data_used;
1209
1210           if (pop_debug)
1211             fprintf (stderr, "<<< %s\n", server->buffer + found);
1212           return (server->buffer + found);
1213         }
1214       else
1215         {
1216           memcpy (server->buffer,
1217                   server->buffer + server->buffer_index,
1218                   server->data);
1219           /* Record the fact that we've searched the data already in
1220              the buffer for a CRLF, so that when we search below, we
1221              don't have to search the same data twice.  There's a "-
1222              1" here to account for the fact that the last character
1223              of the data we have may be the CR of a CRLF pair, of
1224              which we haven't read the second half yet, so we may have
1225              to search it again when we read more data. */
1226           search_offset = server->data - 1;
1227           server->buffer_index = 0;
1228         }
1229     }
1230   else
1231     {
1232       server->buffer_index = 0;
1233     }
1234
1235   while (1)
1236     {
1237       /* There's a "- 1" here to leave room for the null that we put
1238          at the end of the read data below.  We put the null there so
1239          that find_crlf knows where to stop when we call it. */
1240       if (server->data == server->buffer_size - 1)
1241         {
1242           server->buffer_size += GETLINE_INCR;
1243           server->buffer = (char *)realloc (server->buffer, server->buffer_size);
1244           if (! server->buffer)
1245             {
1246               strcpy (pop_error, "Out of memory in pop_getline");
1247               pop_trash (server);
1248               return (0);
1249             }
1250         }
1251       ret = RECV (server->file, server->buffer + server->data,
1252                   server->buffer_size - server->data - 1, 0);
1253       if (ret < 0)
1254         {
1255           strcpy (pop_error, GETLINE_ERROR);
1256           strncat (pop_error, strerror (errno),
1257                    ERROR_MAX - sizeof (GETLINE_ERROR));
1258           pop_trash (server);
1259           return (0);
1260         }
1261       else if (ret == 0)
1262         {
1263           strcpy (pop_error, "Unexpected EOF from server in pop_getline");
1264           pop_trash (server);
1265           return (0);
1266         }
1267       else
1268         {
1269           char *cp;
1270           server->data += ret;
1271           server->buffer[server->data] = '\0';
1272                
1273           cp = find_crlf (server->buffer + search_offset);
1274           if (cp)
1275             {
1276               int data_used = (cp + 2) - server->buffer;
1277               *cp = '\0';
1278               server->data -= data_used;
1279               server->buffer_index = data_used;
1280
1281               if (pop_debug)
1282                 fprintf (stderr, "<<< %s\n", server->buffer);
1283               return (server->buffer);
1284             }
1285           search_offset += ret;
1286         }
1287     }
1288
1289   /* NOTREACHED */
1290 }
1291
1292 /*
1293  * Function: sendline
1294  *
1295  * Purpose: Sends a line of text to the POP server.  The line of text
1296  *      passed into this function should NOT have the carriage return
1297  *      and linefeed on the end of it.  Periods at beginnings of lines
1298  *      will NOT be treated specially by this function.
1299  *
1300  * Arguments:
1301  *      server  The server to which to send the text.
1302  *      line    The line of text to send.
1303  *
1304  * Return value: Upon successful completion, a value of 0 will be
1305  *      returned.  Otherwise, a non-zero value will be returned, and
1306  *      an error will be copied into pop_error.
1307  *
1308  * Side effects: Closes the connection on error.
1309  */
1310 static int
1311 sendline (popserver server, char *line)
1312 {
1313 #define SENDLINE_ERROR "Error writing to POP server: "
1314   int ret;
1315
1316   ret = fullwrite (server->file, line, strlen (line));
1317   if (ret >= 0)
1318     {                           /* 0 indicates that a blank line was written */
1319       ret = fullwrite (server->file, "\r\n", 2);
1320     }
1321
1322   if (ret < 0)
1323     {
1324       pop_trash (server);
1325       strcpy (pop_error, SENDLINE_ERROR);
1326       strncat (pop_error, strerror (errno),
1327                ERROR_MAX - sizeof (SENDLINE_ERROR));
1328       return (ret);
1329     }
1330
1331   if (pop_debug)
1332     fprintf (stderr, ">>> %s\n", line);
1333
1334   return (0);
1335 }
1336
1337 /*
1338  * Procedure: fullwrite
1339  *
1340  * Purpose: Just like write, but keeps trying until the entire string
1341  *      has been written.
1342  *
1343  * Return value: Same as write.  Pop_error is not set.
1344  */
1345 static int
1346 fullwrite (int fd, char *buf, int nbytes)
1347 {
1348   char *cp;
1349   int ret;
1350
1351   cp = buf;
1352   while ((ret = SEND (fd, cp, nbytes, 0)) > 0)
1353     {
1354       cp += ret;
1355       nbytes -= ret;
1356     }
1357
1358   return (ret);
1359 }
1360
1361 /*
1362  * Procedure getok
1363  *
1364  * Purpose: Reads a line from the server.  If the return indicator is
1365  *      positive, return with a zero exit status.  If not, return with
1366  *      a negative exit status.
1367  *
1368  * Arguments:
1369  *      server  The server to read from.
1370  * 
1371  * Returns: 0 for success, else for failure and puts error in pop_error.
1372  *
1373  * Side effects: On failure, may make the connection unusable.
1374  */
1375 static int
1376 getok (popserver server)
1377 {
1378   char *fromline;
1379
1380   if (! (fromline = pop_getline (server)))
1381     {
1382       return (-1);
1383     }
1384
1385   if (! strncmp (fromline, "+OK", 3))
1386     return (0);
1387   else if (! strncmp (fromline, "-ERR", 4))
1388     {
1389       strncpy (pop_error, fromline, ERROR_MAX);
1390       pop_error[ERROR_MAX-1] = '\0';
1391       return (-1);
1392     }
1393   else
1394     {
1395       strcpy (pop_error,
1396               "Unexpected response from server; expecting +OK or -ERR");
1397       pop_trash (server);
1398       return (-1);
1399     }
1400 }         
1401
1402 #if 0
1403 /*
1404  * Function: gettermination
1405  *
1406  * Purpose: Gets the next line and verifies that it is a termination
1407  *      line (nothing but a dot).
1408  *
1409  * Return value: 0 on success, non-zero with pop_error set on error.
1410  *
1411  * Side effects: Closes the connection on error.
1412  */
1413 static int
1414 gettermination (popserver server)
1415 {
1416   char *fromserver;
1417
1418   fromserver = pop_getline (server);
1419   if (! fromserver)
1420     return (-1);
1421
1422   if (strcmp (fromserver, "."))
1423     {
1424       strcpy (pop_error,
1425               "Unexpected response from server in gettermination");
1426       pop_trash (server);
1427       return (-1);
1428     }
1429
1430   return (0);
1431 }
1432 #endif
1433
1434 /*
1435  * Function pop_close
1436  *
1437  * Purpose: Close a pop connection, sending a "RSET" command to try to
1438  *      preserve any changes that were made and a "QUIT" command to
1439  *      try to get the server to quit, but ignoring any responses that
1440  *      are received.
1441  *
1442  * Side effects: The server is unusable after this function returns.
1443  *      Changes made to the maildrop since the session was started (or
1444  *      since the last pop_reset) may be lost.
1445  */
1446 void 
1447 pop_close (popserver server)
1448 {
1449   pop_trash (server);
1450   free ((char *) server);
1451
1452   return;
1453 }
1454
1455 /*
1456  * Function: pop_trash
1457  *
1458  * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
1459  *      memory associated with the server.  It is legal to call
1460  *      pop_close or pop_quit after this function has been called.
1461  */
1462 static void
1463 pop_trash (popserver server)
1464 {
1465   if (server->file >= 0)
1466     {
1467       /* avoid recursion; sendline can call pop_trash */
1468       if (server->trash_started)
1469         return;
1470       server->trash_started = 1;
1471
1472       sendline (server, "RSET");
1473       sendline (server, "QUIT");
1474
1475       CLOSESOCKET (server->file);
1476       server->file = -1;
1477       if (server->buffer)
1478         {
1479           free (server->buffer);
1480           server->buffer = 0;
1481         }
1482     }
1483
1484 #ifdef WIN32_NATIVE
1485   if (have_winsock)
1486     WSACleanup ();
1487 #endif
1488 }
1489
1490 /* Return a pointer to the first CRLF in IN_STRING,
1491    or 0 if it does not contain one.  */
1492
1493 static char *
1494 find_crlf (char *in_string)
1495 {
1496   while (1)
1497     {
1498       if (! *in_string)
1499         return (0);
1500       else if (*in_string == '\r')
1501         {
1502           if (*++in_string == '\n')
1503             return (in_string - 1);
1504         }
1505       else
1506         in_string++;
1507     }
1508   /* NOTREACHED */
1509 }
1510
1511 #endif /* MAIL_USE_POP */