122701Sdist /* 234920Sbostic * Copyright (c) 1983 Eric P. Allman 362523Sbostic * Copyright (c) 1988, 1993 462523Sbostic * The Regents of the University of California. All rights reserved. 533728Sbostic * 642825Sbostic * %sccs.include.redist.c% 733728Sbostic */ 822701Sdist 922701Sdist #ifndef lint 10*65545Seric static char sccsid[] = "@(#)deliver.c 8.60 (Berkeley) 01/06/94"; 1133728Sbostic #endif /* not lint */ 1222701Sdist 1340961Sbostic #include "sendmail.h" 1433931Sbostic #include <netdb.h> 1533931Sbostic #include <errno.h> 1635651Seric #ifdef NAMED_BIND 1734022Sbostic #include <arpa/nameser.h> 1834022Sbostic #include <resolv.h> 1963753Seric 2063753Seric extern int h_errno; 2135651Seric #endif 22294Seric 2365075Seric extern char SmtpError[]; 2465075Seric 25294Seric /* 2658820Seric ** SENDALL -- actually send all the messages. 2758820Seric ** 2858820Seric ** Parameters: 2958820Seric ** e -- the envelope to send. 3058820Seric ** mode -- the delivery mode to use. If SM_DEFAULT, use 3158820Seric ** the current e->e_sendmode. 3258820Seric ** 3358820Seric ** Returns: 3458820Seric ** none. 3558820Seric ** 3658820Seric ** Side Effects: 3758820Seric ** Scans the send lists and sends everything it finds. 3858820Seric ** Delivers any appropriate error messages. 3958820Seric ** If we are running in a non-interactive mode, takes the 4058820Seric ** appropriate action. 4158820Seric */ 4258820Seric 4358820Seric sendall(e, mode) 4458820Seric ENVELOPE *e; 4558820Seric char mode; 4658820Seric { 4758820Seric register ADDRESS *q; 4858820Seric char *owner; 4958820Seric int otherowners; 5058820Seric register ENVELOPE *ee; 5158820Seric ENVELOPE *splitenv = NULL; 5258929Seric bool announcequeueup; 5358820Seric 5463839Seric /* 5563839Seric ** If we have had global, fatal errors, don't bother sending 5663839Seric ** the message at all if we are in SMTP mode. Local errors 5763839Seric ** (e.g., a single address failing) will still cause the other 5863839Seric ** addresses to be sent. 5963839Seric */ 6063839Seric 6163839Seric if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP) 6261092Seric { 6361092Seric e->e_flags |= EF_CLRQUEUE; 6461092Seric return; 6561092Seric } 6661092Seric 6758820Seric /* determine actual delivery mode */ 6864826Seric CurrentLA = getla(); 6958820Seric if (mode == SM_DEFAULT) 7058820Seric { 7158820Seric mode = e->e_sendmode; 7258820Seric if (mode != SM_VERIFY && 7358820Seric shouldqueue(e->e_msgpriority, e->e_ctime)) 7458820Seric mode = SM_QUEUE; 7558929Seric announcequeueup = mode == SM_QUEUE; 7658820Seric } 7758929Seric else 7858929Seric announcequeueup = FALSE; 7958820Seric 8058820Seric if (tTd(13, 1)) 8158820Seric { 8264310Seric printf("\n===== SENDALL: mode %c, id %s, e_from ", 8364310Seric mode, e->e_id); 8458820Seric printaddr(&e->e_from, FALSE); 8558820Seric printf("sendqueue:\n"); 8658820Seric printaddr(e->e_sendqueue, TRUE); 8758820Seric } 8858820Seric 8958820Seric /* 9058820Seric ** Do any preprocessing necessary for the mode we are running. 9158820Seric ** Check to make sure the hop count is reasonable. 9258820Seric ** Delete sends to the sender in mailing lists. 9358820Seric */ 9458820Seric 9558820Seric CurEnv = e; 9658820Seric 9758820Seric if (e->e_hopcount > MaxHopCount) 9858820Seric { 9958820Seric errno = 0; 10064495Seric syserr("554 too many hops %d (%d max): from %s via %s, to %s", 10158820Seric e->e_hopcount, MaxHopCount, e->e_from.q_paddr, 10264495Seric RealHostName, e->e_sendqueue->q_paddr); 10358820Seric return; 10458820Seric } 10558820Seric 10659435Seric /* 10759435Seric ** Do sender deletion. 10859435Seric ** 10959435Seric ** If the sender has the QQUEUEUP flag set, skip this. 11059435Seric ** This can happen if the name server is hosed when you 11159435Seric ** are trying to send mail. The result is that the sender 11259435Seric ** is instantiated in the queue as a recipient. 11359435Seric */ 11459435Seric 11564118Seric if (!bitset(EF_METOO, e->e_flags) && 11664118Seric !bitset(QQUEUEUP, e->e_from.q_flags)) 11758820Seric { 11858820Seric if (tTd(13, 5)) 11958820Seric { 12058820Seric printf("sendall: QDONTSEND "); 12158820Seric printaddr(&e->e_from, FALSE); 12258820Seric } 12358820Seric e->e_from.q_flags |= QDONTSEND; 12458820Seric (void) recipient(&e->e_from, &e->e_sendqueue, e); 12558820Seric } 12658820Seric 12758820Seric /* 12858820Seric ** Handle alias owners. 12958820Seric ** 13058820Seric ** We scan up the q_alias chain looking for owners. 13158820Seric ** We discard owners that are the same as the return path. 13258820Seric */ 13358820Seric 13458820Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 13558820Seric { 13658820Seric register struct address *a; 13758820Seric 13858820Seric for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias) 13958820Seric continue; 14058820Seric if (a != NULL) 14158820Seric q->q_owner = a->q_owner; 14258820Seric 14358820Seric if (q->q_owner != NULL && 14458820Seric !bitset(QDONTSEND, q->q_flags) && 14558820Seric strcmp(q->q_owner, e->e_from.q_paddr) == 0) 14658820Seric q->q_owner = NULL; 14758820Seric } 14858820Seric 14958820Seric owner = ""; 15058820Seric otherowners = 1; 15158820Seric while (owner != NULL && otherowners > 0) 15258820Seric { 15358820Seric owner = NULL; 15458820Seric otherowners = 0; 15558820Seric 15658820Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 15758820Seric { 15858820Seric if (bitset(QDONTSEND, q->q_flags)) 15958820Seric continue; 16058820Seric 16158820Seric if (q->q_owner != NULL) 16258820Seric { 16358820Seric if (owner == NULL) 16458820Seric owner = q->q_owner; 16558820Seric else if (owner != q->q_owner) 16658820Seric { 16758820Seric if (strcmp(owner, q->q_owner) == 0) 16858820Seric { 16958820Seric /* make future comparisons cheap */ 17058820Seric q->q_owner = owner; 17158820Seric } 17258820Seric else 17358820Seric { 17458820Seric otherowners++; 17558820Seric } 17658820Seric owner = q->q_owner; 17758820Seric } 17858820Seric } 17958820Seric else 18058820Seric { 18158820Seric otherowners++; 18258820Seric } 18358820Seric } 18458820Seric 18558820Seric if (owner != NULL && otherowners > 0) 18658820Seric { 18758820Seric extern HDR *copyheader(); 18858820Seric extern ADDRESS *copyqueue(); 18958820Seric 19058820Seric /* 19158820Seric ** Split this envelope into two. 19258820Seric */ 19358820Seric 19458820Seric ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE)); 19558820Seric *ee = *e; 19658820Seric ee->e_id = NULL; 19758820Seric (void) queuename(ee, '\0'); 19858820Seric 19958820Seric if (tTd(13, 1)) 20058820Seric printf("sendall: split %s into %s\n", 20158820Seric e->e_id, ee->e_id); 20258820Seric 20358820Seric ee->e_header = copyheader(e->e_header); 20458820Seric ee->e_sendqueue = copyqueue(e->e_sendqueue); 20558820Seric ee->e_errorqueue = copyqueue(e->e_errorqueue); 20658916Seric ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS); 20758820Seric setsender(owner, ee, NULL, TRUE); 20858820Seric if (tTd(13, 5)) 20958820Seric { 21058820Seric printf("sendall(split): QDONTSEND "); 21158820Seric printaddr(&ee->e_from, FALSE); 21258820Seric } 21358820Seric ee->e_from.q_flags |= QDONTSEND; 21458820Seric ee->e_dfp = NULL; 21558820Seric ee->e_xfp = NULL; 21658820Seric ee->e_df = NULL; 21758820Seric ee->e_errormode = EM_MAIL; 21858820Seric ee->e_sibling = splitenv; 21958820Seric splitenv = ee; 22058820Seric 22158820Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 22258820Seric if (q->q_owner == owner) 22358820Seric q->q_flags |= QDONTSEND; 22458820Seric for (q = ee->e_sendqueue; q != NULL; q = q->q_next) 22558820Seric if (q->q_owner != owner) 22658820Seric q->q_flags |= QDONTSEND; 22758820Seric 22858820Seric if (e->e_df != NULL && mode != SM_VERIFY) 22958820Seric { 23058820Seric ee->e_dfp = NULL; 23164086Seric ee->e_df = queuename(ee, 'd'); 23264086Seric ee->e_df = newstr(ee->e_df); 23358820Seric if (link(e->e_df, ee->e_df) < 0) 23458820Seric { 23558820Seric syserr("sendall: link(%s, %s)", 23658820Seric e->e_df, ee->e_df); 23758820Seric } 23858820Seric } 23958820Seric 24058820Seric if (mode != SM_VERIFY) 24158820Seric openxscript(ee); 24258820Seric #ifdef LOG 24358820Seric if (LogLevel > 4) 24458820Seric syslog(LOG_INFO, "%s: clone %s", 24558820Seric ee->e_id, e->e_id); 24658820Seric #endif 24758820Seric } 24858820Seric } 24958820Seric 25058820Seric if (owner != NULL) 25158820Seric { 25258820Seric setsender(owner, e, NULL, TRUE); 25358820Seric if (tTd(13, 5)) 25458820Seric { 25558820Seric printf("sendall(owner): QDONTSEND "); 25658820Seric printaddr(&e->e_from, FALSE); 25758820Seric } 25858820Seric e->e_from.q_flags |= QDONTSEND; 25958820Seric e->e_errormode = EM_MAIL; 26058820Seric } 26158820Seric 26258916Seric # ifdef QUEUE 26358916Seric if ((mode == SM_QUEUE || mode == SM_FORK || 26458916Seric (mode != SM_VERIFY && SuperSafe)) && 26558916Seric !bitset(EF_INQUEUE, e->e_flags)) 26658916Seric { 26758916Seric /* be sure everything is instantiated in the queue */ 26858929Seric queueup(e, TRUE, announcequeueup); 26958916Seric for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 27058929Seric queueup(ee, TRUE, announcequeueup); 27158916Seric } 27258916Seric #endif /* QUEUE */ 27358916Seric 27458820Seric if (splitenv != NULL) 27558820Seric { 27658820Seric if (tTd(13, 1)) 27758820Seric { 27858820Seric printf("\nsendall: Split queue; remaining queue:\n"); 27958820Seric printaddr(e->e_sendqueue, TRUE); 28058820Seric } 28158820Seric 28258820Seric for (ee = splitenv; ee != NULL; ee = ee->e_sibling) 28358820Seric { 28458820Seric CurEnv = ee; 28558820Seric sendenvelope(ee, mode); 28658820Seric } 28758820Seric 28858820Seric CurEnv = e; 28958820Seric } 29058820Seric sendenvelope(e, mode); 29158820Seric 29258820Seric for (; splitenv != NULL; splitenv = splitenv->e_sibling) 29358820Seric dropenvelope(splitenv); 29458820Seric } 29558820Seric 29658820Seric sendenvelope(e, mode) 29758820Seric register ENVELOPE *e; 29858820Seric char mode; 29958820Seric { 30058916Seric bool oldverbose; 30158916Seric int pid; 30258820Seric register ADDRESS *q; 30364296Seric char *qf; 30464296Seric char *id; 30558820Seric 30663839Seric /* 30763839Seric ** If we have had global, fatal errors, don't bother sending 30863839Seric ** the message at all if we are in SMTP mode. Local errors 30963839Seric ** (e.g., a single address failing) will still cause the other 31063839Seric ** addresses to be sent. 31163839Seric */ 31263839Seric 31363839Seric if (bitset(EF_FATALERRS, e->e_flags) && OpMode == MD_SMTP) 31463839Seric { 31563839Seric e->e_flags |= EF_CLRQUEUE; 31663839Seric return; 31763839Seric } 31863839Seric 31958916Seric oldverbose = Verbose; 32058916Seric switch (mode) 32158916Seric { 32258916Seric case SM_VERIFY: 32358916Seric Verbose = TRUE; 32458916Seric break; 32558916Seric 32658916Seric case SM_QUEUE: 32758916Seric queueonly: 32858916Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 32958916Seric return; 33058916Seric 33158916Seric case SM_FORK: 33258916Seric if (e->e_xfp != NULL) 33358916Seric (void) fflush(e->e_xfp); 33458916Seric 33564035Seric # ifndef HASFLOCK 33658916Seric /* 33764296Seric ** Since fcntl locking has the interesting semantic that 33864296Seric ** the lock is owned by a process, not by an open file 33964296Seric ** descriptor, we have to flush this to the queue, and 34064296Seric ** then restart from scratch in the child. 34158916Seric */ 34258916Seric 34364296Seric /* save id for future use */ 34464296Seric id = e->e_id; 34558916Seric 34664296Seric /* now drop the envelope in the parent */ 34764296Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE; 34864296Seric dropenvelope(e); 34964296Seric 35064296Seric /* and reacquire in the child */ 35164296Seric (void) dowork(id, TRUE, FALSE, e); 35264296Seric 35364296Seric return; 35464296Seric 35564296Seric # else /* HASFLOCK */ 35664296Seric 35758916Seric pid = fork(); 35858916Seric if (pid < 0) 35958916Seric { 36058916Seric goto queueonly; 36158916Seric } 36258916Seric else if (pid > 0) 36358916Seric { 36464310Seric /* be sure we leave the temp files to our child */ 36564310Seric /* can't call unlockqueue to avoid unlink of xfp */ 36664310Seric if (e->e_lockfp != NULL) 36764310Seric (void) xfclose(e->e_lockfp, "sendenvelope", "lockfp"); 36864310Seric e->e_lockfp = NULL; 36964310Seric 37064310Seric /* close any random open files in the envelope */ 37164310Seric closexscript(e); 37264310Seric if (e->e_dfp != NULL) 37364310Seric (void) xfclose(e->e_dfp, "sendenvelope", e->e_df); 37464310Seric e->e_dfp = NULL; 37564310Seric e->e_id = e->e_df = NULL; 37658916Seric return; 37758916Seric } 37858916Seric 37958916Seric /* double fork to avoid zombies */ 38058916Seric if (fork() > 0) 38158916Seric exit(EX_OK); 38258916Seric 38358916Seric /* be sure we are immune from the terminal */ 38463839Seric disconnect(1, e); 38558916Seric 38658916Seric /* 38758916Seric ** Close any cached connections. 38858916Seric ** 38958916Seric ** We don't send the QUIT protocol because the parent 39058916Seric ** still knows about the connection. 39158916Seric ** 39258916Seric ** This should only happen when delivering an error 39358916Seric ** message. 39458916Seric */ 39558916Seric 39658916Seric mci_flush(FALSE, NULL); 39758916Seric 39864296Seric # endif /* HASFLOCK */ 39964296Seric 40058916Seric break; 40158916Seric } 40258916Seric 40358820Seric /* 40458820Seric ** Run through the list and send everything. 40563965Seric ** 40663965Seric ** Set EF_GLOBALERRS so that error messages during delivery 40763965Seric ** result in returned mail. 40858820Seric */ 40958820Seric 41058820Seric e->e_nsent = 0; 41163965Seric e->e_flags |= EF_GLOBALERRS; 41264696Seric 41364696Seric /* now run through the queue */ 41458820Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next) 41558820Seric { 41664441Seric #ifdef XDEBUG 41764441Seric char wbuf[MAXNAME + 20]; 41864441Seric 41964441Seric (void) sprintf(wbuf, "sendall(%s)", q->q_paddr); 42064441Seric checkfd012(wbuf); 42164441Seric #endif 42258820Seric if (mode == SM_VERIFY) 42358820Seric { 42458820Seric e->e_to = q->q_paddr; 42558820Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 42660173Seric { 42764942Seric if (q->q_host != NULL && q->q_host[0] != '\0') 42864942Seric message("deliverable: mailer %s, host %s, user %s", 42964942Seric q->q_mailer->m_name, 43064942Seric q->q_host, 43164942Seric q->q_user); 43264942Seric else 43364942Seric message("deliverable: mailer %s, user %s", 43464942Seric q->q_mailer->m_name, 43564942Seric q->q_user); 43660173Seric } 43758820Seric } 43858820Seric else if (!bitset(QDONTSEND|QBADADDR, q->q_flags)) 43958820Seric { 44058820Seric # ifdef QUEUE 44158820Seric /* 44258820Seric ** Checkpoint the send list every few addresses 44358820Seric */ 44458820Seric 44558820Seric if (e->e_nsent >= CheckpointInterval) 44658820Seric { 44758820Seric queueup(e, TRUE, FALSE); 44858820Seric e->e_nsent = 0; 44958820Seric } 45058820Seric # endif /* QUEUE */ 45158820Seric (void) deliver(e, q); 45258820Seric } 45358820Seric } 45458820Seric Verbose = oldverbose; 45558820Seric 45664441Seric #ifdef XDEBUG 45764441Seric checkfd012("end of sendenvelope"); 45864441Seric #endif 45964441Seric 46058820Seric if (mode == SM_FORK) 46158820Seric finis(); 46258820Seric } 46358820Seric /* 46458820Seric ** DOFORK -- do a fork, retrying a couple of times on failure. 46558820Seric ** 46658820Seric ** This MUST be a macro, since after a vfork we are running 46758820Seric ** two processes on the same stack!!! 46858820Seric ** 46958820Seric ** Parameters: 47058820Seric ** none. 47158820Seric ** 47258820Seric ** Returns: 47358820Seric ** From a macro??? You've got to be kidding! 47458820Seric ** 47558820Seric ** Side Effects: 47658820Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving: 47758820Seric ** pid of child in parent, zero in child. 47858820Seric ** -1 on unrecoverable error. 47958820Seric ** 48058820Seric ** Notes: 48158820Seric ** I'm awfully sorry this looks so awful. That's 48258820Seric ** vfork for you..... 48358820Seric */ 48458820Seric 48558820Seric # define NFORKTRIES 5 48658820Seric 48758820Seric # ifndef FORK 48858820Seric # define FORK fork 48958820Seric # endif 49058820Seric 49158820Seric # define DOFORK(fORKfN) \ 49258820Seric {\ 49358820Seric register int i;\ 49458820Seric \ 49558820Seric for (i = NFORKTRIES; --i >= 0; )\ 49658820Seric {\ 49758820Seric pid = fORKfN();\ 49858820Seric if (pid >= 0)\ 49958820Seric break;\ 50058820Seric if (i > 0)\ 50158820Seric sleep((unsigned) NFORKTRIES - i);\ 50258820Seric }\ 50358820Seric } 50458820Seric /* 50558820Seric ** DOFORK -- simple fork interface to DOFORK. 50658820Seric ** 50758820Seric ** Parameters: 50858820Seric ** none. 50958820Seric ** 51058820Seric ** Returns: 51158820Seric ** pid of child in parent. 51258820Seric ** zero in child. 51358820Seric ** -1 on error. 51458820Seric ** 51558820Seric ** Side Effects: 51658820Seric ** returns twice, once in parent and once in child. 51758820Seric */ 51858820Seric 51958820Seric dofork() 52058820Seric { 52158820Seric register int pid; 52258820Seric 52358820Seric DOFORK(fork); 52458820Seric return (pid); 52558820Seric } 52658820Seric /* 5274315Seric ** DELIVER -- Deliver a message to a list of addresses. 528294Seric ** 5294315Seric ** This routine delivers to everyone on the same host as the 5304315Seric ** user on the head of the list. It is clever about mailers 5314315Seric ** that don't handle multiple users. It is NOT guaranteed 5324315Seric ** that it will deliver to all these addresses however -- so 5334315Seric ** deliver should be called once for each address on the 5344315Seric ** list. 5354315Seric ** 536294Seric ** Parameters: 5379370Seric ** e -- the envelope to deliver. 5384621Seric ** firstto -- head of the address list to deliver to. 539294Seric ** 540294Seric ** Returns: 541294Seric ** zero -- successfully delivered. 542294Seric ** else -- some failure, see ExitStat for more info. 543294Seric ** 544294Seric ** Side Effects: 545294Seric ** The standard input is passed off to someone. 546294Seric */ 547294Seric 5489370Seric deliver(e, firstto) 5499370Seric register ENVELOPE *e; 5504621Seric ADDRESS *firstto; 551294Seric { 5524452Seric char *host; /* host being sent to */ 5534452Seric char *user; /* user being sent to */ 554294Seric char **pvp; 5553233Seric register char **mvp; 5563233Seric register char *p; 55710306Seric register MAILER *m; /* mailer for this recipient */ 5584397Seric ADDRESS *ctladdr; 55954967Seric register MCI *mci; 5604621Seric register ADDRESS *to = firstto; 5614863Seric bool clever = FALSE; /* running user smtp to this mailer */ 5625032Seric ADDRESS *tochain = NULL; /* chain of users in this mailer call */ 56351951Seric int rcode; /* response code */ 56457454Seric char *firstsig; /* signature of firstto */ 56558820Seric int pid; 56658820Seric char *curhost; 56758820Seric int mpvect[2]; 56858820Seric int rpvect[2]; 56910306Seric char *pv[MAXPV+1]; 57058704Seric char tobuf[TOBUFSIZE]; /* text line of to people */ 57110306Seric char buf[MAXNAME]; 57251951Seric char rpathbuf[MAXNAME]; /* translated return path */ 57357441Seric extern int checkcompat(); 574294Seric 5754488Seric errno = 0; 57658680Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags)) 5773233Seric return (0); 578294Seric 57935651Seric #ifdef NAMED_BIND 58034022Sbostic /* unless interactive, try twice, over a minute */ 58134022Sbostic if (OpMode == MD_DAEMON || OpMode == MD_SMTP) { 58234022Sbostic _res.retrans = 30; 58334022Sbostic _res.retry = 2; 58434022Sbostic } 58536788Sbostic #endif 58634022Sbostic 5876974Seric m = to->q_mailer; 5886974Seric host = to->q_host; 58958803Seric CurEnv = e; /* just in case */ 59059044Seric e->e_statmsg = NULL; 59164718Seric SmtpError[0] = '\0'; 5926974Seric 5937672Seric if (tTd(10, 1)) 5943233Seric printf("\n--deliver, mailer=%d, host=`%s', first user=`%s'\n", 5956974Seric m->m_mno, host, to->q_user); 59664725Seric if (tTd(10, 100)) 59764725Seric printopenfds(FALSE); 598294Seric 599294Seric /* 6005903Seric ** If this mailer is expensive, and if we don't want to make 6015903Seric ** connections now, just mark these addresses and return. 6025903Seric ** This is useful if we want to batch connections to 6035903Seric ** reduce load. This will cause the messages to be 6045903Seric ** queued up, and a daemon will come along to send the 6055903Seric ** messages later. 6065903Seric ** This should be on a per-mailer basis. 6075903Seric */ 6085903Seric 60964658Seric if (NoConnect && bitnset(M_EXPENSIVE, m->m_flags) && !Verbose) 6105903Seric { 6115903Seric for (; to != NULL; to = to->q_next) 6128431Seric { 61358680Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 61458247Seric to->q_mailer != m) 6158431Seric continue; 61663853Seric to->q_flags |= QQUEUEUP; 6179370Seric e->e_to = to->q_paddr; 61858151Seric message("queued"); 61958020Seric if (LogLevel > 8) 62064771Seric logdelivery(m, NULL, "queued", NULL, e); 6218431Seric } 6229370Seric e->e_to = NULL; 6235903Seric return (0); 6245903Seric } 6255903Seric 6265903Seric /* 6273233Seric ** Do initial argv setup. 6283233Seric ** Insert the mailer name. Notice that $x expansion is 6293233Seric ** NOT done on the mailer name. Then, if the mailer has 6303233Seric ** a picky -f flag, we insert it as appropriate. This 6313233Seric ** code does not check for 'pv' overflow; this places a 6323233Seric ** manifest lower limit of 4 for MAXPV. 6338062Seric ** The from address rewrite is expected to make 6348062Seric ** the address relative to the other end. 6352968Seric */ 6362968Seric 6374452Seric /* rewrite from address, using rewriting rules */ 63859163Seric rcode = EX_OK; 63959163Seric (void) strcpy(rpathbuf, remotename(e->e_from.q_paddr, m, 64059163Seric RF_SENDERADDR|RF_CANONICAL, 64159163Seric &rcode, e)); 64258680Seric define('g', rpathbuf, e); /* translated return path */ 6439370Seric define('h', host, e); /* to host */ 6443233Seric Errors = 0; 6453233Seric pvp = pv; 6463233Seric *pvp++ = m->m_argv[0]; 6472968Seric 6483233Seric /* insert -f or -r flag as appropriate */ 64910682Seric if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags))) 6503233Seric { 65110682Seric if (bitnset(M_FOPT, m->m_flags)) 6523233Seric *pvp++ = "-f"; 6533233Seric else 6543233Seric *pvp++ = "-r"; 65551951Seric *pvp++ = newstr(rpathbuf); 6563233Seric } 657294Seric 658294Seric /* 6593233Seric ** Append the other fixed parts of the argv. These run 6603233Seric ** up to the first entry containing "$u". There can only 6613233Seric ** be one of these, and there are only a few more slots 6623233Seric ** in the pv after it. 663294Seric */ 664294Seric 6653233Seric for (mvp = m->m_argv; (p = *++mvp) != NULL; ) 666294Seric { 66758050Seric /* can't use strchr here because of sign extension problems */ 66858050Seric while (*p != '\0') 66958050Seric { 67058050Seric if ((*p++ & 0377) == MACROEXPAND) 67158050Seric { 67258050Seric if (*p == 'u') 67358050Seric break; 67458050Seric } 67558050Seric } 67658050Seric 67758050Seric if (*p != '\0') 6783233Seric break; 6793233Seric 6803233Seric /* this entry is safe -- go ahead and process it */ 6819370Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 6823233Seric *pvp++ = newstr(buf); 6833233Seric if (pvp >= &pv[MAXPV - 3]) 6843233Seric { 68558151Seric syserr("554 Too many parameters to %s before $u", pv[0]); 6863233Seric return (-1); 6873233Seric } 688294Seric } 6894863Seric 6906038Seric /* 6916038Seric ** If we have no substitution for the user name in the argument 6926038Seric ** list, we know that we must supply the names otherwise -- and 6936038Seric ** SMTP is the answer!! 6946038Seric */ 6956038Seric 6963233Seric if (*mvp == NULL) 6974863Seric { 6984863Seric /* running SMTP */ 6995179Seric # ifdef SMTP 7004863Seric clever = TRUE; 7014863Seric *pvp = NULL; 70256795Seric # else /* SMTP */ 7036038Seric /* oops! we don't implement SMTP */ 70464718Seric syserr("554 SMTP style mailer not implemented"); 7055179Seric return (EX_SOFTWARE); 70656795Seric # endif /* SMTP */ 7074863Seric } 708294Seric 709294Seric /* 7103233Seric ** At this point *mvp points to the argument with $u. We 7113233Seric ** run through our address list and append all the addresses 7123233Seric ** we can. If we run out of space, do not fret! We can 7133233Seric ** always send another copy later. 714294Seric */ 715294Seric 7163233Seric tobuf[0] = '\0'; 7179370Seric e->e_to = tobuf; 7184397Seric ctladdr = NULL; 71957454Seric firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e); 7203233Seric for (; to != NULL; to = to->q_next) 721294Seric { 7223233Seric /* avoid sending multiple recipients to dumb mailers */ 72310682Seric if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags)) 7243233Seric break; 7253233Seric 7263233Seric /* if already sent or not for this host, don't send */ 72758680Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) || 72857454Seric to->q_mailer != firstto->q_mailer || 72957454Seric strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0) 7303233Seric continue; 7314397Seric 7328225Seric /* avoid overflowing tobuf */ 73342462Sbostic if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2)) 7348225Seric break; 7358225Seric 7367672Seric if (tTd(10, 1)) 7375032Seric { 7385032Seric printf("\nsend to "); 7395032Seric printaddr(to, FALSE); 7405032Seric } 7415032Seric 7424397Seric /* compute effective uid/gid when sending */ 74365137Seric /* XXX perhaps this should be to->q_mailer != LocalMailer ?? */ 74465137Seric /* XXX perhaps it should be a mailer flag? */ 74565137Seric if (to->q_mailer == ProgMailer || to->q_mailer == FileMailer) 74665137Seric ctladdr = getctladdr(to); 7474397Seric 7483233Seric user = to->q_user; 7499370Seric e->e_to = to->q_paddr; 75057731Seric if (tTd(10, 5)) 75157731Seric { 75257731Seric printf("deliver: QDONTSEND "); 75357731Seric printaddr(to, FALSE); 75457731Seric } 75558680Seric to->q_flags |= QDONTSEND; 7563233Seric 7573233Seric /* 7583233Seric ** Check to see that these people are allowed to 7593233Seric ** talk to each other. 7603233Seric */ 7613233Seric 76210699Seric if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize) 76310699Seric { 76429914Seric NoReturn = TRUE; 76558151Seric usrerr("552 Message is too large; %ld bytes max", m->m_maxsize); 76664771Seric giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, e); 76710699Seric continue; 76810699Seric } 76957441Seric rcode = checkcompat(to, e); 77057459Seric if (rcode != EX_OK) 771294Seric { 77263787Seric markfailure(e, to, rcode); 77364771Seric giveresponse(rcode, m, NULL, ctladdr, e); 7743233Seric continue; 775294Seric } 7763233Seric 7773233Seric /* 7784099Seric ** Strip quote bits from names if the mailer is dumb 7794099Seric ** about them. 7803233Seric */ 7813233Seric 78210682Seric if (bitnset(M_STRIPQ, m->m_flags)) 783294Seric { 78454983Seric stripquotes(user); 78554983Seric stripquotes(host); 7863233Seric } 7873233Seric 7889206Seric /* hack attack -- delivermail compatibility */ 7899206Seric if (m == ProgMailer && *user == '|') 7909206Seric user++; 7919206Seric 7923233Seric /* 7934161Seric ** If an error message has already been given, don't 7944161Seric ** bother to send to this address. 7954161Seric ** 7964161Seric ** >>>>>>>>>> This clause assumes that the local mailer 7974161Seric ** >> NOTE >> cannot do any further aliasing; that 7984161Seric ** >>>>>>>>>> function is subsumed by sendmail. 7994161Seric */ 8004161Seric 8017293Seric if (bitset(QBADADDR|QQUEUEUP, to->q_flags)) 8024161Seric continue; 8034161Seric 8044283Seric /* save statistics.... */ 8059370Seric markstats(e, to); 8064283Seric 8074161Seric /* 8083233Seric ** See if this user name is "special". 8093233Seric ** If the user name has a slash in it, assume that this 8106974Seric ** is a file -- send it off without further ado. Note 8116974Seric ** that this type of addresses is not processed along 8126974Seric ** with the others, so we fudge on the To person. 8133233Seric */ 8143233Seric 81557402Seric if (m == FileMailer) 8163233Seric { 81765060Seric rcode = mailfile(user, ctladdr, e); 81865060Seric giveresponse(rcode, m, NULL, ctladdr, e); 81957402Seric if (rcode == EX_OK) 82057402Seric to->q_flags |= QSENT; 82157402Seric continue; 822294Seric } 8233233Seric 8244315Seric /* 8254315Seric ** Address is verified -- add this user to mailer 8264315Seric ** argv, and add it to the print list of recipients. 8274315Seric */ 8284315Seric 8296059Seric /* link together the chain of recipients */ 8306272Seric to->q_tchain = tochain; 8316272Seric tochain = to; 8326059Seric 8333233Seric /* create list of users for error messages */ 8349388Seric (void) strcat(tobuf, ","); 8354082Seric (void) strcat(tobuf, to->q_paddr); 8369370Seric define('u', user, e); /* to user */ 83764694Seric p = to->q_home; 83864694Seric if (p == NULL && ctladdr != NULL) 83964694Seric p = ctladdr->q_home; 84064694Seric define('z', p, e); /* user's home */ 8413233Seric 8424863Seric /* 8436059Seric ** Expand out this user into argument list. 8444863Seric */ 8454863Seric 8466059Seric if (!clever) 8473233Seric { 8489370Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 8494863Seric *pvp++ = newstr(buf); 8504863Seric if (pvp >= &pv[MAXPV - 2]) 8514863Seric { 8524863Seric /* allow some space for trailing parms */ 8534863Seric break; 8544863Seric } 8554863Seric } 856294Seric } 857294Seric 8584067Seric /* see if any addresses still exist */ 8594067Seric if (tobuf[0] == '\0') 8604863Seric { 8619370Seric define('g', (char *) NULL, e); 8624067Seric return (0); 8634863Seric } 8644067Seric 8653233Seric /* print out messages as full list */ 8669388Seric e->e_to = tobuf + 1; 8673233Seric 868294Seric /* 8693233Seric ** Fill out any parameters after the $u parameter. 870294Seric */ 871294Seric 8724863Seric while (!clever && *++mvp != NULL) 873294Seric { 8749370Seric expand(*mvp, buf, &buf[sizeof buf - 1], e); 8753233Seric *pvp++ = newstr(buf); 8763233Seric if (pvp >= &pv[MAXPV]) 87758151Seric syserr("554 deliver: pv overflow after $u for %s", pv[0]); 878294Seric } 8793233Seric *pvp++ = NULL; 880294Seric 881294Seric /* 882294Seric ** Call the mailer. 8832898Seric ** The argument vector gets built, pipes 884294Seric ** are created as necessary, and we fork & exec as 8852898Seric ** appropriate. 8864863Seric ** If we are running SMTP, we just need to clean up. 887294Seric */ 888294Seric 88965137Seric /*XXX this seems a bit wierd */ 89065137Seric if (ctladdr == NULL && bitset(QGOODUID, e->e_from.q_flags)) 89165137Seric ctladdr = &e->e_from; 89265137Seric 89335651Seric #ifdef NAMED_BIND 89451313Seric if (ConfigLevel < 2) 89551313Seric _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 89635651Seric #endif 89754967Seric 8987672Seric if (tTd(11, 1)) 899294Seric { 9008178Seric printf("openmailer:"); 90158820Seric printav(pv); 902294Seric } 9034488Seric errno = 0; 9043233Seric 90525050Seric CurHostName = m->m_mailer; 90625050Seric 9076038Seric /* 9086038Seric ** Deal with the special case of mail handled through an IPC 9096038Seric ** connection. 9106038Seric ** In this case we don't actually fork. We must be 9116038Seric ** running SMTP for this to work. We will return a 9126038Seric ** zero pid to indicate that we are running IPC. 91311160Seric ** We also handle a debug version that just talks to stdin/out. 9146038Seric */ 9156038Seric 91658820Seric curhost = NULL; 91764334Seric SmtpPhase = NULL; 91864718Seric mci = NULL; 91958820Seric 92064401Seric #ifdef XDEBUG 92164401Seric { 92264401Seric char wbuf[MAXLINE]; 92364401Seric 92464401Seric /* make absolutely certain 0, 1, and 2 are in use */ 92564401Seric sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name); 92664401Seric checkfd012(wbuf); 92764401Seric } 92864401Seric #endif 92964401Seric 93011160Seric /* check for Local Person Communication -- not for mortals!!! */ 93111160Seric if (strcmp(m->m_mailer, "[LPC]") == 0) 93211160Seric { 93354967Seric mci = (MCI *) xalloc(sizeof *mci); 93454993Seric bzero((char *) mci, sizeof *mci); 93553738Seric mci->mci_in = stdin; 93653738Seric mci->mci_out = stdout; 93754967Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 93853751Seric mci->mci_mailer = m; 93911160Seric } 94054967Seric else if (strcmp(m->m_mailer, "[IPC]") == 0 || 94154967Seric strcmp(m->m_mailer, "[TCP]") == 0) 9426038Seric { 94352107Seric #ifdef DAEMON 94457454Seric register int i; 9457285Seric register u_short port; 9466038Seric 94764844Seric if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0') 94864844Seric { 94964844Seric syserr("null host name for %s mailer", m->m_mailer); 95064844Seric rcode = EX_CONFIG; 95164844Seric goto give_up; 95264844Seric } 95364844Seric 95458820Seric CurHostName = pv[1]; 95558820Seric curhost = hostsignature(m, pv[1], e); 95654967Seric 95758479Seric if (curhost == NULL || curhost[0] == '\0') 95858479Seric { 95964844Seric syserr("null host signature for %s", pv[1]); 96058820Seric rcode = EX_OSERR; 96158820Seric goto give_up; 96258479Seric } 96358479Seric 9646038Seric if (!clever) 96558479Seric { 96658151Seric syserr("554 non-clever IPC"); 96764718Seric rcode = EX_CONFIG; 96858820Seric goto give_up; 96958479Seric } 97058820Seric if (pv[2] != NULL) 97158820Seric port = atoi(pv[2]); 9726632Seric else 9737285Seric port = 0; 97458820Seric tryhost: 97557454Seric while (*curhost != '\0') 97629433Sbloom { 97757454Seric register char *p; 97858664Seric static char hostbuf[MAXNAME]; 97957454Seric 98057454Seric /* pull the next host from the signature */ 98157454Seric p = strchr(curhost, ':'); 98257454Seric if (p == NULL) 98357454Seric p = &curhost[strlen(curhost)]; 98464718Seric if (p == curhost) 98564718Seric { 98664718Seric syserr("deliver: null host name in signature"); 98764726Seric curhost++; 98864718Seric continue; 98964718Seric } 99057454Seric strncpy(hostbuf, curhost, p - curhost); 99157454Seric hostbuf[p - curhost] = '\0'; 99257454Seric if (*p != '\0') 99357454Seric p++; 99457454Seric curhost = p; 99557454Seric 99653738Seric /* see if we already know that this host is fried */ 99757454Seric CurHostName = hostbuf; 99857454Seric mci = mci_get(hostbuf, m); 99954967Seric if (mci->mci_state != MCIS_CLOSED) 100057387Seric { 100157387Seric if (tTd(11, 1)) 100257387Seric { 100357387Seric printf("openmailer: "); 100464731Seric mci_dump(mci, FALSE); 100557387Seric } 100657943Seric CurHostName = mci->mci_host; 100758820Seric break; 100857387Seric } 100953751Seric mci->mci_mailer = m; 101054967Seric if (mci->mci_exitstat != EX_OK) 101154967Seric continue; 101254967Seric 101354967Seric /* try the connection */ 101457454Seric setproctitle("%s %s: %s", e->e_id, hostbuf, "user open"); 101558151Seric message("Connecting to %s (%s)...", 101657454Seric hostbuf, m->m_name); 101757454Seric i = makeconnection(hostbuf, port, mci, 101854967Seric bitnset(M_SECURE_PORT, m->m_flags)); 101954967Seric mci->mci_exitstat = i; 102054967Seric mci->mci_errno = errno; 102163753Seric #ifdef NAMED_BIND 102263753Seric mci->mci_herrno = h_errno; 102363753Seric #endif 102454967Seric if (i == EX_OK) 102552106Seric { 102654967Seric mci->mci_state = MCIS_OPENING; 102754967Seric mci_cache(mci); 102863753Seric if (TrafficLogFile != NULL) 102963753Seric fprintf(TrafficLogFile, "%05d == CONNECT %s\n", 103063753Seric getpid(), hostbuf); 103154967Seric break; 103234022Sbostic } 103354967Seric else if (tTd(11, 1)) 103454967Seric printf("openmailer: makeconnection => stat=%d, errno=%d\n", 103554967Seric i, errno); 103653738Seric 103753738Seric /* enter status of this host */ 103853738Seric setstat(i); 103964718Seric 104064718Seric /* should print some message here for -v mode */ 10416047Seric } 104264718Seric if (mci == NULL) 104364718Seric { 104464718Seric syserr("deliver: no host name"); 104564718Seric rcode = EX_OSERR; 104664718Seric goto give_up; 104764718Seric } 104854993Seric mci->mci_pid = 0; 104954967Seric #else /* no DAEMON */ 105058151Seric syserr("554 openmailer: no IPC"); 105157387Seric if (tTd(11, 1)) 105257387Seric printf("openmailer: NULL\n"); 105364718Seric rcode = EX_UNAVAILABLE; 105464718Seric goto give_up; 105554967Seric #endif /* DAEMON */ 10566038Seric } 105754967Seric else 1058294Seric { 105963753Seric if (TrafficLogFile != NULL) 106058852Seric { 106163753Seric char **av; 106258925Seric 106363753Seric fprintf(TrafficLogFile, "%05d === EXEC", getpid()); 106463753Seric for (av = pv; *av != NULL; av++) 106563753Seric fprintf(TrafficLogFile, " %s", *av); 106663753Seric fprintf(TrafficLogFile, "\n"); 106758852Seric } 106858852Seric 106954967Seric /* create a pipe to shove the mail through */ 107054967Seric if (pipe(mpvect) < 0) 107154967Seric { 107258925Seric syserr("%s... openmailer(%s): pipe (to mailer)", 107358925Seric e->e_to, m->m_name); 107457387Seric if (tTd(11, 1)) 107557387Seric printf("openmailer: NULL\n"); 107658820Seric rcode = EX_OSERR; 107758820Seric goto give_up; 107854967Seric } 10794863Seric 108054967Seric /* if this mailer speaks smtp, create a return pipe */ 108154967Seric if (clever && pipe(rpvect) < 0) 108254967Seric { 108358925Seric syserr("%s... openmailer(%s): pipe (from mailer)", 108458925Seric e->e_to, m->m_name); 108554967Seric (void) close(mpvect[0]); 108654967Seric (void) close(mpvect[1]); 108757387Seric if (tTd(11, 1)) 108857387Seric printf("openmailer: NULL\n"); 108958820Seric rcode = EX_OSERR; 109058820Seric goto give_up; 109154967Seric } 10924863Seric 109354967Seric /* 109454967Seric ** Actually fork the mailer process. 109554967Seric ** DOFORK is clever about retrying. 109654967Seric ** 109754967Seric ** Dispose of SIGCHLD signal catchers that may be laying 109854967Seric ** around so that endmail will get it. 109954967Seric */ 11006038Seric 110154967Seric if (e->e_xfp != NULL) 110254967Seric (void) fflush(e->e_xfp); /* for debugging */ 110354967Seric (void) fflush(stdout); 110426434Seric # ifdef SIGCHLD 110564035Seric (void) setsignal(SIGCHLD, SIG_DFL); 110656795Seric # endif /* SIGCHLD */ 110754967Seric DOFORK(FORK); 110854967Seric /* pid is set by DOFORK */ 110954967Seric if (pid < 0) 11104863Seric { 111154967Seric /* failure */ 111258925Seric syserr("%s... openmailer(%s): cannot fork", 111358925Seric e->e_to, m->m_name); 111454967Seric (void) close(mpvect[0]); 111554967Seric (void) close(mpvect[1]); 111654967Seric if (clever) 111754967Seric { 111854967Seric (void) close(rpvect[0]); 111954967Seric (void) close(rpvect[1]); 112054967Seric } 112157387Seric if (tTd(11, 1)) 112257387Seric printf("openmailer: NULL\n"); 112358820Seric rcode = EX_OSERR; 112458820Seric goto give_up; 11254863Seric } 112654967Seric else if (pid == 0) 112754967Seric { 112854967Seric int i; 112956678Seric int saveerrno; 113058675Seric char **ep; 113158675Seric char *env[MAXUSERENVIRON]; 113258675Seric extern char **environ; 113354967Seric extern int DtableSize; 113415772Seric 113554967Seric /* child -- set up input & exec mailer */ 113664035Seric (void) setsignal(SIGINT, SIG_IGN); 113764035Seric (void) setsignal(SIGHUP, SIG_IGN); 113864035Seric (void) setsignal(SIGTERM, SIG_DFL); 11394709Seric 114064145Seric /* reset user and group */ 114164145Seric if (!bitnset(M_RESTR, m->m_flags)) 114264145Seric { 114364145Seric if (ctladdr == NULL || ctladdr->q_uid == 0) 114464145Seric { 114564145Seric (void) initgroups(DefUser, DefGid); 114664837Seric (void) setgid(DefGid); 114764145Seric (void) setuid(DefUid); 114864145Seric } 114964145Seric else 115064145Seric { 115164145Seric (void) initgroups(ctladdr->q_ruser? 115264145Seric ctladdr->q_ruser: ctladdr->q_user, 115364145Seric ctladdr->q_gid); 115464970Seric (void) setgid(ctladdr->q_gid); 115564145Seric (void) setuid(ctladdr->q_uid); 115664145Seric } 115764145Seric } 115864145Seric 115964145Seric if (tTd(11, 2)) 116064145Seric printf("openmailer: running as r/euid=%d/%d\n", 116164145Seric getuid(), geteuid()); 116264145Seric 116358935Seric /* move into some "safe" directory */ 116458935Seric if (m->m_execdir != NULL) 116558935Seric { 116658935Seric char *p, *q; 116758935Seric char buf[MAXLINE]; 116858935Seric 116958935Seric for (p = m->m_execdir; p != NULL; p = q) 117058935Seric { 117158935Seric q = strchr(p, ':'); 117258935Seric if (q != NULL) 117358935Seric *q = '\0'; 117458935Seric expand(p, buf, &buf[sizeof buf] - 1, e); 117558935Seric if (q != NULL) 117658935Seric *q++ = ':'; 117758935Seric if (tTd(11, 20)) 117858935Seric printf("openmailer: trydir %s\n", 117958935Seric buf); 118058935Seric if (buf[0] != '\0' && chdir(buf) >= 0) 118158935Seric break; 118258935Seric } 118358935Seric } 118458935Seric 118554967Seric /* arrange to filter std & diag output of command */ 118654967Seric if (clever) 118754967Seric { 118854967Seric (void) close(rpvect[0]); 118958852Seric if (dup2(rpvect[1], STDOUT_FILENO) < 0) 119058852Seric { 119158925Seric syserr("%s... openmailer(%s): cannot dup pipe %d for stdout", 119258925Seric e->e_to, m->m_name, rpvect[1]); 119358852Seric _exit(EX_OSERR); 119458852Seric } 119554967Seric (void) close(rpvect[1]); 119654967Seric } 119754967Seric else if (OpMode == MD_SMTP || HoldErrs) 119854967Seric { 119954967Seric /* put mailer output in transcript */ 120058852Seric if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0) 120158852Seric { 120258925Seric syserr("%s... openmailer(%s): cannot dup xscript %d for stdout", 120358925Seric e->e_to, m->m_name, 120458852Seric fileno(e->e_xfp)); 120558852Seric _exit(EX_OSERR); 120658852Seric } 120754967Seric } 120858852Seric if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) 120958852Seric { 121058925Seric syserr("%s... openmailer(%s): cannot dup stdout for stderr", 121158925Seric e->e_to, m->m_name); 121258852Seric _exit(EX_OSERR); 121358852Seric } 12144709Seric 121554967Seric /* arrange to get standard input */ 121654967Seric (void) close(mpvect[1]); 121758731Seric if (dup2(mpvect[0], STDIN_FILENO) < 0) 12184417Seric { 121958925Seric syserr("%s... openmailer(%s): cannot dup pipe %d for stdin", 122058925Seric e->e_to, m->m_name, mpvect[0]); 122154967Seric _exit(EX_OSERR); 12224417Seric } 122354967Seric (void) close(mpvect[0]); 12249370Seric 122554967Seric /* arrange for all the files to be closed */ 122654967Seric for (i = 3; i < DtableSize; i++) 122754967Seric { 122854967Seric register int j; 122964145Seric 123054967Seric if ((j = fcntl(i, F_GETFD, 0)) != -1) 123164145Seric (void) fcntl(i, F_SETFD, j | 1); 123254967Seric } 12332774Seric 123458675Seric /* set up the mailer environment */ 123558675Seric i = 0; 123658675Seric env[i++] = "AGENT=sendmail"; 123758675Seric for (ep = environ; *ep != NULL; ep++) 123858675Seric { 123958675Seric if (strncmp(*ep, "TZ=", 3) == 0) 124058675Seric env[i++] = *ep; 124158675Seric } 124258675Seric env[i++] = NULL; 124358675Seric 124454967Seric /* try to execute the mailer */ 124558820Seric execve(m->m_mailer, pv, env); 124656678Seric saveerrno = errno; 124754967Seric syserr("Cannot exec %s", m->m_mailer); 124860008Seric if (m == LocalMailer || transienterror(saveerrno)) 124960008Seric _exit(EX_OSERR); 125054967Seric _exit(EX_UNAVAILABLE); 125151835Seric } 125254967Seric 125354967Seric /* 125454967Seric ** Set up return value. 125554967Seric */ 125654967Seric 125754967Seric mci = (MCI *) xalloc(sizeof *mci); 125854993Seric bzero((char *) mci, sizeof *mci); 125954967Seric mci->mci_mailer = m; 126054967Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN; 126154993Seric mci->mci_pid = pid; 126254967Seric (void) close(mpvect[0]); 126354967Seric mci->mci_out = fdopen(mpvect[1], "w"); 126464724Seric if (mci->mci_out == NULL) 126564724Seric { 126664724Seric syserr("deliver: cannot create mailer output channel, fd=%d", 126764724Seric mpvect[1]); 126864724Seric (void) close(mpvect[1]); 126964724Seric if (clever) 127064724Seric { 127164724Seric (void) close(rpvect[0]); 127264724Seric (void) close(rpvect[1]); 127364724Seric } 127464724Seric rcode = EX_OSERR; 127564724Seric goto give_up; 127664724Seric } 127754967Seric if (clever) 127854967Seric { 127954967Seric (void) close(rpvect[1]); 128054967Seric mci->mci_in = fdopen(rpvect[0], "r"); 128164724Seric if (mci->mci_in == NULL) 128264724Seric { 128364724Seric syserr("deliver: cannot create mailer input channel, fd=%d", 128464724Seric mpvect[1]); 128564724Seric (void) close(rpvect[0]); 128664724Seric fclose(mci->mci_out); 128764724Seric mci->mci_out = NULL; 128864724Seric rcode = EX_OSERR; 128964724Seric goto give_up; 129064724Seric } 129154967Seric } 129254967Seric else 129354967Seric { 129454967Seric mci->mci_flags |= MCIF_TEMP; 129554967Seric mci->mci_in = NULL; 129654967Seric } 1297294Seric } 1298294Seric 12994709Seric /* 130054967Seric ** If we are in SMTP opening state, send initial protocol. 13014709Seric */ 13024709Seric 130354967Seric if (clever && mci->mci_state != MCIS_CLOSED) 13044863Seric { 130554967Seric smtpinit(m, mci, e); 130653738Seric } 130757387Seric if (tTd(11, 1)) 130857387Seric { 130957387Seric printf("openmailer: "); 131064731Seric mci_dump(mci, FALSE); 131157387Seric } 1312294Seric 131358820Seric if (mci->mci_state != MCIS_OPEN) 131458820Seric { 131558820Seric /* couldn't open the mailer */ 131658820Seric rcode = mci->mci_exitstat; 131758820Seric errno = mci->mci_errno; 131863753Seric #ifdef NAMED_BIND 131963753Seric h_errno = mci->mci_herrno; 132063753Seric #endif 132158820Seric if (rcode == EX_OK) 132258820Seric { 132358820Seric /* shouldn't happen */ 132458820Seric syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s", 132558820Seric rcode, mci->mci_state, firstsig); 132658820Seric rcode = EX_SOFTWARE; 132758820Seric } 132864922Seric else if (rcode == EX_TEMPFAIL && curhost != NULL && *curhost != '\0') 132959958Seric { 133059958Seric /* try next MX site */ 133159958Seric goto tryhost; 133259958Seric } 133358820Seric } 133458820Seric else if (!clever) 133558820Seric { 133658820Seric /* 133758820Seric ** Format and send message. 133858820Seric */ 133958820Seric 134058820Seric putfromline(mci->mci_out, m, e); 134158820Seric (*e->e_puthdr)(mci->mci_out, m, e); 134258820Seric putline("\n", mci->mci_out, m); 134359730Seric (*e->e_putbody)(mci->mci_out, m, e, NULL); 134458820Seric 134558820Seric /* get the exit status */ 134658820Seric rcode = endmailer(mci, e, pv); 134758820Seric } 134858820Seric else 134958820Seric #ifdef SMTP 135058820Seric { 135158820Seric /* 135258820Seric ** Send the MAIL FROM: protocol 135358820Seric */ 135458820Seric 135558820Seric rcode = smtpmailfrom(m, mci, e); 135658820Seric if (rcode == EX_OK) 135758820Seric { 135858820Seric register char *t = tobuf; 135958820Seric register int i; 136058820Seric 136158820Seric /* send the recipient list */ 136258820Seric tobuf[0] = '\0'; 136358820Seric for (to = tochain; to != NULL; to = to->q_tchain) 136458820Seric { 136558820Seric e->e_to = to->q_paddr; 136658820Seric if ((i = smtprcpt(to, m, mci, e)) != EX_OK) 136758820Seric { 136858820Seric markfailure(e, to, i); 136964771Seric giveresponse(i, m, mci, ctladdr, e); 137058820Seric } 137158820Seric else 137258820Seric { 137358820Seric *t++ = ','; 137458820Seric for (p = to->q_paddr; *p; *t++ = *p++) 137558820Seric continue; 137664372Seric *t = '\0'; 137758820Seric } 137858820Seric } 137958820Seric 138058820Seric /* now send the data */ 138158820Seric if (tobuf[0] == '\0') 138258820Seric { 138358820Seric rcode = EX_OK; 138458820Seric e->e_to = NULL; 138558820Seric if (bitset(MCIF_CACHED, mci->mci_flags)) 138658820Seric smtprset(m, mci, e); 138758820Seric } 138858820Seric else 138958820Seric { 139058820Seric e->e_to = tobuf + 1; 139158820Seric rcode = smtpdata(m, mci, e); 139258820Seric } 139358820Seric 139458820Seric /* now close the connection */ 139558820Seric if (!bitset(MCIF_CACHED, mci->mci_flags)) 139658820Seric smtpquit(m, mci, e); 139758820Seric } 139864922Seric if (rcode != EX_OK && curhost != NULL && *curhost != '\0') 139958820Seric { 140058820Seric /* try next MX site */ 140158820Seric goto tryhost; 140258820Seric } 140358820Seric } 140458820Seric #else /* not SMTP */ 140558820Seric { 140658820Seric syserr("554 deliver: need SMTP compiled to use clever mailer"); 140758820Seric rcode = EX_CONFIG; 140858820Seric goto give_up; 140958820Seric } 141058820Seric #endif /* SMTP */ 141158820Seric #ifdef NAMED_BIND 141258820Seric if (ConfigLevel < 2) 141358820Seric _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */ 141458820Seric #endif 141558820Seric 141658820Seric /* arrange a return receipt if requested */ 141764718Seric if (rcode == EX_OK && e->e_receiptto != NULL && 141864718Seric bitnset(M_LOCALMAILER, m->m_flags)) 141958820Seric { 142058820Seric e->e_flags |= EF_SENDRECEIPT; 142158820Seric /* do we want to send back more info? */ 142258820Seric } 142358820Seric 142458820Seric /* 142558820Seric ** Do final status disposal. 142658820Seric ** We check for something in tobuf for the SMTP case. 142758820Seric ** If we got a temporary failure, arrange to queue the 142858820Seric ** addressees. 142958820Seric */ 143058820Seric 143158820Seric give_up: 143258820Seric if (tobuf[0] != '\0') 143364771Seric giveresponse(rcode, m, mci, ctladdr, e); 143458820Seric for (to = tochain; to != NULL; to = to->q_tchain) 143558820Seric { 143658820Seric if (rcode != EX_OK) 143758820Seric markfailure(e, to, rcode); 143858820Seric else 143958820Seric { 144058820Seric to->q_flags |= QSENT; 144158820Seric e->e_nsent++; 144264718Seric if (e->e_receiptto != NULL && 144364718Seric bitnset(M_LOCALMAILER, m->m_flags)) 144464718Seric { 144564718Seric fprintf(e->e_xfp, "%s... Successfully delivered\n", 144664718Seric to->q_paddr); 144764718Seric } 144858820Seric } 144958820Seric } 145058820Seric 145158820Seric /* 145258820Seric ** Restore state and return. 145358820Seric */ 145458820Seric 145564401Seric #ifdef XDEBUG 145664401Seric { 145764401Seric char wbuf[MAXLINE]; 145864401Seric 145964401Seric /* make absolutely certain 0, 1, and 2 are in use */ 146064554Seric sprintf(wbuf, "%s... end of deliver(%s)", 146164554Seric e->e_to == NULL ? "NO-TO-LIST" : e->e_to, 146264554Seric m->m_name); 146364401Seric checkfd012(wbuf); 146464401Seric } 146564401Seric #endif 146664401Seric 146758820Seric errno = 0; 146858820Seric define('g', (char *) NULL, e); 146958820Seric return (rcode); 1470294Seric } 1471294Seric /* 147258820Seric ** MARKFAILURE -- mark a failure on a specific address. 147358820Seric ** 147458820Seric ** Parameters: 147558820Seric ** e -- the envelope we are sending. 147658820Seric ** q -- the address to mark. 147758820Seric ** rcode -- the code signifying the particular failure. 147858820Seric ** 147958820Seric ** Returns: 148058820Seric ** none. 148158820Seric ** 148258820Seric ** Side Effects: 148358820Seric ** marks the address (and possibly the envelope) with the 148458820Seric ** failure so that an error will be returned or 148558820Seric ** the message will be queued, as appropriate. 148658820Seric */ 148758820Seric 148858820Seric markfailure(e, q, rcode) 148958820Seric register ENVELOPE *e; 149058820Seric register ADDRESS *q; 149158820Seric int rcode; 149258820Seric { 149358820Seric char buf[MAXLINE]; 149458820Seric 149565051Seric switch (rcode) 149665051Seric { 149765051Seric case EX_OK: 149865051Seric break; 149965051Seric 150065051Seric case EX_TEMPFAIL: 150165051Seric case EX_IOERR: 150265051Seric case EX_OSERR: 150363753Seric q->q_flags |= QQUEUEUP; 150465051Seric break; 150565051Seric 150665051Seric default: 150758820Seric q->q_flags |= QBADADDR; 150865051Seric break; 150965051Seric } 151058820Seric } 151158820Seric /* 151258820Seric ** ENDMAILER -- Wait for mailer to terminate. 151358820Seric ** 151458820Seric ** We should never get fatal errors (e.g., segmentation 151558820Seric ** violation), so we report those specially. For other 151658820Seric ** errors, we choose a status message (into statmsg), 151758820Seric ** and if it represents an error, we print it. 151858820Seric ** 151958820Seric ** Parameters: 152058820Seric ** pid -- pid of mailer. 152158820Seric ** e -- the current envelope. 152258820Seric ** pv -- the parameter vector that invoked the mailer 152358820Seric ** (for error messages). 152458820Seric ** 152558820Seric ** Returns: 152658820Seric ** exit code of mailer. 152758820Seric ** 152858820Seric ** Side Effects: 152958820Seric ** none. 153058820Seric */ 153158820Seric 153258820Seric endmailer(mci, e, pv) 153358820Seric register MCI *mci; 153458820Seric register ENVELOPE *e; 153558820Seric char **pv; 153658820Seric { 153758820Seric int st; 153858820Seric 153958820Seric /* close any connections */ 154058820Seric if (mci->mci_in != NULL) 154165198Seric (void) xfclose(mci->mci_in, mci->mci_mailer->m_name, "mci_in"); 154258820Seric if (mci->mci_out != NULL) 154365198Seric (void) xfclose(mci->mci_out, mci->mci_mailer->m_name, "mci_out"); 154458820Seric mci->mci_in = mci->mci_out = NULL; 154558820Seric mci->mci_state = MCIS_CLOSED; 154658820Seric 154758820Seric /* in the IPC case there is nothing to wait for */ 154858820Seric if (mci->mci_pid == 0) 154958820Seric return (EX_OK); 155058820Seric 155158820Seric /* wait for the mailer process to die and collect status */ 155258820Seric st = waitfor(mci->mci_pid); 155358820Seric if (st == -1) 155458820Seric { 155558820Seric syserr("endmailer %s: wait", pv[0]); 155658820Seric return (EX_SOFTWARE); 155758820Seric } 155858820Seric 155964379Seric if (WIFEXITED(st)) 156058820Seric { 156164379Seric /* normal death -- return status */ 156264379Seric return (WEXITSTATUS(st)); 156364379Seric } 156458820Seric 156564379Seric /* it died a horrid death */ 1566*65545Seric syserr("451 mailer %s died with signal %o", 1567*65545Seric mci->mci_mailer->m_name, st); 156858820Seric 156964379Seric /* log the arguments */ 157065194Seric if (pv != NULL && e->e_xfp != NULL) 157164379Seric { 157264379Seric register char **av; 157358820Seric 157464379Seric fprintf(e->e_xfp, "Arguments:"); 157564379Seric for (av = pv; *av != NULL; av++) 157664379Seric fprintf(e->e_xfp, " %s", *av); 157764379Seric fprintf(e->e_xfp, "\n"); 157858820Seric } 157958820Seric 158064379Seric ExitStat = EX_TEMPFAIL; 158164379Seric return (EX_TEMPFAIL); 158258820Seric } 158358820Seric /* 1584294Seric ** GIVERESPONSE -- Interpret an error response from a mailer 1585294Seric ** 1586294Seric ** Parameters: 1587294Seric ** stat -- the status code from the mailer (high byte 1588294Seric ** only; core dumps must have been taken care of 1589294Seric ** already). 159058337Seric ** m -- the mailer info for this mailer. 159158337Seric ** mci -- the mailer connection info -- can be NULL if the 159258337Seric ** response is given before the connection is made. 159364771Seric ** ctladdr -- the controlling address for the recipient 159464771Seric ** address(es). 159558337Seric ** e -- the current envelope. 1596294Seric ** 1597294Seric ** Returns: 15984082Seric ** none. 1599294Seric ** 1600294Seric ** Side Effects: 16011518Seric ** Errors may be incremented. 1602294Seric ** ExitStat may be set. 1603294Seric */ 1604294Seric 160564771Seric giveresponse(stat, m, mci, ctladdr, e) 1606294Seric int stat; 16079370Seric register MAILER *m; 160858337Seric register MCI *mci; 160964771Seric ADDRESS *ctladdr; 161010105Seric ENVELOPE *e; 1611294Seric { 161260094Seric register const char *statmsg; 1613294Seric extern char *SysExMsg[]; 1614294Seric register int i; 161536788Sbostic extern int N_SysEx; 161610105Seric char buf[MAXLINE]; 1617294Seric 16184315Seric /* 16194315Seric ** Compute status message from code. 16204315Seric */ 16214315Seric 1622294Seric i = stat - EX__BASE; 16239370Seric if (stat == 0) 162458852Seric { 16259370Seric statmsg = "250 Sent"; 162658916Seric if (e->e_statmsg != NULL) 162758852Seric { 162858916Seric (void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg); 162958852Seric statmsg = buf; 163058852Seric } 163158852Seric } 16329370Seric else if (i < 0 || i > N_SysEx) 16339370Seric { 16349370Seric (void) sprintf(buf, "554 unknown mailer error %d", stat); 16359370Seric stat = EX_UNAVAILABLE; 16369370Seric statmsg = buf; 16379370Seric } 163810105Seric else if (stat == EX_TEMPFAIL) 163910105Seric { 164058664Seric (void) strcpy(buf, SysExMsg[i] + 1); 164136788Sbostic #ifdef NAMED_BIND 164225527Smiriam if (h_errno == TRY_AGAIN) 164363993Seric statmsg = errstring(h_errno+E_DNSBASE); 164421061Seric else 164536788Sbostic #endif 164621061Seric { 164725527Smiriam if (errno != 0) 164825527Smiriam statmsg = errstring(errno); 164925527Smiriam else 165025527Smiriam { 165121061Seric #ifdef SMTP 165225527Smiriam statmsg = SmtpError; 165356795Seric #else /* SMTP */ 165425527Smiriam statmsg = NULL; 165556795Seric #endif /* SMTP */ 165625527Smiriam } 165721061Seric } 165821061Seric if (statmsg != NULL && statmsg[0] != '\0') 165921061Seric { 166010124Seric (void) strcat(buf, ": "); 166121061Seric (void) strcat(buf, statmsg); 166210105Seric } 166310105Seric statmsg = buf; 166410105Seric } 166563753Seric #ifdef NAMED_BIND 166663753Seric else if (stat == EX_NOHOST && h_errno != 0) 166763753Seric { 166863993Seric statmsg = errstring(h_errno + E_DNSBASE); 166963753Seric (void) sprintf(buf, "%s (%s)", SysExMsg[i], statmsg); 167063753Seric statmsg = buf; 167163753Seric } 167263753Seric #endif 1673294Seric else 167421061Seric { 1675294Seric statmsg = SysExMsg[i]; 167658664Seric if (*statmsg++ == ':') 167758664Seric { 167858664Seric (void) sprintf(buf, "%s: %s", statmsg, errstring(errno)); 167958664Seric statmsg = buf; 168058664Seric } 168121061Seric } 16829370Seric 16839370Seric /* 16849370Seric ** Print the message as appropriate 16859370Seric */ 16869370Seric 168710105Seric if (stat == EX_OK || stat == EX_TEMPFAIL) 168864718Seric { 168964718Seric extern char MsgBuf[]; 169064718Seric 169158524Seric message(&statmsg[4], errstring(errno)); 169264718Seric if (stat == EX_TEMPFAIL && e->e_xfp != NULL) 169364718Seric fprintf(e->e_xfp, "%s\n", &MsgBuf[4]); 169464718Seric } 1695294Seric else 1696294Seric { 16971518Seric Errors++; 169858524Seric usrerr(statmsg, errstring(errno)); 1699294Seric } 1700294Seric 1701294Seric /* 1702294Seric ** Final cleanup. 1703294Seric ** Log a record of the transaction. Compute the new 1704294Seric ** ExitStat -- if we already had an error, stick with 1705294Seric ** that. 1706294Seric */ 1707294Seric 170858020Seric if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6)) 170964771Seric logdelivery(m, mci, &statmsg[4], ctladdr, e); 17107858Seric 17114621Seric if (stat != EX_TEMPFAIL) 17124621Seric setstat(stat); 171364952Seric if (stat != EX_OK && (stat != EX_TEMPFAIL || e->e_message == NULL)) 171410105Seric { 171510105Seric if (e->e_message != NULL) 171610105Seric free(e->e_message); 171710105Seric e->e_message = newstr(&statmsg[4]); 171810105Seric } 171910124Seric errno = 0; 172036788Sbostic #ifdef NAMED_BIND 172125527Smiriam h_errno = 0; 172236788Sbostic #endif 1723294Seric } 1724294Seric /* 17258496Seric ** LOGDELIVERY -- log the delivery in the system log 17268496Seric ** 172764969Seric ** Care is taken to avoid logging lines that are too long, because 172864969Seric ** some versions of syslog have an unfortunate proclivity for core 172964969Seric ** dumping. This is a hack, to be sure, that is at best empirical. 173064969Seric ** 17318496Seric ** Parameters: 173258337Seric ** m -- the mailer info. Can be NULL for initial queue. 173358337Seric ** mci -- the mailer connection info -- can be NULL if the 173458337Seric ** log is occuring when no connection is active. 173558337Seric ** stat -- the message to print for the status. 173664771Seric ** ctladdr -- the controlling address for the to list. 173758337Seric ** e -- the current envelope. 17388496Seric ** 17398496Seric ** Returns: 17408496Seric ** none 17418496Seric ** 17428496Seric ** Side Effects: 17438496Seric ** none 17448496Seric */ 17458496Seric 174664771Seric logdelivery(m, mci, stat, ctladdr, e) 174758337Seric MAILER *m; 174858337Seric register MCI *mci; 17498496Seric char *stat; 175064771Seric ADDRESS *ctladdr; 175154967Seric register ENVELOPE *e; 17528496Seric { 175358343Seric # ifdef LOG 175464771Seric register char *bp; 175564969Seric register char *p; 175664969Seric int l; 175758418Seric char buf[512]; 17588496Seric 175965059Seric # if (SYSLOG_BUFSIZE) >= 256 176064771Seric bp = buf; 176164771Seric if (ctladdr != NULL) 176264771Seric { 176364771Seric strcpy(bp, ", ctladdr="); 176465016Seric strcat(bp, shortenstring(ctladdr->q_paddr, 83)); 176564771Seric bp += strlen(bp); 176664771Seric if (bitset(QGOODUID, ctladdr->q_flags)) 176764771Seric { 176864771Seric (void) sprintf(bp, " (%d/%d)", 176964771Seric ctladdr->q_uid, ctladdr->q_gid); 177064771Seric bp += strlen(bp); 177164771Seric } 177264771Seric } 177358337Seric 177464771Seric (void) sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE)); 177564771Seric bp += strlen(bp); 177664771Seric 177758513Seric if (m != NULL) 177858305Seric { 177964771Seric (void) strcpy(bp, ", mailer="); 178064771Seric (void) strcat(bp, m->m_name); 178164771Seric bp += strlen(bp); 178258305Seric } 178358513Seric 178458513Seric if (mci != NULL && mci->mci_host != NULL) 178558305Seric { 178658305Seric # ifdef DAEMON 178758755Seric extern SOCKADDR CurHostAddr; 178858513Seric # endif 178958305Seric 179064771Seric (void) strcpy(bp, ", relay="); 179164771Seric (void) strcat(bp, mci->mci_host); 179258513Seric 179358513Seric # ifdef DAEMON 179464771Seric (void) strcat(bp, " ("); 179564771Seric (void) strcat(bp, anynet_ntoa(&CurHostAddr)); 179664771Seric (void) strcat(bp, ")"); 179758305Seric # endif 179858305Seric } 179958343Seric else 180058513Seric { 180158513Seric char *p = macvalue('h', e); 180258343Seric 180358513Seric if (p != NULL && p[0] != '\0') 180458513Seric { 180564771Seric (void) strcpy(bp, ", relay="); 180664771Seric (void) strcat(bp, p); 180758513Seric } 180858513Seric } 180964922Seric bp += strlen(bp); 181064969Seric 181165059Seric #define STATLEN (((SYSLOG_BUFSIZE) - 100) / 4) 181265059Seric #if (STATLEN) < 63 181365059Seric # undef STATLEN 181465059Seric # define STATLEN 63 181565059Seric #endif 181665059Seric #if (STATLEN) > 203 181765059Seric # undef STATLEN 181865059Seric # define STATLEN 203 181965059Seric #endif 182065059Seric 182165059Seric if ((bp - buf) > (sizeof buf - ((STATLEN) + 20))) 182264969Seric { 182364969Seric /* desperation move -- truncate data */ 182465059Seric bp = buf + sizeof buf - ((STATLEN) + 17); 182564969Seric strcpy(bp, "..."); 182664969Seric bp += 3; 182764969Seric } 182864969Seric 182964969Seric (void) strcpy(bp, ", stat="); 183064969Seric bp += strlen(bp); 183165059Seric 183265059Seric (void) strcpy(bp, shortenstring(stat, (STATLEN))); 183358418Seric 183464969Seric l = SYSLOG_BUFSIZE - 100 - strlen(buf); 183564969Seric p = e->e_to; 183664969Seric while (strlen(p) >= l) 183764969Seric { 183864969Seric register char *q = strchr(p + l, ','); 183964969Seric 184065008Seric if (q == NULL) 184164969Seric break; 184264969Seric syslog(LOG_INFO, "%s: to=%.*s [more]%s", 184364969Seric e->e_id, ++q - p, p, buf); 184464969Seric p = q; 184564969Seric } 184664969Seric syslog(LOG_INFO, "%s: to=%s%s", e->e_id, p, buf); 184765059Seric 184865059Seric # else /* we have a very short log buffer size */ 184965059Seric 185065059Seric l = SYSLOG_BUFSIZE - 40; 185165059Seric p = e->e_to; 185265059Seric while (strlen(p) >= l) 185365059Seric { 185465059Seric register char *q = strchr(p + l, ','); 185565059Seric 185665059Seric if (q == NULL) 185765059Seric break; 185865059Seric syslog(LOG_INFO, "%s: to=%.*s [more]", 185965059Seric e->e_id, ++q - p, p); 186065059Seric p = q; 186165059Seric } 186265059Seric syslog(LOG_INFO, "%s: to=%s", e->e_id, p); 186365059Seric 186465059Seric if (ctladdr != NULL) 186565059Seric { 186665059Seric bp = buf; 186765059Seric strcpy(buf, "ctladdr="); 186865059Seric bp += strlen(buf); 186965059Seric strcpy(bp, shortenstring(ctladdr->q_paddr, 83)); 187065059Seric bp += strlen(buf); 187165059Seric if (bitset(QGOODUID, ctladdr->q_flags)) 187265059Seric { 187365059Seric (void) sprintf(bp, " (%d/%d)", 187465059Seric ctladdr->q_uid, ctladdr->q_gid); 187565059Seric bp += strlen(bp); 187665059Seric } 187765059Seric syslog(LOG_INFO, "%s: %s", e->e_id, buf); 187865059Seric } 187965059Seric syslog(LOG_INFO, "%s: delay=%s", 188065059Seric e->e_id, pintvl(curtime() - e->e_ctime, TRUE)); 188165059Seric 188265059Seric if (m != NULL) 188365059Seric syslog(LOG_INFO, "%s: mailer=%s", e->e_id, m->m_name); 188465059Seric 188565059Seric if (mci != NULL && mci->mci_host != NULL) 188665059Seric { 188765059Seric # ifdef DAEMON 188865059Seric extern SOCKADDR CurHostAddr; 188965059Seric # endif 189065059Seric 189165059Seric (void) strcpy(buf, mci->mci_host); 189265059Seric 189365059Seric # ifdef DAEMON 189465059Seric (void) strcat(buf, " ("); 189565059Seric (void) strcat(buf, anynet_ntoa(&CurHostAddr)); 189665059Seric (void) strcat(buf, ")"); 189765059Seric # endif 189865059Seric syslog(LOG_INFO, "%s: relay=%s", e->e_id, buf); 189965059Seric } 190065059Seric else 190165059Seric { 190265059Seric char *p = macvalue('h', e); 190365059Seric 190465059Seric if (p != NULL && p[0] != '\0') 190565059Seric syslog(LOG_INFO, "%s: relay=%s", e->e_id, p); 190665059Seric } 190765059Seric 190865059Seric syslog(LOG_INFO, "%s: stat=%s", e->e_id, shortenstring(stat, 63)); 190965059Seric # endif /* short log buffer */ 191056795Seric # endif /* LOG */ 19118496Seric } 19128496Seric /* 19136974Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever) 1914294Seric ** 19156974Seric ** This can be made an arbitrary message separator by changing $l 1916294Seric ** 191716150Seric ** One of the ugliest hacks seen by human eyes is contained herein: 191816150Seric ** UUCP wants those stupid "remote from <host>" lines. Why oh why 191916150Seric ** does a well-meaning programmer such as myself have to deal with 192016150Seric ** this kind of antique garbage???? 19216974Seric ** 1922294Seric ** Parameters: 19236974Seric ** fp -- the file to output to. 19246974Seric ** m -- the mailer describing this entry. 1925294Seric ** 1926294Seric ** Returns: 19276974Seric ** none 1928294Seric ** 1929294Seric ** Side Effects: 19306974Seric ** outputs some text to fp. 1931294Seric */ 1932294Seric 193354967Seric putfromline(fp, m, e) 19346974Seric register FILE *fp; 19356974Seric register MAILER *m; 193654967Seric ENVELOPE *e; 1937294Seric { 193858050Seric char *template = "\201l\n"; 19396974Seric char buf[MAXLINE]; 1940294Seric 194110682Seric if (bitnset(M_NHDR, m->m_flags)) 19426974Seric return; 19434315Seric 19446974Seric # ifdef UGLYUUCP 194510682Seric if (bitnset(M_UGLYUUCP, m->m_flags)) 19464205Seric { 194712223Seric char *bang; 194812223Seric char xbuf[MAXLINE]; 19496041Seric 195058680Seric expand("\201g", buf, &buf[sizeof buf - 1], e); 195156795Seric bang = strchr(buf, '!'); 19526974Seric if (bang == NULL) 195364370Seric { 195464370Seric errno = 0; 195564370Seric syserr("554 No ! in UUCP From address! (%s given)", buf); 195664370Seric } 19575099Seric else 19589370Seric { 195912223Seric *bang++ = '\0'; 196058050Seric (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf); 196112223Seric template = xbuf; 19629370Seric } 19636974Seric } 196456795Seric # endif /* UGLYUUCP */ 196554967Seric expand(template, buf, &buf[sizeof buf - 1], e); 196610168Seric putline(buf, fp, m); 19675981Seric } 19685981Seric /* 19696974Seric ** PUTBODY -- put the body of a message. 19706974Seric ** 19716974Seric ** Parameters: 19726974Seric ** fp -- file to output onto. 197310168Seric ** m -- a mailer descriptor to control output format. 19749538Seric ** e -- the envelope to put out. 197559730Seric ** separator -- if non-NULL, a message separator that must 197659730Seric ** not be permitted in the resulting message. 19776974Seric ** 19786974Seric ** Returns: 19796974Seric ** none. 19806974Seric ** 19816974Seric ** Side Effects: 19826974Seric ** The message is written onto fp. 19836974Seric */ 19846974Seric 198559730Seric putbody(fp, m, e, separator) 19866974Seric FILE *fp; 19879370Seric MAILER *m; 19889538Seric register ENVELOPE *e; 198959730Seric char *separator; 19906974Seric { 199110168Seric char buf[MAXLINE]; 19926974Seric 19936974Seric /* 19946974Seric ** Output the body of the message 19956974Seric */ 19966974Seric 19979538Seric if (e->e_dfp == NULL) 19986974Seric { 19999538Seric if (e->e_df != NULL) 20009538Seric { 20019538Seric e->e_dfp = fopen(e->e_df, "r"); 20029538Seric if (e->e_dfp == NULL) 200340931Srick syserr("putbody: Cannot open %s for %s from %s", 200464118Seric e->e_df, e->e_to, e->e_from.q_paddr); 20059538Seric } 20069538Seric else 200710168Seric putline("<<< No Message Collected >>>", fp, m); 20089538Seric } 20099538Seric if (e->e_dfp != NULL) 20109538Seric { 20119538Seric rewind(e->e_dfp); 201210168Seric while (!ferror(fp) && fgets(buf, sizeof buf, e->e_dfp) != NULL) 201316875Seric { 201416875Seric if (buf[0] == 'F' && bitnset(M_ESCFROM, m->m_flags) && 201540995Sbostic strncmp(buf, "From ", 5) == 0) 201623102Seric (void) putc('>', fp); 201759730Seric if (buf[0] == '-' && buf[1] == '-' && separator != NULL) 201859730Seric { 201959730Seric /* possible separator */ 202059730Seric int sl = strlen(separator); 202159730Seric 202259730Seric if (strncmp(&buf[2], separator, sl) == 0) 202359730Seric (void) putc(' ', fp); 202459730Seric } 202510168Seric putline(buf, fp, m); 202616875Seric } 20276974Seric 20289538Seric if (ferror(e->e_dfp)) 20296974Seric { 203064718Seric syserr("putbody: %s: read error", e->e_df); 20316974Seric ExitStat = EX_IOERR; 20326974Seric } 20336974Seric } 20346974Seric 203559542Seric /* some mailers want extra blank line at end of message */ 203659542Seric if (bitnset(M_BLANKEND, m->m_flags) && buf[0] != '\0' && buf[0] != '\n') 203759542Seric putline("", fp, m); 203859542Seric 20396974Seric (void) fflush(fp); 20406974Seric if (ferror(fp) && errno != EPIPE) 20416974Seric { 20426974Seric syserr("putbody: write error"); 20436974Seric ExitStat = EX_IOERR; 20446974Seric } 20456974Seric errno = 0; 20466974Seric } 20476974Seric /* 2048294Seric ** MAILFILE -- Send a message to a file. 2049294Seric ** 20504327Seric ** If the file has the setuid/setgid bits set, but NO execute 20514327Seric ** bits, sendmail will try to become the owner of that file 20524327Seric ** rather than the real user. Obviously, this only works if 20534327Seric ** sendmail runs as root. 20544327Seric ** 20559370Seric ** This could be done as a subordinate mailer, except that it 20569370Seric ** is used implicitly to save messages in ~/dead.letter. We 20579370Seric ** view this as being sufficiently important as to include it 20589370Seric ** here. For example, if the system is dying, we shouldn't have 20599370Seric ** to create another process plus some pipes to save the message. 20609370Seric ** 2061294Seric ** Parameters: 2062294Seric ** filename -- the name of the file to send to. 20634397Seric ** ctladdr -- the controlling address header -- includes 20644397Seric ** the userid/groupid to be when sending. 2065294Seric ** 2066294Seric ** Returns: 2067294Seric ** The exit code associated with the operation. 2068294Seric ** 2069294Seric ** Side Effects: 2070294Seric ** none. 2071294Seric */ 2072294Seric 207354967Seric mailfile(filename, ctladdr, e) 2074294Seric char *filename; 20754397Seric ADDRESS *ctladdr; 207654967Seric register ENVELOPE *e; 2077294Seric { 2078294Seric register FILE *f; 20794214Seric register int pid; 208053751Seric int mode; 2081294Seric 208259267Seric if (tTd(11, 1)) 208359267Seric { 208459267Seric printf("mailfile %s\n ctladdr=", filename); 208559267Seric printaddr(ctladdr, FALSE); 208659267Seric } 208759267Seric 208863753Seric if (e->e_xfp != NULL) 208963753Seric fflush(e->e_xfp); 209063753Seric 20914214Seric /* 20924214Seric ** Fork so we can change permissions here. 20934214Seric ** Note that we MUST use fork, not vfork, because of 20944214Seric ** the complications of calling subroutines, etc. 20954214Seric */ 20964067Seric 20974214Seric DOFORK(fork); 20984214Seric 20994214Seric if (pid < 0) 21004214Seric return (EX_OSERR); 21014214Seric else if (pid == 0) 21024214Seric { 21034214Seric /* child -- actually write to file */ 21044327Seric struct stat stb; 21054327Seric 210664035Seric (void) setsignal(SIGINT, SIG_DFL); 210764035Seric (void) setsignal(SIGHUP, SIG_DFL); 210864035Seric (void) setsignal(SIGTERM, SIG_DFL); 210923102Seric (void) umask(OldUmask); 211052673Seric 21114327Seric if (stat(filename, &stb) < 0) 211259745Seric stb.st_mode = FileMode; 211353751Seric mode = stb.st_mode; 211452673Seric 211552673Seric /* limit the errors to those actually caused in the child */ 211652673Seric errno = 0; 211752673Seric ExitStat = EX_OK; 211852673Seric 21194327Seric if (bitset(0111, stb.st_mode)) 21204327Seric exit(EX_CANTCREAT); 212164823Seric if (ctladdr != NULL) 212253751Seric { 212353751Seric /* ignore setuid and setgid bits */ 212453751Seric mode &= ~(S_ISGID|S_ISUID); 212553751Seric } 212653751Seric 212740931Srick /* we have to open the dfile BEFORE setuid */ 212853751Seric if (e->e_dfp == NULL && e->e_df != NULL) 212940931Srick { 213040931Srick e->e_dfp = fopen(e->e_df, "r"); 213152673Seric if (e->e_dfp == NULL) 213252673Seric { 213340931Srick syserr("mailfile: Cannot open %s for %s from %s", 213464118Seric e->e_df, e->e_to, e->e_from.q_paddr); 213540931Srick } 213640931Srick } 213740931Srick 213853751Seric if (!bitset(S_ISGID, mode) || setgid(stb.st_gid) < 0) 21394417Seric { 214064823Seric if (ctladdr == NULL || ctladdr->q_uid == 0) 214152673Seric { 214240972Sbostic (void) initgroups(DefUser, DefGid); 214352673Seric } 214452673Seric else 214552673Seric { 214653751Seric (void) initgroups(ctladdr->q_ruser ? 214753751Seric ctladdr->q_ruser : ctladdr->q_user, 214840972Sbostic ctladdr->q_gid); 214940972Sbostic } 21504417Seric } 215153751Seric if (!bitset(S_ISUID, mode) || setuid(stb.st_uid) < 0) 21524417Seric { 215364823Seric if (ctladdr == NULL || ctladdr->q_uid == 0) 21544417Seric (void) setuid(DefUid); 21554417Seric else 21564417Seric (void) setuid(ctladdr->q_uid); 21574417Seric } 215852673Seric FileName = filename; 215952673Seric LineNumber = 0; 216059745Seric f = dfopen(filename, O_WRONLY|O_CREAT|O_APPEND, FileMode); 21614214Seric if (f == NULL) 216252673Seric { 216364387Seric message("554 cannot open: %s", errstring(errno)); 21644214Seric exit(EX_CANTCREAT); 216552673Seric } 21664214Seric 216759275Seric putfromline(f, FileMailer, e); 216859275Seric (*e->e_puthdr)(f, FileMailer, e); 216959275Seric putline("\n", f, FileMailer); 217059730Seric (*e->e_putbody)(f, FileMailer, e, NULL); 217159275Seric putline("\n", f, FileMailer); 217252673Seric if (ferror(f)) 217352673Seric { 217464387Seric message("451 I/O error: %s", errstring(errno)); 217552673Seric setstat(EX_IOERR); 217652673Seric } 217758680Seric (void) xfclose(f, "mailfile", filename); 21784214Seric (void) fflush(stdout); 21794417Seric 21806887Seric /* reset ISUID & ISGID bits for paranoid systems */ 21814621Seric (void) chmod(filename, (int) stb.st_mode); 218252673Seric exit(ExitStat); 21834315Seric /*NOTREACHED*/ 21844214Seric } 21854214Seric else 21864214Seric { 21874214Seric /* parent -- wait for exit status */ 21889370Seric int st; 21894214Seric 21909370Seric st = waitfor(pid); 219164379Seric if (WIFEXITED(st)) 219264379Seric return (WEXITSTATUS(st)); 219364379Seric else 219464387Seric { 219564387Seric syserr("child died on signal %d", st); 21969370Seric return (EX_UNAVAILABLE); 219764387Seric } 219840931Srick /*NOTREACHED*/ 21994214Seric } 2200294Seric } 22014550Seric /* 220257454Seric ** HOSTSIGNATURE -- return the "signature" for a host. 220357454Seric ** 220457454Seric ** The signature describes how we are going to send this -- it 220557454Seric ** can be just the hostname (for non-Internet hosts) or can be 220657454Seric ** an ordered list of MX hosts. 220757454Seric ** 220857454Seric ** Parameters: 220957454Seric ** m -- the mailer describing this host. 221057454Seric ** host -- the host name. 221157454Seric ** e -- the current envelope. 221257454Seric ** 221357454Seric ** Returns: 221457454Seric ** The signature for this host. 221557454Seric ** 221657454Seric ** Side Effects: 221757454Seric ** Can tweak the symbol table. 221857454Seric */ 221957454Seric 222057454Seric char * 222157454Seric hostsignature(m, host, e) 222257454Seric register MAILER *m; 222357454Seric char *host; 222457454Seric ENVELOPE *e; 222557454Seric { 222657454Seric register char *p; 222757454Seric register STAB *s; 222857454Seric int i; 222957454Seric int len; 223057454Seric #ifdef NAMED_BIND 223157454Seric int nmx; 223257454Seric auto int rcode; 223359076Seric char *hp; 223459076Seric char *endp; 223559111Seric int oldoptions; 223657454Seric char *mxhosts[MAXMXHOSTS + 1]; 223757454Seric #endif 223857454Seric 223957454Seric /* 224057454Seric ** Check to see if this uses IPC -- if not, it can't have MX records. 224157454Seric */ 224257454Seric 224357454Seric p = m->m_mailer; 224457454Seric if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0) 224557454Seric { 224657454Seric /* just an ordinary mailer */ 224757454Seric return host; 224857454Seric } 224957454Seric 225057454Seric /* 225157454Seric ** Look it up in the symbol table. 225257454Seric */ 225357454Seric 225457454Seric s = stab(host, ST_HOSTSIG, ST_ENTER); 225557454Seric if (s->s_hostsig != NULL) 225657454Seric return s->s_hostsig; 225757454Seric 225857454Seric /* 225957454Seric ** Not already there -- create a signature. 226057454Seric */ 226157454Seric 226257454Seric #ifdef NAMED_BIND 226359111Seric if (ConfigLevel < 2) 226459111Seric { 226559111Seric oldoptions = _res.options; 226659111Seric _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */ 226759111Seric } 226859111Seric 226959076Seric for (hp = host; hp != NULL; hp = endp) 227057454Seric { 227159076Seric endp = strchr(hp, ':'); 227259076Seric if (endp != NULL) 227359076Seric *endp = '\0'; 227457454Seric 227559273Seric nmx = getmxrr(hp, mxhosts, TRUE, &rcode); 227657454Seric 227759076Seric if (nmx <= 0) 227859076Seric { 227959076Seric register MCI *mci; 228057454Seric 228159076Seric /* update the connection info for this host */ 228259076Seric mci = mci_get(hp, m); 228359076Seric mci->mci_exitstat = rcode; 228459076Seric mci->mci_errno = errno; 228563753Seric #ifdef NAMED_BIND 228663753Seric mci->mci_herrno = h_errno; 228763753Seric #endif 228859076Seric 228959076Seric /* and return the original host name as the signature */ 229059076Seric nmx = 1; 229159076Seric mxhosts[0] = hp; 229259076Seric } 229359076Seric 229459076Seric len = 0; 229559076Seric for (i = 0; i < nmx; i++) 229659076Seric { 229759076Seric len += strlen(mxhosts[i]) + 1; 229859076Seric } 229959076Seric if (s->s_hostsig != NULL) 230059076Seric len += strlen(s->s_hostsig) + 1; 230159076Seric p = xalloc(len); 230259076Seric if (s->s_hostsig != NULL) 230359076Seric { 230459076Seric (void) strcpy(p, s->s_hostsig); 230559076Seric free(s->s_hostsig); 230659076Seric s->s_hostsig = p; 230759076Seric p += strlen(p); 230857454Seric *p++ = ':'; 230959076Seric } 231059076Seric else 231159076Seric s->s_hostsig = p; 231259076Seric for (i = 0; i < nmx; i++) 231359076Seric { 231459076Seric if (i != 0) 231559076Seric *p++ = ':'; 231659076Seric strcpy(p, mxhosts[i]); 231759076Seric p += strlen(p); 231859076Seric } 231959076Seric if (endp != NULL) 232059076Seric *endp++ = ':'; 232157454Seric } 232257454Seric makelower(s->s_hostsig); 232359111Seric if (ConfigLevel < 2) 232459111Seric _res.options = oldoptions; 232557454Seric #else 232657454Seric /* not using BIND -- the signature is just the host name */ 232757454Seric s->s_hostsig = host; 232857454Seric #endif 232957454Seric if (tTd(17, 1)) 233057454Seric printf("hostsignature(%s) = %s\n", host, s->s_hostsig); 233157454Seric return s->s_hostsig; 233257454Seric } 2333