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*25514Seric static char SccsId[] = "@(#)envelope.c 5.11 (Berkeley) 11/21/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 ENVELOPE *parent; 409536Seric extern putheader(), putbody(); 419536Seric 429536Seric parent = CurEnv; 439536Seric if (e == CurEnv) 449536Seric parent = e->e_parent; 45*25514Seric clearenvelope(e); 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(); 5225014Seric e->e_msgpriority = parent->e_msgsize; 539536Seric e->e_puthdr = putheader; 549536Seric e->e_putbody = putbody; 559536Seric if (CurEnv->e_xfp != NULL) 569536Seric (void) fflush(CurEnv->e_xfp); 579536Seric 589536Seric return (e); 599536Seric } 609536Seric /* 619536Seric ** DROPENVELOPE -- deallocate an envelope. 629536Seric ** 639536Seric ** Parameters: 649536Seric ** e -- the envelope to deallocate. 659536Seric ** 669536Seric ** Returns: 679536Seric ** none. 689536Seric ** 699536Seric ** Side Effects: 709536Seric ** housekeeping necessary to dispose of an envelope. 719536Seric ** Unlocks this queue file. 729536Seric */ 739536Seric 749536Seric dropenvelope(e) 759536Seric register ENVELOPE *e; 769536Seric { 779536Seric bool queueit = FALSE; 789536Seric register ADDRESS *q; 799536Seric 809536Seric #ifdef DEBUG 819536Seric if (tTd(50, 1)) 829536Seric { 839536Seric printf("dropenvelope %x id=", e); 849536Seric xputs(e->e_id); 859536Seric printf(" flags=%o\n", e->e_flags); 869536Seric } 879536Seric #endif DEBUG 889536Seric #ifdef LOG 899536Seric if (LogLevel > 10) 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()); 939536Seric #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 1179621Seric sendtolist(CurEnv->e_receiptto, (ADDRESS *) NULL, &rlist); 1189536Seric (void) returntosender("Return receipt", rlist, FALSE); 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 1429536Seric queueup(e, FALSE, FALSE); 14310754Seric #else QUEUE 14410754Seric syserr("dropenvelope: queueup"); 14510754Seric #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. 1669536Seric ** 1679536Seric ** Returns: 1689536Seric ** none. 1699536Seric ** 1709536Seric ** Side Effects: 1719536Seric ** Closes files associated with the envelope. 1729536Seric ** Marks the envelope as unallocated. 1739536Seric */ 1749536Seric 1759536Seric clearenvelope(e) 1769536Seric register ENVELOPE *e; 1779536Seric { 178*25514Seric register HDR *bh; 179*25514Seric register HDR **nhp; 180*25514Seric extern ENVELOPE BlankEnvelope; 181*25514Seric 1829536Seric /* clear out any file information */ 1839536Seric if (e->e_xfp != NULL) 1849536Seric (void) fclose(e->e_xfp); 1859536Seric if (e->e_dfp != NULL) 1869536Seric (void) fclose(e->e_dfp); 1879536Seric 18824961Seric /* now clear out the data */ 18924965Seric STRUCTCOPY(BlankEnvelope, *e); 190*25514Seric bh = BlankEnvelope.e_header; 191*25514Seric nhp = &e->e_header; 192*25514Seric while (bh != NULL) 193*25514Seric { 194*25514Seric *nhp = (HDR *) xalloc(sizeof *bh); 195*25514Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 196*25514Seric bh = bh->h_link; 197*25514Seric nhp = &(*nhp)->h_link; 198*25514Seric } 1999536Seric } 2009536Seric /* 2019536Seric ** INITSYS -- initialize instantiation of system 2029536Seric ** 2039536Seric ** In Daemon mode, this is done in the child. 2049536Seric ** 2059536Seric ** Parameters: 2069536Seric ** none. 2079536Seric ** 2089536Seric ** Returns: 2099536Seric ** none. 2109536Seric ** 2119536Seric ** Side Effects: 2129536Seric ** Initializes the system macros, some global variables, 2139536Seric ** etc. In particular, the current time in various 2149536Seric ** forms is set. 2159536Seric */ 2169536Seric 2179536Seric initsys() 2189536Seric { 2199536Seric static char cbuf[5]; /* holds hop count */ 2209536Seric static char pbuf[10]; /* holds pid */ 22122963Smiriam #ifdef TTYNAME 2229536Seric static char ybuf[10]; /* holds tty id */ 2239536Seric register char *p; 22422963Smiriam #endif TTYNAME 2259536Seric extern char *ttyname(); 2269536Seric extern char *macvalue(); 2279536Seric extern char Version[]; 2289536Seric 2299536Seric /* 2309536Seric ** Give this envelope a reality. 2319536Seric ** I.e., an id, a transcript, and a creation time. 2329536Seric */ 2339536Seric 2349536Seric openxscript(CurEnv); 2359536Seric CurEnv->e_ctime = curtime(); 2369536Seric 2379536Seric /* 2389536Seric ** Set OutChannel to something useful if stdout isn't it. 2399536Seric ** This arranges that any extra stuff the mailer produces 2409536Seric ** gets sent back to the user on error (because it is 2419536Seric ** tucked away in the transcript). 2429536Seric */ 2439536Seric 2449536Seric if (OpMode == MD_DAEMON && QueueRun) 2459536Seric OutChannel = CurEnv->e_xfp; 2469536Seric 2479536Seric /* 2489536Seric ** Set up some basic system macros. 2499536Seric */ 2509536Seric 2519536Seric /* process id */ 2529536Seric (void) sprintf(pbuf, "%d", getpid()); 2539536Seric define('p', pbuf, CurEnv); 2549536Seric 2559536Seric /* hop count */ 2569536Seric (void) sprintf(cbuf, "%d", CurEnv->e_hopcount); 2579536Seric define('c', cbuf, CurEnv); 2589536Seric 2599536Seric /* time as integer, unix time, arpa time */ 26011932Seric settime(); 2619536Seric 26217472Seric #ifdef TTYNAME 2639536Seric /* tty name */ 2649536Seric if (macvalue('y', CurEnv) == NULL) 2659536Seric { 2669536Seric p = ttyname(2); 2679536Seric if (p != NULL) 2689536Seric { 2699536Seric if (rindex(p, '/') != NULL) 2709536Seric p = rindex(p, '/') + 1; 2719536Seric (void) strcpy(ybuf, p); 2729536Seric define('y', ybuf, CurEnv); 2739536Seric } 2749536Seric } 27517472Seric #endif TTYNAME 2769536Seric } 2779536Seric /* 27811932Seric ** SETTIME -- set the current time. 27911932Seric ** 28011932Seric ** Parameters: 28111932Seric ** none. 28211932Seric ** 28311932Seric ** Returns: 28411932Seric ** none. 28511932Seric ** 28611932Seric ** Side Effects: 28711932Seric ** Sets the various time macros -- $a, $b, $d, $t. 28811932Seric */ 28911932Seric 29011932Seric settime() 29111932Seric { 29211932Seric register char *p; 29311932Seric auto time_t now; 29411932Seric static char tbuf[20]; /* holds "current" time */ 29511932Seric static char dbuf[30]; /* holds ctime(tbuf) */ 29611932Seric register struct tm *tm; 29711932Seric extern char *arpadate(); 29811932Seric extern struct tm *gmtime(); 29911932Seric extern char *macvalue(); 30011932Seric 30111932Seric now = curtime(); 30211932Seric tm = gmtime(&now); 30311932Seric (void) sprintf(tbuf, "%02d%02d%02d%02d%02d", tm->tm_year, tm->tm_mon+1, 30411932Seric tm->tm_mday, tm->tm_hour, tm->tm_min); 30511932Seric define('t', tbuf, CurEnv); 30611932Seric (void) strcpy(dbuf, ctime(&now)); 30711932Seric *index(dbuf, '\n') = '\0'; 30811932Seric if (macvalue('d', CurEnv) == NULL) 30911932Seric define('d', dbuf, CurEnv); 31011932Seric p = newstr(arpadate(dbuf)); 31111932Seric if (macvalue('a', CurEnv) == NULL) 31211932Seric define('a', p, CurEnv); 31311932Seric define('b', p, CurEnv); 31411932Seric } 31511932Seric /* 3169536Seric ** OPENXSCRIPT -- Open transcript file 3179536Seric ** 3189536Seric ** Creates a transcript file for possible eventual mailing or 3199536Seric ** sending back. 3209536Seric ** 3219536Seric ** Parameters: 3229536Seric ** e -- the envelope to create the transcript in/for. 3239536Seric ** 3249536Seric ** Returns: 3259536Seric ** none 3269536Seric ** 3279536Seric ** Side Effects: 3289536Seric ** Creates the transcript file. 3299536Seric */ 3309536Seric 3319536Seric openxscript(e) 3329536Seric register ENVELOPE *e; 3339536Seric { 3349536Seric register char *p; 3359536Seric 33610196Seric # ifdef LOG 33710196Seric if (LogLevel > 19) 33810196Seric syslog(LOG_DEBUG, "%s: openx%s", e->e_id, e->e_xfp == NULL ? "" : " (no)"); 33910196Seric # endif LOG 3409536Seric if (e->e_xfp != NULL) 3419536Seric return; 3429536Seric p = queuename(e, 'x'); 3439536Seric e->e_xfp = fopen(p, "w"); 3449536Seric if (e->e_xfp == NULL) 3459536Seric syserr("Can't create %s", p); 3469536Seric else 3479536Seric (void) chmod(p, 0644); 3489536Seric } 3499536Seric /* 35010196Seric ** CLOSEXSCRIPT -- close the transcript file. 35110196Seric ** 35210196Seric ** Parameters: 35310196Seric ** e -- the envelope containing the transcript to close. 35410196Seric ** 35510196Seric ** Returns: 35610196Seric ** none. 35710196Seric ** 35810196Seric ** Side Effects: 35910196Seric ** none. 36010196Seric */ 36110196Seric 36210196Seric closexscript(e) 36310196Seric register ENVELOPE *e; 36410196Seric { 36510196Seric if (e->e_xfp == NULL) 36610196Seric return; 36710196Seric (void) fclose(e->e_xfp); 36810196Seric e->e_xfp = NULL; 36910196Seric } 37010196Seric /* 3719536Seric ** SETSENDER -- set the person who this message is from 3729536Seric ** 3739536Seric ** Under certain circumstances allow the user to say who 3749536Seric ** s/he is (using -f or -r). These are: 3759536Seric ** 1. The user's uid is zero (root). 3769536Seric ** 2. The user's login name is in an approved list (typically 3779536Seric ** from a network server). 3789536Seric ** 3. The address the user is trying to claim has a 3799536Seric ** "!" character in it (since #2 doesn't do it for 3809536Seric ** us if we are dialing out for UUCP). 3819536Seric ** A better check to replace #3 would be if the 3829536Seric ** effective uid is "UUCP" -- this would require me 3839536Seric ** to rewrite getpwent to "grab" uucp as it went by, 3849536Seric ** make getname more nasty, do another passwd file 3859536Seric ** scan, or compile the UID of "UUCP" into the code, 3869536Seric ** all of which are reprehensible. 3879536Seric ** 3889536Seric ** Assuming all of these fail, we figure out something 3899536Seric ** ourselves. 3909536Seric ** 3919536Seric ** Parameters: 3929536Seric ** from -- the person we would like to believe this message 3939536Seric ** is from, as specified on the command line. 3949536Seric ** 3959536Seric ** Returns: 3969536Seric ** none. 3979536Seric ** 3989536Seric ** Side Effects: 3999536Seric ** sets sendmail's notion of who the from person is. 4009536Seric */ 4019536Seric 4029536Seric setsender(from) 4039536Seric char *from; 4049536Seric { 4059536Seric register char **pvp; 4069536Seric char *realname = NULL; 40718665Seric register struct passwd *pw; 4089536Seric char buf[MAXNAME]; 40916913Seric char pvpbuf[PSBUFSIZE]; 41018665Seric extern struct passwd *getpwnam(); 4119536Seric extern char *macvalue(); 4129536Seric extern char **prescan(); 4139536Seric extern bool safefile(); 4149536Seric extern char *FullName; 4159536Seric 4169536Seric # ifdef DEBUG 4179536Seric if (tTd(45, 1)) 41814786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 4199536Seric # endif DEBUG 4209536Seric 4219536Seric /* 4229536Seric ** Figure out the real user executing us. 4239536Seric ** Username can return errno != 0 on non-errors. 4249536Seric */ 4259536Seric 4269536Seric if (QueueRun || OpMode == MD_SMTP || OpMode == MD_ARPAFTP) 4279536Seric realname = from; 4289536Seric if (realname == NULL || realname[0] == '\0') 4299536Seric { 4309536Seric extern char *username(); 4319536Seric 4329536Seric realname = username(); 4339536Seric } 4349536Seric 4359536Seric /* 4369536Seric ** Determine if this real person is allowed to alias themselves. 4379536Seric */ 4389536Seric 4399536Seric if (from != NULL) 4409536Seric { 4419536Seric extern bool trusteduser(); 4429536Seric 4439536Seric if (!trusteduser(realname) && 4449536Seric # ifdef DEBUG 4459536Seric (!tTd(1, 9) || getuid() != geteuid()) && 4469536Seric # endif DEBUG 4479536Seric index(from, '!') == NULL && getuid() != 0) 4489536Seric { 4499536Seric /* network sends -r regardless (why why why?) */ 4509536Seric /* syserr("%s, you cannot use the -f flag", realname); */ 4519536Seric from = NULL; 4529536Seric } 4539536Seric } 4549536Seric 4559536Seric SuprErrs = TRUE; 45611447Seric if (from == NULL || parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL) 4579536Seric { 45821750Seric /* log garbage addresses for traceback */ 45921750Seric if (from != NULL) 46021750Seric { 46124944Seric # ifdef LOG 46224944Seric if (LogLevel >= 1) 46324944Seric syslog(LOG_ERR, "Unparseable user %s wants to be %s", 46424944Seric realname, from); 46524944Seric # endif LOG 46621750Seric } 4679536Seric from = newstr(realname); 46824944Seric if (parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL && 46924944Seric parseaddr("postmaster", &CurEnv->e_from, 1, '\0') == NULL) 47024944Seric { 47124944Seric syserr("setsender: can't even parse postmaster!"); 47224944Seric } 4739536Seric } 4749536Seric else 4759536Seric FromFlag = TRUE; 4769536Seric CurEnv->e_from.q_flags |= QDONTSEND; 47716162Seric loweraddr(&CurEnv->e_from); 4789536Seric SuprErrs = FALSE; 4799536Seric 48018665Seric if (CurEnv->e_from.q_mailer == LocalMailer && 48118665Seric (pw = getpwnam(CurEnv->e_from.q_user)) != NULL) 4829536Seric { 48317472Seric /* 48417472Seric ** Process passwd file entry. 48517472Seric */ 48617472Seric 4879536Seric 4889536Seric /* extract home directory */ 4899536Seric CurEnv->e_from.q_home = newstr(pw->pw_dir); 49016481Seric define('z', CurEnv->e_from.q_home, CurEnv); 4919536Seric 49211625Seric /* extract user and group id */ 49311625Seric CurEnv->e_from.q_uid = pw->pw_uid; 49411625Seric CurEnv->e_from.q_gid = pw->pw_gid; 49511625Seric 4969536Seric /* if the user has given fullname already, don't redefine */ 4979536Seric if (FullName == NULL) 4989536Seric FullName = macvalue('x', CurEnv); 49911932Seric if (FullName != NULL && FullName[0] == '\0') 5009536Seric FullName = NULL; 5019536Seric 5029536Seric /* extract full name from passwd file */ 5039582Seric if (FullName == NULL && pw->pw_gecos != NULL && 5049582Seric strcmp(pw->pw_name, CurEnv->e_from.q_user) == 0) 5059536Seric { 5069536Seric buildfname(pw->pw_gecos, CurEnv->e_from.q_user, buf); 5079536Seric if (buf[0] != '\0') 5089536Seric FullName = newstr(buf); 5099536Seric } 5109536Seric if (FullName != NULL) 5119536Seric define('x', FullName, CurEnv); 5129536Seric } 51311625Seric else 51411625Seric { 5159536Seric #ifndef V6 51611625Seric if (CurEnv->e_from.q_home == NULL) 51711625Seric CurEnv->e_from.q_home = getenv("HOME"); 5189536Seric #endif V6 51911625Seric CurEnv->e_from.q_uid = getuid(); 52011625Seric CurEnv->e_from.q_gid = getgid(); 52111625Seric } 52211625Seric 5239536Seric if (CurEnv->e_from.q_uid != 0) 5249536Seric { 5259536Seric DefUid = CurEnv->e_from.q_uid; 5269536Seric DefGid = CurEnv->e_from.q_gid; 5279536Seric } 5289536Seric 5299536Seric /* 5309536Seric ** Rewrite the from person to dispose of possible implicit 5319536Seric ** links in the net. 5329536Seric */ 5339536Seric 53416913Seric pvp = prescan(from, '\0', pvpbuf); 5359536Seric if (pvp == NULL) 5369536Seric { 5379536Seric syserr("cannot prescan from (%s)", from); 5389536Seric finis(); 5399536Seric } 5409536Seric rewrite(pvp, 3); 5419536Seric rewrite(pvp, 1); 54225032Seric rewrite(pvp, 4); 5439536Seric cataddr(pvp, buf, sizeof buf); 5449536Seric define('f', newstr(buf), CurEnv); 5459536Seric 5469536Seric /* save the domain spec if this mailer wants it */ 54724944Seric if (CurEnv->e_from.q_mailer != NULL && 54824944Seric bitnset(M_CANONICAL, CurEnv->e_from.q_mailer->m_flags)) 5499536Seric { 5509536Seric extern char **copyplist(); 5519536Seric 5529536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 5539536Seric pvp++; 5549536Seric if (*pvp != NULL) 5559536Seric CurEnv->e_fromdomain = copyplist(pvp, TRUE); 5569536Seric } 5579536Seric } 5589536Seric /* 5599536Seric ** TRUSTEDUSER -- tell us if this user is to be trusted. 5609536Seric ** 5619536Seric ** Parameters: 5629536Seric ** user -- the user to be checked. 5639536Seric ** 5649536Seric ** Returns: 5659536Seric ** TRUE if the user is in an approved list. 5669536Seric ** FALSE otherwise. 5679536Seric ** 5689536Seric ** Side Effects: 5699536Seric ** none. 5709536Seric */ 5719536Seric 5729536Seric bool 5739536Seric trusteduser(user) 5749536Seric char *user; 5759536Seric { 5769536Seric register char **ulist; 5779536Seric extern char *TrustedUsers[]; 5789536Seric 5799536Seric for (ulist = TrustedUsers; *ulist != NULL; ulist++) 5809536Seric if (strcmp(*ulist, user) == 0) 5819536Seric return (TRUE); 5829536Seric return (FALSE); 5839536Seric } 584