smtp.c 20.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833
/* GNU mailutils - a suite of utilities for electronic mail
   Copyright (C) 1999, 2000 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Library Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <sys/types.h>
#include <stdio.h>
#include <pwd.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>

#include <mailutils/stream.h>
#include <mailutils/address.h>
#include <mailer0.h>
#include <registrar0.h>
#include <bio.h>

static struct _record _smtp_record =
{
  MU_SMTP_SCHEME,
  _url_smtp_init,      /* url init.  */
  NULL,                /* Mailbox init.  */
  &_mailer_smtp_init,  /* Mailer init.  */
  NULL, /* Folder init.  */
  NULL, /* No need for a back pointer.  */
  NULL, /* _is_scheme method.  */
  NULL, /* _get_url method.  */
  NULL, /* _get_mailbox method.  */
  NULL, /* _get_mailer method.  */
  NULL  /* _get_folder method.  */
};
/* We export : url parsing and the initialisation of
   the mailbox, via the register entry/record.  */
record_t smtp_record = &_smtp_record;

struct _smtp
{
  char *mailhost;
  char *localhost;
  bio_t bio;
  char *ptr;
  char *nl;
  char *buffer;
  size_t buflen;
  enum smtp_state
  {
    SMTP_NO_STATE, SMTP_OPEN, SMTP_GREETINGS, SMTP_EHLO, SMTP_EHLO_ACK,
    SMTP_HELO, SMTP_HELO_ACK, SMTP_QUIT, SMTP_QUIT_ACK, SMTP_ENV_FROM,
    SMTP_ENV_RCPT, SMTP_MAIL_FROM, SMTP_MAIL_FROM_ACK, SMTP_RCPT_TO,
    SMTP_RCPT_TO_ACK, SMTP_DATA, SMTP_DATA_ACK, SMTP_SEND, SMTP_SEND_ACK,
    SMTP_SEND_DOT
  } state;
  int extended;
  address_t mail_from;
  address_t rcpt_to;
  size_t rcpt_index;
  off_t offset;
  int dsn;
  message_t message;
};

typedef struct _smtp * smtp_t;

/* Usefull little Macros, since these are very repetitive.  */
#define CLEAR_STATE(smtp) \
 smtp->state = SMTP_NO_STATE

/* Clear the state and close the stream.  */
#define CHECK_ERROR_CLOSE(mailer, smtp, status) \
do \
  { \
     if (status != 0) \
       { \
          stream_close (mailer->stream); \
          CLEAR_STATE (smtp); \
          return status; \
       } \
  } \
while (0)

/* Clear the state.  */
#define CHECK_ERROR(smtp, status) \
do \
  { \
     if (status != 0) \
       { \
          CLEAR_STATE (smtp); \
          return status; \
       } \
  } \
while (0)

/* Clear the state for non recoverable error.  */
#define CHECK_EAGAIN(smtp, status) \
do \
  { \
    if (status != 0) \
      { \
         if (status != EAGAIN && status != EINPROGRESS && status != EINTR) \
           { \
             CLEAR_STATE (smtp); \
           } \
         return status; \
      } \
   }  \
while (0)

static void smtp_destroy (mailer_t);
static int smtp_open (mailer_t, int);
static int smtp_close (mailer_t);
static int smtp_send_message (mailer_t, message_t);
static int smtp_writeline (smtp_t smtp, const char *format, ...);
static int smtp_readline (smtp_t);
static int smtp_read_ack (smtp_t);
static int smtp_write (smtp_t);

static int get_rcpt (message_t , address_t *);
static int get_from (message_t , char *, address_t *);

int
_mailer_smtp_init (mailer_t mailer)
{
  smtp_t smtp;

  /* Allocate memory specific to smtp mailer.  */
  smtp = mailer->data = calloc (1, sizeof (*smtp));
  if (mailer->data == NULL)
    return ENOMEM;
  mailer->_destroy = smtp_destroy;
  mailer->_open = smtp_open;
  mailer->_close = smtp_close;
  mailer->_send_message = smtp_send_message;

  return 0;
}

