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[] = "@(#)srvrsmtp.c 6.35 (Berkeley) 03/30/93 (with SMTP)"; 14 #else 15 static char sccsid[] = "@(#)srvrsmtp.c 6.35 (Berkeley) 03/30/93 (without SMTP)"; 16 #endif 17 #endif /* not lint */ 18 19 # include <errno.h> 20 # include <signal.h> 21 22 # ifdef SMTP 23 24 /* 25 ** SMTP -- run the SMTP protocol. 26 ** 27 ** Parameters: 28 ** none. 29 ** 30 ** Returns: 31 ** never. 32 ** 33 ** Side Effects: 34 ** Reads commands from the input channel and processes 35 ** them. 36 */ 37 38 struct cmd 39 { 40 char *cmdname; /* command name */ 41 int cmdcode; /* internal code, see below */ 42 }; 43 44 /* values for cmdcode */ 45 # define CMDERROR 0 /* bad command */ 46 # define CMDMAIL 1 /* mail -- designate sender */ 47 # define CMDRCPT 2 /* rcpt -- designate recipient */ 48 # define CMDDATA 3 /* data -- send message text */ 49 # define CMDRSET 4 /* rset -- reset state */ 50 # define CMDVRFY 5 /* vrfy -- verify address */ 51 # define CMDEXPN 6 /* expn -- expand address */ 52 # define CMDNOOP 7 /* noop -- do nothing */ 53 # define CMDQUIT 8 /* quit -- close connection and die */ 54 # define CMDHELO 9 /* helo -- be polite */ 55 # define CMDHELP 10 /* help -- give usage info */ 56 # define CMDEHLO 11 /* ehlo -- extended helo (RFC 1425) */ 57 /* non-standard commands */ 58 # define CMDONEX 16 /* onex -- sending one transaction only */ 59 # define CMDVERB 17 /* verb -- go into verbose mode */ 60 /* debugging-only commands, only enabled if SMTPDEBUG is defined */ 61 # define CMDDBGQSHOW 24 /* showq -- show send queue */ 62 # define CMDDBGDEBUG 25 /* debug -- set debug mode */ 63 64 static struct cmd CmdTab[] = 65 { 66 "mail", CMDMAIL, 67 "rcpt", CMDRCPT, 68 "data", CMDDATA, 69 "rset", CMDRSET, 70 "vrfy", CMDVRFY, 71 "expn", CMDEXPN, 72 "help", CMDHELP, 73 "noop", CMDNOOP, 74 "quit", CMDQUIT, 75 "helo", CMDHELO, 76 "ehlo", CMDEHLO, 77 "verb", CMDVERB, 78 "onex", CMDONEX, 79 /* 80 * remaining commands are here only 81 * to trap and log attempts to use them 82 */ 83 "showq", CMDDBGQSHOW, 84 "debug", CMDDBGDEBUG, 85 NULL, CMDERROR, 86 }; 87 88 bool InChild = FALSE; /* true if running in a subprocess */ 89 bool OneXact = FALSE; /* one xaction only this run */ 90 91 #define EX_QUIT 22 /* special code for QUIT command */ 92 93 smtp(e) 94 register ENVELOPE *e; 95 { 96 register char *p; 97 register struct cmd *c; 98 char *cmd; 99 static char *skipword(); 100 auto ADDRESS *vrfyqueue; 101 ADDRESS *a; 102 char *sendinghost; 103 bool gotmail; /* mail command received */ 104 bool gothello; /* helo command received */ 105 bool vrfy; /* set if this is a vrfy command */ 106 char *protocol; /* sending protocol */ 107 long msize; /* approximate maximum message size */ 108 auto char *delimptr; 109 char *id; 110 char inp[MAXLINE]; 111 char cmdbuf[MAXLINE]; 112 char hostbuf[MAXNAME]; 113 extern char Version[]; 114 extern char *macvalue(); 115 extern ADDRESS *recipient(); 116 extern ENVELOPE BlankEnvelope; 117 extern ENVELOPE *newenvelope(); 118 extern char *anynet_ntoa(); 119 120 if (OutChannel != stdout) 121 { 122 /* arrange for debugging output to go to remote host */ 123 (void) close(1); 124 (void) dup(fileno(OutChannel)); 125 } 126 settime(e); 127 CurHostName = RealHostName; 128 setproctitle("srvrsmtp %s", CurHostName); 129 expand("\201e", inp, &inp[sizeof inp], e); 130 message("220 %s", inp); 131 SmtpPhase = "startup"; 132 sendinghost = NULL; 133 protocol = NULL; 134 gothello = FALSE; 135 gotmail = FALSE; 136 for (;;) 137 { 138 /* arrange for backout */ 139 if (setjmp(TopFrame) > 0 && InChild) 140 finis(); 141 QuickAbort = FALSE; 142 HoldErrs = FALSE; 143 LogUsrErrs = FALSE; 144 e->e_flags &= ~EF_VRFYONLY; 145 146 /* setup for the read */ 147 e->e_to = NULL; 148 Errors = 0; 149 (void) fflush(stdout); 150 151 /* read the input line */ 152 p = sfgets(inp, sizeof inp, InChannel, TimeOuts.to_nextcommand); 153 154 /* handle errors */ 155 if (p == NULL) 156 { 157 /* end of file, just die */ 158 message("421 %s Lost input channel from %s", 159 MyHostName, CurHostName); 160 #ifdef LOG 161 if (LogLevel > 1) 162 syslog(LOG_NOTICE, "lost input channel from %s", 163 CurHostName); 164 #endif 165 if (InChild) 166 ExitStat = EX_QUIT; 167 finis(); 168 } 169 170 /* clean up end of line */ 171 fixcrlf(inp, TRUE); 172 173 /* echo command to transcript */ 174 if (e->e_xfp != NULL) 175 fprintf(e->e_xfp, "<<< %s\n", inp); 176 177 /* break off command */ 178 for (p = inp; isascii(*p) && isspace(*p); p++) 179 continue; 180 cmd = cmdbuf; 181 while (*p != '\0' && 182 !(isascii(*p) && isspace(*p)) && 183 cmd < &cmdbuf[sizeof cmdbuf - 2]) 184 *cmd++ = *p++; 185 *cmd = '\0'; 186 187 /* throw away leading whitespace */ 188 while (isascii(*p) && isspace(*p)) 189 p++; 190 191 /* decode command */ 192 for (c = CmdTab; c->cmdname != NULL; c++) 193 { 194 if (!strcasecmp(c->cmdname, cmdbuf)) 195 break; 196 } 197 198 /* reset errors */ 199 errno = 0; 200 201 /* process command */ 202 switch (c->cmdcode) 203 { 204 case CMDHELO: /* hello -- introduce yourself */ 205 case CMDEHLO: /* extended hello */ 206 if (c->cmdcode == CMDEHLO) 207 { 208 protocol = "ESMTP"; 209 SmtpPhase = "EHLO"; 210 } 211 else 212 { 213 protocol = "SMTP"; 214 SmtpPhase = "HELO"; 215 } 216 setproctitle("%s: %s", CurHostName, inp); 217 if (strcasecmp(p, MyHostName) == 0) 218 { 219 /* 220 ** Didn't know about alias or MX, 221 ** or connected to an echo server 222 */ 223 224 message("553 %s config error: mail loops back to myself", 225 MyHostName); 226 break; 227 } 228 (void) strcpy(hostbuf, p); 229 (void) strcat(hostbuf, " ("); 230 (void) strcat(hostbuf, anynet_ntoa(&RealHostAddr)); 231 if (strcasecmp(p, RealHostName) != 0) 232 { 233 auth_warning(e, "Host %s claimed to be %s", 234 RealHostName, p); 235 (void) strcat(hostbuf, "; "); 236 (void) strcat(hostbuf, RealHostName); 237 } 238 (void) strcat(hostbuf, ")"); 239 sendinghost = newstr(hostbuf); 240 241 /* send ext. message -- old systems must ignore */ 242 message("250-%s Hello %s, pleased to meet you", 243 MyHostName, sendinghost); 244 if (!bitset(PRIV_NOEXPN, PrivacyFlags)) 245 message("250-EXPN"); 246 message("250-SIZE"); 247 message("250 HELP"); 248 gothello = TRUE; 249 break; 250 251 case CMDMAIL: /* mail -- designate sender */ 252 SmtpPhase = "MAIL"; 253 254 /* force a sending host even if no HELO given */ 255 if (sendinghost == NULL && macvalue('s', e) == NULL) 256 sendinghost = RealHostName; 257 258 /* check for validity of this command */ 259 if (!gothello) 260 { 261 if (bitset(PRIV_NEEDMAILHELO, PrivacyFlags)) 262 { 263 message("503 Polite people say HELO first"); 264 break; 265 } 266 else 267 { 268 auth_warning(e, 269 "Host %s didn't use HELO protocol", 270 RealHostName); 271 } 272 } 273 if (gotmail) 274 { 275 message("503 Sender already specified"); 276 break; 277 } 278 if (InChild) 279 { 280 errno = 0; 281 syserr("503 Nested MAIL command: MAIL %s", p); 282 finis(); 283 } 284 285 /* fork a subprocess to process this command */ 286 if (runinchild("SMTP-MAIL", e) > 0) 287 break; 288 if (sendinghost != NULL) 289 define('s', sendinghost, e); 290 if (protocol == NULL) 291 protocol = "SMTP"; 292 define('r', protocol, e); 293 initsys(e); 294 setproctitle("%s %s: %s", e->e_id, CurHostName, inp); 295 296 /* child -- go do the processing */ 297 p = skipword(p, "from"); 298 if (p == NULL) 299 break; 300 if (setjmp(TopFrame) > 0) 301 { 302 /* this failed -- undo work */ 303 if (InChild) 304 finis(); 305 break; 306 } 307 QuickAbort = TRUE; 308 309 /* must parse sender first */ 310 delimptr = NULL; 311 setsender(p, e, &delimptr, FALSE); 312 p = delimptr; 313 if (p != NULL && *p != '\0') 314 *p++ = '\0'; 315 316 /* now parse ESMTP arguments */ 317 msize = 0; 318 for (; p != NULL && *p != '\0'; p++) 319 { 320 char *kp; 321 char *vp; 322 323 /* locate the beginning of the keyword */ 324 while (isascii(*p) && isspace(*p)) 325 p++; 326 if (*p == '\0') 327 break; 328 kp = p; 329 330 /* skip to the value portion */ 331 while (isascii(*p) && isalnum(*p) || *p == '-') 332 p++; 333 if (*p == '=') 334 { 335 *p++ = '\0'; 336 vp = p; 337 338 /* skip to the end of the value */ 339 while (*p != '\0' && *p != ' ' && 340 !(isascii(*p) && iscntrl(*p)) && 341 *p != '=') 342 p++; 343 } 344 345 if (*p != '\0') 346 *p++ = '\0'; 347 348 if (tTd(19, 1)) 349 printf("MAIL: got arg %s=%s\n", kp, 350 vp == NULL ? "<null>" : vp); 351 352 if (strcasecmp(kp, "size") == 0) 353 { 354 if (kp == NULL) 355 { 356 usrerr("501 SIZE requires a value"); 357 /* NOTREACHED */ 358 } 359 msize = atol(vp); 360 } 361 else 362 { 363 usrerr("501 %s parameter unrecognized", kp); 364 /* NOTREACHED */ 365 } 366 } 367 368 if (!enoughspace(msize)) 369 { 370 message("452 Insufficient disk space; try again later"); 371 break; 372 } 373 message("250 Sender ok"); 374 gotmail = TRUE; 375 break; 376 377 case CMDRCPT: /* rcpt -- designate recipient */ 378 if (!gotmail) 379 { 380 usrerr("503 Need MAIL before RCPT"); 381 break; 382 } 383 SmtpPhase = "RCPT"; 384 setproctitle("%s %s: %s", e->e_id, CurHostName, inp); 385 if (setjmp(TopFrame) > 0) 386 { 387 e->e_flags &= ~EF_FATALERRS; 388 break; 389 } 390 QuickAbort = TRUE; 391 LogUsrErrs = TRUE; 392 393 /* optimization -- if queueing, don't expand aliases */ 394 if (e->e_sendmode == SM_QUEUE) 395 e->e_flags |= EF_VRFYONLY; 396 397 p = skipword(p, "to"); 398 if (p == NULL) 399 break; 400 a = parseaddr(p, (ADDRESS *) NULL, 1, ' ', NULL, e); 401 if (a == NULL) 402 break; 403 a->q_flags |= QPRIMARY; 404 a = recipient(a, &e->e_sendqueue, e); 405 if (Errors != 0) 406 break; 407 408 /* no errors during parsing, but might be a duplicate */ 409 e->e_to = p; 410 if (!bitset(QBADADDR, a->q_flags)) 411 message("250 Recipient ok"); 412 else 413 { 414 /* punt -- should keep message in ADDRESS.... */ 415 message("550 Addressee unknown"); 416 } 417 e->e_to = NULL; 418 break; 419 420 case CMDDATA: /* data -- text of mail */ 421 SmtpPhase = "DATA"; 422 if (!gotmail) 423 { 424 message("503 Need MAIL command"); 425 break; 426 } 427 else if (e->e_nrcpts <= 0) 428 { 429 message("503 Need RCPT (recipient)"); 430 break; 431 } 432 433 /* collect the text of the message */ 434 SmtpPhase = "collect"; 435 setproctitle("%s %s: %s", e->e_id, CurHostName, inp); 436 collect(TRUE, e); 437 if (Errors != 0) 438 break; 439 440 /* 441 ** Arrange to send to everyone. 442 ** If sending to multiple people, mail back 443 ** errors rather than reporting directly. 444 ** In any case, don't mail back errors for 445 ** anything that has happened up to 446 ** now (the other end will do this). 447 ** Truncate our transcript -- the mail has gotten 448 ** to us successfully, and if we have 449 ** to mail this back, it will be easier 450 ** on the reader. 451 ** Then send to everyone. 452 ** Finally give a reply code. If an error has 453 ** already been given, don't mail a 454 ** message back. 455 ** We goose error returns by clearing error bit. 456 */ 457 458 SmtpPhase = "delivery"; 459 if (e->e_nrcpts != 1) 460 { 461 HoldErrs = TRUE; 462 e->e_errormode = EM_MAIL; 463 } 464 e->e_flags &= ~EF_FATALERRS; 465 e->e_xfp = freopen(queuename(e, 'x'), "w", e->e_xfp); 466 id = e->e_id; 467 468 /* send to all recipients */ 469 sendall(e, SM_DEFAULT); 470 e->e_to = NULL; 471 472 /* save statistics */ 473 markstats(e, (ADDRESS *) NULL); 474 475 /* issue success if appropriate and reset */ 476 if (Errors == 0 || HoldErrs) 477 message("250 %s Message accepted for delivery", id); 478 else 479 e->e_flags &= ~EF_FATALERRS; 480 481 /* if in a child, pop back to our parent */ 482 if (InChild) 483 finis(); 484 485 /* clean up a bit */ 486 gotmail = FALSE; 487 dropenvelope(e); 488 CurEnv = e = newenvelope(e, CurEnv); 489 e->e_flags = BlankEnvelope.e_flags; 490 break; 491 492 case CMDRSET: /* rset -- reset state */ 493 message("250 Reset state"); 494 if (InChild) 495 finis(); 496 497 /* clean up a bit */ 498 gotmail = FALSE; 499 dropenvelope(e); 500 CurEnv = e = newenvelope(e, CurEnv); 501 break; 502 503 case CMDVRFY: /* vrfy -- verify address */ 504 case CMDEXPN: /* expn -- expand address */ 505 vrfy = c->cmdcode == CMDVRFY; 506 if (bitset(vrfy ? PRIV_NOVRFY : PRIV_NOEXPN, 507 PrivacyFlags)) 508 { 509 if (vrfy) 510 message("252 Who's to say?"); 511 else 512 message("502 That's none of your business"); 513 break; 514 } 515 else if (!gothello && 516 bitset(vrfy ? PRIV_NEEDVRFYHELO : PRIV_NEEDEXPNHELO, 517 PrivacyFlags)) 518 { 519 message("503 I demand that you introduce yourself first"); 520 break; 521 } 522 if (runinchild(vrfy ? "SMTP-VRFY" : "SMTP-EXPN", e) > 0) 523 break; 524 setproctitle("%s: %s", CurHostName, inp); 525 #ifdef LOG 526 if (LogLevel > 5) 527 syslog(LOG_INFO, "%s: %s", CurHostName, inp); 528 #endif 529 vrfyqueue = NULL; 530 QuickAbort = TRUE; 531 if (vrfy) 532 e->e_flags |= EF_VRFYONLY; 533 (void) sendtolist(p, (ADDRESS *) NULL, &vrfyqueue, e); 534 if (Errors != 0) 535 { 536 if (InChild) 537 finis(); 538 break; 539 } 540 while (vrfyqueue != NULL) 541 { 542 register ADDRESS *a = vrfyqueue->q_next; 543 char *code; 544 545 while (a != NULL && bitset(QDONTSEND|QBADADDR, a->q_flags)) 546 a = a->q_next; 547 548 if (!bitset(QDONTSEND|QBADADDR, vrfyqueue->q_flags)) 549 printvrfyaddr(vrfyqueue, a == NULL); 550 else if (a == NULL) 551 message("554 Self destructive alias loop"); 552 vrfyqueue = a; 553 } 554 if (InChild) 555 finis(); 556 break; 557 558 case CMDHELP: /* help -- give user info */ 559 help(p); 560 break; 561 562 case CMDNOOP: /* noop -- do nothing */ 563 message("200 OK"); 564 break; 565 566 case CMDQUIT: /* quit -- leave mail */ 567 message("221 %s closing connection", MyHostName); 568 if (InChild) 569 ExitStat = EX_QUIT; 570 finis(); 571 572 case CMDVERB: /* set verbose mode */ 573 Verbose = TRUE; 574 e->e_sendmode = SM_DELIVER; 575 message("200 Verbose mode"); 576 break; 577 578 case CMDONEX: /* doing one transaction only */ 579 OneXact = TRUE; 580 message("200 Only one transaction"); 581 break; 582 583 # ifdef SMTPDEBUG 584 case CMDDBGQSHOW: /* show queues */ 585 printf("Send Queue="); 586 printaddr(e->e_sendqueue, TRUE); 587 break; 588 589 case CMDDBGDEBUG: /* set debug mode */ 590 tTsetup(tTdvect, sizeof tTdvect, "0-99.1"); 591 tTflag(p); 592 message("200 Debug set"); 593 break; 594 595 # else /* not SMTPDEBUG */ 596 597 case CMDDBGQSHOW: /* show queues */ 598 case CMDDBGDEBUG: /* set debug mode */ 599 # ifdef LOG 600 if (LogLevel > 0) 601 syslog(LOG_NOTICE, 602 "\"%s\" command from %s (%s)", 603 c->cmdname, RealHostName, 604 anynet_ntoa(&RealHostAddr)); 605 # endif 606 /* FALL THROUGH */ 607 # endif /* SMTPDEBUG */ 608 609 case CMDERROR: /* unknown command */ 610 message("500 Command unrecognized"); 611 break; 612 613 default: 614 errno = 0; 615 syserr("500 smtp: unknown code %d", c->cmdcode); 616 break; 617 } 618 } 619 } 620 /* 621 ** SKIPWORD -- skip a fixed word. 622 ** 623 ** Parameters: 624 ** p -- place to start looking. 625 ** w -- word to skip. 626 ** 627 ** Returns: 628 ** p following w. 629 ** NULL on error. 630 ** 631 ** Side Effects: 632 ** clobbers the p data area. 633 */ 634 635 static char * 636 skipword(p, w) 637 register char *p; 638 char *w; 639 { 640 register char *q; 641 642 /* find beginning of word */ 643 while (isascii(*p) && isspace(*p)) 644 p++; 645 q = p; 646 647 /* find end of word */ 648 while (*p != '\0' && *p != ':' && !(isascii(*p) && isspace(*p))) 649 p++; 650 while (isascii(*p) && isspace(*p)) 651 *p++ = '\0'; 652 if (*p != ':') 653 { 654 syntax: 655 message("501 Syntax error"); 656 Errors++; 657 return (NULL); 658 } 659 *p++ = '\0'; 660 while (isascii(*p) && isspace(*p)) 661 p++; 662 663 /* see if the input word matches desired word */ 664 if (strcasecmp(q, w)) 665 goto syntax; 666 667 return (p); 668 } 669 /* 670 ** PRINTVRFYADDR -- print an entry in the verify queue 671 ** 672 ** Parameters: 673 ** a -- the address to print 674 ** last -- set if this is the last one. 675 ** 676 ** Returns: 677 ** none. 678 ** 679 ** Side Effects: 680 ** Prints the appropriate 250 codes. 681 */ 682 683 printvrfyaddr(a, last) 684 register ADDRESS *a; 685 bool last; 686 { 687 char fmtbuf[20]; 688 689 strcpy(fmtbuf, "250"); 690 fmtbuf[3] = last ? ' ' : '-'; 691 692 if (strchr(a->q_paddr, '<') != NULL) 693 strcpy(&fmtbuf[4], "%s"); 694 else if (a->q_fullname == NULL) 695 strcpy(&fmtbuf[4], "<%s>"); 696 else 697 { 698 strcpy(&fmtbuf[4], "%s <%s>"); 699 message(fmtbuf, a->q_fullname, a->q_paddr); 700 return; 701 } 702 message(fmtbuf, a->q_paddr); 703 } 704 /* 705 ** HELP -- implement the HELP command. 706 ** 707 ** Parameters: 708 ** topic -- the topic we want help for. 709 ** 710 ** Returns: 711 ** none. 712 ** 713 ** Side Effects: 714 ** outputs the help file to message output. 715 */ 716 717 help(topic) 718 char *topic; 719 { 720 register FILE *hf; 721 int len; 722 char buf[MAXLINE]; 723 bool noinfo; 724 725 if (HelpFile == NULL || (hf = fopen(HelpFile, "r")) == NULL) 726 { 727 /* no help */ 728 errno = 0; 729 message("502 HELP not implemented"); 730 return; 731 } 732 733 if (topic == NULL || *topic == '\0') 734 topic = "smtp"; 735 else 736 makelower(topic); 737 738 len = strlen(topic); 739 noinfo = TRUE; 740 741 while (fgets(buf, sizeof buf, hf) != NULL) 742 { 743 if (strncmp(buf, topic, len) == 0) 744 { 745 register char *p; 746 747 p = strchr(buf, '\t'); 748 if (p == NULL) 749 p = buf; 750 else 751 p++; 752 fixcrlf(p, TRUE); 753 message("214-%s", p); 754 noinfo = FALSE; 755 } 756 } 757 758 if (noinfo) 759 message("504 HELP topic unknown"); 760 else 761 message("214 End of HELP info"); 762 (void) fclose(hf); 763 } 764 /* 765 ** RUNINCHILD -- return twice -- once in the child, then in the parent again 766 ** 767 ** Parameters: 768 ** label -- a string used in error messages 769 ** 770 ** Returns: 771 ** zero in the child 772 ** one in the parent 773 ** 774 ** Side Effects: 775 ** none. 776 */ 777 778 runinchild(label, e) 779 char *label; 780 register ENVELOPE *e; 781 { 782 int childpid; 783 784 if (!OneXact) 785 { 786 childpid = dofork(); 787 if (childpid < 0) 788 { 789 syserr("%s: cannot fork", label); 790 return (1); 791 } 792 if (childpid > 0) 793 { 794 auto int st; 795 796 /* parent -- wait for child to complete */ 797 st = waitfor(childpid); 798 if (st == -1) 799 syserr("%s: lost child", label); 800 801 /* if we exited on a QUIT command, complete the process */ 802 if (st == (EX_QUIT << 8)) 803 finis(); 804 805 return (1); 806 } 807 else 808 { 809 /* child */ 810 InChild = TRUE; 811 QuickAbort = FALSE; 812 clearenvelope(e, FALSE); 813 } 814 } 815 816 /* open alias database */ 817 initaliases(AliasFile, FALSE, e); 818 819 return (0); 820 } 821 822 # endif /* SMTP */ 823