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.23 (Berkeley) 03/18/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) && 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 bh = BlankEnvelope.e_header; 203 nhp = &e->e_header; 204 while (bh != NULL) 205 { 206 *nhp = (HDR *) xalloc(sizeof *bh); 207 bcopy((char *) bh, (char *) *nhp, sizeof *bh); 208 bh = bh->h_link; 209 nhp = &(*nhp)->h_link; 210 } 211 } 212 /* 213 ** INITSYS -- initialize instantiation of system 214 ** 215 ** In Daemon mode, this is done in the child. 216 ** 217 ** Parameters: 218 ** none. 219 ** 220 ** Returns: 221 ** none. 222 ** 223 ** Side Effects: 224 ** Initializes the system macros, some global variables, 225 ** etc. In particular, the current time in various 226 ** forms is set. 227 */ 228 229 initsys(e) 230 register ENVELOPE *e; 231 { 232 static char cbuf[5]; /* holds hop count */ 233 static char pbuf[10]; /* holds pid */ 234 #ifdef TTYNAME 235 static char ybuf[10]; /* holds tty id */ 236 register char *p; 237 #endif /* TTYNAME */ 238 extern char *ttyname(); 239 extern char *macvalue(); 240 extern char Version[]; 241 242 /* 243 ** Give this envelope a reality. 244 ** I.e., an id, a transcript, and a creation time. 245 */ 246 247 openxscript(e); 248 e->e_ctime = curtime(); 249 250 /* 251 ** Set OutChannel to something useful if stdout isn't it. 252 ** This arranges that any extra stuff the mailer produces 253 ** gets sent back to the user on error (because it is 254 ** tucked away in the transcript). 255 */ 256 257 if (OpMode == MD_DAEMON && QueueRun && e->e_xfp != NULL) 258 OutChannel = e->e_xfp; 259 260 /* 261 ** Set up some basic system macros. 262 */ 263 264 /* process id */ 265 (void) sprintf(pbuf, "%d", getpid()); 266 define('p', pbuf, e); 267 268 /* hop count */ 269 (void) sprintf(cbuf, "%d", e->e_hopcount); 270 define('c', cbuf, e); 271 272 /* time as integer, unix time, arpa time */ 273 settime(e); 274 275 #ifdef TTYNAME 276 /* tty name */ 277 if (macvalue('y', e) == NULL) 278 { 279 p = ttyname(2); 280 if (p != NULL) 281 { 282 if (strrchr(p, '/') != NULL) 283 p = strrchr(p, '/') + 1; 284 (void) strcpy(ybuf, p); 285 define('y', ybuf, e); 286 } 287 } 288 #endif /* TTYNAME */ 289 } 290 /* 291 ** SETTIME -- set the current time. 292 ** 293 ** Parameters: 294 ** none. 295 ** 296 ** Returns: 297 ** none. 298 ** 299 ** Side Effects: 300 ** Sets the various time macros -- $a, $b, $d, $t. 301 */ 302 303 settime(e) 304 register ENVELOPE *e; 305 { 306 register char *p; 307 auto time_t now; 308 static char tbuf[20]; /* holds "current" time */ 309 static char dbuf[30]; /* holds ctime(tbuf) */ 310 register struct tm *tm; 311 extern char *arpadate(); 312 extern struct tm *gmtime(); 313 extern char *macvalue(); 314 315 now = curtime(); 316 tm = gmtime(&now); 317 (void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900, 318 tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min); 319 define('t', tbuf, e); 320 (void) strcpy(dbuf, ctime(&now)); 321 p = strchr(dbuf, '\n'); 322 if (p != NULL) 323 *p = '\0'; 324 define('d', dbuf, e); 325 p = newstr(arpadate(dbuf)); 326 if (macvalue('a', e) == NULL) 327 define('a', p, e); 328 define('b', p, e); 329 } 330 /* 331 ** OPENXSCRIPT -- Open transcript file 332 ** 333 ** Creates a transcript file for possible eventual mailing or 334 ** sending back. 335 ** 336 ** Parameters: 337 ** e -- the envelope to create the transcript in/for. 338 ** 339 ** Returns: 340 ** none 341 ** 342 ** Side Effects: 343 ** Creates the transcript file. 344 */ 345 346 openxscript(e) 347 register ENVELOPE *e; 348 { 349 register char *p; 350 int fd; 351 352 if (e->e_xfp != NULL) 353 return; 354 p = queuename(e, 'x'); 355 fd = open(p, O_WRONLY|O_CREAT, 0644); 356 if (fd < 0) 357 syserr("Can't create %s", p); 358 else 359 e->e_xfp = fdopen(fd, "w"); 360 } 361 /* 362 ** CLOSEXSCRIPT -- close the transcript file. 363 ** 364 ** Parameters: 365 ** e -- the envelope containing the transcript to close. 366 ** 367 ** Returns: 368 ** none. 369 ** 370 ** Side Effects: 371 ** none. 372 */ 373 374 closexscript(e) 375 register ENVELOPE *e; 376 { 377 if (e->e_xfp == NULL) 378 return; 379 (void) xfclose(e->e_xfp, "closexscript", e->e_id); 380 e->e_xfp = NULL; 381 } 382 /* 383 ** SETSENDER -- set the person who this message is from 384 ** 385 ** Under certain circumstances allow the user to say who 386 ** s/he is (using -f or -r). These are: 387 ** 1. The user's uid is zero (root). 388 ** 2. The user's login name is in an approved list (typically 389 ** from a network server). 390 ** 3. The address the user is trying to claim has a 391 ** "!" character in it (since #2 doesn't do it for 392 ** us if we are dialing out for UUCP). 393 ** A better check to replace #3 would be if the 394 ** effective uid is "UUCP" -- this would require me 395 ** to rewrite getpwent to "grab" uucp as it went by, 396 ** make getname more nasty, do another passwd file 397 ** scan, or compile the UID of "UUCP" into the code, 398 ** all of which are reprehensible. 399 ** 400 ** Assuming all of these fail, we figure out something 401 ** ourselves. 402 ** 403 ** Parameters: 404 ** from -- the person we would like to believe this message 405 ** is from, as specified on the command line. 406 ** e -- the envelope in which we would like the sender set. 407 ** delimptr -- if non-NULL, set to the location of the 408 ** trailing delimiter. 409 ** internal -- set if this address is coming from an internal 410 ** source such as an owner alias. 411 ** 412 ** Returns: 413 ** none. 414 ** 415 ** Side Effects: 416 ** sets sendmail's notion of who the from person is. 417 */ 418 419 setsender(from, e, delimptr, internal) 420 char *from; 421 register ENVELOPE *e; 422 char **delimptr; 423 bool internal; 424 { 425 register char **pvp; 426 char *realname = NULL; 427 register struct passwd *pw; 428 char *delimchar = NULL; 429 char buf[MAXNAME]; 430 char pvpbuf[PSBUFSIZE]; 431 extern struct passwd *getpwnam(); 432 extern char *macvalue(); 433 extern char **prescan(); 434 extern char *FullName; 435 436 if (tTd(45, 1)) 437 printf("setsender(%s)\n", from == NULL ? "" : from); 438 439 /* 440 ** Figure out the real user executing us. 441 ** Username can return errno != 0 on non-errors. 442 */ 443 444 if (QueueRun || OpMode == MD_SMTP) 445 realname = from; 446 if (realname == NULL || realname[0] == '\0') 447 { 448 extern char *username(); 449 450 realname = username(); 451 } 452 453 /* 454 SuprErrs = TRUE; 455 */ 456 if (from == NULL || 457 parseaddr(from, &e->e_from, 1, ' ', delimptr, e) == NULL) 458 { 459 /* log garbage addresses for traceback */ 460 # ifdef LOG 461 if (from != NULL && LogLevel > 2) 462 { 463 char *host = RealHostName; 464 465 if (host == NULL) 466 host = MyHostName; 467 syslog(LOG_NOTICE, 468 "from=%s unparseable, received from %s@%s", 469 from, realname, host); 470 } 471 # endif /* LOG */ 472 if (from != NULL) 473 SuprErrs = TRUE; 474 if (from == realname || 475 parseaddr(from = newstr(realname), &e->e_from, 1, ' ', NULL, e) == NULL) 476 { 477 SuprErrs = TRUE; 478 if (parseaddr("postmaster", &e->e_from, 1, ' ', NULL, e) == NULL) 479 syserr("553 setsender: can't even parse postmaster!"); 480 } 481 } 482 else 483 FromFlag = TRUE; 484 e->e_from.q_flags |= QDONTSEND; 485 if (tTd(45, 5)) 486 { 487 printf("setsender: QDONTSEND "); 488 printaddr(&e->e_from, FALSE); 489 } 490 SuprErrs = FALSE; 491 492 pvp = NULL; 493 if (e->e_from.q_mailer == LocalMailer) 494 { 495 # ifdef USERDB 496 register char *p; 497 extern char *udbsender(); 498 # endif 499 500 if (!internal) 501 { 502 /* if the user has given fullname already, don't redefine */ 503 if (FullName == NULL) 504 FullName = macvalue('x', e); 505 if (FullName != NULL && FullName[0] == '\0') 506 FullName = NULL; 507 508 # ifdef USERDB 509 p = udbsender(from); 510 511 if (p != NULL) 512 { 513 /* 514 ** We have an alternate address for the sender 515 */ 516 517 pvp = prescan(p, '\0', pvpbuf, NULL); 518 } 519 # endif /* USERDB */ 520 } 521 522 if ((pw = getpwnam(e->e_from.q_user)) != NULL) 523 { 524 /* 525 ** Process passwd file entry. 526 */ 527 528 529 /* extract home directory */ 530 e->e_from.q_home = newstr(pw->pw_dir); 531 define('z', e->e_from.q_home, e); 532 533 /* extract user and group id */ 534 e->e_from.q_uid = pw->pw_uid; 535 e->e_from.q_gid = pw->pw_gid; 536 537 /* extract full name from passwd file */ 538 if (FullName == NULL && pw->pw_gecos != NULL && 539 strcmp(pw->pw_name, e->e_from.q_user) == 0 && 540 !internal) 541 { 542 buildfname(pw->pw_gecos, e->e_from.q_user, buf); 543 if (buf[0] != '\0') 544 FullName = newstr(buf); 545 } 546 } 547 if (FullName != NULL && !internal) 548 define('x', FullName, e); 549 } 550 else if (!internal) 551 { 552 if (e->e_from.q_home == NULL) 553 e->e_from.q_home = getenv("HOME"); 554 e->e_from.q_uid = getuid(); 555 e->e_from.q_gid = getgid(); 556 } 557 558 /* 559 ** Rewrite the from person to dispose of possible implicit 560 ** links in the net. 561 */ 562 563 if (pvp == NULL) 564 pvp = prescan(from, '\0', pvpbuf, NULL); 565 if (pvp == NULL) 566 { 567 /* don't need to give error -- prescan did that already */ 568 # ifdef LOG 569 if (LogLevel > 2) 570 syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 571 # endif 572 finis(); 573 } 574 rewrite(pvp, 3); 575 rewrite(pvp, 1); 576 rewrite(pvp, 4); 577 cataddr(pvp, buf, sizeof buf, '\0'); 578 e->e_sender = newstr(buf); 579 define('f', e->e_sender, e); 580 581 /* save the domain spec if this mailer wants it */ 582 if (!internal && e->e_from.q_mailer != NULL && 583 bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 584 { 585 extern char **copyplist(); 586 587 while (*pvp != NULL && strcmp(*pvp, "@") != 0) 588 pvp++; 589 if (*pvp != NULL) 590 e->e_fromdomain = copyplist(pvp, TRUE); 591 } 592 } 593