static void
smtp_destroy(mailer_t mailer)
{
  smtp_t smtp = mailer->data;
  smtp_close (mailer);
  if (smtp->mailhost)
    free (smtp->mailhost);
  if (smtp->localhost)
    free (smtp->localhost);
  if (smtp->bio)
    bio_destroy (&(smtp->bio));
  if (smtp->buffer)
    free (smtp->buffer);
  if (smtp->mail_from)
    address_destroy (&(smtp->mail_from));
  if (smtp->rcpt_to)
    address_destroy (&(smtp->rcpt_to));
  free (smtp);
  mailer->data = NULL;
}

static int
smtp_open (mailer_t mailer, int flags)
{
  smtp_t smtp = mailer->data;
  int status;
  long port;
  size_t buf_len = 0;

  /* Sanity checks.  */
  if (smtp == NULL)
    return EINVAL;

  mailer->flags = flags | MU_STREAM_SMTP;

  /* Fetch the mailer server name and the port in the url_t.  */
  if ((status = url_get_host (mailer->url, NULL, 0, &buf_len)) != 0
      || buf_len == 0 || (status = url_get_port (mailer->url, &port)) != 0)
    return status;

  switch (smtp->state)
    {
    case SMTP_NO_STATE:
      /* Get the mailhost.  */
      if (smtp->mailhost)
	{
	  free (smtp->mailhost);
	  smtp->mailhost = NULL;
	}
      smtp->mailhost = calloc (buf_len + 1, sizeof (char));
      if (smtp->mailhost == NULL)
	return ENOMEM;
      url_get_host (mailer->url, smtp->mailhost, buf_len + 1, NULL);

      if (smtp->localhost)
	{
	  free (smtp->localhost);
	  smtp->localhost = NULL;
	}
      /* Fetch our localhost name.  */
      buf_len = 64;
      do
	{
	  char *tmp;
	  errno = 0;
	  buf_len *= 2;    /* Initial guess */
	  tmp = realloc (smtp->localhost, buf_len);
	  if (tmp == NULL)
	    {
	      if (smtp->localhost)
		free (smtp->localhost);
	      smtp->localhost = NULL;
	      free (smtp->mailhost);
	      smtp->mailhost = NULL;
	      return ENOMEM;
	    }
	  smtp->localhost = tmp;
	}
      while (((status = gethostname(smtp->localhost, buf_len)) == 0
	      && !memchr (smtp->localhost, '\0', buf_len))
#ifdef ENAMETOOLONG
	     || errno == ENAMETOOLONG
#endif
	     );
      if (status != 0 && errno != 0)
	{
	  /* gethostname failed, abort.  */
	  free (smtp->localhost);
	  smtp->localhost = NULL;
	  free (smtp->mailhost);
	  smtp->mailhost = NULL;
	  return EINVAL;
	}

      /* Many SMTP servers prefer a FQDN.  */
      if (strchr (smtp->localhost, '.') == NULL)
	{
	  struct hostent *hp = gethostbyname (smtp->localhost);
	  if (hp == NULL)
	    {
	      /* Don't flag it as an error some SMTP servers can get the FQDN
		 by themselves even if the client is lying, probably
		 with getpeername().  */
	      // return EINVAL;
	    }
	  else
	    {
	      free (smtp->localhost);
	      smtp->localhost = strdup (hp->h_name);
	      if (smtp->localhost == NULL)
		{
		  free (smtp->mailhost);
		  smtp->mailhost = NULL;
		  return ENOMEM;
		}
	    }
	}

      /* allocate a working io buffer.  */
      if (smtp->buffer == NULL)
        {
          smtp->buflen = 512; /* Initial guess.  */
          smtp->buffer = malloc (smtp->buflen + 1);
          if (smtp->buffer == NULL)
            {
              CHECK_ERROR (smtp, ENOMEM);
            }
          smtp->ptr = smtp->buffer;
        }

      /* Create a TCP stack if one is not given.  */
      if (mailer->stream == NULL)
	{
	  status = tcp_stream_create (&(mailer->stream));
	  CHECK_ERROR (smtp, status);
	}
      status = bio_create (&(smtp->bio), mailer->stream);
      CHECK_ERROR (smtp, status);
      smtp->state = SMTP_OPEN;

    case SMTP_OPEN:
      MAILER_DEBUG2 (mailer, MU_DEBUG_PROT, "smtp_open (%s:%d)\n", smtp->mailhost, port);
      status = stream_open (mailer->stream, smtp->mailhost, port,
			    mailer->flags);
      CHECK_EAGAIN (smtp, status);
      smtp->state = SMTP_GREETINGS;

    case SMTP_GREETINGS:
      /* Swallow the greetings.  */
      status = smtp_read_ack (smtp);
      CHECK_EAGAIN (smtp, status);
      MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
      if (smtp->buffer[0] != '2')
	{
	  stream_close (mailer->stream);
	  return EACCES;
	}
      status = smtp_writeline (smtp, "EHLO %s\r\n", smtp->localhost);
      CHECK_ERROR (smtp, status);
      MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
      smtp->state = SMTP_EHLO;

    case SMTP_EHLO:
      /* We first try Extended SMTP.  */
      status = smtp_write (smtp);
      CHECK_EAGAIN (smtp, status);
      smtp->state = SMTP_EHLO_ACK;

    case SMTP_EHLO_ACK:
      status = smtp_read_ack (smtp);
      CHECK_EAGAIN (smtp, status);
      MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
      if (smtp->buffer[0] != '2')
	{
	  smtp->extended = 0;
	  status = smtp_writeline (smtp, "HELO %s\r\n", smtp->localhost);
	  CHECK_ERROR (smtp, status);
	  smtp->state = SMTP_HELO;
	}
      else
	{
	  smtp->extended = 1;
	  break;
	}

    case SMTP_HELO:
      if (!smtp->extended)
	{
	  status = smtp_write (smtp);
	  CHECK_EAGAIN (smtp, status);
	}
      smtp->state = SMTP_HELO_ACK;

    case SMTP_HELO_ACK:
      if (!smtp->extended)
	{
	  status = smtp_read_ack (smtp);
	  CHECK_EAGAIN (smtp, status);
	  MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
	  if (smtp->buffer[0] != '2')
	    {
	      stream_close (mailer->stream);
	      CLEAR_STATE (smtp);
	      return EACCES;
	    }
	}

    default:
      break;
    }
  smtp->state = SMTP_NO_STATE;
  return 0;
}

