122704Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 333729Sbostic * Copyright (c) 1988 Regents of the University of California. 433729Sbostic * All rights reserved. 533729Sbostic * 6*42826Sbostic * %sccs.include.redist.c% 733729Sbostic */ 822704Sdist 922704Sdist #ifndef lint 10*42826Sbostic static char sccsid[] = "@(#)envelope.c 5.22 (Berkeley) 06/01/90"; 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 889536Seric if (LogLevel > 10) 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 1169621Seric sendtolist(CurEnv->e_receiptto, (ADDRESS *) NULL, &rlist); 1179536Seric (void) returntosender("Return receipt", rlist, FALSE); 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 14140933Srick FILE *lockfp, *queueup(); 14240933Srick lockfp = queueup(e, FALSE, FALSE); 14340933Srick if (lockfp != NULL) 14440933Srick (void) fclose(lockfp); 14510754Seric #else QUEUE 14610754Seric syserr("dropenvelope: queueup"); 14710754Seric #endif QUEUE 14810754Seric } 1499536Seric 1509536Seric /* now unlock the job */ 15110196Seric closexscript(e); 1529536Seric unlockqueue(e); 1539536Seric 1549536Seric /* make sure that this envelope is marked unused */ 1559536Seric e->e_id = e->e_df = NULL; 15624944Seric if (e->e_dfp != NULL) 15724944Seric (void) fclose(e->e_dfp); 15810196Seric e->e_dfp = NULL; 1599536Seric } 1609536Seric /* 1619536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 1629536Seric ** 1639536Seric ** This is normally used by a child process to get a clean 1649536Seric ** envelope without disturbing the parent. 1659536Seric ** 1669536Seric ** Parameters: 1679536Seric ** e -- the envelope to clear. 16825611Seric ** fullclear - if set, the current envelope is total 16925611Seric ** garbage and should be ignored; otherwise, 17025611Seric ** release any resources it may indicate. 1719536Seric ** 1729536Seric ** Returns: 1739536Seric ** none. 1749536Seric ** 1759536Seric ** Side Effects: 1769536Seric ** Closes files associated with the envelope. 1779536Seric ** Marks the envelope as unallocated. 1789536Seric */ 1799536Seric 18025611Seric clearenvelope(e, fullclear) 1819536Seric register ENVELOPE *e; 18225611Seric bool fullclear; 1839536Seric { 18425514Seric register HDR *bh; 18525514Seric register HDR **nhp; 18625514Seric extern ENVELOPE BlankEnvelope; 18725514Seric 18825611Seric if (!fullclear) 18925611Seric { 19025611Seric /* clear out any file information */ 19125611Seric if (e->e_xfp != NULL) 19225611Seric (void) fclose(e->e_xfp); 19325611Seric if (e->e_dfp != NULL) 19425611Seric (void) fclose(e->e_dfp); 19525611Seric } 1969536Seric 19724961Seric /* now clear out the data */ 19824965Seric STRUCTCOPY(BlankEnvelope, *e); 19925514Seric bh = BlankEnvelope.e_header; 20025514Seric nhp = &e->e_header; 20125514Seric while (bh != NULL) 20225514Seric { 20325514Seric *nhp = (HDR *) xalloc(sizeof *bh); 20425514Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 20525514Seric bh = bh->h_link; 20625514Seric nhp = &(*nhp)->h_link; 20725514Seric } 2089536Seric } 2099536Seric /* 2109536Seric ** INITSYS -- initialize instantiation of system 2119536Seric ** 2129536Seric ** In Daemon mode, this is done in the child. 2139536Seric ** 2149536Seric ** Parameters: 2159536Seric ** none. 2169536Seric ** 2179536Seric ** Returns: 2189536Seric ** none. 2199536Seric ** 2209536Seric ** Side Effects: 2219536Seric ** Initializes the system macros, some global variables, 2229536Seric ** etc. In particular, the current time in various 2239536Seric ** forms is set. 2249536Seric */ 2259536Seric 2269536Seric initsys() 2279536Seric { 2289536Seric static char cbuf[5]; /* holds hop count */ 2299536Seric static char pbuf[10]; /* holds pid */ 23022963Smiriam #ifdef TTYNAME 2319536Seric static char ybuf[10]; /* holds tty id */ 2329536Seric register char *p; 23322963Smiriam #endif TTYNAME 2349536Seric extern char *ttyname(); 2359536Seric extern char *macvalue(); 2369536Seric extern char Version[]; 2379536Seric 2389536Seric /* 2399536Seric ** Give this envelope a reality. 2409536Seric ** I.e., an id, a transcript, and a creation time. 2419536Seric */ 2429536Seric 2439536Seric openxscript(CurEnv); 2449536Seric CurEnv->e_ctime = curtime(); 2459536Seric 2469536Seric /* 2479536Seric ** Set OutChannel to something useful if stdout isn't it. 2489536Seric ** This arranges that any extra stuff the mailer produces 2499536Seric ** gets sent back to the user on error (because it is 2509536Seric ** tucked away in the transcript). 2519536Seric */ 2529536Seric 2539536Seric if (OpMode == MD_DAEMON && QueueRun) 2549536Seric OutChannel = CurEnv->e_xfp; 2559536Seric 2569536Seric /* 2579536Seric ** Set up some basic system macros. 2589536Seric */ 2599536Seric 2609536Seric /* process id */ 2619536Seric (void) sprintf(pbuf, "%d", getpid()); 2629536Seric define('p', pbuf, CurEnv); 2639536Seric 2649536Seric /* hop count */ 2659536Seric (void) sprintf(cbuf, "%d", CurEnv->e_hopcount); 2669536Seric define('c', cbuf, CurEnv); 2679536Seric 2689536Seric /* time as integer, unix time, arpa time */ 26911932Seric settime(); 2709536Seric 27117472Seric #ifdef TTYNAME 2729536Seric /* tty name */ 2739536Seric if (macvalue('y', CurEnv) == NULL) 2749536Seric { 2759536Seric p = ttyname(2); 2769536Seric if (p != NULL) 2779536Seric { 2789536Seric if (rindex(p, '/') != NULL) 2799536Seric p = rindex(p, '/') + 1; 2809536Seric (void) strcpy(ybuf, p); 2819536Seric define('y', ybuf, CurEnv); 2829536Seric } 2839536Seric } 28417472Seric #endif TTYNAME 2859536Seric } 2869536Seric /* 28711932Seric ** SETTIME -- set the current time. 28811932Seric ** 28911932Seric ** Parameters: 29011932Seric ** none. 29111932Seric ** 29211932Seric ** Returns: 29311932Seric ** none. 29411932Seric ** 29511932Seric ** Side Effects: 29611932Seric ** Sets the various time macros -- $a, $b, $d, $t. 29711932Seric */ 29811932Seric 29911932Seric settime() 30011932Seric { 30111932Seric register char *p; 30211932Seric auto time_t now; 30311932Seric static char tbuf[20]; /* holds "current" time */ 30411932Seric static char dbuf[30]; /* holds ctime(tbuf) */ 30511932Seric register struct tm *tm; 30611932Seric extern char *arpadate(); 30711932Seric extern struct tm *gmtime(); 30811932Seric extern char *macvalue(); 30911932Seric 31011932Seric now = curtime(); 31111932Seric tm = gmtime(&now); 31211932Seric (void) sprintf(tbuf, "%02d%02d%02d%02d%02d", tm->tm_year, tm->tm_mon+1, 31311932Seric tm->tm_mday, tm->tm_hour, tm->tm_min); 31411932Seric define('t', tbuf, CurEnv); 31511932Seric (void) strcpy(dbuf, ctime(&now)); 31611932Seric *index(dbuf, '\n') = '\0'; 31711932Seric if (macvalue('d', CurEnv) == NULL) 31811932Seric define('d', dbuf, CurEnv); 31911932Seric p = newstr(arpadate(dbuf)); 32011932Seric if (macvalue('a', CurEnv) == NULL) 32111932Seric define('a', p, CurEnv); 32211932Seric define('b', p, CurEnv); 32311932Seric } 32411932Seric /* 3259536Seric ** OPENXSCRIPT -- Open transcript file 3269536Seric ** 3279536Seric ** Creates a transcript file for possible eventual mailing or 3289536Seric ** sending back. 3299536Seric ** 3309536Seric ** Parameters: 3319536Seric ** e -- the envelope to create the transcript in/for. 3329536Seric ** 3339536Seric ** Returns: 3349536Seric ** none 3359536Seric ** 3369536Seric ** Side Effects: 3379536Seric ** Creates the transcript file. 3389536Seric */ 3399536Seric 3409536Seric openxscript(e) 3419536Seric register ENVELOPE *e; 3429536Seric { 3439536Seric register char *p; 34440933Srick int fd; 3459536Seric 34610196Seric # ifdef LOG 34710196Seric if (LogLevel > 19) 34810196Seric syslog(LOG_DEBUG, "%s: openx%s", e->e_id, e->e_xfp == NULL ? "" : " (no)"); 34910196Seric # endif LOG 3509536Seric if (e->e_xfp != NULL) 3519536Seric return; 3529536Seric p = queuename(e, 'x'); 35340933Srick fd = open(p, O_WRONLY|O_CREAT, 0644); 35440933Srick if (fd < 0) 3559536Seric syserr("Can't create %s", p); 3569536Seric else 35740933Srick e->e_xfp = fdopen(fd, "w"); 3589536Seric } 3599536Seric /* 36010196Seric ** CLOSEXSCRIPT -- close the transcript file. 36110196Seric ** 36210196Seric ** Parameters: 36310196Seric ** e -- the envelope containing the transcript to close. 36410196Seric ** 36510196Seric ** Returns: 36610196Seric ** none. 36710196Seric ** 36810196Seric ** Side Effects: 36910196Seric ** none. 37010196Seric */ 37110196Seric 37210196Seric closexscript(e) 37310196Seric register ENVELOPE *e; 37410196Seric { 37510196Seric if (e->e_xfp == NULL) 37610196Seric return; 37710196Seric (void) fclose(e->e_xfp); 37810196Seric e->e_xfp = NULL; 37910196Seric } 38010196Seric /* 3819536Seric ** SETSENDER -- set the person who this message is from 3829536Seric ** 3839536Seric ** Under certain circumstances allow the user to say who 3849536Seric ** s/he is (using -f or -r). These are: 3859536Seric ** 1. The user's uid is zero (root). 3869536Seric ** 2. The user's login name is in an approved list (typically 3879536Seric ** from a network server). 3889536Seric ** 3. The address the user is trying to claim has a 3899536Seric ** "!" character in it (since #2 doesn't do it for 3909536Seric ** us if we are dialing out for UUCP). 3919536Seric ** A better check to replace #3 would be if the 3929536Seric ** effective uid is "UUCP" -- this would require me 3939536Seric ** to rewrite getpwent to "grab" uucp as it went by, 3949536Seric ** make getname more nasty, do another passwd file 3959536Seric ** scan, or compile the UID of "UUCP" into the code, 3969536Seric ** all of which are reprehensible. 3979536Seric ** 3989536Seric ** Assuming all of these fail, we figure out something 3999536Seric ** ourselves. 4009536Seric ** 4019536Seric ** Parameters: 4029536Seric ** from -- the person we would like to believe this message 4039536Seric ** is from, as specified on the command line. 4049536Seric ** 4059536Seric ** Returns: 4069536Seric ** none. 4079536Seric ** 4089536Seric ** Side Effects: 4099536Seric ** sets sendmail's notion of who the from person is. 4109536Seric */ 4119536Seric 4129536Seric setsender(from) 4139536Seric char *from; 4149536Seric { 4159536Seric register char **pvp; 4169536Seric char *realname = NULL; 41718665Seric register struct passwd *pw; 4189536Seric char buf[MAXNAME]; 41916913Seric char pvpbuf[PSBUFSIZE]; 42018665Seric extern struct passwd *getpwnam(); 4219536Seric extern char *macvalue(); 4229536Seric extern char **prescan(); 4239536Seric extern bool safefile(); 4249536Seric extern char *FullName; 4259536Seric 4269536Seric if (tTd(45, 1)) 42714786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 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 45136230Skarels if (!trusteduser(realname) && getuid() != geteuid() && 4529536Seric index(from, '!') == NULL && getuid() != 0) 4539536Seric { 4549536Seric /* network sends -r regardless (why why why?) */ 4559536Seric /* syserr("%s, you cannot use the -f flag", realname); */ 4569536Seric from = NULL; 4579536Seric } 4589536Seric } 4599536Seric 4609536Seric SuprErrs = TRUE; 46111447Seric if (from == NULL || parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL) 4629536Seric { 46321750Seric /* log garbage addresses for traceback */ 46421750Seric if (from != NULL) 46521750Seric { 46624944Seric # ifdef LOG 46724944Seric if (LogLevel >= 1) 46836230Skarels if (realname == from && RealHostName != NULL) 46936230Skarels syslog(LOG_NOTICE, 47036230Skarels "from=%s unparseable, received from %s", 47136230Skarels from, RealHostName); 47236230Skarels else 47336230Skarels syslog(LOG_NOTICE, 47436230Skarels "Unparseable username %s wants from=%s", 47536230Skarels realname, from); 47624944Seric # endif LOG 47721750Seric } 4789536Seric from = newstr(realname); 47924944Seric if (parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL && 48024944Seric parseaddr("postmaster", &CurEnv->e_from, 1, '\0') == NULL) 48124944Seric { 48224944Seric syserr("setsender: can't even parse postmaster!"); 48324944Seric } 4849536Seric } 4859536Seric else 4869536Seric FromFlag = TRUE; 4879536Seric CurEnv->e_from.q_flags |= QDONTSEND; 48816162Seric loweraddr(&CurEnv->e_from); 4899536Seric SuprErrs = FALSE; 4909536Seric 49118665Seric if (CurEnv->e_from.q_mailer == LocalMailer && 49218665Seric (pw = getpwnam(CurEnv->e_from.q_user)) != NULL) 4939536Seric { 49417472Seric /* 49517472Seric ** Process passwd file entry. 49617472Seric */ 49717472Seric 4989536Seric 4999536Seric /* extract home directory */ 5009536Seric CurEnv->e_from.q_home = newstr(pw->pw_dir); 50116481Seric define('z', CurEnv->e_from.q_home, CurEnv); 5029536Seric 50311625Seric /* extract user and group id */ 50411625Seric CurEnv->e_from.q_uid = pw->pw_uid; 50511625Seric CurEnv->e_from.q_gid = pw->pw_gid; 50611625Seric 5079536Seric /* if the user has given fullname already, don't redefine */ 5089536Seric if (FullName == NULL) 5099536Seric FullName = macvalue('x', CurEnv); 51011932Seric if (FullName != NULL && FullName[0] == '\0') 5119536Seric FullName = NULL; 5129536Seric 5139536Seric /* extract full name from passwd file */ 5149582Seric if (FullName == NULL && pw->pw_gecos != NULL && 5159582Seric strcmp(pw->pw_name, CurEnv->e_from.q_user) == 0) 5169536Seric { 5179536Seric buildfname(pw->pw_gecos, CurEnv->e_from.q_user, buf); 5189536Seric if (buf[0] != '\0') 5199536Seric FullName = newstr(buf); 5209536Seric } 5219536Seric if (FullName != NULL) 5229536Seric define('x', FullName, CurEnv); 5239536Seric } 52411625Seric else 52511625Seric { 52611625Seric if (CurEnv->e_from.q_home == NULL) 52711625Seric CurEnv->e_from.q_home = getenv("HOME"); 52811625Seric CurEnv->e_from.q_uid = getuid(); 52911625Seric CurEnv->e_from.q_gid = getgid(); 53011625Seric } 53111625Seric 5329536Seric /* 5339536Seric ** Rewrite the from person to dispose of possible implicit 5349536Seric ** links in the net. 5359536Seric */ 5369536Seric 53716913Seric pvp = prescan(from, '\0', pvpbuf); 5389536Seric if (pvp == NULL) 5399536Seric { 54036233Skarels # ifdef LOG 54136233Skarels if (LogLevel >= 1) 54236233Skarels syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 54336233Skarels # endif 54436230Skarels usrerr("cannot prescan from (%s)", from); 5459536Seric finis(); 5469536Seric } 5479536Seric rewrite(pvp, 3); 5489536Seric rewrite(pvp, 1); 54925032Seric rewrite(pvp, 4); 5509536Seric cataddr(pvp, buf, sizeof buf); 5519536Seric define('f', newstr(buf), CurEnv); 5529536Seric 5539536Seric /* save the domain spec if this mailer wants it */ 55424944Seric if (CurEnv->e_from.q_mailer != NULL && 55524944Seric bitnset(M_CANONICAL, CurEnv->e_from.q_mailer->m_flags)) 5569536Seric { 5579536Seric extern char **copyplist(); 5589536Seric 5599536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 5609536Seric pvp++; 5619536Seric if (*pvp != NULL) 5629536Seric CurEnv->e_fromdomain = copyplist(pvp, TRUE); 5639536Seric } 5649536Seric } 5659536Seric /* 5669536Seric ** TRUSTEDUSER -- tell us if this user is to be trusted. 5679536Seric ** 5689536Seric ** Parameters: 5699536Seric ** user -- the user to be checked. 5709536Seric ** 5719536Seric ** Returns: 5729536Seric ** TRUE if the user is in an approved list. 5739536Seric ** FALSE otherwise. 5749536Seric ** 5759536Seric ** Side Effects: 5769536Seric ** none. 5779536Seric */ 5789536Seric 5799536Seric bool 5809536Seric trusteduser(user) 5819536Seric char *user; 5829536Seric { 5839536Seric register char **ulist; 5849536Seric extern char *TrustedUsers[]; 5859536Seric 5869536Seric for (ulist = TrustedUsers; *ulist != NULL; ulist++) 5879536Seric if (strcmp(*ulist, user) == 0) 5889536Seric return (TRUE); 5899536Seric return (FALSE); 5909536Seric } 591