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