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