1 #include <pwd.h> 2 #include <time.h> 3 #include "sendmail.h" 4 #include <sys/stat.h> 5 6 SCCSID(@(#)envelope.c 3.8 02/03/83); 7 8 /* 9 ** NEWENVELOPE -- allocate a new envelope 10 ** 11 ** Supports inheritance. 12 ** 13 ** Parameters: 14 ** e -- the new envelope to fill in. 15 ** 16 ** Returns: 17 ** e. 18 ** 19 ** Side Effects: 20 ** none. 21 */ 22 23 ENVELOPE * 24 newenvelope(e) 25 register ENVELOPE *e; 26 { 27 register HDR *bh; 28 register HDR **nhp; 29 register ENVELOPE *parent; 30 extern putheader(), putbody(); 31 extern ENVELOPE BlankEnvelope; 32 33 parent = CurEnv; 34 if (e == CurEnv) 35 parent = e->e_parent; 36 clear((char *) e, sizeof *e); 37 bmove((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from); 38 e->e_parent = parent; 39 e->e_ctime = curtime(); 40 e->e_puthdr = putheader; 41 e->e_putbody = putbody; 42 bh = BlankEnvelope.e_header; 43 nhp = &e->e_header; 44 while (bh != NULL) 45 { 46 *nhp = (HDR *) xalloc(sizeof *bh); 47 bmove((char *) bh, (char *) *nhp, sizeof *bh); 48 bh = bh->h_link; 49 nhp = &(*nhp)->h_link; 50 } 51 if (CurEnv->e_xfp != NULL) 52 (void) fflush(CurEnv->e_xfp); 53 54 return (e); 55 } 56 /* 57 ** DROPENVELOPE -- deallocate an envelope. 58 ** 59 ** Parameters: 60 ** e -- the envelope to deallocate. 61 ** 62 ** Returns: 63 ** none. 64 ** 65 ** Side Effects: 66 ** housekeeping necessary to dispose of an envelope. 67 ** Unlocks this queue file. 68 */ 69 70 dropenvelope(e) 71 register ENVELOPE *e; 72 { 73 bool queueit = FALSE; 74 register ADDRESS *q; 75 76 #ifdef DEBUG 77 if (tTd(50, 1)) 78 { 79 printf("dropenvelope %x id=", e); 80 xputs(e->e_id); 81 printf(" flags=%o\n", e->e_flags); 82 } 83 #endif DEBUG 84 #ifdef LOG 85 if (LogLevel > 10) 86 syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d", 87 e->e_id == NULL ? "(none)" : e->e_id, 88 e->e_flags, getpid()); 89 #endif LOG 90 91 /* we must have an id to remove disk files */ 92 if (e->e_id == NULL) 93 return; 94 95 /* 96 ** Extract state information from dregs of send list. 97 */ 98 99 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 100 { 101 if (bitset(QQUEUEUP, q->q_flags)) 102 queueit = TRUE; 103 } 104 105 /* 106 ** Send back return receipts as requested. 107 */ 108 109 if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 110 { 111 auto ADDRESS *rlist; 112 113 sendtolist(CurEnv->e_receiptto, (ADDRESS *) NULL, &rlist); 114 (void) returntosender("Return receipt", rlist, FALSE); 115 } 116 117 /* 118 ** Arrange to send error messages if there are fatal errors. 119 */ 120 121 if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags)) 122 savemail(e); 123 124 /* 125 ** Instantiate or deinstantiate the queue. 126 */ 127 128 if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 129 bitset(EF_CLRQUEUE, e->e_flags)) 130 { 131 if (e->e_dfp != NULL) 132 (void) fclose(e->e_dfp); 133 if (e->e_df != NULL) 134 xunlink(e->e_df); 135 xunlink(queuename(e, 'q')); 136 } 137 else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 138 queueup(e, FALSE, FALSE); 139 140 /* now unlock the job */ 141 closexscript(e); 142 unlockqueue(e); 143 144 /* make sure that this envelope is marked unused */ 145 e->e_id = e->e_df = NULL; 146 e->e_dfp = NULL; 147 } 148 /* 149 ** CLEARENVELOPE -- clear an envelope without unlocking 150 ** 151 ** This is normally used by a child process to get a clean 152 ** envelope without disturbing the parent. 153 ** 154 ** Parameters: 155 ** e -- the envelope to clear. 156 ** 157 ** Returns: 158 ** none. 159 ** 160 ** Side Effects: 161 ** Closes files associated with the envelope. 162 ** Marks the envelope as unallocated. 163 */ 164 165 clearenvelope(e) 166 register ENVELOPE *e; 167 { 168 /* clear out any file information */ 169 if (e->e_xfp != NULL) 170 (void) fclose(e->e_xfp); 171 if (e->e_dfp != NULL) 172 (void) fclose(e->e_dfp); 173 e->e_xfp = e->e_dfp = NULL; 174 175 /* now expunge names of objects */ 176 e->e_df = e->e_id = NULL; 177 178 /* and the flags which are now meaningless */ 179 e->e_flags = 0; 180 } 181 /* 182 ** UNLOCKQUEUE -- unlock the queue entry for a specified envelope 183 ** 184 ** Parameters: 185 ** e -- the envelope to unlock. 186 ** 187 ** Returns: 188 ** none 189 ** 190 ** Side Effects: 191 ** unlocks the queue for `e'. 192 */ 193 194 unlockqueue(e) 195 ENVELOPE *e; 196 { 197 /* remove the transcript */ 198 #ifdef DEBUG 199 # ifdef LOG 200 if (LogLevel > 19) 201 syslog(LOG_DEBUG, "%s: unlock", e->e_id); 202 # endif LOG 203 if (!tTd(51, 4)) 204 #endif DEBUG 205 xunlink(queuename(e, 'x')); 206 207 /* last but not least, remove the lock */ 208 xunlink(queuename(e, 'l')); 209 } 210 /* 211 ** INITSYS -- initialize instantiation of system 212 ** 213 ** In Daemon mode, this is done in the child. 214 ** 215 ** Parameters: 216 ** none. 217 ** 218 ** Returns: 219 ** none. 220 ** 221 ** Side Effects: 222 ** Initializes the system macros, some global variables, 223 ** etc. In particular, the current time in various 224 ** forms is set. 225 */ 226 227 initsys() 228 { 229 auto time_t now; 230 static char cbuf[5]; /* holds hop count */ 231 static char dbuf[30]; /* holds ctime(tbuf) */ 232 static char pbuf[10]; /* holds pid */ 233 static char tbuf[20]; /* holds "current" time */ 234 static char ybuf[10]; /* holds tty id */ 235 register char *p; 236 register struct tm *tm; 237 extern char *ttyname(); 238 extern char *arpadate(); 239 extern struct tm *gmtime(); 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(CurEnv); 249 CurEnv->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) 259 OutChannel = CurEnv->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, CurEnv); 268 269 /* hop count */ 270 (void) sprintf(cbuf, "%d", CurEnv->e_hopcount); 271 define('c', cbuf, CurEnv); 272 273 /* time as integer, unix time, arpa time */ 274 now = curtime(); 275 tm = gmtime(&now); 276 (void) sprintf(tbuf, "%02d%02d%02d%02d%02d", tm->tm_year, tm->tm_mon+1, 277 tm->tm_mday, tm->tm_hour, tm->tm_min); 278 define('t', tbuf, CurEnv); 279 (void) strcpy(dbuf, ctime(&now)); 280 *index(dbuf, '\n') = '\0'; 281 if (macvalue('d', CurEnv) == NULL) 282 define('d', dbuf, CurEnv); 283 p = newstr(arpadate(dbuf)); 284 if (macvalue('a', CurEnv) == NULL) 285 define('a', p, CurEnv); 286 define('b', p, CurEnv); 287 288 /* tty name */ 289 if (macvalue('y', CurEnv) == NULL) 290 { 291 p = ttyname(2); 292 if (p != NULL) 293 { 294 if (rindex(p, '/') != NULL) 295 p = rindex(p, '/') + 1; 296 (void) strcpy(ybuf, p); 297 define('y', ybuf, CurEnv); 298 } 299 } 300 } 301 /* 302 ** QUEUENAME -- build a file name in the queue directory for this envelope. 303 ** 304 ** Assigns an id code if one does not already exist. 305 ** This code is very careful to avoid trashing existing files 306 ** under any circumstances. 307 ** We first create an nf file that is only used when 308 ** assigning an id. This file is always empty, so that 309 ** we can never accidently truncate an lf file. 310 ** 311 ** Parameters: 312 ** e -- envelope to build it in/from. 313 ** type -- the file type, used as the first character 314 ** of the file name. 315 ** 316 ** Returns: 317 ** a pointer to the new file name (in a static buffer). 318 ** 319 ** Side Effects: 320 ** Will create the lf and qf files if no id code is 321 ** already assigned. This will cause the envelope 322 ** to be modified. 323 */ 324 325 char * 326 queuename(e, type) 327 register ENVELOPE *e; 328 char type; 329 { 330 static char buf[MAXNAME]; 331 static int pid = -1; 332 char c1 = 'A'; 333 char c2 = 'A'; 334 335 if (e->e_id == NULL) 336 { 337 char qf[20]; 338 char lf[20]; 339 char nf[20]; 340 341 /* find a unique id */ 342 if (pid != getpid()) 343 { 344 /* new process -- start back at "AA" */ 345 pid = getpid(); 346 c1 = 'A'; 347 c2 = 'A' - 1; 348 } 349 (void) sprintf(qf, "qfAA%05d", pid); 350 strcpy(lf, qf); 351 lf[0] = 'l'; 352 strcpy(nf, qf); 353 nf[0] = 'n'; 354 355 while (c1 < '~' || c2 < 'Z') 356 { 357 int i; 358 359 if (c2 >= 'Z') 360 { 361 c1++; 362 c2 = 'A' - 1; 363 } 364 qf[2] = lf[2] = nf[2] = c1; 365 qf[3] = lf[3] = nf[3] = ++c2; 366 # ifdef DEBUG 367 if (tTd(7, 20)) 368 printf("queuename: trying \"%s\"\n", nf); 369 # endif DEBUG 370 if (access(lf, 0) >= 0 || access(qf, 0) >= 0) 371 continue; 372 errno = 0; 373 i = creat(nf, FileMode); 374 if (i < 0) 375 { 376 (void) unlink(nf); /* kernel bug */ 377 continue; 378 } 379 (void) close(i); 380 i = link(nf, lf); 381 (void) unlink(nf); 382 if (i < 0) 383 continue; 384 if (link(lf, qf) >= 0) 385 break; 386 (void) unlink(lf); 387 } 388 if (c1 >= '~' && c2 >= 'Z') 389 { 390 syserr("queuename: Cannot create \"%s\" in \"%s\"", 391 lf, QueueDir); 392 exit(EX_OSERR); 393 } 394 e->e_id = newstr(&qf[2]); 395 define('i', e->e_id, e); 396 # ifdef DEBUG 397 if (tTd(7, 1)) 398 printf("queuename: assigned id %s, env=%x\n", e->e_id, e); 399 # ifdef LOG 400 if (LogLevel > 16) 401 syslog(LOG_DEBUG, "%s: assigned id", e->e_id); 402 # endif LOG 403 # endif DEBUG 404 } 405 406 if (type == '\0') 407 return (NULL); 408 (void) sprintf(buf, "%cf%s", type, e->e_id); 409 # ifdef DEBUG 410 if (tTd(7, 2)) 411 printf("queuename: %s\n", buf); 412 # endif DEBUG 413 return (buf); 414 } 415 /* 416 ** OPENXSCRIPT -- Open transcript file 417 ** 418 ** Creates a transcript file for possible eventual mailing or 419 ** sending back. 420 ** 421 ** Parameters: 422 ** e -- the envelope to create the transcript in/for. 423 ** 424 ** Returns: 425 ** none 426 ** 427 ** Side Effects: 428 ** Creates the transcript file. 429 */ 430 431 openxscript(e) 432 register ENVELOPE *e; 433 { 434 register char *p; 435 436 # ifdef LOG 437 if (LogLevel > 19) 438 syslog(LOG_DEBUG, "%s: openx%s", e->e_id, e->e_xfp == NULL ? "" : " (no)"); 439 # endif LOG 440 if (e->e_xfp != NULL) 441 return; 442 p = queuename(e, 'x'); 443 e->e_xfp = fopen(p, "w"); 444 if (e->e_xfp == NULL) 445 syserr("Can't create %s", p); 446 else 447 (void) chmod(p, 0644); 448 } 449 /* 450 ** CLOSEXSCRIPT -- close the transcript file. 451 ** 452 ** Parameters: 453 ** e -- the envelope containing the transcript to close. 454 ** 455 ** Returns: 456 ** none. 457 ** 458 ** Side Effects: 459 ** none. 460 */ 461 462 closexscript(e) 463 register ENVELOPE *e; 464 { 465 if (e->e_xfp == NULL) 466 return; 467 (void) fclose(e->e_xfp); 468 e->e_xfp = NULL; 469 } 470 /* 471 ** SETSENDER -- set the person who this message is from 472 ** 473 ** Under certain circumstances allow the user to say who 474 ** s/he is (using -f or -r). These are: 475 ** 1. The user's uid is zero (root). 476 ** 2. The user's login name is in an approved list (typically 477 ** from a network server). 478 ** 3. The address the user is trying to claim has a 479 ** "!" character in it (since #2 doesn't do it for 480 ** us if we are dialing out for UUCP). 481 ** A better check to replace #3 would be if the 482 ** effective uid is "UUCP" -- this would require me 483 ** to rewrite getpwent to "grab" uucp as it went by, 484 ** make getname more nasty, do another passwd file 485 ** scan, or compile the UID of "UUCP" into the code, 486 ** all of which are reprehensible. 487 ** 488 ** Assuming all of these fail, we figure out something 489 ** ourselves. 490 ** 491 ** Parameters: 492 ** from -- the person we would like to believe this message 493 ** is from, as specified on the command line. 494 ** 495 ** Returns: 496 ** none. 497 ** 498 ** Side Effects: 499 ** sets sendmail's notion of who the from person is. 500 */ 501 502 setsender(from) 503 char *from; 504 { 505 register char **pvp; 506 register struct passwd *pw = NULL; 507 char *realname = NULL; 508 char buf[MAXNAME]; 509 extern char *macvalue(); 510 extern char **prescan(); 511 extern bool safefile(); 512 extern char *FullName; 513 514 # ifdef DEBUG 515 if (tTd(45, 1)) 516 printf("setsender(%s)\n", from); 517 # endif DEBUG 518 519 /* 520 ** Figure out the real user executing us. 521 ** Username can return errno != 0 on non-errors. 522 */ 523 524 if (QueueRun || OpMode == MD_SMTP || OpMode == MD_ARPAFTP) 525 realname = from; 526 if (realname == NULL || realname[0] == '\0') 527 { 528 extern char *username(); 529 530 realname = username(); 531 errno = 0; 532 } 533 if (realname == NULL || realname[0] == '\0') 534 { 535 extern struct passwd *getpwuid(); 536 537 pw = getpwuid(getruid()); 538 if (pw != NULL) 539 realname = pw->pw_name; 540 } 541 if (realname == NULL || realname[0] == '\0') 542 { 543 syserr("Who are you?"); 544 realname = "root"; 545 } 546 547 /* 548 ** Determine if this real person is allowed to alias themselves. 549 */ 550 551 if (from != NULL) 552 { 553 extern bool trusteduser(); 554 555 if (!trusteduser(realname) && 556 # ifdef DEBUG 557 (!tTd(1, 9) || getuid() != geteuid()) && 558 # endif DEBUG 559 index(from, '!') == NULL && getuid() != 0) 560 { 561 /* network sends -r regardless (why why why?) */ 562 /* syserr("%s, you cannot use the -f flag", realname); */ 563 from = NULL; 564 } 565 } 566 567 SuprErrs = TRUE; 568 if (from == NULL || parseaddr(from, &CurEnv->e_from, 1) == NULL) 569 { 570 from = newstr(realname); 571 (void) parseaddr(from, &CurEnv->e_from, 1); 572 } 573 else 574 FromFlag = TRUE; 575 CurEnv->e_from.q_flags |= QDONTSEND; 576 SuprErrs = FALSE; 577 578 if (pw == NULL && CurEnv->e_from.q_mailer == LocalMailer) 579 { 580 extern struct passwd *getpwnam(); 581 582 pw = getpwnam(CurEnv->e_from.q_user); 583 } 584 585 /* 586 ** Process passwd file entry. 587 */ 588 589 if (pw != NULL) 590 { 591 /* extract home directory */ 592 CurEnv->e_from.q_home = newstr(pw->pw_dir); 593 594 /* run user's .mailcf file */ 595 define('z', CurEnv->e_from.q_home, CurEnv); 596 expand("$z/.mailcf", buf, &buf[sizeof buf - 1], CurEnv); 597 if (safefile(buf, getruid(), S_IREAD)) 598 readcf(buf, FALSE); 599 600 /* if the user has given fullname already, don't redefine */ 601 if (FullName == NULL) 602 FullName = macvalue('x', CurEnv); 603 if (FullName[0] == '\0') 604 FullName = NULL; 605 606 /* extract full name from passwd file */ 607 if (FullName == NULL && pw->pw_gecos != NULL && 608 strcmp(pw->pw_name, CurEnv->e_from.q_user) == 0) 609 { 610 buildfname(pw->pw_gecos, CurEnv->e_from.q_user, buf); 611 if (buf[0] != '\0') 612 FullName = newstr(buf); 613 } 614 if (FullName != NULL) 615 define('x', FullName, CurEnv); 616 } 617 618 #ifndef V6 619 if (CurEnv->e_from.q_home == NULL) 620 CurEnv->e_from.q_home = getenv("HOME"); 621 #endif V6 622 CurEnv->e_from.q_uid = getuid(); 623 CurEnv->e_from.q_gid = getgid(); 624 if (CurEnv->e_from.q_uid != 0) 625 { 626 DefUid = CurEnv->e_from.q_uid; 627 DefGid = CurEnv->e_from.q_gid; 628 } 629 630 /* 631 ** Rewrite the from person to dispose of possible implicit 632 ** links in the net. 633 */ 634 635 pvp = prescan(from, '\0'); 636 if (pvp == NULL) 637 { 638 syserr("cannot prescan from (%s)", from); 639 finis(); 640 } 641 rewrite(pvp, 3); 642 rewrite(pvp, 1); 643 cataddr(pvp, buf, sizeof buf); 644 define('f', newstr(buf), CurEnv); 645 646 /* save the domain spec if this mailer wants it */ 647 if (bitnset(M_CANONICAL, CurEnv->e_from.q_mailer->m_flags)) 648 { 649 extern char **copyplist(); 650 651 while (*pvp != NULL && strcmp(*pvp, "@") != 0) 652 pvp++; 653 if (*pvp != NULL) 654 CurEnv->e_fromdomain = copyplist(pvp, TRUE); 655 } 656 } 657 /* 658 ** TRUSTEDUSER -- tell us if this user is to be trusted. 659 ** 660 ** Parameters: 661 ** user -- the user to be checked. 662 ** 663 ** Returns: 664 ** TRUE if the user is in an approved list. 665 ** FALSE otherwise. 666 ** 667 ** Side Effects: 668 ** none. 669 */ 670 671 bool 672 trusteduser(user) 673 char *user; 674 { 675 register char **ulist; 676 extern char *TrustedUsers[]; 677 678 for (ulist = TrustedUsers; *ulist != NULL; ulist++) 679 if (strcmp(*ulist, user) == 0) 680 return (TRUE); 681 return (FALSE); 682 } 683