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