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