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