122704Sdist /* 2*34921Sbostic * 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 7*34921Sbostic * provided that the above copyright notice and this paragraph are 8*34921Sbostic * duplicated in all such forms and that any documentation, 9*34921Sbostic * advertising materials, and other materials related to such 10*34921Sbostic * distribution and use acknowledge that the software was developed 11*34921Sbostic * by the University of California, Berkeley. The name of the 12*34921Sbostic * University may not be used to endorse or promote products derived 13*34921Sbostic * from this software without specific prior written permission. 14*34921Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 15*34921Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 16*34921Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1733729Sbostic */ 1822704Sdist 1922704Sdist #ifndef lint 20*34921Sbostic static char sccsid[] = "@(#)envelope.c 5.15 (Berkeley) 06/30/88"; 2133729Sbostic #endif /* not lint */ 2222704Sdist 239536Seric #include <pwd.h> 2413587Swnj #include <sys/time.h> 259536Seric #include "sendmail.h" 269536Seric #include <sys/stat.h> 279536Seric 289536Seric /* 299536Seric ** NEWENVELOPE -- allocate a new envelope 309536Seric ** 319536Seric ** Supports inheritance. 329536Seric ** 339536Seric ** Parameters: 349536Seric ** e -- the new envelope to fill in. 359536Seric ** 369536Seric ** Returns: 379536Seric ** e. 389536Seric ** 399536Seric ** Side Effects: 409536Seric ** none. 419536Seric */ 429536Seric 439536Seric ENVELOPE * 449536Seric newenvelope(e) 459536Seric register ENVELOPE *e; 469536Seric { 479536Seric register ENVELOPE *parent; 489536Seric extern putheader(), putbody(); 4925611Seric extern ENVELOPE BlankEnvelope; 509536Seric 519536Seric parent = CurEnv; 529536Seric if (e == CurEnv) 539536Seric parent = e->e_parent; 5425611Seric clearenvelope(e, TRUE); 5524944Seric if (e == CurEnv) 5624944Seric bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from); 5724944Seric else 5824944Seric bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from); 599536Seric e->e_parent = parent; 609536Seric e->e_ctime = curtime(); 6125014Seric e->e_msgpriority = parent->e_msgsize; 629536Seric e->e_puthdr = putheader; 639536Seric e->e_putbody = putbody; 649536Seric if (CurEnv->e_xfp != NULL) 659536Seric (void) fflush(CurEnv->e_xfp); 669536Seric 679536Seric return (e); 689536Seric } 699536Seric /* 709536Seric ** DROPENVELOPE -- deallocate an envelope. 719536Seric ** 729536Seric ** Parameters: 739536Seric ** e -- the envelope to deallocate. 749536Seric ** 759536Seric ** Returns: 769536Seric ** none. 779536Seric ** 789536Seric ** Side Effects: 799536Seric ** housekeeping necessary to dispose of an envelope. 809536Seric ** Unlocks this queue file. 819536Seric */ 829536Seric 839536Seric dropenvelope(e) 849536Seric register ENVELOPE *e; 859536Seric { 869536Seric bool queueit = FALSE; 879536Seric register ADDRESS *q; 889536Seric 899536Seric #ifdef DEBUG 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 #endif DEBUG 979536Seric #ifdef LOG 989536Seric if (LogLevel > 10) 999536Seric syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d", 1009536Seric e->e_id == NULL ? "(none)" : e->e_id, 1019536Seric e->e_flags, getpid()); 1029536Seric #endif LOG 1039536Seric 1049536Seric /* we must have an id to remove disk files */ 1059536Seric if (e->e_id == NULL) 1069536Seric return; 1079536Seric 1089536Seric /* 1099536Seric ** Extract state information from dregs of send list. 1109536Seric */ 1119536Seric 1129536Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1139536Seric { 1149536Seric if (bitset(QQUEUEUP, q->q_flags)) 1159536Seric queueit = TRUE; 1169536Seric } 1179536Seric 1189536Seric /* 1199536Seric ** Send back return receipts as requested. 1209536Seric */ 1219536Seric 1229536Seric if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 1239536Seric { 12410844Seric auto ADDRESS *rlist = NULL; 1259536Seric 1269621Seric sendtolist(CurEnv->e_receiptto, (ADDRESS *) NULL, &rlist); 1279536Seric (void) returntosender("Return receipt", rlist, FALSE); 1289536Seric } 1299536Seric 1309536Seric /* 1319536Seric ** Arrange to send error messages if there are fatal errors. 1329536Seric */ 1339536Seric 13410754Seric if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && ErrorMode != EM_QUIET) 1359536Seric savemail(e); 1369536Seric 1379536Seric /* 1389536Seric ** Instantiate or deinstantiate the queue. 1399536Seric */ 1409536Seric 1419536Seric if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 1429536Seric bitset(EF_CLRQUEUE, e->e_flags)) 1439536Seric { 14423497Seric if (e->e_df != NULL) 14523497Seric xunlink(e->e_df); 1469536Seric xunlink(queuename(e, 'q')); 1479536Seric } 1489536Seric else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 14910754Seric { 15010754Seric #ifdef QUEUE 1519536Seric queueup(e, FALSE, FALSE); 15210754Seric #else QUEUE 15310754Seric syserr("dropenvelope: queueup"); 15410754Seric #endif QUEUE 15510754Seric } 1569536Seric 1579536Seric /* now unlock the job */ 15810196Seric closexscript(e); 1599536Seric unlockqueue(e); 1609536Seric 1619536Seric /* make sure that this envelope is marked unused */ 1629536Seric e->e_id = e->e_df = NULL; 16324944Seric if (e->e_dfp != NULL) 16424944Seric (void) fclose(e->e_dfp); 16510196Seric e->e_dfp = NULL; 1669536Seric } 1679536Seric /* 1689536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 1699536Seric ** 1709536Seric ** This is normally used by a child process to get a clean 1719536Seric ** envelope without disturbing the parent. 1729536Seric ** 1739536Seric ** Parameters: 1749536Seric ** e -- the envelope to clear. 17525611Seric ** fullclear - if set, the current envelope is total 17625611Seric ** garbage and should be ignored; otherwise, 17725611Seric ** release any resources it may indicate. 1789536Seric ** 1799536Seric ** Returns: 1809536Seric ** none. 1819536Seric ** 1829536Seric ** Side Effects: 1839536Seric ** Closes files associated with the envelope. 1849536Seric ** Marks the envelope as unallocated. 1859536Seric */ 1869536Seric 18725611Seric clearenvelope(e, fullclear) 1889536Seric register ENVELOPE *e; 18925611Seric bool fullclear; 1909536Seric { 19125514Seric register HDR *bh; 19225514Seric register HDR **nhp; 19325514Seric extern ENVELOPE BlankEnvelope; 19425514Seric 19525611Seric if (!fullclear) 19625611Seric { 19725611Seric /* clear out any file information */ 19825611Seric if (e->e_xfp != NULL) 19925611Seric (void) fclose(e->e_xfp); 20025611Seric if (e->e_dfp != NULL) 20125611Seric (void) fclose(e->e_dfp); 20225611Seric } 2039536Seric 20424961Seric /* now clear out the data */ 20524965Seric STRUCTCOPY(BlankEnvelope, *e); 20625514Seric bh = BlankEnvelope.e_header; 20725514Seric nhp = &e->e_header; 20825514Seric while (bh != NULL) 20925514Seric { 21025514Seric *nhp = (HDR *) xalloc(sizeof *bh); 21125514Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 21225514Seric bh = bh->h_link; 21325514Seric nhp = &(*nhp)->h_link; 21425514Seric } 2159536Seric } 2169536Seric /* 2179536Seric ** INITSYS -- initialize instantiation of system 2189536Seric ** 2199536Seric ** In Daemon mode, this is done in the child. 2209536Seric ** 2219536Seric ** Parameters: 2229536Seric ** none. 2239536Seric ** 2249536Seric ** Returns: 2259536Seric ** none. 2269536Seric ** 2279536Seric ** Side Effects: 2289536Seric ** Initializes the system macros, some global variables, 2299536Seric ** etc. In particular, the current time in various 2309536Seric ** forms is set. 2319536Seric */ 2329536Seric 2339536Seric initsys() 2349536Seric { 2359536Seric static char cbuf[5]; /* holds hop count */ 2369536Seric static char pbuf[10]; /* holds pid */ 23722963Smiriam #ifdef TTYNAME 2389536Seric static char ybuf[10]; /* holds tty id */ 2399536Seric register char *p; 24022963Smiriam #endif TTYNAME 2419536Seric extern char *ttyname(); 2429536Seric extern char *macvalue(); 2439536Seric extern char Version[]; 2449536Seric 2459536Seric /* 2469536Seric ** Give this envelope a reality. 2479536Seric ** I.e., an id, a transcript, and a creation time. 2489536Seric */ 2499536Seric 2509536Seric openxscript(CurEnv); 2519536Seric CurEnv->e_ctime = curtime(); 2529536Seric 2539536Seric /* 2549536Seric ** Set OutChannel to something useful if stdout isn't it. 2559536Seric ** This arranges that any extra stuff the mailer produces 2569536Seric ** gets sent back to the user on error (because it is 2579536Seric ** tucked away in the transcript). 2589536Seric */ 2599536Seric 2609536Seric if (OpMode == MD_DAEMON && QueueRun) 2619536Seric OutChannel = CurEnv->e_xfp; 2629536Seric 2639536Seric /* 2649536Seric ** Set up some basic system macros. 2659536Seric */ 2669536Seric 2679536Seric /* process id */ 2689536Seric (void) sprintf(pbuf, "%d", getpid()); 2699536Seric define('p', pbuf, CurEnv); 2709536Seric 2719536Seric /* hop count */ 2729536Seric (void) sprintf(cbuf, "%d", CurEnv->e_hopcount); 2739536Seric define('c', cbuf, CurEnv); 2749536Seric 2759536Seric /* time as integer, unix time, arpa time */ 27611932Seric settime(); 2779536Seric 27817472Seric #ifdef TTYNAME 2799536Seric /* tty name */ 2809536Seric if (macvalue('y', CurEnv) == NULL) 2819536Seric { 2829536Seric p = ttyname(2); 2839536Seric if (p != NULL) 2849536Seric { 2859536Seric if (rindex(p, '/') != NULL) 2869536Seric p = rindex(p, '/') + 1; 2879536Seric (void) strcpy(ybuf, p); 2889536Seric define('y', ybuf, CurEnv); 2899536Seric } 2909536Seric } 29117472Seric #endif TTYNAME 2929536Seric } 2939536Seric /* 29411932Seric ** SETTIME -- set the current time. 29511932Seric ** 29611932Seric ** Parameters: 29711932Seric ** none. 29811932Seric ** 29911932Seric ** Returns: 30011932Seric ** none. 30111932Seric ** 30211932Seric ** Side Effects: 30311932Seric ** Sets the various time macros -- $a, $b, $d, $t. 30411932Seric */ 30511932Seric 30611932Seric settime() 30711932Seric { 30811932Seric register char *p; 30911932Seric auto time_t now; 31011932Seric static char tbuf[20]; /* holds "current" time */ 31111932Seric static char dbuf[30]; /* holds ctime(tbuf) */ 31211932Seric register struct tm *tm; 31311932Seric extern char *arpadate(); 31411932Seric extern struct tm *gmtime(); 31511932Seric extern char *macvalue(); 31611932Seric 31711932Seric now = curtime(); 31811932Seric tm = gmtime(&now); 31911932Seric (void) sprintf(tbuf, "%02d%02d%02d%02d%02d", tm->tm_year, tm->tm_mon+1, 32011932Seric tm->tm_mday, tm->tm_hour, tm->tm_min); 32111932Seric define('t', tbuf, CurEnv); 32211932Seric (void) strcpy(dbuf, ctime(&now)); 32311932Seric *index(dbuf, '\n') = '\0'; 32411932Seric if (macvalue('d', CurEnv) == NULL) 32511932Seric define('d', dbuf, CurEnv); 32611932Seric p = newstr(arpadate(dbuf)); 32711932Seric if (macvalue('a', CurEnv) == NULL) 32811932Seric define('a', p, CurEnv); 32911932Seric define('b', p, CurEnv); 33011932Seric } 33111932Seric /* 3329536Seric ** OPENXSCRIPT -- Open transcript file 3339536Seric ** 3349536Seric ** Creates a transcript file for possible eventual mailing or 3359536Seric ** sending back. 3369536Seric ** 3379536Seric ** Parameters: 3389536Seric ** e -- the envelope to create the transcript in/for. 3399536Seric ** 3409536Seric ** Returns: 3419536Seric ** none 3429536Seric ** 3439536Seric ** Side Effects: 3449536Seric ** Creates the transcript file. 3459536Seric */ 3469536Seric 3479536Seric openxscript(e) 3489536Seric register ENVELOPE *e; 3499536Seric { 3509536Seric register char *p; 3519536Seric 35210196Seric # ifdef LOG 35310196Seric if (LogLevel > 19) 35410196Seric syslog(LOG_DEBUG, "%s: openx%s", e->e_id, e->e_xfp == NULL ? "" : " (no)"); 35510196Seric # endif LOG 3569536Seric if (e->e_xfp != NULL) 3579536Seric return; 3589536Seric p = queuename(e, 'x'); 3599536Seric e->e_xfp = fopen(p, "w"); 3609536Seric if (e->e_xfp == NULL) 3619536Seric syserr("Can't create %s", p); 3629536Seric else 3639536Seric (void) chmod(p, 0644); 3649536Seric } 3659536Seric /* 36610196Seric ** CLOSEXSCRIPT -- close the transcript file. 36710196Seric ** 36810196Seric ** Parameters: 36910196Seric ** e -- the envelope containing the transcript to close. 37010196Seric ** 37110196Seric ** Returns: 37210196Seric ** none. 37310196Seric ** 37410196Seric ** Side Effects: 37510196Seric ** none. 37610196Seric */ 37710196Seric 37810196Seric closexscript(e) 37910196Seric register ENVELOPE *e; 38010196Seric { 38110196Seric if (e->e_xfp == NULL) 38210196Seric return; 38310196Seric (void) fclose(e->e_xfp); 38410196Seric e->e_xfp = NULL; 38510196Seric } 38610196Seric /* 3879536Seric ** SETSENDER -- set the person who this message is from 3889536Seric ** 3899536Seric ** Under certain circumstances allow the user to say who 3909536Seric ** s/he is (using -f or -r). These are: 3919536Seric ** 1. The user's uid is zero (root). 3929536Seric ** 2. The user's login name is in an approved list (typically 3939536Seric ** from a network server). 3949536Seric ** 3. The address the user is trying to claim has a 3959536Seric ** "!" character in it (since #2 doesn't do it for 3969536Seric ** us if we are dialing out for UUCP). 3979536Seric ** A better check to replace #3 would be if the 3989536Seric ** effective uid is "UUCP" -- this would require me 3999536Seric ** to rewrite getpwent to "grab" uucp as it went by, 4009536Seric ** make getname more nasty, do another passwd file 4019536Seric ** scan, or compile the UID of "UUCP" into the code, 4029536Seric ** all of which are reprehensible. 4039536Seric ** 4049536Seric ** Assuming all of these fail, we figure out something 4059536Seric ** ourselves. 4069536Seric ** 4079536Seric ** Parameters: 4089536Seric ** from -- the person we would like to believe this message 4099536Seric ** is from, as specified on the command line. 4109536Seric ** 4119536Seric ** Returns: 4129536Seric ** none. 4139536Seric ** 4149536Seric ** Side Effects: 4159536Seric ** sets sendmail's notion of who the from person is. 4169536Seric */ 4179536Seric 4189536Seric setsender(from) 4199536Seric char *from; 4209536Seric { 4219536Seric register char **pvp; 4229536Seric char *realname = NULL; 42318665Seric register struct passwd *pw; 4249536Seric char buf[MAXNAME]; 42516913Seric char pvpbuf[PSBUFSIZE]; 42618665Seric extern struct passwd *getpwnam(); 4279536Seric extern char *macvalue(); 4289536Seric extern char **prescan(); 4299536Seric extern bool safefile(); 4309536Seric extern char *FullName; 4319536Seric 4329536Seric # ifdef DEBUG 4339536Seric if (tTd(45, 1)) 43414786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 4359536Seric # endif DEBUG 4369536Seric 4379536Seric /* 4389536Seric ** Figure out the real user executing us. 4399536Seric ** Username can return errno != 0 on non-errors. 4409536Seric */ 4419536Seric 4429536Seric if (QueueRun || OpMode == MD_SMTP || OpMode == MD_ARPAFTP) 4439536Seric realname = from; 4449536Seric if (realname == NULL || realname[0] == '\0') 4459536Seric { 4469536Seric extern char *username(); 4479536Seric 4489536Seric realname = username(); 4499536Seric } 4509536Seric 4519536Seric /* 4529536Seric ** Determine if this real person is allowed to alias themselves. 4539536Seric */ 4549536Seric 4559536Seric if (from != NULL) 4569536Seric { 4579536Seric extern bool trusteduser(); 4589536Seric 4599536Seric if (!trusteduser(realname) && 4609536Seric # ifdef DEBUG 4619536Seric (!tTd(1, 9) || getuid() != geteuid()) && 4629536Seric # endif DEBUG 4639536Seric index(from, '!') == NULL && getuid() != 0) 4649536Seric { 4659536Seric /* network sends -r regardless (why why why?) */ 4669536Seric /* syserr("%s, you cannot use the -f flag", realname); */ 4679536Seric from = NULL; 4689536Seric } 4699536Seric } 4709536Seric 4719536Seric SuprErrs = TRUE; 47211447Seric if (from == NULL || parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL) 4739536Seric { 47421750Seric /* log garbage addresses for traceback */ 47521750Seric if (from != NULL) 47621750Seric { 47724944Seric # ifdef LOG 47824944Seric if (LogLevel >= 1) 47924944Seric syslog(LOG_ERR, "Unparseable user %s wants to be %s", 48024944Seric 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 { 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