19536Seric #include <pwd.h> 213587Swnj #include <sys/time.h> 39536Seric #include "sendmail.h" 49536Seric #include <sys/stat.h> 59536Seric 6*17472Seric SCCSID(@(#)envelope.c 4.8 12/05/84); 79536Seric 89536Seric /* 99536Seric ** NEWENVELOPE -- allocate a new envelope 109536Seric ** 119536Seric ** Supports inheritance. 129536Seric ** 139536Seric ** Parameters: 149536Seric ** e -- the new envelope to fill in. 159536Seric ** 169536Seric ** Returns: 179536Seric ** e. 189536Seric ** 199536Seric ** Side Effects: 209536Seric ** none. 219536Seric */ 229536Seric 239536Seric ENVELOPE * 249536Seric newenvelope(e) 259536Seric register ENVELOPE *e; 269536Seric { 279536Seric register HDR *bh; 289536Seric register HDR **nhp; 299536Seric register ENVELOPE *parent; 309536Seric extern putheader(), putbody(); 319536Seric extern ENVELOPE BlankEnvelope; 329536Seric 339536Seric parent = CurEnv; 349536Seric if (e == CurEnv) 359536Seric parent = e->e_parent; 3616888Seric bzero((char *) e, sizeof *e); 3716888Seric bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from); 389536Seric e->e_parent = parent; 399536Seric e->e_ctime = curtime(); 409536Seric e->e_puthdr = putheader; 419536Seric e->e_putbody = putbody; 429536Seric bh = BlankEnvelope.e_header; 439536Seric nhp = &e->e_header; 449536Seric while (bh != NULL) 459536Seric { 469536Seric *nhp = (HDR *) xalloc(sizeof *bh); 4716888Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 489536Seric bh = bh->h_link; 499536Seric nhp = &(*nhp)->h_link; 509536Seric } 519536Seric if (CurEnv->e_xfp != NULL) 529536Seric (void) fflush(CurEnv->e_xfp); 539536Seric 549536Seric return (e); 559536Seric } 569536Seric /* 579536Seric ** DROPENVELOPE -- deallocate an envelope. 589536Seric ** 599536Seric ** Parameters: 609536Seric ** e -- the envelope to deallocate. 619536Seric ** 629536Seric ** Returns: 639536Seric ** none. 649536Seric ** 659536Seric ** Side Effects: 669536Seric ** housekeeping necessary to dispose of an envelope. 679536Seric ** Unlocks this queue file. 689536Seric */ 699536Seric 709536Seric dropenvelope(e) 719536Seric register ENVELOPE *e; 729536Seric { 739536Seric bool queueit = FALSE; 749536Seric register ADDRESS *q; 759536Seric 769536Seric #ifdef DEBUG 779536Seric if (tTd(50, 1)) 789536Seric { 799536Seric printf("dropenvelope %x id=", e); 809536Seric xputs(e->e_id); 819536Seric printf(" flags=%o\n", e->e_flags); 829536Seric } 839536Seric #endif DEBUG 849536Seric #ifdef LOG 859536Seric if (LogLevel > 10) 869536Seric syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d", 879536Seric e->e_id == NULL ? "(none)" : e->e_id, 889536Seric e->e_flags, getpid()); 899536Seric #endif LOG 909536Seric 919536Seric /* we must have an id to remove disk files */ 929536Seric if (e->e_id == NULL) 939536Seric return; 949536Seric 959536Seric /* 969536Seric ** Extract state information from dregs of send list. 979536Seric */ 989536Seric 999536Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1009536Seric { 1019536Seric if (bitset(QQUEUEUP, q->q_flags)) 1029536Seric queueit = TRUE; 1039536Seric } 1049536Seric 1059536Seric /* 1069536Seric ** Send back return receipts as requested. 1079536Seric */ 1089536Seric 1099536Seric if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 1109536Seric { 11110844Seric auto ADDRESS *rlist = NULL; 1129536Seric 1139621Seric sendtolist(CurEnv->e_receiptto, (ADDRESS *) NULL, &rlist); 1149536Seric (void) returntosender("Return receipt", rlist, FALSE); 1159536Seric } 1169536Seric 1179536Seric /* 1189536Seric ** Arrange to send error messages if there are fatal errors. 1199536Seric */ 1209536Seric 12110754Seric if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && ErrorMode != EM_QUIET) 1229536Seric savemail(e); 1239536Seric 1249536Seric /* 1259536Seric ** Instantiate or deinstantiate the queue. 1269536Seric */ 1279536Seric 1289536Seric if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 1299536Seric bitset(EF_CLRQUEUE, e->e_flags)) 1309536Seric { 1319536Seric if (e->e_dfp != NULL) 1329536Seric (void) fclose(e->e_dfp); 1339536Seric if (e->e_df != NULL) 1349536Seric xunlink(e->e_df); 1359536Seric xunlink(queuename(e, 'q')); 1369536Seric } 1379536Seric else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 13810754Seric { 13910754Seric #ifdef QUEUE 1409536Seric queueup(e, FALSE, FALSE); 14110754Seric #else QUEUE 14210754Seric syserr("dropenvelope: queueup"); 14310754Seric #endif QUEUE 14410754Seric } 1459536Seric 1469536Seric /* now unlock the job */ 14710196Seric closexscript(e); 1489536Seric unlockqueue(e); 1499536Seric 1509536Seric /* make sure that this envelope is marked unused */ 1519536Seric e->e_id = e->e_df = NULL; 15210196Seric e->e_dfp = NULL; 1539536Seric } 1549536Seric /* 1559536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 1569536Seric ** 1579536Seric ** This is normally used by a child process to get a clean 1589536Seric ** envelope without disturbing the parent. 1599536Seric ** 1609536Seric ** Parameters: 1619536Seric ** e -- the envelope to clear. 1629536Seric ** 1639536Seric ** Returns: 1649536Seric ** none. 1659536Seric ** 1669536Seric ** Side Effects: 1679536Seric ** Closes files associated with the envelope. 1689536Seric ** Marks the envelope as unallocated. 1699536Seric */ 1709536Seric 1719536Seric clearenvelope(e) 1729536Seric register ENVELOPE *e; 1739536Seric { 1749536Seric /* clear out any file information */ 1759536Seric if (e->e_xfp != NULL) 1769536Seric (void) fclose(e->e_xfp); 1779536Seric if (e->e_dfp != NULL) 1789536Seric (void) fclose(e->e_dfp); 1799536Seric e->e_xfp = e->e_dfp = NULL; 1809536Seric 1819536Seric /* now expunge names of objects */ 1829536Seric e->e_df = e->e_id = NULL; 1839536Seric 1849536Seric /* and the flags which are now meaningless */ 1859536Seric e->e_flags = 0; 1869536Seric } 1879536Seric /* 1889536Seric ** INITSYS -- initialize instantiation of system 1899536Seric ** 1909536Seric ** In Daemon mode, this is done in the child. 1919536Seric ** 1929536Seric ** Parameters: 1939536Seric ** none. 1949536Seric ** 1959536Seric ** Returns: 1969536Seric ** none. 1979536Seric ** 1989536Seric ** Side Effects: 1999536Seric ** Initializes the system macros, some global variables, 2009536Seric ** etc. In particular, the current time in various 2019536Seric ** forms is set. 2029536Seric */ 2039536Seric 2049536Seric initsys() 2059536Seric { 2069536Seric static char cbuf[5]; /* holds hop count */ 2079536Seric static char pbuf[10]; /* holds pid */ 2089536Seric static char ybuf[10]; /* holds tty id */ 2099536Seric register char *p; 2109536Seric extern char *ttyname(); 2119536Seric extern char *macvalue(); 2129536Seric extern char Version[]; 2139536Seric 2149536Seric /* 2159536Seric ** Give this envelope a reality. 2169536Seric ** I.e., an id, a transcript, and a creation time. 2179536Seric */ 2189536Seric 2199536Seric openxscript(CurEnv); 2209536Seric CurEnv->e_ctime = curtime(); 2219536Seric 2229536Seric /* 2239536Seric ** Set OutChannel to something useful if stdout isn't it. 2249536Seric ** This arranges that any extra stuff the mailer produces 2259536Seric ** gets sent back to the user on error (because it is 2269536Seric ** tucked away in the transcript). 2279536Seric */ 2289536Seric 2299536Seric if (OpMode == MD_DAEMON && QueueRun) 2309536Seric OutChannel = CurEnv->e_xfp; 2319536Seric 2329536Seric /* 2339536Seric ** Set up some basic system macros. 2349536Seric */ 2359536Seric 2369536Seric /* process id */ 2379536Seric (void) sprintf(pbuf, "%d", getpid()); 2389536Seric define('p', pbuf, CurEnv); 2399536Seric 2409536Seric /* hop count */ 2419536Seric (void) sprintf(cbuf, "%d", CurEnv->e_hopcount); 2429536Seric define('c', cbuf, CurEnv); 2439536Seric 2449536Seric /* time as integer, unix time, arpa time */ 24511932Seric settime(); 2469536Seric 247*17472Seric #ifdef TTYNAME 2489536Seric /* tty name */ 2499536Seric if (macvalue('y', CurEnv) == NULL) 2509536Seric { 2519536Seric p = ttyname(2); 2529536Seric if (p != NULL) 2539536Seric { 2549536Seric if (rindex(p, '/') != NULL) 2559536Seric p = rindex(p, '/') + 1; 2569536Seric (void) strcpy(ybuf, p); 2579536Seric define('y', ybuf, CurEnv); 2589536Seric } 2599536Seric } 260*17472Seric #endif TTYNAME 2619536Seric } 2629536Seric /* 26311932Seric ** SETTIME -- set the current time. 26411932Seric ** 26511932Seric ** Parameters: 26611932Seric ** none. 26711932Seric ** 26811932Seric ** Returns: 26911932Seric ** none. 27011932Seric ** 27111932Seric ** Side Effects: 27211932Seric ** Sets the various time macros -- $a, $b, $d, $t. 27311932Seric */ 27411932Seric 27511932Seric settime() 27611932Seric { 27711932Seric register char *p; 27811932Seric auto time_t now; 27911932Seric static char tbuf[20]; /* holds "current" time */ 28011932Seric static char dbuf[30]; /* holds ctime(tbuf) */ 28111932Seric register struct tm *tm; 28211932Seric extern char *arpadate(); 28311932Seric extern struct tm *gmtime(); 28411932Seric extern char *macvalue(); 28511932Seric 28611932Seric now = curtime(); 28711932Seric tm = gmtime(&now); 28811932Seric (void) sprintf(tbuf, "%02d%02d%02d%02d%02d", tm->tm_year, tm->tm_mon+1, 28911932Seric tm->tm_mday, tm->tm_hour, tm->tm_min); 29011932Seric define('t', tbuf, CurEnv); 29111932Seric (void) strcpy(dbuf, ctime(&now)); 29211932Seric *index(dbuf, '\n') = '\0'; 29311932Seric if (macvalue('d', CurEnv) == NULL) 29411932Seric define('d', dbuf, CurEnv); 29511932Seric p = newstr(arpadate(dbuf)); 29611932Seric if (macvalue('a', CurEnv) == NULL) 29711932Seric define('a', p, CurEnv); 29811932Seric define('b', p, CurEnv); 29911932Seric } 30011932Seric /* 3019536Seric ** OPENXSCRIPT -- Open transcript file 3029536Seric ** 3039536Seric ** Creates a transcript file for possible eventual mailing or 3049536Seric ** sending back. 3059536Seric ** 3069536Seric ** Parameters: 3079536Seric ** e -- the envelope to create the transcript in/for. 3089536Seric ** 3099536Seric ** Returns: 3109536Seric ** none 3119536Seric ** 3129536Seric ** Side Effects: 3139536Seric ** Creates the transcript file. 3149536Seric */ 3159536Seric 3169536Seric openxscript(e) 3179536Seric register ENVELOPE *e; 3189536Seric { 3199536Seric register char *p; 3209536Seric 32110196Seric # ifdef LOG 32210196Seric if (LogLevel > 19) 32310196Seric syslog(LOG_DEBUG, "%s: openx%s", e->e_id, e->e_xfp == NULL ? "" : " (no)"); 32410196Seric # endif LOG 3259536Seric if (e->e_xfp != NULL) 3269536Seric return; 3279536Seric p = queuename(e, 'x'); 3289536Seric e->e_xfp = fopen(p, "w"); 3299536Seric if (e->e_xfp == NULL) 3309536Seric syserr("Can't create %s", p); 3319536Seric else 3329536Seric (void) chmod(p, 0644); 3339536Seric } 3349536Seric /* 33510196Seric ** CLOSEXSCRIPT -- close the transcript file. 33610196Seric ** 33710196Seric ** Parameters: 33810196Seric ** e -- the envelope containing the transcript to close. 33910196Seric ** 34010196Seric ** Returns: 34110196Seric ** none. 34210196Seric ** 34310196Seric ** Side Effects: 34410196Seric ** none. 34510196Seric */ 34610196Seric 34710196Seric closexscript(e) 34810196Seric register ENVELOPE *e; 34910196Seric { 35010196Seric if (e->e_xfp == NULL) 35110196Seric return; 35210196Seric (void) fclose(e->e_xfp); 35310196Seric e->e_xfp = NULL; 35410196Seric } 35510196Seric /* 3569536Seric ** SETSENDER -- set the person who this message is from 3579536Seric ** 3589536Seric ** Under certain circumstances allow the user to say who 3599536Seric ** s/he is (using -f or -r). These are: 3609536Seric ** 1. The user's uid is zero (root). 3619536Seric ** 2. The user's login name is in an approved list (typically 3629536Seric ** from a network server). 3639536Seric ** 3. The address the user is trying to claim has a 3649536Seric ** "!" character in it (since #2 doesn't do it for 3659536Seric ** us if we are dialing out for UUCP). 3669536Seric ** A better check to replace #3 would be if the 3679536Seric ** effective uid is "UUCP" -- this would require me 3689536Seric ** to rewrite getpwent to "grab" uucp as it went by, 3699536Seric ** make getname more nasty, do another passwd file 3709536Seric ** scan, or compile the UID of "UUCP" into the code, 3719536Seric ** all of which are reprehensible. 3729536Seric ** 3739536Seric ** Assuming all of these fail, we figure out something 3749536Seric ** ourselves. 3759536Seric ** 3769536Seric ** Parameters: 3779536Seric ** from -- the person we would like to believe this message 3789536Seric ** is from, as specified on the command line. 3799536Seric ** 3809536Seric ** Returns: 3819536Seric ** none. 3829536Seric ** 3839536Seric ** Side Effects: 3849536Seric ** sets sendmail's notion of who the from person is. 3859536Seric */ 3869536Seric 3879536Seric setsender(from) 3889536Seric char *from; 3899536Seric { 3909536Seric register char **pvp; 3919536Seric char *realname = NULL; 3929536Seric char buf[MAXNAME]; 39316913Seric char pvpbuf[PSBUFSIZE]; 3949536Seric extern char *macvalue(); 3959536Seric extern char **prescan(); 3969536Seric extern bool safefile(); 3979536Seric extern char *FullName; 3989536Seric 3999536Seric # ifdef DEBUG 4009536Seric if (tTd(45, 1)) 40114786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 4029536Seric # endif DEBUG 4039536Seric 4049536Seric /* 4059536Seric ** Figure out the real user executing us. 4069536Seric ** Username can return errno != 0 on non-errors. 4079536Seric */ 4089536Seric 4099536Seric if (QueueRun || OpMode == MD_SMTP || OpMode == MD_ARPAFTP) 4109536Seric realname = from; 4119536Seric if (realname == NULL || realname[0] == '\0') 4129536Seric { 4139536Seric extern char *username(); 4149536Seric 4159536Seric realname = username(); 4169536Seric } 4179536Seric 4189536Seric /* 4199536Seric ** Determine if this real person is allowed to alias themselves. 4209536Seric */ 4219536Seric 4229536Seric if (from != NULL) 4239536Seric { 4249536Seric extern bool trusteduser(); 4259536Seric 4269536Seric if (!trusteduser(realname) && 4279536Seric # ifdef DEBUG 4289536Seric (!tTd(1, 9) || getuid() != geteuid()) && 4299536Seric # endif DEBUG 4309536Seric index(from, '!') == NULL && getuid() != 0) 4319536Seric { 4329536Seric /* network sends -r regardless (why why why?) */ 4339536Seric /* syserr("%s, you cannot use the -f flag", realname); */ 4349536Seric from = NULL; 4359536Seric } 4369536Seric } 4379536Seric 4389536Seric SuprErrs = TRUE; 43911447Seric if (from == NULL || parseaddr(from, &CurEnv->e_from, 1, '\0') == NULL) 4409536Seric { 4419536Seric from = newstr(realname); 44211447Seric (void) parseaddr(from, &CurEnv->e_from, 1, '\0'); 4439536Seric } 4449536Seric else 4459536Seric FromFlag = TRUE; 4469536Seric CurEnv->e_from.q_flags |= QDONTSEND; 44716162Seric loweraddr(&CurEnv->e_from); 4489536Seric SuprErrs = FALSE; 4499536Seric 450*17472Seric if (CurEnv->e_from.q_mailer == LocalMailer) 4519536Seric { 452*17472Seric register struct passwd *pw; 4539536Seric extern struct passwd *getpwnam(); 4549536Seric 455*17472Seric /* 456*17472Seric ** Process passwd file entry. 457*17472Seric */ 458*17472Seric 4599536Seric pw = getpwnam(CurEnv->e_from.q_user); 4609536Seric 4619536Seric /* extract home directory */ 4629536Seric CurEnv->e_from.q_home = newstr(pw->pw_dir); 46316481Seric define('z', CurEnv->e_from.q_home, CurEnv); 4649536Seric 46511625Seric /* extract user and group id */ 46611625Seric CurEnv->e_from.q_uid = pw->pw_uid; 46711625Seric CurEnv->e_from.q_gid = pw->pw_gid; 46811625Seric 4699536Seric /* if the user has given fullname already, don't redefine */ 4709536Seric if (FullName == NULL) 4719536Seric FullName = macvalue('x', CurEnv); 47211932Seric if (FullName != NULL && FullName[0] == '\0') 4739536Seric FullName = NULL; 4749536Seric 4759536Seric /* extract full name from passwd file */ 4769582Seric if (FullName == NULL && pw->pw_gecos != NULL && 4779582Seric strcmp(pw->pw_name, CurEnv->e_from.q_user) == 0) 4789536Seric { 4799536Seric buildfname(pw->pw_gecos, CurEnv->e_from.q_user, buf); 4809536Seric if (buf[0] != '\0') 4819536Seric FullName = newstr(buf); 4829536Seric } 4839536Seric if (FullName != NULL) 4849536Seric define('x', FullName, CurEnv); 4859536Seric } 48611625Seric else 48711625Seric { 4889536Seric #ifndef V6 48911625Seric if (CurEnv->e_from.q_home == NULL) 49011625Seric CurEnv->e_from.q_home = getenv("HOME"); 4919536Seric #endif V6 49211625Seric CurEnv->e_from.q_uid = getuid(); 49311625Seric CurEnv->e_from.q_gid = getgid(); 49411625Seric } 49511625Seric 4969536Seric if (CurEnv->e_from.q_uid != 0) 4979536Seric { 4989536Seric DefUid = CurEnv->e_from.q_uid; 4999536Seric DefGid = CurEnv->e_from.q_gid; 5009536Seric } 5019536Seric 5029536Seric /* 5039536Seric ** Rewrite the from person to dispose of possible implicit 5049536Seric ** links in the net. 5059536Seric */ 5069536Seric 50716913Seric pvp = prescan(from, '\0', pvpbuf); 5089536Seric if (pvp == NULL) 5099536Seric { 5109536Seric syserr("cannot prescan from (%s)", from); 5119536Seric finis(); 5129536Seric } 5139536Seric rewrite(pvp, 3); 5149536Seric rewrite(pvp, 1); 51511286Seric rewrite(pvp, 4); 5169536Seric cataddr(pvp, buf, sizeof buf); 5179536Seric define('f', newstr(buf), CurEnv); 5189536Seric 5199536Seric /* save the domain spec if this mailer wants it */ 52010690Seric if (bitnset(M_CANONICAL, CurEnv->e_from.q_mailer->m_flags)) 5219536Seric { 5229536Seric extern char **copyplist(); 5239536Seric 5249536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 5259536Seric pvp++; 5269536Seric if (*pvp != NULL) 5279536Seric CurEnv->e_fromdomain = copyplist(pvp, TRUE); 5289536Seric } 5299536Seric } 5309536Seric /* 5319536Seric ** TRUSTEDUSER -- tell us if this user is to be trusted. 5329536Seric ** 5339536Seric ** Parameters: 5349536Seric ** user -- the user to be checked. 5359536Seric ** 5369536Seric ** Returns: 5379536Seric ** TRUE if the user is in an approved list. 5389536Seric ** FALSE otherwise. 5399536Seric ** 5409536Seric ** Side Effects: 5419536Seric ** none. 5429536Seric */ 5439536Seric 5449536Seric bool 5459536Seric trusteduser(user) 5469536Seric char *user; 5479536Seric { 5489536Seric register char **ulist; 5499536Seric extern char *TrustedUsers[]; 5509536Seric 5519536Seric for (ulist = TrustedUsers; *ulist != NULL; ulist++) 5529536Seric if (strcmp(*ulist, user) == 0) 5539536Seric return (TRUE); 5549536Seric return (FALSE); 5559536Seric } 556