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*59698Seric static char sccsid[] = "@(#)envelope.c 6.34 (Berkeley) 05/03/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 739536Seric dropenvelope(e) 749536Seric register ENVELOPE *e; 759536Seric { 769536Seric bool queueit = FALSE; 779536Seric register ADDRESS *q; 7857943Seric char *id = e->e_id; 799536Seric 809536Seric if (tTd(50, 1)) 819536Seric { 8258680Seric printf("dropenvelope %x: id=", e); 839536Seric xputs(e->e_id); 8458680Seric printf(", flags=%o\n", e->e_flags); 859536Seric } 8657943Seric 8758680Seric /* we must have an id to remove disk files */ 8857943Seric if (id == NULL) 8958680Seric return; 9057943Seric 919536Seric #ifdef LOG 9258020Seric if (LogLevel > 84) 939536Seric syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d", 9457943Seric id, e->e_flags, getpid()); 9556795Seric #endif /* LOG */ 969536Seric 979536Seric /* 989536Seric ** Extract state information from dregs of send list. 999536Seric */ 1009536Seric 1019536Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1029536Seric { 1039536Seric if (bitset(QQUEUEUP, q->q_flags)) 1049536Seric queueit = TRUE; 1059536Seric } 1069536Seric 1079536Seric /* 1089536Seric ** Send back return receipts as requested. 1099536Seric */ 1109536Seric 1119536Seric if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 1129536Seric { 11310844Seric auto ADDRESS *rlist = NULL; 1149536Seric 11558082Seric (void) sendtolist(e->e_receiptto, (ADDRESS *) NULL, &rlist, e); 11655012Seric (void) returntosender("Return receipt", rlist, FALSE, e); 1179536Seric } 1189536Seric 1199536Seric /* 1209536Seric ** Arrange to send error messages if there are fatal errors. 1219536Seric */ 1229536Seric 12358734Seric if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && 12458734Seric e->e_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); 202*59698Seric if (Verbose) 203*59698Seric e->e_sendmode = SM_DELIVER; 20425514Seric bh = BlankEnvelope.e_header; 20525514Seric nhp = &e->e_header; 20625514Seric while (bh != NULL) 20725514Seric { 20825514Seric *nhp = (HDR *) xalloc(sizeof *bh); 20925514Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 21025514Seric bh = bh->h_link; 21125514Seric nhp = &(*nhp)->h_link; 21225514Seric } 2139536Seric } 2149536Seric /* 2159536Seric ** INITSYS -- initialize instantiation of system 2169536Seric ** 2179536Seric ** In Daemon mode, this is done in the child. 2189536Seric ** 2199536Seric ** Parameters: 2209536Seric ** none. 2219536Seric ** 2229536Seric ** Returns: 2239536Seric ** none. 2249536Seric ** 2259536Seric ** Side Effects: 2269536Seric ** Initializes the system macros, some global variables, 2279536Seric ** etc. In particular, the current time in various 2289536Seric ** forms is set. 2299536Seric */ 2309536Seric 23155012Seric initsys(e) 23255012Seric register ENVELOPE *e; 2339536Seric { 2349536Seric static char cbuf[5]; /* holds hop count */ 2359536Seric static char pbuf[10]; /* holds pid */ 23622963Smiriam #ifdef TTYNAME 23759304Seric static char ybuf[60]; /* holds tty id */ 2389536Seric register char *p; 23956795Seric #endif /* TTYNAME */ 2409536Seric extern char *ttyname(); 2419536Seric extern char *macvalue(); 2429536Seric extern char Version[]; 2439536Seric 2449536Seric /* 2459536Seric ** Give this envelope a reality. 2469536Seric ** I.e., an id, a transcript, and a creation time. 2479536Seric */ 2489536Seric 24955012Seric openxscript(e); 25055012Seric e->e_ctime = curtime(); 2519536Seric 2529536Seric /* 2539536Seric ** Set OutChannel to something useful if stdout isn't it. 2549536Seric ** This arranges that any extra stuff the mailer produces 2559536Seric ** gets sent back to the user on error (because it is 2569536Seric ** tucked away in the transcript). 2579536Seric */ 2589536Seric 25958737Seric if (OpMode == MD_DAEMON && !bitset(EF_QUEUERUN, e->e_flags) && 26058737Seric e->e_xfp != NULL) 26155012Seric OutChannel = e->e_xfp; 2629536Seric 2639536Seric /* 2649536Seric ** Set up some basic system macros. 2659536Seric */ 2669536Seric 2679536Seric /* process id */ 2689536Seric (void) sprintf(pbuf, "%d", getpid()); 26955012Seric define('p', pbuf, e); 2709536Seric 2719536Seric /* hop count */ 27255012Seric (void) sprintf(cbuf, "%d", e->e_hopcount); 27355012Seric define('c', cbuf, e); 2749536Seric 2759536Seric /* time as integer, unix time, arpa time */ 27655012Seric settime(e); 2779536Seric 27817472Seric #ifdef TTYNAME 2799536Seric /* tty name */ 28055012Seric if (macvalue('y', e) == NULL) 2819536Seric { 2829536Seric p = ttyname(2); 2839536Seric if (p != NULL) 2849536Seric { 28556795Seric if (strrchr(p, '/') != NULL) 28656795Seric p = strrchr(p, '/') + 1; 2879536Seric (void) strcpy(ybuf, p); 28855012Seric define('y', ybuf, e); 2899536Seric } 2909536Seric } 29156795Seric #endif /* TTYNAME */ 2929536Seric } 2939536Seric /* 29411932Seric ** SETTIME -- set the current time. 29511932Seric ** 29611932Seric ** Parameters: 29711932Seric ** none. 29811932Seric ** 29911932Seric ** Returns: 30011932Seric ** none. 30111932Seric ** 30211932Seric ** Side Effects: 30311932Seric ** Sets the various time macros -- $a, $b, $d, $t. 30411932Seric */ 30511932Seric 30655012Seric settime(e) 30755012Seric register ENVELOPE *e; 30811932Seric { 30911932Seric register char *p; 31011932Seric auto time_t now; 31111932Seric static char tbuf[20]; /* holds "current" time */ 31211932Seric static char dbuf[30]; /* holds ctime(tbuf) */ 31311932Seric register struct tm *tm; 31411932Seric extern char *arpadate(); 31511932Seric extern struct tm *gmtime(); 31611932Seric extern char *macvalue(); 31711932Seric 31811932Seric now = curtime(); 31911932Seric tm = gmtime(&now); 32057014Seric (void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900, 32157014Seric tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min); 32255012Seric define('t', tbuf, e); 32311932Seric (void) strcpy(dbuf, ctime(&now)); 32458131Seric p = strchr(dbuf, '\n'); 32558131Seric if (p != NULL) 32658131Seric *p = '\0'; 32758131Seric define('d', dbuf, e); 32811932Seric p = newstr(arpadate(dbuf)); 32955012Seric if (macvalue('a', e) == NULL) 33055012Seric define('a', p, e); 33155012Seric define('b', p, e); 33211932Seric } 33311932Seric /* 3349536Seric ** OPENXSCRIPT -- Open transcript file 3359536Seric ** 3369536Seric ** Creates a transcript file for possible eventual mailing or 3379536Seric ** sending back. 3389536Seric ** 3399536Seric ** Parameters: 3409536Seric ** e -- the envelope to create the transcript in/for. 3419536Seric ** 3429536Seric ** Returns: 3439536Seric ** none 3449536Seric ** 3459536Seric ** Side Effects: 3469536Seric ** Creates the transcript file. 3479536Seric */ 3489536Seric 34958803Seric #ifndef O_APPEND 35058803Seric #define O_APPEND 0 35158803Seric #endif 35258803Seric 3539536Seric openxscript(e) 3549536Seric register ENVELOPE *e; 3559536Seric { 3569536Seric register char *p; 35740933Srick int fd; 3589536Seric 3599536Seric if (e->e_xfp != NULL) 3609536Seric return; 3619536Seric p = queuename(e, 'x'); 36258803Seric fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644); 36340933Srick if (fd < 0) 3649536Seric syserr("Can't create %s", p); 3659536Seric else 36640933Srick e->e_xfp = fdopen(fd, "w"); 3679536Seric } 3689536Seric /* 36910196Seric ** CLOSEXSCRIPT -- close the transcript file. 37010196Seric ** 37110196Seric ** Parameters: 37210196Seric ** e -- the envelope containing the transcript to close. 37310196Seric ** 37410196Seric ** Returns: 37510196Seric ** none. 37610196Seric ** 37710196Seric ** Side Effects: 37810196Seric ** none. 37910196Seric */ 38010196Seric 38110196Seric closexscript(e) 38210196Seric register ENVELOPE *e; 38310196Seric { 38410196Seric if (e->e_xfp == NULL) 38510196Seric return; 38658680Seric (void) xfclose(e->e_xfp, "closexscript", e->e_id); 38710196Seric e->e_xfp = NULL; 38810196Seric } 38910196Seric /* 3909536Seric ** SETSENDER -- set the person who this message is from 3919536Seric ** 3929536Seric ** Under certain circumstances allow the user to say who 3939536Seric ** s/he is (using -f or -r). These are: 3949536Seric ** 1. The user's uid is zero (root). 3959536Seric ** 2. The user's login name is in an approved list (typically 3969536Seric ** from a network server). 3979536Seric ** 3. The address the user is trying to claim has a 3989536Seric ** "!" character in it (since #2 doesn't do it for 3999536Seric ** us if we are dialing out for UUCP). 4009536Seric ** A better check to replace #3 would be if the 4019536Seric ** effective uid is "UUCP" -- this would require me 4029536Seric ** to rewrite getpwent to "grab" uucp as it went by, 4039536Seric ** make getname more nasty, do another passwd file 4049536Seric ** scan, or compile the UID of "UUCP" into the code, 4059536Seric ** all of which are reprehensible. 4069536Seric ** 4079536Seric ** Assuming all of these fail, we figure out something 4089536Seric ** ourselves. 4099536Seric ** 4109536Seric ** Parameters: 4119536Seric ** from -- the person we would like to believe this message 4129536Seric ** is from, as specified on the command line. 41353182Seric ** e -- the envelope in which we would like the sender set. 41458333Seric ** delimptr -- if non-NULL, set to the location of the 41558333Seric ** trailing delimiter. 41658704Seric ** internal -- set if this address is coming from an internal 41758704Seric ** source such as an owner alias. 4189536Seric ** 4199536Seric ** Returns: 42058704Seric ** none. 4219536Seric ** 4229536Seric ** Side Effects: 4239536Seric ** sets sendmail's notion of who the from person is. 4249536Seric */ 4259536Seric 42658704Seric setsender(from, e, delimptr, internal) 4279536Seric char *from; 42853182Seric register ENVELOPE *e; 42958333Seric char **delimptr; 43058704Seric bool internal; 4319536Seric { 4329536Seric register char **pvp; 4339536Seric char *realname = NULL; 43418665Seric register struct passwd *pw; 43558727Seric char delimchar; 4369536Seric char buf[MAXNAME]; 43716913Seric char pvpbuf[PSBUFSIZE]; 43818665Seric extern struct passwd *getpwnam(); 4399536Seric extern char *macvalue(); 4409536Seric extern char **prescan(); 4419536Seric extern char *FullName; 4429536Seric 4439536Seric if (tTd(45, 1)) 44414786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 4459536Seric 4469536Seric /* 4479536Seric ** Figure out the real user executing us. 4489536Seric ** Username can return errno != 0 on non-errors. 4499536Seric */ 4509536Seric 45158737Seric if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP) 4529536Seric realname = from; 4539536Seric if (realname == NULL || realname[0] == '\0') 4549536Seric { 4559536Seric extern char *username(); 4569536Seric 4579536Seric realname = username(); 4589536Seric } 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