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*56795Seric static char sccsid[] = "@(#)envelope.c 5.32 (Berkeley) 11/14/92"; 1133729Sbostic #endif /* not lint */ 1222704Sdist 1336928Sbostic #include <sys/types.h> 1436928Sbostic #include <sys/time.h> 1536928Sbostic #include <sys/stat.h> 169536Seric #include <pwd.h> 1740933Srick #include <sys/file.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. 279536Seric ** 289536Seric ** Returns: 299536Seric ** e. 309536Seric ** 319536Seric ** Side Effects: 329536Seric ** none. 339536Seric */ 349536Seric 359536Seric ENVELOPE * 369536Seric newenvelope(e) 379536Seric register ENVELOPE *e; 389536Seric { 399536Seric register ENVELOPE *parent; 409536Seric extern putheader(), putbody(); 4125611Seric extern ENVELOPE BlankEnvelope; 429536Seric 439536Seric parent = CurEnv; 4456215Seric if (e == CurEnv && 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; 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 } 889536Seric #ifdef LOG 8955360Seric if (LogLevel > 12) 909536Seric syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d", 919536Seric e->e_id == NULL ? "(none)" : e->e_id, 929536Seric e->e_flags, getpid()); 93*56795Seric #endif /* LOG */ 949536Seric 959536Seric /* we must have an id to remove disk files */ 969536Seric if (e->e_id == NULL) 979536Seric return; 989536Seric 999536Seric /* 1009536Seric ** Extract state information from dregs of send list. 1019536Seric */ 1029536Seric 1039536Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1049536Seric { 1059536Seric if (bitset(QQUEUEUP, q->q_flags)) 1069536Seric queueit = TRUE; 1079536Seric } 1089536Seric 1099536Seric /* 1109536Seric ** Send back return receipts as requested. 1119536Seric */ 1129536Seric 1139536Seric if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 1149536Seric { 11510844Seric auto ADDRESS *rlist = NULL; 1169536Seric 11755012Seric sendtolist(e->e_receiptto, (ADDRESS *) NULL, &rlist, e); 11855012Seric (void) returntosender("Return receipt", rlist, FALSE, e); 1199536Seric } 1209536Seric 1219536Seric /* 1229536Seric ** Arrange to send error messages if there are fatal errors. 1239536Seric */ 1249536Seric 12510754Seric if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && 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); 143*56795Seric #else /* QUEUE */ 14410754Seric syserr("dropenvelope: queueup"); 145*56795Seric #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 */ 1539536Seric e->e_id = e->e_df = NULL; 15424944Seric if (e->e_dfp != NULL) 15524944Seric (void) fclose(e->e_dfp); 15610196Seric e->e_dfp = NULL; 1579536Seric } 1589536Seric /* 1599536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 1609536Seric ** 1619536Seric ** This is normally used by a child process to get a clean 1629536Seric ** envelope without disturbing the parent. 1639536Seric ** 1649536Seric ** Parameters: 1659536Seric ** e -- the envelope to clear. 16625611Seric ** fullclear - if set, the current envelope is total 16725611Seric ** garbage and should be ignored; otherwise, 16825611Seric ** release any resources it may indicate. 1699536Seric ** 1709536Seric ** Returns: 1719536Seric ** none. 1729536Seric ** 1739536Seric ** Side Effects: 1749536Seric ** Closes files associated with the envelope. 1759536Seric ** Marks the envelope as unallocated. 1769536Seric */ 1779536Seric 17825611Seric clearenvelope(e, fullclear) 1799536Seric register ENVELOPE *e; 18025611Seric bool fullclear; 1819536Seric { 18225514Seric register HDR *bh; 18325514Seric register HDR **nhp; 18425514Seric extern ENVELOPE BlankEnvelope; 18525514Seric 18625611Seric if (!fullclear) 18725611Seric { 18825611Seric /* clear out any file information */ 18925611Seric if (e->e_xfp != NULL) 19025611Seric (void) fclose(e->e_xfp); 19125611Seric if (e->e_dfp != NULL) 19225611Seric (void) fclose(e->e_dfp); 19325611Seric } 1949536Seric 19524961Seric /* now clear out the data */ 19624965Seric STRUCTCOPY(BlankEnvelope, *e); 19725514Seric bh = BlankEnvelope.e_header; 19825514Seric nhp = &e->e_header; 19925514Seric while (bh != NULL) 20025514Seric { 20125514Seric *nhp = (HDR *) xalloc(sizeof *bh); 20225514Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 20325514Seric bh = bh->h_link; 20425514Seric nhp = &(*nhp)->h_link; 20525514Seric } 2069536Seric } 2079536Seric /* 2089536Seric ** INITSYS -- initialize instantiation of system 2099536Seric ** 2109536Seric ** In Daemon mode, this is done in the child. 2119536Seric ** 2129536Seric ** Parameters: 2139536Seric ** none. 2149536Seric ** 2159536Seric ** Returns: 2169536Seric ** none. 2179536Seric ** 2189536Seric ** Side Effects: 2199536Seric ** Initializes the system macros, some global variables, 2209536Seric ** etc. In particular, the current time in various 2219536Seric ** forms is set. 2229536Seric */ 2239536Seric 22455012Seric initsys(e) 22555012Seric register ENVELOPE *e; 2269536Seric { 2279536Seric static char cbuf[5]; /* holds hop count */ 2289536Seric static char pbuf[10]; /* holds pid */ 22922963Smiriam #ifdef TTYNAME 2309536Seric static char ybuf[10]; /* holds tty id */ 2319536Seric register char *p; 232*56795Seric #endif /* TTYNAME */ 2339536Seric extern char *ttyname(); 2349536Seric extern char *macvalue(); 2359536Seric extern char Version[]; 2369536Seric 2379536Seric /* 2389536Seric ** Give this envelope a reality. 2399536Seric ** I.e., an id, a transcript, and a creation time. 2409536Seric */ 2419536Seric 24255012Seric openxscript(e); 24355012Seric e->e_ctime = curtime(); 2449536Seric 2459536Seric /* 2469536Seric ** Set OutChannel to something useful if stdout isn't it. 2479536Seric ** This arranges that any extra stuff the mailer produces 2489536Seric ** gets sent back to the user on error (because it is 2499536Seric ** tucked away in the transcript). 2509536Seric */ 2519536Seric 2529536Seric if (OpMode == MD_DAEMON && QueueRun) 25355012Seric OutChannel = e->e_xfp; 2549536Seric 2559536Seric /* 2569536Seric ** Set up some basic system macros. 2579536Seric */ 2589536Seric 2599536Seric /* process id */ 2609536Seric (void) sprintf(pbuf, "%d", getpid()); 26155012Seric define('p', pbuf, e); 2629536Seric 2639536Seric /* hop count */ 26455012Seric (void) sprintf(cbuf, "%d", e->e_hopcount); 26555012Seric define('c', cbuf, e); 2669536Seric 2679536Seric /* time as integer, unix time, arpa time */ 26855012Seric settime(e); 2699536Seric 27017472Seric #ifdef TTYNAME 2719536Seric /* tty name */ 27255012Seric if (macvalue('y', e) == NULL) 2739536Seric { 2749536Seric p = ttyname(2); 2759536Seric if (p != NULL) 2769536Seric { 277*56795Seric if (strrchr(p, '/') != NULL) 278*56795Seric p = strrchr(p, '/') + 1; 2799536Seric (void) strcpy(ybuf, p); 28055012Seric define('y', ybuf, e); 2819536Seric } 2829536Seric } 283*56795Seric #endif /* TTYNAME */ 2849536Seric } 2859536Seric /* 28611932Seric ** SETTIME -- set the current time. 28711932Seric ** 28811932Seric ** Parameters: 28911932Seric ** none. 29011932Seric ** 29111932Seric ** Returns: 29211932Seric ** none. 29311932Seric ** 29411932Seric ** Side Effects: 29511932Seric ** Sets the various time macros -- $a, $b, $d, $t. 29611932Seric */ 29711932Seric 29855012Seric settime(e) 29955012Seric register ENVELOPE *e; 30011932Seric { 30111932Seric register char *p; 30211932Seric auto time_t now; 30311932Seric static char tbuf[20]; /* holds "current" time */ 30411932Seric static char dbuf[30]; /* holds ctime(tbuf) */ 30511932Seric register struct tm *tm; 30611932Seric extern char *arpadate(); 30711932Seric extern struct tm *gmtime(); 30811932Seric extern char *macvalue(); 30911932Seric 31011932Seric now = curtime(); 31111932Seric tm = gmtime(&now); 31211932Seric (void) sprintf(tbuf, "%02d%02d%02d%02d%02d", tm->tm_year, tm->tm_mon+1, 31311932Seric tm->tm_mday, tm->tm_hour, tm->tm_min); 31455012Seric define('t', tbuf, e); 31511932Seric (void) strcpy(dbuf, ctime(&now)); 316*56795Seric *strchr(dbuf, '\n') = '\0'; 31755012Seric if (macvalue('d', e) == NULL) 31855012Seric define('d', dbuf, e); 31911932Seric p = newstr(arpadate(dbuf)); 32055012Seric if (macvalue('a', e) == NULL) 32155012Seric define('a', p, e); 32255012Seric define('b', p, e); 32311932Seric } 32411932Seric /* 3259536Seric ** OPENXSCRIPT -- Open transcript file 3269536Seric ** 3279536Seric ** Creates a transcript file for possible eventual mailing or 3289536Seric ** sending back. 3299536Seric ** 3309536Seric ** Parameters: 3319536Seric ** e -- the envelope to create the transcript in/for. 3329536Seric ** 3339536Seric ** Returns: 3349536Seric ** none 3359536Seric ** 3369536Seric ** Side Effects: 3379536Seric ** Creates the transcript file. 3389536Seric */ 3399536Seric 3409536Seric openxscript(e) 3419536Seric register ENVELOPE *e; 3429536Seric { 3439536Seric register char *p; 34440933Srick int fd; 3459536Seric 34610196Seric # ifdef LOG 34710196Seric if (LogLevel > 19) 34810196Seric syslog(LOG_DEBUG, "%s: openx%s", e->e_id, e->e_xfp == NULL ? "" : " (no)"); 349*56795Seric # endif /* LOG */ 3509536Seric if (e->e_xfp != NULL) 3519536Seric return; 3529536Seric p = queuename(e, 'x'); 35340933Srick fd = open(p, O_WRONLY|O_CREAT, 0644); 35440933Srick if (fd < 0) 3559536Seric syserr("Can't create %s", p); 3569536Seric else 35740933Srick e->e_xfp = fdopen(fd, "w"); 3589536Seric } 3599536Seric /* 36010196Seric ** CLOSEXSCRIPT -- close the transcript file. 36110196Seric ** 36210196Seric ** Parameters: 36310196Seric ** e -- the envelope containing the transcript to close. 36410196Seric ** 36510196Seric ** Returns: 36610196Seric ** none. 36710196Seric ** 36810196Seric ** Side Effects: 36910196Seric ** none. 37010196Seric */ 37110196Seric 37210196Seric closexscript(e) 37310196Seric register ENVELOPE *e; 37410196Seric { 37510196Seric if (e->e_xfp == NULL) 37610196Seric return; 37710196Seric (void) fclose(e->e_xfp); 37810196Seric e->e_xfp = NULL; 37910196Seric } 38010196Seric /* 3819536Seric ** SETSENDER -- set the person who this message is from 3829536Seric ** 3839536Seric ** Under certain circumstances allow the user to say who 3849536Seric ** s/he is (using -f or -r). These are: 3859536Seric ** 1. The user's uid is zero (root). 3869536Seric ** 2. The user's login name is in an approved list (typically 3879536Seric ** from a network server). 3889536Seric ** 3. The address the user is trying to claim has a 3899536Seric ** "!" character in it (since #2 doesn't do it for 3909536Seric ** us if we are dialing out for UUCP). 3919536Seric ** A better check to replace #3 would be if the 3929536Seric ** effective uid is "UUCP" -- this would require me 3939536Seric ** to rewrite getpwent to "grab" uucp as it went by, 3949536Seric ** make getname more nasty, do another passwd file 3959536Seric ** scan, or compile the UID of "UUCP" into the code, 3969536Seric ** all of which are reprehensible. 3979536Seric ** 3989536Seric ** Assuming all of these fail, we figure out something 3999536Seric ** ourselves. 4009536Seric ** 4019536Seric ** Parameters: 4029536Seric ** from -- the person we would like to believe this message 4039536Seric ** is from, as specified on the command line. 40453182Seric ** e -- the envelope in which we would like the sender set. 4059536Seric ** 4069536Seric ** Returns: 4079536Seric ** none. 4089536Seric ** 4099536Seric ** Side Effects: 4109536Seric ** sets sendmail's notion of who the from person is. 4119536Seric */ 4129536Seric 41353182Seric setsender(from, e) 4149536Seric char *from; 41553182Seric register ENVELOPE *e; 4169536Seric { 4179536Seric register char **pvp; 4189536Seric char *realname = NULL; 41918665Seric register struct passwd *pw; 4209536Seric char buf[MAXNAME]; 42116913Seric char pvpbuf[PSBUFSIZE]; 42218665Seric extern struct passwd *getpwnam(); 4239536Seric extern char *macvalue(); 4249536Seric extern char **prescan(); 4259536Seric extern bool safefile(); 4269536Seric extern char *FullName; 4279536Seric 4289536Seric if (tTd(45, 1)) 42914786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 4309536Seric 4319536Seric /* 4329536Seric ** Figure out the real user executing us. 4339536Seric ** Username can return errno != 0 on non-errors. 4349536Seric */ 4359536Seric 43652220Seric if (QueueRun || OpMode == MD_SMTP) 4379536Seric realname = from; 4389536Seric if (realname == NULL || realname[0] == '\0') 4399536Seric { 4409536Seric extern char *username(); 4419536Seric 4429536Seric realname = username(); 4439536Seric } 4449536Seric 4459536Seric /* 4469536Seric ** Determine if this real person is allowed to alias themselves. 4479536Seric */ 4489536Seric 4499536Seric if (from != NULL) 4509536Seric { 4519536Seric extern bool trusteduser(); 4529536Seric 45336230Skarels if (!trusteduser(realname) && getuid() != geteuid() && 454*56795Seric strchr(from, '!') == NULL && getuid() != 0) 4559536Seric { 4569536Seric /* network sends -r regardless (why why why?) */ 4579536Seric /* syserr("%s, you cannot use the -f flag", realname); */ 4589536Seric from = NULL; 4599536Seric } 4609536Seric } 4619536Seric 4629536Seric SuprErrs = TRUE; 46355012Seric if (from == NULL || parseaddr(from, &e->e_from, 1, '\0', e) == NULL) 4649536Seric { 46521750Seric /* log garbage addresses for traceback */ 46655173Seric # ifdef LOG 46755173Seric if (from != NULL && LogLevel >= 1) 46821750Seric { 46955173Seric char *host = RealHostName; 47055173Seric 47155173Seric if (host == NULL) 47255173Seric host = MyHostName; 47355173Seric syslog(LOG_NOTICE, 47455173Seric "from=%s unparseable, received from %s@%s", 47555173Seric from, realname, host); 47655173Seric } 477*56795Seric # endif /* LOG */ 4789536Seric from = newstr(realname); 47955012Seric if (parseaddr(from, &e->e_from, 1, '\0', e) == NULL && 48055012Seric parseaddr("postmaster", &e->e_from, 1, '\0', e) == NULL) 48124944Seric { 48224944Seric syserr("setsender: can't even parse postmaster!"); 48324944Seric } 4849536Seric } 4859536Seric else 4869536Seric FromFlag = TRUE; 48753182Seric e->e_from.q_flags |= QDONTSEND; 48853182Seric loweraddr(&e->e_from); 4899536Seric SuprErrs = FALSE; 4909536Seric 49153736Seric pvp = NULL; 49253736Seric if (e->e_from.q_mailer == LocalMailer) 4939536Seric { 49453736Seric # ifdef USERDB 49553736Seric register char *p; 49653736Seric extern char *udbsender(); 49753736Seric # endif 49817472Seric 4999536Seric /* if the user has given fullname already, don't redefine */ 5009536Seric if (FullName == NULL) 50153182Seric FullName = macvalue('x', e); 50211932Seric if (FullName != NULL && FullName[0] == '\0') 5039536Seric FullName = NULL; 5049536Seric 50553736Seric # ifdef USERDB 50653736Seric p = udbsender(from); 50753736Seric 50853736Seric if (p != NULL) 5099536Seric { 51053736Seric /* 51153736Seric ** We have an alternate address for the sender 51253736Seric */ 51353736Seric 51453736Seric pvp = prescan(p, '\0', pvpbuf); 5159536Seric } 51653736Seric # endif /* USERDB */ 51753736Seric 51853736Seric if ((pw = getpwnam(e->e_from.q_user)) != NULL) 51953736Seric { 52053736Seric /* 52153736Seric ** Process passwd file entry. 52253736Seric */ 52353736Seric 52453736Seric 52553736Seric /* extract home directory */ 52653736Seric e->e_from.q_home = newstr(pw->pw_dir); 52753736Seric define('z', e->e_from.q_home, e); 52853736Seric 52953736Seric /* extract user and group id */ 53053736Seric e->e_from.q_uid = pw->pw_uid; 53153736Seric e->e_from.q_gid = pw->pw_gid; 53253736Seric 53353736Seric /* extract full name from passwd file */ 53453736Seric if (FullName == NULL && pw->pw_gecos != NULL && 53553736Seric strcmp(pw->pw_name, e->e_from.q_user) == 0) 53653736Seric { 53753736Seric buildfname(pw->pw_gecos, e->e_from.q_user, buf); 53853736Seric if (buf[0] != '\0') 53953736Seric FullName = newstr(buf); 54053736Seric } 54153736Seric } 5429536Seric if (FullName != NULL) 54353182Seric define('x', FullName, e); 5449536Seric } 54511625Seric else 54611625Seric { 54753182Seric if (e->e_from.q_home == NULL) 54853182Seric e->e_from.q_home = getenv("HOME"); 54953182Seric e->e_from.q_uid = getuid(); 55053182Seric e->e_from.q_gid = getgid(); 55111625Seric } 55211625Seric 5539536Seric /* 5549536Seric ** Rewrite the from person to dispose of possible implicit 5559536Seric ** links in the net. 5569536Seric */ 5579536Seric 5589536Seric if (pvp == NULL) 55953736Seric pvp = prescan(from, '\0', pvpbuf); 56053736Seric if (pvp == NULL) 5619536Seric { 56236233Skarels # ifdef LOG 56336233Skarels if (LogLevel >= 1) 56436233Skarels syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 56536233Skarels # endif 56636230Skarels usrerr("cannot prescan from (%s)", from); 5679536Seric finis(); 5689536Seric } 5699536Seric rewrite(pvp, 3); 5709536Seric rewrite(pvp, 1); 57125032Seric rewrite(pvp, 4); 5729536Seric cataddr(pvp, buf, sizeof buf); 57353182Seric e->e_sender = e->e_returnpath = newstr(buf); 5749536Seric 57553182Seric define('f', e->e_sender, e); 57651951Seric 5779536Seric /* save the domain spec if this mailer wants it */ 57853182Seric if (e->e_from.q_mailer != NULL && 57953182Seric bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 5809536Seric { 5819536Seric extern char **copyplist(); 5829536Seric 5839536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 5849536Seric pvp++; 5859536Seric if (*pvp != NULL) 58653182Seric e->e_fromdomain = copyplist(pvp, TRUE); 5879536Seric } 5889536Seric } 5899536Seric /* 5909536Seric ** TRUSTEDUSER -- tell us if this user is to be trusted. 5919536Seric ** 5929536Seric ** Parameters: 5939536Seric ** user -- the user to be checked. 5949536Seric ** 5959536Seric ** Returns: 5969536Seric ** TRUE if the user is in an approved list. 5979536Seric ** FALSE otherwise. 5989536Seric ** 5999536Seric ** Side Effects: 6009536Seric ** none. 6019536Seric */ 6029536Seric 6039536Seric bool 6049536Seric trusteduser(user) 6059536Seric char *user; 6069536Seric { 6079536Seric register char **ulist; 6089536Seric extern char *TrustedUsers[]; 6099536Seric 6109536Seric for (ulist = TrustedUsers; *ulist != NULL; ulist++) 6119536Seric if (strcmp(*ulist, user) == 0) 6129536Seric return (TRUE); 6139536Seric return (FALSE); 6149536Seric } 615