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*68802Seric static char sccsid[] = "@(#)envelope.c 8.61 (Berkeley) 04/13/95"; 1133729Sbostic #endif /* not lint */ 1222704Sdist 1358332Seric #include "sendmail.h" 149536Seric 159536Seric /* 169536Seric ** NEWENVELOPE -- allocate a new envelope 179536Seric ** 189536Seric ** Supports inheritance. 199536Seric ** 209536Seric ** Parameters: 219536Seric ** e -- the new envelope to fill in. 2258179Seric ** parent -- the envelope to be the parent of e. 239536Seric ** 249536Seric ** Returns: 259536Seric ** e. 269536Seric ** 279536Seric ** Side Effects: 289536Seric ** none. 299536Seric */ 309536Seric 319536Seric ENVELOPE * 3258179Seric newenvelope(e, parent) 339536Seric register ENVELOPE *e; 3458179Seric register ENVELOPE *parent; 359536Seric { 369536Seric extern putheader(), putbody(); 3725611Seric extern ENVELOPE BlankEnvelope; 389536Seric 3958179Seric if (e == parent && e->e_parent != NULL) 409536Seric parent = e->e_parent; 4125611Seric clearenvelope(e, TRUE); 4224944Seric if (e == CurEnv) 4324944Seric bcopy((char *) &NullAddress, (char *) &e->e_from, sizeof e->e_from); 4424944Seric else 4524944Seric bcopy((char *) &CurEnv->e_from, (char *) &e->e_from, sizeof e->e_from); 469536Seric e->e_parent = parent; 479536Seric e->e_ctime = curtime(); 4856215Seric if (parent != NULL) 4956215Seric e->e_msgpriority = parent->e_msgsize; 509536Seric e->e_puthdr = putheader; 519536Seric e->e_putbody = putbody; 529536Seric if (CurEnv->e_xfp != NULL) 539536Seric (void) fflush(CurEnv->e_xfp); 549536Seric 559536Seric return (e); 569536Seric } 579536Seric /* 589536Seric ** DROPENVELOPE -- deallocate an envelope. 599536Seric ** 609536Seric ** Parameters: 619536Seric ** e -- the envelope to deallocate. 629536Seric ** 639536Seric ** Returns: 649536Seric ** none. 659536Seric ** 669536Seric ** Side Effects: 679536Seric ** housekeeping necessary to dispose of an envelope. 689536Seric ** Unlocks this queue file. 699536Seric */ 709536Seric 7160494Seric void 729536Seric dropenvelope(e) 739536Seric register ENVELOPE *e; 749536Seric { 759536Seric bool queueit = FALSE; 7668498Seric bool failure_return = FALSE; 7768498Seric bool success_return = FALSE; 789536Seric register ADDRESS *q; 7957943Seric char *id = e->e_id; 8063753Seric char buf[MAXLINE]; 819536Seric 829536Seric if (tTd(50, 1)) 839536Seric { 8468567Seric extern void printenvflags(); 8568567Seric 8658680Seric printf("dropenvelope %x: id=", e); 879536Seric xputs(e->e_id); 8868567Seric printf(", flags="); 8968567Seric printenvflags(e); 9063753Seric if (tTd(50, 10)) 9163753Seric { 9263753Seric printf("sendq="); 9363753Seric printaddr(e->e_sendqueue, TRUE); 9463753Seric } 959536Seric } 9657943Seric 9758680Seric /* we must have an id to remove disk files */ 9857943Seric if (id == NULL) 9958680Seric return; 10057943Seric 1019536Seric #ifdef LOG 10265089Seric if (LogLevel > 4 && bitset(EF_LOGSENDER, e->e_flags)) 10365089Seric logsender(e, NULL); 10458020Seric if (LogLevel > 84) 10565089Seric syslog(LOG_DEBUG, "dropenvelope, id=%s, flags=0x%x, pid=%d", 10657943Seric id, e->e_flags, getpid()); 10756795Seric #endif /* LOG */ 10865089Seric e->e_flags &= ~EF_LOGSENDER; 1099536Seric 11063753Seric /* post statistics */ 11163753Seric poststats(StatFile); 11263753Seric 1139536Seric /* 1149536Seric ** Extract state information from dregs of send list. 1159536Seric */ 1169536Seric 11764743Seric e->e_flags &= ~EF_QUEUERUN; 1189536Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 1199536Seric { 1209536Seric if (bitset(QQUEUEUP, q->q_flags)) 1219536Seric queueit = TRUE; 12268498Seric 12368498Seric /* see if a notification is needed */ 12468564Seric if (bitset(QBADADDR, q->q_flags) && 12568498Seric bitset(QPINGONFAILURE, q->q_flags)) 12663839Seric { 12768498Seric failure_return = TRUE; 12868498Seric if (q->q_owner == NULL && !emptyaddr(&e->e_from)) 12963839Seric (void) sendtolist(e->e_from.q_paddr, NULL, 13068498Seric &e->e_errorqueue, 0, e); 13163839Seric } 13268603Seric else if (bitset(QPINGONSUCCESS, q->q_flags) && 13368603Seric ((bitset(QSENT, q->q_flags) && 13468603Seric bitnset(M_LOCALMAILER, q->q_mailer->m_flags)) || 13568603Seric bitset(QRELAYED|QEXPLODED, q->q_flags))) 13668498Seric { 13768498Seric success_return = TRUE; 13868498Seric } 1399536Seric } 1409536Seric 14168559Seric if (e->e_class < 0) 14268559Seric e->e_flags |= EF_NO_BODY_RETN; 14368559Seric 1449536Seric /* 14563753Seric ** See if the message timed out. 14663753Seric */ 14763753Seric 14863753Seric if (!queueit) 14963753Seric /* nothing to do */ ; 15068498Seric else if (curtime() > e->e_ctime + TimeOuts.to_q_return[e->e_timeoutclass]) 15163753Seric { 15263839Seric (void) sprintf(buf, "Cannot send message for %s", 15368498Seric pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE)); 15463839Seric if (e->e_message != NULL) 15563839Seric free(e->e_message); 15663839Seric e->e_message = newstr(buf); 15763839Seric message(buf); 15863839Seric e->e_flags |= EF_CLRQUEUE; 15968498Seric failure_return = TRUE; 16063787Seric fprintf(e->e_xfp, "Message could not be delivered for %s\n", 16168498Seric pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE)); 16263787Seric fprintf(e->e_xfp, "Message will be deleted from queue\n"); 16363787Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 16463787Seric { 16563787Seric if (bitset(QQUEUEUP, q->q_flags)) 16663787Seric q->q_flags |= QBADADDR; 16763787Seric } 16863753Seric } 16968498Seric else if (TimeOuts.to_q_warning[e->e_timeoutclass] > 0 && 17068498Seric curtime() > e->e_ctime + TimeOuts.to_q_warning[e->e_timeoutclass]) 17163753Seric { 17268498Seric bool delay_return = FALSE; 17368498Seric 17468498Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 17568498Seric { 17668498Seric if (bitset(QQUEUEUP, q->q_flags) && 17768498Seric bitset(QPINGONDELAY, q->q_flags)) 17868498Seric { 17968498Seric q->q_flags |= QREPORT; 18068498Seric delay_return = TRUE; 18168498Seric } 18268498Seric } 18368498Seric if (delay_return && 18468498Seric !bitset(EF_WARNING|EF_RESPONSE, e->e_flags) && 18563753Seric e->e_class >= 0 && 18668498Seric strcmp(e->e_from.q_paddr, "<>") != 0 && 18768498Seric strncasecmp(e->e_from.q_paddr, "owner-", 6) != 0 && 18868706Seric (strlen(e->e_from.q_paddr) <= (SIZE_T) 8 || 18968498Seric strcasecmp(&e->e_from.q_paddr[strlen(e->e_from.q_paddr) - 8], "-request") != 0)) 19063753Seric { 19163753Seric (void) sprintf(buf, 19268498Seric "Warning: cannot send message for %s", 19368498Seric pintvl(TimeOuts.to_q_warning[e->e_timeoutclass], FALSE)); 19463753Seric if (e->e_message != NULL) 19563753Seric free(e->e_message); 19663753Seric e->e_message = newstr(buf); 19763753Seric message(buf); 19863839Seric e->e_flags |= EF_WARNING; 19968498Seric failure_return = TRUE; 20063753Seric } 20163753Seric fprintf(e->e_xfp, 20263753Seric "Warning: message still undelivered after %s\n", 20368498Seric pintvl(TimeOuts.to_q_warning[e->e_timeoutclass], FALSE)); 20463753Seric fprintf(e->e_xfp, "Will keep trying until message is %s old\n", 20568498Seric pintvl(TimeOuts.to_q_return[e->e_timeoutclass], FALSE)); 20668498Seric } 20768498Seric 20868498Seric if (tTd(50, 2)) 20968498Seric printf("failure_return=%d success_return=%d queueit=%d\n", 21068498Seric failure_return, success_return, queueit); 21168498Seric 21268498Seric /* 21368498Seric ** If we had some fatal error, but no addresses are marked as 21468498Seric ** bad, mark them _all_ as bad. 21568498Seric */ 21668498Seric 21768498Seric if (bitset(EF_FATALERRS, e->e_flags) && !failure_return) 21868498Seric { 21968498Seric failure_return = TRUE; 22068019Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 22168019Seric { 22268498Seric if (!bitset(QDONTSEND, q->q_flags)) 22368498Seric q->q_flags |= QBADADDR; 22468019Seric } 22568019Seric } 22668019Seric 22768019Seric /* 2289536Seric ** Send back return receipts as requested. 2299536Seric */ 2309536Seric 23168498Seric /* 23266783Seric if (e->e_receiptto != NULL && bitset(EF_SENDRECEIPT, e->e_flags) 23366783Seric && !bitset(PRIV_NORECEIPTS, PrivacyFlags)) 23468498Seric */ 23568498Seric if (e->e_receiptto == NULL) 23668498Seric e->e_receiptto = e->e_from.q_paddr; 23768498Seric if (success_return && !failure_return && 23868498Seric !bitset(PRIV_NORECEIPTS, PrivacyFlags) && 23968498Seric strcmp(e->e_receiptto, "<>") != 0) 2409536Seric { 24110844Seric auto ADDRESS *rlist = NULL; 2429536Seric 24368498Seric e->e_flags |= EF_SENDRECEIPT; 24468498Seric (void) sendtolist(e->e_receiptto, NULLADDR, &rlist, 0, e); 24568559Seric (void) returntosender("Return receipt", rlist, FALSE, e); 2469536Seric } 24768498Seric e->e_flags &= ~EF_SENDRECEIPT; 2489536Seric 2499536Seric /* 2509536Seric ** Arrange to send error messages if there are fatal errors. 2519536Seric */ 2529536Seric 25368498Seric if (failure_return && e->e_errormode != EM_QUIET) 25468559Seric savemail(e, !bitset(EF_NO_BODY_RETN, e->e_flags)); 2559536Seric 2569536Seric /* 25763849Seric ** Arrange to send warning messages to postmaster as requested. 25863849Seric */ 25963849Seric 260*68802Seric if ((failure_return || bitset(EF_PM_NOTIFY, e->e_flags)) && 261*68802Seric PostMasterCopy != NULL && 26264363Seric !bitset(EF_RESPONSE, e->e_flags) && e->e_class >= 0) 26363849Seric { 26463849Seric auto ADDRESS *rlist = NULL; 26563849Seric 26668498Seric (void) sendtolist(PostMasterCopy, NULLADDR, &rlist, 0, e); 26763849Seric (void) returntosender(e->e_message, rlist, FALSE, e); 26863849Seric } 26963849Seric 27063849Seric /* 2719536Seric ** Instantiate or deinstantiate the queue. 2729536Seric */ 2739536Seric 2749536Seric if ((!queueit && !bitset(EF_KEEPQUEUE, e->e_flags)) || 2759536Seric bitset(EF_CLRQUEUE, e->e_flags)) 2769536Seric { 27764307Seric if (tTd(50, 1)) 27868567Seric { 27968567Seric extern void printenvflags(); 28068567Seric 28168567Seric printf("\n===== Dropping [dq]f%s... queueit=%d, e_flags=", 28268567Seric e->e_id, queueit); 28368567Seric printenvflags(e); 28468567Seric } 28568564Seric xunlink(queuename(e, 'd')); 2869536Seric xunlink(queuename(e, 'q')); 28763839Seric 28863839Seric #ifdef LOG 28963839Seric if (LogLevel > 10) 29063839Seric syslog(LOG_INFO, "%s: done", id); 29163839Seric #endif 2929536Seric } 2939536Seric else if (queueit || !bitset(EF_INQUEUE, e->e_flags)) 29410754Seric { 29510754Seric #ifdef QUEUE 29664307Seric queueup(e, bitset(EF_KEEPQUEUE, e->e_flags), FALSE); 29756795Seric #else /* QUEUE */ 29858151Seric syserr("554 dropenvelope: queueup"); 29956795Seric #endif /* QUEUE */ 30010754Seric } 3019536Seric 3029536Seric /* now unlock the job */ 30310196Seric closexscript(e); 3049536Seric unlockqueue(e); 3059536Seric 3069536Seric /* make sure that this envelope is marked unused */ 30724944Seric if (e->e_dfp != NULL) 30868564Seric (void) xfclose(e->e_dfp, "dropenvelope df", e->e_id); 30910196Seric e->e_dfp = NULL; 31068564Seric e->e_id = NULL; 31168564Seric e->e_flags &= ~EF_HAS_DF; 3129536Seric } 3139536Seric /* 3149536Seric ** CLEARENVELOPE -- clear an envelope without unlocking 3159536Seric ** 3169536Seric ** This is normally used by a child process to get a clean 3179536Seric ** envelope without disturbing the parent. 3189536Seric ** 3199536Seric ** Parameters: 3209536Seric ** e -- the envelope to clear. 32125611Seric ** fullclear - if set, the current envelope is total 32225611Seric ** garbage and should be ignored; otherwise, 32325611Seric ** release any resources it may indicate. 3249536Seric ** 3259536Seric ** Returns: 3269536Seric ** none. 3279536Seric ** 3289536Seric ** Side Effects: 3299536Seric ** Closes files associated with the envelope. 3309536Seric ** Marks the envelope as unallocated. 3319536Seric */ 3329536Seric 33360494Seric void 33425611Seric clearenvelope(e, fullclear) 3359536Seric register ENVELOPE *e; 33625611Seric bool fullclear; 3379536Seric { 33825514Seric register HDR *bh; 33925514Seric register HDR **nhp; 34025514Seric extern ENVELOPE BlankEnvelope; 34125514Seric 34225611Seric if (!fullclear) 34325611Seric { 34425611Seric /* clear out any file information */ 34525611Seric if (e->e_xfp != NULL) 34658680Seric (void) xfclose(e->e_xfp, "clearenvelope xfp", e->e_id); 34725611Seric if (e->e_dfp != NULL) 34868564Seric (void) xfclose(e->e_dfp, "clearenvelope dfp", e->e_id); 34958680Seric e->e_xfp = e->e_dfp = NULL; 35025611Seric } 3519536Seric 35224961Seric /* now clear out the data */ 35324965Seric STRUCTCOPY(BlankEnvelope, *e); 35459698Seric if (Verbose) 35559698Seric e->e_sendmode = SM_DELIVER; 35625514Seric bh = BlankEnvelope.e_header; 35725514Seric nhp = &e->e_header; 35825514Seric while (bh != NULL) 35925514Seric { 36025514Seric *nhp = (HDR *) xalloc(sizeof *bh); 36125514Seric bcopy((char *) bh, (char *) *nhp, sizeof *bh); 36225514Seric bh = bh->h_link; 36325514Seric nhp = &(*nhp)->h_link; 36425514Seric } 3659536Seric } 3669536Seric /* 3679536Seric ** INITSYS -- initialize instantiation of system 3689536Seric ** 3699536Seric ** In Daemon mode, this is done in the child. 3709536Seric ** 3719536Seric ** Parameters: 3729536Seric ** none. 3739536Seric ** 3749536Seric ** Returns: 3759536Seric ** none. 3769536Seric ** 3779536Seric ** Side Effects: 3789536Seric ** Initializes the system macros, some global variables, 3799536Seric ** etc. In particular, the current time in various 3809536Seric ** forms is set. 3819536Seric */ 3829536Seric 38360494Seric void 38455012Seric initsys(e) 38555012Seric register ENVELOPE *e; 3869536Seric { 38764768Seric char cbuf[5]; /* holds hop count */ 38864768Seric char pbuf[10]; /* holds pid */ 38922963Smiriam #ifdef TTYNAME 39059304Seric static char ybuf[60]; /* holds tty id */ 3919536Seric register char *p; 39256795Seric #endif /* TTYNAME */ 3939536Seric extern char *ttyname(); 39460494Seric extern void settime(); 3959536Seric extern char Version[]; 3969536Seric 3979536Seric /* 3989536Seric ** Give this envelope a reality. 3999536Seric ** I.e., an id, a transcript, and a creation time. 4009536Seric */ 4019536Seric 40255012Seric openxscript(e); 40355012Seric e->e_ctime = curtime(); 4049536Seric 4059536Seric /* 4069536Seric ** Set OutChannel to something useful if stdout isn't it. 4079536Seric ** This arranges that any extra stuff the mailer produces 4089536Seric ** gets sent back to the user on error (because it is 4099536Seric ** tucked away in the transcript). 4109536Seric */ 4119536Seric 41264760Seric if (OpMode == MD_DAEMON && bitset(EF_QUEUERUN, e->e_flags) && 41358737Seric e->e_xfp != NULL) 41455012Seric OutChannel = e->e_xfp; 4159536Seric 4169536Seric /* 4179536Seric ** Set up some basic system macros. 4189536Seric */ 4199536Seric 4209536Seric /* process id */ 4219536Seric (void) sprintf(pbuf, "%d", getpid()); 42264768Seric define('p', newstr(pbuf), e); 4239536Seric 4249536Seric /* hop count */ 42555012Seric (void) sprintf(cbuf, "%d", e->e_hopcount); 42664768Seric define('c', newstr(cbuf), e); 4279536Seric 4289536Seric /* time as integer, unix time, arpa time */ 42955012Seric settime(e); 4309536Seric 43117472Seric #ifdef TTYNAME 4329536Seric /* tty name */ 43355012Seric if (macvalue('y', e) == NULL) 4349536Seric { 4359536Seric p = ttyname(2); 4369536Seric if (p != NULL) 4379536Seric { 43856795Seric if (strrchr(p, '/') != NULL) 43956795Seric p = strrchr(p, '/') + 1; 4409536Seric (void) strcpy(ybuf, p); 44155012Seric define('y', ybuf, e); 4429536Seric } 4439536Seric } 44456795Seric #endif /* TTYNAME */ 4459536Seric } 4469536Seric /* 44711932Seric ** SETTIME -- set the current time. 44811932Seric ** 44911932Seric ** Parameters: 45011932Seric ** none. 45111932Seric ** 45211932Seric ** Returns: 45311932Seric ** none. 45411932Seric ** 45511932Seric ** Side Effects: 45611932Seric ** Sets the various time macros -- $a, $b, $d, $t. 45711932Seric */ 45811932Seric 45960494Seric void 46055012Seric settime(e) 46155012Seric register ENVELOPE *e; 46211932Seric { 46311932Seric register char *p; 46411932Seric auto time_t now; 46564768Seric char tbuf[20]; /* holds "current" time */ 46664768Seric char dbuf[30]; /* holds ctime(tbuf) */ 46711932Seric register struct tm *tm; 46811932Seric extern char *arpadate(); 46911932Seric extern struct tm *gmtime(); 47011932Seric 47111932Seric now = curtime(); 47211932Seric tm = gmtime(&now); 47357014Seric (void) sprintf(tbuf, "%04d%02d%02d%02d%02d", tm->tm_year + 1900, 47457014Seric tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min); 47564768Seric define('t', newstr(tbuf), e); 47611932Seric (void) strcpy(dbuf, ctime(&now)); 47758131Seric p = strchr(dbuf, '\n'); 47858131Seric if (p != NULL) 47958131Seric *p = '\0'; 48064768Seric define('d', newstr(dbuf), e); 48164086Seric p = arpadate(dbuf); 48264086Seric p = newstr(p); 48355012Seric if (macvalue('a', e) == NULL) 48455012Seric define('a', p, e); 48555012Seric define('b', p, e); 48611932Seric } 48711932Seric /* 4889536Seric ** OPENXSCRIPT -- Open transcript file 4899536Seric ** 4909536Seric ** Creates a transcript file for possible eventual mailing or 4919536Seric ** sending back. 4929536Seric ** 4939536Seric ** Parameters: 4949536Seric ** e -- the envelope to create the transcript in/for. 4959536Seric ** 4969536Seric ** Returns: 4979536Seric ** none 4989536Seric ** 4999536Seric ** Side Effects: 5009536Seric ** Creates the transcript file. 5019536Seric */ 5029536Seric 50358803Seric #ifndef O_APPEND 50458803Seric #define O_APPEND 0 50558803Seric #endif 50658803Seric 50760494Seric void 5089536Seric openxscript(e) 5099536Seric register ENVELOPE *e; 5109536Seric { 5119536Seric register char *p; 51240933Srick int fd; 5139536Seric 5149536Seric if (e->e_xfp != NULL) 5159536Seric return; 5169536Seric p = queuename(e, 'x'); 51758803Seric fd = open(p, O_WRONLY|O_CREAT|O_APPEND, 0644); 51840933Srick if (fd < 0) 51963753Seric { 52063753Seric syserr("Can't create transcript file %s", p); 52163753Seric fd = open("/dev/null", O_WRONLY, 0644); 52263753Seric if (fd < 0) 52363753Seric syserr("!Can't open /dev/null"); 52463753Seric } 52568352Seric e->e_xfp = fdopen(fd, "a"); 52664724Seric if (e->e_xfp == NULL) 52764724Seric syserr("!Can't create transcript stream %s", p); 52868498Seric #ifdef HASSETVBUF 52968498Seric setvbuf(e->e_xfp, NULL, _IOLBF, 0); 53068498Seric #else 53168498Seric setlinebuf(e->e_xfp); 53268498Seric #endif 53364743Seric if (tTd(46, 9)) 53464743Seric { 53564743Seric printf("openxscript(%s):\n ", p); 53664743Seric dumpfd(fileno(e->e_xfp), TRUE, FALSE); 53764743Seric } 5389536Seric } 5399536Seric /* 54010196Seric ** CLOSEXSCRIPT -- close the transcript file. 54110196Seric ** 54210196Seric ** Parameters: 54310196Seric ** e -- the envelope containing the transcript to close. 54410196Seric ** 54510196Seric ** Returns: 54610196Seric ** none. 54710196Seric ** 54810196Seric ** Side Effects: 54910196Seric ** none. 55010196Seric */ 55110196Seric 55260494Seric void 55310196Seric closexscript(e) 55410196Seric register ENVELOPE *e; 55510196Seric { 55610196Seric if (e->e_xfp == NULL) 55710196Seric return; 55858680Seric (void) xfclose(e->e_xfp, "closexscript", e->e_id); 55910196Seric e->e_xfp = NULL; 56010196Seric } 56110196Seric /* 5629536Seric ** SETSENDER -- set the person who this message is from 5639536Seric ** 5649536Seric ** Under certain circumstances allow the user to say who 5659536Seric ** s/he is (using -f or -r). These are: 5669536Seric ** 1. The user's uid is zero (root). 5679536Seric ** 2. The user's login name is in an approved list (typically 5689536Seric ** from a network server). 5699536Seric ** 3. The address the user is trying to claim has a 5709536Seric ** "!" character in it (since #2 doesn't do it for 5719536Seric ** us if we are dialing out for UUCP). 5729536Seric ** A better check to replace #3 would be if the 5739536Seric ** effective uid is "UUCP" -- this would require me 5749536Seric ** to rewrite getpwent to "grab" uucp as it went by, 5759536Seric ** make getname more nasty, do another passwd file 5769536Seric ** scan, or compile the UID of "UUCP" into the code, 5779536Seric ** all of which are reprehensible. 5789536Seric ** 5799536Seric ** Assuming all of these fail, we figure out something 5809536Seric ** ourselves. 5819536Seric ** 5829536Seric ** Parameters: 5839536Seric ** from -- the person we would like to believe this message 5849536Seric ** is from, as specified on the command line. 58553182Seric ** e -- the envelope in which we would like the sender set. 58658333Seric ** delimptr -- if non-NULL, set to the location of the 58758333Seric ** trailing delimiter. 58858704Seric ** internal -- set if this address is coming from an internal 58958704Seric ** source such as an owner alias. 5909536Seric ** 5919536Seric ** Returns: 59258704Seric ** none. 5939536Seric ** 5949536Seric ** Side Effects: 5959536Seric ** sets sendmail's notion of who the from person is. 5969536Seric */ 5979536Seric 59860494Seric void 59958704Seric setsender(from, e, delimptr, internal) 6009536Seric char *from; 60153182Seric register ENVELOPE *e; 60258333Seric char **delimptr; 60358704Seric bool internal; 6049536Seric { 6059536Seric register char **pvp; 6069536Seric char *realname = NULL; 60718665Seric register struct passwd *pw; 60858727Seric char delimchar; 60964147Seric char *bp; 61064147Seric char buf[MAXNAME + 2]; 61116913Seric char pvpbuf[PSBUFSIZE]; 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 71968693Seric if ((pw = sm_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 76968711Seric pvp = prescan(from, delimchar, pvpbuf, sizeof pvpbuf, NULL, 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 { 79968736Seric char **lastat; 8009536Seric extern char **copyplist(); 8019536Seric 80268736Seric /* get rid of any pesky angle brackets */ 80368736Seric (void) rewrite(pvp, 3, 0, e); 80468736Seric (void) rewrite(pvp, 1, 0, e); 80568736Seric (void) rewrite(pvp, 4, 0, e); 80668736Seric 80768736Seric /* strip off to the last "@" sign */ 80868736Seric for (lastat = NULL; *pvp != NULL; pvp++) 80968736Seric if (strcmp(*pvp, "@") == 0) 81068736Seric lastat = pvp; 81168736Seric if (lastat != NULL) 81268736Seric { 81368736Seric e->e_fromdomain = copyplist(lastat, TRUE); 81468736Seric if (tTd(45, 3)) 81568736Seric { 81668736Seric printf("Saving from domain: "); 81768736Seric printav(e->e_fromdomain); 81868736Seric } 81968736Seric } 8209536Seric } 8219536Seric } 82268567Seric /* 82368567Seric ** PRINTENVFLAGS -- print envelope flags for debugging 82468567Seric ** 82568567Seric ** Parameters: 82668567Seric ** e -- the envelope with the flags to be printed. 82768567Seric ** 82868567Seric ** Returns: 82968567Seric ** none. 83068567Seric */ 83168567Seric 83268567Seric struct eflags 83368567Seric { 83468567Seric char *ef_name; 83568567Seric u_long ef_bit; 83668567Seric }; 83768567Seric 83868567Seric struct eflags EnvelopeFlags[] = 83968567Seric { 84068567Seric "OLDSTYLE", EF_OLDSTYLE, 84168567Seric "INQUEUE", EF_INQUEUE, 84268567Seric "NO_BODY_RETN", EF_NO_BODY_RETN, 84368567Seric "CLRQUEUE", EF_CLRQUEUE, 84468567Seric "SENDRECEIPT", EF_SENDRECEIPT, 84568567Seric "FATALERRS", EF_FATALERRS, 84668567Seric "KEEPQUEUE", EF_KEEPQUEUE, 84768567Seric "RESPONSE", EF_RESPONSE, 84868567Seric "RESENT", EF_RESENT, 84968567Seric "VRFYONLY", EF_VRFYONLY, 85068567Seric "WARNING", EF_WARNING, 85168567Seric "QUEUERUN", EF_QUEUERUN, 85268567Seric "GLOBALERRS", EF_GLOBALERRS, 85368567Seric "PM_NOTIFY", EF_PM_NOTIFY, 85468567Seric "METOO", EF_METOO, 85568567Seric "LOGSENDER", EF_LOGSENDER, 85668567Seric "NORECEIPT", EF_NORECEIPT, 85768567Seric "HAS8BIT", EF_HAS8BIT, 85868567Seric "NL_NOT_EOL", EF_NL_NOT_EOL, 85968567Seric "CRLF_NOT_EOL", EF_CRLF_NOT_EOL, 86068567Seric "RET_PARAM", EF_RET_PARAM, 86168567Seric "HAS_DF", EF_HAS_DF, 86268567Seric NULL 86368567Seric }; 86468567Seric 86568567Seric void 86668567Seric printenvflags(e) 86768567Seric register ENVELOPE *e; 86868567Seric { 86968567Seric register struct eflags *ef; 87068567Seric bool first = TRUE; 87168567Seric 87268567Seric printf("%lx", e->e_flags); 87368567Seric for (ef = EnvelopeFlags; ef->ef_name != NULL; ef++) 87468567Seric { 87568567Seric if (!bitset(ef->ef_bit, e->e_flags)) 87668567Seric continue; 87768567Seric if (first) 87868567Seric printf("<%s", ef->ef_name); 87968567Seric else 88068567Seric printf(",%s", ef->ef_name); 88168567Seric first = FALSE; 88268567Seric } 88368567Seric if (!first) 88468567Seric printf(">\n"); 88568567Seric } 886