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*68564Seric static char sccsid[] = "@(#)envelope.c 8.54 (Berkeley) 03/21/95"; 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; 7768498Seric bool failure_return = FALSE; 7868498Seric bool success_return = FALSE; 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); 8765089Seric 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 9558680Seric /* we must have an id to remove disk files */ 9657943Seric if (id == NULL) 9758680Seric return; 9857943Seric 999536Seric #ifdef LOG 10065089Seric if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags)) 10165089Seric logsender(e, NULL); 10258020Seric if (LogLevel > 84) 10365089Seric syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=0x%x, pid=%d", 10457943Seric id, e->e_flags, getpid()); 10556795Seric #endif /* LOG */ 10665089Seric e->e_flags &= ~EF_LOGSENDER; 1079536Seric 10863753Seric /* post statistics */ 10963753Seric poststats(StatFile); 11063753Seric 1119536Seric /* 1129536Seric ** Extract state information from dregs of send list. 1139536Seric */ 1149536Seric 11564743Seric e->e_flags &= ~EF_QUEUERUN; 1169536Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1179536Seric { 1189536Seric if (bitset(QQUEUEUP, q->q_flags)) 1199536Seric queueit = TRUE; 12068498Seric 12168498Seric /* see if a notification is needed */ 122*68564Seric if (bitset(QBADADDR, q->q_flags) && 12368498Seric bitset(QPINGONFAILURE, q->q_flags)) 12463839Seric { 12568498Seric failure_return = TRUE; 12668498Seric if (q->q_owner == NULL && !emptyaddr(&e->e_from)) 12763839Seric (void) sendtolist(e->e_from.q_paddr, NULL, 12868498Seric &e->e_errorqueue, 0, e); 12963839Seric } 13068498Seric else if (bitset(QSENT, q->q_flags) && 13168498Seric bitnset(M_LOCALMAILER, q->q_mailer->m_flags) && 13268498Seric bitset(QPINGONSUCCESS, q->q_flags)) 13368498Seric { 13468498Seric success_return = TRUE; 13568498Seric } 13668498Seric else if (bitset(QRELAYED, q->q_flags)) 13768498Seric { 13868498Seric success_return = TRUE; 13968498Seric } 14068498Seric else 14168559Seric { 14268498Seric continue; 14368498Seric } 1449536Seric } 1459536Seric 14668559Seric if (e->e_class < 0) 14768559Seric e->e_flags |= EF_NO_BODY_RETN; 14868559Seric 1499536Seric /* 15063753Seric ** See if the message timed out. 15163753Seric */ 15263753Seric 15363753Seric if (!queueit) 15463753Seric /* nothing to do */ ; 15568498Seric else if (curtime() > e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass]) 15663753Seric { 15763839Seric (void) sprintf(buf, "Cannot send message for %s", 15868498Seric pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE)); 15963839Seric if (e->e_message != NULL) 16063839Seric free(e->e_message); 16163839Seric e->e_message = newstr(buf); 16263839Seric message(buf); 16363839Seric e->e_flags |= EF_CLRQUEUE; 16468498Seric failure_return = TRUE; 16563787Seric fprintf(e->e_xfp, "Message could not be delivered for %s\n", 16668498Seric pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE)); 16763787Seric fprintf(e->e_xfp, "Message will be deleted from queue\n"); 16863787Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 16963787Seric { 17063787Seric if (bitset(QQUEUEUP, q->q_flags)) 17163787Seric q->q_flags |= QBADADDR; 17263787Seric } 17363753Seric } 17468498Seric else if (TimeOuts.to_q_warning[e->e_timeoutclass] > 0 && 17568498Seric curtime() > e->e_ctime + TimeOuts.to_q_warning[e->e_timeoutclass]) 17663753Seric { 17768498Seric bool delay_return = FALSE; 17868498Seric 17968498Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 18068498Seric { 18168498Seric if (bitset(QQUEUEUP, q->q_flags) && 18268498Seric bitset(QPINGONDELAY, q->q_flags)) 18368498Seric { 18468498Seric q->q_flags |= QREPORT; 18568498Seric delay_return = TRUE; 18668498Seric } 18768498Seric } 18868498Seric if (delay_return && 18968498Seric !bitset(EF_WARNING|EF_RESPONSE, e->e_flags) && 19063753Seric e->e_class >= 0 && 19168498Seric strcmp(e->e_from.q_paddr, "<>") != 0 && 19268498Seric strncasecmp(e->e_from.q_paddr, "owner-", 6) != 0 && 19368498Seric (strlen(e->e_from.q_paddr) <= 8 || 19468498Seric strcasecmp(&e->e_from.q_paddr[strlen(e->e_from.q_paddr) - 8], "-request") != 0)) 19563753Seric { 19663753Seric (void) sprintf(buf, 19768498Seric "Warning: cannot send message for %s", 19868498Seric pintvl(TimeOuts.to_q_warning[e->e_timeoutclass], FALSE)); 19963753Seric if (e->e_message != NULL) 20063753Seric free(e->e_message); 20163753Seric e->e_message = newstr(buf); 20263753Seric message(buf); 20363839Seric e->e_flags |= EF_WARNING; 20468498Seric failure_return = TRUE; 20563753Seric } 20663753Seric fprintf(e->e_xfp, 20763753Seric "Warning: message still undelivered after %s\n", 20868498Seric pintvl(TimeOuts.to_q_warning[e->e_timeoutclass], FALSE)); 20963753Seric fprintf(e->e_xfp, "Will keep trying until message is %s old\n", 21068498Seric pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE)); 21168498Seric } 21268498Seric 21368498Seric if (tTd(50, 2)) 21468498Seric printf("failure_return=%d success_return=%d queueit=%d\n", 21568498Seric failure_return, success_return, queueit); 21668498Seric 21768498Seric /* 21868498Seric ** If we had some fatal error, but no addresses are marked as 21968498Seric ** bad, mark them _all_ as bad. 22068498Seric */ 22168498Seric 22268498Seric if (bitset(EF_FATALERRS, e->e_flags) && !failure_return) 22368498Seric { 22468498Seric failure_return = TRUE; 22568019Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 22668019Seric { 22768498Seric if (!bitset(QDONTSEND, q->q_flags)) 22868498Seric q->q_flags |= QBADADDR; 22968019Seric } 23068019Seric } 23168019Seric 23268019Seric /* 2339536Seric ** Send back return receipts as requested. 2349536Seric */ 2359536Seric 23668498Seric /* 23766783Seric if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags) 23866783Seric && !bitset(PRIV_NORECEIPTS, PrivacyFlags)) 23968498Seric */ 24068498Seric if (e->e_receiptto == NULL) 24168498Seric e->e_receiptto = e->e_from.q_paddr; 24268498Seric if (success_return && !failure_return && 24368498Seric !bitset(PRIV_NORECEIPTS, PrivacyFlags) && 24468498Seric strcmp(e->e_receiptto, "<>") != 0) 2459536Seric { 24610844Seric auto ADDRESS *rlist = NULL; 2479536Seric 24868498Seric e->e_flags |= EF_SENDRECEIPT; 24968498Seric (void) sendtolist(e->e_receiptto, NULLADDR, &rlist, 0, e); 25068559Seric (void) returntosender("Return receipt", rlist, FALSE, e); 2519536Seric } 25268498Seric e->e_flags &= ~EF_SENDRECEIPT; 2539536Seric 2549536Seric /* 2559536Seric ** Arrange to send error messages if there are fatal errors. 2569536Seric */ 2579536Seric 25868498Seric if (failure_return && e->e_errormode != EM_QUIET) 25968559Seric savemail(e, !bitset(EF_NO_BODY_RETN, e->e_flags)); 2609536Seric 2619536Seric /* 26263849Seric ** Arrange to send warning messages to postmaster as requested. 26363849Seric */ 26463849Seric 26563849Seric if (bitset(EF_PM_NOTIFY, e->e_flags) && PostMasterCopy != NULL && 26664363Seric !bitset(EF_RESPONSE, e->e_flags) && e->e_class >= 0) 26763849Seric { 26863849Seric auto ADDRESS *rlist = NULL; 26963849Seric 27068498Seric (void) sendtolist(PostMasterCopy, NULLADDR, &rlist, 0, e); 27163849Seric (void) returntosender(e->e_message, rlist, FALSE, e); 27263849Seric } 27363849Seric 27463849Seric /* 2759536Seric ** Instantiate or deinstantiate the queue. 2769536Seric */ 2779536Seric 2789536Seric if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 2799536Seric bitset(EF_CLRQUEUE, e->e_flags)) 2809536Seric { 28164307Seric if (tTd(50, 1)) 28268498Seric printf("\n===== Dropping [dq]f%s (queueit=%d, e_flags=%x) =====\n\n", 28368498Seric e->e_id, queueit, e->e_flags); 284*68564Seric xunlink(queuename(e, 'd')); 2859536Seric xunlink(queuename(e, 'q')); 28663839Seric 28763839Seric #ifdef LOG 28863839Seric if (LogLevel > 10) 28963839Seric syslog(LOG_INFO, "%s: done", id); 29063839Seric #endif 2919536Seric } 2929536Seric else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 29310754Seric { 29410754Seric #ifdef QUEUE 29564307Seric queueup(e, bitset(EF_KEEPQUEUE, e->e_flags), FALSE); 29656795Seric #else /* QUEUE */ 29758151Seric syserr("554 dropenvelope: queueup"); 29856795Seric #endif /* QUEUE */ 29910754Seric } 3009536Seric 3019536Seric /* now unlock the job */ 30210196Seric closexscript(e); 3039536Seric unlockqueue(e); 3049536Seric 3059536Seric /* make sure that this envelope is marked unused */ 30624944Seric if (e->e_dfp != NULL) 307*68564Seric (void) xfclose(e->e_dfp, "dropenvelope df", e->e_id); 30810196Seric e->e_dfp = NULL; 309*68564Seric e->e_id = NULL; 310*68564Seric e->e_flags &= ~EF_HAS_DF; 3119536Seric } 3129536Seric /* 3139536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 3149536Seric ** 3159536Seric ** This is normally used by a child process to get a clean 3169536Seric ** envelope without disturbing the parent. 3179536Seric ** 3189536Seric ** Parameters: 3199536Seric ** e -- the envelope to clear. 32025611Seric ** fullclear - if set, the current envelope is total 32125611Seric ** garbage and should be ignored; otherwise, 32225611Seric ** release any resources it may indicate. 3239536Seric ** 3249536Seric ** Returns: 3259536Seric ** none. 3269536Seric ** 3279536Seric ** Side Effects: 3289536Seric ** Closes files associated with the envelope. 3299536Seric ** Marks the envelope as unallocated. 3309536Seric */ 3319536Seric 33260494Seric void 33325611Seric clearenvelope(e, fullclear) 3349536Seric register ENVELOPE *e; 33525611Seric bool fullclear; 3369536Seric { 33725514Seric register HDR *bh; 33825514Seric register HDR **nhp; 33925514Seric extern ENVELOPE BlankEnvelope; 34025514Seric 34125611Seric if (!fullclear) 34225611Seric { 34325611Seric /* clear out any file information */ 34425611Seric if (e->e_xfp != NULL) 34558680Seric (void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id); 34625611Seric if (e->e_dfp != NULL) 347*68564Seric (void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_id); 34858680Seric e->e_xfp = e->e_dfp = NULL; 34925611Seric } 3509536Seric 35124961Seric /* now clear out the data */ 35224965Seric STRUCTCOPY(BlankEnvelope, *e); 35359698Seric if (Verbose) 35459698Seric e->e_sendmode = SM_DELIVER; 35525514Seric bh = BlankEnvelope.e_header; 35625514Seric nhp = &e->e_header; 35725514Seric while (bh != NULL) 35825514Seric { 35925514Seric *nhp = (HDR *) xalloc(sizeof *bh); 36025514Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 36125514Seric bh = bh->h_link; 36225514Seric nhp = &(*nhp)->h_link; 36325514Seric } 3649536Seric } 3659536Seric /* 3669536Seric ** INITSYS -- initialize instantiation of system 3679536Seric ** 3689536Seric ** In Daemon mode, this is done in the child. 3699536Seric ** 3709536Seric ** Parameters: 3719536Seric ** none. 3729536Seric ** 3739536Seric ** Returns: 3749536Seric ** none. 3759536Seric ** 3769536Seric ** Side Effects: 3779536Seric ** Initializes the system macros, some global variables, 3789536Seric ** etc. In particular, the current time in various 3799536Seric ** forms is set. 3809536Seric */ 3819536Seric 38260494Seric void 38355012Seric initsys(e) 38455012Seric register ENVELOPE *e; 3859536Seric { 38664768Seric char cbuf[5]; /* holds hop count */ 38764768Seric char pbuf[10]; /* holds pid */ 38822963Smiriam #ifdef TTYNAME 38959304Seric static char ybuf[60]; /* holds tty id */ 3909536Seric register char *p; 39156795Seric #endif /* TTYNAME */ 3929536Seric extern char *ttyname(); 39360494Seric extern void settime(); 3949536Seric extern char Version[]; 3959536Seric 3969536Seric /* 3979536Seric ** Give this envelope a reality. 3989536Seric ** I.e., an id, a transcript, and a creation time. 3999536Seric */ 4009536Seric 40155012Seric openxscript(e); 40255012Seric e->e_ctime = curtime(); 4039536Seric 4049536Seric /* 4059536Seric ** Set OutChannel to something useful if stdout isn't it. 4069536Seric ** This arranges that any extra stuff the mailer produces 4079536Seric ** gets sent back to the user on error (because it is 4089536Seric ** tucked away in the transcript). 4099536Seric */ 4109536Seric 41164760Seric if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) && 41258737Seric e->e_xfp != NULL) 41355012Seric OutChannel = e->e_xfp; 4149536Seric 4159536Seric /* 4169536Seric ** Set up some basic system macros. 4179536Seric */ 4189536Seric 4199536Seric /* process id */ 4209536Seric (void) sprintf(pbuf, "%d", getpid()); 42164768Seric define('p', newstr(pbuf), e); 4229536Seric 4239536Seric /* hop count */ 42455012Seric (void) sprintf(cbuf, "%d", e->e_hopcount); 42564768Seric define('c', newstr(cbuf), e); 4269536Seric 4279536Seric /* time as integer, unix time, arpa time */ 42855012Seric settime(e); 4299536Seric 43017472Seric #ifdef TTYNAME 4319536Seric /* tty name */ 43255012Seric if (macvalue('y', e) == NULL) 4339536Seric { 4349536Seric p = ttyname(2); 4359536Seric if (p != NULL) 4369536Seric { 43756795Seric if (strrchr(p, '/') != NULL) 43856795Seric p = strrchr(p, '/') + 1; 4399536Seric (void) strcpy(ybuf, p); 44055012Seric define('y', ybuf, e); 4419536Seric } 4429536Seric } 44356795Seric #endif /* TTYNAME */ 4449536Seric } 4459536Seric /* 44611932Seric ** SETTIME -- set the current time. 44711932Seric ** 44811932Seric ** Parameters: 44911932Seric ** none. 45011932Seric ** 45111932Seric ** Returns: 45211932Seric ** none. 45311932Seric ** 45411932Seric ** Side Effects: 45511932Seric ** Sets the various time macros -- $a, $b, $d, $t. 45611932Seric */ 45711932Seric 45860494Seric void 45955012Seric settime(e) 46055012Seric register ENVELOPE *e; 46111932Seric { 46211932Seric register char *p; 46311932Seric auto time_t now; 46464768Seric char tbuf[20]; /* holds "current" time */ 46564768Seric char dbuf[30]; /* holds ctime(tbuf) */ 46611932Seric register struct tm *tm; 46711932Seric extern char *arpadate(); 46811932Seric extern struct tm *gmtime(); 46911932Seric 47011932Seric now = curtime(); 47111932Seric tm = gmtime(&now); 47257014Seric (void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900, 47357014Seric tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min); 47464768Seric define('t', newstr(tbuf), e); 47511932Seric (void) strcpy(dbuf, ctime(&now)); 47658131Seric p = strchr(dbuf, '\n'); 47758131Seric if (p != NULL) 47858131Seric *p = '\0'; 47964768Seric define('d', newstr(dbuf), e); 48064086Seric p = arpadate(dbuf); 48164086Seric p = newstr(p); 48255012Seric if (macvalue('a', e) == NULL) 48355012Seric define('a', p, e); 48455012Seric define('b', p, e); 48511932Seric } 48611932Seric /* 4879536Seric ** OPENXSCRIPT -- Open transcript file 4889536Seric ** 4899536Seric ** Creates a transcript file for possible eventual mailing or 4909536Seric ** sending back. 4919536Seric ** 4929536Seric ** Parameters: 4939536Seric ** e -- the envelope to create the transcript in/for. 4949536Seric ** 4959536Seric ** Returns: 4969536Seric ** none 4979536Seric ** 4989536Seric ** Side Effects: 4999536Seric ** Creates the transcript file. 5009536Seric */ 5019536Seric 50258803Seric #ifndef O_APPEND 50358803Seric #define O_APPEND 0 50458803Seric #endif 50558803Seric 50660494Seric void 5079536Seric openxscript(e) 5089536Seric register ENVELOPE *e; 5099536Seric { 5109536Seric register char *p; 51140933Srick int fd; 5129536Seric 5139536Seric if (e->e_xfp != NULL) 5149536Seric return; 5159536Seric p = queuename(e, 'x'); 51658803Seric fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644); 51740933Srick if (fd < 0) 51863753Seric { 51963753Seric syserr("Can't create transcript file %s", p); 52063753Seric fd = open("/dev/null", O_WRONLY, 0644); 52163753Seric if (fd < 0) 52263753Seric syserr("!Can't open /dev/null"); 52363753Seric } 52468352Seric e->e_xfp = fdopen(fd, "a"); 52564724Seric if (e->e_xfp == NULL) 52664724Seric syserr("!Can't create transcript stream %s", p); 52768498Seric #ifdef HASSETVBUF 52868498Seric setvbuf(e->e_xfp, NULL, _IOLBF, 0); 52968498Seric #else 53068498Seric setlinebuf(e->e_xfp); 53168498Seric #endif 53264743Seric if (tTd(46, 9)) 53364743Seric { 53464743Seric printf("openxscript(%s):\n ", p); 53564743Seric dumpfd(fileno(e->e_xfp), TRUE, FALSE); 53664743Seric } 5379536Seric } 5389536Seric /* 53910196Seric ** CLOSEXSCRIPT -- close the transcript file. 54010196Seric ** 54110196Seric ** Parameters: 54210196Seric ** e -- the envelope containing the transcript to close. 54310196Seric ** 54410196Seric ** Returns: 54510196Seric ** none. 54610196Seric ** 54710196Seric ** Side Effects: 54810196Seric ** none. 54910196Seric */ 55010196Seric 55160494Seric void 55210196Seric closexscript(e) 55310196Seric register ENVELOPE *e; 55410196Seric { 55510196Seric if (e->e_xfp == NULL) 55610196Seric return; 55758680Seric (void) xfclose(e->e_xfp, "closexscript", e->e_id); 55810196Seric e->e_xfp = NULL; 55910196Seric } 56010196Seric /* 5619536Seric ** SETSENDER -- set the person who this message is from 5629536Seric ** 5639536Seric ** Under certain circumstances allow the user to say who 5649536Seric ** s/he is (using -f or -r). These are: 5659536Seric ** 1. The user's uid is zero (root). 5669536Seric ** 2. The user's login name is in an approved list (typically 5679536Seric ** from a network server). 5689536Seric ** 3. The address the user is trying to claim has a 5699536Seric ** "!" character in it (since #2 doesn't do it for 5709536Seric ** us if we are dialing out for UUCP). 5719536Seric ** A better check to replace #3 would be if the 5729536Seric ** effective uid is "UUCP" -- this would require me 5739536Seric ** to rewrite getpwent to "grab" uucp as it went by, 5749536Seric ** make getname more nasty, do another passwd file 5759536Seric ** scan, or compile the UID of "UUCP" into the code, 5769536Seric ** all of which are reprehensible. 5779536Seric ** 5789536Seric ** Assuming all of these fail, we figure out something 5799536Seric ** ourselves. 5809536Seric ** 5819536Seric ** Parameters: 5829536Seric ** from -- the person we would like to believe this message 5839536Seric ** is from, as specified on the command line. 58453182Seric ** e -- the envelope in which we would like the sender set. 58558333Seric ** delimptr -- if non-NULL, set to the location of the 58658333Seric ** trailing delimiter. 58758704Seric ** internal -- set if this address is coming from an internal 58858704Seric ** source such as an owner alias. 5899536Seric ** 5909536Seric ** Returns: 59158704Seric ** none. 5929536Seric ** 5939536Seric ** Side Effects: 5949536Seric ** sets sendmail's notion of who the from person is. 5959536Seric */ 5969536Seric 59760494Seric void 59858704Seric setsender(from, e, delimptr, internal) 5999536Seric char *from; 60053182Seric register ENVELOPE *e; 60158333Seric char **delimptr; 60258704Seric bool internal; 6039536Seric { 6049536Seric register char **pvp; 6059536Seric char *realname = NULL; 60618665Seric register struct passwd *pw; 60758727Seric char delimchar; 60864147Seric char *bp; 60964147Seric char buf[MAXNAME + 2]; 61016913Seric char pvpbuf[PSBUFSIZE]; 61118665Seric extern struct passwd *getpwnam(); 6129536Seric extern char *FullName; 6139536Seric 6149536Seric if (tTd(45, 1)) 61514786Seric printf("setsender(%s)\n", from == NULL ? "" : from); 6169536Seric 6179536Seric /* 6189536Seric ** Figure out the real user executing us. 6199536Seric ** Username can return errno != 0 on non-errors. 6209536Seric */ 6219536Seric 62265580Seric if (bitset(EF_QUEUERUN, e->e_flags) || OpMode == MD_SMTP || 62365983Seric OpMode == MD_ARPAFTP || OpMode == MD_DAEMON) 6249536Seric realname = from; 6259536Seric if (realname == NULL || realname[0] == '\0') 6269536Seric realname = username(); 6279536Seric 62859027Seric if (ConfigLevel < 2) 62959027Seric SuprErrs = TRUE; 63059027Seric 63158727Seric delimchar = internal ? '\0' : ' '; 63264793Seric e->e_from.q_flags = QBADADDR; 63358333Seric if (from == NULL || 63464284Seric parseaddr(from, &e->e_from, RF_COPYALL|RF_SENDERADDR, 63564793Seric delimchar, delimptr, e) == NULL || 63664793Seric bitset(QBADADDR, e->e_from.q_flags) || 63764793Seric e->e_from.q_mailer == ProgMailer || 63864793Seric e->e_from.q_mailer == FileMailer || 63964793Seric e->e_from.q_mailer == InclMailer) 6409536Seric { 64121750Seric /* log garbage addresses for traceback */ 64255173Seric # ifdef LOG 64358020Seric if (from != NULL && LogLevel > 2) 64421750Seric { 64558951Seric char *p; 64658951Seric char ebuf[MAXNAME * 2 + 2]; 64755173Seric 64858951Seric p = macvalue('_', e); 64958951Seric if (p == NULL) 65058951Seric { 65158951Seric char *host = RealHostName; 65258951Seric if (host == NULL) 65358951Seric host = MyHostName; 65458951Seric (void) sprintf(ebuf, "%s@%s", realname, host); 65558951Seric p = ebuf; 65658951Seric } 65755173Seric syslog(LOG_NOTICE, 65864793Seric "setsender: %s: invalid or unparseable, received from %s", 65965015Seric shortenstring(from, 83), p); 66055173Seric } 66156795Seric # endif /* LOG */ 66257589Seric if (from != NULL) 66364793Seric { 66464793Seric if (!bitset(QBADADDR, e->e_from.q_flags)) 66564793Seric { 66664793Seric /* it was a bogus mailer in the from addr */ 66764793Seric usrerr("553 Invalid sender address"); 66864793Seric } 66957589Seric SuprErrs = TRUE; 67064793Seric } 67157589Seric if (from == realname || 67264284Seric parseaddr(from = newstr(realname), &e->e_from, 67364284Seric RF_COPYALL|RF_SENDERADDR, ' ', NULL, e) == NULL) 67424944Seric { 67564793Seric char nbuf[100]; 67664793Seric 67757589Seric SuprErrs = TRUE; 67868529Seric expand("\201n", nbuf, sizeof nbuf, e); 67964793Seric if (parseaddr(from = newstr(nbuf), &e->e_from, 68064793Seric RF_COPYALL, ' ', NULL, e) == NULL && 68164793Seric parseaddr(from = "postmaster", &e->e_from, 68264793Seric RF_COPYALL, ' ', NULL, e) == NULL) 68358151Seric syserr("553 setsender: can't even parse postmaster!"); 68424944Seric } 6859536Seric } 6869536Seric else 6879536Seric FromFlag = TRUE; 68853182Seric e->e_from.q_flags |= QDONTSEND; 68957731Seric if (tTd(45, 5)) 69057731Seric { 69157731Seric printf("setsender: QDONTSEND "); 69257731Seric printaddr(&e->e_from, FALSE); 69357731Seric } 6949536Seric SuprErrs = FALSE; 6959536Seric 69668498Seric # ifdef USERDB 69768498Seric if (bitnset(M_CHECKUDB, e->e_from.q_mailer->m_flags)) 69868460Seric { 69953736Seric register char *p; 70053736Seric extern char *udbsender(); 70117472Seric 70268498Seric p = udbsender(e->e_from.q_user); 70368498Seric if (p != NULL) 70468498Seric from = p; 70568498Seric } 70668498Seric # endif /* USERDB */ 70768498Seric 70868498Seric if (bitnset(M_HASPWENT, e->e_from.q_mailer->m_flags)) 70968498Seric { 71058704Seric if (!internal) 71158704Seric { 71268498Seric /* if the user already given fullname don't redefine */ 71358704Seric if (FullName == NULL) 71458704Seric FullName = macvalue('x', e); 71558704Seric if (FullName != NULL && FullName[0] == '\0') 71658704Seric FullName = NULL; 7179536Seric } 71853736Seric 71953736Seric if ((pw = getpwnam(e->e_from.q_user)) != NULL) 72053736Seric { 72153736Seric /* 72253736Seric ** Process passwd file entry. 72353736Seric */ 72453736Seric 72553736Seric /* extract home directory */ 72665822Seric if (strcmp(pw->pw_dir, "/") == 0) 72765822Seric e->e_from.q_home = newstr(""); 72865822Seric else 72965822Seric e->e_from.q_home = newstr(pw->pw_dir); 73053736Seric define('z', e->e_from.q_home, e); 73153736Seric 73253736Seric /* extract user and group id */ 73353736Seric e->e_from.q_uid = pw->pw_uid; 73453736Seric e->e_from.q_gid = pw->pw_gid; 73565023Seric e->e_from.q_flags |= QGOODUID; 73653736Seric 73753736Seric /* extract full name from passwd file */ 73853736Seric if (FullName == NULL && pw->pw_gecos != NULL && 73958704Seric strcmp(pw->pw_name, e->e_from.q_user) == 0 && 74058704Seric !internal) 74153736Seric { 74265015Seric buildfname(pw->pw_gecos, e->e_from.q_user, buf); 74353736Seric if (buf[0] != '\0') 74453736Seric FullName = newstr(buf); 74553736Seric } 74653736Seric } 74758704Seric if (FullName != NULL && !internal) 74853182Seric define('x', FullName, e); 7499536Seric } 75065580Seric else if (!internal && OpMode != MD_DAEMON) 75111625Seric { 75253182Seric if (e->e_from.q_home == NULL) 75365822Seric { 75453182Seric e->e_from.q_home = getenv("HOME"); 75566049Seric if (e->e_from.q_home != NULL && 75666049Seric strcmp(e->e_from.q_home, "/") == 0) 75765822Seric e->e_from.q_home++; 75865822Seric } 75963787Seric e->e_from.q_uid = RealUid; 76063787Seric e->e_from.q_gid = RealGid; 76165023Seric e->e_from.q_flags |= QGOODUID; 76211625Seric } 76311625Seric 7649536Seric /* 7659536Seric ** Rewrite the from person to dispose of possible implicit 7669536Seric ** links in the net. 7679536Seric */ 7689536Seric 76968498Seric pvp = prescan(from, delimchar, pvpbuf, sizeof pvpbuf, NULL); 7709536Seric if (pvp == NULL) 7719536Seric { 77258403Seric /* don't need to give error -- prescan did that already */ 77336233Skarels # ifdef LOG 77458020Seric if (LogLevel > 2) 77536233Skarels syslog(LOG_NOTICE, "cannot prescan from (%s)", from); 77636233Skarels # endif 7779536Seric finis(); 7789536Seric } 77968498Seric /* 78065071Seric (void) rewrite(pvp, 3, 0, e); 78165071Seric (void) rewrite(pvp, 1, 0, e); 78265071Seric (void) rewrite(pvp, 4, 0, e); 78368498Seric */ 78464147Seric bp = buf + 1; 78564147Seric cataddr(pvp, NULL, bp, sizeof buf - 2, '\0'); 78668498Seric if (*bp == '@' && !bitnset(M_NOBRACKET, e->e_from.q_mailer->m_flags)) 78764147Seric { 78864147Seric /* heuristic: route-addr: add angle brackets */ 78964147Seric strcat(bp, ">"); 79064147Seric *--bp = '<'; 79164147Seric } 79264147Seric e->e_sender = newstr(bp); 79358704Seric define('f', e->e_sender, e); 7949536Seric 7959536Seric /* save the domain spec if this mailer wants it */ 79665584Seric if (e->e_from.q_mailer != NULL && 79753182Seric bitnset(M_CANONICAL, e->e_from.q_mailer->m_flags)) 7989536Seric { 7999536Seric extern char **copyplist(); 8009536Seric 8019536Seric while (*pvp != NULL && strcmp(*pvp, "@") != 0) 8029536Seric pvp++; 8039536Seric if (*pvp != NULL) 80453182Seric e->e_fromdomain = copyplist(pvp, TRUE); 8059536Seric } 8069536Seric } 807