122704Sdist /* 2*33729Sbostic * Copyright (c) 1988 Regents of the University of California. 3*33729Sbostic * All rights reserved. 4*33729Sbostic * 5*33729Sbostic * Redistribution and use in source and binary forms are permitted 6*33729Sbostic * provided that this notice is preserved and that due credit is given 7*33729Sbostic * to the University of California at Berkeley. The name of the University 8*33729Sbostic * may not be used to endorse or promote products derived from this 9*33729Sbostic * software without specific prior written permission. This software 10*33729Sbostic * is provided ``as is'' without express or implied warranty. 11*33729Sbostic * 12*33729Sbostic * Sendmail 13*33729Sbostic * Copyright (c) 1983 Eric P. Allman 14*33729Sbostic * Berkeley, California 15*33729Sbostic */ 1622704Sdist 1722704Sdist #ifndef lint 18*33729Sbostic static char sccsid[] = "@(#)envelope.c 5.13 (Berkeley) 03/13/88"; 19*33729Sbostic #endif /* not lint */ 2022704Sdist 219536Seric #include <pwd.h> 2213587Swnj #include <sys/time.h> 239536Seric #include "sendmail.h" 249536Seric #include <sys/stat.h> 259536Seric 269536Seric /* 279536Seric ** NEWENVELOPE -- allocate a new envelope 289536Seric ** 299536Seric ** Supports inheritance. 309536Seric ** 319536Seric ** Parameters: 329536Seric ** e -- the new envelope to fill in. 339536Seric ** 349536Seric ** Returns: 359536Seric ** e. 369536Seric ** 379536Seric ** Side Effects: 389536Seric ** none. 399536Seric */ 409536Seric 419536Seric ENVELOPE * 429536Seric newenvelope(e) 439536Seric register ENVELOPE *e; 449536Seric { 459536Seric register ENVELOPE *parent; 469536Seric extern putheader(), putbody(); 4725611Seric extern ENVELOPE BlankEnvelope; 489536Seric 499536Seric parent = CurEnv; 509536Seric if (e == CurEnv) 519536Seric parent = e->e_parent; 5225611Seric clearenvelope(e, TRUE); 5324944Seric if (e == CurEnv) 5424944Seric bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from); 5524944Seric else 5624944Seric bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from); 579536Seric e->e_parent = parent; 589536Seric e->e_ctime = curtime(); 5925014Seric e->e_msgpriority = parent->e_msgsize; 609536Seric e->e_puthdr = putheader; 619536Seric e->e_putbody = putbody; 629536Seric if (CurEnv->e_xfp != NULL) 639536Seric (void) fflush(CurEnv->e_xfp); 649536Seric 659536Seric return (e); 669536Seric } 679536Seric /* 689536Seric ** DROPENVELOPE -- deallocate an envelope. 699536Seric ** 709536Seric ** Parameters: 719536Seric ** e -- the envelope to deallocate. 729536Seric ** 739536Seric ** Returns: 749536Seric ** none. 759536Seric ** 769536Seric ** Side Effects: 779536Seric ** housekeeping necessary to dispose of an envelope. 789536Seric ** Unlocks this queue file. 799536Seric */ 809536Seric 819536Seric dropenvelope(e) 829536Seric register ENVELOPE *e; 839536Seric { 849536Seric bool queueit = FALSE; 859536Seric register ADDRESS *q; 869536Seric 879536Seric #ifdef DEBUG 889536Seric if (tTd(50, 1)) 899536Seric { 909536Seric printf("dropenvelope %x id=", e); 919536Seric xputs(e->e_id); 929536Seric printf(" flags=%o\n", e->e_flags); 939536Seric } 949536Seric #endif DEBUG 959536Seric #ifdef LOG 969536Seric if (LogLevel > 10) 979536Seric syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d", 989536Seric e->e_id == NULL ? "(none)" : e->e_id, 999536Seric e->e_flags, getpid()); 1009536Seric #endif LOG 1019536Seric 1029536Seric /* we must have an id to remove disk files */ 1039536Seric if (e->e_id == NULL) 1049536Seric return; 1059536Seric 1069536Seric /* 1079536Seric ** Extract state information from dregs of send list. 1089536Seric */ 1099536Seric 1109536Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1119536Seric { 1129536Seric if (bitset(QQUEUEUP, q->q_flags)) 1139536Seric queueit = TRUE; 1149536Seric } 1159536Seric 1169536Seric /* 1179536Seric ** Send back return receipts as requested. 1189536Seric */ 1199536Seric 1209536Seric if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 1219536Seric { 12210844Seric auto ADDRESS *rlist = NULL; 1239536Seric 1249621Seric sendtolist(CurEnv->e_receiptto, (ADDRESS *) NULL, &rlist); 1259536Seric (void) returntosender("Return receipt", rlist, FALSE); 1269536Seric } 1279536Seric 1289536Seric /* 1299536Seric ** Arrange to send error messages if there are fatal errors. 1309536Seric */ 1319536Seric 13210754Seric if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && ErrorMode != EM_QUIET) 1339536Seric savemail(e); 1349536Seric 1359536Seric /* 1369536Seric ** Instantiate or deinstantiate the queue. 1379536Seric */ 1389536Seric 1399536Seric if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 1409536Seric bitset(EF_CLRQUEUE, e->e_flags)) 1419536Seric { 14223497Seric if (e->e_df != NULL) 14323497Seric xunlink(e->e_df); 1449536Seric xunlink(queuename(e, 'q')); 1459536Seric } 1469536Seric else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 14710754Seric { 14810754Seric #ifdef QUEUE 1499536Seric queueup(e, FALSE, FALSE); 15010754Seric #else QUEUE 15110754Seric syserr("dropenvelope: queueup"); 15210754Seric #endif QUEUE 15310754Seric } 1549536Seric 1559536Seric /* now unlock the job */ 15610196Seric closexscript(e); 1579536Seric unlockqueue(e); 1589536Seric 1599536Seric /* make sure that this envelope is marked unused */ 1609536Seric e->e_id = e->e_df = NULL; 16124944Seric if (e->e_dfp != NULL) 16224944Seric (void) fclose(e->e_dfp); 16310196Seric e->e_dfp = NULL; 1649536Seric } 1659536Seric /* 1669536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 1679536Seric ** 1689536Seric ** This is normally used by a child process to get a clean 1699536Seric ** envelope without disturbing the parent. 1709536Seric ** 1719536Seric ** Parameters: 1729536Seric ** e -- the envelope to clear. 17325611Seric ** fullclear - if set, the current envelope is total 17425611Seric ** garbage and should be ignored; otherwise, 17525611Seric ** release any resources it may indicate. 1769536Seric ** 1779536Seric ** Returns: 1789536Seric ** none. 1799536Seric ** 1809536Seric ** Side Effects: 1819536Seric ** Closes files associated with the envelope. 1829536Seric ** Marks the envelope as unallocated. 1839536Seric */ 1849536Seric 18525611Seric clearenvelope(e, fullclear) 1869536Seric register ENVELOPE *e; 18725611Seric bool fullclear; 1889536Seric { 18925514Seric register HDR *bh; 19025514Seric register HDR **nhp; 19125514Seric extern ENVELOPE BlankEnvelope; 19225514Seric 19325611Seric if (!fullclear) 19425611Seric { 19525611Seric /* clear out any file information */ 19625611Seric if (e->e_xfp != NULL) 19725611Seric (void) fclose(e->e_xfp); 19825611Seric if (e->e_dfp != NULL) 19925611Seric (void) fclose(e->e_dfp); 20025611Seric } 2019536Seric 20224961Seric /* now clear out the data */ 20324965Seric STRUCTCOPY(BlankEnvelope, *e); 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 2319536Seric initsys() 2329536Seric { 2339536Seric static char cbuf[5]; /* holds hop count */ 2349536Seric static char pbuf[10]; /* holds pid */ 23522963Smiriam #ifdef TTYNAME 2369536Seric static char ybuf[10]; /* holds tty id */ 2379536Seric register char *p; 23822963Smiriam #endif TTYNAME 2399536Seric extern char *ttyname(); 2409536Seric extern char *macvalue(); 2419536Seric extern char Version[]; 2429536Seric 2439536Seric /* 2449536Seric ** Give this envelope a reality. 2459536Seric ** I.e., an id, a transcript, and a creation time. 2469536Seric */ 2479536Seric 2489536Seric openxscript(CurEnv); 2499536Seric CurEnv->e_ctime = curtime(); 2509536Seric 2519536Seric /* 2529536Seric ** Set OutChannel to something useful if stdout isn't it. 2539536Seric ** This arranges that any extra stuff the mailer produces 2549536Seric ** gets sent back to the user on error (because it is 2559536Seric ** tucked away in the transcript). 2569536Seric */ 2579536Seric 2589536Seric if (OpMode == MD_DAEMON && QueueRun) 2599536Seric OutChannel = CurEnv->e_xfp; 2609536Seric 2619536Seric /* 2629536Seric ** Set up some basic system macros. 2639536Seric */ 2649536Seric 2659536Seric /* process id */ 2669536Seric (void) sprintf(pbuf, "%d", getpid()); 2679536Seric define('p', pbuf, CurEnv); 2689536Seric 2699536Seric /* hop count */ 2709536Seric (void) sprintf(cbuf, "%d", CurEnv->e_hopcount); 2719536Seric define('c', cbuf, CurEnv); 2729536Seric 2739536Seric /* time as integer, unix time, arpa time */ 27411932Seric settime(); 2759536Seric 27617472Seric #ifdef TTYNAME 2779536Seric /* tty name */ 2789536Seric if (macvalue('y', CurEnv) == NULL) 2799536Seric { 2809536Seric p = ttyname(2); 2819536Seric if (p != NULL) 2829536Seric { 2839536Seric if (rindex(p, '/') != NULL) 2849536Seric p = rindex(p, '/') + 1; 2859536Seric (void) strcpy(ybuf, p); 2869536Seric define('y', ybuf, CurEnv); 2879536Seric } 2889536Seric } 28917472Seric #endif TTYNAME 2909536Seric } 2919536Seric /* 29211932Seric ** SETTIME -- set the current time. 29311932Seric ** 29411932Seric ** Parameters: 29511932Seric ** none. 29611932Seric ** 29711932Seric ** Returns: 29811932Seric ** none. 29911932Seric ** 30011932Seric ** Side Effects: 30111932Seric ** Sets the various time macros -- $a, $b, $d, $t. 30211932Seric */ 30311932Seric 30411932Seric settime() 30511932Seric { 30611932Seric register char *p; 30711932Seric auto time_t now; 30811932Seric static char tbuf[20]; /* holds "current" time */ 30911932Seric static char dbuf[30]; /* holds ctime(tbuf) */ 31011932Seric register struct tm *tm; 31111932Seric extern char *arpadate(); 31211932Seric extern struct tm *gmtime(); 31311932Seric extern char *macvalue(); 31411932Seric 31511932Seric now = curtime(); 31611932Seric tm = gmtime(&now); 31711932Seric (void) sprintf(tbuf, "%02d%02d%02d%02d%02d", tm->tm_year, tm->tm_mon+1, 31811932Seric tm->tm_mday, tm->tm_hour, tm->tm_min); 31911932Seric define('t', tbuf, CurEnv); 32011932Seric (void) strcpy(dbuf, ctime(&now)); 32111932Seric *index(dbuf, '\n') = '\0'; 32211932Seric if (macvalue('d', CurEnv) == NULL) 32311932Seric define('d', dbuf, CurEnv); 32411932Seric p = newstr(arpadate(dbuf)); 32511932Seric if (macvalue('a', CurEnv) == NULL) 32611932Seric define('a', p, CurEnv); 32711932Seric define('b', p, CurEnv); 32811932Seric } 32911932Seric /* 3309536Seric ** OPENXSCRIPT -- Open transcript file 3319536Seric ** 3329536Seric ** Creates a transcript file for possible eventual mailing or 3339536Seric ** sending back. 3349536Seric ** 3359536Seric ** Parameters: 3369536Seric ** e -- the envelope to create the transcript in/for. 3379536Seric ** 3389536Seric ** Returns: 3399536Seric ** none 3409536Seric ** 3419536Seric ** Side Effects: 3429536Seric ** Creates the transcript file. 3439536Seric */ 3449536Seric 3459536Seric openxscript(e) 3469536Seric register ENVELOPE *e; 3479536Seric { 3489536Seric register char *p; 3499536Seric 35010196Seric # ifdef LOG 35110196Seric if (LogLevel > 19) 35210196Seric syslog(LOG_DEBUG, "%s: openx%s", e->e_id, e->e_xfp == NULL ? "" : " (no)"); 35310196Seric # endif LOG 3549536Seric if (e->e_xfp != NULL) 3559536Seric return; 3569536Seric p = queuename(e, 'x'); 3579536Seric e->e_xfp = fopen(p, "w"); 3589536Seric if (e->e_xfp == NULL) 3599536Seric syserr("Can't create %s", p); 3609536Seric else 3619536Seric (void) chmod(p, 0644); 3629536Seric } 3639536Seric /* 36410196Seric ** CLOSEXSCRIPT -- close the transcript file. 36510196Seric ** 36610196Seric ** Parameters: 36710196Seric ** e -- the envelope containing the transcript to close. 36810196Seric ** 36910196Seric ** Returns: 37010196Seric ** none. 37110196Seric ** 37210196Seric ** Side Effects: 37310196Seric ** none. 37410196Seric */ 37510196Seric 37610196Seric closexscript(e) 37710196Seric register ENVELOPE *e; 37810196Seric { 37910196Seric if (e->e_xfp == NULL) 38010196Seric return; 38110196Seric (void) fclose(e->e_xfp); 38210196Seric e->e_xfp = NULL; 38310196Seric } 38410196Seric /* 3859536Seric ** SETSENDER -- set the person who this message is from 3869536Seric ** 3879536Seric ** Under certain circumstances allow the user to say who 3889536Seric ** s/he is (using -f or -r). These are: 3899536Seric ** 1. The user's uid is zero (root). 3909536Seric ** 2. The user's login name is in an approved list (typically 3919536Seric ** from a network server). 3929536Seric ** 3. The address the user is trying to claim has a 3939536Seric ** "!" character in it (since #2 doesn't do it for 3949536Seric ** us if we are dialing out for UUCP). 3959536Seric ** A better check to replace #3 would be if the 3969536Seric ** effective uid is "UUCP" -- this would require me 3979536Seric ** to rewrite getpwent to "grab" uucp as it went by, 3989536Seric ** make getname more nasty, do another passwd file 3999536Seric ** scan, or compile the UID of "UUCP" into the code, 4009536Seric ** all of which are reprehensible. 4019536Seric ** 4029536Seric ** Assuming all of these fail, we figure out something 4039536Seric ** ourselves. 4049536Seric ** 4059536Seric ** Parameters: 4069536Seric ** from -- the person we would like to believe this message 4079536Seric ** is from, as specified on the command line. 4089536Seric ** 4099536Seric ** Returns: 4109536Seric ** none. 4119536Seric ** 4129536Seric ** Side Effects: 4139536Seric ** sets sendmail's notion of who the from person is. 4149536Seric */ 4159536Seric 4169536Seric setsender(from) 4179536Seric char *from; 4189536Seric { 4199536Seric register char **pvp; 4209536Seric char *realname = NULL; 42118665Seric register struct passwd *pw; 4229536Seric char buf[MAXNAME]; 42316913Seric char pvpbuf[PSBUFSIZE]; 42418665Seric extern struct passwd *getpwnam(); 4259536Seric extern char *macvalue(); 4269536Seric extern char **prescan(); 4279536Seric extern bool safefile(); 4289536Seric extern char *FullName; 4299536Seric 4309536Seric # ifdef DEBUG 4319536Seric if (tTd(45, 1)) 43214786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 4339536Seric # endif DEBUG 4349536Seric 4359536Seric /* 4369536Seric ** Figure out the real user executing us. 4379536Seric ** Username can return errno != 0 on non-errors. 4389536Seric */ 4399536Seric 4409536Seric if (QueueRun || OpMode == MD_SMTP || OpMode == MD_ARPAFTP) 4419536Seric realname = from; 4429536Seric if (realname == NULL || realname[0] == '\0') 4439536Seric { 4449536Seric extern char *username(); 4459536Seric 4469536Seric realname = username(); 4479536Seric } 4489536Seric 4499536Seric /* 4509536Seric ** Determine if this real person is allowed to alias themselves. 4519536Seric */ 4529536Seric 4539536Seric if (from != NULL) 4549536Seric { 4559536Seric extern bool trusteduser(); 4569536Seric 4579536Seric if (!trusteduser(realname) && 4589536Seric # ifdef DEBUG 4599536Seric (!tTd(1, 9) || getuid() != geteuid()) && 4609536Seric # endif DEBUG 4619536Seric index(from, '!') == NULL && getuid() != 0) 4629536Seric { 4639536Seric /* network sends -r regardless (why why why?) */ 4649536Seric /* syserr("%s, you cannot use the -f flag", realname); */ 4659536Seric from = NULL; 4669536Seric } 4679536Seric } 4689536Seric 4699536Seric SuprErrs = TRUE; 47011447Seric if (from == NULL || parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL) 4719536Seric { 47221750Seric /* log garbage addresses for traceback */ 47321750Seric if (from != NULL) 47421750Seric { 47524944Seric # ifdef LOG 47624944Seric if (LogLevel >= 1) 47724944Seric syslog(LOG_ERR, "Unparseable user %s wants to be %s", 47824944Seric realname, from); 47924944Seric # endif LOG 48021750Seric } 4819536Seric from = newstr(realname); 48224944Seric if (parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL && 48324944Seric parseaddr("postmaster", &CurEnv->e_from, 1, '\0') == NULL) 48424944Seric { 48524944Seric syserr("setsender: can't even parse postmaster!"); 48624944Seric } 4879536Seric } 4889536Seric else 4899536Seric FromFlag = TRUE; 4909536Seric CurEnv->e_from.q_flags |= QDONTSEND; 49116162Seric loweraddr(&CurEnv->e_from); 4929536Seric SuprErrs = FALSE; 4939536Seric 49418665Seric if (CurEnv->e_from.q_mailer == LocalMailer && 49518665Seric (pw = getpwnam(CurEnv->e_from.q_user)) != NULL) 4969536Seric { 49717472Seric /* 49817472Seric ** Process passwd file entry. 49917472Seric */ 50017472Seric 5019536Seric 5029536Seric /* extract home directory */ 5039536Seric CurEnv->e_from.q_home = newstr(pw->pw_dir); 50416481Seric define('z', CurEnv->e_from.q_home, CurEnv); 5059536Seric 50611625Seric /* extract user and group id */ 50711625Seric CurEnv->e_from.q_uid = pw->pw_uid; 50811625Seric CurEnv->e_from.q_gid = pw->pw_gid; 50911625Seric 5109536Seric /* if the user has given fullname already, don't redefine */ 5119536Seric if (FullName == NULL) 5129536Seric FullName = macvalue('x', CurEnv); 51311932Seric if (FullName != NULL && FullName[0] == '\0') 5149536Seric FullName = NULL; 5159536Seric 5169536Seric /* extract full name from passwd file */ 5179582Seric if (FullName == NULL && pw->pw_gecos != NULL && 5189582Seric strcmp(pw->pw_name, CurEnv->e_from.q_user) == 0) 5199536Seric { 5209536Seric buildfname(pw->pw_gecos, CurEnv->e_from.q_user, buf); 5219536Seric if (buf[0] != '\0') 5229536Seric FullName = newstr(buf); 5239536Seric } 5249536Seric if (FullName != NULL) 5259536Seric define('x', FullName, CurEnv); 5269536Seric } 52711625Seric else 52811625Seric { 5299536Seric #ifndef V6 53011625Seric if (CurEnv->e_from.q_home == NULL) 53111625Seric CurEnv->e_from.q_home = getenv("HOME"); 5329536Seric #endif V6 53311625Seric CurEnv->e_from.q_uid = getuid(); 53411625Seric CurEnv->e_from.q_gid = getgid(); 53511625Seric } 53611625Seric 5379536Seric if (CurEnv->e_from.q_uid != 0) 5389536Seric { 5399536Seric DefUid = CurEnv->e_from.q_uid; 5409536Seric DefGid = CurEnv->e_from.q_gid; 5419536Seric } 5429536Seric 5439536Seric /* 5449536Seric ** Rewrite the from person to dispose of possible implicit 5459536Seric ** links in the net. 5469536Seric */ 5479536Seric 54816913Seric pvp = prescan(from, '\0', pvpbuf); 5499536Seric if (pvp == NULL) 5509536Seric { 5519536Seric syserr("cannot prescan from (%s)", from); 5529536Seric finis(); 5539536Seric } 5549536Seric rewrite(pvp, 3); 5559536Seric rewrite(pvp, 1); 55625032Seric rewrite(pvp, 4); 5579536Seric cataddr(pvp, buf, sizeof buf); 5589536Seric define('f', newstr(buf), CurEnv); 5599536Seric 5609536Seric /* save the domain spec if this mailer wants it */ 56124944Seric if (CurEnv->e_from.q_mailer != NULL && 56224944Seric bitnset(M_CANONICAL, CurEnv->e_from.q_mailer->m_flags)) 5639536Seric { 5649536Seric extern char **copyplist(); 5659536Seric 5669536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 5679536Seric pvp++; 5689536Seric if (*pvp != NULL) 5699536Seric CurEnv->e_fromdomain = copyplist(pvp, TRUE); 5709536Seric } 5719536Seric } 5729536Seric /* 5739536Seric ** TRUSTEDUSER -- tell us if this user is to be trusted. 5749536Seric ** 5759536Seric ** Parameters: 5769536Seric ** user -- the user to be checked. 5779536Seric ** 5789536Seric ** Returns: 5799536Seric ** TRUE if the user is in an approved list. 5809536Seric ** FALSE otherwise. 5819536Seric ** 5829536Seric ** Side Effects: 5839536Seric ** none. 5849536Seric */ 5859536Seric 5869536Seric bool 5879536Seric trusteduser(user) 5889536Seric char *user; 5899536Seric { 5909536Seric register char **ulist; 5919536Seric extern char *TrustedUsers[]; 5929536Seric 5939536Seric for (ulist = TrustedUsers; *ulist != NULL; ulist++) 5949536Seric if (strcmp(*ulist, user) == 0) 5959536Seric return (TRUE); 5969536Seric return (FALSE); 5979536Seric } 598