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