Commit | Line | Data |
---|---|---|
9c0f2dac | 1 | /* pop.c: client routines for talking to a POP3-protocol post-office server |
b3d90e46 | 2 | Copyright (C) 1991, 1993, 1996, 1997, 1999, 2001, 2002, 2003, 2004, |
a5b68355 | 3 | 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
294981c7 GM |
4 | |
5 | Author: Jonathan Kamens <jik@security.ov.com> | |
9c0f2dac RS |
6 | |
7 | This file is part of GNU Emacs. | |
8 | ||
294981c7 | 9 | GNU Emacs is free software: you can redistribute it and/or modify |
9c0f2dac | 10 | it under the terms of the GNU General Public License as published by |
294981c7 GM |
11 | the Free Software Foundation, either version 3 of the License, or |
12 | (at your option) any later version. | |
9c0f2dac RS |
13 | |
14 | GNU Emacs is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
294981c7 GM |
20 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
21 | ||
9c0f2dac | 22 | |
8148baf9 | 23 | #ifdef HAVE_CONFIG_H |
2f8fe2f4 | 24 | #include <config.h> |
8148baf9 RS |
25 | #else |
26 | #define MAIL_USE_POP | |
27 | #endif | |
c9c3d8f6 | 28 | |
8148baf9 RS |
29 | #ifdef MAIL_USE_POP |
30 | ||
9c0f2dac | 31 | #include <sys/types.h> |
c2668a61 RS |
32 | #ifdef WINDOWSNT |
33 | #include "ntlib.h" | |
34 | #include <winsock.h> | |
35 | #undef SOCKET_ERROR | |
de5c39cf RS |
36 | #define RECV(s,buf,len,flags) recv(s,buf,len,flags) |
37 | #define SEND(s,buf,len,flags) send(s,buf,len,flags) | |
38 | #define CLOSESOCKET(s) closesocket(s) | |
c2668a61 | 39 | #else |
9c0f2dac RS |
40 | #include <netinet/in.h> |
41 | #include <sys/socket.h> | |
de5c39cf RS |
42 | #define RECV(s,buf,len,flags) read(s,buf,len) |
43 | #define SEND(s,buf,len,flags) write(s,buf,len) | |
44 | #define CLOSESOCKET(s) close(s) | |
c2668a61 | 45 | #endif |
9c0f2dac | 46 | #include <pop.h> |
625ad89f | 47 | |
9c0f2dac RS |
48 | #ifdef sun |
49 | #include <malloc.h> | |
8148baf9 | 50 | #endif /* sun */ |
625ad89f | 51 | |
9c0f2dac RS |
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 | |
625ad89f | 62 | |
9c0f2dac | 63 | #include <pwd.h> |
9c0f2dac RS |
64 | #include <netdb.h> |
65 | #include <errno.h> | |
66 | #include <stdio.h> | |
a27738b6 RS |
67 | #ifdef STDC_HEADERS |
68 | #include <string.h> | |
020d3015 KH |
69 | #define index strchr |
70 | #endif | |
020d3015 KH |
71 | #ifdef HAVE_UNISTD_H |
72 | #include <unistd.h> | |
a27738b6 | 73 | #endif |
625ad89f | 74 | |
9c0f2dac | 75 | #ifdef KERBEROS |
a27738b6 RS |
76 | # ifdef HAVE_KRB5_H |
77 | # include <krb5.h> | |
78 | # endif | |
a27738b6 RS |
79 | # ifdef HAVE_KRB_H |
80 | # include <krb.h> | |
81 | # else | |
82 | # ifdef HAVE_KERBEROSIV_KRB_H | |
83 | # include <kerberosIV/krb.h> | |
84 | # else | |
85 | # ifdef HAVE_KERBEROS_KRB_H | |
86 | # include <kerberos/krb.h> | |
87 | # endif | |
88 | # endif | |
89 | # endif | |
90 | # ifdef HAVE_COM_ERR_H | |
91 | # include <com_err.h> | |
92 | # endif | |
8148baf9 | 93 | #endif /* KERBEROS */ |
9c0f2dac | 94 | |
8148baf9 | 95 | #ifdef KERBEROS |
a27738b6 | 96 | #ifndef KERBEROS5 |
9c0f2dac | 97 | extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *, |
8148baf9 RS |
98 | u_long, MSG_DAT *, CREDENTIALS *, Key_schedule, |
99 | struct sockaddr_in *, struct sockaddr_in *, | |
100 | char * */); | |
9c0f2dac | 101 | extern char *krb_realmofhost (/* char * */); |
a27738b6 | 102 | #endif /* ! KERBEROS5 */ |
8148baf9 | 103 | #endif /* KERBEROS */ |
9c0f2dac | 104 | |
c2668a61 | 105 | #ifndef WINDOWSNT |
8148baf9 | 106 | #if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H) |
9c0f2dac RS |
107 | extern int h_errno; |
108 | #endif | |
c2668a61 | 109 | #endif |
9c0f2dac | 110 | |
1cb12638 | 111 | #ifndef __P |
d89d0243 | 112 | # ifdef __STDC__ |
1cb12638 | 113 | # define __P(a) a |
d89d0243 | 114 | # else |
1cb12638 | 115 | # define __P(a) () |
d89d0243 KH |
116 | # endif /* __STDC__ */ |
117 | #endif /* ! __P */ | |
118 | ||
1cb12638 KS |
119 | static int socket_connection __P((char *, int)); |
120 | static int pop_getline __P((popserver, char **)); | |
121 | static int sendline __P((popserver, char *)); | |
122 | static int fullwrite __P((int, char *, int)); | |
123 | static int getok __P((popserver)); | |
9c0f2dac | 124 | #if 0 |
1cb12638 | 125 | static int gettermination __P((popserver)); |
9c0f2dac | 126 | #endif |
1cb12638 KS |
127 | static void pop_trash __P((popserver)); |
128 | static char *find_crlf __P((char *, int)); | |
7a481e50 | 129 | |
5bf244f1 | 130 | #define ERROR_MAX 160 /* a pretty arbitrary size, but needs |
a47bf648 DL |
131 | to be bigger than the original |
132 | value of 80 */ | |
9c0f2dac RS |
133 | #define POP_PORT 110 |
134 | #define KPOP_PORT 1109 | |
c2668a61 | 135 | #define POP_SERVICE "pop3" /* we don't want the POP2 port! */ |
9c0f2dac | 136 | #ifdef KERBEROS |
81264b38 | 137 | #define KPOP_SERVICE "kpop" /* never used: look for 20060515 to see why */ |
9c0f2dac | 138 | #endif |
9c0f2dac RS |
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 */ | |
bbcac09c RS |
248 | #ifdef KERBEROS |
249 | #define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS)) | |
9c0f2dac | 250 | #else |
bbcac09c | 251 | #define DONT_NEED_PASSWORD 0 |
9c0f2dac | 252 | #endif |
177c0ea7 | 253 | |
9c0f2dac RS |
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 | } | |
81264b38 | 266 | if (password) /* always true, detected 20060515 */ |
bbcac09c | 267 | flags |= POP_NO_KERBEROS; |
9c0f2dac | 268 | else |
81264b38 FP |
269 | password = username; /* dead code, detected 20060515 */ |
270 | /** "kpop" service is never used: look for 20060515 to see why **/ | |
9c0f2dac RS |
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 | } | |
80b2cbf2 | 289 | |
9c0f2dac RS |
290 | server->file = sock; |
291 | server->data = 0; | |
292 | server->buffer_index = 0; | |
293 | server->buffer_size = GETLINE_MIN; | |
294 | server->in_multi = 0; | |
c2668a61 | 295 | server->trash_started = 0; |
9c0f2dac RS |
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; | |
0b074993 | 354 | char *end_ptr; |
9c0f2dac RS |
355 | |
356 | if (server->in_multi) | |
357 | { | |
358 | strcpy (pop_error, "In multi-line query in pop_stat"); | |
359 | return (-1); | |
360 | } | |
80b2cbf2 | 361 | |
60b0a379 | 362 | if (sendline (server, "STAT") || (pop_getline (server, &fromserver) < 0)) |
9c0f2dac RS |
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 | ||
0b074993 CY |
380 | errno = 0; |
381 | *count = strtol (&fromserver[4], &end_ptr, 10); | |
382 | /* Check validity of string-to-integer conversion. */ | |
59a431d6 | 383 | if (fromserver + 4 == end_ptr || *end_ptr != ' ' || errno) |
0b074993 CY |
384 | { |
385 | strcpy (pop_error, "Unexpected response from POP server in pop_stat"); | |
386 | pop_trash (server); | |
387 | return (-1); | |
388 | } | |
80b2cbf2 | 389 | |
59a431d6 | 390 | fromserver = end_ptr; |
9c0f2dac | 391 | |
0b074993 CY |
392 | errno = 0; |
393 | *size = strtol (fromserver + 1, &end_ptr, 10); | |
59a431d6 | 394 | if (fromserver + 1 == end_ptr || errno) |
0b074993 CY |
395 | { |
396 | strcpy (pop_error, "Unexpected response from POP server in pop_stat"); | |
397 | pop_trash (server); | |
398 | return (-1); | |
399 | } | |
9c0f2dac RS |
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 | } | |
60b0a379 | 466 | if (pop_getline (server, &fromserver) < 0) |
9c0f2dac RS |
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 | { | |
d89d0243 | 511 | if (pop_multi_next (server, &fromserver) <= 0) |
9c0f2dac RS |
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 | } | |
d89d0243 | 530 | if (pop_multi_next (server, &fromserver) < 0) |
9c0f2dac RS |
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 '>'. | |
d89d0243 KH |
560 | * msg_buf Output parameter to which a buffer containing the |
561 | * message is assigned. | |
177c0ea7 | 562 | * |
d89d0243 KH |
563 | * Return value: The number of bytes in msg_buf, which may contain |
564 | * embedded nulls, not including its final null, or -1 on error | |
565 | * with pop_error set. | |
9c0f2dac RS |
566 | * |
567 | * Side effects: May kill connection on error. | |
568 | */ | |
d89d0243 KH |
569 | int |
570 | pop_retrieve (server, message, markfrom, msg_buf) | |
9c0f2dac RS |
571 | popserver server; |
572 | int message; | |
573 | int markfrom; | |
d89d0243 | 574 | char **msg_buf; |
9c0f2dac RS |
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"); | |
d89d0243 | 583 | return (-1); |
9c0f2dac RS |
584 | } |
585 | ||
586 | if (pop_list (server, message, &IDs, &sizes)) | |
d89d0243 | 587 | return (-1); |
9c0f2dac RS |
588 | |
589 | if (pop_retrieve_first (server, message, &fromserver)) | |
590 | { | |
d89d0243 | 591 | return (-1); |
9c0f2dac RS |
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); | |
7d665945 | 601 | ptr = (char *)malloc (bufsize); |
9c0f2dac RS |
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); | |
d89d0243 | 609 | return (-1); |
9c0f2dac RS |
610 | } |
611 | ||
d89d0243 | 612 | while ((ret = pop_retrieve_next (server, &fromserver)) >= 0) |
9c0f2dac | 613 | { |
9c0f2dac RS |
614 | if (! fromserver) |
615 | { | |
616 | ptr[cp] = '\0'; | |
d89d0243 KH |
617 | *msg_buf = ptr; |
618 | return (cp); | |
9c0f2dac RS |
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; | |
7d665945 | 627 | ptr = (char *)realloc (ptr, bufsize); |
9c0f2dac RS |
628 | if (! ptr) |
629 | { | |
630 | strcpy (pop_error, "Out of memory in pop_retrieve"); | |
631 | pop_retrieve_flush (server); | |
d89d0243 | 632 | return (-1); |
9c0f2dac RS |
633 | } |
634 | fromcount = 0; | |
635 | } | |
636 | ptr[cp++] = '>'; | |
637 | } | |
d89d0243 KH |
638 | bcopy (fromserver, &ptr[cp], ret); |
639 | cp += ret; | |
9c0f2dac RS |
640 | ptr[cp++] = '\n'; |
641 | } | |
642 | ||
d89d0243 KH |
643 | free (ptr); |
644 | return (-1); | |
177c0ea7 | 645 | } |
9c0f2dac RS |
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 | ||
d89d0243 KH |
657 | /* |
658 | Returns a negative number on error, 0 to indicate that the data has | |
659 | all been read (i.e., the server has returned a "." termination | |
660 | line), or a positive number indicating the number of bytes in the | |
661 | returned buffer (which is null-terminated and may contain embedded | |
662 | nulls, but the returned bytecount doesn't include the final null). | |
663 | */ | |
664 | ||
9c0f2dac RS |
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 | ||
d89d0243 KH |
690 | /* |
691 | Returns a negative number on error, 0 to indicate that the data has | |
692 | all been read (i.e., the server has returned a "." termination | |
693 | line), or a positive number indicating the number of bytes in the | |
694 | returned buffer (which is null-terminated and may contain embedded | |
695 | nulls, but the returned bytecount doesn't include the final null). | |
696 | */ | |
697 | ||
9c0f2dac RS |
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 | ||
60b0a379 | 726 | if (sendline (server, command) || (pop_getline (server, response) < 0)) |
9c0f2dac RS |
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 | ||
d89d0243 KH |
750 | /* |
751 | Read the next line of data from SERVER and place a pointer to it | |
752 | into LINE. Return -1 on error, 0 if there are no more lines to read | |
753 | (i.e., the server has returned a line containing only "."), or a | |
754 | positive number indicating the number of bytes in the LINE buffer | |
755 | (not including the final null). The data in that buffer may contain | |
756 | embedded nulls, but does not contain the final CRLF. When returning | |
757 | 0, LINE is set to null. */ | |
758 | ||
9c0f2dac RS |
759 | int |
760 | pop_multi_next (server, line) | |
761 | popserver server; | |
762 | char **line; | |
763 | { | |
764 | char *fromserver; | |
d89d0243 | 765 | int ret; |
9c0f2dac RS |
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 | ||
60b0a379 | 773 | if ((ret = pop_getline (server, &fromserver)) < 0) |
9c0f2dac RS |
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; | |
d89d0243 | 789 | return (ret - 1); |
9c0f2dac RS |
790 | } |
791 | } | |
792 | else | |
793 | { | |
794 | *line = fromserver; | |
d89d0243 | 795 | return (ret); |
9c0f2dac RS |
796 | } |
797 | } | |
798 | ||
799 | int | |
800 | pop_multi_flush (server) | |
801 | popserver server; | |
802 | { | |
803 | char *line; | |
d89d0243 | 804 | int ret; |
9c0f2dac RS |
805 | |
806 | if (! server->in_multi) | |
807 | { | |
808 | return (0); | |
809 | } | |
810 | ||
d89d0243 | 811 | while ((ret = pop_multi_next (server, &line))) |
9c0f2dac | 812 | { |
d89d0243 KH |
813 | if (ret < 0) |
814 | return (-1); | |
9c0f2dac RS |
815 | } |
816 | ||
d89d0243 | 817 | return (0); |
9c0f2dac RS |
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; | |
80b2cbf2 | 898 | |
9c0f2dac RS |
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 | ||
60b0a379 | 908 | if (pop_getline (server, &fromserver) < 0) |
9c0f2dac RS |
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 | { | |
0b074993 CY |
924 | char *end_ptr; |
925 | int count; | |
926 | errno = 0; | |
927 | count = strtol (&fromserver[4], &end_ptr, 10); | |
59a431d6 | 928 | if (fromserver + 4 == end_ptr || errno) |
0b074993 CY |
929 | { |
930 | strcpy (pop_error, "Unexpected response from server in pop_last"); | |
931 | pop_trash (server); | |
932 | return (-1); | |
933 | } | |
934 | return count; | |
9c0f2dac RS |
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 | * | |
8e6208c5 | 977 | * Side Effects: The popserver passed in is unusable after this |
9c0f2dac RS |
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 | ||
c2cd06e6 | 1001 | free (server->buffer); |
9c0f2dac RS |
1002 | free ((char *) server); |
1003 | ||
1004 | return (ret); | |
1005 | } | |
1006 | ||
c2668a61 RS |
1007 | #ifdef WINDOWSNT |
1008 | static int have_winsock = 0; | |
1009 | #endif | |
1010 | ||
9c0f2dac RS |
1011 | /* |
1012 | * Function: socket_connection | |
1013 | * | |
1014 | * Purpose: Opens the network connection with the mail host, without | |
1015 | * doing any sort of I/O with it or anything. | |
1016 | * | |
1017 | * Arguments: | |
1018 | * host The host to which to connect. | |
1019 | * flags Option flags. | |
80b2cbf2 | 1020 | * |
9c0f2dac RS |
1021 | * Return value: A file descriptor indicating the connection, or -1 |
1022 | * indicating failure, in which case an error has been copied | |
1023 | * into pop_error. | |
1024 | */ | |
1025 | static int | |
1026 | socket_connection (host, flags) | |
1027 | char *host; | |
1028 | int flags; | |
1029 | { | |
33a2a872 JD |
1030 | #ifdef HAVE_GETADDRINFO |
1031 | struct addrinfo *res, *it; | |
1032 | struct addrinfo hints; | |
1033 | int ret; | |
1034 | #else /* !HAVE_GETADDRINFO */ | |
9c0f2dac | 1035 | struct hostent *hostent; |
33a2a872 | 1036 | #endif |
9c0f2dac RS |
1037 | struct servent *servent; |
1038 | struct sockaddr_in addr; | |
1039 | char found_port = 0; | |
1040 | char *service; | |
1041 | int sock; | |
ee15f312 | 1042 | char *realhost; |
9c0f2dac | 1043 | #ifdef KERBEROS |
a27738b6 | 1044 | #ifdef KERBEROS5 |
9c0f2dac | 1045 | krb5_error_code rem; |
a27738b6 RS |
1046 | krb5_context kcontext = 0; |
1047 | krb5_auth_context auth_context = 0; | |
9c0f2dac RS |
1048 | krb5_ccache ccdef; |
1049 | krb5_principal client, server; | |
1050 | krb5_error *err_ret; | |
1051 | register char *cp; | |
1052 | #else | |
1053 | KTEXT ticket; | |
1054 | MSG_DAT msg_data; | |
1055 | CREDENTIALS cred; | |
1056 | Key_schedule schedule; | |
1057 | int rem; | |
a27738b6 | 1058 | #endif /* KERBEROS5 */ |
9c0f2dac RS |
1059 | #endif /* KERBEROS */ |
1060 | ||
1061 | int try_count = 0; | |
33a2a872 | 1062 | int connect_ok; |
9c0f2dac | 1063 | |
c2668a61 RS |
1064 | #ifdef WINDOWSNT |
1065 | { | |
1066 | WSADATA winsockData; | |
1067 | if (WSAStartup (0x101, &winsockData) == 0) | |
1068 | have_winsock = 1; | |
1069 | } | |
1070 | #endif | |
1071 | ||
9c0f2dac RS |
1072 | bzero ((char *) &addr, sizeof (addr)); |
1073 | addr.sin_family = AF_INET; | |
1074 | ||
81264b38 | 1075 | /** "kpop" service is never used: look for 20060515 to see why **/ |
9c0f2dac RS |
1076 | #ifdef KERBEROS |
1077 | service = (flags & POP_NO_KERBEROS) ? POP_SERVICE : KPOP_SERVICE; | |
1078 | #else | |
1079 | service = POP_SERVICE; | |
1080 | #endif | |
1081 | ||
1082 | #ifdef HESIOD | |
1083 | if (! (flags & POP_NO_HESIOD)) | |
1084 | { | |
1085 | servent = hes_getservbyname (service, "tcp"); | |
1086 | if (servent) | |
1087 | { | |
1088 | addr.sin_port = servent->s_port; | |
1089 | found_port = 1; | |
1090 | } | |
1091 | } | |
1092 | #endif | |
1093 | if (! found_port) | |
1094 | { | |
1095 | servent = getservbyname (service, "tcp"); | |
1096 | if (servent) | |
1097 | { | |
1098 | addr.sin_port = servent->s_port; | |
1099 | } | |
1100 | else | |
1101 | { | |
81264b38 | 1102 | /** "kpop" service is never used: look for 20060515 to see why **/ |
9c0f2dac RS |
1103 | #ifdef KERBEROS |
1104 | addr.sin_port = htons ((flags & POP_NO_KERBEROS) ? | |
1105 | POP_PORT : KPOP_PORT); | |
1106 | #else | |
1107 | addr.sin_port = htons (POP_PORT); | |
1108 | #endif | |
1109 | } | |
1110 | } | |
1111 | ||
a27738b6 | 1112 | #define POP_SOCKET_ERROR "Could not create socket for POP connection: " |
9c0f2dac RS |
1113 | |
1114 | sock = socket (PF_INET, SOCK_STREAM, 0); | |
1115 | if (sock < 0) | |
1116 | { | |
a27738b6 | 1117 | strcpy (pop_error, POP_SOCKET_ERROR); |
9c0f2dac | 1118 | strncat (pop_error, strerror (errno), |
a27738b6 | 1119 | ERROR_MAX - sizeof (POP_SOCKET_ERROR)); |
9c0f2dac | 1120 | return (-1); |
80b2cbf2 | 1121 | |
9c0f2dac RS |
1122 | } |
1123 | ||
33a2a872 JD |
1124 | #ifdef HAVE_GETADDRINFO |
1125 | memset (&hints, 0, sizeof(hints)); | |
1126 | hints.ai_socktype = SOCK_STREAM; | |
7291159e | 1127 | hints.ai_flags = AI_CANONNAME; |
33a2a872 JD |
1128 | hints.ai_family = AF_INET; |
1129 | do | |
1130 | { | |
1131 | ret = getaddrinfo (host, service, &hints, &res); | |
1132 | try_count++; | |
1133 | if (ret != 0 && (ret != EAI_AGAIN || try_count == 5)) | |
1134 | { | |
1135 | strcpy (pop_error, "Could not determine POP server's address"); | |
1136 | return (-1); | |
1137 | } | |
1138 | } while (ret != 0); | |
1139 | ||
1140 | if (ret == 0) | |
1141 | { | |
1142 | it = res; | |
1143 | while (it) | |
1144 | { | |
1145 | if (it->ai_addrlen == sizeof (addr)) | |
1146 | { | |
1147 | struct sockaddr_in *in_a = (struct sockaddr_in *) it->ai_addr; | |
1148 | bcopy (&in_a->sin_addr, (char *) &addr.sin_addr, | |
1149 | sizeof (addr.sin_addr)); | |
1150 | if (! connect (sock, (struct sockaddr *) &addr, sizeof (addr))) | |
1151 | break; | |
1152 | } | |
1153 | it = it->ai_next; | |
1154 | } | |
1155 | connect_ok = it != NULL; | |
ee15f312 JD |
1156 | if (connect_ok) |
1157 | { | |
1158 | realhost = alloca (strlen (it->ai_canonname) + 1); | |
1159 | strcpy (realhost, it->ai_canonname); | |
1160 | } | |
33a2a872 JD |
1161 | freeaddrinfo (res); |
1162 | } | |
1163 | #else /* !HAVE_GETADDRINFO */ | |
b72f3bcb EZ |
1164 | do |
1165 | { | |
1166 | hostent = gethostbyname (host); | |
1167 | try_count++; | |
1168 | if ((! hostent) && ((h_errno != TRY_AGAIN) || (try_count == 5))) | |
1169 | { | |
1170 | strcpy (pop_error, "Could not determine POP server's address"); | |
1171 | return (-1); | |
1172 | } | |
1173 | } while (! hostent); | |
1174 | ||
9c0f2dac RS |
1175 | while (*hostent->h_addr_list) |
1176 | { | |
1177 | bcopy (*hostent->h_addr_list, (char *) &addr.sin_addr, | |
1178 | hostent->h_length); | |
1179 | if (! connect (sock, (struct sockaddr *) &addr, sizeof (addr))) | |
1180 | break; | |
1181 | hostent->h_addr_list++; | |
1182 | } | |
33a2a872 | 1183 | connect_ok = *hostent->h_addr_list != NULL; |
ee15f312 JD |
1184 | if (! connect_ok) |
1185 | { | |
1186 | realhost = alloca (strlen (hostent->h_name) + 1); | |
1187 | strcpy (realhost, hostent->h_name); | |
1188 | } | |
1189 | ||
33a2a872 | 1190 | #endif /* !HAVE_GETADDRINFO */ |
9c0f2dac RS |
1191 | |
1192 | #define CONNECT_ERROR "Could not connect to POP server: " | |
80b2cbf2 | 1193 | |
33a2a872 | 1194 | if (! connect_ok) |
9c0f2dac | 1195 | { |
de5c39cf | 1196 | CLOSESOCKET (sock); |
9c0f2dac RS |
1197 | strcpy (pop_error, CONNECT_ERROR); |
1198 | strncat (pop_error, strerror (errno), | |
1199 | ERROR_MAX - sizeof (CONNECT_ERROR)); | |
1200 | return (-1); | |
80b2cbf2 | 1201 | |
9c0f2dac RS |
1202 | } |
1203 | ||
1204 | #ifdef KERBEROS | |
33a2a872 | 1205 | |
9c0f2dac RS |
1206 | #define KRB_ERROR "Kerberos error connecting to POP server: " |
1207 | if (! (flags & POP_NO_KERBEROS)) | |
1208 | { | |
a27738b6 RS |
1209 | #ifdef KERBEROS5 |
1210 | if ((rem = krb5_init_context (&kcontext))) | |
9c0f2dac RS |
1211 | { |
1212 | krb5error: | |
a27738b6 RS |
1213 | if (auth_context) |
1214 | krb5_auth_con_free (kcontext, auth_context); | |
1215 | if (kcontext) | |
1216 | krb5_free_context (kcontext); | |
9c0f2dac RS |
1217 | strcpy (pop_error, KRB_ERROR); |
1218 | strncat (pop_error, error_message (rem), | |
1219 | ERROR_MAX - sizeof(KRB_ERROR)); | |
de5c39cf | 1220 | CLOSESOCKET (sock); |
9c0f2dac RS |
1221 | return (-1); |
1222 | } | |
1223 | ||
a27738b6 RS |
1224 | if ((rem = krb5_auth_con_init (kcontext, &auth_context))) |
1225 | goto krb5error; | |
80b2cbf2 | 1226 | |
a27738b6 RS |
1227 | if (rem = krb5_cc_default (kcontext, &ccdef)) |
1228 | goto krb5error; | |
1229 | ||
1230 | if (rem = krb5_cc_get_principal (kcontext, ccdef, &client)) | |
1231 | goto krb5error; | |
9c0f2dac | 1232 | |
33a2a872 | 1233 | for (cp = realhost; *cp; cp++) |
9c0f2dac RS |
1234 | { |
1235 | if (isupper (*cp)) | |
1236 | { | |
1237 | *cp = tolower (*cp); | |
1238 | } | |
1239 | } | |
1240 | ||
33a2a872 | 1241 | if (rem = krb5_sname_to_principal (kcontext, realhost, |
a27738b6 RS |
1242 | POP_SERVICE, FALSE, &server)) |
1243 | goto krb5error; | |
9c0f2dac | 1244 | |
a27738b6 RS |
1245 | rem = krb5_sendauth (kcontext, &auth_context, |
1246 | (krb5_pointer) &sock, "KPOPV1.0", client, server, | |
9c0f2dac RS |
1247 | AP_OPTS_MUTUAL_REQUIRED, |
1248 | 0, /* no checksum */ | |
1249 | 0, /* no creds, use ccache instead */ | |
1250 | ccdef, | |
9c0f2dac | 1251 | &err_ret, |
a27738b6 | 1252 | 0, /* don't need subsession key */ |
9c0f2dac | 1253 | 0); /* don't need reply */ |
a27738b6 | 1254 | krb5_free_principal (kcontext, server); |
9c0f2dac RS |
1255 | if (rem) |
1256 | { | |
5fe92a72 CY |
1257 | strcpy (pop_error, KRB_ERROR); |
1258 | strncat (pop_error, error_message (rem), | |
1259 | ERROR_MAX - sizeof (KRB_ERROR)); | |
1260 | #if defined HAVE_KRB5_ERROR_TEXT | |
9c0f2dac RS |
1261 | if (err_ret && err_ret->text.length) |
1262 | { | |
9c0f2dac RS |
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 | } | |
5fe92a72 CY |
1271 | #elif defined HAVE_KRB5_ERROR_E_TEXT |
1272 | if (err_ret && err_ret->e_text && strlen(*err_ret->e_text)) | |
9c0f2dac | 1273 | { |
5fe92a72 CY |
1274 | strncat (pop_error, " [server says '", |
1275 | ERROR_MAX - strlen (pop_error) - 1); | |
1276 | strncat (pop_error, *err_ret->e_text, | |
1277 | ERROR_MAX - strlen (pop_error) - 1); | |
1278 | strncat (pop_error, "']", | |
1279 | ERROR_MAX - strlen (pop_error) - 1); | |
9c0f2dac | 1280 | } |
5fe92a72 | 1281 | #endif |
9c0f2dac | 1282 | if (err_ret) |
a27738b6 RS |
1283 | krb5_free_error (kcontext, err_ret); |
1284 | krb5_auth_con_free (kcontext, auth_context); | |
1285 | krb5_free_context (kcontext); | |
9c0f2dac | 1286 | |
de5c39cf | 1287 | CLOSESOCKET (sock); |
9c0f2dac RS |
1288 | return (-1); |
1289 | } | |
80b2cbf2 | 1290 | #else /* ! KERBEROS5 */ |
9c0f2dac | 1291 | ticket = (KTEXT) malloc (sizeof (KTEXT_ST)); |
51a66525 CH |
1292 | rem = krb_sendauth (0L, sock, ticket, "pop", realhost, |
1293 | (char *) krb_realmofhost (realhost), | |
9c0f2dac RS |
1294 | (unsigned long) 0, &msg_data, &cred, schedule, |
1295 | (struct sockaddr_in *) 0, | |
1296 | (struct sockaddr_in *) 0, | |
1297 | "KPOPV0.1"); | |
1298 | free ((char *) ticket); | |
1299 | if (rem != KSUCCESS) | |
1300 | { | |
1301 | strcpy (pop_error, KRB_ERROR); | |
1302 | strncat (pop_error, krb_err_txt[rem], | |
1303 | ERROR_MAX - sizeof (KRB_ERROR)); | |
de5c39cf | 1304 | CLOSESOCKET (sock); |
9c0f2dac RS |
1305 | return (-1); |
1306 | } | |
a27738b6 | 1307 | #endif /* KERBEROS5 */ |
9c0f2dac RS |
1308 | } |
1309 | #endif /* KERBEROS */ | |
1310 | ||
1311 | return (sock); | |
1312 | } /* socket_connection */ | |
1313 | ||
1314 | /* | |
60b0a379 | 1315 | * Function: pop_getline |
9c0f2dac RS |
1316 | * |
1317 | * Purpose: Get a line of text from the connection and return a | |
1318 | * pointer to it. The carriage return and linefeed at the end of | |
1319 | * the line are stripped, but periods at the beginnings of lines | |
1320 | * are NOT dealt with in any special way. | |
1321 | * | |
1322 | * Arguments: | |
1323 | * server The server from which to get the line of text. | |
1324 | * | |
d89d0243 KH |
1325 | * Returns: The number of characters in the line, which is returned in |
1326 | * LINE, not including the final null. A return value of 0 | |
1327 | * indicates a blank line. A negative return value indicates an | |
1328 | * error (in which case the contents of LINE are undefined. In | |
1329 | * case of error, an error message is copied into pop_error. | |
9c0f2dac | 1330 | * |
60b0a379 | 1331 | * Notes: The line returned is overwritten with each call to pop_getline. |
9c0f2dac RS |
1332 | * |
1333 | * Side effects: Closes the connection on error. | |
d89d0243 KH |
1334 | * |
1335 | * THE RETURNED LINE MAY CONTAIN EMBEDDED NULLS! | |
9c0f2dac | 1336 | */ |
d89d0243 | 1337 | static int |
60b0a379 | 1338 | pop_getline (server, line) |
9c0f2dac | 1339 | popserver server; |
d89d0243 | 1340 | char **line; |
9c0f2dac RS |
1341 | { |
1342 | #define GETLINE_ERROR "Error reading from server: " | |
1343 | ||
1344 | int ret; | |
b6606ad8 RS |
1345 | int search_offset = 0; |
1346 | ||
9c0f2dac RS |
1347 | if (server->data) |
1348 | { | |
d89d0243 KH |
1349 | char *cp = find_crlf (server->buffer + server->buffer_index, |
1350 | server->data); | |
9c0f2dac RS |
1351 | if (cp) |
1352 | { | |
1353 | int found; | |
1354 | int data_used; | |
1355 | ||
1356 | found = server->buffer_index; | |
1357 | data_used = (cp + 2) - server->buffer - found; | |
80b2cbf2 | 1358 | |
9c0f2dac RS |
1359 | *cp = '\0'; /* terminate the string to be returned */ |
1360 | server->data -= data_used; | |
1361 | server->buffer_index += data_used; | |
1362 | ||
1363 | if (pop_debug) | |
d89d0243 KH |
1364 | /* Embedded nulls will truncate this output prematurely, |
1365 | but that's OK because it's just for debugging anyway. */ | |
9c0f2dac | 1366 | fprintf (stderr, "<<< %s\n", server->buffer + found); |
d89d0243 KH |
1367 | *line = server->buffer + found; |
1368 | return (data_used - 2); | |
9c0f2dac RS |
1369 | } |
1370 | else | |
1371 | { | |
1372 | bcopy (server->buffer + server->buffer_index, | |
1373 | server->buffer, server->data); | |
b6606ad8 RS |
1374 | /* Record the fact that we've searched the data already in |
1375 | the buffer for a CRLF, so that when we search below, we | |
1376 | don't have to search the same data twice. There's a "- | |
1377 | 1" here to account for the fact that the last character | |
1378 | of the data we have may be the CR of a CRLF pair, of | |
1379 | which we haven't read the second half yet, so we may have | |
1380 | to search it again when we read more data. */ | |
1381 | search_offset = server->data - 1; | |
9c0f2dac RS |
1382 | server->buffer_index = 0; |
1383 | } | |
1384 | } | |
1385 | else | |
1386 | { | |
1387 | server->buffer_index = 0; | |
1388 | } | |
1389 | ||
1390 | while (1) | |
1391 | { | |
bbcac09c RS |
1392 | /* There's a "- 1" here to leave room for the null that we put |
1393 | at the end of the read data below. We put the null there so | |
1394 | that find_crlf knows where to stop when we call it. */ | |
1395 | if (server->data == server->buffer_size - 1) | |
9c0f2dac | 1396 | { |
bbcac09c RS |
1397 | server->buffer_size += GETLINE_INCR; |
1398 | server->buffer = (char *)realloc (server->buffer, server->buffer_size); | |
1399 | if (! server->buffer) | |
9c0f2dac | 1400 | { |
bbcac09c RS |
1401 | strcpy (pop_error, "Out of memory in pop_getline"); |
1402 | pop_trash (server); | |
1403 | return (-1); | |
0ea95737 | 1404 | } |
0ea95737 | 1405 | } |
bbcac09c RS |
1406 | ret = RECV (server->file, server->buffer + server->data, |
1407 | server->buffer_size - server->data - 1, 0); | |
9c0f2dac RS |
1408 | if (ret < 0) |
1409 | { | |
1410 | strcpy (pop_error, GETLINE_ERROR); | |
1411 | strncat (pop_error, strerror (errno), | |
1412 | ERROR_MAX - sizeof (GETLINE_ERROR)); | |
1413 | pop_trash (server); | |
d89d0243 | 1414 | return (-1); |
9c0f2dac RS |
1415 | } |
1416 | else if (ret == 0) | |
1417 | { | |
60b0a379 | 1418 | strcpy (pop_error, "Unexpected EOF from server in pop_getline"); |
9c0f2dac | 1419 | pop_trash (server); |
d89d0243 | 1420 | return (-1); |
9c0f2dac RS |
1421 | } |
1422 | else | |
1423 | { | |
7a481e50 | 1424 | char *cp; |
9c0f2dac | 1425 | server->data += ret; |
7a481e50 | 1426 | server->buffer[server->data] = '\0'; |
80b2cbf2 | 1427 | |
d89d0243 KH |
1428 | cp = find_crlf (server->buffer + search_offset, |
1429 | server->data - search_offset); | |
9c0f2dac RS |
1430 | if (cp) |
1431 | { | |
1432 | int data_used = (cp + 2) - server->buffer; | |
1433 | *cp = '\0'; | |
1434 | server->data -= data_used; | |
1435 | server->buffer_index = data_used; | |
1436 | ||
1437 | if (pop_debug) | |
1438 | fprintf (stderr, "<<< %s\n", server->buffer); | |
d89d0243 KH |
1439 | *line = server->buffer; |
1440 | return (data_used - 2); | |
9c0f2dac | 1441 | } |
a8f44d07 RS |
1442 | /* As above, the "- 1" here is to account for the fact that |
1443 | we may have read a CR without its accompanying LF. */ | |
1444 | search_offset += ret - 1; | |
9c0f2dac RS |
1445 | } |
1446 | } | |
1447 | ||
1448 | /* NOTREACHED */ | |
1449 | } | |
1450 | ||
1451 | /* | |
1452 | * Function: sendline | |
1453 | * | |
1454 | * Purpose: Sends a line of text to the POP server. The line of text | |
1455 | * passed into this function should NOT have the carriage return | |
1456 | * and linefeed on the end of it. Periods at beginnings of lines | |
1457 | * will NOT be treated specially by this function. | |
1458 | * | |
1459 | * Arguments: | |
1460 | * server The server to which to send the text. | |
1461 | * line The line of text to send. | |
1462 | * | |
1463 | * Return value: Upon successful completion, a value of 0 will be | |
1464 | * returned. Otherwise, a non-zero value will be returned, and | |
1465 | * an error will be copied into pop_error. | |
1466 | * | |
1467 | * Side effects: Closes the connection on error. | |
1468 | */ | |
1469 | static int | |
1470 | sendline (server, line) | |
1471 | popserver server; | |
1472 | char *line; | |
1473 | { | |
1474 | #define SENDLINE_ERROR "Error writing to POP server: " | |
1475 | int ret; | |
ec641d50 KR |
1476 | char *buf; |
1477 | ||
1478 | /* Combine the string and the CR-LF into one buffer. Otherwise, two | |
1479 | reasonable network stack optimizations, Nagle's algorithm and | |
1480 | delayed acks, combine to delay us a fraction of a second on every | |
1481 | message we send. (Movemail writes line without \r\n, client | |
1482 | kernel sends packet, server kernel delays the ack to see if it | |
1483 | can combine it with data, movemail writes \r\n, client kernel | |
1484 | waits because it has unacked data already in its outgoing queue, | |
1485 | client kernel eventually times out and sends.) | |
1486 | ||
1487 | This can be something like 0.2s per command, which can add up | |
1488 | over a few dozen messages, and is a big chunk of the time we | |
1489 | spend fetching mail from a server close by. */ | |
1490 | buf = alloca (strlen (line) + 3); | |
1491 | strcpy (buf, line); | |
1492 | strcat (buf, "\r\n"); | |
1493 | ret = fullwrite (server->file, buf, strlen (buf)); | |
9c0f2dac RS |
1494 | |
1495 | if (ret < 0) | |
1496 | { | |
1497 | pop_trash (server); | |
1498 | strcpy (pop_error, SENDLINE_ERROR); | |
1499 | strncat (pop_error, strerror (errno), | |
1500 | ERROR_MAX - sizeof (SENDLINE_ERROR)); | |
1501 | return (ret); | |
1502 | } | |
1503 | ||
1504 | if (pop_debug) | |
1505 | fprintf (stderr, ">>> %s\n", line); | |
1506 | ||
1507 | return (0); | |
1508 | } | |
1509 | ||
1510 | /* | |
1511 | * Procedure: fullwrite | |
1512 | * | |
1513 | * Purpose: Just like write, but keeps trying until the entire string | |
1514 | * has been written. | |
1515 | * | |
1516 | * Return value: Same as write. Pop_error is not set. | |
1517 | */ | |
1518 | static int | |
1519 | fullwrite (fd, buf, nbytes) | |
1520 | int fd; | |
1521 | char *buf; | |
1522 | int nbytes; | |
1523 | { | |
1524 | char *cp; | |
9d3f4cee | 1525 | int ret = 0; |
9c0f2dac RS |
1526 | |
1527 | cp = buf; | |
9d3f4cee | 1528 | while (nbytes && ((ret = SEND (fd, cp, nbytes, 0)) > 0)) |
9c0f2dac RS |
1529 | { |
1530 | cp += ret; | |
1531 | nbytes -= ret; | |
1532 | } | |
1533 | ||
1534 | return (ret); | |
1535 | } | |
1536 | ||
1537 | /* | |
1538 | * Procedure getok | |
1539 | * | |
1540 | * Purpose: Reads a line from the server. If the return indicator is | |
1541 | * positive, return with a zero exit status. If not, return with | |
1542 | * a negative exit status. | |
1543 | * | |
1544 | * Arguments: | |
1545 | * server The server to read from. | |
80b2cbf2 | 1546 | * |
9c0f2dac RS |
1547 | * Returns: 0 for success, else for failure and puts error in pop_error. |
1548 | * | |
8e6208c5 | 1549 | * Side effects: On failure, may make the connection unusable. |
9c0f2dac RS |
1550 | */ |
1551 | static int | |
1552 | getok (server) | |
1553 | popserver server; | |
1554 | { | |
1555 | char *fromline; | |
1556 | ||
60b0a379 | 1557 | if (pop_getline (server, &fromline) < 0) |
9c0f2dac RS |
1558 | { |
1559 | return (-1); | |
1560 | } | |
1561 | ||
1562 | if (! strncmp (fromline, "+OK", 3)) | |
1563 | return (0); | |
1564 | else if (! strncmp (fromline, "-ERR", 4)) | |
1565 | { | |
1566 | strncpy (pop_error, fromline, ERROR_MAX); | |
1567 | pop_error[ERROR_MAX-1] = '\0'; | |
1568 | return (-1); | |
1569 | } | |
1570 | else | |
1571 | { | |
1572 | strcpy (pop_error, | |
1573 | "Unexpected response from server; expecting +OK or -ERR"); | |
1574 | pop_trash (server); | |
1575 | return (-1); | |
1576 | } | |
80b2cbf2 | 1577 | } |
9c0f2dac RS |
1578 | |
1579 | #if 0 | |
1580 | /* | |
1581 | * Function: gettermination | |
1582 | * | |
1583 | * Purpose: Gets the next line and verifies that it is a termination | |
1584 | * line (nothing but a dot). | |
1585 | * | |
1586 | * Return value: 0 on success, non-zero with pop_error set on error. | |
1587 | * | |
1588 | * Side effects: Closes the connection on error. | |
1589 | */ | |
1590 | static int | |
1591 | gettermination (server) | |
1592 | popserver server; | |
1593 | { | |
1594 | char *fromserver; | |
1595 | ||
60b0a379 | 1596 | if (pop_getline (server, &fromserver) < 0) |
9c0f2dac RS |
1597 | return (-1); |
1598 | ||
1599 | if (strcmp (fromserver, ".")) | |
1600 | { | |
1601 | strcpy (pop_error, | |
1602 | "Unexpected response from server in gettermination"); | |
1603 | pop_trash (server); | |
1604 | return (-1); | |
1605 | } | |
1606 | ||
1607 | return (0); | |
1608 | } | |
1609 | #endif | |
1610 | ||
1611 | /* | |
1612 | * Function pop_close | |
1613 | * | |
1614 | * Purpose: Close a pop connection, sending a "RSET" command to try to | |
1615 | * preserve any changes that were made and a "QUIT" command to | |
1616 | * try to get the server to quit, but ignoring any responses that | |
1617 | * are received. | |
1618 | * | |
8e6208c5 | 1619 | * Side effects: The server is unusable after this function returns. |
9c0f2dac RS |
1620 | * Changes made to the maildrop since the session was started (or |
1621 | * since the last pop_reset) may be lost. | |
1622 | */ | |
80b2cbf2 | 1623 | void |
9c0f2dac RS |
1624 | pop_close (server) |
1625 | popserver server; | |
1626 | { | |
1627 | pop_trash (server); | |
1628 | free ((char *) server); | |
1629 | ||
1630 | return; | |
1631 | } | |
1632 | ||
1633 | /* | |
1634 | * Function: pop_trash | |
1635 | * | |
1636 | * Purpose: Like pop_close or pop_quit, but doesn't deallocate the | |
4d90eee4 | 1637 | * memory associated with the server. It is valid to call |
9c0f2dac RS |
1638 | * pop_close or pop_quit after this function has been called. |
1639 | */ | |
1640 | static void | |
1641 | pop_trash (server) | |
1642 | popserver server; | |
1643 | { | |
1644 | if (server->file >= 0) | |
1645 | { | |
c2668a61 RS |
1646 | /* avoid recursion; sendline can call pop_trash */ |
1647 | if (server->trash_started) | |
1648 | return; | |
1649 | server->trash_started = 1; | |
de5c39cf | 1650 | |
9c0f2dac RS |
1651 | sendline (server, "RSET"); |
1652 | sendline (server, "QUIT"); | |
1653 | ||
de5c39cf | 1654 | CLOSESOCKET (server->file); |
9c0f2dac RS |
1655 | server->file = -1; |
1656 | if (server->buffer) | |
1657 | { | |
1658 | free (server->buffer); | |
1659 | server->buffer = 0; | |
1660 | } | |
1661 | } | |
c2668a61 RS |
1662 | |
1663 | #ifdef WINDOWSNT | |
1664 | if (have_winsock) | |
1665 | WSACleanup (); | |
1666 | #endif | |
9c0f2dac RS |
1667 | } |
1668 | ||
d89d0243 KH |
1669 | /* Return a pointer to the first CRLF in IN_STRING, which can contain |
1670 | embedded nulls and has LEN characters in it not including the final | |
1671 | null, or 0 if it does not contain one. */ | |
9c0f2dac RS |
1672 | |
1673 | static char * | |
d89d0243 | 1674 | find_crlf (in_string, len) |
8148baf9 | 1675 | char *in_string; |
d89d0243 | 1676 | int len; |
9c0f2dac | 1677 | { |
d89d0243 | 1678 | while (len--) |
9c0f2dac | 1679 | { |
d89d0243 | 1680 | if (*in_string == '\r') |
8148baf9 RS |
1681 | { |
1682 | if (*++in_string == '\n') | |
1683 | return (in_string - 1); | |
1684 | } | |
1685 | else | |
1686 | in_string++; | |
9c0f2dac | 1687 | } |
d89d0243 | 1688 | return (0); |
9c0f2dac RS |
1689 | } |
1690 | ||
1691 | #endif /* MAIL_USE_POP */ | |
ab5796a9 MB |
1692 | |
1693 | /* arch-tag: ceb37041-b7ad-49a8-a63d-286618b8367d | |
1694 | (do not change this comment) */ |