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, 2008 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 3, 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. */
31 #include <sys/types.h>
36 #define RECV(s,buf,len,flags) recv(s,buf,len,flags)
37 #define SEND(s,buf,len,flags) send(s,buf,len,flags)
38 #define CLOSESOCKET(s) closesocket(s)
40 #include <netinet/in.h>
41 #include <sys/socket.h>
42 #define RECV(s,buf,len,flags) read(s,buf,len)
43 #define SEND(s,buf,len,flags) write(s,buf,len)
44 #define CLOSESOCKET(s) close(s)
55 * It really shouldn't be necessary to put this declaration here, but
56 * the version of hesiod.h that Athena has installed in release 7.2
57 * doesn't declare this function; I don't know if the 7.3 version of
60 extern struct servent
*hes_getservbyname (/* char *, char * */);
82 # ifdef HAVE_KERBEROSIV_KRB_H
83 # include <kerberosIV/krb.h>
85 # ifdef HAVE_KERBEROS_KRB_H
86 # include <kerberos/krb.h>
90 # ifdef HAVE_COM_ERR_H
97 extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *,
98 u_long, MSG_DAT *, CREDENTIALS *, Key_schedule,
99 struct sockaddr_in *, struct sockaddr_in *,
101 extern char *krb_realmofhost (/* char * */);
102 #endif /* ! KERBEROS5 */
103 #endif /* KERBEROS */
106 #if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H)
116 # endif /* __STDC__ */
119 static int socket_connection
__P((char *, int));
120 static int pop_getline
__P((popserver
, char **));
121 static int sendline
__P((popserver
, char *));
122 static int fullwrite
__P((int, char *, int));
123 static int getok
__P((popserver
));
125 static int gettermination
__P((popserver
));
127 static void pop_trash
__P((popserver
));
128 static char *find_crlf
__P((char *, int));
130 #define ERROR_MAX 160 /* a pretty arbitrary size, but needs
131 to be bigger than the original
134 #define KPOP_PORT 1109
135 #define POP_SERVICE "pop3" /* we don't want the POP2 port! */
137 #define KPOP_SERVICE "kpop" /* never used: look for 20060515 to see why */
140 char pop_error
[ERROR_MAX
];
144 #define min(a,b) (((a) < (b)) ? (a) : (b))
148 * Function: pop_open (char *host, char *username, char *password,
151 * Purpose: Establishes a connection with a post-office server, and
152 * completes the authorization portion of the session.
155 * host The server host with which the connection should be
156 * established. Optional. If omitted, internal
157 * heuristics will be used to determine the server host,
160 * The username of the mail-drop to access. Optional.
161 * If omitted, internal heuristics will be used to
162 * determine the username, if possible.
164 * The password to use for authorization. If omitted,
165 * internal heuristics will be used to determine the
166 * password, if possible.
167 * flags A bit mask containing flags controlling certain
168 * functions of the routine. Valid flags are defined in
171 * Return value: Upon successful establishment of a connection, a
172 * non-null popserver will be returned. Otherwise, null will be
173 * returned, and the string variable pop_error will contain an
174 * explanation of the error.
177 pop_open (host
, username
, password
, flags
)
186 /* Determine the user name */
189 username
= getenv ("USER");
190 if (! (username
&& *username
))
192 username
= getlogin ();
193 if (! (username
&& *username
))
195 struct passwd
*passwd
;
196 passwd
= getpwuid (getuid ());
197 if (passwd
&& passwd
->pw_name
&& *passwd
->pw_name
)
199 username
= passwd
->pw_name
;
203 strcpy (pop_error
, "Could not determine username");
211 * Determine the mail host.
216 host
= getenv ("MAILHOST");
220 if ((! host
) && (! (flags
& POP_NO_HESIOD
)))
222 struct hes_postoffice
*office
;
223 office
= hes_getmailhost (username
);
224 if (office
&& office
->po_type
&& (! strcmp (office
->po_type
, "POP"))
225 && office
->po_name
&& *office
->po_name
&& office
->po_host
228 host
= office
->po_host
;
229 username
= office
->po_name
;
243 strcpy (pop_error
, "Could not determine POP server");
247 /* Determine the password */
249 #define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS))
251 #define DONT_NEED_PASSWORD 0
254 if ((! password
) && (! DONT_NEED_PASSWORD
))
256 if (! (flags
& POP_NO_GETPASS
))
258 password
= getpass ("Enter POP password:");
262 strcpy (pop_error
, "Could not determine POP password");
266 if (password
) /* always true, detected 20060515 */
267 flags
|= POP_NO_KERBEROS
;
269 password
= username
; /* dead code, detected 20060515 */
270 /** "kpop" service is never used: look for 20060515 to see why **/
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
)
356 if (server
->in_multi
)
358 strcpy (pop_error
, "In multi-line query in pop_stat");
362 if (sendline (server
, "STAT") || (pop_getline (server
, &fromserver
) < 0))
365 if (strncmp (fromserver
, "+OK ", 4))
367 if (0 == strncmp (fromserver
, "-ERR", 4))
369 strncpy (pop_error
, fromserver
, ERROR_MAX
);
374 "Unexpected response from POP server in pop_stat");
381 *count
= strtol (&fromserver
[4], &end_ptr
, 10);
382 /* Check validity of string-to-integer conversion. */
383 if (fromserver
+ 4 == end_ptr
|| *end_ptr
!= ' ' || errno
)
385 strcpy (pop_error
, "Unexpected response from POP server in pop_stat");
390 fromserver
= end_ptr
;
393 *size
= strtol (fromserver
+ 1, &end_ptr
, 10);
394 if (fromserver
+ 1 == end_ptr
|| errno
)
396 strcpy (pop_error
, "Unexpected response from POP server in pop_stat");
407 * Purpose: Performs the POP "list" command and returns (in value
408 * parameters) two malloc'd zero-terminated arrays -- one of
409 * message IDs, and a parallel one of sizes.
412 * server The pop connection to talk to.
413 * message The number of the one message about which to get
414 * information, or 0 to get information about all
417 * Return value: 0 on success, non-zero with error in pop_error on
420 * Side effects: On failure, may make further operations on the
421 * connection impossible.
424 pop_list (server
, message
, IDs
, sizes
)
433 if (server
->in_multi
)
435 strcpy (pop_error
, "In multi-line query in pop_list");
444 if (pop_stat (server
, &count
, &size
))
449 *IDs
= (int *) malloc ((how_many
+ 1) * sizeof (int));
450 *sizes
= (int *) malloc ((how_many
+ 1) * sizeof (int));
451 if (! (*IDs
&& *sizes
))
453 strcpy (pop_error
, "Out of memory in pop_list");
459 sprintf (pop_error
, "LIST %d", message
);
460 if (sendline (server
, pop_error
))
462 free ((char *) *IDs
);
463 free ((char *) *sizes
);
466 if (pop_getline (server
, &fromserver
) < 0)
468 free ((char *) *IDs
);
469 free ((char *) *sizes
);
472 if (strncmp (fromserver
, "+OK ", 4))
474 if (! strncmp (fromserver
, "-ERR", 4))
475 strncpy (pop_error
, fromserver
, ERROR_MAX
);
479 "Unexpected response from server in pop_list");
482 free ((char *) *IDs
);
483 free ((char *) *sizes
);
486 (*IDs
)[0] = atoi (&fromserver
[4]);
487 fromserver
= index (&fromserver
[4], ' ');
491 "Badly formatted response from server in pop_list");
493 free ((char *) *IDs
);
494 free ((char *) *sizes
);
497 (*sizes
)[0] = atoi (fromserver
);
498 (*IDs
)[1] = (*sizes
)[1] = 0;
503 if (pop_multi_first (server
, "LIST", &fromserver
))
505 free ((char *) *IDs
);
506 free ((char *) *sizes
);
509 for (i
= 0; i
< how_many
; i
++)
511 if (pop_multi_next (server
, &fromserver
) <= 0)
513 free ((char *) *IDs
);
514 free ((char *) *sizes
);
517 (*IDs
)[i
] = atoi (fromserver
);
518 fromserver
= index (fromserver
, ' ');
522 "Badly formatted response from server in pop_list");
523 free ((char *) *IDs
);
524 free ((char *) *sizes
);
528 (*sizes
)[i
] = atoi (fromserver
);
530 if (pop_multi_next (server
, &fromserver
) < 0)
532 free ((char *) *IDs
);
533 free ((char *) *sizes
);
539 "Too many response lines from server in pop_list");
540 free ((char *) *IDs
);
541 free ((char *) *sizes
);
544 (*IDs
)[i
] = (*sizes
)[i
] = 0;
550 * Function: pop_retrieve
552 * Purpose: Retrieve a specified message from the maildrop.
555 * server The server to retrieve from.
556 * message The message number to retrieve.
558 * If true, then mark the string "From " at the beginning
560 * msg_buf Output parameter to which a buffer containing the
561 * message is assigned.
563 * Return value: The number of bytes in msg_buf, which may contain
564 * embedded nulls, not including its final null, or -1 on error
565 * with pop_error set.
567 * Side effects: May kill connection on error.
570 pop_retrieve (server
, message
, markfrom
, msg_buf
)
576 int *IDs
, *sizes
, bufsize
, fromcount
= 0, cp
= 0;
577 char *ptr
, *fromserver
;
580 if (server
->in_multi
)
582 strcpy (pop_error
, "In multi-line query in pop_retrieve");
586 if (pop_list (server
, message
, &IDs
, &sizes
))
589 if (pop_retrieve_first (server
, message
, &fromserver
))
595 * The "5" below is an arbitrary constant -- I assume that if
596 * there are "From" lines in the text to be marked, there
597 * probably won't be more than 5 of them. If there are, I
598 * allocate more space for them below.
600 bufsize
= sizes
[0] + (markfrom
? 5 : 0);
601 ptr
= (char *)malloc (bufsize
);
603 free ((char *) sizes
);
607 strcpy (pop_error
, "Out of memory in pop_retrieve");
608 pop_retrieve_flush (server
);
612 while ((ret
= pop_retrieve_next (server
, &fromserver
)) >= 0)
620 if (markfrom
&& fromserver
[0] == 'F' && fromserver
[1] == 'r' &&
621 fromserver
[2] == 'o' && fromserver
[3] == 'm' &&
622 fromserver
[4] == ' ')
624 if (++fromcount
== 5)
627 ptr
= (char *)realloc (ptr
, bufsize
);
630 strcpy (pop_error
, "Out of memory in pop_retrieve");
631 pop_retrieve_flush (server
);
638 bcopy (fromserver
, &ptr
[cp
], ret
);
648 pop_retrieve_first (server
, message
, response
)
653 sprintf (pop_error
, "RETR %d", message
);
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_retrieve_next (server
, line
)
670 return (pop_multi_next (server
, line
));
674 pop_retrieve_flush (server
)
677 return (pop_multi_flush (server
));
681 pop_top_first (server
, message
, lines
, response
)
686 sprintf (pop_error
, "TOP %d %d", message
, lines
);
687 return (pop_multi_first (server
, pop_error
, response
));
691 Returns a negative number on error, 0 to indicate that the data has
692 all been read (i.e., the server has returned a "." termination
693 line), or a positive number indicating the number of bytes in the
694 returned buffer (which is null-terminated and may contain embedded
695 nulls, but the returned bytecount doesn't include the final null).
699 pop_top_next (server
, line
)
703 return (pop_multi_next (server
, line
));
707 pop_top_flush (server
)
710 return (pop_multi_flush (server
));
714 pop_multi_first (server
, command
, response
)
719 if (server
->in_multi
)
722 "Already in multi-line query in pop_multi_first");
726 if (sendline (server
, command
) || (pop_getline (server
, response
) < 0))
731 if (0 == strncmp (*response
, "-ERR", 4))
733 strncpy (pop_error
, *response
, ERROR_MAX
);
736 else if (0 == strncmp (*response
, "+OK", 3))
738 for (*response
+= 3; **response
== ' '; (*response
)++) /* empty */;
739 server
->in_multi
= 1;
745 "Unexpected response from server in pop_multi_first");
751 Read the next line of data from SERVER and place a pointer to it
752 into LINE. Return -1 on error, 0 if there are no more lines to read
753 (i.e., the server has returned a line containing only "."), or a
754 positive number indicating the number of bytes in the LINE buffer
755 (not including the final null). The data in that buffer may contain
756 embedded nulls, but does not contain the final CRLF. When returning
757 0, LINE is set to null. */
760 pop_multi_next (server
, line
)
767 if (! server
->in_multi
)
769 strcpy (pop_error
, "Not in multi-line query in pop_multi_next");
773 if ((ret
= pop_getline (server
, &fromserver
)) < 0)
778 if (fromserver
[0] == '.')
783 server
->in_multi
= 0;
788 *line
= fromserver
+ 1;
800 pop_multi_flush (server
)
806 if (! server
->in_multi
)
811 while ((ret
= pop_multi_next (server
, &line
)))
820 /* Function: pop_delete
822 * Purpose: Delete a specified message.
825 * server Server from which to delete the message.
826 * message Message to delete.
828 * Return value: 0 on success, non-zero with error in pop_error
832 pop_delete (server
, message
)
836 if (server
->in_multi
)
838 strcpy (pop_error
, "In multi-line query in pop_delete");
842 sprintf (pop_error
, "DELE %d", message
);
844 if (sendline (server
, pop_error
) || getok (server
))
853 * Purpose: Send a noop command to the server.
856 * server The server to send to.
858 * Return value: 0 on success, non-zero with error in pop_error
861 * Side effects: Closes connection on error.
867 if (server
->in_multi
)
869 strcpy (pop_error
, "In multi-line query in pop_noop");
873 if (sendline (server
, "NOOP") || getok (server
))
882 * Purpose: Find out the highest seen message from the server.
887 * Return value: If successful, the highest seen message, which is
888 * greater than or equal to 0. Otherwise, a negative number with
889 * the error explained in pop_error.
891 * Side effects: Closes the connection on error.
899 if (server
->in_multi
)
901 strcpy (pop_error
, "In multi-line query in pop_last");
905 if (sendline (server
, "LAST"))
908 if (pop_getline (server
, &fromserver
) < 0)
911 if (! strncmp (fromserver
, "-ERR", 4))
913 strncpy (pop_error
, fromserver
, ERROR_MAX
);
916 else if (strncmp (fromserver
, "+OK ", 4))
918 strcpy (pop_error
, "Unexpected response from server in pop_last");
927 count
= strtol (&fromserver
[4], &end_ptr
, 10);
928 if (fromserver
+ 4 == end_ptr
|| errno
)
930 strcpy (pop_error
, "Unexpected response from server in pop_last");
939 * Function: pop_reset
941 * Purpose: Reset the server to its initial connect state
946 * Return value: 0 for success, non-0 with error in pop_error
949 * Side effects: Closes the connection on error.
955 if (pop_retrieve_flush (server
))
960 if (sendline (server
, "RSET") || getok (server
))
969 * Purpose: Quit the connection to the server,
972 * server The server to quit.
974 * Return value: 0 for success, non-zero otherwise with error in
977 * Side Effects: The popserver passed in is unusable after this
978 * function is called, even if an error occurs.
986 if (server
->file
>= 0)
988 if (pop_retrieve_flush (server
))
993 if (sendline (server
, "QUIT") || getok (server
))
998 close (server
->file
);
1002 free (server
->buffer
);
1003 free ((char *) server
);
1009 static int have_winsock
= 0;
1013 * Function: socket_connection
1015 * Purpose: Opens the network connection with the mail host, without
1016 * doing any sort of I/O with it or anything.
1019 * host The host to which to connect.
1020 * flags Option flags.
1022 * Return value: A file descriptor indicating the connection, or -1
1023 * indicating failure, in which case an error has been copied
1027 socket_connection (host
, flags
)
1031 #ifdef HAVE_GETADDRINFO
1032 struct addrinfo
*res
, *it
;
1033 struct addrinfo hints
;
1035 #else /* !HAVE_GETADDRINFO */
1036 struct hostent
*hostent
;
1038 struct servent
*servent
;
1039 struct sockaddr_in addr
;
1040 char found_port
= 0;
1046 krb5_error_code rem
;
1047 krb5_context kcontext
= 0;
1048 krb5_auth_context auth_context
= 0;
1050 krb5_principal client
, server
;
1051 krb5_error
*err_ret
;
1057 Key_schedule schedule
;
1059 #endif /* KERBEROS5 */
1060 #endif /* KERBEROS */
1067 WSADATA winsockData
;
1068 if (WSAStartup (0x101, &winsockData
) == 0)
1073 bzero ((char *) &addr
, sizeof (addr
));
1074 addr
.sin_family
= AF_INET
;
1076 /** "kpop" service is never used: look for 20060515 to see why **/
1078 service
= (flags
& POP_NO_KERBEROS
) ? POP_SERVICE
: KPOP_SERVICE
;
1080 service
= POP_SERVICE
;
1084 if (! (flags
& POP_NO_HESIOD
))
1086 servent
= hes_getservbyname (service
, "tcp");
1089 addr
.sin_port
= servent
->s_port
;
1096 servent
= getservbyname (service
, "tcp");
1099 addr
.sin_port
= servent
->s_port
;
1103 /** "kpop" service is never used: look for 20060515 to see why **/
1105 addr
.sin_port
= htons ((flags
& POP_NO_KERBEROS
) ?
1106 POP_PORT
: KPOP_PORT
);
1108 addr
.sin_port
= htons (POP_PORT
);
1113 #define POP_SOCKET_ERROR "Could not create socket for POP connection: "
1115 sock
= socket (PF_INET
, SOCK_STREAM
, 0);
1118 strcpy (pop_error
, POP_SOCKET_ERROR
);
1119 strncat (pop_error
, strerror (errno
),
1120 ERROR_MAX
- sizeof (POP_SOCKET_ERROR
));
1125 #ifdef HAVE_GETADDRINFO
1126 memset (&hints
, 0, sizeof(hints
));
1127 hints
.ai_socktype
= SOCK_STREAM
;
1128 hints
.ai_flags
= AI_CANONNAME
;
1129 hints
.ai_family
= AF_INET
;
1132 ret
= getaddrinfo (host
, service
, &hints
, &res
);
1134 if (ret
!= 0 && (ret
!= EAI_AGAIN
|| try_count
== 5))
1136 strcpy (pop_error
, "Could not determine POP server's address");
1146 if (it
->ai_addrlen
== sizeof (addr
))
1148 struct sockaddr_in
*in_a
= (struct sockaddr_in
*) it
->ai_addr
;
1149 bcopy (&in_a
->sin_addr
, (char *) &addr
.sin_addr
,
1150 sizeof (addr
.sin_addr
));
1151 if (! connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)))
1156 connect_ok
= it
!= NULL
;
1159 realhost
= alloca (strlen (it
->ai_canonname
) + 1);
1160 strcpy (realhost
, it
->ai_canonname
);
1164 #else /* !HAVE_GETADDRINFO */
1167 hostent
= gethostbyname (host
);
1169 if ((! hostent
) && ((h_errno
!= TRY_AGAIN
) || (try_count
== 5)))
1171 strcpy (pop_error
, "Could not determine POP server's address");
1174 } while (! hostent
);
1176 while (*hostent
->h_addr_list
)
1178 bcopy (*hostent
->h_addr_list
, (char *) &addr
.sin_addr
,
1180 if (! connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)))
1182 hostent
->h_addr_list
++;
1184 connect_ok
= *hostent
->h_addr_list
!= NULL
;
1187 realhost
= alloca (strlen (hostent
->h_name
) + 1);
1188 strcpy (realhost
, hostent
->h_name
);
1191 #endif /* !HAVE_GETADDRINFO */
1193 #define CONNECT_ERROR "Could not connect to POP server: "
1198 strcpy (pop_error
, CONNECT_ERROR
);
1199 strncat (pop_error
, strerror (errno
),
1200 ERROR_MAX
- sizeof (CONNECT_ERROR
));
1207 #define KRB_ERROR "Kerberos error connecting to POP server: "
1208 if (! (flags
& POP_NO_KERBEROS
))
1211 if ((rem
= krb5_init_context (&kcontext
)))
1215 krb5_auth_con_free (kcontext
, auth_context
);
1217 krb5_free_context (kcontext
);
1218 strcpy (pop_error
, KRB_ERROR
);
1219 strncat (pop_error
, error_message (rem
),
1220 ERROR_MAX
- sizeof(KRB_ERROR
));
1225 if ((rem
= krb5_auth_con_init (kcontext
, &auth_context
)))
1228 if (rem
= krb5_cc_default (kcontext
, &ccdef
))
1231 if (rem
= krb5_cc_get_principal (kcontext
, ccdef
, &client
))
1234 for (cp
= realhost
; *cp
; cp
++)
1238 *cp
= tolower (*cp
);
1242 if (rem
= krb5_sname_to_principal (kcontext
, realhost
,
1243 POP_SERVICE
, FALSE
, &server
))
1246 rem
= krb5_sendauth (kcontext
, &auth_context
,
1247 (krb5_pointer
) &sock
, "KPOPV1.0", client
, server
,
1248 AP_OPTS_MUTUAL_REQUIRED
,
1249 0, /* no checksum */
1250 0, /* no creds, use ccache instead */
1253 0, /* don't need subsession key */
1254 0); /* don't need reply */
1255 krb5_free_principal (kcontext
, server
);
1258 if (err_ret
&& err_ret
->text
.length
)
1260 strcpy (pop_error
, KRB_ERROR
);
1261 strncat (pop_error
, error_message (rem
),
1262 ERROR_MAX
- sizeof (KRB_ERROR
));
1263 strncat (pop_error
, " [server says '",
1264 ERROR_MAX
- strlen (pop_error
) - 1);
1265 strncat (pop_error
, err_ret
->text
.data
,
1266 min (ERROR_MAX
- strlen (pop_error
) - 1,
1267 err_ret
->text
.length
));
1268 strncat (pop_error
, "']",
1269 ERROR_MAX
- strlen (pop_error
) - 1);
1273 strcpy (pop_error
, KRB_ERROR
);
1274 strncat (pop_error
, error_message (rem
),
1275 ERROR_MAX
- sizeof (KRB_ERROR
));
1278 krb5_free_error (kcontext
, err_ret
);
1279 krb5_auth_con_free (kcontext
, auth_context
);
1280 krb5_free_context (kcontext
);
1285 #else /* ! KERBEROS5 */
1286 ticket
= (KTEXT
) malloc (sizeof (KTEXT_ST
));
1287 rem
= krb_sendauth (0L, sock
, ticket
, "pop", realhost
,
1288 (char *) krb_realmofhost (realhost
),
1289 (unsigned long) 0, &msg_data
, &cred
, schedule
,
1290 (struct sockaddr_in
*) 0,
1291 (struct sockaddr_in
*) 0,
1293 free ((char *) ticket
);
1294 if (rem
!= KSUCCESS
)
1296 strcpy (pop_error
, KRB_ERROR
);
1297 strncat (pop_error
, krb_err_txt
[rem
],
1298 ERROR_MAX
- sizeof (KRB_ERROR
));
1302 #endif /* KERBEROS5 */
1304 #endif /* KERBEROS */
1307 } /* socket_connection */
1310 * Function: pop_getline
1312 * Purpose: Get a line of text from the connection and return a
1313 * pointer to it. The carriage return and linefeed at the end of
1314 * the line are stripped, but periods at the beginnings of lines
1315 * are NOT dealt with in any special way.
1318 * server The server from which to get the line of text.
1320 * Returns: The number of characters in the line, which is returned in
1321 * LINE, not including the final null. A return value of 0
1322 * indicates a blank line. A negative return value indicates an
1323 * error (in which case the contents of LINE are undefined. In
1324 * case of error, an error message is copied into pop_error.
1326 * Notes: The line returned is overwritten with each call to pop_getline.
1328 * Side effects: Closes the connection on error.
1330 * THE RETURNED LINE MAY CONTAIN EMBEDDED NULLS!
1333 pop_getline (server
, line
)
1337 #define GETLINE_ERROR "Error reading from server: "
1340 int search_offset
= 0;
1344 char *cp
= find_crlf (server
->buffer
+ server
->buffer_index
,
1351 found
= server
->buffer_index
;
1352 data_used
= (cp
+ 2) - server
->buffer
- found
;
1354 *cp
= '\0'; /* terminate the string to be returned */
1355 server
->data
-= data_used
;
1356 server
->buffer_index
+= data_used
;
1359 /* Embedded nulls will truncate this output prematurely,
1360 but that's OK because it's just for debugging anyway. */
1361 fprintf (stderr
, "<<< %s\n", server
->buffer
+ found
);
1362 *line
= server
->buffer
+ found
;
1363 return (data_used
- 2);
1367 bcopy (server
->buffer
+ server
->buffer_index
,
1368 server
->buffer
, server
->data
);
1369 /* Record the fact that we've searched the data already in
1370 the buffer for a CRLF, so that when we search below, we
1371 don't have to search the same data twice. There's a "-
1372 1" here to account for the fact that the last character
1373 of the data we have may be the CR of a CRLF pair, of
1374 which we haven't read the second half yet, so we may have
1375 to search it again when we read more data. */
1376 search_offset
= server
->data
- 1;
1377 server
->buffer_index
= 0;
1382 server
->buffer_index
= 0;
1387 /* There's a "- 1" here to leave room for the null that we put
1388 at the end of the read data below. We put the null there so
1389 that find_crlf knows where to stop when we call it. */
1390 if (server
->data
== server
->buffer_size
- 1)
1392 server
->buffer_size
+= GETLINE_INCR
;
1393 server
->buffer
= (char *)realloc (server
->buffer
, server
->buffer_size
);
1394 if (! server
->buffer
)
1396 strcpy (pop_error
, "Out of memory in pop_getline");
1401 ret
= RECV (server
->file
, server
->buffer
+ server
->data
,
1402 server
->buffer_size
- server
->data
- 1, 0);
1405 strcpy (pop_error
, GETLINE_ERROR
);
1406 strncat (pop_error
, strerror (errno
),
1407 ERROR_MAX
- sizeof (GETLINE_ERROR
));
1413 strcpy (pop_error
, "Unexpected EOF from server in pop_getline");
1420 server
->data
+= ret
;
1421 server
->buffer
[server
->data
] = '\0';
1423 cp
= find_crlf (server
->buffer
+ search_offset
,
1424 server
->data
- search_offset
);
1427 int data_used
= (cp
+ 2) - server
->buffer
;
1429 server
->data
-= data_used
;
1430 server
->buffer_index
= data_used
;
1433 fprintf (stderr
, "<<< %s\n", server
->buffer
);
1434 *line
= server
->buffer
;
1435 return (data_used
- 2);
1437 /* As above, the "- 1" here is to account for the fact that
1438 we may have read a CR without its accompanying LF. */
1439 search_offset
+= ret
- 1;
1447 * Function: sendline
1449 * Purpose: Sends a line of text to the POP server. The line of text
1450 * passed into this function should NOT have the carriage return
1451 * and linefeed on the end of it. Periods at beginnings of lines
1452 * will NOT be treated specially by this function.
1455 * server The server to which to send the text.
1456 * line The line of text to send.
1458 * Return value: Upon successful completion, a value of 0 will be
1459 * returned. Otherwise, a non-zero value will be returned, and
1460 * an error will be copied into pop_error.
1462 * Side effects: Closes the connection on error.
1465 sendline (server
, line
)
1469 #define SENDLINE_ERROR "Error writing to POP server: "
1473 /* Combine the string and the CR-LF into one buffer. Otherwise, two
1474 reasonable network stack optimizations, Nagle's algorithm and
1475 delayed acks, combine to delay us a fraction of a second on every
1476 message we send. (Movemail writes line without \r\n, client
1477 kernel sends packet, server kernel delays the ack to see if it
1478 can combine it with data, movemail writes \r\n, client kernel
1479 waits because it has unacked data already in its outgoing queue,
1480 client kernel eventually times out and sends.)
1482 This can be something like 0.2s per command, which can add up
1483 over a few dozen messages, and is a big chunk of the time we
1484 spend fetching mail from a server close by. */
1485 buf
= alloca (strlen (line
) + 3);
1487 strcat (buf
, "\r\n");
1488 ret
= fullwrite (server
->file
, buf
, strlen (buf
));
1493 strcpy (pop_error
, SENDLINE_ERROR
);
1494 strncat (pop_error
, strerror (errno
),
1495 ERROR_MAX
- sizeof (SENDLINE_ERROR
));
1500 fprintf (stderr
, ">>> %s\n", line
);
1506 * Procedure: fullwrite
1508 * Purpose: Just like write, but keeps trying until the entire string
1511 * Return value: Same as write. Pop_error is not set.
1514 fullwrite (fd
, buf
, nbytes
)
1523 while (nbytes
&& ((ret
= SEND (fd
, cp
, nbytes
, 0)) > 0))
1535 * Purpose: Reads a line from the server. If the return indicator is
1536 * positive, return with a zero exit status. If not, return with
1537 * a negative exit status.
1540 * server The server to read from.
1542 * Returns: 0 for success, else for failure and puts error in pop_error.
1544 * Side effects: On failure, may make the connection unusable.
1552 if (pop_getline (server
, &fromline
) < 0)
1557 if (! strncmp (fromline
, "+OK", 3))
1559 else if (! strncmp (fromline
, "-ERR", 4))
1561 strncpy (pop_error
, fromline
, ERROR_MAX
);
1562 pop_error
[ERROR_MAX
-1] = '\0';
1568 "Unexpected response from server; expecting +OK or -ERR");
1576 * Function: gettermination
1578 * Purpose: Gets the next line and verifies that it is a termination
1579 * line (nothing but a dot).
1581 * Return value: 0 on success, non-zero with pop_error set on error.
1583 * Side effects: Closes the connection on error.
1586 gettermination (server
)
1591 if (pop_getline (server
, &fromserver
) < 0)
1594 if (strcmp (fromserver
, "."))
1597 "Unexpected response from server in gettermination");
1607 * Function pop_close
1609 * Purpose: Close a pop connection, sending a "RSET" command to try to
1610 * preserve any changes that were made and a "QUIT" command to
1611 * try to get the server to quit, but ignoring any responses that
1614 * Side effects: The server is unusable after this function returns.
1615 * Changes made to the maildrop since the session was started (or
1616 * since the last pop_reset) may be lost.
1623 free ((char *) server
);
1629 * Function: pop_trash
1631 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
1632 * memory associated with the server. It is valid to call
1633 * pop_close or pop_quit after this function has been called.
1639 if (server
->file
>= 0)
1641 /* avoid recursion; sendline can call pop_trash */
1642 if (server
->trash_started
)
1644 server
->trash_started
= 1;
1646 sendline (server
, "RSET");
1647 sendline (server
, "QUIT");
1649 CLOSESOCKET (server
->file
);
1653 free (server
->buffer
);
1664 /* Return a pointer to the first CRLF in IN_STRING, which can contain
1665 embedded nulls and has LEN characters in it not including the final
1666 null, or 0 if it does not contain one. */
1669 find_crlf (in_string
, len
)
1675 if (*in_string
== '\r')
1677 if (*++in_string
== '\n')
1678 return (in_string
- 1);
1686 #endif /* MAIL_USE_POP */
1688 /* arch-tag: ceb37041-b7ad-49a8-a63d-286618b8367d
1689 (do not change this comment) */