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