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