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*58727Seric static char sccsid[] = "@(#)envelope.c 6.24 (Berkeley) 03/18/93"; 1133729Sbostic #endif /* not lint */ 1222704Sdist 1358332Seric #include "sendmail.h" 1436928Sbostic #include <sys/time.h> 1536928Sbostic #include <sys/stat.h> 169536Seric #include <pwd.h> 179536Seric 189536Seric /* 199536Seric ** NEWENVELOPE -- allocate a new envelope 209536Seric ** 219536Seric ** Supports inheritance. 229536Seric ** 239536Seric ** Parameters: 249536Seric ** e -- the new envelope to fill in. 2558179Seric ** parent -- the envelope to be the parent of e. 269536Seric ** 279536Seric ** Returns: 289536Seric ** e. 299536Seric ** 309536Seric ** Side Effects: 319536Seric ** none. 329536Seric */ 339536Seric 349536Seric ENVELOPE * 3558179Seric newenvelope(e, parent) 369536Seric register ENVELOPE *e; 3758179Seric register ENVELOPE *parent; 389536Seric { 399536Seric extern putheader(), putbody(); 4025611Seric extern ENVELOPE BlankEnvelope; 419536Seric 4258179Seric if (e == parent && e->e_parent != NULL) 439536Seric parent = e->e_parent; 4425611Seric clearenvelope(e, TRUE); 4524944Seric if (e == CurEnv) 4624944Seric bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from); 4724944Seric else 4824944Seric bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from); 499536Seric e->e_parent = parent; 509536Seric e->e_ctime = curtime(); 5156215Seric if (parent != NULL) 5256215Seric e->e_msgpriority = parent->e_msgsize; 539536Seric e->e_puthdr = putheader; 549536Seric e->e_putbody = putbody; 559536Seric if (CurEnv->e_xfp != NULL) 569536Seric (void) fflush(CurEnv->e_xfp); 579536Seric 589536Seric return (e); 599536Seric } 609536Seric /* 619536Seric ** DROPENVELOPE -- deallocate an envelope. 629536Seric ** 639536Seric ** Parameters: 649536Seric ** e -- the envelope to deallocate. 659536Seric ** 669536Seric ** Returns: 679536Seric ** none. 689536Seric ** 699536Seric ** Side Effects: 709536Seric ** housekeeping necessary to dispose of an envelope. 719536Seric ** Unlocks this queue file. 729536Seric */ 739536Seric 749536Seric dropenvelope(e) 759536Seric register ENVELOPE *e; 769536Seric { 779536Seric bool queueit = FALSE; 789536Seric register ADDRESS *q; 7957943Seric char *id = e->e_id; 809536Seric 819536Seric if (tTd(50, 1)) 829536Seric { 8358680Seric printf("dropenvelope %x: id=", e); 849536Seric xputs(e->e_id); 8558680Seric printf(", flags=%o\n", e->e_flags); 869536Seric } 8757943Seric 8858680Seric /* we must have an id to remove disk files */ 8957943Seric if (id == NULL) 9058680Seric return; 9157943Seric 929536Seric #ifdef LOG 9358020Seric if (LogLevel > 84) 949536Seric syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d", 9557943Seric id, e->e_flags, getpid()); 9656795Seric #endif /* LOG */ 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 11658082Seric (void) 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); 14256795Seric #else /* QUEUE */ 14358151Seric syserr("554 dropenvelope: queueup"); 14456795Seric #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 */ 15224944Seric if (e->e_dfp != NULL) 15358680Seric (void) xfclose(e->e_dfp, "dropenvelope", e->e_df); 15410196Seric e->e_dfp = NULL; 15558680Seric e->e_id = e->e_df = NULL; 15657589Seric 15757589Seric #ifdef LOG 15858020Seric if (LogLevel > 74) 15957943Seric syslog(LOG_INFO, "%s: done", id); 16057589Seric #endif /* LOG */ 1619536Seric } 1629536Seric /* 1639536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 1649536Seric ** 1659536Seric ** This is normally used by a child process to get a clean 1669536Seric ** envelope without disturbing the parent. 1679536Seric ** 1689536Seric ** Parameters: 1699536Seric ** e -- the envelope to clear. 17025611Seric ** fullclear - if set, the current envelope is total 17125611Seric ** garbage and should be ignored; otherwise, 17225611Seric ** release any resources it may indicate. 1739536Seric ** 1749536Seric ** Returns: 1759536Seric ** none. 1769536Seric ** 1779536Seric ** Side Effects: 1789536Seric ** Closes files associated with the envelope. 1799536Seric ** Marks the envelope as unallocated. 1809536Seric */ 1819536Seric 18225611Seric clearenvelope(e, fullclear) 1839536Seric register ENVELOPE *e; 18425611Seric bool fullclear; 1859536Seric { 18625514Seric register HDR *bh; 18725514Seric register HDR **nhp; 18825514Seric extern ENVELOPE BlankEnvelope; 18925514Seric 19025611Seric if (!fullclear) 19125611Seric { 19225611Seric /* clear out any file information */ 19325611Seric if (e->e_xfp != NULL) 19458680Seric (void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id); 19525611Seric if (e->e_dfp != NULL) 19658680Seric (void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_df); 19758680Seric e->e_xfp = e->e_dfp = NULL; 19825611Seric } 1999536Seric 20024961Seric /* now clear out the data */ 20124965Seric STRUCTCOPY(BlankEnvelope, *e); 20225514Seric bh = BlankEnvelope.e_header; 20325514Seric nhp = &e->e_header; 20425514Seric while (bh != NULL) 20525514Seric { 20625514Seric *nhp = (HDR *) xalloc(sizeof *bh); 20725514Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 20825514Seric bh = bh->h_link; 20925514Seric nhp = &(*nhp)->h_link; 21025514Seric } 2119536Seric } 2129536Seric /* 2139536Seric ** INITSYS -- initialize instantiation of system 2149536Seric ** 2159536Seric ** In Daemon mode, this is done in the child. 2169536Seric ** 2179536Seric ** Parameters: 2189536Seric ** none. 2199536Seric ** 2209536Seric ** Returns: 2219536Seric ** none. 2229536Seric ** 2239536Seric ** Side Effects: 2249536Seric ** Initializes the system macros, some global variables, 2259536Seric ** etc. In particular, the current time in various 2269536Seric ** forms is set. 2279536Seric */ 2289536Seric 22955012Seric initsys(e) 23055012Seric register ENVELOPE *e; 2319536Seric { 2329536Seric static char cbuf[5]; /* holds hop count */ 2339536Seric static char pbuf[10]; /* holds pid */ 23422963Smiriam #ifdef TTYNAME 2359536Seric static char ybuf[10]; /* holds tty id */ 2369536Seric register char *p; 23756795Seric #endif /* TTYNAME */ 2389536Seric extern char *ttyname(); 2399536Seric extern char *macvalue(); 2409536Seric extern char Version[]; 2419536Seric 2429536Seric /* 2439536Seric ** Give this envelope a reality. 2449536Seric ** I.e., an id, a transcript, and a creation time. 2459536Seric */ 2469536Seric 24755012Seric openxscript(e); 24855012Seric e->e_ctime = curtime(); 2499536Seric 2509536Seric /* 2519536Seric ** Set OutChannel to something useful if stdout isn't it. 2529536Seric ** This arranges that any extra stuff the mailer produces 2539536Seric ** gets sent back to the user on error (because it is 2549536Seric ** tucked away in the transcript). 2559536Seric */ 2569536Seric 25758069Seric if (OpMode == MD_DAEMON && QueueRun && e->e_xfp != NULL) 25855012Seric OutChannel = e->e_xfp; 2599536Seric 2609536Seric /* 2619536Seric ** Set up some basic system macros. 2629536Seric */ 2639536Seric 2649536Seric /* process id */ 2659536Seric (void) sprintf(pbuf, "%d", getpid()); 26655012Seric define('p', pbuf, e); 2679536Seric 2689536Seric /* hop count */ 26955012Seric (void) sprintf(cbuf, "%d", e->e_hopcount); 27055012Seric define('c', cbuf, e); 2719536Seric 2729536Seric /* time as integer, unix time, arpa time */ 27355012Seric settime(e); 2749536Seric 27517472Seric #ifdef TTYNAME 2769536Seric /* tty name */ 27755012Seric if (macvalue('y', e) == NULL) 2789536Seric { 2799536Seric p = ttyname(2); 2809536Seric if (p != NULL) 2819536Seric { 28256795Seric if (strrchr(p, '/') != NULL) 28356795Seric p = strrchr(p, '/') + 1; 2849536Seric (void) strcpy(ybuf, p); 28555012Seric define('y', ybuf, e); 2869536Seric } 2879536Seric } 28856795Seric #endif /* TTYNAME */ 2899536Seric } 2909536Seric /* 29111932Seric ** SETTIME -- set the current time. 29211932Seric ** 29311932Seric ** Parameters: 29411932Seric ** none. 29511932Seric ** 29611932Seric ** Returns: 29711932Seric ** none. 29811932Seric ** 29911932Seric ** Side Effects: 30011932Seric ** Sets the various time macros -- $a, $b, $d, $t. 30111932Seric */ 30211932Seric 30355012Seric settime(e) 30455012Seric register ENVELOPE *e; 30511932Seric { 30611932Seric register char *p; 30711932Seric auto time_t now; 30811932Seric static char tbuf[20]; /* holds "current" time */ 30911932Seric static char dbuf[30]; /* holds ctime(tbuf) */ 31011932Seric register struct tm *tm; 31111932Seric extern char *arpadate(); 31211932Seric extern struct tm *gmtime(); 31311932Seric extern char *macvalue(); 31411932Seric 31511932Seric now = curtime(); 31611932Seric tm = gmtime(&now); 31757014Seric (void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900, 31857014Seric tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min); 31955012Seric define('t', tbuf, e); 32011932Seric (void) strcpy(dbuf, ctime(&now)); 32158131Seric p = strchr(dbuf, '\n'); 32258131Seric if (p != NULL) 32358131Seric *p = '\0'; 32458131Seric define('d', dbuf, e); 32511932Seric p = newstr(arpadate(dbuf)); 32655012Seric if (macvalue('a', e) == NULL) 32755012Seric define('a', p, e); 32855012Seric define('b', p, e); 32911932Seric } 33011932Seric /* 3319536Seric ** OPENXSCRIPT -- Open transcript file 3329536Seric ** 3339536Seric ** Creates a transcript file for possible eventual mailing or 3349536Seric ** sending back. 3359536Seric ** 3369536Seric ** Parameters: 3379536Seric ** e -- the envelope to create the transcript in/for. 3389536Seric ** 3399536Seric ** Returns: 3409536Seric ** none 3419536Seric ** 3429536Seric ** Side Effects: 3439536Seric ** Creates the transcript file. 3449536Seric */ 3459536Seric 3469536Seric openxscript(e) 3479536Seric register ENVELOPE *e; 3489536Seric { 3499536Seric register char *p; 35040933Srick int fd; 3519536Seric 3529536Seric if (e->e_xfp != NULL) 3539536Seric return; 3549536Seric p = queuename(e, 'x'); 35540933Srick fd = open(p, O_WRONLY|O_CREAT, 0644); 35640933Srick if (fd < 0) 3579536Seric syserr("Can't create %s", p); 3589536Seric else 35940933Srick e->e_xfp = fdopen(fd, "w"); 3609536Seric } 3619536Seric /* 36210196Seric ** CLOSEXSCRIPT -- close the transcript file. 36310196Seric ** 36410196Seric ** Parameters: 36510196Seric ** e -- the envelope containing the transcript to close. 36610196Seric ** 36710196Seric ** Returns: 36810196Seric ** none. 36910196Seric ** 37010196Seric ** Side Effects: 37110196Seric ** none. 37210196Seric */ 37310196Seric 37410196Seric closexscript(e) 37510196Seric register ENVELOPE *e; 37610196Seric { 37710196Seric if (e->e_xfp == NULL) 37810196Seric return; 37958680Seric (void) xfclose(e->e_xfp, "closexscript", e->e_id); 38010196Seric e->e_xfp = NULL; 38110196Seric } 38210196Seric /* 3839536Seric ** SETSENDER -- set the person who this message is from 3849536Seric ** 3859536Seric ** Under certain circumstances allow the user to say who 3869536Seric ** s/he is (using -f or -r). These are: 3879536Seric ** 1. The user's uid is zero (root). 3889536Seric ** 2. The user's login name is in an approved list (typically 3899536Seric ** from a network server). 3909536Seric ** 3. The address the user is trying to claim has a 3919536Seric ** "!" character in it (since #2 doesn't do it for 3929536Seric ** us if we are dialing out for UUCP). 3939536Seric ** A better check to replace #3 would be if the 3949536Seric ** effective uid is "UUCP" -- this would require me 3959536Seric ** to rewrite getpwent to "grab" uucp as it went by, 3969536Seric ** make getname more nasty, do another passwd file 3979536Seric ** scan, or compile the UID of "UUCP" into the code, 3989536Seric ** all of which are reprehensible. 3999536Seric ** 4009536Seric ** Assuming all of these fail, we figure out something 4019536Seric ** ourselves. 4029536Seric ** 4039536Seric ** Parameters: 4049536Seric ** from -- the person we would like to believe this message 4059536Seric ** is from, as specified on the command line. 40653182Seric ** e -- the envelope in which we would like the sender set. 40758333Seric ** delimptr -- if non-NULL, set to the location of the 40858333Seric ** trailing delimiter. 40958704Seric ** internal -- set if this address is coming from an internal 41058704Seric ** source such as an owner alias. 4119536Seric ** 4129536Seric ** Returns: 41358704Seric ** none. 4149536Seric ** 4159536Seric ** Side Effects: 4169536Seric ** sets sendmail's notion of who the from person is. 4179536Seric */ 4189536Seric 41958704Seric setsender(from, e, delimptr, internal) 4209536Seric char *from; 42153182Seric register ENVELOPE *e; 42258333Seric char **delimptr; 42358704Seric bool internal; 4249536Seric { 4259536Seric register char **pvp; 4269536Seric char *realname = NULL; 42718665Seric register struct passwd *pw; 428*58727Seric char delimchar; 4299536Seric char buf[MAXNAME]; 43016913Seric char pvpbuf[PSBUFSIZE]; 43118665Seric extern struct passwd *getpwnam(); 4329536Seric extern char *macvalue(); 4339536Seric extern char **prescan(); 4349536Seric extern char *FullName; 4359536Seric 4369536Seric if (tTd(45, 1)) 43714786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 4389536Seric 4399536Seric /* 4409536Seric ** Figure out the real user executing us. 4419536Seric ** Username can return errno != 0 on non-errors. 4429536Seric */ 4439536Seric 44452220Seric if (QueueRun || OpMode == MD_SMTP) 4459536Seric realname = from; 4469536Seric if (realname == NULL || realname[0] == '\0') 4479536Seric { 4489536Seric extern char *username(); 4499536Seric 4509536Seric realname = username(); 4519536Seric } 4529536Seric 45357589Seric /* 4549536Seric SuprErrs = TRUE; 45557589Seric */ 456*58727Seric delimchar = internal ? '\0' : ' '; 45758333Seric if (from == NULL || 458*58727Seric parseaddr(from, &e->e_from, 1, delimchar, delimptr, e) == NULL) 4599536Seric { 46021750Seric /* log garbage addresses for traceback */ 46155173Seric # ifdef LOG 46258020Seric if (from != NULL && LogLevel > 2) 46321750Seric { 46455173Seric char *host = RealHostName; 46555173Seric 46655173Seric if (host == NULL) 46755173Seric host = MyHostName; 46855173Seric syslog(LOG_NOTICE, 46955173Seric "from=%s unparseable, received from %s@%s", 47055173Seric from, realname, host); 47155173Seric } 47256795Seric # endif /* LOG */ 47357589Seric if (from != NULL) 47457589Seric SuprErrs = TRUE; 47557589Seric if (from == realname || 47658333Seric parseaddr(from = newstr(realname), &e->e_from, 1, ' ', NULL, e) == NULL) 47724944Seric { 47857589Seric SuprErrs = TRUE; 47958333Seric if (parseaddr("postmaster", &e->e_from, 1, ' ', NULL, e) == NULL) 48058151Seric syserr("553 setsender: can't even parse postmaster!"); 48124944Seric } 4829536Seric } 4839536Seric else 4849536Seric FromFlag = TRUE; 48553182Seric e->e_from.q_flags |= QDONTSEND; 48657731Seric if (tTd(45, 5)) 48757731Seric { 48857731Seric printf("setsender: QDONTSEND "); 48957731Seric printaddr(&e->e_from, FALSE); 49057731Seric } 4919536Seric SuprErrs = FALSE; 4929536Seric 49353736Seric pvp = NULL; 49453736Seric if (e->e_from.q_mailer == LocalMailer) 4959536Seric { 49653736Seric # ifdef USERDB 49753736Seric register char *p; 49853736Seric extern char *udbsender(); 49953736Seric # endif 50017472Seric 50158704Seric if (!internal) 50258704Seric { 50358704Seric /* if the user has given fullname already, don't redefine */ 50458704Seric if (FullName == NULL) 50558704Seric FullName = macvalue('x', e); 50658704Seric if (FullName != NULL && FullName[0] == '\0') 50758704Seric FullName = NULL; 5089536Seric 50953736Seric # ifdef USERDB 51058704Seric p = udbsender(from); 51153736Seric 51258704Seric if (p != NULL) 51358704Seric { 51458704Seric /* 51558704Seric ** We have an alternate address for the sender 51658704Seric */ 51753736Seric 51858704Seric pvp = prescan(p, '\0', pvpbuf, NULL); 51958704Seric } 52058704Seric # endif /* USERDB */ 5219536Seric } 52253736Seric 52353736Seric if ((pw = getpwnam(e->e_from.q_user)) != NULL) 52453736Seric { 52553736Seric /* 52653736Seric ** Process passwd file entry. 52753736Seric */ 52853736Seric 52953736Seric 53053736Seric /* extract home directory */ 53153736Seric e->e_from.q_home = newstr(pw->pw_dir); 53253736Seric define('z', e->e_from.q_home, e); 53353736Seric 53453736Seric /* extract user and group id */ 53553736Seric e->e_from.q_uid = pw->pw_uid; 53653736Seric e->e_from.q_gid = pw->pw_gid; 53753736Seric 53853736Seric /* extract full name from passwd file */ 53953736Seric if (FullName == NULL && pw->pw_gecos != NULL && 54058704Seric strcmp(pw->pw_name, e->e_from.q_user) == 0 && 54158704Seric !internal) 54253736Seric { 54353736Seric buildfname(pw->pw_gecos, e->e_from.q_user, buf); 54453736Seric if (buf[0] != '\0') 54553736Seric FullName = newstr(buf); 54653736Seric } 54753736Seric } 54858704Seric if (FullName != NULL && !internal) 54953182Seric define('x', FullName, e); 5509536Seric } 55158704Seric else if (!internal) 55211625Seric { 55353182Seric if (e->e_from.q_home == NULL) 55453182Seric e->e_from.q_home = getenv("HOME"); 55553182Seric e->e_from.q_uid = getuid(); 55653182Seric e->e_from.q_gid = getgid(); 55711625Seric } 55811625Seric 5599536Seric /* 5609536Seric ** Rewrite the from person to dispose of possible implicit 5619536Seric ** links in the net. 5629536Seric */ 5639536Seric 5649536Seric if (pvp == NULL) 56558333Seric pvp = prescan(from, '\0', pvpbuf, NULL); 56653736Seric if (pvp == NULL) 5679536Seric { 56858403Seric /* don't need to give error -- prescan did that already */ 56936233Skarels # ifdef LOG 57058020Seric if (LogLevel > 2) 57136233Skarels syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 57236233Skarels # endif 5739536Seric finis(); 5749536Seric } 5759536Seric rewrite(pvp, 3); 5769536Seric rewrite(pvp, 1); 57725032Seric rewrite(pvp, 4); 57858704Seric cataddr(pvp, buf, sizeof buf, '\0'); 57958704Seric e->e_sender = newstr(buf); 58058704Seric define('f', e->e_sender, e); 5819536Seric 5829536Seric /* save the domain spec if this mailer wants it */ 58358704Seric if (!internal && e->e_from.q_mailer != NULL && 58453182Seric bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 5859536Seric { 5869536Seric extern char **copyplist(); 5879536Seric 5889536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 5899536Seric pvp++; 5909536Seric if (*pvp != NULL) 59153182Seric e->e_fromdomain = copyplist(pvp, TRUE); 5929536Seric } 5939536Seric } 594