122704Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 333729Sbostic * Copyright (c) 1988 Regents of the University of California. 433729Sbostic * All rights reserved. 533729Sbostic * 642826Sbostic * %sccs.include.redist.c% 733729Sbostic */ 822704Sdist 922704Sdist #ifndef lint 10*53736Seric static char sccsid[] = "@(#)envelope.c 5.27 (Berkeley) 05/29/92"; 1133729Sbostic #endif /* not lint */ 1222704Sdist 1336928Sbostic #include <sys/types.h> 1436928Sbostic #include <sys/time.h> 1536928Sbostic #include <sys/stat.h> 169536Seric #include <pwd.h> 1740933Srick #include <sys/file.h> 189536Seric #include "sendmail.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(); 4125611Seric extern ENVELOPE BlankEnvelope; 429536Seric 439536Seric parent = CurEnv; 449536Seric if (e == CurEnv) 459536Seric parent = e->e_parent; 4625611Seric clearenvelope(e, TRUE); 4724944Seric if (e == CurEnv) 4824944Seric bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from); 4924944Seric else 5024944Seric bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from); 519536Seric e->e_parent = parent; 529536Seric e->e_ctime = curtime(); 5325014Seric e->e_msgpriority = parent->e_msgsize; 549536Seric e->e_puthdr = putheader; 559536Seric e->e_putbody = putbody; 569536Seric if (CurEnv->e_xfp != NULL) 579536Seric (void) fflush(CurEnv->e_xfp); 589536Seric 599536Seric return (e); 609536Seric } 619536Seric /* 629536Seric ** DROPENVELOPE -- deallocate an envelope. 639536Seric ** 649536Seric ** Parameters: 659536Seric ** e -- the envelope to deallocate. 669536Seric ** 679536Seric ** Returns: 689536Seric ** none. 699536Seric ** 709536Seric ** Side Effects: 719536Seric ** housekeeping necessary to dispose of an envelope. 729536Seric ** Unlocks this queue file. 739536Seric */ 749536Seric 759536Seric dropenvelope(e) 769536Seric register ENVELOPE *e; 779536Seric { 789536Seric bool queueit = FALSE; 799536Seric register ADDRESS *q; 809536Seric 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 #ifdef LOG 889536Seric if (LogLevel > 10) 899536Seric syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d", 909536Seric e->e_id == NULL ? "(none)" : e->e_id, 919536Seric e->e_flags, getpid()); 929536Seric #endif LOG 939536Seric 949536Seric /* we must have an id to remove disk files */ 959536Seric if (e->e_id == NULL) 969536Seric return; 979536Seric 989536Seric /* 999536Seric ** Extract state information from dregs of send list. 1009536Seric */ 1019536Seric 1029536Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1039536Seric { 1049536Seric if (bitset(QQUEUEUP, q->q_flags)) 1059536Seric queueit = TRUE; 1069536Seric } 1079536Seric 1089536Seric /* 1099536Seric ** Send back return receipts as requested. 1109536Seric */ 1119536Seric 1129536Seric if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 1139536Seric { 11410844Seric auto ADDRESS *rlist = NULL; 1159536Seric 1169621Seric sendtolist(CurEnv->e_receiptto, (ADDRESS *) NULL, &rlist); 1179536Seric (void) returntosender("Return receipt", rlist, FALSE); 1189536Seric } 1199536Seric 1209536Seric /* 1219536Seric ** Arrange to send error messages if there are fatal errors. 1229536Seric */ 1239536Seric 12410754Seric if (bitset(EF_FATALERRS|EF_TIMEOUT, e->e_flags) && ErrorMode != EM_QUIET) 1259536Seric savemail(e); 1269536Seric 1279536Seric /* 1289536Seric ** Instantiate or deinstantiate the queue. 1299536Seric */ 1309536Seric 1319536Seric if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 1329536Seric bitset(EF_CLRQUEUE, e->e_flags)) 1339536Seric { 13423497Seric if (e->e_df != NULL) 13523497Seric xunlink(e->e_df); 1369536Seric xunlink(queuename(e, 'q')); 1379536Seric } 1389536Seric else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 13910754Seric { 14010754Seric #ifdef QUEUE 14151920Seric queueup(e, FALSE, FALSE); 14210754Seric #else QUEUE 14310754Seric syserr("dropenvelope: queueup"); 14410754Seric #endif QUEUE 14510754Seric } 1469536Seric 1479536Seric /* now unlock the job */ 14810196Seric closexscript(e); 1499536Seric unlockqueue(e); 1509536Seric 1519536Seric /* make sure that this envelope is marked unused */ 1529536Seric e->e_id = e->e_df = NULL; 15324944Seric if (e->e_dfp != NULL) 15424944Seric (void) fclose(e->e_dfp); 15510196Seric e->e_dfp = NULL; 1569536Seric } 1579536Seric /* 1589536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 1599536Seric ** 1609536Seric ** This is normally used by a child process to get a clean 1619536Seric ** envelope without disturbing the parent. 1629536Seric ** 1639536Seric ** Parameters: 1649536Seric ** e -- the envelope to clear. 16525611Seric ** fullclear - if set, the current envelope is total 16625611Seric ** garbage and should be ignored; otherwise, 16725611Seric ** release any resources it may indicate. 1689536Seric ** 1699536Seric ** Returns: 1709536Seric ** none. 1719536Seric ** 1729536Seric ** Side Effects: 1739536Seric ** Closes files associated with the envelope. 1749536Seric ** Marks the envelope as unallocated. 1759536Seric */ 1769536Seric 17725611Seric clearenvelope(e, fullclear) 1789536Seric register ENVELOPE *e; 17925611Seric bool fullclear; 1809536Seric { 18125514Seric register HDR *bh; 18225514Seric register HDR **nhp; 18325514Seric extern ENVELOPE BlankEnvelope; 18425514Seric 18525611Seric if (!fullclear) 18625611Seric { 18725611Seric /* clear out any file information */ 18825611Seric if (e->e_xfp != NULL) 18925611Seric (void) fclose(e->e_xfp); 19025611Seric if (e->e_dfp != NULL) 19125611Seric (void) fclose(e->e_dfp); 19225611Seric } 1939536Seric 19424961Seric /* now clear out the data */ 19524965Seric STRUCTCOPY(BlankEnvelope, *e); 19625514Seric bh = BlankEnvelope.e_header; 19725514Seric nhp = &e->e_header; 19825514Seric while (bh != NULL) 19925514Seric { 20025514Seric *nhp = (HDR *) xalloc(sizeof *bh); 20125514Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 20225514Seric bh = bh->h_link; 20325514Seric nhp = &(*nhp)->h_link; 20425514Seric } 2059536Seric } 2069536Seric /* 2079536Seric ** INITSYS -- initialize instantiation of system 2089536Seric ** 2099536Seric ** In Daemon mode, this is done in the child. 2109536Seric ** 2119536Seric ** Parameters: 2129536Seric ** none. 2139536Seric ** 2149536Seric ** Returns: 2159536Seric ** none. 2169536Seric ** 2179536Seric ** Side Effects: 2189536Seric ** Initializes the system macros, some global variables, 2199536Seric ** etc. In particular, the current time in various 2209536Seric ** forms is set. 2219536Seric */ 2229536Seric 2239536Seric initsys() 2249536Seric { 2259536Seric static char cbuf[5]; /* holds hop count */ 2269536Seric static char pbuf[10]; /* holds pid */ 22722963Smiriam #ifdef TTYNAME 2289536Seric static char ybuf[10]; /* holds tty id */ 2299536Seric register char *p; 23022963Smiriam #endif TTYNAME 2319536Seric extern char *ttyname(); 2329536Seric extern char *macvalue(); 2339536Seric extern char Version[]; 2349536Seric 2359536Seric /* 2369536Seric ** Give this envelope a reality. 2379536Seric ** I.e., an id, a transcript, and a creation time. 2389536Seric */ 2399536Seric 2409536Seric openxscript(CurEnv); 2419536Seric CurEnv->e_ctime = curtime(); 2429536Seric 2439536Seric /* 2449536Seric ** Set OutChannel to something useful if stdout isn't it. 2459536Seric ** This arranges that any extra stuff the mailer produces 2469536Seric ** gets sent back to the user on error (because it is 2479536Seric ** tucked away in the transcript). 2489536Seric */ 2499536Seric 2509536Seric if (OpMode == MD_DAEMON && QueueRun) 2519536Seric OutChannel = CurEnv->e_xfp; 2529536Seric 2539536Seric /* 2549536Seric ** Set up some basic system macros. 2559536Seric */ 2569536Seric 2579536Seric /* process id */ 2589536Seric (void) sprintf(pbuf, "%d", getpid()); 2599536Seric define('p', pbuf, CurEnv); 2609536Seric 2619536Seric /* hop count */ 2629536Seric (void) sprintf(cbuf, "%d", CurEnv->e_hopcount); 2639536Seric define('c', cbuf, CurEnv); 2649536Seric 2659536Seric /* time as integer, unix time, arpa time */ 26611932Seric settime(); 2679536Seric 26817472Seric #ifdef TTYNAME 2699536Seric /* tty name */ 2709536Seric if (macvalue('y', CurEnv) == NULL) 2719536Seric { 2729536Seric p = ttyname(2); 2739536Seric if (p != NULL) 2749536Seric { 2759536Seric if (rindex(p, '/') != NULL) 2769536Seric p = rindex(p, '/') + 1; 2779536Seric (void) strcpy(ybuf, p); 2789536Seric define('y', ybuf, CurEnv); 2799536Seric } 2809536Seric } 28117472Seric #endif TTYNAME 2829536Seric } 2839536Seric /* 28411932Seric ** SETTIME -- set the current time. 28511932Seric ** 28611932Seric ** Parameters: 28711932Seric ** none. 28811932Seric ** 28911932Seric ** Returns: 29011932Seric ** none. 29111932Seric ** 29211932Seric ** Side Effects: 29311932Seric ** Sets the various time macros -- $a, $b, $d, $t. 29411932Seric */ 29511932Seric 29611932Seric settime() 29711932Seric { 29811932Seric register char *p; 29911932Seric auto time_t now; 30011932Seric static char tbuf[20]; /* holds "current" time */ 30111932Seric static char dbuf[30]; /* holds ctime(tbuf) */ 30211932Seric register struct tm *tm; 30311932Seric extern char *arpadate(); 30411932Seric extern struct tm *gmtime(); 30511932Seric extern char *macvalue(); 30611932Seric 30711932Seric now = curtime(); 30811932Seric tm = gmtime(&now); 30911932Seric (void) sprintf(tbuf, "%02d%02d%02d%02d%02d", tm->tm_year, tm->tm_mon+1, 31011932Seric tm->tm_mday, tm->tm_hour, tm->tm_min); 31111932Seric define('t', tbuf, CurEnv); 31211932Seric (void) strcpy(dbuf, ctime(&now)); 31311932Seric *index(dbuf, '\n') = '\0'; 31411932Seric if (macvalue('d', CurEnv) == NULL) 31511932Seric define('d', dbuf, CurEnv); 31611932Seric p = newstr(arpadate(dbuf)); 31711932Seric if (macvalue('a', CurEnv) == NULL) 31811932Seric define('a', p, CurEnv); 31911932Seric define('b', p, CurEnv); 32011932Seric } 32111932Seric /* 3229536Seric ** OPENXSCRIPT -- Open transcript file 3239536Seric ** 3249536Seric ** Creates a transcript file for possible eventual mailing or 3259536Seric ** sending back. 3269536Seric ** 3279536Seric ** Parameters: 3289536Seric ** e -- the envelope to create the transcript in/for. 3299536Seric ** 3309536Seric ** Returns: 3319536Seric ** none 3329536Seric ** 3339536Seric ** Side Effects: 3349536Seric ** Creates the transcript file. 3359536Seric */ 3369536Seric 3379536Seric openxscript(e) 3389536Seric register ENVELOPE *e; 3399536Seric { 3409536Seric register char *p; 34140933Srick int fd; 3429536Seric 34310196Seric # ifdef LOG 34410196Seric if (LogLevel > 19) 34510196Seric syslog(LOG_DEBUG, "%s: openx%s", e->e_id, e->e_xfp == NULL ? "" : " (no)"); 34610196Seric # endif LOG 3479536Seric if (e->e_xfp != NULL) 3489536Seric return; 3499536Seric p = queuename(e, 'x'); 35040933Srick fd = open(p, O_WRONLY|O_CREAT, 0644); 35140933Srick if (fd < 0) 3529536Seric syserr("Can't create %s", p); 3539536Seric else 35440933Srick e->e_xfp = fdopen(fd, "w"); 3559536Seric } 3569536Seric /* 35710196Seric ** CLOSEXSCRIPT -- close the transcript file. 35810196Seric ** 35910196Seric ** Parameters: 36010196Seric ** e -- the envelope containing the transcript to close. 36110196Seric ** 36210196Seric ** Returns: 36310196Seric ** none. 36410196Seric ** 36510196Seric ** Side Effects: 36610196Seric ** none. 36710196Seric */ 36810196Seric 36910196Seric closexscript(e) 37010196Seric register ENVELOPE *e; 37110196Seric { 37210196Seric if (e->e_xfp == NULL) 37310196Seric return; 37410196Seric (void) fclose(e->e_xfp); 37510196Seric e->e_xfp = NULL; 37610196Seric } 37710196Seric /* 3789536Seric ** SETSENDER -- set the person who this message is from 3799536Seric ** 3809536Seric ** Under certain circumstances allow the user to say who 3819536Seric ** s/he is (using -f or -r). These are: 3829536Seric ** 1. The user's uid is zero (root). 3839536Seric ** 2. The user's login name is in an approved list (typically 3849536Seric ** from a network server). 3859536Seric ** 3. The address the user is trying to claim has a 3869536Seric ** "!" character in it (since #2 doesn't do it for 3879536Seric ** us if we are dialing out for UUCP). 3889536Seric ** A better check to replace #3 would be if the 3899536Seric ** effective uid is "UUCP" -- this would require me 3909536Seric ** to rewrite getpwent to "grab" uucp as it went by, 3919536Seric ** make getname more nasty, do another passwd file 3929536Seric ** scan, or compile the UID of "UUCP" into the code, 3939536Seric ** all of which are reprehensible. 3949536Seric ** 3959536Seric ** Assuming all of these fail, we figure out something 3969536Seric ** ourselves. 3979536Seric ** 3989536Seric ** Parameters: 3999536Seric ** from -- the person we would like to believe this message 4009536Seric ** is from, as specified on the command line. 40153182Seric ** e -- the envelope in which we would like the sender set. 4029536Seric ** 4039536Seric ** Returns: 4049536Seric ** none. 4059536Seric ** 4069536Seric ** Side Effects: 4079536Seric ** sets sendmail's notion of who the from person is. 4089536Seric */ 4099536Seric 41053182Seric setsender(from, e) 4119536Seric char *from; 41253182Seric register ENVELOPE *e; 4139536Seric { 4149536Seric register char **pvp; 4159536Seric char *realname = NULL; 41618665Seric register struct passwd *pw; 4179536Seric char buf[MAXNAME]; 41816913Seric char pvpbuf[PSBUFSIZE]; 41918665Seric extern struct passwd *getpwnam(); 4209536Seric extern char *macvalue(); 4219536Seric extern char **prescan(); 4229536Seric extern bool safefile(); 4239536Seric extern char *FullName; 4249536Seric 4259536Seric if (tTd(45, 1)) 42614786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 4279536Seric 4289536Seric /* 4299536Seric ** Figure out the real user executing us. 4309536Seric ** Username can return errno != 0 on non-errors. 4319536Seric */ 4329536Seric 43352220Seric if (QueueRun || OpMode == MD_SMTP) 4349536Seric realname = from; 4359536Seric if (realname == NULL || realname[0] == '\0') 4369536Seric { 4379536Seric extern char *username(); 4389536Seric 4399536Seric realname = username(); 4409536Seric } 4419536Seric 4429536Seric /* 4439536Seric ** Determine if this real person is allowed to alias themselves. 4449536Seric */ 4459536Seric 4469536Seric if (from != NULL) 4479536Seric { 4489536Seric extern bool trusteduser(); 4499536Seric 45036230Skarels if (!trusteduser(realname) && getuid() != geteuid() && 4519536Seric index(from, '!') == NULL && getuid() != 0) 4529536Seric { 4539536Seric /* network sends -r regardless (why why why?) */ 4549536Seric /* syserr("%s, you cannot use the -f flag", realname); */ 4559536Seric from = NULL; 4569536Seric } 4579536Seric } 4589536Seric 4599536Seric SuprErrs = TRUE; 46053182Seric if (from == NULL || parseaddr(from, &e->e_from, 1, '\0') == NULL) 4619536Seric { 46221750Seric /* log garbage addresses for traceback */ 46321750Seric if (from != NULL) 46421750Seric { 46524944Seric # ifdef LOG 46624944Seric if (LogLevel >= 1) 46736230Skarels if (realname == from && RealHostName != NULL) 46836230Skarels syslog(LOG_NOTICE, 46936230Skarels "from=%s unparseable, received from %s", 47036230Skarels from, RealHostName); 47136230Skarels else 47236230Skarels syslog(LOG_NOTICE, 47336230Skarels "Unparseable username %s wants from=%s", 47436230Skarels realname, from); 47524944Seric # endif LOG 47621750Seric } 4779536Seric from = newstr(realname); 47853182Seric if (parseaddr(from, &e->e_from, 1, '\0') == NULL && 47953182Seric parseaddr("postmaster", &e->e_from, 1, '\0') == NULL) 48024944Seric { 48124944Seric syserr("setsender: can't even parse postmaster!"); 48224944Seric } 4839536Seric } 4849536Seric else 4859536Seric FromFlag = TRUE; 48653182Seric e->e_from.q_flags |= QDONTSEND; 48753182Seric loweraddr(&e->e_from); 4889536Seric SuprErrs = FALSE; 4899536Seric 490*53736Seric pvp = NULL; 491*53736Seric if (e->e_from.q_mailer == LocalMailer) 4929536Seric { 493*53736Seric # ifdef USERDB 494*53736Seric register char *p; 495*53736Seric extern char *udbsender(); 496*53736Seric # endif 49717472Seric 4989536Seric /* if the user has given fullname already, don't redefine */ 4999536Seric if (FullName == NULL) 50053182Seric FullName = macvalue('x', e); 50111932Seric if (FullName != NULL && FullName[0] == '\0') 5029536Seric FullName = NULL; 5039536Seric 504*53736Seric # ifdef USERDB 505*53736Seric p = udbsender(from); 506*53736Seric 507*53736Seric if (p != NULL) 5089536Seric { 509*53736Seric /* 510*53736Seric ** We have an alternate address for the sender 511*53736Seric */ 512*53736Seric 513*53736Seric pvp = prescan(p, '\0', pvpbuf); 5149536Seric } 515*53736Seric # endif /* USERDB */ 516*53736Seric 517*53736Seric if ((pw = getpwnam(e->e_from.q_user)) != NULL) 518*53736Seric { 519*53736Seric /* 520*53736Seric ** Process passwd file entry. 521*53736Seric */ 522*53736Seric 523*53736Seric 524*53736Seric /* extract home directory */ 525*53736Seric e->e_from.q_home = newstr(pw->pw_dir); 526*53736Seric define('z', e->e_from.q_home, e); 527*53736Seric 528*53736Seric /* extract user and group id */ 529*53736Seric e->e_from.q_uid = pw->pw_uid; 530*53736Seric e->e_from.q_gid = pw->pw_gid; 531*53736Seric 532*53736Seric /* extract full name from passwd file */ 533*53736Seric if (FullName == NULL && pw->pw_gecos != NULL && 534*53736Seric strcmp(pw->pw_name, e->e_from.q_user) == 0) 535*53736Seric { 536*53736Seric buildfname(pw->pw_gecos, e->e_from.q_user, buf); 537*53736Seric if (buf[0] != '\0') 538*53736Seric FullName = newstr(buf); 539*53736Seric } 540*53736Seric } 5419536Seric if (FullName != NULL) 54253182Seric define('x', FullName, e); 5439536Seric } 54411625Seric else 54511625Seric { 54653182Seric if (e->e_from.q_home == NULL) 54753182Seric e->e_from.q_home = getenv("HOME"); 54853182Seric e->e_from.q_uid = getuid(); 54953182Seric e->e_from.q_gid = getgid(); 55011625Seric } 55111625Seric 5529536Seric /* 5539536Seric ** Rewrite the from person to dispose of possible implicit 5549536Seric ** links in the net. 5559536Seric */ 5569536Seric 5579536Seric if (pvp == NULL) 558*53736Seric pvp = prescan(from, '\0', pvpbuf); 559*53736Seric if (pvp == NULL) 5609536Seric { 56136233Skarels # ifdef LOG 56236233Skarels if (LogLevel >= 1) 56336233Skarels syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 56436233Skarels # endif 56536230Skarels usrerr("cannot prescan from (%s)", from); 5669536Seric finis(); 5679536Seric } 5689536Seric rewrite(pvp, 3); 5699536Seric rewrite(pvp, 1); 57025032Seric rewrite(pvp, 4); 5719536Seric cataddr(pvp, buf, sizeof buf); 57253182Seric e->e_sender = e->e_returnpath = newstr(buf); 5739536Seric 57453182Seric define('f', e->e_sender, e); 57551951Seric 5769536Seric /* save the domain spec if this mailer wants it */ 57753182Seric if (e->e_from.q_mailer != NULL && 57853182Seric bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 5799536Seric { 5809536Seric extern char **copyplist(); 5819536Seric 5829536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 5839536Seric pvp++; 5849536Seric if (*pvp != NULL) 58553182Seric e->e_fromdomain = copyplist(pvp, TRUE); 5869536Seric } 5879536Seric } 5889536Seric /* 5899536Seric ** TRUSTEDUSER -- tell us if this user is to be trusted. 5909536Seric ** 5919536Seric ** Parameters: 5929536Seric ** user -- the user to be checked. 5939536Seric ** 5949536Seric ** Returns: 5959536Seric ** TRUE if the user is in an approved list. 5969536Seric ** FALSE otherwise. 5979536Seric ** 5989536Seric ** Side Effects: 5999536Seric ** none. 6009536Seric */ 6019536Seric 6029536Seric bool 6039536Seric trusteduser(user) 6049536Seric char *user; 6059536Seric { 6069536Seric register char **ulist; 6079536Seric extern char *TrustedUsers[]; 6089536Seric 6099536Seric for (ulist = TrustedUsers; *ulist != NULL; ulist++) 6109536Seric if (strcmp(*ulist, user) == 0) 6119536Seric return (TRUE); 6129536Seric return (FALSE); 6139536Seric } 614