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