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.27 (Berkeley) 04/26/93 (with SMTP)"; 14 #else 15 static char sccsid[] = "@(#)usersmtp.c 6.27 (Berkeley) 04/26/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 STAB *stab(); 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, NULL); 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 ** HELO_OPTIONS -- process the options on a HELO line. 208 ** 209 ** Parameters: 210 ** line -- the response line. 211 ** m -- the mailer. 212 ** mci -- the mailer connection info. 213 ** e -- the envelope. 214 ** 215 ** Returns: 216 ** none. 217 */ 218 219 void 220 helo_options(line, m, mci, e) 221 char *line; 222 MAILER *m; 223 register MCI *mci; 224 ENVELOPE *e; 225 { 226 register char *p; 227 228 if (strlen(line) < 5) 229 return; 230 line += 4; 231 p = strchr(line, ' '); 232 if (p != NULL) 233 *p++ = '\0'; 234 if (strcasecmp(line, "size") == 0) 235 { 236 mci->mci_flags |= MCIF_SIZE; 237 if (p != NULL) 238 mci->mci_maxsize = atol(p); 239 } 240 else if (strcasecmp(line, "8bitmime") == 0) 241 mci->mci_flags |= MCIF_8BITMIME; 242 else if (strcasecmp(line, "expn") == 0) 243 mci->mci_flags |= MCIF_EXPN; 244 } 245 /* 246 ** SMTPMAILFROM -- send MAIL command 247 ** 248 ** Parameters: 249 ** m -- the mailer. 250 ** mci -- the mailer connection structure. 251 ** e -- the envelope (including the sender to specify). 252 */ 253 254 smtpmailfrom(m, mci, e) 255 struct mailer *m; 256 MCI *mci; 257 ENVELOPE *e; 258 { 259 int r; 260 char buf[MAXNAME]; 261 char optbuf[MAXLINE]; 262 263 if (tTd(17, 2)) 264 printf("smtpmailfrom: CurHost=%s\n", CurHostName); 265 266 /* set up appropriate options to include */ 267 if (bitset(MCIF_SIZE, mci->mci_flags)) 268 sprintf(optbuf, " SIZE=%ld", e->e_msgsize); 269 else 270 strcpy(optbuf, ""); 271 272 /* 273 ** Send the MAIL command. 274 ** Designates the sender. 275 */ 276 277 mci->mci_state = MCIS_ACTIVE; 278 279 if (bitset(EF_RESPONSE, e->e_flags)) 280 (void) strcpy(buf, ""); 281 else 282 expand("\201g", buf, &buf[sizeof buf - 1], e); 283 if (e->e_from.q_mailer == LocalMailer || 284 !bitnset(M_FROMPATH, m->m_flags)) 285 { 286 smtpmessage("MAIL From:<%s>%s", m, mci, buf, optbuf); 287 } 288 else 289 { 290 smtpmessage("MAIL From:<@%s%c%s>%s", m, mci, MyHostName, 291 buf[0] == '@' ? ',' : ':', buf, optbuf); 292 } 293 SmtpPhase = mci->mci_phase = "MAIL wait"; 294 setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 295 r = reply(m, mci, e, TimeOuts.to_mail, NULL); 296 if (r < 0 || REPLYTYPE(r) == 4) 297 { 298 mci->mci_exitstat = EX_TEMPFAIL; 299 mci->mci_errno = errno; 300 smtpquit(m, mci, e); 301 return EX_TEMPFAIL; 302 } 303 else if (r == 250) 304 { 305 mci->mci_exitstat = EX_OK; 306 return EX_OK; 307 } 308 else if (r == 552) 309 { 310 /* signal service unavailable */ 311 mci->mci_exitstat = EX_UNAVAILABLE; 312 smtpquit(m, mci, e); 313 return EX_UNAVAILABLE; 314 } 315 316 #ifdef LOG 317 if (LogLevel > 1) 318 { 319 syslog(LOG_CRIT, "%s: SMTP MAIL protocol error: %s", 320 e->e_id, SmtpReplyBuffer); 321 } 322 #endif 323 324 /* protocol error -- close up */ 325 smtpquit(m, mci, e); 326 mci->mci_exitstat = EX_PROTOCOL; 327 return EX_PROTOCOL; 328 } 329 /* 330 ** SMTPRCPT -- designate recipient. 331 ** 332 ** Parameters: 333 ** to -- address of recipient. 334 ** m -- the mailer we are sending to. 335 ** mci -- the connection info for this transaction. 336 ** e -- the envelope for this transaction. 337 ** 338 ** Returns: 339 ** exit status corresponding to recipient status. 340 ** 341 ** Side Effects: 342 ** Sends the mail via SMTP. 343 */ 344 345 smtprcpt(to, m, mci, e) 346 ADDRESS *to; 347 register MAILER *m; 348 MCI *mci; 349 ENVELOPE *e; 350 { 351 register int r; 352 353 smtpmessage("RCPT To:<%s>", m, mci, to->q_user); 354 355 SmtpPhase = mci->mci_phase = "RCPT wait"; 356 setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 357 r = reply(m, mci, e, TimeOuts.to_rcpt, NULL); 358 if (r < 0 || REPLYTYPE(r) == 4) 359 return (EX_TEMPFAIL); 360 else if (REPLYTYPE(r) == 2) 361 return (EX_OK); 362 else if (r == 550 || r == 551 || r == 553) 363 return (EX_NOUSER); 364 else if (r == 552 || r == 554) 365 return (EX_UNAVAILABLE); 366 367 #ifdef LOG 368 if (LogLevel > 1) 369 { 370 syslog(LOG_CRIT, "%s: SMTP RCPT protocol error: %s", 371 e->e_id, SmtpReplyBuffer); 372 } 373 #endif 374 375 return (EX_PROTOCOL); 376 } 377 /* 378 ** SMTPDATA -- send the data and clean up the transaction. 379 ** 380 ** Parameters: 381 ** m -- mailer being sent to. 382 ** e -- the envelope for this message. 383 ** 384 ** Returns: 385 ** exit status corresponding to DATA command. 386 ** 387 ** Side Effects: 388 ** none. 389 */ 390 391 smtpdata(m, mci, e) 392 struct mailer *m; 393 register MCI *mci; 394 register ENVELOPE *e; 395 { 396 register int r; 397 398 /* 399 ** Send the data. 400 ** First send the command and check that it is ok. 401 ** Then send the data. 402 ** Follow it up with a dot to terminate. 403 ** Finally get the results of the transaction. 404 */ 405 406 /* send the command and check ok to proceed */ 407 smtpmessage("DATA", m, mci); 408 SmtpPhase = mci->mci_phase = "DATA wait"; 409 setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 410 r = reply(m, mci, e, TimeOuts.to_datainit, NULL); 411 if (r < 0 || REPLYTYPE(r) == 4) 412 { 413 smtpquit(m, mci, e); 414 return (EX_TEMPFAIL); 415 } 416 else if (r == 554) 417 { 418 smtprset(m, mci, e); 419 return (EX_UNAVAILABLE); 420 } 421 else if (r != 354) 422 { 423 #ifdef LOG 424 if (LogLevel > 1) 425 { 426 syslog(LOG_CRIT, "%s: SMTP DATA-1 protocol error: %s", 427 e->e_id, SmtpReplyBuffer); 428 } 429 #endif 430 smtprset(m, mci, e); 431 return (EX_PROTOCOL); 432 } 433 434 /* now output the actual message */ 435 (*e->e_puthdr)(mci->mci_out, m, e); 436 putline("\n", mci->mci_out, m); 437 (*e->e_putbody)(mci->mci_out, m, e); 438 439 /* terminate the message */ 440 fprintf(mci->mci_out, ".%s", m->m_eol); 441 if (Verbose) 442 nmessage(">>> ."); 443 444 /* check for the results of the transaction */ 445 SmtpPhase = mci->mci_phase = "result wait"; 446 setproctitle("%s %s: %s", e->e_id, CurHostName, mci->mci_phase); 447 r = reply(m, mci, e, TimeOuts.to_datafinal, NULL); 448 if (r < 0) 449 { 450 smtpquit(m, mci, e); 451 return (EX_TEMPFAIL); 452 } 453 mci->mci_state = MCIS_OPEN; 454 e->e_statmsg = newstr(&SmtpReplyBuffer[4]); 455 if (REPLYTYPE(r) == 4) 456 return (EX_TEMPFAIL); 457 else if (r == 250) 458 return (EX_OK); 459 else if (r == 552 || r == 554) 460 return (EX_UNAVAILABLE); 461 #ifdef LOG 462 if (LogLevel > 1) 463 { 464 syslog(LOG_CRIT, "%s: SMTP DATA-2 protocol error: %s", 465 e->e_id, SmtpReplyBuffer); 466 } 467 #endif 468 return (EX_PROTOCOL); 469 } 470 /* 471 ** SMTPQUIT -- close the SMTP connection. 472 ** 473 ** Parameters: 474 ** m -- a pointer to the mailer. 475 ** 476 ** Returns: 477 ** none. 478 ** 479 ** Side Effects: 480 ** sends the final protocol and closes the connection. 481 */ 482 483 smtpquit(m, mci, e) 484 register MAILER *m; 485 register MCI *mci; 486 ENVELOPE *e; 487 { 488 int i; 489 490 /* send the quit message if we haven't gotten I/O error */ 491 if (mci->mci_state != MCIS_ERROR) 492 { 493 smtpmessage("QUIT", m, mci); 494 (void) reply(m, mci, e, TimeOuts.to_quit, NULL); 495 if (mci->mci_state == MCIS_CLOSED) 496 return; 497 } 498 499 /* now actually close the connection and pick up the zombie */ 500 i = endmailer(mci, e, m->m_argv); 501 if (i != EX_OK) 502 syserr("451 smtpquit %s: stat %d", m->m_argv[0], i); 503 } 504 /* 505 ** SMTPRSET -- send a RSET (reset) command 506 */ 507 508 smtprset(m, mci, e) 509 register MAILER *m; 510 register MCI *mci; 511 ENVELOPE *e; 512 { 513 int r; 514 515 smtpmessage("RSET", m, mci); 516 r = reply(m, mci, e, TimeOuts.to_rset, NULL); 517 if (r < 0) 518 mci->mci_state = MCIS_ERROR; 519 else if (REPLYTYPE(r) == 2) 520 { 521 mci->mci_state = MCIS_OPEN; 522 return; 523 } 524 smtpquit(m, mci, e); 525 } 526 /* 527 ** SMTPPROBE -- check the connection state 528 */ 529 530 smtpprobe(mci) 531 register MCI *mci; 532 { 533 int r; 534 MAILER *m = mci->mci_mailer; 535 extern ENVELOPE BlankEnvelope; 536 ENVELOPE *e = &BlankEnvelope; 537 538 smtpmessage("RSET", m, mci); 539 r = reply(m, mci, e, TimeOuts.to_miscshort, NULL); 540 if (r < 0 || REPLYTYPE(r) != 2) 541 smtpquit(m, mci, e); 542 return r; 543 } 544 /* 545 ** REPLY -- read arpanet reply 546 ** 547 ** Parameters: 548 ** m -- the mailer we are reading the reply from. 549 ** mci -- the mailer connection info structure. 550 ** e -- the current envelope. 551 ** timeout -- the timeout for reads. 552 ** pfunc -- processing function for second and subsequent 553 ** lines of response -- if null, no special 554 ** processing is done. 555 ** 556 ** Returns: 557 ** reply code it reads. 558 ** 559 ** Side Effects: 560 ** flushes the mail file. 561 */ 562 563 reply(m, mci, e, timeout, pfunc) 564 MAILER *m; 565 MCI *mci; 566 ENVELOPE *e; 567 time_t timeout; 568 void (*pfunc)(); 569 { 570 register char *bufp; 571 register int r; 572 bool firstline = TRUE; 573 char junkbuf[MAXLINE]; 574 575 if (mci->mci_out != NULL) 576 (void) fflush(mci->mci_out); 577 578 if (tTd(18, 1)) 579 printf("reply\n"); 580 581 /* 582 ** Read the input line, being careful not to hang. 583 */ 584 585 for (bufp = SmtpReplyBuffer;; bufp = junkbuf) 586 { 587 register char *p; 588 extern time_t curtime(); 589 590 /* actually do the read */ 591 if (e->e_xfp != NULL) 592 (void) fflush(e->e_xfp); /* for debugging */ 593 594 /* if we are in the process of closing just give the code */ 595 if (mci->mci_state == MCIS_CLOSED) 596 return (SMTPCLOSING); 597 598 if (mci->mci_out != NULL) 599 fflush(mci->mci_out); 600 601 /* get the line from the other side */ 602 p = sfgets(bufp, MAXLINE, mci->mci_in, timeout); 603 mci->mci_lastuse = curtime(); 604 605 if (p == NULL) 606 { 607 extern char MsgBuf[]; /* err.c */ 608 609 /* if the remote end closed early, fake an error */ 610 if (errno == 0) 611 # ifdef ECONNRESET 612 errno = ECONNRESET; 613 # else /* ECONNRESET */ 614 errno = EPIPE; 615 # endif /* ECONNRESET */ 616 617 mci->mci_errno = errno; 618 mci->mci_exitstat = EX_TEMPFAIL; 619 message("451 %s: reply: read error from %s", 620 e->e_id == NULL ? "NOQUEUE" : e->e_id, 621 mci->mci_host); 622 /* if debugging, pause so we can see state */ 623 if (tTd(18, 100)) 624 pause(); 625 # ifdef LOG 626 if (LogLevel > 1) 627 syslog(LOG_INFO, "%s", &MsgBuf[4]); 628 # endif /* LOG */ 629 mci->mci_state = MCIS_ERROR; 630 smtpquit(m, mci, e); 631 return (-1); 632 } 633 fixcrlf(bufp, TRUE); 634 635 if (e->e_xfp != NULL && strchr("45", bufp[0]) != NULL) 636 { 637 /* serious error -- log the previous command */ 638 if (SmtpMsgBuffer[0] != '\0') 639 fprintf(e->e_xfp, ">>> %s\n", SmtpMsgBuffer); 640 SmtpMsgBuffer[0] = '\0'; 641 642 /* now log the message as from the other side */ 643 fprintf(e->e_xfp, "<<< %s\n", bufp); 644 } 645 646 /* display the input for verbose mode */ 647 if (Verbose) 648 nmessage("%s", bufp); 649 650 /* process the line */ 651 if (pfunc != NULL && !firstline) 652 (*pfunc)(bufp, m, mci, e); 653 654 firstline = FALSE; 655 656 /* if continuation is required, we can go on */ 657 if (bufp[3] == '-') 658 continue; 659 660 /* ignore improperly formated input */ 661 if (!(isascii(bufp[0]) && isdigit(bufp[0]))) 662 continue; 663 664 /* decode the reply code */ 665 r = atoi(bufp); 666 667 /* extra semantics: 0xx codes are "informational" */ 668 if (r >= 100) 669 break; 670 } 671 672 /* 673 ** Now look at SmtpReplyBuffer -- only care about the first 674 ** line of the response from here on out. 675 */ 676 677 /* save temporary failure messages for posterity */ 678 if (SmtpReplyBuffer[0] == '4' && SmtpError[0] == '\0') 679 (void) strcpy(SmtpError, SmtpReplyBuffer); 680 681 /* reply code 421 is "Service Shutting Down" */ 682 if (r == SMTPCLOSING && mci->mci_state != MCIS_SSD) 683 { 684 /* send the quit protocol */ 685 mci->mci_state = MCIS_SSD; 686 smtpquit(m, mci, e); 687 } 688 689 return (r); 690 } 691 /* 692 ** SMTPMESSAGE -- send message to server 693 ** 694 ** Parameters: 695 ** f -- format 696 ** m -- the mailer to control formatting. 697 ** a, b, c -- parameters 698 ** 699 ** Returns: 700 ** none. 701 ** 702 ** Side Effects: 703 ** writes message to mci->mci_out. 704 */ 705 706 /*VARARGS1*/ 707 #ifdef __STDC__ 708 smtpmessage(char *f, MAILER *m, MCI *mci, ...) 709 #else 710 smtpmessage(f, m, mci, va_alist) 711 char *f; 712 MAILER *m; 713 MCI *mci; 714 va_dcl 715 #endif 716 { 717 VA_LOCAL_DECL 718 719 VA_START(mci); 720 (void) vsprintf(SmtpMsgBuffer, f, ap); 721 VA_END; 722 723 if (tTd(18, 1) || Verbose) 724 nmessage(">>> %s", SmtpMsgBuffer); 725 if (mci->mci_out != NULL) 726 { 727 fprintf(mci->mci_out, "%s%s", SmtpMsgBuffer, 728 m == NULL ? "\r\n" : m->m_eol); 729 } 730 else if (tTd(18, 1)) 731 { 732 printf("smtpmessage: NULL mci_out\n"); 733 } 734 } 735 736 # endif /* SMTP */ 737