1 /* pop.c: client routines for talking to a POP3-protocol post-office server
3 Copyright (C) 1991, 1993, 1996-1997, 1999, 2001-2011
4 Free Software Foundation, Inc.
6 Author: Jonathan Kamens <jik@security.ov.com>
8 This file is part of GNU Emacs.
10 GNU Emacs is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
15 GNU Emacs is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
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 * */);
81 # ifdef HAVE_KERBEROSIV_KRB_H
82 # include <kerberosIV/krb.h>
84 # ifdef HAVE_KERBEROS_KRB_H
85 # include <kerberos/krb.h>
89 # ifdef HAVE_COM_ERR_H
96 extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *,
97 u_long, MSG_DAT *, CREDENTIALS *, Key_schedule,
98 struct sockaddr_in *, struct sockaddr_in *,
100 extern char *krb_realmofhost (/* char * */);
101 #endif /* ! KERBEROS5 */
102 #endif /* KERBEROS */
105 #if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H)
110 static int socket_connection (char *, int);
111 static int pop_getline (popserver
, char **);
112 static int sendline (popserver
, const char *);
113 static int fullwrite (int, char *, int);
114 static int getok (popserver
);
116 static int gettermination (popserver
);
118 static void pop_trash (popserver
);
119 static char *find_crlf (char *, int);
121 #define ERROR_MAX 160 /* a pretty arbitrary size, but needs
122 to be bigger than the original
125 #define KPOP_PORT 1109
126 #define POP_SERVICE "pop3" /* we don't want the POP2 port! */
128 #define KPOP_SERVICE "kpop" /* never used: look for 20060515 to see why */
131 char pop_error
[ERROR_MAX
];
135 #define min(a,b) (((a) < (b)) ? (a) : (b))
139 * Function: pop_open (char *host, char *username, char *password,
142 * Purpose: Establishes a connection with a post-office server, and
143 * completes the authorization portion of the session.
146 * host The server host with which the connection should be
147 * established. Optional. If omitted, internal
148 * heuristics will be used to determine the server host,
151 * The username of the mail-drop to access. Optional.
152 * If omitted, internal heuristics will be used to
153 * determine the username, if possible.
155 * The password to use for authorization. If omitted,
156 * internal heuristics will be used to determine the
157 * password, if possible.
158 * flags A bit mask containing flags controlling certain
159 * functions of the routine. Valid flags are defined in
162 * Return value: Upon successful establishment of a connection, a
163 * non-null popserver will be returned. Otherwise, null will be
164 * returned, and the string variable pop_error will contain an
165 * explanation of the error.
168 pop_open (char *host
, char *username
, char *password
, int flags
)
173 /* Determine the user name */
176 username
= getenv ("USER");
177 if (! (username
&& *username
))
179 username
= getlogin ();
180 if (! (username
&& *username
))
182 struct passwd
*passwd
;
183 passwd
= getpwuid (getuid ());
184 if (passwd
&& passwd
->pw_name
&& *passwd
->pw_name
)
186 username
= passwd
->pw_name
;
190 strcpy (pop_error
, "Could not determine username");
198 * Determine the mail host.
203 host
= getenv ("MAILHOST");
207 if ((! host
) && (! (flags
& POP_NO_HESIOD
)))
209 struct hes_postoffice
*office
;
210 office
= hes_getmailhost (username
);
211 if (office
&& office
->po_type
&& (! strcmp (office
->po_type
, "POP"))
212 && office
->po_name
&& *office
->po_name
&& office
->po_host
215 host
= office
->po_host
;
216 username
= office
->po_name
;
230 strcpy (pop_error
, "Could not determine POP server");
234 /* Determine the password */
236 #define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS))
238 #define DONT_NEED_PASSWORD 0
241 if ((! password
) && (! DONT_NEED_PASSWORD
))
243 if (! (flags
& POP_NO_GETPASS
))
245 password
= getpass ("Enter POP password:");
249 strcpy (pop_error
, "Could not determine POP password");
253 if (password
) /* always true, detected 20060515 */
254 flags
|= POP_NO_KERBEROS
;
256 password
= username
; /* dead code, detected 20060515 */
257 /** "kpop" service is never used: look for 20060515 to see why **/
259 sock
= socket_connection (host
, flags
);
263 server
= (popserver
) malloc (sizeof (struct _popserver
));
266 strcpy (pop_error
, "Out of memory in pop_open");
269 server
->buffer
= (char *) malloc (GETLINE_MIN
);
270 if (! server
->buffer
)
272 strcpy (pop_error
, "Out of memory in pop_open");
273 free ((char *) server
);
279 server
->buffer_index
= 0;
280 server
->buffer_size
= GETLINE_MIN
;
281 server
->in_multi
= 0;
282 server
->trash_started
= 0;
288 * I really shouldn't use the pop_error variable like this, but....
290 if (strlen (username
) > ERROR_MAX
- 6)
294 "Username too long; recompile pop.c with larger ERROR_MAX");
297 sprintf (pop_error
, "USER %s", username
);
299 if (sendline (server
, pop_error
) || getok (server
))
304 if (strlen (password
) > ERROR_MAX
- 6)
308 "Password too long; recompile pop.c with larger ERROR_MAX");
311 sprintf (pop_error
, "PASS %s", password
);
313 if (sendline (server
, pop_error
) || getok (server
))
324 * Purpose: Issue the STAT command to the server and return (in the
325 * value parameters) the number of messages in the maildrop and
326 * the total size of the maildrop.
328 * Return value: 0 on success, or non-zero with an error in pop_error
331 * Side effects: On failure, may make further operations on the
332 * connection impossible.
335 pop_stat (popserver server
, int *count
, int *size
)
340 if (server
->in_multi
)
342 strcpy (pop_error
, "In multi-line query in pop_stat");
346 if (sendline (server
, "STAT") || (pop_getline (server
, &fromserver
) < 0))
349 if (strncmp (fromserver
, "+OK ", 4))
351 if (0 == strncmp (fromserver
, "-ERR", 4))
353 strncpy (pop_error
, fromserver
, ERROR_MAX
);
358 "Unexpected response from POP server in pop_stat");
365 *count
= strtol (&fromserver
[4], &end_ptr
, 10);
366 /* Check validity of string-to-integer conversion. */
367 if (fromserver
+ 4 == end_ptr
|| *end_ptr
!= ' ' || errno
)
369 strcpy (pop_error
, "Unexpected response from POP server in pop_stat");
374 fromserver
= end_ptr
;
377 *size
= strtol (fromserver
+ 1, &end_ptr
, 10);
378 if (fromserver
+ 1 == end_ptr
|| errno
)
380 strcpy (pop_error
, "Unexpected response from POP server in pop_stat");
391 * Purpose: Performs the POP "list" command and returns (in value
392 * parameters) two malloc'd zero-terminated arrays -- one of
393 * message IDs, and a parallel one of sizes.
396 * server The pop connection to talk to.
397 * message The number of the one message about which to get
398 * information, or 0 to get information about all
401 * Return value: 0 on success, non-zero with error in pop_error on
404 * Side effects: On failure, may make further operations on the
405 * connection impossible.
408 pop_list (popserver server
, int message
, int **IDs
, int **sizes
)
413 if (server
->in_multi
)
415 strcpy (pop_error
, "In multi-line query in pop_list");
424 if (pop_stat (server
, &count
, &size
))
429 *IDs
= (int *) malloc ((how_many
+ 1) * sizeof (int));
430 *sizes
= (int *) malloc ((how_many
+ 1) * sizeof (int));
431 if (! (*IDs
&& *sizes
))
433 strcpy (pop_error
, "Out of memory in pop_list");
439 sprintf (pop_error
, "LIST %d", message
);
440 if (sendline (server
, pop_error
))
442 free ((char *) *IDs
);
443 free ((char *) *sizes
);
446 if (pop_getline (server
, &fromserver
) < 0)
448 free ((char *) *IDs
);
449 free ((char *) *sizes
);
452 if (strncmp (fromserver
, "+OK ", 4))
454 if (! strncmp (fromserver
, "-ERR", 4))
455 strncpy (pop_error
, fromserver
, ERROR_MAX
);
459 "Unexpected response from server in pop_list");
462 free ((char *) *IDs
);
463 free ((char *) *sizes
);
466 (*IDs
)[0] = atoi (&fromserver
[4]);
467 fromserver
= strchr (&fromserver
[4], ' ');
471 "Badly formatted response from server in pop_list");
473 free ((char *) *IDs
);
474 free ((char *) *sizes
);
477 (*sizes
)[0] = atoi (fromserver
);
478 (*IDs
)[1] = (*sizes
)[1] = 0;
483 if (pop_multi_first (server
, "LIST", &fromserver
))
485 free ((char *) *IDs
);
486 free ((char *) *sizes
);
489 for (i
= 0; i
< how_many
; i
++)
491 if (pop_multi_next (server
, &fromserver
) <= 0)
493 free ((char *) *IDs
);
494 free ((char *) *sizes
);
497 (*IDs
)[i
] = atoi (fromserver
);
498 fromserver
= strchr (fromserver
, ' ');
502 "Badly formatted response from server in pop_list");
503 free ((char *) *IDs
);
504 free ((char *) *sizes
);
508 (*sizes
)[i
] = atoi (fromserver
);
510 if (pop_multi_next (server
, &fromserver
) < 0)
512 free ((char *) *IDs
);
513 free ((char *) *sizes
);
519 "Too many response lines from server in pop_list");
520 free ((char *) *IDs
);
521 free ((char *) *sizes
);
524 (*IDs
)[i
] = (*sizes
)[i
] = 0;
530 * Function: pop_retrieve
532 * Purpose: Retrieve a specified message from the maildrop.
535 * server The server to retrieve from.
536 * message The message number to retrieve.
538 * If true, then mark the string "From " at the beginning
540 * msg_buf Output parameter to which a buffer containing the
541 * message is assigned.
543 * Return value: The number of bytes in msg_buf, which may contain
544 * embedded nulls, not including its final null, or -1 on error
545 * with pop_error set.
547 * Side effects: May kill connection on error.
550 pop_retrieve (popserver server
, int message
, int markfrom
, char **msg_buf
)
552 int *IDs
, *sizes
, bufsize
, fromcount
= 0, cp
= 0;
553 char *ptr
, *fromserver
;
556 if (server
->in_multi
)
558 strcpy (pop_error
, "In multi-line query in pop_retrieve");
562 if (pop_list (server
, message
, &IDs
, &sizes
))
565 if (pop_retrieve_first (server
, message
, &fromserver
))
571 * The "5" below is an arbitrary constant -- I assume that if
572 * there are "From" lines in the text to be marked, there
573 * probably won't be more than 5 of them. If there are, I
574 * allocate more space for them below.
576 bufsize
= sizes
[0] + (markfrom
? 5 : 0);
577 ptr
= (char *)malloc (bufsize
);
579 free ((char *) sizes
);
583 strcpy (pop_error
, "Out of memory in pop_retrieve");
584 pop_retrieve_flush (server
);
588 while ((ret
= pop_retrieve_next (server
, &fromserver
)) >= 0)
596 if (markfrom
&& fromserver
[0] == 'F' && fromserver
[1] == 'r' &&
597 fromserver
[2] == 'o' && fromserver
[3] == 'm' &&
598 fromserver
[4] == ' ')
600 if (++fromcount
== 5)
603 ptr
= (char *)realloc (ptr
, bufsize
);
606 strcpy (pop_error
, "Out of memory in pop_retrieve");
607 pop_retrieve_flush (server
);
614 memcpy (&ptr
[cp
], fromserver
, ret
);
624 pop_retrieve_first (popserver server
, int message
, char **response
)
626 sprintf (pop_error
, "RETR %d", message
);
627 return (pop_multi_first (server
, pop_error
, response
));
631 Returns a negative number on error, 0 to indicate that the data has
632 all been read (i.e., the server has returned a "." termination
633 line), or a positive number indicating the number of bytes in the
634 returned buffer (which is null-terminated and may contain embedded
635 nulls, but the returned bytecount doesn't include the final null).
639 pop_retrieve_next (popserver server
, char **line
)
641 return (pop_multi_next (server
, line
));
645 pop_retrieve_flush (popserver server
)
647 return (pop_multi_flush (server
));
651 pop_top_first (popserver server
, int message
, int lines
, char **response
)
653 sprintf (pop_error
, "TOP %d %d", message
, lines
);
654 return (pop_multi_first (server
, pop_error
, response
));
658 Returns a negative number on error, 0 to indicate that the data has
659 all been read (i.e., the server has returned a "." termination
660 line), or a positive number indicating the number of bytes in the
661 returned buffer (which is null-terminated and may contain embedded
662 nulls, but the returned bytecount doesn't include the final null).
666 pop_top_next (popserver server
, char **line
)
668 return (pop_multi_next (server
, line
));
672 pop_top_flush (popserver server
)
674 return (pop_multi_flush (server
));
678 pop_multi_first (popserver server
, const char *command
, char **response
)
680 if (server
->in_multi
)
683 "Already in multi-line query in pop_multi_first");
687 if (sendline (server
, command
) || (pop_getline (server
, response
) < 0))
692 if (0 == strncmp (*response
, "-ERR", 4))
694 strncpy (pop_error
, *response
, ERROR_MAX
);
697 else if (0 == strncmp (*response
, "+OK", 3))
699 for (*response
+= 3; **response
== ' '; (*response
)++) /* empty */;
700 server
->in_multi
= 1;
706 "Unexpected response from server in pop_multi_first");
712 Read the next line of data from SERVER and place a pointer to it
713 into LINE. Return -1 on error, 0 if there are no more lines to read
714 (i.e., the server has returned a line containing only "."), or a
715 positive number indicating the number of bytes in the LINE buffer
716 (not including the final null). The data in that buffer may contain
717 embedded nulls, but does not contain the final CRLF. When returning
718 0, LINE is set to null. */
721 pop_multi_next (popserver server
, char **line
)
726 if (! server
->in_multi
)
728 strcpy (pop_error
, "Not in multi-line query in pop_multi_next");
732 if ((ret
= pop_getline (server
, &fromserver
)) < 0)
737 if (fromserver
[0] == '.')
742 server
->in_multi
= 0;
747 *line
= fromserver
+ 1;
759 pop_multi_flush (popserver server
)
764 if (! server
->in_multi
)
769 while ((ret
= pop_multi_next (server
, &line
)))
778 /* Function: pop_delete
780 * Purpose: Delete a specified message.
783 * server Server from which to delete the message.
784 * message Message to delete.
786 * Return value: 0 on success, non-zero with error in pop_error
790 pop_delete (popserver server
, int message
)
792 if (server
->in_multi
)
794 strcpy (pop_error
, "In multi-line query in pop_delete");
798 sprintf (pop_error
, "DELE %d", message
);
800 if (sendline (server
, pop_error
) || getok (server
))
809 * Purpose: Send a noop command to the server.
812 * server The server to send to.
814 * Return value: 0 on success, non-zero with error in pop_error
817 * Side effects: Closes connection on error.
820 pop_noop (popserver server
)
822 if (server
->in_multi
)
824 strcpy (pop_error
, "In multi-line query in pop_noop");
828 if (sendline (server
, "NOOP") || getok (server
))
837 * Purpose: Find out the highest seen message from the server.
842 * Return value: If successful, the highest seen message, which is
843 * greater than or equal to 0. Otherwise, a negative number with
844 * the error explained in pop_error.
846 * Side effects: Closes the connection on error.
849 pop_last (popserver server
)
853 if (server
->in_multi
)
855 strcpy (pop_error
, "In multi-line query in pop_last");
859 if (sendline (server
, "LAST"))
862 if (pop_getline (server
, &fromserver
) < 0)
865 if (! strncmp (fromserver
, "-ERR", 4))
867 strncpy (pop_error
, fromserver
, ERROR_MAX
);
870 else if (strncmp (fromserver
, "+OK ", 4))
872 strcpy (pop_error
, "Unexpected response from server in pop_last");
881 count
= strtol (&fromserver
[4], &end_ptr
, 10);
882 if (fromserver
+ 4 == end_ptr
|| errno
)
884 strcpy (pop_error
, "Unexpected response from server in pop_last");
893 * Function: pop_reset
895 * Purpose: Reset the server to its initial connect state
900 * Return value: 0 for success, non-0 with error in pop_error
903 * Side effects: Closes the connection on error.
906 pop_reset (popserver server
)
908 if (pop_retrieve_flush (server
))
913 if (sendline (server
, "RSET") || getok (server
))
922 * Purpose: Quit the connection to the server,
925 * server The server to quit.
927 * Return value: 0 for success, non-zero otherwise with error in
930 * Side Effects: The popserver passed in is unusable after this
931 * function is called, even if an error occurs.
934 pop_quit (popserver server
)
938 if (server
->file
>= 0)
940 if (pop_retrieve_flush (server
))
945 if (sendline (server
, "QUIT") || getok (server
))
950 close (server
->file
);
953 free (server
->buffer
);
954 free ((char *) server
);
960 static int have_winsock
= 0;
964 * Function: socket_connection
966 * Purpose: Opens the network connection with the mail host, without
967 * doing any sort of I/O with it or anything.
970 * host The host to which to connect.
971 * flags Option flags.
973 * Return value: A file descriptor indicating the connection, or -1
974 * indicating failure, in which case an error has been copied
978 socket_connection (char *host
, int flags
)
980 #ifdef HAVE_GETADDRINFO
981 struct addrinfo
*res
, *it
;
982 struct addrinfo hints
;
984 #else /* !HAVE_GETADDRINFO */
985 struct hostent
*hostent
;
987 struct servent
*servent
;
988 struct sockaddr_in addr
;
996 krb5_context kcontext
= 0;
997 krb5_auth_context auth_context
= 0;
999 krb5_principal client
, server
;
1000 krb5_error
*err_ret
;
1006 Key_schedule schedule
;
1008 #endif /* KERBEROS5 */
1009 #endif /* KERBEROS */
1016 WSADATA winsockData
;
1017 if (WSAStartup (0x101, &winsockData
) == 0)
1022 memset (&addr
, 0, sizeof (addr
));
1023 addr
.sin_family
= AF_INET
;
1025 /** "kpop" service is never used: look for 20060515 to see why **/
1027 service
= (flags
& POP_NO_KERBEROS
) ? POP_SERVICE
: KPOP_SERVICE
;
1029 service
= POP_SERVICE
;
1033 if (! (flags
& POP_NO_HESIOD
))
1035 servent
= hes_getservbyname (service
, "tcp");
1038 addr
.sin_port
= servent
->s_port
;
1045 servent
= getservbyname (service
, "tcp");
1048 addr
.sin_port
= servent
->s_port
;
1052 /** "kpop" service is never used: look for 20060515 to see why **/
1054 addr
.sin_port
= htons ((flags
& POP_NO_KERBEROS
) ?
1055 POP_PORT
: KPOP_PORT
);
1057 addr
.sin_port
= htons (POP_PORT
);
1062 #define POP_SOCKET_ERROR "Could not create socket for POP connection: "
1064 sock
= socket (PF_INET
, SOCK_STREAM
, 0);
1067 strcpy (pop_error
, POP_SOCKET_ERROR
);
1068 strncat (pop_error
, strerror (errno
),
1069 ERROR_MAX
- sizeof (POP_SOCKET_ERROR
));
1074 #ifdef HAVE_GETADDRINFO
1075 memset (&hints
, 0, sizeof(hints
));
1076 hints
.ai_socktype
= SOCK_STREAM
;
1077 hints
.ai_flags
= AI_CANONNAME
;
1078 hints
.ai_family
= AF_INET
;
1081 ret
= getaddrinfo (host
, service
, &hints
, &res
);
1083 if (ret
!= 0 && (ret
!= EAI_AGAIN
|| try_count
== 5))
1085 strcpy (pop_error
, "Could not determine POP server's address");
1095 if (it
->ai_addrlen
== sizeof (addr
))
1097 struct sockaddr_in
*in_a
= (struct sockaddr_in
*) it
->ai_addr
;
1098 memcpy (&addr
.sin_addr
, &in_a
->sin_addr
, sizeof (addr
.sin_addr
));
1099 if (! connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)))
1104 connect_ok
= it
!= NULL
;
1107 realhost
= alloca (strlen (it
->ai_canonname
) + 1);
1108 strcpy (realhost
, it
->ai_canonname
);
1112 #else /* !HAVE_GETADDRINFO */
1115 hostent
= gethostbyname (host
);
1117 if ((! hostent
) && ((h_errno
!= TRY_AGAIN
) || (try_count
== 5)))
1119 strcpy (pop_error
, "Could not determine POP server's address");
1122 } while (! hostent
);
1124 while (*hostent
->h_addr_list
)
1126 memcpy (&addr
.sin_addr
, *hostent
->h_addr_list
, hostent
->h_length
);
1127 if (! connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)))
1129 hostent
->h_addr_list
++;
1131 connect_ok
= *hostent
->h_addr_list
!= NULL
;
1134 realhost
= alloca (strlen (hostent
->h_name
) + 1);
1135 strcpy (realhost
, hostent
->h_name
);
1138 #endif /* !HAVE_GETADDRINFO */
1140 #define CONNECT_ERROR "Could not connect to POP server: "
1145 strcpy (pop_error
, CONNECT_ERROR
);
1146 strncat (pop_error
, strerror (errno
),
1147 ERROR_MAX
- sizeof (CONNECT_ERROR
));
1154 #define KRB_ERROR "Kerberos error connecting to POP server: "
1155 if (! (flags
& POP_NO_KERBEROS
))
1158 if ((rem
= krb5_init_context (&kcontext
)))
1162 krb5_auth_con_free (kcontext
, auth_context
);
1164 krb5_free_context (kcontext
);
1165 strcpy (pop_error
, KRB_ERROR
);
1166 strncat (pop_error
, error_message (rem
),
1167 ERROR_MAX
- sizeof(KRB_ERROR
));
1172 if ((rem
= krb5_auth_con_init (kcontext
, &auth_context
)))
1175 if (rem
= krb5_cc_default (kcontext
, &ccdef
))
1178 if (rem
= krb5_cc_get_principal (kcontext
, ccdef
, &client
))
1181 for (cp
= realhost
; *cp
; cp
++)
1185 *cp
= tolower (*cp
);
1189 if (rem
= krb5_sname_to_principal (kcontext
, realhost
,
1190 POP_SERVICE
, FALSE
, &server
))
1193 rem
= krb5_sendauth (kcontext
, &auth_context
,
1194 (krb5_pointer
) &sock
, "KPOPV1.0", client
, server
,
1195 AP_OPTS_MUTUAL_REQUIRED
,
1196 0, /* no checksum */
1197 0, /* no creds, use ccache instead */
1200 0, /* don't need subsession key */
1201 0); /* don't need reply */
1202 krb5_free_principal (kcontext
, server
);
1205 strcpy (pop_error
, KRB_ERROR
);
1206 strncat (pop_error
, error_message (rem
),
1207 ERROR_MAX
- sizeof (KRB_ERROR
));
1208 #if defined HAVE_KRB5_ERROR_TEXT
1209 if (err_ret
&& err_ret
->text
.length
)
1211 strncat (pop_error
, " [server says '",
1212 ERROR_MAX
- strlen (pop_error
) - 1);
1213 strncat (pop_error
, err_ret
->text
.data
,
1214 min (ERROR_MAX
- strlen (pop_error
) - 1,
1215 err_ret
->text
.length
));
1216 strncat (pop_error
, "']",
1217 ERROR_MAX
- strlen (pop_error
) - 1);
1219 #elif defined HAVE_KRB5_ERROR_E_TEXT
1220 if (err_ret
&& err_ret
->e_text
&& strlen(*err_ret
->e_text
))
1222 strncat (pop_error
, " [server says '",
1223 ERROR_MAX
- strlen (pop_error
) - 1);
1224 strncat (pop_error
, *err_ret
->e_text
,
1225 ERROR_MAX
- strlen (pop_error
) - 1);
1226 strncat (pop_error
, "']",
1227 ERROR_MAX
- strlen (pop_error
) - 1);
1231 krb5_free_error (kcontext
, err_ret
);
1232 krb5_auth_con_free (kcontext
, auth_context
);
1233 krb5_free_context (kcontext
);
1238 #else /* ! KERBEROS5 */
1239 ticket
= (KTEXT
) malloc (sizeof (KTEXT_ST
));
1240 rem
= krb_sendauth (0L, sock
, ticket
, "pop", realhost
,
1241 (char *) krb_realmofhost (realhost
),
1242 (unsigned long) 0, &msg_data
, &cred
, schedule
,
1243 (struct sockaddr_in
*) 0,
1244 (struct sockaddr_in
*) 0,
1246 free ((char *) ticket
);
1247 if (rem
!= KSUCCESS
)
1249 strcpy (pop_error
, KRB_ERROR
);
1250 strncat (pop_error
, krb_err_txt
[rem
],
1251 ERROR_MAX
- sizeof (KRB_ERROR
));
1255 #endif /* KERBEROS5 */
1257 #endif /* KERBEROS */
1260 } /* socket_connection */
1263 * Function: pop_getline
1265 * Purpose: Get a line of text from the connection and return a
1266 * pointer to it. The carriage return and linefeed at the end of
1267 * the line are stripped, but periods at the beginnings of lines
1268 * are NOT dealt with in any special way.
1271 * server The server from which to get the line of text.
1273 * Returns: The number of characters in the line, which is returned in
1274 * LINE, not including the final null. A return value of 0
1275 * indicates a blank line. A negative return value indicates an
1276 * error (in which case the contents of LINE are undefined. In
1277 * case of error, an error message is copied into pop_error.
1279 * Notes: The line returned is overwritten with each call to pop_getline.
1281 * Side effects: Closes the connection on error.
1283 * THE RETURNED LINE MAY CONTAIN EMBEDDED NULLS!
1286 pop_getline (popserver server
, char **line
)
1288 #define GETLINE_ERROR "Error reading from server: "
1291 int search_offset
= 0;
1295 char *cp
= find_crlf (server
->buffer
+ server
->buffer_index
,
1302 found
= server
->buffer_index
;
1303 data_used
= (cp
+ 2) - server
->buffer
- found
;
1305 *cp
= '\0'; /* terminate the string to be returned */
1306 server
->data
-= data_used
;
1307 server
->buffer_index
+= data_used
;
1310 /* Embedded nulls will truncate this output prematurely,
1311 but that's OK because it's just for debugging anyway. */
1312 fprintf (stderr
, "<<< %s\n", server
->buffer
+ found
);
1313 *line
= server
->buffer
+ found
;
1314 return (data_used
- 2);
1318 memmove (server
->buffer
, server
->buffer
+ server
->buffer_index
,
1320 /* Record the fact that we've searched the data already in
1321 the buffer for a CRLF, so that when we search below, we
1322 don't have to search the same data twice. There's a "-
1323 1" here to account for the fact that the last character
1324 of the data we have may be the CR of a CRLF pair, of
1325 which we haven't read the second half yet, so we may have
1326 to search it again when we read more data. */
1327 search_offset
= server
->data
- 1;
1328 server
->buffer_index
= 0;
1333 server
->buffer_index
= 0;
1338 /* There's a "- 1" here to leave room for the null that we put
1339 at the end of the read data below. We put the null there so
1340 that find_crlf knows where to stop when we call it. */
1341 if (server
->data
== server
->buffer_size
- 1)
1343 server
->buffer_size
+= GETLINE_INCR
;
1344 server
->buffer
= (char *)realloc (server
->buffer
, server
->buffer_size
);
1345 if (! server
->buffer
)
1347 strcpy (pop_error
, "Out of memory in pop_getline");
1352 ret
= RECV (server
->file
, server
->buffer
+ server
->data
,
1353 server
->buffer_size
- server
->data
- 1, 0);
1356 strcpy (pop_error
, GETLINE_ERROR
);
1357 strncat (pop_error
, strerror (errno
),
1358 ERROR_MAX
- sizeof (GETLINE_ERROR
));
1364 strcpy (pop_error
, "Unexpected EOF from server in pop_getline");
1371 server
->data
+= ret
;
1372 server
->buffer
[server
->data
] = '\0';
1374 cp
= find_crlf (server
->buffer
+ search_offset
,
1375 server
->data
- search_offset
);
1378 int data_used
= (cp
+ 2) - server
->buffer
;
1380 server
->data
-= data_used
;
1381 server
->buffer_index
= data_used
;
1384 fprintf (stderr
, "<<< %s\n", server
->buffer
);
1385 *line
= server
->buffer
;
1386 return (data_used
- 2);
1388 /* As above, the "- 1" here is to account for the fact that
1389 we may have read a CR without its accompanying LF. */
1390 search_offset
+= ret
- 1;
1398 * Function: sendline
1400 * Purpose: Sends a line of text to the POP server. The line of text
1401 * passed into this function should NOT have the carriage return
1402 * and linefeed on the end of it. Periods at beginnings of lines
1403 * will NOT be treated specially by this function.
1406 * server The server to which to send the text.
1407 * line The line of text to send.
1409 * Return value: Upon successful completion, a value of 0 will be
1410 * returned. Otherwise, a non-zero value will be returned, and
1411 * an error will be copied into pop_error.
1413 * Side effects: Closes the connection on error.
1416 sendline (popserver server
, const char *line
)
1418 #define SENDLINE_ERROR "Error writing to POP server: "
1422 /* Combine the string and the CR-LF into one buffer. Otherwise, two
1423 reasonable network stack optimizations, Nagle's algorithm and
1424 delayed acks, combine to delay us a fraction of a second on every
1425 message we send. (Movemail writes line without \r\n, client
1426 kernel sends packet, server kernel delays the ack to see if it
1427 can combine it with data, movemail writes \r\n, client kernel
1428 waits because it has unacked data already in its outgoing queue,
1429 client kernel eventually times out and sends.)
1431 This can be something like 0.2s per command, which can add up
1432 over a few dozen messages, and is a big chunk of the time we
1433 spend fetching mail from a server close by. */
1434 buf
= alloca (strlen (line
) + 3);
1436 strcat (buf
, "\r\n");
1437 ret
= fullwrite (server
->file
, buf
, strlen (buf
));
1442 strcpy (pop_error
, SENDLINE_ERROR
);
1443 strncat (pop_error
, strerror (errno
),
1444 ERROR_MAX
- sizeof (SENDLINE_ERROR
));
1449 fprintf (stderr
, ">>> %s\n", line
);
1455 * Procedure: fullwrite
1457 * Purpose: Just like write, but keeps trying until the entire string
1460 * Return value: Same as write. Pop_error is not set.
1463 fullwrite (int fd
, char *buf
, int nbytes
)
1469 while (nbytes
&& ((ret
= SEND (fd
, cp
, nbytes
, 0)) > 0))
1481 * Purpose: Reads a line from the server. If the return indicator is
1482 * positive, return with a zero exit status. If not, return with
1483 * a negative exit status.
1486 * server The server to read from.
1488 * Returns: 0 for success, else for failure and puts error in pop_error.
1490 * Side effects: On failure, may make the connection unusable.
1493 getok (popserver server
)
1497 if (pop_getline (server
, &fromline
) < 0)
1502 if (! strncmp (fromline
, "+OK", 3))
1504 else if (! strncmp (fromline
, "-ERR", 4))
1506 strncpy (pop_error
, fromline
, ERROR_MAX
);
1507 pop_error
[ERROR_MAX
-1] = '\0';
1513 "Unexpected response from server; expecting +OK or -ERR");
1521 * Function: gettermination
1523 * Purpose: Gets the next line and verifies that it is a termination
1524 * line (nothing but a dot).
1526 * Return value: 0 on success, non-zero with pop_error set on error.
1528 * Side effects: Closes the connection on error.
1531 gettermination (server
)
1536 if (pop_getline (server
, &fromserver
) < 0)
1539 if (strcmp (fromserver
, "."))
1542 "Unexpected response from server in gettermination");
1552 * Function pop_close
1554 * Purpose: Close a pop connection, sending a "RSET" command to try to
1555 * preserve any changes that were made and a "QUIT" command to
1556 * try to get the server to quit, but ignoring any responses that
1559 * Side effects: The server is unusable after this function returns.
1560 * Changes made to the maildrop since the session was started (or
1561 * since the last pop_reset) may be lost.
1564 pop_close (popserver server
)
1567 free ((char *) server
);
1573 * Function: pop_trash
1575 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
1576 * memory associated with the server. It is valid to call
1577 * pop_close or pop_quit after this function has been called.
1580 pop_trash (popserver server
)
1582 if (server
->file
>= 0)
1584 /* avoid recursion; sendline can call pop_trash */
1585 if (server
->trash_started
)
1587 server
->trash_started
= 1;
1589 sendline (server
, "RSET");
1590 sendline (server
, "QUIT");
1592 CLOSESOCKET (server
->file
);
1596 free (server
->buffer
);
1607 /* Return a pointer to the first CRLF in IN_STRING, which can contain
1608 embedded nulls and has LEN characters in it not including the final
1609 null, or 0 if it does not contain one. */
1612 find_crlf (char *in_string
, int len
)
1616 if (*in_string
== '\r')
1618 if (*++in_string
== '\n')
1619 return (in_string
- 1);
1627 #endif /* MAIL_USE_POP */