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 Written by Jonathan Kamens, jik@security.ov.com.
6 This file is part of GNU Emacs.
8 GNU Emacs 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 2, or (at your option)
13 GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
24 #define NO_SHORTNAMES /* Tell config not to load remap.h */
32 #include <sys/types.h>
37 #define RECV(s,buf,len,flags) recv(s,buf,len,flags)
38 #define SEND(s,buf,len,flags) send(s,buf,len,flags)
39 #define CLOSESOCKET(s) closesocket(s)
41 #include <netinet/in.h>
42 #include <sys/socket.h>
43 #define RECV(s,buf,len,flags) read(s,buf,len)
44 #define SEND(s,buf,len,flags) write(s,buf,len)
45 #define CLOSESOCKET(s) close(s)
56 * It really shouldn't be necessary to put this declaration here, but
57 * the version of hesiod.h that Athena has installed in release 7.2
58 * doesn't declare this function; I don't know if the 7.3 version of
61 extern struct servent
*hes_getservbyname (/* char *, char * */);
83 # ifdef HAVE_KERBEROSIV_KRB_H
84 # include <kerberosIV/krb.h>
86 # ifdef HAVE_KERBEROS_KRB_H
87 # include <kerberos/krb.h>
91 # ifdef HAVE_COM_ERR_H
98 extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *,
99 u_long, MSG_DAT *, CREDENTIALS *, Key_schedule,
100 struct sockaddr_in *, struct sockaddr_in *,
102 extern char *krb_realmofhost (/* char * */);
103 #endif /* ! KERBEROS5 */
104 #endif /* KERBEROS */
107 #if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H)
117 # endif /* __STDC__ */
120 static int socket_connection
__P((char *, int));
121 static int pop_getline
__P((popserver
, char **));
122 static int sendline
__P((popserver
, char *));
123 static int fullwrite
__P((int, char *, int));
124 static int getok
__P((popserver
));
126 static int gettermination
__P((popserver
));
128 static void pop_trash
__P((popserver
));
129 static char *find_crlf
__P((char *, int));
131 #define ERROR_MAX 160 /* a pretty arbitrary size, but needs
132 to be bigger than the original
135 #define KPOP_PORT 1109
136 #define POP_SERVICE "pop3" /* we don't want the POP2 port! */
138 #define KPOP_SERVICE "kpop"
141 char pop_error
[ERROR_MAX
];
145 #define min(a,b) (((a) < (b)) ? (a) : (b))
149 * Function: pop_open (char *host, char *username, char *password,
152 * Purpose: Establishes a connection with a post-office server, and
153 * completes the authorization portion of the session.
156 * host The server host with which the connection should be
157 * established. Optional. If omitted, internal
158 * heuristics will be used to determine the server host,
161 * The username of the mail-drop to access. Optional.
162 * If omitted, internal heuristics will be used to
163 * determine the username, if possible.
165 * The password to use for authorization. If omitted,
166 * internal heuristics will be used to determine the
167 * password, if possible.
168 * flags A bit mask containing flags controlling certain
169 * functions of the routine. Valid flags are defined in
172 * Return value: Upon successful establishment of a connection, a
173 * non-null popserver will be returned. Otherwise, null will be
174 * returned, and the string variable pop_error will contain an
175 * explanation of the error.
178 pop_open (host
, username
, password
, flags
)
187 /* Determine the user name */
190 username
= getenv ("USER");
191 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");
212 * Determine the mail host.
217 host
= getenv ("MAILHOST");
221 if ((! host
) && (! (flags
& POP_NO_HESIOD
)))
223 struct hes_postoffice
*office
;
224 office
= hes_getmailhost (username
);
225 if (office
&& office
->po_type
&& (! strcmp (office
->po_type
, "POP"))
226 && office
->po_name
&& *office
->po_name
&& office
->po_host
229 host
= office
->po_host
;
230 username
= office
->po_name
;
244 strcpy (pop_error
, "Could not determine POP server");
248 /* Determine the password */
250 #define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS))
252 #define DONT_NEED_PASSWORD 0
255 if ((! password
) && (! DONT_NEED_PASSWORD
))
257 if (! (flags
& POP_NO_GETPASS
))
259 password
= getpass ("Enter POP password:");
263 strcpy (pop_error
, "Could not determine POP password");
268 flags
|= POP_NO_KERBEROS
;
272 sock
= socket_connection (host
, flags
);
276 server
= (popserver
) malloc (sizeof (struct _popserver
));
279 strcpy (pop_error
, "Out of memory in pop_open");
282 server
->buffer
= (char *) malloc (GETLINE_MIN
);
283 if (! server
->buffer
)
285 strcpy (pop_error
, "Out of memory in pop_open");
286 free ((char *) server
);
292 server
->buffer_index
= 0;
293 server
->buffer_size
= GETLINE_MIN
;
294 server
->in_multi
= 0;
295 server
->trash_started
= 0;
301 * I really shouldn't use the pop_error variable like this, but....
303 if (strlen (username
) > ERROR_MAX
- 6)
307 "Username too long; recompile pop.c with larger ERROR_MAX");
310 sprintf (pop_error
, "USER %s", username
);
312 if (sendline (server
, pop_error
) || getok (server
))
317 if (strlen (password
) > ERROR_MAX
- 6)
321 "Password too long; recompile pop.c with larger ERROR_MAX");
324 sprintf (pop_error
, "PASS %s", password
);
326 if (sendline (server
, pop_error
) || getok (server
))
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.
341 * Return value: 0 on success, or non-zero with an error in pop_error
344 * Side effects: On failure, may make further operations on the
345 * connection impossible.
348 pop_stat (server
, count
, size
)
355 if (server
->in_multi
)
357 strcpy (pop_error
, "In multi-line query in pop_stat");
361 if (sendline (server
, "STAT") || (pop_getline (server
, &fromserver
) < 0))
364 if (strncmp (fromserver
, "+OK ", 4))
366 if (0 == strncmp (fromserver
, "-ERR", 4))
368 strncpy (pop_error
, fromserver
, ERROR_MAX
);
373 "Unexpected response from POP server in pop_stat");
379 *count
= atoi (&fromserver
[4]);
381 fromserver
= index (&fromserver
[4], ' ');
385 "Badly formatted response from server in pop_stat");
390 *size
= atoi (fromserver
+ 1);
398 * Purpose: Performs the POP "list" command and returns (in value
399 * parameters) two malloc'd zero-terminated arrays -- one of
400 * message IDs, and a parallel one of sizes.
403 * server The pop connection to talk to.
404 * message The number of the one message about which to get
405 * information, or 0 to get information about all
408 * Return value: 0 on success, non-zero with error in pop_error on
411 * Side effects: On failure, may make further operations on the
412 * connection impossible.
415 pop_list (server
, message
, IDs
, sizes
)
424 if (server
->in_multi
)
426 strcpy (pop_error
, "In multi-line query in pop_list");
435 if (pop_stat (server
, &count
, &size
))
440 *IDs
= (int *) malloc ((how_many
+ 1) * sizeof (int));
441 *sizes
= (int *) malloc ((how_many
+ 1) * sizeof (int));
442 if (! (*IDs
&& *sizes
))
444 strcpy (pop_error
, "Out of memory in pop_list");
450 sprintf (pop_error
, "LIST %d", message
);
451 if (sendline (server
, pop_error
))
453 free ((char *) *IDs
);
454 free ((char *) *sizes
);
457 if (pop_getline (server
, &fromserver
) < 0)
459 free ((char *) *IDs
);
460 free ((char *) *sizes
);
463 if (strncmp (fromserver
, "+OK ", 4))
465 if (! strncmp (fromserver
, "-ERR", 4))
466 strncpy (pop_error
, fromserver
, ERROR_MAX
);
470 "Unexpected response from server in pop_list");
473 free ((char *) *IDs
);
474 free ((char *) *sizes
);
477 (*IDs
)[0] = atoi (&fromserver
[4]);
478 fromserver
= index (&fromserver
[4], ' ');
482 "Badly formatted response from server in pop_list");
484 free ((char *) *IDs
);
485 free ((char *) *sizes
);
488 (*sizes
)[0] = atoi (fromserver
);
489 (*IDs
)[1] = (*sizes
)[1] = 0;
494 if (pop_multi_first (server
, "LIST", &fromserver
))
496 free ((char *) *IDs
);
497 free ((char *) *sizes
);
500 for (i
= 0; i
< how_many
; i
++)
502 if (pop_multi_next (server
, &fromserver
) <= 0)
504 free ((char *) *IDs
);
505 free ((char *) *sizes
);
508 (*IDs
)[i
] = atoi (fromserver
);
509 fromserver
= index (fromserver
, ' ');
513 "Badly formatted response from server in pop_list");
514 free ((char *) *IDs
);
515 free ((char *) *sizes
);
519 (*sizes
)[i
] = atoi (fromserver
);
521 if (pop_multi_next (server
, &fromserver
) < 0)
523 free ((char *) *IDs
);
524 free ((char *) *sizes
);
530 "Too many response lines from server in pop_list");
531 free ((char *) *IDs
);
532 free ((char *) *sizes
);
535 (*IDs
)[i
] = (*sizes
)[i
] = 0;
541 * Function: pop_retrieve
543 * Purpose: Retrieve a specified message from the maildrop.
546 * server The server to retrieve from.
547 * message The message number to retrieve.
549 * If true, then mark the string "From " at the beginning
551 * msg_buf Output parameter to which a buffer containing the
552 * message is assigned.
554 * Return value: The number of bytes in msg_buf, which may contain
555 * embedded nulls, not including its final null, or -1 on error
556 * with pop_error set.
558 * Side effects: May kill connection on error.
561 pop_retrieve (server
, message
, markfrom
, msg_buf
)
567 int *IDs
, *sizes
, bufsize
, fromcount
= 0, cp
= 0;
568 char *ptr
, *fromserver
;
571 if (server
->in_multi
)
573 strcpy (pop_error
, "In multi-line query in pop_retrieve");
577 if (pop_list (server
, message
, &IDs
, &sizes
))
580 if (pop_retrieve_first (server
, message
, &fromserver
))
586 * The "5" below is an arbitrary constant -- I assume that if
587 * there are "From" lines in the text to be marked, there
588 * probably won't be more than 5 of them. If there are, I
589 * allocate more space for them below.
591 bufsize
= sizes
[0] + (markfrom
? 5 : 0);
592 ptr
= (char *)malloc (bufsize
);
594 free ((char *) sizes
);
598 strcpy (pop_error
, "Out of memory in pop_retrieve");
599 pop_retrieve_flush (server
);
603 while ((ret
= pop_retrieve_next (server
, &fromserver
)) >= 0)
611 if (markfrom
&& fromserver
[0] == 'F' && fromserver
[1] == 'r' &&
612 fromserver
[2] == 'o' && fromserver
[3] == 'm' &&
613 fromserver
[4] == ' ')
615 if (++fromcount
== 5)
618 ptr
= (char *)realloc (ptr
, bufsize
);
621 strcpy (pop_error
, "Out of memory in pop_retrieve");
622 pop_retrieve_flush (server
);
629 bcopy (fromserver
, &ptr
[cp
], ret
);
639 pop_retrieve_first (server
, message
, response
)
644 sprintf (pop_error
, "RETR %d", message
);
645 return (pop_multi_first (server
, pop_error
, response
));
649 Returns a negative number on error, 0 to indicate that the data has
650 all been read (i.e., the server has returned a "." termination
651 line), or a positive number indicating the number of bytes in the
652 returned buffer (which is null-terminated and may contain embedded
653 nulls, but the returned bytecount doesn't include the final null).
657 pop_retrieve_next (server
, line
)
661 return (pop_multi_next (server
, line
));
665 pop_retrieve_flush (server
)
668 return (pop_multi_flush (server
));
672 pop_top_first (server
, message
, lines
, response
)
677 sprintf (pop_error
, "TOP %d %d", message
, lines
);
678 return (pop_multi_first (server
, pop_error
, response
));
682 Returns a negative number on error, 0 to indicate that the data has
683 all been read (i.e., the server has returned a "." termination
684 line), or a positive number indicating the number of bytes in the
685 returned buffer (which is null-terminated and may contain embedded
686 nulls, but the returned bytecount doesn't include the final null).
690 pop_top_next (server
, line
)
694 return (pop_multi_next (server
, line
));
698 pop_top_flush (server
)
701 return (pop_multi_flush (server
));
705 pop_multi_first (server
, command
, response
)
710 if (server
->in_multi
)
713 "Already in multi-line query in pop_multi_first");
717 if (sendline (server
, command
) || (pop_getline (server
, response
) < 0))
722 if (0 == strncmp (*response
, "-ERR", 4))
724 strncpy (pop_error
, *response
, ERROR_MAX
);
727 else if (0 == strncmp (*response
, "+OK", 3))
729 for (*response
+= 3; **response
== ' '; (*response
)++) /* empty */;
730 server
->in_multi
= 1;
736 "Unexpected response from server in pop_multi_first");
742 Read the next line of data from SERVER and place a pointer to it
743 into LINE. Return -1 on error, 0 if there are no more lines to read
744 (i.e., the server has returned a line containing only "."), or a
745 positive number indicating the number of bytes in the LINE buffer
746 (not including the final null). The data in that buffer may contain
747 embedded nulls, but does not contain the final CRLF. When returning
748 0, LINE is set to null. */
751 pop_multi_next (server
, line
)
758 if (! server
->in_multi
)
760 strcpy (pop_error
, "Not in multi-line query in pop_multi_next");
764 if ((ret
= pop_getline (server
, &fromserver
)) < 0)
769 if (fromserver
[0] == '.')
774 server
->in_multi
= 0;
779 *line
= fromserver
+ 1;
791 pop_multi_flush (server
)
797 if (! server
->in_multi
)
802 while ((ret
= pop_multi_next (server
, &line
)))
811 /* Function: pop_delete
813 * Purpose: Delete a specified message.
816 * server Server from which to delete the message.
817 * message Message to delete.
819 * Return value: 0 on success, non-zero with error in pop_error
823 pop_delete (server
, message
)
827 if (server
->in_multi
)
829 strcpy (pop_error
, "In multi-line query in pop_delete");
833 sprintf (pop_error
, "DELE %d", message
);
835 if (sendline (server
, pop_error
) || getok (server
))
844 * Purpose: Send a noop command to the server.
847 * server The server to send to.
849 * Return value: 0 on success, non-zero with error in pop_error
852 * Side effects: Closes connection on error.
858 if (server
->in_multi
)
860 strcpy (pop_error
, "In multi-line query in pop_noop");
864 if (sendline (server
, "NOOP") || getok (server
))
873 * Purpose: Find out the highest seen message from the server.
878 * Return value: If successful, the highest seen message, which is
879 * greater than or equal to 0. Otherwise, a negative number with
880 * the error explained in pop_error.
882 * Side effects: Closes the connection on error.
890 if (server
->in_multi
)
892 strcpy (pop_error
, "In multi-line query in pop_last");
896 if (sendline (server
, "LAST"))
899 if (pop_getline (server
, &fromserver
) < 0)
902 if (! strncmp (fromserver
, "-ERR", 4))
904 strncpy (pop_error
, fromserver
, ERROR_MAX
);
907 else if (strncmp (fromserver
, "+OK ", 4))
909 strcpy (pop_error
, "Unexpected response from server in pop_last");
915 return (atoi (&fromserver
[4]));
920 * Function: pop_reset
922 * Purpose: Reset the server to its initial connect state
927 * Return value: 0 for success, non-0 with error in pop_error
930 * Side effects: Closes the connection on error.
936 if (pop_retrieve_flush (server
))
941 if (sendline (server
, "RSET") || getok (server
))
950 * Purpose: Quit the connection to the server,
953 * server The server to quit.
955 * Return value: 0 for success, non-zero otherwise with error in
958 * Side Effects: The popserver passed in is unusable after this
959 * function is called, even if an error occurs.
967 if (server
->file
>= 0)
969 if (pop_retrieve_flush (server
))
974 if (sendline (server
, "QUIT") || getok (server
))
979 close (server
->file
);
983 free (server
->buffer
);
984 free ((char *) server
);
990 static int have_winsock
= 0;
994 * Function: socket_connection
996 * Purpose: Opens the network connection with the mail host, without
997 * doing any sort of I/O with it or anything.
1000 * host The host to which to connect.
1001 * flags Option flags.
1003 * Return value: A file descriptor indicating the connection, or -1
1004 * indicating failure, in which case an error has been copied
1008 socket_connection (host
, flags
)
1012 struct hostent
*hostent
;
1013 struct servent
*servent
;
1014 struct sockaddr_in addr
;
1015 char found_port
= 0;
1020 krb5_error_code rem
;
1021 krb5_context kcontext
= 0;
1022 krb5_auth_context auth_context
= 0;
1024 krb5_principal client
, server
;
1025 krb5_error
*err_ret
;
1031 Key_schedule schedule
;
1034 #endif /* KERBEROS5 */
1035 #endif /* KERBEROS */
1041 WSADATA winsockData
;
1042 if (WSAStartup (0x101, &winsockData
) == 0)
1047 bzero ((char *) &addr
, sizeof (addr
));
1048 addr
.sin_family
= AF_INET
;
1051 service
= (flags
& POP_NO_KERBEROS
) ? POP_SERVICE
: KPOP_SERVICE
;
1053 service
= POP_SERVICE
;
1057 if (! (flags
& POP_NO_HESIOD
))
1059 servent
= hes_getservbyname (service
, "tcp");
1062 addr
.sin_port
= servent
->s_port
;
1069 servent
= getservbyname (service
, "tcp");
1072 addr
.sin_port
= servent
->s_port
;
1077 addr
.sin_port
= htons ((flags
& POP_NO_KERBEROS
) ?
1078 POP_PORT
: KPOP_PORT
);
1080 addr
.sin_port
= htons (POP_PORT
);
1085 #define POP_SOCKET_ERROR "Could not create socket for POP connection: "
1087 sock
= socket (PF_INET
, SOCK_STREAM
, 0);
1090 strcpy (pop_error
, POP_SOCKET_ERROR
);
1091 strncat (pop_error
, strerror (errno
),
1092 ERROR_MAX
- sizeof (POP_SOCKET_ERROR
));
1099 hostent
= gethostbyname (host
);
1101 if ((! hostent
) && ((h_errno
!= TRY_AGAIN
) || (try_count
== 5)))
1103 strcpy (pop_error
, "Could not determine POP server's address");
1106 } while (! hostent
);
1108 while (*hostent
->h_addr_list
)
1110 bcopy (*hostent
->h_addr_list
, (char *) &addr
.sin_addr
,
1112 if (! connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)))
1114 hostent
->h_addr_list
++;
1117 #define CONNECT_ERROR "Could not connect to POP server: "
1119 if (! *hostent
->h_addr_list
)
1122 strcpy (pop_error
, CONNECT_ERROR
);
1123 strncat (pop_error
, strerror (errno
),
1124 ERROR_MAX
- sizeof (CONNECT_ERROR
));
1130 #define KRB_ERROR "Kerberos error connecting to POP server: "
1131 if (! (flags
& POP_NO_KERBEROS
))
1134 if ((rem
= krb5_init_context (&kcontext
)))
1138 krb5_auth_con_free (kcontext
, auth_context
);
1140 krb5_free_context (kcontext
);
1141 strcpy (pop_error
, KRB_ERROR
);
1142 strncat (pop_error
, error_message (rem
),
1143 ERROR_MAX
- sizeof(KRB_ERROR
));
1148 if ((rem
= krb5_auth_con_init (kcontext
, &auth_context
)))
1151 if (rem
= krb5_cc_default (kcontext
, &ccdef
))
1154 if (rem
= krb5_cc_get_principal (kcontext
, ccdef
, &client
))
1157 for (cp
= hostent
->h_name
; *cp
; cp
++)
1161 *cp
= tolower (*cp
);
1165 if (rem
= krb5_sname_to_principal (kcontext
, hostent
->h_name
,
1166 POP_SERVICE
, FALSE
, &server
))
1169 rem
= krb5_sendauth (kcontext
, &auth_context
,
1170 (krb5_pointer
) &sock
, "KPOPV1.0", client
, server
,
1171 AP_OPTS_MUTUAL_REQUIRED
,
1172 0, /* no checksum */
1173 0, /* no creds, use ccache instead */
1176 0, /* don't need subsession key */
1177 0); /* don't need reply */
1178 krb5_free_principal (kcontext
, server
);
1181 if (err_ret
&& err_ret
->text
.length
)
1183 strcpy (pop_error
, KRB_ERROR
);
1184 strncat (pop_error
, error_message (rem
),
1185 ERROR_MAX
- sizeof (KRB_ERROR
));
1186 strncat (pop_error
, " [server says '",
1187 ERROR_MAX
- strlen (pop_error
) - 1);
1188 strncat (pop_error
, err_ret
->text
.data
,
1189 min (ERROR_MAX
- strlen (pop_error
) - 1,
1190 err_ret
->text
.length
));
1191 strncat (pop_error
, "']",
1192 ERROR_MAX
- strlen (pop_error
) - 1);
1196 strcpy (pop_error
, KRB_ERROR
);
1197 strncat (pop_error
, error_message (rem
),
1198 ERROR_MAX
- sizeof (KRB_ERROR
));
1201 krb5_free_error (kcontext
, err_ret
);
1202 krb5_auth_con_free (kcontext
, auth_context
);
1203 krb5_free_context (kcontext
);
1208 #else /* ! KERBEROS5 */
1209 ticket
= (KTEXT
) malloc (sizeof (KTEXT_ST
));
1210 realhost
= strdup (hostent
->h_name
);
1211 rem
= krb_sendauth (0L, sock
, ticket
, "pop", realhost
,
1212 (char *) krb_realmofhost (realhost
),
1213 (unsigned long) 0, &msg_data
, &cred
, schedule
,
1214 (struct sockaddr_in
*) 0,
1215 (struct sockaddr_in
*) 0,
1217 free ((char *) ticket
);
1219 if (rem
!= KSUCCESS
)
1221 strcpy (pop_error
, KRB_ERROR
);
1222 strncat (pop_error
, krb_err_txt
[rem
],
1223 ERROR_MAX
- sizeof (KRB_ERROR
));
1227 #endif /* KERBEROS5 */
1229 #endif /* KERBEROS */
1232 } /* socket_connection */
1235 * Function: pop_getline
1237 * Purpose: Get a line of text from the connection and return a
1238 * pointer to it. The carriage return and linefeed at the end of
1239 * the line are stripped, but periods at the beginnings of lines
1240 * are NOT dealt with in any special way.
1243 * server The server from which to get the line of text.
1245 * Returns: The number of characters in the line, which is returned in
1246 * LINE, not including the final null. A return value of 0
1247 * indicates a blank line. A negative return value indicates an
1248 * error (in which case the contents of LINE are undefined. In
1249 * case of error, an error message is copied into pop_error.
1251 * Notes: The line returned is overwritten with each call to pop_getline.
1253 * Side effects: Closes the connection on error.
1255 * THE RETURNED LINE MAY CONTAIN EMBEDDED NULLS!
1258 pop_getline (server
, line
)
1262 #define GETLINE_ERROR "Error reading from server: "
1265 int search_offset
= 0;
1269 char *cp
= find_crlf (server
->buffer
+ server
->buffer_index
,
1276 found
= server
->buffer_index
;
1277 data_used
= (cp
+ 2) - server
->buffer
- found
;
1279 *cp
= '\0'; /* terminate the string to be returned */
1280 server
->data
-= data_used
;
1281 server
->buffer_index
+= data_used
;
1284 /* Embedded nulls will truncate this output prematurely,
1285 but that's OK because it's just for debugging anyway. */
1286 fprintf (stderr
, "<<< %s\n", server
->buffer
+ found
);
1287 *line
= server
->buffer
+ found
;
1288 return (data_used
- 2);
1292 bcopy (server
->buffer
+ server
->buffer_index
,
1293 server
->buffer
, server
->data
);
1294 /* Record the fact that we've searched the data already in
1295 the buffer for a CRLF, so that when we search below, we
1296 don't have to search the same data twice. There's a "-
1297 1" here to account for the fact that the last character
1298 of the data we have may be the CR of a CRLF pair, of
1299 which we haven't read the second half yet, so we may have
1300 to search it again when we read more data. */
1301 search_offset
= server
->data
- 1;
1302 server
->buffer_index
= 0;
1307 server
->buffer_index
= 0;
1312 /* There's a "- 1" here to leave room for the null that we put
1313 at the end of the read data below. We put the null there so
1314 that find_crlf knows where to stop when we call it. */
1315 if (server
->data
== server
->buffer_size
- 1)
1317 server
->buffer_size
+= GETLINE_INCR
;
1318 server
->buffer
= (char *)realloc (server
->buffer
, server
->buffer_size
);
1319 if (! server
->buffer
)
1321 strcpy (pop_error
, "Out of memory in pop_getline");
1326 ret
= RECV (server
->file
, server
->buffer
+ server
->data
,
1327 server
->buffer_size
- server
->data
- 1, 0);
1330 strcpy (pop_error
, GETLINE_ERROR
);
1331 strncat (pop_error
, strerror (errno
),
1332 ERROR_MAX
- sizeof (GETLINE_ERROR
));
1338 strcpy (pop_error
, "Unexpected EOF from server in pop_getline");
1345 server
->data
+= ret
;
1346 server
->buffer
[server
->data
] = '\0';
1348 cp
= find_crlf (server
->buffer
+ search_offset
,
1349 server
->data
- search_offset
);
1352 int data_used
= (cp
+ 2) - server
->buffer
;
1354 server
->data
-= data_used
;
1355 server
->buffer_index
= data_used
;
1358 fprintf (stderr
, "<<< %s\n", server
->buffer
);
1359 *line
= server
->buffer
;
1360 return (data_used
- 2);
1362 /* As above, the "- 1" here is to account for the fact that
1363 we may have read a CR without its accompanying LF. */
1364 search_offset
+= ret
- 1;
1372 * Function: sendline
1374 * Purpose: Sends a line of text to the POP server. The line of text
1375 * passed into this function should NOT have the carriage return
1376 * and linefeed on the end of it. Periods at beginnings of lines
1377 * will NOT be treated specially by this function.
1380 * server The server to which to send the text.
1381 * line The line of text to send.
1383 * Return value: Upon successful completion, a value of 0 will be
1384 * returned. Otherwise, a non-zero value will be returned, and
1385 * an error will be copied into pop_error.
1387 * Side effects: Closes the connection on error.
1390 sendline (server
, line
)
1394 #define SENDLINE_ERROR "Error writing to POP server: "
1398 /* Combine the string and the CR-LF into one buffer. Otherwise, two
1399 reasonable network stack optimizations, Nagle's algorithm and
1400 delayed acks, combine to delay us a fraction of a second on every
1401 message we send. (Movemail writes line without \r\n, client
1402 kernel sends packet, server kernel delays the ack to see if it
1403 can combine it with data, movemail writes \r\n, client kernel
1404 waits because it has unacked data already in its outgoing queue,
1405 client kernel eventually times out and sends.)
1407 This can be something like 0.2s per command, which can add up
1408 over a few dozen messages, and is a big chunk of the time we
1409 spend fetching mail from a server close by. */
1410 buf
= alloca (strlen (line
) + 3);
1412 strcat (buf
, "\r\n");
1413 ret
= fullwrite (server
->file
, buf
, strlen (buf
));
1418 strcpy (pop_error
, SENDLINE_ERROR
);
1419 strncat (pop_error
, strerror (errno
),
1420 ERROR_MAX
- sizeof (SENDLINE_ERROR
));
1425 fprintf (stderr
, ">>> %s\n", line
);
1431 * Procedure: fullwrite
1433 * Purpose: Just like write, but keeps trying until the entire string
1436 * Return value: Same as write. Pop_error is not set.
1439 fullwrite (fd
, buf
, nbytes
)
1448 while (nbytes
&& ((ret
= SEND (fd
, cp
, nbytes
, 0)) > 0))
1460 * Purpose: Reads a line from the server. If the return indicator is
1461 * positive, return with a zero exit status. If not, return with
1462 * a negative exit status.
1465 * server The server to read from.
1467 * Returns: 0 for success, else for failure and puts error in pop_error.
1469 * Side effects: On failure, may make the connection unusable.
1477 if (pop_getline (server
, &fromline
) < 0)
1482 if (! strncmp (fromline
, "+OK", 3))
1484 else if (! strncmp (fromline
, "-ERR", 4))
1486 strncpy (pop_error
, fromline
, ERROR_MAX
);
1487 pop_error
[ERROR_MAX
-1] = '\0';
1493 "Unexpected response from server; expecting +OK or -ERR");
1501 * Function: gettermination
1503 * Purpose: Gets the next line and verifies that it is a termination
1504 * line (nothing but a dot).
1506 * Return value: 0 on success, non-zero with pop_error set on error.
1508 * Side effects: Closes the connection on error.
1511 gettermination (server
)
1516 if (pop_getline (server
, &fromserver
) < 0)
1519 if (strcmp (fromserver
, "."))
1522 "Unexpected response from server in gettermination");
1532 * Function pop_close
1534 * Purpose: Close a pop connection, sending a "RSET" command to try to
1535 * preserve any changes that were made and a "QUIT" command to
1536 * try to get the server to quit, but ignoring any responses that
1539 * Side effects: The server is unusable after this function returns.
1540 * Changes made to the maildrop since the session was started (or
1541 * since the last pop_reset) may be lost.
1548 free ((char *) server
);
1554 * Function: pop_trash
1556 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
1557 * memory associated with the server. It is legal to call
1558 * pop_close or pop_quit after this function has been called.
1564 if (server
->file
>= 0)
1566 /* avoid recursion; sendline can call pop_trash */
1567 if (server
->trash_started
)
1569 server
->trash_started
= 1;
1571 sendline (server
, "RSET");
1572 sendline (server
, "QUIT");
1574 CLOSESOCKET (server
->file
);
1578 free (server
->buffer
);
1589 /* Return a pointer to the first CRLF in IN_STRING, which can contain
1590 embedded nulls and has LEN characters in it not including the final
1591 null, or 0 if it does not contain one. */
1594 find_crlf (in_string
, len
)
1600 if (*in_string
== '\r')
1602 if (*++in_string
== '\n')
1603 return (in_string
- 1);
1611 #endif /* MAIL_USE_POP */
1613 /* arch-tag: ceb37041-b7ad-49a8-a63d-286618b8367d
1614 (do not change this comment) */