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.21 (Berkeley) 03/16/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 #include <fcntl.h> 18 19 /* 20 ** NEWENVELOPE -- allocate a new envelope 21 ** 22 ** Supports inheritance. 23 ** 24 ** Parameters: 25 ** e -- the new envelope to fill in. 26 ** parent -- the envelope to be the parent of e. 27 ** 28 ** Returns: 29 ** e. 30 ** 31 ** Side Effects: 32 ** none. 33 */ 34 35 ENVELOPE * 36 newenvelope(e, parent) 37 register ENVELOPE *e; 38 register ENVELOPE *parent; 39 { 40 extern putheader(), putbody(); 41 extern ENVELOPE BlankEnvelope; 42 43 if (e == parent && e->e_parent != NULL) 44 parent = e->e_parent; 45 clearenvelope(e, TRUE); 46 if (e == CurEnv) 47 bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from); 48 else 49 bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from); 50 e->e_parent = parent; 51 e->e_ctime = curtime(); 52 if (parent != NULL) 53 e->e_msgpriority = parent->e_msgsize; 54 e->e_puthdr = putheader; 55 e->e_putbody = putbody; 56 if (CurEnv->e_xfp != NULL) 57 (void) fflush(CurEnv->e_xfp); 58 59 return (e); 60 } 61 /* 62 ** DROPENVELOPE -- deallocate an envelope. 63 ** 64 ** Parameters: 65 ** e -- the envelope to deallocate. 66 ** 67 ** Returns: 68 ** none. 69 ** 70 ** Side Effects: 71 ** housekeeping necessary to dispose of an envelope. 72 ** Unlocks this queue file. 73 */ 74 75 dropenvelope(e) 76 register ENVELOPE *e; 77 { 78 bool queueit = FALSE; 79 register ADDRESS *q; 80 char *id = e->e_id; 81 82 if (tTd(50, 1)) 83 { 84 printf("dropenvelope %x: id=", e); 85 xputs(e->e_id); 86 printf(", flags=%o\n", e->e_flags); 87 } 88 89 /* we must have an id to remove disk files */ 90 if (id == NULL) 91 return; 92 93 #ifdef LOG 94 if (LogLevel > 84) 95 syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d", 96 id, e->e_flags, getpid()); 97 #endif /* LOG */ 98 99 /* 100 ** Extract state information from dregs of send list. 101 */ 102 103 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 104 { 105 if (bitset(QQUEUEUP, q->q_flags)) 106 queueit = TRUE; 107 } 108 109 /* 110 ** Send back return receipts as requested. 111 */ 112 113 if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 114 { 115 auto ADDRESS *rlist = NULL; 116 117 (void) sendtolist(e->e_receiptto, (ADDRESS *) NULL, &rlist, e); 118 (void) returntosender("Return receipt", rlist, FALSE, e); 119 } 120 121 /* 122 ** Arrange to send error messages if there are fatal errors. 123 */ 124 125 if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && 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 ** 411 ** Returns: 412 ** pointer to the delimiter terminating the from address. 413 ** 414 ** Side Effects: 415 ** sets sendmail's notion of who the from person is. 416 */ 417 418 char * 419 setsender(from, e, delimptr) 420 char *from; 421 register ENVELOPE *e; 422 char **delimptr; 423 { 424 register char **pvp; 425 char *realname = NULL; 426 register struct passwd *pw; 427 char *delimchar = NULL; 428 char buf[MAXNAME]; 429 char pvpbuf[PSBUFSIZE]; 430 extern struct passwd *getpwnam(); 431 extern char *macvalue(); 432 extern char **prescan(); 433 extern char *FullName; 434 435 if (tTd(45, 1)) 436 printf("setsender(%s)\n", from == NULL ? "" : from); 437 438 /* 439 ** Figure out the real user executing us. 440 ** Username can return errno != 0 on non-errors. 441 */ 442 443 if (QueueRun || OpMode == MD_SMTP) 444 realname = from; 445 if (realname == NULL || realname[0] == '\0') 446 { 447 extern char *username(); 448 449 realname = username(); 450 } 451 452 /* 453 SuprErrs = TRUE; 454 */ 455 if (from == NULL || 456 parseaddr(from, &e->e_from, 1, ' ', delimptr, e) == NULL) 457 { 458 /* log garbage addresses for traceback */ 459 # ifdef LOG 460 if (from != NULL && LogLevel > 2) 461 { 462 char *host = RealHostName; 463 464 if (host == NULL) 465 host = MyHostName; 466 syslog(LOG_NOTICE, 467 "from=%s unparseable, received from %s@%s", 468 from, realname, host); 469 } 470 # endif /* LOG */ 471 if (from != NULL) 472 SuprErrs = TRUE; 473 if (from == realname || 474 parseaddr(from = newstr(realname), &e->e_from, 1, ' ', NULL, e) == NULL) 475 { 476 SuprErrs = TRUE; 477 if (parseaddr("postmaster", &e->e_from, 1, ' ', NULL, e) == NULL) 478 syserr("553 setsender: can't even parse postmaster!"); 479 } 480 } 481 else 482 FromFlag = TRUE; 483 e->e_from.q_flags |= QDONTSEND; 484 if (tTd(45, 5)) 485 { 486 printf("setsender: QDONTSEND "); 487 printaddr(&e->e_from, FALSE); 488 } 489 SuprErrs = FALSE; 490 491 pvp = NULL; 492 if (e->e_from.q_mailer == LocalMailer) 493 { 494 # ifdef USERDB 495 register char *p; 496 extern char *udbsender(); 497 # endif 498 499 /* if the user has given fullname already, don't redefine */ 500 if (FullName == NULL) 501 FullName = macvalue('x', e); 502 if (FullName != NULL && FullName[0] == '\0') 503 FullName = NULL; 504 505 # ifdef USERDB 506 p = udbsender(from); 507 508 if (p != NULL) 509 { 510 /* 511 ** We have an alternate address for the sender 512 */ 513 514 pvp = prescan(p, '\0', pvpbuf, NULL); 515 } 516 # endif /* USERDB */ 517 518 if ((pw = getpwnam(e->e_from.q_user)) != NULL) 519 { 520 /* 521 ** Process passwd file entry. 522 */ 523 524 525 /* extract home directory */ 526 e->e_from.q_home = newstr(pw->pw_dir); 527 define('z', e->e_from.q_home, e); 528 529 /* extract user and group id */ 530 e->e_from.q_uid = pw->pw_uid; 531 e->e_from.q_gid = pw->pw_gid; 532 533 /* extract full name from passwd file */ 534 if (FullName == NULL && pw->pw_gecos != NULL && 535 strcmp(pw->pw_name, e->e_from.q_user) == 0) 536 { 537 buildfname(pw->pw_gecos, e->e_from.q_user, buf); 538 if (buf[0] != '\0') 539 FullName = newstr(buf); 540 } 541 } 542 if (FullName != NULL) 543 define('x', FullName, e); 544 } 545 else 546 { 547 if (e->e_from.q_home == NULL) 548 e->e_from.q_home = getenv("HOME"); 549 e->e_from.q_uid = getuid(); 550 e->e_from.q_gid = getgid(); 551 } 552 553 /* 554 ** Rewrite the from person to dispose of possible implicit 555 ** links in the net. 556 */ 557 558 if (pvp == NULL) 559 pvp = prescan(from, '\0', pvpbuf, NULL); 560 if (pvp == NULL) 561 { 562 /* don't need to give error -- prescan did that already */ 563 # ifdef LOG 564 if (LogLevel > 2) 565 syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 566 # endif 567 finis(); 568 } 569 rewrite(pvp, 3); 570 rewrite(pvp, 1); 571 rewrite(pvp, 4); 572 573 define('f', e->e_from.q_paddr, e); 574 575 /* save the domain spec if this mailer wants it */ 576 if (e->e_from.q_mailer != NULL && 577 bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 578 { 579 extern char **copyplist(); 580 581 while (*pvp != NULL && strcmp(*pvp, "@") != 0) 582 pvp++; 583 if (*pvp != NULL) 584 e->e_fromdomain = copyplist(pvp, TRUE); 585 } 586 } 587