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*65983Seric static char sccsid[] = "@(#)envelope.c 8.31 (Berkeley) 02/03/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 1879536Seric if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 1889536Seric { 18910844Seric auto ADDRESS *rlist = NULL; 1909536Seric 19164284Seric (void) sendtolist(e->e_receiptto, NULLADDR, &rlist, e); 19255012Seric (void) returntosender("Return receipt", rlist, FALSE, e); 19365054Seric e->e_flags &= ~EF_SENDRECEIPT; 1949536Seric } 1959536Seric 1969536Seric /* 1979536Seric ** Arrange to send error messages if there are fatal errors. 1989536Seric */ 1999536Seric 20063839Seric if (saveit && e->e_errormode != EM_QUIET) 2019536Seric savemail(e); 2029536Seric 2039536Seric /* 20463849Seric ** Arrange to send warning messages to postmaster as requested. 20563849Seric */ 20663849Seric 20763849Seric if (bitset(EF_PM_NOTIFY, e->e_flags) && PostMasterCopy != NULL && 20864363Seric !bitset(EF_RESPONSE, e->e_flags) && e->e_class >= 0) 20963849Seric { 21063849Seric auto ADDRESS *rlist = NULL; 21163849Seric 21264284Seric (void) sendtolist(PostMasterCopy, NULLADDR, &rlist, e); 21363849Seric (void) returntosender(e->e_message, rlist, FALSE, e); 21463849Seric } 21563849Seric 21663849Seric /* 2179536Seric ** Instantiate or deinstantiate the queue. 2189536Seric */ 2199536Seric 2209536Seric if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 2219536Seric bitset(EF_CLRQUEUE, e->e_flags)) 2229536Seric { 22364307Seric if (tTd(50, 1)) 22464307Seric printf("\n===== Dropping [dq]f%s =====\n\n", e->e_id); 22523497Seric if (e->e_df != NULL) 22623497Seric xunlink(e->e_df); 2279536Seric xunlink(queuename(e, 'q')); 22863839Seric 22963839Seric #ifdef LOG 23063839Seric if (LogLevel > 10) 23163839Seric syslog(LOG_INFO, "%s: done", id); 23263839Seric #endif 2339536Seric } 2349536Seric else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 23510754Seric { 23610754Seric #ifdef QUEUE 23764307Seric queueup(e, bitset(EF_KEEPQUEUE, e->e_flags), FALSE); 23856795Seric #else /* QUEUE */ 23958151Seric syserr("554 dropenvelope: queueup"); 24056795Seric #endif /* QUEUE */ 24110754Seric } 2429536Seric 2439536Seric /* now unlock the job */ 24410196Seric closexscript(e); 2459536Seric unlockqueue(e); 2469536Seric 2479536Seric /* make sure that this envelope is marked unused */ 24824944Seric if (e->e_dfp != NULL) 24958680Seric (void) xfclose(e->e_dfp, "dropenvelope", e->e_df); 25010196Seric e->e_dfp = NULL; 25158680Seric e->e_id = e->e_df = NULL; 2529536Seric } 2539536Seric /* 2549536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 2559536Seric ** 2569536Seric ** This is normally used by a child process to get a clean 2579536Seric ** envelope without disturbing the parent. 2589536Seric ** 2599536Seric ** Parameters: 2609536Seric ** e -- the envelope to clear. 26125611Seric ** fullclear - if set, the current envelope is total 26225611Seric ** garbage and should be ignored; otherwise, 26325611Seric ** release any resources it may indicate. 2649536Seric ** 2659536Seric ** Returns: 2669536Seric ** none. 2679536Seric ** 2689536Seric ** Side Effects: 2699536Seric ** Closes files associated with the envelope. 2709536Seric ** Marks the envelope as unallocated. 2719536Seric */ 2729536Seric 27360494Seric void 27425611Seric clearenvelope(e, fullclear) 2759536Seric register ENVELOPE *e; 27625611Seric bool fullclear; 2779536Seric { 27825514Seric register HDR *bh; 27925514Seric register HDR **nhp; 28025514Seric extern ENVELOPE BlankEnvelope; 28125514Seric 28225611Seric if (!fullclear) 28325611Seric { 28425611Seric /* clear out any file information */ 28525611Seric if (e->e_xfp != NULL) 28658680Seric (void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id); 28725611Seric if (e->e_dfp != NULL) 28858680Seric (void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_df); 28958680Seric e->e_xfp = e->e_dfp = NULL; 29025611Seric } 2919536Seric 29224961Seric /* now clear out the data */ 29324965Seric STRUCTCOPY(BlankEnvelope, *e); 29459698Seric if (Verbose) 29559698Seric e->e_sendmode = SM_DELIVER; 29625514Seric bh = BlankEnvelope.e_header; 29725514Seric nhp = &e->e_header; 29825514Seric while (bh != NULL) 29925514Seric { 30025514Seric *nhp = (HDR *) xalloc(sizeof *bh); 30125514Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 30225514Seric bh = bh->h_link; 30325514Seric nhp = &(*nhp)->h_link; 30425514Seric } 3059536Seric } 3069536Seric /* 3079536Seric ** INITSYS -- initialize instantiation of system 3089536Seric ** 3099536Seric ** In Daemon mode, this is done in the child. 3109536Seric ** 3119536Seric ** Parameters: 3129536Seric ** none. 3139536Seric ** 3149536Seric ** Returns: 3159536Seric ** none. 3169536Seric ** 3179536Seric ** Side Effects: 3189536Seric ** Initializes the system macros, some global variables, 3199536Seric ** etc. In particular, the current time in various 3209536Seric ** forms is set. 3219536Seric */ 3229536Seric 32360494Seric void 32455012Seric initsys(e) 32555012Seric register ENVELOPE *e; 3269536Seric { 32764768Seric char cbuf[5]; /* holds hop count */ 32864768Seric char pbuf[10]; /* holds pid */ 32922963Smiriam #ifdef TTYNAME 33059304Seric static char ybuf[60]; /* holds tty id */ 3319536Seric register char *p; 33256795Seric #endif /* TTYNAME */ 3339536Seric extern char *ttyname(); 33460494Seric extern void settime(); 3359536Seric extern char Version[]; 3369536Seric 3379536Seric /* 3389536Seric ** Give this envelope a reality. 3399536Seric ** I.e., an id, a transcript, and a creation time. 3409536Seric */ 3419536Seric 34255012Seric openxscript(e); 34355012Seric e->e_ctime = curtime(); 3449536Seric 3459536Seric /* 3469536Seric ** Set OutChannel to something useful if stdout isn't it. 3479536Seric ** This arranges that any extra stuff the mailer produces 3489536Seric ** gets sent back to the user on error (because it is 3499536Seric ** tucked away in the transcript). 3509536Seric */ 3519536Seric 35264760Seric if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) && 35358737Seric e->e_xfp != NULL) 35455012Seric OutChannel = e->e_xfp; 3559536Seric 3569536Seric /* 3579536Seric ** Set up some basic system macros. 3589536Seric */ 3599536Seric 3609536Seric /* process id */ 3619536Seric (void) sprintf(pbuf, "%d", getpid()); 36264768Seric define('p', newstr(pbuf), e); 3639536Seric 3649536Seric /* hop count */ 36555012Seric (void) sprintf(cbuf, "%d", e->e_hopcount); 36664768Seric define('c', newstr(cbuf), e); 3679536Seric 3689536Seric /* time as integer, unix time, arpa time */ 36955012Seric settime(e); 3709536Seric 37117472Seric #ifdef TTYNAME 3729536Seric /* tty name */ 37355012Seric if (macvalue('y', e) == NULL) 3749536Seric { 3759536Seric p = ttyname(2); 3769536Seric if (p != NULL) 3779536Seric { 37856795Seric if (strrchr(p, '/') != NULL) 37956795Seric p = strrchr(p, '/') + 1; 3809536Seric (void) strcpy(ybuf, p); 38155012Seric define('y', ybuf, e); 3829536Seric } 3839536Seric } 38456795Seric #endif /* TTYNAME */ 3859536Seric } 3869536Seric /* 38711932Seric ** SETTIME -- set the current time. 38811932Seric ** 38911932Seric ** Parameters: 39011932Seric ** none. 39111932Seric ** 39211932Seric ** Returns: 39311932Seric ** none. 39411932Seric ** 39511932Seric ** Side Effects: 39611932Seric ** Sets the various time macros -- $a, $b, $d, $t. 39711932Seric */ 39811932Seric 39960494Seric void 40055012Seric settime(e) 40155012Seric register ENVELOPE *e; 40211932Seric { 40311932Seric register char *p; 40411932Seric auto time_t now; 40564768Seric char tbuf[20]; /* holds "current" time */ 40664768Seric char dbuf[30]; /* holds ctime(tbuf) */ 40711932Seric register struct tm *tm; 40811932Seric extern char *arpadate(); 40911932Seric extern struct tm *gmtime(); 41011932Seric 41111932Seric now = curtime(); 41211932Seric tm = gmtime(&now); 41357014Seric (void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900, 41457014Seric tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min); 41564768Seric define('t', newstr(tbuf), e); 41611932Seric (void) strcpy(dbuf, ctime(&now)); 41758131Seric p = strchr(dbuf, '\n'); 41858131Seric if (p != NULL) 41958131Seric *p = '\0'; 42064768Seric define('d', newstr(dbuf), e); 42164086Seric p = arpadate(dbuf); 42264086Seric p = newstr(p); 42355012Seric if (macvalue('a', e) == NULL) 42455012Seric define('a', p, e); 42555012Seric define('b', p, e); 42611932Seric } 42711932Seric /* 4289536Seric ** OPENXSCRIPT -- Open transcript file 4299536Seric ** 4309536Seric ** Creates a transcript file for possible eventual mailing or 4319536Seric ** sending back. 4329536Seric ** 4339536Seric ** Parameters: 4349536Seric ** e -- the envelope to create the transcript in/for. 4359536Seric ** 4369536Seric ** Returns: 4379536Seric ** none 4389536Seric ** 4399536Seric ** Side Effects: 4409536Seric ** Creates the transcript file. 4419536Seric */ 4429536Seric 44358803Seric #ifndef O_APPEND 44458803Seric #define O_APPEND 0 44558803Seric #endif 44658803Seric 44760494Seric void 4489536Seric openxscript(e) 4499536Seric register ENVELOPE *e; 4509536Seric { 4519536Seric register char *p; 45240933Srick int fd; 4539536Seric 4549536Seric if (e->e_xfp != NULL) 4559536Seric return; 4569536Seric p = queuename(e, 'x'); 45758803Seric fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644); 45840933Srick if (fd < 0) 45963753Seric { 46063753Seric syserr("Can't create transcript file %s", p); 46163753Seric fd = open("/dev/null", O_WRONLY, 0644); 46263753Seric if (fd < 0) 46363753Seric syserr("!Can't open /dev/null"); 46463753Seric } 46563753Seric e->e_xfp = fdopen(fd, "w"); 46664724Seric if (e->e_xfp == NULL) 46764724Seric { 46864724Seric syserr("!Can't create transcript stream %s", p); 46964724Seric } 47064743Seric if (tTd(46, 9)) 47164743Seric { 47264743Seric printf("openxscript(%s):\n ", p); 47364743Seric dumpfd(fileno(e->e_xfp), TRUE, FALSE); 47464743Seric } 4759536Seric } 4769536Seric /* 47710196Seric ** CLOSEXSCRIPT -- close the transcript file. 47810196Seric ** 47910196Seric ** Parameters: 48010196Seric ** e -- the envelope containing the transcript to close. 48110196Seric ** 48210196Seric ** Returns: 48310196Seric ** none. 48410196Seric ** 48510196Seric ** Side Effects: 48610196Seric ** none. 48710196Seric */ 48810196Seric 48960494Seric void 49010196Seric closexscript(e) 49110196Seric register ENVELOPE *e; 49210196Seric { 49310196Seric if (e->e_xfp == NULL) 49410196Seric return; 49558680Seric (void) xfclose(e->e_xfp, "closexscript", e->e_id); 49610196Seric e->e_xfp = NULL; 49710196Seric } 49810196Seric /* 4999536Seric ** SETSENDER -- set the person who this message is from 5009536Seric ** 5019536Seric ** Under certain circumstances allow the user to say who 5029536Seric ** s/he is (using -f or -r). These are: 5039536Seric ** 1. The user's uid is zero (root). 5049536Seric ** 2. The user's login name is in an approved list (typically 5059536Seric ** from a network server). 5069536Seric ** 3. The address the user is trying to claim has a 5079536Seric ** "!" character in it (since #2 doesn't do it for 5089536Seric ** us if we are dialing out for UUCP). 5099536Seric ** A better check to replace #3 would be if the 5109536Seric ** effective uid is "UUCP" -- this would require me 5119536Seric ** to rewrite getpwent to "grab" uucp as it went by, 5129536Seric ** make getname more nasty, do another passwd file 5139536Seric ** scan, or compile the UID of "UUCP" into the code, 5149536Seric ** all of which are reprehensible. 5159536Seric ** 5169536Seric ** Assuming all of these fail, we figure out something 5179536Seric ** ourselves. 5189536Seric ** 5199536Seric ** Parameters: 5209536Seric ** from -- the person we would like to believe this message 5219536Seric ** is from, as specified on the command line. 52253182Seric ** e -- the envelope in which we would like the sender set. 52358333Seric ** delimptr -- if non-NULL, set to the location of the 52458333Seric ** trailing delimiter. 52558704Seric ** internal -- set if this address is coming from an internal 52658704Seric ** source such as an owner alias. 5279536Seric ** 5289536Seric ** Returns: 52958704Seric ** none. 5309536Seric ** 5319536Seric ** Side Effects: 5329536Seric ** sets sendmail's notion of who the from person is. 5339536Seric */ 5349536Seric 53560494Seric void 53658704Seric setsender(from, e, delimptr, internal) 5379536Seric char *from; 53853182Seric register ENVELOPE *e; 53958333Seric char **delimptr; 54058704Seric bool internal; 5419536Seric { 5429536Seric register char **pvp; 5439536Seric char *realname = NULL; 54418665Seric register struct passwd *pw; 54558727Seric char delimchar; 54664147Seric char *bp; 54764147Seric char buf[MAXNAME + 2]; 54816913Seric char pvpbuf[PSBUFSIZE]; 54918665Seric extern struct passwd *getpwnam(); 5509536Seric extern char *FullName; 5519536Seric 5529536Seric if (tTd(45, 1)) 55314786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 5549536Seric 5559536Seric /* 5569536Seric ** Figure out the real user executing us. 5579536Seric ** Username can return errno != 0 on non-errors. 5589536Seric */ 5599536Seric 56065580Seric if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP || 561*65983Seric OpMode == MD_ARPAFTP || OpMode == MD_DAEMON) 5629536Seric realname = from; 5639536Seric if (realname == NULL || realname[0] == '\0') 5649536Seric realname = username(); 5659536Seric 56659027Seric if (ConfigLevel < 2) 56759027Seric SuprErrs = TRUE; 56859027Seric 56958727Seric delimchar = internal ? '\0' : ' '; 57064793Seric e->e_from.q_flags = QBADADDR; 57158333Seric if (from == NULL || 57264284Seric parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR, 57364793Seric delimchar, delimptr, e) == NULL || 57464793Seric bitset(QBADADDR, e->e_from.q_flags) || 57564793Seric e->e_from.q_mailer == ProgMailer || 57664793Seric e->e_from.q_mailer == FileMailer || 57764793Seric e->e_from.q_mailer == InclMailer) 5789536Seric { 57921750Seric /* log garbage addresses for traceback */ 58055173Seric # ifdef LOG 58158020Seric if (from != NULL && LogLevel > 2) 58221750Seric { 58358951Seric char *p; 58458951Seric char ebuf[MAXNAME * 2 + 2]; 58555173Seric 58658951Seric p = macvalue('_', e); 58758951Seric if (p == NULL) 58858951Seric { 58958951Seric char *host = RealHostName; 59058951Seric if (host == NULL) 59158951Seric host = MyHostName; 59258951Seric (void) sprintf(ebuf, "%s@%s", realname, host); 59358951Seric p = ebuf; 59458951Seric } 59555173Seric syslog(LOG_NOTICE, 59664793Seric "setsender: %s: invalid or unparseable, received from %s", 59765015Seric shortenstring(from, 83), p); 59855173Seric } 59956795Seric # endif /* LOG */ 60057589Seric if (from != NULL) 60164793Seric { 60264793Seric if (!bitset(QBADADDR, e->e_from.q_flags)) 60364793Seric { 60464793Seric /* it was a bogus mailer in the from addr */ 60564793Seric usrerr("553 Invalid sender address"); 60664793Seric } 60757589Seric SuprErrs = TRUE; 60864793Seric } 60957589Seric if (from == realname || 61064284Seric parseaddr(from = newstr(realname), &e->e_from, 61164284Seric RF_COPYALL|RF_SENDERADDR, ' ', NULL, e) == NULL) 61224944Seric { 61364793Seric char nbuf[100]; 61464793Seric 61557589Seric SuprErrs = TRUE; 61664808Seric expand("\201n", nbuf, &nbuf[sizeof nbuf], e); 61764793Seric if (parseaddr(from = newstr(nbuf), &e->e_from, 61864793Seric RF_COPYALL, ' ', NULL, e) == NULL && 61964793Seric parseaddr(from = "postmaster", &e->e_from, 62064793Seric RF_COPYALL, ' ', NULL, e) == NULL) 62158151Seric syserr("553 setsender: can't even parse postmaster!"); 62224944Seric } 6239536Seric } 6249536Seric else 6259536Seric FromFlag = TRUE; 62653182Seric e->e_from.q_flags |= QDONTSEND; 62757731Seric if (tTd(45, 5)) 62857731Seric { 62957731Seric printf("setsender: QDONTSEND "); 63057731Seric printaddr(&e->e_from, FALSE); 63157731Seric } 6329536Seric SuprErrs = FALSE; 6339536Seric 63453736Seric pvp = NULL; 63553736Seric if (e->e_from.q_mailer == LocalMailer) 6369536Seric { 63753736Seric # ifdef USERDB 63853736Seric register char *p; 63953736Seric extern char *udbsender(); 64053736Seric # endif 64117472Seric 64258704Seric if (!internal) 64358704Seric { 64458704Seric /* if the user has given fullname already, don't redefine */ 64558704Seric if (FullName == NULL) 64658704Seric FullName = macvalue('x', e); 64758704Seric if (FullName != NULL && FullName[0] == '\0') 64858704Seric FullName = NULL; 6499536Seric 65053736Seric # ifdef USERDB 65164284Seric p = udbsender(e->e_from.q_user); 65253736Seric 65358704Seric if (p != NULL) 65458704Seric { 65558704Seric /* 65658704Seric ** We have an alternate address for the sender 65758704Seric */ 65853736Seric 65965066Seric pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL); 66058704Seric } 66158704Seric # endif /* USERDB */ 6629536Seric } 66353736Seric 66453736Seric if ((pw = getpwnam(e->e_from.q_user)) != NULL) 66553736Seric { 66653736Seric /* 66753736Seric ** Process passwd file entry. 66853736Seric */ 66953736Seric 67053736Seric /* extract home directory */ 67165822Seric if (strcmp(pw->pw_dir, "/") == 0) 67265822Seric e->e_from.q_home = newstr(""); 67365822Seric else 67465822Seric e->e_from.q_home = newstr(pw->pw_dir); 67553736Seric define('z', e->e_from.q_home, e); 67653736Seric 67753736Seric /* extract user and group id */ 67853736Seric e->e_from.q_uid = pw->pw_uid; 67953736Seric e->e_from.q_gid = pw->pw_gid; 68065023Seric e->e_from.q_flags |= QGOODUID; 68153736Seric 68253736Seric /* extract full name from passwd file */ 68353736Seric if (FullName == NULL && pw->pw_gecos != NULL && 68458704Seric strcmp(pw->pw_name, e->e_from.q_user) == 0 && 68558704Seric !internal) 68653736Seric { 68765015Seric buildfname(pw->pw_gecos, e->e_from.q_user, buf); 68853736Seric if (buf[0] != '\0') 68953736Seric FullName = newstr(buf); 69053736Seric } 69153736Seric } 69258704Seric if (FullName != NULL && !internal) 69353182Seric define('x', FullName, e); 6949536Seric } 69565580Seric else if (!internal && OpMode != MD_DAEMON) 69611625Seric { 69753182Seric if (e->e_from.q_home == NULL) 69865822Seric { 69953182Seric e->e_from.q_home = getenv("HOME"); 70065822Seric if (strcmp(e->e_from.q_home, "/") == 0) 70165822Seric e->e_from.q_home++; 70265822Seric } 70363787Seric e->e_from.q_uid = RealUid; 70463787Seric e->e_from.q_gid = RealGid; 70565023Seric e->e_from.q_flags |= QGOODUID; 70611625Seric } 70711625Seric 7089536Seric /* 7099536Seric ** Rewrite the from person to dispose of possible implicit 7109536Seric ** links in the net. 7119536Seric */ 7129536Seric 7139536Seric if (pvp == NULL) 71465066Seric pvp = prescan(from, delimchar, pvpbuf, sizeof pvpbuf, NULL); 71553736Seric if (pvp == NULL) 7169536Seric { 71758403Seric /* don't need to give error -- prescan did that already */ 71836233Skarels # ifdef LOG 71958020Seric if (LogLevel > 2) 72036233Skarels syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 72136233Skarels # endif 7229536Seric finis(); 7239536Seric } 72465071Seric (void) rewrite(pvp, 3, 0, e); 72565071Seric (void) rewrite(pvp, 1, 0, e); 72665071Seric (void) rewrite(pvp, 4, 0, e); 72764147Seric bp = buf + 1; 72864147Seric cataddr(pvp, NULL, bp, sizeof buf - 2, '\0'); 72964147Seric if (*bp == '@') 73064147Seric { 73164147Seric /* heuristic: route-addr: add angle brackets */ 73264147Seric strcat(bp, ">"); 73364147Seric *--bp = '<'; 73464147Seric } 73564147Seric e->e_sender = newstr(bp); 73658704Seric define('f', e->e_sender, e); 7379536Seric 7389536Seric /* save the domain spec if this mailer wants it */ 73965584Seric if (e->e_from.q_mailer != NULL && 74053182Seric bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 7419536Seric { 7429536Seric extern char **copyplist(); 7439536Seric 7449536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 7459536Seric pvp++; 7469536Seric if (*pvp != NULL) 74753182Seric e->e_fromdomain = copyplist(pvp, TRUE); 7489536Seric } 7499536Seric } 750