declare smobs in alloc.c
[bpt/emacs.git] / lib-src / pop.c
CommitLineData
9c0f2dac 1/* pop.c: client routines for talking to a POP3-protocol post-office server
95df8112 2
ba318903 3Copyright (C) 1991, 1993, 1996-1997, 1999, 2001-2014 Free Software
ab422c4d 4Foundation, Inc.
294981c7
GM
5
6Author: Jonathan Kamens <jik@security.ov.com>
9c0f2dac
RS
7
8This file is part of GNU Emacs.
9
294981c7 10GNU Emacs is free software: you can redistribute it and/or modify
9c0f2dac 11it under the terms of the GNU General Public License as published by
294981c7
GM
12the Free Software Foundation, either version 3 of the License, or
13(at your option) any later version.
9c0f2dac
RS
14
15GNU Emacs is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License for more details.
19
20You should have received a copy of the GNU General Public License
294981c7
GM
21along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22
9c0f2dac 23
2f8fe2f4 24#include <config.h>
c9c3d8f6 25
8148baf9
RS
26#ifdef MAIL_USE_POP
27
9c0f2dac 28#include <sys/types.h>
c2668a61
RS
29#ifdef WINDOWSNT
30#include "ntlib.h"
31#include <winsock.h>
32#undef SOCKET_ERROR
5e617bc2
JB
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)
c2668a61 36#else
9c0f2dac
RS
37#include <netinet/in.h>
38#include <sys/socket.h>
5e617bc2
JB
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)
c2668a61 42#endif
9c0f2dac 43#include <pop.h>
625ad89f 44
9c0f2dac
RS
45#ifdef sun
46#include <malloc.h>
8148baf9 47#endif /* sun */
625ad89f 48
9c0f2dac
RS
49#ifdef HESIOD
50#include <hesiod.h>
51/*
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
55 * hesiod.h does.
56 */
57extern struct servent *hes_getservbyname (/* char *, char * */);
58#endif
625ad89f 59
9c0f2dac 60#include <pwd.h>
9c0f2dac
RS
61#include <netdb.h>
62#include <errno.h>
63#include <stdio.h>
a27738b6 64#include <string.h>
020d3015 65#include <unistd.h>
625ad89f 66
9c0f2dac 67#ifdef KERBEROS
a27738b6
RS
68# ifdef HAVE_KRB5_H
69# include <krb5.h>
70# endif
a27738b6
RS
71# ifdef HAVE_KRB_H
72# include <krb.h>
73# else
74# ifdef HAVE_KERBEROSIV_KRB_H
75# include <kerberosIV/krb.h>
76# else
77# ifdef HAVE_KERBEROS_KRB_H
78# include <kerberos/krb.h>
79# endif
80# endif
81# endif
82# ifdef HAVE_COM_ERR_H
83# include <com_err.h>
84# endif
8148baf9 85#endif /* KERBEROS */
9c0f2dac 86
ba01e9d7
PE
87#include <min-max.h>
88
8148baf9 89#ifdef KERBEROS
a27738b6 90#ifndef KERBEROS5
9c0f2dac 91extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *,
8148baf9
RS
92 u_long, MSG_DAT *, CREDENTIALS *, Key_schedule,
93 struct sockaddr_in *, struct sockaddr_in *,
94 char * */);
9c0f2dac 95extern char *krb_realmofhost (/* char * */);
a27738b6 96#endif /* ! KERBEROS5 */
8148baf9 97#endif /* KERBEROS */
9c0f2dac 98
c2668a61 99#ifndef WINDOWSNT
cf38a720 100#ifndef HAVE_H_ERRNO
9c0f2dac
RS
101extern int h_errno;
102#endif
c2668a61 103#endif
9c0f2dac 104
f57e2426
J
105static int socket_connection (char *, int);
106static int pop_getline (popserver, char **);
988e88ab 107static int sendline (popserver, const char *);
f57e2426
J
108static int fullwrite (int, char *, int);
109static int getok (popserver);
9c0f2dac 110#if 0
f57e2426 111static int gettermination (popserver);
9c0f2dac 112#endif
f57e2426
J
113static void pop_trash (popserver);
114static char *find_crlf (char *, int);
7a481e50 115
5bf244f1 116#define ERROR_MAX 160 /* a pretty arbitrary size, but needs
a47bf648
DL
117 to be bigger than the original
118 value of 80 */
9c0f2dac 119#define POP_PORT 110
c2668a61 120#define POP_SERVICE "pop3" /* we don't want the POP2 port! */
9c0f2dac 121#ifdef KERBEROS
0d7fdc0e 122#define KPOP_PORT 1109
81264b38 123#define KPOP_SERVICE "kpop" /* never used: look for 20060515 to see why */
9c0f2dac 124#endif
9c0f2dac
RS
125
126char pop_error[ERROR_MAX];
96c06863 127bool pop_debug = false;
9c0f2dac 128
9c0f2dac
RS
129/*
130 * Function: pop_open (char *host, char *username, char *password,
131 * int flags)
132 *
133 * Purpose: Establishes a connection with a post-office server, and
134 * completes the authorization portion of the session.
135 *
136 * Arguments:
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,
140 * if possible.
141 * username
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.
145 * password
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
151 * the file pop.h
152 *
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.
157 */
158popserver
873fbd0b 159pop_open (char *host, char *username, char *password, int flags)
9c0f2dac
RS
160{
161 int sock;
162 popserver server;
163
164 /* Determine the user name */
165 if (! username)
166 {
167 username = getenv ("USER");
168 if (! (username && *username))
169 {
170 username = getlogin ();
171 if (! (username && *username))
172 {
173 struct passwd *passwd;
174 passwd = getpwuid (getuid ());
175 if (passwd && passwd->pw_name && *passwd->pw_name)
176 {
177 username = passwd->pw_name;
178 }
179 else
180 {
181 strcpy (pop_error, "Could not determine username");
182 return (0);
183 }
184 }
185 }
186 }
187
188 /*
189 * Determine the mail host.
190 */
191
192 if (! host)
193 {
194 host = getenv ("MAILHOST");
195 }
196
197#ifdef HESIOD
198 if ((! host) && (! (flags & POP_NO_HESIOD)))
199 {
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
204 && *office->po_host)
205 {
206 host = office->po_host;
207 username = office->po_name;
208 }
209 }
210#endif
211
212#ifdef MAILHOST
213 if (! host)
214 {
215 host = MAILHOST;
216 }
217#endif
218
219 if (! host)
220 {
221 strcpy (pop_error, "Could not determine POP server");
222 return (0);
223 }
224
225 /* Determine the password */
bbcac09c
RS
226#ifdef KERBEROS
227#define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS))
9c0f2dac 228#else
bbcac09c 229#define DONT_NEED_PASSWORD 0
9c0f2dac 230#endif
177c0ea7 231
9c0f2dac
RS
232 if ((! password) && (! DONT_NEED_PASSWORD))
233 {
234 if (! (flags & POP_NO_GETPASS))
235 {
236 password = getpass ("Enter POP password:");
237 }
238 if (! password)
239 {
240 strcpy (pop_error, "Could not determine POP password");
241 return (0);
242 }
243 }
81264b38 244 if (password) /* always true, detected 20060515 */
bbcac09c 245 flags |= POP_NO_KERBEROS;
9c0f2dac 246 else
81264b38
FP
247 password = username; /* dead code, detected 20060515 */
248 /** "kpop" service is never used: look for 20060515 to see why **/
9c0f2dac
RS
249
250 sock = socket_connection (host, flags);
251 if (sock == -1)
252 return (0);
253
254 server = (popserver) malloc (sizeof (struct _popserver));
255 if (! server)
256 {
257 strcpy (pop_error, "Out of memory in pop_open");
258 return (0);
259 }
260 server->buffer = (char *) malloc (GETLINE_MIN);
261 if (! server->buffer)
262 {
263 strcpy (pop_error, "Out of memory in pop_open");
264 free ((char *) server);
265 return (0);
266 }
80b2cbf2 267
9c0f2dac
RS
268 server->file = sock;
269 server->data = 0;
270 server->buffer_index = 0;
271 server->buffer_size = GETLINE_MIN;
96c06863
PE
272 server->in_multi = false;
273 server->trash_started = false;
9c0f2dac
RS
274
275 if (getok (server))
276 return (0);
277
278 /*
279 * I really shouldn't use the pop_error variable like this, but....
280 */
281 if (strlen (username) > ERROR_MAX - 6)
282 {
283 pop_close (server);
284 strcpy (pop_error,
285 "Username too long; recompile pop.c with larger ERROR_MAX");
286 return (0);
287 }
288 sprintf (pop_error, "USER %s", username);
289
290 if (sendline (server, pop_error) || getok (server))
291 {
292 return (0);
293 }
294
295 if (strlen (password) > ERROR_MAX - 6)
296 {
297 pop_close (server);
298 strcpy (pop_error,
299 "Password too long; recompile pop.c with larger ERROR_MAX");
300 return (0);
301 }
302 sprintf (pop_error, "PASS %s", password);
303
304 if (sendline (server, pop_error) || getok (server))
305 {
306 return (0);
307 }
308
309 return (server);
310}
311
312/*
313 * Function: pop_stat
314 *
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.
318 *
319 * Return value: 0 on success, or non-zero with an error in pop_error
320 * in failure.
321 *
322 * Side effects: On failure, may make further operations on the
323 * connection impossible.
324 */
325int
873fbd0b 326pop_stat (popserver server, int *count, int *size)
9c0f2dac
RS
327{
328 char *fromserver;
0b074993 329 char *end_ptr;
9c0f2dac
RS
330
331 if (server->in_multi)
332 {
333 strcpy (pop_error, "In multi-line query in pop_stat");
334 return (-1);
335 }
80b2cbf2 336
60b0a379 337 if (sendline (server, "STAT") || (pop_getline (server, &fromserver) < 0))
9c0f2dac
RS
338 return (-1);
339
340 if (strncmp (fromserver, "+OK ", 4))
341 {
342 if (0 == strncmp (fromserver, "-ERR", 4))
e99a530f 343 snprintf (pop_error, ERROR_MAX, "%s", fromserver);
9c0f2dac
RS
344 else
345 {
346 strcpy (pop_error,
347 "Unexpected response from POP server in pop_stat");
348 pop_trash (server);
349 }
350 return (-1);
351 }
352
0b074993
CY
353 errno = 0;
354 *count = strtol (&fromserver[4], &end_ptr, 10);
355 /* Check validity of string-to-integer conversion. */
59a431d6 356 if (fromserver + 4 == end_ptr || *end_ptr != ' ' || errno)
0b074993
CY
357 {
358 strcpy (pop_error, "Unexpected response from POP server in pop_stat");
359 pop_trash (server);
360 return (-1);
361 }
80b2cbf2 362
59a431d6 363 fromserver = end_ptr;
9c0f2dac 364
0b074993
CY
365 errno = 0;
366 *size = strtol (fromserver + 1, &end_ptr, 10);
59a431d6 367 if (fromserver + 1 == end_ptr || errno)
0b074993
CY
368 {
369 strcpy (pop_error, "Unexpected response from POP server in pop_stat");
370 pop_trash (server);
371 return (-1);
372 }
9c0f2dac
RS
373
374 return (0);
375}
376
377/*
378 * Function: pop_list
379 *
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.
383 *
384 * Arguments:
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
388 * messages.
389 *
390 * Return value: 0 on success, non-zero with error in pop_error on
391 * failure.
392 *
393 * Side effects: On failure, may make further operations on the
394 * connection impossible.
395 */
396int
873fbd0b 397pop_list (popserver server, int message, int **IDs, int **sizes)
9c0f2dac
RS
398{
399 int how_many, i;
400 char *fromserver;
401
402 if (server->in_multi)
403 {
404 strcpy (pop_error, "In multi-line query in pop_list");
405 return (-1);
406 }
407
408 if (message)
409 how_many = 1;
410 else
411 {
412 int count, size;
413 if (pop_stat (server, &count, &size))
414 return (-1);
415 how_many = count;
416 }
417
418 *IDs = (int *) malloc ((how_many + 1) * sizeof (int));
419 *sizes = (int *) malloc ((how_many + 1) * sizeof (int));
420 if (! (*IDs && *sizes))
421 {
422 strcpy (pop_error, "Out of memory in pop_list");
423 return (-1);
424 }
425
426 if (message)
427 {
428 sprintf (pop_error, "LIST %d", message);
429 if (sendline (server, pop_error))
430 {
431 free ((char *) *IDs);
432 free ((char *) *sizes);
433 return (-1);
434 }
60b0a379 435 if (pop_getline (server, &fromserver) < 0)
9c0f2dac
RS
436 {
437 free ((char *) *IDs);
438 free ((char *) *sizes);
439 return (-1);
440 }
441 if (strncmp (fromserver, "+OK ", 4))
442 {
443 if (! strncmp (fromserver, "-ERR", 4))
e99a530f 444 snprintf (pop_error, ERROR_MAX, "%s", fromserver);
9c0f2dac
RS
445 else
446 {
447 strcpy (pop_error,
448 "Unexpected response from server in pop_list");
449 pop_trash (server);
450 }
451 free ((char *) *IDs);
452 free ((char *) *sizes);
453 return (-1);
454 }
455 (*IDs)[0] = atoi (&fromserver[4]);
8966b757 456 fromserver = strchr (&fromserver[4], ' ');
9c0f2dac
RS
457 if (! fromserver)
458 {
459 strcpy (pop_error,
460 "Badly formatted response from server in pop_list");
461 pop_trash (server);
462 free ((char *) *IDs);
463 free ((char *) *sizes);
464 return (-1);
465 }
466 (*sizes)[0] = atoi (fromserver);
467 (*IDs)[1] = (*sizes)[1] = 0;
468 return (0);
469 }
470 else
471 {
472 if (pop_multi_first (server, "LIST", &fromserver))
473 {
474 free ((char *) *IDs);
475 free ((char *) *sizes);
476 return (-1);
477 }
478 for (i = 0; i < how_many; i++)
479 {
d89d0243 480 if (pop_multi_next (server, &fromserver) <= 0)
9c0f2dac
RS
481 {
482 free ((char *) *IDs);
483 free ((char *) *sizes);
484 return (-1);
485 }
486 (*IDs)[i] = atoi (fromserver);
8966b757 487 fromserver = strchr (fromserver, ' ');
9c0f2dac
RS
488 if (! fromserver)
489 {
490 strcpy (pop_error,
491 "Badly formatted response from server in pop_list");
492 free ((char *) *IDs);
493 free ((char *) *sizes);
494 pop_trash (server);
495 return (-1);
496 }
497 (*sizes)[i] = atoi (fromserver);
498 }
d89d0243 499 if (pop_multi_next (server, &fromserver) < 0)
9c0f2dac
RS
500 {
501 free ((char *) *IDs);
502 free ((char *) *sizes);
503 return (-1);
504 }
505 else if (fromserver)
506 {
507 strcpy (pop_error,
508 "Too many response lines from server in pop_list");
509 free ((char *) *IDs);
510 free ((char *) *sizes);
511 return (-1);
512 }
513 (*IDs)[i] = (*sizes)[i] = 0;
514 return (0);
515 }
516}
517
518/*
519 * Function: pop_retrieve
520 *
521 * Purpose: Retrieve a specified message from the maildrop.
522 *
523 * Arguments:
524 * server The server to retrieve from.
525 * message The message number to retrieve.
526 * markfrom
527 * If true, then mark the string "From " at the beginning
528 * of lines with '>'.
d89d0243
KH
529 * msg_buf Output parameter to which a buffer containing the
530 * message is assigned.
177c0ea7 531 *
d89d0243
KH
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.
9c0f2dac
RS
535 *
536 * Side effects: May kill connection on error.
537 */
d89d0243 538int
873fbd0b 539pop_retrieve (popserver server, int message, int markfrom, char **msg_buf)
9c0f2dac
RS
540{
541 int *IDs, *sizes, bufsize, fromcount = 0, cp = 0;
542 char *ptr, *fromserver;
543 int ret;
544
545 if (server->in_multi)
546 {
547 strcpy (pop_error, "In multi-line query in pop_retrieve");
d89d0243 548 return (-1);
9c0f2dac
RS
549 }
550
551 if (pop_list (server, message, &IDs, &sizes))
d89d0243 552 return (-1);
9c0f2dac
RS
553
554 if (pop_retrieve_first (server, message, &fromserver))
555 {
d89d0243 556 return (-1);
9c0f2dac
RS
557 }
558
559 /*
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.
564 */
565 bufsize = sizes[0] + (markfrom ? 5 : 0);
7d665945 566 ptr = (char *)malloc (bufsize);
9c0f2dac
RS
567 free ((char *) IDs);
568 free ((char *) sizes);
569
570 if (! ptr)
571 {
572 strcpy (pop_error, "Out of memory in pop_retrieve");
573 pop_retrieve_flush (server);
d89d0243 574 return (-1);
9c0f2dac
RS
575 }
576
d89d0243 577 while ((ret = pop_retrieve_next (server, &fromserver)) >= 0)
9c0f2dac 578 {
9c0f2dac
RS
579 if (! fromserver)
580 {
581 ptr[cp] = '\0';
d89d0243
KH
582 *msg_buf = ptr;
583 return (cp);
9c0f2dac
RS
584 }
585 if (markfrom && fromserver[0] == 'F' && fromserver[1] == 'r' &&
586 fromserver[2] == 'o' && fromserver[3] == 'm' &&
587 fromserver[4] == ' ')
588 {
589 if (++fromcount == 5)
590 {
591 bufsize += 5;
7d665945 592 ptr = (char *)realloc (ptr, bufsize);
9c0f2dac
RS
593 if (! ptr)
594 {
595 strcpy (pop_error, "Out of memory in pop_retrieve");
596 pop_retrieve_flush (server);
d89d0243 597 return (-1);
9c0f2dac
RS
598 }
599 fromcount = 0;
600 }
601 ptr[cp++] = '>';
602 }
72af86bd 603 memcpy (&ptr[cp], fromserver, ret);
d89d0243 604 cp += ret;
9c0f2dac
RS
605 ptr[cp++] = '\n';
606 }
607
d89d0243
KH
608 free (ptr);
609 return (-1);
177c0ea7 610}
9c0f2dac
RS
611
612int
873fbd0b 613pop_retrieve_first (popserver server, int message, char **response)
9c0f2dac
RS
614{
615 sprintf (pop_error, "RETR %d", message);
616 return (pop_multi_first (server, pop_error, response));
617}
618
d89d0243
KH
619/*
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).
625 */
626
9c0f2dac 627int
873fbd0b 628pop_retrieve_next (popserver server, char **line)
9c0f2dac
RS
629{
630 return (pop_multi_next (server, line));
631}
632
633int
873fbd0b 634pop_retrieve_flush (popserver server)
9c0f2dac
RS
635{
636 return (pop_multi_flush (server));
637}
638
639int
873fbd0b 640pop_top_first (popserver server, int message, int lines, char **response)
9c0f2dac
RS
641{
642 sprintf (pop_error, "TOP %d %d", message, lines);
643 return (pop_multi_first (server, pop_error, response));
644}
645
d89d0243
KH
646/*
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).
652 */
653
9c0f2dac 654int
873fbd0b 655pop_top_next (popserver server, char **line)
9c0f2dac
RS
656{
657 return (pop_multi_next (server, line));
658}
659
660int
873fbd0b 661pop_top_flush (popserver server)
9c0f2dac
RS
662{
663 return (pop_multi_flush (server));
664}
665
666int
988e88ab 667pop_multi_first (popserver server, const char *command, char **response)
9c0f2dac
RS
668{
669 if (server->in_multi)
670 {
671 strcpy (pop_error,
672 "Already in multi-line query in pop_multi_first");
673 return (-1);
674 }
675
60b0a379 676 if (sendline (server, command) || (pop_getline (server, response) < 0))
9c0f2dac
RS
677 {
678 return (-1);
679 }
680
681 if (0 == strncmp (*response, "-ERR", 4))
682 {
e99a530f 683 snprintf (pop_error, ERROR_MAX, "%s", *response);
9c0f2dac
RS
684 return (-1);
685 }
686 else if (0 == strncmp (*response, "+OK", 3))
687 {
688 for (*response += 3; **response == ' '; (*response)++) /* empty */;
96c06863 689 server->in_multi = true;
9c0f2dac
RS
690 return (0);
691 }
692 else
693 {
694 strcpy (pop_error,
695 "Unexpected response from server in pop_multi_first");
696 return (-1);
697 }
698}
699
d89d0243
KH
700/*
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. */
708
9c0f2dac 709int
873fbd0b 710pop_multi_next (popserver server, char **line)
9c0f2dac
RS
711{
712 char *fromserver;
d89d0243 713 int ret;
9c0f2dac
RS
714
715 if (! server->in_multi)
716 {
717 strcpy (pop_error, "Not in multi-line query in pop_multi_next");
718 return (-1);
719 }
720
60b0a379 721 if ((ret = pop_getline (server, &fromserver)) < 0)
9c0f2dac
RS
722 {
723 return (-1);
724 }
725
726 if (fromserver[0] == '.')
727 {
728 if (! fromserver[1])
729 {
730 *line = 0;
96c06863 731 server->in_multi = false;
9c0f2dac
RS
732 return (0);
733 }
734 else
735 {
736 *line = fromserver + 1;
d89d0243 737 return (ret - 1);
9c0f2dac
RS
738 }
739 }
740 else
741 {
742 *line = fromserver;
d89d0243 743 return (ret);
9c0f2dac
RS
744 }
745}
746
747int
873fbd0b 748pop_multi_flush (popserver server)
9c0f2dac
RS
749{
750 char *line;
d89d0243 751 int ret;
9c0f2dac
RS
752
753 if (! server->in_multi)
754 {
755 return (0);
756 }
757
d89d0243 758 while ((ret = pop_multi_next (server, &line)))
9c0f2dac 759 {
d89d0243
KH
760 if (ret < 0)
761 return (-1);
9c0f2dac
RS
762 }
763
d89d0243 764 return (0);
9c0f2dac
RS
765}
766
767/* Function: pop_delete
768 *
769 * Purpose: Delete a specified message.
770 *
771 * Arguments:
772 * server Server from which to delete the message.
773 * message Message to delete.
774 *
775 * Return value: 0 on success, non-zero with error in pop_error
776 * otherwise.
777 */
778int
873fbd0b 779pop_delete (popserver server, int message)
9c0f2dac
RS
780{
781 if (server->in_multi)
782 {
783 strcpy (pop_error, "In multi-line query in pop_delete");
784 return (-1);
785 }
786
787 sprintf (pop_error, "DELE %d", message);
788
789 if (sendline (server, pop_error) || getok (server))
790 return (-1);
791
792 return (0);
793}
794
795/*
796 * Function: pop_noop
797 *
798 * Purpose: Send a noop command to the server.
799 *
800 * Argument:
801 * server The server to send to.
802 *
803 * Return value: 0 on success, non-zero with error in pop_error
804 * otherwise.
805 *
806 * Side effects: Closes connection on error.
807 */
808int
873fbd0b 809pop_noop (popserver server)
9c0f2dac
RS
810{
811 if (server->in_multi)
812 {
813 strcpy (pop_error, "In multi-line query in pop_noop");
814 return (-1);
815 }
816
817 if (sendline (server, "NOOP") || getok (server))
818 return (-1);
819
820 return (0);
821}
822
823/*
824 * Function: pop_last
825 *
826 * Purpose: Find out the highest seen message from the server.
827 *
828 * Arguments:
829 * server The server.
830 *
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.
834 *
835 * Side effects: Closes the connection on error.
836 */
837int
873fbd0b 838pop_last (popserver server)
9c0f2dac
RS
839{
840 char *fromserver;
80b2cbf2 841
9c0f2dac
RS
842 if (server->in_multi)
843 {
844 strcpy (pop_error, "In multi-line query in pop_last");
845 return (-1);
846 }
847
848 if (sendline (server, "LAST"))
849 return (-1);
850
60b0a379 851 if (pop_getline (server, &fromserver) < 0)
9c0f2dac
RS
852 return (-1);
853
854 if (! strncmp (fromserver, "-ERR", 4))
855 {
e99a530f 856 snprintf (pop_error, ERROR_MAX, "%s", fromserver);
9c0f2dac
RS
857 return (-1);
858 }
859 else if (strncmp (fromserver, "+OK ", 4))
860 {
861 strcpy (pop_error, "Unexpected response from server in pop_last");
862 pop_trash (server);
863 return (-1);
864 }
865 else
866 {
0b074993
CY
867 char *end_ptr;
868 int count;
869 errno = 0;
870 count = strtol (&fromserver[4], &end_ptr, 10);
59a431d6 871 if (fromserver + 4 == end_ptr || errno)
0b074993
CY
872 {
873 strcpy (pop_error, "Unexpected response from server in pop_last");
874 pop_trash (server);
875 return (-1);
876 }
877 return count;
9c0f2dac
RS
878 }
879}
880
881/*
882 * Function: pop_reset
883 *
884 * Purpose: Reset the server to its initial connect state
885 *
886 * Arguments:
887 * server The server.
888 *
889 * Return value: 0 for success, non-0 with error in pop_error
890 * otherwise.
891 *
892 * Side effects: Closes the connection on error.
893 */
894int
873fbd0b 895pop_reset (popserver server)
9c0f2dac
RS
896{
897 if (pop_retrieve_flush (server))
898 {
899 return (-1);
900 }
901
902 if (sendline (server, "RSET") || getok (server))
903 return (-1);
904
905 return (0);
906}
907
908/*
909 * Function: pop_quit
910 *
911 * Purpose: Quit the connection to the server,
912 *
913 * Arguments:
914 * server The server to quit.
915 *
916 * Return value: 0 for success, non-zero otherwise with error in
917 * pop_error.
918 *
8e6208c5 919 * Side Effects: The popserver passed in is unusable after this
9c0f2dac
RS
920 * function is called, even if an error occurs.
921 */
922int
873fbd0b 923pop_quit (popserver server)
9c0f2dac
RS
924{
925 int ret = 0;
926
927 if (server->file >= 0)
928 {
929 if (pop_retrieve_flush (server))
930 {
931 ret = -1;
932 }
933
934 if (sendline (server, "QUIT") || getok (server))
935 {
936 ret = -1;
937 }
938
939 close (server->file);
940 }
941
c2cd06e6 942 free (server->buffer);
9c0f2dac
RS
943 free ((char *) server);
944
945 return (ret);
946}
947
c2668a61
RS
948#ifdef WINDOWSNT
949static int have_winsock = 0;
950#endif
951
9c0f2dac
RS
952/*
953 * Function: socket_connection
954 *
955 * Purpose: Opens the network connection with the mail host, without
956 * doing any sort of I/O with it or anything.
957 *
958 * Arguments:
959 * host The host to which to connect.
960 * flags Option flags.
80b2cbf2 961 *
9c0f2dac
RS
962 * Return value: A file descriptor indicating the connection, or -1
963 * indicating failure, in which case an error has been copied
964 * into pop_error.
965 */
966static int
873fbd0b 967socket_connection (char *host, int flags)
9c0f2dac 968{
33a2a872
JD
969#ifdef HAVE_GETADDRINFO
970 struct addrinfo *res, *it;
971 struct addrinfo hints;
972 int ret;
973#else /* !HAVE_GETADDRINFO */
9c0f2dac 974 struct hostent *hostent;
33a2a872 975#endif
9c0f2dac
RS
976 struct servent *servent;
977 struct sockaddr_in addr;
978 char found_port = 0;
988e88ab 979 const char *service;
9c0f2dac 980 int sock;
ee15f312 981 char *realhost;
9c0f2dac 982#ifdef KERBEROS
a27738b6 983#ifdef KERBEROS5
9c0f2dac 984 krb5_error_code rem;
a27738b6
RS
985 krb5_context kcontext = 0;
986 krb5_auth_context auth_context = 0;
9c0f2dac
RS
987 krb5_ccache ccdef;
988 krb5_principal client, server;
989 krb5_error *err_ret;
990 register char *cp;
991#else
992 KTEXT ticket;
993 MSG_DAT msg_data;
994 CREDENTIALS cred;
995 Key_schedule schedule;
996 int rem;
a27738b6 997#endif /* KERBEROS5 */
9c0f2dac
RS
998#endif /* KERBEROS */
999
1000 int try_count = 0;
33a2a872 1001 int connect_ok;
9c0f2dac 1002
c2668a61
RS
1003#ifdef WINDOWSNT
1004 {
1005 WSADATA winsockData;
1006 if (WSAStartup (0x101, &winsockData) == 0)
1007 have_winsock = 1;
1008 }
1009#endif
1010
72af86bd 1011 memset (&addr, 0, sizeof (addr));
9c0f2dac
RS
1012 addr.sin_family = AF_INET;
1013
81264b38 1014 /** "kpop" service is never used: look for 20060515 to see why **/
9c0f2dac
RS
1015#ifdef KERBEROS
1016 service = (flags & POP_NO_KERBEROS) ? POP_SERVICE : KPOP_SERVICE;
1017#else
1018 service = POP_SERVICE;
1019#endif
1020
1021#ifdef HESIOD
1022 if (! (flags & POP_NO_HESIOD))
1023 {
1024 servent = hes_getservbyname (service, "tcp");
1025 if (servent)
1026 {
1027 addr.sin_port = servent->s_port;
1028 found_port = 1;
1029 }
1030 }
1031#endif
1032 if (! found_port)
1033 {
1034 servent = getservbyname (service, "tcp");
1035 if (servent)
1036 {
1037 addr.sin_port = servent->s_port;
1038 }
1039 else
1040 {
81264b38 1041 /** "kpop" service is never used: look for 20060515 to see why **/
9c0f2dac
RS
1042#ifdef KERBEROS
1043 addr.sin_port = htons ((flags & POP_NO_KERBEROS) ?
1044 POP_PORT : KPOP_PORT);
1045#else
1046 addr.sin_port = htons (POP_PORT);
1047#endif
1048 }
1049 }
1050
a27738b6 1051#define POP_SOCKET_ERROR "Could not create socket for POP connection: "
9c0f2dac
RS
1052
1053 sock = socket (PF_INET, SOCK_STREAM, 0);
1054 if (sock < 0)
1055 {
e99a530f
PE
1056 snprintf (pop_error, ERROR_MAX, "%s%s",
1057 POP_SOCKET_ERROR, strerror (errno));
9c0f2dac 1058 return (-1);
80b2cbf2 1059
9c0f2dac
RS
1060 }
1061
33a2a872 1062#ifdef HAVE_GETADDRINFO
5e617bc2 1063 memset (&hints, 0, sizeof (hints));
33a2a872 1064 hints.ai_socktype = SOCK_STREAM;
7291159e 1065 hints.ai_flags = AI_CANONNAME;
33a2a872
JD
1066 hints.ai_family = AF_INET;
1067 do
1068 {
1069 ret = getaddrinfo (host, service, &hints, &res);
1070 try_count++;
1071 if (ret != 0 && (ret != EAI_AGAIN || try_count == 5))
1072 {
1073 strcpy (pop_error, "Could not determine POP server's address");
1074 return (-1);
1075 }
1076 } while (ret != 0);
1077
31ff141c
PE
1078 for (it = res; it; it = it->ai_next)
1079 if (it->ai_addrlen == sizeof addr)
1080 {
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))
1084 break;
1085 }
1086 connect_ok = it != NULL;
1087 if (connect_ok)
33a2a872 1088 {
31ff141c
PE
1089 realhost = alloca (strlen (it->ai_canonname) + 1);
1090 strcpy (realhost, it->ai_canonname);
33a2a872 1091 }
31ff141c
PE
1092 freeaddrinfo (res);
1093
33a2a872 1094#else /* !HAVE_GETADDRINFO */
b72f3bcb
EZ
1095 do
1096 {
1097 hostent = gethostbyname (host);
1098 try_count++;
1099 if ((! hostent) && ((h_errno != TRY_AGAIN) || (try_count == 5)))
1100 {
1101 strcpy (pop_error, "Could not determine POP server's address");
1102 return (-1);
1103 }
1104 } while (! hostent);
1105
9c0f2dac
RS
1106 while (*hostent->h_addr_list)
1107 {
72af86bd 1108 memcpy (&addr.sin_addr, *hostent->h_addr_list, hostent->h_length);
9c0f2dac
RS
1109 if (! connect (sock, (struct sockaddr *) &addr, sizeof (addr)))
1110 break;
1111 hostent->h_addr_list++;
1112 }
33a2a872 1113 connect_ok = *hostent->h_addr_list != NULL;
ee15f312
JD
1114 if (! connect_ok)
1115 {
1116 realhost = alloca (strlen (hostent->h_name) + 1);
1117 strcpy (realhost, hostent->h_name);
1118 }
1119
33a2a872 1120#endif /* !HAVE_GETADDRINFO */
9c0f2dac
RS
1121
1122#define CONNECT_ERROR "Could not connect to POP server: "
80b2cbf2 1123
33a2a872 1124 if (! connect_ok)
9c0f2dac 1125 {
de5c39cf 1126 CLOSESOCKET (sock);
e99a530f 1127 snprintf (pop_error, ERROR_MAX, "%s%s", CONNECT_ERROR, strerror (errno));
9c0f2dac 1128 return (-1);
80b2cbf2 1129
9c0f2dac
RS
1130 }
1131
1132#ifdef KERBEROS
33a2a872 1133
9c0f2dac
RS
1134#define KRB_ERROR "Kerberos error connecting to POP server: "
1135 if (! (flags & POP_NO_KERBEROS))
1136 {
a27738b6
RS
1137#ifdef KERBEROS5
1138 if ((rem = krb5_init_context (&kcontext)))
9c0f2dac
RS
1139 {
1140 krb5error:
a27738b6
RS
1141 if (auth_context)
1142 krb5_auth_con_free (kcontext, auth_context);
1143 if (kcontext)
1144 krb5_free_context (kcontext);
e99a530f
PE
1145 snprintf (pop_error, ERROR_MAX, "%s%s",
1146 KRB_ERROR, error_message (rem));
de5c39cf 1147 CLOSESOCKET (sock);
9c0f2dac
RS
1148 return (-1);
1149 }
1150
a27738b6
RS
1151 if ((rem = krb5_auth_con_init (kcontext, &auth_context)))
1152 goto krb5error;
80b2cbf2 1153
a27738b6
RS
1154 if (rem = krb5_cc_default (kcontext, &ccdef))
1155 goto krb5error;
1156
1157 if (rem = krb5_cc_get_principal (kcontext, ccdef, &client))
1158 goto krb5error;
9c0f2dac 1159
33a2a872 1160 for (cp = realhost; *cp; cp++)
9c0f2dac
RS
1161 {
1162 if (isupper (*cp))
1163 {
1164 *cp = tolower (*cp);
1165 }
1166 }
1167
33a2a872 1168 if (rem = krb5_sname_to_principal (kcontext, realhost,
a27738b6
RS
1169 POP_SERVICE, FALSE, &server))
1170 goto krb5error;
9c0f2dac 1171
a27738b6
RS
1172 rem = krb5_sendauth (kcontext, &auth_context,
1173 (krb5_pointer) &sock, "KPOPV1.0", client, server,
9c0f2dac
RS
1174 AP_OPTS_MUTUAL_REQUIRED,
1175 0, /* no checksum */
1176 0, /* no creds, use ccache instead */
1177 ccdef,
9c0f2dac 1178 &err_ret,
a27738b6 1179 0, /* don't need subsession key */
9c0f2dac 1180 0); /* don't need reply */
a27738b6 1181 krb5_free_principal (kcontext, server);
9c0f2dac
RS
1182 if (rem)
1183 {
e99a530f
PE
1184 int pop_error_len = snprintf (pop_error, ERROR_MAX, "%s%s",
1185 KRB_ERROR, error_message (rem));
5fe92a72 1186#if defined HAVE_KRB5_ERROR_TEXT
9c0f2dac
RS
1187 if (err_ret && err_ret->text.length)
1188 {
e99a530f
PE
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);
9c0f2dac 1192 }
5fe92a72 1193#elif defined HAVE_KRB5_ERROR_E_TEXT
e99a530f 1194 if (err_ret && err_ret->e_text && **err_ret->e_text)
b2e37dad 1195 snprintf (pop_error + pop_error_len, ERROR_MAX - pop_error_len,
e99a530f 1196 " [server says '%s']", *err_ret->e_text);
5fe92a72 1197#endif
9c0f2dac 1198 if (err_ret)
a27738b6
RS
1199 krb5_free_error (kcontext, err_ret);
1200 krb5_auth_con_free (kcontext, auth_context);
1201 krb5_free_context (kcontext);
9c0f2dac 1202
de5c39cf 1203 CLOSESOCKET (sock);
9c0f2dac
RS
1204 return (-1);
1205 }
80b2cbf2 1206#else /* ! KERBEROS5 */
9c0f2dac 1207 ticket = (KTEXT) malloc (sizeof (KTEXT_ST));
51a66525
CH
1208 rem = krb_sendauth (0L, sock, ticket, "pop", realhost,
1209 (char *) krb_realmofhost (realhost),
9c0f2dac
RS
1210 (unsigned long) 0, &msg_data, &cred, schedule,
1211 (struct sockaddr_in *) 0,
1212 (struct sockaddr_in *) 0,
1213 "KPOPV0.1");
1214 free ((char *) ticket);
1215 if (rem != KSUCCESS)
1216 {
e99a530f 1217 snprintf (pop_error, ERROR_MAX, "%s%s", KRB_ERROR, krb_err_txt[rem]);
de5c39cf 1218 CLOSESOCKET (sock);
9c0f2dac
RS
1219 return (-1);
1220 }
a27738b6 1221#endif /* KERBEROS5 */
9c0f2dac
RS
1222 }
1223#endif /* KERBEROS */
1224
1225 return (sock);
1226} /* socket_connection */
1227
1228/*
60b0a379 1229 * Function: pop_getline
9c0f2dac
RS
1230 *
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.
1235 *
1236 * Arguments:
1237 * server The server from which to get the line of text.
1238 *
d89d0243
KH
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.
9c0f2dac 1244 *
60b0a379 1245 * Notes: The line returned is overwritten with each call to pop_getline.
9c0f2dac
RS
1246 *
1247 * Side effects: Closes the connection on error.
d89d0243
KH
1248 *
1249 * THE RETURNED LINE MAY CONTAIN EMBEDDED NULLS!
9c0f2dac 1250 */
d89d0243 1251static int
873fbd0b 1252pop_getline (popserver server, char **line)
9c0f2dac
RS
1253{
1254#define GETLINE_ERROR "Error reading from server: "
1255
1256 int ret;
b6606ad8
RS
1257 int search_offset = 0;
1258
9c0f2dac
RS
1259 if (server->data)
1260 {
d89d0243
KH
1261 char *cp = find_crlf (server->buffer + server->buffer_index,
1262 server->data);
9c0f2dac
RS
1263 if (cp)
1264 {
1265 int found;
1266 int data_used;
1267
1268 found = server->buffer_index;
1269 data_used = (cp + 2) - server->buffer - found;
80b2cbf2 1270
9c0f2dac
RS
1271 *cp = '\0'; /* terminate the string to be returned */
1272 server->data -= data_used;
1273 server->buffer_index += data_used;
1274
1275 if (pop_debug)
d89d0243
KH
1276 /* Embedded nulls will truncate this output prematurely,
1277 but that's OK because it's just for debugging anyway. */
9c0f2dac 1278 fprintf (stderr, "<<< %s\n", server->buffer + found);
d89d0243
KH
1279 *line = server->buffer + found;
1280 return (data_used - 2);
9c0f2dac
RS
1281 }
1282 else
1283 {
72af86bd
AS
1284 memmove (server->buffer, server->buffer + server->buffer_index,
1285 server->data);
b6606ad8
RS
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;
9c0f2dac
RS
1294 server->buffer_index = 0;
1295 }
1296 }
1297 else
1298 {
1299 server->buffer_index = 0;
1300 }
1301
1302 while (1)
1303 {
bbcac09c
RS
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)
9c0f2dac 1308 {
bbcac09c
RS
1309 server->buffer_size += GETLINE_INCR;
1310 server->buffer = (char *)realloc (server->buffer, server->buffer_size);
1311 if (! server->buffer)
9c0f2dac 1312 {
bbcac09c
RS
1313 strcpy (pop_error, "Out of memory in pop_getline");
1314 pop_trash (server);
1315 return (-1);
0ea95737 1316 }
0ea95737 1317 }
bbcac09c
RS
1318 ret = RECV (server->file, server->buffer + server->data,
1319 server->buffer_size - server->data - 1, 0);
9c0f2dac
RS
1320 if (ret < 0)
1321 {
e99a530f
PE
1322 snprintf (pop_error, ERROR_MAX, "%s%s",
1323 GETLINE_ERROR, strerror (errno));
9c0f2dac 1324 pop_trash (server);
d89d0243 1325 return (-1);
9c0f2dac
RS
1326 }
1327 else if (ret == 0)
1328 {
60b0a379 1329 strcpy (pop_error, "Unexpected EOF from server in pop_getline");
9c0f2dac 1330 pop_trash (server);
d89d0243 1331 return (-1);
9c0f2dac
RS
1332 }
1333 else
1334 {
7a481e50 1335 char *cp;
9c0f2dac 1336 server->data += ret;
7a481e50 1337 server->buffer[server->data] = '\0';
80b2cbf2 1338
d89d0243
KH
1339 cp = find_crlf (server->buffer + search_offset,
1340 server->data - search_offset);
9c0f2dac
RS
1341 if (cp)
1342 {
1343 int data_used = (cp + 2) - server->buffer;
1344 *cp = '\0';
1345 server->data -= data_used;
1346 server->buffer_index = data_used;
1347
1348 if (pop_debug)
1349 fprintf (stderr, "<<< %s\n", server->buffer);
d89d0243
KH
1350 *line = server->buffer;
1351 return (data_used - 2);
9c0f2dac 1352 }
a8f44d07
RS
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;
9c0f2dac
RS
1356 }
1357 }
1358
1359 /* NOTREACHED */
1360}
1361
1362/*
1363 * Function: sendline
1364 *
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.
1369 *
1370 * Arguments:
1371 * server The server to which to send the text.
1372 * line The line of text to send.
1373 *
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.
1377 *
1378 * Side effects: Closes the connection on error.
1379 */
1380static int
988e88ab 1381sendline (popserver server, const char *line)
9c0f2dac
RS
1382{
1383#define SENDLINE_ERROR "Error writing to POP server: "
1384 int ret;
ec641d50
KR
1385 char *buf;
1386
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.)
1395
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);
1400 strcpy (buf, line);
1401 strcat (buf, "\r\n");
1402 ret = fullwrite (server->file, buf, strlen (buf));
9c0f2dac
RS
1403
1404 if (ret < 0)
1405 {
1406 pop_trash (server);
e99a530f 1407 snprintf (pop_error, ERROR_MAX, "%s%s", SENDLINE_ERROR, strerror (errno));
9c0f2dac
RS
1408 return (ret);
1409 }
1410
1411 if (pop_debug)
1412 fprintf (stderr, ">>> %s\n", line);
1413
1414 return (0);
1415}
1416
1417/*
1418 * Procedure: fullwrite
1419 *
1420 * Purpose: Just like write, but keeps trying until the entire string
1421 * has been written.
1422 *
1423 * Return value: Same as write. Pop_error is not set.
1424 */
1425static int
873fbd0b 1426fullwrite (int fd, char *buf, int nbytes)
9c0f2dac
RS
1427{
1428 char *cp;
9d3f4cee 1429 int ret = 0;
9c0f2dac
RS
1430
1431 cp = buf;
9d3f4cee 1432 while (nbytes && ((ret = SEND (fd, cp, nbytes, 0)) > 0))
9c0f2dac
RS
1433 {
1434 cp += ret;
1435 nbytes -= ret;
1436 }
1437
1438 return (ret);
1439}
1440
1441/*
1442 * Procedure getok
1443 *
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.
1447 *
1448 * Arguments:
1449 * server The server to read from.
80b2cbf2 1450 *
9c0f2dac
RS
1451 * Returns: 0 for success, else for failure and puts error in pop_error.
1452 *
8e6208c5 1453 * Side effects: On failure, may make the connection unusable.
9c0f2dac
RS
1454 */
1455static int
873fbd0b 1456getok (popserver server)
9c0f2dac
RS
1457{
1458 char *fromline;
1459
60b0a379 1460 if (pop_getline (server, &fromline) < 0)
9c0f2dac
RS
1461 {
1462 return (-1);
1463 }
1464
1465 if (! strncmp (fromline, "+OK", 3))
1466 return (0);
1467 else if (! strncmp (fromline, "-ERR", 4))
1468 {
e99a530f 1469 snprintf (pop_error, ERROR_MAX, "%s", fromline);
9c0f2dac
RS
1470 return (-1);
1471 }
1472 else
1473 {
1474 strcpy (pop_error,
1475 "Unexpected response from server; expecting +OK or -ERR");
1476 pop_trash (server);
1477 return (-1);
1478 }
80b2cbf2 1479}
9c0f2dac
RS
1480
1481#if 0
1482/*
1483 * Function: gettermination
1484 *
1485 * Purpose: Gets the next line and verifies that it is a termination
1486 * line (nothing but a dot).
1487 *
1488 * Return value: 0 on success, non-zero with pop_error set on error.
1489 *
1490 * Side effects: Closes the connection on error.
1491 */
1492static int
1493gettermination (server)
1494 popserver server;
1495{
1496 char *fromserver;
1497
60b0a379 1498 if (pop_getline (server, &fromserver) < 0)
9c0f2dac
RS
1499 return (-1);
1500
1501 if (strcmp (fromserver, "."))
1502 {
1503 strcpy (pop_error,
1504 "Unexpected response from server in gettermination");
1505 pop_trash (server);
1506 return (-1);
1507 }
1508
1509 return (0);
1510}
1511#endif
1512
1513/*
1514 * Function pop_close
1515 *
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
1519 * are received.
1520 *
8e6208c5 1521 * Side effects: The server is unusable after this function returns.
9c0f2dac
RS
1522 * Changes made to the maildrop since the session was started (or
1523 * since the last pop_reset) may be lost.
1524 */
80b2cbf2 1525void
873fbd0b 1526pop_close (popserver server)
9c0f2dac
RS
1527{
1528 pop_trash (server);
1529 free ((char *) server);
1530
1531 return;
1532}
1533
1534/*
1535 * Function: pop_trash
1536 *
1537 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
4d90eee4 1538 * memory associated with the server. It is valid to call
9c0f2dac
RS
1539 * pop_close or pop_quit after this function has been called.
1540 */
1541static void
873fbd0b 1542pop_trash (popserver server)
9c0f2dac
RS
1543{
1544 if (server->file >= 0)
1545 {
c2668a61
RS
1546 /* avoid recursion; sendline can call pop_trash */
1547 if (server->trash_started)
1548 return;
96c06863 1549 server->trash_started = true;
de5c39cf 1550
9c0f2dac
RS
1551 sendline (server, "RSET");
1552 sendline (server, "QUIT");
1553
de5c39cf 1554 CLOSESOCKET (server->file);
9c0f2dac
RS
1555 server->file = -1;
1556 if (server->buffer)
1557 {
1558 free (server->buffer);
1559 server->buffer = 0;
1560 }
1561 }
c2668a61
RS
1562
1563#ifdef WINDOWSNT
1564 if (have_winsock)
1565 WSACleanup ();
1566#endif
9c0f2dac
RS
1567}
1568
d89d0243
KH
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. */
9c0f2dac
RS
1572
1573static char *
873fbd0b 1574find_crlf (char *in_string, int len)
9c0f2dac 1575{
d89d0243 1576 while (len--)
9c0f2dac 1577 {
d89d0243 1578 if (*in_string == '\r')
8148baf9
RS
1579 {
1580 if (*++in_string == '\n')
1581 return (in_string - 1);
1582 }
1583 else
1584 in_string++;
9c0f2dac 1585 }
d89d0243 1586 return (0);
9c0f2dac
RS
1587}
1588
1589#endif /* MAIL_USE_POP */