122704Sdist /* 234921Sbostic * Copyright (c) 1983 Eric P. Allman 362525Sbostic * Copyright (c) 1988, 1993 462525Sbostic * The Regents of the University of California. All rights reserved. 533729Sbostic * 642826Sbostic * %sccs.include.redist.c% 733729Sbostic */ 822704Sdist 922704Sdist #ifndef lint 10*67880Seric static char sccsid[] = "@(#)envelope.c 8.42 (Berkeley) 11/04/94"; 1133729Sbostic #endif /* not lint */ 1222704Sdist 1358332Seric #include "sendmail.h" 149536Seric #include <pwd.h> 159536Seric 169536Seric /* 179536Seric ** NEWENVELOPE -- allocate a new envelope 189536Seric ** 199536Seric ** Supports inheritance. 209536Seric ** 219536Seric ** Parameters: 229536Seric ** e -- the new envelope to fill in. 2358179Seric ** parent -- the envelope to be the parent of e. 249536Seric ** 259536Seric ** Returns: 269536Seric ** e. 279536Seric ** 289536Seric ** Side Effects: 299536Seric ** none. 309536Seric */ 319536Seric 329536Seric ENVELOPE * 3358179Seric newenvelope(e, parent) 349536Seric register ENVELOPE *e; 3558179Seric register ENVELOPE *parent; 369536Seric { 379536Seric extern putheader(), putbody(); 3825611Seric extern ENVELOPE BlankEnvelope; 399536Seric 4058179Seric if (e == parent && e->e_parent != NULL) 419536Seric parent = e->e_parent; 4225611Seric clearenvelope(e, TRUE); 4324944Seric if (e == CurEnv) 4424944Seric bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from); 4524944Seric else 4624944Seric bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from); 479536Seric e->e_parent = parent; 489536Seric e->e_ctime = curtime(); 4956215Seric if (parent != NULL) 5056215Seric e->e_msgpriority = parent->e_msgsize; 519536Seric e->e_puthdr = putheader; 529536Seric e->e_putbody = putbody; 539536Seric if (CurEnv->e_xfp != NULL) 549536Seric (void) fflush(CurEnv->e_xfp); 559536Seric 569536Seric return (e); 579536Seric } 589536Seric /* 599536Seric ** DROPENVELOPE -- deallocate an envelope. 609536Seric ** 619536Seric ** Parameters: 629536Seric ** e -- the envelope to deallocate. 639536Seric ** 649536Seric ** Returns: 659536Seric ** none. 669536Seric ** 679536Seric ** Side Effects: 689536Seric ** housekeeping necessary to dispose of an envelope. 699536Seric ** Unlocks this queue file. 709536Seric */ 719536Seric 7260494Seric void 739536Seric dropenvelope(e) 749536Seric register ENVELOPE *e; 759536Seric { 769536Seric bool queueit = FALSE; 77*67880Seric bool failure_return = FALSE; 78*67880Seric bool success_return = FALSE; 799536Seric register ADDRESS *q; 8057943Seric char *id = e->e_id; 81*67880Seric bool return_no, return_yes; 8263753Seric char buf[MAXLINE]; 839536Seric 849536Seric if (tTd(50, 1)) 859536Seric { 8658680Seric printf("dropenvelope %x: id=", e); 879536Seric xputs(e->e_id); 8865089Seric printf(", flags=0x%x\n", e->e_flags); 8963753Seric if (tTd(50, 10)) 9063753Seric { 9163753Seric printf("sendq="); 9263753Seric printaddr(e->e_sendqueue, TRUE); 9363753Seric } 949536Seric } 9557943Seric 9658680Seric /* we must have an id to remove disk files */ 9757943Seric if (id == NULL) 9858680Seric return; 9957943Seric 1009536Seric #ifdef LOG 10165089Seric if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags)) 10265089Seric logsender(e, NULL); 10358020Seric if (LogLevel > 84) 10465089Seric syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=0x%x, pid=%d", 10557943Seric id, e->e_flags, getpid()); 10656795Seric #endif /* LOG */ 10765089Seric e->e_flags &= ~EF_LOGSENDER; 1089536Seric 10963753Seric /* post statistics */ 11063753Seric poststats(StatFile); 11163753Seric 1129536Seric /* 1139536Seric ** Extract state information from dregs of send list. 1149536Seric */ 1159536Seric 11664743Seric e->e_flags &= ~EF_QUEUERUN; 117*67880Seric return_no = return_yes = FALSE; 1189536Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1199536Seric { 1209536Seric if (bitset(QQUEUEUP, q->q_flags)) 1219536Seric queueit = TRUE; 122*67880Seric 123*67880Seric /* see if a notification is needed */ 124*67880Seric if (bitset(QBADADDR, q->q_flags) && 125*67880Seric bitset(QPINGONFAILURE, q->q_flags)) 12663839Seric { 127*67880Seric failure_return = TRUE; 12863839Seric if (q->q_owner == NULL && 12963839Seric strcmp(e->e_from.q_paddr, "<>") != 0) 13063839Seric (void) sendtolist(e->e_from.q_paddr, NULL, 13163839Seric &e->e_errorqueue, e); 13263839Seric } 133*67880Seric else if (bitset(QSENT, q->q_flags) && 134*67880Seric bitnset(M_LOCALMAILER, q->q_mailer->m_flags) && 135*67880Seric bitset(QPINGONSUCCESS, q->q_flags)) 136*67880Seric { 137*67880Seric success_return = TRUE; 138*67880Seric } 139*67880Seric else 140*67880Seric continue; 141*67880Seric 142*67880Seric /* common code for error returns and return receipts */ 143*67880Seric 144*67880Seric /* test for returning the body */ 145*67880Seric if (!bitset(QHASRETPARAM, q->q_flags)) 146*67880Seric { 147*67880Seric if (!bitset(EF_NORETURN, e->e_flags)) 148*67880Seric return_yes = TRUE; 149*67880Seric } 150*67880Seric else if (bitset(QNOBODYRETURN, q->q_flags)) 151*67880Seric return_no = TRUE; 152*67880Seric else 153*67880Seric return_yes = TRUE; 1549536Seric } 155*67880Seric if (return_no && !return_yes) 156*67880Seric e->e_flags |= EF_NORETURN; 1579536Seric 1589536Seric /* 15963753Seric ** See if the message timed out. 16063753Seric */ 16163753Seric 16263753Seric if (!queueit) 16363753Seric /* nothing to do */ ; 16467730Seric else if (curtime() > e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass]) 16563753Seric { 16663839Seric (void) sprintf(buf, "Cannot send message for %s", 16767730Seric pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE)); 16863839Seric if (e->e_message != NULL) 16963839Seric free(e->e_message); 17063839Seric e->e_message = newstr(buf); 17163839Seric message(buf); 17263839Seric e->e_flags |= EF_CLRQUEUE; 173*67880Seric failure_return = TRUE; 17463787Seric fprintf(e->e_xfp, "Message could not be delivered for %s\n", 17567730Seric pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE)); 17663787Seric fprintf(e->e_xfp, "Message will be deleted from queue\n"); 17763787Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 17863787Seric { 17963787Seric if (bitset(QQUEUEUP, q->q_flags)) 18063787Seric q->q_flags |= QBADADDR; 18163787Seric } 18263753Seric } 18367730Seric else if (TimeOuts.to_q_warning[e->e_timeoutclass] > 0 && 18467730Seric curtime() > e->e_ctime + TimeOuts.to_q_warning[e->e_timeoutclass]) 18563753Seric { 18663753Seric if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) && 18763753Seric e->e_class >= 0 && 18867691Seric strcmp(e->e_from.q_paddr, "<>") != 0 && 18967691Seric strncasecmp(e->e_from.q_paddr, "owner-", 6) != 0 && 19067691Seric (strlen(e->e_from.q_paddr) <= 8 || 19167691Seric strcasecmp(&e->e_from.q_paddr[strlen(e->e_from.q_paddr) - 8], "-request") != 0)) 19263753Seric { 19363753Seric (void) sprintf(buf, 19467261Seric "Warning: cannot send message for %s", 19567730Seric pintvl(TimeOuts.to_q_warning[e->e_timeoutclass], FALSE)); 19663753Seric if (e->e_message != NULL) 19763753Seric free(e->e_message); 19863753Seric e->e_message = newstr(buf); 19963753Seric message(buf); 20063839Seric e->e_flags |= EF_WARNING; 201*67880Seric failure_return = TRUE; 20263753Seric } 20363753Seric fprintf(e->e_xfp, 20463753Seric "Warning: message still undelivered after %s\n", 20567730Seric pintvl(TimeOuts.to_q_warning[e->e_timeoutclass], FALSE)); 20663753Seric fprintf(e->e_xfp, "Will keep trying until message is %s old\n", 20767730Seric pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE)); 20863787Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 20963787Seric { 21063787Seric if (bitset(QQUEUEUP, q->q_flags)) 21163787Seric q->q_flags |= QREPORT; 21263787Seric } 21363753Seric } 21463753Seric 215*67880Seric if (tTd(50, 2)) 216*67880Seric printf("failure_return=%d success_return=%d queueit=%d\n", 217*67880Seric failure_return, success_return, queueit); 218*67880Seric 21963753Seric /* 2209536Seric ** Send back return receipts as requested. 2219536Seric */ 2229536Seric 223*67880Seric /* 22466783Seric if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags) 22566783Seric && !bitset(PRIV_NORECEIPTS, PrivacyFlags)) 226*67880Seric */ 227*67880Seric if (e->e_receiptto == NULL) 228*67880Seric e->e_receiptto = e->e_from.q_paddr; 229*67880Seric if (success_return && strcmp(e->e_receiptto, "<>") != 0) 2309536Seric { 23110844Seric auto ADDRESS *rlist = NULL; 2329536Seric 233*67880Seric e->e_flags |= EF_SENDRECEIPT; 23464284Seric (void) sendtolist(e->e_receiptto, NULLADDR, &rlist, e); 23555012Seric (void) returntosender("Return receipt", rlist, FALSE, e); 2369536Seric } 23767682Seric e->e_flags &= ~EF_SENDRECEIPT; 2389536Seric 2399536Seric /* 2409536Seric ** Arrange to send error messages if there are fatal errors. 2419536Seric */ 2429536Seric 243*67880Seric if (failure_return && e->e_errormode != EM_QUIET) 2449536Seric savemail(e); 2459536Seric 2469536Seric /* 24763849Seric ** Arrange to send warning messages to postmaster as requested. 24863849Seric */ 24963849Seric 25063849Seric if (bitset(EF_PM_NOTIFY, e->e_flags) && PostMasterCopy != NULL && 25164363Seric !bitset(EF_RESPONSE, e->e_flags) && e->e_class >= 0) 25263849Seric { 25363849Seric auto ADDRESS *rlist = NULL; 25463849Seric 25564284Seric (void) sendtolist(PostMasterCopy, NULLADDR, &rlist, e); 25663849Seric (void) returntosender(e->e_message, rlist, FALSE, e); 25763849Seric } 25863849Seric 25963849Seric /* 2609536Seric ** Instantiate or deinstantiate the queue. 2619536Seric */ 2629536Seric 2639536Seric if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 2649536Seric bitset(EF_CLRQUEUE, e->e_flags)) 2659536Seric { 26664307Seric if (tTd(50, 1)) 267*67880Seric printf("\n===== Dropping [dq]f%s (queueit=%d, e_flags=%x) =====\n\n", 268*67880Seric e->e_id, queueit, e->e_flags); 26923497Seric if (e->e_df != NULL) 27023497Seric xunlink(e->e_df); 2719536Seric xunlink(queuename(e, 'q')); 27263839Seric 27363839Seric #ifdef LOG 27463839Seric if (LogLevel > 10) 27563839Seric syslog(LOG_INFO, "%s: done", id); 27663839Seric #endif 2779536Seric } 2789536Seric else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 27910754Seric { 28010754Seric #ifdef QUEUE 28164307Seric queueup(e, bitset(EF_KEEPQUEUE, e->e_flags), FALSE); 28256795Seric #else /* QUEUE */ 28358151Seric syserr("554 dropenvelope: queueup"); 28456795Seric #endif /* QUEUE */ 28510754Seric } 2869536Seric 2879536Seric /* now unlock the job */ 28810196Seric closexscript(e); 2899536Seric unlockqueue(e); 2909536Seric 2919536Seric /* make sure that this envelope is marked unused */ 29224944Seric if (e->e_dfp != NULL) 29358680Seric (void) xfclose(e->e_dfp, "dropenvelope", e->e_df); 29410196Seric e->e_dfp = NULL; 29558680Seric e->e_id = e->e_df = NULL; 2969536Seric } 2979536Seric /* 2989536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 2999536Seric ** 3009536Seric ** This is normally used by a child process to get a clean 3019536Seric ** envelope without disturbing the parent. 3029536Seric ** 3039536Seric ** Parameters: 3049536Seric ** e -- the envelope to clear. 30525611Seric ** fullclear - if set, the current envelope is total 30625611Seric ** garbage and should be ignored; otherwise, 30725611Seric ** release any resources it may indicate. 3089536Seric ** 3099536Seric ** Returns: 3109536Seric ** none. 3119536Seric ** 3129536Seric ** Side Effects: 3139536Seric ** Closes files associated with the envelope. 3149536Seric ** Marks the envelope as unallocated. 3159536Seric */ 3169536Seric 31760494Seric void 31825611Seric clearenvelope(e, fullclear) 3199536Seric register ENVELOPE *e; 32025611Seric bool fullclear; 3219536Seric { 32225514Seric register HDR *bh; 32325514Seric register HDR **nhp; 32425514Seric extern ENVELOPE BlankEnvelope; 32525514Seric 32625611Seric if (!fullclear) 32725611Seric { 32825611Seric /* clear out any file information */ 32925611Seric if (e->e_xfp != NULL) 33058680Seric (void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id); 33125611Seric if (e->e_dfp != NULL) 33258680Seric (void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_df); 33358680Seric e->e_xfp = e->e_dfp = NULL; 33425611Seric } 3359536Seric 33624961Seric /* now clear out the data */ 33724965Seric STRUCTCOPY(BlankEnvelope, *e); 33859698Seric if (Verbose) 33959698Seric e->e_sendmode = SM_DELIVER; 34025514Seric bh = BlankEnvelope.e_header; 34125514Seric nhp = &e->e_header; 34225514Seric while (bh != NULL) 34325514Seric { 34425514Seric *nhp = (HDR *) xalloc(sizeof *bh); 34525514Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 34625514Seric bh = bh->h_link; 34725514Seric nhp = &(*nhp)->h_link; 34825514Seric } 3499536Seric } 3509536Seric /* 3519536Seric ** INITSYS -- initialize instantiation of system 3529536Seric ** 3539536Seric ** In Daemon mode, this is done in the child. 3549536Seric ** 3559536Seric ** Parameters: 3569536Seric ** none. 3579536Seric ** 3589536Seric ** Returns: 3599536Seric ** none. 3609536Seric ** 3619536Seric ** Side Effects: 3629536Seric ** Initializes the system macros, some global variables, 3639536Seric ** etc. In particular, the current time in various 3649536Seric ** forms is set. 3659536Seric */ 3669536Seric 36760494Seric void 36855012Seric initsys(e) 36955012Seric register ENVELOPE *e; 3709536Seric { 37164768Seric char cbuf[5]; /* holds hop count */ 37264768Seric char pbuf[10]; /* holds pid */ 37322963Smiriam #ifdef TTYNAME 37459304Seric static char ybuf[60]; /* holds tty id */ 3759536Seric register char *p; 37656795Seric #endif /* TTYNAME */ 3779536Seric extern char *ttyname(); 37860494Seric extern void settime(); 3799536Seric extern char Version[]; 3809536Seric 3819536Seric /* 3829536Seric ** Give this envelope a reality. 3839536Seric ** I.e., an id, a transcript, and a creation time. 3849536Seric */ 3859536Seric 38655012Seric openxscript(e); 38755012Seric e->e_ctime = curtime(); 3889536Seric 3899536Seric /* 3909536Seric ** Set OutChannel to something useful if stdout isn't it. 3919536Seric ** This arranges that any extra stuff the mailer produces 3929536Seric ** gets sent back to the user on error (because it is 3939536Seric ** tucked away in the transcript). 3949536Seric */ 3959536Seric 39664760Seric if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) && 39758737Seric e->e_xfp != NULL) 39855012Seric OutChannel = e->e_xfp; 3999536Seric 4009536Seric /* 4019536Seric ** Set up some basic system macros. 4029536Seric */ 4039536Seric 4049536Seric /* process id */ 4059536Seric (void) sprintf(pbuf, "%d", getpid()); 40664768Seric define('p', newstr(pbuf), e); 4079536Seric 4089536Seric /* hop count */ 40955012Seric (void) sprintf(cbuf, "%d", e->e_hopcount); 41064768Seric define('c', newstr(cbuf), e); 4119536Seric 4129536Seric /* time as integer, unix time, arpa time */ 41355012Seric settime(e); 4149536Seric 41517472Seric #ifdef TTYNAME 4169536Seric /* tty name */ 41755012Seric if (macvalue('y', e) == NULL) 4189536Seric { 4199536Seric p = ttyname(2); 4209536Seric if (p != NULL) 4219536Seric { 42256795Seric if (strrchr(p, '/') != NULL) 42356795Seric p = strrchr(p, '/') + 1; 4249536Seric (void) strcpy(ybuf, p); 42555012Seric define('y', ybuf, e); 4269536Seric } 4279536Seric } 42856795Seric #endif /* TTYNAME */ 4299536Seric } 4309536Seric /* 43111932Seric ** SETTIME -- set the current time. 43211932Seric ** 43311932Seric ** Parameters: 43411932Seric ** none. 43511932Seric ** 43611932Seric ** Returns: 43711932Seric ** none. 43811932Seric ** 43911932Seric ** Side Effects: 44011932Seric ** Sets the various time macros -- $a, $b, $d, $t. 44111932Seric */ 44211932Seric 44360494Seric void 44455012Seric settime(e) 44555012Seric register ENVELOPE *e; 44611932Seric { 44711932Seric register char *p; 44811932Seric auto time_t now; 44964768Seric char tbuf[20]; /* holds "current" time */ 45064768Seric char dbuf[30]; /* holds ctime(tbuf) */ 45111932Seric register struct tm *tm; 45211932Seric extern char *arpadate(); 45311932Seric extern struct tm *gmtime(); 45411932Seric 45511932Seric now = curtime(); 45611932Seric tm = gmtime(&now); 45757014Seric (void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900, 45857014Seric tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min); 45964768Seric define('t', newstr(tbuf), e); 46011932Seric (void) strcpy(dbuf, ctime(&now)); 46158131Seric p = strchr(dbuf, '\n'); 46258131Seric if (p != NULL) 46358131Seric *p = '\0'; 46464768Seric define('d', newstr(dbuf), e); 46564086Seric p = arpadate(dbuf); 46664086Seric p = newstr(p); 46755012Seric if (macvalue('a', e) == NULL) 46855012Seric define('a', p, e); 46955012Seric define('b', p, e); 47011932Seric } 47111932Seric /* 4729536Seric ** OPENXSCRIPT -- Open transcript file 4739536Seric ** 4749536Seric ** Creates a transcript file for possible eventual mailing or 4759536Seric ** sending back. 4769536Seric ** 4779536Seric ** Parameters: 4789536Seric ** e -- the envelope to create the transcript in/for. 4799536Seric ** 4809536Seric ** Returns: 4819536Seric ** none 4829536Seric ** 4839536Seric ** Side Effects: 4849536Seric ** Creates the transcript file. 4859536Seric */ 4869536Seric 48758803Seric #ifndef O_APPEND 48858803Seric #define O_APPEND 0 48958803Seric #endif 49058803Seric 49160494Seric void 4929536Seric openxscript(e) 4939536Seric register ENVELOPE *e; 4949536Seric { 4959536Seric register char *p; 49640933Srick int fd; 4979536Seric 4989536Seric if (e->e_xfp != NULL) 4999536Seric return; 5009536Seric p = queuename(e, 'x'); 50158803Seric fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644); 50240933Srick if (fd < 0) 50363753Seric { 50463753Seric syserr("Can't create transcript file %s", p); 50563753Seric fd = open("/dev/null", O_WRONLY, 0644); 50663753Seric if (fd < 0) 50763753Seric syserr("!Can't open /dev/null"); 50863753Seric } 50963753Seric e->e_xfp = fdopen(fd, "w"); 51064724Seric if (e->e_xfp == NULL) 51164724Seric { 51264724Seric syserr("!Can't create transcript stream %s", p); 51364724Seric } 51464743Seric if (tTd(46, 9)) 51564743Seric { 51664743Seric printf("openxscript(%s):\n ", p); 51764743Seric dumpfd(fileno(e->e_xfp), TRUE, FALSE); 51864743Seric } 5199536Seric } 5209536Seric /* 52110196Seric ** CLOSEXSCRIPT -- close the transcript file. 52210196Seric ** 52310196Seric ** Parameters: 52410196Seric ** e -- the envelope containing the transcript to close. 52510196Seric ** 52610196Seric ** Returns: 52710196Seric ** none. 52810196Seric ** 52910196Seric ** Side Effects: 53010196Seric ** none. 53110196Seric */ 53210196Seric 53360494Seric void 53410196Seric closexscript(e) 53510196Seric register ENVELOPE *e; 53610196Seric { 53710196Seric if (e->e_xfp == NULL) 53810196Seric return; 53958680Seric (void) xfclose(e->e_xfp, "closexscript", e->e_id); 54010196Seric e->e_xfp = NULL; 54110196Seric } 54210196Seric /* 5439536Seric ** SETSENDER -- set the person who this message is from 5449536Seric ** 5459536Seric ** Under certain circumstances allow the user to say who 5469536Seric ** s/he is (using -f or -r). These are: 5479536Seric ** 1. The user's uid is zero (root). 5489536Seric ** 2. The user's login name is in an approved list (typically 5499536Seric ** from a network server). 5509536Seric ** 3. The address the user is trying to claim has a 5519536Seric ** "!" character in it (since #2 doesn't do it for 5529536Seric ** us if we are dialing out for UUCP). 5539536Seric ** A better check to replace #3 would be if the 5549536Seric ** effective uid is "UUCP" -- this would require me 5559536Seric ** to rewrite getpwent to "grab" uucp as it went by, 5569536Seric ** make getname more nasty, do another passwd file 5579536Seric ** scan, or compile the UID of "UUCP" into the code, 5589536Seric ** all of which are reprehensible. 5599536Seric ** 5609536Seric ** Assuming all of these fail, we figure out something 5619536Seric ** ourselves. 5629536Seric ** 5639536Seric ** Parameters: 5649536Seric ** from -- the person we would like to believe this message 5659536Seric ** is from, as specified on the command line. 56653182Seric ** e -- the envelope in which we would like the sender set. 56758333Seric ** delimptr -- if non-NULL, set to the location of the 56858333Seric ** trailing delimiter. 56958704Seric ** internal -- set if this address is coming from an internal 57058704Seric ** source such as an owner alias. 5719536Seric ** 5729536Seric ** Returns: 57358704Seric ** none. 5749536Seric ** 5759536Seric ** Side Effects: 5769536Seric ** sets sendmail's notion of who the from person is. 5779536Seric */ 5789536Seric 57960494Seric void 58058704Seric setsender(from, e, delimptr, internal) 5819536Seric char *from; 58253182Seric register ENVELOPE *e; 58358333Seric char **delimptr; 58458704Seric bool internal; 5859536Seric { 5869536Seric register char **pvp; 5879536Seric char *realname = NULL; 58818665Seric register struct passwd *pw; 58958727Seric char delimchar; 59064147Seric char *bp; 59164147Seric char buf[MAXNAME + 2]; 59216913Seric char pvpbuf[PSBUFSIZE]; 59318665Seric extern struct passwd *getpwnam(); 5949536Seric extern char *FullName; 5959536Seric 5969536Seric if (tTd(45, 1)) 59714786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 5989536Seric 5999536Seric /* 6009536Seric ** Figure out the real user executing us. 6019536Seric ** Username can return errno != 0 on non-errors. 6029536Seric */ 6039536Seric 60465580Seric if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP || 60565983Seric OpMode == MD_ARPAFTP || OpMode == MD_DAEMON) 6069536Seric realname = from; 6079536Seric if (realname == NULL || realname[0] == '\0') 6089536Seric realname = username(); 6099536Seric 61059027Seric if (ConfigLevel < 2) 61159027Seric SuprErrs = TRUE; 61259027Seric 61358727Seric delimchar = internal ? '\0' : ' '; 61464793Seric e->e_from.q_flags = QBADADDR; 61558333Seric if (from == NULL || 61664284Seric parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR, 61764793Seric delimchar, delimptr, e) == NULL || 61864793Seric bitset(QBADADDR, e->e_from.q_flags) || 61964793Seric e->e_from.q_mailer == ProgMailer || 62064793Seric e->e_from.q_mailer == FileMailer || 62164793Seric e->e_from.q_mailer == InclMailer) 6229536Seric { 62321750Seric /* log garbage addresses for traceback */ 62455173Seric # ifdef LOG 62558020Seric if (from != NULL && LogLevel > 2) 62621750Seric { 62758951Seric char *p; 62858951Seric char ebuf[MAXNAME * 2 + 2]; 62955173Seric 63058951Seric p = macvalue('_', e); 63158951Seric if (p == NULL) 63258951Seric { 63358951Seric char *host = RealHostName; 63458951Seric if (host == NULL) 63558951Seric host = MyHostName; 63658951Seric (void) sprintf(ebuf, "%s@%s", realname, host); 63758951Seric p = ebuf; 63858951Seric } 63955173Seric syslog(LOG_NOTICE, 64064793Seric "setsender: %s: invalid or unparseable, received from %s", 64165015Seric shortenstring(from, 83), p); 64255173Seric } 64356795Seric # endif /* LOG */ 64457589Seric if (from != NULL) 64564793Seric { 64664793Seric if (!bitset(QBADADDR, e->e_from.q_flags)) 64764793Seric { 64864793Seric /* it was a bogus mailer in the from addr */ 64964793Seric usrerr("553 Invalid sender address"); 65064793Seric } 65157589Seric SuprErrs = TRUE; 65264793Seric } 65357589Seric if (from == realname || 65464284Seric parseaddr(from = newstr(realname), &e->e_from, 65564284Seric RF_COPYALL|RF_SENDERADDR, ' ', NULL, e) == NULL) 65624944Seric { 65764793Seric char nbuf[100]; 65864793Seric 65957589Seric SuprErrs = TRUE; 66064808Seric expand("\201n", nbuf, &nbuf[sizeof nbuf], e); 66164793Seric if (parseaddr(from = newstr(nbuf), &e->e_from, 66264793Seric RF_COPYALL, ' ', NULL, e) == NULL && 66364793Seric parseaddr(from = "postmaster", &e->e_from, 66464793Seric RF_COPYALL, ' ', NULL, e) == NULL) 66558151Seric syserr("553 setsender: can't even parse postmaster!"); 66624944Seric } 6679536Seric } 6689536Seric else 6699536Seric FromFlag = TRUE; 67053182Seric e->e_from.q_flags |= QDONTSEND; 67157731Seric if (tTd(45, 5)) 67257731Seric { 67357731Seric printf("setsender: QDONTSEND "); 67457731Seric printaddr(&e->e_from, FALSE); 67557731Seric } 6769536Seric SuprErrs = FALSE; 6779536Seric 67867765Seric # ifdef USERDB 67967472Seric if (bitnset(M_CHECKUDB, e->e_from.q_mailer->m_flags)) 6809536Seric { 68153736Seric register char *p; 68253736Seric extern char *udbsender(); 68317472Seric 68467765Seric p = udbsender(e->e_from.q_user); 68567765Seric if (p != NULL) 68667765Seric from = p; 68767765Seric } 68867765Seric # endif /* USERDB */ 68967765Seric 69067765Seric if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags)) 69167765Seric { 69258704Seric if (!internal) 69358704Seric { 69467765Seric /* if the user already given fullname don't redefine */ 69558704Seric if (FullName == NULL) 69658704Seric FullName = macvalue('x', e); 69758704Seric if (FullName != NULL && FullName[0] == '\0') 69858704Seric FullName = NULL; 6999536Seric } 70053736Seric 70153736Seric if ((pw = getpwnam(e->e_from.q_user)) != NULL) 70253736Seric { 70353736Seric /* 70453736Seric ** Process passwd file entry. 70553736Seric */ 70653736Seric 70753736Seric /* extract home directory */ 70865822Seric if (strcmp(pw->pw_dir, "/") == 0) 70965822Seric e->e_from.q_home = newstr(""); 71065822Seric else 71165822Seric e->e_from.q_home = newstr(pw->pw_dir); 71253736Seric define('z', e->e_from.q_home, e); 71353736Seric 71453736Seric /* extract user and group id */ 71553736Seric e->e_from.q_uid = pw->pw_uid; 71653736Seric e->e_from.q_gid = pw->pw_gid; 71765023Seric e->e_from.q_flags |= QGOODUID; 71853736Seric 71953736Seric /* extract full name from passwd file */ 72053736Seric if (FullName == NULL && pw->pw_gecos != NULL && 72158704Seric strcmp(pw->pw_name, e->e_from.q_user) == 0 && 72258704Seric !internal) 72353736Seric { 72465015Seric buildfname(pw->pw_gecos, e->e_from.q_user, buf); 72553736Seric if (buf[0] != '\0') 72653736Seric FullName = newstr(buf); 72753736Seric } 72853736Seric } 72958704Seric if (FullName != NULL && !internal) 73053182Seric define('x', FullName, e); 7319536Seric } 73265580Seric else if (!internal && OpMode != MD_DAEMON) 73311625Seric { 73453182Seric if (e->e_from.q_home == NULL) 73565822Seric { 73653182Seric e->e_from.q_home = getenv("HOME"); 73766049Seric if (e->e_from.q_home != NULL && 73866049Seric strcmp(e->e_from.q_home, "/") == 0) 73965822Seric e->e_from.q_home++; 74065822Seric } 74163787Seric e->e_from.q_uid = RealUid; 74263787Seric e->e_from.q_gid = RealGid; 74365023Seric e->e_from.q_flags |= QGOODUID; 74411625Seric } 74511625Seric 7469536Seric /* 7479536Seric ** Rewrite the from person to dispose of possible implicit 7489536Seric ** links in the net. 7499536Seric */ 7509536Seric 75167765Seric pvp = prescan(from, delimchar, pvpbuf, sizeof pvpbuf, NULL); 7529536Seric if (pvp == NULL) 7539536Seric { 75458403Seric /* don't need to give error -- prescan did that already */ 75536233Skarels # ifdef LOG 75658020Seric if (LogLevel > 2) 75736233Skarels syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 75836233Skarels # endif 7599536Seric finis(); 7609536Seric } 76167765Seric /* 76265071Seric (void) rewrite(pvp, 3, 0, e); 76365071Seric (void) rewrite(pvp, 1, 0, e); 76465071Seric (void) rewrite(pvp, 4, 0, e); 76567765Seric */ 76664147Seric bp = buf + 1; 76764147Seric cataddr(pvp, NULL, bp, sizeof buf - 2, '\0'); 76867619Seric if (*bp == '@' && !bitnset(M_NOBRACKET, e->e_from.q_mailer->m_flags)) 76964147Seric { 77064147Seric /* heuristic: route-addr: add angle brackets */ 77164147Seric strcat(bp, ">"); 77264147Seric *--bp = '<'; 77364147Seric } 77464147Seric e->e_sender = newstr(bp); 77558704Seric define('f', e->e_sender, e); 7769536Seric 7779536Seric /* save the domain spec if this mailer wants it */ 77865584Seric if (e->e_from.q_mailer != NULL && 77953182Seric bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 7809536Seric { 7819536Seric extern char **copyplist(); 7829536Seric 7839536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 7849536Seric pvp++; 7859536Seric if (*pvp != NULL) 78653182Seric e->e_fromdomain = copyplist(pvp, TRUE); 7879536Seric } 7889536Seric } 789