1 /* 2 * Copyright (c) 1998-2003 Sendmail, Inc. and its suppliers. 3 * All rights reserved. 4 * Copyright (c) 1983, 1995-1997 Eric P. Allman. All rights reserved. 5 * Copyright (c) 1988, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * By using this file, you agree to the terms and conditions set 9 * forth in the LICENSE file which can be found at the top level of 10 * the sendmail distribution. 11 * 12 */ 13 14 #pragma ident "%Z%%M% %I% %E% SMI" 15 16 #include <sendmail.h> 17 18 SM_RCSID("@(#)$Id: envelope.c,v 8.294 2005/02/16 23:38:51 ca Exp $") 19 20 /* 21 ** CLRSESSENVELOPE -- clear session oriented data in an envelope 22 ** 23 ** Parameters: 24 ** e -- the envelope to clear. 25 ** 26 ** Returns: 27 ** none. 28 */ 29 30 void 31 clrsessenvelope(e) 32 ENVELOPE *e; 33 { 34 #if SASL 35 macdefine(&e->e_macro, A_PERM, macid("{auth_type}"), ""); 36 macdefine(&e->e_macro, A_PERM, macid("{auth_authen}"), ""); 37 macdefine(&e->e_macro, A_PERM, macid("{auth_author}"), ""); 38 macdefine(&e->e_macro, A_PERM, macid("{auth_ssf}"), ""); 39 #endif /* SASL */ 40 #if STARTTLS 41 macdefine(&e->e_macro, A_PERM, macid("{cert_issuer}"), ""); 42 macdefine(&e->e_macro, A_PERM, macid("{cert_subject}"), ""); 43 macdefine(&e->e_macro, A_PERM, macid("{cipher_bits}"), ""); 44 macdefine(&e->e_macro, A_PERM, macid("{cipher}"), ""); 45 macdefine(&e->e_macro, A_PERM, macid("{tls_version}"), ""); 46 macdefine(&e->e_macro, A_PERM, macid("{verify}"), ""); 47 # if _FFR_TLS_1 48 macdefine(&e->e_macro, A_PERM, macid("{alg_bits}"), ""); 49 macdefine(&e->e_macro, A_PERM, macid("{cn_issuer}"), ""); 50 macdefine(&e->e_macro, A_PERM, macid("{cn_subject}"), ""); 51 # endif /* _FFR_TLS_1 */ 52 #endif /* STARTTLS */ 53 } 54 55 /* 56 ** NEWENVELOPE -- fill in a new envelope 57 ** 58 ** Supports inheritance. 59 ** 60 ** Parameters: 61 ** e -- the new envelope to fill in. 62 ** parent -- the envelope to be the parent of e. 63 ** rpool -- either NULL, or a pointer to a resource pool 64 ** from which envelope memory is allocated, and 65 ** to which envelope resources are attached. 66 ** 67 ** Returns: 68 ** e. 69 ** 70 ** Side Effects: 71 ** none. 72 */ 73 74 ENVELOPE * 75 newenvelope(e, parent, rpool) 76 register ENVELOPE *e; 77 register ENVELOPE *parent; 78 SM_RPOOL_T *rpool; 79 { 80 /* 81 ** This code used to read: 82 ** if (e == parent && e->e_parent != NULL) 83 ** parent = e->e_parent; 84 ** So if e == parent && e->e_parent == NULL then we would 85 ** set e->e_parent = e, which creates a loop in the e_parent chain. 86 ** This meant macvalue() could go into an infinite loop. 87 */ 88 89 if (e == parent) 90 parent = e->e_parent; 91 clearenvelope(e, true, rpool); 92 if (e == CurEnv) 93 memmove((char *) &e->e_from, 94 (char *) &NullAddress, 95 sizeof e->e_from); 96 else 97 memmove((char *) &e->e_from, 98 (char *) &CurEnv->e_from, 99 sizeof e->e_from); 100 e->e_parent = parent; 101 assign_queueid(e); 102 e->e_ctime = curtime(); 103 if (parent != NULL) 104 { 105 e->e_msgpriority = parent->e_msgsize; 106 if (parent->e_quarmsg == NULL) 107 { 108 e->e_quarmsg = NULL; 109 macdefine(&e->e_macro, A_PERM, 110 macid("{quarantine}"), ""); 111 } 112 else 113 { 114 e->e_quarmsg = sm_rpool_strdup_x(rpool, 115 parent->e_quarmsg); 116 macdefine(&e->e_macro, A_PERM, 117 macid("{quarantine}"), e->e_quarmsg); 118 } 119 } 120 e->e_puthdr = putheader; 121 e->e_putbody = putbody; 122 if (CurEnv->e_xfp != NULL) 123 (void) sm_io_flush(CurEnv->e_xfp, SM_TIME_DEFAULT); 124 125 return e; 126 } 127 128 /* values for msg_timeout, see also IS_* below for usage (bit layout) */ 129 #define MSG_T_O 0x01 /* normal timeout */ 130 #define MSG_T_O_NOW 0x02 /* NOW timeout */ 131 #define MSG_NOT_BY 0x04 /* Deliver-By time exceeded, mode R */ 132 #define MSG_WARN 0x10 /* normal queue warning */ 133 #define MSG_WARN_BY 0x20 /* Deliver-By time exceeded, mode N */ 134 135 #define IS_MSG_ERR(x) (((x) & 0x0f) != 0) /* return an error */ 136 137 /* immediate return */ 138 #define IS_IMM_RET(x) (((x) & (MSG_T_O_NOW|MSG_NOT_BY)) != 0) 139 #define IS_MSG_WARN(x) (((x) & 0xf0) != 0) /* return a warning */ 140 141 /* 142 ** DROPENVELOPE -- deallocate an envelope. 143 ** 144 ** Parameters: 145 ** e -- the envelope to deallocate. 146 ** fulldrop -- if set, do return receipts. 147 ** split -- if true, split by recipient if message is queued up 148 ** 149 ** Returns: 150 ** none. 151 ** 152 ** Side Effects: 153 ** housekeeping necessary to dispose of an envelope. 154 ** Unlocks this queue file. 155 */ 156 157 void 158 dropenvelope(e, fulldrop, split) 159 register ENVELOPE *e; 160 bool fulldrop; 161 bool split; 162 { 163 bool panic = false; 164 bool queueit = false; 165 int msg_timeout = 0; 166 bool failure_return = false; 167 bool delay_return = false; 168 bool success_return = false; 169 bool pmnotify = bitset(EF_PM_NOTIFY, e->e_flags); 170 bool done = false; 171 register ADDRESS *q; 172 char *id = e->e_id; 173 time_t now; 174 char buf[MAXLINE]; 175 176 if (tTd(50, 1)) 177 { 178 sm_dprintf("dropenvelope %p: id=", e); 179 xputs(sm_debug_file(), e->e_id); 180 sm_dprintf(", flags="); 181 printenvflags(e); 182 if (tTd(50, 10)) 183 { 184 sm_dprintf("sendq="); 185 printaddr(sm_debug_file(), e->e_sendqueue, true); 186 } 187 } 188 189 if (LogLevel > 84) 190 sm_syslog(LOG_DEBUG, id, 191 "dropenvelope, e_flags=0x%lx, OpMode=%c, pid=%d", 192 e->e_flags, OpMode, (int) CurrentPid); 193 194 /* we must have an id to remove disk files */ 195 if (id == NULL) 196 return; 197 198 /* if verify-only mode, we can skip most of this */ 199 if (OpMode == MD_VERIFY) 200 goto simpledrop; 201 202 if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags)) 203 logsender(e, NULL); 204 e->e_flags &= ~EF_LOGSENDER; 205 206 /* post statistics */ 207 poststats(StatFile); 208 209 /* 210 ** Extract state information from dregs of send list. 211 */ 212 213 now = curtime(); 214 if (now >= e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass]) 215 msg_timeout = MSG_T_O; 216 if (IS_DLVR_RETURN(e) && e->e_deliver_by > 0 && 217 now >= e->e_ctime + e->e_deliver_by && 218 !bitset(EF_RESPONSE, e->e_flags)) 219 { 220 msg_timeout = MSG_NOT_BY; 221 e->e_flags |= EF_FATALERRS|EF_CLRQUEUE; 222 } 223 else if (TimeOuts.to_q_return[e->e_timeoutclass] == NOW && 224 !bitset(EF_RESPONSE, e->e_flags)) 225 { 226 msg_timeout = MSG_T_O_NOW; 227 e->e_flags |= EF_FATALERRS|EF_CLRQUEUE; 228 } 229 230 e->e_flags &= ~EF_QUEUERUN; 231 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 232 { 233 if (QS_IS_UNDELIVERED(q->q_state)) 234 queueit = true; 235 236 /* see if a notification is needed */ 237 if (bitset(QPINGONFAILURE, q->q_flags) && 238 ((IS_MSG_ERR(msg_timeout) && 239 QS_IS_UNDELIVERED(q->q_state)) || 240 QS_IS_BADADDR(q->q_state) || 241 IS_IMM_RET(msg_timeout))) 242 { 243 failure_return = true; 244 if (!done && q->q_owner == NULL && 245 !emptyaddr(&e->e_from)) 246 { 247 (void) sendtolist(e->e_from.q_paddr, NULLADDR, 248 &e->e_errorqueue, 0, e); 249 done = true; 250 } 251 } 252 else if ((bitset(QPINGONSUCCESS, q->q_flags) && 253 ((QS_IS_SENT(q->q_state) && 254 bitnset(M_LOCALMAILER, q->q_mailer->m_flags)) || 255 bitset(QRELAYED|QEXPANDED|QDELIVERED, q->q_flags))) || 256 bitset(QBYTRACE, q->q_flags) || 257 bitset(QBYNRELAY, q->q_flags)) 258 { 259 success_return = true; 260 } 261 } 262 263 if (e->e_class < 0) 264 e->e_flags |= EF_NO_BODY_RETN; 265 266 /* 267 ** See if the message timed out. 268 */ 269 270 if (!queueit) 271 /* EMPTY */ 272 /* nothing to do */ ; 273 else if (IS_MSG_ERR(msg_timeout)) 274 { 275 if (failure_return) 276 { 277 if (msg_timeout == MSG_NOT_BY) 278 { 279 (void) sm_snprintf(buf, sizeof buf, 280 "delivery time expired %lds", 281 e->e_deliver_by); 282 } 283 else 284 { 285 (void) sm_snprintf(buf, sizeof buf, 286 "Cannot send message for %s", 287 pintvl(TimeOuts.to_q_return[e->e_timeoutclass], 288 false)); 289 } 290 291 /* don't free, allocated from e_rpool */ 292 e->e_message = sm_rpool_strdup_x(e->e_rpool, buf); 293 message(buf); 294 e->e_flags |= EF_CLRQUEUE; 295 } 296 if (msg_timeout == MSG_NOT_BY) 297 { 298 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT, 299 "Delivery time (%lds) expired\n", 300 e->e_deliver_by); 301 } 302 else 303 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT, 304 "Message could not be delivered for %s\n", 305 pintvl(TimeOuts.to_q_return[e->e_timeoutclass], 306 false)); 307 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT, 308 "Message will be deleted from queue\n"); 309 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 310 { 311 if (QS_IS_UNDELIVERED(q->q_state)) 312 { 313 q->q_state = QS_BADADDR; 314 if (msg_timeout == MSG_NOT_BY) 315 q->q_status = "5.4.7"; 316 else 317 q->q_status = "4.4.7"; 318 } 319 } 320 } 321 else 322 { 323 if (TimeOuts.to_q_warning[e->e_timeoutclass] > 0 && 324 now >= e->e_ctime + 325 TimeOuts.to_q_warning[e->e_timeoutclass]) 326 msg_timeout = MSG_WARN; 327 else if (IS_DLVR_NOTIFY(e) && 328 e->e_deliver_by > 0 && 329 now >= e->e_ctime + e->e_deliver_by) 330 msg_timeout = MSG_WARN_BY; 331 332 if (IS_MSG_WARN(msg_timeout)) 333 { 334 if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) && 335 e->e_class >= 0 && 336 e->e_from.q_paddr != NULL && 337 strcmp(e->e_from.q_paddr, "<>") != 0 && 338 sm_strncasecmp(e->e_from.q_paddr, "owner-", 6) != 0 && 339 (strlen(e->e_from.q_paddr) <= 8 || 340 sm_strcasecmp(&e->e_from.q_paddr[strlen(e->e_from.q_paddr) - 8], 341 "-request") != 0)) 342 { 343 for (q = e->e_sendqueue; q != NULL; 344 q = q->q_next) 345 { 346 if (QS_IS_UNDELIVERED(q->q_state) 347 #if _FFR_NODELAYDSN_ON_HOLD 348 && !bitnset(M_HOLD, 349 q->q_mailer->m_flags) 350 #endif /* _FFR_NODELAYDSN_ON_HOLD */ 351 ) 352 { 353 if (msg_timeout == 354 MSG_WARN_BY && 355 (bitset(QPINGONDELAY, 356 q->q_flags) || 357 !bitset(QHASNOTIFY, 358 q->q_flags)) 359 ) 360 { 361 q->q_flags |= QBYNDELAY; 362 delay_return = true; 363 } 364 if (bitset(QPINGONDELAY, 365 q->q_flags)) 366 { 367 q->q_flags |= QDELAYED; 368 delay_return = true; 369 } 370 } 371 } 372 } 373 if (delay_return) 374 { 375 if (msg_timeout == MSG_WARN_BY) 376 { 377 (void) sm_snprintf(buf, sizeof buf, 378 "Warning: Delivery time (%lds) exceeded", 379 e->e_deliver_by); 380 } 381 else 382 (void) sm_snprintf(buf, sizeof buf, 383 "Warning: could not send message for past %s", 384 pintvl(TimeOuts.to_q_warning[e->e_timeoutclass], 385 false)); 386 387 /* don't free, allocated from e_rpool */ 388 e->e_message = sm_rpool_strdup_x(e->e_rpool, 389 buf); 390 message(buf); 391 e->e_flags |= EF_WARNING; 392 } 393 if (msg_timeout == MSG_WARN_BY) 394 { 395 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT, 396 "Warning: Delivery time (%lds) exceeded\n", 397 e->e_deliver_by); 398 } 399 else 400 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT, 401 "Warning: message still undelivered after %s\n", 402 pintvl(TimeOuts.to_q_warning[e->e_timeoutclass], 403 false)); 404 (void) sm_io_fprintf(e->e_xfp, SM_TIME_DEFAULT, 405 "Will keep trying until message is %s old\n", 406 pintvl(TimeOuts.to_q_return[e->e_timeoutclass], 407 false)); 408 } 409 } 410 411 if (tTd(50, 2)) 412 sm_dprintf("failure_return=%d delay_return=%d success_return=%d queueit=%d\n", 413 failure_return, delay_return, success_return, queueit); 414 415 /* 416 ** If we had some fatal error, but no addresses are marked as 417 ** bad, mark them _all_ as bad. 418 */ 419 420 if (bitset(EF_FATALERRS, e->e_flags) && !failure_return) 421 { 422 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 423 { 424 if ((QS_IS_OK(q->q_state) || 425 QS_IS_VERIFIED(q->q_state)) && 426 bitset(QPINGONFAILURE, q->q_flags)) 427 { 428 failure_return = true; 429 q->q_state = QS_BADADDR; 430 } 431 } 432 } 433 434 /* 435 ** Send back return receipts as requested. 436 */ 437 438 if (success_return && !failure_return && !delay_return && fulldrop && 439 !bitset(PRIV_NORECEIPTS, PrivacyFlags) && 440 strcmp(e->e_from.q_paddr, "<>") != 0) 441 { 442 auto ADDRESS *rlist = NULL; 443 444 if (tTd(50, 8)) 445 sm_dprintf("dropenvelope(%s): sending return receipt\n", 446 id); 447 e->e_flags |= EF_SENDRECEIPT; 448 (void) sendtolist(e->e_from.q_paddr, NULLADDR, &rlist, 0, e); 449 (void) returntosender("Return receipt", rlist, RTSF_NO_BODY, e); 450 } 451 e->e_flags &= ~EF_SENDRECEIPT; 452 453 /* 454 ** Arrange to send error messages if there are fatal errors. 455 */ 456 457 if ((failure_return || delay_return) && e->e_errormode != EM_QUIET) 458 { 459 if (tTd(50, 8)) 460 sm_dprintf("dropenvelope(%s): saving mail\n", id); 461 panic = savemail(e, !bitset(EF_NO_BODY_RETN, e->e_flags)); 462 } 463 464 /* 465 ** Arrange to send warning messages to postmaster as requested. 466 */ 467 468 if ((failure_return || pmnotify) && 469 PostMasterCopy != NULL && 470 !bitset(EF_RESPONSE, e->e_flags) && 471 e->e_class >= 0) 472 { 473 auto ADDRESS *rlist = NULL; 474 char pcopy[MAXNAME]; 475 476 if (failure_return) 477 { 478 expand(PostMasterCopy, pcopy, sizeof pcopy, e); 479 480 if (tTd(50, 8)) 481 sm_dprintf("dropenvelope(%s): sending postmaster copy to %s\n", 482 id, pcopy); 483 (void) sendtolist(pcopy, NULLADDR, &rlist, 0, e); 484 } 485 if (pmnotify) 486 (void) sendtolist("postmaster", NULLADDR, 487 &rlist, 0, e); 488 (void) returntosender(e->e_message, rlist, 489 RTSF_PM_BOUNCE|RTSF_NO_BODY, e); 490 } 491 492 /* 493 ** Instantiate or deinstantiate the queue. 494 */ 495 496 simpledrop: 497 if (tTd(50, 8)) 498 sm_dprintf("dropenvelope(%s): at simpledrop, queueit=%d\n", 499 id, queueit); 500 if (!queueit || bitset(EF_CLRQUEUE, e->e_flags)) 501 { 502 if (tTd(50, 1)) 503 { 504 sm_dprintf("\n===== Dropping queue files for %s... queueit=%d, e_flags=", 505 e->e_id, queueit); 506 printenvflags(e); 507 } 508 if (!panic) 509 (void) xunlink(queuename(e, DATAFL_LETTER)); 510 if (panic && QueueMode == QM_LOST) 511 { 512 /* 513 ** leave the Qf file behind as 514 ** the delivery attempt failed. 515 */ 516 517 /* EMPTY */ 518 } 519 else 520 if (xunlink(queuename(e, ANYQFL_LETTER)) == 0) 521 { 522 /* add to available space in filesystem */ 523 updfs(e, -1, panic ? 0 : -1, "dropenvelope"); 524 } 525 526 if (e->e_ntries > 0 && LogLevel > 9) 527 sm_syslog(LOG_INFO, id, "done; delay=%s, ntries=%d", 528 pintvl(curtime() - e->e_ctime, true), 529 e->e_ntries); 530 } 531 else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 532 { 533 if (!split) 534 queueup(e, false, true); 535 else 536 { 537 ENVELOPE *oldsib; 538 ENVELOPE *ee; 539 540 /* 541 ** Save old sibling and set it to NULL to avoid 542 ** queueing up the same envelopes again. 543 ** This requires that envelopes in that list have 544 ** been take care of before (or at some other place). 545 */ 546 547 oldsib = e->e_sibling; 548 e->e_sibling = NULL; 549 if (!split_by_recipient(e) && 550 bitset(EF_FATALERRS, e->e_flags)) 551 { 552 syserr("!dropenvelope(%s): cannot commit data file %s, uid=%d", 553 e->e_id, queuename(e, DATAFL_LETTER), 554 (int) geteuid()); 555 } 556 for (ee = e->e_sibling; ee != NULL; ee = ee->e_sibling) 557 queueup(ee, false, true); 558 queueup(e, false, true); 559 560 /* clean up */ 561 for (ee = e->e_sibling; ee != NULL; ee = ee->e_sibling) 562 { 563 /* now unlock the job */ 564 if (tTd(50, 8)) 565 sm_dprintf("dropenvelope(%s): unlocking job\n", 566 ee->e_id); 567 closexscript(ee); 568 unlockqueue(ee); 569 570 /* this envelope is marked unused */ 571 if (ee->e_dfp != NULL) 572 { 573 (void) sm_io_close(ee->e_dfp, 574 SM_TIME_DEFAULT); 575 ee->e_dfp = NULL; 576 } 577 ee->e_id = NULL; 578 ee->e_flags &= ~EF_HAS_DF; 579 } 580 e->e_sibling = oldsib; 581 } 582 } 583 584 /* now unlock the job */ 585 if (tTd(50, 8)) 586 sm_dprintf("dropenvelope(%s): unlocking job\n", id); 587 closexscript(e); 588 unlockqueue(e); 589 590 /* make sure that this envelope is marked unused */ 591 if (e->e_dfp != NULL) 592 { 593 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT); 594 e->e_dfp = NULL; 595 } 596 e->e_id = NULL; 597 e->e_flags &= ~EF_HAS_DF; 598 } 599 /* 600 ** CLEARENVELOPE -- clear an envelope without unlocking 601 ** 602 ** This is normally used by a child process to get a clean 603 ** envelope without disturbing the parent. 604 ** 605 ** Parameters: 606 ** e -- the envelope to clear. 607 ** fullclear - if set, the current envelope is total 608 ** garbage and should be ignored; otherwise, 609 ** release any resources it may indicate. 610 ** rpool -- either NULL, or a pointer to a resource pool 611 ** from which envelope memory is allocated, and 612 ** to which envelope resources are attached. 613 ** 614 ** Returns: 615 ** none. 616 ** 617 ** Side Effects: 618 ** Closes files associated with the envelope. 619 ** Marks the envelope as unallocated. 620 */ 621 622 void 623 clearenvelope(e, fullclear, rpool) 624 register ENVELOPE *e; 625 bool fullclear; 626 SM_RPOOL_T *rpool; 627 { 628 register HDR *bh; 629 register HDR **nhp; 630 extern ENVELOPE BlankEnvelope; 631 char **p; 632 633 if (!fullclear) 634 { 635 /* clear out any file information */ 636 if (e->e_xfp != NULL) 637 (void) sm_io_close(e->e_xfp, SM_TIME_DEFAULT); 638 if (e->e_dfp != NULL) 639 (void) sm_io_close(e->e_dfp, SM_TIME_DEFAULT); 640 e->e_xfp = e->e_dfp = NULL; 641 } 642 643 /* 644 ** Copy BlankEnvelope into *e. 645 ** It is not safe to simply copy pointers to strings; 646 ** the strings themselves must be copied (or set to NULL). 647 ** The problem is that when we assign a new string value to 648 ** a member of BlankEnvelope, we free the old string. 649 ** We did not need to do this copying in sendmail 8.11 :-( 650 ** and it is a potential performance hit. Reference counted 651 ** strings are one way out. 652 */ 653 654 *e = BlankEnvelope; 655 e->e_message = NULL; 656 e->e_qfletter = '\0'; 657 e->e_quarmsg = NULL; 658 macdefine(&e->e_macro, A_PERM, macid("{quarantine}"), ""); 659 660 /* 661 ** Copy the macro table. 662 ** We might be able to avoid this by zeroing the macro table 663 ** and always searching BlankEnvelope.e_macro after e->e_macro 664 ** in macvalue(). 665 */ 666 667 for (p = &e->e_macro.mac_table[0]; 668 p <= &e->e_macro.mac_table[MAXMACROID]; 669 ++p) 670 { 671 if (*p != NULL) 672 *p = sm_rpool_strdup_x(rpool, *p); 673 } 674 675 /* 676 ** XXX There are many strings in the envelope structure 677 ** XXX that we are not attempting to copy here. 678 ** XXX Investigate this further. 679 */ 680 681 e->e_rpool = rpool; 682 e->e_macro.mac_rpool = rpool; 683 if (Verbose) 684 set_delivery_mode(SM_DELIVER, e); 685 bh = BlankEnvelope.e_header; 686 nhp = &e->e_header; 687 while (bh != NULL) 688 { 689 *nhp = (HDR *) sm_rpool_malloc_x(rpool, sizeof *bh); 690 memmove((char *) *nhp, (char *) bh, sizeof *bh); 691 bh = bh->h_link; 692 nhp = &(*nhp)->h_link; 693 } 694 } 695 /* 696 ** INITSYS -- initialize instantiation of system 697 ** 698 ** In Daemon mode, this is done in the child. 699 ** 700 ** Parameters: 701 ** e -- the envelope to use. 702 ** 703 ** Returns: 704 ** none. 705 ** 706 ** Side Effects: 707 ** Initializes the system macros, some global variables, 708 ** etc. In particular, the current time in various 709 ** forms is set. 710 */ 711 712 void 713 initsys(e) 714 register ENVELOPE *e; 715 { 716 char buf[10]; 717 #ifdef TTYNAME 718 static char ybuf[60]; /* holds tty id */ 719 register char *p; 720 extern char *ttyname(); 721 #endif /* TTYNAME */ 722 723 /* 724 ** Give this envelope a reality. 725 ** I.e., an id, a transcript, and a creation time. 726 ** We don't select the queue until all of the recipients are known. 727 */ 728 729 openxscript(e); 730 e->e_ctime = curtime(); 731 e->e_qfletter = '\0'; 732 733 /* 734 ** Set OutChannel to something useful if stdout isn't it. 735 ** This arranges that any extra stuff the mailer produces 736 ** gets sent back to the user on error (because it is 737 ** tucked away in the transcript). 738 */ 739 740 if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) && 741 e->e_xfp != NULL) 742 OutChannel = e->e_xfp; 743 744 /* 745 ** Set up some basic system macros. 746 */ 747 748 /* process id */ 749 (void) sm_snprintf(buf, sizeof buf, "%d", (int) CurrentPid); 750 macdefine(&e->e_macro, A_TEMP, 'p', buf); 751 752 /* hop count */ 753 (void) sm_snprintf(buf, sizeof buf, "%d", e->e_hopcount); 754 macdefine(&e->e_macro, A_TEMP, 'c', buf); 755 756 /* time as integer, unix time, arpa time */ 757 settime(e); 758 759 /* Load average */ 760 sm_getla(); 761 762 #ifdef TTYNAME 763 /* tty name */ 764 if (macvalue('y', e) == NULL) 765 { 766 p = ttyname(2); 767 if (p != NULL) 768 { 769 if (strrchr(p, '/') != NULL) 770 p = strrchr(p, '/') + 1; 771 (void) sm_strlcpy(ybuf, sizeof ybuf, p); 772 macdefine(&e->e_macro, A_PERM, 'y', ybuf); 773 } 774 } 775 #endif /* TTYNAME */ 776 } 777 /* 778 ** SETTIME -- set the current time. 779 ** 780 ** Parameters: 781 ** e -- the envelope in which the macros should be set. 782 ** 783 ** Returns: 784 ** none. 785 ** 786 ** Side Effects: 787 ** Sets the various time macros -- $a, $b, $d, $t. 788 */ 789 790 void 791 settime(e) 792 register ENVELOPE *e; 793 { 794 register char *p; 795 auto time_t now; 796 char buf[30]; 797 register struct tm *tm; 798 799 now = curtime(); 800 (void) sm_snprintf(buf, sizeof buf, "%ld", (long) now); 801 macdefine(&e->e_macro, A_TEMP, macid("{time}"), buf); 802 tm = gmtime(&now); 803 (void) sm_snprintf(buf, sizeof buf, "%04d%02d%02d%02d%02d", 804 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, 805 tm->tm_hour, tm->tm_min); 806 macdefine(&e->e_macro, A_TEMP, 't', buf); 807 (void) sm_strlcpy(buf, ctime(&now), sizeof buf); 808 p = strchr(buf, '\n'); 809 if (p != NULL) 810 *p = '\0'; 811 macdefine(&e->e_macro, A_TEMP, 'd', buf); 812 macdefine(&e->e_macro, A_TEMP, 'b', arpadate(buf)); 813 if (macvalue('a', e) == NULL) 814 macdefine(&e->e_macro, A_PERM, 'a', macvalue('b', e)); 815 } 816 /* 817 ** OPENXSCRIPT -- Open transcript file 818 ** 819 ** Creates a transcript file for possible eventual mailing or 820 ** sending back. 821 ** 822 ** Parameters: 823 ** e -- the envelope to create the transcript in/for. 824 ** 825 ** Returns: 826 ** none 827 ** 828 ** Side Effects: 829 ** Creates the transcript file. 830 */ 831 832 #ifndef O_APPEND 833 # define O_APPEND 0 834 #endif /* ! O_APPEND */ 835 836 void 837 openxscript(e) 838 register ENVELOPE *e; 839 { 840 register char *p; 841 842 if (e->e_xfp != NULL) 843 return; 844 845 #if 0 846 if (e->e_lockfp == NULL && bitset(EF_INQUEUE, e->e_flags)) 847 syserr("openxscript: job not locked"); 848 #endif /* 0 */ 849 850 p = queuename(e, XSCRPT_LETTER); 851 e->e_xfp = bfopen(p, FileMode, XscriptFileBufferSize, 852 SFF_NOTEXCL|SFF_OPENASROOT); 853 854 if (e->e_xfp == NULL) 855 { 856 syserr("Can't create transcript file %s", p); 857 e->e_xfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, 858 SM_PATH_DEVNULL, SM_IO_RDWR, NULL); 859 if (e->e_xfp == NULL) 860 syserr("!Can't open %s", SM_PATH_DEVNULL); 861 } 862 (void) sm_io_setvbuf(e->e_xfp, SM_TIME_DEFAULT, NULL, SM_IO_LBF, 0); 863 if (tTd(46, 9)) 864 { 865 sm_dprintf("openxscript(%s):\n ", p); 866 dumpfd(sm_io_getinfo(e->e_xfp, SM_IO_WHAT_FD, NULL), true, 867 false); 868 } 869 } 870 /* 871 ** CLOSEXSCRIPT -- close the transcript file. 872 ** 873 ** Parameters: 874 ** e -- the envelope containing the transcript to close. 875 ** 876 ** Returns: 877 ** none. 878 ** 879 ** Side Effects: 880 ** none. 881 */ 882 883 void 884 closexscript(e) 885 register ENVELOPE *e; 886 { 887 if (e->e_xfp == NULL) 888 return; 889 #if 0 890 if (e->e_lockfp == NULL) 891 syserr("closexscript: job not locked"); 892 #endif /* 0 */ 893 (void) sm_io_close(e->e_xfp, SM_TIME_DEFAULT); 894 e->e_xfp = NULL; 895 } 896 /* 897 ** SETSENDER -- set the person who this message is from 898 ** 899 ** Under certain circumstances allow the user to say who 900 ** s/he is (using -f or -r). These are: 901 ** 1. The user's uid is zero (root). 902 ** 2. The user's login name is in an approved list (typically 903 ** from a network server). 904 ** 3. The address the user is trying to claim has a 905 ** "!" character in it (since #2 doesn't do it for 906 ** us if we are dialing out for UUCP). 907 ** A better check to replace #3 would be if the 908 ** effective uid is "UUCP" -- this would require me 909 ** to rewrite getpwent to "grab" uucp as it went by, 910 ** make getname more nasty, do another passwd file 911 ** scan, or compile the UID of "UUCP" into the code, 912 ** all of which are reprehensible. 913 ** 914 ** Assuming all of these fail, we figure out something 915 ** ourselves. 916 ** 917 ** Parameters: 918 ** from -- the person we would like to believe this message 919 ** is from, as specified on the command line. 920 ** e -- the envelope in which we would like the sender set. 921 ** delimptr -- if non-NULL, set to the location of the 922 ** trailing delimiter. 923 ** delimchar -- the character that will delimit the sender 924 ** address. 925 ** internal -- set if this address is coming from an internal 926 ** source such as an owner alias. 927 ** 928 ** Returns: 929 ** none. 930 ** 931 ** Side Effects: 932 ** sets sendmail's notion of who the from person is. 933 */ 934 935 void 936 setsender(from, e, delimptr, delimchar, internal) 937 char *from; 938 register ENVELOPE *e; 939 char **delimptr; 940 int delimchar; 941 bool internal; 942 { 943 register char **pvp; 944 char *realname = NULL; 945 char *bp; 946 char buf[MAXNAME + 2]; 947 char pvpbuf[PSBUFSIZE]; 948 extern char *FullName; 949 950 if (tTd(45, 1)) 951 sm_dprintf("setsender(%s)\n", from == NULL ? "" : from); 952 953 /* may be set from earlier calls */ 954 macdefine(&e->e_macro, A_PERM, 'x', ""); 955 956 /* 957 ** Figure out the real user executing us. 958 ** Username can return errno != 0 on non-errors. 959 */ 960 961 if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP || 962 OpMode == MD_ARPAFTP || OpMode == MD_DAEMON) 963 realname = from; 964 if (realname == NULL || realname[0] == '\0') 965 realname = username(); 966 967 if (ConfigLevel < 2) 968 SuprErrs = true; 969 970 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e s"); 971 972 /* preset state for then clause in case from == NULL */ 973 e->e_from.q_state = QS_BADADDR; 974 e->e_from.q_flags = 0; 975 if (from == NULL || 976 parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR, 977 delimchar, delimptr, e, false) == NULL || 978 QS_IS_BADADDR(e->e_from.q_state) || 979 e->e_from.q_mailer == ProgMailer || 980 e->e_from.q_mailer == FileMailer || 981 e->e_from.q_mailer == InclMailer) 982 { 983 /* log garbage addresses for traceback */ 984 if (from != NULL && LogLevel > 2) 985 { 986 char *p; 987 char ebuf[MAXNAME * 2 + 2]; 988 989 p = macvalue('_', e); 990 if (p == NULL) 991 { 992 char *host = RealHostName; 993 994 if (host == NULL) 995 host = MyHostName; 996 (void) sm_snprintf(ebuf, sizeof ebuf, 997 "%.*s@%.*s", MAXNAME, 998 realname, MAXNAME, host); 999 p = ebuf; 1000 } 1001 sm_syslog(LOG_NOTICE, e->e_id, 1002 "setsender: %s: invalid or unparsable, received from %s", 1003 shortenstring(from, 83), p); 1004 } 1005 if (from != NULL) 1006 { 1007 if (!QS_IS_BADADDR(e->e_from.q_state)) 1008 { 1009 /* it was a bogus mailer in the from addr */ 1010 e->e_status = "5.1.7"; 1011 usrerrenh(e->e_status, 1012 "553 Invalid sender address"); 1013 } 1014 SuprErrs = true; 1015 } 1016 if (from == realname || 1017 parseaddr(from = realname, 1018 &e->e_from, RF_COPYALL|RF_SENDERADDR, ' ', 1019 NULL, e, false) == NULL) 1020 { 1021 char nbuf[100]; 1022 1023 SuprErrs = true; 1024 expand("\201n", nbuf, sizeof nbuf, e); 1025 from = sm_rpool_strdup_x(e->e_rpool, nbuf); 1026 if (parseaddr(from, &e->e_from, RF_COPYALL, ' ', 1027 NULL, e, false) == NULL && 1028 parseaddr(from = "postmaster", &e->e_from, 1029 RF_COPYALL, ' ', NULL, e, false) == NULL) 1030 syserr("553 5.3.0 setsender: can't even parse postmaster!"); 1031 } 1032 } 1033 else 1034 FromFlag = true; 1035 e->e_from.q_state = QS_SENDER; 1036 if (tTd(45, 5)) 1037 { 1038 sm_dprintf("setsender: QS_SENDER "); 1039 printaddr(sm_debug_file(), &e->e_from, false); 1040 } 1041 SuprErrs = false; 1042 1043 #if USERDB 1044 if (bitnset(M_CHECKUDB, e->e_from.q_mailer->m_flags)) 1045 { 1046 register char *p; 1047 1048 p = udbsender(e->e_from.q_user, e->e_rpool); 1049 if (p != NULL) 1050 from = p; 1051 } 1052 #endif /* USERDB */ 1053 1054 if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags)) 1055 { 1056 SM_MBDB_T user; 1057 1058 if (!internal) 1059 { 1060 /* if the user already given fullname don't redefine */ 1061 if (FullName == NULL) 1062 FullName = macvalue('x', e); 1063 if (FullName != NULL) 1064 { 1065 if (FullName[0] == '\0') 1066 FullName = NULL; 1067 else 1068 FullName = newstr(FullName); 1069 } 1070 } 1071 1072 if (e->e_from.q_user[0] != '\0' && 1073 sm_mbdb_lookup(e->e_from.q_user, &user) == EX_OK) 1074 { 1075 /* 1076 ** Process passwd file entry. 1077 */ 1078 1079 /* extract home directory */ 1080 if (*user.mbdb_homedir == '\0') 1081 e->e_from.q_home = NULL; 1082 else if (strcmp(user.mbdb_homedir, "/") == 0) 1083 e->e_from.q_home = ""; 1084 else 1085 e->e_from.q_home = sm_rpool_strdup_x(e->e_rpool, 1086 user.mbdb_homedir); 1087 macdefine(&e->e_macro, A_PERM, 'z', e->e_from.q_home); 1088 1089 /* extract user and group id */ 1090 if (user.mbdb_uid != SM_NO_UID) 1091 { 1092 e->e_from.q_uid = user.mbdb_uid; 1093 e->e_from.q_gid = user.mbdb_gid; 1094 e->e_from.q_flags |= QGOODUID; 1095 } 1096 1097 /* extract full name from passwd file */ 1098 if (FullName == NULL && !internal && 1099 user.mbdb_fullname[0] != '\0' && 1100 strcmp(user.mbdb_name, e->e_from.q_user) == 0) 1101 { 1102 FullName = newstr(user.mbdb_fullname); 1103 } 1104 } 1105 else 1106 { 1107 e->e_from.q_home = NULL; 1108 } 1109 if (FullName != NULL && !internal) 1110 macdefine(&e->e_macro, A_TEMP, 'x', FullName); 1111 } 1112 else if (!internal && OpMode != MD_DAEMON && OpMode != MD_SMTP) 1113 { 1114 if (e->e_from.q_home == NULL) 1115 { 1116 e->e_from.q_home = getenv("HOME"); 1117 if (e->e_from.q_home != NULL) 1118 { 1119 if (*e->e_from.q_home == '\0') 1120 e->e_from.q_home = NULL; 1121 else if (strcmp(e->e_from.q_home, "/") == 0) 1122 e->e_from.q_home++; 1123 } 1124 } 1125 e->e_from.q_uid = RealUid; 1126 e->e_from.q_gid = RealGid; 1127 e->e_from.q_flags |= QGOODUID; 1128 } 1129 1130 /* 1131 ** Rewrite the from person to dispose of possible implicit 1132 ** links in the net. 1133 */ 1134 1135 pvp = prescan(from, delimchar, pvpbuf, sizeof pvpbuf, NULL, NULL, false); 1136 if (pvp == NULL) 1137 { 1138 /* don't need to give error -- prescan did that already */ 1139 if (LogLevel > 2) 1140 sm_syslog(LOG_NOTICE, e->e_id, 1141 "cannot prescan from (%s)", 1142 shortenstring(from, MAXSHORTSTR)); 1143 finis(true, true, ExitStat); 1144 } 1145 (void) REWRITE(pvp, 3, e); 1146 (void) REWRITE(pvp, 1, e); 1147 (void) REWRITE(pvp, 4, e); 1148 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL); 1149 bp = buf + 1; 1150 cataddr(pvp, NULL, bp, sizeof buf - 2, '\0'); 1151 if (*bp == '@' && !bitnset(M_NOBRACKET, e->e_from.q_mailer->m_flags)) 1152 { 1153 /* heuristic: route-addr: add angle brackets */ 1154 (void) sm_strlcat(bp, ">", sizeof buf - 1); 1155 *--bp = '<'; 1156 } 1157 e->e_sender = sm_rpool_strdup_x(e->e_rpool, bp); 1158 macdefine(&e->e_macro, A_PERM, 'f', e->e_sender); 1159 1160 /* save the domain spec if this mailer wants it */ 1161 if (e->e_from.q_mailer != NULL && 1162 bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 1163 { 1164 char **lastat; 1165 1166 /* get rid of any pesky angle brackets */ 1167 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), "e s"); 1168 (void) REWRITE(pvp, 3, e); 1169 (void) REWRITE(pvp, 1, e); 1170 (void) REWRITE(pvp, 4, e); 1171 macdefine(&e->e_macro, A_PERM, macid("{addr_type}"), NULL); 1172 1173 /* strip off to the last "@" sign */ 1174 for (lastat = NULL; *pvp != NULL; pvp++) 1175 { 1176 if (strcmp(*pvp, "@") == 0) 1177 lastat = pvp; 1178 } 1179 if (lastat != NULL) 1180 { 1181 e->e_fromdomain = copyplist(lastat, true, e->e_rpool); 1182 if (tTd(45, 3)) 1183 { 1184 sm_dprintf("Saving from domain: "); 1185 printav(sm_debug_file(), e->e_fromdomain); 1186 } 1187 } 1188 } 1189 } 1190 /* 1191 ** PRINTENVFLAGS -- print envelope flags for debugging 1192 ** 1193 ** Parameters: 1194 ** e -- the envelope with the flags to be printed. 1195 ** 1196 ** Returns: 1197 ** none. 1198 */ 1199 1200 struct eflags 1201 { 1202 char *ef_name; 1203 unsigned long ef_bit; 1204 }; 1205 1206 static struct eflags EnvelopeFlags[] = 1207 { 1208 { "OLDSTYLE", EF_OLDSTYLE }, 1209 { "INQUEUE", EF_INQUEUE }, 1210 { "NO_BODY_RETN", EF_NO_BODY_RETN }, 1211 { "CLRQUEUE", EF_CLRQUEUE }, 1212 { "SENDRECEIPT", EF_SENDRECEIPT }, 1213 { "FATALERRS", EF_FATALERRS }, 1214 { "DELETE_BCC", EF_DELETE_BCC }, 1215 { "RESPONSE", EF_RESPONSE }, 1216 { "RESENT", EF_RESENT }, 1217 { "VRFYONLY", EF_VRFYONLY }, 1218 { "WARNING", EF_WARNING }, 1219 { "QUEUERUN", EF_QUEUERUN }, 1220 { "GLOBALERRS", EF_GLOBALERRS }, 1221 { "PM_NOTIFY", EF_PM_NOTIFY }, 1222 { "METOO", EF_METOO }, 1223 { "LOGSENDER", EF_LOGSENDER }, 1224 { "NORECEIPT", EF_NORECEIPT }, 1225 { "HAS8BIT", EF_HAS8BIT }, 1226 { "NL_NOT_EOL", EF_NL_NOT_EOL }, 1227 { "CRLF_NOT_EOL", EF_CRLF_NOT_EOL }, 1228 { "RET_PARAM", EF_RET_PARAM }, 1229 { "HAS_DF", EF_HAS_DF }, 1230 { "IS_MIME", EF_IS_MIME }, 1231 { "DONT_MIME", EF_DONT_MIME }, 1232 { "DISCARD", EF_DISCARD }, 1233 { "TOOBIG", EF_TOOBIG }, 1234 { "SPLIT", EF_SPLIT }, 1235 { "UNSAFE", EF_UNSAFE }, 1236 { NULL, 0 } 1237 }; 1238 1239 void 1240 printenvflags(e) 1241 register ENVELOPE *e; 1242 { 1243 register struct eflags *ef; 1244 bool first = true; 1245 1246 sm_dprintf("%lx", e->e_flags); 1247 for (ef = EnvelopeFlags; ef->ef_name != NULL; ef++) 1248 { 1249 if (!bitset(ef->ef_bit, e->e_flags)) 1250 continue; 1251 if (first) 1252 sm_dprintf("<%s", ef->ef_name); 1253 else 1254 sm_dprintf(",%s", ef->ef_name); 1255 first = false; 1256 } 1257 if (!first) 1258 sm_dprintf(">\n"); 1259 } 1260