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.19 (Berkeley) 03/03/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 if (id == NULL) 90 id = "(none)"; 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 /* we must have an id to remove disk files */ 99 if (e->e_id == NULL) 100 return; 101 102 /* 103 ** Extract state information from dregs of send list. 104 */ 105 106 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 107 { 108 if (bitset(QQUEUEUP, q->q_flags)) 109 queueit = TRUE; 110 } 111 112 /* 113 ** Send back return receipts as requested. 114 */ 115 116 if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 117 { 118 auto ADDRESS *rlist = NULL; 119 120 (void) sendtolist(e->e_receiptto, (ADDRESS *) NULL, &rlist, e); 121 (void) returntosender("Return receipt", rlist, FALSE, e); 122 } 123 124 /* 125 ** Arrange to send error messages if there are fatal errors. 126 */ 127 128 if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && ErrorMode != EM_QUIET) 129 savemail(e); 130 131 /* 132 ** Instantiate or deinstantiate the queue. 133 */ 134 135 if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 136 bitset(EF_CLRQUEUE, e->e_flags)) 137 { 138 if (e->e_df != NULL) 139 xunlink(e->e_df); 140 xunlink(queuename(e, 'q')); 141 } 142 else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 143 { 144 #ifdef QUEUE 145 queueup(e, FALSE, FALSE); 146 #else /* QUEUE */ 147 syserr("554 dropenvelope: queueup"); 148 #endif /* QUEUE */ 149 } 150 151 /* now unlock the job */ 152 closexscript(e); 153 unlockqueue(e); 154 155 /* make sure that this envelope is marked unused */ 156 e->e_id = e->e_df = NULL; 157 if (e->e_dfp != NULL) 158 (void) fclose(e->e_dfp); 159 e->e_dfp = NULL; 160 161 #ifdef LOG 162 if (LogLevel > 74) 163 syslog(LOG_INFO, "%s: done", id); 164 #endif /* LOG */ 165 } 166 /* 167 ** CLEARENVELOPE -- clear an envelope without unlocking 168 ** 169 ** This is normally used by a child process to get a clean 170 ** envelope without disturbing the parent. 171 ** 172 ** Parameters: 173 ** e -- the envelope to clear. 174 ** fullclear - if set, the current envelope is total 175 ** garbage and should be ignored; otherwise, 176 ** release any resources it may indicate. 177 ** 178 ** Returns: 179 ** none. 180 ** 181 ** Side Effects: 182 ** Closes files associated with the envelope. 183 ** Marks the envelope as unallocated. 184 */ 185 186 clearenvelope(e, fullclear) 187 register ENVELOPE *e; 188 bool fullclear; 189 { 190 register HDR *bh; 191 register HDR **nhp; 192 extern ENVELOPE BlankEnvelope; 193 194 if (!fullclear) 195 { 196 /* clear out any file information */ 197 if (e->e_xfp != NULL) 198 (void) fclose(e->e_xfp); 199 if (e->e_dfp != NULL) 200 (void) fclose(e->e_dfp); 201 } 202 203 /* now clear out the data */ 204 STRUCTCOPY(BlankEnvelope, *e); 205 bh = BlankEnvelope.e_header; 206 nhp = &e->e_header; 207 while (bh != NULL) 208 { 209 *nhp = (HDR *) xalloc(sizeof *bh); 210 bcopy((char *) bh, (char *) *nhp, sizeof *bh); 211 bh = bh->h_link; 212 nhp = &(*nhp)->h_link; 213 } 214 } 215 /* 216 ** INITSYS -- initialize instantiation of system 217 ** 218 ** In Daemon mode, this is done in the child. 219 ** 220 ** Parameters: 221 ** none. 222 ** 223 ** Returns: 224 ** none. 225 ** 226 ** Side Effects: 227 ** Initializes the system macros, some global variables, 228 ** etc. In particular, the current time in various 229 ** forms is set. 230 */ 231 232 initsys(e) 233 register ENVELOPE *e; 234 { 235 static char cbuf[5]; /* holds hop count */ 236 static char pbuf[10]; /* holds pid */ 237 #ifdef TTYNAME 238 static char ybuf[10]; /* holds tty id */ 239 register char *p; 240 #endif /* TTYNAME */ 241 extern char *ttyname(); 242 extern char *macvalue(); 243 extern char Version[]; 244 245 /* 246 ** Give this envelope a reality. 247 ** I.e., an id, a transcript, and a creation time. 248 */ 249 250 openxscript(e); 251 e->e_ctime = curtime(); 252 253 /* 254 ** Set OutChannel to something useful if stdout isn't it. 255 ** This arranges that any extra stuff the mailer produces 256 ** gets sent back to the user on error (because it is 257 ** tucked away in the transcript). 258 */ 259 260 if (OpMode == MD_DAEMON && QueueRun && 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 openxscript(e) 350 register ENVELOPE *e; 351 { 352 register char *p; 353 int fd; 354 355 if (e->e_xfp != NULL) 356 return; 357 p = queuename(e, 'x'); 358 fd = open(p, O_WRONLY|O_CREAT, 0644); 359 if (fd < 0) 360 syserr("Can't create %s", p); 361 else 362 e->e_xfp = fdopen(fd, "w"); 363 } 364 /* 365 ** CLOSEXSCRIPT -- close the transcript file. 366 ** 367 ** Parameters: 368 ** e -- the envelope containing the transcript to close. 369 ** 370 ** Returns: 371 ** none. 372 ** 373 ** Side Effects: 374 ** none. 375 */ 376 377 closexscript(e) 378 register ENVELOPE *e; 379 { 380 if (e->e_xfp == NULL) 381 return; 382 (void) fclose(e->e_xfp); 383 e->e_xfp = NULL; 384 } 385 /* 386 ** SETSENDER -- set the person who this message is from 387 ** 388 ** Under certain circumstances allow the user to say who 389 ** s/he is (using -f or -r). These are: 390 ** 1. The user's uid is zero (root). 391 ** 2. The user's login name is in an approved list (typically 392 ** from a network server). 393 ** 3. The address the user is trying to claim has a 394 ** "!" character in it (since #2 doesn't do it for 395 ** us if we are dialing out for UUCP). 396 ** A better check to replace #3 would be if the 397 ** effective uid is "UUCP" -- this would require me 398 ** to rewrite getpwent to "grab" uucp as it went by, 399 ** make getname more nasty, do another passwd file 400 ** scan, or compile the UID of "UUCP" into the code, 401 ** all of which are reprehensible. 402 ** 403 ** Assuming all of these fail, we figure out something 404 ** ourselves. 405 ** 406 ** Parameters: 407 ** from -- the person we would like to believe this message 408 ** is from, as specified on the command line. 409 ** e -- the envelope in which we would like the sender set. 410 ** delimptr -- if non-NULL, set to the location of the 411 ** trailing delimiter. 412 ** 413 ** Returns: 414 ** pointer to the delimiter terminating the from address. 415 ** 416 ** Side Effects: 417 ** sets sendmail's notion of who the from person is. 418 */ 419 420 char * 421 setsender(from, e, delimptr) 422 char *from; 423 register ENVELOPE *e; 424 char **delimptr; 425 { 426 register char **pvp; 427 char *realname = NULL; 428 register struct passwd *pw; 429 char *delimchar = NULL; 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 if (from == NULL || 458 parseaddr(from, &e->e_from, 1, ' ', delimptr, e) == NULL) 459 { 460 /* log garbage addresses for traceback */ 461 # ifdef LOG 462 if (from != NULL && LogLevel > 2) 463 { 464 char *host = RealHostName; 465 466 if (host == NULL) 467 host = MyHostName; 468 syslog(LOG_NOTICE, 469 "from=%s unparseable, received from %s@%s", 470 from, realname, host); 471 } 472 # endif /* LOG */ 473 if (from != NULL) 474 SuprErrs = TRUE; 475 if (from == realname || 476 parseaddr(from = newstr(realname), &e->e_from, 1, ' ', NULL, e) == NULL) 477 { 478 SuprErrs = TRUE; 479 if (parseaddr("postmaster", &e->e_from, 1, ' ', NULL, e) == NULL) 480 syserr("553 setsender: can't even parse postmaster!"); 481 } 482 } 483 else 484 FromFlag = TRUE; 485 e->e_from.q_flags |= QDONTSEND; 486 if (tTd(45, 5)) 487 { 488 printf("setsender: QDONTSEND "); 489 printaddr(&e->e_from, FALSE); 490 } 491 loweraddr(&e->e_from); 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 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 if ((pw = getpwnam(e->e_from.q_user)) != NULL) 522 { 523 /* 524 ** Process passwd file entry. 525 */ 526 527 528 /* extract home directory */ 529 e->e_from.q_home = newstr(pw->pw_dir); 530 define('z', e->e_from.q_home, e); 531 532 /* extract user and group id */ 533 e->e_from.q_uid = pw->pw_uid; 534 e->e_from.q_gid = pw->pw_gid; 535 536 /* extract full name from passwd file */ 537 if (FullName == NULL && pw->pw_gecos != NULL && 538 strcmp(pw->pw_name, e->e_from.q_user) == 0) 539 { 540 buildfname(pw->pw_gecos, e->e_from.q_user, buf); 541 if (buf[0] != '\0') 542 FullName = newstr(buf); 543 } 544 } 545 if (FullName != NULL) 546 define('x', FullName, e); 547 } 548 else 549 { 550 if (e->e_from.q_home == NULL) 551 e->e_from.q_home = getenv("HOME"); 552 e->e_from.q_uid = getuid(); 553 e->e_from.q_gid = getgid(); 554 } 555 556 /* 557 ** Rewrite the from person to dispose of possible implicit 558 ** links in the net. 559 */ 560 561 if (pvp == NULL) 562 pvp = prescan(from, '\0', pvpbuf, NULL); 563 if (pvp == NULL) 564 { 565 /* don't need to give error -- prescan did that already */ 566 # ifdef LOG 567 if (LogLevel > 2) 568 syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 569 # endif 570 finis(); 571 } 572 rewrite(pvp, 3); 573 rewrite(pvp, 1); 574 rewrite(pvp, 4); 575 cataddr(pvp, buf, sizeof buf, '\0'); 576 e->e_sender = e->e_returnpath = newstr(buf); 577 578 define('f', e->e_sender, e); 579 580 /* save the domain spec if this mailer wants it */ 581 if (e->e_from.q_mailer != NULL && 582 bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 583 { 584 extern char **copyplist(); 585 586 while (*pvp != NULL && strcmp(*pvp, "@") != 0) 587 pvp++; 588 if (*pvp != NULL) 589 e->e_fromdomain = copyplist(pvp, TRUE); 590 } 591 } 592