(x_error_quitter): Add newline to error message.
[emacs.git] / lib-src / pop.c
blob77d7595801bd54513f28629efd7581c2d5d6ac05
1 /* pop.c: client routines for talking to a POP3-protocol post-office server
2 Copyright (c) 1991,1993 Free Software Foundation, Inc.
3 Written by Jonathan Kamens, jik@security.ov.com.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #ifdef HAVE_CONFIG_H
23 #define NO_SHORTNAMES /* Tell config not to load remap.h */
24 #include <../src/config.h>
25 #else
26 #define MAIL_USE_POP
27 #endif
29 #ifdef MAIL_USE_POP
31 #ifdef HAVE_CONFIG_H
32 /* Cancel these substitutions made in config.h */
33 #undef open
34 #undef read
35 #undef write
36 #undef close
37 #endif
39 #include <sys/types.h>
40 #include <netinet/in.h>
41 #include <sys/socket.h>
42 #include <pop.h>
44 #ifdef sun
45 #include <malloc.h>
46 #endif /* sun */
48 #ifdef HESIOD
49 #include <hesiod.h>
51 * It really shouldn't be necessary to put this declaration here, but
52 * the version of hesiod.h that Athena has installed in release 7.2
53 * doesn't declare this function; I don't know if the 7.3 version of
54 * hesiod.h does.
56 extern struct servent *hes_getservbyname (/* char *, char * */);
57 #endif
59 #include <pwd.h>
60 #include <netdb.h>
61 #include <errno.h>
62 #include <stdio.h>
64 #ifdef KERBEROS
65 #ifndef KRB5
66 #include <des.h>
67 #include <krb.h>
68 #else /* KRB5 */
69 #include <krb5/krb5.h>
70 #include <krb5/ext-proto.h>
71 #include <ctype.h>
72 #endif /* KRB5 */
73 #endif /* KERBEROS */
75 extern char *getenv (/* char * */);
76 extern char *getlogin (/* void */);
77 extern char *getpass (/* char * */);
78 extern char *strerror (/* int */);
79 extern char *index ();
81 #ifdef KERBEROS
82 #ifndef KRB5
83 extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *,
84 u_long, MSG_DAT *, CREDENTIALS *, Key_schedule,
85 struct sockaddr_in *, struct sockaddr_in *,
86 char * */);
87 extern char *krb_realmofhost (/* char * */);
88 #endif /* ! KRB5 */
89 #endif /* KERBEROS */
91 #if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H)
92 extern int h_errno;
93 #endif
95 static int socket_connection (/* char *, int */);
96 static char *getline (/* popserver */);
97 static int sendline (/* popserver, char * */);
98 static int fullwrite (/* int, char *, int */);
99 static int getok (/* popserver */);
100 #if 0
101 static int gettermination (/* popserver */);
102 #endif
103 static void pop_trash (/* popserver */);
104 static char *find_crlf (/* char * */);
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
118 char pop_error[ERROR_MAX];
119 int pop_debug = 0;
121 #ifndef min
122 #define min(a,b) (((a) < (b)) ? (a) : (b))
123 #endif
126 * Function: pop_open (char *host, char *username, char *password,
127 * int flags)
129 * Purpose: Establishes a connection with a post-office server, and
130 * completes the authorization portion of the session.
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
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.
154 popserver
155 pop_open (host, username, password, flags)
156 char *host;
157 char *username;
158 char *password;
159 int flags;
161 int sock;
162 popserver server;
164 /* Determine the user name */
165 if (! username)
167 username = getenv ("USER");
168 if (! (username && *username))
170 username = getlogin ();
171 if (! (username && *username))
173 struct passwd *passwd;
174 passwd = getpwuid (getuid ());
175 if (passwd && passwd->pw_name && *passwd->pw_name)
177 username = passwd->pw_name;
179 else
181 strcpy (pop_error, "Could not determine username");
182 return (0);
189 * Determine the mail host.
192 if (! host)
194 host = getenv ("MAILHOST");
197 #ifdef HESIOD
198 if ((! host) && (! (flags & POP_NO_HESIOD)))
200 struct hes_postoffice *office;
201 office = hes_getmailhost (username);
202 if (office && office->po_type && (! strcmp (office->po_type, "POP"))
203 && office->po_name && *office->po_name && office->po_host
204 && *office->po_host)
206 host = office->po_host;
207 username = office->po_name;
210 #endif
212 #ifdef MAILHOST
213 if (! host)
215 host = MAILHOST;
217 #endif
219 if (! host)
221 strcpy (pop_error, "Could not determine POP server");
222 return (0);
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
232 if ((! password) && (! DONT_NEED_PASSWORD))
234 if (! (flags & POP_NO_GETPASS))
236 password = getpass ("Enter POP password:");
238 if (! password)
240 strcpy (pop_error, "Could not determine POP password");
241 return (0);
244 if (password)
245 flags |= POP_NO_KERBEROS;
246 else
247 password = username;
249 sock = socket_connection (host, flags);
250 if (sock == -1)
251 return (0);
253 server = (popserver) malloc (sizeof (struct _popserver));
254 if (! server)
256 strcpy (pop_error, "Out of memory in pop_open");
257 return (0);
259 server->buffer = (char *) malloc (GETLINE_MIN);
260 if (! server->buffer)
262 strcpy (pop_error, "Out of memory in pop_open");
263 free ((char *) server);
264 return (0);
267 server->file = sock;
268 server->data = 0;
269 server->buffer_index = 0;
270 server->buffer_size = GETLINE_MIN;
271 server->in_multi = 0;
273 if (getok (server))
274 return (0);
277 * I really shouldn't use the pop_error variable like this, but....
279 if (strlen (username) > ERROR_MAX - 6)
281 pop_close (server);
282 strcpy (pop_error,
283 "Username too long; recompile pop.c with larger ERROR_MAX");
284 return (0);
286 sprintf (pop_error, "USER %s", username);
288 if (sendline (server, pop_error) || getok (server))
290 return (0);
293 if (strlen (password) > ERROR_MAX - 6)
295 pop_close (server);
296 strcpy (pop_error,
297 "Password too long; recompile pop.c with larger ERROR_MAX");
298 return (0);
300 sprintf (pop_error, "PASS %s", password);
302 if (sendline (server, pop_error) || getok (server))
304 return (0);
307 return (server);
311 * Function: pop_stat
313 * Purpose: Issue the STAT command to the server and return (in the
314 * value parameters) the number of messages in the maildrop and
315 * the total size of the maildrop.
317 * Return value: 0 on success, or non-zero with an error in pop_error
318 * in failure.
320 * Side effects: On failure, may make further operations on the
321 * connection impossible.
324 pop_stat (server, count, size)
325 popserver server;
326 int *count;
327 int *size;
329 char *fromserver;
331 if (server->in_multi)
333 strcpy (pop_error, "In multi-line query in pop_stat");
334 return (-1);
337 if (sendline (server, "STAT") || (! (fromserver = getline (server))))
338 return (-1);
340 if (strncmp (fromserver, "+OK ", 4))
342 if (0 == strncmp (fromserver, "-ERR", 4))
344 strncpy (pop_error, fromserver, ERROR_MAX);
346 else
348 strcpy (pop_error,
349 "Unexpected response from POP server in pop_stat");
350 pop_trash (server);
352 return (-1);
355 *count = atoi (&fromserver[4]);
357 fromserver = index (&fromserver[4], ' ');
358 if (! fromserver)
360 strcpy (pop_error,
361 "Badly formatted response from server in pop_stat");
362 pop_trash (server);
363 return (-1);
366 *size = atoi (fromserver + 1);
368 return (0);
372 * Function: pop_list
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.
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.
384 * Return value: 0 on success, non-zero with error in pop_error on
385 * failure.
387 * Side effects: On failure, may make further operations on the
388 * connection impossible.
391 pop_list (server, message, IDs, sizes)
392 popserver server;
393 int message;
394 int **IDs;
395 int **sizes;
397 int how_many, i;
398 char *fromserver;
400 if (server->in_multi)
402 strcpy (pop_error, "In multi-line query in pop_list");
403 return (-1);
406 if (message)
407 how_many = 1;
408 else
410 int count, size;
411 if (pop_stat (server, &count, &size))
412 return (-1);
413 how_many = count;
416 *IDs = (int *) malloc ((how_many + 1) * sizeof (int));
417 *sizes = (int *) malloc ((how_many + 1) * sizeof (int));
418 if (! (*IDs && *sizes))
420 strcpy (pop_error, "Out of memory in pop_list");
421 return (-1);
424 if (message)
426 sprintf (pop_error, "LIST %d", message);
427 if (sendline (server, pop_error))
429 free ((char *) *IDs);
430 free ((char *) *sizes);
431 return (-1);
433 if (! (fromserver = getline (server)))
435 free ((char *) *IDs);
436 free ((char *) *sizes);
437 return (-1);
439 if (strncmp (fromserver, "+OK ", 4))
441 if (! strncmp (fromserver, "-ERR", 4))
442 strncpy (pop_error, fromserver, ERROR_MAX);
443 else
445 strcpy (pop_error,
446 "Unexpected response from server in pop_list");
447 pop_trash (server);
449 free ((char *) *IDs);
450 free ((char *) *sizes);
451 return (-1);
453 (*IDs)[0] = atoi (&fromserver[4]);
454 fromserver = index (&fromserver[4], ' ');
455 if (! fromserver)
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);
464 (*sizes)[0] = atoi (fromserver);
465 (*IDs)[1] = (*sizes)[1] = 0;
466 return (0);
468 else
470 if (pop_multi_first (server, "LIST", &fromserver))
472 free ((char *) *IDs);
473 free ((char *) *sizes);
474 return (-1);
476 for (i = 0; i < how_many; i++)
478 if (pop_multi_next (server, &fromserver))
480 free ((char *) *IDs);
481 free ((char *) *sizes);
482 return (-1);
484 (*IDs)[i] = atoi (fromserver);
485 fromserver = index (fromserver, ' ');
486 if (! fromserver)
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);
495 (*sizes)[i] = atoi (fromserver);
497 if (pop_multi_next (server, &fromserver))
499 free ((char *) *IDs);
500 free ((char *) *sizes);
501 return (-1);
503 else if (fromserver)
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);
511 (*IDs)[i] = (*sizes)[i] = 0;
512 return (0);
517 * Function: pop_retrieve
519 * Purpose: Retrieve a specified message from the maildrop.
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 '>'.
528 * Return value: A string pointing to the message, if successful, or
529 * null with pop_error set if not.
531 * Side effects: May kill connection on error.
533 char *
534 pop_retrieve (server, message, markfrom)
535 popserver server;
536 int message;
537 int markfrom;
539 int *IDs, *sizes, bufsize, fromcount = 0, cp = 0;
540 char *ptr, *fromserver;
541 int ret;
543 if (server->in_multi)
545 strcpy (pop_error, "In multi-line query in pop_retrieve");
546 return (0);
549 if (pop_list (server, message, &IDs, &sizes))
550 return (0);
552 if (pop_retrieve_first (server, message, &fromserver))
554 return (0);
558 * The "5" below is an arbitrary constant -- I assume that if
559 * there are "From" lines in the text to be marked, there
560 * probably won't be more than 5 of them. If there are, I
561 * allocate more space for them below.
563 bufsize = sizes[0] + (markfrom ? 5 : 0);
564 ptr = (char *)malloc (bufsize);
565 free ((char *) IDs);
566 free ((char *) sizes);
568 if (! ptr)
570 strcpy (pop_error, "Out of memory in pop_retrieve");
571 pop_retrieve_flush (server);
572 return (0);
575 while (! (ret = pop_retrieve_next (server, &fromserver)))
577 int linesize;
579 if (! fromserver)
581 ptr[cp] = '\0';
582 return (ptr);
584 if (markfrom && fromserver[0] == 'F' && fromserver[1] == 'r' &&
585 fromserver[2] == 'o' && fromserver[3] == 'm' &&
586 fromserver[4] == ' ')
588 if (++fromcount == 5)
590 bufsize += 5;
591 ptr = (char *)realloc (ptr, bufsize);
592 if (! ptr)
594 strcpy (pop_error, "Out of memory in pop_retrieve");
595 pop_retrieve_flush (server);
596 return (0);
598 fromcount = 0;
600 ptr[cp++] = '>';
602 linesize = strlen (fromserver);
603 bcopy (fromserver, &ptr[cp], linesize);
604 cp += linesize;
605 ptr[cp++] = '\n';
608 if (ret)
610 free (ptr);
611 return (0);
616 pop_retrieve_first (server, message, response)
617 popserver server;
618 int message;
619 char **response;
621 sprintf (pop_error, "RETR %d", message);
622 return (pop_multi_first (server, pop_error, response));
626 pop_retrieve_next (server, line)
627 popserver server;
628 char **line;
630 return (pop_multi_next (server, line));
634 pop_retrieve_flush (server)
635 popserver server;
637 return (pop_multi_flush (server));
641 pop_top_first (server, message, lines, response)
642 popserver server;
643 int message, lines;
644 char **response;
646 sprintf (pop_error, "TOP %d %d", message, lines);
647 return (pop_multi_first (server, pop_error, response));
651 pop_top_next (server, line)
652 popserver server;
653 char **line;
655 return (pop_multi_next (server, line));
659 pop_top_flush (server)
660 popserver server;
662 return (pop_multi_flush (server));
666 pop_multi_first (server, command, response)
667 popserver server;
668 char *command;
669 char **response;
671 if (server->in_multi)
673 strcpy (pop_error,
674 "Already in multi-line query in pop_multi_first");
675 return (-1);
678 if (sendline (server, command) || (! (*response = getline (server))))
680 return (-1);
683 if (0 == strncmp (*response, "-ERR", 4))
685 strncpy (pop_error, *response, ERROR_MAX);
686 return (-1);
688 else if (0 == strncmp (*response, "+OK", 3))
690 for (*response += 3; **response == ' '; (*response)++) /* empty */;
691 server->in_multi = 1;
692 return (0);
694 else
696 strcpy (pop_error,
697 "Unexpected response from server in pop_multi_first");
698 return (-1);
703 pop_multi_next (server, line)
704 popserver server;
705 char **line;
707 char *fromserver;
709 if (! server->in_multi)
711 strcpy (pop_error, "Not in multi-line query in pop_multi_next");
712 return (-1);
715 fromserver = getline (server);
716 if (! fromserver)
718 return (-1);
721 if (fromserver[0] == '.')
723 if (! fromserver[1])
725 *line = 0;
726 server->in_multi = 0;
727 return (0);
729 else
731 *line = fromserver + 1;
732 return (0);
735 else
737 *line = fromserver;
738 return (0);
743 pop_multi_flush (server)
744 popserver server;
746 char *line;
748 if (! server->in_multi)
750 return (0);
753 while (! pop_multi_next (server, &line))
755 if (! line)
757 return (0);
761 return (-1);
764 /* Function: pop_delete
766 * Purpose: Delete a specified message.
768 * Arguments:
769 * server Server from which to delete the message.
770 * message Message to delete.
772 * Return value: 0 on success, non-zero with error in pop_error
773 * otherwise.
776 pop_delete (server, message)
777 popserver server;
778 int message;
780 if (server->in_multi)
782 strcpy (pop_error, "In multi-line query in pop_delete");
783 return (-1);
786 sprintf (pop_error, "DELE %d", message);
788 if (sendline (server, pop_error) || getok (server))
789 return (-1);
791 return (0);
795 * Function: pop_noop
797 * Purpose: Send a noop command to the server.
799 * Argument:
800 * server The server to send to.
802 * Return value: 0 on success, non-zero with error in pop_error
803 * otherwise.
805 * Side effects: Closes connection on error.
808 pop_noop (server)
809 popserver server;
811 if (server->in_multi)
813 strcpy (pop_error, "In multi-line query in pop_noop");
814 return (-1);
817 if (sendline (server, "NOOP") || getok (server))
818 return (-1);
820 return (0);
824 * Function: pop_last
826 * Purpose: Find out the highest seen message from the server.
828 * Arguments:
829 * server The server.
831 * Return value: If successful, the highest seen message, which is
832 * greater than or equal to 0. Otherwise, a negative number with
833 * the error explained in pop_error.
835 * Side effects: Closes the connection on error.
838 pop_last (server)
839 popserver server;
841 char *fromserver;
843 if (server->in_multi)
845 strcpy (pop_error, "In multi-line query in pop_last");
846 return (-1);
849 if (sendline (server, "LAST"))
850 return (-1);
852 if (! (fromserver = getline (server)))
853 return (-1);
855 if (! strncmp (fromserver, "-ERR", 4))
857 strncpy (pop_error, fromserver, ERROR_MAX);
858 return (-1);
860 else if (strncmp (fromserver, "+OK ", 4))
862 strcpy (pop_error, "Unexpected response from server in pop_last");
863 pop_trash (server);
864 return (-1);
866 else
868 return (atoi (&fromserver[4]));
873 * Function: pop_reset
875 * Purpose: Reset the server to its initial connect state
877 * Arguments:
878 * server The server.
880 * Return value: 0 for success, non-0 with error in pop_error
881 * otherwise.
883 * Side effects: Closes the connection on error.
886 pop_reset (server)
887 popserver server;
889 if (pop_retrieve_flush (server))
891 return (-1);
894 if (sendline (server, "RSET") || getok (server))
895 return (-1);
897 return (0);
901 * Function: pop_quit
903 * Purpose: Quit the connection to the server,
905 * Arguments:
906 * server The server to quit.
908 * Return value: 0 for success, non-zero otherwise with error in
909 * pop_error.
911 * Side Effects: The popserver passed in is unusable after this
912 * function is called, even if an error occurs.
915 pop_quit (server)
916 popserver server;
918 int ret = 0;
920 if (server->file >= 0)
922 if (pop_retrieve_flush (server))
924 ret = -1;
927 if (sendline (server, "QUIT") || getok (server))
929 ret = -1;
932 close (server->file);
935 if (server->buffer)
936 free (server->buffer);
937 free ((char *) server);
939 return (ret);
943 * Function: socket_connection
945 * Purpose: Opens the network connection with the mail host, without
946 * doing any sort of I/O with it or anything.
948 * Arguments:
949 * host The host to which to connect.
950 * flags Option flags.
952 * Return value: A file descriptor indicating the connection, or -1
953 * indicating failure, in which case an error has been copied
954 * into pop_error.
956 static int
957 socket_connection (host, flags)
958 char *host;
959 int flags;
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 */
983 int try_count = 0;
987 hostent = gethostbyname (host);
988 try_count++;
989 if ((! hostent) && ((h_errno != TRY_AGAIN) || (try_count == 5)))
991 strcpy (pop_error, "Could not determine POP server's address");
992 return (-1);
994 } while (! hostent);
996 bzero ((char *) &addr, sizeof (addr));
997 addr.sin_family = AF_INET;
999 #ifdef KERBEROS
1000 service = (flags & POP_NO_KERBEROS) ? POP_SERVICE : KPOP_SERVICE;
1001 #else
1002 service = POP_SERVICE;
1003 #endif
1005 #ifdef HESIOD
1006 if (! (flags & POP_NO_HESIOD))
1008 servent = hes_getservbyname (service, "tcp");
1009 if (servent)
1011 addr.sin_port = servent->s_port;
1012 found_port = 1;
1015 #endif
1016 if (! found_port)
1018 servent = getservbyname (service, "tcp");
1019 if (servent)
1021 addr.sin_port = servent->s_port;
1023 else
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
1034 #define SOCKET_ERROR "Could not create socket for POP connection: "
1036 sock = socket (PF_INET, SOCK_STREAM, 0);
1037 if (sock < 0)
1039 strcpy (pop_error, SOCKET_ERROR);
1040 strncat (pop_error, strerror (errno),
1041 ERROR_MAX - sizeof (SOCKET_ERROR));
1042 return (-1);
1046 while (*hostent->h_addr_list)
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++;
1055 #define CONNECT_ERROR "Could not connect to POP server: "
1057 if (! *hostent->h_addr_list)
1059 (void) close (sock);
1060 strcpy (pop_error, CONNECT_ERROR);
1061 strncat (pop_error, strerror (errno),
1062 ERROR_MAX - sizeof (CONNECT_ERROR));
1063 return (-1);
1067 #ifdef KERBEROS
1068 #define KRB_ERROR "Kerberos error connecting to POP server: "
1069 if (! (flags & POP_NO_KERBEROS))
1071 #ifdef KRB5
1072 krb5_init_ets ();
1074 if (rem = krb5_cc_default (&ccdef))
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);
1084 if (rem = krb5_cc_get_principal (ccdef, &client))
1086 goto krb5error;
1089 for (cp = hostent->h_name; *cp; cp++)
1091 if (isupper (*cp))
1093 *cp = tolower (*cp);
1097 if (rem = krb5_sname_to_principal (hostent->h_name, POP_SERVICE,
1098 FALSE, &server))
1100 goto krb5error;
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)
1115 if (err_ret && err_ret->text.length)
1117 strcpy (pop_error, KRB_ERROR);
1118 strncat (pop_error, error_message (rem),
1119 ERROR_MAX - sizeof (KRB_ERROR));
1120 strncat (pop_error, " [server says '",
1121 ERROR_MAX - strlen (pop_error) - 1);
1122 strncat (pop_error, err_ret->text.data,
1123 min (ERROR_MAX - strlen (pop_error) - 1,
1124 err_ret->text.length));
1125 strncat (pop_error, "']",
1126 ERROR_MAX - strlen (pop_error) - 1);
1128 else
1130 strcpy (pop_error, KRB_ERROR);
1131 strncat (pop_error, error_message (rem),
1132 ERROR_MAX - sizeof (KRB_ERROR));
1134 if (err_ret)
1135 krb5_free_error (err_ret);
1137 (void) close (sock);
1138 return (-1);
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)
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);
1157 #endif /* KRB5 */
1159 #endif /* KERBEROS */
1161 return (sock);
1162 } /* socket_connection */
1165 * Function: getline
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.
1172 * Arguments:
1173 * server The server from which to get the line of text.
1175 * Returns: A non-null pointer if successful, or a null pointer on any
1176 * error, with an error message copied into pop_error.
1178 * Notes: The line returned is overwritten with each call to getline.
1180 * Side effects: Closes the connection on error.
1182 static char *
1183 getline (server)
1184 popserver server;
1186 #define GETLINE_ERROR "Error reading from server: "
1188 int ret;
1189 int search_offset = 0;
1191 if (server->data)
1193 char *cp = find_crlf (server->buffer + server->buffer_index);
1194 if (cp)
1196 int found;
1197 int data_used;
1199 found = server->buffer_index;
1200 data_used = (cp + 2) - server->buffer - found;
1202 *cp = '\0'; /* terminate the string to be returned */
1203 server->data -= data_used;
1204 server->buffer_index += data_used;
1206 if (pop_debug)
1207 fprintf (stderr, "<<< %s\n", server->buffer + found);
1208 return (server->buffer + found);
1210 else
1212 bcopy (server->buffer + server->buffer_index,
1213 server->buffer, server->data);
1214 /* Record the fact that we've searched the data already in
1215 the buffer for a CRLF, so that when we search below, we
1216 don't have to search the same data twice. There's a "-
1217 1" here to account for the fact that the last character
1218 of the data we have may be the CR of a CRLF pair, of
1219 which we haven't read the second half yet, so we may have
1220 to search it again when we read more data. */
1221 search_offset = server->data - 1;
1222 server->buffer_index = 0;
1225 else
1227 server->buffer_index = 0;
1230 while (1)
1232 /* There's a "- 1" here to leave room for the null that we put
1233 at the end of the read data below. We put the null there so
1234 that find_crlf knows where to stop when we call it. */
1235 if (server->data == server->buffer_size - 1)
1237 server->buffer_size += GETLINE_INCR;
1238 server->buffer = (char *)realloc (server->buffer, server->buffer_size);
1239 if (! server->buffer)
1241 strcpy (pop_error, "Out of memory in getline");
1242 pop_trash (server);
1243 return (0);
1246 ret = read (server->file, server->buffer + server->data,
1247 server->buffer_size - server->data - 1);
1248 if (ret < 0)
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);
1256 else if (ret == 0)
1258 strcpy (pop_error, "Unexpected EOF from server in getline");
1259 pop_trash (server);
1260 return (0);
1262 else
1264 char *cp;
1265 server->data += ret;
1266 server->buffer[server->data] = '\0';
1268 cp = find_crlf (server->buffer + search_offset);
1269 if (cp)
1271 int data_used = (cp + 2) - server->buffer;
1272 *cp = '\0';
1273 server->data -= data_used;
1274 server->buffer_index = data_used;
1276 if (pop_debug)
1277 fprintf (stderr, "<<< %s\n", server->buffer);
1278 return (server->buffer);
1280 search_offset += ret;
1284 /* NOTREACHED */
1288 * Function: sendline
1290 * Purpose: Sends a line of text to the POP server. The line of text
1291 * passed into this function should NOT have the carriage return
1292 * and linefeed on the end of it. Periods at beginnings of lines
1293 * will NOT be treated specially by this function.
1295 * Arguments:
1296 * server The server to which to send the text.
1297 * line The line of text to send.
1299 * Return value: Upon successful completion, a value of 0 will be
1300 * returned. Otherwise, a non-zero value will be returned, and
1301 * an error will be copied into pop_error.
1303 * Side effects: Closes the connection on error.
1305 static int
1306 sendline (server, line)
1307 popserver server;
1308 char *line;
1310 #define SENDLINE_ERROR "Error writing to POP server: "
1311 int ret;
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);
1319 if (ret < 0)
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);
1328 if (pop_debug)
1329 fprintf (stderr, ">>> %s\n", line);
1331 return (0);
1335 * Procedure: fullwrite
1337 * Purpose: Just like write, but keeps trying until the entire string
1338 * has been written.
1340 * Return value: Same as write. Pop_error is not set.
1342 static int
1343 fullwrite (fd, buf, nbytes)
1344 int fd;
1345 char *buf;
1346 int nbytes;
1348 char *cp;
1349 int ret;
1351 cp = buf;
1352 while ((ret = write (fd, cp, nbytes)) > 0)
1354 cp += ret;
1355 nbytes -= ret;
1358 return (ret);
1362 * Procedure getok
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.
1368 * Arguments:
1369 * server The server to read from.
1371 * Returns: 0 for success, else for failure and puts error in pop_error.
1373 * Side effects: On failure, may make the connection unusable.
1375 static int
1376 getok (server)
1377 popserver server;
1379 char *fromline;
1381 if (! (fromline = getline (server)))
1383 return (-1);
1386 if (! strncmp (fromline, "+OK", 3))
1387 return (0);
1388 else if (! strncmp (fromline, "-ERR", 4))
1390 strncpy (pop_error, fromline, ERROR_MAX);
1391 pop_error[ERROR_MAX-1] = '\0';
1392 return (-1);
1394 else
1396 strcpy (pop_error,
1397 "Unexpected response from server; expecting +OK or -ERR");
1398 pop_trash (server);
1399 return (-1);
1403 #if 0
1405 * Function: gettermination
1407 * Purpose: Gets the next line and verifies that it is a termination
1408 * line (nothing but a dot).
1410 * Return value: 0 on success, non-zero with pop_error set on error.
1412 * Side effects: Closes the connection on error.
1414 static int
1415 gettermination (server)
1416 popserver server;
1418 char *fromserver;
1420 fromserver = getline (server);
1421 if (! fromserver)
1422 return (-1);
1424 if (strcmp (fromserver, "."))
1426 strcpy (pop_error,
1427 "Unexpected response from server in gettermination");
1428 pop_trash (server);
1429 return (-1);
1432 return (0);
1434 #endif
1437 * Function pop_close
1439 * Purpose: Close a pop connection, sending a "RSET" command to try to
1440 * preserve any changes that were made and a "QUIT" command to
1441 * try to get the server to quit, but ignoring any responses that
1442 * are received.
1444 * Side effects: The server is unusable after this function returns.
1445 * Changes made to the maildrop since the session was started (or
1446 * since the last pop_reset) may be lost.
1448 void
1449 pop_close (server)
1450 popserver server;
1452 pop_trash (server);
1453 free ((char *) server);
1455 return;
1459 * Function: pop_trash
1461 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
1462 * memory associated with the server. It is legal to call
1463 * pop_close or pop_quit after this function has been called.
1465 static void
1466 pop_trash (server)
1467 popserver server;
1469 if (server->file >= 0)
1471 sendline (server, "RSET");
1472 sendline (server, "QUIT");
1474 close (server->file);
1475 server->file = -1;
1476 if (server->buffer)
1478 free (server->buffer);
1479 server->buffer = 0;
1484 /* Return a pointer to the first CRLF in IN_STRING,
1485 or 0 if it does not contain one. */
1487 static char *
1488 find_crlf (in_string)
1489 char *in_string;
1491 while (1)
1493 if (! *in_string)
1494 return (0);
1495 else if (*in_string == '\r')
1497 if (*++in_string == '\n')
1498 return (in_string - 1);
1500 else
1501 in_string++;
1503 /* NOTREACHED */
1506 #endif /* MAIL_USE_POP */