1 /* pop.c: client routines for talking to a POP3-protocol post-office server
2 Copyright (c) 1991,1993 Free Software Foundation, Inc.
3 Written by Jonathan Kamens, jik@security.ov.com.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #define NO_SHORTNAMES /* Tell config not to load remap.h */
23 #include <../src/config.h>
31 /* Cancel these substitutions made in config.h */
38 #include <sys/types.h>
39 #include <netinet/in.h>
40 #include <sys/socket.h>
50 * It really shouldn't be necessary to put this declaration here, but
51 * the version of hesiod.h that Athena has installed in release 7.2
52 * doesn't declare this function; I don't know if the 7.3 version of
55 extern struct servent
*hes_getservbyname (/* char *, char * */);
68 #include <krb5/krb5.h>
69 #include <krb5/ext-proto.h>
74 extern char *getenv (/* char * */);
75 extern char *getlogin (/* void */);
76 extern char *getpass (/* char * */);
77 extern char *strerror (/* int */);
81 extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *,
82 u_long, MSG_DAT *, CREDENTIALS *, Key_schedule,
83 struct sockaddr_in *, struct sockaddr_in *,
85 extern char *krb_realmofhost (/* char * */);
89 #if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H)
93 static int socket_connection (/* char *, int */);
94 static char *getline (/* popserver */);
95 static int sendline (/* popserver, char * */);
96 static int fullwrite (/* int, char *, int */);
97 static int getok (/* popserver */);
99 static int gettermination (/* popserver */);
101 static void pop_trash (/* popserver */);
102 static char *find_crlf (/* char * */);
104 #define ERROR_MAX 80 /* a pretty arbitrary size */
106 #define KPOP_PORT 1109
107 #define POP_SERVICE "pop"
110 #define KPOP_SERVICE "k5pop";
112 #define KPOP_SERVICE "kpop"
116 char pop_error
[ERROR_MAX
];
120 #define min(a,b) (((a) < (b)) ? (a) : (b))
124 * Function: pop_open (char *host, char *username, char *password,
127 * Purpose: Establishes a connection with a post-office server, and
128 * completes the authorization portion of the session.
131 * host The server host with which the connection should be
132 * established. Optional. If omitted, internal
133 * heuristics will be used to determine the server host,
136 * The username of the mail-drop to access. Optional.
137 * If omitted, internal heuristics will be used to
138 * determine the username, if possible.
140 * The password to use for authorization. If omitted,
141 * internal heuristics will be used to determine the
142 * password, if possible.
143 * flags A bit mask containing flags controlling certain
144 * functions of the routine. Valid flags are defined in
147 * Return value: Upon successful establishment of a connection, a
148 * non-null popserver will be returned. Otherwise, null will be
149 * returned, and the string variable pop_error will contain an
150 * explanation of the error.
153 pop_open (host
, username
, password
, flags
)
162 /* Determine the user name */
165 username
= getenv ("USER");
166 if (! (username
&& *username
))
168 username
= getlogin ();
169 if (! (username
&& *username
))
171 struct passwd
*passwd
;
172 passwd
= getpwuid (getuid ());
173 if (passwd
&& passwd
->pw_name
&& *passwd
->pw_name
)
175 username
= passwd
->pw_name
;
179 strcpy (pop_error
, "Could not determine username");
187 * Determine the mail host.
192 host
= getenv ("MAILHOST");
196 if ((! host
) && (! (flags
& POP_NO_HESIOD
)))
198 struct hes_postoffice
*office
;
199 office
= hes_getmailhost (username
);
200 if (office
&& office
->po_type
&& (! strcmp (office
->po_type
, "POP"))
201 && office
->po_name
&& *office
->po_name
&& office
->po_host
204 host
= office
->po_host
;
205 username
= office
->po_name
;
219 strcpy (pop_error
, "Could not determine POP server");
223 /* Determine the password */
225 #define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS))
227 #define DONT_NEED_PASSWORD 0
230 if ((! password
) && (! DONT_NEED_PASSWORD
))
232 if (! (flags
& POP_NO_GETPASS
))
234 password
= getpass ("Enter POP password:");
238 strcpy (pop_error
, "Could not determine POP password");
243 flags
|= POP_NO_KERBEROS
;
247 sock
= socket_connection (host
, flags
);
251 server
= (popserver
) malloc (sizeof (struct _popserver
));
254 strcpy (pop_error
, "Out of memory in pop_open");
257 server
->buffer
= (char *) malloc (GETLINE_MIN
);
258 if (! server
->buffer
)
260 strcpy (pop_error
, "Out of memory in pop_open");
261 free ((char *) server
);
267 server
->buffer_index
= 0;
268 server
->buffer_size
= GETLINE_MIN
;
269 server
->in_multi
= 0;
275 * I really shouldn't use the pop_error variable like this, but....
277 if (strlen (username
) > ERROR_MAX
- 6)
281 "Username too long; recompile pop.c with larger ERROR_MAX");
284 sprintf (pop_error
, "USER %s", username
);
286 if (sendline (server
, pop_error
) || getok (server
))
291 if (strlen (password
) > ERROR_MAX
- 6)
295 "Password too long; recompile pop.c with larger ERROR_MAX");
298 sprintf (pop_error
, "PASS %s", password
);
300 if (sendline (server
, pop_error
) || getok (server
))
311 * Purpose: Issue the STAT command to the server and return (in the
312 * value parameters) the number of messages in the maildrop and
313 * the total size of the maildrop.
315 * Return value: 0 on success, or non-zero with an error in pop_error
318 * Side effects: On failure, may make further operations on the
319 * connection impossible.
322 pop_stat (server
, count
, size
)
329 if (server
->in_multi
)
331 strcpy (pop_error
, "In multi-line query in pop_stat");
335 if (sendline (server
, "STAT") || (! (fromserver
= getline (server
))))
338 if (strncmp (fromserver
, "+OK ", 4))
340 if (0 == strncmp (fromserver
, "-ERR", 4))
342 strncpy (pop_error
, fromserver
, ERROR_MAX
);
347 "Unexpected response from POP server in pop_stat");
353 *count
= atoi (&fromserver
[4]);
355 fromserver
= index (&fromserver
[4], ' ');
359 "Badly formatted response from server in pop_stat");
364 *size
= atoi (fromserver
+ 1);
372 * Purpose: Performs the POP "list" command and returns (in value
373 * parameters) two malloc'd zero-terminated arrays -- one of
374 * message IDs, and a parallel one of sizes.
377 * server The pop connection to talk to.
378 * message The number of the one message about which to get
379 * information, or 0 to get information about all
382 * Return value: 0 on success, non-zero with error in pop_error on
385 * Side effects: On failure, may make further operations on the
386 * connection impossible.
389 pop_list (server
, message
, IDs
, sizes
)
398 if (server
->in_multi
)
400 strcpy (pop_error
, "In multi-line query in pop_list");
409 if (pop_stat (server
, &count
, &size
))
414 *IDs
= (int *) malloc ((how_many
+ 1) * sizeof (int));
415 *sizes
= (int *) malloc ((how_many
+ 1) * sizeof (int));
416 if (! (*IDs
&& *sizes
))
418 strcpy (pop_error
, "Out of memory in pop_list");
424 sprintf (pop_error
, "LIST %d", message
);
425 if (sendline (server
, pop_error
))
427 free ((char *) *IDs
);
428 free ((char *) *sizes
);
431 if (! (fromserver
= getline (server
)))
433 free ((char *) *IDs
);
434 free ((char *) *sizes
);
437 if (strncmp (fromserver
, "+OK ", 4))
439 if (! strncmp (fromserver
, "-ERR", 4))
440 strncpy (pop_error
, fromserver
, ERROR_MAX
);
444 "Unexpected response from server in pop_list");
447 free ((char *) *IDs
);
448 free ((char *) *sizes
);
451 (*IDs
)[0] = atoi (&fromserver
[4]);
452 fromserver
= index (&fromserver
[4], ' ');
456 "Badly formatted response from server in pop_list");
458 free ((char *) *IDs
);
459 free ((char *) *sizes
);
462 (*sizes
)[0] = atoi (fromserver
);
463 (*IDs
)[1] = (*sizes
)[1] = 0;
468 if (pop_multi_first (server
, "LIST", &fromserver
))
470 free ((char *) *IDs
);
471 free ((char *) *sizes
);
474 for (i
= 0; i
< how_many
; i
++)
476 if (pop_multi_next (server
, &fromserver
))
478 free ((char *) *IDs
);
479 free ((char *) *sizes
);
482 (*IDs
)[i
] = atoi (fromserver
);
483 fromserver
= index (fromserver
, ' ');
487 "Badly formatted response from server in pop_list");
488 free ((char *) *IDs
);
489 free ((char *) *sizes
);
493 (*sizes
)[i
] = atoi (fromserver
);
495 if (pop_multi_next (server
, &fromserver
))
497 free ((char *) *IDs
);
498 free ((char *) *sizes
);
504 "Too many response lines from server in pop_list");
505 free ((char *) *IDs
);
506 free ((char *) *sizes
);
509 (*IDs
)[i
] = (*sizes
)[i
] = 0;
515 * Function: pop_retrieve
517 * Purpose: Retrieve a specified message from the maildrop.
520 * server The server to retrieve from.
521 * message The message number to retrieve.
523 * If true, then mark the string "From " at the beginning
526 * Return value: A string pointing to the message, if successful, or
527 * null with pop_error set if not.
529 * Side effects: May kill connection on error.
532 pop_retrieve (server
, message
, markfrom
)
537 int *IDs
, *sizes
, bufsize
, fromcount
= 0, cp
= 0;
538 char *ptr
, *fromserver
;
541 if (server
->in_multi
)
543 strcpy (pop_error
, "In multi-line query in pop_retrieve");
547 if (pop_list (server
, message
, &IDs
, &sizes
))
550 if (pop_retrieve_first (server
, message
, &fromserver
))
556 * The "5" below is an arbitrary constant -- I assume that if
557 * there are "From" lines in the text to be marked, there
558 * probably won't be more than 5 of them. If there are, I
559 * allocate more space for them below.
561 bufsize
= sizes
[0] + (markfrom
? 5 : 0);
562 ptr
= malloc (bufsize
);
564 free ((char *) sizes
);
568 strcpy (pop_error
, "Out of memory in pop_retrieve");
569 pop_retrieve_flush (server
);
573 while (! (ret
= pop_retrieve_next (server
, &fromserver
)))
582 if (markfrom
&& fromserver
[0] == 'F' && fromserver
[1] == 'r' &&
583 fromserver
[2] == 'o' && fromserver
[3] == 'm' &&
584 fromserver
[4] == ' ')
586 if (++fromcount
== 5)
589 ptr
= realloc (ptr
, bufsize
);
592 strcpy (pop_error
, "Out of memory in pop_retrieve");
593 pop_retrieve_flush (server
);
600 linesize
= strlen (fromserver
);
601 bcopy (fromserver
, &ptr
[cp
], linesize
);
614 pop_retrieve_first (server
, message
, response
)
619 sprintf (pop_error
, "RETR %d", message
);
620 return (pop_multi_first (server
, pop_error
, response
));
624 pop_retrieve_next (server
, line
)
628 return (pop_multi_next (server
, line
));
632 pop_retrieve_flush (server
)
635 return (pop_multi_flush (server
));
639 pop_top_first (server
, message
, lines
, response
)
644 sprintf (pop_error
, "TOP %d %d", message
, lines
);
645 return (pop_multi_first (server
, pop_error
, response
));
649 pop_top_next (server
, line
)
653 return (pop_multi_next (server
, line
));
657 pop_top_flush (server
)
660 return (pop_multi_flush (server
));
664 pop_multi_first (server
, command
, response
)
669 if (server
->in_multi
)
672 "Already in multi-line query in pop_multi_first");
676 if (sendline (server
, command
) || (! (*response
= getline (server
))))
681 if (0 == strncmp (*response
, "-ERR", 4))
683 strncpy (pop_error
, *response
, ERROR_MAX
);
686 else if (0 == strncmp (*response
, "+OK", 3))
688 for (*response
+= 3; **response
== ' '; (*response
)++) /* empty */;
689 server
->in_multi
= 1;
695 "Unexpected response from server in pop_multi_first");
701 pop_multi_next (server
, line
)
707 if (! server
->in_multi
)
709 strcpy (pop_error
, "Not in multi-line query in pop_multi_next");
713 fromserver
= getline (server
);
719 if (fromserver
[0] == '.')
724 server
->in_multi
= 0;
729 *line
= fromserver
+ 1;
741 pop_multi_flush (server
)
746 if (! server
->in_multi
)
751 while (! pop_multi_next (server
, &line
))
762 /* Function: pop_delete
764 * Purpose: Delete a specified message.
767 * server Server from which to delete the message.
768 * message Message to delete.
770 * Return value: 0 on success, non-zero with error in pop_error
774 pop_delete (server
, message
)
778 if (server
->in_multi
)
780 strcpy (pop_error
, "In multi-line query in pop_delete");
784 sprintf (pop_error
, "DELE %d", message
);
786 if (sendline (server
, pop_error
) || getok (server
))
795 * Purpose: Send a noop command to the server.
798 * server The server to send to.
800 * Return value: 0 on success, non-zero with error in pop_error
803 * Side effects: Closes connection on error.
809 if (server
->in_multi
)
811 strcpy (pop_error
, "In multi-line query in pop_noop");
815 if (sendline (server
, "NOOP") || getok (server
))
824 * Purpose: Find out the highest seen message from the server.
829 * Return value: If successful, the highest seen message, which is
830 * greater than or equal to 0. Otherwise, a negative number with
831 * the error explained in pop_error.
833 * Side effects: Closes the connection on error.
841 if (server
->in_multi
)
843 strcpy (pop_error
, "In multi-line query in pop_last");
847 if (sendline (server
, "LAST"))
850 if (! (fromserver
= getline (server
)))
853 if (! strncmp (fromserver
, "-ERR", 4))
855 strncpy (pop_error
, fromserver
, ERROR_MAX
);
858 else if (strncmp (fromserver
, "+OK ", 4))
860 strcpy (pop_error
, "Unexpected response from server in pop_last");
866 return (atoi (&fromserver
[4]));
871 * Function: pop_reset
873 * Purpose: Reset the server to its initial connect state
878 * Return value: 0 for success, non-0 with error in pop_error
881 * Side effects: Closes the connection on error.
887 if (pop_retrieve_flush (server
))
892 if (sendline (server
, "RSET") || getok (server
))
901 * Purpose: Quit the connection to the server,
904 * server The server to quit.
906 * Return value: 0 for success, non-zero otherwise with error in
909 * Side Effects: The popserver passed in is unuseable after this
910 * function is called, even if an error occurs.
918 if (server
->file
>= 0)
920 if (pop_retrieve_flush (server
))
925 if (sendline (server
, "QUIT") || getok (server
))
930 close (server
->file
);
934 free (server
->buffer
);
935 free ((char *) server
);
941 * Function: socket_connection
943 * Purpose: Opens the network connection with the mail host, without
944 * doing any sort of I/O with it or anything.
947 * host The host to which to connect.
948 * flags Option flags.
950 * Return value: A file descriptor indicating the connection, or -1
951 * indicating failure, in which case an error has been copied
955 socket_connection (host
, flags
)
959 struct hostent
*hostent
;
960 struct servent
*servent
;
961 struct sockaddr_in addr
;
969 krb5_principal client
, server
;
976 Key_schedule schedule
;
979 #endif /* KERBEROS */
985 hostent
= gethostbyname (host
);
987 if ((! hostent
) && ((h_errno
!= TRY_AGAIN
) || (try_count
== 5)))
989 strcpy (pop_error
, "Could not determine POP server's address");
994 bzero ((char *) &addr
, sizeof (addr
));
995 addr
.sin_family
= AF_INET
;
998 service
= (flags
& POP_NO_KERBEROS
) ? POP_SERVICE
: KPOP_SERVICE
;
1000 service
= POP_SERVICE
;
1004 if (! (flags
& POP_NO_HESIOD
))
1006 servent
= hes_getservbyname (service
, "tcp");
1009 addr
.sin_port
= servent
->s_port
;
1016 servent
= getservbyname (service
, "tcp");
1019 addr
.sin_port
= servent
->s_port
;
1024 addr
.sin_port
= htons ((flags
& POP_NO_KERBEROS
) ?
1025 POP_PORT
: KPOP_PORT
);
1027 addr
.sin_port
= htons (POP_PORT
);
1032 #define SOCKET_ERROR "Could not create socket for POP connection: "
1034 sock
= socket (PF_INET
, SOCK_STREAM
, 0);
1037 strcpy (pop_error
, SOCKET_ERROR
);
1038 strncat (pop_error
, strerror (errno
),
1039 ERROR_MAX
- sizeof (SOCKET_ERROR
));
1044 while (*hostent
->h_addr_list
)
1046 bcopy (*hostent
->h_addr_list
, (char *) &addr
.sin_addr
,
1048 if (! connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)))
1050 hostent
->h_addr_list
++;
1053 #define CONNECT_ERROR "Could not connect to POP server: "
1055 if (! *hostent
->h_addr_list
)
1057 (void) close (sock
);
1058 strcpy (pop_error
, CONNECT_ERROR
);
1059 strncat (pop_error
, strerror (errno
),
1060 ERROR_MAX
- sizeof (CONNECT_ERROR
));
1066 #define KRB_ERROR "Kerberos error connecting to POP server: "
1067 if (! (flags
& POP_NO_KERBEROS
))
1072 if (rem
= krb5_cc_default (&ccdef
))
1075 strcpy (pop_error
, KRB_ERROR
);
1076 strncat (pop_error
, error_message (rem
),
1077 ERROR_MAX
- sizeof(KRB_ERROR
));
1078 (void) close (sock
);
1082 if (rem
= krb5_cc_get_principal (ccdef
, &client
))
1087 for (cp
= hostent
->h_name
; *cp
; cp
++)
1091 *cp
= tolower (*cp
);
1095 if (rem
= krb5_sname_to_principal (hostent
->h_name
, POP_SERVICE
,
1101 rem
= krb5_sendauth ((krb5_pointer
) &sock
, "KPOPV1.0", client
, server
,
1102 AP_OPTS_MUTUAL_REQUIRED
,
1103 0, /* no checksum */
1104 0, /* no creds, use ccache instead */
1106 0, /* don't need seq # */
1107 0, /* don't need subsession key */
1109 0); /* don't need reply */
1110 krb5_free_principal (server
);
1113 if (err_ret
&& err_ret
->text
.length
)
1115 strcpy (pop_error
, KRB_ERROR
);
1116 strncat (pop_error
, error_message (rem
),
1117 ERROR_MAX
- sizeof (KRB_ERROR
));
1118 strncat (pop_error
, " [server says '",
1119 ERROR_MAX
- strlen (pop_error
) - 1);
1120 strncat (pop_error
, err_ret
->text
.data
,
1121 min (ERROR_MAX
- strlen (pop_error
) - 1,
1122 err_ret
->text
.length
));
1123 strncat (pop_error
, "']",
1124 ERROR_MAX
- strlen (pop_error
) - 1);
1128 strcpy (pop_error
, KRB_ERROR
);
1129 strncat (pop_error
, error_message (rem
),
1130 ERROR_MAX
- sizeof (KRB_ERROR
));
1133 krb5_free_error (err_ret
);
1135 (void) close (sock
);
1139 ticket
= (KTEXT
) malloc (sizeof (KTEXT_ST
));
1140 rem
= krb_sendauth (0L, sock
, ticket
, "pop", hostent
->h_name
,
1141 (char *) krb_realmofhost (hostent
->h_name
),
1142 (unsigned long) 0, &msg_data
, &cred
, schedule
,
1143 (struct sockaddr_in
*) 0,
1144 (struct sockaddr_in
*) 0,
1146 free ((char *) ticket
);
1147 if (rem
!= KSUCCESS
)
1149 strcpy (pop_error
, KRB_ERROR
);
1150 strncat (pop_error
, krb_err_txt
[rem
],
1151 ERROR_MAX
- sizeof (KRB_ERROR
));
1152 (void) close (sock
);
1157 #endif /* KERBEROS */
1160 } /* socket_connection */
1165 * Purpose: Get a line of text from the connection and return a
1166 * pointer to it. The carriage return and linefeed at the end of
1167 * the line are stripped, but periods at the beginnings of lines
1168 * are NOT dealt with in any special way.
1171 * server The server from which to get the line of text.
1173 * Returns: A non-null pointer if successful, or a null pointer on any
1174 * error, with an error message copied into pop_error.
1176 * Notes: The line returned is overwritten with each call to getline.
1178 * Side effects: Closes the connection on error.
1184 #define GETLINE_ERROR "Error reading from server: "
1187 int search_offset
= 0;
1191 char *cp
= find_crlf (server
->buffer
+ server
->buffer_index
);
1197 found
= server
->buffer_index
;
1198 data_used
= (cp
+ 2) - server
->buffer
- found
;
1200 *cp
= '\0'; /* terminate the string to be returned */
1201 server
->data
-= data_used
;
1202 server
->buffer_index
+= data_used
;
1205 fprintf (stderr
, "<<< %s\n", server
->buffer
+ found
);
1206 return (server
->buffer
+ found
);
1210 bcopy (server
->buffer
+ server
->buffer_index
,
1211 server
->buffer
, server
->data
);
1212 /* Record the fact that we've searched the data already in
1213 the buffer for a CRLF, so that when we search below, we
1214 don't have to search the same data twice. There's a "-
1215 1" here to account for the fact that the last character
1216 of the data we have may be the CR of a CRLF pair, of
1217 which we haven't read the second half yet, so we may have
1218 to search it again when we read more data. */
1219 search_offset
= server
->data
- 1;
1220 server
->buffer_index
= 0;
1225 server
->buffer_index
= 0;
1230 /* There's a "- 1" here to leave room for the null that we put
1231 at the end of the read data below. We put the null there so
1232 that find_crlf knows where to stop when we call it. */
1233 if (server
->data
== server
->buffer_size
- 1)
1235 server
->buffer_size
+= GETLINE_INCR
;
1236 server
->buffer
= realloc (server
->buffer
, server
->buffer_size
);
1237 if (! server
->buffer
)
1239 strcpy (pop_error
, "Out of memory in getline");
1244 ret
= read (server
->file
, server
->buffer
+ server
->data
,
1245 server
->buffer_size
- server
->data
- 1);
1248 strcpy (pop_error
, GETLINE_ERROR
);
1249 strncat (pop_error
, strerror (errno
),
1250 ERROR_MAX
- sizeof (GETLINE_ERROR
));
1256 strcpy (pop_error
, "Unexpected EOF from server in getline");
1263 server
->data
+= ret
;
1264 server
->buffer
[server
->data
] = '\0';
1266 cp
= find_crlf (server
->buffer
+ search_offset
);
1269 int data_used
= (cp
+ 2) - server
->buffer
;
1271 server
->data
-= data_used
;
1272 server
->buffer_index
= data_used
;
1275 fprintf (stderr
, "<<< %s\n", server
->buffer
);
1276 return (server
->buffer
);
1278 search_offset
+= ret
;
1286 * Function: sendline
1288 * Purpose: Sends a line of text to the POP server. The line of text
1289 * passed into this function should NOT have the carriage return
1290 * and linefeed on the end of it. Periods at beginnings of lines
1291 * will NOT be treated specially by this function.
1294 * server The server to which to send the text.
1295 * line The line of text to send.
1297 * Return value: Upon successful completion, a value of 0 will be
1298 * returned. Otherwise, a non-zero value will be returned, and
1299 * an error will be copied into pop_error.
1301 * Side effects: Closes the connection on error.
1304 sendline (server
, line
)
1308 #define SENDLINE_ERROR "Error writing to POP server: "
1311 ret
= fullwrite (server
->file
, line
, strlen (line
));
1313 { /* 0 indicates that a blank line was written */
1314 ret
= fullwrite (server
->file
, "\r\n", 2);
1320 strcpy (pop_error
, SENDLINE_ERROR
);
1321 strncat (pop_error
, strerror (errno
),
1322 ERROR_MAX
- sizeof (SENDLINE_ERROR
));
1327 fprintf (stderr
, ">>> %s\n", line
);
1333 * Procedure: fullwrite
1335 * Purpose: Just like write, but keeps trying until the entire string
1338 * Return value: Same as write. Pop_error is not set.
1341 fullwrite (fd
, buf
, nbytes
)
1350 while ((ret
= write (fd
, cp
, nbytes
)) > 0)
1362 * Purpose: Reads a line from the server. If the return indicator is
1363 * positive, return with a zero exit status. If not, return with
1364 * a negative exit status.
1367 * server The server to read from.
1369 * Returns: 0 for success, else for failure and puts error in pop_error.
1371 * Side effects: On failure, may make the connection unuseable.
1379 if (! (fromline
= getline (server
)))
1384 if (! strncmp (fromline
, "+OK", 3))
1386 else if (! strncmp (fromline
, "-ERR", 4))
1388 strncpy (pop_error
, fromline
, ERROR_MAX
);
1389 pop_error
[ERROR_MAX
-1] = '\0';
1395 "Unexpected response from server; expecting +OK or -ERR");
1403 * Function: gettermination
1405 * Purpose: Gets the next line and verifies that it is a termination
1406 * line (nothing but a dot).
1408 * Return value: 0 on success, non-zero with pop_error set on error.
1410 * Side effects: Closes the connection on error.
1413 gettermination (server
)
1418 fromserver
= getline (server
);
1422 if (strcmp (fromserver
, "."))
1425 "Unexpected response from server in gettermination");
1435 * Function pop_close
1437 * Purpose: Close a pop connection, sending a "RSET" command to try to
1438 * preserve any changes that were made and a "QUIT" command to
1439 * try to get the server to quit, but ignoring any responses that
1442 * Side effects: The server is unuseable after this function returns.
1443 * Changes made to the maildrop since the session was started (or
1444 * since the last pop_reset) may be lost.
1451 free ((char *) server
);
1457 * Function: pop_trash
1459 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
1460 * memory associated with the server. It is legal to call
1461 * pop_close or pop_quit after this function has been called.
1467 if (server
->file
>= 0)
1469 sendline (server
, "RSET");
1470 sendline (server
, "QUIT");
1472 close (server
->file
);
1476 free (server
->buffer
);
1482 /* Return a pointer to the first CRLF in IN_STRING,
1483 or 0 if it does not contain one. */
1486 find_crlf (in_string
)
1493 else if (*in_string
== '\r')
1495 if (*++in_string
== '\n')
1496 return (in_string
- 1);
1504 #endif /* MAIL_USE_POP */