1 /* pop.c: client routines for talking to a POP3-protocol post-office server
2 Copyright (C) 1991, 1993, 1996, 1997, 1999, 2002, 2003, 2004,
3 2005, 2006 Free Software Foundation, Inc.
4 Copyright (C) 2001 Ben Wing.
5 Written by Jonathan Kamens, jik@security.ov.com.
7 This file is part of GNU Emacs.
9 GNU Emacs is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GNU Emacs is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Emacs; see the file COPYING. If not, write to
21 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 /* Synched up with: FSF 22.0.50. */
27 #define NO_SHORTNAMES /* Tell config not to load remap.h */
28 #define DONT_ENCAPSULATE
36 #include <sys/types.h>
40 #define RECV(s,buf,len,flags) recv(s,buf,len,flags)
41 #define SEND(s,buf,len,flags) send(s,buf,len,flags)
42 #define CLOSESOCKET(s) closesocket(s)
44 #include <netinet/in.h>
45 #include <sys/socket.h>
46 #define RECV(s,buf,len,flags) read(s,buf,len)
47 #define SEND(s,buf,len,flags) write(s,buf,len)
48 #define CLOSESOCKET(s) close(s)
59 * It really shouldn't be necessary to put this declaration here, but
60 * the version of hesiod.h that Athena has installed in release 7.2
61 * doesn't declare this function; I don't know if the 7.3 version of
64 extern struct servent *hes_getservbyname (/* char *, char * */);
67 #include "../src/syspwd.h"
81 #include "../src/syswait.h"
83 #include "../src/systime.h"
93 #include <krb5/krb5.h>
94 #include <krb5/ext-proto.h>
101 extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *,
102 u_long, MSG_DAT *, CREDENTIALS *, Key_schedule,
103 struct sockaddr_in *, struct sockaddr_in *,
105 extern char *krb_realmofhost (/* char * */);
107 #endif /* KERBEROS */
110 #if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H)
115 static int socket_connection (char *, int);
116 static int pop_getline (popserver, char **);
117 static int sendline (popserver, char *);
118 static int fullwrite (int, char *, int);
119 static int getok (popserver);
121 static int gettermination (popserver);
123 static void pop_trash (popserver);
124 static char *find_crlf (char *, int);
126 #define ERROR_MAX 160 /* a pretty arbitrary size, but needs
127 to be bigger than the original
130 #define KPOP_PORT 1109
131 #if defined(WIN32_NATIVE) || defined(CYGWIN)
132 #define POP_SERVICE "pop3" /* we don't want the POP2 port! */
134 #define POP_SERVICE "pop"
138 #define KPOP_SERVICE "k5pop"
140 #define KPOP_SERVICE "kpop"
144 char pop_error[ERROR_MAX];
148 #define min(a,b) (((a) < (b)) ? (a) : (b))
152 * Function: pop_open (char *host, char *username, char *password,
155 * Purpose: Establishes a connection with a post-office server, and
156 * completes the authorization portion of the session.
159 * host The server host with which the connection should be
160 * established. Optional. If omitted, internal
161 * heuristics will be used to determine the server host,
164 * The username of the mail-drop to access. Optional.
165 * If omitted, internal heuristics will be used to
166 * determine the username, if possible.
168 * The password to use for authorization. If omitted,
169 * internal heuristics will be used to determine the
170 * password, if possible.
171 * flags A bit mask containing flags controlling certain
172 * functions of the routine. Valid flags are defined in
175 * Return value: Upon successful establishment of a connection, a
176 * non-null popserver will be returned. Otherwise, null will be
177 * returned, and the string variable pop_error will contain an
178 * explanation of the error.
181 pop_open (char *host, char *username, char *password, int flags)
186 /* Determine the user name */
189 username = getenv ("USER");
190 if (! (username && *username))
193 username = getlogin ();
194 if (! (username && *username))
196 struct passwd *passwd;
197 passwd = getpwuid (getuid ());
198 if (passwd && passwd->pw_name && *passwd->pw_name)
200 username = passwd->pw_name;
204 strcpy (pop_error, "Could not determine username");
209 strcpy (pop_error, "Could not determine username");
216 * Determine the mail host.
221 host = getenv ("MAILHOST");
225 if ((! host) && (! (flags & POP_NO_HESIOD)))
227 struct hes_postoffice *office;
228 office = hes_getmailhost (username);
229 if (office && office->po_type && (! strcmp (office->po_type, "POP"))
230 && office->po_name && *office->po_name && office->po_host
233 host = office->po_host;
234 username = office->po_name;
248 strcpy (pop_error, "Could not determine POP server");
252 /* Determine the password */
254 #define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS))
256 #define DONT_NEED_PASSWORD 0
259 if ((! password) && (! DONT_NEED_PASSWORD))
262 if (! (flags & POP_NO_GETPASS))
264 password = getpass ("Enter POP password:");
269 strcpy (pop_error, "Could not determine POP password");
274 flags |= POP_NO_KERBEROS;
278 sock = socket_connection (host, flags);
282 server = (popserver) malloc (sizeof (struct _popserver));
285 strcpy (pop_error, "Out of memory in pop_open");
288 server->buffer = (char *) malloc (GETLINE_MIN);
289 if (! server->buffer)
291 strcpy (pop_error, "Out of memory in pop_open");
292 free ((char *) server);
298 server->buffer_index = 0;
299 server->buffer_size = GETLINE_MIN;
300 server->in_multi = 0;
301 server->trash_started = 0;
307 * I really shouldn't use the pop_error variable like this, but....
309 if (strlen (username) > ERROR_MAX - 6)
313 "Username too long; recompile pop.c with larger ERROR_MAX");
316 sprintf (pop_error, "USER %s", username);
318 if (sendline (server, pop_error) || getok (server))
323 if (strlen (password) > ERROR_MAX - 6)
327 "Password too long; recompile pop.c with larger ERROR_MAX");
330 sprintf (pop_error, "PASS %s", password);
332 if (sendline (server, pop_error) || getok (server))
343 * Purpose: Issue the STAT command to the server and return (in the
344 * value parameters) the number of messages in the maildrop and
345 * the total size of the maildrop.
347 * Return value: 0 on success, or non-zero with an error in pop_error
350 * Side effects: On failure, may make further operations on the
351 * connection impossible.
354 pop_stat (popserver server, int *count, int *size)
358 if (server->in_multi)
360 strcpy (pop_error, "In multi-line query in pop_stat");
364 if (sendline (server, "STAT") || (pop_getline (server, &fromserver) < 0))
367 if (strncmp (fromserver, "+OK ", 4))
369 if (0 == strncmp (fromserver, "-ERR", 4))
371 strncpy (pop_error, fromserver, ERROR_MAX);
376 "Unexpected response from POP server in pop_stat");
382 *count = atoi (&fromserver[4]);
384 fromserver = strchr (&fromserver[4], ' ');
388 "Badly formatted response from server in pop_stat");
393 *size = atoi (fromserver + 1);
401 * Purpose: Performs the POP "list" command and returns (in value
402 * parameters) two malloc'd zero-terminated arrays -- one of
403 * message IDs, and a parallel one of sizes.
406 * server The pop connection to talk to.
407 * message The number of the one message about which to get
408 * information, or 0 to get information about all
411 * Return value: 0 on success, non-zero with error in pop_error on
414 * Side effects: On failure, may make further operations on the
415 * connection impossible.
418 pop_list (popserver server, int message, int **IDs, int **sizes)
423 if (server->in_multi)
425 strcpy (pop_error, "In multi-line query in pop_list");
434 if (pop_stat (server, &count, &size))
439 *IDs = (int *) malloc ((how_many + 1) * sizeof (int));
440 *sizes = (int *) malloc ((how_many + 1) * sizeof (int));
441 if (! (*IDs && *sizes))
443 strcpy (pop_error, "Out of memory in pop_list");
449 sprintf (pop_error, "LIST %d", message);
450 if (sendline (server, pop_error))
452 free ((char *) *IDs);
453 free ((char *) *sizes);
456 if (pop_getline (server, &fromserver) < 0)
458 free ((char *) *IDs);
459 free ((char *) *sizes);
462 if (strncmp (fromserver, "+OK ", 4))
464 if (! strncmp (fromserver, "-ERR", 4))
465 strncpy (pop_error, fromserver, ERROR_MAX);
469 "Unexpected response from server in pop_list");
472 free ((char *) *IDs);
473 free ((char *) *sizes);
476 (*IDs)[0] = atoi (&fromserver[4]);
477 fromserver = strchr (&fromserver[4], ' ');
481 "Badly formatted response from server in pop_list");
483 free ((char *) *IDs);
484 free ((char *) *sizes);
487 (*sizes)[0] = atoi (fromserver);
488 (*IDs)[1] = (*sizes)[1] = 0;
493 if (pop_multi_first (server, "LIST", &fromserver))
495 free ((char *) *IDs);
496 free ((char *) *sizes);
499 for (i = 0; i < how_many; i++)
501 if (pop_multi_next (server, &fromserver) <= 0)
503 free ((char *) *IDs);
504 free ((char *) *sizes);
507 (*IDs)[i] = atoi (fromserver);
508 fromserver = strchr (fromserver, ' ');
512 "Badly formatted response from server in pop_list");
513 free ((char *) *IDs);
514 free ((char *) *sizes);
518 (*sizes)[i] = atoi (fromserver);
520 if (pop_multi_next (server, &fromserver) < 0)
522 free ((char *) *IDs);
523 free ((char *) *sizes);
529 "Too many response lines from server in pop_list");
530 free ((char *) *IDs);
531 free ((char *) *sizes);
534 (*IDs)[i] = (*sizes)[i] = 0;
540 * Function: pop_retrieve
542 * Purpose: Retrieve a specified message from the maildrop.
545 * server The server to retrieve from.
546 * message The message number to retrieve.
548 * If true, then mark the string "From " at the beginning
550 * msg_buf Output parameter to which a buffer containing the
551 * message is assigned.
553 * Return value: The number of bytes in msg_buf, which may contain
554 * embedded nulls, not including its final null, or -1 on error
555 * with pop_error set.
557 * Side effects: May kill connection on error.
560 pop_retrieve (popserver server, int message, int markfrom, char **msg_buf)
562 int *IDs, *sizes, bufsize, fromcount = 0, cp = 0;
563 char *ptr, *fromserver;
566 if (server->in_multi)
568 strcpy (pop_error, "In multi-line query in pop_retrieve");
572 if (pop_list (server, message, &IDs, &sizes))
575 if (pop_retrieve_first (server, message, &fromserver))
581 * The "5" below is an arbitrary constant -- I assume that if
582 * there are "From" lines in the text to be marked, there
583 * probably won't be more than 5 of them. If there are, I
584 * allocate more space for them below.
586 bufsize = sizes[0] + (markfrom ? 5 : 0);
587 ptr = (char *)malloc (bufsize);
589 free ((char *) sizes);
593 strcpy (pop_error, "Out of memory in pop_retrieve");
594 pop_retrieve_flush (server);
598 while ((ret = pop_retrieve_next (server, &fromserver)) >= 0)
606 if (markfrom && fromserver[0] == 'F' && fromserver[1] == 'r' &&
607 fromserver[2] == 'o' && fromserver[3] == 'm' &&
608 fromserver[4] == ' ')
610 if (++fromcount == 5)
613 ptr = (char *)realloc (ptr, bufsize);
616 strcpy (pop_error, "Out of memory in pop_retrieve");
617 pop_retrieve_flush (server);
624 memcpy (&ptr[cp], fromserver, ret);
634 pop_retrieve_first (popserver server, int message, char **response)
636 sprintf (pop_error, "RETR %d", message);
637 return (pop_multi_first (server, pop_error, response));
641 Returns a negative number on error, 0 to indicate that the data has
642 all been read (i.e., the server has returned a "." termination
643 line), or a positive number indicating the number of bytes in the
644 returned buffer (which is null-terminated and may contain embedded
645 nulls, but the returned bytecount doesn't include the final null).
649 pop_retrieve_next (popserver server, char **line)
651 return (pop_multi_next (server, line));
655 pop_retrieve_flush (popserver server)
657 return (pop_multi_flush (server));
661 pop_top_first (popserver server, int message, int lines, char **response)
663 sprintf (pop_error, "TOP %d %d", message, lines);
664 return (pop_multi_first (server, pop_error, response));
668 Returns a negative number on error, 0 to indicate that the data has
669 all been read (i.e., the server has returned a "." termination
670 line), or a positive number indicating the number of bytes in the
671 returned buffer (which is null-terminated and may contain embedded
672 nulls, but the returned bytecount doesn't include the final null).
676 pop_top_next (popserver server, char **line)
678 return (pop_multi_next (server, line));
682 pop_top_flush (popserver server)
684 return (pop_multi_flush (server));
688 pop_multi_first (popserver server, char *command, char **response)
690 if (server->in_multi)
693 "Already in multi-line query in pop_multi_first");
697 if (sendline (server, command) || (pop_getline (server, response) < 0))
702 if (0 == strncmp (*response, "-ERR", 4))
704 strncpy (pop_error, *response, ERROR_MAX);
707 else if (0 == strncmp (*response, "+OK", 3))
709 for (*response += 3; **response == ' '; (*response)++) /* empty */;
710 server->in_multi = 1;
716 "Unexpected response from server in pop_multi_first");
722 Read the next line of data from SERVER and place a pointer to it
723 into LINE. Return -1 on error, 0 if there are no more lines to read
724 (i.e., the server has returned a line containing only "."), or a
725 positive number indicating the number of bytes in the LINE buffer
726 (not including the final null). The data in that buffer may contain
727 embedded nulls, but does not contain the final CRLF. When returning
728 0, LINE is set to null. */
731 pop_multi_next (popserver server, char **line)
736 if (! server->in_multi)
738 strcpy (pop_error, "Not in multi-line query in pop_multi_next");
742 if ((ret = pop_getline (server, &fromserver)) < 0)
747 if (fromserver[0] == '.')
752 server->in_multi = 0;
757 *line = fromserver + 1;
769 pop_multi_flush (popserver server)
774 if (! server->in_multi)
779 while ((ret = pop_multi_next (server, &line)))
790 /* Function: pop_delete
792 * Purpose: Delete a specified message.
795 * server Server from which to delete the message.
796 * message Message to delete.
798 * Return value: 0 on success, non-zero with error in pop_error
802 pop_delete (popserver server, int message)
804 if (server->in_multi)
806 strcpy (pop_error, "In multi-line query in pop_delete");
810 sprintf (pop_error, "DELE %d", message);
812 if (sendline (server, pop_error) || getok (server))
821 * Purpose: Send a noop command to the server.
824 * server The server to send to.
826 * Return value: 0 on success, non-zero with error in pop_error
829 * Side effects: Closes connection on error.
832 pop_noop (popserver server)
834 if (server->in_multi)
836 strcpy (pop_error, "In multi-line query in pop_noop");
840 if (sendline (server, "NOOP") || getok (server))
849 * Purpose: Find out the highest seen message from the server.
854 * Return value: If successful, the highest seen message, which is
855 * greater than or equal to 0. Otherwise, a negative number with
856 * the error explained in pop_error.
858 * Side effects: Closes the connection on error.
861 pop_last (popserver server)
865 if (server->in_multi)
867 strcpy (pop_error, "In multi-line query in pop_last");
871 if (sendline (server, "LAST"))
874 if (pop_getline (server, &fromserver) < 0)
877 if (! strncmp (fromserver, "-ERR", 4))
879 strncpy (pop_error, fromserver, ERROR_MAX);
882 else if (strncmp (fromserver, "+OK ", 4))
884 strcpy (pop_error, "Unexpected response from server in pop_last");
890 return (atoi (&fromserver[4]));
895 * Function: pop_reset
897 * Purpose: Reset the server to its initial connect state
902 * Return value: 0 for success, non-0 with error in pop_error
905 * Side effects: Closes the connection on error.
908 pop_reset (popserver server)
910 if (pop_retrieve_flush (server))
915 if (sendline (server, "RSET") || getok (server))
924 * Purpose: Quit the connection to the server,
927 * server The server to quit.
929 * Return value: 0 for success, non-zero otherwise with error in
932 * Side Effects: The popserver passed in is unusable after this
933 * function is called, even if an error occurs.
936 pop_quit (popserver server)
940 if (server->file >= 0)
942 if (pop_retrieve_flush (server))
947 if (sendline (server, "QUIT") || getok (server))
952 CLOSESOCKET (server->file);
956 free (server->buffer);
957 free ((char *) server);
963 static int have_winsock = 0;
967 * Function: socket_connection
969 * Purpose: Opens the network connection with the mail host, without
970 * doing any sort of I/O with it or anything.
973 * host The host to which to connect.
974 * flags Option flags.
976 * Return value: A file descriptor indicating the connection, or -1
977 * indicating failure, in which case an error has been copied
981 socket_connection (char *host, int flags)
983 struct hostent *hostent;
984 struct servent *servent;
985 struct sockaddr_in addr;
992 krb5_context kcontext = 0;
993 krb5_auth_context auth_context = 0;
995 krb5_principal client, server;
1002 Key_schedule schedule;
1006 #endif /* KERBEROS */
1012 WSADATA winsockData;
1013 if (WSAStartup (0x101, &winsockData) == 0)
1018 memset (&addr, 0, sizeof (addr));
1019 addr.sin_family = AF_INET;
1022 service = (flags & POP_NO_KERBEROS) ? POP_SERVICE : KPOP_SERVICE;
1024 service = POP_SERVICE;
1028 if (! (flags & POP_NO_HESIOD))
1030 servent = hes_getservbyname (service, "tcp");
1033 addr.sin_port = servent->s_port;
1040 servent = getservbyname (service, "tcp");
1043 addr.sin_port = servent->s_port;
1048 addr.sin_port = htons ((flags & POP_NO_KERBEROS) ?
1049 POP_PORT : KPOP_PORT);
1051 addr.sin_port = htons (POP_PORT);
1056 #define POP_SOCKET_ERROR "Could not create socket for POP connection: "
1058 sock = socket (PF_INET, SOCK_STREAM, 0);
1061 strcpy (pop_error, POP_SOCKET_ERROR);
1062 strncat (pop_error, strerror (errno),
1063 ERROR_MAX - sizeof (POP_SOCKET_ERROR));
1070 hostent = gethostbyname (host);
1073 #ifndef BROKEN_CYGWIN
1074 && ((h_errno != TRY_AGAIN) || (try_count == 5))
1078 strcpy (pop_error, "Could not determine POP server's address");
1081 } while (! hostent);
1083 while (*hostent->h_addr_list)
1085 memcpy (&addr.sin_addr, *hostent->h_addr_list, hostent->h_length);
1086 if (! connect (sock, (struct sockaddr *) &addr, sizeof (addr)))
1088 hostent->h_addr_list++;
1091 #define CONNECT_ERROR "Could not connect to POP server: "
1093 if (! *hostent->h_addr_list)
1096 strcpy (pop_error, CONNECT_ERROR);
1097 strncat (pop_error, strerror (errno),
1098 ERROR_MAX - sizeof (CONNECT_ERROR));
1104 #define KRB_ERROR "Kerberos error connecting to POP server: "
1105 if (! (flags & POP_NO_KERBEROS))
1108 if ((rem = krb5_init_context (&kcontext)))
1112 krb5_auth_con_free (kcontext, auth_context);
1114 krb5_free_context (kcontext);
1115 strcpy (pop_error, KRB_ERROR);
1116 strncat (pop_error, error_message (rem),
1117 ERROR_MAX - sizeof(KRB_ERROR));
1122 if ((rem = krb5_auth_con_init (kcontext, &auth_context)))
1125 if (rem = krb5_cc_default (kcontext, &ccdef))
1128 if (rem = krb5_cc_get_principal (kcontext, ccdef, &client))
1131 for (cp = hostent->h_name; *cp; cp++)
1135 *cp = tolower (*cp);
1139 if (rem = krb5_sname_to_principal (kcontext, hostent->h_name,
1140 POP_SERVICE, FALSE, &server))
1143 rem = krb5_sendauth (kcontext, &auth_context,
1144 (krb5_pointer) &sock, "KPOPV1.0", client, server,
1145 AP_OPTS_MUTUAL_REQUIRED,
1146 0, /* no checksum */
1147 0, /* no creds, use ccache instead */
1150 0, /* don't need subsession key */
1151 0); /* don't need reply */
1152 krb5_free_principal (kcontext, server);
1155 if (err_ret && err_ret->text.length)
1157 strcpy (pop_error, KRB_ERROR);
1158 strncat (pop_error, error_message (rem),
1159 ERROR_MAX - sizeof (KRB_ERROR));
1160 strncat (pop_error, " [server says '",
1161 ERROR_MAX - strlen (pop_error) - 1);
1162 strncat (pop_error, err_ret->text.data,
1163 min (ERROR_MAX - strlen (pop_error) - 1,
1164 err_ret->text.length));
1165 strncat (pop_error, "']",
1166 ERROR_MAX - strlen (pop_error) - 1);
1170 strcpy (pop_error, KRB_ERROR);
1171 strncat (pop_error, error_message (rem),
1172 ERROR_MAX - sizeof (KRB_ERROR));
1175 krb5_free_error (kcontext, err_ret);
1176 krb5_auth_con_free (kcontext, auth_context);
1177 krb5_free_context (kcontext);
1183 ticket = (KTEXT) malloc (sizeof (KTEXT_ST));
1184 rem = krb_sendauth (0L, sock, ticket, "pop", realhost,
1185 (char *) krb_realmofhost (realhost),
1186 (unsigned long) 0, &msg_data, &cred, schedule,
1187 (struct sockaddr_in *) 0,
1188 (struct sockaddr_in *) 0,
1190 free ((char *) ticket);
1192 if (rem != KSUCCESS)
1194 strcpy (pop_error, KRB_ERROR);
1195 strncat (pop_error, krb_err_txt[rem],
1196 ERROR_MAX - sizeof (KRB_ERROR));
1202 #endif /* KERBEROS */
1205 } /* socket_connection */
1208 * Function: pop_getline
1210 * Purpose: Get a line of text from the connection and return a
1211 * pointer to it. The carriage return and linefeed at the end of
1212 * the line are stripped, but periods at the beginnings of lines
1213 * are NOT dealt with in any special way.
1216 * server The server from which to get the line of text.
1218 * Returns: The number of characters in the line, which is returned in
1219 * LINE, not including the final null. A return value of 0
1220 * indicates a blank line. A negative return value indicates an
1221 * error (in which case the contents of LINE are undefined. In
1222 * case of error, an error message is copied into pop_error.
1224 * Notes: The line returned is overwritten with each call to pop_getline.
1226 * Side effects: Closes the connection on error.
1228 * THE RETURNED LINE MAY CONTAIN EMBEDDED NULLS!
1231 pop_getline (popserver server, char **line)
1233 #define GETLINE_ERROR "Error reading from server: "
1236 int search_offset = 0;
1240 char *cp = find_crlf (server->buffer + server->buffer_index,
1247 found = server->buffer_index;
1248 data_used = (cp + 2) - server->buffer - found;
1250 *cp = '\0'; /* terminate the string to be returned */
1251 server->data -= data_used;
1252 server->buffer_index += data_used;
1255 /* Embedded nulls will truncate this output prematurely,
1256 but that's OK because it's just for debugging anyway. */
1257 fprintf (stderr, "<<< %s\n", server->buffer + found);
1258 *line = server->buffer + found;
1259 return (data_used - 2);
1263 memcpy (server->buffer,
1264 server->buffer + server->buffer_index,
1266 /* Record the fact that we've searched the data already in
1267 the buffer for a CRLF, so that when we search below, we
1268 don't have to search the same data twice. There's a "-
1269 1" here to account for the fact that the last character
1270 of the data we have may be the CR of a CRLF pair, of
1271 which we haven't read the second half yet, so we may have
1272 to search it again when we read more data. */
1273 search_offset = server->data - 1;
1274 server->buffer_index = 0;
1279 server->buffer_index = 0;
1284 /* There's a "- 1" here to leave room for the null that we put
1285 at the end of the read data below. We put the null there so
1286 that find_crlf knows where to stop when we call it. */
1287 if (server->data == server->buffer_size - 1)
1289 server->buffer_size += GETLINE_INCR;
1290 server->buffer = (char *)realloc (server->buffer, server->buffer_size);
1291 if (! server->buffer)
1293 strcpy (pop_error, "Out of memory in pop_getline");
1298 ret = RECV (server->file, server->buffer + server->data,
1299 server->buffer_size - server->data - 1, 0);
1302 strcpy (pop_error, GETLINE_ERROR);
1303 strncat (pop_error, strerror (errno),
1304 ERROR_MAX - sizeof (GETLINE_ERROR));
1310 strcpy (pop_error, "Unexpected EOF from server in pop_getline");
1317 server->data += ret;
1318 server->buffer[server->data] = '\0';
1320 cp = find_crlf (server->buffer + search_offset,
1321 server->data - search_offset);
1324 int data_used = (cp + 2) - server->buffer;
1326 server->data -= data_used;
1327 server->buffer_index = data_used;
1330 fprintf (stderr, "<<< %s\n", server->buffer);
1331 *line = server->buffer;
1332 return (data_used - 2);
1334 /* As above, the "- 1" here is to account for the fact that
1335 we may have read a CR without its accompanying LF. */
1336 search_offset += ret - 1;
1344 * Function: sendline
1346 * Purpose: Sends a line of text to the POP server. The line of text
1347 * passed into this function should NOT have the carriage return
1348 * and linefeed on the end of it. Periods at beginnings of lines
1349 * will NOT be treated specially by this function.
1352 * server The server to which to send the text.
1353 * line The line of text to send.
1355 * Return value: Upon successful completion, a value of 0 will be
1356 * returned. Otherwise, a non-zero value will be returned, and
1357 * an error will be copied into pop_error.
1359 * Side effects: Closes the connection on error.
1362 sendline (popserver server, char *line)
1364 #define SENDLINE_ERROR "Error writing to POP server: "
1368 /* Combine the string and the CR-LF into one buffer. Otherwise, two
1369 reasonable network stack optimizations, Nagle's algorithm and
1370 delayed acks, combine to delay us a fraction of a second on every
1371 message we send. (Movemail writes line without \r\n, client
1372 kernel sends packet, server kernel delays the ack to see if it
1373 can combine it with data, movemail writes \r\n, client kernel
1374 waits because it has unacked data already in its outgoing queue,
1375 client kernel eventually times out and sends.)
1377 This can be something like 0.2s per command, which can add up
1378 over a few dozen messages, and is a big chunk of the time we
1379 spend fetching mail from a server close by. */
1380 buf = alloca (strlen (line) + 3);
1382 strcat (buf, "\r\n");
1383 ret = fullwrite (server->file, line, strlen (line));
1388 strcpy (pop_error, SENDLINE_ERROR);
1389 strncat (pop_error, strerror (errno),
1390 ERROR_MAX - sizeof (SENDLINE_ERROR));
1395 fprintf (stderr, ">>> %s\n", line);
1401 * Procedure: fullwrite
1403 * Purpose: Just like write, but keeps trying until the entire string
1406 * Return value: Same as write. Pop_error is not set.
1409 fullwrite (int fd, char *buf, int nbytes)
1415 while (nbytes && ((ret = SEND (fd, cp, nbytes, 0)) > 0))
1427 * Purpose: Reads a line from the server. If the return indicator is
1428 * positive, return with a zero exit status. If not, return with
1429 * a negative exit status.
1432 * server The server to read from.
1434 * Returns: 0 for success, else for failure and puts error in pop_error.
1436 * Side effects: On failure, may make the connection unusable.
1439 getok (popserver server)
1443 if (pop_getline (server, &fromline) < 0)
1448 if (! strncmp (fromline, "+OK", 3))
1450 else if (! strncmp (fromline, "-ERR", 4))
1452 strncpy (pop_error, fromline, ERROR_MAX);
1453 pop_error[ERROR_MAX-1] = '\0';
1459 "Unexpected response from server; expecting +OK or -ERR");
1467 * Function: gettermination
1469 * Purpose: Gets the next line and verifies that it is a termination
1470 * line (nothing but a dot).
1472 * Return value: 0 on success, non-zero with pop_error set on error.
1474 * Side effects: Closes the connection on error.
1477 gettermination (popserver server)
1481 if (pop_getline (server, &fromserver) < 0)
1484 if (strcmp (fromserver, "."))
1487 "Unexpected response from server in gettermination");
1497 * Function pop_close
1499 * Purpose: Close a pop connection, sending a "RSET" command to try to
1500 * preserve any changes that were made and a "QUIT" command to
1501 * try to get the server to quit, but ignoring any responses that
1504 * Side effects: The server is unusable after this function returns.
1505 * Changes made to the maildrop since the session was started (or
1506 * since the last pop_reset) may be lost.
1509 pop_close (popserver server)
1512 free ((char *) server);
1518 * Function: pop_trash
1520 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
1521 * memory associated with the server. It is legal to call
1522 * pop_close or pop_quit after this function has been called.
1525 pop_trash (popserver server)
1527 if (server->file >= 0)
1529 /* avoid recursion; sendline can call pop_trash */
1530 if (server->trash_started)
1532 server->trash_started = 1;
1534 sendline (server, "RSET");
1535 sendline (server, "QUIT");
1537 CLOSESOCKET (server->file);
1541 free (server->buffer);
1552 /* Return a pointer to the first CRLF in IN_STRING, which can contain
1553 embedded nulls and has LEN characters in it not including the final
1554 null, or 0 if it does not contain one. */
1557 find_crlf (char *in_string, int len)
1561 if (*in_string == '\r')
1563 if (*++in_string == '\n')
1564 return (in_string - 1);
1572 #endif /* MAIL_USE_POP */