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.2 (Berkeley) 06/07/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 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_puthdr = putheader; 53 e->e_putbody = putbody; 54 bh = BlankEnvelope.e_header; 55 nhp = &e->e_header; 56 while (bh != NULL) 57 { 58 *nhp = (HDR *) xalloc(sizeof *bh); 59 bcopy((char *) bh, (char *) *nhp, sizeof *bh); 60 bh = bh->h_link; 61 nhp = &(*nhp)->h_link; 62 } 63 if (CurEnv->e_xfp != NULL) 64 (void) fflush(CurEnv->e_xfp); 65 66 return (e); 67 } 68 /* 69 ** DROPENVELOPE -- deallocate an envelope. 70 ** 71 ** Parameters: 72 ** e -- the envelope to deallocate. 73 ** 74 ** Returns: 75 ** none. 76 ** 77 ** Side Effects: 78 ** housekeeping necessary to dispose of an envelope. 79 ** Unlocks this queue file. 80 */ 81 82 dropenvelope(e) 83 register ENVELOPE *e; 84 { 85 bool queueit = FALSE; 86 register ADDRESS *q; 87 88 #ifdef DEBUG 89 if (tTd(50, 1)) 90 { 91 printf("dropenvelope %x id=", e); 92 xputs(e->e_id); 93 printf(" flags=%o\n", e->e_flags); 94 } 95 #endif DEBUG 96 #ifdef LOG 97 if (LogLevel > 10) 98 syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d", 99 e->e_id == NULL ? "(none)" : e->e_id, 100 e->e_flags, getpid()); 101 #endif LOG 102 103 /* we must have an id to remove disk files */ 104 if (e->e_id == NULL) 105 return; 106 107 /* 108 ** Extract state information from dregs of send list. 109 */ 110 111 for (q = e->e_sendqueue; q != NULL; q = q->q_next) 112 { 113 if (bitset(QQUEUEUP, q->q_flags)) 114 queueit = TRUE; 115 } 116 117 /* 118 ** Send back return receipts as requested. 119 */ 120 121 if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 122 { 123 auto ADDRESS *rlist = NULL; 124 125 sendtolist(CurEnv->e_receiptto, (ADDRESS *) NULL, &rlist); 126 (void) returntosender("Return receipt", rlist, FALSE); 127 } 128 129 /* 130 ** Arrange to send error messages if there are fatal errors. 131 */ 132 133 if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && ErrorMode != EM_QUIET) 134 savemail(e); 135 136 /* 137 ** Instantiate or deinstantiate the queue. 138 */ 139 140 if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 141 bitset(EF_CLRQUEUE, e->e_flags)) 142 { 143 if (e->e_dfp != NULL) 144 (void) fclose(e->e_dfp); 145 xunlink(queuename(e, 'q')); 146 } 147 else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 148 { 149 #ifdef QUEUE 150 queueup(e, FALSE, FALSE); 151 #else QUEUE 152 syserr("dropenvelope: queueup"); 153 #endif QUEUE 154 } 155 156 /* now unlock the job */ 157 closexscript(e); 158 unlockqueue(e); 159 160 /* make sure that this envelope is marked unused */ 161 if (e->e_df != NULL) 162 xunlink(e->e_df); 163 e->e_id = e->e_df = NULL; 164 e->e_dfp = NULL; 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 ** 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) 184 register ENVELOPE *e; 185 { 186 /* clear out any file information */ 187 if (e->e_xfp != NULL) 188 (void) fclose(e->e_xfp); 189 if (e->e_dfp != NULL) 190 (void) fclose(e->e_dfp); 191 e->e_xfp = e->e_dfp = NULL; 192 193 /* now expunge names of objects */ 194 e->e_df = e->e_id = NULL; 195 196 /* and the flags which are now meaningless */ 197 e->e_flags = 0; 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 syslog(LOG_ERR, "Unparseable user %s wants to be %s", 461 realname, from); 462 } 463 from = newstr(realname); 464 (void) parseaddr(from, &CurEnv->e_from, 1, '\0'); 465 } 466 else 467 FromFlag = TRUE; 468 CurEnv->e_from.q_flags |= QDONTSEND; 469 loweraddr(&CurEnv->e_from); 470 SuprErrs = FALSE; 471 472 if (CurEnv->e_from.q_mailer == LocalMailer && 473 (pw = getpwnam(CurEnv->e_from.q_user)) != NULL) 474 { 475 /* 476 ** Process passwd file entry. 477 */ 478 479 480 /* extract home directory */ 481 CurEnv->e_from.q_home = newstr(pw->pw_dir); 482 define('z', CurEnv->e_from.q_home, CurEnv); 483 484 /* extract user and group id */ 485 CurEnv->e_from.q_uid = pw->pw_uid; 486 CurEnv->e_from.q_gid = pw->pw_gid; 487 488 /* if the user has given fullname already, don't redefine */ 489 if (FullName == NULL) 490 FullName = macvalue('x', CurEnv); 491 if (FullName != NULL && FullName[0] == '\0') 492 FullName = NULL; 493 494 /* extract full name from passwd file */ 495 if (FullName == NULL && pw->pw_gecos != NULL && 496 strcmp(pw->pw_name, CurEnv->e_from.q_user) == 0) 497 { 498 buildfname(pw->pw_gecos, CurEnv->e_from.q_user, buf); 499 if (buf[0] != '\0') 500 FullName = newstr(buf); 501 } 502 if (FullName != NULL) 503 define('x', FullName, CurEnv); 504 } 505 else 506 { 507 #ifndef V6 508 if (CurEnv->e_from.q_home == NULL) 509 CurEnv->e_from.q_home = getenv("HOME"); 510 #endif V6 511 CurEnv->e_from.q_uid = getuid(); 512 CurEnv->e_from.q_gid = getgid(); 513 } 514 515 if (CurEnv->e_from.q_uid != 0) 516 { 517 DefUid = CurEnv->e_from.q_uid; 518 DefGid = CurEnv->e_from.q_gid; 519 } 520 521 /* 522 ** Rewrite the from person to dispose of possible implicit 523 ** links in the net. 524 */ 525 526 pvp = prescan(from, '\0', pvpbuf); 527 if (pvp == NULL) 528 { 529 syserr("cannot prescan from (%s)", from); 530 finis(); 531 } 532 rewrite(pvp, 3); 533 rewrite(pvp, 1); 534 rewrite(pvp, 4); 535 cataddr(pvp, buf, sizeof buf); 536 define('f', newstr(buf), CurEnv); 537 538 /* save the domain spec if this mailer wants it */ 539 if (bitnset(M_CANONICAL, CurEnv->e_from.q_mailer->m_flags)) 540 { 541 extern char **copyplist(); 542 543 while (*pvp != NULL && strcmp(*pvp, "@") != 0) 544 pvp++; 545 if (*pvp != NULL) 546 CurEnv->e_fromdomain = copyplist(pvp, TRUE); 547 } 548 } 549 /* 550 ** TRUSTEDUSER -- tell us if this user is to be trusted. 551 ** 552 ** Parameters: 553 ** user -- the user to be checked. 554 ** 555 ** Returns: 556 ** TRUE if the user is in an approved list. 557 ** FALSE otherwise. 558 ** 559 ** Side Effects: 560 ** none. 561 */ 562 563 bool 564 trusteduser(user) 565 char *user; 566 { 567 register char **ulist; 568 extern char *TrustedUsers[]; 569 570 for (ulist = TrustedUsers; *ulist != NULL; ulist++) 571 if (strcmp(*ulist, user) == 0) 572 return (TRUE); 573 return (FALSE); 574 } 575