static int
smtp_close (mailer_t mailer)
{
  smtp_t smtp = mailer->data;
  int status;
  switch (smtp->state)
    {
    case SMTP_NO_STATE:
      status = smtp_writeline (smtp, "Quit\r\n");
      CHECK_ERROR (smtp, status);
      MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
      smtp->state = SMTP_QUIT;

    case SMTP_QUIT:
      status = smtp_write (smtp);
      CHECK_EAGAIN (smtp, status);
      smtp->state = SMTP_QUIT_ACK;

    case SMTP_QUIT_ACK:
      status = smtp_read_ack (smtp);
      CHECK_EAGAIN (smtp, status);
      MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);

    default:
      break;
    }
  return stream_close (mailer->stream);
}

static int
smtp_send_message(mailer_t mailer, message_t msg)
{
  smtp_t smtp = mailer->data;
  int status;

  if (smtp == NULL || msg == NULL)
    return EINVAL;

  switch (smtp->state)
    {
    case SMTP_NO_STATE:
      smtp->state = SMTP_ENV_FROM;
      status = get_from (msg, smtp->localhost, &smtp->mail_from);
      CHECK_ERROR (smtp, status);
      status = get_rcpt (msg, &smtp->rcpt_to);
      CHECK_ERROR (smtp, status);

    case SMTP_ENV_FROM:
      {
	size_t len = 0;
	char *from;
	address_get_email (smtp->mail_from, 1, NULL, 0, &len);
	if (len == 0)
	  CHECK_ERROR (smtp, EINVAL);
	from = calloc (len + 1, sizeof (char));
	if (from == NULL)
	  CHECK_ERROR (smtp, ENOMEM);
	address_get_email (smtp->mail_from, 1, from, len + 1, NULL);
	status = smtp_writeline (smtp, "MAIL FROM: %s\r\n", from);
	free (from);
	address_destroy (&smtp->mail_from);
	CHECK_ERROR (smtp, status);
	MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
	smtp->state = SMTP_MAIL_FROM;
      }

    case SMTP_MAIL_FROM:
      status = smtp_write (smtp);
      CHECK_EAGAIN (smtp, status);
      smtp->state = SMTP_MAIL_FROM_ACK;

    case SMTP_MAIL_FROM_ACK:
      status = smtp_read_ack (smtp);
      CHECK_EAGAIN (smtp, status);
      MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
      if (smtp->buffer[0] != '2')
	{
	  stream_close (mailer->stream);
	  CLEAR_STATE (smtp);
	  return EACCES;
	}

      /* We use a goto, since we may have multiple recipients,
	 we come back here and doit all over again ... Not pretty.  */
    case SMTP_ENV_RCPT:
    RCPT_TO:
      {
	size_t i = 0;
	smtp->rcpt_index++;
	address_get_count (smtp->rcpt_to, &i);
	if (smtp->rcpt_index <= i)
	  {
	    size_t len = 0;
	    char *to;
	    address_get_email (smtp->rcpt_to, smtp->rcpt_index, NULL, 0, &len);
	    if (len == 0)
	      CHECK_ERROR (smtp, EINVAL);
	    to = calloc (len + 1, sizeof (char));
	    if (to == NULL)
	      CHECK_ERROR (smtp, ENOMEM);
	    address_get_email (smtp->rcpt_to, smtp->rcpt_index, to, len + 1, NULL);
	    status = smtp_writeline (smtp, "RCPT TO: %s\r\n", to);
	    free (to);
	    CHECK_ERROR (smtp, status);
	    MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
	    smtp->state = SMTP_RCPT_TO;
	  }
	else
	  {
	    address_destroy (&(smtp->rcpt_to));
	    smtp->rcpt_index = 0;
	    smtp->state = SMTP_DATA;
	  }
      }

    case SMTP_RCPT_TO:
      if (smtp->rcpt_to)
	{
	  status = smtp_write (smtp);
	  CHECK_EAGAIN (smtp, status);
	  smtp->state = SMTP_RCPT_TO_ACK;
	}

    case SMTP_RCPT_TO_ACK:
      if (smtp->rcpt_to)
	{
	  status = smtp_read_ack (smtp);
	  CHECK_EAGAIN (smtp, status);
	  MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
	  if (smtp->buffer[0] != '2')
	    {
	      stream_close (mailer->stream);
	      CLEAR_STATE (smtp);
	      return EACCES;
	    }
	  goto RCPT_TO;
	}
      /* We are done with the rcpt.  */
      status = smtp_writeline (smtp, "DATA\r\n");
      CHECK_ERROR (smtp, status);
      MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
      smtp->state = SMTP_DATA;

    case SMTP_DATA:
      status = smtp_write (smtp);
      CHECK_EAGAIN (smtp, status);
      smtp->state = SMTP_DATA_ACK;

    case SMTP_DATA_ACK:
      status = smtp_read_ack (smtp);
      CHECK_EAGAIN (smtp, status);
      MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
      if (smtp->buffer[0] != '3')
	{
	  stream_close (mailer->stream);
	  CLEAR_STATE (smtp);
	  return EACCES;
	}
      smtp->offset = 0;
      smtp->state = SMTP_SEND;

    case SMTP_SEND:
      {
	stream_t stream;
	size_t n = 0;
	char data[256] = "";
	/* We may be here after an EAGAIN so check if we have something
	   in the buffer and flush it.  */
	status = smtp_write (smtp);
	CHECK_EAGAIN (smtp, status);
	message_get_stream (msg, &stream);
	while ((status = stream_readline (stream, data, sizeof (data) - 1,
					  smtp->offset, &n)) == 0 && n > 0)
	  {
	    if (data [n - 1] == '\n')
	      data [n -1] = '\0';
	    if (data[0] == '.')
	      {
		status = smtp_writeline (smtp, ".%s\r\n", data);
		CHECK_ERROR (smtp, status);
	      }
	    else
	      {
		status = smtp_writeline (smtp, "%s\r\n", data);
		CHECK_ERROR (smtp, status);
	      }
	    smtp->offset += n;
	    MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
		status = smtp_write (smtp);
	    CHECK_EAGAIN (smtp, status);
	  }
	smtp->offset = 0;
	status = smtp_writeline (smtp, ".\r\n");
	CHECK_ERROR (smtp, status);
	smtp->state = SMTP_SEND_DOT;
      }

    case SMTP_SEND_DOT:
      status = smtp_write (smtp);
      CHECK_EAGAIN (smtp, status);
      smtp->state = SMTP_SEND_ACK;

    case SMTP_SEND_ACK:
      status = smtp_read_ack (smtp);
      CHECK_EAGAIN (smtp, status);
      MAILER_DEBUG0 (mailer, MU_DEBUG_PROT, smtp->buffer);
      observable_notify (mailer->observable, MU_EVT_MAILER_MESSAGE_SENT);
      if (smtp->buffer[0] != '2')
	{
	  stream_close (mailer->stream);
	  CLEAR_STATE (smtp);
	  return EACCES;
	}

    default:
      break;
    }
  CLEAR_STATE (smtp);
  return 0;
}

