122701Sdist /*
268839Seric * Copyright (c) 1983, 1995 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*69963Seric static char sccsid[] = "@(#)deliver.c 8.160 (Berkeley) 06/21/95";
1133728Sbostic #endif /* not lint */
1222701Sdist
1340961Sbostic #include "sendmail.h"
1433931Sbostic #include <errno.h>
1566334Seric #if NAMED_BIND
1634022Sbostic #include <resolv.h>
1763753Seric
1863753Seric extern int h_errno;
1935651Seric #endif
20294Seric
2165075Seric extern char SmtpError[];
2265075Seric
23294Seric /*
2458820Seric ** SENDALL -- actually send all the messages.
2558820Seric **
2658820Seric ** Parameters:
2758820Seric ** e -- the envelope to send.
2858820Seric ** mode -- the delivery mode to use. If SM_DEFAULT, use
2958820Seric ** the current e->e_sendmode.
3058820Seric **
3158820Seric ** Returns:
3258820Seric ** none.
3358820Seric **
3458820Seric ** Side Effects:
3558820Seric ** Scans the send lists and sends everything it finds.
3658820Seric ** Delivers any appropriate error messages.
3758820Seric ** If we are running in a non-interactive mode, takes the
3858820Seric ** appropriate action.
3958820Seric */
4058820Seric
4168627Seric void
sendall(e,mode)4258820Seric sendall(e, mode)
4358820Seric ENVELOPE *e;
4458820Seric char mode;
4558820Seric {
4658820Seric register ADDRESS *q;
4758820Seric char *owner;
4858820Seric int otherowners;
4966009Seric register ENVELOPE *ee;
5066009Seric ENVELOPE *splitenv = NULL;
5168627Seric bool oldverbose = Verbose;
5268627Seric bool somedeliveries = FALSE;
5368627Seric int pid;
5468627Seric extern void sendenvelope();
5558820Seric
5663839Seric /*
5763839Seric ** If we have had global, fatal errors, don't bother sending
5863839Seric ** the message at all if we are in SMTP mode. Local errors
5963839Seric ** (e.g., a single address failing) will still cause the other
6063839Seric ** addresses to be sent.
6163839Seric */
6263839Seric
6365580Seric if (bitset(EF_FATALERRS, e->e_flags) &&
6465580Seric (OpMode == MD_SMTP || OpMode == MD_DAEMON))
6561092Seric {
6661092Seric e->e_flags |= EF_CLRQUEUE;
6761092Seric return;
6861092Seric }
6961092Seric
7058820Seric /* determine actual delivery mode */
7164826Seric CurrentLA = getla();
7258820Seric if (mode == SM_DEFAULT)
7358820Seric {
7458820Seric mode = e->e_sendmode;
7558820Seric if (mode != SM_VERIFY &&
7658820Seric shouldqueue(e->e_msgpriority, e->e_ctime))
7758820Seric mode = SM_QUEUE;
7858820Seric }
7958820Seric
8058820Seric if (tTd(13, 1))
8158820Seric {
8268627Seric extern void printenvflags();
8368627Seric
8464310Seric printf("\n===== SENDALL: mode %c, id %s, e_from ",
8564310Seric mode, e->e_id);
8658820Seric printaddr(&e->e_from, FALSE);
8768627Seric printf("\te_flags = ");
8868627Seric printenvflags(e);
8958820Seric printf("sendqueue:\n");
9058820Seric printaddr(e->e_sendqueue, TRUE);
9158820Seric }
9258820Seric
9358820Seric /*
9458820Seric ** Do any preprocessing necessary for the mode we are running.
9558820Seric ** Check to make sure the hop count is reasonable.
9658820Seric ** Delete sends to the sender in mailing lists.
9758820Seric */
9858820Seric
9958820Seric CurEnv = e;
10058820Seric
10158820Seric if (e->e_hopcount > MaxHopCount)
10258820Seric {
10358820Seric errno = 0;
10468627Seric queueup(e, TRUE, mode == SM_QUEUE);
10566258Seric e->e_flags |= EF_FATALERRS|EF_PM_NOTIFY|EF_CLRQUEUE;
10668850Seric syserr("554 Too many hops %d (%d max): from %s via %s, to %s",
10758820Seric e->e_hopcount, MaxHopCount, e->e_from.q_paddr,
10866003Seric RealHostName == NULL ? "localhost" : RealHostName,
10966003Seric e->e_sendqueue->q_paddr);
11068850Seric e->e_sendqueue->q_status = "5.4.6";
11158820Seric return;
11258820Seric }
11358820Seric
11459435Seric /*
11559435Seric ** Do sender deletion.
11659435Seric **
11759435Seric ** If the sender has the QQUEUEUP flag set, skip this.
11859435Seric ** This can happen if the name server is hosed when you
11959435Seric ** are trying to send mail. The result is that the sender
12059435Seric ** is instantiated in the queue as a recipient.
12159435Seric */
12259435Seric
12364118Seric if (!bitset(EF_METOO, e->e_flags) &&
12464118Seric !bitset(QQUEUEUP, e->e_from.q_flags))
12558820Seric {
12658820Seric if (tTd(13, 5))
12758820Seric {
12858820Seric printf("sendall: QDONTSEND ");
12958820Seric printaddr(&e->e_from, FALSE);
13058820Seric }
13158820Seric e->e_from.q_flags |= QDONTSEND;
13268627Seric (void) recipient(&e->e_from, &e->e_sendqueue, 0, e);
13358820Seric }
13458820Seric
13558820Seric /*
13658820Seric ** Handle alias owners.
13758820Seric **
13858820Seric ** We scan up the q_alias chain looking for owners.
13958820Seric ** We discard owners that are the same as the return path.
14058820Seric */
14158820Seric
14258820Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next)
14358820Seric {
14458820Seric register struct address *a;
14558820Seric
14658820Seric for (a = q; a != NULL && a->q_owner == NULL; a = a->q_alias)
14758820Seric continue;
14858820Seric if (a != NULL)
14958820Seric q->q_owner = a->q_owner;
15058820Seric
15158820Seric if (q->q_owner != NULL &&
15258820Seric !bitset(QDONTSEND, q->q_flags) &&
15358820Seric strcmp(q->q_owner, e->e_from.q_paddr) == 0)
15458820Seric q->q_owner = NULL;
15558820Seric }
15658820Seric
15758820Seric owner = "";
15858820Seric otherowners = 1;
15958820Seric while (owner != NULL && otherowners > 0)
16058820Seric {
16158820Seric owner = NULL;
16258820Seric otherowners = 0;
16358820Seric
16458820Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next)
16558820Seric {
16658820Seric if (bitset(QDONTSEND, q->q_flags))
16758820Seric continue;
16858820Seric
16958820Seric if (q->q_owner != NULL)
17058820Seric {
17158820Seric if (owner == NULL)
17258820Seric owner = q->q_owner;
17358820Seric else if (owner != q->q_owner)
17458820Seric {
17558820Seric if (strcmp(owner, q->q_owner) == 0)
17658820Seric {
17758820Seric /* make future comparisons cheap */
17858820Seric q->q_owner = owner;
17958820Seric }
18058820Seric else
18158820Seric {
18258820Seric otherowners++;
18358820Seric }
18458820Seric owner = q->q_owner;
18558820Seric }
18658820Seric }
18758820Seric else
18858820Seric {
18958820Seric otherowners++;
19058820Seric }
19168627Seric
19268627Seric /*
19368627Seric ** If this mailer is expensive, and if we don't
19468627Seric ** want to make connections now, just mark these
19568627Seric ** addresses and return. This is useful if we
19668627Seric ** want to batch connections to reduce load. This
19768627Seric ** will cause the messages to be queued up, and a
19868627Seric ** daemon will come along to send the messages later.
19968627Seric */
20068627Seric
20168627Seric if (bitset(QBADADDR|QQUEUEUP, q->q_flags))
20268627Seric continue;
20368627Seric if (NoConnect && !Verbose &&
20468627Seric bitnset(M_EXPENSIVE, q->q_mailer->m_flags))
20568627Seric {
20668627Seric q->q_flags |= QQUEUEUP;
20768627Seric e->e_to = q->q_paddr;
20868627Seric message("queued");
20968627Seric if (LogLevel > 8)
21068627Seric logdelivery(q->q_mailer, NULL,
21168627Seric "queued", NULL,
21268627Seric (time_t) 0, e);
21368627Seric e->e_to = NULL;
21468627Seric }
21568627Seric else
21668627Seric {
21768627Seric somedeliveries = TRUE;
21868627Seric }
21958820Seric }
22058820Seric
22158820Seric if (owner != NULL && otherowners > 0)
22258820Seric {
22358820Seric extern HDR *copyheader();
22458820Seric extern ADDRESS *copyqueue();
22558820Seric
22658820Seric /*
22758820Seric ** Split this envelope into two.
22858820Seric */
22958820Seric
23058820Seric ee = (ENVELOPE *) xalloc(sizeof(ENVELOPE));
23158820Seric *ee = *e;
23258820Seric ee->e_id = NULL;
23358820Seric (void) queuename(ee, '\0');
23458820Seric
23558820Seric if (tTd(13, 1))
23658820Seric printf("sendall: split %s into %s\n",
23758820Seric e->e_id, ee->e_id);
23858820Seric
23958820Seric ee->e_header = copyheader(e->e_header);
24058820Seric ee->e_sendqueue = copyqueue(e->e_sendqueue);
24158820Seric ee->e_errorqueue = copyqueue(e->e_errorqueue);
24268627Seric ee->e_flags = e->e_flags & ~(EF_INQUEUE|EF_CLRQUEUE|EF_FATALERRS|EF_SENDRECEIPT|EF_RET_PARAM);
24366784Seric ee->e_flags |= EF_NORECEIPT;
24458820Seric setsender(owner, ee, NULL, TRUE);
24558820Seric if (tTd(13, 5))
24658820Seric {
24758820Seric printf("sendall(split): QDONTSEND ");
24858820Seric printaddr(&ee->e_from, FALSE);
24958820Seric }
25058820Seric ee->e_from.q_flags |= QDONTSEND;
25158820Seric ee->e_dfp = NULL;
25258820Seric ee->e_xfp = NULL;
25358820Seric ee->e_errormode = EM_MAIL;
25466009Seric ee->e_sibling = splitenv;
25566009Seric splitenv = ee;
25658820Seric
25758820Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next)
25868627Seric {
25958820Seric if (q->q_owner == owner)
26066305Seric {
26158820Seric q->q_flags |= QDONTSEND;
26266305Seric q->q_flags &= ~QQUEUEUP;
26366305Seric }
26468627Seric }
26558820Seric for (q = ee->e_sendqueue; q != NULL; q = q->q_next)
26668627Seric {
26758820Seric if (q->q_owner != owner)
26866305Seric {
26958820Seric q->q_flags |= QDONTSEND;
27066305Seric q->q_flags &= ~QQUEUEUP;
27166305Seric }
27268627Seric else
27368627Seric {
27468627Seric /* clear DSN parameters */
27568627Seric q->q_flags &= ~(QHASNOTIFY|QPINGONSUCCESS);
27668627Seric q->q_flags |= QPINGONFAILURE|QPINGONDELAY;
27768627Seric }
27868627Seric }
27958820Seric
28068627Seric if (mode != SM_VERIFY && bitset(EF_HAS_DF, e->e_flags))
28158820Seric {
28268627Seric char df1buf[20], df2buf[20];
28368627Seric
28458820Seric ee->e_dfp = NULL;
28568627Seric strcpy(df1buf, queuename(e, 'd'));
28668627Seric strcpy(df2buf, queuename(ee, 'd'));
28768627Seric if (link(df1buf, df2buf) < 0)
28858820Seric {
28958820Seric syserr("sendall: link(%s, %s)",
29068627Seric df1buf, df2buf);
29158820Seric }
29258820Seric }
29358820Seric #ifdef LOG
29458820Seric if (LogLevel > 4)
29566012Seric syslog(LOG_INFO, "%s: clone %s, owner=%s",
29666012Seric ee->e_id, e->e_id, owner);
29758820Seric #endif
29858820Seric }
29958820Seric }
30058820Seric
30158820Seric if (owner != NULL)
30258820Seric {
30358820Seric setsender(owner, e, NULL, TRUE);
30458820Seric if (tTd(13, 5))
30558820Seric {
30658820Seric printf("sendall(owner): QDONTSEND ");
30758820Seric printaddr(&e->e_from, FALSE);
30858820Seric }
30958820Seric e->e_from.q_flags |= QDONTSEND;
31058820Seric e->e_errormode = EM_MAIL;
31166784Seric e->e_flags |= EF_NORECEIPT;
31258820Seric }
31358820Seric
31468627Seric /* if nothing to be delivered, just queue up everything */
31568627Seric if (!somedeliveries && mode != SM_QUEUE && mode != SM_VERIFY)
31668627Seric mode = SM_QUEUE;
31768627Seric
31866009Seric # ifdef QUEUE
31966009Seric if ((mode == SM_QUEUE || mode == SM_FORK ||
32066009Seric (mode != SM_VERIFY && SuperSafe)) &&
32166009Seric !bitset(EF_INQUEUE, e->e_flags))
32258916Seric {
32366009Seric /* be sure everything is instantiated in the queue */
32468627Seric queueup(e, TRUE, mode == SM_QUEUE);
32566009Seric for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
32668627Seric queueup(ee, TRUE, mode == SM_QUEUE);
32758916Seric }
32866009Seric #endif /* QUEUE */
32958916Seric
33063839Seric /*
33168627Seric ** If we belong in background, fork now.
33263839Seric */
33363839Seric
33458916Seric switch (mode)
33558916Seric {
33658916Seric case SM_VERIFY:
33758916Seric Verbose = TRUE;
33858916Seric break;
33958916Seric
34058916Seric case SM_QUEUE:
34158916Seric queueonly:
34268627Seric if (e->e_nrcpts > 0)
34368627Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
34458916Seric return;
34558916Seric
34658916Seric case SM_FORK:
34758916Seric if (e->e_xfp != NULL)
34858916Seric (void) fflush(e->e_xfp);
34958916Seric
35065830Seric # if !HASFLOCK
35158916Seric /*
35264296Seric ** Since fcntl locking has the interesting semantic that
35364296Seric ** the lock is owned by a process, not by an open file
35464296Seric ** descriptor, we have to flush this to the queue, and
35564296Seric ** then restart from scratch in the child.
35658916Seric */
35758916Seric
35868627Seric {
35968627Seric /* save id for future use */
36068627Seric char *qid = e->e_id;
36158916Seric
36268627Seric /* now drop the envelope in the parent */
36368627Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
36468627Seric dropenvelope(e);
36564296Seric
36668627Seric /* and reacquire in the child */
36768627Seric (void) dowork(qid, TRUE, FALSE, e);
36868627Seric }
36964296Seric
37064296Seric return;
37164296Seric
37264296Seric # else /* HASFLOCK */
37364296Seric
37458916Seric pid = fork();
37558916Seric if (pid < 0)
37658916Seric {
37758916Seric goto queueonly;
37858916Seric }
37958916Seric else if (pid > 0)
38058916Seric {
38164310Seric /* be sure we leave the temp files to our child */
38264310Seric /* can't call unlockqueue to avoid unlink of xfp */
38364310Seric if (e->e_lockfp != NULL)
38468627Seric (void) xfclose(e->e_lockfp, "sendenvelope lockfp", e->e_id);
38564310Seric e->e_lockfp = NULL;
38664310Seric
38764310Seric /* close any random open files in the envelope */
38864310Seric closexscript(e);
38964310Seric if (e->e_dfp != NULL)
39068627Seric (void) xfclose(e->e_dfp, "sendenvelope dfp", e->e_id);
39164310Seric e->e_dfp = NULL;
39268627Seric e->e_id = NULL;
39368627Seric e->e_flags &= ~EF_HAS_DF;
39466017Seric
39566017Seric /* catch intermediate zombie */
39666017Seric (void) waitfor(pid);
39758916Seric return;
39858916Seric }
39958916Seric
40058916Seric /* double fork to avoid zombies */
40166017Seric pid = fork();
40266017Seric if (pid > 0)
40358916Seric exit(EX_OK);
40458916Seric
40558916Seric /* be sure we are immune from the terminal */
40663839Seric disconnect(1, e);
40758916Seric
40866017Seric /* prevent parent from waiting if there was an error */
40966017Seric if (pid < 0)
41066017Seric {
41166017Seric e->e_flags |= EF_INQUEUE|EF_KEEPQUEUE;
41266017Seric finis();
41366017Seric }
41466017Seric
41558916Seric /*
41658916Seric ** Close any cached connections.
41758916Seric **
41858916Seric ** We don't send the QUIT protocol because the parent
41958916Seric ** still knows about the connection.
42058916Seric **
42158916Seric ** This should only happen when delivering an error
42258916Seric ** message.
42358916Seric */
42458916Seric
42558916Seric mci_flush(FALSE, NULL);
42658916Seric
42764296Seric # endif /* HASFLOCK */
42864296Seric
42958916Seric break;
43058916Seric }
43158916Seric
43268627Seric if (splitenv != NULL)
43368627Seric {
43468627Seric if (tTd(13, 1))
43568627Seric {
43668627Seric printf("\nsendall: Split queue; remaining queue:\n");
43768627Seric printaddr(e->e_sendqueue, TRUE);
43868627Seric }
43968627Seric
44068627Seric for (ee = splitenv; ee != NULL; ee = ee->e_sibling)
44168627Seric {
44268627Seric CurEnv = ee;
44368627Seric if (mode != SM_VERIFY)
44468627Seric openxscript(ee);
44568627Seric sendenvelope(ee, mode);
44668627Seric dropenvelope(ee);
44768627Seric }
44868627Seric
44968627Seric CurEnv = e;
45068627Seric }
45168627Seric sendenvelope(e, mode);
45268627Seric Verbose = oldverbose;
45368627Seric }
45468627Seric
45568627Seric void
sendenvelope(e,mode)45668627Seric sendenvelope(e, mode)
45768627Seric register ENVELOPE *e;
45868627Seric char mode;
45968627Seric {
46068627Seric register ADDRESS *q;
46168627Seric bool didany;
46268627Seric
46358820Seric /*
46468627Seric ** If we have had global, fatal errors, don't bother sending
46568627Seric ** the message at all if we are in SMTP mode. Local errors
46668627Seric ** (e.g., a single address failing) will still cause the other
46768627Seric ** addresses to be sent.
46868627Seric */
46968627Seric
47068627Seric if (bitset(EF_FATALERRS, e->e_flags) &&
47168627Seric (OpMode == MD_SMTP || OpMode == MD_DAEMON))
47268627Seric {
47368627Seric e->e_flags |= EF_CLRQUEUE;
47468627Seric return;
47568627Seric }
47668627Seric
47768627Seric /*
47858820Seric ** Run through the list and send everything.
47963965Seric **
48063965Seric ** Set EF_GLOBALERRS so that error messages during delivery
48163965Seric ** result in returned mail.
48258820Seric */
48358820Seric
48458820Seric e->e_nsent = 0;
48563965Seric e->e_flags |= EF_GLOBALERRS;
48668627Seric didany = FALSE;
48764696Seric
48864696Seric /* now run through the queue */
48958820Seric for (q = e->e_sendqueue; q != NULL; q = q->q_next)
49058820Seric {
49169881Seric #if XDEBUG
49264441Seric char wbuf[MAXNAME + 20];
49364441Seric
49464441Seric (void) sprintf(wbuf, "sendall(%s)", q->q_paddr);
49564441Seric checkfd012(wbuf);
49664441Seric #endif
49758820Seric if (mode == SM_VERIFY)
49858820Seric {
49958820Seric e->e_to = q->q_paddr;
50058820Seric if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
50160173Seric {
50264942Seric if (q->q_host != NULL && q->q_host[0] != '\0')
50364942Seric message("deliverable: mailer %s, host %s, user %s",
50464942Seric q->q_mailer->m_name,
50564942Seric q->q_host,
50664942Seric q->q_user);
50764942Seric else
50864942Seric message("deliverable: mailer %s, user %s",
50964942Seric q->q_mailer->m_name,
51064942Seric q->q_user);
51160173Seric }
51258820Seric }
51358820Seric else if (!bitset(QDONTSEND|QBADADDR, q->q_flags))
51458820Seric {
51558820Seric # ifdef QUEUE
51658820Seric /*
51758820Seric ** Checkpoint the send list every few addresses
51858820Seric */
51958820Seric
52058820Seric if (e->e_nsent >= CheckpointInterval)
52158820Seric {
52258820Seric queueup(e, TRUE, FALSE);
52358820Seric e->e_nsent = 0;
52458820Seric }
52558820Seric # endif /* QUEUE */
52658820Seric (void) deliver(e, q);
52768627Seric didany = TRUE;
52858820Seric }
52958820Seric }
53068627Seric if (didany)
53168627Seric {
53268627Seric e->e_dtime = curtime();
53368627Seric e->e_ntries++;
53468627Seric }
53558820Seric
53669881Seric #if XDEBUG
53764441Seric checkfd012("end of sendenvelope");
53864441Seric #endif
53964441Seric
54058820Seric if (mode == SM_FORK)
54158820Seric finis();
54258820Seric }
54358820Seric /*
54458820Seric ** DOFORK -- do a fork, retrying a couple of times on failure.
54558820Seric **
54658820Seric ** This MUST be a macro, since after a vfork we are running
54758820Seric ** two processes on the same stack!!!
54858820Seric **
54958820Seric ** Parameters:
55058820Seric ** none.
55158820Seric **
55258820Seric ** Returns:
55358820Seric ** From a macro??? You've got to be kidding!
55458820Seric **
55558820Seric ** Side Effects:
55658820Seric ** Modifies the ==> LOCAL <== variable 'pid', leaving:
55758820Seric ** pid of child in parent, zero in child.
55858820Seric ** -1 on unrecoverable error.
55958820Seric **
56058820Seric ** Notes:
56158820Seric ** I'm awfully sorry this looks so awful. That's
56258820Seric ** vfork for you.....
56358820Seric */
56458820Seric
56558820Seric # define NFORKTRIES 5
56658820Seric
56758820Seric # ifndef FORK
56858820Seric # define FORK fork
56958820Seric # endif
57058820Seric
57158820Seric # define DOFORK(fORKfN) \
57258820Seric {\
57358820Seric register int i;\
57458820Seric \
57558820Seric for (i = NFORKTRIES; --i >= 0; )\
57658820Seric {\
57758820Seric pid = fORKfN();\
57858820Seric if (pid >= 0)\
57958820Seric break;\
58058820Seric if (i > 0)\
58158820Seric sleep((unsigned) NFORKTRIES - i);\
58258820Seric }\
58358820Seric }
58458820Seric /*
58558820Seric ** DOFORK -- simple fork interface to DOFORK.
58658820Seric **
58758820Seric ** Parameters:
58858820Seric ** none.
58958820Seric **
59058820Seric ** Returns:
59158820Seric ** pid of child in parent.
59258820Seric ** zero in child.
59358820Seric ** -1 on error.
59458820Seric **
59558820Seric ** Side Effects:
59658820Seric ** returns twice, once in parent and once in child.
59758820Seric */
59858820Seric
59969748Seric int
dofork()60058820Seric dofork()
60158820Seric {
60268627Seric register int pid = -1;
60358820Seric
60458820Seric DOFORK(fork);
60558820Seric return (pid);
60658820Seric }
60758820Seric /*
6084315Seric ** DELIVER -- Deliver a message to a list of addresses.
609294Seric **
6104315Seric ** This routine delivers to everyone on the same host as the
6114315Seric ** user on the head of the list. It is clever about mailers
6124315Seric ** that don't handle multiple users. It is NOT guaranteed
6134315Seric ** that it will deliver to all these addresses however -- so
6144315Seric ** deliver should be called once for each address on the
6154315Seric ** list.
6164315Seric **
617294Seric ** Parameters:
6189370Seric ** e -- the envelope to deliver.
6194621Seric ** firstto -- head of the address list to deliver to.
620294Seric **
621294Seric ** Returns:
622294Seric ** zero -- successfully delivered.
623294Seric ** else -- some failure, see ExitStat for more info.
624294Seric **
625294Seric ** Side Effects:
626294Seric ** The standard input is passed off to someone.
627294Seric */
628294Seric
62968627Seric int
deliver(e,firstto)6309370Seric deliver(e, firstto)
6319370Seric register ENVELOPE *e;
6324621Seric ADDRESS *firstto;
633294Seric {
6344452Seric char *host; /* host being sent to */
6354452Seric char *user; /* user being sent to */
636294Seric char **pvp;
6373233Seric register char **mvp;
6383233Seric register char *p;
63910306Seric register MAILER *m; /* mailer for this recipient */
6404397Seric ADDRESS *ctladdr;
64154967Seric register MCI *mci;
6424621Seric register ADDRESS *to = firstto;
6434863Seric bool clever = FALSE; /* running user smtp to this mailer */
64468627Seric ADDRESS *tochain = NULL; /* users chain in this mailer call */
64551951Seric int rcode; /* response code */
64657454Seric char *firstsig; /* signature of firstto */
64768627Seric int pid = -1;
64858820Seric char *curhost;
64968627Seric time_t xstart;
65058820Seric int mpvect[2];
65158820Seric int rpvect[2];
65210306Seric char *pv[MAXPV+1];
65358704Seric char tobuf[TOBUFSIZE]; /* text line of to people */
65468627Seric char buf[MAXNAME + 1];
65568627Seric char rpathbuf[MAXNAME + 1]; /* translated return path */
65657441Seric extern int checkcompat();
65769748Seric extern void markfailure __P((ENVELOPE *, ADDRESS *, MCI *, int));
658294Seric
6594488Seric errno = 0;
66058680Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags))
6613233Seric return (0);
662294Seric
66366334Seric #if NAMED_BIND
66434022Sbostic /* unless interactive, try twice, over a minute */
66565580Seric if (OpMode == MD_DAEMON || OpMode == MD_SMTP)
66665580Seric {
66734022Sbostic _res.retrans = 30;
66834022Sbostic _res.retry = 2;
66934022Sbostic }
67036788Sbostic #endif
67134022Sbostic
6726974Seric m = to->q_mailer;
6736974Seric host = to->q_host;
67458803Seric CurEnv = e; /* just in case */
67559044Seric e->e_statmsg = NULL;
67664718Seric SmtpError[0] = '\0';
67768627Seric xstart = curtime();
6786974Seric
6797672Seric if (tTd(10, 1))
68066010Seric printf("\n--deliver, id=%s, mailer=%s, host=`%s', first user=`%s'\n",
68166010Seric e->e_id, m->m_name, host, to->q_user);
68264725Seric if (tTd(10, 100))
68364725Seric printopenfds(FALSE);
684294Seric
685294Seric /*
6863233Seric ** Do initial argv setup.
6873233Seric ** Insert the mailer name. Notice that $x expansion is
6883233Seric ** NOT done on the mailer name. Then, if the mailer has
6893233Seric ** a picky -f flag, we insert it as appropriate. This
6903233Seric ** code does not check for 'pv' overflow; this places a
6913233Seric ** manifest lower limit of 4 for MAXPV.
6928062Seric ** The from address rewrite is expected to make
6938062Seric ** the address relative to the other end.
6942968Seric */
6952968Seric
6964452Seric /* rewrite from address, using rewriting rules */
69759163Seric rcode = EX_OK;
69868627Seric if (bitnset(M_UDBENVELOPE, e->e_from.q_mailer->m_flags))
69968627Seric p = e->e_sender;
70068627Seric else
70168627Seric p = e->e_from.q_paddr;
70268627Seric (void) strcpy(rpathbuf, remotename(p, m,
70359163Seric RF_SENDERADDR|RF_CANONICAL,
70459163Seric &rcode, e));
70558680Seric define('g', rpathbuf, e); /* translated return path */
7069370Seric define('h', host, e); /* to host */
7073233Seric Errors = 0;
7083233Seric pvp = pv;
7093233Seric *pvp++ = m->m_argv[0];
7102968Seric
7113233Seric /* insert -f or -r flag as appropriate */
71210682Seric if (FromFlag && (bitnset(M_FOPT, m->m_flags) || bitnset(M_ROPT, m->m_flags)))
7133233Seric {
71410682Seric if (bitnset(M_FOPT, m->m_flags))
7153233Seric *pvp++ = "-f";
7163233Seric else
7173233Seric *pvp++ = "-r";
71851951Seric *pvp++ = newstr(rpathbuf);
7193233Seric }
720294Seric
721294Seric /*
7223233Seric ** Append the other fixed parts of the argv. These run
7233233Seric ** up to the first entry containing "$u". There can only
7243233Seric ** be one of these, and there are only a few more slots
7253233Seric ** in the pv after it.
726294Seric */
727294Seric
7283233Seric for (mvp = m->m_argv; (p = *++mvp) != NULL; )
729294Seric {
73058050Seric /* can't use strchr here because of sign extension problems */
73158050Seric while (*p != '\0')
73258050Seric {
73358050Seric if ((*p++ & 0377) == MACROEXPAND)
73458050Seric {
73558050Seric if (*p == 'u')
73658050Seric break;
73758050Seric }
73858050Seric }
73958050Seric
74058050Seric if (*p != '\0')
7413233Seric break;
7423233Seric
7433233Seric /* this entry is safe -- go ahead and process it */
74468627Seric expand(*mvp, buf, sizeof buf, e);
7453233Seric *pvp++ = newstr(buf);
7463233Seric if (pvp >= &pv[MAXPV - 3])
7473233Seric {
74858151Seric syserr("554 Too many parameters to %s before $u", pv[0]);
7493233Seric return (-1);
7503233Seric }
751294Seric }
7524863Seric
7536038Seric /*
7546038Seric ** If we have no substitution for the user name in the argument
7556038Seric ** list, we know that we must supply the names otherwise -- and
7566038Seric ** SMTP is the answer!!
7576038Seric */
7586038Seric
7593233Seric if (*mvp == NULL)
7604863Seric {
7614863Seric /* running SMTP */
7625179Seric # ifdef SMTP
7634863Seric clever = TRUE;
7644863Seric *pvp = NULL;
76556795Seric # else /* SMTP */
7666038Seric /* oops! we don't implement SMTP */
76764718Seric syserr("554 SMTP style mailer not implemented");
7685179Seric return (EX_SOFTWARE);
76956795Seric # endif /* SMTP */
7704863Seric }
771294Seric
772294Seric /*
7733233Seric ** At this point *mvp points to the argument with $u. We
7743233Seric ** run through our address list and append all the addresses
7753233Seric ** we can. If we run out of space, do not fret! We can
7763233Seric ** always send another copy later.
777294Seric */
778294Seric
7793233Seric tobuf[0] = '\0';
7809370Seric e->e_to = tobuf;
7814397Seric ctladdr = NULL;
78257454Seric firstsig = hostsignature(firstto->q_mailer, firstto->q_host, e);
7833233Seric for (; to != NULL; to = to->q_next)
784294Seric {
7853233Seric /* avoid sending multiple recipients to dumb mailers */
78610682Seric if (tobuf[0] != '\0' && !bitnset(M_MUSER, m->m_flags))
7873233Seric break;
7883233Seric
7893233Seric /* if already sent or not for this host, don't send */
79058680Seric if (bitset(QDONTSEND|QBADADDR|QQUEUEUP, to->q_flags) ||
79157454Seric to->q_mailer != firstto->q_mailer ||
79257454Seric strcmp(hostsignature(to->q_mailer, to->q_host, e), firstsig) != 0)
7933233Seric continue;
7944397Seric
7958225Seric /* avoid overflowing tobuf */
79642462Sbostic if (sizeof tobuf < (strlen(to->q_paddr) + strlen(tobuf) + 2))
7978225Seric break;
7988225Seric
7997672Seric if (tTd(10, 1))
8005032Seric {
8015032Seric printf("\nsend to ");
8025032Seric printaddr(to, FALSE);
8035032Seric }
8045032Seric
8054397Seric /* compute effective uid/gid when sending */
80668627Seric if (bitnset(M_RUNASRCPT, to->q_mailer->m_flags))
80765137Seric ctladdr = getctladdr(to);
8084397Seric
80968627Seric if (tTd(10, 2))
81068627Seric {
81168627Seric printf("ctladdr=");
81268627Seric printaddr(ctladdr, FALSE);
81368627Seric }
81468627Seric
8153233Seric user = to->q_user;
8169370Seric e->e_to = to->q_paddr;
81757731Seric if (tTd(10, 5))
81857731Seric {
81957731Seric printf("deliver: QDONTSEND ");
82057731Seric printaddr(to, FALSE);
82157731Seric }
82258680Seric to->q_flags |= QDONTSEND;
8233233Seric
8243233Seric /*
8253233Seric ** Check to see that these people are allowed to
8263233Seric ** talk to each other.
8273233Seric */
8283233Seric
82910699Seric if (m->m_maxsize != 0 && e->e_msgsize > m->m_maxsize)
83010699Seric {
83168627Seric e->e_flags |= EF_NO_BODY_RETN;
83269748Seric to->q_status = "5.2.3";
83358151Seric usrerr("552 Message is too large; %ld bytes max", m->m_maxsize);
83468627Seric giveresponse(EX_UNAVAILABLE, m, NULL, ctladdr, xstart, e);
83510699Seric continue;
83610699Seric }
83768627Seric #if NAMED_BIND
83868627Seric h_errno = 0;
83968627Seric #endif
84057441Seric rcode = checkcompat(to, e);
84157459Seric if (rcode != EX_OK)
842294Seric {
84368627Seric markfailure(e, to, NULL, rcode);
84468627Seric giveresponse(rcode, m, NULL, ctladdr, xstart, e);
8453233Seric continue;
846294Seric }
8473233Seric
8483233Seric /*
8494099Seric ** Strip quote bits from names if the mailer is dumb
8504099Seric ** about them.
8513233Seric */
8523233Seric
85310682Seric if (bitnset(M_STRIPQ, m->m_flags))
854294Seric {
85554983Seric stripquotes(user);
85654983Seric stripquotes(host);
8573233Seric }
8583233Seric
8599206Seric /* hack attack -- delivermail compatibility */
8609206Seric if (m == ProgMailer && *user == '|')
8619206Seric user++;
8629206Seric
8633233Seric /*
8644161Seric ** If an error message has already been given, don't
8654161Seric ** bother to send to this address.
8664161Seric **
8674161Seric ** >>>>>>>>>> This clause assumes that the local mailer
8684161Seric ** >> NOTE >> cannot do any further aliasing; that
8694161Seric ** >>>>>>>>>> function is subsumed by sendmail.
8704161Seric */
8714161Seric
8727293Seric if (bitset(QBADADDR|QQUEUEUP, to->q_flags))
8734161Seric continue;
8744161Seric
8754283Seric /* save statistics.... */
8769370Seric markstats(e, to);
8774283Seric
8784161Seric /*
8793233Seric ** See if this user name is "special".
8803233Seric ** If the user name has a slash in it, assume that this
8816974Seric ** is a file -- send it off without further ado. Note
8826974Seric ** that this type of addresses is not processed along
8836974Seric ** with the others, so we fudge on the To person.
8843233Seric */
8853233Seric
88668880Seric if (strcmp(m->m_mailer, "[FILE]") == 0)
8873233Seric {
88869787Seric rcode = mailfile(user, ctladdr, SFF_CREAT, e);
88968627Seric giveresponse(rcode, m, NULL, ctladdr, xstart, e);
89068627Seric e->e_nsent++;
89157402Seric if (rcode == EX_OK)
89268627Seric {
89357402Seric to->q_flags |= QSENT;
89468627Seric if (bitnset(M_LOCALMAILER, m->m_flags) &&
89568627Seric (e->e_receiptto != NULL ||
89668627Seric bitset(QPINGONSUCCESS, to->q_flags)))
89768627Seric {
89868868Seric to->q_flags |= QDELIVERED;
89968850Seric to->q_status = "2.1.5";
90068627Seric fprintf(e->e_xfp, "%s... Successfully delivered\n",
90168627Seric to->q_paddr);
90268627Seric }
90368627Seric }
90468627Seric to->q_statdate = curtime();
90557402Seric continue;
906294Seric }
9073233Seric
9084315Seric /*
9094315Seric ** Address is verified -- add this user to mailer
9104315Seric ** argv, and add it to the print list of recipients.
9114315Seric */
9124315Seric
9136059Seric /* link together the chain of recipients */
9146272Seric to->q_tchain = tochain;
9156272Seric tochain = to;
9166059Seric
9173233Seric /* create list of users for error messages */
9189388Seric (void) strcat(tobuf, ",");
9194082Seric (void) strcat(tobuf, to->q_paddr);
9209370Seric define('u', user, e); /* to user */
92164694Seric p = to->q_home;
92264694Seric if (p == NULL && ctladdr != NULL)
92364694Seric p = ctladdr->q_home;
92464694Seric define('z', p, e); /* user's home */
9253233Seric
9264863Seric /*
9276059Seric ** Expand out this user into argument list.
9284863Seric */
9294863Seric
9306059Seric if (!clever)
9313233Seric {
93268627Seric expand(*mvp, buf, sizeof buf, e);
9334863Seric *pvp++ = newstr(buf);
9344863Seric if (pvp >= &pv[MAXPV - 2])
9354863Seric {
9364863Seric /* allow some space for trailing parms */
9374863Seric break;
9384863Seric }
9394863Seric }
940294Seric }
941294Seric
9424067Seric /* see if any addresses still exist */
9434067Seric if (tobuf[0] == '\0')
9444863Seric {
9459370Seric define('g', (char *) NULL, e);
9464067Seric return (0);
9474863Seric }
9484067Seric
9493233Seric /* print out messages as full list */
9509388Seric e->e_to = tobuf + 1;
9513233Seric
952294Seric /*
9533233Seric ** Fill out any parameters after the $u parameter.
954294Seric */
955294Seric
9564863Seric while (!clever && *++mvp != NULL)
957294Seric {
95868627Seric expand(*mvp, buf, sizeof buf, e);
9593233Seric *pvp++ = newstr(buf);
9603233Seric if (pvp >= &pv[MAXPV])
96158151Seric syserr("554 deliver: pv overflow after $u for %s", pv[0]);
962294Seric }
9633233Seric *pvp++ = NULL;
964294Seric
965294Seric /*
966294Seric ** Call the mailer.
9672898Seric ** The argument vector gets built, pipes
968294Seric ** are created as necessary, and we fork & exec as
9692898Seric ** appropriate.
9704863Seric ** If we are running SMTP, we just need to clean up.
971294Seric */
972294Seric
97365137Seric /*XXX this seems a bit wierd */
97469698Seric if (ctladdr == NULL && m != ProgMailer && m != FileMailer &&
97565750Seric bitset(QGOODUID, e->e_from.q_flags))
97665137Seric ctladdr = &e->e_from;
97765137Seric
97866334Seric #if NAMED_BIND
97951313Seric if (ConfigLevel < 2)
98051313Seric _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */
98135651Seric #endif
98254967Seric
9837672Seric if (tTd(11, 1))
984294Seric {
9858178Seric printf("openmailer:");
98658820Seric printav(pv);
987294Seric }
9884488Seric errno = 0;
98968627Seric #if NAMED_BIND
99068627Seric h_errno = 0;
99168627Seric #endif
9923233Seric
99368627Seric CurHostName = NULL;
99425050Seric
9956038Seric /*
9966038Seric ** Deal with the special case of mail handled through an IPC
9976038Seric ** connection.
9986038Seric ** In this case we don't actually fork. We must be
9996038Seric ** running SMTP for this to work. We will return a
10006038Seric ** zero pid to indicate that we are running IPC.
100111160Seric ** We also handle a debug version that just talks to stdin/out.
10026038Seric */
10036038Seric
100458820Seric curhost = NULL;
100564334Seric SmtpPhase = NULL;
100664718Seric mci = NULL;
100758820Seric
100869881Seric #if XDEBUG
100964401Seric {
101064401Seric char wbuf[MAXLINE];
101164401Seric
101264401Seric /* make absolutely certain 0, 1, and 2 are in use */
101364401Seric sprintf(wbuf, "%s... openmailer(%s)", e->e_to, m->m_name);
101464401Seric checkfd012(wbuf);
101564401Seric }
101664401Seric #endif
101764401Seric
101868627Seric /* check for 8-bit available */
101968627Seric if (bitset(EF_HAS8BIT, e->e_flags) &&
102068627Seric bitnset(M_7BITS, m->m_flags) &&
102169922Seric (!bitset(MM_MIME8BIT, MimeMode) ||
102269922Seric bitset(EF_DONT_MIME, e->e_flags)))
102368627Seric {
102468627Seric usrerr("554 Cannot send 8-bit data to 7-bit destination");
102568627Seric rcode = EX_DATAERR;
102668857Seric e->e_status = "5.6.3";
102768627Seric goto give_up;
102868627Seric }
102968627Seric
103011160Seric /* check for Local Person Communication -- not for mortals!!! */
103111160Seric if (strcmp(m->m_mailer, "[LPC]") == 0)
103211160Seric {
103354967Seric mci = (MCI *) xalloc(sizeof *mci);
103454993Seric bzero((char *) mci, sizeof *mci);
103553738Seric mci->mci_in = stdin;
103653738Seric mci->mci_out = stdout;
103754967Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
103853751Seric mci->mci_mailer = m;
103911160Seric }
104054967Seric else if (strcmp(m->m_mailer, "[IPC]") == 0 ||
104154967Seric strcmp(m->m_mailer, "[TCP]") == 0)
10426038Seric {
104352107Seric #ifdef DAEMON
104457454Seric register int i;
104568627Seric register u_short port = 0;
10466038Seric
104764844Seric if (pv[0] == NULL || pv[1] == NULL || pv[1][0] == '\0')
104864844Seric {
104964844Seric syserr("null host name for %s mailer", m->m_mailer);
105064844Seric rcode = EX_CONFIG;
105164844Seric goto give_up;
105264844Seric }
105364844Seric
105458820Seric CurHostName = pv[1];
105558820Seric curhost = hostsignature(m, pv[1], e);
105654967Seric
105758479Seric if (curhost == NULL || curhost[0] == '\0')
105858479Seric {
105964844Seric syserr("null host signature for %s", pv[1]);
106067133Seric rcode = EX_CONFIG;
106158820Seric goto give_up;
106258479Seric }
106358479Seric
10646038Seric if (!clever)
106558479Seric {
106658151Seric syserr("554 non-clever IPC");
106764718Seric rcode = EX_CONFIG;
106858820Seric goto give_up;
106958479Seric }
107058820Seric if (pv[2] != NULL)
107169840Seric {
107269840Seric port = htons(atoi(pv[2]));
107369840Seric if (port == 0)
107469840Seric {
107569840Seric struct servent *sp = getservbyname(pv[2], "tcp");
107669840Seric
107769840Seric if (sp == NULL)
107869840Seric syserr("Service %s unknown", pv[2]);
107969840Seric else
108069840Seric port = sp->s_port;
108169840Seric }
108269840Seric }
108358820Seric tryhost:
108457454Seric while (*curhost != '\0')
108529433Sbloom {
108657454Seric register char *p;
108768627Seric static char hostbuf[MAXNAME + 1];
108857454Seric
108957454Seric /* pull the next host from the signature */
109057454Seric p = strchr(curhost, ':');
109157454Seric if (p == NULL)
109257454Seric p = &curhost[strlen(curhost)];
109364718Seric if (p == curhost)
109464718Seric {
109564718Seric syserr("deliver: null host name in signature");
109664726Seric curhost++;
109764718Seric continue;
109864718Seric }
109957454Seric strncpy(hostbuf, curhost, p - curhost);
110057454Seric hostbuf[p - curhost] = '\0';
110157454Seric if (*p != '\0')
110257454Seric p++;
110357454Seric curhost = p;
110457454Seric
110553738Seric /* see if we already know that this host is fried */
110657454Seric CurHostName = hostbuf;
110757454Seric mci = mci_get(hostbuf, m);
110854967Seric if (mci->mci_state != MCIS_CLOSED)
110957387Seric {
111057387Seric if (tTd(11, 1))
111157387Seric {
111257387Seric printf("openmailer: ");
111364731Seric mci_dump(mci, FALSE);
111457387Seric }
111557943Seric CurHostName = mci->mci_host;
111668627Seric message("Using cached connection to %s via %s...",
111768627Seric hostbuf, m->m_name);
111858820Seric break;
111957387Seric }
112053751Seric mci->mci_mailer = m;
112154967Seric if (mci->mci_exitstat != EX_OK)
112254967Seric continue;
112354967Seric
112454967Seric /* try the connection */
112557454Seric setproctitle("%s %s: %s", e->e_id, hostbuf, "user open");
112668627Seric message("Connecting to %s via %s...",
112757454Seric hostbuf, m->m_name);
112857454Seric i = makeconnection(hostbuf, port, mci,
112954967Seric bitnset(M_SECURE_PORT, m->m_flags));
113054967Seric mci->mci_exitstat = i;
113154967Seric mci->mci_errno = errno;
113266334Seric #if NAMED_BIND
113363753Seric mci->mci_herrno = h_errno;
113463753Seric #endif
113554967Seric if (i == EX_OK)
113652106Seric {
113754967Seric mci->mci_state = MCIS_OPENING;
113854967Seric mci_cache(mci);
113963753Seric if (TrafficLogFile != NULL)
114063753Seric fprintf(TrafficLogFile, "%05d == CONNECT %s\n",
114163753Seric getpid(), hostbuf);
114254967Seric break;
114334022Sbostic }
114454967Seric else if (tTd(11, 1))
114554967Seric printf("openmailer: makeconnection => stat=%d, errno=%d\n",
114654967Seric i, errno);
114753738Seric
114853738Seric /* enter status of this host */
114953738Seric setstat(i);
115064718Seric
115164718Seric /* should print some message here for -v mode */
11526047Seric }
115364718Seric if (mci == NULL)
115464718Seric {
115564718Seric syserr("deliver: no host name");
115664718Seric rcode = EX_OSERR;
115764718Seric goto give_up;
115864718Seric }
115954993Seric mci->mci_pid = 0;
116054967Seric #else /* no DAEMON */
116158151Seric syserr("554 openmailer: no IPC");
116257387Seric if (tTd(11, 1))
116357387Seric printf("openmailer: NULL\n");
116464718Seric rcode = EX_UNAVAILABLE;
116564718Seric goto give_up;
116654967Seric #endif /* DAEMON */
11676038Seric }
116854967Seric else
1169294Seric {
117068627Seric /* flush any expired connections */
117168627Seric (void) mci_scan(NULL);
117268627Seric
117368627Seric /* announce the connection to verbose listeners */
117468627Seric if (host == NULL || host[0] == '\0')
117568627Seric message("Connecting to %s...", m->m_name);
117668627Seric else
117768627Seric message("Connecting to %s via %s...", host, m->m_name);
117863753Seric if (TrafficLogFile != NULL)
117958852Seric {
118063753Seric char **av;
118158925Seric
118263753Seric fprintf(TrafficLogFile, "%05d === EXEC", getpid());
118363753Seric for (av = pv; *av != NULL; av++)
118463753Seric fprintf(TrafficLogFile, " %s", *av);
118563753Seric fprintf(TrafficLogFile, "\n");
118658852Seric }
118758852Seric
118854967Seric /* create a pipe to shove the mail through */
118954967Seric if (pipe(mpvect) < 0)
119054967Seric {
119158925Seric syserr("%s... openmailer(%s): pipe (to mailer)",
119258925Seric e->e_to, m->m_name);
119357387Seric if (tTd(11, 1))
119457387Seric printf("openmailer: NULL\n");
119558820Seric rcode = EX_OSERR;
119658820Seric goto give_up;
119754967Seric }
11984863Seric
119954967Seric /* if this mailer speaks smtp, create a return pipe */
120054967Seric if (clever && pipe(rpvect) < 0)
120154967Seric {
120258925Seric syserr("%s... openmailer(%s): pipe (from mailer)",
120358925Seric e->e_to, m->m_name);
120454967Seric (void) close(mpvect[0]);
120554967Seric (void) close(mpvect[1]);
120657387Seric if (tTd(11, 1))
120757387Seric printf("openmailer: NULL\n");
120858820Seric rcode = EX_OSERR;
120958820Seric goto give_up;
121054967Seric }
12114863Seric
121254967Seric /*
121354967Seric ** Actually fork the mailer process.
121454967Seric ** DOFORK is clever about retrying.
121554967Seric **
121654967Seric ** Dispose of SIGCHLD signal catchers that may be laying
121754967Seric ** around so that endmail will get it.
121854967Seric */
12196038Seric
122054967Seric if (e->e_xfp != NULL)
122154967Seric (void) fflush(e->e_xfp); /* for debugging */
122254967Seric (void) fflush(stdout);
122326434Seric # ifdef SIGCHLD
122464035Seric (void) setsignal(SIGCHLD, SIG_DFL);
122556795Seric # endif /* SIGCHLD */
122654967Seric DOFORK(FORK);
122754967Seric /* pid is set by DOFORK */
122854967Seric if (pid < 0)
12294863Seric {
123054967Seric /* failure */
123158925Seric syserr("%s... openmailer(%s): cannot fork",
123258925Seric e->e_to, m->m_name);
123354967Seric (void) close(mpvect[0]);
123454967Seric (void) close(mpvect[1]);
123554967Seric if (clever)
123654967Seric {
123754967Seric (void) close(rpvect[0]);
123854967Seric (void) close(rpvect[1]);
123954967Seric }
124057387Seric if (tTd(11, 1))
124157387Seric printf("openmailer: NULL\n");
124258820Seric rcode = EX_OSERR;
124358820Seric goto give_up;
12444863Seric }
124554967Seric else if (pid == 0)
124654967Seric {
124754967Seric int i;
124856678Seric int saveerrno;
124969719Seric struct stat stb;
125054967Seric extern int DtableSize;
125115772Seric
125268475Seric if (e->e_lockfp != NULL)
125368589Seric (void) close(fileno(e->e_lockfp));
125468475Seric
125554967Seric /* child -- set up input & exec mailer */
125664035Seric (void) setsignal(SIGINT, SIG_IGN);
125764035Seric (void) setsignal(SIGHUP, SIG_IGN);
125864035Seric (void) setsignal(SIGTERM, SIG_DFL);
12594709Seric
126069880Seric if (m != FileMailer || stat(tochain->q_user, &stb) < 0)
126169719Seric stb.st_mode = 0;
126269719Seric
126368816Seric /* tweak niceness */
126468816Seric if (m->m_nice != 0)
126568816Seric nice(m->m_nice);
126668816Seric
126769719Seric /* reset group id */
126868627Seric if (bitnset(M_SPECIFIC_UID, m->m_flags))
126968627Seric (void) setgid(m->m_gid);
127069719Seric else if (bitset(S_ISGID, stb.st_mode))
127169719Seric (void) setgid(stb.st_gid);
127269719Seric else if (ctladdr != NULL && ctladdr->q_gid != 0)
127368627Seric {
127468627Seric (void) initgroups(ctladdr->q_ruser?
127568627Seric ctladdr->q_ruser: ctladdr->q_user,
127668627Seric ctladdr->q_gid);
127768627Seric (void) setgid(ctladdr->q_gid);
127868627Seric }
127968627Seric else
128068627Seric {
128168627Seric (void) initgroups(DefUser, DefGid);
128268627Seric if (m->m_gid == 0)
128364837Seric (void) setgid(DefGid);
128468627Seric else
128568627Seric (void) setgid(m->m_gid);
128669719Seric }
128769719Seric
128869719Seric /* reset user id */
1289*69963Seric endpwent();
129069719Seric if (bitnset(M_SPECIFIC_UID, m->m_flags))
129169719Seric (void) setuid(m->m_uid);
129269719Seric else if (bitset(S_ISUID, stb.st_mode))
129369719Seric (void) setuid(stb.st_uid);
129469719Seric else if (ctladdr != NULL && ctladdr->q_uid != 0)
129569719Seric (void) setuid(ctladdr->q_uid);
129669719Seric else
129769719Seric {
129868627Seric if (m->m_uid == 0)
129964145Seric (void) setuid(DefUid);
130064145Seric else
130168627Seric (void) setuid(m->m_uid);
130264145Seric }
130364145Seric
130464145Seric if (tTd(11, 2))
130564145Seric printf("openmailer: running as r/euid=%d/%d\n",
130664145Seric getuid(), geteuid());
130764145Seric
130858935Seric /* move into some "safe" directory */
130958935Seric if (m->m_execdir != NULL)
131058935Seric {
131158935Seric char *p, *q;
131268627Seric char buf[MAXLINE + 1];
131358935Seric
131458935Seric for (p = m->m_execdir; p != NULL; p = q)
131558935Seric {
131658935Seric q = strchr(p, ':');
131758935Seric if (q != NULL)
131858935Seric *q = '\0';
131968627Seric expand(p, buf, sizeof buf, e);
132058935Seric if (q != NULL)
132158935Seric *q++ = ':';
132258935Seric if (tTd(11, 20))
132358935Seric printf("openmailer: trydir %s\n",
132458935Seric buf);
132558935Seric if (buf[0] != '\0' && chdir(buf) >= 0)
132658935Seric break;
132758935Seric }
132858935Seric }
132958935Seric
133054967Seric /* arrange to filter std & diag output of command */
133154967Seric if (clever)
133254967Seric {
133354967Seric (void) close(rpvect[0]);
133458852Seric if (dup2(rpvect[1], STDOUT_FILENO) < 0)
133558852Seric {
133658925Seric syserr("%s... openmailer(%s): cannot dup pipe %d for stdout",
133758925Seric e->e_to, m->m_name, rpvect[1]);
133858852Seric _exit(EX_OSERR);
133958852Seric }
134054967Seric (void) close(rpvect[1]);
134154967Seric }
134266089Seric else if (OpMode == MD_SMTP || OpMode == MD_DAEMON ||
134366089Seric HoldErrs || DisConnected)
134454967Seric {
134554967Seric /* put mailer output in transcript */
134658852Seric if (dup2(fileno(e->e_xfp), STDOUT_FILENO) < 0)
134758852Seric {
134858925Seric syserr("%s... openmailer(%s): cannot dup xscript %d for stdout",
134958925Seric e->e_to, m->m_name,
135058852Seric fileno(e->e_xfp));
135158852Seric _exit(EX_OSERR);
135258852Seric }
135354967Seric }
135458852Seric if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
135558852Seric {
135658925Seric syserr("%s... openmailer(%s): cannot dup stdout for stderr",
135758925Seric e->e_to, m->m_name);
135858852Seric _exit(EX_OSERR);
135958852Seric }
13604709Seric
136154967Seric /* arrange to get standard input */
136254967Seric (void) close(mpvect[1]);
136358731Seric if (dup2(mpvect[0], STDIN_FILENO) < 0)
13644417Seric {
136558925Seric syserr("%s... openmailer(%s): cannot dup pipe %d for stdin",
136658925Seric e->e_to, m->m_name, mpvect[0]);
136754967Seric _exit(EX_OSERR);
13684417Seric }
136954967Seric (void) close(mpvect[0]);
13709370Seric
137154967Seric /* arrange for all the files to be closed */
137254967Seric for (i = 3; i < DtableSize; i++)
137354967Seric {
137454967Seric register int j;
137564145Seric
137654967Seric if ((j = fcntl(i, F_GETFD, 0)) != -1)
137764145Seric (void) fcntl(i, F_SETFD, j | 1);
137854967Seric }
13792774Seric
138066746Seric /* run disconnected from terminal */
138166746Seric (void) setsid();
138266746Seric
138354967Seric /* try to execute the mailer */
138469476Seric execve(m->m_mailer, (ARGV_T) pv, (ARGV_T) UserEnviron);
138556678Seric saveerrno = errno;
138654967Seric syserr("Cannot exec %s", m->m_mailer);
138768627Seric if (bitnset(M_LOCALMAILER, m->m_flags) ||
138868627Seric transienterror(saveerrno))
138960008Seric _exit(EX_OSERR);
139054967Seric _exit(EX_UNAVAILABLE);
139151835Seric }
139254967Seric
139354967Seric /*
139454967Seric ** Set up return value.
139554967Seric */
139654967Seric
139754967Seric mci = (MCI *) xalloc(sizeof *mci);
139854993Seric bzero((char *) mci, sizeof *mci);
139954967Seric mci->mci_mailer = m;
140054967Seric mci->mci_state = clever ? MCIS_OPENING : MCIS_OPEN;
140154993Seric mci->mci_pid = pid;
140254967Seric (void) close(mpvect[0]);
140354967Seric mci->mci_out = fdopen(mpvect[1], "w");
140464724Seric if (mci->mci_out == NULL)
140564724Seric {
140664724Seric syserr("deliver: cannot create mailer output channel, fd=%d",
140764724Seric mpvect[1]);
140864724Seric (void) close(mpvect[1]);
140964724Seric if (clever)
141064724Seric {
141164724Seric (void) close(rpvect[0]);
141264724Seric (void) close(rpvect[1]);
141364724Seric }
141464724Seric rcode = EX_OSERR;
141564724Seric goto give_up;
141664724Seric }
141754967Seric if (clever)
141854967Seric {
141954967Seric (void) close(rpvect[1]);
142054967Seric mci->mci_in = fdopen(rpvect[0], "r");
142164724Seric if (mci->mci_in == NULL)
142264724Seric {
142364724Seric syserr("deliver: cannot create mailer input channel, fd=%d",
142464724Seric mpvect[1]);
142564724Seric (void) close(rpvect[0]);
142664724Seric fclose(mci->mci_out);
142764724Seric mci->mci_out = NULL;
142864724Seric rcode = EX_OSERR;
142964724Seric goto give_up;
143064724Seric }
143154967Seric }
143254967Seric else
143354967Seric {
143454967Seric mci->mci_flags |= MCIF_TEMP;
143554967Seric mci->mci_in = NULL;
143654967Seric }
1437294Seric }
1438294Seric
14394709Seric /*
144054967Seric ** If we are in SMTP opening state, send initial protocol.
14414709Seric */
14424709Seric
144354967Seric if (clever && mci->mci_state != MCIS_CLOSED)
14444863Seric {
144554967Seric smtpinit(m, mci, e);
144653738Seric }
144768627Seric
144868627Seric if (bitset(EF_HAS8BIT, e->e_flags) && bitnset(M_7BITS, m->m_flags))
144968627Seric mci->mci_flags |= MCIF_CVT8TO7;
145068627Seric else
145168627Seric mci->mci_flags &= ~MCIF_CVT8TO7;
145268627Seric
145357387Seric if (tTd(11, 1))
145457387Seric {
145557387Seric printf("openmailer: ");
145664731Seric mci_dump(mci, FALSE);
145757387Seric }
1458294Seric
145958820Seric if (mci->mci_state != MCIS_OPEN)
146058820Seric {
146158820Seric /* couldn't open the mailer */
146258820Seric rcode = mci->mci_exitstat;
146358820Seric errno = mci->mci_errno;
146466334Seric #if NAMED_BIND
146563753Seric h_errno = mci->mci_herrno;
146663753Seric #endif
146758820Seric if (rcode == EX_OK)
146858820Seric {
146958820Seric /* shouldn't happen */
147058820Seric syserr("554 deliver: rcode=%d, mci_state=%d, sig=%s",
147158820Seric rcode, mci->mci_state, firstsig);
147258820Seric rcode = EX_SOFTWARE;
147358820Seric }
147468627Seric else if (curhost != NULL && *curhost != '\0')
147559958Seric {
147659958Seric /* try next MX site */
147759958Seric goto tryhost;
147859958Seric }
147958820Seric }
148058820Seric else if (!clever)
148158820Seric {
148258820Seric /*
148358820Seric ** Format and send message.
148458820Seric */
148558820Seric
148665870Seric putfromline(mci, e);
148768627Seric (*e->e_puthdr)(mci, e->e_header, e);
148868228Seric (*e->e_putbody)(mci, e, NULL);
148958820Seric
149058820Seric /* get the exit status */
149158820Seric rcode = endmailer(mci, e, pv);
149258820Seric }
149358820Seric else
149458820Seric #ifdef SMTP
149558820Seric {
149658820Seric /*
149758820Seric ** Send the MAIL FROM: protocol
149858820Seric */
149958820Seric
150058820Seric rcode = smtpmailfrom(m, mci, e);
150158820Seric if (rcode == EX_OK)
150258820Seric {
150358820Seric register char *t = tobuf;
150458820Seric register int i;
150558820Seric
150658820Seric /* send the recipient list */
150758820Seric tobuf[0] = '\0';
150858820Seric for (to = tochain; to != NULL; to = to->q_tchain)
150958820Seric {
151058820Seric e->e_to = to->q_paddr;
151158820Seric if ((i = smtprcpt(to, m, mci, e)) != EX_OK)
151258820Seric {
151368627Seric markfailure(e, to, mci, i);
151468627Seric giveresponse(i, m, mci, ctladdr, xstart, e);
151558820Seric }
151658820Seric else
151758820Seric {
151858820Seric *t++ = ',';
151958820Seric for (p = to->q_paddr; *p; *t++ = *p++)
152058820Seric continue;
152164372Seric *t = '\0';
152258820Seric }
152358820Seric }
152458820Seric
152558820Seric /* now send the data */
152658820Seric if (tobuf[0] == '\0')
152758820Seric {
152858820Seric rcode = EX_OK;
152958820Seric e->e_to = NULL;
153058820Seric if (bitset(MCIF_CACHED, mci->mci_flags))
153158820Seric smtprset(m, mci, e);
153258820Seric }
153358820Seric else
153458820Seric {
153558820Seric e->e_to = tobuf + 1;
153658820Seric rcode = smtpdata(m, mci, e);
153758820Seric }
153858820Seric
153958820Seric /* now close the connection */
154058820Seric if (!bitset(MCIF_CACHED, mci->mci_flags))
154158820Seric smtpquit(m, mci, e);
154258820Seric }
154364922Seric if (rcode != EX_OK && curhost != NULL && *curhost != '\0')
154458820Seric {
154558820Seric /* try next MX site */
154658820Seric goto tryhost;
154758820Seric }
154858820Seric }
154958820Seric #else /* not SMTP */
155058820Seric {
155158820Seric syserr("554 deliver: need SMTP compiled to use clever mailer");
155258820Seric rcode = EX_CONFIG;
155358820Seric goto give_up;
155458820Seric }
155558820Seric #endif /* SMTP */
155666334Seric #if NAMED_BIND
155758820Seric if (ConfigLevel < 2)
155858820Seric _res.options |= RES_DEFNAMES | RES_DNSRCH; /* XXX */
155958820Seric #endif
156058820Seric
156158820Seric /* arrange a return receipt if requested */
156264718Seric if (rcode == EX_OK && e->e_receiptto != NULL &&
156364718Seric bitnset(M_LOCALMAILER, m->m_flags))
156458820Seric {
156558820Seric e->e_flags |= EF_SENDRECEIPT;
156658820Seric /* do we want to send back more info? */
156758820Seric }
156858820Seric
156958820Seric /*
157058820Seric ** Do final status disposal.
157158820Seric ** We check for something in tobuf for the SMTP case.
157258820Seric ** If we got a temporary failure, arrange to queue the
157358820Seric ** addressees.
157458820Seric */
157558820Seric
157658820Seric give_up:
157758820Seric if (tobuf[0] != '\0')
157868627Seric giveresponse(rcode, m, mci, ctladdr, xstart, e);
157958820Seric for (to = tochain; to != NULL; to = to->q_tchain)
158058820Seric {
158158820Seric if (rcode != EX_OK)
158268627Seric markfailure(e, to, mci, rcode);
158368627Seric else if (!bitset(QBADADDR|QQUEUEUP, to->q_flags))
158458820Seric {
158558820Seric to->q_flags |= QSENT;
158668627Seric to->q_statdate = curtime();
158758820Seric e->e_nsent++;
158868627Seric if (bitnset(M_LOCALMAILER, m->m_flags) &&
158968627Seric (e->e_receiptto != NULL ||
159068627Seric bitset(QPINGONSUCCESS, to->q_flags)))
159164718Seric {
159268868Seric to->q_flags |= QDELIVERED;
159368850Seric to->q_status = "2.1.5";
159464718Seric fprintf(e->e_xfp, "%s... Successfully delivered\n",
159564718Seric to->q_paddr);
159664718Seric }
159768627Seric else if (bitset(QPINGONSUCCESS, to->q_flags) &&
159868627Seric bitset(QPRIMARY, to->q_flags) &&
159968627Seric !bitset(MCIF_DSN, mci->mci_flags))
160068627Seric {
160168627Seric to->q_flags |= QRELAYED;
160268627Seric fprintf(e->e_xfp, "%s... relayed; expect no further notifications\n",
160368627Seric to->q_paddr);
160468627Seric }
160558820Seric }
160658820Seric }
160758820Seric
160858820Seric /*
160958820Seric ** Restore state and return.
161058820Seric */
161158820Seric
161269881Seric #if XDEBUG
161364401Seric {
161464401Seric char wbuf[MAXLINE];
161564401Seric
161664401Seric /* make absolutely certain 0, 1, and 2 are in use */
161764554Seric sprintf(wbuf, "%s... end of deliver(%s)",
161864554Seric e->e_to == NULL ? "NO-TO-LIST" : e->e_to,
161964554Seric m->m_name);
162064401Seric checkfd012(wbuf);
162164401Seric }
162264401Seric #endif
162364401Seric
162458820Seric errno = 0;
162558820Seric define('g', (char *) NULL, e);
162658820Seric return (rcode);
1627294Seric }
1628294Seric /*
162958820Seric ** MARKFAILURE -- mark a failure on a specific address.
163058820Seric **
163158820Seric ** Parameters:
163258820Seric ** e -- the envelope we are sending.
163358820Seric ** q -- the address to mark.
163468627Seric ** mci -- mailer connection information.
163558820Seric ** rcode -- the code signifying the particular failure.
163658820Seric **
163758820Seric ** Returns:
163858820Seric ** none.
163958820Seric **
164058820Seric ** Side Effects:
164158820Seric ** marks the address (and possibly the envelope) with the
164258820Seric ** failure so that an error will be returned or
164358820Seric ** the message will be queued, as appropriate.
164458820Seric */
164558820Seric
164669748Seric void
markfailure(e,q,mci,rcode)164768627Seric markfailure(e, q, mci, rcode)
164858820Seric register ENVELOPE *e;
164958820Seric register ADDRESS *q;
165068627Seric register MCI *mci;
165158820Seric int rcode;
165258820Seric {
165368627Seric char *stat = NULL;
165458820Seric
165565051Seric switch (rcode)
165665051Seric {
165765051Seric case EX_OK:
165865051Seric break;
165965051Seric
166065051Seric case EX_TEMPFAIL:
166165051Seric case EX_IOERR:
166265051Seric case EX_OSERR:
166363753Seric q->q_flags |= QQUEUEUP;
166465051Seric break;
166565051Seric
166665051Seric default:
166758820Seric q->q_flags |= QBADADDR;
166865051Seric break;
166965051Seric }
167068627Seric
167168857Seric /* find most specific error code possible */
167268627Seric if (q->q_status == NULL && mci != NULL)
167368627Seric q->q_status = mci->mci_status;
167468857Seric if (q->q_status == NULL)
167568857Seric q->q_status = e->e_status;
167668857Seric if (q->q_status == NULL)
167768627Seric {
167868857Seric switch (rcode)
167968857Seric {
168068857Seric case EX_USAGE:
168168857Seric stat = "5.5.4";
168268857Seric break;
168368627Seric
168468857Seric case EX_DATAERR:
168568857Seric stat = "5.5.2";
168668857Seric break;
168768627Seric
168868857Seric case EX_NOUSER:
168968857Seric stat = "5.1.1";
169068857Seric break;
169168627Seric
169268857Seric case EX_NOHOST:
169368857Seric stat = "5.1.2";
169468857Seric break;
169568627Seric
169668857Seric case EX_NOINPUT:
169768857Seric case EX_CANTCREAT:
169868857Seric case EX_NOPERM:
169968857Seric stat = "5.3.0";
170068857Seric break;
170168627Seric
170268857Seric case EX_UNAVAILABLE:
170368857Seric case EX_SOFTWARE:
170468857Seric case EX_OSFILE:
170568857Seric case EX_PROTOCOL:
170668857Seric case EX_CONFIG:
170768857Seric stat = "5.5.0";
170868857Seric break;
170968627Seric
171068857Seric case EX_OSERR:
171168857Seric case EX_IOERR:
171268857Seric stat = "4.5.0";
171368857Seric break;
171468857Seric
171568857Seric case EX_TEMPFAIL:
171668857Seric stat = "4.2.0";
171768857Seric break;
171868857Seric }
171968857Seric if (stat != NULL)
172068857Seric q->q_status = stat;
172168627Seric }
172268627Seric
172368627Seric q->q_statdate = curtime();
172468627Seric if (CurHostName != NULL && CurHostName[0] != '\0')
172568627Seric q->q_statmta = newstr(CurHostName);
172668627Seric if (rcode != EX_OK && q->q_rstatus == NULL)
172768627Seric {
172868627Seric char buf[30];
172968627Seric
173068627Seric (void) sprintf(buf, "%d", rcode);
173168627Seric q->q_rstatus = newstr(buf);
173268627Seric }
173358820Seric }
173458820Seric /*
173558820Seric ** ENDMAILER -- Wait for mailer to terminate.
173658820Seric **
173758820Seric ** We should never get fatal errors (e.g., segmentation
173858820Seric ** violation), so we report those specially. For other
173958820Seric ** errors, we choose a status message (into statmsg),
174058820Seric ** and if it represents an error, we print it.
174158820Seric **
174258820Seric ** Parameters:
174358820Seric ** pid -- pid of mailer.
174458820Seric ** e -- the current envelope.
174558820Seric ** pv -- the parameter vector that invoked the mailer
174658820Seric ** (for error messages).
174758820Seric **
174858820Seric ** Returns:
174958820Seric ** exit code of mailer.
175058820Seric **
175158820Seric ** Side Effects:
175258820Seric ** none.
175358820Seric */
175458820Seric
175569748Seric int
endmailer(mci,e,pv)175658820Seric endmailer(mci, e, pv)
175758820Seric register MCI *mci;
175858820Seric register ENVELOPE *e;
175958820Seric char **pv;
176058820Seric {
176158820Seric int st;
176258820Seric
176358820Seric /* close any connections */
176458820Seric if (mci->mci_in != NULL)
176565198Seric (void) xfclose(mci->mci_in, mci->mci_mailer->m_name, "mci_in");
176658820Seric if (mci->mci_out != NULL)
176765198Seric (void) xfclose(mci->mci_out, mci->mci_mailer->m_name, "mci_out");
176858820Seric mci->mci_in = mci->mci_out = NULL;
176958820Seric mci->mci_state = MCIS_CLOSED;
177058820Seric
177158820Seric /* in the IPC case there is nothing to wait for */
177258820Seric if (mci->mci_pid == 0)
177358820Seric return (EX_OK);
177458820Seric
177558820Seric /* wait for the mailer process to die and collect status */
177658820Seric st = waitfor(mci->mci_pid);
177758820Seric if (st == -1)
177858820Seric {
177958820Seric syserr("endmailer %s: wait", pv[0]);
178058820Seric return (EX_SOFTWARE);
178158820Seric }
178258820Seric
178364379Seric if (WIFEXITED(st))
178458820Seric {
178564379Seric /* normal death -- return status */
178664379Seric return (WEXITSTATUS(st));
178764379Seric }
178858820Seric
178964379Seric /* it died a horrid death */
179065545Seric syserr("451 mailer %s died with signal %o",
179165545Seric mci->mci_mailer->m_name, st);
179258820Seric
179364379Seric /* log the arguments */
179465194Seric if (pv != NULL && e->e_xfp != NULL)
179564379Seric {
179664379Seric register char **av;
179758820Seric
179864379Seric fprintf(e->e_xfp, "Arguments:");
179964379Seric for (av = pv; *av != NULL; av++)
180064379Seric fprintf(e->e_xfp, " %s", *av);
180164379Seric fprintf(e->e_xfp, "\n");
180258820Seric }
180358820Seric
180464379Seric ExitStat = EX_TEMPFAIL;
180564379Seric return (EX_TEMPFAIL);
180658820Seric }
180758820Seric /*
1808294Seric ** GIVERESPONSE -- Interpret an error response from a mailer
1809294Seric **
1810294Seric ** Parameters:
1811294Seric ** stat -- the status code from the mailer (high byte
1812294Seric ** only; core dumps must have been taken care of
1813294Seric ** already).
181458337Seric ** m -- the mailer info for this mailer.
181558337Seric ** mci -- the mailer connection info -- can be NULL if the
181658337Seric ** response is given before the connection is made.
181764771Seric ** ctladdr -- the controlling address for the recipient
181864771Seric ** address(es).
181968627Seric ** xstart -- the transaction start time, for computing
182068627Seric ** transaction delays.
182158337Seric ** e -- the current envelope.
1822294Seric **
1823294Seric ** Returns:
18244082Seric ** none.
1825294Seric **
1826294Seric ** Side Effects:
18271518Seric ** Errors may be incremented.
1828294Seric ** ExitStat may be set.
1829294Seric */
1830294Seric
183169748Seric void
giveresponse(stat,m,mci,ctladdr,xstart,e)183268627Seric giveresponse(stat, m, mci, ctladdr, xstart, e)
1833294Seric int stat;
18349370Seric register MAILER *m;
183558337Seric register MCI *mci;
183664771Seric ADDRESS *ctladdr;
183768627Seric time_t xstart;
183810105Seric ENVELOPE *e;
1839294Seric {
184060094Seric register const char *statmsg;
1841294Seric extern char *SysExMsg[];
1842294Seric register int i;
184336788Sbostic extern int N_SysEx;
184410105Seric char buf[MAXLINE];
1845294Seric
18464315Seric /*
18474315Seric ** Compute status message from code.
18484315Seric */
18494315Seric
1850294Seric i = stat - EX__BASE;
18519370Seric if (stat == 0)
185258852Seric {
18539370Seric statmsg = "250 Sent";
185458916Seric if (e->e_statmsg != NULL)
185558852Seric {
185658916Seric (void) sprintf(buf, "%s (%s)", statmsg, e->e_statmsg);
185758852Seric statmsg = buf;
185858852Seric }
185958852Seric }
18609370Seric else if (i < 0 || i > N_SysEx)
18619370Seric {
18629370Seric (void) sprintf(buf, "554 unknown mailer error %d", stat);
18639370Seric stat = EX_UNAVAILABLE;
18649370Seric statmsg = buf;
18659370Seric }
186610105Seric else if (stat == EX_TEMPFAIL)
186710105Seric {
186858664Seric (void) strcpy(buf, SysExMsg[i] + 1);
186966334Seric #if NAMED_BIND
187025527Smiriam if (h_errno == TRY_AGAIN)
187163993Seric statmsg = errstring(h_errno+E_DNSBASE);
187221061Seric else
187336788Sbostic #endif
187421061Seric {
187525527Smiriam if (errno != 0)
187625527Smiriam statmsg = errstring(errno);
187725527Smiriam else
187825527Smiriam {
187921061Seric #ifdef SMTP
188025527Smiriam statmsg = SmtpError;
188156795Seric #else /* SMTP */
188225527Smiriam statmsg = NULL;
188356795Seric #endif /* SMTP */
188425527Smiriam }
188521061Seric }
188621061Seric if (statmsg != NULL && statmsg[0] != '\0')
188721061Seric {
188810124Seric (void) strcat(buf, ": ");
188921061Seric (void) strcat(buf, statmsg);
189010105Seric }
189110105Seric statmsg = buf;
189210105Seric }
189366334Seric #if NAMED_BIND
189463753Seric else if (stat == EX_NOHOST && h_errno != 0)
189563753Seric {
189663993Seric statmsg = errstring(h_errno + E_DNSBASE);
189767077Seric (void) sprintf(buf, "%s (%s)", SysExMsg[i] + 1, statmsg);
189863753Seric statmsg = buf;
189963753Seric }
190063753Seric #endif
1901294Seric else
190221061Seric {
1903294Seric statmsg = SysExMsg[i];
190458664Seric if (*statmsg++ == ':')
190558664Seric {
190658664Seric (void) sprintf(buf, "%s: %s", statmsg, errstring(errno));
190758664Seric statmsg = buf;
190858664Seric }
190921061Seric }
19109370Seric
19119370Seric /*
19129370Seric ** Print the message as appropriate
19139370Seric */
19149370Seric
191510105Seric if (stat == EX_OK || stat == EX_TEMPFAIL)
191664718Seric {
191764718Seric extern char MsgBuf[];
191864718Seric
191966307Seric message("%s", &statmsg[4]);
192064718Seric if (stat == EX_TEMPFAIL && e->e_xfp != NULL)
192164718Seric fprintf(e->e_xfp, "%s\n", &MsgBuf[4]);
192264718Seric }
1923294Seric else
1924294Seric {
192567077Seric char mbuf[8];
192667077Seric
19271518Seric Errors++;
192867077Seric sprintf(mbuf, "%.3s %%s", statmsg);
192967077Seric usrerr(mbuf, &statmsg[4]);
1930294Seric }
1931294Seric
1932294Seric /*
1933294Seric ** Final cleanup.
1934294Seric ** Log a record of the transaction. Compute the new
1935294Seric ** ExitStat -- if we already had an error, stick with
1936294Seric ** that.
1937294Seric */
1938294Seric
193958020Seric if (LogLevel > ((stat == EX_TEMPFAIL) ? 8 : (stat == EX_OK) ? 7 : 6))
194068627Seric logdelivery(m, mci, &statmsg[4], ctladdr, xstart, e);
19417858Seric
194266323Seric if (tTd(11, 2))
194366323Seric printf("giveresponse: stat=%d, e->e_message=%s\n",
194467962Seric stat, e->e_message == NULL ? "<NULL>" : e->e_message);
194566323Seric
19464621Seric if (stat != EX_TEMPFAIL)
19474621Seric setstat(stat);
194864952Seric if (stat != EX_OK && (stat != EX_TEMPFAIL || e->e_message == NULL))
194910105Seric {
195010105Seric if (e->e_message != NULL)
195110105Seric free(e->e_message);
195210105Seric e->e_message = newstr(&statmsg[4]);
195310105Seric }
195410124Seric errno = 0;
195566334Seric #if NAMED_BIND
195625527Smiriam h_errno = 0;
195736788Sbostic #endif
1958294Seric }
1959294Seric /*
19608496Seric ** LOGDELIVERY -- log the delivery in the system log
19618496Seric **
196264969Seric ** Care is taken to avoid logging lines that are too long, because
196364969Seric ** some versions of syslog have an unfortunate proclivity for core
196464969Seric ** dumping. This is a hack, to be sure, that is at best empirical.
196564969Seric **
19668496Seric ** Parameters:
196758337Seric ** m -- the mailer info. Can be NULL for initial queue.
196858337Seric ** mci -- the mailer connection info -- can be NULL if the
196958337Seric ** log is occuring when no connection is active.
197058337Seric ** stat -- the message to print for the status.
197164771Seric ** ctladdr -- the controlling address for the to list.
197268627Seric ** xstart -- the transaction start time, used for
197368627Seric ** computing transaction delay.
197458337Seric ** e -- the current envelope.
19758496Seric **
19768496Seric ** Returns:
19778496Seric ** none
19788496Seric **
19798496Seric ** Side Effects:
19808496Seric ** none
19818496Seric */
19828496Seric
198369748Seric void
logdelivery(m,mci,stat,ctladdr,xstart,e)198468627Seric logdelivery(m, mci, stat, ctladdr, xstart, e)
198558337Seric MAILER *m;
198658337Seric register MCI *mci;
198769748Seric const char *stat;
198864771Seric ADDRESS *ctladdr;
198968627Seric time_t xstart;
199054967Seric register ENVELOPE *e;
19918496Seric {
199258343Seric # ifdef LOG
199364771Seric register char *bp;
199464969Seric register char *p;
199564969Seric int l;
199658418Seric char buf[512];
19978496Seric
199865059Seric # if (SYSLOG_BUFSIZE) >= 256
199964771Seric bp = buf;
200064771Seric if (ctladdr != NULL)
200164771Seric {
200264771Seric strcpy(bp, ", ctladdr=");
200365016Seric strcat(bp, shortenstring(ctladdr->q_paddr, 83));
200464771Seric bp += strlen(bp);
200564771Seric if (bitset(QGOODUID, ctladdr->q_flags))
200664771Seric {
200764771Seric (void) sprintf(bp, " (%d/%d)",
200864771Seric ctladdr->q_uid, ctladdr->q_gid);
200964771Seric bp += strlen(bp);
201064771Seric }
201164771Seric }
201258337Seric
201368627Seric sprintf(bp, ", delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
201464771Seric bp += strlen(bp);
201564771Seric
201668627Seric if (xstart != (time_t) 0)
201768627Seric {
201868627Seric sprintf(bp, ", xdelay=%s", pintvl(curtime() - xstart, TRUE));
201968627Seric bp += strlen(bp);
202068627Seric }
202168627Seric
202258513Seric if (m != NULL)
202358305Seric {
202464771Seric (void) strcpy(bp, ", mailer=");
202564771Seric (void) strcat(bp, m->m_name);
202664771Seric bp += strlen(bp);
202758305Seric }
202858513Seric
202958513Seric if (mci != NULL && mci->mci_host != NULL)
203058305Seric {
203158305Seric # ifdef DAEMON
203258755Seric extern SOCKADDR CurHostAddr;
203358513Seric # endif
203458305Seric
203564771Seric (void) strcpy(bp, ", relay=");
203664771Seric (void) strcat(bp, mci->mci_host);
203758513Seric
203858513Seric # ifdef DAEMON
203965919Seric (void) strcat(bp, " [");
204064771Seric (void) strcat(bp, anynet_ntoa(&CurHostAddr));
204165919Seric (void) strcat(bp, "]");
204258305Seric # endif
204358305Seric }
204466894Seric else if (strcmp(stat, "queued") != 0)
204558513Seric {
204658513Seric char *p = macvalue('h', e);
204758343Seric
204858513Seric if (p != NULL && p[0] != '\0')
204958513Seric {
205064771Seric (void) strcpy(bp, ", relay=");
205164771Seric (void) strcat(bp, p);
205258513Seric }
205358513Seric }
205464922Seric bp += strlen(bp);
205564969Seric
205665059Seric #define STATLEN (((SYSLOG_BUFSIZE) - 100) / 4)
205765059Seric #if (STATLEN) < 63
205865059Seric # undef STATLEN
205965059Seric # define STATLEN 63
206065059Seric #endif
206165059Seric #if (STATLEN) > 203
206265059Seric # undef STATLEN
206365059Seric # define STATLEN 203
206465059Seric #endif
206565059Seric
206665059Seric if ((bp - buf) > (sizeof buf - ((STATLEN) + 20)))
206764969Seric {
206864969Seric /* desperation move -- truncate data */
206965059Seric bp = buf + sizeof buf - ((STATLEN) + 17);
207064969Seric strcpy(bp, "...");
207164969Seric bp += 3;
207264969Seric }
207364969Seric
207464969Seric (void) strcpy(bp, ", stat=");
207564969Seric bp += strlen(bp);
207665059Seric
207765059Seric (void) strcpy(bp, shortenstring(stat, (STATLEN)));
207858418Seric
207964969Seric l = SYSLOG_BUFSIZE - 100 - strlen(buf);
208064969Seric p = e->e_to;
208168706Seric while (strlen(p) >= (SIZE_T) l)
208264969Seric {
208364969Seric register char *q = strchr(p + l, ',');
208464969Seric
208565008Seric if (q == NULL)
208664969Seric break;
208764969Seric syslog(LOG_INFO, "%s: to=%.*s [more]%s",
208864969Seric e->e_id, ++q - p, p, buf);
208964969Seric p = q;
209064969Seric }
209164969Seric syslog(LOG_INFO, "%s: to=%s%s", e->e_id, p, buf);
209265059Seric
209365059Seric # else /* we have a very short log buffer size */
209465059Seric
209566258Seric l = SYSLOG_BUFSIZE - 85;
209665059Seric p = e->e_to;
209765059Seric while (strlen(p) >= l)
209865059Seric {
209965059Seric register char *q = strchr(p + l, ',');
210065059Seric
210165059Seric if (q == NULL)
210265059Seric break;
210365059Seric syslog(LOG_INFO, "%s: to=%.*s [more]",
210465059Seric e->e_id, ++q - p, p);
210565059Seric p = q;
210665059Seric }
210765059Seric syslog(LOG_INFO, "%s: to=%s", e->e_id, p);
210865059Seric
210965059Seric if (ctladdr != NULL)
211065059Seric {
211165059Seric bp = buf;
211265059Seric strcpy(buf, "ctladdr=");
211365059Seric bp += strlen(buf);
211465059Seric strcpy(bp, shortenstring(ctladdr->q_paddr, 83));
211565059Seric bp += strlen(buf);
211665059Seric if (bitset(QGOODUID, ctladdr->q_flags))
211765059Seric {
211865059Seric (void) sprintf(bp, " (%d/%d)",
211965059Seric ctladdr->q_uid, ctladdr->q_gid);
212065059Seric bp += strlen(bp);
212165059Seric }
212265059Seric syslog(LOG_INFO, "%s: %s", e->e_id, buf);
212365059Seric }
212465650Seric bp = buf;
212565650Seric sprintf(bp, "delay=%s", pintvl(curtime() - e->e_ctime, TRUE));
212665650Seric bp += strlen(bp);
212768627Seric if (xstart != (time_t) 0)
212868627Seric {
212968627Seric sprintf(bp, ", xdelay=%s", pintvl(curtime() - xstart, TRUE));
213068627Seric bp += strlen(bp);
213168627Seric }
213265059Seric
213365059Seric if (m != NULL)
213465650Seric {
213565650Seric sprintf(bp, ", mailer=%s", m->m_name);
213665650Seric bp += strlen(bp);
213765650Seric }
213866009Seric syslog(LOG_INFO, "%s: %s", e->e_id, buf);
213965059Seric
214066009Seric buf[0] = '\0';
214165059Seric if (mci != NULL && mci->mci_host != NULL)
214265059Seric {
214365059Seric # ifdef DAEMON
214465059Seric extern SOCKADDR CurHostAddr;
214565059Seric # endif
214665059Seric
214766021Seric sprintf(buf, "relay=%s", mci->mci_host);
214865059Seric
214965059Seric # ifdef DAEMON
215066009Seric (void) strcat(buf, " [");
215166009Seric (void) strcat(buf, anynet_ntoa(&CurHostAddr));
215266009Seric (void) strcat(buf, "]");
215365059Seric # endif
215465059Seric }
215566894Seric else if (strcmp(stat, "queued") != 0)
215665059Seric {
215765059Seric char *p = macvalue('h', e);
215865059Seric
215965059Seric if (p != NULL && p[0] != '\0')
216066021Seric sprintf(buf, "relay=%s", p);
216165059Seric }
216266009Seric if (buf[0] != '\0')
216366009Seric syslog(LOG_INFO, "%s: %s", e->e_id, buf);
216465059Seric
216565059Seric syslog(LOG_INFO, "%s: stat=%s", e->e_id, shortenstring(stat, 63));
216665059Seric # endif /* short log buffer */
216756795Seric # endif /* LOG */
21688496Seric }
21698496Seric /*
21706974Seric ** PUTFROMLINE -- output a UNIX-style from line (or whatever)
2171294Seric **
21726974Seric ** This can be made an arbitrary message separator by changing $l
2173294Seric **
217416150Seric ** One of the ugliest hacks seen by human eyes is contained herein:
217516150Seric ** UUCP wants those stupid "remote from <host>" lines. Why oh why
217616150Seric ** does a well-meaning programmer such as myself have to deal with
217716150Seric ** this kind of antique garbage????
21786974Seric **
2179294Seric ** Parameters:
218065870Seric ** mci -- the connection information.
218165870Seric ** e -- the envelope.
2182294Seric **
2183294Seric ** Returns:
21846974Seric ** none
2185294Seric **
2186294Seric ** Side Effects:
21876974Seric ** outputs some text to fp.
2188294Seric */
2189294Seric
219069748Seric void
putfromline(mci,e)219165870Seric putfromline(mci, e)
219265870Seric register MCI *mci;
219354967Seric ENVELOPE *e;
2194294Seric {
219558050Seric char *template = "\201l\n";
21966974Seric char buf[MAXLINE];
2197294Seric
219865870Seric if (bitnset(M_NHDR, mci->mci_mailer->m_flags))
21996974Seric return;
22004315Seric
220165870Seric if (bitnset(M_UGLYUUCP, mci->mci_mailer->m_flags))
22024205Seric {
220312223Seric char *bang;
220412223Seric char xbuf[MAXLINE];
22056041Seric
220668627Seric expand("\201g", buf, sizeof buf, e);
220756795Seric bang = strchr(buf, '!');
22086974Seric if (bang == NULL)
220964370Seric {
221064370Seric errno = 0;
221164370Seric syserr("554 No ! in UUCP From address! (%s given)", buf);
221264370Seric }
22135099Seric else
22149370Seric {
221512223Seric *bang++ = '\0';
221658050Seric (void) sprintf(xbuf, "From %s \201d remote from %s\n", bang, buf);
221712223Seric template = xbuf;
22189370Seric }
22196974Seric }
222068627Seric expand(template, buf, sizeof buf, e);
222168850Seric putxline(buf, mci, PXLF_NOTHINGSPECIAL);
22225981Seric }
22235981Seric /*
22246974Seric ** PUTBODY -- put the body of a message.
22256974Seric **
22266974Seric ** Parameters:
222765870Seric ** mci -- the connection information.
22289538Seric ** e -- the envelope to put out.
222959730Seric ** separator -- if non-NULL, a message separator that must
223059730Seric ** not be permitted in the resulting message.
22316974Seric **
22326974Seric ** Returns:
22336974Seric ** none.
22346974Seric **
22356974Seric ** Side Effects:
22366974Seric ** The message is written onto fp.
22376974Seric */
22386974Seric
223968627Seric /* values for output state variable */
224068627Seric #define OS_HEAD 0 /* at beginning of line */
224168627Seric #define OS_CR 1 /* read a carriage return */
224268627Seric #define OS_INLINE 2 /* putting rest of line */
224368627Seric
224469748Seric void
putbody(mci,e,separator)224568228Seric putbody(mci, e, separator)
224665870Seric register MCI *mci;
22479538Seric register ENVELOPE *e;
224859730Seric char *separator;
22496974Seric {
225010168Seric char buf[MAXLINE];
22516974Seric
22526974Seric /*
22536974Seric ** Output the body of the message
22546974Seric */
22556974Seric
225668627Seric if (e->e_dfp == NULL && bitset(EF_HAS_DF, e->e_flags))
225768627Seric {
225868627Seric char *df = queuename(e, 'd');
225968627Seric
226068627Seric e->e_dfp = fopen(df, "r");
226168627Seric if (e->e_dfp == NULL)
226268627Seric syserr("putbody: Cannot open %s for %s from %s",
226368627Seric df, e->e_to, e->e_from.q_paddr);
226468627Seric }
22659538Seric if (e->e_dfp == NULL)
22666974Seric {
226768627Seric if (bitset(MCIF_INHEADER, mci->mci_flags))
22689538Seric {
226968627Seric putline("", mci);
227068627Seric mci->mci_flags &= ~MCIF_INHEADER;
22719538Seric }
227268627Seric putline("<<< No Message Collected >>>", mci);
227368627Seric goto endofmessage;
227468627Seric }
227568627Seric if (e->e_dfino == (ino_t) 0)
227668627Seric {
227768627Seric struct stat stbuf;
227868627Seric
227968627Seric if (fstat(fileno(e->e_dfp), &stbuf) < 0)
228068627Seric e->e_dfino = -1;
228168609Seric else
228268627Seric {
228368627Seric e->e_dfdev = stbuf.st_dev;
228468627Seric e->e_dfino = stbuf.st_ino;
228568627Seric }
228668515Seric }
228768627Seric rewind(e->e_dfp);
228868627Seric
228969480Seric #if MIME8TO7
229068627Seric if (bitset(MCIF_CVT8TO7, mci->mci_flags))
229168515Seric {
229268627Seric char *boundaries[MAXMIMENESTING + 1];
229368627Seric
229468627Seric /*
229568627Seric ** Do 8 to 7 bit MIME conversion.
229668627Seric */
229768627Seric
229868627Seric /* make sure it looks like a MIME message */
229968627Seric if (hvalue("MIME-Version", e->e_header) == NULL)
230068627Seric putline("MIME-Version: 1.0", mci);
230168627Seric
230268627Seric if (hvalue("Content-Type", e->e_header) == NULL)
230368515Seric {
230468627Seric sprintf(buf, "Content-Type: text/plain; charset=%s",
230568627Seric defcharset(e));
230667813Seric putline(buf, mci);
230767813Seric }
230867813Seric
230968627Seric /* now do the hard work */
231068627Seric boundaries[0] = NULL;
231168627Seric mime8to7(mci, e->e_header, e, boundaries, M87F_OUTER);
231268627Seric }
231368627Seric else
231469480Seric #endif
231568627Seric {
231668627Seric int ostate;
231768627Seric register char *bp;
231868627Seric register char *pbp;
231968627Seric register int c;
232068627Seric int padc;
232168627Seric char *buflim;
232268627Seric int pos = 0;
232368627Seric char peekbuf[10];
232468627Seric
232568627Seric /* we can pass it through unmodified */
232668627Seric if (bitset(MCIF_INHEADER, mci->mci_flags))
232716875Seric {
232868627Seric putline("", mci);
232968627Seric mci->mci_flags &= ~MCIF_INHEADER;
233067546Seric }
233168627Seric
233268627Seric /* determine end of buffer; allow for short mailer lines */
233368627Seric buflim = &buf[sizeof buf - 1];
233468627Seric if (mci->mci_mailer->m_linelimit > 0 &&
233568627Seric mci->mci_mailer->m_linelimit < sizeof buf - 1)
233668627Seric buflim = &buf[mci->mci_mailer->m_linelimit - 1];
233768627Seric
233868627Seric /* copy temp file to output with mapping */
233968627Seric ostate = OS_HEAD;
234068627Seric bp = buf;
234168627Seric pbp = peekbuf;
234268627Seric while (!ferror(mci->mci_out))
234368627Seric {
234468627Seric register char *xp;
234568627Seric
234668627Seric if (pbp > peekbuf)
234768627Seric c = *--pbp;
234868878Seric else if ((c = getc(e->e_dfp)) == EOF)
234968627Seric break;
235068627Seric if (bitset(MCIF_7BIT, mci->mci_flags))
235168627Seric c &= 0x7f;
235268627Seric switch (ostate)
235368627Seric {
235468627Seric case OS_HEAD:
235568627Seric if (c != '\r' && c != '\n' && bp < buflim)
235668627Seric {
235768627Seric *bp++ = c;
235868627Seric break;
235968627Seric }
236068627Seric
236168627Seric /* check beginning of line for special cases */
236268627Seric *bp = '\0';
236368627Seric pos = 0;
236468627Seric padc = EOF;
236568627Seric if (buf[0] == 'F' &&
236668627Seric bitnset(M_ESCFROM, mci->mci_mailer->m_flags) &&
236768627Seric strncmp(buf, "From ", 5) == 0)
236868627Seric {
236968627Seric padc = '>';
237068627Seric }
237168627Seric if (buf[0] == '-' && buf[1] == '-' &&
237268627Seric separator != NULL)
237368627Seric {
237468627Seric /* possible separator */
237568627Seric int sl = strlen(separator);
237668627Seric
237768627Seric if (strncmp(&buf[2], separator, sl) == 0)
237868627Seric padc = ' ';
237968627Seric }
238068627Seric if (buf[0] == '.' &&
238168627Seric bitnset(M_XDOT, mci->mci_mailer->m_flags))
238268627Seric {
238368627Seric padc = '.';
238468627Seric }
238568627Seric
238668627Seric /* now copy out saved line */
238768627Seric if (TrafficLogFile != NULL)
238868627Seric {
238968627Seric fprintf(TrafficLogFile, "%05d >>> ", getpid());
239068627Seric if (padc != EOF)
239168878Seric putc(padc, TrafficLogFile);
239268627Seric for (xp = buf; xp < bp; xp++)
239368878Seric putc(*xp, TrafficLogFile);
239468627Seric if (c == '\n')
239568627Seric fputs(mci->mci_mailer->m_eol,
239668627Seric TrafficLogFile);
239768627Seric }
239868627Seric if (padc != EOF)
239968627Seric {
240068878Seric putc(padc, mci->mci_out);
240168627Seric pos++;
240268627Seric }
240368627Seric for (xp = buf; xp < bp; xp++)
240468878Seric putc(*xp, mci->mci_out);
240568627Seric if (c == '\n')
240668627Seric {
240768627Seric fputs(mci->mci_mailer->m_eol,
240868627Seric mci->mci_out);
240968627Seric pos = 0;
241068627Seric }
241168627Seric else
241268627Seric {
241368627Seric pos += bp - buf;
241468627Seric if (c != '\r')
241568627Seric *pbp++ = c;
241668627Seric }
241768627Seric bp = buf;
241868627Seric
241968627Seric /* determine next state */
242068627Seric if (c == '\n')
242168627Seric ostate = OS_HEAD;
242268627Seric else if (c == '\r')
242368627Seric ostate = OS_CR;
242468627Seric else
242568627Seric ostate = OS_INLINE;
242668627Seric continue;
242768627Seric
242868627Seric case OS_CR:
242968627Seric if (c == '\n')
243068627Seric {
243168627Seric /* got CRLF */
243268627Seric fputs(mci->mci_mailer->m_eol, mci->mci_out);
243368627Seric if (TrafficLogFile != NULL)
243468627Seric {
243568627Seric fputs(mci->mci_mailer->m_eol,
243668627Seric TrafficLogFile);
243768627Seric }
243868627Seric ostate = OS_HEAD;
243968627Seric continue;
244068627Seric }
244168627Seric
244268627Seric /* had a naked carriage return */
244368627Seric *pbp++ = c;
244468627Seric c = '\r';
244568627Seric goto putch;
244668627Seric
244768627Seric case OS_INLINE:
244868627Seric if (c == '\r')
244968627Seric {
245068627Seric ostate = OS_CR;
245168627Seric continue;
245268627Seric }
245368627Seric putch:
245468627Seric if (mci->mci_mailer->m_linelimit > 0 &&
245568627Seric pos > mci->mci_mailer->m_linelimit &&
245668627Seric c != '\n')
245768627Seric {
245868627Seric putc('!', mci->mci_out);
245968627Seric fputs(mci->mci_mailer->m_eol, mci->mci_out);
246068627Seric if (TrafficLogFile != NULL)
246168627Seric {
246268627Seric fprintf(TrafficLogFile, "!%s",
246368627Seric mci->mci_mailer->m_eol);
246468627Seric }
246568627Seric ostate = OS_HEAD;
246668627Seric *pbp++ = c;
246768627Seric continue;
246868627Seric }
246968627Seric if (TrafficLogFile != NULL)
247068878Seric putc(c, TrafficLogFile);
247168627Seric putc(c, mci->mci_out);
247268627Seric pos++;
247368627Seric ostate = c == '\n' ? OS_HEAD : OS_INLINE;
247468627Seric break;
247568627Seric }
247668627Seric }
247767599Seric }
24786974Seric
247968627Seric if (ferror(e->e_dfp))
248068627Seric {
248168627Seric syserr("putbody: df%s: read error", e->e_id);
248268627Seric ExitStat = EX_IOERR;
248368627Seric }
248468627Seric
248568627Seric endofmessage:
248659542Seric /* some mailers want extra blank line at end of message */
248765870Seric if (bitnset(M_BLANKEND, mci->mci_mailer->m_flags) &&
248865870Seric buf[0] != '\0' && buf[0] != '\n')
248965870Seric putline("", mci);
249059542Seric
249165870Seric (void) fflush(mci->mci_out);
249265870Seric if (ferror(mci->mci_out) && errno != EPIPE)
24936974Seric {
24946974Seric syserr("putbody: write error");
24956974Seric ExitStat = EX_IOERR;
24966974Seric }
24976974Seric errno = 0;
24986974Seric }
24996974Seric /*
2500294Seric ** MAILFILE -- Send a message to a file.
2501294Seric **
25024327Seric ** If the file has the setuid/setgid bits set, but NO execute
25034327Seric ** bits, sendmail will try to become the owner of that file
25044327Seric ** rather than the real user. Obviously, this only works if
25054327Seric ** sendmail runs as root.
25064327Seric **
25079370Seric ** This could be done as a subordinate mailer, except that it
25089370Seric ** is used implicitly to save messages in ~/dead.letter. We
25099370Seric ** view this as being sufficiently important as to include it
25109370Seric ** here. For example, if the system is dying, we shouldn't have
25119370Seric ** to create another process plus some pipes to save the message.
25129370Seric **
2513294Seric ** Parameters:
2514294Seric ** filename -- the name of the file to send to.
25154397Seric ** ctladdr -- the controlling address header -- includes
25164397Seric ** the userid/groupid to be when sending.
251769787Seric ** sfflags -- flags for opening.
251869787Seric ** e -- the current envelope.
2519294Seric **
2520294Seric ** Returns:
2521294Seric ** The exit code associated with the operation.
2522294Seric **
2523294Seric ** Side Effects:
2524294Seric ** none.
2525294Seric */
2526294Seric
252769748Seric int
mailfile(filename,ctladdr,sfflags,e)252869787Seric mailfile(filename, ctladdr, sfflags, e)
2529294Seric char *filename;
25304397Seric ADDRESS *ctladdr;
253169787Seric int sfflags;
253254967Seric register ENVELOPE *e;
2533294Seric {
2534294Seric register FILE *f;
253568627Seric register int pid = -1;
253653751Seric int mode;
2537294Seric
253859267Seric if (tTd(11, 1))
253959267Seric {
254059267Seric printf("mailfile %s\n ctladdr=", filename);
254159267Seric printaddr(ctladdr, FALSE);
254259267Seric }
254359267Seric
254463753Seric if (e->e_xfp != NULL)
254563753Seric fflush(e->e_xfp);
254663753Seric
25474214Seric /*
25484214Seric ** Fork so we can change permissions here.
25494214Seric ** Note that we MUST use fork, not vfork, because of
25504214Seric ** the complications of calling subroutines, etc.
25514214Seric */
25524067Seric
25534214Seric DOFORK(fork);
25544214Seric
25554214Seric if (pid < 0)
25564214Seric return (EX_OSERR);
25574214Seric else if (pid == 0)
25584214Seric {
25594214Seric /* child -- actually write to file */
25604327Seric struct stat stb;
256168475Seric struct stat fsb;
256265870Seric MCI mcibuf;
256368490Seric int oflags = O_WRONLY|O_APPEND;
25644327Seric
256568475Seric if (e->e_lockfp != NULL)
256668689Seric (void) close(fileno(e->e_lockfp));
256768475Seric
256864035Seric (void) setsignal(SIGINT, SIG_DFL);
256964035Seric (void) setsignal(SIGHUP, SIG_DFL);
257064035Seric (void) setsignal(SIGTERM, SIG_DFL);
257123102Seric (void) umask(OldUmask);
257269787Seric e->e_to = filename;
257369787Seric ExitStat = EX_OK;
257452673Seric
257568627Seric #ifdef HASLSTAT
257668627Seric if ((SafeFileEnv != NULL ? lstat(filename, &stb)
257768627Seric : stat(filename, &stb)) < 0)
257868627Seric #else
25794327Seric if (stat(filename, &stb) < 0)
258068627Seric #endif
258168490Seric {
258259745Seric stb.st_mode = FileMode;
258368490Seric oflags |= O_CREAT|O_EXCL;
258468490Seric }
258568627Seric else if (bitset(0111, stb.st_mode) || stb.st_nlink != 1 ||
258668627Seric (SafeFileEnv != NULL && !S_ISREG(stb.st_mode)))
258768475Seric exit(EX_CANTCREAT);
258853751Seric mode = stb.st_mode;
258952673Seric
259052673Seric /* limit the errors to those actually caused in the child */
259152673Seric errno = 0;
259252673Seric ExitStat = EX_OK;
259352673Seric
259469787Seric if (ctladdr != NULL || bitset(SFF_RUNASREALUID, sfflags))
259553751Seric {
259653751Seric /* ignore setuid and setgid bits */
259753751Seric mode &= ~(S_ISGID|S_ISUID);
259853751Seric }
259953751Seric
260040931Srick /* we have to open the dfile BEFORE setuid */
260168627Seric if (e->e_dfp == NULL && bitset(EF_HAS_DF, e->e_flags))
260240931Srick {
260368627Seric char *df = queuename(e, 'd');
260468627Seric
260568627Seric e->e_dfp = fopen(df, "r");
260652673Seric if (e->e_dfp == NULL)
260752673Seric {
260840931Srick syserr("mailfile: Cannot open %s for %s from %s",
260968627Seric df, e->e_to, e->e_from.q_paddr);
261040931Srick }
261140931Srick }
261240931Srick
261368627Seric if (SafeFileEnv != NULL && SafeFileEnv[0] != '\0')
261468490Seric {
261568627Seric int i;
261668627Seric
261768627Seric if (chroot(SafeFileEnv) < 0)
261868490Seric {
261968627Seric syserr("mailfile: Cannot chroot(%s)",
262068627Seric SafeFileEnv);
262168627Seric exit(EX_CANTCREAT);
262268490Seric }
262368627Seric i = strlen(SafeFileEnv);
262468627Seric if (strncmp(SafeFileEnv, filename, i) == 0)
262568627Seric filename += i;
262668627Seric }
262768627Seric if (chdir("/") < 0)
262868627Seric syserr("mailfile: cannot chdir(/)");
262968627Seric
263069787Seric /* select a new user to run as */
263169787Seric if (!bitset(SFF_RUNASREALUID, sfflags))
263268627Seric {
263369787Seric if (bitset(S_ISUID, mode))
263469787Seric {
263569787Seric RealUserName = NULL;
263669787Seric RealUid = stb.st_uid;
263769787Seric }
263869787Seric else if (ctladdr != NULL && ctladdr->q_uid != 0)
263969787Seric {
264069787Seric if (ctladdr->q_ruser != NULL)
264169787Seric RealUserName = ctladdr->q_ruser;
264269787Seric else
264369787Seric RealUserName = ctladdr->q_user;
264469787Seric RealUid = ctladdr->q_uid;
264569787Seric }
264669787Seric else if (FileMailer != NULL && FileMailer->m_uid != 0)
264769787Seric {
264869787Seric RealUserName = DefUser;
264969787Seric RealUid = FileMailer->m_uid;
265069787Seric }
265169787Seric else
265269787Seric {
265369787Seric RealUserName = DefUser;
265469787Seric RealUid = DefUid;
265569787Seric }
265669787Seric
265769787Seric /* select a new group to run as */
265869787Seric if (bitset(S_ISGID, mode))
265969787Seric RealGid = stb.st_gid;
266069787Seric else if (ctladdr != NULL && ctladdr->q_uid != 0)
266169787Seric RealGid = ctladdr->q_gid;
266268627Seric else if (FileMailer != NULL && FileMailer->m_gid != 0)
266369787Seric RealGid = FileMailer->m_gid;
266468627Seric else
266569787Seric RealGid = DefGid;
26664417Seric }
266769787Seric
266869787Seric /* last ditch */
266969787Seric if (!bitset(SFF_ROOTOK, sfflags))
26704417Seric {
267169787Seric if (RealUid == 0)
267269787Seric RealUid = DefUid;
267369787Seric if (RealGid == 0)
267469787Seric RealGid = DefGid;
26754417Seric }
267669787Seric
267769787Seric /* now set the group and user ids */
2678*69963Seric endpwent();
267969787Seric if (RealUserName != NULL)
268069787Seric (void) initgroups(RealUserName, RealGid);
268169787Seric else
268269787Seric (void) setgid(RealGid);
268369787Seric (void) setuid(RealUid);
268469787Seric
268569787Seric sfflags |= SFF_NOPATHCHECK;
268669787Seric sfflags &= ~SFF_OPENASROOT;
268769787Seric f = safefopen(filename, oflags, FileMode, sfflags);
26884214Seric if (f == NULL)
268952673Seric {
269064387Seric message("554 cannot open: %s", errstring(errno));
26914214Seric exit(EX_CANTCREAT);
269252673Seric }
26934214Seric
269465870Seric bzero(&mcibuf, sizeof mcibuf);
269565870Seric mcibuf.mci_mailer = FileMailer;
269665870Seric mcibuf.mci_out = f;
269765870Seric if (bitnset(M_7BITS, FileMailer->m_flags))
269865870Seric mcibuf.mci_flags |= MCIF_7BIT;
269965870Seric
270065870Seric putfromline(&mcibuf, e);
270168627Seric (*e->e_puthdr)(&mcibuf, e->e_header, e);
270268228Seric (*e->e_putbody)(&mcibuf, e, NULL);
270365870Seric putline("\n", &mcibuf);
270452673Seric if (ferror(f))
270552673Seric {
270664387Seric message("451 I/O error: %s", errstring(errno));
270752673Seric setstat(EX_IOERR);
270852673Seric }
270958680Seric (void) xfclose(f, "mailfile", filename);
27104214Seric (void) fflush(stdout);
27114417Seric
27126887Seric /* reset ISUID & ISGID bits for paranoid systems */
27134621Seric (void) chmod(filename, (int) stb.st_mode);
271452673Seric exit(ExitStat);
27154315Seric /*NOTREACHED*/
27164214Seric }
27174214Seric else
27184214Seric {
27194214Seric /* parent -- wait for exit status */
27209370Seric int st;
27214214Seric
27229370Seric st = waitfor(pid);
272364379Seric if (WIFEXITED(st))
272464379Seric return (WEXITSTATUS(st));
272564379Seric else
272664387Seric {
272764387Seric syserr("child died on signal %d", st);
27289370Seric return (EX_UNAVAILABLE);
272964387Seric }
273040931Srick /*NOTREACHED*/
27314214Seric }
2732294Seric }
27334550Seric /*
273457454Seric ** HOSTSIGNATURE -- return the "signature" for a host.
273557454Seric **
273657454Seric ** The signature describes how we are going to send this -- it
273757454Seric ** can be just the hostname (for non-Internet hosts) or can be
273857454Seric ** an ordered list of MX hosts.
273957454Seric **
274057454Seric ** Parameters:
274157454Seric ** m -- the mailer describing this host.
274257454Seric ** host -- the host name.
274357454Seric ** e -- the current envelope.
274457454Seric **
274557454Seric ** Returns:
274657454Seric ** The signature for this host.
274757454Seric **
274857454Seric ** Side Effects:
274957454Seric ** Can tweak the symbol table.
275057454Seric */
275157454Seric
275257454Seric char *
hostsignature(m,host,e)275357454Seric hostsignature(m, host, e)
275457454Seric register MAILER *m;
275557454Seric char *host;
275657454Seric ENVELOPE *e;
275757454Seric {
275857454Seric register char *p;
275957454Seric register STAB *s;
276057454Seric int i;
276157454Seric int len;
276266334Seric #if NAMED_BIND
276357454Seric int nmx;
276457454Seric auto int rcode;
276559076Seric char *hp;
276659076Seric char *endp;
276768627Seric int oldoptions = _res.options;
276857454Seric char *mxhosts[MAXMXHOSTS + 1];
276957454Seric #endif
277057454Seric
277157454Seric /*
277257454Seric ** Check to see if this uses IPC -- if not, it can't have MX records.
277357454Seric */
277457454Seric
277557454Seric p = m->m_mailer;
277657454Seric if (strcmp(p, "[IPC]") != 0 && strcmp(p, "[TCP]") != 0)
277757454Seric {
277857454Seric /* just an ordinary mailer */
277957454Seric return host;
278057454Seric }
278157454Seric
278257454Seric /*
278357454Seric ** Look it up in the symbol table.
278457454Seric */
278557454Seric
278657454Seric s = stab(host, ST_HOSTSIG, ST_ENTER);
278757454Seric if (s->s_hostsig != NULL)
278857454Seric return s->s_hostsig;
278957454Seric
279057454Seric /*
279157454Seric ** Not already there -- create a signature.
279257454Seric */
279357454Seric
279466334Seric #if NAMED_BIND
279559111Seric if (ConfigLevel < 2)
279659111Seric _res.options &= ~(RES_DEFNAMES | RES_DNSRCH); /* XXX */
279759111Seric
279859076Seric for (hp = host; hp != NULL; hp = endp)
279957454Seric {
280059076Seric endp = strchr(hp, ':');
280159076Seric if (endp != NULL)
280259076Seric *endp = '\0';
280357454Seric
280459273Seric nmx = getmxrr(hp, mxhosts, TRUE, &rcode);
280557454Seric
280659076Seric if (nmx <= 0)
280759076Seric {
280859076Seric register MCI *mci;
280957454Seric
281059076Seric /* update the connection info for this host */
281159076Seric mci = mci_get(hp, m);
281259076Seric mci->mci_exitstat = rcode;
281359076Seric mci->mci_errno = errno;
281463753Seric mci->mci_herrno = h_errno;
281559076Seric
281659076Seric /* and return the original host name as the signature */
281759076Seric nmx = 1;
281859076Seric mxhosts[0] = hp;
281959076Seric }
282059076Seric
282159076Seric len = 0;
282259076Seric for (i = 0; i < nmx; i++)
282359076Seric {
282459076Seric len += strlen(mxhosts[i]) + 1;
282559076Seric }
282659076Seric if (s->s_hostsig != NULL)
282759076Seric len += strlen(s->s_hostsig) + 1;
282859076Seric p = xalloc(len);
282959076Seric if (s->s_hostsig != NULL)
283059076Seric {
283159076Seric (void) strcpy(p, s->s_hostsig);
283259076Seric free(s->s_hostsig);
283359076Seric s->s_hostsig = p;
283459076Seric p += strlen(p);
283557454Seric *p++ = ':';
283659076Seric }
283759076Seric else
283859076Seric s->s_hostsig = p;
283959076Seric for (i = 0; i < nmx; i++)
284059076Seric {
284159076Seric if (i != 0)
284259076Seric *p++ = ':';
284359076Seric strcpy(p, mxhosts[i]);
284459076Seric p += strlen(p);
284559076Seric }
284659076Seric if (endp != NULL)
284759076Seric *endp++ = ':';
284857454Seric }
284957454Seric makelower(s->s_hostsig);
285059111Seric if (ConfigLevel < 2)
285159111Seric _res.options = oldoptions;
285257454Seric #else
285357454Seric /* not using BIND -- the signature is just the host name */
285457454Seric s->s_hostsig = host;
285557454Seric #endif
285657454Seric if (tTd(17, 1))
285757454Seric printf("hostsignature(%s) = %s\n", host, s->s_hostsig);
285857454Seric return s->s_hostsig;
285957454Seric }
2860