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, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #define NO_SHORTNAMES /* Tell config not to load remap.h */
24 #include <../src/config.h>
32 /* Cancel these substitutions made in config.h */
39 #include <sys/types.h>
40 #include <netinet/in.h>
41 #include <sys/socket.h>
51 * It really shouldn't be necessary to put this declaration here, but
52 * the version of hesiod.h that Athena has installed in release 7.2
53 * doesn't declare this function; I don't know if the 7.3 version of
56 extern struct servent
*hes_getservbyname (/* char *, char * */);
69 #include <krb5/krb5.h>
70 #include <krb5/ext-proto.h>
75 extern char *getenv (/* char * */);
76 extern char *getlogin (/* void */);
77 extern char *getpass (/* char * */);
78 extern char *strerror (/* int */);
79 extern char *index ();
83 extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *,
84 u_long, MSG_DAT *, CREDENTIALS *, Key_schedule,
85 struct sockaddr_in *, struct sockaddr_in *,
87 extern char *krb_realmofhost (/* char * */);
91 #if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H)
95 static int socket_connection (/* char *, int */);
96 static char *getline (/* popserver */);
97 static int sendline (/* popserver, char * */);
98 static int fullwrite (/* int, char *, int */);
99 static int getok (/* popserver */);
101 static int gettermination (/* popserver */);
103 static void pop_trash (/* popserver */);
104 static char *find_crlf (/* char * */);
106 #define ERROR_MAX 80 /* a pretty arbitrary size */
108 #define KPOP_PORT 1109
109 #define POP_SERVICE "pop"
112 #define KPOP_SERVICE "k5pop";
114 #define KPOP_SERVICE "kpop"
118 char pop_error
[ERROR_MAX
];
122 #define min(a,b) (((a) < (b)) ? (a) : (b))
126 * Function: pop_open (char *host, char *username, char *password,
129 * Purpose: Establishes a connection with a post-office server, and
130 * completes the authorization portion of the session.
133 * host The server host with which the connection should be
134 * established. Optional. If omitted, internal
135 * heuristics will be used to determine the server host,
138 * The username of the mail-drop to access. Optional.
139 * If omitted, internal heuristics will be used to
140 * determine the username, if possible.
142 * The password to use for authorization. If omitted,
143 * internal heuristics will be used to determine the
144 * password, if possible.
145 * flags A bit mask containing flags controlling certain
146 * functions of the routine. Valid flags are defined in
149 * Return value: Upon successful establishment of a connection, a
150 * non-null popserver will be returned. Otherwise, null will be
151 * returned, and the string variable pop_error will contain an
152 * explanation of the error.
155 pop_open (host
, username
, password
, 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");
245 flags
|= POP_NO_KERBEROS
;
249 sock
= socket_connection (host
, flags
);
253 server
= (popserver
) malloc (sizeof (struct _popserver
));
256 strcpy (pop_error
, "Out of memory in pop_open");
259 server
->buffer
= (char *) malloc (GETLINE_MIN
);
260 if (! server
->buffer
)
262 strcpy (pop_error
, "Out of memory in pop_open");
263 free ((char *) server
);
269 server
->buffer_index
= 0;
270 server
->buffer_size
= GETLINE_MIN
;
271 server
->in_multi
= 0;
277 * I really shouldn't use the pop_error variable like this, but....
279 if (strlen (username
) > ERROR_MAX
- 6)
283 "Username too long; recompile pop.c with larger ERROR_MAX");
286 sprintf (pop_error
, "USER %s", username
);
288 if (sendline (server
, pop_error
) || getok (server
))
293 if (strlen (password
) > ERROR_MAX
- 6)
297 "Password too long; recompile pop.c with larger ERROR_MAX");
300 sprintf (pop_error
, "PASS %s", password
);
302 if (sendline (server
, pop_error
) || getok (server
))
313 * Purpose: Issue the STAT command to the server and return (in the
314 * value parameters) the number of messages in the maildrop and
315 * the total size of the maildrop.
317 * Return value: 0 on success, or non-zero with an error in pop_error
320 * Side effects: On failure, may make further operations on the
321 * connection impossible.
324 pop_stat (server
, count
, size
)
331 if (server
->in_multi
)
333 strcpy (pop_error
, "In multi-line query in pop_stat");
337 if (sendline (server
, "STAT") || (! (fromserver
= getline (server
))))
340 if (strncmp (fromserver
, "+OK ", 4))
342 if (0 == strncmp (fromserver
, "-ERR", 4))
344 strncpy (pop_error
, fromserver
, ERROR_MAX
);
349 "Unexpected response from POP server in pop_stat");
355 *count
= atoi (&fromserver
[4]);
357 fromserver
= index (&fromserver
[4], ' ');
361 "Badly formatted response from server in pop_stat");
366 *size
= atoi (fromserver
+ 1);
374 * Purpose: Performs the POP "list" command and returns (in value
375 * parameters) two malloc'd zero-terminated arrays -- one of
376 * message IDs, and a parallel one of sizes.
379 * server The pop connection to talk to.
380 * message The number of the one message about which to get
381 * information, or 0 to get information about all
384 * Return value: 0 on success, non-zero with error in pop_error on
387 * Side effects: On failure, may make further operations on the
388 * connection impossible.
391 pop_list (server
, message
, IDs
, sizes
)
400 if (server
->in_multi
)
402 strcpy (pop_error
, "In multi-line query in pop_list");
411 if (pop_stat (server
, &count
, &size
))
416 *IDs
= (int *) malloc ((how_many
+ 1) * sizeof (int));
417 *sizes
= (int *) malloc ((how_many
+ 1) * sizeof (int));
418 if (! (*IDs
&& *sizes
))
420 strcpy (pop_error
, "Out of memory in pop_list");
426 sprintf (pop_error
, "LIST %d", message
);
427 if (sendline (server
, pop_error
))
429 free ((char *) *IDs
);
430 free ((char *) *sizes
);
433 if (! (fromserver
= getline (server
)))
435 free ((char *) *IDs
);
436 free ((char *) *sizes
);
439 if (strncmp (fromserver
, "+OK ", 4))
441 if (! strncmp (fromserver
, "-ERR", 4))
442 strncpy (pop_error
, fromserver
, ERROR_MAX
);
446 "Unexpected response from server in pop_list");
449 free ((char *) *IDs
);
450 free ((char *) *sizes
);
453 (*IDs
)[0] = atoi (&fromserver
[4]);
454 fromserver
= index (&fromserver
[4], ' ');
458 "Badly formatted response from server in pop_list");
460 free ((char *) *IDs
);
461 free ((char *) *sizes
);
464 (*sizes
)[0] = atoi (fromserver
);
465 (*IDs
)[1] = (*sizes
)[1] = 0;
470 if (pop_multi_first (server
, "LIST", &fromserver
))
472 free ((char *) *IDs
);
473 free ((char *) *sizes
);
476 for (i
= 0; i
< how_many
; i
++)
478 if (pop_multi_next (server
, &fromserver
))
480 free ((char *) *IDs
);
481 free ((char *) *sizes
);
484 (*IDs
)[i
] = atoi (fromserver
);
485 fromserver
= index (fromserver
, ' ');
489 "Badly formatted response from server in pop_list");
490 free ((char *) *IDs
);
491 free ((char *) *sizes
);
495 (*sizes
)[i
] = atoi (fromserver
);
497 if (pop_multi_next (server
, &fromserver
))
499 free ((char *) *IDs
);
500 free ((char *) *sizes
);
506 "Too many response lines from server in pop_list");
507 free ((char *) *IDs
);
508 free ((char *) *sizes
);
511 (*IDs
)[i
] = (*sizes
)[i
] = 0;
517 * Function: pop_retrieve
519 * Purpose: Retrieve a specified message from the maildrop.
522 * server The server to retrieve from.
523 * message The message number to retrieve.
525 * If true, then mark the string "From " at the beginning
528 * Return value: A string pointing to the message, if successful, or
529 * null with pop_error set if not.
531 * Side effects: May kill connection on error.
534 pop_retrieve (server
, message
, markfrom
)
539 int *IDs
, *sizes
, bufsize
, fromcount
= 0, cp
= 0;
540 char *ptr
, *fromserver
;
543 if (server
->in_multi
)
545 strcpy (pop_error
, "In multi-line query in pop_retrieve");
549 if (pop_list (server
, message
, &IDs
, &sizes
))
552 if (pop_retrieve_first (server
, message
, &fromserver
))
558 * The "5" below is an arbitrary constant -- I assume that if
559 * there are "From" lines in the text to be marked, there
560 * probably won't be more than 5 of them. If there are, I
561 * allocate more space for them below.
563 bufsize
= sizes
[0] + (markfrom
? 5 : 0);
564 ptr
= (char *)malloc (bufsize
);
566 free ((char *) sizes
);
570 strcpy (pop_error
, "Out of memory in pop_retrieve");
571 pop_retrieve_flush (server
);
575 while (! (ret
= pop_retrieve_next (server
, &fromserver
)))
584 if (markfrom
&& fromserver
[0] == 'F' && fromserver
[1] == 'r' &&
585 fromserver
[2] == 'o' && fromserver
[3] == 'm' &&
586 fromserver
[4] == ' ')
588 if (++fromcount
== 5)
591 ptr
= (char *)realloc (ptr
, bufsize
);
594 strcpy (pop_error
, "Out of memory in pop_retrieve");
595 pop_retrieve_flush (server
);
602 linesize
= strlen (fromserver
);
603 bcopy (fromserver
, &ptr
[cp
], linesize
);
616 pop_retrieve_first (server
, message
, response
)
621 sprintf (pop_error
, "RETR %d", message
);
622 return (pop_multi_first (server
, pop_error
, response
));
626 pop_retrieve_next (server
, line
)
630 return (pop_multi_next (server
, line
));
634 pop_retrieve_flush (server
)
637 return (pop_multi_flush (server
));
641 pop_top_first (server
, message
, lines
, response
)
646 sprintf (pop_error
, "TOP %d %d", message
, lines
);
647 return (pop_multi_first (server
, pop_error
, response
));
651 pop_top_next (server
, line
)
655 return (pop_multi_next (server
, line
));
659 pop_top_flush (server
)
662 return (pop_multi_flush (server
));
666 pop_multi_first (server
, command
, response
)
671 if (server
->in_multi
)
674 "Already in multi-line query in pop_multi_first");
678 if (sendline (server
, command
) || (! (*response
= getline (server
))))
683 if (0 == strncmp (*response
, "-ERR", 4))
685 strncpy (pop_error
, *response
, ERROR_MAX
);
688 else if (0 == strncmp (*response
, "+OK", 3))
690 for (*response
+= 3; **response
== ' '; (*response
)++) /* empty */;
691 server
->in_multi
= 1;
697 "Unexpected response from server in pop_multi_first");
703 pop_multi_next (server
, line
)
709 if (! server
->in_multi
)
711 strcpy (pop_error
, "Not in multi-line query in pop_multi_next");
715 fromserver
= getline (server
);
721 if (fromserver
[0] == '.')
726 server
->in_multi
= 0;
731 *line
= fromserver
+ 1;
743 pop_multi_flush (server
)
748 if (! server
->in_multi
)
753 while (! pop_multi_next (server
, &line
))
764 /* Function: pop_delete
766 * Purpose: Delete a specified message.
769 * server Server from which to delete the message.
770 * message Message to delete.
772 * Return value: 0 on success, non-zero with error in pop_error
776 pop_delete (server
, message
)
780 if (server
->in_multi
)
782 strcpy (pop_error
, "In multi-line query in pop_delete");
786 sprintf (pop_error
, "DELE %d", message
);
788 if (sendline (server
, pop_error
) || getok (server
))
797 * Purpose: Send a noop command to the server.
800 * server The server to send to.
802 * Return value: 0 on success, non-zero with error in pop_error
805 * Side effects: Closes connection on error.
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.
843 if (server
->in_multi
)
845 strcpy (pop_error
, "In multi-line query in pop_last");
849 if (sendline (server
, "LAST"))
852 if (! (fromserver
= getline (server
)))
855 if (! strncmp (fromserver
, "-ERR", 4))
857 strncpy (pop_error
, fromserver
, ERROR_MAX
);
860 else if (strncmp (fromserver
, "+OK ", 4))
862 strcpy (pop_error
, "Unexpected response from server in pop_last");
868 return (atoi (&fromserver
[4]));
873 * Function: pop_reset
875 * Purpose: Reset the server to its initial connect state
880 * Return value: 0 for success, non-0 with error in pop_error
883 * Side effects: Closes the connection on error.
889 if (pop_retrieve_flush (server
))
894 if (sendline (server
, "RSET") || getok (server
))
903 * Purpose: Quit the connection to the server,
906 * server The server to quit.
908 * Return value: 0 for success, non-zero otherwise with error in
911 * Side Effects: The popserver passed in is unusable after this
912 * function is called, even if an error occurs.
920 if (server
->file
>= 0)
922 if (pop_retrieve_flush (server
))
927 if (sendline (server
, "QUIT") || getok (server
))
932 close (server
->file
);
936 free (server
->buffer
);
937 free ((char *) server
);
943 * Function: socket_connection
945 * Purpose: Opens the network connection with the mail host, without
946 * doing any sort of I/O with it or anything.
949 * host The host to which to connect.
950 * flags Option flags.
952 * Return value: A file descriptor indicating the connection, or -1
953 * indicating failure, in which case an error has been copied
957 socket_connection (host
, flags
)
961 struct hostent
*hostent
;
962 struct servent
*servent
;
963 struct sockaddr_in addr
;
971 krb5_principal client
, server
;
978 Key_schedule schedule
;
981 #endif /* KERBEROS */
987 hostent
= gethostbyname (host
);
989 if ((! hostent
) && ((h_errno
!= TRY_AGAIN
) || (try_count
== 5)))
991 strcpy (pop_error
, "Could not determine POP server's address");
996 bzero ((char *) &addr
, sizeof (addr
));
997 addr
.sin_family
= AF_INET
;
1000 service
= (flags
& POP_NO_KERBEROS
) ? POP_SERVICE
: KPOP_SERVICE
;
1002 service
= POP_SERVICE
;
1006 if (! (flags
& POP_NO_HESIOD
))
1008 servent
= hes_getservbyname (service
, "tcp");
1011 addr
.sin_port
= servent
->s_port
;
1018 servent
= getservbyname (service
, "tcp");
1021 addr
.sin_port
= servent
->s_port
;
1026 addr
.sin_port
= htons ((flags
& POP_NO_KERBEROS
) ?
1027 POP_PORT
: KPOP_PORT
);
1029 addr
.sin_port
= htons (POP_PORT
);
1034 #define SOCKET_ERROR "Could not create socket for POP connection: "
1036 sock
= socket (PF_INET
, SOCK_STREAM
, 0);
1039 strcpy (pop_error
, SOCKET_ERROR
);
1040 strncat (pop_error
, strerror (errno
),
1041 ERROR_MAX
- sizeof (SOCKET_ERROR
));
1046 while (*hostent
->h_addr_list
)
1048 bcopy (*hostent
->h_addr_list
, (char *) &addr
.sin_addr
,
1050 if (! connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)))
1052 hostent
->h_addr_list
++;
1055 #define CONNECT_ERROR "Could not connect to POP server: "
1057 if (! *hostent
->h_addr_list
)
1059 (void) close (sock
);
1060 strcpy (pop_error
, CONNECT_ERROR
);
1061 strncat (pop_error
, strerror (errno
),
1062 ERROR_MAX
- sizeof (CONNECT_ERROR
));
1068 #define KRB_ERROR "Kerberos error connecting to POP server: "
1069 if (! (flags
& POP_NO_KERBEROS
))
1074 if (rem
= krb5_cc_default (&ccdef
))
1077 strcpy (pop_error
, KRB_ERROR
);
1078 strncat (pop_error
, error_message (rem
),
1079 ERROR_MAX
- sizeof(KRB_ERROR
));
1080 (void) close (sock
);
1084 if (rem
= krb5_cc_get_principal (ccdef
, &client
))
1089 for (cp
= hostent
->h_name
; *cp
; cp
++)
1093 *cp
= tolower (*cp
);
1097 if (rem
= krb5_sname_to_principal (hostent
->h_name
, POP_SERVICE
,
1103 rem
= krb5_sendauth ((krb5_pointer
) &sock
, "KPOPV1.0", client
, server
,
1104 AP_OPTS_MUTUAL_REQUIRED
,
1105 0, /* no checksum */
1106 0, /* no creds, use ccache instead */
1108 0, /* don't need seq # */
1109 0, /* don't need subsession key */
1111 0); /* don't need reply */
1112 krb5_free_principal (server
);
1115 if (err_ret
&& err_ret
->text
.length
)
1117 strcpy (pop_error
, KRB_ERROR
);
1118 strncat (pop_error
, error_message (rem
),
1119 ERROR_MAX
- sizeof (KRB_ERROR
));
1120 strncat (pop_error
, " [server says '",
1121 ERROR_MAX
- strlen (pop_error
) - 1);
1122 strncat (pop_error
, err_ret
->text
.data
,
1123 min (ERROR_MAX
- strlen (pop_error
) - 1,
1124 err_ret
->text
.length
));
1125 strncat (pop_error
, "']",
1126 ERROR_MAX
- strlen (pop_error
) - 1);
1130 strcpy (pop_error
, KRB_ERROR
);
1131 strncat (pop_error
, error_message (rem
),
1132 ERROR_MAX
- sizeof (KRB_ERROR
));
1135 krb5_free_error (err_ret
);
1137 (void) close (sock
);
1141 ticket
= (KTEXT
) malloc (sizeof (KTEXT_ST
));
1142 rem
= krb_sendauth (0L, sock
, ticket
, "pop", hostent
->h_name
,
1143 (char *) krb_realmofhost (hostent
->h_name
),
1144 (unsigned long) 0, &msg_data
, &cred
, schedule
,
1145 (struct sockaddr_in
*) 0,
1146 (struct sockaddr_in
*) 0,
1148 free ((char *) ticket
);
1149 if (rem
!= KSUCCESS
)
1151 strcpy (pop_error
, KRB_ERROR
);
1152 strncat (pop_error
, krb_err_txt
[rem
],
1153 ERROR_MAX
- sizeof (KRB_ERROR
));
1154 (void) close (sock
);
1159 #endif /* KERBEROS */
1162 } /* socket_connection */
1167 * Purpose: Get a line of text from the connection and return a
1168 * pointer to it. The carriage return and linefeed at the end of
1169 * the line are stripped, but periods at the beginnings of lines
1170 * are NOT dealt with in any special way.
1173 * server The server from which to get the line of text.
1175 * Returns: A non-null pointer if successful, or a null pointer on any
1176 * error, with an error message copied into pop_error.
1178 * Notes: The line returned is overwritten with each call to getline.
1180 * Side effects: Closes the connection on error.
1186 #define GETLINE_ERROR "Error reading from server: "
1189 int search_offset
= 0;
1193 char *cp
= find_crlf (server
->buffer
+ server
->buffer_index
);
1199 found
= server
->buffer_index
;
1200 data_used
= (cp
+ 2) - server
->buffer
- found
;
1202 *cp
= '\0'; /* terminate the string to be returned */
1203 server
->data
-= data_used
;
1204 server
->buffer_index
+= data_used
;
1207 fprintf (stderr
, "<<< %s\n", server
->buffer
+ found
);
1208 return (server
->buffer
+ found
);
1212 bcopy (server
->buffer
+ server
->buffer_index
,
1213 server
->buffer
, server
->data
);
1214 /* Record the fact that we've searched the data already in
1215 the buffer for a CRLF, so that when we search below, we
1216 don't have to search the same data twice. There's a "-
1217 1" here to account for the fact that the last character
1218 of the data we have may be the CR of a CRLF pair, of
1219 which we haven't read the second half yet, so we may have
1220 to search it again when we read more data. */
1221 search_offset
= server
->data
- 1;
1222 server
->buffer_index
= 0;
1227 server
->buffer_index
= 0;
1232 /* There's a "- 1" here to leave room for the null that we put
1233 at the end of the read data below. We put the null there so
1234 that find_crlf knows where to stop when we call it. */
1235 if (server
->data
== server
->buffer_size
- 1)
1237 server
->buffer_size
+= GETLINE_INCR
;
1238 server
->buffer
= (char *)realloc (server
->buffer
, server
->buffer_size
);
1239 if (! server
->buffer
)
1241 strcpy (pop_error
, "Out of memory in getline");
1246 ret
= read (server
->file
, server
->buffer
+ server
->data
,
1247 server
->buffer_size
- server
->data
- 1);
1250 strcpy (pop_error
, GETLINE_ERROR
);
1251 strncat (pop_error
, strerror (errno
),
1252 ERROR_MAX
- sizeof (GETLINE_ERROR
));
1258 strcpy (pop_error
, "Unexpected EOF from server in getline");
1265 server
->data
+= ret
;
1266 server
->buffer
[server
->data
] = '\0';
1268 cp
= find_crlf (server
->buffer
+ search_offset
);
1271 int data_used
= (cp
+ 2) - server
->buffer
;
1273 server
->data
-= data_used
;
1274 server
->buffer_index
= data_used
;
1277 fprintf (stderr
, "<<< %s\n", server
->buffer
);
1278 return (server
->buffer
);
1280 search_offset
+= ret
;
1288 * Function: sendline
1290 * Purpose: Sends a line of text to the POP server. The line of text
1291 * passed into this function should NOT have the carriage return
1292 * and linefeed on the end of it. Periods at beginnings of lines
1293 * will NOT be treated specially by this function.
1296 * server The server to which to send the text.
1297 * line The line of text to send.
1299 * Return value: Upon successful completion, a value of 0 will be
1300 * returned. Otherwise, a non-zero value will be returned, and
1301 * an error will be copied into pop_error.
1303 * Side effects: Closes the connection on error.
1306 sendline (server
, line
)
1310 #define SENDLINE_ERROR "Error writing to POP server: "
1313 ret
= fullwrite (server
->file
, line
, strlen (line
));
1315 { /* 0 indicates that a blank line was written */
1316 ret
= fullwrite (server
->file
, "\r\n", 2);
1322 strcpy (pop_error
, SENDLINE_ERROR
);
1323 strncat (pop_error
, strerror (errno
),
1324 ERROR_MAX
- sizeof (SENDLINE_ERROR
));
1329 fprintf (stderr
, ">>> %s\n", line
);
1335 * Procedure: fullwrite
1337 * Purpose: Just like write, but keeps trying until the entire string
1340 * Return value: Same as write. Pop_error is not set.
1343 fullwrite (fd
, buf
, nbytes
)
1352 while ((ret
= write (fd
, cp
, nbytes
)) > 0)
1364 * Purpose: Reads a line from the server. If the return indicator is
1365 * positive, return with a zero exit status. If not, return with
1366 * a negative exit status.
1369 * server The server to read from.
1371 * Returns: 0 for success, else for failure and puts error in pop_error.
1373 * Side effects: On failure, may make the connection unusable.
1381 if (! (fromline
= getline (server
)))
1386 if (! strncmp (fromline
, "+OK", 3))
1388 else if (! strncmp (fromline
, "-ERR", 4))
1390 strncpy (pop_error
, fromline
, ERROR_MAX
);
1391 pop_error
[ERROR_MAX
-1] = '\0';
1397 "Unexpected response from server; expecting +OK or -ERR");
1405 * Function: gettermination
1407 * Purpose: Gets the next line and verifies that it is a termination
1408 * line (nothing but a dot).
1410 * Return value: 0 on success, non-zero with pop_error set on error.
1412 * Side effects: Closes the connection on error.
1415 gettermination (server
)
1420 fromserver
= getline (server
);
1424 if (strcmp (fromserver
, "."))
1427 "Unexpected response from server in gettermination");
1437 * Function pop_close
1439 * Purpose: Close a pop connection, sending a "RSET" command to try to
1440 * preserve any changes that were made and a "QUIT" command to
1441 * try to get the server to quit, but ignoring any responses that
1444 * Side effects: The server is unusable after this function returns.
1445 * Changes made to the maildrop since the session was started (or
1446 * since the last pop_reset) may be lost.
1453 free ((char *) server
);
1459 * Function: pop_trash
1461 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
1462 * memory associated with the server. It is legal to call
1463 * pop_close or pop_quit after this function has been called.
1469 if (server
->file
>= 0)
1471 sendline (server
, "RSET");
1472 sendline (server
, "QUIT");
1474 close (server
->file
);
1478 free (server
->buffer
);
1484 /* Return a pointer to the first CRLF in IN_STRING,
1485 or 0 if it does not contain one. */
1488 find_crlf (in_string
)
1495 else if (*in_string
== '\r')
1497 if (*++in_string
== '\n')
1498 return (in_string
- 1);
1506 #endif /* MAIL_USE_POP */