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*64808Seric static char sccsid[] = "@(#)envelope.c 8.17 (Berkeley) 10/31/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); 8758680Seric printf(", flags=%o\n", e->e_flags); 8863753Seric if (tTd(50, 10)) 8963753Seric { 9063753Seric printf("sendq="); 9163753Seric printaddr(e->e_sendqueue, TRUE); 9263753Seric } 939536Seric } 9457943Seric 9558680Seric /* we must have an id to remove disk files */ 9657943Seric if (id == NULL) 9758680Seric return; 9857943Seric 999536Seric #ifdef LOG 10058020Seric if (LogLevel > 84) 1019536Seric syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=%o, pid=%d", 10257943Seric id, e->e_flags, getpid()); 10356795Seric #endif /* LOG */ 1049536Seric 10563753Seric /* post statistics */ 10663753Seric poststats(StatFile); 10763753Seric 1089536Seric /* 1099536Seric ** Extract state information from dregs of send list. 1109536Seric */ 1119536Seric 11264743Seric e->e_flags &= ~EF_QUEUERUN; 1139536Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1149536Seric { 1159536Seric if (bitset(QQUEUEUP, q->q_flags)) 1169536Seric queueit = TRUE; 11763839Seric if (!bitset(QDONTSEND, q->q_flags) && 11863839Seric bitset(QBADADDR, q->q_flags)) 11963839Seric { 12063839Seric if (q->q_owner == NULL && 12163839Seric strcmp(e->e_from.q_paddr, "<>") != 0) 12263839Seric (void) sendtolist(e->e_from.q_paddr, NULL, 12363839Seric &e->e_errorqueue, e); 12463839Seric } 1259536Seric } 1269536Seric 1279536Seric /* 12863753Seric ** See if the message timed out. 12963753Seric */ 13063753Seric 13163753Seric if (!queueit) 13263753Seric /* nothing to do */ ; 13363753Seric else if (curtime() > e->e_ctime + TimeOuts.to_q_return) 13463753Seric { 13563839Seric (void) sprintf(buf, "Cannot send message for %s", 13663839Seric pintvl(TimeOuts.to_q_return, FALSE)); 13763839Seric if (e->e_message != NULL) 13863839Seric free(e->e_message); 13963839Seric e->e_message = newstr(buf); 14063839Seric message(buf); 14163839Seric e->e_flags |= EF_CLRQUEUE; 14263839Seric saveit = TRUE; 14363787Seric fprintf(e->e_xfp, "Message could not be delivered for %s\n", 14463787Seric pintvl(TimeOuts.to_q_return, FALSE)); 14563787Seric fprintf(e->e_xfp, "Message will be deleted from queue\n"); 14663787Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 14763787Seric { 14863787Seric if (bitset(QQUEUEUP, q->q_flags)) 14963787Seric q->q_flags |= QBADADDR; 15063787Seric } 15163753Seric } 15263753Seric else if (TimeOuts.to_q_warning > 0 && 15363753Seric curtime() > e->e_ctime + TimeOuts.to_q_warning) 15463753Seric { 15563753Seric if (!bitset(EF_WARNING|EF_RESPONSE, e->e_flags) && 15663753Seric e->e_class >= 0 && 15763753Seric strcmp(e->e_from.q_paddr, "<>") != 0) 15863753Seric { 15963753Seric (void) sprintf(buf, 16063753Seric "warning: cannot send message for %s", 16163753Seric pintvl(TimeOuts.to_q_warning, FALSE)); 16263753Seric if (e->e_message != NULL) 16363753Seric free(e->e_message); 16463753Seric e->e_message = newstr(buf); 16563753Seric message(buf); 16663839Seric e->e_flags |= EF_WARNING; 16763839Seric saveit = TRUE; 16863753Seric } 16963753Seric fprintf(e->e_xfp, 17063753Seric "Warning: message still undelivered after %s\n", 17163753Seric pintvl(TimeOuts.to_q_warning, FALSE)); 17263753Seric fprintf(e->e_xfp, "Will keep trying until message is %s old\n", 17363753Seric pintvl(TimeOuts.to_q_return, FALSE)); 17463787Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 17563787Seric { 17663787Seric if (bitset(QQUEUEUP, q->q_flags)) 17763787Seric q->q_flags |= QREPORT; 17863787Seric } 17963753Seric } 18063753Seric 18163753Seric /* 1829536Seric ** Send back return receipts as requested. 1839536Seric */ 1849536Seric 1859536Seric if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags)) 1869536Seric { 18710844Seric auto ADDRESS *rlist = NULL; 1889536Seric 18964284Seric (void) sendtolist(e->e_receiptto, NULLADDR, &rlist, e); 19055012Seric (void) returntosender("Return receipt", rlist, FALSE, e); 1919536Seric } 1929536Seric 1939536Seric /* 1949536Seric ** Arrange to send error messages if there are fatal errors. 1959536Seric */ 1969536Seric 19763839Seric if (saveit && e->e_errormode != EM_QUIET) 1989536Seric savemail(e); 1999536Seric 2009536Seric /* 20163849Seric ** Arrange to send warning messages to postmaster as requested. 20263849Seric */ 20363849Seric 20463849Seric if (bitset(EF_PM_NOTIFY, e->e_flags) && PostMasterCopy != NULL && 20564363Seric !bitset(EF_RESPONSE, e->e_flags) && e->e_class >= 0) 20663849Seric { 20763849Seric auto ADDRESS *rlist = NULL; 20863849Seric 20964284Seric (void) sendtolist(PostMasterCopy, NULLADDR, &rlist, e); 21063849Seric (void) returntosender(e->e_message, rlist, FALSE, e); 21163849Seric } 21263849Seric 21363849Seric /* 2149536Seric ** Instantiate or deinstantiate the queue. 2159536Seric */ 2169536Seric 2179536Seric if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 2189536Seric bitset(EF_CLRQUEUE, e->e_flags)) 2199536Seric { 22064307Seric if (tTd(50, 1)) 22164307Seric printf("\n===== Dropping [dq]f%s =====\n\n", e->e_id); 22223497Seric if (e->e_df != NULL) 22323497Seric xunlink(e->e_df); 2249536Seric xunlink(queuename(e, 'q')); 22563839Seric 22663839Seric #ifdef LOG 22763839Seric if (LogLevel > 10) 22863839Seric syslog(LOG_INFO, "%s: done", id); 22963839Seric #endif 2309536Seric } 2319536Seric else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 23210754Seric { 23310754Seric #ifdef QUEUE 23464307Seric queueup(e, bitset(EF_KEEPQUEUE, e->e_flags), FALSE); 23556795Seric #else /* QUEUE */ 23658151Seric syserr("554 dropenvelope: queueup"); 23756795Seric #endif /* QUEUE */ 23810754Seric } 2399536Seric 2409536Seric /* now unlock the job */ 24110196Seric closexscript(e); 2429536Seric unlockqueue(e); 2439536Seric 2449536Seric /* make sure that this envelope is marked unused */ 24524944Seric if (e->e_dfp != NULL) 24658680Seric (void) xfclose(e->e_dfp, "dropenvelope", e->e_df); 24710196Seric e->e_dfp = NULL; 24858680Seric e->e_id = e->e_df = NULL; 24964401Seric #ifdef XDEBUG 25064401Seric checkfd012("dropenvelope"); 25164401Seric #endif 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 56058737Seric if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP) 5619536Seric realname = from; 5629536Seric if (realname == NULL || realname[0] == '\0') 5639536Seric realname = username(); 5649536Seric 56559027Seric if (ConfigLevel < 2) 56659027Seric SuprErrs = TRUE; 56759027Seric 56858727Seric delimchar = internal ? '\0' : ' '; 56964793Seric e->e_from.q_flags = QBADADDR; 57058333Seric if (from == NULL || 57164284Seric parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR, 57264793Seric delimchar, delimptr, e) == NULL || 57364793Seric bitset(QBADADDR, e->e_from.q_flags) || 57464793Seric e->e_from.q_mailer == ProgMailer || 57564793Seric e->e_from.q_mailer == FileMailer || 57664793Seric e->e_from.q_mailer == InclMailer) 5779536Seric { 57821750Seric /* log garbage addresses for traceback */ 57955173Seric # ifdef LOG 58058020Seric if (from != NULL && LogLevel > 2) 58121750Seric { 58258951Seric char *p; 58358951Seric char ebuf[MAXNAME * 2 + 2]; 58455173Seric 58558951Seric p = macvalue('_', e); 58658951Seric if (p == NULL) 58758951Seric { 58858951Seric char *host = RealHostName; 58958951Seric if (host == NULL) 59058951Seric host = MyHostName; 59158951Seric (void) sprintf(ebuf, "%s@%s", realname, host); 59258951Seric p = ebuf; 59358951Seric } 59455173Seric syslog(LOG_NOTICE, 59564793Seric "setsender: %s: invalid or unparseable, received from %s", 59658951Seric from, p); 59755173Seric } 59856795Seric # endif /* LOG */ 59957589Seric if (from != NULL) 60064793Seric { 60164793Seric if (!bitset(QBADADDR, e->e_from.q_flags)) 60264793Seric { 60364793Seric /* it was a bogus mailer in the from addr */ 60464793Seric usrerr("553 Invalid sender address"); 60564793Seric } 60657589Seric SuprErrs = TRUE; 60764793Seric } 60857589Seric if (from == realname || 60964284Seric parseaddr(from = newstr(realname), &e->e_from, 61064284Seric RF_COPYALL|RF_SENDERADDR, ' ', NULL, e) == NULL) 61124944Seric { 61264793Seric char nbuf[100]; 61364793Seric 61457589Seric SuprErrs = TRUE; 615*64808Seric expand("\201n", nbuf, &nbuf[sizeof nbuf], e); 61664793Seric if (parseaddr(from = newstr(nbuf), &e->e_from, 61764793Seric RF_COPYALL, ' ', NULL, e) == NULL && 61864793Seric parseaddr(from = "postmaster", &e->e_from, 61964793Seric RF_COPYALL, ' ', NULL, e) == NULL) 62058151Seric syserr("553 setsender: can't even parse postmaster!"); 62124944Seric } 6229536Seric } 6239536Seric else 6249536Seric FromFlag = TRUE; 62553182Seric e->e_from.q_flags |= QDONTSEND; 62657731Seric if (tTd(45, 5)) 62757731Seric { 62857731Seric printf("setsender: QDONTSEND "); 62957731Seric printaddr(&e->e_from, FALSE); 63057731Seric } 6319536Seric SuprErrs = FALSE; 6329536Seric 63353736Seric pvp = NULL; 63453736Seric if (e->e_from.q_mailer == LocalMailer) 6359536Seric { 63653736Seric # ifdef USERDB 63753736Seric register char *p; 63853736Seric extern char *udbsender(); 63953736Seric # endif 64017472Seric 64158704Seric if (!internal) 64258704Seric { 64358704Seric /* if the user has given fullname already, don't redefine */ 64458704Seric if (FullName == NULL) 64558704Seric FullName = macvalue('x', e); 64658704Seric if (FullName != NULL && FullName[0] == '\0') 64758704Seric FullName = NULL; 6489536Seric 64953736Seric # ifdef USERDB 65064284Seric p = udbsender(e->e_from.q_user); 65153736Seric 65258704Seric if (p != NULL) 65358704Seric { 65458704Seric /* 65558704Seric ** We have an alternate address for the sender 65658704Seric */ 65753736Seric 65858704Seric pvp = prescan(p, '\0', pvpbuf, NULL); 65958704Seric } 66058704Seric # endif /* USERDB */ 6619536Seric } 66253736Seric 66353736Seric if ((pw = getpwnam(e->e_from.q_user)) != NULL) 66453736Seric { 66553736Seric /* 66653736Seric ** Process passwd file entry. 66753736Seric */ 66853736Seric 66953736Seric 67053736Seric /* extract home directory */ 67153736Seric e->e_from.q_home = newstr(pw->pw_dir); 67253736Seric define('z', e->e_from.q_home, e); 67353736Seric 67453736Seric /* extract user and group id */ 67553736Seric e->e_from.q_uid = pw->pw_uid; 67653736Seric e->e_from.q_gid = pw->pw_gid; 67753736Seric 67853736Seric /* extract full name from passwd file */ 67953736Seric if (FullName == NULL && pw->pw_gecos != NULL && 68058704Seric strcmp(pw->pw_name, e->e_from.q_user) == 0 && 68158704Seric !internal) 68253736Seric { 68353736Seric buildfname(pw->pw_gecos, e->e_from.q_user, buf); 68453736Seric if (buf[0] != '\0') 68553736Seric FullName = newstr(buf); 68653736Seric } 68753736Seric } 68858704Seric if (FullName != NULL && !internal) 68953182Seric define('x', FullName, e); 6909536Seric } 69158704Seric else if (!internal) 69211625Seric { 69353182Seric if (e->e_from.q_home == NULL) 69453182Seric e->e_from.q_home = getenv("HOME"); 69563787Seric e->e_from.q_uid = RealUid; 69663787Seric e->e_from.q_gid = RealGid; 69711625Seric } 69811625Seric 6999536Seric /* 7009536Seric ** Rewrite the from person to dispose of possible implicit 7019536Seric ** links in the net. 7029536Seric */ 7039536Seric 7049536Seric if (pvp == NULL) 70558333Seric pvp = prescan(from, '\0', pvpbuf, NULL); 70653736Seric if (pvp == NULL) 7079536Seric { 70858403Seric /* don't need to give error -- prescan did that already */ 70936233Skarels # ifdef LOG 71058020Seric if (LogLevel > 2) 71136233Skarels syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 71236233Skarels # endif 7139536Seric finis(); 7149536Seric } 71559084Seric (void) rewrite(pvp, 3, e); 71659084Seric (void) rewrite(pvp, 1, e); 71759084Seric (void) rewrite(pvp, 4, e); 71864147Seric bp = buf + 1; 71964147Seric cataddr(pvp, NULL, bp, sizeof buf - 2, '\0'); 72064147Seric if (*bp == '@') 72164147Seric { 72264147Seric /* heuristic: route-addr: add angle brackets */ 72364147Seric strcat(bp, ">"); 72464147Seric *--bp = '<'; 72564147Seric } 72664147Seric e->e_sender = newstr(bp); 72758704Seric define('f', e->e_sender, e); 7289536Seric 7299536Seric /* save the domain spec if this mailer wants it */ 73058704Seric if (!internal && e->e_from.q_mailer != NULL && 73153182Seric bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 7329536Seric { 7339536Seric extern char **copyplist(); 7349536Seric 7359536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 7369536Seric pvp++; 7379536Seric if (*pvp != NULL) 73853182Seric e->e_fromdomain = copyplist(pvp, TRUE); 7399536Seric } 7409536Seric } 741