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.24 (Berkeley) 11/04/94 (with SMTP)"; 14 #else 15 static char sccsid[] = "@(#)usersmtp.c 8.24 (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 !bitnset(M_8BITS, m->m_flags) && 579 e->e_bodytype != NULL && 580 strcasecmp(e->e_bodytype, "7bit") != 0) 581 { 582 /* must convert from 8bit MIME format to 7bit encoded */ 583 mci->mci_flags |= MCIF_CVT8TO7; 584 } 585 586 (*e->e_puthdr)(mci, e->e_header, e); 587 (*e->e_putbody)(mci, e, NULL); 588 589 /* 590 ** Cleanup after sending message. 591 */ 592 593 clrevent(ev); 594 595 if (ferror(mci->mci_out)) 596 { 597 /* error during processing -- don't send the dot */ 598 mci->mci_errno = EIO; 599 mci->mci_exitstat = EX_IOERR; 600 mci->mci_state = MCIS_ERROR; 601 smtpquit(m, mci, e); 602 return EX_IOERR; 603 } 604 605 /* terminate the message */ 606 fprintf(mci->mci_out, ".%s", m->m_eol); 607 if (TrafficLogFile != NULL) 608 fprintf(TrafficLogFile, "%05d >>> .\n", getpid()); 609 if (Verbose) 610 nmessage(">>> ."); 611 612 /* check for the results of the transaction */ 613 SmtpPhase = mci->mci_phase = "client DATA 250"; 614 setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 615 r = reply(m, mci, e, TimeOuts.to_datafinal, NULL); 616 if (r < 0) 617 { 618 smtpquit(m, mci, e); 619 return (EX_TEMPFAIL); 620 } 621 mci->mci_state = MCIS_OPEN; 622 e->e_statmsg = newstr(&SmtpReplyBuffer[4]); 623 if (REPLYTYPE(r) == 4) 624 return (EX_TEMPFAIL); 625 else if (r == 250) 626 return (EX_OK); 627 else if (r == 552 || r == 554) 628 return (EX_UNAVAILABLE); 629 #ifdef LOG 630 if (LogLevel > 1) 631 { 632 syslog(LOG_CRIT, "%s: %s: SMTP DATA-2 protocol error: %s", 633 e->e_id, mci->mci_host, SmtpReplyBuffer); 634 } 635 #endif 636 return (EX_PROTOCOL); 637 } 638 639 640 static int 641 datatimeout() 642 { 643 longjmp(CtxDataTimeout, 1); 644 } 645 /* 646 ** SMTPQUIT -- close the SMTP connection. 647 ** 648 ** Parameters: 649 ** m -- a pointer to the mailer. 650 ** 651 ** Returns: 652 ** none. 653 ** 654 ** Side Effects: 655 ** sends the final protocol and closes the connection. 656 */ 657 658 smtpquit(m, mci, e) 659 register MAILER *m; 660 register MCI *mci; 661 ENVELOPE *e; 662 { 663 bool oldSuprErrs = SuprErrs; 664 665 /* 666 ** Suppress errors here -- we may be processing a different 667 ** job when we do the quit connection, and we don't want the 668 ** new job to be penalized for something that isn't it's 669 ** problem. 670 */ 671 672 SuprErrs = TRUE; 673 674 /* send the quit message if we haven't gotten I/O error */ 675 if (mci->mci_state != MCIS_ERROR) 676 { 677 SmtpPhase = "client QUIT"; 678 smtpmessage("QUIT", m, mci); 679 (void) reply(m, mci, e, TimeOuts.to_quit, NULL); 680 SuprErrs = oldSuprErrs; 681 if (mci->mci_state == MCIS_CLOSED) 682 { 683 SuprErrs = oldSuprErrs; 684 return; 685 } 686 } 687 688 /* now actually close the connection and pick up the zombie */ 689 (void) endmailer(mci, e, NULL); 690 691 SuprErrs = oldSuprErrs; 692 } 693 /* 694 ** SMTPRSET -- send a RSET (reset) command 695 */ 696 697 smtprset(m, mci, e) 698 register MAILER *m; 699 register MCI *mci; 700 ENVELOPE *e; 701 { 702 int r; 703 704 SmtpPhase = "client RSET"; 705 smtpmessage("RSET", m, mci); 706 r = reply(m, mci, e, TimeOuts.to_rset, NULL); 707 if (r < 0) 708 mci->mci_state = MCIS_ERROR; 709 else if (REPLYTYPE(r) == 2) 710 { 711 mci->mci_state = MCIS_OPEN; 712 return; 713 } 714 smtpquit(m, mci, e); 715 } 716 /* 717 ** SMTPPROBE -- check the connection state 718 */ 719 720 smtpprobe(mci) 721 register MCI *mci; 722 { 723 int r; 724 MAILER *m = mci->mci_mailer; 725 extern ENVELOPE BlankEnvelope; 726 ENVELOPE *e = &BlankEnvelope; 727 728 SmtpPhase = "client probe"; 729 smtpmessage("RSET", m, mci); 730 r = reply(m, mci, e, TimeOuts.to_miscshort, NULL); 731 if (r < 0 || REPLYTYPE(r) != 2) 732 smtpquit(m, mci, e); 733 return r; 734 } 735 /* 736 ** REPLY -- read arpanet reply 737 ** 738 ** Parameters: 739 ** m -- the mailer we are reading the reply from. 740 ** mci -- the mailer connection info structure. 741 ** e -- the current envelope. 742 ** timeout -- the timeout for reads. 743 ** pfunc -- processing function for second and subsequent 744 ** lines of response -- if null, no special 745 ** processing is done. 746 ** 747 ** Returns: 748 ** reply code it reads. 749 ** 750 ** Side Effects: 751 ** flushes the mail file. 752 */ 753 754 reply(m, mci, e, timeout, pfunc) 755 MAILER *m; 756 MCI *mci; 757 ENVELOPE *e; 758 time_t timeout; 759 void (*pfunc)(); 760 { 761 register char *bufp; 762 register int r; 763 bool firstline = TRUE; 764 char junkbuf[MAXLINE]; 765 766 if (mci->mci_out != NULL) 767 (void) fflush(mci->mci_out); 768 769 if (tTd(18, 1)) 770 printf("reply\n"); 771 772 /* 773 ** Read the input line, being careful not to hang. 774 */ 775 776 for (bufp = SmtpReplyBuffer;; bufp = junkbuf) 777 { 778 register char *p; 779 extern time_t curtime(); 780 781 /* actually do the read */ 782 if (e->e_xfp != NULL) 783 (void) fflush(e->e_xfp); /* for debugging */ 784 785 /* if we are in the process of closing just give the code */ 786 if (mci->mci_state == MCIS_CLOSED) 787 return (SMTPCLOSING); 788 789 if (mci->mci_out != NULL) 790 fflush(mci->mci_out); 791 792 /* get the line from the other side */ 793 p = sfgets(bufp, MAXLINE, mci->mci_in, timeout, SmtpPhase); 794 mci->mci_lastuse = curtime(); 795 796 if (p == NULL) 797 { 798 bool oldholderrs; 799 extern char MsgBuf[]; /* err.c */ 800 801 /* if the remote end closed early, fake an error */ 802 if (errno == 0) 803 # ifdef ECONNRESET 804 errno = ECONNRESET; 805 # else /* ECONNRESET */ 806 errno = EPIPE; 807 # endif /* ECONNRESET */ 808 809 mci->mci_errno = errno; 810 mci->mci_exitstat = EX_TEMPFAIL; 811 oldholderrs = HoldErrs; 812 HoldErrs = TRUE; 813 usrerr("451 reply: read error from %s", mci->mci_host); 814 815 /* if debugging, pause so we can see state */ 816 if (tTd(18, 100)) 817 pause(); 818 mci->mci_state = MCIS_ERROR; 819 smtpquit(m, mci, e); 820 #ifdef XDEBUG 821 { 822 char wbuf[MAXLINE]; 823 char *p = wbuf; 824 if (e->e_to != NULL) 825 { 826 sprintf(p, "%s... ", e->e_to); 827 p += strlen(p); 828 } 829 sprintf(p, "reply(%s) during %s", 830 mci->mci_host, SmtpPhase); 831 checkfd012(wbuf); 832 } 833 #endif 834 HoldErrs = oldholderrs; 835 return (-1); 836 } 837 fixcrlf(bufp, TRUE); 838 839 /* EHLO failure is not a real error */ 840 if (e->e_xfp != NULL && (bufp[0] == '4' || 841 (bufp[0] == '5' && strncmp(SmtpMsgBuffer, "EHLO", 4) != 0))) 842 { 843 /* serious error -- log the previous command */ 844 if (SmtpNeedIntro) 845 { 846 /* inform user who we are chatting with */ 847 fprintf(CurEnv->e_xfp, 848 "... while talking to %s:\n", 849 CurHostName); 850 SmtpNeedIntro = FALSE; 851 } 852 if (SmtpMsgBuffer[0] != '\0') 853 fprintf(e->e_xfp, ">>> %s\n", SmtpMsgBuffer); 854 SmtpMsgBuffer[0] = '\0'; 855 856 /* now log the message as from the other side */ 857 fprintf(e->e_xfp, "<<< %s\n", bufp); 858 } 859 860 /* display the input for verbose mode */ 861 if (Verbose) 862 nmessage("050 %s", bufp); 863 864 /* process the line */ 865 if (pfunc != NULL && !firstline) 866 (*pfunc)(bufp, m, mci, e); 867 868 firstline = FALSE; 869 870 /* if continuation is required, we can go on */ 871 if (bufp[3] == '-') 872 continue; 873 874 /* ignore improperly formated input */ 875 if (!(isascii(bufp[0]) && isdigit(bufp[0]))) 876 continue; 877 878 /* decode the reply code */ 879 r = atoi(bufp); 880 881 /* extra semantics: 0xx codes are "informational" */ 882 if (r >= 100) 883 break; 884 } 885 886 /* 887 ** Now look at SmtpReplyBuffer -- only care about the first 888 ** line of the response from here on out. 889 */ 890 891 /* save temporary failure messages for posterity */ 892 if (SmtpReplyBuffer[0] == '4' && SmtpError[0] == '\0') 893 (void) strcpy(SmtpError, SmtpReplyBuffer); 894 895 /* reply code 421 is "Service Shutting Down" */ 896 if (r == SMTPCLOSING && mci->mci_state != MCIS_SSD) 897 { 898 /* send the quit protocol */ 899 mci->mci_state = MCIS_SSD; 900 smtpquit(m, mci, e); 901 } 902 903 return (r); 904 } 905 /* 906 ** SMTPMESSAGE -- send message to server 907 ** 908 ** Parameters: 909 ** f -- format 910 ** m -- the mailer to control formatting. 911 ** a, b, c -- parameters 912 ** 913 ** Returns: 914 ** none. 915 ** 916 ** Side Effects: 917 ** writes message to mci->mci_out. 918 */ 919 920 /*VARARGS1*/ 921 #ifdef __STDC__ 922 smtpmessage(char *f, MAILER *m, MCI *mci, ...) 923 #else 924 smtpmessage(f, m, mci, va_alist) 925 char *f; 926 MAILER *m; 927 MCI *mci; 928 va_dcl 929 #endif 930 { 931 VA_LOCAL_DECL 932 933 VA_START(mci); 934 (void) vsprintf(SmtpMsgBuffer, f, ap); 935 VA_END; 936 937 if (tTd(18, 1) || Verbose) 938 nmessage(">>> %s", SmtpMsgBuffer); 939 if (TrafficLogFile != NULL) 940 fprintf(TrafficLogFile, "%05d >>> %s\n", getpid(), SmtpMsgBuffer); 941 if (mci->mci_out != NULL) 942 { 943 fprintf(mci->mci_out, "%s%s", SmtpMsgBuffer, 944 m == NULL ? "\r\n" : m->m_eol); 945 } 946 else if (tTd(18, 1)) 947 { 948 printf("smtpmessage: NULL mci_out\n"); 949 } 950 } 951 952 # endif /* SMTP */ 953