(datearg): If rlog options are specified explicitly,
[emacs/old-mirror.git] / lib-src / pop.c
blob125df24f130fa81c1f91ddbb26292c9dbead5e40
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #ifdef HAVE_CONFIG_H
22 #define NO_SHORTNAMES /* Tell config not to load remap.h */
23 #include <../src/config.h>
24 #else
25 #define MAIL_USE_POP
26 #endif
28 #ifdef MAIL_USE_POP
30 #ifdef HAVE_CONFIG_H
31 /* Cancel these substitutions made in config.h */
32 #undef open
33 #undef read
34 #undef write
35 #undef close
36 #endif
38 #include <sys/types.h>
39 #include <netinet/in.h>
40 #include <sys/socket.h>
41 #include <pop.h>
43 #ifdef sun
44 #include <malloc.h>
45 #endif /* sun */
47 #ifdef HESIOD
48 #include <hesiod.h>
50 * It really shouldn't be necessary to put this declaration here, but
51 * the version of hesiod.h that Athena has installed in release 7.2
52 * doesn't declare this function; I don't know if the 7.3 version of
53 * hesiod.h does.
55 extern struct servent *hes_getservbyname (/* char *, char * */);
56 #endif
58 #include <pwd.h>
59 #include <netdb.h>
60 #include <errno.h>
61 #include <stdio.h>
63 #ifdef KERBEROS
64 #ifndef KRB5
65 #include <des.h>
66 #include <krb.h>
67 #else /* KRB5 */
68 #include <krb5/krb5.h>
69 #include <krb5/ext-proto.h>
70 #include <ctype.h>
71 #endif /* KRB5 */
72 #endif /* KERBEROS */
74 extern char *getenv (/* char * */);
75 extern char *getlogin (/* void */);
76 extern char *getpass (/* char * */);
77 extern char *strerror (/* int */);
79 #ifdef KERBEROS
80 #ifndef KRB5
81 extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *,
82 u_long, MSG_DAT *, CREDENTIALS *, Key_schedule,
83 struct sockaddr_in *, struct sockaddr_in *,
84 char * */);
85 extern char *krb_realmofhost (/* char * */);
86 #endif /* ! KRB5 */
87 #endif /* KERBEROS */
89 #if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H)
90 extern int h_errno;
91 #endif
93 static int socket_connection (/* char *, int */);
94 static char *getline (/* popserver */);
95 static int sendline (/* popserver, char * */);
96 static int fullwrite (/* int, char *, int */);
97 static int getok (/* popserver */);
98 #if 0
99 static int gettermination (/* popserver */);
100 #endif
101 static void pop_trash (/* popserver */);
102 static char *find_crlf (/* char * */);
104 #define ERROR_MAX 80 /* a pretty arbitrary size */
105 #define POP_PORT 110
106 #define KPOP_PORT 1109
107 #define POP_SERVICE "pop"
108 #ifdef KERBEROS
109 #ifdef KRB5
110 #define KPOP_SERVICE "k5pop";
111 #else
112 #define KPOP_SERVICE "kpop"
113 #endif
114 #endif
116 char pop_error[ERROR_MAX];
117 int pop_debug = 0;
119 #ifndef min
120 #define min(a,b) (((a) < (b)) ? (a) : (b))
121 #endif
124 * Function: pop_open (char *host, char *username, char *password,
125 * int flags)
127 * Purpose: Establishes a connection with a post-office server, and
128 * completes the authorization portion of the session.
130 * Arguments:
131 * host The server host with which the connection should be
132 * established. Optional. If omitted, internal
133 * heuristics will be used to determine the server host,
134 * if possible.
135 * username
136 * The username of the mail-drop to access. Optional.
137 * If omitted, internal heuristics will be used to
138 * determine the username, if possible.
139 * password
140 * The password to use for authorization. If omitted,
141 * internal heuristics will be used to determine the
142 * password, if possible.
143 * flags A bit mask containing flags controlling certain
144 * functions of the routine. Valid flags are defined in
145 * the file pop.h
147 * Return value: Upon successful establishment of a connection, a
148 * non-null popserver will be returned. Otherwise, null will be
149 * returned, and the string variable pop_error will contain an
150 * explanation of the error.
152 popserver
153 pop_open (host, username, password, flags)
154 char *host;
155 char *username;
156 char *password;
157 int flags;
159 int sock;
160 popserver server;
162 /* Determine the user name */
163 if (! username)
165 username = getenv ("USER");
166 if (! (username && *username))
168 username = getlogin ();
169 if (! (username && *username))
171 struct passwd *passwd;
172 passwd = getpwuid (getuid ());
173 if (passwd && passwd->pw_name && *passwd->pw_name)
175 username = passwd->pw_name;
177 else
179 strcpy (pop_error, "Could not determine username");
180 return (0);
187 * Determine the mail host.
190 if (! host)
192 host = getenv ("MAILHOST");
195 #ifdef HESIOD
196 if ((! host) && (! (flags & POP_NO_HESIOD)))
198 struct hes_postoffice *office;
199 office = hes_getmailhost (username);
200 if (office && office->po_type && (! strcmp (office->po_type, "POP"))
201 && office->po_name && *office->po_name && office->po_host
202 && *office->po_host)
204 host = office->po_host;
205 username = office->po_name;
208 #endif
210 #ifdef MAILHOST
211 if (! host)
213 host = MAILHOST;
215 #endif
217 if (! host)
219 strcpy (pop_error, "Could not determine POP server");
220 return (0);
223 /* Determine the password */
224 #ifdef KERBEROS
225 #define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS))
226 #else
227 #define DONT_NEED_PASSWORD 0
228 #endif
230 if ((! password) && (! DONT_NEED_PASSWORD))
232 if (! (flags & POP_NO_GETPASS))
234 password = getpass ("Enter POP password:");
236 if (! password)
238 strcpy (pop_error, "Could not determine POP password");
239 return (0);
242 if (password)
243 flags |= POP_NO_KERBEROS;
244 else
245 password = username;
247 sock = socket_connection (host, flags);
248 if (sock == -1)
249 return (0);
251 server = (popserver) malloc (sizeof (struct _popserver));
252 if (! server)
254 strcpy (pop_error, "Out of memory in pop_open");
255 return (0);
257 server->buffer = (char *) malloc (GETLINE_MIN);
258 if (! server->buffer)
260 strcpy (pop_error, "Out of memory in pop_open");
261 free ((char *) server);
262 return (0);
265 server->file = sock;
266 server->data = 0;
267 server->buffer_index = 0;
268 server->buffer_size = GETLINE_MIN;
269 server->in_multi = 0;
271 if (getok (server))
272 return (0);
275 * I really shouldn't use the pop_error variable like this, but....
277 if (strlen (username) > ERROR_MAX - 6)
279 pop_close (server);
280 strcpy (pop_error,
281 "Username too long; recompile pop.c with larger ERROR_MAX");
282 return (0);
284 sprintf (pop_error, "USER %s", username);
286 if (sendline (server, pop_error) || getok (server))
288 return (0);
291 if (strlen (password) > ERROR_MAX - 6)
293 pop_close (server);
294 strcpy (pop_error,
295 "Password too long; recompile pop.c with larger ERROR_MAX");
296 return (0);
298 sprintf (pop_error, "PASS %s", password);
300 if (sendline (server, pop_error) || getok (server))
302 return (0);
305 return (server);
309 * Function: pop_stat
311 * Purpose: Issue the STAT command to the server and return (in the
312 * value parameters) the number of messages in the maildrop and
313 * the total size of the maildrop.
315 * Return value: 0 on success, or non-zero with an error in pop_error
316 * in failure.
318 * Side effects: On failure, may make further operations on the
319 * connection impossible.
322 pop_stat (server, count, size)
323 popserver server;
324 int *count;
325 int *size;
327 char *fromserver;
329 if (server->in_multi)
331 strcpy (pop_error, "In multi-line query in pop_stat");
332 return (-1);
335 if (sendline (server, "STAT") || (! (fromserver = getline (server))))
336 return (-1);
338 if (strncmp (fromserver, "+OK ", 4))
340 if (0 == strncmp (fromserver, "-ERR", 4))
342 strncpy (pop_error, fromserver, ERROR_MAX);
344 else
346 strcpy (pop_error,
347 "Unexpected response from POP server in pop_stat");
348 pop_trash (server);
350 return (-1);
353 *count = atoi (&fromserver[4]);
355 fromserver = index (&fromserver[4], ' ');
356 if (! fromserver)
358 strcpy (pop_error,
359 "Badly formatted response from server in pop_stat");
360 pop_trash (server);
361 return (-1);
364 *size = atoi (fromserver + 1);
366 return (0);
370 * Function: pop_list
372 * Purpose: Performs the POP "list" command and returns (in value
373 * parameters) two malloc'd zero-terminated arrays -- one of
374 * message IDs, and a parallel one of sizes.
376 * Arguments:
377 * server The pop connection to talk to.
378 * message The number of the one message about which to get
379 * information, or 0 to get information about all
380 * messages.
382 * Return value: 0 on success, non-zero with error in pop_error on
383 * failure.
385 * Side effects: On failure, may make further operations on the
386 * connection impossible.
389 pop_list (server, message, IDs, sizes)
390 popserver server;
391 int message;
392 int **IDs;
393 int **sizes;
395 int how_many, i;
396 char *fromserver;
398 if (server->in_multi)
400 strcpy (pop_error, "In multi-line query in pop_list");
401 return (-1);
404 if (message)
405 how_many = 1;
406 else
408 int count, size;
409 if (pop_stat (server, &count, &size))
410 return (-1);
411 how_many = count;
414 *IDs = (int *) malloc ((how_many + 1) * sizeof (int));
415 *sizes = (int *) malloc ((how_many + 1) * sizeof (int));
416 if (! (*IDs && *sizes))
418 strcpy (pop_error, "Out of memory in pop_list");
419 return (-1);
422 if (message)
424 sprintf (pop_error, "LIST %d", message);
425 if (sendline (server, pop_error))
427 free ((char *) *IDs);
428 free ((char *) *sizes);
429 return (-1);
431 if (! (fromserver = getline (server)))
433 free ((char *) *IDs);
434 free ((char *) *sizes);
435 return (-1);
437 if (strncmp (fromserver, "+OK ", 4))
439 if (! strncmp (fromserver, "-ERR", 4))
440 strncpy (pop_error, fromserver, ERROR_MAX);
441 else
443 strcpy (pop_error,
444 "Unexpected response from server in pop_list");
445 pop_trash (server);
447 free ((char *) *IDs);
448 free ((char *) *sizes);
449 return (-1);
451 (*IDs)[0] = atoi (&fromserver[4]);
452 fromserver = index (&fromserver[4], ' ');
453 if (! fromserver)
455 strcpy (pop_error,
456 "Badly formatted response from server in pop_list");
457 pop_trash (server);
458 free ((char *) *IDs);
459 free ((char *) *sizes);
460 return (-1);
462 (*sizes)[0] = atoi (fromserver);
463 (*IDs)[1] = (*sizes)[1] = 0;
464 return (0);
466 else
468 if (pop_multi_first (server, "LIST", &fromserver))
470 free ((char *) *IDs);
471 free ((char *) *sizes);
472 return (-1);
474 for (i = 0; i < how_many; i++)
476 if (pop_multi_next (server, &fromserver))
478 free ((char *) *IDs);
479 free ((char *) *sizes);
480 return (-1);
482 (*IDs)[i] = atoi (fromserver);
483 fromserver = index (fromserver, ' ');
484 if (! fromserver)
486 strcpy (pop_error,
487 "Badly formatted response from server in pop_list");
488 free ((char *) *IDs);
489 free ((char *) *sizes);
490 pop_trash (server);
491 return (-1);
493 (*sizes)[i] = atoi (fromserver);
495 if (pop_multi_next (server, &fromserver))
497 free ((char *) *IDs);
498 free ((char *) *sizes);
499 return (-1);
501 else if (fromserver)
503 strcpy (pop_error,
504 "Too many response lines from server in pop_list");
505 free ((char *) *IDs);
506 free ((char *) *sizes);
507 return (-1);
509 (*IDs)[i] = (*sizes)[i] = 0;
510 return (0);
515 * Function: pop_retrieve
517 * Purpose: Retrieve a specified message from the maildrop.
519 * Arguments:
520 * server The server to retrieve from.
521 * message The message number to retrieve.
522 * markfrom
523 * If true, then mark the string "From " at the beginning
524 * of lines with '>'.
526 * Return value: A string pointing to the message, if successful, or
527 * null with pop_error set if not.
529 * Side effects: May kill connection on error.
531 char *
532 pop_retrieve (server, message, markfrom)
533 popserver server;
534 int message;
535 int markfrom;
537 int *IDs, *sizes, bufsize, fromcount = 0, cp = 0;
538 char *ptr, *fromserver;
539 int ret;
541 if (server->in_multi)
543 strcpy (pop_error, "In multi-line query in pop_retrieve");
544 return (0);
547 if (pop_list (server, message, &IDs, &sizes))
548 return (0);
550 if (pop_retrieve_first (server, message, &fromserver))
552 return (0);
556 * The "5" below is an arbitrary constant -- I assume that if
557 * there are "From" lines in the text to be marked, there
558 * probably won't be more than 5 of them. If there are, I
559 * allocate more space for them below.
561 bufsize = sizes[0] + (markfrom ? 5 : 0);
562 ptr = malloc (bufsize);
563 free ((char *) IDs);
564 free ((char *) sizes);
566 if (! ptr)
568 strcpy (pop_error, "Out of memory in pop_retrieve");
569 pop_retrieve_flush (server);
570 return (0);
573 while (! (ret = pop_retrieve_next (server, &fromserver)))
575 int linesize;
577 if (! fromserver)
579 ptr[cp] = '\0';
580 return (ptr);
582 if (markfrom && fromserver[0] == 'F' && fromserver[1] == 'r' &&
583 fromserver[2] == 'o' && fromserver[3] == 'm' &&
584 fromserver[4] == ' ')
586 if (++fromcount == 5)
588 bufsize += 5;
589 ptr = realloc (ptr, bufsize);
590 if (! ptr)
592 strcpy (pop_error, "Out of memory in pop_retrieve");
593 pop_retrieve_flush (server);
594 return (0);
596 fromcount = 0;
598 ptr[cp++] = '>';
600 linesize = strlen (fromserver);
601 bcopy (fromserver, &ptr[cp], linesize);
602 cp += linesize;
603 ptr[cp++] = '\n';
606 if (ret)
608 free (ptr);
609 return (0);
614 pop_retrieve_first (server, message, response)
615 popserver server;
616 int message;
617 char **response;
619 sprintf (pop_error, "RETR %d", message);
620 return (pop_multi_first (server, pop_error, response));
624 pop_retrieve_next (server, line)
625 popserver server;
626 char **line;
628 return (pop_multi_next (server, line));
632 pop_retrieve_flush (server)
633 popserver server;
635 return (pop_multi_flush (server));
639 pop_top_first (server, message, lines, response)
640 popserver server;
641 int message, lines;
642 char **response;
644 sprintf (pop_error, "TOP %d %d", message, lines);
645 return (pop_multi_first (server, pop_error, response));
649 pop_top_next (server, line)
650 popserver server;
651 char **line;
653 return (pop_multi_next (server, line));
657 pop_top_flush (server)
658 popserver server;
660 return (pop_multi_flush (server));
664 pop_multi_first (server, command, response)
665 popserver server;
666 char *command;
667 char **response;
669 if (server->in_multi)
671 strcpy (pop_error,
672 "Already in multi-line query in pop_multi_first");
673 return (-1);
676 if (sendline (server, command) || (! (*response = getline (server))))
678 return (-1);
681 if (0 == strncmp (*response, "-ERR", 4))
683 strncpy (pop_error, *response, ERROR_MAX);
684 return (-1);
686 else if (0 == strncmp (*response, "+OK", 3))
688 for (*response += 3; **response == ' '; (*response)++) /* empty */;
689 server->in_multi = 1;
690 return (0);
692 else
694 strcpy (pop_error,
695 "Unexpected response from server in pop_multi_first");
696 return (-1);
701 pop_multi_next (server, line)
702 popserver server;
703 char **line;
705 char *fromserver;
707 if (! server->in_multi)
709 strcpy (pop_error, "Not in multi-line query in pop_multi_next");
710 return (-1);
713 fromserver = getline (server);
714 if (! fromserver)
716 return (-1);
719 if (fromserver[0] == '.')
721 if (! fromserver[1])
723 *line = 0;
724 server->in_multi = 0;
725 return (0);
727 else
729 *line = fromserver + 1;
730 return (0);
733 else
735 *line = fromserver;
736 return (0);
741 pop_multi_flush (server)
742 popserver server;
744 char *line;
746 if (! server->in_multi)
748 return (0);
751 while (! pop_multi_next (server, &line))
753 if (! line)
755 return (0);
759 return (-1);
762 /* Function: pop_delete
764 * Purpose: Delete a specified message.
766 * Arguments:
767 * server Server from which to delete the message.
768 * message Message to delete.
770 * Return value: 0 on success, non-zero with error in pop_error
771 * otherwise.
774 pop_delete (server, message)
775 popserver server;
776 int message;
778 if (server->in_multi)
780 strcpy (pop_error, "In multi-line query in pop_delete");
781 return (-1);
784 sprintf (pop_error, "DELE %d", message);
786 if (sendline (server, pop_error) || getok (server))
787 return (-1);
789 return (0);
793 * Function: pop_noop
795 * Purpose: Send a noop command to the server.
797 * Argument:
798 * server The server to send to.
800 * Return value: 0 on success, non-zero with error in pop_error
801 * otherwise.
803 * Side effects: Closes connection on error.
806 pop_noop (server)
807 popserver server;
809 if (server->in_multi)
811 strcpy (pop_error, "In multi-line query in pop_noop");
812 return (-1);
815 if (sendline (server, "NOOP") || getok (server))
816 return (-1);
818 return (0);
822 * Function: pop_last
824 * Purpose: Find out the highest seen message from the server.
826 * Arguments:
827 * server The server.
829 * Return value: If successful, the highest seen message, which is
830 * greater than or equal to 0. Otherwise, a negative number with
831 * the error explained in pop_error.
833 * Side effects: Closes the connection on error.
836 pop_last (server)
837 popserver server;
839 char *fromserver;
841 if (server->in_multi)
843 strcpy (pop_error, "In multi-line query in pop_last");
844 return (-1);
847 if (sendline (server, "LAST"))
848 return (-1);
850 if (! (fromserver = getline (server)))
851 return (-1);
853 if (! strncmp (fromserver, "-ERR", 4))
855 strncpy (pop_error, fromserver, ERROR_MAX);
856 return (-1);
858 else if (strncmp (fromserver, "+OK ", 4))
860 strcpy (pop_error, "Unexpected response from server in pop_last");
861 pop_trash (server);
862 return (-1);
864 else
866 return (atoi (&fromserver[4]));
871 * Function: pop_reset
873 * Purpose: Reset the server to its initial connect state
875 * Arguments:
876 * server The server.
878 * Return value: 0 for success, non-0 with error in pop_error
879 * otherwise.
881 * Side effects: Closes the connection on error.
884 pop_reset (server)
885 popserver server;
887 if (pop_retrieve_flush (server))
889 return (-1);
892 if (sendline (server, "RSET") || getok (server))
893 return (-1);
895 return (0);
899 * Function: pop_quit
901 * Purpose: Quit the connection to the server,
903 * Arguments:
904 * server The server to quit.
906 * Return value: 0 for success, non-zero otherwise with error in
907 * pop_error.
909 * Side Effects: The popserver passed in is unuseable after this
910 * function is called, even if an error occurs.
913 pop_quit (server)
914 popserver server;
916 int ret = 0;
918 if (server->file >= 0)
920 if (pop_retrieve_flush (server))
922 ret = -1;
925 if (sendline (server, "QUIT") || getok (server))
927 ret = -1;
930 close (server->file);
933 if (server->buffer)
934 free (server->buffer);
935 free ((char *) server);
937 return (ret);
941 * Function: socket_connection
943 * Purpose: Opens the network connection with the mail host, without
944 * doing any sort of I/O with it or anything.
946 * Arguments:
947 * host The host to which to connect.
948 * flags Option flags.
950 * Return value: A file descriptor indicating the connection, or -1
951 * indicating failure, in which case an error has been copied
952 * into pop_error.
954 static int
955 socket_connection (host, flags)
956 char *host;
957 int flags;
959 struct hostent *hostent;
960 struct servent *servent;
961 struct sockaddr_in addr;
962 char found_port = 0;
963 char *service;
964 int sock;
965 #ifdef KERBEROS
966 #ifdef KRB5
967 krb5_error_code rem;
968 krb5_ccache ccdef;
969 krb5_principal client, server;
970 krb5_error *err_ret;
971 register char *cp;
972 #else
973 KTEXT ticket;
974 MSG_DAT msg_data;
975 CREDENTIALS cred;
976 Key_schedule schedule;
977 int rem;
978 #endif /* KRB5 */
979 #endif /* KERBEROS */
981 int try_count = 0;
985 hostent = gethostbyname (host);
986 try_count++;
987 if ((! hostent) && ((h_errno != TRY_AGAIN) || (try_count == 5)))
989 strcpy (pop_error, "Could not determine POP server's address");
990 return (-1);
992 } while (! hostent);
994 bzero ((char *) &addr, sizeof (addr));
995 addr.sin_family = AF_INET;
997 #ifdef KERBEROS
998 service = (flags & POP_NO_KERBEROS) ? POP_SERVICE : KPOP_SERVICE;
999 #else
1000 service = POP_SERVICE;
1001 #endif
1003 #ifdef HESIOD
1004 if (! (flags & POP_NO_HESIOD))
1006 servent = hes_getservbyname (service, "tcp");
1007 if (servent)
1009 addr.sin_port = servent->s_port;
1010 found_port = 1;
1013 #endif
1014 if (! found_port)
1016 servent = getservbyname (service, "tcp");
1017 if (servent)
1019 addr.sin_port = servent->s_port;
1021 else
1023 #ifdef KERBEROS
1024 addr.sin_port = htons ((flags & POP_NO_KERBEROS) ?
1025 POP_PORT : KPOP_PORT);
1026 #else
1027 addr.sin_port = htons (POP_PORT);
1028 #endif
1032 #define SOCKET_ERROR "Could not create socket for POP connection: "
1034 sock = socket (PF_INET, SOCK_STREAM, 0);
1035 if (sock < 0)
1037 strcpy (pop_error, SOCKET_ERROR);
1038 strncat (pop_error, strerror (errno),
1039 ERROR_MAX - sizeof (SOCKET_ERROR));
1040 return (-1);
1044 while (*hostent->h_addr_list)
1046 bcopy (*hostent->h_addr_list, (char *) &addr.sin_addr,
1047 hostent->h_length);
1048 if (! connect (sock, (struct sockaddr *) &addr, sizeof (addr)))
1049 break;
1050 hostent->h_addr_list++;
1053 #define CONNECT_ERROR "Could not connect to POP server: "
1055 if (! *hostent->h_addr_list)
1057 (void) close (sock);
1058 strcpy (pop_error, CONNECT_ERROR);
1059 strncat (pop_error, strerror (errno),
1060 ERROR_MAX - sizeof (CONNECT_ERROR));
1061 return (-1);
1065 #ifdef KERBEROS
1066 #define KRB_ERROR "Kerberos error connecting to POP server: "
1067 if (! (flags & POP_NO_KERBEROS))
1069 #ifdef KRB5
1070 krb5_init_ets ();
1072 if (rem = krb5_cc_default (&ccdef))
1074 krb5error:
1075 strcpy (pop_error, KRB_ERROR);
1076 strncat (pop_error, error_message (rem),
1077 ERROR_MAX - sizeof(KRB_ERROR));
1078 (void) close (sock);
1079 return (-1);
1082 if (rem = krb5_cc_get_principal (ccdef, &client))
1084 goto krb5error;
1087 for (cp = hostent->h_name; *cp; cp++)
1089 if (isupper (*cp))
1091 *cp = tolower (*cp);
1095 if (rem = krb5_sname_to_principal (hostent->h_name, POP_SERVICE,
1096 FALSE, &server))
1098 goto krb5error;
1101 rem = krb5_sendauth ((krb5_pointer) &sock, "KPOPV1.0", client, server,
1102 AP_OPTS_MUTUAL_REQUIRED,
1103 0, /* no checksum */
1104 0, /* no creds, use ccache instead */
1105 ccdef,
1106 0, /* don't need seq # */
1107 0, /* don't need subsession key */
1108 &err_ret,
1109 0); /* don't need reply */
1110 krb5_free_principal (server);
1111 if (rem)
1113 if (err_ret && err_ret->text.length)
1115 strcpy (pop_error, KRB_ERROR);
1116 strncat (pop_error, error_message (rem),
1117 ERROR_MAX - sizeof (KRB_ERROR));
1118 strncat (pop_error, " [server says '",
1119 ERROR_MAX - strlen (pop_error) - 1);
1120 strncat (pop_error, err_ret->text.data,
1121 min (ERROR_MAX - strlen (pop_error) - 1,
1122 err_ret->text.length));
1123 strncat (pop_error, "']",
1124 ERROR_MAX - strlen (pop_error) - 1);
1126 else
1128 strcpy (pop_error, KRB_ERROR);
1129 strncat (pop_error, error_message (rem),
1130 ERROR_MAX - sizeof (KRB_ERROR));
1132 if (err_ret)
1133 krb5_free_error (err_ret);
1135 (void) close (sock);
1136 return (-1);
1138 #else /* ! KRB5 */
1139 ticket = (KTEXT) malloc (sizeof (KTEXT_ST));
1140 rem = krb_sendauth (0L, sock, ticket, "pop", hostent->h_name,
1141 (char *) krb_realmofhost (hostent->h_name),
1142 (unsigned long) 0, &msg_data, &cred, schedule,
1143 (struct sockaddr_in *) 0,
1144 (struct sockaddr_in *) 0,
1145 "KPOPV0.1");
1146 free ((char *) ticket);
1147 if (rem != KSUCCESS)
1149 strcpy (pop_error, KRB_ERROR);
1150 strncat (pop_error, krb_err_txt[rem],
1151 ERROR_MAX - sizeof (KRB_ERROR));
1152 (void) close (sock);
1153 return (-1);
1155 #endif /* KRB5 */
1157 #endif /* KERBEROS */
1159 return (sock);
1160 } /* socket_connection */
1163 * Function: getline
1165 * Purpose: Get a line of text from the connection and return a
1166 * pointer to it. The carriage return and linefeed at the end of
1167 * the line are stripped, but periods at the beginnings of lines
1168 * are NOT dealt with in any special way.
1170 * Arguments:
1171 * server The server from which to get the line of text.
1173 * Returns: A non-null pointer if successful, or a null pointer on any
1174 * error, with an error message copied into pop_error.
1176 * Notes: The line returned is overwritten with each call to getline.
1178 * Side effects: Closes the connection on error.
1180 static char *
1181 getline (server)
1182 popserver server;
1184 #define GETLINE_ERROR "Error reading from server: "
1186 int ret;
1187 int search_offset = 0;
1189 if (server->data)
1191 char *cp = find_crlf (server->buffer + server->buffer_index);
1192 if (cp)
1194 int found;
1195 int data_used;
1197 found = server->buffer_index;
1198 data_used = (cp + 2) - server->buffer - found;
1200 *cp = '\0'; /* terminate the string to be returned */
1201 server->data -= data_used;
1202 server->buffer_index += data_used;
1204 if (pop_debug)
1205 fprintf (stderr, "<<< %s\n", server->buffer + found);
1206 return (server->buffer + found);
1208 else
1210 bcopy (server->buffer + server->buffer_index,
1211 server->buffer, server->data);
1212 /* Record the fact that we've searched the data already in
1213 the buffer for a CRLF, so that when we search below, we
1214 don't have to search the same data twice. There's a "-
1215 1" here to account for the fact that the last character
1216 of the data we have may be the CR of a CRLF pair, of
1217 which we haven't read the second half yet, so we may have
1218 to search it again when we read more data. */
1219 search_offset = server->data - 1;
1220 server->buffer_index = 0;
1223 else
1225 server->buffer_index = 0;
1228 while (1)
1230 /* There's a "- 1" here to leave room for the null that we put
1231 at the end of the read data below. We put the null there so
1232 that find_crlf knows where to stop when we call it. */
1233 if (server->data == server->buffer_size - 1)
1235 server->buffer_size += GETLINE_INCR;
1236 server->buffer = realloc (server->buffer, server->buffer_size);
1237 if (! server->buffer)
1239 strcpy (pop_error, "Out of memory in getline");
1240 pop_trash (server);
1241 return (0);
1244 ret = read (server->file, server->buffer + server->data,
1245 server->buffer_size - server->data - 1);
1246 if (ret < 0)
1248 strcpy (pop_error, GETLINE_ERROR);
1249 strncat (pop_error, strerror (errno),
1250 ERROR_MAX - sizeof (GETLINE_ERROR));
1251 pop_trash (server);
1252 return (0);
1254 else if (ret == 0)
1256 strcpy (pop_error, "Unexpected EOF from server in getline");
1257 pop_trash (server);
1258 return (0);
1260 else
1262 char *cp;
1263 server->data += ret;
1264 server->buffer[server->data] = '\0';
1266 cp = find_crlf (server->buffer + search_offset);
1267 if (cp)
1269 int data_used = (cp + 2) - server->buffer;
1270 *cp = '\0';
1271 server->data -= data_used;
1272 server->buffer_index = data_used;
1274 if (pop_debug)
1275 fprintf (stderr, "<<< %s\n", server->buffer);
1276 return (server->buffer);
1278 search_offset += ret;
1282 /* NOTREACHED */
1286 * Function: sendline
1288 * Purpose: Sends a line of text to the POP server. The line of text
1289 * passed into this function should NOT have the carriage return
1290 * and linefeed on the end of it. Periods at beginnings of lines
1291 * will NOT be treated specially by this function.
1293 * Arguments:
1294 * server The server to which to send the text.
1295 * line The line of text to send.
1297 * Return value: Upon successful completion, a value of 0 will be
1298 * returned. Otherwise, a non-zero value will be returned, and
1299 * an error will be copied into pop_error.
1301 * Side effects: Closes the connection on error.
1303 static int
1304 sendline (server, line)
1305 popserver server;
1306 char *line;
1308 #define SENDLINE_ERROR "Error writing to POP server: "
1309 int ret;
1311 ret = fullwrite (server->file, line, strlen (line));
1312 if (ret >= 0)
1313 { /* 0 indicates that a blank line was written */
1314 ret = fullwrite (server->file, "\r\n", 2);
1317 if (ret < 0)
1319 pop_trash (server);
1320 strcpy (pop_error, SENDLINE_ERROR);
1321 strncat (pop_error, strerror (errno),
1322 ERROR_MAX - sizeof (SENDLINE_ERROR));
1323 return (ret);
1326 if (pop_debug)
1327 fprintf (stderr, ">>> %s\n", line);
1329 return (0);
1333 * Procedure: fullwrite
1335 * Purpose: Just like write, but keeps trying until the entire string
1336 * has been written.
1338 * Return value: Same as write. Pop_error is not set.
1340 static int
1341 fullwrite (fd, buf, nbytes)
1342 int fd;
1343 char *buf;
1344 int nbytes;
1346 char *cp;
1347 int ret;
1349 cp = buf;
1350 while ((ret = write (fd, cp, nbytes)) > 0)
1352 cp += ret;
1353 nbytes -= ret;
1356 return (ret);
1360 * Procedure getok
1362 * Purpose: Reads a line from the server. If the return indicator is
1363 * positive, return with a zero exit status. If not, return with
1364 * a negative exit status.
1366 * Arguments:
1367 * server The server to read from.
1369 * Returns: 0 for success, else for failure and puts error in pop_error.
1371 * Side effects: On failure, may make the connection unuseable.
1373 static int
1374 getok (server)
1375 popserver server;
1377 char *fromline;
1379 if (! (fromline = getline (server)))
1381 return (-1);
1384 if (! strncmp (fromline, "+OK", 3))
1385 return (0);
1386 else if (! strncmp (fromline, "-ERR", 4))
1388 strncpy (pop_error, fromline, ERROR_MAX);
1389 pop_error[ERROR_MAX-1] = '\0';
1390 return (-1);
1392 else
1394 strcpy (pop_error,
1395 "Unexpected response from server; expecting +OK or -ERR");
1396 pop_trash (server);
1397 return (-1);
1401 #if 0
1403 * Function: gettermination
1405 * Purpose: Gets the next line and verifies that it is a termination
1406 * line (nothing but a dot).
1408 * Return value: 0 on success, non-zero with pop_error set on error.
1410 * Side effects: Closes the connection on error.
1412 static int
1413 gettermination (server)
1414 popserver server;
1416 char *fromserver;
1418 fromserver = getline (server);
1419 if (! fromserver)
1420 return (-1);
1422 if (strcmp (fromserver, "."))
1424 strcpy (pop_error,
1425 "Unexpected response from server in gettermination");
1426 pop_trash (server);
1427 return (-1);
1430 return (0);
1432 #endif
1435 * Function pop_close
1437 * Purpose: Close a pop connection, sending a "RSET" command to try to
1438 * preserve any changes that were made and a "QUIT" command to
1439 * try to get the server to quit, but ignoring any responses that
1440 * are received.
1442 * Side effects: The server is unuseable after this function returns.
1443 * Changes made to the maildrop since the session was started (or
1444 * since the last pop_reset) may be lost.
1446 void
1447 pop_close (server)
1448 popserver server;
1450 pop_trash (server);
1451 free ((char *) server);
1453 return;
1457 * Function: pop_trash
1459 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
1460 * memory associated with the server. It is legal to call
1461 * pop_close or pop_quit after this function has been called.
1463 static void
1464 pop_trash (server)
1465 popserver server;
1467 if (server->file >= 0)
1469 sendline (server, "RSET");
1470 sendline (server, "QUIT");
1472 close (server->file);
1473 server->file = -1;
1474 if (server->buffer)
1476 free (server->buffer);
1477 server->buffer = 0;
1482 /* Return a pointer to the first CRLF in IN_STRING,
1483 or 0 if it does not contain one. */
1485 static char *
1486 find_crlf (in_string)
1487 char *in_string;
1489 while (1)
1491 if (! *in_string)
1492 return (0);
1493 else if (*in_string == '\r')
1495 if (*++in_string == '\n')
1496 return (in_string - 1);
1498 else
1499 in_string++;
1501 /* NOTREACHED */
1504 #endif /* MAIL_USE_POP */