122704Sdist /* 222704Sdist ** Sendmail 322704Sdist ** Copyright (c) 1983 Eric P. Allman 422704Sdist ** Berkeley, California 522704Sdist ** 622704Sdist ** Copyright (c) 1983 Regents of the University of California. 722704Sdist ** All rights reserved. The Berkeley software License Agreement 822704Sdist ** specifies the terms and conditions for redistribution. 922704Sdist */ 1022704Sdist 1122704Sdist #ifndef lint 12*24961Seric static char SccsId[] = "@(#)envelope.c 5.5 (Berkeley) 09/19/85"; 1322704Sdist #endif not lint 1422704Sdist 159536Seric #include <pwd.h> 1613587Swnj #include <sys/time.h> 179536Seric #include "sendmail.h" 189536Seric #include <sys/stat.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 HDR *bh; 409536Seric register HDR **nhp; 419536Seric register ENVELOPE *parent; 429536Seric extern putheader(), putbody(); 439536Seric extern ENVELOPE BlankEnvelope; 449536Seric 459536Seric parent = CurEnv; 469536Seric if (e == CurEnv) 479536Seric parent = e->e_parent; 4816888Seric bzero((char *) e, sizeof *e); 4924944Seric if (e == CurEnv) 5024944Seric bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from); 5124944Seric else 5224944Seric bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from); 539536Seric e->e_parent = parent; 549536Seric e->e_ctime = curtime(); 559536Seric e->e_puthdr = putheader; 569536Seric e->e_putbody = putbody; 579536Seric bh = BlankEnvelope.e_header; 589536Seric nhp = &e->e_header; 599536Seric while (bh != NULL) 609536Seric { 619536Seric *nhp = (HDR *) xalloc(sizeof *bh); 6216888Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 639536Seric bh = bh->h_link; 649536Seric nhp = &(*nhp)->h_link; 659536Seric } 669536Seric if (CurEnv->e_xfp != NULL) 679536Seric (void) fflush(CurEnv->e_xfp); 689536Seric 699536Seric return (e); 709536Seric } 719536Seric /* 729536Seric ** DROPENVELOPE -- deallocate an envelope. 739536Seric ** 749536Seric ** Parameters: 759536Seric ** e -- the envelope to deallocate. 769536Seric ** 779536Seric ** Returns: 789536Seric ** none. 799536Seric ** 809536Seric ** Side Effects: 819536Seric ** housekeeping necessary to dispose of an envelope. 829536Seric ** Unlocks this queue file. 839536Seric */ 849536Seric 859536Seric dropenvelope(e) 869536Seric register ENVELOPE *e; 879536Seric { 889536Seric bool queueit = FALSE; 899536Seric register ADDRESS *q; 909536Seric 919536Seric #ifdef DEBUG 929536Seric if (tTd(50, 1)) 939536Seric { 949536Seric printf("dropenvelope %x id=", e); 959536Seric xputs(e->e_id); 969536Seric printf(" flags=%o\n", e->e_flags); 979536Seric } 989536Seric #endif DEBUG 999536Seric #ifdef LOG 1009536Seric if (LogLevel > 10) 1019536Seric syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d", 1029536Seric e->e_id == NULL ? "(none)" : e->e_id, 1039536Seric e->e_flags, getpid()); 1049536Seric #endif LOG 1059536Seric 1069536Seric /* we must have an id to remove disk files */ 1079536Seric if (e->e_id == NULL) 1089536Seric return; 1099536Seric 1109536Seric /* 1119536Seric ** Extract state information from dregs of send list. 1129536Seric */ 1139536Seric 1149536Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1159536Seric { 1169536Seric if (bitset(QQUEUEUP, q->q_flags)) 1179536Seric queueit = TRUE; 1189536Seric } 1199536Seric 1209536Seric /* 1219536Seric ** Send back return receipts as requested. 1229536Seric */ 1239536Seric 1249536Seric if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 1259536Seric { 12610844Seric auto ADDRESS *rlist = NULL; 1279536Seric 1289621Seric sendtolist(CurEnv->e_receiptto, (ADDRESS *) NULL, &rlist); 1299536Seric (void) returntosender("Return receipt", rlist, FALSE); 1309536Seric } 1319536Seric 1329536Seric /* 1339536Seric ** Arrange to send error messages if there are fatal errors. 1349536Seric */ 1359536Seric 13610754Seric if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && ErrorMode != EM_QUIET) 1379536Seric savemail(e); 1389536Seric 1399536Seric /* 1409536Seric ** Instantiate or deinstantiate the queue. 1419536Seric */ 1429536Seric 1439536Seric if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 1449536Seric bitset(EF_CLRQUEUE, e->e_flags)) 1459536Seric { 14623497Seric if (e->e_df != NULL) 14723497Seric xunlink(e->e_df); 1489536Seric xunlink(queuename(e, 'q')); 1499536Seric } 1509536Seric else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 15110754Seric { 15210754Seric #ifdef QUEUE 1539536Seric queueup(e, FALSE, FALSE); 15410754Seric #else QUEUE 15510754Seric syserr("dropenvelope: queueup"); 15610754Seric #endif QUEUE 15710754Seric } 1589536Seric 1599536Seric /* now unlock the job */ 16010196Seric closexscript(e); 1619536Seric unlockqueue(e); 1629536Seric 1639536Seric /* make sure that this envelope is marked unused */ 1649536Seric e->e_id = e->e_df = NULL; 16524944Seric if (e->e_dfp != NULL) 16624944Seric (void) fclose(e->e_dfp); 16710196Seric e->e_dfp = NULL; 1689536Seric } 1699536Seric /* 1709536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 1719536Seric ** 1729536Seric ** This is normally used by a child process to get a clean 1739536Seric ** envelope without disturbing the parent. 1749536Seric ** 1759536Seric ** Parameters: 1769536Seric ** e -- the envelope to clear. 1779536Seric ** 1789536Seric ** Returns: 1799536Seric ** none. 1809536Seric ** 1819536Seric ** Side Effects: 1829536Seric ** Closes files associated with the envelope. 1839536Seric ** Marks the envelope as unallocated. 1849536Seric */ 1859536Seric 1869536Seric clearenvelope(e) 1879536Seric register ENVELOPE *e; 1889536Seric { 1899536Seric /* clear out any file information */ 1909536Seric if (e->e_xfp != NULL) 1919536Seric (void) fclose(e->e_xfp); 1929536Seric if (e->e_dfp != NULL) 1939536Seric (void) fclose(e->e_dfp); 1949536Seric 195*24961Seric /* now clear out the data */ 196*24961Seric bzero((char *) e, sizeof *e); 1979536Seric } 1989536Seric /* 1999536Seric ** INITSYS -- initialize instantiation of system 2009536Seric ** 2019536Seric ** In Daemon mode, this is done in the child. 2029536Seric ** 2039536Seric ** Parameters: 2049536Seric ** none. 2059536Seric ** 2069536Seric ** Returns: 2079536Seric ** none. 2089536Seric ** 2099536Seric ** Side Effects: 2109536Seric ** Initializes the system macros, some global variables, 2119536Seric ** etc. In particular, the current time in various 2129536Seric ** forms is set. 2139536Seric */ 2149536Seric 2159536Seric initsys() 2169536Seric { 2179536Seric static char cbuf[5]; /* holds hop count */ 2189536Seric static char pbuf[10]; /* holds pid */ 21922963Smiriam #ifdef TTYNAME 2209536Seric static char ybuf[10]; /* holds tty id */ 2219536Seric register char *p; 22222963Smiriam #endif TTYNAME 2239536Seric extern char *ttyname(); 2249536Seric extern char *macvalue(); 2259536Seric extern char Version[]; 2269536Seric 2279536Seric /* 2289536Seric ** Give this envelope a reality. 2299536Seric ** I.e., an id, a transcript, and a creation time. 2309536Seric */ 2319536Seric 2329536Seric openxscript(CurEnv); 2339536Seric CurEnv->e_ctime = curtime(); 2349536Seric 2359536Seric /* 2369536Seric ** Set OutChannel to something useful if stdout isn't it. 2379536Seric ** This arranges that any extra stuff the mailer produces 2389536Seric ** gets sent back to the user on error (because it is 2399536Seric ** tucked away in the transcript). 2409536Seric */ 2419536Seric 2429536Seric if (OpMode == MD_DAEMON && QueueRun) 2439536Seric OutChannel = CurEnv->e_xfp; 2449536Seric 2459536Seric /* 2469536Seric ** Set up some basic system macros. 2479536Seric */ 2489536Seric 2499536Seric /* process id */ 2509536Seric (void) sprintf(pbuf, "%d", getpid()); 2519536Seric define('p', pbuf, CurEnv); 2529536Seric 2539536Seric /* hop count */ 2549536Seric (void) sprintf(cbuf, "%d", CurEnv->e_hopcount); 2559536Seric define('c', cbuf, CurEnv); 2569536Seric 2579536Seric /* time as integer, unix time, arpa time */ 25811932Seric settime(); 2599536Seric 26017472Seric #ifdef TTYNAME 2619536Seric /* tty name */ 2629536Seric if (macvalue('y', CurEnv) == NULL) 2639536Seric { 2649536Seric p = ttyname(2); 2659536Seric if (p != NULL) 2669536Seric { 2679536Seric if (rindex(p, '/') != NULL) 2689536Seric p = rindex(p, '/') + 1; 2699536Seric (void) strcpy(ybuf, p); 2709536Seric define('y', ybuf, CurEnv); 2719536Seric } 2729536Seric } 27317472Seric #endif TTYNAME 2749536Seric } 2759536Seric /* 27611932Seric ** SETTIME -- set the current time. 27711932Seric ** 27811932Seric ** Parameters: 27911932Seric ** none. 28011932Seric ** 28111932Seric ** Returns: 28211932Seric ** none. 28311932Seric ** 28411932Seric ** Side Effects: 28511932Seric ** Sets the various time macros -- $a, $b, $d, $t. 28611932Seric */ 28711932Seric 28811932Seric settime() 28911932Seric { 29011932Seric register char *p; 29111932Seric auto time_t now; 29211932Seric static char tbuf[20]; /* holds "current" time */ 29311932Seric static char dbuf[30]; /* holds ctime(tbuf) */ 29411932Seric register struct tm *tm; 29511932Seric extern char *arpadate(); 29611932Seric extern struct tm *gmtime(); 29711932Seric extern char *macvalue(); 29811932Seric 29911932Seric now = curtime(); 30011932Seric tm = gmtime(&now); 30111932Seric (void) sprintf(tbuf, "%02d%02d%02d%02d%02d", tm->tm_year, tm->tm_mon+1, 30211932Seric tm->tm_mday, tm->tm_hour, tm->tm_min); 30311932Seric define('t', tbuf, CurEnv); 30411932Seric (void) strcpy(dbuf, ctime(&now)); 30511932Seric *index(dbuf, '\n') = '\0'; 30611932Seric if (macvalue('d', CurEnv) == NULL) 30711932Seric define('d', dbuf, CurEnv); 30811932Seric p = newstr(arpadate(dbuf)); 30911932Seric if (macvalue('a', CurEnv) == NULL) 31011932Seric define('a', p, CurEnv); 31111932Seric define('b', p, CurEnv); 31211932Seric } 31311932Seric /* 3149536Seric ** OPENXSCRIPT -- Open transcript file 3159536Seric ** 3169536Seric ** Creates a transcript file for possible eventual mailing or 3179536Seric ** sending back. 3189536Seric ** 3199536Seric ** Parameters: 3209536Seric ** e -- the envelope to create the transcript in/for. 3219536Seric ** 3229536Seric ** Returns: 3239536Seric ** none 3249536Seric ** 3259536Seric ** Side Effects: 3269536Seric ** Creates the transcript file. 3279536Seric */ 3289536Seric 3299536Seric openxscript(e) 3309536Seric register ENVELOPE *e; 3319536Seric { 3329536Seric register char *p; 3339536Seric 33410196Seric # ifdef LOG 33510196Seric if (LogLevel > 19) 33610196Seric syslog(LOG_DEBUG, "%s: openx%s", e->e_id, e->e_xfp == NULL ? "" : " (no)"); 33710196Seric # endif LOG 3389536Seric if (e->e_xfp != NULL) 3399536Seric return; 3409536Seric p = queuename(e, 'x'); 3419536Seric e->e_xfp = fopen(p, "w"); 3429536Seric if (e->e_xfp == NULL) 3439536Seric syserr("Can't create %s", p); 3449536Seric else 3459536Seric (void) chmod(p, 0644); 3469536Seric } 3479536Seric /* 34810196Seric ** CLOSEXSCRIPT -- close the transcript file. 34910196Seric ** 35010196Seric ** Parameters: 35110196Seric ** e -- the envelope containing the transcript to close. 35210196Seric ** 35310196Seric ** Returns: 35410196Seric ** none. 35510196Seric ** 35610196Seric ** Side Effects: 35710196Seric ** none. 35810196Seric */ 35910196Seric 36010196Seric closexscript(e) 36110196Seric register ENVELOPE *e; 36210196Seric { 36310196Seric if (e->e_xfp == NULL) 36410196Seric return; 36510196Seric (void) fclose(e->e_xfp); 36610196Seric e->e_xfp = NULL; 36710196Seric } 36810196Seric /* 3699536Seric ** SETSENDER -- set the person who this message is from 3709536Seric ** 3719536Seric ** Under certain circumstances allow the user to say who 3729536Seric ** s/he is (using -f or -r). These are: 3739536Seric ** 1. The user's uid is zero (root). 3749536Seric ** 2. The user's login name is in an approved list (typically 3759536Seric ** from a network server). 3769536Seric ** 3. The address the user is trying to claim has a 3779536Seric ** "!" character in it (since #2 doesn't do it for 3789536Seric ** us if we are dialing out for UUCP). 3799536Seric ** A better check to replace #3 would be if the 3809536Seric ** effective uid is "UUCP" -- this would require me 3819536Seric ** to rewrite getpwent to "grab" uucp as it went by, 3829536Seric ** make getname more nasty, do another passwd file 3839536Seric ** scan, or compile the UID of "UUCP" into the code, 3849536Seric ** all of which are reprehensible. 3859536Seric ** 3869536Seric ** Assuming all of these fail, we figure out something 3879536Seric ** ourselves. 3889536Seric ** 3899536Seric ** Parameters: 3909536Seric ** from -- the person we would like to believe this message 3919536Seric ** is from, as specified on the command line. 3929536Seric ** 3939536Seric ** Returns: 3949536Seric ** none. 3959536Seric ** 3969536Seric ** Side Effects: 3979536Seric ** sets sendmail's notion of who the from person is. 3989536Seric */ 3999536Seric 4009536Seric setsender(from) 4019536Seric char *from; 4029536Seric { 4039536Seric register char **pvp; 4049536Seric char *realname = NULL; 40518665Seric register struct passwd *pw; 4069536Seric char buf[MAXNAME]; 40716913Seric char pvpbuf[PSBUFSIZE]; 40818665Seric extern struct passwd *getpwnam(); 4099536Seric extern char *macvalue(); 4109536Seric extern char **prescan(); 4119536Seric extern bool safefile(); 4129536Seric extern char *FullName; 4139536Seric 4149536Seric # ifdef DEBUG 4159536Seric if (tTd(45, 1)) 41614786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 4179536Seric # endif DEBUG 4189536Seric 4199536Seric /* 4209536Seric ** Figure out the real user executing us. 4219536Seric ** Username can return errno != 0 on non-errors. 4229536Seric */ 4239536Seric 4249536Seric if (QueueRun || OpMode == MD_SMTP || OpMode == MD_ARPAFTP) 4259536Seric realname = from; 4269536Seric if (realname == NULL || realname[0] == '\0') 4279536Seric { 4289536Seric extern char *username(); 4299536Seric 4309536Seric realname = username(); 4319536Seric } 4329536Seric 4339536Seric /* 4349536Seric ** Determine if this real person is allowed to alias themselves. 4359536Seric */ 4369536Seric 4379536Seric if (from != NULL) 4389536Seric { 4399536Seric extern bool trusteduser(); 4409536Seric 4419536Seric if (!trusteduser(realname) && 4429536Seric # ifdef DEBUG 4439536Seric (!tTd(1, 9) || getuid() != geteuid()) && 4449536Seric # endif DEBUG 4459536Seric index(from, '!') == NULL && getuid() != 0) 4469536Seric { 4479536Seric /* network sends -r regardless (why why why?) */ 4489536Seric /* syserr("%s, you cannot use the -f flag", realname); */ 4499536Seric from = NULL; 4509536Seric } 4519536Seric } 4529536Seric 4539536Seric SuprErrs = TRUE; 45411447Seric if (from == NULL || parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL) 4559536Seric { 45621750Seric /* log garbage addresses for traceback */ 45721750Seric if (from != NULL) 45821750Seric { 45924944Seric # ifdef LOG 46024944Seric if (LogLevel >= 1) 46124944Seric syslog(LOG_ERR, "Unparseable user %s wants to be %s", 46224944Seric realname, from); 46324944Seric # endif LOG 46421750Seric } 4659536Seric from = newstr(realname); 46624944Seric if (parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL && 46724944Seric parseaddr("postmaster", &CurEnv->e_from, 1, '\0') == NULL) 46824944Seric { 46924944Seric syserr("setsender: can't even parse postmaster!"); 47024944Seric } 4719536Seric } 4729536Seric else 4739536Seric FromFlag = TRUE; 4749536Seric CurEnv->e_from.q_flags |= QDONTSEND; 47516162Seric loweraddr(&CurEnv->e_from); 4769536Seric SuprErrs = FALSE; 4779536Seric 47818665Seric if (CurEnv->e_from.q_mailer == LocalMailer && 47918665Seric (pw = getpwnam(CurEnv->e_from.q_user)) != NULL) 4809536Seric { 48117472Seric /* 48217472Seric ** Process passwd file entry. 48317472Seric */ 48417472Seric 4859536Seric 4869536Seric /* extract home directory */ 4879536Seric CurEnv->e_from.q_home = newstr(pw->pw_dir); 48816481Seric define('z', CurEnv->e_from.q_home, CurEnv); 4899536Seric 49011625Seric /* extract user and group id */ 49111625Seric CurEnv->e_from.q_uid = pw->pw_uid; 49211625Seric CurEnv->e_from.q_gid = pw->pw_gid; 49311625Seric 4949536Seric /* if the user has given fullname already, don't redefine */ 4959536Seric if (FullName == NULL) 4969536Seric FullName = macvalue('x', CurEnv); 49711932Seric if (FullName != NULL && FullName[0] == '\0') 4989536Seric FullName = NULL; 4999536Seric 5009536Seric /* extract full name from passwd file */ 5019582Seric if (FullName == NULL && pw->pw_gecos != NULL && 5029582Seric strcmp(pw->pw_name, CurEnv->e_from.q_user) == 0) 5039536Seric { 5049536Seric buildfname(pw->pw_gecos, CurEnv->e_from.q_user, buf); 5059536Seric if (buf[0] != '\0') 5069536Seric FullName = newstr(buf); 5079536Seric } 5089536Seric if (FullName != NULL) 5099536Seric define('x', FullName, CurEnv); 5109536Seric } 51111625Seric else 51211625Seric { 5139536Seric #ifndef V6 51411625Seric if (CurEnv->e_from.q_home == NULL) 51511625Seric CurEnv->e_from.q_home = getenv("HOME"); 5169536Seric #endif V6 51711625Seric CurEnv->e_from.q_uid = getuid(); 51811625Seric CurEnv->e_from.q_gid = getgid(); 51911625Seric } 52011625Seric 5219536Seric if (CurEnv->e_from.q_uid != 0) 5229536Seric { 5239536Seric DefUid = CurEnv->e_from.q_uid; 5249536Seric DefGid = CurEnv->e_from.q_gid; 5259536Seric } 5269536Seric 5279536Seric /* 5289536Seric ** Rewrite the from person to dispose of possible implicit 5299536Seric ** links in the net. 5309536Seric */ 5319536Seric 53216913Seric pvp = prescan(from, '\0', pvpbuf); 5339536Seric if (pvp == NULL) 5349536Seric { 5359536Seric syserr("cannot prescan from (%s)", from); 5369536Seric finis(); 5379536Seric } 5389536Seric rewrite(pvp, 3); 5399536Seric rewrite(pvp, 1); 54011286Seric rewrite(pvp, 4); 5419536Seric cataddr(pvp, buf, sizeof buf); 5429536Seric define('f', newstr(buf), CurEnv); 5439536Seric 5449536Seric /* save the domain spec if this mailer wants it */ 54524944Seric if (CurEnv->e_from.q_mailer != NULL && 54624944Seric bitnset(M_CANONICAL, CurEnv->e_from.q_mailer->m_flags)) 5479536Seric { 5489536Seric extern char **copyplist(); 5499536Seric 5509536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 5519536Seric pvp++; 5529536Seric if (*pvp != NULL) 5539536Seric CurEnv->e_fromdomain = copyplist(pvp, TRUE); 5549536Seric } 5559536Seric } 5569536Seric /* 5579536Seric ** TRUSTEDUSER -- tell us if this user is to be trusted. 5589536Seric ** 5599536Seric ** Parameters: 5609536Seric ** user -- the user to be checked. 5619536Seric ** 5629536Seric ** Returns: 5639536Seric ** TRUE if the user is in an approved list. 5649536Seric ** FALSE otherwise. 5659536Seric ** 5669536Seric ** Side Effects: 5679536Seric ** none. 5689536Seric */ 5699536Seric 5709536Seric bool 5719536Seric trusteduser(user) 5729536Seric char *user; 5739536Seric { 5749536Seric register char **ulist; 5759536Seric extern char *TrustedUsers[]; 5769536Seric 5779536Seric for (ulist = TrustedUsers; *ulist != NULL; ulist++) 5789536Seric if (strcmp(*ulist, user) == 0) 5799536Seric return (TRUE); 5809536Seric return (FALSE); 5819536Seric } 582