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