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