Mercurial > emacs
annotate lib-src/pop.c @ 92296:28f0a7903f8b
(Info-goto-node): Remove declaration.
(org-info): Use info rather than Info-goto-node.
author | Glenn Morris <rgm@gnu.org> |
---|---|
date | Thu, 28 Feb 2008 03:48:53 +0000 |
parents | 9ed7610349d3 |
children | 3a4bc081639c |
rev | line source |
---|---|
9158 | 1 /* pop.c: client routines for talking to a POP3-protocol post-office server |
75250
6d19c76d81c5
Update copyright for years from Emacs 21 to present (mainly adding
Glenn Morris <rgm@gnu.org>
parents:
70857
diff
changeset
|
2 Copyright (C) 1991, 1993, 1996, 1997, 1999, 2001, 2002, 2003, 2004, |
79748 | 3 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
9158 | 4 Written by Jonathan Kamens, jik@security.ov.com. |
5 | |
6 This file is part of GNU Emacs. | |
7 | |
8 GNU Emacs is free software; you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
78257
1f2482de3237
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
75250
diff
changeset
|
10 the Free Software Foundation; either version 3, or (at your option) |
9158 | 11 any later version. |
12 | |
13 GNU Emacs is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with GNU Emacs; see the file COPYING. If not, write to | |
64083 | 20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
21 Boston, MA 02110-1301, USA. */ | |
9158 | 22 |
9613
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
23 #ifdef HAVE_CONFIG_H |
42412 | 24 #include <config.h> |
9613
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
25 #else |
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
26 #define MAIL_USE_POP |
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
27 #endif |
9592
132798b0352b
Include ../src/config.h.
Richard M. Stallman <rms@gnu.org>
parents:
9591
diff
changeset
|
28 |
9613
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
29 #ifdef MAIL_USE_POP |
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
30 |
9158 | 31 #include <sys/types.h> |
15102
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
32 #ifdef WINDOWSNT |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
33 #include "ntlib.h" |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
34 #include <winsock.h> |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
35 #undef SOCKET_ERROR |
15108
442458addd50
(SEND, RECV): Renamed from send, recv.
Richard M. Stallman <rms@gnu.org>
parents:
15102
diff
changeset
|
36 #define RECV(s,buf,len,flags) recv(s,buf,len,flags) |
442458addd50
(SEND, RECV): Renamed from send, recv.
Richard M. Stallman <rms@gnu.org>
parents:
15102
diff
changeset
|
37 #define SEND(s,buf,len,flags) send(s,buf,len,flags) |
442458addd50
(SEND, RECV): Renamed from send, recv.
Richard M. Stallman <rms@gnu.org>
parents:
15102
diff
changeset
|
38 #define CLOSESOCKET(s) closesocket(s) |
15102
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
39 #else |
9158 | 40 #include <netinet/in.h> |
41 #include <sys/socket.h> | |
15108
442458addd50
(SEND, RECV): Renamed from send, recv.
Richard M. Stallman <rms@gnu.org>
parents:
15102
diff
changeset
|
42 #define RECV(s,buf,len,flags) read(s,buf,len) |
442458addd50
(SEND, RECV): Renamed from send, recv.
Richard M. Stallman <rms@gnu.org>
parents:
15102
diff
changeset
|
43 #define SEND(s,buf,len,flags) write(s,buf,len) |
442458addd50
(SEND, RECV): Renamed from send, recv.
Richard M. Stallman <rms@gnu.org>
parents:
15102
diff
changeset
|
44 #define CLOSESOCKET(s) close(s) |
15102
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
45 #endif |
9158 | 46 #include <pop.h> |
9600
f0d3266e07d7
Fix mismatch in conditionals.
Richard M. Stallman <rms@gnu.org>
parents:
9594
diff
changeset
|
47 |
9158 | 48 #ifdef sun |
49 #include <malloc.h> | |
9613
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
50 #endif /* sun */ |
9600
f0d3266e07d7
Fix mismatch in conditionals.
Richard M. Stallman <rms@gnu.org>
parents:
9594
diff
changeset
|
51 |
9158 | 52 #ifdef HESIOD |
53 #include <hesiod.h> | |
54 /* | |
55 * It really shouldn't be necessary to put this declaration here, but | |
56 * the version of hesiod.h that Athena has installed in release 7.2 | |
57 * doesn't declare this function; I don't know if the 7.3 version of | |
58 * hesiod.h does. | |
59 */ | |
60 extern struct servent *hes_getservbyname (/* char *, char * */); | |
61 #endif | |
9600
f0d3266e07d7
Fix mismatch in conditionals.
Richard M. Stallman <rms@gnu.org>
parents:
9594
diff
changeset
|
62 |
9158 | 63 #include <pwd.h> |
64 #include <netdb.h> | |
65 #include <errno.h> | |
66 #include <stdio.h> | |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
67 #ifdef STDC_HEADERS |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
68 #include <string.h> |
19978
83050d7e3f64
Use system header files instead of declaring C-library
Karl Heuer <kwzh@gnu.org>
parents:
19018
diff
changeset
|
69 #define index strchr |
83050d7e3f64
Use system header files instead of declaring C-library
Karl Heuer <kwzh@gnu.org>
parents:
19018
diff
changeset
|
70 #endif |
83050d7e3f64
Use system header files instead of declaring C-library
Karl Heuer <kwzh@gnu.org>
parents:
19018
diff
changeset
|
71 #ifdef HAVE_UNISTD_H |
83050d7e3f64
Use system header files instead of declaring C-library
Karl Heuer <kwzh@gnu.org>
parents:
19018
diff
changeset
|
72 #include <unistd.h> |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
73 #endif |
9600
f0d3266e07d7
Fix mismatch in conditionals.
Richard M. Stallman <rms@gnu.org>
parents:
9594
diff
changeset
|
74 |
9158 | 75 #ifdef KERBEROS |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
76 # ifdef HAVE_KRB5_H |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
77 # include <krb5.h> |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
78 # endif |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
79 # ifdef HAVE_KRB_H |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
80 # include <krb.h> |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
81 # else |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
82 # ifdef HAVE_KERBEROSIV_KRB_H |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
83 # include <kerberosIV/krb.h> |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
84 # else |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
85 # ifdef HAVE_KERBEROS_KRB_H |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
86 # include <kerberos/krb.h> |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
87 # endif |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
88 # endif |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
89 # endif |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
90 # ifdef HAVE_COM_ERR_H |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
91 # include <com_err.h> |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
92 # endif |
9613
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
93 #endif /* KERBEROS */ |
9158 | 94 |
9613
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
95 #ifdef KERBEROS |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
96 #ifndef KERBEROS5 |
9158 | 97 extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *, |
9613
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
98 u_long, MSG_DAT *, CREDENTIALS *, Key_schedule, |
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
99 struct sockaddr_in *, struct sockaddr_in *, |
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
100 char * */); |
9158 | 101 extern char *krb_realmofhost (/* char * */); |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
102 #endif /* ! KERBEROS5 */ |
9613
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
103 #endif /* KERBEROS */ |
9158 | 104 |
15102
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
105 #ifndef WINDOWSNT |
9613
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
106 #if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H) |
9158 | 107 extern int h_errno; |
108 #endif | |
15102
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
109 #endif |
9158 | 110 |
49053
2990b2f8bfb5
(__P): Renamed from _P to avoid problems on Cygwin.
Kim F. Storm <storm@cua.dk>
parents:
45328
diff
changeset
|
111 #ifndef __P |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
112 # ifdef __STDC__ |
49053
2990b2f8bfb5
(__P): Renamed from _P to avoid problems on Cygwin.
Kim F. Storm <storm@cua.dk>
parents:
45328
diff
changeset
|
113 # define __P(a) a |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
114 # else |
49053
2990b2f8bfb5
(__P): Renamed from _P to avoid problems on Cygwin.
Kim F. Storm <storm@cua.dk>
parents:
45328
diff
changeset
|
115 # define __P(a) () |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
116 # endif /* __STDC__ */ |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
117 #endif /* ! __P */ |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
118 |
49053
2990b2f8bfb5
(__P): Renamed from _P to avoid problems on Cygwin.
Kim F. Storm <storm@cua.dk>
parents:
45328
diff
changeset
|
119 static int socket_connection __P((char *, int)); |
2990b2f8bfb5
(__P): Renamed from _P to avoid problems on Cygwin.
Kim F. Storm <storm@cua.dk>
parents:
45328
diff
changeset
|
120 static int pop_getline __P((popserver, char **)); |
2990b2f8bfb5
(__P): Renamed from _P to avoid problems on Cygwin.
Kim F. Storm <storm@cua.dk>
parents:
45328
diff
changeset
|
121 static int sendline __P((popserver, char *)); |
2990b2f8bfb5
(__P): Renamed from _P to avoid problems on Cygwin.
Kim F. Storm <storm@cua.dk>
parents:
45328
diff
changeset
|
122 static int fullwrite __P((int, char *, int)); |
2990b2f8bfb5
(__P): Renamed from _P to avoid problems on Cygwin.
Kim F. Storm <storm@cua.dk>
parents:
45328
diff
changeset
|
123 static int getok __P((popserver)); |
9158 | 124 #if 0 |
49053
2990b2f8bfb5
(__P): Renamed from _P to avoid problems on Cygwin.
Kim F. Storm <storm@cua.dk>
parents:
45328
diff
changeset
|
125 static int gettermination __P((popserver)); |
9158 | 126 #endif |
49053
2990b2f8bfb5
(__P): Renamed from _P to avoid problems on Cygwin.
Kim F. Storm <storm@cua.dk>
parents:
45328
diff
changeset
|
127 static void pop_trash __P((popserver)); |
2990b2f8bfb5
(__P): Renamed from _P to avoid problems on Cygwin.
Kim F. Storm <storm@cua.dk>
parents:
45328
diff
changeset
|
128 static char *find_crlf __P((char *, int)); |
9591
0774e217e8aa
Don't declare malloc, realloc, free.
Richard M. Stallman <rms@gnu.org>
parents:
9158
diff
changeset
|
129 |
29047 | 130 #define ERROR_MAX 160 /* a pretty arbitrary size, but needs |
29784 | 131 to be bigger than the original |
132 value of 80 */ | |
9158 | 133 #define POP_PORT 110 |
134 #define KPOP_PORT 1109 | |
15102
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
135 #define POP_SERVICE "pop3" /* we don't want the POP2 port! */ |
9158 | 136 #ifdef KERBEROS |
70857
d2542ee8a5c9
(pop_open, socket_connection, KPOP_SERVICE): Added
Francesco Potortì <pot@gnu.org>
parents:
68647
diff
changeset
|
137 #define KPOP_SERVICE "kpop" /* never used: look for 20060515 to see why */ |
9158 | 138 #endif |
139 | |
140 char pop_error[ERROR_MAX]; | |
141 int pop_debug = 0; | |
142 | |
143 #ifndef min | |
144 #define min(a,b) (((a) < (b)) ? (a) : (b)) | |
145 #endif | |
146 | |
147 /* | |
148 * Function: pop_open (char *host, char *username, char *password, | |
149 * int flags) | |
150 * | |
151 * Purpose: Establishes a connection with a post-office server, and | |
152 * completes the authorization portion of the session. | |
153 * | |
154 * Arguments: | |
155 * host The server host with which the connection should be | |
156 * established. Optional. If omitted, internal | |
157 * heuristics will be used to determine the server host, | |
158 * if possible. | |
159 * username | |
160 * The username of the mail-drop to access. Optional. | |
161 * If omitted, internal heuristics will be used to | |
162 * determine the username, if possible. | |
163 * password | |
164 * The password to use for authorization. If omitted, | |
165 * internal heuristics will be used to determine the | |
166 * password, if possible. | |
167 * flags A bit mask containing flags controlling certain | |
168 * functions of the routine. Valid flags are defined in | |
169 * the file pop.h | |
170 * | |
171 * Return value: Upon successful establishment of a connection, a | |
172 * non-null popserver will be returned. Otherwise, null will be | |
173 * returned, and the string variable pop_error will contain an | |
174 * explanation of the error. | |
175 */ | |
176 popserver | |
177 pop_open (host, username, password, flags) | |
178 char *host; | |
179 char *username; | |
180 char *password; | |
181 int flags; | |
182 { | |
183 int sock; | |
184 popserver server; | |
185 | |
186 /* Determine the user name */ | |
187 if (! username) | |
188 { | |
189 username = getenv ("USER"); | |
190 if (! (username && *username)) | |
191 { | |
192 username = getlogin (); | |
193 if (! (username && *username)) | |
194 { | |
195 struct passwd *passwd; | |
196 passwd = getpwuid (getuid ()); | |
197 if (passwd && passwd->pw_name && *passwd->pw_name) | |
198 { | |
199 username = passwd->pw_name; | |
200 } | |
201 else | |
202 { | |
203 strcpy (pop_error, "Could not determine username"); | |
204 return (0); | |
205 } | |
206 } | |
207 } | |
208 } | |
209 | |
210 /* | |
211 * Determine the mail host. | |
212 */ | |
213 | |
214 if (! host) | |
215 { | |
216 host = getenv ("MAILHOST"); | |
217 } | |
218 | |
219 #ifdef HESIOD | |
220 if ((! host) && (! (flags & POP_NO_HESIOD))) | |
221 { | |
222 struct hes_postoffice *office; | |
223 office = hes_getmailhost (username); | |
224 if (office && office->po_type && (! strcmp (office->po_type, "POP")) | |
225 && office->po_name && *office->po_name && office->po_host | |
226 && *office->po_host) | |
227 { | |
228 host = office->po_host; | |
229 username = office->po_name; | |
230 } | |
231 } | |
232 #endif | |
233 | |
234 #ifdef MAILHOST | |
235 if (! host) | |
236 { | |
237 host = MAILHOST; | |
238 } | |
239 #endif | |
240 | |
241 if (! host) | |
242 { | |
243 strcpy (pop_error, "Could not determine POP server"); | |
244 return (0); | |
245 } | |
246 | |
247 /* Determine the password */ | |
22236 | 248 #ifdef KERBEROS |
249 #define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS)) | |
9158 | 250 #else |
22236 | 251 #define DONT_NEED_PASSWORD 0 |
9158 | 252 #endif |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49053
diff
changeset
|
253 |
9158 | 254 if ((! password) && (! DONT_NEED_PASSWORD)) |
255 { | |
256 if (! (flags & POP_NO_GETPASS)) | |
257 { | |
258 password = getpass ("Enter POP password:"); | |
259 } | |
260 if (! password) | |
261 { | |
262 strcpy (pop_error, "Could not determine POP password"); | |
263 return (0); | |
264 } | |
265 } | |
70857
d2542ee8a5c9
(pop_open, socket_connection, KPOP_SERVICE): Added
Francesco Potortì <pot@gnu.org>
parents:
68647
diff
changeset
|
266 if (password) /* always true, detected 20060515 */ |
22236 | 267 flags |= POP_NO_KERBEROS; |
9158 | 268 else |
70857
d2542ee8a5c9
(pop_open, socket_connection, KPOP_SERVICE): Added
Francesco Potortì <pot@gnu.org>
parents:
68647
diff
changeset
|
269 password = username; /* dead code, detected 20060515 */ |
d2542ee8a5c9
(pop_open, socket_connection, KPOP_SERVICE): Added
Francesco Potortì <pot@gnu.org>
parents:
68647
diff
changeset
|
270 /** "kpop" service is never used: look for 20060515 to see why **/ |
9158 | 271 |
272 sock = socket_connection (host, flags); | |
273 if (sock == -1) | |
274 return (0); | |
275 | |
276 server = (popserver) malloc (sizeof (struct _popserver)); | |
277 if (! server) | |
278 { | |
279 strcpy (pop_error, "Out of memory in pop_open"); | |
280 return (0); | |
281 } | |
282 server->buffer = (char *) malloc (GETLINE_MIN); | |
283 if (! server->buffer) | |
284 { | |
285 strcpy (pop_error, "Out of memory in pop_open"); | |
286 free ((char *) server); | |
287 return (0); | |
288 } | |
42439
d8a417105504
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
42412
diff
changeset
|
289 |
9158 | 290 server->file = sock; |
291 server->data = 0; | |
292 server->buffer_index = 0; | |
293 server->buffer_size = GETLINE_MIN; | |
294 server->in_multi = 0; | |
15102
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
295 server->trash_started = 0; |
9158 | 296 |
297 if (getok (server)) | |
298 return (0); | |
299 | |
300 /* | |
301 * I really shouldn't use the pop_error variable like this, but.... | |
302 */ | |
303 if (strlen (username) > ERROR_MAX - 6) | |
304 { | |
305 pop_close (server); | |
306 strcpy (pop_error, | |
307 "Username too long; recompile pop.c with larger ERROR_MAX"); | |
308 return (0); | |
309 } | |
310 sprintf (pop_error, "USER %s", username); | |
311 | |
312 if (sendline (server, pop_error) || getok (server)) | |
313 { | |
314 return (0); | |
315 } | |
316 | |
317 if (strlen (password) > ERROR_MAX - 6) | |
318 { | |
319 pop_close (server); | |
320 strcpy (pop_error, | |
321 "Password too long; recompile pop.c with larger ERROR_MAX"); | |
322 return (0); | |
323 } | |
324 sprintf (pop_error, "PASS %s", password); | |
325 | |
326 if (sendline (server, pop_error) || getok (server)) | |
327 { | |
328 return (0); | |
329 } | |
330 | |
331 return (server); | |
332 } | |
333 | |
334 /* | |
335 * Function: pop_stat | |
336 * | |
337 * Purpose: Issue the STAT command to the server and return (in the | |
338 * value parameters) the number of messages in the maildrop and | |
339 * the total size of the maildrop. | |
340 * | |
341 * Return value: 0 on success, or non-zero with an error in pop_error | |
342 * in failure. | |
343 * | |
344 * Side effects: On failure, may make further operations on the | |
345 * connection impossible. | |
346 */ | |
347 int | |
348 pop_stat (server, count, size) | |
349 popserver server; | |
350 int *count; | |
351 int *size; | |
352 { | |
353 char *fromserver; | |
79817
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
354 char *end_ptr; |
9158 | 355 |
356 if (server->in_multi) | |
357 { | |
358 strcpy (pop_error, "In multi-line query in pop_stat"); | |
359 return (-1); | |
360 } | |
42439
d8a417105504
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
42412
diff
changeset
|
361 |
21295
f399e51d6d11
(pop_getline): Renamed from getline.
Richard M. Stallman <rms@gnu.org>
parents:
20418
diff
changeset
|
362 if (sendline (server, "STAT") || (pop_getline (server, &fromserver) < 0)) |
9158 | 363 return (-1); |
364 | |
365 if (strncmp (fromserver, "+OK ", 4)) | |
366 { | |
367 if (0 == strncmp (fromserver, "-ERR", 4)) | |
368 { | |
369 strncpy (pop_error, fromserver, ERROR_MAX); | |
370 } | |
371 else | |
372 { | |
373 strcpy (pop_error, | |
374 "Unexpected response from POP server in pop_stat"); | |
375 pop_trash (server); | |
376 } | |
377 return (-1); | |
378 } | |
379 | |
79817
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
380 errno = 0; |
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
381 *count = strtol (&fromserver[4], &end_ptr, 10); |
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
382 /* Check validity of string-to-integer conversion. */ |
79905
de27c7b1b268
(pop_stat, pop_last): Fix last fix.
Chong Yidong <cyd@stupidchicken.com>
parents:
79817
diff
changeset
|
383 if (fromserver + 4 == end_ptr || *end_ptr != ' ' || errno) |
79817
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
384 { |
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
385 strcpy (pop_error, "Unexpected response from POP server in pop_stat"); |
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
386 pop_trash (server); |
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
387 return (-1); |
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
388 } |
42439
d8a417105504
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
42412
diff
changeset
|
389 |
79905
de27c7b1b268
(pop_stat, pop_last): Fix last fix.
Chong Yidong <cyd@stupidchicken.com>
parents:
79817
diff
changeset
|
390 fromserver = end_ptr; |
9158 | 391 |
79817
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
392 errno = 0; |
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
393 *size = strtol (fromserver + 1, &end_ptr, 10); |
79905
de27c7b1b268
(pop_stat, pop_last): Fix last fix.
Chong Yidong <cyd@stupidchicken.com>
parents:
79817
diff
changeset
|
394 if (fromserver + 1 == end_ptr || errno) |
79817
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
395 { |
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
396 strcpy (pop_error, "Unexpected response from POP server in pop_stat"); |
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
397 pop_trash (server); |
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
398 return (-1); |
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
399 } |
9158 | 400 |
401 return (0); | |
402 } | |
403 | |
404 /* | |
405 * Function: pop_list | |
406 * | |
407 * Purpose: Performs the POP "list" command and returns (in value | |
408 * parameters) two malloc'd zero-terminated arrays -- one of | |
409 * message IDs, and a parallel one of sizes. | |
410 * | |
411 * Arguments: | |
412 * server The pop connection to talk to. | |
413 * message The number of the one message about which to get | |
414 * information, or 0 to get information about all | |
415 * messages. | |
416 * | |
417 * Return value: 0 on success, non-zero with error in pop_error on | |
418 * failure. | |
419 * | |
420 * Side effects: On failure, may make further operations on the | |
421 * connection impossible. | |
422 */ | |
423 int | |
424 pop_list (server, message, IDs, sizes) | |
425 popserver server; | |
426 int message; | |
427 int **IDs; | |
428 int **sizes; | |
429 { | |
430 int how_many, i; | |
431 char *fromserver; | |
432 | |
433 if (server->in_multi) | |
434 { | |
435 strcpy (pop_error, "In multi-line query in pop_list"); | |
436 return (-1); | |
437 } | |
438 | |
439 if (message) | |
440 how_many = 1; | |
441 else | |
442 { | |
443 int count, size; | |
444 if (pop_stat (server, &count, &size)) | |
445 return (-1); | |
446 how_many = count; | |
447 } | |
448 | |
449 *IDs = (int *) malloc ((how_many + 1) * sizeof (int)); | |
450 *sizes = (int *) malloc ((how_many + 1) * sizeof (int)); | |
451 if (! (*IDs && *sizes)) | |
452 { | |
453 strcpy (pop_error, "Out of memory in pop_list"); | |
454 return (-1); | |
455 } | |
456 | |
457 if (message) | |
458 { | |
459 sprintf (pop_error, "LIST %d", message); | |
460 if (sendline (server, pop_error)) | |
461 { | |
462 free ((char *) *IDs); | |
463 free ((char *) *sizes); | |
464 return (-1); | |
465 } | |
21295
f399e51d6d11
(pop_getline): Renamed from getline.
Richard M. Stallman <rms@gnu.org>
parents:
20418
diff
changeset
|
466 if (pop_getline (server, &fromserver) < 0) |
9158 | 467 { |
468 free ((char *) *IDs); | |
469 free ((char *) *sizes); | |
470 return (-1); | |
471 } | |
472 if (strncmp (fromserver, "+OK ", 4)) | |
473 { | |
474 if (! strncmp (fromserver, "-ERR", 4)) | |
475 strncpy (pop_error, fromserver, ERROR_MAX); | |
476 else | |
477 { | |
478 strcpy (pop_error, | |
479 "Unexpected response from server in pop_list"); | |
480 pop_trash (server); | |
481 } | |
482 free ((char *) *IDs); | |
483 free ((char *) *sizes); | |
484 return (-1); | |
485 } | |
486 (*IDs)[0] = atoi (&fromserver[4]); | |
487 fromserver = index (&fromserver[4], ' '); | |
488 if (! fromserver) | |
489 { | |
490 strcpy (pop_error, | |
491 "Badly formatted response from server in pop_list"); | |
492 pop_trash (server); | |
493 free ((char *) *IDs); | |
494 free ((char *) *sizes); | |
495 return (-1); | |
496 } | |
497 (*sizes)[0] = atoi (fromserver); | |
498 (*IDs)[1] = (*sizes)[1] = 0; | |
499 return (0); | |
500 } | |
501 else | |
502 { | |
503 if (pop_multi_first (server, "LIST", &fromserver)) | |
504 { | |
505 free ((char *) *IDs); | |
506 free ((char *) *sizes); | |
507 return (-1); | |
508 } | |
509 for (i = 0; i < how_many; i++) | |
510 { | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
511 if (pop_multi_next (server, &fromserver) <= 0) |
9158 | 512 { |
513 free ((char *) *IDs); | |
514 free ((char *) *sizes); | |
515 return (-1); | |
516 } | |
517 (*IDs)[i] = atoi (fromserver); | |
518 fromserver = index (fromserver, ' '); | |
519 if (! fromserver) | |
520 { | |
521 strcpy (pop_error, | |
522 "Badly formatted response from server in pop_list"); | |
523 free ((char *) *IDs); | |
524 free ((char *) *sizes); | |
525 pop_trash (server); | |
526 return (-1); | |
527 } | |
528 (*sizes)[i] = atoi (fromserver); | |
529 } | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
530 if (pop_multi_next (server, &fromserver) < 0) |
9158 | 531 { |
532 free ((char *) *IDs); | |
533 free ((char *) *sizes); | |
534 return (-1); | |
535 } | |
536 else if (fromserver) | |
537 { | |
538 strcpy (pop_error, | |
539 "Too many response lines from server in pop_list"); | |
540 free ((char *) *IDs); | |
541 free ((char *) *sizes); | |
542 return (-1); | |
543 } | |
544 (*IDs)[i] = (*sizes)[i] = 0; | |
545 return (0); | |
546 } | |
547 } | |
548 | |
549 /* | |
550 * Function: pop_retrieve | |
551 * | |
552 * Purpose: Retrieve a specified message from the maildrop. | |
553 * | |
554 * Arguments: | |
555 * server The server to retrieve from. | |
556 * message The message number to retrieve. | |
557 * markfrom | |
558 * If true, then mark the string "From " at the beginning | |
559 * of lines with '>'. | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
560 * msg_buf Output parameter to which a buffer containing the |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
561 * message is assigned. |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49053
diff
changeset
|
562 * |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
563 * Return value: The number of bytes in msg_buf, which may contain |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
564 * embedded nulls, not including its final null, or -1 on error |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
565 * with pop_error set. |
9158 | 566 * |
567 * Side effects: May kill connection on error. | |
568 */ | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
569 int |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
570 pop_retrieve (server, message, markfrom, msg_buf) |
9158 | 571 popserver server; |
572 int message; | |
573 int markfrom; | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
574 char **msg_buf; |
9158 | 575 { |
576 int *IDs, *sizes, bufsize, fromcount = 0, cp = 0; | |
577 char *ptr, *fromserver; | |
578 int ret; | |
579 | |
580 if (server->in_multi) | |
581 { | |
582 strcpy (pop_error, "In multi-line query in pop_retrieve"); | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
583 return (-1); |
9158 | 584 } |
585 | |
586 if (pop_list (server, message, &IDs, &sizes)) | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
587 return (-1); |
9158 | 588 |
589 if (pop_retrieve_first (server, message, &fromserver)) | |
590 { | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
591 return (-1); |
9158 | 592 } |
593 | |
594 /* | |
595 * The "5" below is an arbitrary constant -- I assume that if | |
596 * there are "From" lines in the text to be marked, there | |
597 * probably won't be more than 5 of them. If there are, I | |
598 * allocate more space for them below. | |
599 */ | |
600 bufsize = sizes[0] + (markfrom ? 5 : 0); | |
14238
b55e97028105
(pop_retrieve, getline): Avoid type clashes.
Karl Heuer <kwzh@gnu.org>
parents:
14186
diff
changeset
|
601 ptr = (char *)malloc (bufsize); |
9158 | 602 free ((char *) IDs); |
603 free ((char *) sizes); | |
604 | |
605 if (! ptr) | |
606 { | |
607 strcpy (pop_error, "Out of memory in pop_retrieve"); | |
608 pop_retrieve_flush (server); | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
609 return (-1); |
9158 | 610 } |
611 | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
612 while ((ret = pop_retrieve_next (server, &fromserver)) >= 0) |
9158 | 613 { |
614 if (! fromserver) | |
615 { | |
616 ptr[cp] = '\0'; | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
617 *msg_buf = ptr; |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
618 return (cp); |
9158 | 619 } |
620 if (markfrom && fromserver[0] == 'F' && fromserver[1] == 'r' && | |
621 fromserver[2] == 'o' && fromserver[3] == 'm' && | |
622 fromserver[4] == ' ') | |
623 { | |
624 if (++fromcount == 5) | |
625 { | |
626 bufsize += 5; | |
14238
b55e97028105
(pop_retrieve, getline): Avoid type clashes.
Karl Heuer <kwzh@gnu.org>
parents:
14186
diff
changeset
|
627 ptr = (char *)realloc (ptr, bufsize); |
9158 | 628 if (! ptr) |
629 { | |
630 strcpy (pop_error, "Out of memory in pop_retrieve"); | |
631 pop_retrieve_flush (server); | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
632 return (-1); |
9158 | 633 } |
634 fromcount = 0; | |
635 } | |
636 ptr[cp++] = '>'; | |
637 } | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
638 bcopy (fromserver, &ptr[cp], ret); |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
639 cp += ret; |
9158 | 640 ptr[cp++] = '\n'; |
641 } | |
642 | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
643 free (ptr); |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
644 return (-1); |
49600
23a1cea22d13
Trailing whitespace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49053
diff
changeset
|
645 } |
9158 | 646 |
647 int | |
648 pop_retrieve_first (server, message, response) | |
649 popserver server; | |
650 int message; | |
651 char **response; | |
652 { | |
653 sprintf (pop_error, "RETR %d", message); | |
654 return (pop_multi_first (server, pop_error, response)); | |
655 } | |
656 | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
657 /* |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
658 Returns a negative number on error, 0 to indicate that the data has |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
659 all been read (i.e., the server has returned a "." termination |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
660 line), or a positive number indicating the number of bytes in the |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
661 returned buffer (which is null-terminated and may contain embedded |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
662 nulls, but the returned bytecount doesn't include the final null). |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
663 */ |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
664 |
9158 | 665 int |
666 pop_retrieve_next (server, line) | |
667 popserver server; | |
668 char **line; | |
669 { | |
670 return (pop_multi_next (server, line)); | |
671 } | |
672 | |
673 int | |
674 pop_retrieve_flush (server) | |
675 popserver server; | |
676 { | |
677 return (pop_multi_flush (server)); | |
678 } | |
679 | |
680 int | |
681 pop_top_first (server, message, lines, response) | |
682 popserver server; | |
683 int message, lines; | |
684 char **response; | |
685 { | |
686 sprintf (pop_error, "TOP %d %d", message, lines); | |
687 return (pop_multi_first (server, pop_error, response)); | |
688 } | |
689 | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
690 /* |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
691 Returns a negative number on error, 0 to indicate that the data has |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
692 all been read (i.e., the server has returned a "." termination |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
693 line), or a positive number indicating the number of bytes in the |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
694 returned buffer (which is null-terminated and may contain embedded |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
695 nulls, but the returned bytecount doesn't include the final null). |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
696 */ |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
697 |
9158 | 698 int |
699 pop_top_next (server, line) | |
700 popserver server; | |
701 char **line; | |
702 { | |
703 return (pop_multi_next (server, line)); | |
704 } | |
705 | |
706 int | |
707 pop_top_flush (server) | |
708 popserver server; | |
709 { | |
710 return (pop_multi_flush (server)); | |
711 } | |
712 | |
713 int | |
714 pop_multi_first (server, command, response) | |
715 popserver server; | |
716 char *command; | |
717 char **response; | |
718 { | |
719 if (server->in_multi) | |
720 { | |
721 strcpy (pop_error, | |
722 "Already in multi-line query in pop_multi_first"); | |
723 return (-1); | |
724 } | |
725 | |
21295
f399e51d6d11
(pop_getline): Renamed from getline.
Richard M. Stallman <rms@gnu.org>
parents:
20418
diff
changeset
|
726 if (sendline (server, command) || (pop_getline (server, response) < 0)) |
9158 | 727 { |
728 return (-1); | |
729 } | |
730 | |
731 if (0 == strncmp (*response, "-ERR", 4)) | |
732 { | |
733 strncpy (pop_error, *response, ERROR_MAX); | |
734 return (-1); | |
735 } | |
736 else if (0 == strncmp (*response, "+OK", 3)) | |
737 { | |
738 for (*response += 3; **response == ' '; (*response)++) /* empty */; | |
739 server->in_multi = 1; | |
740 return (0); | |
741 } | |
742 else | |
743 { | |
744 strcpy (pop_error, | |
745 "Unexpected response from server in pop_multi_first"); | |
746 return (-1); | |
747 } | |
748 } | |
749 | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
750 /* |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
751 Read the next line of data from SERVER and place a pointer to it |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
752 into LINE. Return -1 on error, 0 if there are no more lines to read |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
753 (i.e., the server has returned a line containing only "."), or a |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
754 positive number indicating the number of bytes in the LINE buffer |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
755 (not including the final null). The data in that buffer may contain |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
756 embedded nulls, but does not contain the final CRLF. When returning |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
757 0, LINE is set to null. */ |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
758 |
9158 | 759 int |
760 pop_multi_next (server, line) | |
761 popserver server; | |
762 char **line; | |
763 { | |
764 char *fromserver; | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
765 int ret; |
9158 | 766 |
767 if (! server->in_multi) | |
768 { | |
769 strcpy (pop_error, "Not in multi-line query in pop_multi_next"); | |
770 return (-1); | |
771 } | |
772 | |
21295
f399e51d6d11
(pop_getline): Renamed from getline.
Richard M. Stallman <rms@gnu.org>
parents:
20418
diff
changeset
|
773 if ((ret = pop_getline (server, &fromserver)) < 0) |
9158 | 774 { |
775 return (-1); | |
776 } | |
777 | |
778 if (fromserver[0] == '.') | |
779 { | |
780 if (! fromserver[1]) | |
781 { | |
782 *line = 0; | |
783 server->in_multi = 0; | |
784 return (0); | |
785 } | |
786 else | |
787 { | |
788 *line = fromserver + 1; | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
789 return (ret - 1); |
9158 | 790 } |
791 } | |
792 else | |
793 { | |
794 *line = fromserver; | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
795 return (ret); |
9158 | 796 } |
797 } | |
798 | |
799 int | |
800 pop_multi_flush (server) | |
801 popserver server; | |
802 { | |
803 char *line; | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
804 int ret; |
9158 | 805 |
806 if (! server->in_multi) | |
807 { | |
808 return (0); | |
809 } | |
810 | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
811 while ((ret = pop_multi_next (server, &line))) |
9158 | 812 { |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
813 if (ret < 0) |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
814 return (-1); |
9158 | 815 } |
816 | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
817 return (0); |
9158 | 818 } |
819 | |
820 /* Function: pop_delete | |
821 * | |
822 * Purpose: Delete a specified message. | |
823 * | |
824 * Arguments: | |
825 * server Server from which to delete the message. | |
826 * message Message to delete. | |
827 * | |
828 * Return value: 0 on success, non-zero with error in pop_error | |
829 * otherwise. | |
830 */ | |
831 int | |
832 pop_delete (server, message) | |
833 popserver server; | |
834 int message; | |
835 { | |
836 if (server->in_multi) | |
837 { | |
838 strcpy (pop_error, "In multi-line query in pop_delete"); | |
839 return (-1); | |
840 } | |
841 | |
842 sprintf (pop_error, "DELE %d", message); | |
843 | |
844 if (sendline (server, pop_error) || getok (server)) | |
845 return (-1); | |
846 | |
847 return (0); | |
848 } | |
849 | |
850 /* | |
851 * Function: pop_noop | |
852 * | |
853 * Purpose: Send a noop command to the server. | |
854 * | |
855 * Argument: | |
856 * server The server to send to. | |
857 * | |
858 * Return value: 0 on success, non-zero with error in pop_error | |
859 * otherwise. | |
860 * | |
861 * Side effects: Closes connection on error. | |
862 */ | |
863 int | |
864 pop_noop (server) | |
865 popserver server; | |
866 { | |
867 if (server->in_multi) | |
868 { | |
869 strcpy (pop_error, "In multi-line query in pop_noop"); | |
870 return (-1); | |
871 } | |
872 | |
873 if (sendline (server, "NOOP") || getok (server)) | |
874 return (-1); | |
875 | |
876 return (0); | |
877 } | |
878 | |
879 /* | |
880 * Function: pop_last | |
881 * | |
882 * Purpose: Find out the highest seen message from the server. | |
883 * | |
884 * Arguments: | |
885 * server The server. | |
886 * | |
887 * Return value: If successful, the highest seen message, which is | |
888 * greater than or equal to 0. Otherwise, a negative number with | |
889 * the error explained in pop_error. | |
890 * | |
891 * Side effects: Closes the connection on error. | |
892 */ | |
893 int | |
894 pop_last (server) | |
895 popserver server; | |
896 { | |
897 char *fromserver; | |
42439
d8a417105504
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
42412
diff
changeset
|
898 |
9158 | 899 if (server->in_multi) |
900 { | |
901 strcpy (pop_error, "In multi-line query in pop_last"); | |
902 return (-1); | |
903 } | |
904 | |
905 if (sendline (server, "LAST")) | |
906 return (-1); | |
907 | |
21295
f399e51d6d11
(pop_getline): Renamed from getline.
Richard M. Stallman <rms@gnu.org>
parents:
20418
diff
changeset
|
908 if (pop_getline (server, &fromserver) < 0) |
9158 | 909 return (-1); |
910 | |
911 if (! strncmp (fromserver, "-ERR", 4)) | |
912 { | |
913 strncpy (pop_error, fromserver, ERROR_MAX); | |
914 return (-1); | |
915 } | |
916 else if (strncmp (fromserver, "+OK ", 4)) | |
917 { | |
918 strcpy (pop_error, "Unexpected response from server in pop_last"); | |
919 pop_trash (server); | |
920 return (-1); | |
921 } | |
922 else | |
923 { | |
79817
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
924 char *end_ptr; |
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
925 int count; |
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
926 errno = 0; |
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
927 count = strtol (&fromserver[4], &end_ptr, 10); |
79905
de27c7b1b268
(pop_stat, pop_last): Fix last fix.
Chong Yidong <cyd@stupidchicken.com>
parents:
79817
diff
changeset
|
928 if (fromserver + 4 == end_ptr || errno) |
79817
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
929 { |
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
930 strcpy (pop_error, "Unexpected response from server in pop_last"); |
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
931 pop_trash (server); |
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
932 return (-1); |
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
933 } |
dd778207ebb2
(pop_stat, pop_last): Check validity of string-to-integer
Chong Yidong <cyd@stupidchicken.com>
parents:
79748
diff
changeset
|
934 return count; |
9158 | 935 } |
936 } | |
937 | |
938 /* | |
939 * Function: pop_reset | |
940 * | |
941 * Purpose: Reset the server to its initial connect state | |
942 * | |
943 * Arguments: | |
944 * server The server. | |
945 * | |
946 * Return value: 0 for success, non-0 with error in pop_error | |
947 * otherwise. | |
948 * | |
949 * Side effects: Closes the connection on error. | |
950 */ | |
951 int | |
952 pop_reset (server) | |
953 popserver server; | |
954 { | |
955 if (pop_retrieve_flush (server)) | |
956 { | |
957 return (-1); | |
958 } | |
959 | |
960 if (sendline (server, "RSET") || getok (server)) | |
961 return (-1); | |
962 | |
963 return (0); | |
964 } | |
965 | |
966 /* | |
967 * Function: pop_quit | |
968 * | |
969 * Purpose: Quit the connection to the server, | |
970 * | |
971 * Arguments: | |
972 * server The server to quit. | |
973 * | |
974 * Return value: 0 for success, non-zero otherwise with error in | |
975 * pop_error. | |
976 * | |
14036 | 977 * Side Effects: The popserver passed in is unusable after this |
9158 | 978 * function is called, even if an error occurs. |
979 */ | |
980 int | |
981 pop_quit (server) | |
982 popserver server; | |
983 { | |
984 int ret = 0; | |
985 | |
986 if (server->file >= 0) | |
987 { | |
988 if (pop_retrieve_flush (server)) | |
989 { | |
990 ret = -1; | |
991 } | |
992 | |
993 if (sendline (server, "QUIT") || getok (server)) | |
994 { | |
995 ret = -1; | |
996 } | |
997 | |
998 close (server->file); | |
999 } | |
1000 | |
1001 if (server->buffer) | |
1002 free (server->buffer); | |
1003 free ((char *) server); | |
1004 | |
1005 return (ret); | |
1006 } | |
1007 | |
15102
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1008 #ifdef WINDOWSNT |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1009 static int have_winsock = 0; |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1010 #endif |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1011 |
9158 | 1012 /* |
1013 * Function: socket_connection | |
1014 * | |
1015 * Purpose: Opens the network connection with the mail host, without | |
1016 * doing any sort of I/O with it or anything. | |
1017 * | |
1018 * Arguments: | |
1019 * host The host to which to connect. | |
1020 * flags Option flags. | |
42439
d8a417105504
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
42412
diff
changeset
|
1021 * |
9158 | 1022 * Return value: A file descriptor indicating the connection, or -1 |
1023 * indicating failure, in which case an error has been copied | |
1024 * into pop_error. | |
1025 */ | |
1026 static int | |
1027 socket_connection (host, flags) | |
1028 char *host; | |
1029 int flags; | |
1030 { | |
86210
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1031 #ifdef HAVE_GETADDRINFO |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1032 struct addrinfo *res, *it; |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1033 struct addrinfo hints; |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1034 int ret; |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1035 #else /* !HAVE_GETADDRINFO */ |
9158 | 1036 struct hostent *hostent; |
86210
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1037 #endif |
9158 | 1038 struct servent *servent; |
1039 struct sockaddr_in addr; | |
1040 char found_port = 0; | |
1041 char *service; | |
1042 int sock; | |
86235
79cf32b82b23
(socket_connection): Move realhost out of #ifdefs.
Jan Djärv <jan.h.d@swipnet.se>
parents:
86210
diff
changeset
|
1043 char *realhost; |
9158 | 1044 #ifdef KERBEROS |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1045 #ifdef KERBEROS5 |
9158 | 1046 krb5_error_code rem; |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1047 krb5_context kcontext = 0; |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1048 krb5_auth_context auth_context = 0; |
9158 | 1049 krb5_ccache ccdef; |
1050 krb5_principal client, server; | |
1051 krb5_error *err_ret; | |
1052 register char *cp; | |
1053 #else | |
1054 KTEXT ticket; | |
1055 MSG_DAT msg_data; | |
1056 CREDENTIALS cred; | |
1057 Key_schedule schedule; | |
1058 int rem; | |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1059 #endif /* KERBEROS5 */ |
9158 | 1060 #endif /* KERBEROS */ |
1061 | |
1062 int try_count = 0; | |
86210
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1063 int connect_ok; |
9158 | 1064 |
15102
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1065 #ifdef WINDOWSNT |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1066 { |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1067 WSADATA winsockData; |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1068 if (WSAStartup (0x101, &winsockData) == 0) |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1069 have_winsock = 1; |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1070 } |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1071 #endif |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1072 |
9158 | 1073 bzero ((char *) &addr, sizeof (addr)); |
1074 addr.sin_family = AF_INET; | |
1075 | |
70857
d2542ee8a5c9
(pop_open, socket_connection, KPOP_SERVICE): Added
Francesco Potortì <pot@gnu.org>
parents:
68647
diff
changeset
|
1076 /** "kpop" service is never used: look for 20060515 to see why **/ |
9158 | 1077 #ifdef KERBEROS |
1078 service = (flags & POP_NO_KERBEROS) ? POP_SERVICE : KPOP_SERVICE; | |
1079 #else | |
1080 service = POP_SERVICE; | |
1081 #endif | |
1082 | |
1083 #ifdef HESIOD | |
1084 if (! (flags & POP_NO_HESIOD)) | |
1085 { | |
1086 servent = hes_getservbyname (service, "tcp"); | |
1087 if (servent) | |
1088 { | |
1089 addr.sin_port = servent->s_port; | |
1090 found_port = 1; | |
1091 } | |
1092 } | |
1093 #endif | |
1094 if (! found_port) | |
1095 { | |
1096 servent = getservbyname (service, "tcp"); | |
1097 if (servent) | |
1098 { | |
1099 addr.sin_port = servent->s_port; | |
1100 } | |
1101 else | |
1102 { | |
70857
d2542ee8a5c9
(pop_open, socket_connection, KPOP_SERVICE): Added
Francesco Potortì <pot@gnu.org>
parents:
68647
diff
changeset
|
1103 /** "kpop" service is never used: look for 20060515 to see why **/ |
9158 | 1104 #ifdef KERBEROS |
1105 addr.sin_port = htons ((flags & POP_NO_KERBEROS) ? | |
1106 POP_PORT : KPOP_PORT); | |
1107 #else | |
1108 addr.sin_port = htons (POP_PORT); | |
1109 #endif | |
1110 } | |
1111 } | |
1112 | |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1113 #define POP_SOCKET_ERROR "Could not create socket for POP connection: " |
9158 | 1114 |
1115 sock = socket (PF_INET, SOCK_STREAM, 0); | |
1116 if (sock < 0) | |
1117 { | |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1118 strcpy (pop_error, POP_SOCKET_ERROR); |
9158 | 1119 strncat (pop_error, strerror (errno), |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1120 ERROR_MAX - sizeof (POP_SOCKET_ERROR)); |
9158 | 1121 return (-1); |
42439
d8a417105504
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
42412
diff
changeset
|
1122 |
9158 | 1123 } |
1124 | |
86210
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1125 #ifdef HAVE_GETADDRINFO |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1126 memset (&hints, 0, sizeof(hints)); |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1127 hints.ai_socktype = SOCK_STREAM; |
86504
8b582ee742ce
(socket_connection): Remove AI_ADDRCONFIG.
Jan Djärv <jan.h.d@swipnet.se>
parents:
86235
diff
changeset
|
1128 hints.ai_flags = AI_CANONNAME; |
86210
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1129 hints.ai_family = AF_INET; |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1130 do |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1131 { |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1132 ret = getaddrinfo (host, service, &hints, &res); |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1133 try_count++; |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1134 if (ret != 0 && (ret != EAI_AGAIN || try_count == 5)) |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1135 { |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1136 strcpy (pop_error, "Could not determine POP server's address"); |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1137 return (-1); |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1138 } |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1139 } while (ret != 0); |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1140 |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1141 if (ret == 0) |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1142 { |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1143 it = res; |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1144 while (it) |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1145 { |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1146 if (it->ai_addrlen == sizeof (addr)) |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1147 { |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1148 struct sockaddr_in *in_a = (struct sockaddr_in *) it->ai_addr; |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1149 bcopy (&in_a->sin_addr, (char *) &addr.sin_addr, |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1150 sizeof (addr.sin_addr)); |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1151 if (! connect (sock, (struct sockaddr *) &addr, sizeof (addr))) |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1152 break; |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1153 } |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1154 it = it->ai_next; |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1155 } |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1156 connect_ok = it != NULL; |
86235
79cf32b82b23
(socket_connection): Move realhost out of #ifdefs.
Jan Djärv <jan.h.d@swipnet.se>
parents:
86210
diff
changeset
|
1157 if (connect_ok) |
79cf32b82b23
(socket_connection): Move realhost out of #ifdefs.
Jan Djärv <jan.h.d@swipnet.se>
parents:
86210
diff
changeset
|
1158 { |
79cf32b82b23
(socket_connection): Move realhost out of #ifdefs.
Jan Djärv <jan.h.d@swipnet.se>
parents:
86210
diff
changeset
|
1159 realhost = alloca (strlen (it->ai_canonname) + 1); |
79cf32b82b23
(socket_connection): Move realhost out of #ifdefs.
Jan Djärv <jan.h.d@swipnet.se>
parents:
86210
diff
changeset
|
1160 strcpy (realhost, it->ai_canonname); |
79cf32b82b23
(socket_connection): Move realhost out of #ifdefs.
Jan Djärv <jan.h.d@swipnet.se>
parents:
86210
diff
changeset
|
1161 } |
86210
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1162 freeaddrinfo (res); |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1163 } |
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1164 #else /* !HAVE_GETADDRINFO */ |
45328
cbcc456b9a76
(socket_connection): Move the code to resolve the POP
Eli Zaretskii <eliz@gnu.org>
parents:
42439
diff
changeset
|
1165 do |
cbcc456b9a76
(socket_connection): Move the code to resolve the POP
Eli Zaretskii <eliz@gnu.org>
parents:
42439
diff
changeset
|
1166 { |
cbcc456b9a76
(socket_connection): Move the code to resolve the POP
Eli Zaretskii <eliz@gnu.org>
parents:
42439
diff
changeset
|
1167 hostent = gethostbyname (host); |
cbcc456b9a76
(socket_connection): Move the code to resolve the POP
Eli Zaretskii <eliz@gnu.org>
parents:
42439
diff
changeset
|
1168 try_count++; |
cbcc456b9a76
(socket_connection): Move the code to resolve the POP
Eli Zaretskii <eliz@gnu.org>
parents:
42439
diff
changeset
|
1169 if ((! hostent) && ((h_errno != TRY_AGAIN) || (try_count == 5))) |
cbcc456b9a76
(socket_connection): Move the code to resolve the POP
Eli Zaretskii <eliz@gnu.org>
parents:
42439
diff
changeset
|
1170 { |
cbcc456b9a76
(socket_connection): Move the code to resolve the POP
Eli Zaretskii <eliz@gnu.org>
parents:
42439
diff
changeset
|
1171 strcpy (pop_error, "Could not determine POP server's address"); |
cbcc456b9a76
(socket_connection): Move the code to resolve the POP
Eli Zaretskii <eliz@gnu.org>
parents:
42439
diff
changeset
|
1172 return (-1); |
cbcc456b9a76
(socket_connection): Move the code to resolve the POP
Eli Zaretskii <eliz@gnu.org>
parents:
42439
diff
changeset
|
1173 } |
cbcc456b9a76
(socket_connection): Move the code to resolve the POP
Eli Zaretskii <eliz@gnu.org>
parents:
42439
diff
changeset
|
1174 } while (! hostent); |
cbcc456b9a76
(socket_connection): Move the code to resolve the POP
Eli Zaretskii <eliz@gnu.org>
parents:
42439
diff
changeset
|
1175 |
9158 | 1176 while (*hostent->h_addr_list) |
1177 { | |
1178 bcopy (*hostent->h_addr_list, (char *) &addr.sin_addr, | |
1179 hostent->h_length); | |
1180 if (! connect (sock, (struct sockaddr *) &addr, sizeof (addr))) | |
1181 break; | |
1182 hostent->h_addr_list++; | |
1183 } | |
86210
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1184 connect_ok = *hostent->h_addr_list != NULL; |
86235
79cf32b82b23
(socket_connection): Move realhost out of #ifdefs.
Jan Djärv <jan.h.d@swipnet.se>
parents:
86210
diff
changeset
|
1185 if (! connect_ok) |
79cf32b82b23
(socket_connection): Move realhost out of #ifdefs.
Jan Djärv <jan.h.d@swipnet.se>
parents:
86210
diff
changeset
|
1186 { |
79cf32b82b23
(socket_connection): Move realhost out of #ifdefs.
Jan Djärv <jan.h.d@swipnet.se>
parents:
86210
diff
changeset
|
1187 realhost = alloca (strlen (hostent->h_name) + 1); |
79cf32b82b23
(socket_connection): Move realhost out of #ifdefs.
Jan Djärv <jan.h.d@swipnet.se>
parents:
86210
diff
changeset
|
1188 strcpy (realhost, hostent->h_name); |
79cf32b82b23
(socket_connection): Move realhost out of #ifdefs.
Jan Djärv <jan.h.d@swipnet.se>
parents:
86210
diff
changeset
|
1189 } |
79cf32b82b23
(socket_connection): Move realhost out of #ifdefs.
Jan Djärv <jan.h.d@swipnet.se>
parents:
86210
diff
changeset
|
1190 |
86210
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1191 #endif /* !HAVE_GETADDRINFO */ |
9158 | 1192 |
1193 #define CONNECT_ERROR "Could not connect to POP server: " | |
42439
d8a417105504
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
42412
diff
changeset
|
1194 |
86210
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1195 if (! connect_ok) |
9158 | 1196 { |
15108
442458addd50
(SEND, RECV): Renamed from send, recv.
Richard M. Stallman <rms@gnu.org>
parents:
15102
diff
changeset
|
1197 CLOSESOCKET (sock); |
9158 | 1198 strcpy (pop_error, CONNECT_ERROR); |
1199 strncat (pop_error, strerror (errno), | |
1200 ERROR_MAX - sizeof (CONNECT_ERROR)); | |
1201 return (-1); | |
42439
d8a417105504
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
42412
diff
changeset
|
1202 |
9158 | 1203 } |
1204 | |
1205 #ifdef KERBEROS | |
86210
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1206 |
9158 | 1207 #define KRB_ERROR "Kerberos error connecting to POP server: " |
1208 if (! (flags & POP_NO_KERBEROS)) | |
1209 { | |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1210 #ifdef KERBEROS5 |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1211 if ((rem = krb5_init_context (&kcontext))) |
9158 | 1212 { |
1213 krb5error: | |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1214 if (auth_context) |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1215 krb5_auth_con_free (kcontext, auth_context); |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1216 if (kcontext) |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1217 krb5_free_context (kcontext); |
9158 | 1218 strcpy (pop_error, KRB_ERROR); |
1219 strncat (pop_error, error_message (rem), | |
1220 ERROR_MAX - sizeof(KRB_ERROR)); | |
15108
442458addd50
(SEND, RECV): Renamed from send, recv.
Richard M. Stallman <rms@gnu.org>
parents:
15102
diff
changeset
|
1221 CLOSESOCKET (sock); |
9158 | 1222 return (-1); |
1223 } | |
1224 | |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1225 if ((rem = krb5_auth_con_init (kcontext, &auth_context))) |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1226 goto krb5error; |
42439
d8a417105504
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
42412
diff
changeset
|
1227 |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1228 if (rem = krb5_cc_default (kcontext, &ccdef)) |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1229 goto krb5error; |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1230 |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1231 if (rem = krb5_cc_get_principal (kcontext, ccdef, &client)) |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1232 goto krb5error; |
9158 | 1233 |
86210
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1234 for (cp = realhost; *cp; cp++) |
9158 | 1235 { |
1236 if (isupper (*cp)) | |
1237 { | |
1238 *cp = tolower (*cp); | |
1239 } | |
1240 } | |
1241 | |
86210
7ae58179773b
(socket_connection): Use getaddrinfo if available.
Jan Djärv <jan.h.d@swipnet.se>
parents:
78257
diff
changeset
|
1242 if (rem = krb5_sname_to_principal (kcontext, realhost, |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1243 POP_SERVICE, FALSE, &server)) |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1244 goto krb5error; |
9158 | 1245 |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1246 rem = krb5_sendauth (kcontext, &auth_context, |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1247 (krb5_pointer) &sock, "KPOPV1.0", client, server, |
9158 | 1248 AP_OPTS_MUTUAL_REQUIRED, |
1249 0, /* no checksum */ | |
1250 0, /* no creds, use ccache instead */ | |
1251 ccdef, | |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1252 &err_ret, |
9158 | 1253 0, /* don't need subsession key */ |
1254 0); /* don't need reply */ | |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1255 krb5_free_principal (kcontext, server); |
9158 | 1256 if (rem) |
1257 { | |
1258 if (err_ret && err_ret->text.length) | |
1259 { | |
1260 strcpy (pop_error, KRB_ERROR); | |
1261 strncat (pop_error, error_message (rem), | |
1262 ERROR_MAX - sizeof (KRB_ERROR)); | |
1263 strncat (pop_error, " [server says '", | |
1264 ERROR_MAX - strlen (pop_error) - 1); | |
1265 strncat (pop_error, err_ret->text.data, | |
1266 min (ERROR_MAX - strlen (pop_error) - 1, | |
1267 err_ret->text.length)); | |
1268 strncat (pop_error, "']", | |
1269 ERROR_MAX - strlen (pop_error) - 1); | |
1270 } | |
1271 else | |
1272 { | |
1273 strcpy (pop_error, KRB_ERROR); | |
1274 strncat (pop_error, error_message (rem), | |
1275 ERROR_MAX - sizeof (KRB_ERROR)); | |
1276 } | |
1277 if (err_ret) | |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1278 krb5_free_error (kcontext, err_ret); |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1279 krb5_auth_con_free (kcontext, auth_context); |
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1280 krb5_free_context (kcontext); |
9158 | 1281 |
15108
442458addd50
(SEND, RECV): Renamed from send, recv.
Richard M. Stallman <rms@gnu.org>
parents:
15102
diff
changeset
|
1282 CLOSESOCKET (sock); |
9158 | 1283 return (-1); |
1284 } | |
42439
d8a417105504
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
42412
diff
changeset
|
1285 #else /* ! KERBEROS5 */ |
9158 | 1286 ticket = (KTEXT) malloc (sizeof (KTEXT_ST)); |
16603
a12b8815bcf0
gethostbyname() may return a pointer to static data, which is
Charles Hannum <mycroft@gnu.org>
parents:
15934
diff
changeset
|
1287 rem = krb_sendauth (0L, sock, ticket, "pop", realhost, |
a12b8815bcf0
gethostbyname() may return a pointer to static data, which is
Charles Hannum <mycroft@gnu.org>
parents:
15934
diff
changeset
|
1288 (char *) krb_realmofhost (realhost), |
9158 | 1289 (unsigned long) 0, &msg_data, &cred, schedule, |
1290 (struct sockaddr_in *) 0, | |
1291 (struct sockaddr_in *) 0, | |
1292 "KPOPV0.1"); | |
1293 free ((char *) ticket); | |
1294 if (rem != KSUCCESS) | |
1295 { | |
1296 strcpy (pop_error, KRB_ERROR); | |
1297 strncat (pop_error, krb_err_txt[rem], | |
1298 ERROR_MAX - sizeof (KRB_ERROR)); | |
15108
442458addd50
(SEND, RECV): Renamed from send, recv.
Richard M. Stallman <rms@gnu.org>
parents:
15102
diff
changeset
|
1299 CLOSESOCKET (sock); |
9158 | 1300 return (-1); |
1301 } | |
19018
f1f7c254aa77
Support auto-configuration of both Kerberos V4 and
Richard M. Stallman <rms@gnu.org>
parents:
17683
diff
changeset
|
1302 #endif /* KERBEROS5 */ |
9158 | 1303 } |
1304 #endif /* KERBEROS */ | |
1305 | |
1306 return (sock); | |
1307 } /* socket_connection */ | |
1308 | |
1309 /* | |
21295
f399e51d6d11
(pop_getline): Renamed from getline.
Richard M. Stallman <rms@gnu.org>
parents:
20418
diff
changeset
|
1310 * Function: pop_getline |
9158 | 1311 * |
1312 * Purpose: Get a line of text from the connection and return a | |
1313 * pointer to it. The carriage return and linefeed at the end of | |
1314 * the line are stripped, but periods at the beginnings of lines | |
1315 * are NOT dealt with in any special way. | |
1316 * | |
1317 * Arguments: | |
1318 * server The server from which to get the line of text. | |
1319 * | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1320 * Returns: The number of characters in the line, which is returned in |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1321 * LINE, not including the final null. A return value of 0 |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1322 * indicates a blank line. A negative return value indicates an |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1323 * error (in which case the contents of LINE are undefined. In |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1324 * case of error, an error message is copied into pop_error. |
9158 | 1325 * |
21295
f399e51d6d11
(pop_getline): Renamed from getline.
Richard M. Stallman <rms@gnu.org>
parents:
20418
diff
changeset
|
1326 * Notes: The line returned is overwritten with each call to pop_getline. |
9158 | 1327 * |
1328 * Side effects: Closes the connection on error. | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1329 * |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1330 * THE RETURNED LINE MAY CONTAIN EMBEDDED NULLS! |
9158 | 1331 */ |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1332 static int |
21295
f399e51d6d11
(pop_getline): Renamed from getline.
Richard M. Stallman <rms@gnu.org>
parents:
20418
diff
changeset
|
1333 pop_getline (server, line) |
9158 | 1334 popserver server; |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1335 char **line; |
9158 | 1336 { |
1337 #define GETLINE_ERROR "Error reading from server: " | |
1338 | |
1339 int ret; | |
9674
e5a897cf215d
(getline): When a search of already-read input for CRLF
Richard M. Stallman <rms@gnu.org>
parents:
9613
diff
changeset
|
1340 int search_offset = 0; |
e5a897cf215d
(getline): When a search of already-read input for CRLF
Richard M. Stallman <rms@gnu.org>
parents:
9613
diff
changeset
|
1341 |
9158 | 1342 if (server->data) |
1343 { | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1344 char *cp = find_crlf (server->buffer + server->buffer_index, |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1345 server->data); |
9158 | 1346 if (cp) |
1347 { | |
1348 int found; | |
1349 int data_used; | |
1350 | |
1351 found = server->buffer_index; | |
1352 data_used = (cp + 2) - server->buffer - found; | |
42439
d8a417105504
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
42412
diff
changeset
|
1353 |
9158 | 1354 *cp = '\0'; /* terminate the string to be returned */ |
1355 server->data -= data_used; | |
1356 server->buffer_index += data_used; | |
1357 | |
1358 if (pop_debug) | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1359 /* Embedded nulls will truncate this output prematurely, |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1360 but that's OK because it's just for debugging anyway. */ |
9158 | 1361 fprintf (stderr, "<<< %s\n", server->buffer + found); |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1362 *line = server->buffer + found; |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1363 return (data_used - 2); |
9158 | 1364 } |
1365 else | |
1366 { | |
1367 bcopy (server->buffer + server->buffer_index, | |
1368 server->buffer, server->data); | |
9674
e5a897cf215d
(getline): When a search of already-read input for CRLF
Richard M. Stallman <rms@gnu.org>
parents:
9613
diff
changeset
|
1369 /* Record the fact that we've searched the data already in |
e5a897cf215d
(getline): When a search of already-read input for CRLF
Richard M. Stallman <rms@gnu.org>
parents:
9613
diff
changeset
|
1370 the buffer for a CRLF, so that when we search below, we |
e5a897cf215d
(getline): When a search of already-read input for CRLF
Richard M. Stallman <rms@gnu.org>
parents:
9613
diff
changeset
|
1371 don't have to search the same data twice. There's a "- |
e5a897cf215d
(getline): When a search of already-read input for CRLF
Richard M. Stallman <rms@gnu.org>
parents:
9613
diff
changeset
|
1372 1" here to account for the fact that the last character |
e5a897cf215d
(getline): When a search of already-read input for CRLF
Richard M. Stallman <rms@gnu.org>
parents:
9613
diff
changeset
|
1373 of the data we have may be the CR of a CRLF pair, of |
e5a897cf215d
(getline): When a search of already-read input for CRLF
Richard M. Stallman <rms@gnu.org>
parents:
9613
diff
changeset
|
1374 which we haven't read the second half yet, so we may have |
e5a897cf215d
(getline): When a search of already-read input for CRLF
Richard M. Stallman <rms@gnu.org>
parents:
9613
diff
changeset
|
1375 to search it again when we read more data. */ |
e5a897cf215d
(getline): When a search of already-read input for CRLF
Richard M. Stallman <rms@gnu.org>
parents:
9613
diff
changeset
|
1376 search_offset = server->data - 1; |
9158 | 1377 server->buffer_index = 0; |
1378 } | |
1379 } | |
1380 else | |
1381 { | |
1382 server->buffer_index = 0; | |
1383 } | |
1384 | |
1385 while (1) | |
1386 { | |
22236 | 1387 /* There's a "- 1" here to leave room for the null that we put |
1388 at the end of the read data below. We put the null there so | |
1389 that find_crlf knows where to stop when we call it. */ | |
1390 if (server->data == server->buffer_size - 1) | |
9158 | 1391 { |
22236 | 1392 server->buffer_size += GETLINE_INCR; |
1393 server->buffer = (char *)realloc (server->buffer, server->buffer_size); | |
1394 if (! server->buffer) | |
9158 | 1395 { |
22236 | 1396 strcpy (pop_error, "Out of memory in pop_getline"); |
1397 pop_trash (server); | |
1398 return (-1); | |
9158 | 1399 } |
1400 } | |
22236 | 1401 ret = RECV (server->file, server->buffer + server->data, |
1402 server->buffer_size - server->data - 1, 0); | |
9158 | 1403 if (ret < 0) |
1404 { | |
1405 strcpy (pop_error, GETLINE_ERROR); | |
1406 strncat (pop_error, strerror (errno), | |
1407 ERROR_MAX - sizeof (GETLINE_ERROR)); | |
1408 pop_trash (server); | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1409 return (-1); |
9158 | 1410 } |
1411 else if (ret == 0) | |
1412 { | |
21295
f399e51d6d11
(pop_getline): Renamed from getline.
Richard M. Stallman <rms@gnu.org>
parents:
20418
diff
changeset
|
1413 strcpy (pop_error, "Unexpected EOF from server in pop_getline"); |
9158 | 1414 pop_trash (server); |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1415 return (-1); |
9158 | 1416 } |
1417 else | |
1418 { | |
9591
0774e217e8aa
Don't declare malloc, realloc, free.
Richard M. Stallman <rms@gnu.org>
parents:
9158
diff
changeset
|
1419 char *cp; |
9158 | 1420 server->data += ret; |
9591
0774e217e8aa
Don't declare malloc, realloc, free.
Richard M. Stallman <rms@gnu.org>
parents:
9158
diff
changeset
|
1421 server->buffer[server->data] = '\0'; |
42439
d8a417105504
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
42412
diff
changeset
|
1422 |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1423 cp = find_crlf (server->buffer + search_offset, |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1424 server->data - search_offset); |
9158 | 1425 if (cp) |
1426 { | |
1427 int data_used = (cp + 2) - server->buffer; | |
1428 *cp = '\0'; | |
1429 server->data -= data_used; | |
1430 server->buffer_index = data_used; | |
1431 | |
1432 if (pop_debug) | |
1433 fprintf (stderr, "<<< %s\n", server->buffer); | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1434 *line = server->buffer; |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1435 return (data_used - 2); |
9158 | 1436 } |
17683
975d093d45fb
(getline): Don't miss CRLF pairs when the CR and LF are
Richard M. Stallman <rms@gnu.org>
parents:
16697
diff
changeset
|
1437 /* As above, the "- 1" here is to account for the fact that |
975d093d45fb
(getline): Don't miss CRLF pairs when the CR and LF are
Richard M. Stallman <rms@gnu.org>
parents:
16697
diff
changeset
|
1438 we may have read a CR without its accompanying LF. */ |
975d093d45fb
(getline): Don't miss CRLF pairs when the CR and LF are
Richard M. Stallman <rms@gnu.org>
parents:
16697
diff
changeset
|
1439 search_offset += ret - 1; |
9158 | 1440 } |
1441 } | |
1442 | |
1443 /* NOTREACHED */ | |
1444 } | |
1445 | |
1446 /* | |
1447 * Function: sendline | |
1448 * | |
1449 * Purpose: Sends a line of text to the POP server. The line of text | |
1450 * passed into this function should NOT have the carriage return | |
1451 * and linefeed on the end of it. Periods at beginnings of lines | |
1452 * will NOT be treated specially by this function. | |
1453 * | |
1454 * Arguments: | |
1455 * server The server to which to send the text. | |
1456 * line The line of text to send. | |
1457 * | |
1458 * Return value: Upon successful completion, a value of 0 will be | |
1459 * returned. Otherwise, a non-zero value will be returned, and | |
1460 * an error will be copied into pop_error. | |
1461 * | |
1462 * Side effects: Closes the connection on error. | |
1463 */ | |
1464 static int | |
1465 sendline (server, line) | |
1466 popserver server; | |
1467 char *line; | |
1468 { | |
1469 #define SENDLINE_ERROR "Error writing to POP server: " | |
1470 int ret; | |
64293
0238a53b950b
Don't include des.h (or variants thereof); krb.h will do it.
Ken Raeburn <raeburn@raeburn.org>
parents:
64083
diff
changeset
|
1471 char *buf; |
9158 | 1472 |
64293
0238a53b950b
Don't include des.h (or variants thereof); krb.h will do it.
Ken Raeburn <raeburn@raeburn.org>
parents:
64083
diff
changeset
|
1473 /* Combine the string and the CR-LF into one buffer. Otherwise, two |
0238a53b950b
Don't include des.h (or variants thereof); krb.h will do it.
Ken Raeburn <raeburn@raeburn.org>
parents:
64083
diff
changeset
|
1474 reasonable network stack optimizations, Nagle's algorithm and |
0238a53b950b
Don't include des.h (or variants thereof); krb.h will do it.
Ken Raeburn <raeburn@raeburn.org>
parents:
64083
diff
changeset
|
1475 delayed acks, combine to delay us a fraction of a second on every |
0238a53b950b
Don't include des.h (or variants thereof); krb.h will do it.
Ken Raeburn <raeburn@raeburn.org>
parents:
64083
diff
changeset
|
1476 message we send. (Movemail writes line without \r\n, client |
0238a53b950b
Don't include des.h (or variants thereof); krb.h will do it.
Ken Raeburn <raeburn@raeburn.org>
parents:
64083
diff
changeset
|
1477 kernel sends packet, server kernel delays the ack to see if it |
0238a53b950b
Don't include des.h (or variants thereof); krb.h will do it.
Ken Raeburn <raeburn@raeburn.org>
parents:
64083
diff
changeset
|
1478 can combine it with data, movemail writes \r\n, client kernel |
0238a53b950b
Don't include des.h (or variants thereof); krb.h will do it.
Ken Raeburn <raeburn@raeburn.org>
parents:
64083
diff
changeset
|
1479 waits because it has unacked data already in its outgoing queue, |
0238a53b950b
Don't include des.h (or variants thereof); krb.h will do it.
Ken Raeburn <raeburn@raeburn.org>
parents:
64083
diff
changeset
|
1480 client kernel eventually times out and sends.) |
0238a53b950b
Don't include des.h (or variants thereof); krb.h will do it.
Ken Raeburn <raeburn@raeburn.org>
parents:
64083
diff
changeset
|
1481 |
0238a53b950b
Don't include des.h (or variants thereof); krb.h will do it.
Ken Raeburn <raeburn@raeburn.org>
parents:
64083
diff
changeset
|
1482 This can be something like 0.2s per command, which can add up |
0238a53b950b
Don't include des.h (or variants thereof); krb.h will do it.
Ken Raeburn <raeburn@raeburn.org>
parents:
64083
diff
changeset
|
1483 over a few dozen messages, and is a big chunk of the time we |
0238a53b950b
Don't include des.h (or variants thereof); krb.h will do it.
Ken Raeburn <raeburn@raeburn.org>
parents:
64083
diff
changeset
|
1484 spend fetching mail from a server close by. */ |
0238a53b950b
Don't include des.h (or variants thereof); krb.h will do it.
Ken Raeburn <raeburn@raeburn.org>
parents:
64083
diff
changeset
|
1485 buf = alloca (strlen (line) + 3); |
0238a53b950b
Don't include des.h (or variants thereof); krb.h will do it.
Ken Raeburn <raeburn@raeburn.org>
parents:
64083
diff
changeset
|
1486 strcpy (buf, line); |
0238a53b950b
Don't include des.h (or variants thereof); krb.h will do it.
Ken Raeburn <raeburn@raeburn.org>
parents:
64083
diff
changeset
|
1487 strcat (buf, "\r\n"); |
0238a53b950b
Don't include des.h (or variants thereof); krb.h will do it.
Ken Raeburn <raeburn@raeburn.org>
parents:
64083
diff
changeset
|
1488 ret = fullwrite (server->file, buf, strlen (buf)); |
9158 | 1489 |
1490 if (ret < 0) | |
1491 { | |
1492 pop_trash (server); | |
1493 strcpy (pop_error, SENDLINE_ERROR); | |
1494 strncat (pop_error, strerror (errno), | |
1495 ERROR_MAX - sizeof (SENDLINE_ERROR)); | |
1496 return (ret); | |
1497 } | |
1498 | |
1499 if (pop_debug) | |
1500 fprintf (stderr, ">>> %s\n", line); | |
1501 | |
1502 return (0); | |
1503 } | |
1504 | |
1505 /* | |
1506 * Procedure: fullwrite | |
1507 * | |
1508 * Purpose: Just like write, but keeps trying until the entire string | |
1509 * has been written. | |
1510 * | |
1511 * Return value: Same as write. Pop_error is not set. | |
1512 */ | |
1513 static int | |
1514 fullwrite (fd, buf, nbytes) | |
1515 int fd; | |
1516 char *buf; | |
1517 int nbytes; | |
1518 { | |
1519 char *cp; | |
20204
44f91b648e03
(fullwrite): Get rid of an extra call to write. Problem
Karl Heuer <kwzh@gnu.org>
parents:
19978
diff
changeset
|
1520 int ret = 0; |
9158 | 1521 |
1522 cp = buf; | |
20204
44f91b648e03
(fullwrite): Get rid of an extra call to write. Problem
Karl Heuer <kwzh@gnu.org>
parents:
19978
diff
changeset
|
1523 while (nbytes && ((ret = SEND (fd, cp, nbytes, 0)) > 0)) |
9158 | 1524 { |
1525 cp += ret; | |
1526 nbytes -= ret; | |
1527 } | |
1528 | |
1529 return (ret); | |
1530 } | |
1531 | |
1532 /* | |
1533 * Procedure getok | |
1534 * | |
1535 * Purpose: Reads a line from the server. If the return indicator is | |
1536 * positive, return with a zero exit status. If not, return with | |
1537 * a negative exit status. | |
1538 * | |
1539 * Arguments: | |
1540 * server The server to read from. | |
42439
d8a417105504
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
42412
diff
changeset
|
1541 * |
9158 | 1542 * Returns: 0 for success, else for failure and puts error in pop_error. |
1543 * | |
14036 | 1544 * Side effects: On failure, may make the connection unusable. |
9158 | 1545 */ |
1546 static int | |
1547 getok (server) | |
1548 popserver server; | |
1549 { | |
1550 char *fromline; | |
1551 | |
21295
f399e51d6d11
(pop_getline): Renamed from getline.
Richard M. Stallman <rms@gnu.org>
parents:
20418
diff
changeset
|
1552 if (pop_getline (server, &fromline) < 0) |
9158 | 1553 { |
1554 return (-1); | |
1555 } | |
1556 | |
1557 if (! strncmp (fromline, "+OK", 3)) | |
1558 return (0); | |
1559 else if (! strncmp (fromline, "-ERR", 4)) | |
1560 { | |
1561 strncpy (pop_error, fromline, ERROR_MAX); | |
1562 pop_error[ERROR_MAX-1] = '\0'; | |
1563 return (-1); | |
1564 } | |
1565 else | |
1566 { | |
1567 strcpy (pop_error, | |
1568 "Unexpected response from server; expecting +OK or -ERR"); | |
1569 pop_trash (server); | |
1570 return (-1); | |
1571 } | |
42439
d8a417105504
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
42412
diff
changeset
|
1572 } |
9158 | 1573 |
1574 #if 0 | |
1575 /* | |
1576 * Function: gettermination | |
1577 * | |
1578 * Purpose: Gets the next line and verifies that it is a termination | |
1579 * line (nothing but a dot). | |
1580 * | |
1581 * Return value: 0 on success, non-zero with pop_error set on error. | |
1582 * | |
1583 * Side effects: Closes the connection on error. | |
1584 */ | |
1585 static int | |
1586 gettermination (server) | |
1587 popserver server; | |
1588 { | |
1589 char *fromserver; | |
1590 | |
21295
f399e51d6d11
(pop_getline): Renamed from getline.
Richard M. Stallman <rms@gnu.org>
parents:
20418
diff
changeset
|
1591 if (pop_getline (server, &fromserver) < 0) |
9158 | 1592 return (-1); |
1593 | |
1594 if (strcmp (fromserver, ".")) | |
1595 { | |
1596 strcpy (pop_error, | |
1597 "Unexpected response from server in gettermination"); | |
1598 pop_trash (server); | |
1599 return (-1); | |
1600 } | |
1601 | |
1602 return (0); | |
1603 } | |
1604 #endif | |
1605 | |
1606 /* | |
1607 * Function pop_close | |
1608 * | |
1609 * Purpose: Close a pop connection, sending a "RSET" command to try to | |
1610 * preserve any changes that were made and a "QUIT" command to | |
1611 * try to get the server to quit, but ignoring any responses that | |
1612 * are received. | |
1613 * | |
14036 | 1614 * Side effects: The server is unusable after this function returns. |
9158 | 1615 * Changes made to the maildrop since the session was started (or |
1616 * since the last pop_reset) may be lost. | |
1617 */ | |
42439
d8a417105504
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
42412
diff
changeset
|
1618 void |
9158 | 1619 pop_close (server) |
1620 popserver server; | |
1621 { | |
1622 pop_trash (server); | |
1623 free ((char *) server); | |
1624 | |
1625 return; | |
1626 } | |
1627 | |
1628 /* | |
1629 * Function: pop_trash | |
1630 * | |
1631 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the | |
80029
e1b6ef5c6464
*** empty log message ***
Juanma Barranquero <lekktu@gmail.com>
parents:
79905
diff
changeset
|
1632 * memory associated with the server. It is valid to call |
9158 | 1633 * pop_close or pop_quit after this function has been called. |
1634 */ | |
1635 static void | |
1636 pop_trash (server) | |
1637 popserver server; | |
1638 { | |
1639 if (server->file >= 0) | |
1640 { | |
15102
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1641 /* avoid recursion; sendline can call pop_trash */ |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1642 if (server->trash_started) |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1643 return; |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1644 server->trash_started = 1; |
15108
442458addd50
(SEND, RECV): Renamed from send, recv.
Richard M. Stallman <rms@gnu.org>
parents:
15102
diff
changeset
|
1645 |
9158 | 1646 sendline (server, "RSET"); |
1647 sendline (server, "QUIT"); | |
1648 | |
15108
442458addd50
(SEND, RECV): Renamed from send, recv.
Richard M. Stallman <rms@gnu.org>
parents:
15102
diff
changeset
|
1649 CLOSESOCKET (server->file); |
9158 | 1650 server->file = -1; |
1651 if (server->buffer) | |
1652 { | |
1653 free (server->buffer); | |
1654 server->buffer = 0; | |
1655 } | |
1656 } | |
15102
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1657 |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1658 #ifdef WINDOWSNT |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1659 if (have_winsock) |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1660 WSACleanup (); |
ed550d0805ca
[WINDOWSNT]: Include winsock.h and ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14238
diff
changeset
|
1661 #endif |
9158 | 1662 } |
1663 | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1664 /* Return a pointer to the first CRLF in IN_STRING, which can contain |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1665 embedded nulls and has LEN characters in it not including the final |
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1666 null, or 0 if it does not contain one. */ |
9158 | 1667 |
1668 static char * | |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1669 find_crlf (in_string, len) |
9613
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
1670 char *in_string; |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1671 int len; |
9158 | 1672 { |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1673 while (len--) |
9158 | 1674 { |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1675 if (*in_string == '\r') |
9613
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
1676 { |
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
1677 if (*++in_string == '\n') |
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
1678 return (in_string - 1); |
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
1679 } |
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
1680 else |
530b4511e5b3
Only include ../src/config.h if HAVE_CONFIG_H is
Richard M. Stallman <rms@gnu.org>
parents:
9600
diff
changeset
|
1681 in_string++; |
9158 | 1682 } |
20418
7e1538a45702
Allow messages retrieved from the POP
Karl Heuer <kwzh@gnu.org>
parents:
20204
diff
changeset
|
1683 return (0); |
9158 | 1684 } |
1685 | |
1686 #endif /* MAIL_USE_POP */ | |
52401 | 1687 |
1688 /* arch-tag: ceb37041-b7ad-49a8-a63d-286618b8367d | |
1689 (do not change this comment) */ |