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*58403Seric static char sccsid[] = "@(#)envelope.c 6.19 (Berkeley) 03/03/93"; 1133729Sbostic #endif /* not lint */ 1222704Sdist 1358332Seric #include "sendmail.h" 1436928Sbostic #include <sys/time.h> 1536928Sbostic #include <sys/stat.h> 169536Seric #include <pwd.h> 1757737Seric #include <fcntl.h> 189536Seric 199536Seric /* 209536Seric ** NEWENVELOPE -- allocate a new envelope 219536Seric ** 229536Seric ** Supports inheritance. 239536Seric ** 249536Seric ** Parameters: 259536Seric ** e -- the new envelope to fill in. 2658179Seric ** parent -- the envelope to be the parent of e. 279536Seric ** 289536Seric ** Returns: 299536Seric ** e. 309536Seric ** 319536Seric ** Side Effects: 329536Seric ** none. 339536Seric */ 349536Seric 359536Seric ENVELOPE * 3658179Seric newenvelope(e, parent) 379536Seric register ENVELOPE *e; 3858179Seric register ENVELOPE *parent; 399536Seric { 409536Seric extern putheader(), putbody(); 4125611Seric extern ENVELOPE BlankEnvelope; 429536Seric 4358179Seric if (e == parent && e->e_parent != NULL) 449536Seric parent = e->e_parent; 4525611Seric clearenvelope(e, TRUE); 4624944Seric if (e == CurEnv) 4724944Seric bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from); 4824944Seric else 4924944Seric bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from); 509536Seric e->e_parent = parent; 519536Seric e->e_ctime = curtime(); 5256215Seric if (parent != NULL) 5356215Seric e->e_msgpriority = parent->e_msgsize; 549536Seric e->e_puthdr = putheader; 559536Seric e->e_putbody = putbody; 569536Seric if (CurEnv->e_xfp != NULL) 579536Seric (void) fflush(CurEnv->e_xfp); 589536Seric 599536Seric return (e); 609536Seric } 619536Seric /* 629536Seric ** DROPENVELOPE -- deallocate an envelope. 639536Seric ** 649536Seric ** Parameters: 659536Seric ** e -- the envelope to deallocate. 669536Seric ** 679536Seric ** Returns: 689536Seric ** none. 699536Seric ** 709536Seric ** Side Effects: 719536Seric ** housekeeping necessary to dispose of an envelope. 729536Seric ** Unlocks this queue file. 739536Seric */ 749536Seric 759536Seric dropenvelope(e) 769536Seric register ENVELOPE *e; 779536Seric { 789536Seric bool queueit = FALSE; 799536Seric register ADDRESS *q; 8057943Seric char *id = e->e_id; 819536Seric 829536Seric if (tTd(50, 1)) 839536Seric { 849536Seric printf("dropenvelope %x id=", e); 859536Seric xputs(e->e_id); 869536Seric printf(" flags=%o\n", e->e_flags); 879536Seric } 8857943Seric 8957943Seric if (id == NULL) 9057943Seric id = "(none)"; 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 /* we must have an id to remove disk files */ 999536Seric if (e->e_id == NULL) 1009536Seric return; 1019536Seric 1029536Seric /* 1039536Seric ** Extract state information from dregs of send list. 1049536Seric */ 1059536Seric 1069536Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1079536Seric { 1089536Seric if (bitset(QQUEUEUP, q->q_flags)) 1099536Seric queueit = TRUE; 1109536Seric } 1119536Seric 1129536Seric /* 1139536Seric ** Send back return receipts as requested. 1149536Seric */ 1159536Seric 1169536Seric if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 1179536Seric { 11810844Seric auto ADDRESS *rlist = NULL; 1199536Seric 12058082Seric (void) sendtolist(e->e_receiptto, (ADDRESS *) NULL, &rlist, e); 12155012Seric (void) returntosender("Return receipt", rlist, FALSE, e); 1229536Seric } 1239536Seric 1249536Seric /* 1259536Seric ** Arrange to send error messages if there are fatal errors. 1269536Seric */ 1279536Seric 12810754Seric if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && ErrorMode != EM_QUIET) 1299536Seric savemail(e); 1309536Seric 1319536Seric /* 1329536Seric ** Instantiate or deinstantiate the queue. 1339536Seric */ 1349536Seric 1359536Seric if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 1369536Seric bitset(EF_CLRQUEUE, e->e_flags)) 1379536Seric { 13823497Seric if (e->e_df != NULL) 13923497Seric xunlink(e->e_df); 1409536Seric xunlink(queuename(e, 'q')); 1419536Seric } 1429536Seric else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 14310754Seric { 14410754Seric #ifdef QUEUE 14551920Seric queueup(e, FALSE, FALSE); 14656795Seric #else /* QUEUE */ 14758151Seric syserr("554 dropenvelope: queueup"); 14856795Seric #endif /* QUEUE */ 14910754Seric } 1509536Seric 1519536Seric /* now unlock the job */ 15210196Seric closexscript(e); 1539536Seric unlockqueue(e); 1549536Seric 1559536Seric /* make sure that this envelope is marked unused */ 1569536Seric e->e_id = e->e_df = NULL; 15724944Seric if (e->e_dfp != NULL) 15824944Seric (void) fclose(e->e_dfp); 15910196Seric e->e_dfp = NULL; 16057589Seric 16157589Seric #ifdef LOG 16258020Seric if (LogLevel > 74) 16357943Seric syslog(LOG_INFO, "%s: done", id); 16457589Seric #endif /* LOG */ 1659536Seric } 1669536Seric /* 1679536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 1689536Seric ** 1699536Seric ** This is normally used by a child process to get a clean 1709536Seric ** envelope without disturbing the parent. 1719536Seric ** 1729536Seric ** Parameters: 1739536Seric ** e -- the envelope to clear. 17425611Seric ** fullclear - if set, the current envelope is total 17525611Seric ** garbage and should be ignored; otherwise, 17625611Seric ** release any resources it may indicate. 1779536Seric ** 1789536Seric ** Returns: 1799536Seric ** none. 1809536Seric ** 1819536Seric ** Side Effects: 1829536Seric ** Closes files associated with the envelope. 1839536Seric ** Marks the envelope as unallocated. 1849536Seric */ 1859536Seric 18625611Seric clearenvelope(e, fullclear) 1879536Seric register ENVELOPE *e; 18825611Seric bool fullclear; 1899536Seric { 19025514Seric register HDR *bh; 19125514Seric register HDR **nhp; 19225514Seric extern ENVELOPE BlankEnvelope; 19325514Seric 19425611Seric if (!fullclear) 19525611Seric { 19625611Seric /* clear out any file information */ 19725611Seric if (e->e_xfp != NULL) 19825611Seric (void) fclose(e->e_xfp); 19925611Seric if (e->e_dfp != NULL) 20025611Seric (void) fclose(e->e_dfp); 20125611Seric } 2029536Seric 20324961Seric /* now clear out the data */ 20424965Seric STRUCTCOPY(BlankEnvelope, *e); 20525514Seric bh = BlankEnvelope.e_header; 20625514Seric nhp = &e->e_header; 20725514Seric while (bh != NULL) 20825514Seric { 20925514Seric *nhp = (HDR *) xalloc(sizeof *bh); 21025514Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 21125514Seric bh = bh->h_link; 21225514Seric nhp = &(*nhp)->h_link; 21325514Seric } 2149536Seric } 2159536Seric /* 2169536Seric ** INITSYS -- initialize instantiation of system 2179536Seric ** 2189536Seric ** In Daemon mode, this is done in the child. 2199536Seric ** 2209536Seric ** Parameters: 2219536Seric ** none. 2229536Seric ** 2239536Seric ** Returns: 2249536Seric ** none. 2259536Seric ** 2269536Seric ** Side Effects: 2279536Seric ** Initializes the system macros, some global variables, 2289536Seric ** etc. In particular, the current time in various 2299536Seric ** forms is set. 2309536Seric */ 2319536Seric 23255012Seric initsys(e) 23355012Seric register ENVELOPE *e; 2349536Seric { 2359536Seric static char cbuf[5]; /* holds hop count */ 2369536Seric static char pbuf[10]; /* holds pid */ 23722963Smiriam #ifdef TTYNAME 2389536Seric static char ybuf[10]; /* holds tty id */ 2399536Seric register char *p; 24056795Seric #endif /* TTYNAME */ 2419536Seric extern char *ttyname(); 2429536Seric extern char *macvalue(); 2439536Seric extern char Version[]; 2449536Seric 2459536Seric /* 2469536Seric ** Give this envelope a reality. 2479536Seric ** I.e., an id, a transcript, and a creation time. 2489536Seric */ 2499536Seric 25055012Seric openxscript(e); 25155012Seric e->e_ctime = curtime(); 2529536Seric 2539536Seric /* 2549536Seric ** Set OutChannel to something useful if stdout isn't it. 2559536Seric ** This arranges that any extra stuff the mailer produces 2569536Seric ** gets sent back to the user on error (because it is 2579536Seric ** tucked away in the transcript). 2589536Seric */ 2599536Seric 26058069Seric if (OpMode == MD_DAEMON && QueueRun && 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 3499536Seric openxscript(e) 3509536Seric register ENVELOPE *e; 3519536Seric { 3529536Seric register char *p; 35340933Srick int fd; 3549536Seric 3559536Seric if (e->e_xfp != NULL) 3569536Seric return; 3579536Seric p = queuename(e, 'x'); 35840933Srick fd = open(p, O_WRONLY|O_CREAT, 0644); 35940933Srick if (fd < 0) 3609536Seric syserr("Can't create %s", p); 3619536Seric else 36240933Srick e->e_xfp = fdopen(fd, "w"); 3639536Seric } 3649536Seric /* 36510196Seric ** CLOSEXSCRIPT -- close the transcript file. 36610196Seric ** 36710196Seric ** Parameters: 36810196Seric ** e -- the envelope containing the transcript to close. 36910196Seric ** 37010196Seric ** Returns: 37110196Seric ** none. 37210196Seric ** 37310196Seric ** Side Effects: 37410196Seric ** none. 37510196Seric */ 37610196Seric 37710196Seric closexscript(e) 37810196Seric register ENVELOPE *e; 37910196Seric { 38010196Seric if (e->e_xfp == NULL) 38110196Seric return; 38210196Seric (void) fclose(e->e_xfp); 38310196Seric e->e_xfp = NULL; 38410196Seric } 38510196Seric /* 3869536Seric ** SETSENDER -- set the person who this message is from 3879536Seric ** 3889536Seric ** Under certain circumstances allow the user to say who 3899536Seric ** s/he is (using -f or -r). These are: 3909536Seric ** 1. The user's uid is zero (root). 3919536Seric ** 2. The user's login name is in an approved list (typically 3929536Seric ** from a network server). 3939536Seric ** 3. The address the user is trying to claim has a 3949536Seric ** "!" character in it (since #2 doesn't do it for 3959536Seric ** us if we are dialing out for UUCP). 3969536Seric ** A better check to replace #3 would be if the 3979536Seric ** effective uid is "UUCP" -- this would require me 3989536Seric ** to rewrite getpwent to "grab" uucp as it went by, 3999536Seric ** make getname more nasty, do another passwd file 4009536Seric ** scan, or compile the UID of "UUCP" into the code, 4019536Seric ** all of which are reprehensible. 4029536Seric ** 4039536Seric ** Assuming all of these fail, we figure out something 4049536Seric ** ourselves. 4059536Seric ** 4069536Seric ** Parameters: 4079536Seric ** from -- the person we would like to believe this message 4089536Seric ** is from, as specified on the command line. 40953182Seric ** e -- the envelope in which we would like the sender set. 41058333Seric ** delimptr -- if non-NULL, set to the location of the 41158333Seric ** trailing delimiter. 4129536Seric ** 4139536Seric ** Returns: 41458333Seric ** pointer to the delimiter terminating the from address. 4159536Seric ** 4169536Seric ** Side Effects: 4179536Seric ** sets sendmail's notion of who the from person is. 4189536Seric */ 4199536Seric 42058333Seric char * 42158333Seric setsender(from, e, delimptr) 4229536Seric char *from; 42353182Seric register ENVELOPE *e; 42458333Seric char **delimptr; 4259536Seric { 4269536Seric register char **pvp; 4279536Seric char *realname = NULL; 42818665Seric register struct passwd *pw; 42958333Seric char *delimchar = NULL; 4309536Seric char buf[MAXNAME]; 43116913Seric char pvpbuf[PSBUFSIZE]; 43218665Seric extern struct passwd *getpwnam(); 4339536Seric extern char *macvalue(); 4349536Seric extern char **prescan(); 4359536Seric extern char *FullName; 4369536Seric 4379536Seric if (tTd(45, 1)) 43814786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 4399536Seric 4409536Seric /* 4419536Seric ** Figure out the real user executing us. 4429536Seric ** Username can return errno != 0 on non-errors. 4439536Seric */ 4449536Seric 44552220Seric if (QueueRun || OpMode == MD_SMTP) 4469536Seric realname = from; 4479536Seric if (realname == NULL || realname[0] == '\0') 4489536Seric { 4499536Seric extern char *username(); 4509536Seric 4519536Seric realname = username(); 4529536Seric } 4539536Seric 45457589Seric /* 4559536Seric SuprErrs = TRUE; 45657589Seric */ 45758333Seric if (from == NULL || 45858333Seric parseaddr(from, &e->e_from, 1, ' ', 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 } 49153182Seric loweraddr(&e->e_from); 4929536Seric SuprErrs = FALSE; 4939536Seric 49453736Seric pvp = NULL; 49553736Seric if (e->e_from.q_mailer == LocalMailer) 4969536Seric { 49753736Seric # ifdef USERDB 49853736Seric register char *p; 49953736Seric extern char *udbsender(); 50053736Seric # endif 50117472Seric 5029536Seric /* if the user has given fullname already, don't redefine */ 5039536Seric if (FullName == NULL) 50453182Seric FullName = macvalue('x', e); 50511932Seric if (FullName != NULL && FullName[0] == '\0') 5069536Seric FullName = NULL; 5079536Seric 50853736Seric # ifdef USERDB 50953736Seric p = udbsender(from); 51053736Seric 51153736Seric if (p != NULL) 5129536Seric { 51353736Seric /* 51453736Seric ** We have an alternate address for the sender 51553736Seric */ 51653736Seric 51758333Seric pvp = prescan(p, '\0', pvpbuf, NULL); 5189536Seric } 51953736Seric # endif /* USERDB */ 52053736Seric 52153736Seric if ((pw = getpwnam(e->e_from.q_user)) != NULL) 52253736Seric { 52353736Seric /* 52453736Seric ** Process passwd file entry. 52553736Seric */ 52653736Seric 52753736Seric 52853736Seric /* extract home directory */ 52953736Seric e->e_from.q_home = newstr(pw->pw_dir); 53053736Seric define('z', e->e_from.q_home, e); 53153736Seric 53253736Seric /* extract user and group id */ 53353736Seric e->e_from.q_uid = pw->pw_uid; 53453736Seric e->e_from.q_gid = pw->pw_gid; 53553736Seric 53653736Seric /* extract full name from passwd file */ 53753736Seric if (FullName == NULL && pw->pw_gecos != NULL && 53853736Seric strcmp(pw->pw_name, e->e_from.q_user) == 0) 53953736Seric { 54053736Seric buildfname(pw->pw_gecos, e->e_from.q_user, buf); 54153736Seric if (buf[0] != '\0') 54253736Seric FullName = newstr(buf); 54353736Seric } 54453736Seric } 5459536Seric if (FullName != NULL) 54653182Seric define('x', FullName, e); 5479536Seric } 54811625Seric else 54911625Seric { 55053182Seric if (e->e_from.q_home == NULL) 55153182Seric e->e_from.q_home = getenv("HOME"); 55253182Seric e->e_from.q_uid = getuid(); 55353182Seric e->e_from.q_gid = getgid(); 55411625Seric } 55511625Seric 5569536Seric /* 5579536Seric ** Rewrite the from person to dispose of possible implicit 5589536Seric ** links in the net. 5599536Seric */ 5609536Seric 5619536Seric if (pvp == NULL) 56258333Seric pvp = prescan(from, '\0', pvpbuf, NULL); 56353736Seric if (pvp == NULL) 5649536Seric { 565*58403Seric /* don't need to give error -- prescan did that already */ 56636233Skarels # ifdef LOG 56758020Seric if (LogLevel > 2) 56836233Skarels syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 56936233Skarels # endif 5709536Seric finis(); 5719536Seric } 5729536Seric rewrite(pvp, 3); 5739536Seric rewrite(pvp, 1); 57425032Seric rewrite(pvp, 4); 57558082Seric cataddr(pvp, buf, sizeof buf, '\0'); 57653182Seric e->e_sender = e->e_returnpath = newstr(buf); 5779536Seric 57853182Seric define('f', e->e_sender, e); 57951951Seric 5809536Seric /* save the domain spec if this mailer wants it */ 58153182Seric if (e->e_from.q_mailer != NULL && 58253182Seric bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 5839536Seric { 5849536Seric extern char **copyplist(); 5859536Seric 5869536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 5879536Seric pvp++; 5889536Seric if (*pvp != NULL) 58953182Seric e->e_fromdomain = copyplist(pvp, TRUE); 5909536Seric } 5919536Seric } 592