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