122704Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 333729Sbostic * Copyright (c) 1988 Regents of the University of California. 433729Sbostic * All rights reserved. 533729Sbostic * 633729Sbostic * Redistribution and use in source and binary forms are permitted 734921Sbostic * provided that the above copyright notice and this paragraph are 834921Sbostic * duplicated in all such forms and that any documentation, 934921Sbostic * advertising materials, and other materials related to such 1034921Sbostic * distribution and use acknowledge that the software was developed 1134921Sbostic * by the University of California, Berkeley. The name of the 1234921Sbostic * University may not be used to endorse or promote products derived 1334921Sbostic * from this software without specific prior written permission. 1434921Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1534921Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1634921Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1733729Sbostic */ 1822704Sdist 1922704Sdist #ifndef lint 20*36928Sbostic static char sccsid[] = "@(#)envelope.c 5.19 (Berkeley) 02/27/89"; 2133729Sbostic #endif /* not lint */ 2222704Sdist 23*36928Sbostic #include <sys/types.h> 24*36928Sbostic #include <sys/time.h> 25*36928Sbostic #include <sys/stat.h> 269536Seric #include <pwd.h> 279536Seric #include "sendmail.h" 289536Seric 299536Seric /* 309536Seric ** NEWENVELOPE -- allocate a new envelope 319536Seric ** 329536Seric ** Supports inheritance. 339536Seric ** 349536Seric ** Parameters: 359536Seric ** e -- the new envelope to fill in. 369536Seric ** 379536Seric ** Returns: 389536Seric ** e. 399536Seric ** 409536Seric ** Side Effects: 419536Seric ** none. 429536Seric */ 439536Seric 449536Seric ENVELOPE * 459536Seric newenvelope(e) 469536Seric register ENVELOPE *e; 479536Seric { 489536Seric register ENVELOPE *parent; 499536Seric extern putheader(), putbody(); 5025611Seric extern ENVELOPE BlankEnvelope; 519536Seric 529536Seric parent = CurEnv; 539536Seric if (e == CurEnv) 549536Seric parent = e->e_parent; 5525611Seric clearenvelope(e, TRUE); 5624944Seric if (e == CurEnv) 5724944Seric bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from); 5824944Seric else 5924944Seric bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from); 609536Seric e->e_parent = parent; 619536Seric e->e_ctime = curtime(); 6225014Seric e->e_msgpriority = parent->e_msgsize; 639536Seric e->e_puthdr = putheader; 649536Seric e->e_putbody = putbody; 659536Seric if (CurEnv->e_xfp != NULL) 669536Seric (void) fflush(CurEnv->e_xfp); 679536Seric 689536Seric return (e); 699536Seric } 709536Seric /* 719536Seric ** DROPENVELOPE -- deallocate an envelope. 729536Seric ** 739536Seric ** Parameters: 749536Seric ** e -- the envelope to deallocate. 759536Seric ** 769536Seric ** Returns: 779536Seric ** none. 789536Seric ** 799536Seric ** Side Effects: 809536Seric ** housekeeping necessary to dispose of an envelope. 819536Seric ** Unlocks this queue file. 829536Seric */ 839536Seric 849536Seric dropenvelope(e) 859536Seric register ENVELOPE *e; 869536Seric { 879536Seric bool queueit = FALSE; 889536Seric register ADDRESS *q; 899536Seric 909536Seric if (tTd(50, 1)) 919536Seric { 929536Seric printf("dropenvelope %x id=", e); 939536Seric xputs(e->e_id); 949536Seric printf(" flags=%o\n", e->e_flags); 959536Seric } 969536Seric #ifdef LOG 979536Seric if (LogLevel > 10) 989536Seric syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d", 999536Seric e->e_id == NULL ? "(none)" : e->e_id, 1009536Seric e->e_flags, getpid()); 1019536Seric #endif LOG 1029536Seric 1039536Seric /* we must have an id to remove disk files */ 1049536Seric if (e->e_id == NULL) 1059536Seric return; 1069536Seric 1079536Seric /* 1089536Seric ** Extract state information from dregs of send list. 1099536Seric */ 1109536Seric 1119536Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1129536Seric { 1139536Seric if (bitset(QQUEUEUP, q->q_flags)) 1149536Seric queueit = TRUE; 1159536Seric } 1169536Seric 1179536Seric /* 1189536Seric ** Send back return receipts as requested. 1199536Seric */ 1209536Seric 1219536Seric if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 1229536Seric { 12310844Seric auto ADDRESS *rlist = NULL; 1249536Seric 1259621Seric sendtolist(CurEnv->e_receiptto, (ADDRESS *) NULL, &rlist); 1269536Seric (void) returntosender("Return receipt", rlist, FALSE); 1279536Seric } 1289536Seric 1299536Seric /* 1309536Seric ** Arrange to send error messages if there are fatal errors. 1319536Seric */ 1329536Seric 13310754Seric if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && ErrorMode != EM_QUIET) 1349536Seric savemail(e); 1359536Seric 1369536Seric /* 1379536Seric ** Instantiate or deinstantiate the queue. 1389536Seric */ 1399536Seric 1409536Seric if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 1419536Seric bitset(EF_CLRQUEUE, e->e_flags)) 1429536Seric { 14323497Seric if (e->e_df != NULL) 14423497Seric xunlink(e->e_df); 1459536Seric xunlink(queuename(e, 'q')); 1469536Seric } 1479536Seric else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 14810754Seric { 14910754Seric #ifdef QUEUE 1509536Seric queueup(e, FALSE, FALSE); 15110754Seric #else QUEUE 15210754Seric syserr("dropenvelope: queueup"); 15310754Seric #endif QUEUE 15410754Seric } 1559536Seric 1569536Seric /* now unlock the job */ 15710196Seric closexscript(e); 1589536Seric unlockqueue(e); 1599536Seric 1609536Seric /* make sure that this envelope is marked unused */ 1619536Seric e->e_id = e->e_df = NULL; 16224944Seric if (e->e_dfp != NULL) 16324944Seric (void) fclose(e->e_dfp); 16410196Seric e->e_dfp = NULL; 1659536Seric } 1669536Seric /* 1679536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 1689536Seric ** 1699536Seric ** This is normally used by a child process to get a clean 1709536Seric ** envelope without disturbing the parent. 1719536Seric ** 1729536Seric ** Parameters: 1739536Seric ** e -- the envelope to clear. 17425611Seric ** fullclear - if set, the current envelope is total 17525611Seric ** garbage and should be ignored; otherwise, 17625611Seric ** release any resources it may indicate. 1779536Seric ** 1789536Seric ** Returns: 1799536Seric ** none. 1809536Seric ** 1819536Seric ** Side Effects: 1829536Seric ** Closes files associated with the envelope. 1839536Seric ** Marks the envelope as unallocated. 1849536Seric */ 1859536Seric 18625611Seric clearenvelope(e, fullclear) 1879536Seric register ENVELOPE *e; 18825611Seric bool fullclear; 1899536Seric { 19025514Seric register HDR *bh; 19125514Seric register HDR **nhp; 19225514Seric extern ENVELOPE BlankEnvelope; 19325514Seric 19425611Seric if (!fullclear) 19525611Seric { 19625611Seric /* clear out any file information */ 19725611Seric if (e->e_xfp != NULL) 19825611Seric (void) fclose(e->e_xfp); 19925611Seric if (e->e_dfp != NULL) 20025611Seric (void) fclose(e->e_dfp); 20125611Seric } 2029536Seric 20324961Seric /* now clear out the data */ 20424965Seric STRUCTCOPY(BlankEnvelope, *e); 20525514Seric bh = BlankEnvelope.e_header; 20625514Seric nhp = &e->e_header; 20725514Seric while (bh != NULL) 20825514Seric { 20925514Seric *nhp = (HDR *) xalloc(sizeof *bh); 21025514Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 21125514Seric bh = bh->h_link; 21225514Seric nhp = &(*nhp)->h_link; 21325514Seric } 2149536Seric } 2159536Seric /* 2169536Seric ** INITSYS -- initialize instantiation of system 2179536Seric ** 2189536Seric ** In Daemon mode, this is done in the child. 2199536Seric ** 2209536Seric ** Parameters: 2219536Seric ** none. 2229536Seric ** 2239536Seric ** Returns: 2249536Seric ** none. 2259536Seric ** 2269536Seric ** Side Effects: 2279536Seric ** Initializes the system macros, some global variables, 2289536Seric ** etc. In particular, the current time in various 2299536Seric ** forms is set. 2309536Seric */ 2319536Seric 2329536Seric initsys() 2339536Seric { 2349536Seric static char cbuf[5]; /* holds hop count */ 2359536Seric static char pbuf[10]; /* holds pid */ 23622963Smiriam #ifdef TTYNAME 2379536Seric static char ybuf[10]; /* holds tty id */ 2389536Seric register char *p; 23922963Smiriam #endif TTYNAME 2409536Seric extern char *ttyname(); 2419536Seric extern char *macvalue(); 2429536Seric extern char Version[]; 2439536Seric 2449536Seric /* 2459536Seric ** Give this envelope a reality. 2469536Seric ** I.e., an id, a transcript, and a creation time. 2479536Seric */ 2489536Seric 2499536Seric openxscript(CurEnv); 2509536Seric CurEnv->e_ctime = curtime(); 2519536Seric 2529536Seric /* 2539536Seric ** Set OutChannel to something useful if stdout isn't it. 2549536Seric ** This arranges that any extra stuff the mailer produces 2559536Seric ** gets sent back to the user on error (because it is 2569536Seric ** tucked away in the transcript). 2579536Seric */ 2589536Seric 2599536Seric if (OpMode == MD_DAEMON && QueueRun) 2609536Seric OutChannel = CurEnv->e_xfp; 2619536Seric 2629536Seric /* 2639536Seric ** Set up some basic system macros. 2649536Seric */ 2659536Seric 2669536Seric /* process id */ 2679536Seric (void) sprintf(pbuf, "%d", getpid()); 2689536Seric define('p', pbuf, CurEnv); 2699536Seric 2709536Seric /* hop count */ 2719536Seric (void) sprintf(cbuf, "%d", CurEnv->e_hopcount); 2729536Seric define('c', cbuf, CurEnv); 2739536Seric 2749536Seric /* time as integer, unix time, arpa time */ 27511932Seric settime(); 2769536Seric 27717472Seric #ifdef TTYNAME 2789536Seric /* tty name */ 2799536Seric if (macvalue('y', CurEnv) == NULL) 2809536Seric { 2819536Seric p = ttyname(2); 2829536Seric if (p != NULL) 2839536Seric { 2849536Seric if (rindex(p, '/') != NULL) 2859536Seric p = rindex(p, '/') + 1; 2869536Seric (void) strcpy(ybuf, p); 2879536Seric define('y', ybuf, CurEnv); 2889536Seric } 2899536Seric } 29017472Seric #endif TTYNAME 2919536Seric } 2929536Seric /* 29311932Seric ** SETTIME -- set the current time. 29411932Seric ** 29511932Seric ** Parameters: 29611932Seric ** none. 29711932Seric ** 29811932Seric ** Returns: 29911932Seric ** none. 30011932Seric ** 30111932Seric ** Side Effects: 30211932Seric ** Sets the various time macros -- $a, $b, $d, $t. 30311932Seric */ 30411932Seric 30511932Seric settime() 30611932Seric { 30711932Seric register char *p; 30811932Seric auto time_t now; 30911932Seric static char tbuf[20]; /* holds "current" time */ 31011932Seric static char dbuf[30]; /* holds ctime(tbuf) */ 31111932Seric register struct tm *tm; 31211932Seric extern char *arpadate(); 31311932Seric extern struct tm *gmtime(); 31411932Seric extern char *macvalue(); 31511932Seric 31611932Seric now = curtime(); 31711932Seric tm = gmtime(&now); 31811932Seric (void) sprintf(tbuf, "%02d%02d%02d%02d%02d", tm->tm_year, tm->tm_mon+1, 31911932Seric tm->tm_mday, tm->tm_hour, tm->tm_min); 32011932Seric define('t', tbuf, CurEnv); 32111932Seric (void) strcpy(dbuf, ctime(&now)); 32211932Seric *index(dbuf, '\n') = '\0'; 32311932Seric if (macvalue('d', CurEnv) == NULL) 32411932Seric define('d', dbuf, CurEnv); 32511932Seric p = newstr(arpadate(dbuf)); 32611932Seric if (macvalue('a', CurEnv) == NULL) 32711932Seric define('a', p, CurEnv); 32811932Seric define('b', p, CurEnv); 32911932Seric } 33011932Seric /* 3319536Seric ** OPENXSCRIPT -- Open transcript file 3329536Seric ** 3339536Seric ** Creates a transcript file for possible eventual mailing or 3349536Seric ** sending back. 3359536Seric ** 3369536Seric ** Parameters: 3379536Seric ** e -- the envelope to create the transcript in/for. 3389536Seric ** 3399536Seric ** Returns: 3409536Seric ** none 3419536Seric ** 3429536Seric ** Side Effects: 3439536Seric ** Creates the transcript file. 3449536Seric */ 3459536Seric 3469536Seric openxscript(e) 3479536Seric register ENVELOPE *e; 3489536Seric { 3499536Seric register char *p; 3509536Seric 35110196Seric # ifdef LOG 35210196Seric if (LogLevel > 19) 35310196Seric syslog(LOG_DEBUG, "%s: openx%s", e->e_id, e->e_xfp == NULL ? "" : " (no)"); 35410196Seric # endif LOG 3559536Seric if (e->e_xfp != NULL) 3569536Seric return; 3579536Seric p = queuename(e, 'x'); 3589536Seric e->e_xfp = fopen(p, "w"); 3599536Seric if (e->e_xfp == NULL) 3609536Seric syserr("Can't create %s", p); 3619536Seric else 3629536Seric (void) chmod(p, 0644); 3639536Seric } 3649536Seric /* 36510196Seric ** CLOSEXSCRIPT -- close the transcript file. 36610196Seric ** 36710196Seric ** Parameters: 36810196Seric ** e -- the envelope containing the transcript to close. 36910196Seric ** 37010196Seric ** Returns: 37110196Seric ** none. 37210196Seric ** 37310196Seric ** Side Effects: 37410196Seric ** none. 37510196Seric */ 37610196Seric 37710196Seric closexscript(e) 37810196Seric register ENVELOPE *e; 37910196Seric { 38010196Seric if (e->e_xfp == NULL) 38110196Seric return; 38210196Seric (void) fclose(e->e_xfp); 38310196Seric e->e_xfp = NULL; 38410196Seric } 38510196Seric /* 3869536Seric ** SETSENDER -- set the person who this message is from 3879536Seric ** 3889536Seric ** Under certain circumstances allow the user to say who 3899536Seric ** s/he is (using -f or -r). These are: 3909536Seric ** 1. The user's uid is zero (root). 3919536Seric ** 2. The user's login name is in an approved list (typically 3929536Seric ** from a network server). 3939536Seric ** 3. The address the user is trying to claim has a 3949536Seric ** "!" character in it (since #2 doesn't do it for 3959536Seric ** us if we are dialing out for UUCP). 3969536Seric ** A better check to replace #3 would be if the 3979536Seric ** effective uid is "UUCP" -- this would require me 3989536Seric ** to rewrite getpwent to "grab" uucp as it went by, 3999536Seric ** make getname more nasty, do another passwd file 4009536Seric ** scan, or compile the UID of "UUCP" into the code, 4019536Seric ** all of which are reprehensible. 4029536Seric ** 4039536Seric ** Assuming all of these fail, we figure out something 4049536Seric ** ourselves. 4059536Seric ** 4069536Seric ** Parameters: 4079536Seric ** from -- the person we would like to believe this message 4089536Seric ** is from, as specified on the command line. 4099536Seric ** 4109536Seric ** Returns: 4119536Seric ** none. 4129536Seric ** 4139536Seric ** Side Effects: 4149536Seric ** sets sendmail's notion of who the from person is. 4159536Seric */ 4169536Seric 4179536Seric setsender(from) 4189536Seric char *from; 4199536Seric { 4209536Seric register char **pvp; 4219536Seric char *realname = NULL; 42218665Seric register struct passwd *pw; 4239536Seric char buf[MAXNAME]; 42416913Seric char pvpbuf[PSBUFSIZE]; 42518665Seric extern struct passwd *getpwnam(); 4269536Seric extern char *macvalue(); 4279536Seric extern char **prescan(); 4289536Seric extern bool safefile(); 4299536Seric extern char *FullName; 4309536Seric 4319536Seric if (tTd(45, 1)) 43214786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 4339536Seric 4349536Seric /* 4359536Seric ** Figure out the real user executing us. 4369536Seric ** Username can return errno != 0 on non-errors. 4379536Seric */ 4389536Seric 4399536Seric if (QueueRun || OpMode == MD_SMTP || OpMode == MD_ARPAFTP) 4409536Seric realname = from; 4419536Seric if (realname == NULL || realname[0] == '\0') 4429536Seric { 4439536Seric extern char *username(); 4449536Seric 4459536Seric realname = username(); 4469536Seric } 4479536Seric 4489536Seric /* 4499536Seric ** Determine if this real person is allowed to alias themselves. 4509536Seric */ 4519536Seric 4529536Seric if (from != NULL) 4539536Seric { 4549536Seric extern bool trusteduser(); 4559536Seric 45636230Skarels if (!trusteduser(realname) && getuid() != geteuid() && 4579536Seric index(from, '!') == NULL && getuid() != 0) 4589536Seric { 4599536Seric /* network sends -r regardless (why why why?) */ 4609536Seric /* syserr("%s, you cannot use the -f flag", realname); */ 4619536Seric from = NULL; 4629536Seric } 4639536Seric } 4649536Seric 4659536Seric SuprErrs = TRUE; 46611447Seric if (from == NULL || parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL) 4679536Seric { 46821750Seric /* log garbage addresses for traceback */ 46921750Seric if (from != NULL) 47021750Seric { 47124944Seric # ifdef LOG 47224944Seric if (LogLevel >= 1) 47336230Skarels if (realname == from && RealHostName != NULL) 47436230Skarels syslog(LOG_NOTICE, 47536230Skarels "from=%s unparseable, received from %s", 47636230Skarels from, RealHostName); 47736230Skarels else 47836230Skarels syslog(LOG_NOTICE, 47936230Skarels "Unparseable username %s wants from=%s", 48036230Skarels realname, from); 48124944Seric # endif LOG 48221750Seric } 4839536Seric from = newstr(realname); 48424944Seric if (parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL && 48524944Seric parseaddr("postmaster", &CurEnv->e_from, 1, '\0') == NULL) 48624944Seric { 48724944Seric syserr("setsender: can't even parse postmaster!"); 48824944Seric } 4899536Seric } 4909536Seric else 4919536Seric FromFlag = TRUE; 4929536Seric CurEnv->e_from.q_flags |= QDONTSEND; 49316162Seric loweraddr(&CurEnv->e_from); 4949536Seric SuprErrs = FALSE; 4959536Seric 49618665Seric if (CurEnv->e_from.q_mailer == LocalMailer && 49718665Seric (pw = getpwnam(CurEnv->e_from.q_user)) != NULL) 4989536Seric { 49917472Seric /* 50017472Seric ** Process passwd file entry. 50117472Seric */ 50217472Seric 5039536Seric 5049536Seric /* extract home directory */ 5059536Seric CurEnv->e_from.q_home = newstr(pw->pw_dir); 50616481Seric define('z', CurEnv->e_from.q_home, CurEnv); 5079536Seric 50811625Seric /* extract user and group id */ 50911625Seric CurEnv->e_from.q_uid = pw->pw_uid; 51011625Seric CurEnv->e_from.q_gid = pw->pw_gid; 51111625Seric 5129536Seric /* if the user has given fullname already, don't redefine */ 5139536Seric if (FullName == NULL) 5149536Seric FullName = macvalue('x', CurEnv); 51511932Seric if (FullName != NULL && FullName[0] == '\0') 5169536Seric FullName = NULL; 5179536Seric 5189536Seric /* extract full name from passwd file */ 5199582Seric if (FullName == NULL && pw->pw_gecos != NULL && 5209582Seric strcmp(pw->pw_name, CurEnv->e_from.q_user) == 0) 5219536Seric { 5229536Seric buildfname(pw->pw_gecos, CurEnv->e_from.q_user, buf); 5239536Seric if (buf[0] != '\0') 5249536Seric FullName = newstr(buf); 5259536Seric } 5269536Seric if (FullName != NULL) 5279536Seric define('x', FullName, CurEnv); 5289536Seric } 52911625Seric else 53011625Seric { 53111625Seric if (CurEnv->e_from.q_home == NULL) 53211625Seric CurEnv->e_from.q_home = getenv("HOME"); 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 { 55136233Skarels # ifdef LOG 55236233Skarels if (LogLevel >= 1) 55336233Skarels syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 55436233Skarels # endif 55536230Skarels usrerr("cannot prescan from (%s)", from); 5569536Seric finis(); 5579536Seric } 5589536Seric rewrite(pvp, 3); 5599536Seric rewrite(pvp, 1); 56025032Seric rewrite(pvp, 4); 5619536Seric cataddr(pvp, buf, sizeof buf); 5629536Seric define('f', newstr(buf), CurEnv); 5639536Seric 5649536Seric /* save the domain spec if this mailer wants it */ 56524944Seric if (CurEnv->e_from.q_mailer != NULL && 56624944Seric bitnset(M_CANONICAL, CurEnv->e_from.q_mailer->m_flags)) 5679536Seric { 5689536Seric extern char **copyplist(); 5699536Seric 5709536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 5719536Seric pvp++; 5729536Seric if (*pvp != NULL) 5739536Seric CurEnv->e_fromdomain = copyplist(pvp, TRUE); 5749536Seric } 5759536Seric } 5769536Seric /* 5779536Seric ** TRUSTEDUSER -- tell us if this user is to be trusted. 5789536Seric ** 5799536Seric ** Parameters: 5809536Seric ** user -- the user to be checked. 5819536Seric ** 5829536Seric ** Returns: 5839536Seric ** TRUE if the user is in an approved list. 5849536Seric ** FALSE otherwise. 5859536Seric ** 5869536Seric ** Side Effects: 5879536Seric ** none. 5889536Seric */ 5899536Seric 5909536Seric bool 5919536Seric trusteduser(user) 5929536Seric char *user; 5939536Seric { 5949536Seric register char **ulist; 5959536Seric extern char *TrustedUsers[]; 5969536Seric 5979536Seric for (ulist = TrustedUsers; *ulist != NULL; ulist++) 5989536Seric if (strcmp(*ulist, user) == 0) 5999536Seric return (TRUE); 6009536Seric return (FALSE); 6019536Seric } 602