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