122704Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 333729Sbostic * Copyright (c) 1988 Regents of the University of California. 433729Sbostic * All rights reserved. 533729Sbostic * 642826Sbostic * %sccs.include.redist.c% 733729Sbostic */ 822704Sdist 922704Sdist #ifndef lint 10*55360Seric static char sccsid[] = "@(#)envelope.c 5.30 (Berkeley) 07/19/92"; 1133729Sbostic #endif /* not lint */ 1222704Sdist 1336928Sbostic #include <sys/types.h> 1436928Sbostic #include <sys/time.h> 1536928Sbostic #include <sys/stat.h> 169536Seric #include <pwd.h> 1740933Srick #include <sys/file.h> 189536Seric #include "sendmail.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(); 4125611Seric extern ENVELOPE BlankEnvelope; 429536Seric 439536Seric parent = CurEnv; 449536Seric if (e == CurEnv) 459536Seric parent = e->e_parent; 4625611Seric 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 if (tTd(50, 1)) 829536Seric { 839536Seric printf("dropenvelope %x id=", e); 849536Seric xputs(e->e_id); 859536Seric printf(" flags=%o\n", e->e_flags); 869536Seric } 879536Seric #ifdef LOG 88*55360Seric if (LogLevel > 12) 899536Seric syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d", 909536Seric e->e_id == NULL ? "(none)" : e->e_id, 919536Seric e->e_flags, getpid()); 929536Seric #endif LOG 939536Seric 949536Seric /* we must have an id to remove disk files */ 959536Seric if (e->e_id == NULL) 969536Seric return; 979536Seric 989536Seric /* 999536Seric ** Extract state information from dregs of send list. 1009536Seric */ 1019536Seric 1029536Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1039536Seric { 1049536Seric if (bitset(QQUEUEUP, q->q_flags)) 1059536Seric queueit = TRUE; 1069536Seric } 1079536Seric 1089536Seric /* 1099536Seric ** Send back return receipts as requested. 1109536Seric */ 1119536Seric 1129536Seric if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 1139536Seric { 11410844Seric auto ADDRESS *rlist = NULL; 1159536Seric 11655012Seric sendtolist(e->e_receiptto, (ADDRESS *) NULL, &rlist, e); 11755012Seric (void) returntosender("Return receipt", rlist, FALSE, e); 1189536Seric } 1199536Seric 1209536Seric /* 1219536Seric ** Arrange to send error messages if there are fatal errors. 1229536Seric */ 1239536Seric 12410754Seric if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && ErrorMode != EM_QUIET) 1259536Seric savemail(e); 1269536Seric 1279536Seric /* 1289536Seric ** Instantiate or deinstantiate the queue. 1299536Seric */ 1309536Seric 1319536Seric if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 1329536Seric bitset(EF_CLRQUEUE, e->e_flags)) 1339536Seric { 13423497Seric if (e->e_df != NULL) 13523497Seric xunlink(e->e_df); 1369536Seric xunlink(queuename(e, 'q')); 1379536Seric } 1389536Seric else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 13910754Seric { 14010754Seric #ifdef QUEUE 14151920Seric queueup(e, FALSE, FALSE); 14210754Seric #else QUEUE 14310754Seric syserr("dropenvelope: queueup"); 14410754Seric #endif QUEUE 14510754Seric } 1469536Seric 1479536Seric /* now unlock the job */ 14810196Seric closexscript(e); 1499536Seric unlockqueue(e); 1509536Seric 1519536Seric /* make sure that this envelope is marked unused */ 1529536Seric e->e_id = e->e_df = NULL; 15324944Seric if (e->e_dfp != NULL) 15424944Seric (void) fclose(e->e_dfp); 15510196Seric e->e_dfp = NULL; 1569536Seric } 1579536Seric /* 1589536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 1599536Seric ** 1609536Seric ** This is normally used by a child process to get a clean 1619536Seric ** envelope without disturbing the parent. 1629536Seric ** 1639536Seric ** Parameters: 1649536Seric ** e -- the envelope to clear. 16525611Seric ** fullclear - if set, the current envelope is total 16625611Seric ** garbage and should be ignored; otherwise, 16725611Seric ** release any resources it may indicate. 1689536Seric ** 1699536Seric ** Returns: 1709536Seric ** none. 1719536Seric ** 1729536Seric ** Side Effects: 1739536Seric ** Closes files associated with the envelope. 1749536Seric ** Marks the envelope as unallocated. 1759536Seric */ 1769536Seric 17725611Seric clearenvelope(e, fullclear) 1789536Seric register ENVELOPE *e; 17925611Seric bool fullclear; 1809536Seric { 18125514Seric register HDR *bh; 18225514Seric register HDR **nhp; 18325514Seric extern ENVELOPE BlankEnvelope; 18425514Seric 18525611Seric if (!fullclear) 18625611Seric { 18725611Seric /* clear out any file information */ 18825611Seric if (e->e_xfp != NULL) 18925611Seric (void) fclose(e->e_xfp); 19025611Seric if (e->e_dfp != NULL) 19125611Seric (void) fclose(e->e_dfp); 19225611Seric } 1939536Seric 19424961Seric /* now clear out the data */ 19524965Seric STRUCTCOPY(BlankEnvelope, *e); 19625514Seric bh = BlankEnvelope.e_header; 19725514Seric nhp = &e->e_header; 19825514Seric while (bh != NULL) 19925514Seric { 20025514Seric *nhp = (HDR *) xalloc(sizeof *bh); 20125514Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 20225514Seric bh = bh->h_link; 20325514Seric nhp = &(*nhp)->h_link; 20425514Seric } 2059536Seric } 2069536Seric /* 2079536Seric ** INITSYS -- initialize instantiation of system 2089536Seric ** 2099536Seric ** In Daemon mode, this is done in the child. 2109536Seric ** 2119536Seric ** Parameters: 2129536Seric ** none. 2139536Seric ** 2149536Seric ** Returns: 2159536Seric ** none. 2169536Seric ** 2179536Seric ** Side Effects: 2189536Seric ** Initializes the system macros, some global variables, 2199536Seric ** etc. In particular, the current time in various 2209536Seric ** forms is set. 2219536Seric */ 2229536Seric 22355012Seric initsys(e) 22455012Seric register ENVELOPE *e; 2259536Seric { 2269536Seric static char cbuf[5]; /* holds hop count */ 2279536Seric static char pbuf[10]; /* holds pid */ 22822963Smiriam #ifdef TTYNAME 2299536Seric static char ybuf[10]; /* holds tty id */ 2309536Seric register char *p; 23122963Smiriam #endif TTYNAME 2329536Seric extern char *ttyname(); 2339536Seric extern char *macvalue(); 2349536Seric extern char Version[]; 2359536Seric 2369536Seric /* 2379536Seric ** Give this envelope a reality. 2389536Seric ** I.e., an id, a transcript, and a creation time. 2399536Seric */ 2409536Seric 24155012Seric openxscript(e); 24255012Seric e->e_ctime = curtime(); 2439536Seric 2449536Seric /* 2459536Seric ** Set OutChannel to something useful if stdout isn't it. 2469536Seric ** This arranges that any extra stuff the mailer produces 2479536Seric ** gets sent back to the user on error (because it is 2489536Seric ** tucked away in the transcript). 2499536Seric */ 2509536Seric 2519536Seric if (OpMode == MD_DAEMON && QueueRun) 25255012Seric OutChannel = e->e_xfp; 2539536Seric 2549536Seric /* 2559536Seric ** Set up some basic system macros. 2569536Seric */ 2579536Seric 2589536Seric /* process id */ 2599536Seric (void) sprintf(pbuf, "%d", getpid()); 26055012Seric define('p', pbuf, e); 2619536Seric 2629536Seric /* hop count */ 26355012Seric (void) sprintf(cbuf, "%d", e->e_hopcount); 26455012Seric define('c', cbuf, e); 2659536Seric 2669536Seric /* time as integer, unix time, arpa time */ 26755012Seric settime(e); 2689536Seric 26917472Seric #ifdef TTYNAME 2709536Seric /* tty name */ 27155012Seric if (macvalue('y', e) == NULL) 2729536Seric { 2739536Seric p = ttyname(2); 2749536Seric if (p != NULL) 2759536Seric { 2769536Seric if (rindex(p, '/') != NULL) 2779536Seric p = rindex(p, '/') + 1; 2789536Seric (void) strcpy(ybuf, p); 27955012Seric define('y', ybuf, e); 2809536Seric } 2819536Seric } 28217472Seric #endif TTYNAME 2839536Seric } 2849536Seric /* 28511932Seric ** SETTIME -- set the current time. 28611932Seric ** 28711932Seric ** Parameters: 28811932Seric ** none. 28911932Seric ** 29011932Seric ** Returns: 29111932Seric ** none. 29211932Seric ** 29311932Seric ** Side Effects: 29411932Seric ** Sets the various time macros -- $a, $b, $d, $t. 29511932Seric */ 29611932Seric 29755012Seric settime(e) 29855012Seric register ENVELOPE *e; 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); 31355012Seric define('t', tbuf, e); 31411932Seric (void) strcpy(dbuf, ctime(&now)); 31511932Seric *index(dbuf, '\n') = '\0'; 31655012Seric if (macvalue('d', e) == NULL) 31755012Seric define('d', dbuf, e); 31811932Seric p = newstr(arpadate(dbuf)); 31955012Seric if (macvalue('a', e) == NULL) 32055012Seric define('a', p, e); 32155012Seric define('b', p, e); 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; 34340933Srick int fd; 3449536Seric 34510196Seric # ifdef LOG 34610196Seric if (LogLevel > 19) 34710196Seric syslog(LOG_DEBUG, "%s: openx%s", e->e_id, e->e_xfp == NULL ? "" : " (no)"); 34810196Seric # endif LOG 3499536Seric if (e->e_xfp != NULL) 3509536Seric return; 3519536Seric p = queuename(e, 'x'); 35240933Srick fd = open(p, O_WRONLY|O_CREAT, 0644); 35340933Srick if (fd < 0) 3549536Seric syserr("Can't create %s", p); 3559536Seric else 35640933Srick e->e_xfp = fdopen(fd, "w"); 3579536Seric } 3589536Seric /* 35910196Seric ** CLOSEXSCRIPT -- close the transcript file. 36010196Seric ** 36110196Seric ** Parameters: 36210196Seric ** e -- the envelope containing the transcript to close. 36310196Seric ** 36410196Seric ** Returns: 36510196Seric ** none. 36610196Seric ** 36710196Seric ** Side Effects: 36810196Seric ** none. 36910196Seric */ 37010196Seric 37110196Seric closexscript(e) 37210196Seric register ENVELOPE *e; 37310196Seric { 37410196Seric if (e->e_xfp == NULL) 37510196Seric return; 37610196Seric (void) fclose(e->e_xfp); 37710196Seric e->e_xfp = NULL; 37810196Seric } 37910196Seric /* 3809536Seric ** SETSENDER -- set the person who this message is from 3819536Seric ** 3829536Seric ** Under certain circumstances allow the user to say who 3839536Seric ** s/he is (using -f or -r). These are: 3849536Seric ** 1. The user's uid is zero (root). 3859536Seric ** 2. The user's login name is in an approved list (typically 3869536Seric ** from a network server). 3879536Seric ** 3. The address the user is trying to claim has a 3889536Seric ** "!" character in it (since #2 doesn't do it for 3899536Seric ** us if we are dialing out for UUCP). 3909536Seric ** A better check to replace #3 would be if the 3919536Seric ** effective uid is "UUCP" -- this would require me 3929536Seric ** to rewrite getpwent to "grab" uucp as it went by, 3939536Seric ** make getname more nasty, do another passwd file 3949536Seric ** scan, or compile the UID of "UUCP" into the code, 3959536Seric ** all of which are reprehensible. 3969536Seric ** 3979536Seric ** Assuming all of these fail, we figure out something 3989536Seric ** ourselves. 3999536Seric ** 4009536Seric ** Parameters: 4019536Seric ** from -- the person we would like to believe this message 4029536Seric ** is from, as specified on the command line. 40353182Seric ** e -- the envelope in which we would like the sender set. 4049536Seric ** 4059536Seric ** Returns: 4069536Seric ** none. 4079536Seric ** 4089536Seric ** Side Effects: 4099536Seric ** sets sendmail's notion of who the from person is. 4109536Seric */ 4119536Seric 41253182Seric setsender(from, e) 4139536Seric char *from; 41453182Seric register ENVELOPE *e; 4159536Seric { 4169536Seric register char **pvp; 4179536Seric char *realname = NULL; 41818665Seric register struct passwd *pw; 4199536Seric char buf[MAXNAME]; 42016913Seric char pvpbuf[PSBUFSIZE]; 42118665Seric extern struct passwd *getpwnam(); 4229536Seric extern char *macvalue(); 4239536Seric extern char **prescan(); 4249536Seric extern bool safefile(); 4259536Seric extern char *FullName; 4269536Seric 4279536Seric if (tTd(45, 1)) 42814786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 4299536Seric 4309536Seric /* 4319536Seric ** Figure out the real user executing us. 4329536Seric ** Username can return errno != 0 on non-errors. 4339536Seric */ 4349536Seric 43552220Seric if (QueueRun || OpMode == MD_SMTP) 4369536Seric realname = from; 4379536Seric if (realname == NULL || realname[0] == '\0') 4389536Seric { 4399536Seric extern char *username(); 4409536Seric 4419536Seric realname = username(); 4429536Seric } 4439536Seric 4449536Seric /* 4459536Seric ** Determine if this real person is allowed to alias themselves. 4469536Seric */ 4479536Seric 4489536Seric if (from != NULL) 4499536Seric { 4509536Seric extern bool trusteduser(); 4519536Seric 45236230Skarels if (!trusteduser(realname) && getuid() != geteuid() && 4539536Seric index(from, '!') == NULL && getuid() != 0) 4549536Seric { 4559536Seric /* network sends -r regardless (why why why?) */ 4569536Seric /* syserr("%s, you cannot use the -f flag", realname); */ 4579536Seric from = NULL; 4589536Seric } 4599536Seric } 4609536Seric 4619536Seric SuprErrs = TRUE; 46255012Seric if (from == NULL || parseaddr(from, &e->e_from, 1, '\0', e) == NULL) 4639536Seric { 46421750Seric /* log garbage addresses for traceback */ 46555173Seric # ifdef LOG 46655173Seric if (from != NULL && LogLevel >= 1) 46721750Seric { 46855173Seric char *host = RealHostName; 46955173Seric 47055173Seric if (host == NULL) 47155173Seric host = MyHostName; 47255173Seric syslog(LOG_NOTICE, 47355173Seric "from=%s unparseable, received from %s@%s", 47455173Seric from, realname, host); 47555173Seric } 47624944Seric # endif LOG 4779536Seric from = newstr(realname); 47855012Seric if (parseaddr(from, &e->e_from, 1, '\0', e) == NULL && 47955012Seric parseaddr("postmaster", &e->e_from, 1, '\0', e) == NULL) 48024944Seric { 48124944Seric syserr("setsender: can't even parse postmaster!"); 48224944Seric } 4839536Seric } 4849536Seric else 4859536Seric FromFlag = TRUE; 48653182Seric e->e_from.q_flags |= QDONTSEND; 48753182Seric loweraddr(&e->e_from); 4889536Seric SuprErrs = FALSE; 4899536Seric 49053736Seric pvp = NULL; 49153736Seric if (e->e_from.q_mailer == LocalMailer) 4929536Seric { 49353736Seric # ifdef USERDB 49453736Seric register char *p; 49553736Seric extern char *udbsender(); 49653736Seric # endif 49717472Seric 4989536Seric /* if the user has given fullname already, don't redefine */ 4999536Seric if (FullName == NULL) 50053182Seric FullName = macvalue('x', e); 50111932Seric if (FullName != NULL && FullName[0] == '\0') 5029536Seric FullName = NULL; 5039536Seric 50453736Seric # ifdef USERDB 50553736Seric p = udbsender(from); 50653736Seric 50753736Seric if (p != NULL) 5089536Seric { 50953736Seric /* 51053736Seric ** We have an alternate address for the sender 51153736Seric */ 51253736Seric 51353736Seric pvp = prescan(p, '\0', pvpbuf); 5149536Seric } 51553736Seric # endif /* USERDB */ 51653736Seric 51753736Seric if ((pw = getpwnam(e->e_from.q_user)) != NULL) 51853736Seric { 51953736Seric /* 52053736Seric ** Process passwd file entry. 52153736Seric */ 52253736Seric 52353736Seric 52453736Seric /* extract home directory */ 52553736Seric e->e_from.q_home = newstr(pw->pw_dir); 52653736Seric define('z', e->e_from.q_home, e); 52753736Seric 52853736Seric /* extract user and group id */ 52953736Seric e->e_from.q_uid = pw->pw_uid; 53053736Seric e->e_from.q_gid = pw->pw_gid; 53153736Seric 53253736Seric /* extract full name from passwd file */ 53353736Seric if (FullName == NULL && pw->pw_gecos != NULL && 53453736Seric strcmp(pw->pw_name, e->e_from.q_user) == 0) 53553736Seric { 53653736Seric buildfname(pw->pw_gecos, e->e_from.q_user, buf); 53753736Seric if (buf[0] != '\0') 53853736Seric FullName = newstr(buf); 53953736Seric } 54053736Seric } 5419536Seric if (FullName != NULL) 54253182Seric define('x', FullName, e); 5439536Seric } 54411625Seric else 54511625Seric { 54653182Seric if (e->e_from.q_home == NULL) 54753182Seric e->e_from.q_home = getenv("HOME"); 54853182Seric e->e_from.q_uid = getuid(); 54953182Seric e->e_from.q_gid = getgid(); 55011625Seric } 55111625Seric 5529536Seric /* 5539536Seric ** Rewrite the from person to dispose of possible implicit 5549536Seric ** links in the net. 5559536Seric */ 5569536Seric 5579536Seric if (pvp == NULL) 55853736Seric pvp = prescan(from, '\0', pvpbuf); 55953736Seric if (pvp == NULL) 5609536Seric { 56136233Skarels # ifdef LOG 56236233Skarels if (LogLevel >= 1) 56336233Skarels syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 56436233Skarels # endif 56536230Skarels usrerr("cannot prescan from (%s)", from); 5669536Seric finis(); 5679536Seric } 5689536Seric rewrite(pvp, 3); 5699536Seric rewrite(pvp, 1); 57025032Seric rewrite(pvp, 4); 5719536Seric cataddr(pvp, buf, sizeof buf); 57253182Seric e->e_sender = e->e_returnpath = newstr(buf); 5739536Seric 57453182Seric define('f', e->e_sender, e); 57551951Seric 5769536Seric /* save the domain spec if this mailer wants it */ 57753182Seric if (e->e_from.q_mailer != NULL && 57853182Seric bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 5799536Seric { 5809536Seric extern char **copyplist(); 5819536Seric 5829536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 5839536Seric pvp++; 5849536Seric if (*pvp != NULL) 58553182Seric e->e_fromdomain = copyplist(pvp, TRUE); 5869536Seric } 5879536Seric } 5889536Seric /* 5899536Seric ** TRUSTEDUSER -- tell us if this user is to be trusted. 5909536Seric ** 5919536Seric ** Parameters: 5929536Seric ** user -- the user to be checked. 5939536Seric ** 5949536Seric ** Returns: 5959536Seric ** TRUE if the user is in an approved list. 5969536Seric ** FALSE otherwise. 5979536Seric ** 5989536Seric ** Side Effects: 5999536Seric ** none. 6009536Seric */ 6019536Seric 6029536Seric bool 6039536Seric trusteduser(user) 6049536Seric char *user; 6059536Seric { 6069536Seric register char **ulist; 6079536Seric extern char *TrustedUsers[]; 6089536Seric 6099536Seric for (ulist = TrustedUsers; *ulist != NULL; ulist++) 6109536Seric if (strcmp(*ulist, user) == 0) 6119536Seric return (TRUE); 6129536Seric return (FALSE); 6139536Seric } 614