122704Sdist /* 222704Sdist ** Sendmail 322704Sdist ** Copyright (c) 1983 Eric P. Allman 422704Sdist ** Berkeley, California 522704Sdist ** 622704Sdist ** Copyright (c) 1983 Regents of the University of California. 722704Sdist ** All rights reserved. The Berkeley software License Agreement 822704Sdist ** specifies the terms and conditions for redistribution. 922704Sdist */ 1022704Sdist 1122704Sdist #ifndef lint 12*25611Seric static char SccsId[] = "@(#)envelope.c 5.12 (Berkeley) 12/17/85"; 1322704Sdist #endif not lint 1422704Sdist 159536Seric #include <pwd.h> 1613587Swnj #include <sys/time.h> 179536Seric #include "sendmail.h" 189536Seric #include <sys/stat.h> 199536Seric 209536Seric /* 219536Seric ** NEWENVELOPE -- allocate a new envelope 229536Seric ** 239536Seric ** Supports inheritance. 249536Seric ** 259536Seric ** Parameters: 269536Seric ** e -- the new envelope to fill in. 279536Seric ** 289536Seric ** Returns: 299536Seric ** e. 309536Seric ** 319536Seric ** Side Effects: 329536Seric ** none. 339536Seric */ 349536Seric 359536Seric ENVELOPE * 369536Seric newenvelope(e) 379536Seric register ENVELOPE *e; 389536Seric { 399536Seric register ENVELOPE *parent; 409536Seric extern putheader(), putbody(); 41*25611Seric extern ENVELOPE BlankEnvelope; 429536Seric 439536Seric parent = CurEnv; 449536Seric if (e == CurEnv) 459536Seric parent = e->e_parent; 46*25611Seric clearenvelope(e, TRUE); 4724944Seric if (e == CurEnv) 4824944Seric bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from); 4924944Seric else 5024944Seric bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from); 519536Seric e->e_parent = parent; 529536Seric e->e_ctime = curtime(); 5325014Seric e->e_msgpriority = parent->e_msgsize; 549536Seric e->e_puthdr = putheader; 559536Seric e->e_putbody = putbody; 569536Seric if (CurEnv->e_xfp != NULL) 579536Seric (void) fflush(CurEnv->e_xfp); 589536Seric 599536Seric return (e); 609536Seric } 619536Seric /* 629536Seric ** DROPENVELOPE -- deallocate an envelope. 639536Seric ** 649536Seric ** Parameters: 659536Seric ** e -- the envelope to deallocate. 669536Seric ** 679536Seric ** Returns: 689536Seric ** none. 699536Seric ** 709536Seric ** Side Effects: 719536Seric ** housekeeping necessary to dispose of an envelope. 729536Seric ** Unlocks this queue file. 739536Seric */ 749536Seric 759536Seric dropenvelope(e) 769536Seric register ENVELOPE *e; 779536Seric { 789536Seric bool queueit = FALSE; 799536Seric register ADDRESS *q; 809536Seric 819536Seric #ifdef DEBUG 829536Seric if (tTd(50, 1)) 839536Seric { 849536Seric printf("dropenvelope %x id=", e); 859536Seric xputs(e->e_id); 869536Seric printf(" flags=%o\n", e->e_flags); 879536Seric } 889536Seric #endif DEBUG 899536Seric #ifdef LOG 909536Seric if (LogLevel > 10) 919536Seric syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d", 929536Seric e->e_id == NULL ? "(none)" : e->e_id, 939536Seric e->e_flags, getpid()); 949536Seric #endif LOG 959536Seric 969536Seric /* we must have an id to remove disk files */ 979536Seric if (e->e_id == NULL) 989536Seric return; 999536Seric 1009536Seric /* 1019536Seric ** Extract state information from dregs of send list. 1029536Seric */ 1039536Seric 1049536Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1059536Seric { 1069536Seric if (bitset(QQUEUEUP, q->q_flags)) 1079536Seric queueit = TRUE; 1089536Seric } 1099536Seric 1109536Seric /* 1119536Seric ** Send back return receipts as requested. 1129536Seric */ 1139536Seric 1149536Seric if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 1159536Seric { 11610844Seric auto ADDRESS *rlist = NULL; 1179536Seric 1189621Seric sendtolist(CurEnv->e_receiptto, (ADDRESS *) NULL, &rlist); 1199536Seric (void) returntosender("Return receipt", rlist, FALSE); 1209536Seric } 1219536Seric 1229536Seric /* 1239536Seric ** Arrange to send error messages if there are fatal errors. 1249536Seric */ 1259536Seric 12610754Seric if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && ErrorMode != EM_QUIET) 1279536Seric savemail(e); 1289536Seric 1299536Seric /* 1309536Seric ** Instantiate or deinstantiate the queue. 1319536Seric */ 1329536Seric 1339536Seric if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 1349536Seric bitset(EF_CLRQUEUE, e->e_flags)) 1359536Seric { 13623497Seric if (e->e_df != NULL) 13723497Seric xunlink(e->e_df); 1389536Seric xunlink(queuename(e, 'q')); 1399536Seric } 1409536Seric else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 14110754Seric { 14210754Seric #ifdef QUEUE 1439536Seric queueup(e, FALSE, FALSE); 14410754Seric #else QUEUE 14510754Seric syserr("dropenvelope: queueup"); 14610754Seric #endif QUEUE 14710754Seric } 1489536Seric 1499536Seric /* now unlock the job */ 15010196Seric closexscript(e); 1519536Seric unlockqueue(e); 1529536Seric 1539536Seric /* make sure that this envelope is marked unused */ 1549536Seric e->e_id = e->e_df = NULL; 15524944Seric if (e->e_dfp != NULL) 15624944Seric (void) fclose(e->e_dfp); 15710196Seric e->e_dfp = NULL; 1589536Seric } 1599536Seric /* 1609536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 1619536Seric ** 1629536Seric ** This is normally used by a child process to get a clean 1639536Seric ** envelope without disturbing the parent. 1649536Seric ** 1659536Seric ** Parameters: 1669536Seric ** e -- the envelope to clear. 167*25611Seric ** fullclear - if set, the current envelope is total 168*25611Seric ** garbage and should be ignored; otherwise, 169*25611Seric ** release any resources it may indicate. 1709536Seric ** 1719536Seric ** Returns: 1729536Seric ** none. 1739536Seric ** 1749536Seric ** Side Effects: 1759536Seric ** Closes files associated with the envelope. 1769536Seric ** Marks the envelope as unallocated. 1779536Seric */ 1789536Seric 179*25611Seric clearenvelope(e, fullclear) 1809536Seric register ENVELOPE *e; 181*25611Seric bool fullclear; 1829536Seric { 18325514Seric register HDR *bh; 18425514Seric register HDR **nhp; 18525514Seric extern ENVELOPE BlankEnvelope; 18625514Seric 187*25611Seric if (!fullclear) 188*25611Seric { 189*25611Seric /* clear out any file information */ 190*25611Seric if (e->e_xfp != NULL) 191*25611Seric (void) fclose(e->e_xfp); 192*25611Seric if (e->e_dfp != NULL) 193*25611Seric (void) fclose(e->e_dfp); 194*25611Seric } 1959536Seric 19624961Seric /* now clear out the data */ 19724965Seric STRUCTCOPY(BlankEnvelope, *e); 19825514Seric bh = BlankEnvelope.e_header; 19925514Seric nhp = &e->e_header; 20025514Seric while (bh != NULL) 20125514Seric { 20225514Seric *nhp = (HDR *) xalloc(sizeof *bh); 20325514Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 20425514Seric bh = bh->h_link; 20525514Seric nhp = &(*nhp)->h_link; 20625514Seric } 2079536Seric } 2089536Seric /* 2099536Seric ** INITSYS -- initialize instantiation of system 2109536Seric ** 2119536Seric ** In Daemon mode, this is done in the child. 2129536Seric ** 2139536Seric ** Parameters: 2149536Seric ** none. 2159536Seric ** 2169536Seric ** Returns: 2179536Seric ** none. 2189536Seric ** 2199536Seric ** Side Effects: 2209536Seric ** Initializes the system macros, some global variables, 2219536Seric ** etc. In particular, the current time in various 2229536Seric ** forms is set. 2239536Seric */ 2249536Seric 2259536Seric initsys() 2269536Seric { 2279536Seric static char cbuf[5]; /* holds hop count */ 2289536Seric static char pbuf[10]; /* holds pid */ 22922963Smiriam #ifdef TTYNAME 2309536Seric static char ybuf[10]; /* holds tty id */ 2319536Seric register char *p; 23222963Smiriam #endif TTYNAME 2339536Seric extern char *ttyname(); 2349536Seric extern char *macvalue(); 2359536Seric extern char Version[]; 2369536Seric 2379536Seric /* 2389536Seric ** Give this envelope a reality. 2399536Seric ** I.e., an id, a transcript, and a creation time. 2409536Seric */ 2419536Seric 2429536Seric openxscript(CurEnv); 2439536Seric CurEnv->e_ctime = curtime(); 2449536Seric 2459536Seric /* 2469536Seric ** Set OutChannel to something useful if stdout isn't it. 2479536Seric ** This arranges that any extra stuff the mailer produces 2489536Seric ** gets sent back to the user on error (because it is 2499536Seric ** tucked away in the transcript). 2509536Seric */ 2519536Seric 2529536Seric if (OpMode == MD_DAEMON && QueueRun) 2539536Seric OutChannel = CurEnv->e_xfp; 2549536Seric 2559536Seric /* 2569536Seric ** Set up some basic system macros. 2579536Seric */ 2589536Seric 2599536Seric /* process id */ 2609536Seric (void) sprintf(pbuf, "%d", getpid()); 2619536Seric define('p', pbuf, CurEnv); 2629536Seric 2639536Seric /* hop count */ 2649536Seric (void) sprintf(cbuf, "%d", CurEnv->e_hopcount); 2659536Seric define('c', cbuf, CurEnv); 2669536Seric 2679536Seric /* time as integer, unix time, arpa time */ 26811932Seric settime(); 2699536Seric 27017472Seric #ifdef TTYNAME 2719536Seric /* tty name */ 2729536Seric if (macvalue('y', CurEnv) == NULL) 2739536Seric { 2749536Seric p = ttyname(2); 2759536Seric if (p != NULL) 2769536Seric { 2779536Seric if (rindex(p, '/') != NULL) 2789536Seric p = rindex(p, '/') + 1; 2799536Seric (void) strcpy(ybuf, p); 2809536Seric define('y', ybuf, CurEnv); 2819536Seric } 2829536Seric } 28317472Seric #endif TTYNAME 2849536Seric } 2859536Seric /* 28611932Seric ** SETTIME -- set the current time. 28711932Seric ** 28811932Seric ** Parameters: 28911932Seric ** none. 29011932Seric ** 29111932Seric ** Returns: 29211932Seric ** none. 29311932Seric ** 29411932Seric ** Side Effects: 29511932Seric ** Sets the various time macros -- $a, $b, $d, $t. 29611932Seric */ 29711932Seric 29811932Seric settime() 29911932Seric { 30011932Seric register char *p; 30111932Seric auto time_t now; 30211932Seric static char tbuf[20]; /* holds "current" time */ 30311932Seric static char dbuf[30]; /* holds ctime(tbuf) */ 30411932Seric register struct tm *tm; 30511932Seric extern char *arpadate(); 30611932Seric extern struct tm *gmtime(); 30711932Seric extern char *macvalue(); 30811932Seric 30911932Seric now = curtime(); 31011932Seric tm = gmtime(&now); 31111932Seric (void) sprintf(tbuf, "%02d%02d%02d%02d%02d", tm->tm_year, tm->tm_mon+1, 31211932Seric tm->tm_mday, tm->tm_hour, tm->tm_min); 31311932Seric define('t', tbuf, CurEnv); 31411932Seric (void) strcpy(dbuf, ctime(&now)); 31511932Seric *index(dbuf, '\n') = '\0'; 31611932Seric if (macvalue('d', CurEnv) == NULL) 31711932Seric define('d', dbuf, CurEnv); 31811932Seric p = newstr(arpadate(dbuf)); 31911932Seric if (macvalue('a', CurEnv) == NULL) 32011932Seric define('a', p, CurEnv); 32111932Seric define('b', p, CurEnv); 32211932Seric } 32311932Seric /* 3249536Seric ** OPENXSCRIPT -- Open transcript file 3259536Seric ** 3269536Seric ** Creates a transcript file for possible eventual mailing or 3279536Seric ** sending back. 3289536Seric ** 3299536Seric ** Parameters: 3309536Seric ** e -- the envelope to create the transcript in/for. 3319536Seric ** 3329536Seric ** Returns: 3339536Seric ** none 3349536Seric ** 3359536Seric ** Side Effects: 3369536Seric ** Creates the transcript file. 3379536Seric */ 3389536Seric 3399536Seric openxscript(e) 3409536Seric register ENVELOPE *e; 3419536Seric { 3429536Seric register char *p; 3439536Seric 34410196Seric # ifdef LOG 34510196Seric if (LogLevel > 19) 34610196Seric syslog(LOG_DEBUG, "%s: openx%s", e->e_id, e->e_xfp == NULL ? "" : " (no)"); 34710196Seric # endif LOG 3489536Seric if (e->e_xfp != NULL) 3499536Seric return; 3509536Seric p = queuename(e, 'x'); 3519536Seric e->e_xfp = fopen(p, "w"); 3529536Seric if (e->e_xfp == NULL) 3539536Seric syserr("Can't create %s", p); 3549536Seric else 3559536Seric (void) chmod(p, 0644); 3569536Seric } 3579536Seric /* 35810196Seric ** CLOSEXSCRIPT -- close the transcript file. 35910196Seric ** 36010196Seric ** Parameters: 36110196Seric ** e -- the envelope containing the transcript to close. 36210196Seric ** 36310196Seric ** Returns: 36410196Seric ** none. 36510196Seric ** 36610196Seric ** Side Effects: 36710196Seric ** none. 36810196Seric */ 36910196Seric 37010196Seric closexscript(e) 37110196Seric register ENVELOPE *e; 37210196Seric { 37310196Seric if (e->e_xfp == NULL) 37410196Seric return; 37510196Seric (void) fclose(e->e_xfp); 37610196Seric e->e_xfp = NULL; 37710196Seric } 37810196Seric /* 3799536Seric ** SETSENDER -- set the person who this message is from 3809536Seric ** 3819536Seric ** Under certain circumstances allow the user to say who 3829536Seric ** s/he is (using -f or -r). These are: 3839536Seric ** 1. The user's uid is zero (root). 3849536Seric ** 2. The user's login name is in an approved list (typically 3859536Seric ** from a network server). 3869536Seric ** 3. The address the user is trying to claim has a 3879536Seric ** "!" character in it (since #2 doesn't do it for 3889536Seric ** us if we are dialing out for UUCP). 3899536Seric ** A better check to replace #3 would be if the 3909536Seric ** effective uid is "UUCP" -- this would require me 3919536Seric ** to rewrite getpwent to "grab" uucp as it went by, 3929536Seric ** make getname more nasty, do another passwd file 3939536Seric ** scan, or compile the UID of "UUCP" into the code, 3949536Seric ** all of which are reprehensible. 3959536Seric ** 3969536Seric ** Assuming all of these fail, we figure out something 3979536Seric ** ourselves. 3989536Seric ** 3999536Seric ** Parameters: 4009536Seric ** from -- the person we would like to believe this message 4019536Seric ** is from, as specified on the command line. 4029536Seric ** 4039536Seric ** Returns: 4049536Seric ** none. 4059536Seric ** 4069536Seric ** Side Effects: 4079536Seric ** sets sendmail's notion of who the from person is. 4089536Seric */ 4099536Seric 4109536Seric setsender(from) 4119536Seric char *from; 4129536Seric { 4139536Seric register char **pvp; 4149536Seric char *realname = NULL; 41518665Seric register struct passwd *pw; 4169536Seric char buf[MAXNAME]; 41716913Seric char pvpbuf[PSBUFSIZE]; 41818665Seric extern struct passwd *getpwnam(); 4199536Seric extern char *macvalue(); 4209536Seric extern char **prescan(); 4219536Seric extern bool safefile(); 4229536Seric extern char *FullName; 4239536Seric 4249536Seric # ifdef DEBUG 4259536Seric if (tTd(45, 1)) 42614786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 4279536Seric # endif DEBUG 4289536Seric 4299536Seric /* 4309536Seric ** Figure out the real user executing us. 4319536Seric ** Username can return errno != 0 on non-errors. 4329536Seric */ 4339536Seric 4349536Seric if (QueueRun || OpMode == MD_SMTP || OpMode == MD_ARPAFTP) 4359536Seric realname = from; 4369536Seric if (realname == NULL || realname[0] == '\0') 4379536Seric { 4389536Seric extern char *username(); 4399536Seric 4409536Seric realname = username(); 4419536Seric } 4429536Seric 4439536Seric /* 4449536Seric ** Determine if this real person is allowed to alias themselves. 4459536Seric */ 4469536Seric 4479536Seric if (from != NULL) 4489536Seric { 4499536Seric extern bool trusteduser(); 4509536Seric 4519536Seric if (!trusteduser(realname) && 4529536Seric # ifdef DEBUG 4539536Seric (!tTd(1, 9) || getuid() != geteuid()) && 4549536Seric # endif DEBUG 4559536Seric index(from, '!') == NULL && getuid() != 0) 4569536Seric { 4579536Seric /* network sends -r regardless (why why why?) */ 4589536Seric /* syserr("%s, you cannot use the -f flag", realname); */ 4599536Seric from = NULL; 4609536Seric } 4619536Seric } 4629536Seric 4639536Seric SuprErrs = TRUE; 46411447Seric if (from == NULL || parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL) 4659536Seric { 46621750Seric /* log garbage addresses for traceback */ 46721750Seric if (from != NULL) 46821750Seric { 46924944Seric # ifdef LOG 47024944Seric if (LogLevel >= 1) 47124944Seric syslog(LOG_ERR, "Unparseable user %s wants to be %s", 47224944Seric realname, from); 47324944Seric # endif LOG 47421750Seric } 4759536Seric from = newstr(realname); 47624944Seric if (parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL && 47724944Seric parseaddr("postmaster", &CurEnv->e_from, 1, '\0') == NULL) 47824944Seric { 47924944Seric syserr("setsender: can't even parse postmaster!"); 48024944Seric } 4819536Seric } 4829536Seric else 4839536Seric FromFlag = TRUE; 4849536Seric CurEnv->e_from.q_flags |= QDONTSEND; 48516162Seric loweraddr(&CurEnv->e_from); 4869536Seric SuprErrs = FALSE; 4879536Seric 48818665Seric if (CurEnv->e_from.q_mailer == LocalMailer && 48918665Seric (pw = getpwnam(CurEnv->e_from.q_user)) != NULL) 4909536Seric { 49117472Seric /* 49217472Seric ** Process passwd file entry. 49317472Seric */ 49417472Seric 4959536Seric 4969536Seric /* extract home directory */ 4979536Seric CurEnv->e_from.q_home = newstr(pw->pw_dir); 49816481Seric define('z', CurEnv->e_from.q_home, CurEnv); 4999536Seric 50011625Seric /* extract user and group id */ 50111625Seric CurEnv->e_from.q_uid = pw->pw_uid; 50211625Seric CurEnv->e_from.q_gid = pw->pw_gid; 50311625Seric 5049536Seric /* if the user has given fullname already, don't redefine */ 5059536Seric if (FullName == NULL) 5069536Seric FullName = macvalue('x', CurEnv); 50711932Seric if (FullName != NULL && FullName[0] == '\0') 5089536Seric FullName = NULL; 5099536Seric 5109536Seric /* extract full name from passwd file */ 5119582Seric if (FullName == NULL && pw->pw_gecos != NULL && 5129582Seric strcmp(pw->pw_name, CurEnv->e_from.q_user) == 0) 5139536Seric { 5149536Seric buildfname(pw->pw_gecos, CurEnv->e_from.q_user, buf); 5159536Seric if (buf[0] != '\0') 5169536Seric FullName = newstr(buf); 5179536Seric } 5189536Seric if (FullName != NULL) 5199536Seric define('x', FullName, CurEnv); 5209536Seric } 52111625Seric else 52211625Seric { 5239536Seric #ifndef V6 52411625Seric if (CurEnv->e_from.q_home == NULL) 52511625Seric CurEnv->e_from.q_home = getenv("HOME"); 5269536Seric #endif V6 52711625Seric CurEnv->e_from.q_uid = getuid(); 52811625Seric CurEnv->e_from.q_gid = getgid(); 52911625Seric } 53011625Seric 5319536Seric if (CurEnv->e_from.q_uid != 0) 5329536Seric { 5339536Seric DefUid = CurEnv->e_from.q_uid; 5349536Seric DefGid = CurEnv->e_from.q_gid; 5359536Seric } 5369536Seric 5379536Seric /* 5389536Seric ** Rewrite the from person to dispose of possible implicit 5399536Seric ** links in the net. 5409536Seric */ 5419536Seric 54216913Seric pvp = prescan(from, '\0', pvpbuf); 5439536Seric if (pvp == NULL) 5449536Seric { 5459536Seric syserr("cannot prescan from (%s)", from); 5469536Seric finis(); 5479536Seric } 5489536Seric rewrite(pvp, 3); 5499536Seric rewrite(pvp, 1); 55025032Seric rewrite(pvp, 4); 5519536Seric cataddr(pvp, buf, sizeof buf); 5529536Seric define('f', newstr(buf), CurEnv); 5539536Seric 5549536Seric /* save the domain spec if this mailer wants it */ 55524944Seric if (CurEnv->e_from.q_mailer != NULL && 55624944Seric bitnset(M_CANONICAL, CurEnv->e_from.q_mailer->m_flags)) 5579536Seric { 5589536Seric extern char **copyplist(); 5599536Seric 5609536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 5619536Seric pvp++; 5629536Seric if (*pvp != NULL) 5639536Seric CurEnv->e_fromdomain = copyplist(pvp, TRUE); 5649536Seric } 5659536Seric } 5669536Seric /* 5679536Seric ** TRUSTEDUSER -- tell us if this user is to be trusted. 5689536Seric ** 5699536Seric ** Parameters: 5709536Seric ** user -- the user to be checked. 5719536Seric ** 5729536Seric ** Returns: 5739536Seric ** TRUE if the user is in an approved list. 5749536Seric ** FALSE otherwise. 5759536Seric ** 5769536Seric ** Side Effects: 5779536Seric ** none. 5789536Seric */ 5799536Seric 5809536Seric bool 5819536Seric trusteduser(user) 5829536Seric char *user; 5839536Seric { 5849536Seric register char **ulist; 5859536Seric extern char *TrustedUsers[]; 5869536Seric 5879536Seric for (ulist = TrustedUsers; *ulist != NULL; ulist++) 5889536Seric if (strcmp(*ulist, user) == 0) 5899536Seric return (TRUE); 5909536Seric return (FALSE); 5919536Seric } 592