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*58737Seric static char sccsid[] = "@(#)envelope.c 6.26 (Berkeley) 03/19/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 12458734Seric if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && 12558734Seric e->e_errormode != EM_QUIET) 1269536Seric savemail(e); 1279536Seric 1289536Seric /* 1299536Seric ** Instantiate or deinstantiate the queue. 1309536Seric */ 1319536Seric 1329536Seric if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 1339536Seric bitset(EF_CLRQUEUE, e->e_flags)) 1349536Seric { 13523497Seric if (e->e_df != NULL) 13623497Seric xunlink(e->e_df); 1379536Seric xunlink(queuename(e, 'q')); 1389536Seric } 1399536Seric else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 14010754Seric { 14110754Seric #ifdef QUEUE 14251920Seric queueup(e, FALSE, FALSE); 14356795Seric #else /* QUEUE */ 14458151Seric syserr("554 dropenvelope: queueup"); 14556795Seric #endif /* QUEUE */ 14610754Seric } 1479536Seric 1489536Seric /* now unlock the job */ 14910196Seric closexscript(e); 1509536Seric unlockqueue(e); 1519536Seric 1529536Seric /* make sure that this envelope is marked unused */ 15324944Seric if (e->e_dfp != NULL) 15458680Seric (void) xfclose(e->e_dfp, "dropenvelope", e->e_df); 15510196Seric e->e_dfp = NULL; 15658680Seric e->e_id = e->e_df = NULL; 15757589Seric 15857589Seric #ifdef LOG 15958020Seric if (LogLevel > 74) 16057943Seric syslog(LOG_INFO, "%s: done", id); 16157589Seric #endif /* LOG */ 1629536Seric } 1639536Seric /* 1649536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 1659536Seric ** 1669536Seric ** This is normally used by a child process to get a clean 1679536Seric ** envelope without disturbing the parent. 1689536Seric ** 1699536Seric ** Parameters: 1709536Seric ** e -- the envelope to clear. 17125611Seric ** fullclear - if set, the current envelope is total 17225611Seric ** garbage and should be ignored; otherwise, 17325611Seric ** release any resources it may indicate. 1749536Seric ** 1759536Seric ** Returns: 1769536Seric ** none. 1779536Seric ** 1789536Seric ** Side Effects: 1799536Seric ** Closes files associated with the envelope. 1809536Seric ** Marks the envelope as unallocated. 1819536Seric */ 1829536Seric 18325611Seric clearenvelope(e, fullclear) 1849536Seric register ENVELOPE *e; 18525611Seric bool fullclear; 1869536Seric { 18725514Seric register HDR *bh; 18825514Seric register HDR **nhp; 18925514Seric extern ENVELOPE BlankEnvelope; 19025514Seric 19125611Seric if (!fullclear) 19225611Seric { 19325611Seric /* clear out any file information */ 19425611Seric if (e->e_xfp != NULL) 19558680Seric (void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id); 19625611Seric if (e->e_dfp != NULL) 19758680Seric (void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_df); 19858680Seric e->e_xfp = e->e_dfp = NULL; 19925611Seric } 2009536Seric 20124961Seric /* now clear out the data */ 20224965Seric STRUCTCOPY(BlankEnvelope, *e); 20325514Seric bh = BlankEnvelope.e_header; 20425514Seric nhp = &e->e_header; 20525514Seric while (bh != NULL) 20625514Seric { 20725514Seric *nhp = (HDR *) xalloc(sizeof *bh); 20825514Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 20925514Seric bh = bh->h_link; 21025514Seric nhp = &(*nhp)->h_link; 21125514Seric } 2129536Seric } 2139536Seric /* 2149536Seric ** INITSYS -- initialize instantiation of system 2159536Seric ** 2169536Seric ** In Daemon mode, this is done in the child. 2179536Seric ** 2189536Seric ** Parameters: 2199536Seric ** none. 2209536Seric ** 2219536Seric ** Returns: 2229536Seric ** none. 2239536Seric ** 2249536Seric ** Side Effects: 2259536Seric ** Initializes the system macros, some global variables, 2269536Seric ** etc. In particular, the current time in various 2279536Seric ** forms is set. 2289536Seric */ 2299536Seric 23055012Seric initsys(e) 23155012Seric register ENVELOPE *e; 2329536Seric { 2339536Seric static char cbuf[5]; /* holds hop count */ 2349536Seric static char pbuf[10]; /* holds pid */ 23522963Smiriam #ifdef TTYNAME 2369536Seric static char ybuf[10]; /* holds tty id */ 2379536Seric register char *p; 23856795Seric #endif /* TTYNAME */ 2399536Seric extern char *ttyname(); 2409536Seric extern char *macvalue(); 2419536Seric extern char Version[]; 2429536Seric 2439536Seric /* 2449536Seric ** Give this envelope a reality. 2459536Seric ** I.e., an id, a transcript, and a creation time. 2469536Seric */ 2479536Seric 24855012Seric openxscript(e); 24955012Seric e->e_ctime = curtime(); 2509536Seric 2519536Seric /* 2529536Seric ** Set OutChannel to something useful if stdout isn't it. 2539536Seric ** This arranges that any extra stuff the mailer produces 2549536Seric ** gets sent back to the user on error (because it is 2559536Seric ** tucked away in the transcript). 2569536Seric */ 2579536Seric 258*58737Seric if (OpMode == MD_DAEMON && !bitset(EF_QUEUERUN, e->e_flags) && 259*58737Seric e->e_xfp != NULL) 26055012Seric OutChannel = e->e_xfp; 2619536Seric 2629536Seric /* 2639536Seric ** Set up some basic system macros. 2649536Seric */ 2659536Seric 2669536Seric /* process id */ 2679536Seric (void) sprintf(pbuf, "%d", getpid()); 26855012Seric define('p', pbuf, e); 2699536Seric 2709536Seric /* hop count */ 27155012Seric (void) sprintf(cbuf, "%d", e->e_hopcount); 27255012Seric define('c', cbuf, e); 2739536Seric 2749536Seric /* time as integer, unix time, arpa time */ 27555012Seric settime(e); 2769536Seric 27717472Seric #ifdef TTYNAME 2789536Seric /* tty name */ 27955012Seric if (macvalue('y', e) == NULL) 2809536Seric { 2819536Seric p = ttyname(2); 2829536Seric if (p != NULL) 2839536Seric { 28456795Seric if (strrchr(p, '/') != NULL) 28556795Seric p = strrchr(p, '/') + 1; 2869536Seric (void) strcpy(ybuf, p); 28755012Seric define('y', ybuf, e); 2889536Seric } 2899536Seric } 29056795Seric #endif /* TTYNAME */ 2919536Seric } 2929536Seric /* 29311932Seric ** SETTIME -- set the current time. 29411932Seric ** 29511932Seric ** Parameters: 29611932Seric ** none. 29711932Seric ** 29811932Seric ** Returns: 29911932Seric ** none. 30011932Seric ** 30111932Seric ** Side Effects: 30211932Seric ** Sets the various time macros -- $a, $b, $d, $t. 30311932Seric */ 30411932Seric 30555012Seric settime(e) 30655012Seric register ENVELOPE *e; 30711932Seric { 30811932Seric register char *p; 30911932Seric auto time_t now; 31011932Seric static char tbuf[20]; /* holds "current" time */ 31111932Seric static char dbuf[30]; /* holds ctime(tbuf) */ 31211932Seric register struct tm *tm; 31311932Seric extern char *arpadate(); 31411932Seric extern struct tm *gmtime(); 31511932Seric extern char *macvalue(); 31611932Seric 31711932Seric now = curtime(); 31811932Seric tm = gmtime(&now); 31957014Seric (void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900, 32057014Seric tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min); 32155012Seric define('t', tbuf, e); 32211932Seric (void) strcpy(dbuf, ctime(&now)); 32358131Seric p = strchr(dbuf, '\n'); 32458131Seric if (p != NULL) 32558131Seric *p = '\0'; 32658131Seric define('d', dbuf, e); 32711932Seric p = newstr(arpadate(dbuf)); 32855012Seric if (macvalue('a', e) == NULL) 32955012Seric define('a', p, e); 33055012Seric define('b', p, e); 33111932Seric } 33211932Seric /* 3339536Seric ** OPENXSCRIPT -- Open transcript file 3349536Seric ** 3359536Seric ** Creates a transcript file for possible eventual mailing or 3369536Seric ** sending back. 3379536Seric ** 3389536Seric ** Parameters: 3399536Seric ** e -- the envelope to create the transcript in/for. 3409536Seric ** 3419536Seric ** Returns: 3429536Seric ** none 3439536Seric ** 3449536Seric ** Side Effects: 3459536Seric ** Creates the transcript file. 3469536Seric */ 3479536Seric 3489536Seric openxscript(e) 3499536Seric register ENVELOPE *e; 3509536Seric { 3519536Seric register char *p; 35240933Srick int fd; 3539536Seric 3549536Seric if (e->e_xfp != NULL) 3559536Seric return; 3569536Seric p = queuename(e, 'x'); 35740933Srick fd = open(p, O_WRONLY|O_CREAT, 0644); 35840933Srick if (fd < 0) 3599536Seric syserr("Can't create %s", p); 3609536Seric else 36140933Srick e->e_xfp = fdopen(fd, "w"); 3629536Seric } 3639536Seric /* 36410196Seric ** CLOSEXSCRIPT -- close the transcript file. 36510196Seric ** 36610196Seric ** Parameters: 36710196Seric ** e -- the envelope containing the transcript to close. 36810196Seric ** 36910196Seric ** Returns: 37010196Seric ** none. 37110196Seric ** 37210196Seric ** Side Effects: 37310196Seric ** none. 37410196Seric */ 37510196Seric 37610196Seric closexscript(e) 37710196Seric register ENVELOPE *e; 37810196Seric { 37910196Seric if (e->e_xfp == NULL) 38010196Seric return; 38158680Seric (void) xfclose(e->e_xfp, "closexscript", e->e_id); 38210196Seric e->e_xfp = NULL; 38310196Seric } 38410196Seric /* 3859536Seric ** SETSENDER -- set the person who this message is from 3869536Seric ** 3879536Seric ** Under certain circumstances allow the user to say who 3889536Seric ** s/he is (using -f or -r). These are: 3899536Seric ** 1. The user's uid is zero (root). 3909536Seric ** 2. The user's login name is in an approved list (typically 3919536Seric ** from a network server). 3929536Seric ** 3. The address the user is trying to claim has a 3939536Seric ** "!" character in it (since #2 doesn't do it for 3949536Seric ** us if we are dialing out for UUCP). 3959536Seric ** A better check to replace #3 would be if the 3969536Seric ** effective uid is "UUCP" -- this would require me 3979536Seric ** to rewrite getpwent to "grab" uucp as it went by, 3989536Seric ** make getname more nasty, do another passwd file 3999536Seric ** scan, or compile the UID of "UUCP" into the code, 4009536Seric ** all of which are reprehensible. 4019536Seric ** 4029536Seric ** Assuming all of these fail, we figure out something 4039536Seric ** ourselves. 4049536Seric ** 4059536Seric ** Parameters: 4069536Seric ** from -- the person we would like to believe this message 4079536Seric ** is from, as specified on the command line. 40853182Seric ** e -- the envelope in which we would like the sender set. 40958333Seric ** delimptr -- if non-NULL, set to the location of the 41058333Seric ** trailing delimiter. 41158704Seric ** internal -- set if this address is coming from an internal 41258704Seric ** source such as an owner alias. 4139536Seric ** 4149536Seric ** Returns: 41558704Seric ** none. 4169536Seric ** 4179536Seric ** Side Effects: 4189536Seric ** sets sendmail's notion of who the from person is. 4199536Seric */ 4209536Seric 42158704Seric setsender(from, e, delimptr, internal) 4229536Seric char *from; 42353182Seric register ENVELOPE *e; 42458333Seric char **delimptr; 42558704Seric bool internal; 4269536Seric { 4279536Seric register char **pvp; 4289536Seric char *realname = NULL; 42918665Seric register struct passwd *pw; 43058727Seric char delimchar; 4319536Seric char buf[MAXNAME]; 43216913Seric char pvpbuf[PSBUFSIZE]; 43318665Seric extern struct passwd *getpwnam(); 4349536Seric extern char *macvalue(); 4359536Seric extern char **prescan(); 4369536Seric extern char *FullName; 4379536Seric 4389536Seric if (tTd(45, 1)) 43914786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 4409536Seric 4419536Seric /* 4429536Seric ** Figure out the real user executing us. 4439536Seric ** Username can return errno != 0 on non-errors. 4449536Seric */ 4459536Seric 446*58737Seric if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP) 4479536Seric realname = from; 4489536Seric if (realname == NULL || realname[0] == '\0') 4499536Seric { 4509536Seric extern char *username(); 4519536Seric 4529536Seric realname = username(); 4539536Seric } 4549536Seric 45557589Seric /* 4569536Seric SuprErrs = TRUE; 45757589Seric */ 45858727Seric delimchar = internal ? '\0' : ' '; 45958333Seric if (from == NULL || 46058727Seric parseaddr(from, &e->e_from, 1, delimchar, delimptr, e) == NULL) 4619536Seric { 46221750Seric /* log garbage addresses for traceback */ 46355173Seric # ifdef LOG 46458020Seric if (from != NULL && LogLevel > 2) 46521750Seric { 46655173Seric char *host = RealHostName; 46755173Seric 46855173Seric if (host == NULL) 46955173Seric host = MyHostName; 47055173Seric syslog(LOG_NOTICE, 47155173Seric "from=%s unparseable, received from %s@%s", 47255173Seric from, realname, host); 47355173Seric } 47456795Seric # endif /* LOG */ 47557589Seric if (from != NULL) 47657589Seric SuprErrs = TRUE; 47757589Seric if (from == realname || 47858333Seric parseaddr(from = newstr(realname), &e->e_from, 1, ' ', NULL, e) == NULL) 47924944Seric { 48057589Seric SuprErrs = TRUE; 48158333Seric if (parseaddr("postmaster", &e->e_from, 1, ' ', NULL, e) == NULL) 48258151Seric syserr("553 setsender: can't even parse postmaster!"); 48324944Seric } 4849536Seric } 4859536Seric else 4869536Seric FromFlag = TRUE; 48753182Seric e->e_from.q_flags |= QDONTSEND; 48857731Seric if (tTd(45, 5)) 48957731Seric { 49057731Seric printf("setsender: QDONTSEND "); 49157731Seric printaddr(&e->e_from, FALSE); 49257731Seric } 4939536Seric SuprErrs = FALSE; 4949536Seric 49553736Seric pvp = NULL; 49653736Seric if (e->e_from.q_mailer == LocalMailer) 4979536Seric { 49853736Seric # ifdef USERDB 49953736Seric register char *p; 50053736Seric extern char *udbsender(); 50153736Seric # endif 50217472Seric 50358704Seric if (!internal) 50458704Seric { 50558704Seric /* if the user has given fullname already, don't redefine */ 50658704Seric if (FullName == NULL) 50758704Seric FullName = macvalue('x', e); 50858704Seric if (FullName != NULL && FullName[0] == '\0') 50958704Seric FullName = NULL; 5109536Seric 51153736Seric # ifdef USERDB 51258704Seric p = udbsender(from); 51353736Seric 51458704Seric if (p != NULL) 51558704Seric { 51658704Seric /* 51758704Seric ** We have an alternate address for the sender 51858704Seric */ 51953736Seric 52058704Seric pvp = prescan(p, '\0', pvpbuf, NULL); 52158704Seric } 52258704Seric # endif /* USERDB */ 5239536Seric } 52453736Seric 52553736Seric if ((pw = getpwnam(e->e_from.q_user)) != NULL) 52653736Seric { 52753736Seric /* 52853736Seric ** Process passwd file entry. 52953736Seric */ 53053736Seric 53153736Seric 53253736Seric /* extract home directory */ 53353736Seric e->e_from.q_home = newstr(pw->pw_dir); 53453736Seric define('z', e->e_from.q_home, e); 53553736Seric 53653736Seric /* extract user and group id */ 53753736Seric e->e_from.q_uid = pw->pw_uid; 53853736Seric e->e_from.q_gid = pw->pw_gid; 53953736Seric 54053736Seric /* extract full name from passwd file */ 54153736Seric if (FullName == NULL && pw->pw_gecos != NULL && 54258704Seric strcmp(pw->pw_name, e->e_from.q_user) == 0 && 54358704Seric !internal) 54453736Seric { 54553736Seric buildfname(pw->pw_gecos, e->e_from.q_user, buf); 54653736Seric if (buf[0] != '\0') 54753736Seric FullName = newstr(buf); 54853736Seric } 54953736Seric } 55058704Seric if (FullName != NULL && !internal) 55153182Seric define('x', FullName, e); 5529536Seric } 55358704Seric else if (!internal) 55411625Seric { 55553182Seric if (e->e_from.q_home == NULL) 55653182Seric e->e_from.q_home = getenv("HOME"); 55753182Seric e->e_from.q_uid = getuid(); 55853182Seric e->e_from.q_gid = getgid(); 55911625Seric } 56011625Seric 5619536Seric /* 5629536Seric ** Rewrite the from person to dispose of possible implicit 5639536Seric ** links in the net. 5649536Seric */ 5659536Seric 5669536Seric if (pvp == NULL) 56758333Seric pvp = prescan(from, '\0', pvpbuf, NULL); 56853736Seric if (pvp == NULL) 5699536Seric { 57058403Seric /* don't need to give error -- prescan did that already */ 57136233Skarels # ifdef LOG 57258020Seric if (LogLevel > 2) 57336233Skarels syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 57436233Skarels # endif 5759536Seric finis(); 5769536Seric } 5779536Seric rewrite(pvp, 3); 5789536Seric rewrite(pvp, 1); 57925032Seric rewrite(pvp, 4); 58058704Seric cataddr(pvp, buf, sizeof buf, '\0'); 58158704Seric e->e_sender = newstr(buf); 58258704Seric define('f', e->e_sender, e); 5839536Seric 5849536Seric /* save the domain spec if this mailer wants it */ 58558704Seric if (!internal && e->e_from.q_mailer != NULL && 58653182Seric bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 5879536Seric { 5889536Seric extern char **copyplist(); 5899536Seric 5909536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 5919536Seric pvp++; 5929536Seric if (*pvp != NULL) 59353182Seric e->e_fromdomain = copyplist(pvp, TRUE); 5949536Seric } 5959536Seric } 596