static int
get_from (message_t msg, char *localhost, address_t *pmail_from)
{
  int status;
  size_t size = 0;
  char *from;
  header_t header = NULL;

  message_get_header (msg, &header);
  status = header_get_value (header, MU_HEADER_FROM, NULL, 0, &size);
  /* If it's not in the header create one form the passwd.  */
  if (status != 0 || size == 0)
    {
      struct passwd *pwd = getpwuid (getuid ());
      /* Not in the passwd ???? We have a problem.  */
      if (pwd == 0 || pwd->pw_name == NULL)
	{
	  size = 10 + strlen (localhost) + 1;
	  from = calloc (size, sizeof (char));
	  if (from == NULL)
	    return ENOMEM;
	  snprintf (from, size, "%d@%s", getuid(), localhost);
	}
      else
	{
	  from  = calloc (strlen (pwd->pw_name) + 1
			  + strlen (localhost) + 1, sizeof (char));
	  if (from == NULL)
	    return ENOMEM;
	  sprintf(from, "%s@%s", pwd->pw_name, localhost);
	}
    }
  else
    {
      from = calloc (size + 1, sizeof (char));
      if (from == NULL)
	return ENOMEM;
      header_get_value (header, MU_HEADER_FROM, from, size + 1, NULL);
    }
  /* Check if a Fully Qualified Name, some smtp servers
     notably sendmail insists on it, for good reasons.  */
  if (strchr (from, '@') == NULL)
    {
      char *tmp;
      tmp = malloc (strlen (from) + 1 +strlen (localhost) + 1);
      if (tmp == NULL)
	{
	  free (from);
	  return ENOMEM;
	}
      sprintf (tmp, "%s@%s", from, localhost);
      free (from);
      from = tmp;
    }
  status = address_create (pmail_from, from);
  free (from);
  return status;
}

