1 /* 2 ** Sendmail 3 ** Copyright (c) 1983 Eric P. Allman 4 ** Berkeley, California 5 ** 6 ** Copyright (c) 1983 Regents of the University of California. 7 ** All rights reserved. The Berkeley software License Agreement 8 ** specifies the terms and conditions for redistribution. 9 */ 10 11 12 # include <errno.h> 13 # include "sendmail.h" 14 # include <signal.h> 15 16 # ifndef SMTP 17 # ifndef lint 18 static char SccsId[] = "@(#)srvrsmtp.c 5.15 (Berkeley) 09/23/85 (no SMTP)"; 19 # endif not lint 20 # else SMTP 21 22 # ifndef lint 23 static char SccsId[] = "@(#)srvrsmtp.c 5.15 (Berkeley) 09/23/85"; 24 # endif not lint 25 26 /* 27 ** SMTP -- run the SMTP protocol. 28 ** 29 ** Parameters: 30 ** none. 31 ** 32 ** Returns: 33 ** never. 34 ** 35 ** Side Effects: 36 ** Reads commands from the input channel and processes 37 ** them. 38 */ 39 40 struct cmd 41 { 42 char *cmdname; /* command name */ 43 int cmdcode; /* internal code, see below */ 44 }; 45 46 /* values for cmdcode */ 47 # define CMDERROR 0 /* bad command */ 48 # define CMDMAIL 1 /* mail -- designate sender */ 49 # define CMDRCPT 2 /* rcpt -- designate recipient */ 50 # define CMDDATA 3 /* data -- send message text */ 51 # define CMDRSET 4 /* rset -- reset state */ 52 # define CMDVRFY 5 /* vrfy -- verify address */ 53 # define CMDHELP 6 /* help -- give usage info */ 54 # define CMDNOOP 7 /* noop -- do nothing */ 55 # define CMDQUIT 8 /* quit -- close connection and die */ 56 # define CMDHELO 9 /* helo -- be polite */ 57 # define CMDDBGQSHOW 10 /* showq -- show send queue (DEBUG) */ 58 # define CMDDBGDEBUG 11 /* debug -- set debug mode */ 59 # define CMDVERB 12 /* verb -- go into verbose mode */ 60 # define CMDDBGKILL 13 /* kill -- kill sendmail */ 61 # define CMDDBGWIZ 14 /* wiz -- become a wizard */ 62 # define CMDONEX 15 /* onex -- sending one transaction only */ 63 64 static struct cmd CmdTab[] = 65 { 66 "mail", CMDMAIL, 67 "rcpt", CMDRCPT, 68 "data", CMDDATA, 69 "rset", CMDRSET, 70 "vrfy", CMDVRFY, 71 "expn", CMDVRFY, 72 "help", CMDHELP, 73 "noop", CMDNOOP, 74 "quit", CMDQUIT, 75 "helo", CMDHELO, 76 "verb", CMDVERB, 77 "onex", CMDONEX, 78 # ifdef DEBUG 79 "showq", CMDDBGQSHOW, 80 "debug", CMDDBGDEBUG, 81 # endif DEBUG 82 # ifdef WIZ 83 "kill", CMDDBGKILL, 84 # endif WIZ 85 "wiz", CMDDBGWIZ, 86 NULL, CMDERROR, 87 }; 88 89 # ifdef WIZ 90 bool IsWiz = FALSE; /* set if we are a wizard */ 91 # endif WIZ 92 char *WizWord; /* the wizard word to compare against */ 93 bool InChild = FALSE; /* true if running in a subprocess */ 94 bool OneXact = FALSE; /* one xaction only this run */ 95 96 #define EX_QUIT 22 /* special code for QUIT command */ 97 98 smtp() 99 { 100 register char *p; 101 register struct cmd *c; 102 char *cmd; 103 extern char *skipword(); 104 extern bool sameword(); 105 bool hasmail; /* mail command received */ 106 auto ADDRESS *vrfyqueue; 107 ADDRESS *a; 108 char inp[MAXLINE]; 109 char cmdbuf[100]; 110 extern char Version[]; 111 extern tick(); 112 extern bool iswiz(); 113 extern char *arpadate(); 114 extern char *macvalue(); 115 extern ADDRESS *recipient(); 116 extern ENVELOPE BlankEnvelope; 117 extern ENVELOPE *newenvelope(); 118 119 hasmail = FALSE; 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(); 127 if (RealHostName != NULL) 128 setproctitle("srvrsmtp %s", RealHostName); 129 expand("\001e", inp, &inp[sizeof inp], CurEnv); 130 message("220", inp); 131 SmtpPhase = "startup"; 132 for (;;) 133 { 134 /* arrange for backout */ 135 if (setjmp(TopFrame) > 0 && InChild) 136 finis(); 137 QuickAbort = FALSE; 138 HoldErrs = FALSE; 139 140 /* setup for the read */ 141 CurEnv->e_to = NULL; 142 Errors = 0; 143 (void) fflush(stdout); 144 145 /* read the input line */ 146 p = sfgets(inp, sizeof inp, InChannel); 147 148 /* handle errors */ 149 if (p == NULL) 150 { 151 /* end of file, just die */ 152 message("421", "%s Lost input channel", HostName); 153 finis(); 154 } 155 156 /* clean up end of line */ 157 fixcrlf(inp, TRUE); 158 159 /* echo command to transcript */ 160 if (CurEnv->e_xfp != NULL) 161 fprintf(CurEnv->e_xfp, "<<< %s\n", inp); 162 163 /* break off command */ 164 for (p = inp; isspace(*p); p++) 165 continue; 166 cmd = p; 167 for (cmd = cmdbuf; *p != '\0' && !isspace(*p); ) 168 *cmd++ = *p++; 169 *cmd = '\0'; 170 171 /* decode command */ 172 for (c = CmdTab; c->cmdname != NULL; c++) 173 { 174 if (sameword(c->cmdname, cmdbuf)) 175 break; 176 } 177 178 /* process command */ 179 switch (c->cmdcode) 180 { 181 case CMDHELO: /* hello -- introduce yourself */ 182 SmtpPhase = "HELO"; 183 setproctitle("%s: %s", RealHostName, inp); 184 if (sameword(p, HostName)) 185 { 186 /* connected to an echo server */ 187 message("553", "%s I refuse to talk to myself", 188 HostName); 189 break; 190 } 191 if (RealHostName != NULL && !sameword(p, RealHostName)) 192 { 193 char hostbuf[MAXNAME]; 194 195 (void) sprintf(hostbuf, "%s (%s)", p, RealHostName); 196 define('s', newstr(hostbuf), CurEnv); 197 } 198 else 199 define('s', newstr(p), CurEnv); 200 message("250", "%s Hello %s, pleased to meet you", 201 HostName, p); 202 break; 203 204 case CMDMAIL: /* mail -- designate sender */ 205 SmtpPhase = "MAIL"; 206 207 /* force a sending host even if no HELO given */ 208 if (RealHostName != NULL && macvalue('s', CurEnv) == NULL) 209 define('s', RealHostName, CurEnv); 210 211 /* check for validity of this command */ 212 if (hasmail) 213 { 214 message("503", "Sender already specified"); 215 break; 216 } 217 if (InChild) 218 { 219 syserr("Nested MAIL command"); 220 exit(0); 221 } 222 223 /* fork a subprocess to process this command */ 224 if (runinchild("SMTP-MAIL") > 0) 225 break; 226 initsys(); 227 setproctitle("%s %s: %s", CurEnv->e_id, 228 RealHostName, inp); 229 230 /* child -- go do the processing */ 231 p = skipword(p, "from"); 232 if (p == NULL) 233 break; 234 setsender(p); 235 if (Errors == 0) 236 { 237 message("250", "Sender ok"); 238 hasmail = TRUE; 239 } 240 else if (InChild) 241 finis(); 242 break; 243 244 case CMDRCPT: /* rcpt -- designate recipient */ 245 SmtpPhase = "RCPT"; 246 setproctitle("%s %s: %s", CurEnv->e_id, 247 RealHostName, inp); 248 if (setjmp(TopFrame) > 0) 249 { 250 CurEnv->e_flags &= ~EF_FATALERRS; 251 break; 252 } 253 QuickAbort = TRUE; 254 p = skipword(p, "to"); 255 if (p == NULL) 256 break; 257 a = parseaddr(p, (ADDRESS *) NULL, 1, '\0'); 258 if (a == NULL) 259 break; 260 a->q_flags |= QPRIMARY; 261 a = recipient(a, &CurEnv->e_sendqueue); 262 if (Errors != 0) 263 break; 264 265 /* no errors during parsing, but might be a duplicate */ 266 CurEnv->e_to = p; 267 if (!bitset(QBADADDR, a->q_flags)) 268 message("250", "Recipient ok"); 269 else 270 { 271 /* punt -- should keep message in ADDRESS.... */ 272 message("550", "Addressee unknown"); 273 } 274 CurEnv->e_to = NULL; 275 break; 276 277 case CMDDATA: /* data -- text of mail */ 278 SmtpPhase = "DATA"; 279 if (!hasmail) 280 { 281 message("503", "Need MAIL command"); 282 break; 283 } 284 else if (CurEnv->e_nrcpts <= 0) 285 { 286 message("503", "Need RCPT (recipient)"); 287 break; 288 } 289 290 /* collect the text of the message */ 291 SmtpPhase = "collect"; 292 setproctitle("%s %s: %s", CurEnv->e_id, 293 RealHostName, inp); 294 collect(TRUE); 295 if (Errors != 0) 296 break; 297 298 /* 299 ** Arrange to send to everyone. 300 ** If sending to multiple people, mail back 301 ** errors rather than reporting directly. 302 ** In any case, don't mail back errors for 303 ** anything that has happened up to 304 ** now (the other end will do this). 305 ** Truncate our transcript -- the mail has gotten 306 ** to us successfully, and if we have 307 ** to mail this back, it will be easier 308 ** on the reader. 309 ** Then send to everyone. 310 ** Finally give a reply code. If an error has 311 ** already been given, don't mail a 312 ** message back. 313 ** We goose error returns by clearing error bit. 314 */ 315 316 SmtpPhase = "delivery"; 317 if (CurEnv->e_nrcpts != 1) 318 { 319 HoldErrs = TRUE; 320 ErrorMode = EM_MAIL; 321 } 322 CurEnv->e_flags &= ~EF_FATALERRS; 323 CurEnv->e_xfp = freopen(queuename(CurEnv, 'x'), "w", CurEnv->e_xfp); 324 325 /* send to all recipients */ 326 sendall(CurEnv, SM_DEFAULT); 327 CurEnv->e_to = NULL; 328 329 /* save statistics */ 330 markstats(CurEnv, (ADDRESS *) NULL); 331 332 /* issue success if appropriate and reset */ 333 if (Errors == 0 || HoldErrs) 334 message("250", "Ok"); 335 else 336 CurEnv->e_flags &= ~EF_FATALERRS; 337 338 /* if in a child, pop back to our parent */ 339 if (InChild) 340 finis(); 341 342 /* clean up a bit */ 343 hasmail = 0; 344 dropenvelope(CurEnv); 345 CurEnv = newenvelope(CurEnv); 346 CurEnv->e_flags = BlankEnvelope.e_flags; 347 break; 348 349 case CMDRSET: /* rset -- reset state */ 350 message("250", "Reset state"); 351 if (InChild) 352 finis(); 353 break; 354 355 case CMDVRFY: /* vrfy -- verify address */ 356 if (runinchild("SMTP-VRFY") > 0) 357 break; 358 setproctitle("%s: %s", RealHostName, inp); 359 vrfyqueue = NULL; 360 QuickAbort = TRUE; 361 sendtolist(p, (ADDRESS *) NULL, &vrfyqueue); 362 if (Errors != 0) 363 { 364 if (InChild) 365 finis(); 366 break; 367 } 368 while (vrfyqueue != NULL) 369 { 370 register ADDRESS *a = vrfyqueue->q_next; 371 char *code; 372 373 while (a != NULL && bitset(QDONTSEND|QBADADDR, a->q_flags)) 374 a = a->q_next; 375 376 if (!bitset(QDONTSEND|QBADADDR, vrfyqueue->q_flags)) 377 { 378 if (a != NULL) 379 code = "250-"; 380 else 381 code = "250"; 382 if (vrfyqueue->q_fullname == NULL) 383 message(code, "<%s>", vrfyqueue->q_paddr); 384 else 385 message(code, "%s <%s>", 386 vrfyqueue->q_fullname, vrfyqueue->q_paddr); 387 } 388 else if (a == NULL) 389 message("554", "Self destructive alias loop"); 390 vrfyqueue = a; 391 } 392 if (InChild) 393 finis(); 394 break; 395 396 case CMDHELP: /* help -- give user info */ 397 if (*p == '\0') 398 p = "SMTP"; 399 help(p); 400 break; 401 402 case CMDNOOP: /* noop -- do nothing */ 403 message("200", "OK"); 404 break; 405 406 case CMDQUIT: /* quit -- leave mail */ 407 message("221", "%s closing connection", HostName); 408 if (InChild) 409 ExitStat = EX_QUIT; 410 finis(); 411 412 case CMDVERB: /* set verbose mode */ 413 Verbose = TRUE; 414 SendMode = SM_DELIVER; 415 message("200", "Verbose mode"); 416 break; 417 418 case CMDONEX: /* doing one transaction only */ 419 OneXact = TRUE; 420 message("200", "Only one transaction"); 421 break; 422 423 # ifdef DEBUG 424 case CMDDBGQSHOW: /* show queues */ 425 printf("Send Queue="); 426 printaddr(CurEnv->e_sendqueue, TRUE); 427 break; 428 429 case CMDDBGDEBUG: /* set debug mode */ 430 tTsetup(tTdvect, sizeof tTdvect, "0-99.1"); 431 tTflag(p); 432 message("200", "Debug set"); 433 break; 434 # endif DEBUG 435 436 # ifdef WIZ 437 case CMDDBGKILL: /* kill the parent */ 438 if (!iswiz()) 439 break; 440 if (kill(MotherPid, SIGTERM) >= 0) 441 message("200", "Mother is dead"); 442 else 443 message("500", "Can't kill Mom"); 444 break; 445 446 case CMDDBGWIZ: /* become a wizard */ 447 if (WizWord != NULL) 448 { 449 char seed[3]; 450 extern char *crypt(); 451 452 (void) strncpy(seed, WizWord, 2); 453 if (strcmp(WizWord, crypt(p, seed)) == 0) 454 { 455 IsWiz = TRUE; 456 message("200", "Please pass, oh mighty wizard"); 457 break; 458 } 459 } 460 message("500", "You are no wizard!"); 461 break; 462 463 # else WIZ 464 case CMDDBGWIZ: /* try to become a wizard */ 465 message("500", "You wascal wabbit! Wandering wizards won't win!"); 466 break; 467 # endif WIZ 468 469 case CMDERROR: /* unknown command */ 470 message("500", "Command unrecognized"); 471 break; 472 473 default: 474 syserr("smtp: unknown code %d", c->cmdcode); 475 break; 476 } 477 } 478 } 479 /* 480 ** SKIPWORD -- skip a fixed word. 481 ** 482 ** Parameters: 483 ** p -- place to start looking. 484 ** w -- word to skip. 485 ** 486 ** Returns: 487 ** p following w. 488 ** NULL on error. 489 ** 490 ** Side Effects: 491 ** clobbers the p data area. 492 */ 493 494 static char * 495 skipword(p, w) 496 register char *p; 497 char *w; 498 { 499 register char *q; 500 extern bool sameword(); 501 502 /* find beginning of word */ 503 while (isspace(*p)) 504 p++; 505 q = p; 506 507 /* find end of word */ 508 while (*p != '\0' && *p != ':' && !isspace(*p)) 509 p++; 510 while (isspace(*p)) 511 *p++ = '\0'; 512 if (*p != ':') 513 { 514 syntax: 515 message("501", "Syntax error"); 516 Errors++; 517 return (NULL); 518 } 519 *p++ = '\0'; 520 while (isspace(*p)) 521 p++; 522 523 /* see if the input word matches desired word */ 524 if (!sameword(q, w)) 525 goto syntax; 526 527 return (p); 528 } 529 /* 530 ** HELP -- implement the HELP command. 531 ** 532 ** Parameters: 533 ** topic -- the topic we want help for. 534 ** 535 ** Returns: 536 ** none. 537 ** 538 ** Side Effects: 539 ** outputs the help file to message output. 540 */ 541 542 help(topic) 543 char *topic; 544 { 545 register FILE *hf; 546 int len; 547 char buf[MAXLINE]; 548 bool noinfo; 549 550 if (HelpFile == NULL || (hf = fopen(HelpFile, "r")) == NULL) 551 { 552 /* no help */ 553 errno = 0; 554 message("502", "HELP not implemented"); 555 return; 556 } 557 558 len = strlen(topic); 559 makelower(topic); 560 noinfo = TRUE; 561 562 while (fgets(buf, sizeof buf, hf) != NULL) 563 { 564 if (strncmp(buf, topic, len) == 0) 565 { 566 register char *p; 567 568 p = index(buf, '\t'); 569 if (p == NULL) 570 p = buf; 571 else 572 p++; 573 fixcrlf(p, TRUE); 574 message("214-", p); 575 noinfo = FALSE; 576 } 577 } 578 579 if (noinfo) 580 message("504", "HELP topic unknown"); 581 else 582 message("214", "End of HELP info"); 583 (void) fclose(hf); 584 } 585 /* 586 ** ISWIZ -- tell us if we are a wizard 587 ** 588 ** If not, print a nasty message. 589 ** 590 ** Parameters: 591 ** none. 592 ** 593 ** Returns: 594 ** TRUE if we are a wizard. 595 ** FALSE if we are not a wizard. 596 ** 597 ** Side Effects: 598 ** Prints a 500 exit stat if we are not a wizard. 599 */ 600 601 #ifdef WIZ 602 603 bool 604 iswiz() 605 { 606 if (!IsWiz) 607 message("500", "Mere mortals musn't mutter that mantra"); 608 return (IsWiz); 609 } 610 611 #endif WIZ 612 /* 613 ** RUNINCHILD -- return twice -- once in the child, then in the parent again 614 ** 615 ** Parameters: 616 ** label -- a string used in error messages 617 ** 618 ** Returns: 619 ** zero in the child 620 ** one in the parent 621 ** 622 ** Side Effects: 623 ** none. 624 */ 625 626 runinchild(label) 627 char *label; 628 { 629 int childpid; 630 631 if (!OneXact) 632 { 633 childpid = dofork(); 634 if (childpid < 0) 635 { 636 syserr("%s: cannot fork", label); 637 return (1); 638 } 639 if (childpid > 0) 640 { 641 auto int st; 642 643 /* parent -- wait for child to complete */ 644 st = waitfor(childpid); 645 if (st == -1) 646 syserr("%s: lost child", label); 647 648 /* if we exited on a QUIT command, complete the process */ 649 if (st == (EX_QUIT << 8)) 650 finis(); 651 652 return (1); 653 } 654 else 655 { 656 /* child */ 657 InChild = TRUE; 658 clearenvelope(CurEnv); 659 } 660 } 661 662 /* open alias database */ 663 initaliases(AliasFile, FALSE); 664 665 return (0); 666 } 667 668 # endif SMTP 669