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*66783Seric static char sccsid[] = "@(#)envelope.c 8.34 (Berkeley) 04/14/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; 7763839Seric bool saveit = bitset(EF_FATALERRS, e->e_flags); 789536Seric register ADDRESS *q; 7957943Seric char *id = e->e_id; 8063753Seric char buf[MAXLINE]; 819536Seric 829536Seric if (tTd(50, 1)) 839536Seric { 8458680Seric printf("dropenvelope %x: id=", e); 859536Seric xputs(e->e_id); 8665089Seric printf(", flags=0x%x\n", e->e_flags); 8763753Seric if (tTd(50, 10)) 8863753Seric { 8963753Seric printf("sendq="); 9063753Seric printaddr(e->e_sendqueue, TRUE); 9163753Seric } 929536Seric } 9357943Seric 9458680Seric /* we must have an id to remove disk files */ 9557943Seric if (id == NULL) 9658680Seric return; 9757943Seric 989536Seric #ifdef LOG 9965089Seric if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags)) 10065089Seric logsender(e, NULL); 10158020Seric if (LogLevel > 84) 10265089Seric syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=0x%x, pid=%d", 10357943Seric id, e->e_flags, getpid()); 10456795Seric #endif /* LOG */ 10565089Seric e->e_flags &= ~EF_LOGSENDER; 1069536Seric 10763753Seric /* post statistics */ 10863753Seric poststats(StatFile); 10963753Seric 1109536Seric /* 1119536Seric ** Extract state information from dregs of send list. 1129536Seric */ 1139536Seric 11464743Seric e->e_flags &= ~EF_QUEUERUN; 1159536Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1169536Seric { 1179536Seric if (bitset(QQUEUEUP, q->q_flags)) 1189536Seric queueit = TRUE; 11963839Seric if (!bitset(QDONTSEND, q->q_flags) && 12063839Seric bitset(QBADADDR, q->q_flags)) 12163839Seric { 12263839Seric if (q->q_owner == NULL && 12363839Seric strcmp(e->e_from.q_paddr, "<>") != 0) 12463839Seric (void) sendtolist(e->e_from.q_paddr, NULL, 12563839Seric &e->e_errorqueue, e); 12663839Seric } 1279536Seric } 1289536Seric 1299536Seric /* 13063753Seric ** See if the message timed out. 13163753Seric */ 13263753Seric 13363753Seric if (!queueit) 13463753Seric /* nothing to do */ ; 13563753Seric else if (curtime() > e->e_ctime + TimeOuts.to_q_return) 13663753Seric { 13763839Seric (void) sprintf(buf, "Cannot send message for %s", 13863839Seric pintvl(TimeOuts.to_q_return, FALSE)); 13963839Seric if (e->e_message != NULL) 14063839Seric free(e->e_message); 14163839Seric e->e_message = newstr(buf); 14263839Seric message(buf); 14363839Seric e->e_flags |= EF_CLRQUEUE; 14463839Seric saveit = TRUE; 14563787Seric fprintf(e->e_xfp, "Message could not be delivered for %s\n", 14663787Seric pintvl(TimeOuts.to_q_return, FALSE)); 14763787Seric fprintf(e->e_xfp, "Message will be deleted from queue\n"); 14863787Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 14963787Seric { 15063787Seric if (bitset(QQUEUEUP, q->q_flags)) 15163787Seric q->q_flags |= QBADADDR; 15263787Seric } 15363753Seric } 15463753Seric else if (TimeOuts.to_q_warning > 0 && 15563753Seric curtime() > e->e_ctime + TimeOuts.to_q_warning) 15663753Seric { 15763753Seric if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) && 15863753Seric e->e_class >= 0 && 15963753Seric strcmp(e->e_from.q_paddr, "<>") != 0) 16063753Seric { 16163753Seric (void) sprintf(buf, 16263753Seric "warning: cannot send message for %s", 16363753Seric pintvl(TimeOuts.to_q_warning, FALSE)); 16463753Seric if (e->e_message != NULL) 16563753Seric free(e->e_message); 16663753Seric e->e_message = newstr(buf); 16763753Seric message(buf); 16863839Seric e->e_flags |= EF_WARNING; 16963839Seric saveit = TRUE; 17063753Seric } 17163753Seric fprintf(e->e_xfp, 17263753Seric "Warning: message still undelivered after %s\n", 17363753Seric pintvl(TimeOuts.to_q_warning, FALSE)); 17463753Seric fprintf(e->e_xfp, "Will keep trying until message is %s old\n", 17563753Seric pintvl(TimeOuts.to_q_return, FALSE)); 17663787Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 17763787Seric { 17863787Seric if (bitset(QQUEUEUP, q->q_flags)) 17963787Seric q->q_flags |= QREPORT; 18063787Seric } 18163753Seric } 18263753Seric 18363753Seric /* 1849536Seric ** Send back return receipts as requested. 1859536Seric */ 1869536Seric 187*66783Seric if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags) 188*66783Seric && !bitset(PRIV_NORECEIPTS, PrivacyFlags)) 1899536Seric { 19010844Seric auto ADDRESS *rlist = NULL; 1919536Seric 19264284Seric (void) sendtolist(e->e_receiptto, NULLADDR, &rlist, e); 19355012Seric (void) returntosender("Return receipt", rlist, FALSE, e); 19465054Seric e->e_flags &= ~EF_SENDRECEIPT; 1959536Seric } 1969536Seric 1979536Seric /* 1989536Seric ** Arrange to send error messages if there are fatal errors. 1999536Seric */ 2009536Seric 20163839Seric if (saveit && e->e_errormode != EM_QUIET) 2029536Seric savemail(e); 2039536Seric 2049536Seric /* 20563849Seric ** Arrange to send warning messages to postmaster as requested. 20663849Seric */ 20763849Seric 20863849Seric if (bitset(EF_PM_NOTIFY, e->e_flags) && PostMasterCopy != NULL && 20964363Seric !bitset(EF_RESPONSE, e->e_flags) && e->e_class >= 0) 21063849Seric { 21163849Seric auto ADDRESS *rlist = NULL; 21263849Seric 21364284Seric (void) sendtolist(PostMasterCopy, NULLADDR, &rlist, e); 21463849Seric (void) returntosender(e->e_message, rlist, FALSE, e); 21563849Seric } 21663849Seric 21763849Seric /* 2189536Seric ** Instantiate or deinstantiate the queue. 2199536Seric */ 2209536Seric 2219536Seric if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 2229536Seric bitset(EF_CLRQUEUE, e->e_flags)) 2239536Seric { 22464307Seric if (tTd(50, 1)) 22564307Seric printf("\n===== Dropping [dq]f%s =====\n\n", e->e_id); 22623497Seric if (e->e_df != NULL) 22723497Seric xunlink(e->e_df); 2289536Seric xunlink(queuename(e, 'q')); 22963839Seric 23063839Seric #ifdef LOG 23163839Seric if (LogLevel > 10) 23263839Seric syslog(LOG_INFO, "%s: done", id); 23363839Seric #endif 2349536Seric } 2359536Seric else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 23610754Seric { 23710754Seric #ifdef QUEUE 23864307Seric queueup(e, bitset(EF_KEEPQUEUE, e->e_flags), FALSE); 23956795Seric #else /* QUEUE */ 24058151Seric syserr("554 dropenvelope: queueup"); 24156795Seric #endif /* QUEUE */ 24210754Seric } 2439536Seric 2449536Seric /* now unlock the job */ 24510196Seric closexscript(e); 2469536Seric unlockqueue(e); 2479536Seric 2489536Seric /* make sure that this envelope is marked unused */ 24924944Seric if (e->e_dfp != NULL) 25058680Seric (void) xfclose(e->e_dfp, "dropenvelope", e->e_df); 25110196Seric e->e_dfp = NULL; 25258680Seric e->e_id = e->e_df = NULL; 2539536Seric } 2549536Seric /* 2559536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 2569536Seric ** 2579536Seric ** This is normally used by a child process to get a clean 2589536Seric ** envelope without disturbing the parent. 2599536Seric ** 2609536Seric ** Parameters: 2619536Seric ** e -- the envelope to clear. 26225611Seric ** fullclear - if set, the current envelope is total 26325611Seric ** garbage and should be ignored; otherwise, 26425611Seric ** release any resources it may indicate. 2659536Seric ** 2669536Seric ** Returns: 2679536Seric ** none. 2689536Seric ** 2699536Seric ** Side Effects: 2709536Seric ** Closes files associated with the envelope. 2719536Seric ** Marks the envelope as unallocated. 2729536Seric */ 2739536Seric 27460494Seric void 27525611Seric clearenvelope(e, fullclear) 2769536Seric register ENVELOPE *e; 27725611Seric bool fullclear; 2789536Seric { 27925514Seric register HDR *bh; 28025514Seric register HDR **nhp; 28125514Seric extern ENVELOPE BlankEnvelope; 28225514Seric 28325611Seric if (!fullclear) 28425611Seric { 28525611Seric /* clear out any file information */ 28625611Seric if (e->e_xfp != NULL) 28758680Seric (void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id); 28825611Seric if (e->e_dfp != NULL) 28958680Seric (void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_df); 29058680Seric e->e_xfp = e->e_dfp = NULL; 29125611Seric } 2929536Seric 29324961Seric /* now clear out the data */ 29424965Seric STRUCTCOPY(BlankEnvelope, *e); 29559698Seric if (Verbose) 29659698Seric e->e_sendmode = SM_DELIVER; 29725514Seric bh = BlankEnvelope.e_header; 29825514Seric nhp = &e->e_header; 29925514Seric while (bh != NULL) 30025514Seric { 30125514Seric *nhp = (HDR *) xalloc(sizeof *bh); 30225514Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 30325514Seric bh = bh->h_link; 30425514Seric nhp = &(*nhp)->h_link; 30525514Seric } 3069536Seric } 3079536Seric /* 3089536Seric ** INITSYS -- initialize instantiation of system 3099536Seric ** 3109536Seric ** In Daemon mode, this is done in the child. 3119536Seric ** 3129536Seric ** Parameters: 3139536Seric ** none. 3149536Seric ** 3159536Seric ** Returns: 3169536Seric ** none. 3179536Seric ** 3189536Seric ** Side Effects: 3199536Seric ** Initializes the system macros, some global variables, 3209536Seric ** etc. In particular, the current time in various 3219536Seric ** forms is set. 3229536Seric */ 3239536Seric 32460494Seric void 32555012Seric initsys(e) 32655012Seric register ENVELOPE *e; 3279536Seric { 32864768Seric char cbuf[5]; /* holds hop count */ 32964768Seric char pbuf[10]; /* holds pid */ 33022963Smiriam #ifdef TTYNAME 33159304Seric static char ybuf[60]; /* holds tty id */ 3329536Seric register char *p; 33356795Seric #endif /* TTYNAME */ 3349536Seric extern char *ttyname(); 33560494Seric extern void settime(); 3369536Seric extern char Version[]; 3379536Seric 3389536Seric /* 3399536Seric ** Give this envelope a reality. 3409536Seric ** I.e., an id, a transcript, and a creation time. 3419536Seric */ 3429536Seric 34355012Seric openxscript(e); 34455012Seric e->e_ctime = curtime(); 3459536Seric 3469536Seric /* 3479536Seric ** Set OutChannel to something useful if stdout isn't it. 3489536Seric ** This arranges that any extra stuff the mailer produces 3499536Seric ** gets sent back to the user on error (because it is 3509536Seric ** tucked away in the transcript). 3519536Seric */ 3529536Seric 35364760Seric if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) && 35458737Seric e->e_xfp != NULL) 35555012Seric OutChannel = e->e_xfp; 3569536Seric 3579536Seric /* 3589536Seric ** Set up some basic system macros. 3599536Seric */ 3609536Seric 3619536Seric /* process id */ 3629536Seric (void) sprintf(pbuf, "%d", getpid()); 36364768Seric define('p', newstr(pbuf), e); 3649536Seric 3659536Seric /* hop count */ 36655012Seric (void) sprintf(cbuf, "%d", e->e_hopcount); 36764768Seric define('c', newstr(cbuf), e); 3689536Seric 3699536Seric /* time as integer, unix time, arpa time */ 37055012Seric settime(e); 3719536Seric 37217472Seric #ifdef TTYNAME 3739536Seric /* tty name */ 37455012Seric if (macvalue('y', e) == NULL) 3759536Seric { 3769536Seric p = ttyname(2); 3779536Seric if (p != NULL) 3789536Seric { 37956795Seric if (strrchr(p, '/') != NULL) 38056795Seric p = strrchr(p, '/') + 1; 3819536Seric (void) strcpy(ybuf, p); 38255012Seric define('y', ybuf, e); 3839536Seric } 3849536Seric } 38556795Seric #endif /* TTYNAME */ 3869536Seric } 3879536Seric /* 38811932Seric ** SETTIME -- set the current time. 38911932Seric ** 39011932Seric ** Parameters: 39111932Seric ** none. 39211932Seric ** 39311932Seric ** Returns: 39411932Seric ** none. 39511932Seric ** 39611932Seric ** Side Effects: 39711932Seric ** Sets the various time macros -- $a, $b, $d, $t. 39811932Seric */ 39911932Seric 40060494Seric void 40155012Seric settime(e) 40255012Seric register ENVELOPE *e; 40311932Seric { 40411932Seric register char *p; 40511932Seric auto time_t now; 40664768Seric char tbuf[20]; /* holds "current" time */ 40764768Seric char dbuf[30]; /* holds ctime(tbuf) */ 40811932Seric register struct tm *tm; 40911932Seric extern char *arpadate(); 41011932Seric extern struct tm *gmtime(); 41111932Seric 41211932Seric now = curtime(); 41311932Seric tm = gmtime(&now); 41457014Seric (void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900, 41557014Seric tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min); 41664768Seric define('t', newstr(tbuf), e); 41711932Seric (void) strcpy(dbuf, ctime(&now)); 41858131Seric p = strchr(dbuf, '\n'); 41958131Seric if (p != NULL) 42058131Seric *p = '\0'; 42164768Seric define('d', newstr(dbuf), e); 42264086Seric p = arpadate(dbuf); 42364086Seric p = newstr(p); 42455012Seric if (macvalue('a', e) == NULL) 42555012Seric define('a', p, e); 42655012Seric define('b', p, e); 42711932Seric } 42811932Seric /* 4299536Seric ** OPENXSCRIPT -- Open transcript file 4309536Seric ** 4319536Seric ** Creates a transcript file for possible eventual mailing or 4329536Seric ** sending back. 4339536Seric ** 4349536Seric ** Parameters: 4359536Seric ** e -- the envelope to create the transcript in/for. 4369536Seric ** 4379536Seric ** Returns: 4389536Seric ** none 4399536Seric ** 4409536Seric ** Side Effects: 4419536Seric ** Creates the transcript file. 4429536Seric */ 4439536Seric 44458803Seric #ifndef O_APPEND 44558803Seric #define O_APPEND 0 44658803Seric #endif 44758803Seric 44860494Seric void 4499536Seric openxscript(e) 4509536Seric register ENVELOPE *e; 4519536Seric { 4529536Seric register char *p; 45340933Srick int fd; 4549536Seric 4559536Seric if (e->e_xfp != NULL) 4569536Seric return; 4579536Seric p = queuename(e, 'x'); 45858803Seric fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644); 45940933Srick if (fd < 0) 46063753Seric { 46163753Seric syserr("Can't create transcript file %s", p); 46263753Seric fd = open("/dev/null", O_WRONLY, 0644); 46363753Seric if (fd < 0) 46463753Seric syserr("!Can't open /dev/null"); 46563753Seric } 46663753Seric e->e_xfp = fdopen(fd, "w"); 46764724Seric if (e->e_xfp == NULL) 46864724Seric { 46964724Seric syserr("!Can't create transcript stream %s", p); 47064724Seric } 47164743Seric if (tTd(46, 9)) 47264743Seric { 47364743Seric printf("openxscript(%s):\n ", p); 47464743Seric dumpfd(fileno(e->e_xfp), TRUE, FALSE); 47564743Seric } 4769536Seric } 4779536Seric /* 47810196Seric ** CLOSEXSCRIPT -- close the transcript file. 47910196Seric ** 48010196Seric ** Parameters: 48110196Seric ** e -- the envelope containing the transcript to close. 48210196Seric ** 48310196Seric ** Returns: 48410196Seric ** none. 48510196Seric ** 48610196Seric ** Side Effects: 48710196Seric ** none. 48810196Seric */ 48910196Seric 49060494Seric void 49110196Seric closexscript(e) 49210196Seric register ENVELOPE *e; 49310196Seric { 49410196Seric if (e->e_xfp == NULL) 49510196Seric return; 49658680Seric (void) xfclose(e->e_xfp, "closexscript", e->e_id); 49710196Seric e->e_xfp = NULL; 49810196Seric } 49910196Seric /* 5009536Seric ** SETSENDER -- set the person who this message is from 5019536Seric ** 5029536Seric ** Under certain circumstances allow the user to say who 5039536Seric ** s/he is (using -f or -r). These are: 5049536Seric ** 1. The user's uid is zero (root). 5059536Seric ** 2. The user's login name is in an approved list (typically 5069536Seric ** from a network server). 5079536Seric ** 3. The address the user is trying to claim has a 5089536Seric ** "!" character in it (since #2 doesn't do it for 5099536Seric ** us if we are dialing out for UUCP). 5109536Seric ** A better check to replace #3 would be if the 5119536Seric ** effective uid is "UUCP" -- this would require me 5129536Seric ** to rewrite getpwent to "grab" uucp as it went by, 5139536Seric ** make getname more nasty, do another passwd file 5149536Seric ** scan, or compile the UID of "UUCP" into the code, 5159536Seric ** all of which are reprehensible. 5169536Seric ** 5179536Seric ** Assuming all of these fail, we figure out something 5189536Seric ** ourselves. 5199536Seric ** 5209536Seric ** Parameters: 5219536Seric ** from -- the person we would like to believe this message 5229536Seric ** is from, as specified on the command line. 52353182Seric ** e -- the envelope in which we would like the sender set. 52458333Seric ** delimptr -- if non-NULL, set to the location of the 52558333Seric ** trailing delimiter. 52658704Seric ** internal -- set if this address is coming from an internal 52758704Seric ** source such as an owner alias. 5289536Seric ** 5299536Seric ** Returns: 53058704Seric ** none. 5319536Seric ** 5329536Seric ** Side Effects: 5339536Seric ** sets sendmail's notion of who the from person is. 5349536Seric */ 5359536Seric 53660494Seric void 53758704Seric setsender(from, e, delimptr, internal) 5389536Seric char *from; 53953182Seric register ENVELOPE *e; 54058333Seric char **delimptr; 54158704Seric bool internal; 5429536Seric { 5439536Seric register char **pvp; 5449536Seric char *realname = NULL; 54518665Seric register struct passwd *pw; 54658727Seric char delimchar; 54764147Seric char *bp; 54864147Seric char buf[MAXNAME + 2]; 54916913Seric char pvpbuf[PSBUFSIZE]; 55018665Seric extern struct passwd *getpwnam(); 5519536Seric extern char *FullName; 5529536Seric 5539536Seric if (tTd(45, 1)) 55414786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 5559536Seric 5569536Seric /* 5579536Seric ** Figure out the real user executing us. 5589536Seric ** Username can return errno != 0 on non-errors. 5599536Seric */ 5609536Seric 56165580Seric if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP || 56265983Seric OpMode == MD_ARPAFTP || OpMode == MD_DAEMON) 5639536Seric realname = from; 5649536Seric if (realname == NULL || realname[0] == '\0') 5659536Seric realname = username(); 5669536Seric 56759027Seric if (ConfigLevel < 2) 56859027Seric SuprErrs = TRUE; 56959027Seric 57058727Seric delimchar = internal ? '\0' : ' '; 57164793Seric e->e_from.q_flags = QBADADDR; 57258333Seric if (from == NULL || 57364284Seric parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR, 57464793Seric delimchar, delimptr, e) == NULL || 57564793Seric bitset(QBADADDR, e->e_from.q_flags) || 57664793Seric e->e_from.q_mailer == ProgMailer || 57764793Seric e->e_from.q_mailer == FileMailer || 57864793Seric e->e_from.q_mailer == InclMailer) 5799536Seric { 58021750Seric /* log garbage addresses for traceback */ 58155173Seric # ifdef LOG 58258020Seric if (from != NULL && LogLevel > 2) 58321750Seric { 58458951Seric char *p; 58558951Seric char ebuf[MAXNAME * 2 + 2]; 58655173Seric 58758951Seric p = macvalue('_', e); 58858951Seric if (p == NULL) 58958951Seric { 59058951Seric char *host = RealHostName; 59158951Seric if (host == NULL) 59258951Seric host = MyHostName; 59358951Seric (void) sprintf(ebuf, "%s@%s", realname, host); 59458951Seric p = ebuf; 59558951Seric } 59655173Seric syslog(LOG_NOTICE, 59764793Seric "setsender: %s: invalid or unparseable, received from %s", 59865015Seric shortenstring(from, 83), p); 59955173Seric } 60056795Seric # endif /* LOG */ 60157589Seric if (from != NULL) 60264793Seric { 60364793Seric if (!bitset(QBADADDR, e->e_from.q_flags)) 60464793Seric { 60564793Seric /* it was a bogus mailer in the from addr */ 60664793Seric usrerr("553 Invalid sender address"); 60764793Seric } 60857589Seric SuprErrs = TRUE; 60964793Seric } 61057589Seric if (from == realname || 61164284Seric parseaddr(from = newstr(realname), &e->e_from, 61264284Seric RF_COPYALL|RF_SENDERADDR, ' ', NULL, e) == NULL) 61324944Seric { 61464793Seric char nbuf[100]; 61564793Seric 61657589Seric SuprErrs = TRUE; 61764808Seric expand("\201n", nbuf, &nbuf[sizeof nbuf], e); 61864793Seric if (parseaddr(from = newstr(nbuf), &e->e_from, 61964793Seric RF_COPYALL, ' ', NULL, e) == NULL && 62064793Seric parseaddr(from = "postmaster", &e->e_from, 62164793Seric RF_COPYALL, ' ', NULL, e) == NULL) 62258151Seric syserr("553 setsender: can't even parse postmaster!"); 62324944Seric } 6249536Seric } 6259536Seric else 6269536Seric FromFlag = TRUE; 62753182Seric e->e_from.q_flags |= QDONTSEND; 62857731Seric if (tTd(45, 5)) 62957731Seric { 63057731Seric printf("setsender: QDONTSEND "); 63157731Seric printaddr(&e->e_from, FALSE); 63257731Seric } 6339536Seric SuprErrs = FALSE; 6349536Seric 63553736Seric pvp = NULL; 63653736Seric if (e->e_from.q_mailer == LocalMailer) 6379536Seric { 63853736Seric # ifdef USERDB 63953736Seric register char *p; 64053736Seric extern char *udbsender(); 64153736Seric # endif 64217472Seric 64358704Seric if (!internal) 64458704Seric { 64558704Seric /* if the user has given fullname already, don't redefine */ 64658704Seric if (FullName == NULL) 64758704Seric FullName = macvalue('x', e); 64858704Seric if (FullName != NULL && FullName[0] == '\0') 64958704Seric FullName = NULL; 6509536Seric 65153736Seric # ifdef USERDB 65264284Seric p = udbsender(e->e_from.q_user); 65353736Seric 65458704Seric if (p != NULL) 65558704Seric { 65658704Seric /* 65758704Seric ** We have an alternate address for the sender 65858704Seric */ 65953736Seric 66065066Seric pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL); 66158704Seric } 66258704Seric # endif /* USERDB */ 6639536Seric } 66453736Seric 66553736Seric if ((pw = getpwnam(e->e_from.q_user)) != NULL) 66653736Seric { 66753736Seric /* 66853736Seric ** Process passwd file entry. 66953736Seric */ 67053736Seric 67153736Seric /* extract home directory */ 67265822Seric if (strcmp(pw->pw_dir, "/") == 0) 67365822Seric e->e_from.q_home = newstr(""); 67465822Seric else 67565822Seric e->e_from.q_home = newstr(pw->pw_dir); 67653736Seric define('z', e->e_from.q_home, e); 67753736Seric 67853736Seric /* extract user and group id */ 67953736Seric e->e_from.q_uid = pw->pw_uid; 68053736Seric e->e_from.q_gid = pw->pw_gid; 68165023Seric e->e_from.q_flags |= QGOODUID; 68253736Seric 68353736Seric /* extract full name from passwd file */ 68453736Seric if (FullName == NULL && pw->pw_gecos != NULL && 68558704Seric strcmp(pw->pw_name, e->e_from.q_user) == 0 && 68658704Seric !internal) 68753736Seric { 68865015Seric buildfname(pw->pw_gecos, e->e_from.q_user, buf); 68953736Seric if (buf[0] != '\0') 69053736Seric FullName = newstr(buf); 69153736Seric } 69253736Seric } 69358704Seric if (FullName != NULL && !internal) 69453182Seric define('x', FullName, e); 6959536Seric } 69665580Seric else if (!internal && OpMode != MD_DAEMON) 69711625Seric { 69853182Seric if (e->e_from.q_home == NULL) 69965822Seric { 70053182Seric e->e_from.q_home = getenv("HOME"); 70166049Seric if (e->e_from.q_home != NULL && 70266049Seric strcmp(e->e_from.q_home, "/") == 0) 70365822Seric e->e_from.q_home++; 70465822Seric } 70563787Seric e->e_from.q_uid = RealUid; 70663787Seric e->e_from.q_gid = RealGid; 70765023Seric e->e_from.q_flags |= QGOODUID; 70811625Seric } 70911625Seric 7109536Seric /* 7119536Seric ** Rewrite the from person to dispose of possible implicit 7129536Seric ** links in the net. 7139536Seric */ 7149536Seric 7159536Seric if (pvp == NULL) 71665066Seric pvp = prescan(from, delimchar, pvpbuf, sizeof pvpbuf, NULL); 71753736Seric if (pvp == NULL) 7189536Seric { 71958403Seric /* don't need to give error -- prescan did that already */ 72036233Skarels # ifdef LOG 72158020Seric if (LogLevel > 2) 72236233Skarels syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 72336233Skarels # endif 7249536Seric finis(); 7259536Seric } 72665071Seric (void) rewrite(pvp, 3, 0, e); 72765071Seric (void) rewrite(pvp, 1, 0, e); 72865071Seric (void) rewrite(pvp, 4, 0, e); 72964147Seric bp = buf + 1; 73064147Seric cataddr(pvp, NULL, bp, sizeof buf - 2, '\0'); 73164147Seric if (*bp == '@') 73264147Seric { 73364147Seric /* heuristic: route-addr: add angle brackets */ 73464147Seric strcat(bp, ">"); 73564147Seric *--bp = '<'; 73664147Seric } 73764147Seric e->e_sender = newstr(bp); 73858704Seric define('f', e->e_sender, e); 7399536Seric 7409536Seric /* save the domain spec if this mailer wants it */ 74165584Seric if (e->e_from.q_mailer != NULL && 74253182Seric bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 7439536Seric { 7449536Seric extern char **copyplist(); 7459536Seric 7469536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 7479536Seric pvp++; 7489536Seric if (*pvp != NULL) 74953182Seric e->e_fromdomain = copyplist(pvp, TRUE); 7509536Seric } 7519536Seric } 752