static int
get_rcpt (message_t msg, address_t *prcpt_to)
{
  char *rcpt;
  int status;
  size_t size = 0;
  header_t header = NULL;

  message_get_header (msg, &header);
  status = header_get_value (header, MU_HEADER_TO, NULL, 0, &size);
  if (status == 0 && size != 0)
    {
      char *tmp;
      size_t len;
      rcpt = calloc (size + 1, sizeof (char));
      if (rcpt == NULL)
	return ENOMEM;
      header_get_value (header, MU_HEADER_TO, rcpt, size + 1, NULL);

      size = 0;
      status = header_get_value (header, MU_HEADER_CC, NULL, 0, &size);
      if (status == 0 && size != 0)
	{
	  len = strlen (rcpt);
	  tmp = realloc (rcpt, (len + 1 + size + 1) * sizeof (char));
	  if (tmp == NULL)
	    {
	      free (rcpt);
	      return ENOMEM;
	    }
	  else
	    rcpt = tmp;
	  rcpt[len] = ',';
	  header_get_value (header, MU_HEADER_CC, rcpt + len + 1,
			    size + 1, NULL);

	  size = 0;
	  status = header_get_value (header, MU_HEADER_BCC, NULL, 0, &size);
	  if (status == 0 && size != 0)
	    {
	      len = strlen (rcpt);
	      tmp = realloc (rcpt, (len + 1 + size + 1) * sizeof (char));
	      if (tmp == NULL)
		{
		  free (rcpt);
		  return ENOMEM;
		}
	      else
		rcpt = tmp;
	      rcpt[len] = ',';
	      header_get_value (header, MU_HEADER_BCC, rcpt + len + 1,
				size + 1, NULL);
	    }
	}
      status = address_create (prcpt_to, rcpt);
      free (rcpt);
      return status;
    }
  return EINVAL;
}

