xref: /csrg-svn/usr.sbin/sendmail/src/mci.c (revision 64739)
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*64739Seric static char sccsid[] = "@(#)mci.c	8.5 (Berkeley) 10/21/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 
81*64739Seric 	if (tTd(42, 5))
82*64739Seric 		printf("mci_cache: caching %x (%s) in slot %d\n",
83*64739Seric 			mci, mci->mci_host, mcislot - MciCache);
84*64739Seric #ifdef LOG
85*64739Seric 	if (tTd(91, 100))
86*64739Seric 		syslog(LOG_DEBUG, "%s: mci_cache: caching %x (%s) in slot %d",
87*64739Seric 			CurEnv->e_id, mci, mci->mci_host, mcislot - MciCache);
88*64739Seric #endif
89*64739Seric 
9054963Seric 	*mcislot = mci;
9154963Seric 	mci->mci_flags |= MCIF_CACHED;
9254963Seric }
9354963Seric /*
9454963Seric **  MCI_SCAN -- scan the cache, flush junk, and return best slot
9554963Seric **
9654963Seric **	Parameters:
9754963Seric **		savemci -- never flush this one.  Can be null.
9854963Seric **
9954963Seric **	Returns:
10054963Seric **		The LRU (or empty) slot.
10154963Seric */
10254963Seric 
10354967Seric MCI **
10454963Seric mci_scan(savemci)
10554967Seric 	MCI *savemci;
10654963Seric {
10754963Seric 	time_t now;
10854967Seric 	register MCI **bestmci;
10954967Seric 	register MCI *mci;
11054963Seric 	register int i;
11154963Seric 
11254963Seric 	if (MciCache == NULL)
11354963Seric 	{
11454963Seric 		/* first call */
11554967Seric 		MciCache = (MCI **) xalloc(MaxMciCache * sizeof *MciCache);
11656215Seric 		bzero((char *) MciCache, MaxMciCache * sizeof *MciCache);
11754963Seric 		return (&MciCache[0]);
11854963Seric 	}
11954963Seric 
12054963Seric 	now = curtime();
12154963Seric 	bestmci = &MciCache[0];
12254963Seric 	for (i = 0; i < MaxMciCache; i++)
12354963Seric 	{
12454963Seric 		mci = MciCache[i];
12554963Seric 		if (mci == NULL || mci->mci_state == MCIS_CLOSED)
12654963Seric 		{
12754963Seric 			bestmci = &MciCache[i];
12854963Seric 			continue;
12954963Seric 		}
13054963Seric 		if (mci->mci_lastuse + MciCacheTimeout < now && mci != savemci)
13154963Seric 		{
13254963Seric 			/* connection idle too long -- close it */
13354963Seric 			bestmci = &MciCache[i];
13458082Seric 			mci_uncache(bestmci, TRUE);
13554963Seric 			continue;
13654963Seric 		}
13754963Seric 		if (*bestmci == NULL)
13854963Seric 			continue;
13954963Seric 		if (mci->mci_lastuse < (*bestmci)->mci_lastuse)
14054963Seric 			bestmci = &MciCache[i];
14154963Seric 	}
14254963Seric 	return bestmci;
14354963Seric }
14454963Seric /*
14554963Seric **  MCI_UNCACHE -- remove a connection from a slot.
14654963Seric **
14754963Seric **	May close a connection.
14854963Seric **
14954963Seric **	Parameters:
15054963Seric **		mcislot -- the slot to empty.
15158082Seric **		doquit -- if TRUE, send QUIT protocol on this connection.
15258082Seric **			  if FALSE, we are assumed to be in a forked child;
15358082Seric **				all we want to do is close the file(s).
15454963Seric **
15554963Seric **	Returns:
15654963Seric **		none.
15754963Seric */
15854963Seric 
15958082Seric mci_uncache(mcislot, doquit)
16054967Seric 	register MCI **mcislot;
16158082Seric 	bool doquit;
16254963Seric {
16354967Seric 	register MCI *mci;
16455020Seric 	extern ENVELOPE BlankEnvelope;
16554963Seric 
16654963Seric 	mci = *mcislot;
16754963Seric 	if (mci == NULL)
16854963Seric 		return;
16954963Seric 	*mcislot = NULL;
17054963Seric 
171*64739Seric 	if (tTd(42, 5))
172*64739Seric 		printf("mci_uncache: uncaching %x (%s) from slot %d (%d)\n",
173*64739Seric 			mci, mci->mci_host, mcislot - MciCache, doquit);
174*64739Seric #ifdef LOG
175*64739Seric 	if (tTd(91, 100))
176*64739Seric 		syslog(LOG_DEBUG, "%s: mci_uncache: uncaching %x (%s) from slot %d (%d)",
177*64739Seric 			CurEnv->e_id, mci, mci->mci_host, mcislot - MciCache, doquit);
178*64739Seric #endif
179*64739Seric 
18058082Seric 	if (doquit)
18158082Seric 	{
18258151Seric 		message("Closing connection to %s", mci->mci_host);
18355467Seric 
18458082Seric 		mci->mci_flags &= ~MCIF_CACHED;
18558082Seric 
18658082Seric 		/* only uses the envelope to flush the transcript file */
18758082Seric 		if (mci->mci_state != MCIS_CLOSED)
18858082Seric 			smtpquit(mci->mci_mailer, mci, &BlankEnvelope);
18959156Seric #ifdef XLA
19059156Seric 		xla_host_end(mci->mci_host);
19159156Seric #endif
19258082Seric 	}
19358082Seric 	else
19458082Seric 	{
19558082Seric 		if (mci->mci_in != NULL)
19658680Seric 			xfclose(mci->mci_in, "mci_uncache", "mci_in");
19758082Seric 		if (mci->mci_out != NULL)
19858680Seric 			xfclose(mci->mci_out, "mci_uncache", "mci_out");
19958082Seric 		mci->mci_in = mci->mci_out = NULL;
20058082Seric 		mci->mci_state = MCIS_CLOSED;
20158082Seric 		mci->mci_exitstat = EX_OK;
20258082Seric 		mci->mci_errno = 0;
20358082Seric 		mci->mci_flags = 0;
20458082Seric 	}
20554963Seric }
20654963Seric /*
20754963Seric **  MCI_FLUSH -- flush the entire cache
20858082Seric **
20958082Seric **	Parameters:
21058082Seric **		doquit -- if TRUE, send QUIT protocol.
21158082Seric **			  if FALSE, just close the connection.
21258082Seric **		allbut -- but leave this one open.
21358082Seric **
21458082Seric **	Returns:
21558082Seric **		none.
21654963Seric */
21754963Seric 
21858082Seric mci_flush(doquit, allbut)
21958082Seric 	bool doquit;
22058082Seric 	MCI *allbut;
22154963Seric {
22254963Seric 	register int i;
22354963Seric 
22454963Seric 	if (MciCache == NULL)
22554963Seric 		return;
22654963Seric 
22754963Seric 	for (i = 0; i < MaxMciCache; i++)
22858082Seric 		if (allbut != MciCache[i])
22958082Seric 			mci_uncache(&MciCache[i], doquit);
23054963Seric }
23154963Seric /*
23254963Seric **  MCI_GET -- get information about a particular host
23354963Seric */
23454963Seric 
23554967Seric MCI *
23654963Seric mci_get(host, m)
23754963Seric 	char *host;
23854963Seric 	MAILER *m;
23954963Seric {
24054967Seric 	register MCI *mci;
24155467Seric 	register STAB *s;
24254967Seric 
24358907Seric #ifdef DAEMON
24458907Seric 	extern SOCKADDR CurHostAddr;
24558907Seric 
24658907Seric 	/* clear CurHostAddr so we don't get a bogus address with this name */
24758907Seric 	bzero(&CurHostAddr, sizeof CurHostAddr);
24863937Seric #endif
24958907Seric 
25055467Seric 	s = stab(host, ST_MCI + m->m_mno, ST_ENTER);
25155467Seric 	mci = &s->s_mci;
25255467Seric 	mci->mci_host = s->s_name;
25354967Seric 
25454967Seric 	if (tTd(42, 2))
25554967Seric 	{
25654967Seric 		printf("mci_get(%s %s): mci_state=%d, _flags=%x, _exitstat=%d, _errno=%d\n",
25754967Seric 			host, m->m_name, mci->mci_state, mci->mci_flags,
25854967Seric 			mci->mci_exitstat, mci->mci_errno);
25954967Seric 	}
26054967Seric 
26157977Seric 	if (mci->mci_state == MCIS_OPEN)
26254967Seric 	{
26357977Seric 		/* poke the connection to see if it's still alive */
26458867Seric 		smtpprobe(mci);
26557977Seric 
26657977Seric 		/* reset the stored state in the event of a timeout */
26757977Seric 		if (mci->mci_state != MCIS_OPEN)
26857977Seric 		{
26957977Seric 			mci->mci_errno = 0;
27057977Seric 			mci->mci_exitstat = EX_OK;
27157977Seric 			mci->mci_state = MCIS_CLOSED;
27257977Seric 		}
27354967Seric 	}
27454967Seric 
27554967Seric 	return mci;
27654963Seric }
27757380Seric /*
27857380Seric **  MCI_DUMP -- dump the contents of an MCI structure.
27957380Seric **
28057380Seric **	Parameters:
28157380Seric **		mci -- the MCI structure to dump.
28257380Seric **
28357380Seric **	Returns:
28457380Seric **		none.
28557380Seric **
28657380Seric **	Side Effects:
28757380Seric **		none.
28857380Seric */
28957380Seric 
29064731Seric mci_dump(mci, logit)
29157380Seric 	register MCI *mci;
29264731Seric 	bool logit;
29357380Seric {
29464731Seric 	register char *p;
29564731Seric 	char *sep;
29664731Seric 	char buf[1000];
29757380Seric 	extern char *ctime();
29857380Seric 
29964731Seric 	sep = logit ? " " : "\n\t";
30064731Seric 	p = buf;
30164731Seric 	sprintf(p, "MCI@%x: ", mci);
30264731Seric 	p += strlen(p);
30357380Seric 	if (mci == NULL)
30457380Seric 	{
30564731Seric 		sprintf(p, "NULL");
30664731Seric 		goto printit;
30757380Seric 	}
30864731Seric 	sprintf(p, "flags=%o, errno=%d, herrno=%d, exitstat=%d, state=%d, pid=%d,%s",
30963753Seric 		mci->mci_flags, mci->mci_errno, mci->mci_herrno,
31064731Seric 		mci->mci_exitstat, mci->mci_state, mci->mci_pid, sep);
31164731Seric 	p += strlen(p);
31264731Seric 	sprintf(p, "maxsize=%ld, phase=%s, mailer=%s,%s",
31363753Seric 		mci->mci_maxsize,
31457380Seric 		mci->mci_phase == NULL ? "NULL" : mci->mci_phase,
31564731Seric 		mci->mci_mailer == NULL ? "NULL" : mci->mci_mailer->m_name,
31664731Seric 		sep);
31764731Seric 	p += strlen(p);
31864731Seric 	sprintf(p, "host=%s, lastuse=%s",
31957380Seric 		mci->mci_host == NULL ? "NULL" : mci->mci_host,
32057380Seric 		ctime(&mci->mci_lastuse));
32164731Seric printit:
32264731Seric 	if (logit)
32364731Seric 		syslog(LOG_INFO, "%s", buf);
32464731Seric 	else
32564731Seric 		printf("%s\n", buf);
32657380Seric }
32764731Seric /*
32864731Seric **  MCI_DUMP_ALL -- print the entire MCI cache
32964731Seric **
33064731Seric **	Parameters:
33164731Seric **		logit -- if set, log the result instead of printing
33264731Seric **			to stdout.
33364731Seric **
33464731Seric **	Returns:
33564731Seric **		none.
33664731Seric */
33764731Seric 
33864731Seric mci_dump_all(logit)
33964731Seric 	bool logit;
34064731Seric {
34164731Seric 	register int i;
34264731Seric 
34364731Seric 	for (i = 0; i < MaxMciCache; i++)
34464731Seric 		mci_dump(MciCache[i], logit);
34564731Seric }
346