1 /* 2 * Copyright (c) 1983 Eric P. Allman 3 * Copyright (c) 1988, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * %sccs.include.redist.c% 7 */ 8 9 #ifndef lint 10 static char sccsid[] = "@(#)envelope.c 8.24 (Berkeley) 12/10/93"; 11 #endif /* not lint */ 12 13 #include "sendmail.h" 14 #include <sys/time.h> 15 #include <pwd.h> 16 17 /* 18 ** NEWENVELOPE -- allocate a new envelope 19 ** 20 ** Supports inheritance. 21 ** 22 ** Parameters: 23 ** e -- the new envelope to fill in. 24 ** parent -- the envelope to be the parent of e. 25 ** 26 ** Returns: 27 ** e. 28 ** 29 ** Side Effects: 30 ** none. 31 */ 32 33 ENVELOPE * 34 newenvelope(e, parent) 35 register ENVELOPE *e; 36 register ENVELOPE *parent; 37 { 38 extern putheader(), putbody(); 39 extern ENVELOPE BlankEnvelope; 40 41 if (e == parent && e->e_parent != NULL) 42 parent = e->e_parent; 43 clearenvelope(e, TRUE); 44 if (e == CurEnv) 45 bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from); 46 else 47 bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from); 48 e->e_parent = parent; 49 e->e_ctime = curtime(); 50 if (parent != NULL) 51 e->e_msgpriority = parent->e_msgsize; 52 e->e_puthdr = putheader; 53 e->e_putbody = putbody; 54 if (CurEnv->e_xfp != NULL) 55 (void) fflush(CurEnv->e_xfp); 56 57 return (e); 58 } 59 /* 60 ** DROPENVELOPE -- deallocate an envelope. 61 ** 62 ** Parameters: 63 ** e -- the envelope to deallocate. 64 ** 65 ** Returns: 66 ** none. 67 ** 68 ** Side Effects: 69 ** housekeeping necessary to dispose of an envelope. 70 ** Unlocks this queue file. 71 */ 72 73 void 74 dropenvelope(e) 75 register ENVELOPE *e; 76 { 77 bool queueit = FALSE; 78 bool saveit = bitset(EF_FATALERRS, e->e_flags); 79 register ADDRESS *q; 80 char *id = e->e_id; 81 char buf[MAXLINE]; 82 83 if (tTd(50, 1)) 84 { 85 printf("dropenvelope %x: id=", e); 86 xputs(e->e_id); 87 printf(", flags=%o\n", e->e_flags); 88 if (tTd(50, 10)) 89 { 90 printf("sendq="); 91 printaddr(e->e_sendqueue, TRUE); 92 } 93 } 94 95 #ifdef XDEBUG 96 checkfd012("dropenvelope 1"); 97 #endif 98 99 /* we must have an id to remove disk files */ 100 if (id == NULL) 101 return; 102 103 #ifdef LOG 104 if (LogLevel > 84) 105 syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d", 106 id, e->e_flags, getpid()); 107 #endif /* LOG */ 108 109 /* post statistics */ 110 poststats(StatFile); 111 112 /* 113 ** Extract state information from dregs of send list. 114 */ 115 116 e->e_flags &= ~EF_QUEUERUN; 117 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 118 { 119 if (bitset(QQUEUEUP, q->q_flags)) 120 queueit = TRUE; 121 if (!bitset(QDONTSEND, q->q_flags) && 122 bitset(QBADADDR, q->q_flags)) 123 { 124 if (q->q_owner == NULL && 125 strcmp(e->e_from.q_paddr, "<>") != 0) 126 (void) sendtolist(e->e_from.q_paddr, NULL, 127 &e->e_errorqueue, e); 128 } 129 } 130 131 /* 132 ** See if the message timed out. 133 */ 134 135 if (!queueit) 136 /* nothing to do */ ; 137 else if (curtime() > e->e_ctime + TimeOuts.to_q_return) 138 { 139 (void) sprintf(buf, "Cannot send message for %s", 140 pintvl(TimeOuts.to_q_return, FALSE)); 141 if (e->e_message != NULL) 142 free(e->e_message); 143 e->e_message = newstr(buf); 144 message(buf); 145 e->e_flags |= EF_CLRQUEUE; 146 saveit = TRUE; 147 fprintf(e->e_xfp, "Message could not be delivered for %s\n", 148 pintvl(TimeOuts.to_q_return, FALSE)); 149 fprintf(e->e_xfp, "Message will be deleted from queue\n"); 150 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 151 { 152 if (bitset(QQUEUEUP, q->q_flags)) 153 q->q_flags |= QBADADDR; 154 } 155 } 156 else if (TimeOuts.to_q_warning > 0 && 157 curtime() > e->e_ctime + TimeOuts.to_q_warning) 158 { 159 if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) && 160 e->e_class >= 0 && 161 strcmp(e->e_from.q_paddr, "<>") != 0) 162 { 163 (void) sprintf(buf, 164 "warning: cannot send message for %s", 165 pintvl(TimeOuts.to_q_warning, FALSE)); 166 if (e->e_message != NULL) 167 free(e->e_message); 168 e->e_message = newstr(buf); 169 message(buf); 170 e->e_flags |= EF_WARNING; 171 saveit = TRUE; 172 } 173 fprintf(e->e_xfp, 174 "Warning: message still undelivered after %s\n", 175 pintvl(TimeOuts.to_q_warning, FALSE)); 176 fprintf(e->e_xfp, "Will keep trying until message is %s old\n", 177 pintvl(TimeOuts.to_q_return, FALSE)); 178 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 179 { 180 if (bitset(QQUEUEUP, q->q_flags)) 181 q->q_flags |= QREPORT; 182 } 183 } 184 185 /* 186 ** Send back return receipts as requested. 187 */ 188 189 if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 190 { 191 auto ADDRESS *rlist = NULL; 192 193 (void) sendtolist(e->e_receiptto, NULLADDR, &rlist, e); 194 (void) returntosender("Return receipt", rlist, FALSE, e); 195 e->e_flags &= ~EF_SENDRECEIPT; 196 } 197 198 /* 199 ** Arrange to send error messages if there are fatal errors. 200 */ 201 202 if (saveit && e->e_errormode != EM_QUIET) 203 savemail(e); 204 205 #ifdef XDEBUG 206 checkfd012("dropenvelope 2"); 207 #endif 208 209 /* 210 ** Arrange to send warning messages to postmaster as requested. 211 */ 212 213 if (bitset(EF_PM_NOTIFY, e->e_flags) && PostMasterCopy != NULL && 214 !bitset(EF_RESPONSE, e->e_flags) && e->e_class >= 0) 215 { 216 auto ADDRESS *rlist = NULL; 217 218 (void) sendtolist(PostMasterCopy, NULLADDR, &rlist, e); 219 (void) returntosender(e->e_message, rlist, FALSE, e); 220 } 221 222 /* 223 ** Instantiate or deinstantiate the queue. 224 */ 225 226 if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 227 bitset(EF_CLRQUEUE, e->e_flags)) 228 { 229 if (tTd(50, 1)) 230 printf("\n===== Dropping [dq]f%s =====\n\n", e->e_id); 231 if (e->e_df != NULL) 232 xunlink(e->e_df); 233 xunlink(queuename(e, 'q')); 234 235 #ifdef LOG 236 if (LogLevel > 10) 237 syslog(LOG_INFO, "%s: done", id); 238 #endif 239 } 240 else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 241 { 242 #ifdef QUEUE 243 queueup(e, bitset(EF_KEEPQUEUE, e->e_flags), FALSE); 244 #else /* QUEUE */ 245 syserr("554 dropenvelope: queueup"); 246 #endif /* QUEUE */ 247 } 248 249 #ifdef XDEBUG 250 checkfd012("dropenvelope 3"); 251 #endif 252 253 /* now unlock the job */ 254 closexscript(e); 255 unlockqueue(e); 256 257 /* make sure that this envelope is marked unused */ 258 if (e->e_dfp != NULL) 259 (void) xfclose(e->e_dfp, "dropenvelope", e->e_df); 260 e->e_dfp = NULL; 261 e->e_id = e->e_df = NULL; 262 #ifdef XDEBUG 263 checkfd012("dropenvelope 4"); 264 #endif 265 } 266 /* 267 ** CLEARENVELOPE -- clear an envelope without unlocking 268 ** 269 ** This is normally used by a child process to get a clean 270 ** envelope without disturbing the parent. 271 ** 272 ** Parameters: 273 ** e -- the envelope to clear. 274 ** fullclear - if set, the current envelope is total 275 ** garbage and should be ignored; otherwise, 276 ** release any resources it may indicate. 277 ** 278 ** Returns: 279 ** none. 280 ** 281 ** Side Effects: 282 ** Closes files associated with the envelope. 283 ** Marks the envelope as unallocated. 284 */ 285 286 void 287 clearenvelope(e, fullclear) 288 register ENVELOPE *e; 289 bool fullclear; 290 { 291 register HDR *bh; 292 register HDR **nhp; 293 extern ENVELOPE BlankEnvelope; 294 295 if (!fullclear) 296 { 297 /* clear out any file information */ 298 if (e->e_xfp != NULL) 299 (void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id); 300 if (e->e_dfp != NULL) 301 (void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_df); 302 e->e_xfp = e->e_dfp = NULL; 303 } 304 305 /* now clear out the data */ 306 STRUCTCOPY(BlankEnvelope, *e); 307 if (Verbose) 308 e->e_sendmode = SM_DELIVER; 309 bh = BlankEnvelope.e_header; 310 nhp = &e->e_header; 311 while (bh != NULL) 312 { 313 *nhp = (HDR *) xalloc(sizeof *bh); 314 bcopy((char *) bh, (char *) *nhp, sizeof *bh); 315 bh = bh->h_link; 316 nhp = &(*nhp)->h_link; 317 } 318 } 319 /* 320 ** INITSYS -- initialize instantiation of system 321 ** 322 ** In Daemon mode, this is done in the child. 323 ** 324 ** Parameters: 325 ** none. 326 ** 327 ** Returns: 328 ** none. 329 ** 330 ** Side Effects: 331 ** Initializes the system macros, some global variables, 332 ** etc. In particular, the current time in various 333 ** forms is set. 334 */ 335 336 void 337 initsys(e) 338 register ENVELOPE *e; 339 { 340 char cbuf[5]; /* holds hop count */ 341 char pbuf[10]; /* holds pid */ 342 #ifdef TTYNAME 343 static char ybuf[60]; /* holds tty id */ 344 register char *p; 345 #endif /* TTYNAME */ 346 extern char *ttyname(); 347 extern void settime(); 348 extern char Version[]; 349 350 /* 351 ** Give this envelope a reality. 352 ** I.e., an id, a transcript, and a creation time. 353 */ 354 355 openxscript(e); 356 e->e_ctime = curtime(); 357 358 /* 359 ** Set OutChannel to something useful if stdout isn't it. 360 ** This arranges that any extra stuff the mailer produces 361 ** gets sent back to the user on error (because it is 362 ** tucked away in the transcript). 363 */ 364 365 if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) && 366 e->e_xfp != NULL) 367 OutChannel = e->e_xfp; 368 369 /* 370 ** Set up some basic system macros. 371 */ 372 373 /* process id */ 374 (void) sprintf(pbuf, "%d", getpid()); 375 define('p', newstr(pbuf), e); 376 377 /* hop count */ 378 (void) sprintf(cbuf, "%d", e->e_hopcount); 379 define('c', newstr(cbuf), e); 380 381 /* time as integer, unix time, arpa time */ 382 settime(e); 383 384 #ifdef TTYNAME 385 /* tty name */ 386 if (macvalue('y', e) == NULL) 387 { 388 p = ttyname(2); 389 if (p != NULL) 390 { 391 if (strrchr(p, '/') != NULL) 392 p = strrchr(p, '/') + 1; 393 (void) strcpy(ybuf, p); 394 define('y', ybuf, e); 395 } 396 } 397 #endif /* TTYNAME */ 398 } 399 /* 400 ** SETTIME -- set the current time. 401 ** 402 ** Parameters: 403 ** none. 404 ** 405 ** Returns: 406 ** none. 407 ** 408 ** Side Effects: 409 ** Sets the various time macros -- $a, $b, $d, $t. 410 */ 411 412 void 413 settime(e) 414 register ENVELOPE *e; 415 { 416 register char *p; 417 auto time_t now; 418 char tbuf[20]; /* holds "current" time */ 419 char dbuf[30]; /* holds ctime(tbuf) */ 420 register struct tm *tm; 421 extern char *arpadate(); 422 extern struct tm *gmtime(); 423 424 now = curtime(); 425 tm = gmtime(&now); 426 (void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900, 427 tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min); 428 define('t', newstr(tbuf), e); 429 (void) strcpy(dbuf, ctime(&now)); 430 p = strchr(dbuf, '\n'); 431 if (p != NULL) 432 *p = '\0'; 433 define('d', newstr(dbuf), e); 434 p = arpadate(dbuf); 435 p = newstr(p); 436 if (macvalue('a', e) == NULL) 437 define('a', p, e); 438 define('b', p, e); 439 } 440 /* 441 ** OPENXSCRIPT -- Open transcript file 442 ** 443 ** Creates a transcript file for possible eventual mailing or 444 ** sending back. 445 ** 446 ** Parameters: 447 ** e -- the envelope to create the transcript in/for. 448 ** 449 ** Returns: 450 ** none 451 ** 452 ** Side Effects: 453 ** Creates the transcript file. 454 */ 455 456 #ifndef O_APPEND 457 #define O_APPEND 0 458 #endif 459 460 void 461 openxscript(e) 462 register ENVELOPE *e; 463 { 464 register char *p; 465 int fd; 466 467 if (e->e_xfp != NULL) 468 return; 469 p = queuename(e, 'x'); 470 fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644); 471 if (fd < 0) 472 { 473 syserr("Can't create transcript file %s", p); 474 fd = open("/dev/null", O_WRONLY, 0644); 475 if (fd < 0) 476 syserr("!Can't open /dev/null"); 477 } 478 e->e_xfp = fdopen(fd, "w"); 479 if (e->e_xfp == NULL) 480 { 481 syserr("!Can't create transcript stream %s", p); 482 } 483 if (tTd(46, 9)) 484 { 485 printf("openxscript(%s):\n ", p); 486 dumpfd(fileno(e->e_xfp), TRUE, FALSE); 487 } 488 } 489 /* 490 ** CLOSEXSCRIPT -- close the transcript file. 491 ** 492 ** Parameters: 493 ** e -- the envelope containing the transcript to close. 494 ** 495 ** Returns: 496 ** none. 497 ** 498 ** Side Effects: 499 ** none. 500 */ 501 502 void 503 closexscript(e) 504 register ENVELOPE *e; 505 { 506 if (e->e_xfp == NULL) 507 return; 508 (void) xfclose(e->e_xfp, "closexscript", e->e_id); 509 e->e_xfp = NULL; 510 } 511 /* 512 ** SETSENDER -- set the person who this message is from 513 ** 514 ** Under certain circumstances allow the user to say who 515 ** s/he is (using -f or -r). These are: 516 ** 1. The user's uid is zero (root). 517 ** 2. The user's login name is in an approved list (typically 518 ** from a network server). 519 ** 3. The address the user is trying to claim has a 520 ** "!" character in it (since #2 doesn't do it for 521 ** us if we are dialing out for UUCP). 522 ** A better check to replace #3 would be if the 523 ** effective uid is "UUCP" -- this would require me 524 ** to rewrite getpwent to "grab" uucp as it went by, 525 ** make getname more nasty, do another passwd file 526 ** scan, or compile the UID of "UUCP" into the code, 527 ** all of which are reprehensible. 528 ** 529 ** Assuming all of these fail, we figure out something 530 ** ourselves. 531 ** 532 ** Parameters: 533 ** from -- the person we would like to believe this message 534 ** is from, as specified on the command line. 535 ** e -- the envelope in which we would like the sender set. 536 ** delimptr -- if non-NULL, set to the location of the 537 ** trailing delimiter. 538 ** internal -- set if this address is coming from an internal 539 ** source such as an owner alias. 540 ** 541 ** Returns: 542 ** none. 543 ** 544 ** Side Effects: 545 ** sets sendmail's notion of who the from person is. 546 */ 547 548 void 549 setsender(from, e, delimptr, internal) 550 char *from; 551 register ENVELOPE *e; 552 char **delimptr; 553 bool internal; 554 { 555 register char **pvp; 556 char *realname = NULL; 557 register struct passwd *pw; 558 char delimchar; 559 char *bp; 560 char buf[MAXNAME + 2]; 561 char pvpbuf[PSBUFSIZE]; 562 extern struct passwd *getpwnam(); 563 extern char *FullName; 564 565 if (tTd(45, 1)) 566 printf("setsender(%s)\n", from == NULL ? "" : from); 567 568 /* 569 ** Figure out the real user executing us. 570 ** Username can return errno != 0 on non-errors. 571 */ 572 573 if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP) 574 realname = from; 575 if (realname == NULL || realname[0] == '\0') 576 realname = username(); 577 578 if (ConfigLevel < 2) 579 SuprErrs = TRUE; 580 581 delimchar = internal ? '\0' : ' '; 582 e->e_from.q_flags = QBADADDR; 583 if (from == NULL || 584 parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR, 585 delimchar, delimptr, e) == NULL || 586 bitset(QBADADDR, e->e_from.q_flags) || 587 e->e_from.q_mailer == ProgMailer || 588 e->e_from.q_mailer == FileMailer || 589 e->e_from.q_mailer == InclMailer) 590 { 591 /* log garbage addresses for traceback */ 592 # ifdef LOG 593 if (from != NULL && LogLevel > 2) 594 { 595 char *p; 596 char ebuf[MAXNAME * 2 + 2]; 597 598 p = macvalue('_', e); 599 if (p == NULL) 600 { 601 char *host = RealHostName; 602 if (host == NULL) 603 host = MyHostName; 604 (void) sprintf(ebuf, "%s@%s", realname, host); 605 p = ebuf; 606 } 607 syslog(LOG_NOTICE, 608 "setsender: %s: invalid or unparseable, received from %s", 609 shortenstring(from, 83), p); 610 } 611 # endif /* LOG */ 612 if (from != NULL) 613 { 614 if (!bitset(QBADADDR, e->e_from.q_flags)) 615 { 616 /* it was a bogus mailer in the from addr */ 617 usrerr("553 Invalid sender address"); 618 } 619 SuprErrs = TRUE; 620 } 621 if (from == realname || 622 parseaddr(from = newstr(realname), &e->e_from, 623 RF_COPYALL|RF_SENDERADDR, ' ', NULL, e) == NULL) 624 { 625 char nbuf[100]; 626 627 SuprErrs = TRUE; 628 expand("\201n", nbuf, &nbuf[sizeof nbuf], e); 629 if (parseaddr(from = newstr(nbuf), &e->e_from, 630 RF_COPYALL, ' ', NULL, e) == NULL && 631 parseaddr(from = "postmaster", &e->e_from, 632 RF_COPYALL, ' ', NULL, e) == NULL) 633 syserr("553 setsender: can't even parse postmaster!"); 634 } 635 } 636 else 637 FromFlag = TRUE; 638 e->e_from.q_flags |= QDONTSEND; 639 if (tTd(45, 5)) 640 { 641 printf("setsender: QDONTSEND "); 642 printaddr(&e->e_from, FALSE); 643 } 644 SuprErrs = FALSE; 645 646 pvp = NULL; 647 if (e->e_from.q_mailer == LocalMailer) 648 { 649 # ifdef USERDB 650 register char *p; 651 extern char *udbsender(); 652 # endif 653 654 if (!internal) 655 { 656 /* if the user has given fullname already, don't redefine */ 657 if (FullName == NULL) 658 FullName = macvalue('x', e); 659 if (FullName != NULL && FullName[0] == '\0') 660 FullName = NULL; 661 662 # ifdef USERDB 663 p = udbsender(e->e_from.q_user); 664 665 if (p != NULL) 666 { 667 /* 668 ** We have an alternate address for the sender 669 */ 670 671 pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL); 672 } 673 # endif /* USERDB */ 674 } 675 676 if ((pw = getpwnam(e->e_from.q_user)) != NULL) 677 { 678 /* 679 ** Process passwd file entry. 680 */ 681 682 683 /* extract home directory */ 684 e->e_from.q_home = newstr(pw->pw_dir); 685 define('z', e->e_from.q_home, e); 686 687 /* extract user and group id */ 688 e->e_from.q_uid = pw->pw_uid; 689 e->e_from.q_gid = pw->pw_gid; 690 e->e_from.q_flags |= QGOODUID; 691 692 /* extract full name from passwd file */ 693 if (FullName == NULL && pw->pw_gecos != NULL && 694 strcmp(pw->pw_name, e->e_from.q_user) == 0 && 695 !internal) 696 { 697 buildfname(pw->pw_gecos, e->e_from.q_user, buf); 698 if (buf[0] != '\0') 699 FullName = newstr(buf); 700 } 701 } 702 if (FullName != NULL && !internal) 703 define('x', FullName, e); 704 } 705 else if (!internal) 706 { 707 if (e->e_from.q_home == NULL) 708 e->e_from.q_home = getenv("HOME"); 709 e->e_from.q_uid = RealUid; 710 e->e_from.q_gid = RealGid; 711 e->e_from.q_flags |= QGOODUID; 712 } 713 714 /* 715 ** Rewrite the from person to dispose of possible implicit 716 ** links in the net. 717 */ 718 719 if (pvp == NULL) 720 pvp = prescan(from, delimchar, pvpbuf, sizeof pvpbuf, NULL); 721 if (pvp == NULL) 722 { 723 /* don't need to give error -- prescan did that already */ 724 # ifdef LOG 725 if (LogLevel > 2) 726 syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 727 # endif 728 finis(); 729 } 730 (void) rewrite(pvp, 3, 0, e); 731 (void) rewrite(pvp, 1, 0, e); 732 (void) rewrite(pvp, 4, 0, e); 733 bp = buf + 1; 734 cataddr(pvp, NULL, bp, sizeof buf - 2, '\0'); 735 if (*bp == '@') 736 { 737 /* heuristic: route-addr: add angle brackets */ 738 strcat(bp, ">"); 739 *--bp = '<'; 740 } 741 e->e_sender = newstr(bp); 742 define('f', e->e_sender, e); 743 744 /* save the domain spec if this mailer wants it */ 745 if (!internal && e->e_from.q_mailer != NULL && 746 bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 747 { 748 extern char **copyplist(); 749 750 while (*pvp != NULL && strcmp(*pvp, "@") != 0) 751 pvp++; 752 if (*pvp != NULL) 753 e->e_fromdomain = copyplist(pvp, TRUE); 754 } 755 } 756