(gnus-group-enter-directory): Virtual server definition
[emacs/old-mirror.git] / lib-src / pop.c
blob4bd1d98bebac1ab724dfd083e0951e460f2a37ec
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.
5 This file is part of GNU Emacs.
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.
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.
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. */
22 #ifdef HAVE_CONFIG_H
23 #define NO_SHORTNAMES /* Tell config not to load remap.h */
24 #include <../src/config.h>
25 #else
26 #define MAIL_USE_POP
27 #endif
29 #ifdef MAIL_USE_POP
31 #ifdef HAVE_CONFIG_H
32 /* Cancel these substitutions made in config.h */
33 #undef open
34 #undef read
35 #undef write
36 #undef close
37 #endif
39 #include <sys/types.h>
40 #ifdef WINDOWSNT
41 #include "ntlib.h"
42 #include <winsock.h>
43 #undef SOCKET_ERROR
44 #define RECV(s,buf,len,flags) recv(s,buf,len,flags)
45 #define SEND(s,buf,len,flags) send(s,buf,len,flags)
46 #define CLOSESOCKET(s) closesocket(s)
47 #else
48 #include <netinet/in.h>
49 #include <sys/socket.h>
50 #define RECV(s,buf,len,flags) read(s,buf,len)
51 #define SEND(s,buf,len,flags) write(s,buf,len)
52 #define CLOSESOCKET(s) close(s)
53 #endif
54 #include <pop.h>
56 #ifdef sun
57 #include <malloc.h>
58 #endif /* sun */
60 #ifdef HESIOD
61 #include <hesiod.h>
63 * It really shouldn't be necessary to put this declaration here, but
64 * the version of hesiod.h that Athena has installed in release 7.2
65 * doesn't declare this function; I don't know if the 7.3 version of
66 * hesiod.h does.
68 extern struct servent *hes_getservbyname (/* char *, char * */);
69 #endif
71 #include <pwd.h>
72 #include <netdb.h>
73 #include <errno.h>
74 #include <stdio.h>
76 #ifdef KERBEROS
77 #ifndef KRB5
78 #include <des.h>
79 #include <krb.h>
80 #else /* KRB5 */
81 #include <krb5/krb5.h>
82 #include <krb5/ext-proto.h>
83 #include <ctype.h>
84 #endif /* KRB5 */
85 #endif /* KERBEROS */
87 extern char *getenv (/* char * */);
88 extern char *getlogin (/* void */);
89 extern char *getpass (/* char * */);
90 extern char *strerror (/* int */);
91 extern char *index ();
93 #ifdef KERBEROS
94 #ifndef KRB5
95 extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *,
96 u_long, MSG_DAT *, CREDENTIALS *, Key_schedule,
97 struct sockaddr_in *, struct sockaddr_in *,
98 char * */);
99 extern char *krb_realmofhost (/* char * */);
100 #endif /* ! KRB5 */
101 #endif /* KERBEROS */
103 #ifndef WINDOWSNT
104 #if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H)
105 extern int h_errno;
106 #endif
107 #endif
109 static int socket_connection (/* char *, int */);
110 static char *getline (/* popserver */);
111 static int sendline (/* popserver, char * */);
112 static int fullwrite (/* int, char *, int */);
113 static int getok (/* popserver */);
114 #if 0
115 static int gettermination (/* popserver */);
116 #endif
117 static void pop_trash (/* popserver */);
118 static char *find_crlf (/* char * */);
120 #define ERROR_MAX 80 /* a pretty arbitrary size */
121 #define POP_PORT 110
122 #define KPOP_PORT 1109
123 #ifdef WINDOWSNT
124 #define POP_SERVICE "pop3" /* we don't want the POP2 port! */
125 #else
126 #define POP_SERVICE "pop"
127 #endif
128 #ifdef KERBEROS
129 #ifdef KRB5
130 #define KPOP_SERVICE "k5pop";
131 #else
132 #define KPOP_SERVICE "kpop"
133 #endif
134 #endif
136 char pop_error[ERROR_MAX];
137 int pop_debug = 0;
139 #ifndef min
140 #define min(a,b) (((a) < (b)) ? (a) : (b))
141 #endif
144 * Function: pop_open (char *host, char *username, char *password,
145 * int flags)
147 * Purpose: Establishes a connection with a post-office server, and
148 * completes the authorization portion of the session.
150 * Arguments:
151 * host The server host with which the connection should be
152 * established. Optional. If omitted, internal
153 * heuristics will be used to determine the server host,
154 * if possible.
155 * username
156 * The username of the mail-drop to access. Optional.
157 * If omitted, internal heuristics will be used to
158 * determine the username, if possible.
159 * password
160 * The password to use for authorization. If omitted,
161 * internal heuristics will be used to determine the
162 * password, if possible.
163 * flags A bit mask containing flags controlling certain
164 * functions of the routine. Valid flags are defined in
165 * the file pop.h
167 * Return value: Upon successful establishment of a connection, a
168 * non-null popserver will be returned. Otherwise, null will be
169 * returned, and the string variable pop_error will contain an
170 * explanation of the error.
172 popserver
173 pop_open (host, username, password, flags)
174 char *host;
175 char *username;
176 char *password;
177 int flags;
179 int sock;
180 popserver server;
182 /* Determine the user name */
183 if (! username)
185 username = getenv ("USER");
186 if (! (username && *username))
188 username = getlogin ();
189 if (! (username && *username))
191 struct passwd *passwd;
192 passwd = getpwuid (getuid ());
193 if (passwd && passwd->pw_name && *passwd->pw_name)
195 username = passwd->pw_name;
197 else
199 strcpy (pop_error, "Could not determine username");
200 return (0);
207 * Determine the mail host.
210 if (! host)
212 host = getenv ("MAILHOST");
215 #ifdef HESIOD
216 if ((! host) && (! (flags & POP_NO_HESIOD)))
218 struct hes_postoffice *office;
219 office = hes_getmailhost (username);
220 if (office && office->po_type && (! strcmp (office->po_type, "POP"))
221 && office->po_name && *office->po_name && office->po_host
222 && *office->po_host)
224 host = office->po_host;
225 username = office->po_name;
228 #endif
230 #ifdef MAILHOST
231 if (! host)
233 host = MAILHOST;
235 #endif
237 if (! host)
239 strcpy (pop_error, "Could not determine POP server");
240 return (0);
243 /* Determine the password */
244 #ifdef KERBEROS
245 #define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS))
246 #else
247 #define DONT_NEED_PASSWORD 0
248 #endif
250 if ((! password) && (! DONT_NEED_PASSWORD))
252 if (! (flags & POP_NO_GETPASS))
254 password = getpass ("Enter POP password:");
256 if (! password)
258 strcpy (pop_error, "Could not determine POP password");
259 return (0);
262 if (password)
263 flags |= POP_NO_KERBEROS;
264 else
265 password = username;
267 sock = socket_connection (host, flags);
268 if (sock == -1)
269 return (0);
271 server = (popserver) malloc (sizeof (struct _popserver));
272 if (! server)
274 strcpy (pop_error, "Out of memory in pop_open");
275 return (0);
277 server->buffer = (char *) malloc (GETLINE_MIN);
278 if (! server->buffer)
280 strcpy (pop_error, "Out of memory in pop_open");
281 free ((char *) server);
282 return (0);
285 server->file = sock;
286 server->data = 0;
287 server->buffer_index = 0;
288 server->buffer_size = GETLINE_MIN;
289 server->in_multi = 0;
290 server->trash_started = 0;
292 if (getok (server))
293 return (0);
296 * I really shouldn't use the pop_error variable like this, but....
298 if (strlen (username) > ERROR_MAX - 6)
300 pop_close (server);
301 strcpy (pop_error,
302 "Username too long; recompile pop.c with larger ERROR_MAX");
303 return (0);
305 sprintf (pop_error, "USER %s", username);
307 if (sendline (server, pop_error) || getok (server))
309 return (0);
312 if (strlen (password) > ERROR_MAX - 6)
314 pop_close (server);
315 strcpy (pop_error,
316 "Password too long; recompile pop.c with larger ERROR_MAX");
317 return (0);
319 sprintf (pop_error, "PASS %s", password);
321 if (sendline (server, pop_error) || getok (server))
323 return (0);
326 return (server);
330 * Function: pop_stat
332 * Purpose: Issue the STAT command to the server and return (in the
333 * value parameters) the number of messages in the maildrop and
334 * the total size of the maildrop.
336 * Return value: 0 on success, or non-zero with an error in pop_error
337 * in failure.
339 * Side effects: On failure, may make further operations on the
340 * connection impossible.
343 pop_stat (server, count, size)
344 popserver server;
345 int *count;
346 int *size;
348 char *fromserver;
350 if (server->in_multi)
352 strcpy (pop_error, "In multi-line query in pop_stat");
353 return (-1);
356 if (sendline (server, "STAT") || (! (fromserver = getline (server))))
357 return (-1);
359 if (strncmp (fromserver, "+OK ", 4))
361 if (0 == strncmp (fromserver, "-ERR", 4))
363 strncpy (pop_error, fromserver, ERROR_MAX);
365 else
367 strcpy (pop_error,
368 "Unexpected response from POP server in pop_stat");
369 pop_trash (server);
371 return (-1);
374 *count = atoi (&fromserver[4]);
376 fromserver = index (&fromserver[4], ' ');
377 if (! fromserver)
379 strcpy (pop_error,
380 "Badly formatted response from server in pop_stat");
381 pop_trash (server);
382 return (-1);
385 *size = atoi (fromserver + 1);
387 return (0);
391 * Function: pop_list
393 * Purpose: Performs the POP "list" command and returns (in value
394 * parameters) two malloc'd zero-terminated arrays -- one of
395 * message IDs, and a parallel one of sizes.
397 * Arguments:
398 * server The pop connection to talk to.
399 * message The number of the one message about which to get
400 * information, or 0 to get information about all
401 * messages.
403 * Return value: 0 on success, non-zero with error in pop_error on
404 * failure.
406 * Side effects: On failure, may make further operations on the
407 * connection impossible.
410 pop_list (server, message, IDs, sizes)
411 popserver server;
412 int message;
413 int **IDs;
414 int **sizes;
416 int how_many, i;
417 char *fromserver;
419 if (server->in_multi)
421 strcpy (pop_error, "In multi-line query in pop_list");
422 return (-1);
425 if (message)
426 how_many = 1;
427 else
429 int count, size;
430 if (pop_stat (server, &count, &size))
431 return (-1);
432 how_many = count;
435 *IDs = (int *) malloc ((how_many + 1) * sizeof (int));
436 *sizes = (int *) malloc ((how_many + 1) * sizeof (int));
437 if (! (*IDs && *sizes))
439 strcpy (pop_error, "Out of memory in pop_list");
440 return (-1);
443 if (message)
445 sprintf (pop_error, "LIST %d", message);
446 if (sendline (server, pop_error))
448 free ((char *) *IDs);
449 free ((char *) *sizes);
450 return (-1);
452 if (! (fromserver = getline (server)))
454 free ((char *) *IDs);
455 free ((char *) *sizes);
456 return (-1);
458 if (strncmp (fromserver, "+OK ", 4))
460 if (! strncmp (fromserver, "-ERR", 4))
461 strncpy (pop_error, fromserver, ERROR_MAX);
462 else
464 strcpy (pop_error,
465 "Unexpected response from server in pop_list");
466 pop_trash (server);
468 free ((char *) *IDs);
469 free ((char *) *sizes);
470 return (-1);
472 (*IDs)[0] = atoi (&fromserver[4]);
473 fromserver = index (&fromserver[4], ' ');
474 if (! fromserver)
476 strcpy (pop_error,
477 "Badly formatted response from server in pop_list");
478 pop_trash (server);
479 free ((char *) *IDs);
480 free ((char *) *sizes);
481 return (-1);
483 (*sizes)[0] = atoi (fromserver);
484 (*IDs)[1] = (*sizes)[1] = 0;
485 return (0);
487 else
489 if (pop_multi_first (server, "LIST", &fromserver))
491 free ((char *) *IDs);
492 free ((char *) *sizes);
493 return (-1);
495 for (i = 0; i < how_many; i++)
497 if (pop_multi_next (server, &fromserver))
499 free ((char *) *IDs);
500 free ((char *) *sizes);
501 return (-1);
503 (*IDs)[i] = atoi (fromserver);
504 fromserver = index (fromserver, ' ');
505 if (! fromserver)
507 strcpy (pop_error,
508 "Badly formatted response from server in pop_list");
509 free ((char *) *IDs);
510 free ((char *) *sizes);
511 pop_trash (server);
512 return (-1);
514 (*sizes)[i] = atoi (fromserver);
516 if (pop_multi_next (server, &fromserver))
518 free ((char *) *IDs);
519 free ((char *) *sizes);
520 return (-1);
522 else if (fromserver)
524 strcpy (pop_error,
525 "Too many response lines from server in pop_list");
526 free ((char *) *IDs);
527 free ((char *) *sizes);
528 return (-1);
530 (*IDs)[i] = (*sizes)[i] = 0;
531 return (0);
536 * Function: pop_retrieve
538 * Purpose: Retrieve a specified message from the maildrop.
540 * Arguments:
541 * server The server to retrieve from.
542 * message The message number to retrieve.
543 * markfrom
544 * If true, then mark the string "From " at the beginning
545 * of lines with '>'.
547 * Return value: A string pointing to the message, if successful, or
548 * null with pop_error set if not.
550 * Side effects: May kill connection on error.
552 char *
553 pop_retrieve (server, message, markfrom)
554 popserver server;
555 int message;
556 int markfrom;
558 int *IDs, *sizes, bufsize, fromcount = 0, cp = 0;
559 char *ptr, *fromserver;
560 int ret;
562 if (server->in_multi)
564 strcpy (pop_error, "In multi-line query in pop_retrieve");
565 return (0);
568 if (pop_list (server, message, &IDs, &sizes))
569 return (0);
571 if (pop_retrieve_first (server, message, &fromserver))
573 return (0);
577 * The "5" below is an arbitrary constant -- I assume that if
578 * there are "From" lines in the text to be marked, there
579 * probably won't be more than 5 of them. If there are, I
580 * allocate more space for them below.
582 bufsize = sizes[0] + (markfrom ? 5 : 0);
583 ptr = (char *)malloc (bufsize);
584 free ((char *) IDs);
585 free ((char *) sizes);
587 if (! ptr)
589 strcpy (pop_error, "Out of memory in pop_retrieve");
590 pop_retrieve_flush (server);
591 return (0);
594 while (! (ret = pop_retrieve_next (server, &fromserver)))
596 int linesize;
598 if (! fromserver)
600 ptr[cp] = '\0';
601 return (ptr);
603 if (markfrom && fromserver[0] == 'F' && fromserver[1] == 'r' &&
604 fromserver[2] == 'o' && fromserver[3] == 'm' &&
605 fromserver[4] == ' ')
607 if (++fromcount == 5)
609 bufsize += 5;
610 ptr = (char *)realloc (ptr, bufsize);
611 if (! ptr)
613 strcpy (pop_error, "Out of memory in pop_retrieve");
614 pop_retrieve_flush (server);
615 return (0);
617 fromcount = 0;
619 ptr[cp++] = '>';
621 linesize = strlen (fromserver);
622 bcopy (fromserver, &ptr[cp], linesize);
623 cp += linesize;
624 ptr[cp++] = '\n';
627 if (ret)
629 free (ptr);
630 return (0);
635 pop_retrieve_first (server, message, response)
636 popserver server;
637 int message;
638 char **response;
640 sprintf (pop_error, "RETR %d", message);
641 return (pop_multi_first (server, pop_error, response));
645 pop_retrieve_next (server, line)
646 popserver server;
647 char **line;
649 return (pop_multi_next (server, line));
653 pop_retrieve_flush (server)
654 popserver server;
656 return (pop_multi_flush (server));
660 pop_top_first (server, message, lines, response)
661 popserver server;
662 int message, lines;
663 char **response;
665 sprintf (pop_error, "TOP %d %d", message, lines);
666 return (pop_multi_first (server, pop_error, response));
670 pop_top_next (server, line)
671 popserver server;
672 char **line;
674 return (pop_multi_next (server, line));
678 pop_top_flush (server)
679 popserver server;
681 return (pop_multi_flush (server));
685 pop_multi_first (server, command, response)
686 popserver server;
687 char *command;
688 char **response;
690 if (server->in_multi)
692 strcpy (pop_error,
693 "Already in multi-line query in pop_multi_first");
694 return (-1);
697 if (sendline (server, command) || (! (*response = getline (server))))
699 return (-1);
702 if (0 == strncmp (*response, "-ERR", 4))
704 strncpy (pop_error, *response, ERROR_MAX);
705 return (-1);
707 else if (0 == strncmp (*response, "+OK", 3))
709 for (*response += 3; **response == ' '; (*response)++) /* empty */;
710 server->in_multi = 1;
711 return (0);
713 else
715 strcpy (pop_error,
716 "Unexpected response from server in pop_multi_first");
717 return (-1);
722 pop_multi_next (server, line)
723 popserver server;
724 char **line;
726 char *fromserver;
728 if (! server->in_multi)
730 strcpy (pop_error, "Not in multi-line query in pop_multi_next");
731 return (-1);
734 fromserver = getline (server);
735 if (! fromserver)
737 return (-1);
740 if (fromserver[0] == '.')
742 if (! fromserver[1])
744 *line = 0;
745 server->in_multi = 0;
746 return (0);
748 else
750 *line = fromserver + 1;
751 return (0);
754 else
756 *line = fromserver;
757 return (0);
762 pop_multi_flush (server)
763 popserver server;
765 char *line;
767 if (! server->in_multi)
769 return (0);
772 while (! pop_multi_next (server, &line))
774 if (! line)
776 return (0);
780 return (-1);
783 /* Function: pop_delete
785 * Purpose: Delete a specified message.
787 * Arguments:
788 * server Server from which to delete the message.
789 * message Message to delete.
791 * Return value: 0 on success, non-zero with error in pop_error
792 * otherwise.
795 pop_delete (server, message)
796 popserver server;
797 int message;
799 if (server->in_multi)
801 strcpy (pop_error, "In multi-line query in pop_delete");
802 return (-1);
805 sprintf (pop_error, "DELE %d", message);
807 if (sendline (server, pop_error) || getok (server))
808 return (-1);
810 return (0);
814 * Function: pop_noop
816 * Purpose: Send a noop command to the server.
818 * Argument:
819 * server The server to send to.
821 * Return value: 0 on success, non-zero with error in pop_error
822 * otherwise.
824 * Side effects: Closes connection on error.
827 pop_noop (server)
828 popserver server;
830 if (server->in_multi)
832 strcpy (pop_error, "In multi-line query in pop_noop");
833 return (-1);
836 if (sendline (server, "NOOP") || getok (server))
837 return (-1);
839 return (0);
843 * Function: pop_last
845 * Purpose: Find out the highest seen message from the server.
847 * Arguments:
848 * server The server.
850 * Return value: If successful, the highest seen message, which is
851 * greater than or equal to 0. Otherwise, a negative number with
852 * the error explained in pop_error.
854 * Side effects: Closes the connection on error.
857 pop_last (server)
858 popserver server;
860 char *fromserver;
862 if (server->in_multi)
864 strcpy (pop_error, "In multi-line query in pop_last");
865 return (-1);
868 if (sendline (server, "LAST"))
869 return (-1);
871 if (! (fromserver = getline (server)))
872 return (-1);
874 if (! strncmp (fromserver, "-ERR", 4))
876 strncpy (pop_error, fromserver, ERROR_MAX);
877 return (-1);
879 else if (strncmp (fromserver, "+OK ", 4))
881 strcpy (pop_error, "Unexpected response from server in pop_last");
882 pop_trash (server);
883 return (-1);
885 else
887 return (atoi (&fromserver[4]));
892 * Function: pop_reset
894 * Purpose: Reset the server to its initial connect state
896 * Arguments:
897 * server The server.
899 * Return value: 0 for success, non-0 with error in pop_error
900 * otherwise.
902 * Side effects: Closes the connection on error.
905 pop_reset (server)
906 popserver server;
908 if (pop_retrieve_flush (server))
910 return (-1);
913 if (sendline (server, "RSET") || getok (server))
914 return (-1);
916 return (0);
920 * Function: pop_quit
922 * Purpose: Quit the connection to the server,
924 * Arguments:
925 * server The server to quit.
927 * Return value: 0 for success, non-zero otherwise with error in
928 * pop_error.
930 * Side Effects: The popserver passed in is unusable after this
931 * function is called, even if an error occurs.
934 pop_quit (server)
935 popserver server;
937 int ret = 0;
939 if (server->file >= 0)
941 if (pop_retrieve_flush (server))
943 ret = -1;
946 if (sendline (server, "QUIT") || getok (server))
948 ret = -1;
951 close (server->file);
954 if (server->buffer)
955 free (server->buffer);
956 free ((char *) server);
958 return (ret);
961 #ifdef WINDOWSNT
962 static int have_winsock = 0;
963 #endif
966 * Function: socket_connection
968 * Purpose: Opens the network connection with the mail host, without
969 * doing any sort of I/O with it or anything.
971 * Arguments:
972 * host The host to which to connect.
973 * flags Option flags.
975 * Return value: A file descriptor indicating the connection, or -1
976 * indicating failure, in which case an error has been copied
977 * into pop_error.
979 static int
980 socket_connection (host, flags)
981 char *host;
982 int flags;
984 struct hostent *hostent;
985 struct servent *servent;
986 struct sockaddr_in addr;
987 char found_port = 0;
988 char *service;
989 int sock;
990 #ifdef KERBEROS
991 #ifdef KRB5
992 krb5_error_code rem;
993 krb5_ccache ccdef;
994 krb5_principal client, server;
995 krb5_error *err_ret;
996 register char *cp;
997 #else
998 KTEXT ticket;
999 MSG_DAT msg_data;
1000 CREDENTIALS cred;
1001 Key_schedule schedule;
1002 int rem;
1003 #endif /* KRB5 */
1004 #endif /* KERBEROS */
1006 int try_count = 0;
1008 #ifdef WINDOWSNT
1010 WSADATA winsockData;
1011 if (WSAStartup (0x101, &winsockData) == 0)
1012 have_winsock = 1;
1014 #endif
1018 hostent = gethostbyname (host);
1019 try_count++;
1020 if ((! hostent) && ((h_errno != TRY_AGAIN) || (try_count == 5)))
1022 strcpy (pop_error, "Could not determine POP server's address");
1023 return (-1);
1025 } while (! hostent);
1027 bzero ((char *) &addr, sizeof (addr));
1028 addr.sin_family = AF_INET;
1030 #ifdef KERBEROS
1031 service = (flags & POP_NO_KERBEROS) ? POP_SERVICE : KPOP_SERVICE;
1032 #else
1033 service = POP_SERVICE;
1034 #endif
1036 #ifdef HESIOD
1037 if (! (flags & POP_NO_HESIOD))
1039 servent = hes_getservbyname (service, "tcp");
1040 if (servent)
1042 addr.sin_port = servent->s_port;
1043 found_port = 1;
1046 #endif
1047 if (! found_port)
1049 servent = getservbyname (service, "tcp");
1050 if (servent)
1052 addr.sin_port = servent->s_port;
1054 else
1056 #ifdef KERBEROS
1057 addr.sin_port = htons ((flags & POP_NO_KERBEROS) ?
1058 POP_PORT : KPOP_PORT);
1059 #else
1060 addr.sin_port = htons (POP_PORT);
1061 #endif
1065 #define SOCKET_ERROR "Could not create socket for POP connection: "
1067 sock = socket (PF_INET, SOCK_STREAM, 0);
1068 if (sock < 0)
1070 strcpy (pop_error, SOCKET_ERROR);
1071 strncat (pop_error, strerror (errno),
1072 ERROR_MAX - sizeof (SOCKET_ERROR));
1073 return (-1);
1077 while (*hostent->h_addr_list)
1079 bcopy (*hostent->h_addr_list, (char *) &addr.sin_addr,
1080 hostent->h_length);
1081 if (! connect (sock, (struct sockaddr *) &addr, sizeof (addr)))
1082 break;
1083 hostent->h_addr_list++;
1086 #define CONNECT_ERROR "Could not connect to POP server: "
1088 if (! *hostent->h_addr_list)
1090 CLOSESOCKET (sock);
1091 strcpy (pop_error, CONNECT_ERROR);
1092 strncat (pop_error, strerror (errno),
1093 ERROR_MAX - sizeof (CONNECT_ERROR));
1094 return (-1);
1098 #ifdef KERBEROS
1099 #define KRB_ERROR "Kerberos error connecting to POP server: "
1100 if (! (flags & POP_NO_KERBEROS))
1102 #ifdef KRB5
1103 krb5_init_ets ();
1105 if (rem = krb5_cc_default (&ccdef))
1107 krb5error:
1108 strcpy (pop_error, KRB_ERROR);
1109 strncat (pop_error, error_message (rem),
1110 ERROR_MAX - sizeof(KRB_ERROR));
1111 CLOSESOCKET (sock);
1112 return (-1);
1115 if (rem = krb5_cc_get_principal (ccdef, &client))
1117 goto krb5error;
1120 for (cp = hostent->h_name; *cp; cp++)
1122 if (isupper (*cp))
1124 *cp = tolower (*cp);
1128 if (rem = krb5_sname_to_principal (hostent->h_name, POP_SERVICE,
1129 FALSE, &server))
1131 goto krb5error;
1134 rem = krb5_sendauth ((krb5_pointer) &sock, "KPOPV1.0", client, server,
1135 AP_OPTS_MUTUAL_REQUIRED,
1136 0, /* no checksum */
1137 0, /* no creds, use ccache instead */
1138 ccdef,
1139 0, /* don't need seq # */
1140 0, /* don't need subsession key */
1141 &err_ret,
1142 0); /* don't need reply */
1143 krb5_free_principal (server);
1144 if (rem)
1146 if (err_ret && err_ret->text.length)
1148 strcpy (pop_error, KRB_ERROR);
1149 strncat (pop_error, error_message (rem),
1150 ERROR_MAX - sizeof (KRB_ERROR));
1151 strncat (pop_error, " [server says '",
1152 ERROR_MAX - strlen (pop_error) - 1);
1153 strncat (pop_error, err_ret->text.data,
1154 min (ERROR_MAX - strlen (pop_error) - 1,
1155 err_ret->text.length));
1156 strncat (pop_error, "']",
1157 ERROR_MAX - strlen (pop_error) - 1);
1159 else
1161 strcpy (pop_error, KRB_ERROR);
1162 strncat (pop_error, error_message (rem),
1163 ERROR_MAX - sizeof (KRB_ERROR));
1165 if (err_ret)
1166 krb5_free_error (err_ret);
1168 CLOSESOCKET (sock);
1169 return (-1);
1171 #else /* ! KRB5 */
1172 ticket = (KTEXT) malloc (sizeof (KTEXT_ST));
1173 rem = krb_sendauth (0L, sock, ticket, "pop", hostent->h_name,
1174 (char *) krb_realmofhost (hostent->h_name),
1175 (unsigned long) 0, &msg_data, &cred, schedule,
1176 (struct sockaddr_in *) 0,
1177 (struct sockaddr_in *) 0,
1178 "KPOPV0.1");
1179 free ((char *) ticket);
1180 if (rem != KSUCCESS)
1182 strcpy (pop_error, KRB_ERROR);
1183 strncat (pop_error, krb_err_txt[rem],
1184 ERROR_MAX - sizeof (KRB_ERROR));
1185 CLOSESOCKET (sock);
1186 return (-1);
1188 #endif /* KRB5 */
1190 #endif /* KERBEROS */
1192 return (sock);
1193 } /* socket_connection */
1196 * Function: getline
1198 * Purpose: Get a line of text from the connection and return a
1199 * pointer to it. The carriage return and linefeed at the end of
1200 * the line are stripped, but periods at the beginnings of lines
1201 * are NOT dealt with in any special way.
1203 * Arguments:
1204 * server The server from which to get the line of text.
1206 * Returns: A non-null pointer if successful, or a null pointer on any
1207 * error, with an error message copied into pop_error.
1209 * Notes: The line returned is overwritten with each call to getline.
1211 * Side effects: Closes the connection on error.
1213 static char *
1214 getline (server)
1215 popserver server;
1217 #define GETLINE_ERROR "Error reading from server: "
1219 int ret;
1220 int search_offset = 0;
1222 if (server->data)
1224 char *cp = find_crlf (server->buffer + server->buffer_index);
1225 if (cp)
1227 int found;
1228 int data_used;
1230 found = server->buffer_index;
1231 data_used = (cp + 2) - server->buffer - found;
1233 *cp = '\0'; /* terminate the string to be returned */
1234 server->data -= data_used;
1235 server->buffer_index += data_used;
1237 if (pop_debug)
1238 fprintf (stderr, "<<< %s\n", server->buffer + found);
1239 return (server->buffer + found);
1241 else
1243 bcopy (server->buffer + server->buffer_index,
1244 server->buffer, server->data);
1245 /* Record the fact that we've searched the data already in
1246 the buffer for a CRLF, so that when we search below, we
1247 don't have to search the same data twice. There's a "-
1248 1" here to account for the fact that the last character
1249 of the data we have may be the CR of a CRLF pair, of
1250 which we haven't read the second half yet, so we may have
1251 to search it again when we read more data. */
1252 search_offset = server->data - 1;
1253 server->buffer_index = 0;
1256 else
1258 server->buffer_index = 0;
1261 while (1)
1263 /* There's a "- 1" here to leave room for the null that we put
1264 at the end of the read data below. We put the null there so
1265 that find_crlf knows where to stop when we call it. */
1266 if (server->data == server->buffer_size - 1)
1268 server->buffer_size += GETLINE_INCR;
1269 server->buffer = (char *)realloc (server->buffer, server->buffer_size);
1270 if (! server->buffer)
1272 strcpy (pop_error, "Out of memory in getline");
1273 pop_trash (server);
1274 return (0);
1277 ret = RECV (server->file, server->buffer + server->data,
1278 server->buffer_size - server->data - 1, 0);
1279 if (ret < 0)
1281 strcpy (pop_error, GETLINE_ERROR);
1282 strncat (pop_error, strerror (errno),
1283 ERROR_MAX - sizeof (GETLINE_ERROR));
1284 pop_trash (server);
1285 return (0);
1287 else if (ret == 0)
1289 strcpy (pop_error, "Unexpected EOF from server in getline");
1290 pop_trash (server);
1291 return (0);
1293 else
1295 char *cp;
1296 server->data += ret;
1297 server->buffer[server->data] = '\0';
1299 cp = find_crlf (server->buffer + search_offset);
1300 if (cp)
1302 int data_used = (cp + 2) - server->buffer;
1303 *cp = '\0';
1304 server->data -= data_used;
1305 server->buffer_index = data_used;
1307 if (pop_debug)
1308 fprintf (stderr, "<<< %s\n", server->buffer);
1309 return (server->buffer);
1311 search_offset += ret;
1315 /* NOTREACHED */
1319 * Function: sendline
1321 * Purpose: Sends a line of text to the POP server. The line of text
1322 * passed into this function should NOT have the carriage return
1323 * and linefeed on the end of it. Periods at beginnings of lines
1324 * will NOT be treated specially by this function.
1326 * Arguments:
1327 * server The server to which to send the text.
1328 * line The line of text to send.
1330 * Return value: Upon successful completion, a value of 0 will be
1331 * returned. Otherwise, a non-zero value will be returned, and
1332 * an error will be copied into pop_error.
1334 * Side effects: Closes the connection on error.
1336 static int
1337 sendline (server, line)
1338 popserver server;
1339 char *line;
1341 #define SENDLINE_ERROR "Error writing to POP server: "
1342 int ret;
1344 ret = fullwrite (server->file, line, strlen (line));
1345 if (ret >= 0)
1346 { /* 0 indicates that a blank line was written */
1347 ret = fullwrite (server->file, "\r\n", 2);
1350 if (ret < 0)
1352 pop_trash (server);
1353 strcpy (pop_error, SENDLINE_ERROR);
1354 strncat (pop_error, strerror (errno),
1355 ERROR_MAX - sizeof (SENDLINE_ERROR));
1356 return (ret);
1359 if (pop_debug)
1360 fprintf (stderr, ">>> %s\n", line);
1362 return (0);
1366 * Procedure: fullwrite
1368 * Purpose: Just like write, but keeps trying until the entire string
1369 * has been written.
1371 * Return value: Same as write. Pop_error is not set.
1373 static int
1374 fullwrite (fd, buf, nbytes)
1375 int fd;
1376 char *buf;
1377 int nbytes;
1379 char *cp;
1380 int ret;
1382 cp = buf;
1383 while ((ret = SEND (fd, cp, nbytes, 0)) > 0)
1385 cp += ret;
1386 nbytes -= ret;
1389 return (ret);
1393 * Procedure getok
1395 * Purpose: Reads a line from the server. If the return indicator is
1396 * positive, return with a zero exit status. If not, return with
1397 * a negative exit status.
1399 * Arguments:
1400 * server The server to read from.
1402 * Returns: 0 for success, else for failure and puts error in pop_error.
1404 * Side effects: On failure, may make the connection unusable.
1406 static int
1407 getok (server)
1408 popserver server;
1410 char *fromline;
1412 if (! (fromline = getline (server)))
1414 return (-1);
1417 if (! strncmp (fromline, "+OK", 3))
1418 return (0);
1419 else if (! strncmp (fromline, "-ERR", 4))
1421 strncpy (pop_error, fromline, ERROR_MAX);
1422 pop_error[ERROR_MAX-1] = '\0';
1423 return (-1);
1425 else
1427 strcpy (pop_error,
1428 "Unexpected response from server; expecting +OK or -ERR");
1429 pop_trash (server);
1430 return (-1);
1434 #if 0
1436 * Function: gettermination
1438 * Purpose: Gets the next line and verifies that it is a termination
1439 * line (nothing but a dot).
1441 * Return value: 0 on success, non-zero with pop_error set on error.
1443 * Side effects: Closes the connection on error.
1445 static int
1446 gettermination (server)
1447 popserver server;
1449 char *fromserver;
1451 fromserver = getline (server);
1452 if (! fromserver)
1453 return (-1);
1455 if (strcmp (fromserver, "."))
1457 strcpy (pop_error,
1458 "Unexpected response from server in gettermination");
1459 pop_trash (server);
1460 return (-1);
1463 return (0);
1465 #endif
1468 * Function pop_close
1470 * Purpose: Close a pop connection, sending a "RSET" command to try to
1471 * preserve any changes that were made and a "QUIT" command to
1472 * try to get the server to quit, but ignoring any responses that
1473 * are received.
1475 * Side effects: The server is unusable after this function returns.
1476 * Changes made to the maildrop since the session was started (or
1477 * since the last pop_reset) may be lost.
1479 void
1480 pop_close (server)
1481 popserver server;
1483 pop_trash (server);
1484 free ((char *) server);
1486 return;
1490 * Function: pop_trash
1492 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
1493 * memory associated with the server. It is legal to call
1494 * pop_close or pop_quit after this function has been called.
1496 static void
1497 pop_trash (server)
1498 popserver server;
1500 if (server->file >= 0)
1502 /* avoid recursion; sendline can call pop_trash */
1503 if (server->trash_started)
1504 return;
1505 server->trash_started = 1;
1507 sendline (server, "RSET");
1508 sendline (server, "QUIT");
1510 CLOSESOCKET (server->file);
1511 server->file = -1;
1512 if (server->buffer)
1514 free (server->buffer);
1515 server->buffer = 0;
1519 #ifdef WINDOWSNT
1520 if (have_winsock)
1521 WSACleanup ();
1522 #endif
1525 /* Return a pointer to the first CRLF in IN_STRING,
1526 or 0 if it does not contain one. */
1528 static char *
1529 find_crlf (in_string)
1530 char *in_string;
1532 while (1)
1534 if (! *in_string)
1535 return (0);
1536 else if (*in_string == '\r')
1538 if (*++in_string == '\n')
1539 return (in_string - 1);
1541 else
1542 in_string++;
1544 /* NOTREACHED */
1547 #endif /* MAIL_USE_POP */