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
|
/*
* Copyright (c) 1983 Eric P. Allman
* Copyright (c) 1988 Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted provided
* that: (1) source distributions retain this entire copyright notice and
* comment, and (2) distributions including binaries display the following
* acknowledgement: ``This product includes software developed by the
* University of California, Berkeley and its contributors'' in the
* documentation or other materials provided with the distribution and in
* all advertising materials mentioning features or use of this software.
* Neither the name of the University nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef lint
static char Version[] = "@(#)smtp.c e07@nikhef.nl (Eric Wassenaar) 980626";
#endif
#include "vrfy.h"
extern int verbose;
extern int debug;
#define REPLYTYPE(r) ((r)/100) /* first digit of smtp reply code */
#define SMTPCLOSING 421 /* "Service Shutting Down" temp failure */
#define SMTP_CLOSED 0 /* connection is closed */
#define SMTP_OPEN 1 /* connection is open for business */
#define SMTP_SSD 2 /* service shutting down */
char SmtpMsgBuffer[BUFSIZ]; /* buffer for outgoing commands */
char SmtpReplyBuffer[BUFSIZ]; /* buffer for incoming replies (first line) */
char SmtpContBuffer[BUFSIZ]; /* buffer for incoming replies (continuation) */
char SmtpErrorBuffer[BUFSIZ]; /* saved temporary failure error messages */
FILE *SmtpOut = NULL; /* smtp output channel */
FILE *SmtpIn = NULL; /* smtp input channel */
char *SmtpPhase = NULL; /* connection state message */
int SmtpState = SMTP_CLOSED; /* current connection state */
int SmtpErrno = 0; /* saved errno from system calls */
char *SmtpCrLf = "\r\n"; /* smtp end-of-line terminator */
/*
** SMTPINIT -- Initiate SMTP connection with remote host
** -----------------------------------------------------
**
** Returns:
** Status code indicating success or failure.
**
** Outputs:
** Sets SmtpIn and SmtpOut to input and output channel.
** Sets SmtpErrno appropriately.
*/
int
smtpinit(host)
char *host; /* remote host to be contacted */
{
register int r;
SmtpIn = SmtpOut = NULL;
SmtpState = SMTP_CLOSED;
SmtpErrorBuffer[0] = '\0';
SmtpErrno = 0;
SmtpPhase = "connect";
if (debug)
printf("smtp phase %s\n", SmtpPhase);
r = makeconnection(host, &SmtpOut, &SmtpIn);
SmtpErrno = errno;
if (r != EX_SUCCESS)
return(r);
SmtpState = SMTP_OPEN;
r = smtpreply("greeting wait", FALSE);
if (r < 0 || REPLYTYPE(r) == 4)
return(EX_TEMPFAIL);
if (REPLYTYPE(r) == 2)
return(EX_SUCCESS);
if (REPLYTYPE(r) == 5)
return(EX_UNAVAILABLE);
return(EX_TEMPFAIL);
}
/*
** SMTPHELO -- Issue the HELO command
** ----------------------------------
**
** In ESMTP mode, first try the EHLO command.
**
** Returns:
** Status code indicating success or failure.
*/
int
smtphelo(name, esmtp)
char *name; /* my own fully qualified hostname */
bool esmtp; /* try EHLO first, if set */
{
register int r;
if (esmtp)
{
r = smtpehlo(name);
if (r != EX_UNAVAILABLE)
return(r);
}
smtpmessage("HELO %s", name);
r = smtpreply("HELO wait", FALSE);
if (r < 0 || REPLYTYPE(r) == 4)
return(EX_TEMPFAIL);
if (REPLYTYPE(r) == 2)
return(EX_SUCCESS);
if (REPLYTYPE(r) == 5)
return(EX_UNAVAILABLE);
return(EX_TEMPFAIL);
}
/*
** SMTPEHLO -- Issue the EHLO command
** ----------------------------------
**
** Returns:
** Status code indicating success or failure.
*/
int
smtpehlo(name)
char *name; /* my own fully qualified hostname */
{
register int r;
smtpmessage("EHLO %s", name);
r = smtpreply("EHLO wait", FALSE);
if (r < 0 || REPLYTYPE(r) == 4)
return(EX_TEMPFAIL);
if (REPLYTYPE(r) == 2)
return(EX_SUCCESS);
if (REPLYTYPE(r) == 5)
return(EX_UNAVAILABLE);
return(EX_TEMPFAIL);
}
/*
** SMTPONEX -- Issue the ONEX command
** ----------------------------------
**
** Returns:
** Status code indicating success or failure.
**
** It is not an error if the remote host does not support this.
*/
int
smtponex()
{
register int r;
smtpmessage("ONEX");
r = smtpreply("ONEX wait", FALSE);
if (r < 0 || REPLYTYPE(r) == 4)
return(EX_TEMPFAIL);
return(EX_SUCCESS);
}
/*
** SMTPVERB -- Issue the VERB command
** ----------------------------------
**
** Returns:
** Status code indicating success or failure.
**
** It is not an error if the remote host does not support this.
**
** Note. Some systems require an 'on' or 'off' parameter.
** Systems that do not require a parameter won't object.
*/
int
smtpverb(onoff)
char *onoff; /* some hosts require parameter */
{
register int r;
smtpmessage("VERB %s", onoff);
r = smtpreply("VERB wait", FALSE);
if (r < 0 || REPLYTYPE(r) == 4)
return(EX_TEMPFAIL);
return(EX_SUCCESS);
}
/*
** SMTPETRN -- Issue the ETRN command
** ----------------------------------
**
** Returns:
** Status code indicating success or failure.
*/
int
smtpetrn(name)
char *name; /* domain name for the ETRN command */
{
register int r;
smtpmessage("ETRN %s", name);
r = smtpreply("ETRN wait", FALSE);
if (r < 0 || REPLYTYPE(r) == 4)
return(EX_TEMPFAIL);
if (REPLYTYPE(r) == 2)
return(EX_SUCCESS);
if (REPLYTYPE(r) == 5)
return(EX_UNAVAILABLE);
return(EX_PROTOCOL);
}
/*
** SMTPRSET -- Issue the RSET command
** ----------------------------------
**
** Returns:
** Status code indicating success or failure.
*/
int
smtprset()
{
register int r;
smtpmessage("RSET");
r = smtpreply("RSET wait", FALSE);
if (r < 0 || REPLYTYPE(r) == 4)
return(EX_TEMPFAIL);
if (REPLYTYPE(r) == 2)
return(EX_SUCCESS);
if (REPLYTYPE(r) == 5)
return(EX_UNAVAILABLE);
return(EX_PROTOCOL);
}
/*
** SMTPMAIL -- Issue the MAIL command
** ----------------------------------
**
** Returns:
** Status code indicating success or failure.
*/
int
smtpmail(address)
char *address; /* sender address specification */
{
register int r;
smtpmessage("MAIL From:<%s>", address);
r = smtpreply("MAIL wait", FALSE);
if (r < 0 || REPLYTYPE(r) == 4)
return(EX_TEMPFAIL);
if (r == 250)
return(EX_SUCCESS);
if (r == 552 || r == 554)
return(EX_UNAVAILABLE);
if (r == 550 || r == 551 || r == 553)
return(EX_UNAVAILABLE);
if (r == 500 || r == 501 || r == 503)
return(EX_UNAVAILABLE);
if (r == 521)
return(EX_UNAVAILABLE);
if (r == 571)
return(EX_UNAVAILABLE);
return(EX_PROTOCOL);
}
/*
** SMTPRCPT -- Issue the RCPT command
** ----------------------------------
**
** Returns:
** Status code indicating success or failure.
*/
int
smtprcpt(address)
char *address; /* recipient address specification */
{
register int r;
smtpmessage("RCPT To:<%s>", address);
r = smtpreply("RCPT wait", TRUE);
if (r < 0 || REPLYTYPE(r) == 4)
return(EX_TEMPFAIL);
if (r == 250 || r == 251)
return(EX_SUCCESS);
if (r == 550 || r == 551 || r == 553)
return(EX_NOUSER);
if (r == 552 || r == 554)
return(EX_UNAVAILABLE);
if (r == 500 || r == 501 || r == 503)
return(EX_UNAVAILABLE);
if (r == 521)
return(EX_UNAVAILABLE);
if (r == 571)
return(EX_UNAVAILABLE);
return(EX_PROTOCOL);
}
/*
** SMTPEXPN -- Issue the EXPN command
** ----------------------------------
**
** Returns:
** Status code indicating success or failure.
*/
int
smtpexpn(address)
char *address; /* address to be verified */
{
register int r;
smtpmessage("EXPN %s", address);
r = smtpreply("EXPN wait", TRUE);
if (r < 0 || REPLYTYPE(r) == 4)
return(EX_TEMPFAIL);
if (REPLYTYPE(r) == 2)
/* address was verified ok */
return(EX_SUCCESS);
if (r == 550 || r == 551 || r == 553)
/* local address but unknown or ambiguous user */
return(EX_NOUSER);
if (r == 552 || r == 554)
/* address could not be verified */
return(EX_UNAVAILABLE);
if (r == 500 || r == 501 || r == 502 || r == 504)
/* command not implemented */
return(EX_UNAVAILABLE);
if (r == 521)
/* not a real mail server */
return(EX_UNAVAILABLE);
return(EX_PROTOCOL);
}
/*
** SMTPVRFY -- Issue the VRFY command
** ----------------------------------
**
** Returns:
** Status code indicating success or failure.
*/
int
smtpvrfy(address)
char *address; /* address to be verified */
{
register int r;
smtpmessage("VRFY %s", address);
r = smtpreply("VRFY wait", TRUE);
if (r < 0 || REPLYTYPE(r) == 4)
return(EX_TEMPFAIL);
if (REPLYTYPE(r) == 2)
/* address was verified ok */
return(EX_SUCCESS);
if (r == 550 || r == 551 || r == 553)
/* local address but unknown or ambiguous user */
return(EX_NOUSER);
if (r == 552 || r == 554)
/* address could not be verified */
return(EX_UNAVAILABLE);
if (r == 500 || r == 501 || r == 502 || r == 504)
/* command not implemented */
return(EX_UNAVAILABLE);
if (r == 521)
/* not a real mail server */
return(EX_UNAVAILABLE);
return(EX_PROTOCOL);
}
/*
** SMTPDATA -- Issue the DATA command
** ----------------------------------
**
** Returns:
** Status code indicating success or failure.
*/
int
smtpdata()
{
register int r;
/*
* Issue the DATA command, and wait for the go-ahead.
*/
smtpmessage("DATA");
r = smtpreply("DATA wait", FALSE);
if (r < 0 || REPLYTYPE(r) == 4)
return(EX_TEMPFAIL);
if (r == 552 || r == 554)
return(EX_UNAVAILABLE);
if (r == 500 || r == 501 || r == 503)
return(EX_UNAVAILABLE);
if (r == 521)
return(EX_UNAVAILABLE);
if (r != 354)
return(EX_PROTOCOL);
/*
* Transmit the message body. This fails only on I/O errors.
*/
if (smtpbody() != EX_SUCCESS)
return(EX_TEMPFAIL);
/*
* Terminate the message, and wait for acceptance.
*/
smtpmessage(".");
r = smtpreply("accept wait", FALSE);
if (r < 0 || REPLYTYPE(r) == 4)
return(EX_TEMPFAIL);
if (r == 552 || r == 554)
return(EX_UNAVAILABLE);
if (r == 500 || r == 501 || r == 503)
return(EX_UNAVAILABLE);
if (r == 521)
return(EX_UNAVAILABLE);
if (r != 250)
return(EX_PROTOCOL);
return(EX_SUCCESS);
}
/*
** SMTPBODY -- Transmit the message itself
** ---------------------------------------
**
** Returns:
** Status code indicating success or failure.
*/
int
smtpbody()
{
return(EX_SUCCESS);
}
/*
** SMTPQUIT -- Issue the QUIT command, and reset connection
** --------------------------------------------------------
**
** Returns:
** Status code indicating success or failure.
**
** This routine may be called recursively.
** Save the previous state across calls.
*/
int
smtpquit()
{
int SaveErrno = SmtpErrno; /* save across recursive calls */
char *SavePhase = SmtpPhase; /* save across recursive calls */
if (SmtpIn == NULL && SmtpOut == NULL)
return(EX_SUCCESS);
if (SmtpState == SMTP_OPEN || SmtpState == SMTP_SSD)
{
smtpmessage("QUIT");
(void) smtpreply("QUIT wait", FALSE);
if (SmtpState == SMTP_CLOSED)
{
SmtpErrno = SaveErrno;
SmtpPhase = SavePhase;
return(EX_SUCCESS);
}
}
if (SmtpIn != NULL)
(void) fclose(SmtpIn);
if (SmtpOut != NULL)
(void) fclose(SmtpOut);
SmtpIn = SmtpOut = NULL;
SmtpState = SMTP_CLOSED;
SmtpErrno = SaveErrno;
SmtpPhase = SavePhase;
return(EX_SUCCESS);
}
/*
** SMTPMESSAGE -- Output an SMTP command
** -------------------------------------
**
** Returns:
** None.
**
** Outputs:
** Saves the command in SmtpMsgBuffer.
**
** The command is always followed by a CR/LF combination.
*/
void
/*VARARGS1*/
smtpmessage(fmt, a, b, c, d)
char *fmt; /* format of message */
char *a, *b, *c, *d; /* optional arguments */
{
if (SmtpOut != NULL)
{
/* construct the output message */
(void) sprintf(SmtpMsgBuffer, fmt, a, b, c, d);
/* display the output in verbose mode */
if (verbose >= 2 || debug)
printf(">>> %s\n", SmtpMsgBuffer);
/* send the message over the channel */
(void) fprintf(SmtpOut, "%s%s", SmtpMsgBuffer, SmtpCrLf);
}
}
/*
** SMTPREPLY -- Read an SMTP reply to a command
** --------------------------------------------
**
** Returns:
** The SMTP reply code if the reply has been received.
** -1 on I/O errors or timeout.
**
** Outputs:
** Saves temporary failures in SmtpErrorBuffer.
** Sets SmtpErrno appropriately.
**
** Side effects:
** Calls response() to process response if requested.
*/
int
smtpreply(phase, check)
char *phase; /* new connection state message */
bool check; /* process response, if set */
{
register int r;
register char *p;
char *buf; /* current smtp reply buffer */
/*
* Define the new SMTP connection state message.
*/
SmtpPhase = phase;
if (debug)
printf("smtp phase %s\n", SmtpPhase);
/*
* Force the previous SMTP message to be written out.
* Make sure the connection is still open, and check for errors.
*/
if (SmtpOut != NULL)
{
if (fflush(SmtpOut) || ferror(SmtpOut))
{
if (errno == 0)
errno = EIO;
SmtpErrno = errno;
SmtpState = SMTP_CLOSED;
(void) smtpquit();
return(-1);
}
}
/*
* Read the response to the SMTP message.
* Only the first line is saved in the main reply buffer.
*/
for (buf = SmtpReplyBuffer;; buf = SmtpContBuffer)
{
/* if we are in the process of closing just give the code */
if (SmtpState == SMTP_CLOSED || SmtpIn == NULL)
{
/* make sure we have a meaningful error message */
if (SmtpErrorBuffer[0] == '\0')
(void) strcpy(SmtpErrorBuffer, "Connection closed");
/* return a valid reply code */
SmtpErrno = 0;
return(SMTPCLOSING);
}
/* get the line from the other side */
p = sfgets(buf, BUFSIZ, SmtpIn);
if (p == NULL)
{
/* if the remote end closed early, fake an error */
if (errno == 0)
errno = ECONNRESET;
SmtpErrno = errno;
SmtpState = SMTP_CLOSED;
(void) smtpquit();
return(-1);
}
/* remove cr/lf combination */
fixcrlf(buf, TRUE);
/* display the input in verbose mode */
if (verbose >= 2 || debug)
printf("<<< %s\n", buf);
/* if continuation is required, we can go on */
if (!is_digit(buf[0]))
continue;
/* decode the reply code */
r = atoi(buf);
/* extra semantics: 0xx codes are "informational" */
if (r < 100)
continue;
/* process response if requested */
if (check)
response(buf);
/* if continuation is required, we can go on */
if (buf[3] == '-')
continue;
/* save temporary failure messages for posterity */
if (SmtpReplyBuffer[0] == '4' && SmtpErrorBuffer[0] == '\0')
(void) strcpy(SmtpErrorBuffer, &SmtpReplyBuffer[4]);
/* reply code 421 is "Service Shutting Down" */
if (r == SMTPCLOSING && SmtpState != SMTP_SSD)
{
/* send the quit protocol */
SmtpState = SMTP_SSD;
(void) smtpquit();
}
/* valid reply code received */
SmtpErrno = 0;
return(r);
}
/*NOTREACHED*/
}
|