/* C99 says that a conforming implementations of snprintf ()
   should return the number of char that would have been call
   but many GNU/Linux && BSD implementations return -1 on error.
   Worse QNX/Neutrino actually does not put the terminal
   null char.  So let's try to cope.  */
static int
smtp_writeline (smtp_t smtp, const char *format, ...)
{
  int len;
  va_list ap;
  int done = 1;

  va_start(ap, format);
  do
    {
      len = vsnprintf (smtp->buffer, smtp->buflen - 1, format, ap);
      if (len < 0 || (len >= (int)smtp->buflen)
	  || !memchr (smtp->buffer, '\0', len + 1))
        {
          smtp->buflen *= 2;
          smtp->buffer = realloc (smtp->buffer, smtp->buflen);
          if (smtp->buffer == NULL)
            return ENOMEM;
	  done = 0;
        }
      else
	done = 1;
    }
  while (!done);
  va_end(ap);
  smtp->ptr = smtp->buffer + len;
  return 0;
}

static int
smtp_write (smtp_t smtp)
{
  int status = 0;
  size_t len;
  if (smtp->ptr > smtp->buffer)
    {
      len = smtp->ptr - smtp->buffer;
      status = bio_write (smtp->bio, smtp->buffer, len, &len);
      if (status == 0)
        {
          memmove (smtp->buffer, smtp->buffer + len, len);
          smtp->ptr -= len;
        }
    }
  else
    {
      smtp->ptr = smtp->buffer;
      len = 0;
    }
  return status;
}

static int
smtp_read_ack (smtp_t smtp)
{
  int status;
  int multi;

  do
    {
      multi = 0;
      status = smtp_readline (smtp);
      if ((smtp->ptr - smtp->buffer) > 4
	  && smtp->buffer[3] == '-')
	multi = 1;
      if (status == 0)
	smtp->ptr = smtp->buffer;
    }
  while (multi && status == 0);

  if (status == 0)
    smtp->ptr = smtp->buffer;
  return status;
}

/* Read a complete line form the pop server. Transform CRLF to LF,
   put a null in the buffer when done.  */
static int
smtp_readline (smtp_t smtp)
{
  size_t n = 0;
  size_t total = smtp->ptr - smtp->buffer;
  int status;

  /* Must get a full line before bailing out.  */
  do
    {
      status = bio_readline (smtp->bio, smtp->buffer + total,
                             smtp->buflen - total, &n);
      if (status != 0)
        return status;

      total += n;
      smtp->nl = memchr (smtp->buffer, '\n', total);
      if (smtp->nl == NULL)  /* Do we have a full line.  */
        {
          /* Allocate a bigger buffer ?  */
          if (total >= smtp->buflen -1)
            {
              smtp->buflen *= 2;
              smtp->buffer = realloc (smtp->buffer, smtp->buflen + 1);
              if (smtp->buffer == NULL)
                return ENOMEM;
            }
        }
      smtp->ptr = smtp->buffer + total;
    }
  while (smtp->nl == NULL);

  /* \r\n --> \n\0  */
  if (smtp->nl > smtp->buffer)
    {
      *(smtp->nl - 1) = '\n';
      *(smtp->nl) = '\0';
      smtp->ptr = smtp->nl;
    }
  return 0;
}