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