1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 # include "sendmail.h" 10 11 #ifndef lint 12 #ifdef SMTP 13 static char sccsid[] = "@(#)usersmtp.c 8.23 (Berkeley) 11/04/94 (with SMTP)"; 14 #else 15 static char sccsid[] = "@(#)usersmtp.c 8.23 (Berkeley) 11/04/94 (without SMTP)"; 16 #endif 17 #endif /* not lint */ 18 19 # include <sysexits.h> 20 # include <errno.h> 21 22 # ifdef SMTP 23 24 /* 25 ** USERSMTP -- run SMTP protocol from the user end. 26 ** 27 ** This protocol is described in RFC821. 28 */ 29 30 #define REPLYTYPE(r) ((r) / 100) /* first digit of reply code */ 31 #define REPLYCLASS(r) (((r) / 10) % 10) /* second digit of reply code */ 32 #define SMTPCLOSING 421 /* "Service Shutting Down" */ 33 34 char SmtpMsgBuffer[MAXLINE]; /* buffer for commands */ 35 char SmtpReplyBuffer[MAXLINE]; /* buffer for replies */ 36 char SmtpError[MAXLINE] = ""; /* save failure error messages */ 37 int SmtpPid; /* pid of mailer */ 38 bool SmtpNeedIntro; /* need "while talking" in transcript */ 39 40 #ifdef __STDC__ 41 extern smtpmessage(char *f, MAILER *m, MCI *mci, ...); 42 #endif 43 /* 44 ** SMTPINIT -- initialize SMTP. 45 ** 46 ** Opens the connection and sends the initial protocol. 47 ** 48 ** Parameters: 49 ** m -- mailer to create connection to. 50 ** pvp -- pointer to parameter vector to pass to 51 ** the mailer. 52 ** 53 ** Returns: 54 ** none. 55 ** 56 ** Side Effects: 57 ** creates connection and sends initial protocol. 58 */ 59 60 smtpinit(m, mci, e) 61 struct mailer *m; 62 register MCI *mci; 63 ENVELOPE *e; 64 { 65 register int r; 66 register char *p; 67 extern void esmtp_check(); 68 extern void helo_options(); 69 70 if (tTd(18, 1)) 71 { 72 printf("smtpinit "); 73 mci_dump(mci, FALSE); 74 } 75 76 /* 77 ** Open the connection to the mailer. 78 */ 79 80 SmtpError[0] = '\0'; 81 CurHostName = mci->mci_host; /* XXX UGLY XXX */ 82 SmtpNeedIntro = TRUE; 83 switch (mci->mci_state) 84 { 85 case MCIS_ACTIVE: 86 /* need to clear old information */ 87 smtprset(m, mci, e); 88 /* fall through */ 89 90 case MCIS_OPEN: 91 return; 92 93 case MCIS_ERROR: 94 case MCIS_SSD: 95 /* shouldn't happen */ 96 smtpquit(m, mci, e); 97 /* fall through */ 98 99 case MCIS_CLOSED: 100 syserr("451 smtpinit: state CLOSED"); 101 return; 102 103 case MCIS_OPENING: 104 break; 105 } 106 107 mci->mci_state = MCIS_OPENING; 108 109 /* 110 ** Get the greeting message. 111 ** This should appear spontaneously. Give it five minutes to 112 ** happen. 113 */ 114 115 SmtpPhase = mci->mci_phase = "client greeting"; 116 setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 117 r = reply(m, mci, e, TimeOuts.to_initial, esmtp_check); 118 if (r < 0 || REPLYTYPE(r) == 4) 119 goto tempfail1; 120 if (REPLYTYPE(r) != 2) 121 goto unavailable; 122 123 /* 124 ** Send the HELO command. 125 ** My mother taught me to always introduce myself. 126 */ 127 128 if (bitnset(M_ESMTP, m->m_flags)) 129 mci->mci_flags |= MCIF_ESMTP; 130 131 tryhelo: 132 if (bitset(MCIF_ESMTP, mci->mci_flags)) 133 { 134 smtpmessage("EHLO %s", m, mci, MyHostName); 135 SmtpPhase = mci->mci_phase = "client EHLO"; 136 } 137 else 138 { 139 smtpmessage("HELO %s", m, mci, MyHostName); 140 SmtpPhase = mci->mci_phase = "client HELO"; 141 } 142 setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 143 r = reply(m, mci, e, TimeOuts.to_helo, helo_options); 144 if (r < 0) 145 goto tempfail1; 146 else if (REPLYTYPE(r) == 5) 147 { 148 if (bitset(MCIF_ESMTP, mci->mci_flags)) 149 { 150 /* try old SMTP instead */ 151 mci->mci_flags &= ~MCIF_ESMTP; 152 goto tryhelo; 153 } 154 goto unavailable; 155 } 156 else if (REPLYTYPE(r) != 2) 157 goto tempfail1; 158 159 /* 160 ** Check to see if we actually ended up talking to ourself. 161 ** This means we didn't know about an alias or MX, or we managed 162 ** to connect to an echo server. 163 */ 164 165 p = strchr(&SmtpReplyBuffer[4], ' '); 166 if (p != NULL) 167 *p = '\0'; 168 if (!bitnset(M_NOLOOPCHECK, m->m_flags) && 169 strcasecmp(&SmtpReplyBuffer[4], MyHostName) == 0) 170 { 171 syserr("553 %s config error: mail loops back to myself", 172 MyHostName); 173 mci->mci_exitstat = EX_CONFIG; 174 mci->mci_errno = 0; 175 smtpquit(m, mci, e); 176 return; 177 } 178 179 /* 180 ** If this is expected to be another sendmail, send some internal 181 ** commands. 182 */ 183 184 if (bitnset(M_INTERNAL, m->m_flags)) 185 { 186 /* tell it to be verbose */ 187 smtpmessage("VERB", m, mci); 188 r = reply(m, mci, e, TimeOuts.to_miscshort, NULL); 189 if (r < 0) 190 goto tempfail2; 191 } 192 193 if (mci->mci_state != MCIS_CLOSED) 194 { 195 mci->mci_state = MCIS_OPEN; 196 return; 197 } 198 199 /* got a 421 error code during startup */ 200 201 tempfail1: 202 tempfail2: 203 mci->mci_exitstat = EX_TEMPFAIL; 204 if (mci->mci_errno == 0) 205 mci->mci_errno = errno; 206 if (mci->mci_state != MCIS_CLOSED) 207 smtpquit(m, mci, e); 208 return; 209 210 unavailable: 211 mci->mci_exitstat = EX_UNAVAILABLE; 212 mci->mci_errno = errno; 213 smtpquit(m, mci, e); 214 return; 215 } 216 /* 217 ** ESMTP_CHECK -- check to see if this implementation likes ESMTP protocol 218 ** 219 ** 220 ** Parameters: 221 ** line -- the response line. 222 ** m -- the mailer. 223 ** mci -- the mailer connection info. 224 ** e -- the envelope. 225 ** 226 ** Returns: 227 ** none. 228 */ 229 230 void 231 esmtp_check(line, m, mci, e) 232 char *line; 233 MAILER *m; 234 register MCI *mci; 235 ENVELOPE *e; 236 { 237 if (strlen(line) < 5) 238 return; 239 line += 4; 240 if (strncmp(line, "ESMTP ", 6) == 0) 241 mci->mci_flags |= MCIF_ESMTP; 242 } 243 /* 244 ** HELO_OPTIONS -- process the options on a HELO line. 245 ** 246 ** Parameters: 247 ** line -- the response line. 248 ** m -- the mailer. 249 ** mci -- the mailer connection info. 250 ** e -- the envelope. 251 ** 252 ** Returns: 253 ** none. 254 */ 255 256 void 257 helo_options(line, m, mci, e) 258 char *line; 259 MAILER *m; 260 register MCI *mci; 261 ENVELOPE *e; 262 { 263 register char *p; 264 265 if (strlen(line) < 5) 266 return; 267 line += 4; 268 p = strchr(line, ' '); 269 if (p != NULL) 270 *p++ = '\0'; 271 if (strcasecmp(line, "size") == 0) 272 { 273 mci->mci_flags |= MCIF_SIZE; 274 if (p != NULL) 275 mci->mci_maxsize = atol(p); 276 } 277 else if (strcasecmp(line, "8bitmime") == 0) 278 { 279 mci->mci_flags |= MCIF_8BITMIME; 280 mci->mci_flags &= ~MCIF_7BIT; 281 } 282 else if (strcasecmp(line, "expn") == 0) 283 mci->mci_flags |= MCIF_EXPN; 284 else if (strcasecmp(line, "dsn") == 0) 285 mci->mci_flags |= MCIF_DSN; 286 } 287 /* 288 ** SMTPMAILFROM -- send MAIL command 289 ** 290 ** Parameters: 291 ** m -- the mailer. 292 ** mci -- the mailer connection structure. 293 ** e -- the envelope (including the sender to specify). 294 */ 295 296 smtpmailfrom(m, mci, e) 297 struct mailer *m; 298 MCI *mci; 299 ENVELOPE *e; 300 { 301 int r; 302 char *bufp; 303 char buf[MAXNAME]; 304 char optbuf[MAXLINE]; 305 306 if (tTd(18, 2)) 307 printf("smtpmailfrom: CurHost=%s\n", CurHostName); 308 309 /* set up appropriate options to include */ 310 if (bitset(MCIF_SIZE, mci->mci_flags) && e->e_msgsize > 0) 311 sprintf(optbuf, " SIZE=%ld", e->e_msgsize); 312 else 313 strcpy(optbuf, ""); 314 315 if (e->e_bodytype != NULL) 316 { 317 if (bitset(MCIF_8BITMIME, mci->mci_flags)) 318 { 319 strcat(optbuf, " BODY="); 320 strcat(optbuf, e->e_bodytype); 321 } 322 else if (!bitset(MM_CVTMIME, MimeMode) && 323 strcasecmp(e->e_bodytype, "7bit") != 0) 324 { 325 /* cannot just send a 7-bit version */ 326 usrerr("%s does not support 8BITMIME", mci->mci_host); 327 return EX_DATAERR; 328 } 329 } 330 331 if (e->e_envid != NULL && bitset(MCIF_DSN, mci->mci_flags)) 332 { 333 strcat(optbuf, " ENVID="); 334 strcat(optbuf, e->e_envid); 335 } 336 337 /* 338 ** Send the MAIL command. 339 ** Designates the sender. 340 */ 341 342 mci->mci_state = MCIS_ACTIVE; 343 344 if (bitset(EF_RESPONSE, e->e_flags) && 345 !bitnset(M_NO_NULL_FROM, m->m_flags)) 346 (void) strcpy(buf, ""); 347 else 348 expand("\201g", buf, &buf[sizeof buf - 1], e); 349 if (buf[0] == '<') 350 { 351 /* strip off <angle brackets> (put back on below) */ 352 bufp = &buf[strlen(buf) - 1]; 353 if (*bufp == '>') 354 *bufp = '\0'; 355 bufp = &buf[1]; 356 } 357 else 358 bufp = buf; 359 if (bitnset(M_LOCALMAILER, e->e_from.q_mailer->m_flags) || 360 !bitnset(M_FROMPATH, m->m_flags)) 361 { 362 smtpmessage("MAIL From:<%s>%s", m, mci, bufp, optbuf); 363 } 364 else 365 { 366 smtpmessage("MAIL From:<@%s%c%s>%s", m, mci, MyHostName, 367 *bufp == '@' ? ',' : ':', bufp, optbuf); 368 } 369 SmtpPhase = mci->mci_phase = "client MAIL"; 370 setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 371 r = reply(m, mci, e, TimeOuts.to_mail, NULL); 372 if (r < 0 || REPLYTYPE(r) == 4) 373 { 374 mci->mci_exitstat = EX_TEMPFAIL; 375 mci->mci_errno = errno; 376 smtpquit(m, mci, e); 377 return EX_TEMPFAIL; 378 } 379 else if (r == 250) 380 { 381 mci->mci_exitstat = EX_OK; 382 return EX_OK; 383 } 384 else if (r == 552) 385 { 386 /* signal service unavailable */ 387 mci->mci_exitstat = EX_UNAVAILABLE; 388 smtpquit(m, mci, e); 389 return EX_UNAVAILABLE; 390 } 391 392 #ifdef LOG 393 if (LogLevel > 1) 394 { 395 syslog(LOG_CRIT, "%s: %s: SMTP MAIL protocol error: %s", 396 e->e_id, mci->mci_host, SmtpReplyBuffer); 397 } 398 #endif 399 400 /* protocol error -- close up */ 401 smtpquit(m, mci, e); 402 mci->mci_exitstat = EX_PROTOCOL; 403 return EX_PROTOCOL; 404 } 405 /* 406 ** SMTPRCPT -- designate recipient. 407 ** 408 ** Parameters: 409 ** to -- address of recipient. 410 ** m -- the mailer we are sending to. 411 ** mci -- the connection info for this transaction. 412 ** e -- the envelope for this transaction. 413 ** 414 ** Returns: 415 ** exit status corresponding to recipient status. 416 ** 417 ** Side Effects: 418 ** Sends the mail via SMTP. 419 */ 420 421 smtprcpt(to, m, mci, e) 422 ADDRESS *to; 423 register MAILER *m; 424 MCI *mci; 425 ENVELOPE *e; 426 { 427 register int r; 428 char optbuf[MAXLINE]; 429 430 strcpy(optbuf, ""); 431 if (bitset(MCIF_DSN, mci->mci_flags)) 432 { 433 strcat(optbuf, " NOTIFY="); 434 if (bitset(QPINGONFAILURE, to->q_flags)) 435 { 436 if (bitset(QPINGONSUCCESS, to->q_flags)) 437 strcat(optbuf, "ALWAYS"); 438 else 439 strcat(optbuf, "FAILURE"); 440 } 441 else 442 { 443 if (bitset(QPINGONSUCCESS, to->q_flags)) 444 strcat(optbuf, "SUCCESS"); 445 else 446 strcat(optbuf, "NEVER"); 447 } 448 if (bitset(QHASRETPARAM, to->q_flags)) 449 { 450 strcat(optbuf, " RET="); 451 if (bitset(QNOBODYRETURN, to->q_flags)) 452 strcat(optbuf, "NO"); 453 else 454 strcat(optbuf, "YES"); 455 } 456 } 457 else if (bitset(QPINGONSUCCESS, to->q_flags)) 458 { 459 to->q_flags |= QRELAYED; 460 fprintf(e->e_xfp, "%s... relayed; expect no further notifications\n", 461 to->q_paddr); 462 } 463 464 smtpmessage("RCPT To:<%s>%s", m, mci, to->q_user, optbuf); 465 466 SmtpPhase = mci->mci_phase = "client RCPT"; 467 setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 468 r = reply(m, mci, e, TimeOuts.to_rcpt, NULL); 469 setstatus(to, SmtpReplyBuffer); 470 if (r < 0 || REPLYTYPE(r) == 4) 471 return (EX_TEMPFAIL); 472 else if (REPLYTYPE(r) == 2) 473 return (EX_OK); 474 else if (r == 550 || r == 551 || r == 553) 475 return (EX_NOUSER); 476 else if (r == 552 || r == 554) 477 return (EX_UNAVAILABLE); 478 479 #ifdef LOG 480 if (LogLevel > 1) 481 { 482 syslog(LOG_CRIT, "%s: %s: SMTP RCPT protocol error: %s", 483 e->e_id, mci->mci_host, SmtpReplyBuffer); 484 } 485 #endif 486 487 return (EX_PROTOCOL); 488 } 489 /* 490 ** SMTPDATA -- send the data and clean up the transaction. 491 ** 492 ** Parameters: 493 ** m -- mailer being sent to. 494 ** e -- the envelope for this message. 495 ** 496 ** Returns: 497 ** exit status corresponding to DATA command. 498 ** 499 ** Side Effects: 500 ** none. 501 */ 502 503 static jmp_buf CtxDataTimeout; 504 static int datatimeout(); 505 506 smtpdata(m, mci, e) 507 struct mailer *m; 508 register MCI *mci; 509 register ENVELOPE *e; 510 { 511 register int r; 512 register EVENT *ev; 513 time_t timeout; 514 515 /* 516 ** Send the data. 517 ** First send the command and check that it is ok. 518 ** Then send the data. 519 ** Follow it up with a dot to terminate. 520 ** Finally get the results of the transaction. 521 */ 522 523 /* send the command and check ok to proceed */ 524 smtpmessage("DATA", m, mci); 525 SmtpPhase = mci->mci_phase = "client DATA 354"; 526 setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 527 r = reply(m, mci, e, TimeOuts.to_datainit, NULL); 528 if (r < 0 || REPLYTYPE(r) == 4) 529 { 530 smtpquit(m, mci, e); 531 return (EX_TEMPFAIL); 532 } 533 else if (r == 554) 534 { 535 smtprset(m, mci, e); 536 return (EX_UNAVAILABLE); 537 } 538 else if (r != 354) 539 { 540 #ifdef LOG 541 if (LogLevel > 1) 542 { 543 syslog(LOG_CRIT, "%s: %s: SMTP DATA-1 protocol error: %s", 544 e->e_id, mci->mci_host, SmtpReplyBuffer); 545 } 546 #endif 547 smtprset(m, mci, e); 548 return (EX_PROTOCOL); 549 } 550 551 /* 552 ** Set timeout around data writes. Make it at least large 553 ** enough for DNS timeouts on all recipients plus some fudge 554 ** factor. The main thing is that it should not be infinite. 555 */ 556 557 if (setjmp(CtxDataTimeout) != 0) 558 { 559 mci->mci_errno = errno; 560 mci->mci_exitstat = EX_TEMPFAIL; 561 mci->mci_state = MCIS_ERROR; 562 syserr("451 timeout writing message to %s", mci->mci_host); 563 smtpquit(m, mci, e); 564 return EX_TEMPFAIL; 565 } 566 567 timeout = e->e_msgsize / 16; 568 if (timeout < (time_t) 60) 569 timeout = (time_t) 60; 570 timeout += e->e_nrcpts * 90; 571 ev = setevent(timeout, datatimeout, 0); 572 573 /* 574 ** Output the actual message. 575 */ 576 577 if (!bitset(MCIF_8BITMIME, mci->mci_flags) && 578 e->e_bodytype != NULL && 579 strcasecmp(e->e_bodytype, "7bit") != 0) 580 { 581 /* must convert from 8bit MIME format to 7bit encoded */ 582 mci->mci_flags |= MCIF_CVT8TO7; 583 } 584 585 (*e->e_puthdr)(mci, e->e_header, e); 586 (*e->e_putbody)(mci, e, NULL); 587 588 /* 589 ** Cleanup after sending message. 590 */ 591 592 clrevent(ev); 593 594 if (ferror(mci->mci_out)) 595 { 596 /* error during processing -- don't send the dot */ 597 mci->mci_errno = EIO; 598 mci->mci_exitstat = EX_IOERR; 599 mci->mci_state = MCIS_ERROR; 600 smtpquit(m, mci, e); 601 return EX_IOERR; 602 } 603 604 /* terminate the message */ 605 fprintf(mci->mci_out, ".%s", m->m_eol); 606 if (TrafficLogFile != NULL) 607 fprintf(TrafficLogFile, "%05d >>> .\n", getpid()); 608 if (Verbose) 609 nmessage(">>> ."); 610 611 /* check for the results of the transaction */ 612 SmtpPhase = mci->mci_phase = "client DATA 250"; 613 setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 614 r = reply(m, mci, e, TimeOuts.to_datafinal, NULL); 615 if (r < 0) 616 { 617 smtpquit(m, mci, e); 618 return (EX_TEMPFAIL); 619 } 620 mci->mci_state = MCIS_OPEN; 621 e->e_statmsg = newstr(&SmtpReplyBuffer[4]); 622 if (REPLYTYPE(r) == 4) 623 return (EX_TEMPFAIL); 624 else if (r == 250) 625 return (EX_OK); 626 else if (r == 552 || r == 554) 627 return (EX_UNAVAILABLE); 628 #ifdef LOG 629 if (LogLevel > 1) 630 { 631 syslog(LOG_CRIT, "%s: %s: SMTP DATA-2 protocol error: %s", 632 e->e_id, mci->mci_host, SmtpReplyBuffer); 633 } 634 #endif 635 return (EX_PROTOCOL); 636 } 637 638 639 static int 640 datatimeout() 641 { 642 longjmp(CtxDataTimeout, 1); 643 } 644 /* 645 ** SMTPQUIT -- close the SMTP connection. 646 ** 647 ** Parameters: 648 ** m -- a pointer to the mailer. 649 ** 650 ** Returns: 651 ** none. 652 ** 653 ** Side Effects: 654 ** sends the final protocol and closes the connection. 655 */ 656 657 smtpquit(m, mci, e) 658 register MAILER *m; 659 register MCI *mci; 660 ENVELOPE *e; 661 { 662 bool oldSuprErrs = SuprErrs; 663 664 /* 665 ** Suppress errors here -- we may be processing a different 666 ** job when we do the quit connection, and we don't want the 667 ** new job to be penalized for something that isn't it's 668 ** problem. 669 */ 670 671 SuprErrs = TRUE; 672 673 /* send the quit message if we haven't gotten I/O error */ 674 if (mci->mci_state != MCIS_ERROR) 675 { 676 SmtpPhase = "client QUIT"; 677 smtpmessage("QUIT", m, mci); 678 (void) reply(m, mci, e, TimeOuts.to_quit, NULL); 679 SuprErrs = oldSuprErrs; 680 if (mci->mci_state == MCIS_CLOSED) 681 { 682 SuprErrs = oldSuprErrs; 683 return; 684 } 685 } 686 687 /* now actually close the connection and pick up the zombie */ 688 (void) endmailer(mci, e, NULL); 689 690 SuprErrs = oldSuprErrs; 691 } 692 /* 693 ** SMTPRSET -- send a RSET (reset) command 694 */ 695 696 smtprset(m, mci, e) 697 register MAILER *m; 698 register MCI *mci; 699 ENVELOPE *e; 700 { 701 int r; 702 703 SmtpPhase = "client RSET"; 704 smtpmessage("RSET", m, mci); 705 r = reply(m, mci, e, TimeOuts.to_rset, NULL); 706 if (r < 0) 707 mci->mci_state = MCIS_ERROR; 708 else if (REPLYTYPE(r) == 2) 709 { 710 mci->mci_state = MCIS_OPEN; 711 return; 712 } 713 smtpquit(m, mci, e); 714 } 715 /* 716 ** SMTPPROBE -- check the connection state 717 */ 718 719 smtpprobe(mci) 720 register MCI *mci; 721 { 722 int r; 723 MAILER *m = mci->mci_mailer; 724 extern ENVELOPE BlankEnvelope; 725 ENVELOPE *e = &BlankEnvelope; 726 727 SmtpPhase = "client probe"; 728 smtpmessage("RSET", m, mci); 729 r = reply(m, mci, e, TimeOuts.to_miscshort, NULL); 730 if (r < 0 || REPLYTYPE(r) != 2) 731 smtpquit(m, mci, e); 732 return r; 733 } 734 /* 735 ** REPLY -- read arpanet reply 736 ** 737 ** Parameters: 738 ** m -- the mailer we are reading the reply from. 739 ** mci -- the mailer connection info structure. 740 ** e -- the current envelope. 741 ** timeout -- the timeout for reads. 742 ** pfunc -- processing function for second and subsequent 743 ** lines of response -- if null, no special 744 ** processing is done. 745 ** 746 ** Returns: 747 ** reply code it reads. 748 ** 749 ** Side Effects: 750 ** flushes the mail file. 751 */ 752 753 reply(m, mci, e, timeout, pfunc) 754 MAILER *m; 755 MCI *mci; 756 ENVELOPE *e; 757 time_t timeout; 758 void (*pfunc)(); 759 { 760 register char *bufp; 761 register int r; 762 bool firstline = TRUE; 763 char junkbuf[MAXLINE]; 764 765 if (mci->mci_out != NULL) 766 (void) fflush(mci->mci_out); 767 768 if (tTd(18, 1)) 769 printf("reply\n"); 770 771 /* 772 ** Read the input line, being careful not to hang. 773 */ 774 775 for (bufp = SmtpReplyBuffer;; bufp = junkbuf) 776 { 777 register char *p; 778 extern time_t curtime(); 779 780 /* actually do the read */ 781 if (e->e_xfp != NULL) 782 (void) fflush(e->e_xfp); /* for debugging */ 783 784 /* if we are in the process of closing just give the code */ 785 if (mci->mci_state == MCIS_CLOSED) 786 return (SMTPCLOSING); 787 788 if (mci->mci_out != NULL) 789 fflush(mci->mci_out); 790 791 /* get the line from the other side */ 792 p = sfgets(bufp, MAXLINE, mci->mci_in, timeout, SmtpPhase); 793 mci->mci_lastuse = curtime(); 794 795 if (p == NULL) 796 { 797 bool oldholderrs; 798 extern char MsgBuf[]; /* err.c */ 799 800 /* if the remote end closed early, fake an error */ 801 if (errno == 0) 802 # ifdef ECONNRESET 803 errno = ECONNRESET; 804 # else /* ECONNRESET */ 805 errno = EPIPE; 806 # endif /* ECONNRESET */ 807 808 mci->mci_errno = errno; 809 mci->mci_exitstat = EX_TEMPFAIL; 810 oldholderrs = HoldErrs; 811 HoldErrs = TRUE; 812 usrerr("451 reply: read error from %s", mci->mci_host); 813 814 /* if debugging, pause so we can see state */ 815 if (tTd(18, 100)) 816 pause(); 817 mci->mci_state = MCIS_ERROR; 818 smtpquit(m, mci, e); 819 #ifdef XDEBUG 820 { 821 char wbuf[MAXLINE]; 822 char *p = wbuf; 823 if (e->e_to != NULL) 824 { 825 sprintf(p, "%s... ", e->e_to); 826 p += strlen(p); 827 } 828 sprintf(p, "reply(%s) during %s", 829 mci->mci_host, SmtpPhase); 830 checkfd012(wbuf); 831 } 832 #endif 833 HoldErrs = oldholderrs; 834 return (-1); 835 } 836 fixcrlf(bufp, TRUE); 837 838 /* EHLO failure is not a real error */ 839 if (e->e_xfp != NULL && (bufp[0] == '4' || 840 (bufp[0] == '5' && strncmp(SmtpMsgBuffer, "EHLO", 4) != 0))) 841 { 842 /* serious error -- log the previous command */ 843 if (SmtpNeedIntro) 844 { 845 /* inform user who we are chatting with */ 846 fprintf(CurEnv->e_xfp, 847 "... while talking to %s:\n", 848 CurHostName); 849 SmtpNeedIntro = FALSE; 850 } 851 if (SmtpMsgBuffer[0] != '\0') 852 fprintf(e->e_xfp, ">>> %s\n", SmtpMsgBuffer); 853 SmtpMsgBuffer[0] = '\0'; 854 855 /* now log the message as from the other side */ 856 fprintf(e->e_xfp, "<<< %s\n", bufp); 857 } 858 859 /* display the input for verbose mode */ 860 if (Verbose) 861 nmessage("050 %s", bufp); 862 863 /* process the line */ 864 if (pfunc != NULL && !firstline) 865 (*pfunc)(bufp, m, mci, e); 866 867 firstline = FALSE; 868 869 /* if continuation is required, we can go on */ 870 if (bufp[3] == '-') 871 continue; 872 873 /* ignore improperly formated input */ 874 if (!(isascii(bufp[0]) && isdigit(bufp[0]))) 875 continue; 876 877 /* decode the reply code */ 878 r = atoi(bufp); 879 880 /* extra semantics: 0xx codes are "informational" */ 881 if (r >= 100) 882 break; 883 } 884 885 /* 886 ** Now look at SmtpReplyBuffer -- only care about the first 887 ** line of the response from here on out. 888 */ 889 890 /* save temporary failure messages for posterity */ 891 if (SmtpReplyBuffer[0] == '4' && SmtpError[0] == '\0') 892 (void) strcpy(SmtpError, SmtpReplyBuffer); 893 894 /* reply code 421 is "Service Shutting Down" */ 895 if (r == SMTPCLOSING && mci->mci_state != MCIS_SSD) 896 { 897 /* send the quit protocol */ 898 mci->mci_state = MCIS_SSD; 899 smtpquit(m, mci, e); 900 } 901 902 return (r); 903 } 904 /* 905 ** SMTPMESSAGE -- send message to server 906 ** 907 ** Parameters: 908 ** f -- format 909 ** m -- the mailer to control formatting. 910 ** a, b, c -- parameters 911 ** 912 ** Returns: 913 ** none. 914 ** 915 ** Side Effects: 916 ** writes message to mci->mci_out. 917 */ 918 919 /*VARARGS1*/ 920 #ifdef __STDC__ 921 smtpmessage(char *f, MAILER *m, MCI *mci, ...) 922 #else 923 smtpmessage(f, m, mci, va_alist) 924 char *f; 925 MAILER *m; 926 MCI *mci; 927 va_dcl 928 #endif 929 { 930 VA_LOCAL_DECL 931 932 VA_START(mci); 933 (void) vsprintf(SmtpMsgBuffer, f, ap); 934 VA_END; 935 936 if (tTd(18, 1) || Verbose) 937 nmessage(">>> %s", SmtpMsgBuffer); 938 if (TrafficLogFile != NULL) 939 fprintf(TrafficLogFile, "%05d >>> %s\n", getpid(), SmtpMsgBuffer); 940 if (mci->mci_out != NULL) 941 { 942 fprintf(mci->mci_out, "%s%s", SmtpMsgBuffer, 943 m == NULL ? "\r\n" : m->m_eol); 944 } 945 else if (tTd(18, 1)) 946 { 947 printf("smtpmessage: NULL mci_out\n"); 948 } 949 } 950 951 # endif /* SMTP */ 952