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*59084Seric static char sccsid[] = "@(#)envelope.c 6.31 (Berkeley) 04/14/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 25858737Seric if (OpMode == MD_DAEMON && !bitset(EF_QUEUERUN, e->e_flags) && 25958737Seric 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 34858803Seric #ifndef O_APPEND 34958803Seric #define O_APPEND 0 35058803Seric #endif 35158803Seric 3529536Seric openxscript(e) 3539536Seric register ENVELOPE *e; 3549536Seric { 3559536Seric register char *p; 35640933Srick int fd; 3579536Seric 3589536Seric if (e->e_xfp != NULL) 3599536Seric return; 3609536Seric p = queuename(e, 'x'); 36158803Seric fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644); 36240933Srick if (fd < 0) 3639536Seric syserr("Can't create %s", p); 3649536Seric else 36540933Srick e->e_xfp = fdopen(fd, "w"); 3669536Seric } 3679536Seric /* 36810196Seric ** CLOSEXSCRIPT -- close the transcript file. 36910196Seric ** 37010196Seric ** Parameters: 37110196Seric ** e -- the envelope containing the transcript to close. 37210196Seric ** 37310196Seric ** Returns: 37410196Seric ** none. 37510196Seric ** 37610196Seric ** Side Effects: 37710196Seric ** none. 37810196Seric */ 37910196Seric 38010196Seric closexscript(e) 38110196Seric register ENVELOPE *e; 38210196Seric { 38310196Seric if (e->e_xfp == NULL) 38410196Seric return; 38558680Seric (void) xfclose(e->e_xfp, "closexscript", e->e_id); 38610196Seric e->e_xfp = NULL; 38710196Seric } 38810196Seric /* 3899536Seric ** SETSENDER -- set the person who this message is from 3909536Seric ** 3919536Seric ** Under certain circumstances allow the user to say who 3929536Seric ** s/he is (using -f or -r). These are: 3939536Seric ** 1. The user's uid is zero (root). 3949536Seric ** 2. The user's login name is in an approved list (typically 3959536Seric ** from a network server). 3969536Seric ** 3. The address the user is trying to claim has a 3979536Seric ** "!" character in it (since #2 doesn't do it for 3989536Seric ** us if we are dialing out for UUCP). 3999536Seric ** A better check to replace #3 would be if the 4009536Seric ** effective uid is "UUCP" -- this would require me 4019536Seric ** to rewrite getpwent to "grab" uucp as it went by, 4029536Seric ** make getname more nasty, do another passwd file 4039536Seric ** scan, or compile the UID of "UUCP" into the code, 4049536Seric ** all of which are reprehensible. 4059536Seric ** 4069536Seric ** Assuming all of these fail, we figure out something 4079536Seric ** ourselves. 4089536Seric ** 4099536Seric ** Parameters: 4109536Seric ** from -- the person we would like to believe this message 4119536Seric ** is from, as specified on the command line. 41253182Seric ** e -- the envelope in which we would like the sender set. 41358333Seric ** delimptr -- if non-NULL, set to the location of the 41458333Seric ** trailing delimiter. 41558704Seric ** internal -- set if this address is coming from an internal 41658704Seric ** source such as an owner alias. 4179536Seric ** 4189536Seric ** Returns: 41958704Seric ** none. 4209536Seric ** 4219536Seric ** Side Effects: 4229536Seric ** sets sendmail's notion of who the from person is. 4239536Seric */ 4249536Seric 42558704Seric setsender(from, e, delimptr, internal) 4269536Seric char *from; 42753182Seric register ENVELOPE *e; 42858333Seric char **delimptr; 42958704Seric bool internal; 4309536Seric { 4319536Seric register char **pvp; 4329536Seric char *realname = NULL; 43318665Seric register struct passwd *pw; 43458727Seric char delimchar; 4359536Seric char buf[MAXNAME]; 43616913Seric char pvpbuf[PSBUFSIZE]; 43718665Seric extern struct passwd *getpwnam(); 4389536Seric extern char *macvalue(); 4399536Seric extern char **prescan(); 4409536Seric extern char *FullName; 4419536Seric 4429536Seric if (tTd(45, 1)) 44314786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 4449536Seric 4459536Seric /* 4469536Seric ** Figure out the real user executing us. 4479536Seric ** Username can return errno != 0 on non-errors. 4489536Seric */ 4499536Seric 45058737Seric if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP) 4519536Seric realname = from; 4529536Seric if (realname == NULL || realname[0] == '\0') 4539536Seric { 4549536Seric extern char *username(); 4559536Seric 4569536Seric realname = username(); 4579536Seric } 4589536Seric 45959027Seric if (ConfigLevel < 2) 46059027Seric SuprErrs = TRUE; 46159027Seric 46258727Seric delimchar = internal ? '\0' : ' '; 46358333Seric if (from == NULL || 46458727Seric parseaddr(from, &e->e_from, 1, delimchar, delimptr, e) == NULL) 4659536Seric { 46621750Seric /* log garbage addresses for traceback */ 46755173Seric # ifdef LOG 46858020Seric if (from != NULL && LogLevel > 2) 46921750Seric { 47058951Seric char *p; 47158951Seric char ebuf[MAXNAME * 2 + 2]; 47255173Seric 47358951Seric p = macvalue('_', e); 47458951Seric if (p == NULL) 47558951Seric { 47658951Seric char *host = RealHostName; 47758951Seric if (host == NULL) 47858951Seric host = MyHostName; 47958951Seric (void) sprintf(ebuf, "%s@%s", realname, host); 48058951Seric p = ebuf; 48158951Seric } 48255173Seric syslog(LOG_NOTICE, 48358951Seric "from=%s unparseable, received from %s", 48458951Seric from, p); 48555173Seric } 48656795Seric # endif /* LOG */ 48757589Seric if (from != NULL) 48857589Seric SuprErrs = TRUE; 48957589Seric if (from == realname || 49058333Seric parseaddr(from = newstr(realname), &e->e_from, 1, ' ', NULL, e) == NULL) 49124944Seric { 49257589Seric SuprErrs = TRUE; 49358333Seric if (parseaddr("postmaster", &e->e_from, 1, ' ', NULL, e) == NULL) 49458151Seric syserr("553 setsender: can't even parse postmaster!"); 49524944Seric } 4969536Seric } 4979536Seric else 4989536Seric FromFlag = TRUE; 49953182Seric e->e_from.q_flags |= QDONTSEND; 50057731Seric if (tTd(45, 5)) 50157731Seric { 50257731Seric printf("setsender: QDONTSEND "); 50357731Seric printaddr(&e->e_from, FALSE); 50457731Seric } 5059536Seric SuprErrs = FALSE; 5069536Seric 50753736Seric pvp = NULL; 50853736Seric if (e->e_from.q_mailer == LocalMailer) 5099536Seric { 51053736Seric # ifdef USERDB 51153736Seric register char *p; 51253736Seric extern char *udbsender(); 51353736Seric # endif 51417472Seric 51558704Seric if (!internal) 51658704Seric { 51758704Seric /* if the user has given fullname already, don't redefine */ 51858704Seric if (FullName == NULL) 51958704Seric FullName = macvalue('x', e); 52058704Seric if (FullName != NULL && FullName[0] == '\0') 52158704Seric FullName = NULL; 5229536Seric 52353736Seric # ifdef USERDB 52458704Seric p = udbsender(from); 52553736Seric 52658704Seric if (p != NULL) 52758704Seric { 52858704Seric /* 52958704Seric ** We have an alternate address for the sender 53058704Seric */ 53153736Seric 53258704Seric pvp = prescan(p, '\0', pvpbuf, NULL); 53358704Seric } 53458704Seric # endif /* USERDB */ 5359536Seric } 53653736Seric 53753736Seric if ((pw = getpwnam(e->e_from.q_user)) != NULL) 53853736Seric { 53953736Seric /* 54053736Seric ** Process passwd file entry. 54153736Seric */ 54253736Seric 54353736Seric 54453736Seric /* extract home directory */ 54553736Seric e->e_from.q_home = newstr(pw->pw_dir); 54653736Seric define('z', e->e_from.q_home, e); 54753736Seric 54853736Seric /* extract user and group id */ 54953736Seric e->e_from.q_uid = pw->pw_uid; 55053736Seric e->e_from.q_gid = pw->pw_gid; 55153736Seric 55253736Seric /* extract full name from passwd file */ 55353736Seric if (FullName == NULL && pw->pw_gecos != NULL && 55458704Seric strcmp(pw->pw_name, e->e_from.q_user) == 0 && 55558704Seric !internal) 55653736Seric { 55753736Seric buildfname(pw->pw_gecos, e->e_from.q_user, buf); 55853736Seric if (buf[0] != '\0') 55953736Seric FullName = newstr(buf); 56053736Seric } 56153736Seric } 56258704Seric if (FullName != NULL && !internal) 56353182Seric define('x', FullName, e); 5649536Seric } 56558704Seric else if (!internal) 56611625Seric { 56753182Seric if (e->e_from.q_home == NULL) 56853182Seric e->e_from.q_home = getenv("HOME"); 56953182Seric e->e_from.q_uid = getuid(); 57053182Seric e->e_from.q_gid = getgid(); 57111625Seric } 57211625Seric 5739536Seric /* 5749536Seric ** Rewrite the from person to dispose of possible implicit 5759536Seric ** links in the net. 5769536Seric */ 5779536Seric 5789536Seric if (pvp == NULL) 57958333Seric pvp = prescan(from, '\0', pvpbuf, NULL); 58053736Seric if (pvp == NULL) 5819536Seric { 58258403Seric /* don't need to give error -- prescan did that already */ 58336233Skarels # ifdef LOG 58458020Seric if (LogLevel > 2) 58536233Skarels syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 58636233Skarels # endif 5879536Seric finis(); 5889536Seric } 589*59084Seric (void) rewrite(pvp, 3, e); 590*59084Seric (void) rewrite(pvp, 1, e); 591*59084Seric (void) rewrite(pvp, 4, e); 59258814Seric cataddr(pvp, NULL, buf, sizeof buf, '\0'); 59358704Seric e->e_sender = newstr(buf); 59458704Seric define('f', e->e_sender, e); 5959536Seric 5969536Seric /* save the domain spec if this mailer wants it */ 59758704Seric if (!internal && e->e_from.q_mailer != NULL && 59853182Seric bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 5999536Seric { 6009536Seric extern char **copyplist(); 6019536Seric 6029536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 6039536Seric pvp++; 6049536Seric if (*pvp != NULL) 60553182Seric e->e_fromdomain = copyplist(pvp, TRUE); 6069536Seric } 6079536Seric } 608