1 /* 2 ** Sendmail 3 ** Copyright (c) 1983 Eric P. Allman 4 ** Berkeley, California 5 ** 6 ** Copyright (c) 1983 Regents of the University of California. 7 ** All rights reserved. The Berkeley software License Agreement 8 ** specifies the terms and conditions for redistribution. 9 */ 10 11 #ifndef lint 12 static char SccsId[] = "@(#)envelope.c 5.11 (Berkeley) 11/21/85"; 13 #endif not lint 14 15 #include <pwd.h> 16 #include <sys/time.h> 17 #include "sendmail.h" 18 #include <sys/stat.h> 19 20 /* 21 ** NEWENVELOPE -- allocate a new envelope 22 ** 23 ** Supports inheritance. 24 ** 25 ** Parameters: 26 ** e -- the new envelope to fill in. 27 ** 28 ** Returns: 29 ** e. 30 ** 31 ** Side Effects: 32 ** none. 33 */ 34 35 ENVELOPE * 36 newenvelope(e) 37 register ENVELOPE *e; 38 { 39 register ENVELOPE *parent; 40 extern putheader(), putbody(); 41 42 parent = CurEnv; 43 if (e == CurEnv) 44 parent = e->e_parent; 45 clearenvelope(e); 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 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 80 #ifdef DEBUG 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 #endif DEBUG 88 #ifdef LOG 89 if (LogLevel > 10) 90 syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d", 91 e->e_id == NULL ? "(none)" : e->e_id, 92 e->e_flags, getpid()); 93 #endif LOG 94 95 /* we must have an id to remove disk files */ 96 if (e->e_id == NULL) 97 return; 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 sendtolist(CurEnv->e_receiptto, (ADDRESS *) NULL, &rlist); 118 (void) returntosender("Return receipt", rlist, FALSE); 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("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 e->e_id = e->e_df = NULL; 154 if (e->e_dfp != NULL) 155 (void) fclose(e->e_dfp); 156 e->e_dfp = NULL; 157 } 158 /* 159 ** CLEARENVELOPE -- clear an envelope without unlocking 160 ** 161 ** This is normally used by a child process to get a clean 162 ** envelope without disturbing the parent. 163 ** 164 ** Parameters: 165 ** e -- the envelope to clear. 166 ** 167 ** Returns: 168 ** none. 169 ** 170 ** Side Effects: 171 ** Closes files associated with the envelope. 172 ** Marks the envelope as unallocated. 173 */ 174 175 clearenvelope(e) 176 register ENVELOPE *e; 177 { 178 register HDR *bh; 179 register HDR **nhp; 180 extern ENVELOPE BlankEnvelope; 181 182 /* clear out any file information */ 183 if (e->e_xfp != NULL) 184 (void) fclose(e->e_xfp); 185 if (e->e_dfp != NULL) 186 (void) fclose(e->e_dfp); 187 188 /* now clear out the data */ 189 STRUCTCOPY(BlankEnvelope, *e); 190 bh = BlankEnvelope.e_header; 191 nhp = &e->e_header; 192 while (bh != NULL) 193 { 194 *nhp = (HDR *) xalloc(sizeof *bh); 195 bcopy((char *) bh, (char *) *nhp, sizeof *bh); 196 bh = bh->h_link; 197 nhp = &(*nhp)->h_link; 198 } 199 } 200 /* 201 ** INITSYS -- initialize instantiation of system 202 ** 203 ** In Daemon mode, this is done in the child. 204 ** 205 ** Parameters: 206 ** none. 207 ** 208 ** Returns: 209 ** none. 210 ** 211 ** Side Effects: 212 ** Initializes the system macros, some global variables, 213 ** etc. In particular, the current time in various 214 ** forms is set. 215 */ 216 217 initsys() 218 { 219 static char cbuf[5]; /* holds hop count */ 220 static char pbuf[10]; /* holds pid */ 221 #ifdef TTYNAME 222 static char ybuf[10]; /* holds tty id */ 223 register char *p; 224 #endif TTYNAME 225 extern char *ttyname(); 226 extern char *macvalue(); 227 extern char Version[]; 228 229 /* 230 ** Give this envelope a reality. 231 ** I.e., an id, a transcript, and a creation time. 232 */ 233 234 openxscript(CurEnv); 235 CurEnv->e_ctime = curtime(); 236 237 /* 238 ** Set OutChannel to something useful if stdout isn't it. 239 ** This arranges that any extra stuff the mailer produces 240 ** gets sent back to the user on error (because it is 241 ** tucked away in the transcript). 242 */ 243 244 if (OpMode == MD_DAEMON && QueueRun) 245 OutChannel = CurEnv->e_xfp; 246 247 /* 248 ** Set up some basic system macros. 249 */ 250 251 /* process id */ 252 (void) sprintf(pbuf, "%d", getpid()); 253 define('p', pbuf, CurEnv); 254 255 /* hop count */ 256 (void) sprintf(cbuf, "%d", CurEnv->e_hopcount); 257 define('c', cbuf, CurEnv); 258 259 /* time as integer, unix time, arpa time */ 260 settime(); 261 262 #ifdef TTYNAME 263 /* tty name */ 264 if (macvalue('y', CurEnv) == NULL) 265 { 266 p = ttyname(2); 267 if (p != NULL) 268 { 269 if (rindex(p, '/') != NULL) 270 p = rindex(p, '/') + 1; 271 (void) strcpy(ybuf, p); 272 define('y', ybuf, CurEnv); 273 } 274 } 275 #endif TTYNAME 276 } 277 /* 278 ** SETTIME -- set the current time. 279 ** 280 ** Parameters: 281 ** none. 282 ** 283 ** Returns: 284 ** none. 285 ** 286 ** Side Effects: 287 ** Sets the various time macros -- $a, $b, $d, $t. 288 */ 289 290 settime() 291 { 292 register char *p; 293 auto time_t now; 294 static char tbuf[20]; /* holds "current" time */ 295 static char dbuf[30]; /* holds ctime(tbuf) */ 296 register struct tm *tm; 297 extern char *arpadate(); 298 extern struct tm *gmtime(); 299 extern char *macvalue(); 300 301 now = curtime(); 302 tm = gmtime(&now); 303 (void) sprintf(tbuf, "%02d%02d%02d%02d%02d", tm->tm_year, tm->tm_mon+1, 304 tm->tm_mday, tm->tm_hour, tm->tm_min); 305 define('t', tbuf, CurEnv); 306 (void) strcpy(dbuf, ctime(&now)); 307 *index(dbuf, '\n') = '\0'; 308 if (macvalue('d', CurEnv) == NULL) 309 define('d', dbuf, CurEnv); 310 p = newstr(arpadate(dbuf)); 311 if (macvalue('a', CurEnv) == NULL) 312 define('a', p, CurEnv); 313 define('b', p, CurEnv); 314 } 315 /* 316 ** OPENXSCRIPT -- Open transcript file 317 ** 318 ** Creates a transcript file for possible eventual mailing or 319 ** sending back. 320 ** 321 ** Parameters: 322 ** e -- the envelope to create the transcript in/for. 323 ** 324 ** Returns: 325 ** none 326 ** 327 ** Side Effects: 328 ** Creates the transcript file. 329 */ 330 331 openxscript(e) 332 register ENVELOPE *e; 333 { 334 register char *p; 335 336 # ifdef LOG 337 if (LogLevel > 19) 338 syslog(LOG_DEBUG, "%s: openx%s", e->e_id, e->e_xfp == NULL ? "" : " (no)"); 339 # endif LOG 340 if (e->e_xfp != NULL) 341 return; 342 p = queuename(e, 'x'); 343 e->e_xfp = fopen(p, "w"); 344 if (e->e_xfp == NULL) 345 syserr("Can't create %s", p); 346 else 347 (void) chmod(p, 0644); 348 } 349 /* 350 ** CLOSEXSCRIPT -- close the transcript file. 351 ** 352 ** Parameters: 353 ** e -- the envelope containing the transcript to close. 354 ** 355 ** Returns: 356 ** none. 357 ** 358 ** Side Effects: 359 ** none. 360 */ 361 362 closexscript(e) 363 register ENVELOPE *e; 364 { 365 if (e->e_xfp == NULL) 366 return; 367 (void) fclose(e->e_xfp); 368 e->e_xfp = NULL; 369 } 370 /* 371 ** SETSENDER -- set the person who this message is from 372 ** 373 ** Under certain circumstances allow the user to say who 374 ** s/he is (using -f or -r). These are: 375 ** 1. The user's uid is zero (root). 376 ** 2. The user's login name is in an approved list (typically 377 ** from a network server). 378 ** 3. The address the user is trying to claim has a 379 ** "!" character in it (since #2 doesn't do it for 380 ** us if we are dialing out for UUCP). 381 ** A better check to replace #3 would be if the 382 ** effective uid is "UUCP" -- this would require me 383 ** to rewrite getpwent to "grab" uucp as it went by, 384 ** make getname more nasty, do another passwd file 385 ** scan, or compile the UID of "UUCP" into the code, 386 ** all of which are reprehensible. 387 ** 388 ** Assuming all of these fail, we figure out something 389 ** ourselves. 390 ** 391 ** Parameters: 392 ** from -- the person we would like to believe this message 393 ** is from, as specified on the command line. 394 ** 395 ** Returns: 396 ** none. 397 ** 398 ** Side Effects: 399 ** sets sendmail's notion of who the from person is. 400 */ 401 402 setsender(from) 403 char *from; 404 { 405 register char **pvp; 406 char *realname = NULL; 407 register struct passwd *pw; 408 char buf[MAXNAME]; 409 char pvpbuf[PSBUFSIZE]; 410 extern struct passwd *getpwnam(); 411 extern char *macvalue(); 412 extern char **prescan(); 413 extern bool safefile(); 414 extern char *FullName; 415 416 # ifdef DEBUG 417 if (tTd(45, 1)) 418 printf("setsender(%s)\n", from == NULL ? "" : from); 419 # endif DEBUG 420 421 /* 422 ** Figure out the real user executing us. 423 ** Username can return errno != 0 on non-errors. 424 */ 425 426 if (QueueRun || OpMode == MD_SMTP || OpMode == MD_ARPAFTP) 427 realname = from; 428 if (realname == NULL || realname[0] == '\0') 429 { 430 extern char *username(); 431 432 realname = username(); 433 } 434 435 /* 436 ** Determine if this real person is allowed to alias themselves. 437 */ 438 439 if (from != NULL) 440 { 441 extern bool trusteduser(); 442 443 if (!trusteduser(realname) && 444 # ifdef DEBUG 445 (!tTd(1, 9) || getuid() != geteuid()) && 446 # endif DEBUG 447 index(from, '!') == NULL && getuid() != 0) 448 { 449 /* network sends -r regardless (why why why?) */ 450 /* syserr("%s, you cannot use the -f flag", realname); */ 451 from = NULL; 452 } 453 } 454 455 SuprErrs = TRUE; 456 if (from == NULL || parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL) 457 { 458 /* log garbage addresses for traceback */ 459 if (from != NULL) 460 { 461 # ifdef LOG 462 if (LogLevel >= 1) 463 syslog(LOG_ERR, "Unparseable user %s wants to be %s", 464 realname, from); 465 # endif LOG 466 } 467 from = newstr(realname); 468 if (parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL && 469 parseaddr("postmaster", &CurEnv->e_from, 1, '\0') == NULL) 470 { 471 syserr("setsender: can't even parse postmaster!"); 472 } 473 } 474 else 475 FromFlag = TRUE; 476 CurEnv->e_from.q_flags |= QDONTSEND; 477 loweraddr(&CurEnv->e_from); 478 SuprErrs = FALSE; 479 480 if (CurEnv->e_from.q_mailer == LocalMailer && 481 (pw = getpwnam(CurEnv->e_from.q_user)) != NULL) 482 { 483 /* 484 ** Process passwd file entry. 485 */ 486 487 488 /* extract home directory */ 489 CurEnv->e_from.q_home = newstr(pw->pw_dir); 490 define('z', CurEnv->e_from.q_home, CurEnv); 491 492 /* extract user and group id */ 493 CurEnv->e_from.q_uid = pw->pw_uid; 494 CurEnv->e_from.q_gid = pw->pw_gid; 495 496 /* if the user has given fullname already, don't redefine */ 497 if (FullName == NULL) 498 FullName = macvalue('x', CurEnv); 499 if (FullName != NULL && FullName[0] == '\0') 500 FullName = NULL; 501 502 /* extract full name from passwd file */ 503 if (FullName == NULL && pw->pw_gecos != NULL && 504 strcmp(pw->pw_name, CurEnv->e_from.q_user) == 0) 505 { 506 buildfname(pw->pw_gecos, CurEnv->e_from.q_user, buf); 507 if (buf[0] != '\0') 508 FullName = newstr(buf); 509 } 510 if (FullName != NULL) 511 define('x', FullName, CurEnv); 512 } 513 else 514 { 515 #ifndef V6 516 if (CurEnv->e_from.q_home == NULL) 517 CurEnv->e_from.q_home = getenv("HOME"); 518 #endif V6 519 CurEnv->e_from.q_uid = getuid(); 520 CurEnv->e_from.q_gid = getgid(); 521 } 522 523 if (CurEnv->e_from.q_uid != 0) 524 { 525 DefUid = CurEnv->e_from.q_uid; 526 DefGid = CurEnv->e_from.q_gid; 527 } 528 529 /* 530 ** Rewrite the from person to dispose of possible implicit 531 ** links in the net. 532 */ 533 534 pvp = prescan(from, '\0', pvpbuf); 535 if (pvp == NULL) 536 { 537 syserr("cannot prescan from (%s)", from); 538 finis(); 539 } 540 rewrite(pvp, 3); 541 rewrite(pvp, 1); 542 rewrite(pvp, 4); 543 cataddr(pvp, buf, sizeof buf); 544 define('f', newstr(buf), CurEnv); 545 546 /* save the domain spec if this mailer wants it */ 547 if (CurEnv->e_from.q_mailer != NULL && 548 bitnset(M_CANONICAL, CurEnv->e_from.q_mailer->m_flags)) 549 { 550 extern char **copyplist(); 551 552 while (*pvp != NULL && strcmp(*pvp, "@") != 0) 553 pvp++; 554 if (*pvp != NULL) 555 CurEnv->e_fromdomain = copyplist(pvp, TRUE); 556 } 557 } 558 /* 559 ** TRUSTEDUSER -- tell us if this user is to be trusted. 560 ** 561 ** Parameters: 562 ** user -- the user to be checked. 563 ** 564 ** Returns: 565 ** TRUE if the user is in an approved list. 566 ** FALSE otherwise. 567 ** 568 ** Side Effects: 569 ** none. 570 */ 571 572 bool 573 trusteduser(user) 574 char *user; 575 { 576 register char **ulist; 577 extern char *TrustedUsers[]; 578 579 for (ulist = TrustedUsers; *ulist != NULL; ulist++) 580 if (strcmp(*ulist, user) == 0) 581 return (TRUE); 582 return (FALSE); 583 } 584