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.33 (Berkeley) 04/27/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 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[60]; /* 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 && !bitset(EF_QUEUERUN, e->e_flags) && 258 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 #ifndef O_APPEND 348 #define O_APPEND 0 349 #endif 350 351 openxscript(e) 352 register ENVELOPE *e; 353 { 354 register char *p; 355 int fd; 356 357 if (e->e_xfp != NULL) 358 return; 359 p = queuename(e, 'x'); 360 fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644); 361 if (fd < 0) 362 syserr("Can't create %s", p); 363 else 364 e->e_xfp = fdopen(fd, "w"); 365 } 366 /* 367 ** CLOSEXSCRIPT -- close the transcript file. 368 ** 369 ** Parameters: 370 ** e -- the envelope containing the transcript to close. 371 ** 372 ** Returns: 373 ** none. 374 ** 375 ** Side Effects: 376 ** none. 377 */ 378 379 closexscript(e) 380 register ENVELOPE *e; 381 { 382 if (e->e_xfp == NULL) 383 return; 384 (void) xfclose(e->e_xfp, "closexscript", e->e_id); 385 e->e_xfp = NULL; 386 } 387 /* 388 ** SETSENDER -- set the person who this message is from 389 ** 390 ** Under certain circumstances allow the user to say who 391 ** s/he is (using -f or -r). These are: 392 ** 1. The user's uid is zero (root). 393 ** 2. The user's login name is in an approved list (typically 394 ** from a network server). 395 ** 3. The address the user is trying to claim has a 396 ** "!" character in it (since #2 doesn't do it for 397 ** us if we are dialing out for UUCP). 398 ** A better check to replace #3 would be if the 399 ** effective uid is "UUCP" -- this would require me 400 ** to rewrite getpwent to "grab" uucp as it went by, 401 ** make getname more nasty, do another passwd file 402 ** scan, or compile the UID of "UUCP" into the code, 403 ** all of which are reprehensible. 404 ** 405 ** Assuming all of these fail, we figure out something 406 ** ourselves. 407 ** 408 ** Parameters: 409 ** from -- the person we would like to believe this message 410 ** is from, as specified on the command line. 411 ** e -- the envelope in which we would like the sender set. 412 ** delimptr -- if non-NULL, set to the location of the 413 ** trailing delimiter. 414 ** internal -- set if this address is coming from an internal 415 ** source such as an owner alias. 416 ** 417 ** Returns: 418 ** none. 419 ** 420 ** Side Effects: 421 ** sets sendmail's notion of who the from person is. 422 */ 423 424 setsender(from, e, delimptr, internal) 425 char *from; 426 register ENVELOPE *e; 427 char **delimptr; 428 bool internal; 429 { 430 register char **pvp; 431 char *realname = NULL; 432 register struct passwd *pw; 433 char delimchar; 434 char buf[MAXNAME]; 435 char pvpbuf[PSBUFSIZE]; 436 extern struct passwd *getpwnam(); 437 extern char *macvalue(); 438 extern char **prescan(); 439 extern char *FullName; 440 441 if (tTd(45, 1)) 442 printf("setsender(%s)\n", from == NULL ? "" : from); 443 444 /* 445 ** Figure out the real user executing us. 446 ** Username can return errno != 0 on non-errors. 447 */ 448 449 if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP) 450 realname = from; 451 if (realname == NULL || realname[0] == '\0') 452 { 453 extern char *username(); 454 455 realname = username(); 456 } 457 458 if (ConfigLevel < 2) 459 SuprErrs = TRUE; 460 461 delimchar = internal ? '\0' : ' '; 462 if (from == NULL || 463 parseaddr(from, &e->e_from, 1, delimchar, delimptr, e) == NULL) 464 { 465 /* log garbage addresses for traceback */ 466 # ifdef LOG 467 if (from != NULL && LogLevel > 2) 468 { 469 char *p; 470 char ebuf[MAXNAME * 2 + 2]; 471 472 p = macvalue('_', e); 473 if (p == NULL) 474 { 475 char *host = RealHostName; 476 if (host == NULL) 477 host = MyHostName; 478 (void) sprintf(ebuf, "%s@%s", realname, host); 479 p = ebuf; 480 } 481 syslog(LOG_NOTICE, 482 "from=%s unparseable, received from %s", 483 from, p); 484 } 485 # endif /* LOG */ 486 if (from != NULL) 487 SuprErrs = TRUE; 488 if (from == realname || 489 parseaddr(from = newstr(realname), &e->e_from, 1, ' ', NULL, e) == NULL) 490 { 491 SuprErrs = TRUE; 492 if (parseaddr("postmaster", &e->e_from, 1, ' ', NULL, e) == NULL) 493 syserr("553 setsender: can't even parse postmaster!"); 494 } 495 } 496 else 497 FromFlag = TRUE; 498 e->e_from.q_flags |= QDONTSEND; 499 if (tTd(45, 5)) 500 { 501 printf("setsender: QDONTSEND "); 502 printaddr(&e->e_from, FALSE); 503 } 504 SuprErrs = FALSE; 505 506 pvp = NULL; 507 if (e->e_from.q_mailer == LocalMailer) 508 { 509 # ifdef USERDB 510 register char *p; 511 extern char *udbsender(); 512 # endif 513 514 if (!internal) 515 { 516 /* if the user has given fullname already, don't redefine */ 517 if (FullName == NULL) 518 FullName = macvalue('x', e); 519 if (FullName != NULL && FullName[0] == '\0') 520 FullName = NULL; 521 522 # ifdef USERDB 523 p = udbsender(from); 524 525 if (p != NULL) 526 { 527 /* 528 ** We have an alternate address for the sender 529 */ 530 531 pvp = prescan(p, '\0', pvpbuf, NULL); 532 } 533 # endif /* USERDB */ 534 } 535 536 if ((pw = getpwnam(e->e_from.q_user)) != NULL) 537 { 538 /* 539 ** Process passwd file entry. 540 */ 541 542 543 /* extract home directory */ 544 e->e_from.q_home = newstr(pw->pw_dir); 545 define('z', e->e_from.q_home, e); 546 547 /* extract user and group id */ 548 e->e_from.q_uid = pw->pw_uid; 549 e->e_from.q_gid = pw->pw_gid; 550 551 /* extract full name from passwd file */ 552 if (FullName == NULL && pw->pw_gecos != NULL && 553 strcmp(pw->pw_name, e->e_from.q_user) == 0 && 554 !internal) 555 { 556 buildfname(pw->pw_gecos, e->e_from.q_user, buf); 557 if (buf[0] != '\0') 558 FullName = newstr(buf); 559 } 560 } 561 if (FullName != NULL && !internal) 562 define('x', FullName, e); 563 } 564 else if (!internal) 565 { 566 if (e->e_from.q_home == NULL) 567 e->e_from.q_home = getenv("HOME"); 568 e->e_from.q_uid = getuid(); 569 e->e_from.q_gid = getgid(); 570 } 571 572 /* 573 ** Rewrite the from person to dispose of possible implicit 574 ** links in the net. 575 */ 576 577 if (pvp == NULL) 578 pvp = prescan(from, '\0', pvpbuf, NULL); 579 if (pvp == NULL) 580 { 581 /* don't need to give error -- prescan did that already */ 582 # ifdef LOG 583 if (LogLevel > 2) 584 syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 585 # endif 586 finis(); 587 } 588 (void) rewrite(pvp, 3, e); 589 (void) rewrite(pvp, 1, e); 590 (void) rewrite(pvp, 4, e); 591 cataddr(pvp, NULL, buf, sizeof buf, '\0'); 592 e->e_sender = newstr(buf); 593 define('f', e->e_sender, e); 594 595 /* save the domain spec if this mailer wants it */ 596 if (!internal && e->e_from.q_mailer != NULL && 597 bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 598 { 599 extern char **copyplist(); 600 601 while (*pvp != NULL && strcmp(*pvp, "@") != 0) 602 pvp++; 603 if (*pvp != NULL) 604 e->e_fromdomain = copyplist(pvp, TRUE); 605 } 606 } 607