1 /* pop.c: client routines for talking to a POP3-protocol post-office server
2 Copyright (C) 1991, 1993, 1996, 1997, 1999, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007 Free Software Foundation, Inc.
4 Written by Jonathan Kamens, jik@security.ov.com.
6 This file is part of SXEmacs.
8 SXEmacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 SXEmacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #define NO_SHORTNAMES /* Tell config not to load remap.h */
30 #include <sys/types.h>
31 #include <netinet/in.h>
32 #include <sys/socket.h>
33 #define RECV(s,buf,len,flags) read(s,buf,len)
34 #define SEND(s,buf,len,flags) write(s,buf,len)
35 #define CLOSESOCKET(s) close(s)
45 * It really shouldn't be necessary to put this declaration here, but
46 * the version of hesiod.h that Athena has installed in release 7.2
47 * doesn't declare this function; I don't know if the 7.3 version of
50 extern struct servent *hes_getservbyname (/* char *, char * */);
66 #if defined WITH_KERBEROS && defined HAVE_KERBEROS
67 # if defined HAVE_KRB5_KRB5_H
68 # include <krb/krb5.h>
69 # elif defined HAVE_KRB5_H
71 # elif defined HAVE_KRB_KRB_H
73 # elif defined HAVE_KRB_H
75 # elif defined HAVE_KERBEROS_KRB_H
76 # include <kerberos/krb.h>
77 # elif defined HAVE_KERBEROSIV_KRB_H
78 # include <kerberosIV/krb.h>
80 # error "What are you doing here?"
82 # if !defined HAVE_KERBEROS5 && defined HAVE_KRB_COM_ERR_H
83 # include <krb/com_err.h>
84 # elif !defined HAVE_KERBEROS5 && defined HAVE_KERBEROSIV_KRB_ERR_H
85 # include <kerberosIV/krb_err.h>
86 # elif !defined HAVE_KERBEROS5 && defined COM_ERR_H
91 #if defined WITH_KERBEROS && defined HAVE_KERBEROS
92 #if !defined HAVE_KERBEROS5
93 extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *,
94 u_long, MSG_DAT *, CREDENTIALS *, Key_schedule,
95 struct sockaddr_in *, struct sockaddr_in *,
97 extern char *krb_realmofhost (/* char * */);
98 #endif /* ! HAVE_KERBEROS5 */
99 #endif /* WITH_KERBEROS && HAVE_KERBEROS */
101 #if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H)
110 # endif /* __STDC__ */
113 static int socket_connection __P((char *, int));
114 static int pop_getline __P((popserver, char **));
115 static int sendline __P((popserver, char *));
116 static int fullwrite __P((int, char *, int));
117 static int getok __P((popserver));
119 static int gettermination __P((popserver));
121 static void pop_trash __P((popserver));
122 static char *find_crlf __P((char *, int));
124 #define ERROR_MAX 160 /* a pretty arbitrary size, but needs
125 to be bigger than the original
128 #define KPOP_PORT 1109
129 #define POP_SERVICE "pop3" /* we don't want the POP2 port! */
131 #define KPOP_SERVICE "kpop" /* never used: look for 20060515 to see why */
134 char pop_error[ERROR_MAX];
138 #define min(a,b) (((a) < (b)) ? (a) : (b))
142 * Function: pop_open (char *host, char *username, char *password,
145 * Purpose: Establishes a connection with a post-office server, and
146 * completes the authorization portion of the session.
149 * host The server host with which the connection should be
150 * established. Optional. If omitted, internal
151 * heuristics will be used to determine the server host,
154 * The username of the mail-drop to access. Optional.
155 * If omitted, internal heuristics will be used to
156 * determine the username, if possible.
158 * The password to use for authorization. If omitted,
159 * internal heuristics will be used to determine the
160 * password, if possible.
161 * flags A bit mask containing flags controlling certain
162 * functions of the routine. Valid flags are defined in
165 * Return value: Upon successful establishment of a connection, a
166 * non-null popserver will be returned. Otherwise, null will be
167 * returned, and the string variable pop_error will contain an
168 * explanation of the error.
171 pop_open (host, username, password, flags)
180 /* Determine the user name */
182 username = getenv ("USER");
183 if (! (username && *username)) {
184 username = getlogin ();
185 if (! (username && *username)) {
186 struct passwd *passwd;
187 passwd = getpwuid (getuid ());
188 if (passwd && passwd->pw_name &&
190 username = passwd->pw_name;
193 "Could not determine username");
201 * Determine the mail host.
205 host = getenv ("MAILHOST");
209 if ((! host) && (! (flags & POP_NO_HESIOD))) {
210 struct hes_postoffice *office;
211 office = hes_getmailhost (username);
212 if (office && office->po_type && (! strcmp (office->po_type, "POP"))
213 && office->po_name && *office->po_name && office->po_host
214 && *office->po_host) {
215 host = office->po_host;
216 username = office->po_name;
228 strcpy (pop_error, "Could not determine POP server");
232 /* Determine the password */
233 #if defined WITH_KERBEROS && defined HAVE_KERBEROS
234 #define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS))
236 #define DONT_NEED_PASSWORD 0
239 if ((! password) && (! DONT_NEED_PASSWORD)) {
240 if (! (flags & POP_NO_GETPASS)) {
241 password = getpass ("Enter POP password:");
244 strcpy (pop_error, "Could not determine POP password");
248 if (password) /* always true, detected 20060515 */
249 flags |= POP_NO_KERBEROS;
251 password = username; /* dead code, detected 20060515 */
252 /** "kpop" service is never used: look for 20060515 to see why **/
254 sock = socket_connection (host, flags);
258 server = (popserver) malloc (sizeof (struct _popserver));
260 strcpy (pop_error, "Out of memory in pop_open");
263 server->buffer = (char *) malloc (GETLINE_MIN);
264 if (! server->buffer) {
265 strcpy (pop_error, "Out of memory in pop_open");
266 free ((char *) server);
272 server->buffer_index = 0;
273 server->buffer_size = GETLINE_MIN;
274 server->in_multi = 0;
275 server->trash_started = 0;
281 * I really shouldn't use the pop_error variable like this, but....
283 if (strlen (username) > ERROR_MAX - 6) {
286 "Username too long; "
287 "recompile pop.c with larger ERROR_MAX");
290 sprintf (pop_error, "USER %s", username);
292 if (sendline (server, pop_error) || getok (server)) {
296 if (strlen (password) > ERROR_MAX - 6) {
299 "Password too long; "
300 "recompile pop.c with larger ERROR_MAX");
303 sprintf (pop_error, "PASS %s", password);
305 if (sendline (server, pop_error) || getok (server)) {
315 * Purpose: Issue the STAT command to the server and return (in the
316 * value parameters) the number of messages in the maildrop and
317 * the total size of the maildrop.
319 * Return value: 0 on success, or non-zero with an error in pop_error
322 * Side effects: On failure, may make further operations on the
323 * connection impossible.
326 pop_stat (server, count, size)
333 if (server->in_multi) {
334 strcpy (pop_error, "In multi-line query in pop_stat");
338 if (sendline (server, "STAT") ||
339 (pop_getline (server, &fromserver) < 0))
342 if (strncmp (fromserver, "+OK ", 4)) {
343 if (0 == strncmp (fromserver, "-ERR", 4)) {
344 strncpy (pop_error, fromserver, ERROR_MAX);
347 "Unexpected response from POP "
348 "server in pop_stat");
354 *count = atoi (&fromserver[4]);
356 fromserver = index (&fromserver[4], ' ');
359 "Badly formatted response from server in pop_stat");
364 *size = atoi (fromserver + 1);
372 * Purpose: Performs the POP "list" command and returns (in value
373 * parameters) two malloc'd zero-terminated arrays -- one of
374 * message IDs, and a parallel one of sizes.
377 * server The pop connection to talk to.
378 * message The number of the one message about which to get
379 * information, or 0 to get information about all
382 * Return value: 0 on success, non-zero with error in pop_error on
385 * Side effects: On failure, may make further operations on the
386 * connection impossible.
389 pop_list (server, message, IDs, sizes)
398 if (server->in_multi) {
399 strcpy (pop_error, "In multi-line query in pop_list");
407 if (pop_stat (server, &count, &size))
412 *IDs = (int *) malloc ((how_many + 1) * sizeof (int));
413 *sizes = (int *) malloc ((how_many + 1) * sizeof (int));
414 if (! (*IDs && *sizes)) {
415 strcpy (pop_error, "Out of memory in pop_list");
420 sprintf (pop_error, "LIST %d", message);
421 if (sendline (server, pop_error)) {
422 free ((char *) *IDs);
423 free ((char *) *sizes);
426 if (pop_getline (server, &fromserver) < 0) {
427 free ((char *) *IDs);
428 free ((char *) *sizes);
431 if (strncmp (fromserver, "+OK ", 4)) {
432 if (! strncmp (fromserver, "-ERR", 4)) {
433 strncpy (pop_error, fromserver, ERROR_MAX);
436 "Unexpected response from "
437 "server in pop_list");
440 free ((char *) *IDs);
441 free ((char *) *sizes);
444 (*IDs)[0] = atoi (&fromserver[4]);
445 fromserver = index (&fromserver[4], ' ');
448 "Badly formatted response from "
449 "server in pop_list");
451 free ((char *) *IDs);
452 free ((char *) *sizes);
455 (*sizes)[0] = atoi (fromserver);
456 (*IDs)[1] = (*sizes)[1] = 0;
459 if (pop_multi_first (server, "LIST", &fromserver)) {
460 free ((char *) *IDs);
461 free ((char *) *sizes);
464 for (i = 0; i < how_many; i++) {
465 if (pop_multi_next (server, &fromserver) <= 0) {
466 free ((char *) *IDs);
467 free ((char *) *sizes);
470 (*IDs)[i] = atoi (fromserver);
471 fromserver = index (fromserver, ' ');
474 "Badly formatted response from "
475 "server in pop_list");
476 free ((char *) *IDs);
477 free ((char *) *sizes);
481 (*sizes)[i] = atoi (fromserver);
483 if (pop_multi_next (server, &fromserver) < 0) {
484 free ((char *) *IDs);
485 free ((char *) *sizes);
487 } else if (fromserver) {
489 "Too many response lines from "
490 "server in pop_list");
491 free ((char *) *IDs);
492 free ((char *) *sizes);
495 (*IDs)[i] = (*sizes)[i] = 0;
501 * Function: pop_retrieve
503 * Purpose: Retrieve a specified message from the maildrop.
506 * server The server to retrieve from.
507 * message The message number to retrieve.
509 * If true, then mark the string "From " at the beginning
511 * msg_buf Output parameter to which a buffer containing the
512 * message is assigned.
514 * Return value: The number of bytes in msg_buf, which may contain
515 * embedded nulls, not including its final null, or -1 on error
516 * with pop_error set.
518 * Side effects: May kill connection on error.
521 pop_retrieve (server, message, markfrom, msg_buf)
527 int *IDs, *sizes, bufsize, fromcount = 0, cp = 0;
528 char *ptr, *fromserver;
531 if (server->in_multi) {
532 strcpy (pop_error, "In multi-line query in pop_retrieve");
536 if (pop_list (server, message, &IDs, &sizes))
539 if (pop_retrieve_first (server, message, &fromserver)) {
544 * The "5" below is an arbitrary constant -- I assume that if
545 * there are "From" lines in the text to be marked, there
546 * probably won't be more than 5 of them. If there are, I
547 * allocate more space for them below.
549 bufsize = sizes[0] + (markfrom ? 5 : 0);
550 ptr = (char *)malloc (bufsize);
552 free ((char *) sizes);
555 strcpy (pop_error, "Out of memory in pop_retrieve");
556 pop_retrieve_flush (server);
560 while ((ret = pop_retrieve_next (server, &fromserver)) >= 0) {
566 if (markfrom && fromserver[0] == 'F' && fromserver[1] == 'r' &&
567 fromserver[2] == 'o' && fromserver[3] == 'm' &&
568 fromserver[4] == ' ') {
569 if (++fromcount == 5) {
571 ptr = (char *)realloc (ptr, bufsize);
573 strcpy (pop_error, "Out of memory "
575 pop_retrieve_flush (server);
582 bcopy (fromserver, &ptr[cp], ret);
592 pop_retrieve_first (server, message, response)
597 sprintf (pop_error, "RETR %d", message);
598 return (pop_multi_first (server, pop_error, response));
602 Returns a negative number on error, 0 to indicate that the data has
603 all been read (i.e., the server has returned a "." termination
604 line), or a positive number indicating the number of bytes in the
605 returned buffer (which is null-terminated and may contain embedded
606 nulls, but the returned bytecount doesn't include the final null).
610 pop_retrieve_next (server, line)
614 return (pop_multi_next (server, line));
618 pop_retrieve_flush (server)
621 return (pop_multi_flush (server));
625 pop_top_first (server, message, lines, response)
630 sprintf (pop_error, "TOP %d %d", message, lines);
631 return (pop_multi_first (server, pop_error, response));
635 Returns a negative number on error, 0 to indicate that the data has
636 all been read (i.e., the server has returned a "." termination
637 line), or a positive number indicating the number of bytes in the
638 returned buffer (which is null-terminated and may contain embedded
639 nulls, but the returned bytecount doesn't include the final null).
643 pop_top_next (server, line)
647 return (pop_multi_next (server, line));
651 pop_top_flush (server)
654 return (pop_multi_flush (server));
658 pop_multi_first (server, command, response)
663 if (server->in_multi) {
665 "Already in multi-line query in pop_multi_first");
669 if (sendline (server, command) ||
670 (pop_getline (server, response) < 0)) {
674 if (0 == strncmp (*response, "-ERR", 4)) {
675 strncpy (pop_error, *response, ERROR_MAX);
677 } else if (0 == strncmp (*response, "+OK", 3)) {
678 for (*response += 3; **response == ' ';
679 (*response)++) /* empty */;
680 server->in_multi = 1;
684 "Unexpected response from "
685 "server in pop_multi_first");
691 Read the next line of data from SERVER and place a pointer to it
692 into LINE. Return -1 on error, 0 if there are no more lines to read
693 (i.e., the server has returned a line containing only "."), or a
694 positive number indicating the number of bytes in the LINE buffer
695 (not including the final null). The data in that buffer may contain
696 embedded nulls, but does not contain the final CRLF. When returning
697 0, LINE is set to null. */
700 pop_multi_next (server, line)
707 if (! server->in_multi) {
708 strcpy (pop_error, "Not in multi-line query in pop_multi_next");
712 if ((ret = pop_getline (server, &fromserver)) < 0) {
716 if (fromserver[0] == '.') {
717 if (! fromserver[1]) {
719 server->in_multi = 0;
722 *line = fromserver + 1;
732 pop_multi_flush (server)
738 if (! server->in_multi) {
742 while ((ret = pop_multi_next (server, &line))) {
750 /* Function: pop_delete
752 * Purpose: Delete a specified message.
755 * server Server from which to delete the message.
756 * message Message to delete.
758 * Return value: 0 on success, non-zero with error in pop_error
762 pop_delete (server, message)
766 if (server->in_multi) {
767 strcpy (pop_error, "In multi-line query in pop_delete");
771 sprintf (pop_error, "DELE %d", message);
773 if (sendline (server, pop_error) || getok (server))
782 * Purpose: Send a noop command to the server.
785 * server The server to send to.
787 * Return value: 0 on success, non-zero with error in pop_error
790 * Side effects: Closes connection on error.
796 if (server->in_multi) {
797 strcpy (pop_error, "In multi-line query in pop_noop");
801 if (sendline (server, "NOOP") || getok (server))
810 * Purpose: Find out the highest seen message from the server.
815 * Return value: If successful, the highest seen message, which is
816 * greater than or equal to 0. Otherwise, a negative number with
817 * the error explained in pop_error.
819 * Side effects: Closes the connection on error.
827 if (server->in_multi) {
828 strcpy (pop_error, "In multi-line query in pop_last");
832 if (sendline (server, "LAST"))
835 if (pop_getline (server, &fromserver) < 0)
838 if (! strncmp (fromserver, "-ERR", 4)) {
839 strncpy (pop_error, fromserver, ERROR_MAX);
841 } else if (strncmp (fromserver, "+OK ", 4)) {
842 strcpy (pop_error, "Unexpected response from server in pop_last");
846 return (atoi (&fromserver[4]));
851 * Function: pop_reset
853 * Purpose: Reset the server to its initial connect state
858 * Return value: 0 for success, non-0 with error in pop_error
861 * Side effects: Closes the connection on error.
867 if (pop_retrieve_flush (server)) {
871 if (sendline (server, "RSET") || getok (server))
880 * Purpose: Quit the connection to the server,
883 * server The server to quit.
885 * Return value: 0 for success, non-zero otherwise with error in
888 * Side Effects: The popserver passed in is unusable after this
889 * function is called, even if an error occurs.
897 if (server->file >= 0) {
898 if (pop_retrieve_flush (server)) {
902 if (sendline (server, "QUIT") || getok (server)) {
906 close (server->file);
910 free (server->buffer);
911 free ((char *) server);
917 * Function: socket_connection
919 * Purpose: Opens the network connection with the mail host, without
920 * doing any sort of I/O with it or anything.
923 * host The host to which to connect.
924 * flags Option flags.
926 * Return value: A file descriptor indicating the connection, or -1
927 * indicating failure, in which case an error has been copied
931 socket_connection (host, flags)
935 struct hostent *hostent;
936 struct servent *servent;
937 struct sockaddr_in addr;
941 #if defined WITH_KERBEROS && defined HAVE_KERBEROS
942 # if defined HAVE_KERBEROS5
944 krb5_context kcontext = 0;
945 krb5_auth_context auth_context = 0;
947 krb5_principal client, server;
950 # else /* !HAVE_KERBEROS5 */
954 Key_schedule schedule;
957 # endif /* HAVE_KERBEROS5 */
958 #endif /* WITH_KERBEROS && HAVE_KERBEROS */
962 bzero ((char *) &addr, sizeof (addr));
963 addr.sin_family = AF_INET;
965 /** "kpop" service is never used: look for 20060515 to see why **/
966 #if defined WITH_KERBEROS && defined HAVE_KERBEROS
967 service = (flags & POP_NO_KERBEROS) ? POP_SERVICE : KPOP_SERVICE;
969 service = POP_SERVICE;
973 if (! (flags & POP_NO_HESIOD)) {
974 servent = hes_getservbyname (service, "tcp");
976 addr.sin_port = servent->s_port;
982 servent = getservbyname (service, "tcp");
984 addr.sin_port = servent->s_port;
986 /** "kpop" service is never used:
987 * look for 20060515 to see why **/
988 #if defined WITH_KERBEROS && defined HAVE_KERBEROS
989 addr.sin_port = htons ((flags & POP_NO_KERBEROS) ?
990 POP_PORT : KPOP_PORT);
992 addr.sin_port = htons (POP_PORT);
997 #define POP_SOCKET_ERROR "Could not create socket for POP connection: "
999 sock = socket (PF_INET, SOCK_STREAM, 0);
1001 strcpy (pop_error, POP_SOCKET_ERROR);
1002 strncat (pop_error, strerror (errno),
1003 ERROR_MAX - sizeof (POP_SOCKET_ERROR));
1009 hostent = gethostbyname (host);
1012 ((h_errno != TRY_AGAIN) || (try_count == 5))) {
1013 strcpy (pop_error, "Could not determine "
1014 "POP server's address");
1017 } while (! hostent);
1019 while (*hostent->h_addr_list) {
1020 bcopy (*hostent->h_addr_list, (char *) &addr.sin_addr,
1022 if (! connect (sock, (struct sockaddr *) &addr, sizeof (addr)))
1024 hostent->h_addr_list++;
1027 #define CONNECT_ERROR "Could not connect to POP server: "
1029 if (! *hostent->h_addr_list) {
1031 strcpy (pop_error, CONNECT_ERROR);
1032 strncat (pop_error, strerror (errno),
1033 ERROR_MAX - sizeof (CONNECT_ERROR));
1037 #if defined WITH_KERBEROS && defined HAVE_KERBEROS
1038 #define KRB_ERROR "Kerberos error connecting to POP server: "
1039 if (! (flags & POP_NO_KERBEROS)) {
1040 # if defined HAVE_KERBEROS5
1041 if ((rem = krb5_init_context (&kcontext))) {
1044 krb5_auth_con_free (kcontext, auth_context);
1046 krb5_free_context (kcontext);
1047 strcpy (pop_error, KRB_ERROR);
1048 strncat (pop_error, error_message (rem),
1049 ERROR_MAX - sizeof(KRB_ERROR));
1054 if ((rem = krb5_auth_con_init (kcontext, &auth_context)))
1057 if (rem = krb5_cc_default (kcontext, &ccdef))
1060 if (rem = krb5_cc_get_principal (kcontext, ccdef, &client))
1063 for (cp = hostent->h_name; *cp; cp++) {
1064 if (isupper (*cp)) {
1065 *cp = tolower (*cp);
1069 if (rem = krb5_sname_to_principal (kcontext, hostent->h_name,
1070 POP_SERVICE, FALSE, &server))
1073 rem = krb5_sendauth (kcontext, &auth_context,
1074 (krb5_pointer) &sock, "KPOPV1.0",
1076 AP_OPTS_MUTUAL_REQUIRED,
1077 0, /* no checksum */
1078 0, /* no creds, use ccache instead */
1081 0, /* don't need subsession key */
1082 0); /* don't need reply */
1083 krb5_free_principal (kcontext, server);
1085 if (err_ret && err_ret->text.length) {
1086 strcpy (pop_error, KRB_ERROR);
1087 strncat (pop_error, error_message (rem),
1088 ERROR_MAX - sizeof (KRB_ERROR));
1089 strncat (pop_error, " [server says '",
1090 ERROR_MAX - strlen (pop_error) - 1);
1091 strncat (pop_error, err_ret->text.data,
1093 strlen (pop_error) - 1,
1094 err_ret->text.length));
1095 strncat (pop_error, "']",
1096 ERROR_MAX - strlen (pop_error) - 1);
1098 strcpy (pop_error, KRB_ERROR);
1099 strncat (pop_error, error_message (rem),
1100 ERROR_MAX - sizeof (KRB_ERROR));
1103 krb5_free_error (kcontext, err_ret);
1104 krb5_auth_con_free (kcontext, auth_context);
1105 krb5_free_context (kcontext);
1110 # else /* ! HAVE_KERBEROS5 */
1111 ticket = (KTEXT) malloc (sizeof (KTEXT_ST));
1112 realhost = strdup (hostent->h_name);
1113 rem = krb_sendauth (0L, sock, ticket, "pop", realhost,
1114 (char *) krb_realmofhost (realhost),
1115 (unsigned long) 0, &msg_data, &cred, schedule,
1116 (struct sockaddr_in *) 0,
1117 (struct sockaddr_in *) 0,
1119 free ((char *) ticket);
1121 if (rem != KSUCCESS) {
1122 strcpy (pop_error, KRB_ERROR);
1123 strncat (pop_error, krb_err_txt[rem],
1124 ERROR_MAX - sizeof (KRB_ERROR));
1128 # endif /* HAVE_KERBEROS5 */
1130 #endif /* WITH_KERBEROS && HAVE_KERBEROS */
1133 } /* socket_connection */
1136 * Function: pop_getline
1138 * Purpose: Get a line of text from the connection and return a
1139 * pointer to it. The carriage return and linefeed at the end of
1140 * the line are stripped, but periods at the beginnings of lines
1141 * are NOT dealt with in any special way.
1144 * server The server from which to get the line of text.
1146 * Returns: The number of characters in the line, which is returned in
1147 * LINE, not including the final null. A return value of 0
1148 * indicates a blank line. A negative return value indicates an
1149 * error (in which case the contents of LINE are undefined. In
1150 * case of error, an error message is copied into pop_error.
1152 * Notes: The line returned is overwritten with each call to pop_getline.
1154 * Side effects: Closes the connection on error.
1156 * THE RETURNED LINE MAY CONTAIN EMBEDDED NULLS!
1159 pop_getline (server, line)
1163 #define GETLINE_ERROR "Error reading from server: "
1166 int search_offset = 0;
1169 char *cp = find_crlf (server->buffer + server->buffer_index,
1175 found = server->buffer_index;
1176 data_used = (cp + 2) - server->buffer - found;
1178 /* terminate the string to be returned */
1180 server->data -= data_used;
1181 server->buffer_index += data_used;
1184 /* Embedded nulls will truncate this output
1185 prematurely, but that's OK because it's just
1186 for debugging anyway. */
1187 fprintf (stderr, "<<< %s\n",
1188 server->buffer + found);
1189 *line = server->buffer + found;
1190 return (data_used - 2);
1192 bcopy (server->buffer + server->buffer_index,
1193 server->buffer, server->data);
1194 /* Record the fact that we've searched the data already
1195 in the buffer for a CRLF, so that when we search
1196 below, we don't have to search the same data twice.
1197 There's a "- 1" here to account for the fact that the
1198 last character of the data we have may be the CR of a
1199 CRLF pair, of which we haven't read the second half
1200 yet, so we may have to search it again when we read
1202 search_offset = server->data - 1;
1203 server->buffer_index = 0;
1206 server->buffer_index = 0;
1210 /* There's a "- 1" here to leave room for the null that we put
1211 at the end of the read data below. We put the null there so
1212 that find_crlf knows where to stop when we call it. */
1213 if (server->data == server->buffer_size - 1) {
1214 server->buffer_size += GETLINE_INCR;
1215 server->buffer = (char *)realloc (
1216 server->buffer, server->buffer_size);
1217 if (! server->buffer) {
1219 "Out of memory in pop_getline");
1224 ret = RECV (server->file, server->buffer + server->data,
1225 server->buffer_size - server->data - 1, 0);
1227 strcpy (pop_error, GETLINE_ERROR);
1228 strncat (pop_error, strerror (errno),
1229 ERROR_MAX - sizeof (GETLINE_ERROR));
1232 } else if (ret == 0) {
1233 strcpy (pop_error, "Unexpected EOF from "
1234 "server in pop_getline");
1239 server->data += ret;
1240 server->buffer[server->data] = '\0';
1242 cp = find_crlf (server->buffer + search_offset,
1243 server->data - search_offset);
1245 int data_used = (cp + 2) - server->buffer;
1247 server->data -= data_used;
1248 server->buffer_index = data_used;
1252 "<<< %s\n", server->buffer);
1253 *line = server->buffer;
1254 return (data_used - 2);
1256 /* As above, the "- 1" here is to account for the fact that
1257 we may have read a CR without its accompanying LF. */
1258 search_offset += ret - 1;
1266 * Function: sendline
1268 * Purpose: Sends a line of text to the POP server. The line of text
1269 * passed into this function should NOT have the carriage return
1270 * and linefeed on the end of it. Periods at beginnings of lines
1271 * will NOT be treated specially by this function.
1274 * server The server to which to send the text.
1275 * line The line of text to send.
1277 * Return value: Upon successful completion, a value of 0 will be
1278 * returned. Otherwise, a non-zero value will be returned, and
1279 * an error will be copied into pop_error.
1281 * Side effects: Closes the connection on error.
1284 sendline (server, line)
1288 #define SENDLINE_ERROR "Error writing to POP server: "
1292 /* Combine the string and the CR-LF into one buffer. Otherwise, two
1293 reasonable network stack optimizations, Nagle's algorithm and
1294 delayed acks, combine to delay us a fraction of a second on every
1295 message we send. (Movemail writes line without \r\n, client
1296 kernel sends packet, server kernel delays the ack to see if it
1297 can combine it with data, movemail writes \r\n, client kernel
1298 waits because it has unacked data already in its outgoing queue,
1299 client kernel eventually times out and sends.)
1301 This can be something like 0.2s per command, which can add up
1302 over a few dozen messages, and is a big chunk of the time we
1303 spend fetching mail from a server close by. */
1304 buf = alloca (strlen (line) + 3);
1306 strcat (buf, "\r\n");
1307 ret = fullwrite (server->file, buf, strlen (buf));
1311 strcpy (pop_error, SENDLINE_ERROR);
1312 strncat (pop_error, strerror (errno),
1313 ERROR_MAX - sizeof (SENDLINE_ERROR));
1318 fprintf (stderr, ">>> %s\n", line);
1324 * Procedure: fullwrite
1326 * Purpose: Just like write, but keeps trying until the entire string
1329 * Return value: Same as write. Pop_error is not set.
1332 fullwrite (fd, buf, nbytes)
1341 while (nbytes && ((ret = SEND (fd, cp, nbytes, 0)) > 0)) {
1352 * Purpose: Reads a line from the server. If the return indicator is
1353 * positive, return with a zero exit status. If not, return with
1354 * a negative exit status.
1357 * server The server to read from.
1359 * Returns: 0 for success, else for failure and puts error in pop_error.
1361 * Side effects: On failure, may make the connection unusable.
1369 if (pop_getline (server, &fromline) < 0) {
1373 if (! strncmp (fromline, "+OK", 3))
1375 else if (! strncmp (fromline, "-ERR", 4)) {
1376 strncpy (pop_error, fromline, ERROR_MAX);
1377 pop_error[ERROR_MAX-1] = '\0';
1381 "Unexpected response from server; "
1382 "expecting +OK or -ERR");
1390 * Function: gettermination
1392 * Purpose: Gets the next line and verifies that it is a termination
1393 * line (nothing but a dot).
1395 * Return value: 0 on success, non-zero with pop_error set on error.
1397 * Side effects: Closes the connection on error.
1400 gettermination (server)
1405 if (pop_getline (server, &fromserver) < 0)
1408 if (strcmp (fromserver, ".")) {
1410 "Unexpected response from server in gettermination");
1420 * Function pop_close
1422 * Purpose: Close a pop connection, sending a "RSET" command to try to
1423 * preserve any changes that were made and a "QUIT" command to
1424 * try to get the server to quit, but ignoring any responses that
1427 * Side effects: The server is unusable after this function returns.
1428 * Changes made to the maildrop since the session was started (or
1429 * since the last pop_reset) may be lost.
1436 free ((char *) server);
1442 * Function: pop_trash
1444 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
1445 * memory associated with the server. It is legal to call
1446 * pop_close or pop_quit after this function has been called.
1452 if (server->file >= 0) {
1453 /* avoid recursion; sendline can call pop_trash */
1454 if (server->trash_started)
1456 server->trash_started = 1;
1458 sendline (server, "RSET");
1459 sendline (server, "QUIT");
1461 CLOSESOCKET (server->file);
1463 if (server->buffer) {
1464 free (server->buffer);
1470 /* Return a pointer to the first CRLF in IN_STRING, which can contain
1471 embedded nulls and has LEN characters in it not including the final
1472 null, or 0 if it does not contain one. */
1475 find_crlf (in_string, len)
1480 if (*in_string == '\r') {
1481 if (*++in_string == '\n')
1482 return (in_string - 1);
1489 #endif /* MAIL_USE_POP */