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