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