1 /* pop.c: client routines for talking to a POP3-protocol post-office server
3 Copyright (C) 1991, 1993, 1996-1997, 1999, 2001-2014 Free Software
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/>. */
28 #include <sys/types.h>
33 #define RECV(s,buf,len,flags) recv (s,buf,len,flags)
34 #define SEND(s,buf,len,flags) send (s,buf,len,flags)
35 #define CLOSESOCKET(s) closesocket (s)
37 #include <netinet/in.h>
38 #include <sys/socket.h>
39 #define RECV(s,buf,len,flags) read (s,buf,len)
40 #define SEND(s,buf,len,flags) write (s,buf,len)
41 #define CLOSESOCKET(s) close (s)
52 * It really shouldn't be necessary to put this declaration here, but
53 * the version of hesiod.h that Athena has installed in release 7.2
54 * doesn't declare this function; I don't know if the 7.3 version of
57 extern struct servent
*hes_getservbyname (/* char *, char * */);
74 # ifdef HAVE_KERBEROSIV_KRB_H
75 # include <kerberosIV/krb.h>
77 # ifdef HAVE_KERBEROS_KRB_H
78 # include <kerberos/krb.h>
82 # ifdef HAVE_COM_ERR_H
91 extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *,
92 u_long, MSG_DAT *, CREDENTIALS *, Key_schedule,
93 struct sockaddr_in *, struct sockaddr_in *,
95 extern char *krb_realmofhost (/* char * */);
96 #endif /* ! KERBEROS5 */
105 static int socket_connection (char *, int);
106 static int pop_getline (popserver
, char **);
107 static int sendline (popserver
, const char *);
108 static int fullwrite (int, char *, int);
109 static int getok (popserver
);
111 static int gettermination (popserver
);
113 static void pop_trash (popserver
);
114 static char *find_crlf (char *, int);
116 #define ERROR_MAX 160 /* a pretty arbitrary size, but needs
117 to be bigger than the original
120 #define POP_SERVICE "pop3" /* we don't want the POP2 port! */
122 #define KPOP_PORT 1109
123 #define KPOP_SERVICE "kpop" /* never used: look for 20060515 to see why */
126 char pop_error
[ERROR_MAX
];
127 bool pop_debug
= false;
130 * Function: pop_open (char *host, char *username, char *password,
133 * Purpose: Establishes a connection with a post-office server, and
134 * completes the authorization portion of the session.
137 * host The server host with which the connection should be
138 * established. Optional. If omitted, internal
139 * heuristics will be used to determine the server host,
142 * The username of the mail-drop to access. Optional.
143 * If omitted, internal heuristics will be used to
144 * determine the username, if possible.
146 * The password to use for authorization. If omitted,
147 * internal heuristics will be used to determine the
148 * password, if possible.
149 * flags A bit mask containing flags controlling certain
150 * functions of the routine. Valid flags are defined in
153 * Return value: Upon successful establishment of a connection, a
154 * non-null popserver will be returned. Otherwise, null will be
155 * returned, and the string variable pop_error will contain an
156 * explanation of the error.
159 pop_open (char *host
, char *username
, char *password
, int flags
)
164 /* Determine the user name */
167 username
= getenv ("USER");
168 if (! (username
&& *username
))
170 username
= getlogin ();
171 if (! (username
&& *username
))
173 struct passwd
*passwd
;
174 passwd
= getpwuid (getuid ());
175 if (passwd
&& passwd
->pw_name
&& *passwd
->pw_name
)
177 username
= passwd
->pw_name
;
181 strcpy (pop_error
, "Could not determine username");
189 * Determine the mail host.
194 host
= getenv ("MAILHOST");
198 if ((! host
) && (! (flags
& POP_NO_HESIOD
)))
200 struct hes_postoffice
*office
;
201 office
= hes_getmailhost (username
);
202 if (office
&& office
->po_type
&& (! strcmp (office
->po_type
, "POP"))
203 && office
->po_name
&& *office
->po_name
&& office
->po_host
206 host
= office
->po_host
;
207 username
= office
->po_name
;
221 strcpy (pop_error
, "Could not determine POP server");
225 /* Determine the password */
227 #define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS))
229 #define DONT_NEED_PASSWORD 0
232 if ((! password
) && (! DONT_NEED_PASSWORD
))
234 if (! (flags
& POP_NO_GETPASS
))
236 password
= getpass ("Enter POP password:");
240 strcpy (pop_error
, "Could not determine POP password");
244 if (password
) /* always true, detected 20060515 */
245 flags
|= POP_NO_KERBEROS
;
247 password
= username
; /* dead code, detected 20060515 */
248 /** "kpop" service is never used: look for 20060515 to see why **/
250 sock
= socket_connection (host
, flags
);
254 server
= (popserver
) malloc (sizeof (struct _popserver
));
257 strcpy (pop_error
, "Out of memory in pop_open");
260 server
->buffer
= (char *) malloc (GETLINE_MIN
);
261 if (! server
->buffer
)
263 strcpy (pop_error
, "Out of memory in pop_open");
264 free ((char *) server
);
270 server
->buffer_index
= 0;
271 server
->buffer_size
= GETLINE_MIN
;
272 server
->in_multi
= false;
273 server
->trash_started
= false;
279 * I really shouldn't use the pop_error variable like this, but....
281 if (strlen (username
) > ERROR_MAX
- 6)
285 "Username too long; recompile pop.c with larger ERROR_MAX");
288 sprintf (pop_error
, "USER %s", username
);
290 if (sendline (server
, pop_error
) || getok (server
))
295 if (strlen (password
) > ERROR_MAX
- 6)
299 "Password too long; recompile pop.c with larger ERROR_MAX");
302 sprintf (pop_error
, "PASS %s", password
);
304 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 (popserver server
, int *count
, int *size
)
331 if (server
->in_multi
)
333 strcpy (pop_error
, "In multi-line query in pop_stat");
337 if (sendline (server
, "STAT") || (pop_getline (server
, &fromserver
) < 0))
340 if (strncmp (fromserver
, "+OK ", 4))
342 if (0 == strncmp (fromserver
, "-ERR", 4))
343 snprintf (pop_error
, ERROR_MAX
, "%s", fromserver
);
347 "Unexpected response from POP server in pop_stat");
354 *count
= strtol (&fromserver
[4], &end_ptr
, 10);
355 /* Check validity of string-to-integer conversion. */
356 if (fromserver
+ 4 == end_ptr
|| *end_ptr
!= ' ' || errno
)
358 strcpy (pop_error
, "Unexpected response from POP server in pop_stat");
363 fromserver
= end_ptr
;
366 *size
= strtol (fromserver
+ 1, &end_ptr
, 10);
367 if (fromserver
+ 1 == end_ptr
|| errno
)
369 strcpy (pop_error
, "Unexpected response from POP server in pop_stat");
380 * Purpose: Performs the POP "list" command and returns (in value
381 * parameters) two malloc'd zero-terminated arrays -- one of
382 * message IDs, and a parallel one of sizes.
385 * server The pop connection to talk to.
386 * message The number of the one message about which to get
387 * information, or 0 to get information about all
390 * Return value: 0 on success, non-zero with error in pop_error on
393 * Side effects: On failure, may make further operations on the
394 * connection impossible.
397 pop_list (popserver server
, int message
, int **IDs
, int **sizes
)
402 if (server
->in_multi
)
404 strcpy (pop_error
, "In multi-line query in pop_list");
413 if (pop_stat (server
, &count
, &size
))
418 *IDs
= (int *) malloc ((how_many
+ 1) * sizeof (int));
419 *sizes
= (int *) malloc ((how_many
+ 1) * sizeof (int));
420 if (! (*IDs
&& *sizes
))
422 strcpy (pop_error
, "Out of memory in pop_list");
428 sprintf (pop_error
, "LIST %d", message
);
429 if (sendline (server
, pop_error
))
431 free ((char *) *IDs
);
432 free ((char *) *sizes
);
435 if (pop_getline (server
, &fromserver
) < 0)
437 free ((char *) *IDs
);
438 free ((char *) *sizes
);
441 if (strncmp (fromserver
, "+OK ", 4))
443 if (! strncmp (fromserver
, "-ERR", 4))
444 snprintf (pop_error
, ERROR_MAX
, "%s", fromserver
);
448 "Unexpected response from server in pop_list");
451 free ((char *) *IDs
);
452 free ((char *) *sizes
);
455 (*IDs
)[0] = atoi (&fromserver
[4]);
456 fromserver
= strchr (&fromserver
[4], ' ');
460 "Badly formatted response from server in pop_list");
462 free ((char *) *IDs
);
463 free ((char *) *sizes
);
466 (*sizes
)[0] = atoi (fromserver
);
467 (*IDs
)[1] = (*sizes
)[1] = 0;
472 if (pop_multi_first (server
, "LIST", &fromserver
))
474 free ((char *) *IDs
);
475 free ((char *) *sizes
);
478 for (i
= 0; i
< how_many
; i
++)
480 if (pop_multi_next (server
, &fromserver
) <= 0)
482 free ((char *) *IDs
);
483 free ((char *) *sizes
);
486 (*IDs
)[i
] = atoi (fromserver
);
487 fromserver
= strchr (fromserver
, ' ');
491 "Badly formatted response from server in pop_list");
492 free ((char *) *IDs
);
493 free ((char *) *sizes
);
497 (*sizes
)[i
] = atoi (fromserver
);
499 if (pop_multi_next (server
, &fromserver
) < 0)
501 free ((char *) *IDs
);
502 free ((char *) *sizes
);
508 "Too many response lines from server in pop_list");
509 free ((char *) *IDs
);
510 free ((char *) *sizes
);
513 (*IDs
)[i
] = (*sizes
)[i
] = 0;
519 * Function: pop_retrieve
521 * Purpose: Retrieve a specified message from the maildrop.
524 * server The server to retrieve from.
525 * message The message number to retrieve.
527 * If true, then mark the string "From " at the beginning
529 * msg_buf Output parameter to which a buffer containing the
530 * message is assigned.
532 * Return value: The number of bytes in msg_buf, which may contain
533 * embedded nulls, not including its final null, or -1 on error
534 * with pop_error set.
536 * Side effects: May kill connection on error.
539 pop_retrieve (popserver server
, int message
, int markfrom
, char **msg_buf
)
541 int *IDs
, *sizes
, bufsize
, fromcount
= 0, cp
= 0;
542 char *ptr
, *fromserver
;
545 if (server
->in_multi
)
547 strcpy (pop_error
, "In multi-line query in pop_retrieve");
551 if (pop_list (server
, message
, &IDs
, &sizes
))
554 if (pop_retrieve_first (server
, message
, &fromserver
))
560 * The "5" below is an arbitrary constant -- I assume that if
561 * there are "From" lines in the text to be marked, there
562 * probably won't be more than 5 of them. If there are, I
563 * allocate more space for them below.
565 bufsize
= sizes
[0] + (markfrom
? 5 : 0);
566 ptr
= (char *)malloc (bufsize
);
568 free ((char *) sizes
);
572 strcpy (pop_error
, "Out of memory in pop_retrieve");
573 pop_retrieve_flush (server
);
577 while ((ret
= pop_retrieve_next (server
, &fromserver
)) >= 0)
585 if (markfrom
&& fromserver
[0] == 'F' && fromserver
[1] == 'r' &&
586 fromserver
[2] == 'o' && fromserver
[3] == 'm' &&
587 fromserver
[4] == ' ')
589 if (++fromcount
== 5)
592 ptr
= (char *)realloc (ptr
, bufsize
);
595 strcpy (pop_error
, "Out of memory in pop_retrieve");
596 pop_retrieve_flush (server
);
603 memcpy (&ptr
[cp
], fromserver
, ret
);
613 pop_retrieve_first (popserver server
, int message
, char **response
)
615 sprintf (pop_error
, "RETR %d", message
);
616 return (pop_multi_first (server
, pop_error
, response
));
620 Returns a negative number on error, 0 to indicate that the data has
621 all been read (i.e., the server has returned a "." termination
622 line), or a positive number indicating the number of bytes in the
623 returned buffer (which is null-terminated and may contain embedded
624 nulls, but the returned bytecount doesn't include the final null).
628 pop_retrieve_next (popserver server
, char **line
)
630 return (pop_multi_next (server
, line
));
634 pop_retrieve_flush (popserver server
)
636 return (pop_multi_flush (server
));
640 pop_top_first (popserver server
, int message
, int lines
, char **response
)
642 sprintf (pop_error
, "TOP %d %d", message
, lines
);
643 return (pop_multi_first (server
, pop_error
, response
));
647 Returns a negative number on error, 0 to indicate that the data has
648 all been read (i.e., the server has returned a "." termination
649 line), or a positive number indicating the number of bytes in the
650 returned buffer (which is null-terminated and may contain embedded
651 nulls, but the returned bytecount doesn't include the final null).
655 pop_top_next (popserver server
, char **line
)
657 return (pop_multi_next (server
, line
));
661 pop_top_flush (popserver server
)
663 return (pop_multi_flush (server
));
667 pop_multi_first (popserver server
, const char *command
, char **response
)
669 if (server
->in_multi
)
672 "Already in multi-line query in pop_multi_first");
676 if (sendline (server
, command
) || (pop_getline (server
, response
) < 0))
681 if (0 == strncmp (*response
, "-ERR", 4))
683 snprintf (pop_error
, ERROR_MAX
, "%s", *response
);
686 else if (0 == strncmp (*response
, "+OK", 3))
688 for (*response
+= 3; **response
== ' '; (*response
)++) /* empty */;
689 server
->in_multi
= true;
695 "Unexpected response from server in pop_multi_first");
701 Read the next line of data from SERVER and place a pointer to it
702 into LINE. Return -1 on error, 0 if there are no more lines to read
703 (i.e., the server has returned a line containing only "."), or a
704 positive number indicating the number of bytes in the LINE buffer
705 (not including the final null). The data in that buffer may contain
706 embedded nulls, but does not contain the final CRLF. When returning
707 0, LINE is set to null. */
710 pop_multi_next (popserver server
, char **line
)
715 if (! server
->in_multi
)
717 strcpy (pop_error
, "Not in multi-line query in pop_multi_next");
721 if ((ret
= pop_getline (server
, &fromserver
)) < 0)
726 if (fromserver
[0] == '.')
731 server
->in_multi
= false;
736 *line
= fromserver
+ 1;
748 pop_multi_flush (popserver server
)
753 if (! server
->in_multi
)
758 while ((ret
= pop_multi_next (server
, &line
)))
767 /* Function: pop_delete
769 * Purpose: Delete a specified message.
772 * server Server from which to delete the message.
773 * message Message to delete.
775 * Return value: 0 on success, non-zero with error in pop_error
779 pop_delete (popserver server
, int message
)
781 if (server
->in_multi
)
783 strcpy (pop_error
, "In multi-line query in pop_delete");
787 sprintf (pop_error
, "DELE %d", message
);
789 if (sendline (server
, pop_error
) || getok (server
))
798 * Purpose: Send a noop command to the server.
801 * server The server to send to.
803 * Return value: 0 on success, non-zero with error in pop_error
806 * Side effects: Closes connection on error.
809 pop_noop (popserver server
)
811 if (server
->in_multi
)
813 strcpy (pop_error
, "In multi-line query in pop_noop");
817 if (sendline (server
, "NOOP") || getok (server
))
826 * Purpose: Find out the highest seen message from the server.
831 * Return value: If successful, the highest seen message, which is
832 * greater than or equal to 0. Otherwise, a negative number with
833 * the error explained in pop_error.
835 * Side effects: Closes the connection on error.
838 pop_last (popserver server
)
842 if (server
->in_multi
)
844 strcpy (pop_error
, "In multi-line query in pop_last");
848 if (sendline (server
, "LAST"))
851 if (pop_getline (server
, &fromserver
) < 0)
854 if (! strncmp (fromserver
, "-ERR", 4))
856 snprintf (pop_error
, ERROR_MAX
, "%s", fromserver
);
859 else if (strncmp (fromserver
, "+OK ", 4))
861 strcpy (pop_error
, "Unexpected response from server in pop_last");
870 count
= strtol (&fromserver
[4], &end_ptr
, 10);
871 if (fromserver
+ 4 == end_ptr
|| errno
)
873 strcpy (pop_error
, "Unexpected response from server in pop_last");
882 * Function: pop_reset
884 * Purpose: Reset the server to its initial connect state
889 * Return value: 0 for success, non-0 with error in pop_error
892 * Side effects: Closes the connection on error.
895 pop_reset (popserver server
)
897 if (pop_retrieve_flush (server
))
902 if (sendline (server
, "RSET") || getok (server
))
911 * Purpose: Quit the connection to the server,
914 * server The server to quit.
916 * Return value: 0 for success, non-zero otherwise with error in
919 * Side Effects: The popserver passed in is unusable after this
920 * function is called, even if an error occurs.
923 pop_quit (popserver server
)
927 if (server
->file
>= 0)
929 if (pop_retrieve_flush (server
))
934 if (sendline (server
, "QUIT") || getok (server
))
939 close (server
->file
);
942 free (server
->buffer
);
943 free ((char *) server
);
949 static int have_winsock
= 0;
953 * Function: socket_connection
955 * Purpose: Opens the network connection with the mail host, without
956 * doing any sort of I/O with it or anything.
959 * host The host to which to connect.
960 * flags Option flags.
962 * Return value: A file descriptor indicating the connection, or -1
963 * indicating failure, in which case an error has been copied
967 socket_connection (char *host
, int flags
)
969 #ifdef HAVE_GETADDRINFO
970 struct addrinfo
*res
, *it
;
971 struct addrinfo hints
;
973 #else /* !HAVE_GETADDRINFO */
974 struct hostent
*hostent
;
976 struct servent
*servent
;
977 struct sockaddr_in addr
;
985 krb5_context kcontext
= 0;
986 krb5_auth_context auth_context
= 0;
988 krb5_principal client
, server
;
995 Key_schedule schedule
;
997 #endif /* KERBEROS5 */
998 #endif /* KERBEROS */
1005 WSADATA winsockData
;
1006 if (WSAStartup (0x101, &winsockData
) == 0)
1011 memset (&addr
, 0, sizeof (addr
));
1012 addr
.sin_family
= AF_INET
;
1014 /** "kpop" service is never used: look for 20060515 to see why **/
1016 service
= (flags
& POP_NO_KERBEROS
) ? POP_SERVICE
: KPOP_SERVICE
;
1018 service
= POP_SERVICE
;
1022 if (! (flags
& POP_NO_HESIOD
))
1024 servent
= hes_getservbyname (service
, "tcp");
1027 addr
.sin_port
= servent
->s_port
;
1034 servent
= getservbyname (service
, "tcp");
1037 addr
.sin_port
= servent
->s_port
;
1041 /** "kpop" service is never used: look for 20060515 to see why **/
1043 addr
.sin_port
= htons ((flags
& POP_NO_KERBEROS
) ?
1044 POP_PORT
: KPOP_PORT
);
1046 addr
.sin_port
= htons (POP_PORT
);
1051 #define POP_SOCKET_ERROR "Could not create socket for POP connection: "
1053 sock
= socket (PF_INET
, SOCK_STREAM
, 0);
1056 snprintf (pop_error
, ERROR_MAX
, "%s%s",
1057 POP_SOCKET_ERROR
, strerror (errno
));
1062 #ifdef HAVE_GETADDRINFO
1063 memset (&hints
, 0, sizeof (hints
));
1064 hints
.ai_socktype
= SOCK_STREAM
;
1065 hints
.ai_flags
= AI_CANONNAME
;
1066 hints
.ai_family
= AF_INET
;
1069 ret
= getaddrinfo (host
, service
, &hints
, &res
);
1071 if (ret
!= 0 && (ret
!= EAI_AGAIN
|| try_count
== 5))
1073 strcpy (pop_error
, "Could not determine POP server's address");
1078 for (it
= res
; it
; it
= it
->ai_next
)
1079 if (it
->ai_addrlen
== sizeof addr
)
1081 struct sockaddr_in
*in_a
= (struct sockaddr_in
*) it
->ai_addr
;
1082 addr
.sin_addr
= in_a
->sin_addr
;
1083 if (! connect (sock
, (struct sockaddr
*) &addr
, sizeof addr
))
1086 connect_ok
= it
!= NULL
;
1089 realhost
= alloca (strlen (it
->ai_canonname
) + 1);
1090 strcpy (realhost
, it
->ai_canonname
);
1094 #else /* !HAVE_GETADDRINFO */
1097 hostent
= gethostbyname (host
);
1099 if ((! hostent
) && ((h_errno
!= TRY_AGAIN
) || (try_count
== 5)))
1101 strcpy (pop_error
, "Could not determine POP server's address");
1104 } while (! hostent
);
1106 while (*hostent
->h_addr_list
)
1108 memcpy (&addr
.sin_addr
, *hostent
->h_addr_list
, hostent
->h_length
);
1109 if (! connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)))
1111 hostent
->h_addr_list
++;
1113 connect_ok
= *hostent
->h_addr_list
!= NULL
;
1116 realhost
= alloca (strlen (hostent
->h_name
) + 1);
1117 strcpy (realhost
, hostent
->h_name
);
1120 #endif /* !HAVE_GETADDRINFO */
1122 #define CONNECT_ERROR "Could not connect to POP server: "
1127 snprintf (pop_error
, ERROR_MAX
, "%s%s", CONNECT_ERROR
, strerror (errno
));
1134 #define KRB_ERROR "Kerberos error connecting to POP server: "
1135 if (! (flags
& POP_NO_KERBEROS
))
1138 if ((rem
= krb5_init_context (&kcontext
)))
1142 krb5_auth_con_free (kcontext
, auth_context
);
1144 krb5_free_context (kcontext
);
1145 snprintf (pop_error
, ERROR_MAX
, "%s%s",
1146 KRB_ERROR
, error_message (rem
));
1151 if ((rem
= krb5_auth_con_init (kcontext
, &auth_context
)))
1154 if (rem
= krb5_cc_default (kcontext
, &ccdef
))
1157 if (rem
= krb5_cc_get_principal (kcontext
, ccdef
, &client
))
1160 for (cp
= realhost
; *cp
; cp
++)
1164 *cp
= tolower (*cp
);
1168 if (rem
= krb5_sname_to_principal (kcontext
, realhost
,
1169 POP_SERVICE
, FALSE
, &server
))
1172 rem
= krb5_sendauth (kcontext
, &auth_context
,
1173 (krb5_pointer
) &sock
, "KPOPV1.0", client
, server
,
1174 AP_OPTS_MUTUAL_REQUIRED
,
1175 0, /* no checksum */
1176 0, /* no creds, use ccache instead */
1179 0, /* don't need subsession key */
1180 0); /* don't need reply */
1181 krb5_free_principal (kcontext
, server
);
1184 int pop_error_len
= snprintf (pop_error
, ERROR_MAX
, "%s%s",
1185 KRB_ERROR
, error_message (rem
));
1186 #if defined HAVE_KRB5_ERROR_TEXT
1187 if (err_ret
&& err_ret
->text
.length
)
1189 int errlen
= err_ret
->text
.length
;
1190 snprintf (pop_error
+ pop_error_len
, ERROR_MAX
- pop_error_len
,
1191 " [server says '.*%s']", errlen
, err_ret
->text
.data
);
1193 #elif defined HAVE_KRB5_ERROR_E_TEXT
1194 if (err_ret
&& err_ret
->e_text
&& **err_ret
->e_text
)
1195 snprintf (pop_error
+ pop_error_len
, ERROR_MAX
- pop_error_len
,
1196 " [server says '%s']", *err_ret
->e_text
);
1199 krb5_free_error (kcontext
, err_ret
);
1200 krb5_auth_con_free (kcontext
, auth_context
);
1201 krb5_free_context (kcontext
);
1206 #else /* ! KERBEROS5 */
1207 ticket
= (KTEXT
) malloc (sizeof (KTEXT_ST
));
1208 rem
= krb_sendauth (0L, sock
, ticket
, "pop", realhost
,
1209 (char *) krb_realmofhost (realhost
),
1210 (unsigned long) 0, &msg_data
, &cred
, schedule
,
1211 (struct sockaddr_in
*) 0,
1212 (struct sockaddr_in
*) 0,
1214 free ((char *) ticket
);
1215 if (rem
!= KSUCCESS
)
1217 snprintf (pop_error
, ERROR_MAX
, "%s%s", KRB_ERROR
, krb_err_txt
[rem
]);
1221 #endif /* KERBEROS5 */
1223 #endif /* KERBEROS */
1226 } /* socket_connection */
1229 * Function: pop_getline
1231 * Purpose: Get a line of text from the connection and return a
1232 * pointer to it. The carriage return and linefeed at the end of
1233 * the line are stripped, but periods at the beginnings of lines
1234 * are NOT dealt with in any special way.
1237 * server The server from which to get the line of text.
1239 * Returns: The number of characters in the line, which is returned in
1240 * LINE, not including the final null. A return value of 0
1241 * indicates a blank line. A negative return value indicates an
1242 * error (in which case the contents of LINE are undefined. In
1243 * case of error, an error message is copied into pop_error.
1245 * Notes: The line returned is overwritten with each call to pop_getline.
1247 * Side effects: Closes the connection on error.
1249 * THE RETURNED LINE MAY CONTAIN EMBEDDED NULLS!
1252 pop_getline (popserver server
, char **line
)
1254 #define GETLINE_ERROR "Error reading from server: "
1257 int search_offset
= 0;
1261 char *cp
= find_crlf (server
->buffer
+ server
->buffer_index
,
1268 found
= server
->buffer_index
;
1269 data_used
= (cp
+ 2) - server
->buffer
- found
;
1271 *cp
= '\0'; /* terminate the string to be returned */
1272 server
->data
-= data_used
;
1273 server
->buffer_index
+= data_used
;
1276 /* Embedded nulls will truncate this output prematurely,
1277 but that's OK because it's just for debugging anyway. */
1278 fprintf (stderr
, "<<< %s\n", server
->buffer
+ found
);
1279 *line
= server
->buffer
+ found
;
1280 return (data_used
- 2);
1284 memmove (server
->buffer
, server
->buffer
+ server
->buffer_index
,
1286 /* Record the fact that we've searched the data already in
1287 the buffer for a CRLF, so that when we search below, we
1288 don't have to search the same data twice. There's a "-
1289 1" here to account for the fact that the last character
1290 of the data we have may be the CR of a CRLF pair, of
1291 which we haven't read the second half yet, so we may have
1292 to search it again when we read more data. */
1293 search_offset
= server
->data
- 1;
1294 server
->buffer_index
= 0;
1299 server
->buffer_index
= 0;
1304 /* There's a "- 1" here to leave room for the null that we put
1305 at the end of the read data below. We put the null there so
1306 that find_crlf knows where to stop when we call it. */
1307 if (server
->data
== server
->buffer_size
- 1)
1309 server
->buffer_size
+= GETLINE_INCR
;
1310 server
->buffer
= (char *)realloc (server
->buffer
, server
->buffer_size
);
1311 if (! server
->buffer
)
1313 strcpy (pop_error
, "Out of memory in pop_getline");
1318 ret
= RECV (server
->file
, server
->buffer
+ server
->data
,
1319 server
->buffer_size
- server
->data
- 1, 0);
1322 snprintf (pop_error
, ERROR_MAX
, "%s%s",
1323 GETLINE_ERROR
, strerror (errno
));
1329 strcpy (pop_error
, "Unexpected EOF from server in pop_getline");
1336 server
->data
+= ret
;
1337 server
->buffer
[server
->data
] = '\0';
1339 cp
= find_crlf (server
->buffer
+ search_offset
,
1340 server
->data
- search_offset
);
1343 int data_used
= (cp
+ 2) - server
->buffer
;
1345 server
->data
-= data_used
;
1346 server
->buffer_index
= data_used
;
1349 fprintf (stderr
, "<<< %s\n", server
->buffer
);
1350 *line
= server
->buffer
;
1351 return (data_used
- 2);
1353 /* As above, the "- 1" here is to account for the fact that
1354 we may have read a CR without its accompanying LF. */
1355 search_offset
+= ret
- 1;
1363 * Function: sendline
1365 * Purpose: Sends a line of text to the POP server. The line of text
1366 * passed into this function should NOT have the carriage return
1367 * and linefeed on the end of it. Periods at beginnings of lines
1368 * will NOT be treated specially by this function.
1371 * server The server to which to send the text.
1372 * line The line of text to send.
1374 * Return value: Upon successful completion, a value of 0 will be
1375 * returned. Otherwise, a non-zero value will be returned, and
1376 * an error will be copied into pop_error.
1378 * Side effects: Closes the connection on error.
1381 sendline (popserver server
, const char *line
)
1383 #define SENDLINE_ERROR "Error writing to POP server: "
1387 /* Combine the string and the CR-LF into one buffer. Otherwise, two
1388 reasonable network stack optimizations, Nagle's algorithm and
1389 delayed acks, combine to delay us a fraction of a second on every
1390 message we send. (Movemail writes line without \r\n, client
1391 kernel sends packet, server kernel delays the ack to see if it
1392 can combine it with data, movemail writes \r\n, client kernel
1393 waits because it has unacked data already in its outgoing queue,
1394 client kernel eventually times out and sends.)
1396 This can be something like 0.2s per command, which can add up
1397 over a few dozen messages, and is a big chunk of the time we
1398 spend fetching mail from a server close by. */
1399 buf
= alloca (strlen (line
) + 3);
1401 strcat (buf
, "\r\n");
1402 ret
= fullwrite (server
->file
, buf
, strlen (buf
));
1407 snprintf (pop_error
, ERROR_MAX
, "%s%s", SENDLINE_ERROR
, strerror (errno
));
1412 fprintf (stderr
, ">>> %s\n", line
);
1418 * Procedure: fullwrite
1420 * Purpose: Just like write, but keeps trying until the entire string
1423 * Return value: Same as write. Pop_error is not set.
1426 fullwrite (int fd
, char *buf
, int nbytes
)
1432 while (nbytes
&& ((ret
= SEND (fd
, cp
, nbytes
, 0)) > 0))
1444 * Purpose: Reads a line from the server. If the return indicator is
1445 * positive, return with a zero exit status. If not, return with
1446 * a negative exit status.
1449 * server The server to read from.
1451 * Returns: 0 for success, else for failure and puts error in pop_error.
1453 * Side effects: On failure, may make the connection unusable.
1456 getok (popserver server
)
1460 if (pop_getline (server
, &fromline
) < 0)
1465 if (! strncmp (fromline
, "+OK", 3))
1467 else if (! strncmp (fromline
, "-ERR", 4))
1469 snprintf (pop_error
, ERROR_MAX
, "%s", fromline
);
1475 "Unexpected response from server; expecting +OK or -ERR");
1483 * Function: gettermination
1485 * Purpose: Gets the next line and verifies that it is a termination
1486 * line (nothing but a dot).
1488 * Return value: 0 on success, non-zero with pop_error set on error.
1490 * Side effects: Closes the connection on error.
1493 gettermination (server
)
1498 if (pop_getline (server
, &fromserver
) < 0)
1501 if (strcmp (fromserver
, "."))
1504 "Unexpected response from server in gettermination");
1514 * Function pop_close
1516 * Purpose: Close a pop connection, sending a "RSET" command to try to
1517 * preserve any changes that were made and a "QUIT" command to
1518 * try to get the server to quit, but ignoring any responses that
1521 * Side effects: The server is unusable after this function returns.
1522 * Changes made to the maildrop since the session was started (or
1523 * since the last pop_reset) may be lost.
1526 pop_close (popserver server
)
1529 free ((char *) server
);
1535 * Function: pop_trash
1537 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
1538 * memory associated with the server. It is valid to call
1539 * pop_close or pop_quit after this function has been called.
1542 pop_trash (popserver server
)
1544 if (server
->file
>= 0)
1546 /* avoid recursion; sendline can call pop_trash */
1547 if (server
->trash_started
)
1549 server
->trash_started
= true;
1551 sendline (server
, "RSET");
1552 sendline (server
, "QUIT");
1554 CLOSESOCKET (server
->file
);
1558 free (server
->buffer
);
1569 /* Return a pointer to the first CRLF in IN_STRING, which can contain
1570 embedded nulls and has LEN characters in it not including the final
1571 null, or 0 if it does not contain one. */
1574 find_crlf (char *in_string
, int len
)
1578 if (*in_string
== '\r')
1580 if (*++in_string
== '\n')
1581 return (in_string
- 1);
1589 #endif /* MAIL_USE_POP */