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.29 (Berkeley) 03/23/93 (with SMTP)"; 14 #else 15 static char sccsid[] = "@(#)srvrsmtp.c 6.29 (Berkeley) 03/23/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 message("503 Polite people say HELO first"); 263 else 264 auth_warning(e, 265 "Host %s didn't use HELO protocol", 266 RealHostName); 267 break; 268 } 269 if (gotmail) 270 { 271 message("503 Sender already specified"); 272 break; 273 } 274 if (InChild) 275 { 276 errno = 0; 277 syserr("503 Nested MAIL command: MAIL %s", p); 278 finis(); 279 } 280 281 /* fork a subprocess to process this command */ 282 if (runinchild("SMTP-MAIL", e) > 0) 283 break; 284 if (sendinghost != NULL) 285 define('s', sendinghost, e); 286 if (protocol == NULL) 287 protocol = "SMTP"; 288 define('r', protocol, e); 289 initsys(e); 290 setproctitle("%s %s: %s", e->e_id, CurHostName, inp); 291 292 /* child -- go do the processing */ 293 p = skipword(p, "from"); 294 if (p == NULL) 295 break; 296 if (setjmp(TopFrame) > 0) 297 { 298 /* this failed -- undo work */ 299 if (InChild) 300 finis(); 301 break; 302 } 303 QuickAbort = TRUE; 304 305 /* must parse sender first */ 306 delimptr = NULL; 307 setsender(p, e, &delimptr, FALSE); 308 p = delimptr; 309 if (p != NULL && *p != '\0') 310 *p++ = '\0'; 311 312 /* now parse ESMTP arguments */ 313 msize = 0; 314 for (; p != NULL && *p != '\0'; p++) 315 { 316 char *kp; 317 char *vp; 318 319 /* locate the beginning of the keyword */ 320 while (isascii(*p) && isspace(*p)) 321 p++; 322 if (*p == '\0') 323 break; 324 kp = p; 325 326 /* skip to the value portion */ 327 while (isascii(*p) && isalnum(*p) || *p == '-') 328 p++; 329 if (*p == '=') 330 { 331 *p++ = '\0'; 332 vp = p; 333 334 /* skip to the end of the value */ 335 while (*p != '\0' && *p != ' ' && 336 !(isascii(*p) && iscntrl(*p)) && 337 *p != '=') 338 p++; 339 } 340 341 if (*p != '\0') 342 *p++ = '\0'; 343 344 if (tTd(19, 1)) 345 printf("MAIL: got arg %s=%s\n", kp, 346 vp == NULL ? "<null>" : vp); 347 348 if (strcasecmp(kp, "size") == 0) 349 { 350 if (kp == NULL) 351 { 352 usrerr("501 SIZE requires a value"); 353 /* NOTREACHED */ 354 } 355 msize = atol(vp); 356 } 357 else 358 { 359 usrerr("501 %s parameter unrecognized", kp); 360 /* NOTREACHED */ 361 } 362 } 363 364 if (!enoughspace(msize)) 365 { 366 message("452 Insufficient disk space; try again later"); 367 break; 368 } 369 message("250 Sender ok"); 370 gotmail = TRUE; 371 break; 372 373 case CMDRCPT: /* rcpt -- designate recipient */ 374 SmtpPhase = "RCPT"; 375 setproctitle("%s %s: %s", e->e_id, CurHostName, inp); 376 if (setjmp(TopFrame) > 0) 377 { 378 e->e_flags &= ~EF_FATALERRS; 379 break; 380 } 381 QuickAbort = TRUE; 382 LogUsrErrs = TRUE; 383 384 /* optimization -- if queueing, don't expand aliases */ 385 if (e->e_sendmode == SM_QUEUE) 386 e->e_flags |= EF_VRFYONLY; 387 388 p = skipword(p, "to"); 389 if (p == NULL) 390 break; 391 a = parseaddr(p, (ADDRESS *) NULL, 1, ' ', NULL, e); 392 if (a == NULL) 393 break; 394 a->q_flags |= QPRIMARY; 395 a = recipient(a, &e->e_sendqueue, e); 396 if (Errors != 0) 397 break; 398 399 /* no errors during parsing, but might be a duplicate */ 400 e->e_to = p; 401 if (!bitset(QBADADDR, a->q_flags)) 402 message("250 Recipient ok"); 403 else 404 { 405 /* punt -- should keep message in ADDRESS.... */ 406 message("550 Addressee unknown"); 407 } 408 e->e_to = NULL; 409 break; 410 411 case CMDDATA: /* data -- text of mail */ 412 SmtpPhase = "DATA"; 413 if (!gotmail) 414 { 415 message("503 Need MAIL command"); 416 break; 417 } 418 else if (e->e_nrcpts <= 0) 419 { 420 message("503 Need RCPT (recipient)"); 421 break; 422 } 423 424 /* collect the text of the message */ 425 SmtpPhase = "collect"; 426 setproctitle("%s %s: %s", e->e_id, CurHostName, inp); 427 collect(TRUE, e); 428 if (Errors != 0) 429 break; 430 431 /* 432 ** Arrange to send to everyone. 433 ** If sending to multiple people, mail back 434 ** errors rather than reporting directly. 435 ** In any case, don't mail back errors for 436 ** anything that has happened up to 437 ** now (the other end will do this). 438 ** Truncate our transcript -- the mail has gotten 439 ** to us successfully, and if we have 440 ** to mail this back, it will be easier 441 ** on the reader. 442 ** Then send to everyone. 443 ** Finally give a reply code. If an error has 444 ** already been given, don't mail a 445 ** message back. 446 ** We goose error returns by clearing error bit. 447 */ 448 449 SmtpPhase = "delivery"; 450 if (e->e_nrcpts != 1) 451 { 452 HoldErrs = TRUE; 453 e->e_errormode = EM_MAIL; 454 } 455 e->e_flags &= ~EF_FATALERRS; 456 e->e_xfp = freopen(queuename(e, 'x'), "w", e->e_xfp); 457 id = e->e_id; 458 459 /* send to all recipients */ 460 sendall(e, SM_DEFAULT); 461 e->e_to = NULL; 462 463 /* save statistics */ 464 markstats(e, (ADDRESS *) NULL); 465 466 /* issue success if appropriate and reset */ 467 if (Errors == 0 || HoldErrs) 468 message("250 %s OK", id); 469 else 470 e->e_flags &= ~EF_FATALERRS; 471 472 /* if in a child, pop back to our parent */ 473 if (InChild) 474 finis(); 475 476 /* clean up a bit */ 477 gotmail = FALSE; 478 dropenvelope(e); 479 CurEnv = e = newenvelope(e, CurEnv); 480 e->e_flags = BlankEnvelope.e_flags; 481 break; 482 483 case CMDRSET: /* rset -- reset state */ 484 message("250 Reset state"); 485 if (InChild) 486 finis(); 487 488 /* clean up a bit */ 489 gotmail = FALSE; 490 dropenvelope(e); 491 CurEnv = e = newenvelope(e, CurEnv); 492 break; 493 494 case CMDVRFY: /* vrfy -- verify address */ 495 case CMDEXPN: /* expn -- expand address */ 496 vrfy = c->cmdcode == CMDVRFY; 497 if (bitset(vrfy ? PRIV_NOVRFY : PRIV_NOEXPN, 498 PrivacyFlags)) 499 { 500 if (vrfy) 501 message("252 Who's to say?"); 502 else 503 message("502 That's none of your business"); 504 break; 505 } 506 else if (!gothello && 507 bitset(vrfy ? PRIV_NEEDVRFYHELO : PRIV_NEEDEXPNHELO, 508 PrivacyFlags)) 509 { 510 message("503 I demand that you introduce yourself first"); 511 break; 512 } 513 if (runinchild(vrfy ? "SMTP-VRFY" : "SMTP-EXPN", e) > 0) 514 break; 515 setproctitle("%s: %s", CurHostName, inp); 516 #ifdef LOG 517 if (LogLevel > 5) 518 syslog(LOG_INFO, "%s: %s", CurHostName, inp); 519 #endif 520 vrfyqueue = NULL; 521 QuickAbort = TRUE; 522 if (vrfy) 523 e->e_flags |= EF_VRFYONLY; 524 (void) sendtolist(p, (ADDRESS *) NULL, &vrfyqueue, e); 525 if (Errors != 0) 526 { 527 if (InChild) 528 finis(); 529 break; 530 } 531 while (vrfyqueue != NULL) 532 { 533 register ADDRESS *a = vrfyqueue->q_next; 534 char *code; 535 536 while (a != NULL && bitset(QDONTSEND|QBADADDR, a->q_flags)) 537 a = a->q_next; 538 539 if (!bitset(QDONTSEND|QBADADDR, vrfyqueue->q_flags)) 540 printvrfyaddr(vrfyqueue, a == NULL); 541 else if (a == NULL) 542 message("554 Self destructive alias loop"); 543 vrfyqueue = a; 544 } 545 if (InChild) 546 finis(); 547 break; 548 549 case CMDHELP: /* help -- give user info */ 550 help(p); 551 break; 552 553 case CMDNOOP: /* noop -- do nothing */ 554 message("200 OK"); 555 break; 556 557 case CMDQUIT: /* quit -- leave mail */ 558 message("221 %s closing connection", MyHostName); 559 if (InChild) 560 ExitStat = EX_QUIT; 561 finis(); 562 563 case CMDVERB: /* set verbose mode */ 564 Verbose = TRUE; 565 e->e_sendmode = SM_DELIVER; 566 message("200 Verbose mode"); 567 break; 568 569 case CMDONEX: /* doing one transaction only */ 570 OneXact = TRUE; 571 message("200 Only one transaction"); 572 break; 573 574 # ifdef SMTPDEBUG 575 case CMDDBGQSHOW: /* show queues */ 576 printf("Send Queue="); 577 printaddr(e->e_sendqueue, TRUE); 578 break; 579 580 case CMDDBGDEBUG: /* set debug mode */ 581 tTsetup(tTdvect, sizeof tTdvect, "0-99.1"); 582 tTflag(p); 583 message("200 Debug set"); 584 break; 585 586 # else /* not SMTPDEBUG */ 587 588 case CMDDBGQSHOW: /* show queues */ 589 case CMDDBGDEBUG: /* set debug mode */ 590 # ifdef LOG 591 if (LogLevel > 0) 592 syslog(LOG_NOTICE, 593 "\"%s\" command from %s (%s)", 594 c->cmdname, RealHostName, 595 anynet_ntoa(&RealHostAddr)); 596 # endif 597 /* FALL THROUGH */ 598 # endif /* SMTPDEBUG */ 599 600 case CMDERROR: /* unknown command */ 601 message("500 Command unrecognized"); 602 break; 603 604 default: 605 errno = 0; 606 syserr("500 smtp: unknown code %d", c->cmdcode); 607 break; 608 } 609 } 610 } 611 /* 612 ** SKIPWORD -- skip a fixed word. 613 ** 614 ** Parameters: 615 ** p -- place to start looking. 616 ** w -- word to skip. 617 ** 618 ** Returns: 619 ** p following w. 620 ** NULL on error. 621 ** 622 ** Side Effects: 623 ** clobbers the p data area. 624 */ 625 626 static char * 627 skipword(p, w) 628 register char *p; 629 char *w; 630 { 631 register char *q; 632 633 /* find beginning of word */ 634 while (isascii(*p) && isspace(*p)) 635 p++; 636 q = p; 637 638 /* find end of word */ 639 while (*p != '\0' && *p != ':' && !(isascii(*p) && isspace(*p))) 640 p++; 641 while (isascii(*p) && isspace(*p)) 642 *p++ = '\0'; 643 if (*p != ':') 644 { 645 syntax: 646 message("501 Syntax error"); 647 Errors++; 648 return (NULL); 649 } 650 *p++ = '\0'; 651 while (isascii(*p) && isspace(*p)) 652 p++; 653 654 /* see if the input word matches desired word */ 655 if (strcasecmp(q, w)) 656 goto syntax; 657 658 return (p); 659 } 660 /* 661 ** PRINTVRFYADDR -- print an entry in the verify queue 662 ** 663 ** Parameters: 664 ** a -- the address to print 665 ** last -- set if this is the last one. 666 ** 667 ** Returns: 668 ** none. 669 ** 670 ** Side Effects: 671 ** Prints the appropriate 250 codes. 672 */ 673 674 printvrfyaddr(a, last) 675 register ADDRESS *a; 676 bool last; 677 { 678 char fmtbuf[20]; 679 680 strcpy(fmtbuf, "250"); 681 fmtbuf[3] = last ? ' ' : '-'; 682 683 if (strchr(a->q_paddr, '<') != NULL) 684 strcpy(&fmtbuf[4], "%s"); 685 else if (a->q_fullname == NULL) 686 strcpy(&fmtbuf[4], "<%s>"); 687 else 688 { 689 strcpy(&fmtbuf[4], "%s <%s>"); 690 message(fmtbuf, a->q_fullname, a->q_paddr); 691 return; 692 } 693 message(fmtbuf, a->q_paddr); 694 } 695 /* 696 ** HELP -- implement the HELP command. 697 ** 698 ** Parameters: 699 ** topic -- the topic we want help for. 700 ** 701 ** Returns: 702 ** none. 703 ** 704 ** Side Effects: 705 ** outputs the help file to message output. 706 */ 707 708 help(topic) 709 char *topic; 710 { 711 register FILE *hf; 712 int len; 713 char buf[MAXLINE]; 714 bool noinfo; 715 716 if (HelpFile == NULL || (hf = fopen(HelpFile, "r")) == NULL) 717 { 718 /* no help */ 719 errno = 0; 720 message("502 HELP not implemented"); 721 return; 722 } 723 724 if (topic == NULL || *topic == '\0') 725 topic = "smtp"; 726 else 727 makelower(topic); 728 729 len = strlen(topic); 730 noinfo = TRUE; 731 732 while (fgets(buf, sizeof buf, hf) != NULL) 733 { 734 if (strncmp(buf, topic, len) == 0) 735 { 736 register char *p; 737 738 p = strchr(buf, '\t'); 739 if (p == NULL) 740 p = buf; 741 else 742 p++; 743 fixcrlf(p, TRUE); 744 message("214-%s", p); 745 noinfo = FALSE; 746 } 747 } 748 749 if (noinfo) 750 message("504 HELP topic unknown"); 751 else 752 message("214 End of HELP info"); 753 (void) fclose(hf); 754 } 755 /* 756 ** RUNINCHILD -- return twice -- once in the child, then in the parent again 757 ** 758 ** Parameters: 759 ** label -- a string used in error messages 760 ** 761 ** Returns: 762 ** zero in the child 763 ** one in the parent 764 ** 765 ** Side Effects: 766 ** none. 767 */ 768 769 runinchild(label, e) 770 char *label; 771 register ENVELOPE *e; 772 { 773 int childpid; 774 775 if (!OneXact) 776 { 777 childpid = dofork(); 778 if (childpid < 0) 779 { 780 syserr("%s: cannot fork", label); 781 return (1); 782 } 783 if (childpid > 0) 784 { 785 auto int st; 786 787 /* parent -- wait for child to complete */ 788 st = waitfor(childpid); 789 if (st == -1) 790 syserr("%s: lost child", label); 791 792 /* if we exited on a QUIT command, complete the process */ 793 if (st == (EX_QUIT << 8)) 794 finis(); 795 796 return (1); 797 } 798 else 799 { 800 /* child */ 801 InChild = TRUE; 802 QuickAbort = FALSE; 803 clearenvelope(e, FALSE); 804 } 805 } 806 807 /* open alias database */ 808 initaliases(AliasFile, FALSE, e); 809 810 return (0); 811 } 812 813 # endif /* SMTP */ 814