122704Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 3*62525Sbostic * Copyright (c) 1988, 1993 4*62525Sbostic * The Regents of the University of California. All rights reserved. 533729Sbostic * 642826Sbostic * %sccs.include.redist.c% 733729Sbostic */ 822704Sdist 922704Sdist #ifndef lint 10*62525Sbostic static char sccsid[] = "@(#)envelope.c 8.1 (Berkeley) 06/07/93"; 1133729Sbostic #endif /* not lint */ 1222704Sdist 1358332Seric #include "sendmail.h" 1436928Sbostic #include <sys/time.h> 159536Seric #include <pwd.h> 169536Seric 179536Seric /* 189536Seric ** NEWENVELOPE -- allocate a new envelope 199536Seric ** 209536Seric ** Supports inheritance. 219536Seric ** 229536Seric ** Parameters: 239536Seric ** e -- the new envelope to fill in. 2458179Seric ** parent -- the envelope to be the parent of e. 259536Seric ** 269536Seric ** Returns: 279536Seric ** e. 289536Seric ** 299536Seric ** Side Effects: 309536Seric ** none. 319536Seric */ 329536Seric 339536Seric ENVELOPE * 3458179Seric newenvelope(e, parent) 359536Seric register ENVELOPE *e; 3658179Seric register ENVELOPE *parent; 379536Seric { 389536Seric extern putheader(), putbody(); 3925611Seric extern ENVELOPE BlankEnvelope; 409536Seric 4158179Seric if (e == parent && e->e_parent != NULL) 429536Seric parent = e->e_parent; 4325611Seric clearenvelope(e, TRUE); 4424944Seric if (e == CurEnv) 4524944Seric bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from); 4624944Seric else 4724944Seric bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from); 489536Seric e->e_parent = parent; 499536Seric e->e_ctime = curtime(); 5056215Seric if (parent != NULL) 5156215Seric e->e_msgpriority = parent->e_msgsize; 529536Seric e->e_puthdr = putheader; 539536Seric e->e_putbody = putbody; 549536Seric if (CurEnv->e_xfp != NULL) 559536Seric (void) fflush(CurEnv->e_xfp); 569536Seric 579536Seric return (e); 589536Seric } 599536Seric /* 609536Seric ** DROPENVELOPE -- deallocate an envelope. 619536Seric ** 629536Seric ** Parameters: 639536Seric ** e -- the envelope to deallocate. 649536Seric ** 659536Seric ** Returns: 669536Seric ** none. 679536Seric ** 689536Seric ** Side Effects: 699536Seric ** housekeeping necessary to dispose of an envelope. 709536Seric ** Unlocks this queue file. 719536Seric */ 729536Seric 7360494Seric void 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 18360494Seric void 18425611Seric clearenvelope(e, fullclear) 1859536Seric register ENVELOPE *e; 18625611Seric bool fullclear; 1879536Seric { 18825514Seric register HDR *bh; 18925514Seric register HDR **nhp; 19025514Seric extern ENVELOPE BlankEnvelope; 19125514Seric 19225611Seric if (!fullclear) 19325611Seric { 19425611Seric /* clear out any file information */ 19525611Seric if (e->e_xfp != NULL) 19658680Seric (void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id); 19725611Seric if (e->e_dfp != NULL) 19858680Seric (void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_df); 19958680Seric e->e_xfp = e->e_dfp = NULL; 20025611Seric } 2019536Seric 20224961Seric /* now clear out the data */ 20324965Seric STRUCTCOPY(BlankEnvelope, *e); 20459698Seric if (Verbose) 20559698Seric e->e_sendmode = SM_DELIVER; 20625514Seric bh = BlankEnvelope.e_header; 20725514Seric nhp = &e->e_header; 20825514Seric while (bh != NULL) 20925514Seric { 21025514Seric *nhp = (HDR *) xalloc(sizeof *bh); 21125514Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 21225514Seric bh = bh->h_link; 21325514Seric nhp = &(*nhp)->h_link; 21425514Seric } 2159536Seric } 2169536Seric /* 2179536Seric ** INITSYS -- initialize instantiation of system 2189536Seric ** 2199536Seric ** In Daemon mode, this is done in the child. 2209536Seric ** 2219536Seric ** Parameters: 2229536Seric ** none. 2239536Seric ** 2249536Seric ** Returns: 2259536Seric ** none. 2269536Seric ** 2279536Seric ** Side Effects: 2289536Seric ** Initializes the system macros, some global variables, 2299536Seric ** etc. In particular, the current time in various 2309536Seric ** forms is set. 2319536Seric */ 2329536Seric 23360494Seric void 23455012Seric initsys(e) 23555012Seric register ENVELOPE *e; 2369536Seric { 2379536Seric static char cbuf[5]; /* holds hop count */ 2389536Seric static char pbuf[10]; /* holds pid */ 23922963Smiriam #ifdef TTYNAME 24059304Seric static char ybuf[60]; /* holds tty id */ 2419536Seric register char *p; 24256795Seric #endif /* TTYNAME */ 2439536Seric extern char *ttyname(); 24460494Seric extern void settime(); 2459536Seric extern char Version[]; 2469536Seric 2479536Seric /* 2489536Seric ** Give this envelope a reality. 2499536Seric ** I.e., an id, a transcript, and a creation time. 2509536Seric */ 2519536Seric 25255012Seric openxscript(e); 25355012Seric e->e_ctime = curtime(); 2549536Seric 2559536Seric /* 2569536Seric ** Set OutChannel to something useful if stdout isn't it. 2579536Seric ** This arranges that any extra stuff the mailer produces 2589536Seric ** gets sent back to the user on error (because it is 2599536Seric ** tucked away in the transcript). 2609536Seric */ 2619536Seric 26258737Seric if (OpMode == MD_DAEMON && !bitset(EF_QUEUERUN, e->e_flags) && 26358737Seric e->e_xfp != NULL) 26455012Seric OutChannel = e->e_xfp; 2659536Seric 2669536Seric /* 2679536Seric ** Set up some basic system macros. 2689536Seric */ 2699536Seric 2709536Seric /* process id */ 2719536Seric (void) sprintf(pbuf, "%d", getpid()); 27255012Seric define('p', pbuf, e); 2739536Seric 2749536Seric /* hop count */ 27555012Seric (void) sprintf(cbuf, "%d", e->e_hopcount); 27655012Seric define('c', cbuf, e); 2779536Seric 2789536Seric /* time as integer, unix time, arpa time */ 27955012Seric settime(e); 2809536Seric 28117472Seric #ifdef TTYNAME 2829536Seric /* tty name */ 28355012Seric if (macvalue('y', e) == NULL) 2849536Seric { 2859536Seric p = ttyname(2); 2869536Seric if (p != NULL) 2879536Seric { 28856795Seric if (strrchr(p, '/') != NULL) 28956795Seric p = strrchr(p, '/') + 1; 2909536Seric (void) strcpy(ybuf, p); 29155012Seric define('y', ybuf, e); 2929536Seric } 2939536Seric } 29456795Seric #endif /* TTYNAME */ 2959536Seric } 2969536Seric /* 29711932Seric ** SETTIME -- set the current time. 29811932Seric ** 29911932Seric ** Parameters: 30011932Seric ** none. 30111932Seric ** 30211932Seric ** Returns: 30311932Seric ** none. 30411932Seric ** 30511932Seric ** Side Effects: 30611932Seric ** Sets the various time macros -- $a, $b, $d, $t. 30711932Seric */ 30811932Seric 30960494Seric void 31055012Seric settime(e) 31155012Seric register ENVELOPE *e; 31211932Seric { 31311932Seric register char *p; 31411932Seric auto time_t now; 31511932Seric static char tbuf[20]; /* holds "current" time */ 31611932Seric static char dbuf[30]; /* holds ctime(tbuf) */ 31711932Seric register struct tm *tm; 31811932Seric extern char *arpadate(); 31911932Seric extern struct tm *gmtime(); 32011932Seric 32111932Seric now = curtime(); 32211932Seric tm = gmtime(&now); 32357014Seric (void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900, 32457014Seric tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min); 32555012Seric define('t', tbuf, e); 32611932Seric (void) strcpy(dbuf, ctime(&now)); 32758131Seric p = strchr(dbuf, '\n'); 32858131Seric if (p != NULL) 32958131Seric *p = '\0'; 33058131Seric define('d', dbuf, e); 33111932Seric p = newstr(arpadate(dbuf)); 33255012Seric if (macvalue('a', e) == NULL) 33355012Seric define('a', p, e); 33455012Seric define('b', p, e); 33511932Seric } 33611932Seric /* 3379536Seric ** OPENXSCRIPT -- Open transcript file 3389536Seric ** 3399536Seric ** Creates a transcript file for possible eventual mailing or 3409536Seric ** sending back. 3419536Seric ** 3429536Seric ** Parameters: 3439536Seric ** e -- the envelope to create the transcript in/for. 3449536Seric ** 3459536Seric ** Returns: 3469536Seric ** none 3479536Seric ** 3489536Seric ** Side Effects: 3499536Seric ** Creates the transcript file. 3509536Seric */ 3519536Seric 35258803Seric #ifndef O_APPEND 35358803Seric #define O_APPEND 0 35458803Seric #endif 35558803Seric 35660494Seric void 3579536Seric openxscript(e) 3589536Seric register ENVELOPE *e; 3599536Seric { 3609536Seric register char *p; 36140933Srick int fd; 3629536Seric 3639536Seric if (e->e_xfp != NULL) 3649536Seric return; 3659536Seric p = queuename(e, 'x'); 36658803Seric fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644); 36740933Srick if (fd < 0) 3689536Seric syserr("Can't create %s", p); 3699536Seric else 37040933Srick e->e_xfp = fdopen(fd, "w"); 3719536Seric } 3729536Seric /* 37310196Seric ** CLOSEXSCRIPT -- close the transcript file. 37410196Seric ** 37510196Seric ** Parameters: 37610196Seric ** e -- the envelope containing the transcript to close. 37710196Seric ** 37810196Seric ** Returns: 37910196Seric ** none. 38010196Seric ** 38110196Seric ** Side Effects: 38210196Seric ** none. 38310196Seric */ 38410196Seric 38560494Seric void 38610196Seric closexscript(e) 38710196Seric register ENVELOPE *e; 38810196Seric { 38910196Seric if (e->e_xfp == NULL) 39010196Seric return; 39158680Seric (void) xfclose(e->e_xfp, "closexscript", e->e_id); 39210196Seric e->e_xfp = NULL; 39310196Seric } 39410196Seric /* 3959536Seric ** SETSENDER -- set the person who this message is from 3969536Seric ** 3979536Seric ** Under certain circumstances allow the user to say who 3989536Seric ** s/he is (using -f or -r). These are: 3999536Seric ** 1. The user's uid is zero (root). 4009536Seric ** 2. The user's login name is in an approved list (typically 4019536Seric ** from a network server). 4029536Seric ** 3. The address the user is trying to claim has a 4039536Seric ** "!" character in it (since #2 doesn't do it for 4049536Seric ** us if we are dialing out for UUCP). 4059536Seric ** A better check to replace #3 would be if the 4069536Seric ** effective uid is "UUCP" -- this would require me 4079536Seric ** to rewrite getpwent to "grab" uucp as it went by, 4089536Seric ** make getname more nasty, do another passwd file 4099536Seric ** scan, or compile the UID of "UUCP" into the code, 4109536Seric ** all of which are reprehensible. 4119536Seric ** 4129536Seric ** Assuming all of these fail, we figure out something 4139536Seric ** ourselves. 4149536Seric ** 4159536Seric ** Parameters: 4169536Seric ** from -- the person we would like to believe this message 4179536Seric ** is from, as specified on the command line. 41853182Seric ** e -- the envelope in which we would like the sender set. 41958333Seric ** delimptr -- if non-NULL, set to the location of the 42058333Seric ** trailing delimiter. 42158704Seric ** internal -- set if this address is coming from an internal 42258704Seric ** source such as an owner alias. 4239536Seric ** 4249536Seric ** Returns: 42558704Seric ** none. 4269536Seric ** 4279536Seric ** Side Effects: 4289536Seric ** sets sendmail's notion of who the from person is. 4299536Seric */ 4309536Seric 43160494Seric void 43258704Seric setsender(from, e, delimptr, internal) 4339536Seric char *from; 43453182Seric register ENVELOPE *e; 43558333Seric char **delimptr; 43658704Seric bool internal; 4379536Seric { 4389536Seric register char **pvp; 4399536Seric char *realname = NULL; 44018665Seric register struct passwd *pw; 44158727Seric char delimchar; 4429536Seric char buf[MAXNAME]; 44316913Seric char pvpbuf[PSBUFSIZE]; 44418665Seric extern struct passwd *getpwnam(); 4459536Seric extern char *FullName; 4469536Seric 4479536Seric if (tTd(45, 1)) 44814786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 4499536Seric 4509536Seric /* 4519536Seric ** Figure out the real user executing us. 4529536Seric ** Username can return errno != 0 on non-errors. 4539536Seric */ 4549536Seric 45558737Seric if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP) 4569536Seric realname = from; 4579536Seric if (realname == NULL || realname[0] == '\0') 4589536Seric realname = username(); 4599536Seric 46059027Seric if (ConfigLevel < 2) 46159027Seric SuprErrs = TRUE; 46259027Seric 46358727Seric delimchar = internal ? '\0' : ' '; 46458333Seric if (from == NULL || 46558727Seric parseaddr(from, &e->e_from, 1, delimchar, delimptr, e) == NULL) 4669536Seric { 46721750Seric /* log garbage addresses for traceback */ 46855173Seric # ifdef LOG 46958020Seric if (from != NULL && LogLevel > 2) 47021750Seric { 47158951Seric char *p; 47258951Seric char ebuf[MAXNAME * 2 + 2]; 47355173Seric 47458951Seric p = macvalue('_', e); 47558951Seric if (p == NULL) 47658951Seric { 47758951Seric char *host = RealHostName; 47858951Seric if (host == NULL) 47958951Seric host = MyHostName; 48058951Seric (void) sprintf(ebuf, "%s@%s", realname, host); 48158951Seric p = ebuf; 48258951Seric } 48355173Seric syslog(LOG_NOTICE, 48458951Seric "from=%s unparseable, received from %s", 48558951Seric from, p); 48655173Seric } 48756795Seric # endif /* LOG */ 48857589Seric if (from != NULL) 48957589Seric SuprErrs = TRUE; 49057589Seric if (from == realname || 49158333Seric parseaddr(from = newstr(realname), &e->e_from, 1, ' ', NULL, e) == NULL) 49224944Seric { 49357589Seric SuprErrs = TRUE; 49458333Seric if (parseaddr("postmaster", &e->e_from, 1, ' ', NULL, e) == NULL) 49558151Seric syserr("553 setsender: can't even parse postmaster!"); 49624944Seric } 4979536Seric } 4989536Seric else 4999536Seric FromFlag = TRUE; 50053182Seric e->e_from.q_flags |= QDONTSEND; 50157731Seric if (tTd(45, 5)) 50257731Seric { 50357731Seric printf("setsender: QDONTSEND "); 50457731Seric printaddr(&e->e_from, FALSE); 50557731Seric } 5069536Seric SuprErrs = FALSE; 5079536Seric 50853736Seric pvp = NULL; 50953736Seric if (e->e_from.q_mailer == LocalMailer) 5109536Seric { 51153736Seric # ifdef USERDB 51253736Seric register char *p; 51353736Seric extern char *udbsender(); 51453736Seric # endif 51517472Seric 51658704Seric if (!internal) 51758704Seric { 51858704Seric /* if the user has given fullname already, don't redefine */ 51958704Seric if (FullName == NULL) 52058704Seric FullName = macvalue('x', e); 52158704Seric if (FullName != NULL && FullName[0] == '\0') 52258704Seric FullName = NULL; 5239536Seric 52453736Seric # ifdef USERDB 52558704Seric p = udbsender(from); 52653736Seric 52758704Seric if (p != NULL) 52858704Seric { 52958704Seric /* 53058704Seric ** We have an alternate address for the sender 53158704Seric */ 53253736Seric 53358704Seric pvp = prescan(p, '\0', pvpbuf, NULL); 53458704Seric } 53558704Seric # endif /* USERDB */ 5369536Seric } 53753736Seric 53853736Seric if ((pw = getpwnam(e->e_from.q_user)) != NULL) 53953736Seric { 54053736Seric /* 54153736Seric ** Process passwd file entry. 54253736Seric */ 54353736Seric 54453736Seric 54553736Seric /* extract home directory */ 54653736Seric e->e_from.q_home = newstr(pw->pw_dir); 54753736Seric define('z', e->e_from.q_home, e); 54853736Seric 54953736Seric /* extract user and group id */ 55053736Seric e->e_from.q_uid = pw->pw_uid; 55153736Seric e->e_from.q_gid = pw->pw_gid; 55253736Seric 55353736Seric /* extract full name from passwd file */ 55453736Seric if (FullName == NULL && pw->pw_gecos != NULL && 55558704Seric strcmp(pw->pw_name, e->e_from.q_user) == 0 && 55658704Seric !internal) 55753736Seric { 55853736Seric buildfname(pw->pw_gecos, e->e_from.q_user, buf); 55953736Seric if (buf[0] != '\0') 56053736Seric FullName = newstr(buf); 56153736Seric } 56253736Seric } 56358704Seric if (FullName != NULL && !internal) 56453182Seric define('x', FullName, e); 5659536Seric } 56658704Seric else if (!internal) 56711625Seric { 56853182Seric if (e->e_from.q_home == NULL) 56953182Seric e->e_from.q_home = getenv("HOME"); 57053182Seric e->e_from.q_uid = getuid(); 57153182Seric e->e_from.q_gid = getgid(); 57211625Seric } 57311625Seric 5749536Seric /* 5759536Seric ** Rewrite the from person to dispose of possible implicit 5769536Seric ** links in the net. 5779536Seric */ 5789536Seric 5799536Seric if (pvp == NULL) 58058333Seric pvp = prescan(from, '\0', pvpbuf, NULL); 58153736Seric if (pvp == NULL) 5829536Seric { 58358403Seric /* don't need to give error -- prescan did that already */ 58436233Skarels # ifdef LOG 58558020Seric if (LogLevel > 2) 58636233Skarels syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 58736233Skarels # endif 5889536Seric finis(); 5899536Seric } 59059084Seric (void) rewrite(pvp, 3, e); 59159084Seric (void) rewrite(pvp, 1, e); 59259084Seric (void) rewrite(pvp, 4, e); 59358814Seric cataddr(pvp, NULL, buf, sizeof buf, '\0'); 59458704Seric e->e_sender = newstr(buf); 59558704Seric define('f', e->e_sender, e); 5969536Seric 5979536Seric /* save the domain spec if this mailer wants it */ 59858704Seric if (!internal && e->e_from.q_mailer != NULL && 59953182Seric bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 6009536Seric { 6019536Seric extern char **copyplist(); 6029536Seric 6039536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 6049536Seric pvp++; 6059536Seric if (*pvp != NULL) 60653182Seric e->e_fromdomain = copyplist(pvp, TRUE); 6079536Seric } 6089536Seric } 609