1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988 Regents of the University of California. 4 * 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 6.32 (Berkeley) 05/27/93 (with SMTP)"; 14 #else 15 static char sccsid[] = "@(#)usersmtp.c 6.32 (Berkeley) 05/27/93 (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 39 #ifdef __STDC__ 40 extern smtpmessage(char *f, MAILER *m, MCI *mci, ...); 41 #endif 42 /* 43 ** SMTPINIT -- initialize SMTP. 44 ** 45 ** Opens the connection and sends the initial protocol. 46 ** 47 ** Parameters: 48 ** m -- mailer to create connection to. 49 ** pvp -- pointer to parameter vector to pass to 50 ** the mailer. 51 ** 52 ** Returns: 53 ** none. 54 ** 55 ** Side Effects: 56 ** creates connection and sends initial protocol. 57 */ 58 59 smtpinit(m, mci, e) 60 struct mailer *m; 61 register MCI *mci; 62 ENVELOPE *e; 63 { 64 register int r; 65 register char *p; 66 extern void esmtp_check(); 67 extern void helo_options(); 68 69 if (tTd(17, 1)) 70 { 71 printf("smtpinit "); 72 mci_dump(mci); 73 } 74 75 /* 76 ** Open the connection to the mailer. 77 */ 78 79 SmtpError[0] = '\0'; 80 CurHostName = mci->mci_host; /* XXX UGLY XXX */ 81 switch (mci->mci_state) 82 { 83 case MCIS_ACTIVE: 84 /* need to clear old information */ 85 smtprset(m, mci, e); 86 /* fall through */ 87 88 case MCIS_OPEN: 89 return; 90 91 case MCIS_ERROR: 92 case MCIS_SSD: 93 /* shouldn't happen */ 94 smtpquit(m, mci, e); 95 /* fall through */ 96 97 case MCIS_CLOSED: 98 syserr("451 smtpinit: state CLOSED"); 99 return; 100 101 case MCIS_OPENING: 102 break; 103 } 104 105 mci->mci_state = MCIS_OPENING; 106 107 /* 108 ** Get the greeting message. 109 ** This should appear spontaneously. Give it five minutes to 110 ** happen. 111 */ 112 113 SmtpPhase = mci->mci_phase = "greeting wait"; 114 setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 115 r = reply(m, mci, e, TimeOuts.to_initial, esmtp_check); 116 if (r < 0 || REPLYTYPE(r) != 2) 117 goto tempfail1; 118 119 /* 120 ** Send the HELO command. 121 ** My mother taught me to always introduce myself. 122 */ 123 124 if (bitnset(M_ESMTP, m->m_flags)) 125 mci->mci_flags |= MCIF_ESMTP; 126 127 tryhelo: 128 if (bitset(MCIF_ESMTP, mci->mci_flags)) 129 { 130 smtpmessage("EHLO %s", m, mci, MyHostName); 131 SmtpPhase = mci->mci_phase = "EHLO wait"; 132 } 133 else 134 { 135 smtpmessage("HELO %s", m, mci, MyHostName); 136 SmtpPhase = mci->mci_phase = "HELO wait"; 137 } 138 setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 139 r = reply(m, mci, e, TimeOuts.to_helo, helo_options); 140 if (r < 0) 141 goto tempfail1; 142 else if (REPLYTYPE(r) == 5) 143 { 144 if (bitset(MCIF_ESMTP, mci->mci_flags)) 145 { 146 /* try old SMTP instead */ 147 mci->mci_flags &= ~MCIF_ESMTP; 148 goto tryhelo; 149 } 150 goto unavailable; 151 } 152 else if (REPLYTYPE(r) != 2) 153 goto tempfail1; 154 155 /* 156 ** Check to see if we actually ended up talking to ourself. 157 ** This means we didn't know about an alias or MX, or we managed 158 ** to connect to an echo server. 159 */ 160 161 p = strchr(&SmtpReplyBuffer[4], ' '); 162 if (p != NULL) 163 *p == '\0'; 164 if (strcasecmp(&SmtpReplyBuffer[4], MyHostName) == 0) 165 { 166 syserr("553 %s config error: mail loops back to myself", 167 MyHostName); 168 mci->mci_exitstat = EX_CONFIG; 169 mci->mci_errno = 0; 170 smtpquit(m, mci, e); 171 return; 172 } 173 174 /* 175 ** If this is expected to be another sendmail, send some internal 176 ** commands. 177 */ 178 179 if (bitnset(M_INTERNAL, m->m_flags)) 180 { 181 /* tell it to be verbose */ 182 smtpmessage("VERB", m, mci); 183 r = reply(m, mci, e, TimeOuts.to_miscshort, NULL); 184 if (r < 0) 185 goto tempfail2; 186 } 187 188 mci->mci_state = MCIS_OPEN; 189 return; 190 191 tempfail1: 192 tempfail2: 193 mci->mci_exitstat = EX_TEMPFAIL; 194 if (mci->mci_errno == 0) 195 mci->mci_errno = errno; 196 if (mci->mci_state != MCIS_CLOSED) 197 smtpquit(m, mci, e); 198 return; 199 200 unavailable: 201 mci->mci_exitstat = EX_UNAVAILABLE; 202 mci->mci_errno = errno; 203 smtpquit(m, mci, e); 204 return; 205 } 206 /* 207 ** ESMTP_CHECK -- check to see if this implementation likes ESMTP protocol 208 ** 209 ** 210 ** Parameters: 211 ** line -- the response line. 212 ** m -- the mailer. 213 ** mci -- the mailer connection info. 214 ** e -- the envelope. 215 ** 216 ** Returns: 217 ** none. 218 */ 219 220 void 221 esmtp_check(line, m, mci, e) 222 char *line; 223 MAILER *m; 224 register MCI *mci; 225 ENVELOPE *e; 226 { 227 if (strlen(line) < 5) 228 return; 229 line += 4; 230 if (strncmp(line, "ESMTP ", 6) == 0) 231 mci->mci_flags |= MCIF_ESMTP; 232 } 233 /* 234 ** HELO_OPTIONS -- process the options on a HELO line. 235 ** 236 ** Parameters: 237 ** line -- the response line. 238 ** m -- the mailer. 239 ** mci -- the mailer connection info. 240 ** e -- the envelope. 241 ** 242 ** Returns: 243 ** none. 244 */ 245 246 void 247 helo_options(line, m, mci, e) 248 char *line; 249 MAILER *m; 250 register MCI *mci; 251 ENVELOPE *e; 252 { 253 register char *p; 254 255 if (strlen(line) < 5) 256 return; 257 line += 4; 258 p = strchr(line, ' '); 259 if (p != NULL) 260 *p++ = '\0'; 261 if (strcasecmp(line, "size") == 0) 262 { 263 mci->mci_flags |= MCIF_SIZE; 264 if (p != NULL) 265 mci->mci_maxsize = atol(p); 266 } 267 else if (strcasecmp(line, "8bitmime") == 0) 268 mci->mci_flags |= MCIF_8BITMIME; 269 else if (strcasecmp(line, "expn") == 0) 270 mci->mci_flags |= MCIF_EXPN; 271 } 272 /* 273 ** SMTPMAILFROM -- send MAIL command 274 ** 275 ** Parameters: 276 ** m -- the mailer. 277 ** mci -- the mailer connection structure. 278 ** e -- the envelope (including the sender to specify). 279 */ 280 281 smtpmailfrom(m, mci, e) 282 struct mailer *m; 283 MCI *mci; 284 ENVELOPE *e; 285 { 286 int r; 287 char buf[MAXNAME]; 288 char optbuf[MAXLINE]; 289 290 if (tTd(17, 2)) 291 printf("smtpmailfrom: CurHost=%s\n", CurHostName); 292 293 /* set up appropriate options to include */ 294 if (bitset(MCIF_SIZE, mci->mci_flags)) 295 sprintf(optbuf, " SIZE=%ld", e->e_msgsize); 296 else 297 strcpy(optbuf, ""); 298 299 /* 300 ** Send the MAIL command. 301 ** Designates the sender. 302 */ 303 304 mci->mci_state = MCIS_ACTIVE; 305 306 if (bitset(EF_RESPONSE, e->e_flags) && 307 !bitnset(M_NO_NULL_FROM, m->m_flags)) 308 (void) strcpy(buf, ""); 309 else 310 expand("\201g", buf, &buf[sizeof buf - 1], e); 311 if (e->e_from.q_mailer == LocalMailer || 312 !bitnset(M_FROMPATH, m->m_flags)) 313 { 314 smtpmessage("MAIL From:<%s>%s", m, mci, buf, optbuf); 315 } 316 else 317 { 318 smtpmessage("MAIL From:<@%s%c%s>%s", m, mci, MyHostName, 319 buf[0] == '@' ? ',' : ':', buf, optbuf); 320 } 321 SmtpPhase = mci->mci_phase = "MAIL wait"; 322 setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 323 r = reply(m, mci, e, TimeOuts.to_mail, NULL); 324 if (r < 0 || REPLYTYPE(r) == 4) 325 { 326 mci->mci_exitstat = EX_TEMPFAIL; 327 mci->mci_errno = errno; 328 smtpquit(m, mci, e); 329 return EX_TEMPFAIL; 330 } 331 else if (r == 250) 332 { 333 mci->mci_exitstat = EX_OK; 334 return EX_OK; 335 } 336 else if (r == 552) 337 { 338 /* signal service unavailable */ 339 mci->mci_exitstat = EX_UNAVAILABLE; 340 smtpquit(m, mci, e); 341 return EX_UNAVAILABLE; 342 } 343 344 #ifdef LOG 345 if (LogLevel > 1) 346 { 347 syslog(LOG_CRIT, "%s: SMTP MAIL protocol error: %s", 348 e->e_id, SmtpReplyBuffer); 349 } 350 #endif 351 352 /* protocol error -- close up */ 353 smtpquit(m, mci, e); 354 mci->mci_exitstat = EX_PROTOCOL; 355 return EX_PROTOCOL; 356 } 357 /* 358 ** SMTPRCPT -- designate recipient. 359 ** 360 ** Parameters: 361 ** to -- address of recipient. 362 ** m -- the mailer we are sending to. 363 ** mci -- the connection info for this transaction. 364 ** e -- the envelope for this transaction. 365 ** 366 ** Returns: 367 ** exit status corresponding to recipient status. 368 ** 369 ** Side Effects: 370 ** Sends the mail via SMTP. 371 */ 372 373 smtprcpt(to, m, mci, e) 374 ADDRESS *to; 375 register MAILER *m; 376 MCI *mci; 377 ENVELOPE *e; 378 { 379 register int r; 380 381 smtpmessage("RCPT To:<%s>", m, mci, to->q_user); 382 383 SmtpPhase = mci->mci_phase = "RCPT wait"; 384 setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 385 r = reply(m, mci, e, TimeOuts.to_rcpt, NULL); 386 if (r < 0 || REPLYTYPE(r) == 4) 387 return (EX_TEMPFAIL); 388 else if (REPLYTYPE(r) == 2) 389 return (EX_OK); 390 else if (r == 550 || r == 551 || r == 553) 391 return (EX_NOUSER); 392 else if (r == 552 || r == 554) 393 return (EX_UNAVAILABLE); 394 395 #ifdef LOG 396 if (LogLevel > 1) 397 { 398 syslog(LOG_CRIT, "%s: SMTP RCPT protocol error: %s", 399 e->e_id, SmtpReplyBuffer); 400 } 401 #endif 402 403 return (EX_PROTOCOL); 404 } 405 /* 406 ** SMTPDATA -- send the data and clean up the transaction. 407 ** 408 ** Parameters: 409 ** m -- mailer being sent to. 410 ** e -- the envelope for this message. 411 ** 412 ** Returns: 413 ** exit status corresponding to DATA command. 414 ** 415 ** Side Effects: 416 ** none. 417 */ 418 419 smtpdata(m, mci, e) 420 struct mailer *m; 421 register MCI *mci; 422 register ENVELOPE *e; 423 { 424 register int r; 425 426 /* 427 ** Send the data. 428 ** First send the command and check that it is ok. 429 ** Then send the data. 430 ** Follow it up with a dot to terminate. 431 ** Finally get the results of the transaction. 432 */ 433 434 /* send the command and check ok to proceed */ 435 smtpmessage("DATA", m, mci); 436 SmtpPhase = mci->mci_phase = "DATA wait"; 437 setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 438 r = reply(m, mci, e, TimeOuts.to_datainit, NULL); 439 if (r < 0 || REPLYTYPE(r) == 4) 440 { 441 smtpquit(m, mci, e); 442 return (EX_TEMPFAIL); 443 } 444 else if (r == 554) 445 { 446 smtprset(m, mci, e); 447 return (EX_UNAVAILABLE); 448 } 449 else if (r != 354) 450 { 451 #ifdef LOG 452 if (LogLevel > 1) 453 { 454 syslog(LOG_CRIT, "%s: SMTP DATA-1 protocol error: %s", 455 e->e_id, SmtpReplyBuffer); 456 } 457 #endif 458 smtprset(m, mci, e); 459 return (EX_PROTOCOL); 460 } 461 462 /* now output the actual message */ 463 (*e->e_puthdr)(mci->mci_out, m, e); 464 putline("\n", mci->mci_out, m); 465 (*e->e_putbody)(mci->mci_out, m, e, NULL); 466 467 /* terminate the message */ 468 fprintf(mci->mci_out, ".%s", m->m_eol); 469 if (Verbose) 470 nmessage(">>> ."); 471 472 /* check for the results of the transaction */ 473 SmtpPhase = mci->mci_phase = "result wait"; 474 setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 475 r = reply(m, mci, e, TimeOuts.to_datafinal, NULL); 476 if (r < 0) 477 { 478 smtpquit(m, mci, e); 479 return (EX_TEMPFAIL); 480 } 481 mci->mci_state = MCIS_OPEN; 482 e->e_statmsg = newstr(&SmtpReplyBuffer[4]); 483 if (REPLYTYPE(r) == 4) 484 return (EX_TEMPFAIL); 485 else if (r == 250) 486 return (EX_OK); 487 else if (r == 552 || r == 554) 488 return (EX_UNAVAILABLE); 489 #ifdef LOG 490 if (LogLevel > 1) 491 { 492 syslog(LOG_CRIT, "%s: SMTP DATA-2 protocol error: %s", 493 e->e_id, SmtpReplyBuffer); 494 } 495 #endif 496 return (EX_PROTOCOL); 497 } 498 /* 499 ** SMTPQUIT -- close the SMTP connection. 500 ** 501 ** Parameters: 502 ** m -- a pointer to the mailer. 503 ** 504 ** Returns: 505 ** none. 506 ** 507 ** Side Effects: 508 ** sends the final protocol and closes the connection. 509 */ 510 511 smtpquit(m, mci, e) 512 register MAILER *m; 513 register MCI *mci; 514 ENVELOPE *e; 515 { 516 int i; 517 518 /* send the quit message if we haven't gotten I/O error */ 519 if (mci->mci_state != MCIS_ERROR) 520 { 521 smtpmessage("QUIT", m, mci); 522 (void) reply(m, mci, e, TimeOuts.to_quit, NULL); 523 if (mci->mci_state == MCIS_CLOSED) 524 return; 525 } 526 527 /* now actually close the connection and pick up the zombie */ 528 i = endmailer(mci, e, m->m_argv); 529 if (i != EX_OK) 530 syserr("451 smtpquit %s: stat %d", m->m_argv[0], i); 531 } 532 /* 533 ** SMTPRSET -- send a RSET (reset) command 534 */ 535 536 smtprset(m, mci, e) 537 register MAILER *m; 538 register MCI *mci; 539 ENVELOPE *e; 540 { 541 int r; 542 543 smtpmessage("RSET", m, mci); 544 r = reply(m, mci, e, TimeOuts.to_rset, NULL); 545 if (r < 0) 546 mci->mci_state = MCIS_ERROR; 547 else if (REPLYTYPE(r) == 2) 548 { 549 mci->mci_state = MCIS_OPEN; 550 return; 551 } 552 smtpquit(m, mci, e); 553 } 554 /* 555 ** SMTPPROBE -- check the connection state 556 */ 557 558 smtpprobe(mci) 559 register MCI *mci; 560 { 561 int r; 562 MAILER *m = mci->mci_mailer; 563 extern ENVELOPE BlankEnvelope; 564 ENVELOPE *e = &BlankEnvelope; 565 566 smtpmessage("RSET", m, mci); 567 r = reply(m, mci, e, TimeOuts.to_miscshort, NULL); 568 if (r < 0 || REPLYTYPE(r) != 2) 569 smtpquit(m, mci, e); 570 return r; 571 } 572 /* 573 ** REPLY -- read arpanet reply 574 ** 575 ** Parameters: 576 ** m -- the mailer we are reading the reply from. 577 ** mci -- the mailer connection info structure. 578 ** e -- the current envelope. 579 ** timeout -- the timeout for reads. 580 ** pfunc -- processing function for second and subsequent 581 ** lines of response -- if null, no special 582 ** processing is done. 583 ** 584 ** Returns: 585 ** reply code it reads. 586 ** 587 ** Side Effects: 588 ** flushes the mail file. 589 */ 590 591 reply(m, mci, e, timeout, pfunc) 592 MAILER *m; 593 MCI *mci; 594 ENVELOPE *e; 595 time_t timeout; 596 void (*pfunc)(); 597 { 598 register char *bufp; 599 register int r; 600 bool firstline = TRUE; 601 char junkbuf[MAXLINE]; 602 603 if (mci->mci_out != NULL) 604 (void) fflush(mci->mci_out); 605 606 if (tTd(18, 1)) 607 printf("reply\n"); 608 609 /* 610 ** Read the input line, being careful not to hang. 611 */ 612 613 for (bufp = SmtpReplyBuffer;; bufp = junkbuf) 614 { 615 register char *p; 616 extern time_t curtime(); 617 618 /* actually do the read */ 619 if (e->e_xfp != NULL) 620 (void) fflush(e->e_xfp); /* for debugging */ 621 622 /* if we are in the process of closing just give the code */ 623 if (mci->mci_state == MCIS_CLOSED) 624 return (SMTPCLOSING); 625 626 if (mci->mci_out != NULL) 627 fflush(mci->mci_out); 628 629 /* get the line from the other side */ 630 p = sfgets(bufp, MAXLINE, mci->mci_in, timeout); 631 mci->mci_lastuse = curtime(); 632 633 if (p == NULL) 634 { 635 extern char MsgBuf[]; /* err.c */ 636 637 /* if the remote end closed early, fake an error */ 638 if (errno == 0) 639 # ifdef ECONNRESET 640 errno = ECONNRESET; 641 # else /* ECONNRESET */ 642 errno = EPIPE; 643 # endif /* ECONNRESET */ 644 645 mci->mci_errno = errno; 646 mci->mci_exitstat = EX_TEMPFAIL; 647 message("451 %s: reply: read error from %s", 648 e->e_id == NULL ? "NOQUEUE" : e->e_id, 649 mci->mci_host); 650 /* if debugging, pause so we can see state */ 651 if (tTd(18, 100)) 652 pause(); 653 # ifdef LOG 654 if (LogLevel > 1) 655 syslog(LOG_INFO, "%s", &MsgBuf[4]); 656 # endif /* LOG */ 657 mci->mci_state = MCIS_ERROR; 658 smtpquit(m, mci, e); 659 return (-1); 660 } 661 fixcrlf(bufp, TRUE); 662 663 if (e->e_xfp != NULL && strchr("45", bufp[0]) != NULL) 664 { 665 /* serious error -- log the previous command */ 666 if (SmtpMsgBuffer[0] != '\0') 667 fprintf(e->e_xfp, ">>> %s\n", SmtpMsgBuffer); 668 SmtpMsgBuffer[0] = '\0'; 669 670 /* now log the message as from the other side */ 671 fprintf(e->e_xfp, "<<< %s\n", bufp); 672 } 673 674 /* display the input for verbose mode */ 675 if (Verbose) 676 nmessage("050 %s", bufp); 677 678 /* process the line */ 679 if (pfunc != NULL && !firstline) 680 (*pfunc)(bufp, m, mci, e); 681 682 firstline = FALSE; 683 684 /* if continuation is required, we can go on */ 685 if (bufp[3] == '-') 686 continue; 687 688 /* ignore improperly formated input */ 689 if (!(isascii(bufp[0]) && isdigit(bufp[0]))) 690 continue; 691 692 /* decode the reply code */ 693 r = atoi(bufp); 694 695 /* extra semantics: 0xx codes are "informational" */ 696 if (r >= 100) 697 break; 698 } 699 700 /* 701 ** Now look at SmtpReplyBuffer -- only care about the first 702 ** line of the response from here on out. 703 */ 704 705 /* save temporary failure messages for posterity */ 706 if (SmtpReplyBuffer[0] == '4' && SmtpError[0] == '\0') 707 (void) strcpy(SmtpError, SmtpReplyBuffer); 708 709 /* reply code 421 is "Service Shutting Down" */ 710 if (r == SMTPCLOSING && mci->mci_state != MCIS_SSD) 711 { 712 /* send the quit protocol */ 713 mci->mci_state = MCIS_SSD; 714 smtpquit(m, mci, e); 715 } 716 717 return (r); 718 } 719 /* 720 ** SMTPMESSAGE -- send message to server 721 ** 722 ** Parameters: 723 ** f -- format 724 ** m -- the mailer to control formatting. 725 ** a, b, c -- parameters 726 ** 727 ** Returns: 728 ** none. 729 ** 730 ** Side Effects: 731 ** writes message to mci->mci_out. 732 */ 733 734 /*VARARGS1*/ 735 #ifdef __STDC__ 736 smtpmessage(char *f, MAILER *m, MCI *mci, ...) 737 #else 738 smtpmessage(f, m, mci, va_alist) 739 char *f; 740 MAILER *m; 741 MCI *mci; 742 va_dcl 743 #endif 744 { 745 VA_LOCAL_DECL 746 747 VA_START(mci); 748 (void) vsprintf(SmtpMsgBuffer, f, ap); 749 VA_END; 750 751 if (tTd(18, 1) || Verbose) 752 nmessage(">>> %s", SmtpMsgBuffer); 753 if (mci->mci_out != NULL) 754 { 755 fprintf(mci->mci_out, "%s%s", SmtpMsgBuffer, 756 m == NULL ? "\r\n" : m->m_eol); 757 } 758 else if (tTd(18, 1)) 759 { 760 printf("smtpmessage: NULL mci_out\n"); 761 } 762 } 763 764 # endif /* SMTP */ 765