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*58323Seric static char sccsid[] = "@(#)envelope.c 6.16 (Berkeley) 02/28/93"; 1133729Sbostic #endif /* not lint */ 1222704Sdist 1336928Sbostic #include <sys/types.h> 1436928Sbostic #include <sys/time.h> 1536928Sbostic #include <sys/stat.h> 169536Seric #include <pwd.h> 1757737Seric #include <fcntl.h> 189536Seric #include "sendmail.h" 199536Seric 209536Seric /* 219536Seric ** NEWENVELOPE -- allocate a new envelope 229536Seric ** 239536Seric ** Supports inheritance. 249536Seric ** 259536Seric ** Parameters: 269536Seric ** e -- the new envelope to fill in. 2758179Seric ** parent -- the envelope to be the parent of e. 289536Seric ** 299536Seric ** Returns: 309536Seric ** e. 319536Seric ** 329536Seric ** Side Effects: 339536Seric ** none. 349536Seric */ 359536Seric 369536Seric ENVELOPE * 3758179Seric newenvelope(e, parent) 389536Seric register ENVELOPE *e; 3958179Seric register ENVELOPE *parent; 409536Seric { 419536Seric extern putheader(), putbody(); 4225611Seric extern ENVELOPE BlankEnvelope; 439536Seric 4458179Seric if (e == parent && e->e_parent != NULL) 459536Seric parent = e->e_parent; 4625611Seric clearenvelope(e, TRUE); 4724944Seric if (e == CurEnv) 4824944Seric bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from); 4924944Seric else 5024944Seric bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from); 519536Seric e->e_parent = parent; 529536Seric e->e_ctime = curtime(); 5356215Seric if (parent != NULL) 5456215Seric e->e_msgpriority = parent->e_msgsize; 559536Seric e->e_puthdr = putheader; 569536Seric e->e_putbody = putbody; 579536Seric if (CurEnv->e_xfp != NULL) 589536Seric (void) fflush(CurEnv->e_xfp); 599536Seric 609536Seric return (e); 619536Seric } 629536Seric /* 639536Seric ** DROPENVELOPE -- deallocate an envelope. 649536Seric ** 659536Seric ** Parameters: 669536Seric ** e -- the envelope to deallocate. 679536Seric ** 689536Seric ** Returns: 699536Seric ** none. 709536Seric ** 719536Seric ** Side Effects: 729536Seric ** housekeeping necessary to dispose of an envelope. 739536Seric ** Unlocks this queue file. 749536Seric */ 759536Seric 769536Seric dropenvelope(e) 779536Seric register ENVELOPE *e; 789536Seric { 799536Seric bool queueit = FALSE; 809536Seric register ADDRESS *q; 8157943Seric char *id = e->e_id; 829536Seric 839536Seric if (tTd(50, 1)) 849536Seric { 859536Seric printf("dropenvelope %x id=", e); 869536Seric xputs(e->e_id); 879536Seric printf(" flags=%o\n", e->e_flags); 889536Seric } 8957943Seric 9057943Seric if (id == NULL) 9157943Seric id = "(none)"; 9257943Seric 939536Seric #ifdef LOG 9458020Seric if (LogLevel > 84) 959536Seric syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d", 9657943Seric id, e->e_flags, getpid()); 9756795Seric #endif /* LOG */ 989536Seric 999536Seric /* we must have an id to remove disk files */ 1009536Seric if (e->e_id == NULL) 1019536Seric return; 1029536Seric 1039536Seric /* 1049536Seric ** Extract state information from dregs of send list. 1059536Seric */ 1069536Seric 1079536Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1089536Seric { 1099536Seric if (bitset(QQUEUEUP, q->q_flags)) 1109536Seric queueit = TRUE; 1119536Seric } 1129536Seric 1139536Seric /* 1149536Seric ** Send back return receipts as requested. 1159536Seric */ 1169536Seric 1179536Seric if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 1189536Seric { 11910844Seric auto ADDRESS *rlist = NULL; 1209536Seric 12158082Seric (void) sendtolist(e->e_receiptto, (ADDRESS *) NULL, &rlist, e); 12255012Seric (void) returntosender("Return receipt", rlist, FALSE, e); 1239536Seric } 1249536Seric 1259536Seric /* 1269536Seric ** Arrange to send error messages if there are fatal errors. 1279536Seric */ 1289536Seric 12910754Seric if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && ErrorMode != EM_QUIET) 1309536Seric savemail(e); 1319536Seric 1329536Seric /* 1339536Seric ** Instantiate or deinstantiate the queue. 1349536Seric */ 1359536Seric 1369536Seric if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 1379536Seric bitset(EF_CLRQUEUE, e->e_flags)) 1389536Seric { 13923497Seric if (e->e_df != NULL) 14023497Seric xunlink(e->e_df); 1419536Seric xunlink(queuename(e, 'q')); 1429536Seric } 1439536Seric else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 14410754Seric { 14510754Seric #ifdef QUEUE 14651920Seric queueup(e, FALSE, FALSE); 14756795Seric #else /* QUEUE */ 14858151Seric syserr("554 dropenvelope: queueup"); 14956795Seric #endif /* QUEUE */ 15010754Seric } 1519536Seric 1529536Seric /* now unlock the job */ 15310196Seric closexscript(e); 1549536Seric unlockqueue(e); 1559536Seric 1569536Seric /* make sure that this envelope is marked unused */ 1579536Seric e->e_id = e->e_df = NULL; 15824944Seric if (e->e_dfp != NULL) 15924944Seric (void) fclose(e->e_dfp); 16010196Seric e->e_dfp = NULL; 16157589Seric 16257589Seric #ifdef LOG 16358020Seric if (LogLevel > 74) 16457943Seric syslog(LOG_INFO, "%s: done", id); 16557589Seric #endif /* LOG */ 1669536Seric } 1679536Seric /* 1689536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 1699536Seric ** 1709536Seric ** This is normally used by a child process to get a clean 1719536Seric ** envelope without disturbing the parent. 1729536Seric ** 1739536Seric ** Parameters: 1749536Seric ** e -- the envelope to clear. 17525611Seric ** fullclear - if set, the current envelope is total 17625611Seric ** garbage and should be ignored; otherwise, 17725611Seric ** release any resources it may indicate. 1789536Seric ** 1799536Seric ** Returns: 1809536Seric ** none. 1819536Seric ** 1829536Seric ** Side Effects: 1839536Seric ** Closes files associated with the envelope. 1849536Seric ** Marks the envelope as unallocated. 1859536Seric */ 1869536Seric 18725611Seric clearenvelope(e, fullclear) 1889536Seric register ENVELOPE *e; 18925611Seric bool fullclear; 1909536Seric { 19125514Seric register HDR *bh; 19225514Seric register HDR **nhp; 19325514Seric extern ENVELOPE BlankEnvelope; 19425514Seric 19525611Seric if (!fullclear) 19625611Seric { 19725611Seric /* clear out any file information */ 19825611Seric if (e->e_xfp != NULL) 19925611Seric (void) fclose(e->e_xfp); 20025611Seric if (e->e_dfp != NULL) 20125611Seric (void) fclose(e->e_dfp); 20225611Seric } 2039536Seric 20424961Seric /* now clear out the data */ 20524965Seric STRUCTCOPY(BlankEnvelope, *e); 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 23355012Seric initsys(e) 23455012Seric register ENVELOPE *e; 2359536Seric { 2369536Seric static char cbuf[5]; /* holds hop count */ 2379536Seric static char pbuf[10]; /* holds pid */ 23822963Smiriam #ifdef TTYNAME 2399536Seric static char ybuf[10]; /* holds tty id */ 2409536Seric register char *p; 24156795Seric #endif /* TTYNAME */ 2429536Seric extern char *ttyname(); 2439536Seric extern char *macvalue(); 2449536Seric extern char Version[]; 2459536Seric 2469536Seric /* 2479536Seric ** Give this envelope a reality. 2489536Seric ** I.e., an id, a transcript, and a creation time. 2499536Seric */ 2509536Seric 25155012Seric openxscript(e); 25255012Seric e->e_ctime = curtime(); 2539536Seric 2549536Seric /* 2559536Seric ** Set OutChannel to something useful if stdout isn't it. 2569536Seric ** This arranges that any extra stuff the mailer produces 2579536Seric ** gets sent back to the user on error (because it is 2589536Seric ** tucked away in the transcript). 2599536Seric */ 2609536Seric 26158069Seric if (OpMode == MD_DAEMON && QueueRun && e->e_xfp != NULL) 26255012Seric OutChannel = e->e_xfp; 2639536Seric 2649536Seric /* 2659536Seric ** Set up some basic system macros. 2669536Seric */ 2679536Seric 2689536Seric /* process id */ 2699536Seric (void) sprintf(pbuf, "%d", getpid()); 27055012Seric define('p', pbuf, e); 2719536Seric 2729536Seric /* hop count */ 27355012Seric (void) sprintf(cbuf, "%d", e->e_hopcount); 27455012Seric define('c', cbuf, e); 2759536Seric 2769536Seric /* time as integer, unix time, arpa time */ 27755012Seric settime(e); 2789536Seric 27917472Seric #ifdef TTYNAME 2809536Seric /* tty name */ 28155012Seric if (macvalue('y', e) == NULL) 2829536Seric { 2839536Seric p = ttyname(2); 2849536Seric if (p != NULL) 2859536Seric { 28656795Seric if (strrchr(p, '/') != NULL) 28756795Seric p = strrchr(p, '/') + 1; 2889536Seric (void) strcpy(ybuf, p); 28955012Seric define('y', ybuf, e); 2909536Seric } 2919536Seric } 29256795Seric #endif /* TTYNAME */ 2939536Seric } 2949536Seric /* 29511932Seric ** SETTIME -- set the current time. 29611932Seric ** 29711932Seric ** Parameters: 29811932Seric ** none. 29911932Seric ** 30011932Seric ** Returns: 30111932Seric ** none. 30211932Seric ** 30311932Seric ** Side Effects: 30411932Seric ** Sets the various time macros -- $a, $b, $d, $t. 30511932Seric */ 30611932Seric 30755012Seric settime(e) 30855012Seric register ENVELOPE *e; 30911932Seric { 31011932Seric register char *p; 31111932Seric auto time_t now; 31211932Seric static char tbuf[20]; /* holds "current" time */ 31311932Seric static char dbuf[30]; /* holds ctime(tbuf) */ 31411932Seric register struct tm *tm; 31511932Seric extern char *arpadate(); 31611932Seric extern struct tm *gmtime(); 31711932Seric extern char *macvalue(); 31811932Seric 31911932Seric now = curtime(); 32011932Seric tm = gmtime(&now); 32157014Seric (void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900, 32257014Seric tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min); 32355012Seric define('t', tbuf, e); 32411932Seric (void) strcpy(dbuf, ctime(&now)); 32558131Seric p = strchr(dbuf, '\n'); 32658131Seric if (p != NULL) 32758131Seric *p = '\0'; 32858131Seric define('d', dbuf, e); 32911932Seric p = newstr(arpadate(dbuf)); 33055012Seric if (macvalue('a', e) == NULL) 33155012Seric define('a', p, e); 33255012Seric define('b', p, e); 33311932Seric } 33411932Seric /* 3359536Seric ** OPENXSCRIPT -- Open transcript file 3369536Seric ** 3379536Seric ** Creates a transcript file for possible eventual mailing or 3389536Seric ** sending back. 3399536Seric ** 3409536Seric ** Parameters: 3419536Seric ** e -- the envelope to create the transcript in/for. 3429536Seric ** 3439536Seric ** Returns: 3449536Seric ** none 3459536Seric ** 3469536Seric ** Side Effects: 3479536Seric ** Creates the transcript file. 3489536Seric */ 3499536Seric 3509536Seric openxscript(e) 3519536Seric register ENVELOPE *e; 3529536Seric { 3539536Seric register char *p; 35440933Srick int fd; 3559536Seric 3569536Seric if (e->e_xfp != NULL) 3579536Seric return; 3589536Seric p = queuename(e, 'x'); 35940933Srick fd = open(p, O_WRONLY|O_CREAT, 0644); 36040933Srick if (fd < 0) 3619536Seric syserr("Can't create %s", p); 3629536Seric else 36340933Srick e->e_xfp = fdopen(fd, "w"); 3649536Seric } 3659536Seric /* 36610196Seric ** CLOSEXSCRIPT -- close the transcript file. 36710196Seric ** 36810196Seric ** Parameters: 36910196Seric ** e -- the envelope containing the transcript to close. 37010196Seric ** 37110196Seric ** Returns: 37210196Seric ** none. 37310196Seric ** 37410196Seric ** Side Effects: 37510196Seric ** none. 37610196Seric */ 37710196Seric 37810196Seric closexscript(e) 37910196Seric register ENVELOPE *e; 38010196Seric { 38110196Seric if (e->e_xfp == NULL) 38210196Seric return; 38310196Seric (void) fclose(e->e_xfp); 38410196Seric e->e_xfp = NULL; 38510196Seric } 38610196Seric /* 3879536Seric ** SETSENDER -- set the person who this message is from 3889536Seric ** 3899536Seric ** Under certain circumstances allow the user to say who 3909536Seric ** s/he is (using -f or -r). These are: 3919536Seric ** 1. The user's uid is zero (root). 3929536Seric ** 2. The user's login name is in an approved list (typically 3939536Seric ** from a network server). 3949536Seric ** 3. The address the user is trying to claim has a 3959536Seric ** "!" character in it (since #2 doesn't do it for 3969536Seric ** us if we are dialing out for UUCP). 3979536Seric ** A better check to replace #3 would be if the 3989536Seric ** effective uid is "UUCP" -- this would require me 3999536Seric ** to rewrite getpwent to "grab" uucp as it went by, 4009536Seric ** make getname more nasty, do another passwd file 4019536Seric ** scan, or compile the UID of "UUCP" into the code, 4029536Seric ** all of which are reprehensible. 4039536Seric ** 4049536Seric ** Assuming all of these fail, we figure out something 4059536Seric ** ourselves. 4069536Seric ** 4079536Seric ** Parameters: 4089536Seric ** from -- the person we would like to believe this message 4099536Seric ** is from, as specified on the command line. 41053182Seric ** e -- the envelope in which we would like the sender set. 4119536Seric ** 4129536Seric ** Returns: 4139536Seric ** none. 4149536Seric ** 4159536Seric ** Side Effects: 4169536Seric ** sets sendmail's notion of who the from person is. 4179536Seric */ 4189536Seric 41953182Seric setsender(from, e) 4209536Seric char *from; 42153182Seric register ENVELOPE *e; 4229536Seric { 4239536Seric register char **pvp; 4249536Seric char *realname = NULL; 42518665Seric register struct passwd *pw; 4269536Seric char buf[MAXNAME]; 42716913Seric char pvpbuf[PSBUFSIZE]; 42818665Seric extern struct passwd *getpwnam(); 4299536Seric extern char *macvalue(); 4309536Seric extern char **prescan(); 4319536Seric extern char *FullName; 4329536Seric 4339536Seric if (tTd(45, 1)) 43414786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 4359536Seric 4369536Seric /* 4379536Seric ** Figure out the real user executing us. 4389536Seric ** Username can return errno != 0 on non-errors. 4399536Seric */ 4409536Seric 44152220Seric if (QueueRun || OpMode == MD_SMTP) 4429536Seric realname = from; 4439536Seric if (realname == NULL || realname[0] == '\0') 4449536Seric { 4459536Seric extern char *username(); 4469536Seric 4479536Seric realname = username(); 4489536Seric } 4499536Seric 45057589Seric /* 4519536Seric SuprErrs = TRUE; 45257589Seric */ 453*58323Seric if (from == NULL || parseaddr(from, &e->e_from, 1, ' ', e) == NULL) 4549536Seric { 45521750Seric /* log garbage addresses for traceback */ 45655173Seric # ifdef LOG 45758020Seric if (from != NULL && LogLevel > 2) 45821750Seric { 45955173Seric char *host = RealHostName; 46055173Seric 46155173Seric if (host == NULL) 46255173Seric host = MyHostName; 46355173Seric syslog(LOG_NOTICE, 46455173Seric "from=%s unparseable, received from %s@%s", 46555173Seric from, realname, host); 46655173Seric } 46756795Seric # endif /* LOG */ 46857589Seric if (from != NULL) 46957589Seric SuprErrs = TRUE; 47057589Seric if (from == realname || 471*58323Seric parseaddr(from = newstr(realname), &e->e_from, 1, ' ', e) == NULL) 47224944Seric { 47357589Seric SuprErrs = TRUE; 474*58323Seric if (parseaddr("postmaster", &e->e_from, 1, ' ', e) == NULL) 47558151Seric syserr("553 setsender: can't even parse postmaster!"); 47624944Seric } 4779536Seric } 4789536Seric else 4799536Seric FromFlag = TRUE; 48053182Seric e->e_from.q_flags |= QDONTSEND; 48157731Seric if (tTd(45, 5)) 48257731Seric { 48357731Seric printf("setsender: QDONTSEND "); 48457731Seric printaddr(&e->e_from, FALSE); 48557731Seric } 48653182Seric loweraddr(&e->e_from); 4879536Seric SuprErrs = FALSE; 4889536Seric 48953736Seric pvp = NULL; 49053736Seric if (e->e_from.q_mailer == LocalMailer) 4919536Seric { 49253736Seric # ifdef USERDB 49353736Seric register char *p; 49453736Seric extern char *udbsender(); 49553736Seric # endif 49617472Seric 4979536Seric /* if the user has given fullname already, don't redefine */ 4989536Seric if (FullName == NULL) 49953182Seric FullName = macvalue('x', e); 50011932Seric if (FullName != NULL && FullName[0] == '\0') 5019536Seric FullName = NULL; 5029536Seric 50353736Seric # ifdef USERDB 50453736Seric p = udbsender(from); 50553736Seric 50653736Seric if (p != NULL) 5079536Seric { 50853736Seric /* 50953736Seric ** We have an alternate address for the sender 51053736Seric */ 51153736Seric 51253736Seric pvp = prescan(p, '\0', pvpbuf); 5139536Seric } 51453736Seric # endif /* USERDB */ 51553736Seric 51653736Seric if ((pw = getpwnam(e->e_from.q_user)) != NULL) 51753736Seric { 51853736Seric /* 51953736Seric ** Process passwd file entry. 52053736Seric */ 52153736Seric 52253736Seric 52353736Seric /* extract home directory */ 52453736Seric e->e_from.q_home = newstr(pw->pw_dir); 52553736Seric define('z', e->e_from.q_home, e); 52653736Seric 52753736Seric /* extract user and group id */ 52853736Seric e->e_from.q_uid = pw->pw_uid; 52953736Seric e->e_from.q_gid = pw->pw_gid; 53053736Seric 53153736Seric /* extract full name from passwd file */ 53253736Seric if (FullName == NULL && pw->pw_gecos != NULL && 53353736Seric strcmp(pw->pw_name, e->e_from.q_user) == 0) 53453736Seric { 53553736Seric buildfname(pw->pw_gecos, e->e_from.q_user, buf); 53653736Seric if (buf[0] != '\0') 53753736Seric FullName = newstr(buf); 53853736Seric } 53953736Seric } 5409536Seric if (FullName != NULL) 54153182Seric define('x', FullName, e); 5429536Seric } 54311625Seric else 54411625Seric { 54553182Seric if (e->e_from.q_home == NULL) 54653182Seric e->e_from.q_home = getenv("HOME"); 54753182Seric e->e_from.q_uid = getuid(); 54853182Seric e->e_from.q_gid = getgid(); 54911625Seric } 55011625Seric 5519536Seric /* 5529536Seric ** Rewrite the from person to dispose of possible implicit 5539536Seric ** links in the net. 5549536Seric */ 5559536Seric 5569536Seric if (pvp == NULL) 55753736Seric pvp = prescan(from, '\0', pvpbuf); 55853736Seric if (pvp == NULL) 5599536Seric { 56036233Skarels # ifdef LOG 56158020Seric if (LogLevel > 2) 56236233Skarels syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 56336233Skarels # endif 56458151Seric usrerr("553 cannot prescan from (%s)", from); 5659536Seric finis(); 5669536Seric } 5679536Seric rewrite(pvp, 3); 5689536Seric rewrite(pvp, 1); 56925032Seric rewrite(pvp, 4); 57058082Seric cataddr(pvp, buf, sizeof buf, '\0'); 57153182Seric e->e_sender = e->e_returnpath = newstr(buf); 5729536Seric 57353182Seric define('f', e->e_sender, e); 57451951Seric 5759536Seric /* save the domain spec if this mailer wants it */ 57653182Seric if (e->e_from.q_mailer != NULL && 57753182Seric bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 5789536Seric { 5799536Seric extern char **copyplist(); 5809536Seric 5819536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 5829536Seric pvp++; 5839536Seric if (*pvp != NULL) 58453182Seric e->e_fromdomain = copyplist(pvp, TRUE); 5859536Seric } 5869536Seric } 587