xref: /csrg-svn/usr.sbin/sendmail/src/mci.c (revision 63753)
154963Seric /*
254963Seric  * Copyright (c) 1983 Eric P. Allman
362526Sbostic  * Copyright (c) 1988, 1993
462526Sbostic  *	The Regents of the University of California.  All rights reserved.
554963Seric  *
654963Seric  * %sccs.include.redist.c%
754963Seric  */
854963Seric 
954963Seric #ifndef lint
10*63753Seric static char sccsid[] = "@(#)mci.c	8.2 (Berkeley) 07/11/93";
1154963Seric #endif /* not lint */
1254963Seric 
1354963Seric #include "sendmail.h"
1454963Seric 
1554963Seric /*
1654967Seric **  Mail Connection Information (MCI) Caching Module.
1754967Seric **
1854967Seric **	There are actually two separate things cached.  The first is
1954967Seric **	the set of all open connections -- these are stored in a
2054967Seric **	(small) list.  The second is stored in the symbol table; it
2154967Seric **	has the overall status for all hosts, whether or not there
2254967Seric **	is a connection open currently.
2354967Seric **
2454967Seric **	There should never be too many connections open (since this
2554967Seric **	could flood the socket table), nor should a connection be
2654967Seric **	allowed to sit idly for too long.
2754967Seric **
2854967Seric **	MaxMciCache is the maximum number of open connections that
2954967Seric **	will be supported.
3054967Seric **
3154967Seric **	MciCacheTimeout is the time (in seconds) that a connection
3254967Seric **	is permitted to survive without activity.
3354967Seric **
3454967Seric **	We actually try any cached connections by sending a NOOP
3554967Seric **	before we use them; if the NOOP fails we close down the
3654967Seric **	connection and reopen it.  Note that this means that a
3754967Seric **	server SMTP that doesn't support NOOP will hose the
3854967Seric **	algorithm -- but that doesn't seem too likely.
3954967Seric */
4054967Seric 
4154967Seric MCI	**MciCache;		/* the open connection cache */
4254967Seric /*
4354963Seric **  MCI_CACHE -- enter a connection structure into the open connection cache
4454963Seric **
4554963Seric **	This may cause something else to be flushed.
4654963Seric **
4754963Seric **	Parameters:
4854963Seric **		mci -- the connection to cache.
4954963Seric **
5054963Seric **	Returns:
5154963Seric **		none.
5254963Seric */
5354963Seric 
5454963Seric mci_cache(mci)
5554967Seric 	register MCI *mci;
5654963Seric {
5754967Seric 	register MCI **mcislot;
5854967Seric 	extern MCI **mci_scan();
5954963Seric 
6054963Seric 	if (MaxMciCache <= 0)
6154963Seric 	{
6254963Seric 		/* we don't support caching */
6354963Seric 		return;
6454963Seric 	}
6554963Seric 
6654963Seric 	/*
6754963Seric 	**  Find the best slot.  This may cause expired connections
6854963Seric 	**  to be closed.
6954963Seric 	*/
7054963Seric 
7154963Seric 	mcislot = mci_scan(mci);
7254963Seric 
7354963Seric 	/* if this is already cached, we are done */
7454963Seric 	if (bitset(MCIF_CACHED, mci->mci_flags))
7554963Seric 		return;
7654963Seric 
7754963Seric 	/* otherwise we may have to clear the slot */
7854963Seric 	if (*mcislot != NULL)
7958082Seric 		mci_uncache(mcislot, TRUE);
8054963Seric 
8154963Seric 	*mcislot = mci;
8254963Seric 	mci->mci_flags |= MCIF_CACHED;
8354963Seric }
8454963Seric /*
8554963Seric **  MCI_SCAN -- scan the cache, flush junk, and return best slot
8654963Seric **
8754963Seric **	Parameters:
8854963Seric **		savemci -- never flush this one.  Can be null.
8954963Seric **
9054963Seric **	Returns:
9154963Seric **		The LRU (or empty) slot.
9254963Seric */
9354963Seric 
9454967Seric MCI **
9554963Seric mci_scan(savemci)
9654967Seric 	MCI *savemci;
9754963Seric {
9854963Seric 	time_t now;
9954967Seric 	register MCI **bestmci;
10054967Seric 	register MCI *mci;
10154963Seric 	register int i;
10254963Seric 
10354963Seric 	if (MciCache == NULL)
10454963Seric 	{
10554963Seric 		/* first call */
10654967Seric 		MciCache = (MCI **) xalloc(MaxMciCache * sizeof *MciCache);
10756215Seric 		bzero((char *) MciCache, MaxMciCache * sizeof *MciCache);
10854963Seric 		return (&MciCache[0]);
10954963Seric 	}
11054963Seric 
11154963Seric 	now = curtime();
11254963Seric 	bestmci = &MciCache[0];
11354963Seric 	for (i = 0; i < MaxMciCache; i++)
11454963Seric 	{
11554963Seric 		mci = MciCache[i];
11654963Seric 		if (mci == NULL || mci->mci_state == MCIS_CLOSED)
11754963Seric 		{
11854963Seric 			bestmci = &MciCache[i];
11954963Seric 			continue;
12054963Seric 		}
12154963Seric 		if (mci->mci_lastuse + MciCacheTimeout < now && mci != savemci)
12254963Seric 		{
12354963Seric 			/* connection idle too long -- close it */
12454963Seric 			bestmci = &MciCache[i];
12558082Seric 			mci_uncache(bestmci, TRUE);
12654963Seric 			continue;
12754963Seric 		}
12854963Seric 		if (*bestmci == NULL)
12954963Seric 			continue;
13054963Seric 		if (mci->mci_lastuse < (*bestmci)->mci_lastuse)
13154963Seric 			bestmci = &MciCache[i];
13254963Seric 	}
13354963Seric 	return bestmci;
13454963Seric }
13554963Seric /*
13654963Seric **  MCI_UNCACHE -- remove a connection from a slot.
13754963Seric **
13854963Seric **	May close a connection.
13954963Seric **
14054963Seric **	Parameters:
14154963Seric **		mcislot -- the slot to empty.
14258082Seric **		doquit -- if TRUE, send QUIT protocol on this connection.
14358082Seric **			  if FALSE, we are assumed to be in a forked child;
14458082Seric **				all we want to do is close the file(s).
14554963Seric **
14654963Seric **	Returns:
14754963Seric **		none.
14854963Seric */
14954963Seric 
15058082Seric mci_uncache(mcislot, doquit)
15154967Seric 	register MCI **mcislot;
15258082Seric 	bool doquit;
15354963Seric {
15454967Seric 	register MCI *mci;
15555020Seric 	extern ENVELOPE BlankEnvelope;
15654963Seric 
15754963Seric 	mci = *mcislot;
15854963Seric 	if (mci == NULL)
15954963Seric 		return;
16054963Seric 	*mcislot = NULL;
16154963Seric 
16258082Seric 	if (doquit)
16358082Seric 	{
16458151Seric 		message("Closing connection to %s", mci->mci_host);
16555467Seric 
16658082Seric 		mci->mci_flags &= ~MCIF_CACHED;
16758082Seric 
16858082Seric 		/* only uses the envelope to flush the transcript file */
16958082Seric 		if (mci->mci_state != MCIS_CLOSED)
17058082Seric 			smtpquit(mci->mci_mailer, mci, &BlankEnvelope);
17159156Seric #ifdef XLA
17259156Seric 		xla_host_end(mci->mci_host);
17359156Seric #endif
17458082Seric 	}
17558082Seric 	else
17658082Seric 	{
17758082Seric 		if (mci->mci_in != NULL)
17858680Seric 			xfclose(mci->mci_in, "mci_uncache", "mci_in");
17958082Seric 		if (mci->mci_out != NULL)
18058680Seric 			xfclose(mci->mci_out, "mci_uncache", "mci_out");
18158082Seric 		mci->mci_in = mci->mci_out = NULL;
18258082Seric 		mci->mci_state = MCIS_CLOSED;
18358082Seric 		mci->mci_exitstat = EX_OK;
18458082Seric 		mci->mci_errno = 0;
18558082Seric 		mci->mci_flags = 0;
18658082Seric 	}
18754963Seric }
18854963Seric /*
18954963Seric **  MCI_FLUSH -- flush the entire cache
19058082Seric **
19158082Seric **	Parameters:
19258082Seric **		doquit -- if TRUE, send QUIT protocol.
19358082Seric **			  if FALSE, just close the connection.
19458082Seric **		allbut -- but leave this one open.
19558082Seric **
19658082Seric **	Returns:
19758082Seric **		none.
19854963Seric */
19954963Seric 
20058082Seric mci_flush(doquit, allbut)
20158082Seric 	bool doquit;
20258082Seric 	MCI *allbut;
20354963Seric {
20454963Seric 	register int i;
20554963Seric 
20654963Seric 	if (MciCache == NULL)
20754963Seric 		return;
20854963Seric 
20954963Seric 	for (i = 0; i < MaxMciCache; i++)
21058082Seric 		if (allbut != MciCache[i])
21158082Seric 			mci_uncache(&MciCache[i], doquit);
21254963Seric }
21354963Seric /*
21454963Seric **  MCI_GET -- get information about a particular host
21554963Seric */
21654963Seric 
21754967Seric MCI *
21854963Seric mci_get(host, m)
21954963Seric 	char *host;
22054963Seric 	MAILER *m;
22154963Seric {
22254967Seric 	register MCI *mci;
22355467Seric 	register STAB *s;
22454967Seric 
22558907Seric #ifdef DAEMON
22658907Seric 	extern SOCKADDR CurHostAddr;
22758907Seric 
22858907Seric 	/* clear CurHostAddr so we don't get a bogus address with this name */
22958907Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
23058907Seric #endif DAEMON
23158907Seric 
23255467Seric 	s = stab(host, ST_MCI + m->m_mno, ST_ENTER);
23355467Seric 	mci = &s->s_mci;
23455467Seric 	mci->mci_host = s->s_name;
23554967Seric 
23654967Seric 	if (tTd(42, 2))
23754967Seric 	{
23854967Seric 		printf("mci_get(%s %s): mci_state=%d, _flags=%x, _exitstat=%d, _errno=%d\n",
23954967Seric 			host, m->m_name, mci->mci_state, mci->mci_flags,
24054967Seric 			mci->mci_exitstat, mci->mci_errno);
24154967Seric 	}
24254967Seric 
24357977Seric 	if (mci->mci_state == MCIS_OPEN)
24454967Seric 	{
24557977Seric 		/* poke the connection to see if it's still alive */
24658867Seric 		smtpprobe(mci);
24757977Seric 
24857977Seric 		/* reset the stored state in the event of a timeout */
24957977Seric 		if (mci->mci_state != MCIS_OPEN)
25057977Seric 		{
25157977Seric 			mci->mci_errno = 0;
25257977Seric 			mci->mci_exitstat = EX_OK;
25357977Seric 			mci->mci_state = MCIS_CLOSED;
25457977Seric 		}
25554967Seric 	}
25654967Seric 
25754967Seric 	return mci;
25854963Seric }
25957380Seric /*
26057380Seric **  MCI_DUMP -- dump the contents of an MCI structure.
26157380Seric **
26257380Seric **	Parameters:
26357380Seric **		mci -- the MCI structure to dump.
26457380Seric **
26557380Seric **	Returns:
26657380Seric **		none.
26757380Seric **
26857380Seric **	Side Effects:
26957380Seric **		none.
27057380Seric */
27157380Seric 
27257380Seric mci_dump(mci)
27357380Seric 	register MCI *mci;
27457380Seric {
27557380Seric 	extern char *ctime();
27657380Seric 
27757380Seric 	printf("MCI@%x: ", mci);
27857380Seric 	if (mci == NULL)
27957380Seric 	{
28057380Seric 		printf("NULL\n");
28157380Seric 		return;
28257380Seric 	}
283*63753Seric 	printf("flags=%o, errno=%d, herrno=%d, exitstat=%d, state=%d, pid=%d,\n",
284*63753Seric 		mci->mci_flags, mci->mci_errno, mci->mci_herrno,
285*63753Seric 		mci->mci_exitstat, mci->mci_state, mci->mci_pid);
286*63753Seric 	printf("\tmaxsize=%ld, phase=%s, mailer=%s,\n",
287*63753Seric 		mci->mci_maxsize,
28857380Seric 		mci->mci_phase == NULL ? "NULL" : mci->mci_phase,
28957380Seric 		mci->mci_mailer == NULL ? "NULL" : mci->mci_mailer->m_name);
29057380Seric 	printf("\thost=%s, lastuse=%s\n",
29157380Seric 		mci->mci_host == NULL ? "NULL" : mci->mci_host,
29257380Seric 		ctime(&mci->mci_lastuse));
29357380Seric }
294