Update FSF's address in the preamble.
[bpt/emacs.git] / lib-src / pop.c
CommitLineData
9c0f2dac
RS
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.
4
5This file is part of GNU Emacs.
6
7GNU Emacs is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
19the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
9c0f2dac 21
8148baf9
RS
22#ifdef HAVE_CONFIG_H
23#define NO_SHORTNAMES /* Tell config not to load remap.h */
c9c3d8f6 24#include <../src/config.h>
8148baf9
RS
25#else
26#define MAIL_USE_POP
27#endif
c9c3d8f6 28
8148baf9
RS
29#ifdef MAIL_USE_POP
30
31#ifdef HAVE_CONFIG_H
32/* Cancel these substitutions made in config.h */
33#undef open
6d683104
RS
34#undef read
35#undef write
8148baf9
RS
36#undef close
37#endif
9c0f2dac
RS
38
39#include <sys/types.h>
40#include <netinet/in.h>
41#include <sys/socket.h>
42#include <pop.h>
625ad89f 43
9c0f2dac
RS
44#ifdef sun
45#include <malloc.h>
8148baf9 46#endif /* sun */
625ad89f 47
9c0f2dac
RS
48#ifdef HESIOD
49#include <hesiod.h>
50/*
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
54 * hesiod.h does.
55 */
56extern struct servent *hes_getservbyname (/* char *, char * */);
57#endif
625ad89f 58
9c0f2dac 59#include <pwd.h>
9c0f2dac
RS
60#include <netdb.h>
61#include <errno.h>
62#include <stdio.h>
625ad89f 63
9c0f2dac
RS
64#ifdef KERBEROS
65#ifndef KRB5
9c0f2dac 66#include <des.h>
7a481e50 67#include <krb.h>
9c0f2dac
RS
68#else /* KRB5 */
69#include <krb5/krb5.h>
70#include <krb5/ext-proto.h>
71#include <ctype.h>
72#endif /* KRB5 */
8148baf9 73#endif /* KERBEROS */
9c0f2dac 74
8148baf9
RS
75extern char *getenv (/* char * */);
76extern char *getlogin (/* void */);
77extern char *getpass (/* char * */);
78extern char *strerror (/* int */);
79
80#ifdef KERBEROS
81#ifndef KRB5
9c0f2dac 82extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *,
8148baf9
RS
83 u_long, MSG_DAT *, CREDENTIALS *, Key_schedule,
84 struct sockaddr_in *, struct sockaddr_in *,
85 char * */);
9c0f2dac 86extern char *krb_realmofhost (/* char * */);
8148baf9
RS
87#endif /* ! KRB5 */
88#endif /* KERBEROS */
9c0f2dac 89
8148baf9 90#if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H)
9c0f2dac
RS
91extern int h_errno;
92#endif
93
94static int socket_connection (/* char *, int */);
95static char *getline (/* popserver */);
96static int sendline (/* popserver, char * */);
97static int fullwrite (/* int, char *, int */);
98static int getok (/* popserver */);
99#if 0
100static int gettermination (/* popserver */);
101#endif
102static void pop_trash (/* popserver */);
8148baf9 103static char *find_crlf (/* char * */);
7a481e50 104
9c0f2dac
RS
105#define ERROR_MAX 80 /* a pretty arbitrary size */
106#define POP_PORT 110
107#define KPOP_PORT 1109
108#define POP_SERVICE "pop"
109#ifdef KERBEROS
110#ifdef KRB5
111#define KPOP_SERVICE "k5pop";
112#else
113#define KPOP_SERVICE "kpop"
114#endif
115#endif
116
117char pop_error[ERROR_MAX];
118int pop_debug = 0;
119
120#ifndef min
121#define min(a,b) (((a) < (b)) ? (a) : (b))
122#endif
123
124/*
125 * Function: pop_open (char *host, char *username, char *password,
126 * int flags)
127 *
128 * Purpose: Establishes a connection with a post-office server, and
129 * completes the authorization portion of the session.
130 *
131 * Arguments:
132 * host The server host with which the connection should be
133 * established. Optional. If omitted, internal
134 * heuristics will be used to determine the server host,
135 * if possible.
136 * username
137 * The username of the mail-drop to access. Optional.
138 * If omitted, internal heuristics will be used to
139 * determine the username, if possible.
140 * password
141 * The password to use for authorization. If omitted,
142 * internal heuristics will be used to determine the
143 * password, if possible.
144 * flags A bit mask containing flags controlling certain
145 * functions of the routine. Valid flags are defined in
146 * the file pop.h
147 *
148 * Return value: Upon successful establishment of a connection, a
149 * non-null popserver will be returned. Otherwise, null will be
150 * returned, and the string variable pop_error will contain an
151 * explanation of the error.
152 */
153popserver
154pop_open (host, username, password, flags)
155 char *host;
156 char *username;
157 char *password;
158 int flags;
159{
160 int sock;
161 popserver server;
162
163 /* Determine the user name */
164 if (! username)
165 {
166 username = getenv ("USER");
167 if (! (username && *username))
168 {
169 username = getlogin ();
170 if (! (username && *username))
171 {
172 struct passwd *passwd;
173 passwd = getpwuid (getuid ());
174 if (passwd && passwd->pw_name && *passwd->pw_name)
175 {
176 username = passwd->pw_name;
177 }
178 else
179 {
180 strcpy (pop_error, "Could not determine username");
181 return (0);
182 }
183 }
184 }
185 }
186
187 /*
188 * Determine the mail host.
189 */
190
191 if (! host)
192 {
193 host = getenv ("MAILHOST");
194 }
195
196#ifdef HESIOD
197 if ((! host) && (! (flags & POP_NO_HESIOD)))
198 {
199 struct hes_postoffice *office;
200 office = hes_getmailhost (username);
201 if (office && office->po_type && (! strcmp (office->po_type, "POP"))
202 && office->po_name && *office->po_name && office->po_host
203 && *office->po_host)
204 {
205 host = office->po_host;
206 username = office->po_name;
207 }
208 }
209#endif
210
211#ifdef MAILHOST
212 if (! host)
213 {
214 host = MAILHOST;
215 }
216#endif
217
218 if (! host)
219 {
220 strcpy (pop_error, "Could not determine POP server");
221 return (0);
222 }
223
224 /* Determine the password */
225#ifdef KERBEROS
226#define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS))
227#else
228#define DONT_NEED_PASSWORD 0
229#endif
230
231 if ((! password) && (! DONT_NEED_PASSWORD))
232 {
233 if (! (flags & POP_NO_GETPASS))
234 {
235 password = getpass ("Enter POP password:");
236 }
237 if (! password)
238 {
239 strcpy (pop_error, "Could not determine POP password");
240 return (0);
241 }
242 }
243 if (password)
244 flags |= POP_NO_KERBEROS;
245 else
246 password = username;
247
248 sock = socket_connection (host, flags);
249 if (sock == -1)
250 return (0);
251
252 server = (popserver) malloc (sizeof (struct _popserver));
253 if (! server)
254 {
255 strcpy (pop_error, "Out of memory in pop_open");
256 return (0);
257 }
258 server->buffer = (char *) malloc (GETLINE_MIN);
259 if (! server->buffer)
260 {
261 strcpy (pop_error, "Out of memory in pop_open");
262 free ((char *) server);
263 return (0);
264 }
265
266 server->file = sock;
267 server->data = 0;
268 server->buffer_index = 0;
269 server->buffer_size = GETLINE_MIN;
270 server->in_multi = 0;
271
272 if (getok (server))
273 return (0);
274
275 /*
276 * I really shouldn't use the pop_error variable like this, but....
277 */
278 if (strlen (username) > ERROR_MAX - 6)
279 {
280 pop_close (server);
281 strcpy (pop_error,
282 "Username too long; recompile pop.c with larger ERROR_MAX");
283 return (0);
284 }
285 sprintf (pop_error, "USER %s", username);
286
287 if (sendline (server, pop_error) || getok (server))
288 {
289 return (0);
290 }
291
292 if (strlen (password) > ERROR_MAX - 6)
293 {
294 pop_close (server);
295 strcpy (pop_error,
296 "Password too long; recompile pop.c with larger ERROR_MAX");
297 return (0);
298 }
299 sprintf (pop_error, "PASS %s", password);
300
301 if (sendline (server, pop_error) || getok (server))
302 {
303 return (0);
304 }
305
306 return (server);
307}
308
309/*
310 * Function: pop_stat
311 *
312 * Purpose: Issue the STAT command to the server and return (in the
313 * value parameters) the number of messages in the maildrop and
314 * the total size of the maildrop.
315 *
316 * Return value: 0 on success, or non-zero with an error in pop_error
317 * in failure.
318 *
319 * Side effects: On failure, may make further operations on the
320 * connection impossible.
321 */
322int
323pop_stat (server, count, size)
324 popserver server;
325 int *count;
326 int *size;
327{
328 char *fromserver;
329
330 if (server->in_multi)
331 {
332 strcpy (pop_error, "In multi-line query in pop_stat");
333 return (-1);
334 }
335
336 if (sendline (server, "STAT") || (! (fromserver = getline (server))))
337 return (-1);
338
339 if (strncmp (fromserver, "+OK ", 4))
340 {
341 if (0 == strncmp (fromserver, "-ERR", 4))
342 {
343 strncpy (pop_error, fromserver, ERROR_MAX);
344 }
345 else
346 {
347 strcpy (pop_error,
348 "Unexpected response from POP server in pop_stat");
349 pop_trash (server);
350 }
351 return (-1);
352 }
353
354 *count = atoi (&fromserver[4]);
355
356 fromserver = index (&fromserver[4], ' ');
357 if (! fromserver)
358 {
359 strcpy (pop_error,
360 "Badly formatted response from server in pop_stat");
361 pop_trash (server);
362 return (-1);
363 }
364
365 *size = atoi (fromserver + 1);
366
367 return (0);
368}
369
370/*
371 * Function: pop_list
372 *
373 * Purpose: Performs the POP "list" command and returns (in value
374 * parameters) two malloc'd zero-terminated arrays -- one of
375 * message IDs, and a parallel one of sizes.
376 *
377 * Arguments:
378 * server The pop connection to talk to.
379 * message The number of the one message about which to get
380 * information, or 0 to get information about all
381 * messages.
382 *
383 * Return value: 0 on success, non-zero with error in pop_error on
384 * failure.
385 *
386 * Side effects: On failure, may make further operations on the
387 * connection impossible.
388 */
389int
390pop_list (server, message, IDs, sizes)
391 popserver server;
392 int message;
393 int **IDs;
394 int **sizes;
395{
396 int how_many, i;
397 char *fromserver;
398
399 if (server->in_multi)
400 {
401 strcpy (pop_error, "In multi-line query in pop_list");
402 return (-1);
403 }
404
405 if (message)
406 how_many = 1;
407 else
408 {
409 int count, size;
410 if (pop_stat (server, &count, &size))
411 return (-1);
412 how_many = count;
413 }
414
415 *IDs = (int *) malloc ((how_many + 1) * sizeof (int));
416 *sizes = (int *) malloc ((how_many + 1) * sizeof (int));
417 if (! (*IDs && *sizes))
418 {
419 strcpy (pop_error, "Out of memory in pop_list");
420 return (-1);
421 }
422
423 if (message)
424 {
425 sprintf (pop_error, "LIST %d", message);
426 if (sendline (server, pop_error))
427 {
428 free ((char *) *IDs);
429 free ((char *) *sizes);
430 return (-1);
431 }
432 if (! (fromserver = getline (server)))
433 {
434 free ((char *) *IDs);
435 free ((char *) *sizes);
436 return (-1);
437 }
438 if (strncmp (fromserver, "+OK ", 4))
439 {
440 if (! strncmp (fromserver, "-ERR", 4))
441 strncpy (pop_error, fromserver, ERROR_MAX);
442 else
443 {
444 strcpy (pop_error,
445 "Unexpected response from server in pop_list");
446 pop_trash (server);
447 }
448 free ((char *) *IDs);
449 free ((char *) *sizes);
450 return (-1);
451 }
452 (*IDs)[0] = atoi (&fromserver[4]);
453 fromserver = index (&fromserver[4], ' ');
454 if (! fromserver)
455 {
456 strcpy (pop_error,
457 "Badly formatted response from server in pop_list");
458 pop_trash (server);
459 free ((char *) *IDs);
460 free ((char *) *sizes);
461 return (-1);
462 }
463 (*sizes)[0] = atoi (fromserver);
464 (*IDs)[1] = (*sizes)[1] = 0;
465 return (0);
466 }
467 else
468 {
469 if (pop_multi_first (server, "LIST", &fromserver))
470 {
471 free ((char *) *IDs);
472 free ((char *) *sizes);
473 return (-1);
474 }
475 for (i = 0; i < how_many; i++)
476 {
477 if (pop_multi_next (server, &fromserver))
478 {
479 free ((char *) *IDs);
480 free ((char *) *sizes);
481 return (-1);
482 }
483 (*IDs)[i] = atoi (fromserver);
484 fromserver = index (fromserver, ' ');
485 if (! fromserver)
486 {
487 strcpy (pop_error,
488 "Badly formatted response from server in pop_list");
489 free ((char *) *IDs);
490 free ((char *) *sizes);
491 pop_trash (server);
492 return (-1);
493 }
494 (*sizes)[i] = atoi (fromserver);
495 }
496 if (pop_multi_next (server, &fromserver))
497 {
498 free ((char *) *IDs);
499 free ((char *) *sizes);
500 return (-1);
501 }
502 else if (fromserver)
503 {
504 strcpy (pop_error,
505 "Too many response lines from server in pop_list");
506 free ((char *) *IDs);
507 free ((char *) *sizes);
508 return (-1);
509 }
510 (*IDs)[i] = (*sizes)[i] = 0;
511 return (0);
512 }
513}
514
515/*
516 * Function: pop_retrieve
517 *
518 * Purpose: Retrieve a specified message from the maildrop.
519 *
520 * Arguments:
521 * server The server to retrieve from.
522 * message The message number to retrieve.
523 * markfrom
524 * If true, then mark the string "From " at the beginning
525 * of lines with '>'.
526 *
527 * Return value: A string pointing to the message, if successful, or
528 * null with pop_error set if not.
529 *
530 * Side effects: May kill connection on error.
531 */
532char *
533pop_retrieve (server, message, markfrom)
534 popserver server;
535 int message;
536 int markfrom;
537{
538 int *IDs, *sizes, bufsize, fromcount = 0, cp = 0;
539 char *ptr, *fromserver;
540 int ret;
541
542 if (server->in_multi)
543 {
544 strcpy (pop_error, "In multi-line query in pop_retrieve");
545 return (0);
546 }
547
548 if (pop_list (server, message, &IDs, &sizes))
549 return (0);
550
551 if (pop_retrieve_first (server, message, &fromserver))
552 {
553 return (0);
554 }
555
556 /*
557 * The "5" below is an arbitrary constant -- I assume that if
558 * there are "From" lines in the text to be marked, there
559 * probably won't be more than 5 of them. If there are, I
560 * allocate more space for them below.
561 */
562 bufsize = sizes[0] + (markfrom ? 5 : 0);
563 ptr = malloc (bufsize);
564 free ((char *) IDs);
565 free ((char *) sizes);
566
567 if (! ptr)
568 {
569 strcpy (pop_error, "Out of memory in pop_retrieve");
570 pop_retrieve_flush (server);
571 return (0);
572 }
573
574 while (! (ret = pop_retrieve_next (server, &fromserver)))
575 {
576 int linesize;
577
578 if (! fromserver)
579 {
580 ptr[cp] = '\0';
581 return (ptr);
582 }
583 if (markfrom && fromserver[0] == 'F' && fromserver[1] == 'r' &&
584 fromserver[2] == 'o' && fromserver[3] == 'm' &&
585 fromserver[4] == ' ')
586 {
587 if (++fromcount == 5)
588 {
589 bufsize += 5;
590 ptr = realloc (ptr, bufsize);
591 if (! ptr)
592 {
593 strcpy (pop_error, "Out of memory in pop_retrieve");
594 pop_retrieve_flush (server);
595 return (0);
596 }
597 fromcount = 0;
598 }
599 ptr[cp++] = '>';
600 }
601 linesize = strlen (fromserver);
602 bcopy (fromserver, &ptr[cp], linesize);
603 cp += linesize;
604 ptr[cp++] = '\n';
605 }
606
607 if (ret)
608 {
609 free (ptr);
610 return (0);
611 }
612}
613
614int
615pop_retrieve_first (server, message, response)
616 popserver server;
617 int message;
618 char **response;
619{
620 sprintf (pop_error, "RETR %d", message);
621 return (pop_multi_first (server, pop_error, response));
622}
623
624int
625pop_retrieve_next (server, line)
626 popserver server;
627 char **line;
628{
629 return (pop_multi_next (server, line));
630}
631
632int
633pop_retrieve_flush (server)
634 popserver server;
635{
636 return (pop_multi_flush (server));
637}
638
639int
640pop_top_first (server, message, lines, response)
641 popserver server;
642 int message, lines;
643 char **response;
644{
645 sprintf (pop_error, "TOP %d %d", message, lines);
646 return (pop_multi_first (server, pop_error, response));
647}
648
649int
650pop_top_next (server, line)
651 popserver server;
652 char **line;
653{
654 return (pop_multi_next (server, line));
655}
656
657int
658pop_top_flush (server)
659 popserver server;
660{
661 return (pop_multi_flush (server));
662}
663
664int
665pop_multi_first (server, command, response)
666 popserver server;
667 char *command;
668 char **response;
669{
670 if (server->in_multi)
671 {
672 strcpy (pop_error,
673 "Already in multi-line query in pop_multi_first");
674 return (-1);
675 }
676
677 if (sendline (server, command) || (! (*response = getline (server))))
678 {
679 return (-1);
680 }
681
682 if (0 == strncmp (*response, "-ERR", 4))
683 {
684 strncpy (pop_error, *response, ERROR_MAX);
685 return (-1);
686 }
687 else if (0 == strncmp (*response, "+OK", 3))
688 {
689 for (*response += 3; **response == ' '; (*response)++) /* empty */;
690 server->in_multi = 1;
691 return (0);
692 }
693 else
694 {
695 strcpy (pop_error,
696 "Unexpected response from server in pop_multi_first");
697 return (-1);
698 }
699}
700
701int
702pop_multi_next (server, line)
703 popserver server;
704 char **line;
705{
706 char *fromserver;
707
708 if (! server->in_multi)
709 {
710 strcpy (pop_error, "Not in multi-line query in pop_multi_next");
711 return (-1);
712 }
713
714 fromserver = getline (server);
715 if (! fromserver)
716 {
717 return (-1);
718 }
719
720 if (fromserver[0] == '.')
721 {
722 if (! fromserver[1])
723 {
724 *line = 0;
725 server->in_multi = 0;
726 return (0);
727 }
728 else
729 {
730 *line = fromserver + 1;
731 return (0);
732 }
733 }
734 else
735 {
736 *line = fromserver;
737 return (0);
738 }
739}
740
741int
742pop_multi_flush (server)
743 popserver server;
744{
745 char *line;
746
747 if (! server->in_multi)
748 {
749 return (0);
750 }
751
752 while (! pop_multi_next (server, &line))
753 {
754 if (! line)
755 {
756 return (0);
757 }
758 }
759
760 return (-1);
761}
762
763/* Function: pop_delete
764 *
765 * Purpose: Delete a specified message.
766 *
767 * Arguments:
768 * server Server from which to delete the message.
769 * message Message to delete.
770 *
771 * Return value: 0 on success, non-zero with error in pop_error
772 * otherwise.
773 */
774int
775pop_delete (server, message)
776 popserver server;
777 int message;
778{
779 if (server->in_multi)
780 {
781 strcpy (pop_error, "In multi-line query in pop_delete");
782 return (-1);
783 }
784
785 sprintf (pop_error, "DELE %d", message);
786
787 if (sendline (server, pop_error) || getok (server))
788 return (-1);
789
790 return (0);
791}
792
793/*
794 * Function: pop_noop
795 *
796 * Purpose: Send a noop command to the server.
797 *
798 * Argument:
799 * server The server to send to.
800 *
801 * Return value: 0 on success, non-zero with error in pop_error
802 * otherwise.
803 *
804 * Side effects: Closes connection on error.
805 */
806int
807pop_noop (server)
808 popserver server;
809{
810 if (server->in_multi)
811 {
812 strcpy (pop_error, "In multi-line query in pop_noop");
813 return (-1);
814 }
815
816 if (sendline (server, "NOOP") || getok (server))
817 return (-1);
818
819 return (0);
820}
821
822/*
823 * Function: pop_last
824 *
825 * Purpose: Find out the highest seen message from the server.
826 *
827 * Arguments:
828 * server The server.
829 *
830 * Return value: If successful, the highest seen message, which is
831 * greater than or equal to 0. Otherwise, a negative number with
832 * the error explained in pop_error.
833 *
834 * Side effects: Closes the connection on error.
835 */
836int
837pop_last (server)
838 popserver server;
839{
840 char *fromserver;
841
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
851 if (! (fromserver = getline (server)))
852 return (-1);
853
854 if (! strncmp (fromserver, "-ERR", 4))
855 {
856 strncpy (pop_error, fromserver, ERROR_MAX);
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 {
867 return (atoi (&fromserver[4]));
868 }
869}
870
871/*
872 * Function: pop_reset
873 *
874 * Purpose: Reset the server to its initial connect state
875 *
876 * Arguments:
877 * server The server.
878 *
879 * Return value: 0 for success, non-0 with error in pop_error
880 * otherwise.
881 *
882 * Side effects: Closes the connection on error.
883 */
884int
885pop_reset (server)
886 popserver server;
887{
888 if (pop_retrieve_flush (server))
889 {
890 return (-1);
891 }
892
893 if (sendline (server, "RSET") || getok (server))
894 return (-1);
895
896 return (0);
897}
898
899/*
900 * Function: pop_quit
901 *
902 * Purpose: Quit the connection to the server,
903 *
904 * Arguments:
905 * server The server to quit.
906 *
907 * Return value: 0 for success, non-zero otherwise with error in
908 * pop_error.
909 *
8e6208c5 910 * Side Effects: The popserver passed in is unusable after this
9c0f2dac
RS
911 * function is called, even if an error occurs.
912 */
913int
914pop_quit (server)
915 popserver server;
916{
917 int ret = 0;
918
919 if (server->file >= 0)
920 {
921 if (pop_retrieve_flush (server))
922 {
923 ret = -1;
924 }
925
926 if (sendline (server, "QUIT") || getok (server))
927 {
928 ret = -1;
929 }
930
931 close (server->file);
932 }
933
934 if (server->buffer)
935 free (server->buffer);
936 free ((char *) server);
937
938 return (ret);
939}
940
941/*
942 * Function: socket_connection
943 *
944 * Purpose: Opens the network connection with the mail host, without
945 * doing any sort of I/O with it or anything.
946 *
947 * Arguments:
948 * host The host to which to connect.
949 * flags Option flags.
950 *
951 * Return value: A file descriptor indicating the connection, or -1
952 * indicating failure, in which case an error has been copied
953 * into pop_error.
954 */
955static int
956socket_connection (host, flags)
957 char *host;
958 int flags;
959{
960 struct hostent *hostent;
961 struct servent *servent;
962 struct sockaddr_in addr;
963 char found_port = 0;
964 char *service;
965 int sock;
966#ifdef KERBEROS
967#ifdef KRB5
968 krb5_error_code rem;
969 krb5_ccache ccdef;
970 krb5_principal client, server;
971 krb5_error *err_ret;
972 register char *cp;
973#else
974 KTEXT ticket;
975 MSG_DAT msg_data;
976 CREDENTIALS cred;
977 Key_schedule schedule;
978 int rem;
979#endif /* KRB5 */
980#endif /* KERBEROS */
981
982 int try_count = 0;
983
984 do
985 {
986 hostent = gethostbyname (host);
987 try_count++;
988 if ((! hostent) && ((h_errno != TRY_AGAIN) || (try_count == 5)))
989 {
990 strcpy (pop_error, "Could not determine POP server's address");
991 return (-1);
992 }
993 } while (! hostent);
994
995 bzero ((char *) &addr, sizeof (addr));
996 addr.sin_family = AF_INET;
997
998#ifdef KERBEROS
999 service = (flags & POP_NO_KERBEROS) ? POP_SERVICE : KPOP_SERVICE;
1000#else
1001 service = POP_SERVICE;
1002#endif
1003
1004#ifdef HESIOD
1005 if (! (flags & POP_NO_HESIOD))
1006 {
1007 servent = hes_getservbyname (service, "tcp");
1008 if (servent)
1009 {
1010 addr.sin_port = servent->s_port;
1011 found_port = 1;
1012 }
1013 }
1014#endif
1015 if (! found_port)
1016 {
1017 servent = getservbyname (service, "tcp");
1018 if (servent)
1019 {
1020 addr.sin_port = servent->s_port;
1021 }
1022 else
1023 {
1024#ifdef KERBEROS
1025 addr.sin_port = htons ((flags & POP_NO_KERBEROS) ?
1026 POP_PORT : KPOP_PORT);
1027#else
1028 addr.sin_port = htons (POP_PORT);
1029#endif
1030 }
1031 }
1032
1033#define SOCKET_ERROR "Could not create socket for POP connection: "
1034
1035 sock = socket (PF_INET, SOCK_STREAM, 0);
1036 if (sock < 0)
1037 {
1038 strcpy (pop_error, SOCKET_ERROR);
1039 strncat (pop_error, strerror (errno),
1040 ERROR_MAX - sizeof (SOCKET_ERROR));
1041 return (-1);
1042
1043 }
1044
1045 while (*hostent->h_addr_list)
1046 {
1047 bcopy (*hostent->h_addr_list, (char *) &addr.sin_addr,
1048 hostent->h_length);
1049 if (! connect (sock, (struct sockaddr *) &addr, sizeof (addr)))
1050 break;
1051 hostent->h_addr_list++;
1052 }
1053
1054#define CONNECT_ERROR "Could not connect to POP server: "
1055
1056 if (! *hostent->h_addr_list)
1057 {
1058 (void) close (sock);
1059 strcpy (pop_error, CONNECT_ERROR);
1060 strncat (pop_error, strerror (errno),
1061 ERROR_MAX - sizeof (CONNECT_ERROR));
1062 return (-1);
1063
1064 }
1065
1066#ifdef KERBEROS
1067#define KRB_ERROR "Kerberos error connecting to POP server: "
1068 if (! (flags & POP_NO_KERBEROS))
1069 {
1070#ifdef KRB5
1071 krb5_init_ets ();
1072
1073 if (rem = krb5_cc_default (&ccdef))
1074 {
1075 krb5error:
1076 strcpy (pop_error, KRB_ERROR);
1077 strncat (pop_error, error_message (rem),
1078 ERROR_MAX - sizeof(KRB_ERROR));
1079 (void) close (sock);
1080 return (-1);
1081 }
1082
1083 if (rem = krb5_cc_get_principal (ccdef, &client))
1084 {
1085 goto krb5error;
1086 }
1087
1088 for (cp = hostent->h_name; *cp; cp++)
1089 {
1090 if (isupper (*cp))
1091 {
1092 *cp = tolower (*cp);
1093 }
1094 }
1095
1096 if (rem = krb5_sname_to_principal (hostent->h_name, POP_SERVICE,
1097 FALSE, &server))
1098 {
1099 goto krb5error;
1100 }
1101
1102 rem = krb5_sendauth ((krb5_pointer) &sock, "KPOPV1.0", client, server,
1103 AP_OPTS_MUTUAL_REQUIRED,
1104 0, /* no checksum */
1105 0, /* no creds, use ccache instead */
1106 ccdef,
1107 0, /* don't need seq # */
1108 0, /* don't need subsession key */
1109 &err_ret,
1110 0); /* don't need reply */
1111 krb5_free_principal (server);
1112 if (rem)
1113 {
1114 if (err_ret && err_ret->text.length)
1115 {
1116 strcpy (pop_error, KRB_ERROR);
1117 strncat (pop_error, error_message (rem),
1118 ERROR_MAX - sizeof (KRB_ERROR));
1119 strncat (pop_error, " [server says '",
1120 ERROR_MAX - strlen (pop_error) - 1);
1121 strncat (pop_error, err_ret->text.data,
1122 min (ERROR_MAX - strlen (pop_error) - 1,
1123 err_ret->text.length));
1124 strncat (pop_error, "']",
1125 ERROR_MAX - strlen (pop_error) - 1);
1126 }
1127 else
1128 {
1129 strcpy (pop_error, KRB_ERROR);
1130 strncat (pop_error, error_message (rem),
1131 ERROR_MAX - sizeof (KRB_ERROR));
1132 }
1133 if (err_ret)
1134 krb5_free_error (err_ret);
1135
1136 (void) close (sock);
1137 return (-1);
1138 }
1139#else /* ! KRB5 */
1140 ticket = (KTEXT) malloc (sizeof (KTEXT_ST));
1141 rem = krb_sendauth (0L, sock, ticket, "pop", hostent->h_name,
1142 (char *) krb_realmofhost (hostent->h_name),
1143 (unsigned long) 0, &msg_data, &cred, schedule,
1144 (struct sockaddr_in *) 0,
1145 (struct sockaddr_in *) 0,
1146 "KPOPV0.1");
1147 free ((char *) ticket);
1148 if (rem != KSUCCESS)
1149 {
1150 strcpy (pop_error, KRB_ERROR);
1151 strncat (pop_error, krb_err_txt[rem],
1152 ERROR_MAX - sizeof (KRB_ERROR));
1153 (void) close (sock);
1154 return (-1);
1155 }
1156#endif /* KRB5 */
1157 }
1158#endif /* KERBEROS */
1159
1160 return (sock);
1161} /* socket_connection */
1162
1163/*
1164 * Function: getline
1165 *
1166 * Purpose: Get a line of text from the connection and return a
1167 * pointer to it. The carriage return and linefeed at the end of
1168 * the line are stripped, but periods at the beginnings of lines
1169 * are NOT dealt with in any special way.
1170 *
1171 * Arguments:
1172 * server The server from which to get the line of text.
1173 *
1174 * Returns: A non-null pointer if successful, or a null pointer on any
1175 * error, with an error message copied into pop_error.
1176 *
1177 * Notes: The line returned is overwritten with each call to getline.
1178 *
1179 * Side effects: Closes the connection on error.
1180 */
1181static char *
1182getline (server)
1183 popserver server;
1184{
1185#define GETLINE_ERROR "Error reading from server: "
1186
1187 int ret;
b6606ad8
RS
1188 int search_offset = 0;
1189
9c0f2dac
RS
1190 if (server->data)
1191 {
8148baf9 1192 char *cp = find_crlf (server->buffer + server->buffer_index);
9c0f2dac
RS
1193 if (cp)
1194 {
1195 int found;
1196 int data_used;
1197
1198 found = server->buffer_index;
1199 data_used = (cp + 2) - server->buffer - found;
1200
1201 *cp = '\0'; /* terminate the string to be returned */
1202 server->data -= data_used;
1203 server->buffer_index += data_used;
1204
1205 if (pop_debug)
1206 fprintf (stderr, "<<< %s\n", server->buffer + found);
1207 return (server->buffer + found);
1208 }
1209 else
1210 {
1211 bcopy (server->buffer + server->buffer_index,
1212 server->buffer, server->data);
b6606ad8
RS
1213 /* Record the fact that we've searched the data already in
1214 the buffer for a CRLF, so that when we search below, we
1215 don't have to search the same data twice. There's a "-
1216 1" here to account for the fact that the last character
1217 of the data we have may be the CR of a CRLF pair, of
1218 which we haven't read the second half yet, so we may have
1219 to search it again when we read more data. */
1220 search_offset = server->data - 1;
9c0f2dac
RS
1221 server->buffer_index = 0;
1222 }
1223 }
1224 else
1225 {
1226 server->buffer_index = 0;
1227 }
1228
1229 while (1)
1230 {
b6606ad8
RS
1231 /* There's a "- 1" here to leave room for the null that we put
1232 at the end of the read data below. We put the null there so
1233 that find_crlf knows where to stop when we call it. */
1234 if (server->data == server->buffer_size - 1)
9c0f2dac
RS
1235 {
1236 server->buffer_size += GETLINE_INCR;
1237 server->buffer = realloc (server->buffer, server->buffer_size);
1238 if (! server->buffer)
1239 {
1240 strcpy (pop_error, "Out of memory in getline");
1241 pop_trash (server);
1242 return (0);
1243 }
1244 }
1245 ret = read (server->file, server->buffer + server->data,
7a481e50 1246 server->buffer_size - server->data - 1);
9c0f2dac
RS
1247 if (ret < 0)
1248 {
1249 strcpy (pop_error, GETLINE_ERROR);
1250 strncat (pop_error, strerror (errno),
1251 ERROR_MAX - sizeof (GETLINE_ERROR));
1252 pop_trash (server);
1253 return (0);
1254 }
1255 else if (ret == 0)
1256 {
1257 strcpy (pop_error, "Unexpected EOF from server in getline");
1258 pop_trash (server);
1259 return (0);
1260 }
1261 else
1262 {
7a481e50 1263 char *cp;
9c0f2dac 1264 server->data += ret;
7a481e50 1265 server->buffer[server->data] = '\0';
9c0f2dac 1266
b6606ad8 1267 cp = find_crlf (server->buffer + search_offset);
9c0f2dac
RS
1268 if (cp)
1269 {
1270 int data_used = (cp + 2) - server->buffer;
1271 *cp = '\0';
1272 server->data -= data_used;
1273 server->buffer_index = data_used;
1274
1275 if (pop_debug)
1276 fprintf (stderr, "<<< %s\n", server->buffer);
1277 return (server->buffer);
1278 }
b6606ad8 1279 search_offset += ret;
9c0f2dac
RS
1280 }
1281 }
1282
1283 /* NOTREACHED */
1284}
1285
1286/*
1287 * Function: sendline
1288 *
1289 * Purpose: Sends a line of text to the POP server. The line of text
1290 * passed into this function should NOT have the carriage return
1291 * and linefeed on the end of it. Periods at beginnings of lines
1292 * will NOT be treated specially by this function.
1293 *
1294 * Arguments:
1295 * server The server to which to send the text.
1296 * line The line of text to send.
1297 *
1298 * Return value: Upon successful completion, a value of 0 will be
1299 * returned. Otherwise, a non-zero value will be returned, and
1300 * an error will be copied into pop_error.
1301 *
1302 * Side effects: Closes the connection on error.
1303 */
1304static int
1305sendline (server, line)
1306 popserver server;
1307 char *line;
1308{
1309#define SENDLINE_ERROR "Error writing to POP server: "
1310 int ret;
1311
1312 ret = fullwrite (server->file, line, strlen (line));
1313 if (ret >= 0)
1314 { /* 0 indicates that a blank line was written */
1315 ret = fullwrite (server->file, "\r\n", 2);
1316 }
1317
1318 if (ret < 0)
1319 {
1320 pop_trash (server);
1321 strcpy (pop_error, SENDLINE_ERROR);
1322 strncat (pop_error, strerror (errno),
1323 ERROR_MAX - sizeof (SENDLINE_ERROR));
1324 return (ret);
1325 }
1326
1327 if (pop_debug)
1328 fprintf (stderr, ">>> %s\n", line);
1329
1330 return (0);
1331}
1332
1333/*
1334 * Procedure: fullwrite
1335 *
1336 * Purpose: Just like write, but keeps trying until the entire string
1337 * has been written.
1338 *
1339 * Return value: Same as write. Pop_error is not set.
1340 */
1341static int
1342fullwrite (fd, buf, nbytes)
1343 int fd;
1344 char *buf;
1345 int nbytes;
1346{
1347 char *cp;
1348 int ret;
1349
1350 cp = buf;
1351 while ((ret = write (fd, cp, nbytes)) > 0)
1352 {
1353 cp += ret;
1354 nbytes -= ret;
1355 }
1356
1357 return (ret);
1358}
1359
1360/*
1361 * Procedure getok
1362 *
1363 * Purpose: Reads a line from the server. If the return indicator is
1364 * positive, return with a zero exit status. If not, return with
1365 * a negative exit status.
1366 *
1367 * Arguments:
1368 * server The server to read from.
1369 *
1370 * Returns: 0 for success, else for failure and puts error in pop_error.
1371 *
8e6208c5 1372 * Side effects: On failure, may make the connection unusable.
9c0f2dac
RS
1373 */
1374static int
1375getok (server)
1376 popserver server;
1377{
1378 char *fromline;
1379
1380 if (! (fromline = getline (server)))
1381 {
1382 return (-1);
1383 }
1384
1385 if (! strncmp (fromline, "+OK", 3))
1386 return (0);
1387 else if (! strncmp (fromline, "-ERR", 4))
1388 {
1389 strncpy (pop_error, fromline, ERROR_MAX);
1390 pop_error[ERROR_MAX-1] = '\0';
1391 return (-1);
1392 }
1393 else
1394 {
1395 strcpy (pop_error,
1396 "Unexpected response from server; expecting +OK or -ERR");
1397 pop_trash (server);
1398 return (-1);
1399 }
1400}
1401
1402#if 0
1403/*
1404 * Function: gettermination
1405 *
1406 * Purpose: Gets the next line and verifies that it is a termination
1407 * line (nothing but a dot).
1408 *
1409 * Return value: 0 on success, non-zero with pop_error set on error.
1410 *
1411 * Side effects: Closes the connection on error.
1412 */
1413static int
1414gettermination (server)
1415 popserver server;
1416{
1417 char *fromserver;
1418
1419 fromserver = getline (server);
1420 if (! fromserver)
1421 return (-1);
1422
1423 if (strcmp (fromserver, "."))
1424 {
1425 strcpy (pop_error,
1426 "Unexpected response from server in gettermination");
1427 pop_trash (server);
1428 return (-1);
1429 }
1430
1431 return (0);
1432}
1433#endif
1434
1435/*
1436 * Function pop_close
1437 *
1438 * Purpose: Close a pop connection, sending a "RSET" command to try to
1439 * preserve any changes that were made and a "QUIT" command to
1440 * try to get the server to quit, but ignoring any responses that
1441 * are received.
1442 *
8e6208c5 1443 * Side effects: The server is unusable after this function returns.
9c0f2dac
RS
1444 * Changes made to the maildrop since the session was started (or
1445 * since the last pop_reset) may be lost.
1446 */
1447void
1448pop_close (server)
1449 popserver server;
1450{
1451 pop_trash (server);
1452 free ((char *) server);
1453
1454 return;
1455}
1456
1457/*
1458 * Function: pop_trash
1459 *
1460 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
1461 * memory associated with the server. It is legal to call
1462 * pop_close or pop_quit after this function has been called.
1463 */
1464static void
1465pop_trash (server)
1466 popserver server;
1467{
1468 if (server->file >= 0)
1469 {
1470 sendline (server, "RSET");
1471 sendline (server, "QUIT");
1472
1473 close (server->file);
1474 server->file = -1;
1475 if (server->buffer)
1476 {
1477 free (server->buffer);
1478 server->buffer = 0;
1479 }
1480 }
1481}
1482
8148baf9
RS
1483/* Return a pointer to the first CRLF in IN_STRING,
1484 or 0 if it does not contain one. */
9c0f2dac
RS
1485
1486static char *
8148baf9
RS
1487find_crlf (in_string)
1488 char *in_string;
9c0f2dac 1489{
8148baf9 1490 while (1)
9c0f2dac 1491 {
8148baf9
RS
1492 if (! *in_string)
1493 return (0);
1494 else if (*in_string == '\r')
1495 {
1496 if (*++in_string == '\n')
1497 return (in_string - 1);
1498 }
1499 else
1500 in_string++;
9c0f2dac 1501 }
8148baf9 1502 /* NOTREACHED */
9c0f2dac
RS
1503}
1504
1505#endif /* MAIL_USE_POP */