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