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