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*65089Seric static char sccsid[] = "@(#)envelope.c 8.25 (Berkeley) 12/11/93"; 1133729Sbostic #endif /* not lint */ 1222704Sdist 1358332Seric #include "sendmail.h" 1436928Sbostic #include <sys/time.h> 159536Seric #include <pwd.h> 169536Seric 179536Seric /* 189536Seric ** NEWENVELOPE -- allocate a new envelope 199536Seric ** 209536Seric ** Supports inheritance. 219536Seric ** 229536Seric ** Parameters: 239536Seric ** e -- the new envelope to fill in. 2458179Seric ** parent -- the envelope to be the parent of e. 259536Seric ** 269536Seric ** Returns: 279536Seric ** e. 289536Seric ** 299536Seric ** Side Effects: 309536Seric ** none. 319536Seric */ 329536Seric 339536Seric ENVELOPE * 3458179Seric newenvelope(e, parent) 359536Seric register ENVELOPE *e; 3658179Seric register ENVELOPE *parent; 379536Seric { 389536Seric extern putheader(), putbody(); 3925611Seric extern ENVELOPE BlankEnvelope; 409536Seric 4158179Seric if (e == parent && e->e_parent != NULL) 429536Seric parent = e->e_parent; 4325611Seric clearenvelope(e, TRUE); 4424944Seric if (e == CurEnv) 4524944Seric bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from); 4624944Seric else 4724944Seric bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from); 489536Seric e->e_parent = parent; 499536Seric e->e_ctime = curtime(); 5056215Seric if (parent != NULL) 5156215Seric e->e_msgpriority = parent->e_msgsize; 529536Seric e->e_puthdr = putheader; 539536Seric e->e_putbody = putbody; 549536Seric if (CurEnv->e_xfp != NULL) 559536Seric (void) fflush(CurEnv->e_xfp); 569536Seric 579536Seric return (e); 589536Seric } 599536Seric /* 609536Seric ** DROPENVELOPE -- deallocate an envelope. 619536Seric ** 629536Seric ** Parameters: 639536Seric ** e -- the envelope to deallocate. 649536Seric ** 659536Seric ** Returns: 669536Seric ** none. 679536Seric ** 689536Seric ** Side Effects: 699536Seric ** housekeeping necessary to dispose of an envelope. 709536Seric ** Unlocks this queue file. 719536Seric */ 729536Seric 7360494Seric void 749536Seric dropenvelope(e) 759536Seric register ENVELOPE *e; 769536Seric { 779536Seric bool queueit = FALSE; 7863839Seric bool saveit = bitset(EF_FATALERRS, e->e_flags); 799536Seric register ADDRESS *q; 8057943Seric char *id = e->e_id; 8163753Seric char buf[MAXLINE]; 829536Seric 839536Seric if (tTd(50, 1)) 849536Seric { 8558680Seric printf("dropenvelope %x: id=", e); 869536Seric xputs(e->e_id); 87*65089Seric printf(", flags=0x%x\n", e->e_flags); 8863753Seric if (tTd(50, 10)) 8963753Seric { 9063753Seric printf("sendq="); 9163753Seric printaddr(e->e_sendqueue, TRUE); 9263753Seric } 939536Seric } 9457943Seric 9564925Seric #ifdef XDEBUG 9664925Seric checkfd012("dropenvelope 1"); 9764925Seric #endif 9864925Seric 9958680Seric /* we must have an id to remove disk files */ 10057943Seric if (id == NULL) 10158680Seric return; 10257943Seric 1039536Seric #ifdef LOG 104*65089Seric if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags)) 105*65089Seric logsender(e, NULL); 10658020Seric if (LogLevel > 84) 107*65089Seric syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=0x%x, pid=%d", 10857943Seric id, e->e_flags, getpid()); 10956795Seric #endif /* LOG */ 110*65089Seric e->e_flags &= ~EF_LOGSENDER; 1119536Seric 11263753Seric /* post statistics */ 11363753Seric poststats(StatFile); 11463753Seric 1159536Seric /* 1169536Seric ** Extract state information from dregs of send list. 1179536Seric */ 1189536Seric 11964743Seric e->e_flags &= ~EF_QUEUERUN; 1209536Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1219536Seric { 1229536Seric if (bitset(QQUEUEUP, q->q_flags)) 1239536Seric queueit = TRUE; 12463839Seric if (!bitset(QDONTSEND, q->q_flags) && 12563839Seric bitset(QBADADDR, q->q_flags)) 12663839Seric { 12763839Seric if (q->q_owner == NULL && 12863839Seric strcmp(e->e_from.q_paddr, "<>") != 0) 12963839Seric (void) sendtolist(e->e_from.q_paddr, NULL, 13063839Seric &e->e_errorqueue, e); 13163839Seric } 1329536Seric } 1339536Seric 1349536Seric /* 13563753Seric ** See if the message timed out. 13663753Seric */ 13763753Seric 13863753Seric if (!queueit) 13963753Seric /* nothing to do */ ; 14063753Seric else if (curtime() > e->e_ctime + TimeOuts.to_q_return) 14163753Seric { 14263839Seric (void) sprintf(buf, "Cannot send message for %s", 14363839Seric pintvl(TimeOuts.to_q_return, FALSE)); 14463839Seric if (e->e_message != NULL) 14563839Seric free(e->e_message); 14663839Seric e->e_message = newstr(buf); 14763839Seric message(buf); 14863839Seric e->e_flags |= EF_CLRQUEUE; 14963839Seric saveit = TRUE; 15063787Seric fprintf(e->e_xfp, "Message could not be delivered for %s\n", 15163787Seric pintvl(TimeOuts.to_q_return, FALSE)); 15263787Seric fprintf(e->e_xfp, "Message will be deleted from queue\n"); 15363787Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 15463787Seric { 15563787Seric if (bitset(QQUEUEUP, q->q_flags)) 15663787Seric q->q_flags |= QBADADDR; 15763787Seric } 15863753Seric } 15963753Seric else if (TimeOuts.to_q_warning > 0 && 16063753Seric curtime() > e->e_ctime + TimeOuts.to_q_warning) 16163753Seric { 16263753Seric if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) && 16363753Seric e->e_class >= 0 && 16463753Seric strcmp(e->e_from.q_paddr, "<>") != 0) 16563753Seric { 16663753Seric (void) sprintf(buf, 16763753Seric "warning: cannot send message for %s", 16863753Seric pintvl(TimeOuts.to_q_warning, FALSE)); 16963753Seric if (e->e_message != NULL) 17063753Seric free(e->e_message); 17163753Seric e->e_message = newstr(buf); 17263753Seric message(buf); 17363839Seric e->e_flags |= EF_WARNING; 17463839Seric saveit = TRUE; 17563753Seric } 17663753Seric fprintf(e->e_xfp, 17763753Seric "Warning: message still undelivered after %s\n", 17863753Seric pintvl(TimeOuts.to_q_warning, FALSE)); 17963753Seric fprintf(e->e_xfp, "Will keep trying until message is %s old\n", 18063753Seric pintvl(TimeOuts.to_q_return, FALSE)); 18163787Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 18263787Seric { 18363787Seric if (bitset(QQUEUEUP, q->q_flags)) 18463787Seric q->q_flags |= QREPORT; 18563787Seric } 18663753Seric } 18763753Seric 18863753Seric /* 1899536Seric ** Send back return receipts as requested. 1909536Seric */ 1919536Seric 1929536Seric if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 1939536Seric { 19410844Seric auto ADDRESS *rlist = NULL; 1959536Seric 19664284Seric (void) sendtolist(e->e_receiptto, NULLADDR, &rlist, e); 19755012Seric (void) returntosender("Return receipt", rlist, FALSE, e); 19865054Seric e->e_flags &= ~EF_SENDRECEIPT; 1999536Seric } 2009536Seric 2019536Seric /* 2029536Seric ** Arrange to send error messages if there are fatal errors. 2039536Seric */ 2049536Seric 20563839Seric if (saveit && e->e_errormode != EM_QUIET) 2069536Seric savemail(e); 2079536Seric 20864925Seric #ifdef XDEBUG 20964925Seric checkfd012("dropenvelope 2"); 21064925Seric #endif 21164925Seric 2129536Seric /* 21363849Seric ** Arrange to send warning messages to postmaster as requested. 21463849Seric */ 21563849Seric 21663849Seric if (bitset(EF_PM_NOTIFY, e->e_flags) && PostMasterCopy != NULL && 21764363Seric !bitset(EF_RESPONSE, e->e_flags) && e->e_class >= 0) 21863849Seric { 21963849Seric auto ADDRESS *rlist = NULL; 22063849Seric 22164284Seric (void) sendtolist(PostMasterCopy, NULLADDR, &rlist, e); 22263849Seric (void) returntosender(e->e_message, rlist, FALSE, e); 22363849Seric } 22463849Seric 22563849Seric /* 2269536Seric ** Instantiate or deinstantiate the queue. 2279536Seric */ 2289536Seric 2299536Seric if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 2309536Seric bitset(EF_CLRQUEUE, e->e_flags)) 2319536Seric { 23264307Seric if (tTd(50, 1)) 23364307Seric printf("\n===== Dropping [dq]f%s =====\n\n", e->e_id); 23423497Seric if (e->e_df != NULL) 23523497Seric xunlink(e->e_df); 2369536Seric xunlink(queuename(e, 'q')); 23763839Seric 23863839Seric #ifdef LOG 23963839Seric if (LogLevel > 10) 24063839Seric syslog(LOG_INFO, "%s: done", id); 24163839Seric #endif 2429536Seric } 2439536Seric else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 24410754Seric { 24510754Seric #ifdef QUEUE 24664307Seric queueup(e, bitset(EF_KEEPQUEUE, e->e_flags), FALSE); 24756795Seric #else /* QUEUE */ 24858151Seric syserr("554 dropenvelope: queueup"); 24956795Seric #endif /* QUEUE */ 25010754Seric } 2519536Seric 25264925Seric #ifdef XDEBUG 25364925Seric checkfd012("dropenvelope 3"); 25464925Seric #endif 25564925Seric 2569536Seric /* now unlock the job */ 25710196Seric closexscript(e); 2589536Seric unlockqueue(e); 2599536Seric 2609536Seric /* make sure that this envelope is marked unused */ 26124944Seric if (e->e_dfp != NULL) 26258680Seric (void) xfclose(e->e_dfp, "dropenvelope", e->e_df); 26310196Seric e->e_dfp = NULL; 26458680Seric e->e_id = e->e_df = NULL; 26564401Seric #ifdef XDEBUG 26664925Seric checkfd012("dropenvelope 4"); 26764401Seric #endif 2689536Seric } 2699536Seric /* 2709536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 2719536Seric ** 2729536Seric ** This is normally used by a child process to get a clean 2739536Seric ** envelope without disturbing the parent. 2749536Seric ** 2759536Seric ** Parameters: 2769536Seric ** e -- the envelope to clear. 27725611Seric ** fullclear - if set, the current envelope is total 27825611Seric ** garbage and should be ignored; otherwise, 27925611Seric ** release any resources it may indicate. 2809536Seric ** 2819536Seric ** Returns: 2829536Seric ** none. 2839536Seric ** 2849536Seric ** Side Effects: 2859536Seric ** Closes files associated with the envelope. 2869536Seric ** Marks the envelope as unallocated. 2879536Seric */ 2889536Seric 28960494Seric void 29025611Seric clearenvelope(e, fullclear) 2919536Seric register ENVELOPE *e; 29225611Seric bool fullclear; 2939536Seric { 29425514Seric register HDR *bh; 29525514Seric register HDR **nhp; 29625514Seric extern ENVELOPE BlankEnvelope; 29725514Seric 29825611Seric if (!fullclear) 29925611Seric { 30025611Seric /* clear out any file information */ 30125611Seric if (e->e_xfp != NULL) 30258680Seric (void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id); 30325611Seric if (e->e_dfp != NULL) 30458680Seric (void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_df); 30558680Seric e->e_xfp = e->e_dfp = NULL; 30625611Seric } 3079536Seric 30824961Seric /* now clear out the data */ 30924965Seric STRUCTCOPY(BlankEnvelope, *e); 31059698Seric if (Verbose) 31159698Seric e->e_sendmode = SM_DELIVER; 31225514Seric bh = BlankEnvelope.e_header; 31325514Seric nhp = &e->e_header; 31425514Seric while (bh != NULL) 31525514Seric { 31625514Seric *nhp = (HDR *) xalloc(sizeof *bh); 31725514Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 31825514Seric bh = bh->h_link; 31925514Seric nhp = &(*nhp)->h_link; 32025514Seric } 3219536Seric } 3229536Seric /* 3239536Seric ** INITSYS -- initialize instantiation of system 3249536Seric ** 3259536Seric ** In Daemon mode, this is done in the child. 3269536Seric ** 3279536Seric ** Parameters: 3289536Seric ** none. 3299536Seric ** 3309536Seric ** Returns: 3319536Seric ** none. 3329536Seric ** 3339536Seric ** Side Effects: 3349536Seric ** Initializes the system macros, some global variables, 3359536Seric ** etc. In particular, the current time in various 3369536Seric ** forms is set. 3379536Seric */ 3389536Seric 33960494Seric void 34055012Seric initsys(e) 34155012Seric register ENVELOPE *e; 3429536Seric { 34364768Seric char cbuf[5]; /* holds hop count */ 34464768Seric char pbuf[10]; /* holds pid */ 34522963Smiriam #ifdef TTYNAME 34659304Seric static char ybuf[60]; /* holds tty id */ 3479536Seric register char *p; 34856795Seric #endif /* TTYNAME */ 3499536Seric extern char *ttyname(); 35060494Seric extern void settime(); 3519536Seric extern char Version[]; 3529536Seric 3539536Seric /* 3549536Seric ** Give this envelope a reality. 3559536Seric ** I.e., an id, a transcript, and a creation time. 3569536Seric */ 3579536Seric 35855012Seric openxscript(e); 35955012Seric e->e_ctime = curtime(); 3609536Seric 3619536Seric /* 3629536Seric ** Set OutChannel to something useful if stdout isn't it. 3639536Seric ** This arranges that any extra stuff the mailer produces 3649536Seric ** gets sent back to the user on error (because it is 3659536Seric ** tucked away in the transcript). 3669536Seric */ 3679536Seric 36864760Seric if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) && 36958737Seric e->e_xfp != NULL) 37055012Seric OutChannel = e->e_xfp; 3719536Seric 3729536Seric /* 3739536Seric ** Set up some basic system macros. 3749536Seric */ 3759536Seric 3769536Seric /* process id */ 3779536Seric (void) sprintf(pbuf, "%d", getpid()); 37864768Seric define('p', newstr(pbuf), e); 3799536Seric 3809536Seric /* hop count */ 38155012Seric (void) sprintf(cbuf, "%d", e->e_hopcount); 38264768Seric define('c', newstr(cbuf), e); 3839536Seric 3849536Seric /* time as integer, unix time, arpa time */ 38555012Seric settime(e); 3869536Seric 38717472Seric #ifdef TTYNAME 3889536Seric /* tty name */ 38955012Seric if (macvalue('y', e) == NULL) 3909536Seric { 3919536Seric p = ttyname(2); 3929536Seric if (p != NULL) 3939536Seric { 39456795Seric if (strrchr(p, '/') != NULL) 39556795Seric p = strrchr(p, '/') + 1; 3969536Seric (void) strcpy(ybuf, p); 39755012Seric define('y', ybuf, e); 3989536Seric } 3999536Seric } 40056795Seric #endif /* TTYNAME */ 4019536Seric } 4029536Seric /* 40311932Seric ** SETTIME -- set the current time. 40411932Seric ** 40511932Seric ** Parameters: 40611932Seric ** none. 40711932Seric ** 40811932Seric ** Returns: 40911932Seric ** none. 41011932Seric ** 41111932Seric ** Side Effects: 41211932Seric ** Sets the various time macros -- $a, $b, $d, $t. 41311932Seric */ 41411932Seric 41560494Seric void 41655012Seric settime(e) 41755012Seric register ENVELOPE *e; 41811932Seric { 41911932Seric register char *p; 42011932Seric auto time_t now; 42164768Seric char tbuf[20]; /* holds "current" time */ 42264768Seric char dbuf[30]; /* holds ctime(tbuf) */ 42311932Seric register struct tm *tm; 42411932Seric extern char *arpadate(); 42511932Seric extern struct tm *gmtime(); 42611932Seric 42711932Seric now = curtime(); 42811932Seric tm = gmtime(&now); 42957014Seric (void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900, 43057014Seric tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min); 43164768Seric define('t', newstr(tbuf), e); 43211932Seric (void) strcpy(dbuf, ctime(&now)); 43358131Seric p = strchr(dbuf, '\n'); 43458131Seric if (p != NULL) 43558131Seric *p = '\0'; 43664768Seric define('d', newstr(dbuf), e); 43764086Seric p = arpadate(dbuf); 43864086Seric p = newstr(p); 43955012Seric if (macvalue('a', e) == NULL) 44055012Seric define('a', p, e); 44155012Seric define('b', p, e); 44211932Seric } 44311932Seric /* 4449536Seric ** OPENXSCRIPT -- Open transcript file 4459536Seric ** 4469536Seric ** Creates a transcript file for possible eventual mailing or 4479536Seric ** sending back. 4489536Seric ** 4499536Seric ** Parameters: 4509536Seric ** e -- the envelope to create the transcript in/for. 4519536Seric ** 4529536Seric ** Returns: 4539536Seric ** none 4549536Seric ** 4559536Seric ** Side Effects: 4569536Seric ** Creates the transcript file. 4579536Seric */ 4589536Seric 45958803Seric #ifndef O_APPEND 46058803Seric #define O_APPEND 0 46158803Seric #endif 46258803Seric 46360494Seric void 4649536Seric openxscript(e) 4659536Seric register ENVELOPE *e; 4669536Seric { 4679536Seric register char *p; 46840933Srick int fd; 4699536Seric 4709536Seric if (e->e_xfp != NULL) 4719536Seric return; 4729536Seric p = queuename(e, 'x'); 47358803Seric fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644); 47440933Srick if (fd < 0) 47563753Seric { 47663753Seric syserr("Can't create transcript file %s", p); 47763753Seric fd = open("/dev/null", O_WRONLY, 0644); 47863753Seric if (fd < 0) 47963753Seric syserr("!Can't open /dev/null"); 48063753Seric } 48163753Seric e->e_xfp = fdopen(fd, "w"); 48264724Seric if (e->e_xfp == NULL) 48364724Seric { 48464724Seric syserr("!Can't create transcript stream %s", p); 48564724Seric } 48664743Seric if (tTd(46, 9)) 48764743Seric { 48864743Seric printf("openxscript(%s):\n ", p); 48964743Seric dumpfd(fileno(e->e_xfp), TRUE, FALSE); 49064743Seric } 4919536Seric } 4929536Seric /* 49310196Seric ** CLOSEXSCRIPT -- close the transcript file. 49410196Seric ** 49510196Seric ** Parameters: 49610196Seric ** e -- the envelope containing the transcript to close. 49710196Seric ** 49810196Seric ** Returns: 49910196Seric ** none. 50010196Seric ** 50110196Seric ** Side Effects: 50210196Seric ** none. 50310196Seric */ 50410196Seric 50560494Seric void 50610196Seric closexscript(e) 50710196Seric register ENVELOPE *e; 50810196Seric { 50910196Seric if (e->e_xfp == NULL) 51010196Seric return; 51158680Seric (void) xfclose(e->e_xfp, "closexscript", e->e_id); 51210196Seric e->e_xfp = NULL; 51310196Seric } 51410196Seric /* 5159536Seric ** SETSENDER -- set the person who this message is from 5169536Seric ** 5179536Seric ** Under certain circumstances allow the user to say who 5189536Seric ** s/he is (using -f or -r). These are: 5199536Seric ** 1. The user's uid is zero (root). 5209536Seric ** 2. The user's login name is in an approved list (typically 5219536Seric ** from a network server). 5229536Seric ** 3. The address the user is trying to claim has a 5239536Seric ** "!" character in it (since #2 doesn't do it for 5249536Seric ** us if we are dialing out for UUCP). 5259536Seric ** A better check to replace #3 would be if the 5269536Seric ** effective uid is "UUCP" -- this would require me 5279536Seric ** to rewrite getpwent to "grab" uucp as it went by, 5289536Seric ** make getname more nasty, do another passwd file 5299536Seric ** scan, or compile the UID of "UUCP" into the code, 5309536Seric ** all of which are reprehensible. 5319536Seric ** 5329536Seric ** Assuming all of these fail, we figure out something 5339536Seric ** ourselves. 5349536Seric ** 5359536Seric ** Parameters: 5369536Seric ** from -- the person we would like to believe this message 5379536Seric ** is from, as specified on the command line. 53853182Seric ** e -- the envelope in which we would like the sender set. 53958333Seric ** delimptr -- if non-NULL, set to the location of the 54058333Seric ** trailing delimiter. 54158704Seric ** internal -- set if this address is coming from an internal 54258704Seric ** source such as an owner alias. 5439536Seric ** 5449536Seric ** Returns: 54558704Seric ** none. 5469536Seric ** 5479536Seric ** Side Effects: 5489536Seric ** sets sendmail's notion of who the from person is. 5499536Seric */ 5509536Seric 55160494Seric void 55258704Seric setsender(from, e, delimptr, internal) 5539536Seric char *from; 55453182Seric register ENVELOPE *e; 55558333Seric char **delimptr; 55658704Seric bool internal; 5579536Seric { 5589536Seric register char **pvp; 5599536Seric char *realname = NULL; 56018665Seric register struct passwd *pw; 56158727Seric char delimchar; 56264147Seric char *bp; 56364147Seric char buf[MAXNAME + 2]; 56416913Seric char pvpbuf[PSBUFSIZE]; 56518665Seric extern struct passwd *getpwnam(); 5669536Seric extern char *FullName; 5679536Seric 5689536Seric if (tTd(45, 1)) 56914786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 5709536Seric 5719536Seric /* 5729536Seric ** Figure out the real user executing us. 5739536Seric ** Username can return errno != 0 on non-errors. 5749536Seric */ 5759536Seric 57658737Seric if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP) 5779536Seric realname = from; 5789536Seric if (realname == NULL || realname[0] == '\0') 5799536Seric realname = username(); 5809536Seric 58159027Seric if (ConfigLevel < 2) 58259027Seric SuprErrs = TRUE; 58359027Seric 58458727Seric delimchar = internal ? '\0' : ' '; 58564793Seric e->e_from.q_flags = QBADADDR; 58658333Seric if (from == NULL || 58764284Seric parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR, 58864793Seric delimchar, delimptr, e) == NULL || 58964793Seric bitset(QBADADDR, e->e_from.q_flags) || 59064793Seric e->e_from.q_mailer == ProgMailer || 59164793Seric e->e_from.q_mailer == FileMailer || 59264793Seric e->e_from.q_mailer == InclMailer) 5939536Seric { 59421750Seric /* log garbage addresses for traceback */ 59555173Seric # ifdef LOG 59658020Seric if (from != NULL && LogLevel > 2) 59721750Seric { 59858951Seric char *p; 59958951Seric char ebuf[MAXNAME * 2 + 2]; 60055173Seric 60158951Seric p = macvalue('_', e); 60258951Seric if (p == NULL) 60358951Seric { 60458951Seric char *host = RealHostName; 60558951Seric if (host == NULL) 60658951Seric host = MyHostName; 60758951Seric (void) sprintf(ebuf, "%s@%s", realname, host); 60858951Seric p = ebuf; 60958951Seric } 61055173Seric syslog(LOG_NOTICE, 61164793Seric "setsender: %s: invalid or unparseable, received from %s", 61265015Seric shortenstring(from, 83), p); 61355173Seric } 61456795Seric # endif /* LOG */ 61557589Seric if (from != NULL) 61664793Seric { 61764793Seric if (!bitset(QBADADDR, e->e_from.q_flags)) 61864793Seric { 61964793Seric /* it was a bogus mailer in the from addr */ 62064793Seric usrerr("553 Invalid sender address"); 62164793Seric } 62257589Seric SuprErrs = TRUE; 62364793Seric } 62457589Seric if (from == realname || 62564284Seric parseaddr(from = newstr(realname), &e->e_from, 62664284Seric RF_COPYALL|RF_SENDERADDR, ' ', NULL, e) == NULL) 62724944Seric { 62864793Seric char nbuf[100]; 62964793Seric 63057589Seric SuprErrs = TRUE; 63164808Seric expand("\201n", nbuf, &nbuf[sizeof nbuf], e); 63264793Seric if (parseaddr(from = newstr(nbuf), &e->e_from, 63364793Seric RF_COPYALL, ' ', NULL, e) == NULL && 63464793Seric parseaddr(from = "postmaster", &e->e_from, 63564793Seric RF_COPYALL, ' ', NULL, e) == NULL) 63658151Seric syserr("553 setsender: can't even parse postmaster!"); 63724944Seric } 6389536Seric } 6399536Seric else 6409536Seric FromFlag = TRUE; 64153182Seric e->e_from.q_flags |= QDONTSEND; 64257731Seric if (tTd(45, 5)) 64357731Seric { 64457731Seric printf("setsender: QDONTSEND "); 64557731Seric printaddr(&e->e_from, FALSE); 64657731Seric } 6479536Seric SuprErrs = FALSE; 6489536Seric 64953736Seric pvp = NULL; 65053736Seric if (e->e_from.q_mailer == LocalMailer) 6519536Seric { 65253736Seric # ifdef USERDB 65353736Seric register char *p; 65453736Seric extern char *udbsender(); 65553736Seric # endif 65617472Seric 65758704Seric if (!internal) 65858704Seric { 65958704Seric /* if the user has given fullname already, don't redefine */ 66058704Seric if (FullName == NULL) 66158704Seric FullName = macvalue('x', e); 66258704Seric if (FullName != NULL && FullName[0] == '\0') 66358704Seric FullName = NULL; 6649536Seric 66553736Seric # ifdef USERDB 66664284Seric p = udbsender(e->e_from.q_user); 66753736Seric 66858704Seric if (p != NULL) 66958704Seric { 67058704Seric /* 67158704Seric ** We have an alternate address for the sender 67258704Seric */ 67353736Seric 67465066Seric pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL); 67558704Seric } 67658704Seric # endif /* USERDB */ 6779536Seric } 67853736Seric 67953736Seric if ((pw = getpwnam(e->e_from.q_user)) != NULL) 68053736Seric { 68153736Seric /* 68253736Seric ** Process passwd file entry. 68353736Seric */ 68453736Seric 68553736Seric 68653736Seric /* extract home directory */ 68753736Seric e->e_from.q_home = newstr(pw->pw_dir); 68853736Seric define('z', e->e_from.q_home, e); 68953736Seric 69053736Seric /* extract user and group id */ 69153736Seric e->e_from.q_uid = pw->pw_uid; 69253736Seric e->e_from.q_gid = pw->pw_gid; 69365023Seric e->e_from.q_flags |= QGOODUID; 69453736Seric 69553736Seric /* extract full name from passwd file */ 69653736Seric if (FullName == NULL && pw->pw_gecos != NULL && 69758704Seric strcmp(pw->pw_name, e->e_from.q_user) == 0 && 69858704Seric !internal) 69953736Seric { 70065015Seric buildfname(pw->pw_gecos, e->e_from.q_user, buf); 70153736Seric if (buf[0] != '\0') 70253736Seric FullName = newstr(buf); 70353736Seric } 70453736Seric } 70558704Seric if (FullName != NULL && !internal) 70653182Seric define('x', FullName, e); 7079536Seric } 70858704Seric else if (!internal) 70911625Seric { 71053182Seric if (e->e_from.q_home == NULL) 71153182Seric e->e_from.q_home = getenv("HOME"); 71263787Seric e->e_from.q_uid = RealUid; 71363787Seric e->e_from.q_gid = RealGid; 71465023Seric e->e_from.q_flags |= QGOODUID; 71511625Seric } 71611625Seric 7179536Seric /* 7189536Seric ** Rewrite the from person to dispose of possible implicit 7199536Seric ** links in the net. 7209536Seric */ 7219536Seric 7229536Seric if (pvp == NULL) 72365066Seric pvp = prescan(from, delimchar, pvpbuf, sizeof pvpbuf, NULL); 72453736Seric if (pvp == NULL) 7259536Seric { 72658403Seric /* don't need to give error -- prescan did that already */ 72736233Skarels # ifdef LOG 72858020Seric if (LogLevel > 2) 72936233Skarels syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 73036233Skarels # endif 7319536Seric finis(); 7329536Seric } 73365071Seric (void) rewrite(pvp, 3, 0, e); 73465071Seric (void) rewrite(pvp, 1, 0, e); 73565071Seric (void) rewrite(pvp, 4, 0, e); 73664147Seric bp = buf + 1; 73764147Seric cataddr(pvp, NULL, bp, sizeof buf - 2, '\0'); 73864147Seric if (*bp == '@') 73964147Seric { 74064147Seric /* heuristic: route-addr: add angle brackets */ 74164147Seric strcat(bp, ">"); 74264147Seric *--bp = '<'; 74364147Seric } 74464147Seric e->e_sender = newstr(bp); 74558704Seric define('f', e->e_sender, e); 7469536Seric 7479536Seric /* save the domain spec if this mailer wants it */ 74858704Seric if (!internal && e->e_from.q_mailer != NULL && 74953182Seric bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 7509536Seric { 7519536Seric extern char **copyplist(); 7529536Seric 7539536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 7549536Seric pvp++; 7559536Seric if (*pvp != NULL) 75653182Seric e->e_fromdomain = copyplist(pvp, TRUE); 7579536Seric } 7589536Seric } 759