DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - metrics - download
Index: T d

⟦b840f6ce9⟧ TextFile

    Length: 33689 (0x8399)
    Types: TextFile
    Names: »daemon.c,v«

Derivation

└─⟦9ae75bfbd⟧ Bits:30007242 EUUGD3: Starter Kit
    └─⟦bfebc70e2⟧ »EurOpenD3/mail/sendmail-5.65b+IDA-1.4.3.tar.Z« 
        └─⟦f9e35cd84⟧ 
            └─⟦this⟧ »sendmail/src/RCS/daemon.c,v« 

TextFile

head	5.36;
branch	5.36.0;
access;
symbols
	UICSO:5.36.0
	VANILLA:5.36;
locks; strict;
comment	@ * @;


5.36
date	90.06.20.08.35.37;	author paul;	state Exp;
branches
	5.36.0.1;
next	;

5.36.0.1
date	90.06.20.09.42.45;	author paul;	state Exp;
branches;
next	5.36.0.2;

5.36.0.2
date	90.06.25.09.20.37;	author paul;	state Exp;
branches;
next	5.36.0.3;

5.36.0.3
date	90.07.06.15.23.50;	author paul;	state Exp;
branches;
next	5.36.0.4;

5.36.0.4
date	90.07.08.17.29.09;	author paul;	state Exp;
branches;
next	5.36.0.5;

5.36.0.5
date	90.09.17.09.31.43;	author paul;	state Exp;
branches;
next	5.36.0.6;

5.36.0.6
date	90.10.02.00.47.14;	author paul;	state Exp;
branches;
next	5.36.0.7;

5.36.0.7
date	90.10.16.14.48.07;	author paul;	state Exp;
branches;
next	5.36.0.8;

5.36.0.8
date	90.10.18.10.43.14;	author paul;	state Exp;
branches;
next	5.36.0.9;

5.36.0.9
date	90.11.14.14.37.56;	author paul;	state Exp;
branches;
next	5.36.0.10;

5.36.0.10
date	90.11.28.17.21.46;	author paul;	state Exp;
branches;
next	5.36.0.11;

5.36.0.11
date	90.11.29.21.17.08;	author paul;	state Exp;
branches;
next	5.36.0.12;

5.36.0.12
date	91.01.19.19.26.02;	author paul;	state Exp;
branches;
next	5.36.0.13;

5.36.0.13
date	91.02.17.02.42.12;	author paul;	state Exp;
branches;
next	5.36.0.14;

5.36.0.14
date	91.03.04.21.48.23;	author paul;	state Exp;
branches;
next	5.36.0.15;

5.36.0.15
date	91.03.06.15.10.33;	author paul;	state Exp;
branches;
next	5.36.0.16;

5.36.0.16
date	91.03.06.16.08.39;	author paul;	state Exp;
branches;
next	;


desc
@@


5.36
log
@5.64 Berkeley release
@
text
@/*
 * Copyright (c) 1983 Eric P. Allman
 * Copyright (c) 1988 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted provided
 * that: (1) source distributions retain this entire copyright notice and
 * comment, and (2) distributions including binaries display the following
 * acknowledgement:  ``This product includes software developed by the
 * University of California, Berkeley and its contributors'' in the
 * documentation or other materials provided with the distribution and in
 * all advertising materials mentioning features or use of this software.
 * Neither the name of the University nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#include <errno.h>
#include "sendmail.h"

#ifndef lint
#ifdef DAEMON
static char sccsid[] = "@@(#)daemon.c	5.36 (Berkeley) 6/1/90 (with daemon mode)";
#else
static char sccsid[] = "@@(#)daemon.c	5.36 (Berkeley) 6/1/90 (without daemon mode)";
#endif
#endif /* not lint */

int la;	/* load average */

#ifdef DAEMON

# include <netdb.h>
# include <sys/signal.h>
# include <sys/wait.h>
# include <sys/time.h>
# include <sys/resource.h>

/*
**  DAEMON.C -- routines to use when running as a daemon.
**
**	This entire file is highly dependent on the 4.2 BSD
**	interprocess communication primitives.  No attempt has
**	been made to make this file portable to Version 7,
**	Version 6, MPX files, etc.  If you should try such a
**	thing yourself, I recommend chucking the entire file
**	and starting from scratch.  Basic semantics are:
**
**	getrequests()
**		Opens a port and initiates a connection.
**		Returns in a child.  Must set InChannel and
**		OutChannel appropriately.
**	clrdaemon()
**		Close any open files associated with getting
**		the connection; this is used when running the queue,
**		etc., to avoid having extra file descriptors during
**		the queue run and to avoid confusing the network
**		code (if it cares).
**	makeconnection(host, port, outfile, infile)
**		Make a connection to the named host on the given
**		port.  Set *outfile and *infile to the files
**		appropriate for communication.  Returns zero on
**		success, else an exit status describing the
**		error.
**	maphostname(hbuf, hbufsize)
**		Convert the entry in hbuf into a canonical form.  It
**		may not be larger than hbufsize.
*/
\f

/*
**  GETREQUESTS -- open mail IPC port and get requests.
**
**	Parameters:
**		none.
**
**	Returns:
**		none.
**
**	Side Effects:
**		Waits until some interesting activity occurs.  When
**		it does, a child is created to process it, and the
**		parent waits for completion.  Return from this
**		routine is always in the child.  The file pointers
**		"InChannel" and "OutChannel" should be set to point
**		to the communication channel.
*/

struct sockaddr_in	SendmailAddress;/* internet address of sendmail */

int	DaemonSocket	= -1;		/* fd describing socket */
char	*NetName;			/* name of home (local?) network */

getrequests()
{
	int t;
	register struct servent *sp;
	int on = 1;
	extern reapchild();

	/*
	**  Set up the address for the mailer.
	*/

	sp = getservbyname("smtp", "tcp");
	if (sp == NULL)
	{
		syserr("server \"smtp\" unknown");
		goto severe;
	}
	SendmailAddress.sin_family = AF_INET;
	SendmailAddress.sin_addr.s_addr = INADDR_ANY;
	SendmailAddress.sin_port = sp->s_port;

	/*
	**  Try to actually open the connection.
	*/

	if (tTd(15, 1))
		printf("getrequests: port 0x%x\n", SendmailAddress.sin_port);

	/* get a socket for the SMTP connection */
	DaemonSocket = socket(AF_INET, SOCK_STREAM, 0);
	if (DaemonSocket < 0)
	{
		/* probably another daemon already */
		syserr("getrequests: can't create socket");
	  severe:
# ifdef LOG
		if (LogLevel > 0)
			syslog(LOG_ALERT, "cannot get connection");
# endif LOG
		finis();
	}

	/* turn on network debugging? */
	if (tTd(15, 15))
		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);

	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof on);
	(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof on);

	if (bind(DaemonSocket, &SendmailAddress, sizeof SendmailAddress) < 0)
	{
		syserr("getrequests: cannot bind");
		(void) close(DaemonSocket);
		goto severe;
	}
	if (listen(DaemonSocket, 10) < 0)
	{
		syserr("getrequests: cannot listen");
		(void) close(DaemonSocket);
		goto severe;
	}

	(void) signal(SIGCHLD, reapchild);

	if (tTd(15, 1))
		printf("getrequests: %d\n", DaemonSocket);

	for (;;)
	{
		register int pid;
		auto int lotherend;
		extern int RefuseLA;

		/* see if we are rejecting connections */
		while ((la = getla()) > RefuseLA)
		{
			setproctitle("rejecting connections: load average: %.2f", (double)la);
			sleep(5);
		}

		/* wait for a connection */
		setproctitle("accepting connections");
		do
		{
			errno = 0;
			lotherend = sizeof RealHostAddr;
			t = accept(DaemonSocket, &RealHostAddr, &lotherend);
		} while (t < 0 && errno == EINTR);
		if (t < 0)
		{
			syserr("getrequests: accept");
			sleep(5);
			continue;
		}

		/*
		**  Create a subprocess to process the mail.
		*/

		if (tTd(15, 2))
			printf("getrequests: forking (fd = %d)\n", t);

		pid = fork();
		if (pid < 0)
		{
			syserr("daemon: cannot fork");
			sleep(10);
			(void) close(t);
			continue;
		}

		if (pid == 0)
		{
			extern struct hostent *gethostbyaddr();
			register struct hostent *hp;
			char buf[MAXNAME];

			/*
			**  CHILD -- return to caller.
			**	Collect verified idea of sending host.
			**	Verify calling user id if possible here.
			*/

			(void) signal(SIGCHLD, SIG_DFL);

			/* determine host name */
			hp = gethostbyaddr((char *) &RealHostAddr.sin_addr, sizeof RealHostAddr.sin_addr, AF_INET);
			if (hp != NULL)
				(void) strcpy(buf, hp->h_name);
			else
			{
				extern char *inet_ntoa();

				/* produce a dotted quad */
				(void) sprintf(buf, "[%s]",
					inet_ntoa(RealHostAddr.sin_addr));
			}

			/* should we check for illegal connection here? XXX */

			RealHostName = newstr(buf);

			(void) close(DaemonSocket);
			InChannel = fdopen(t, "r");
			OutChannel = fdopen(dup(t), "w");
			if (tTd(15, 2))
				printf("getreq: returning\n");
# ifdef LOG
			if (LogLevel > 11)
				syslog(LOG_DEBUG, "connected, pid=%d", getpid());
# endif LOG
			return;
		}

		/* close the port so that others will hang (for a while) */
		(void) close(t);
	}
	/*NOTREACHED*/
}
\f

/*
**  CLRDAEMON -- reset the daemon connection
**
**	Parameters:
**		none.
**
**	Returns:
**		none.
**
**	Side Effects:
**		releases any resources used by the passive daemon.
*/

clrdaemon()
{
	if (DaemonSocket >= 0)
		(void) close(DaemonSocket);
	DaemonSocket = -1;
}
\f

/*
**  MAKECONNECTION -- make a connection to an SMTP socket on another machine.
**
**	Parameters:
**		host -- the name of the host.
**		port -- the port number to connect to.
**		outfile -- a pointer to a place to put the outfile
**			descriptor.
**		infile -- ditto for infile.
**
**	Returns:
**		An exit code telling whether the connection could be
**			made and if not why not.
**
**	Side Effects:
**		none.
*/

makeconnection(host, port, outfile, infile)
	char *host;
	u_short port;
	FILE **outfile;
	FILE **infile;
{
	register int i, s;
	register struct hostent *hp = (struct hostent *)NULL;
	extern char *inet_ntoa();
	int sav_errno;
#ifdef NAMED_BIND
	extern int h_errno;
#endif

	/*
	**  Set up the address for the mailer.
	**	Accept "[a.b.c.d]" syntax for host name.
	*/

#ifdef NAMED_BIND
	h_errno = 0;
#endif
	errno = 0;

	if (host[0] == '[')
	{
		long hid;
		register char *p = index(host, ']');

		if (p != NULL)
		{
			*p = '\0';
			hid = inet_addr(&host[1]);
			*p = ']';
		}
		if (p == NULL || hid == -1)
		{
			usrerr("Invalid numeric domain spec \"%s\"", host);
			return (EX_NOHOST);
		}
		SendmailAddress.sin_addr.s_addr = hid;
	}
	else
	{
		hp = gethostbyname(host);
		if (hp == NULL)
		{
#ifdef NAMED_BIND
			if (errno == ETIMEDOUT || h_errno == TRY_AGAIN)
				return (EX_TEMPFAIL);

			/* if name server is specified, assume temp fail */
			if (errno == ECONNREFUSED && UseNameServer)
				return (EX_TEMPFAIL);
#endif

			/*
			**  XXX Should look for mail forwarder record here
			**  XXX if (h_errno == NO_ADDRESS).
			*/

			return (EX_NOHOST);
		}
		bcopy(hp->h_addr, (char *) &SendmailAddress.sin_addr, hp->h_length);
		i = 1;
	}

	/*
	**  Determine the port number.
	*/

	if (port != 0)
		SendmailAddress.sin_port = htons(port);
	else
	{
		register struct servent *sp = getservbyname("smtp", "tcp");

		if (sp == NULL)
		{
			syserr("makeconnection: server \"smtp\" unknown");
			return (EX_OSFILE);
		}
		SendmailAddress.sin_port = sp->s_port;
	}

	/*
	**  Try to actually open the connection.
	*/

again:
	if (tTd(16, 1))
		printf("makeconnection (%s [%s])\n", host,
		    inet_ntoa(SendmailAddress.sin_addr.s_addr));

	s = socket(AF_INET, SOCK_STREAM, 0);
	if (s < 0)
	{
		syserr("makeconnection: no socket");
		sav_errno = errno;
		goto failure;
	}

	if (tTd(16, 1))
		printf("makeconnection: %d\n", s);

	/* turn on network debugging? */
	if (tTd(16, 14))
	{
		int on = 1;
		(void) setsockopt(DaemonSocket, SOL_SOCKET, SO_DEBUG, (char *)&on, sizeof on);
	}
	if (CurEnv->e_xfp != NULL)
		(void) fflush(CurEnv->e_xfp);		/* for debugging */
	errno = 0;					/* for debugging */
	SendmailAddress.sin_family = AF_INET;
	if (connect(s, &SendmailAddress, sizeof SendmailAddress) < 0)
	{
		sav_errno = errno;
		(void) close(s);
		if (hp && hp->h_addr_list[i])
		{
			bcopy(hp->h_addr_list[i++],
			    (char *)&SendmailAddress.sin_addr, hp->h_length);
			goto again;
		}

		/* failure, decide if temporary or not */
	failure:
		switch (sav_errno)
		{
		  case EISCONN:
		  case ETIMEDOUT:
		  case EINPROGRESS:
		  case EALREADY:
		  case EADDRINUSE:
		  case EHOSTDOWN:
		  case ENETDOWN:
		  case ENETRESET:
		  case ENOBUFS:
		  case ECONNREFUSED:
		  case ECONNRESET:
		  case EHOSTUNREACH:
		  case ENETUNREACH:
			/* there are others, I'm sure..... */
			return (EX_TEMPFAIL);

		  case EPERM:
			/* why is this happening? */
			syserr("makeconnection: funny failure, addr=%lx, port=%x",
				SendmailAddress.sin_addr.s_addr, SendmailAddress.sin_port);
			return (EX_TEMPFAIL);

		  default:
			{
				extern char *errstring();

				message(Arpa_Info, "%s", errstring(sav_errno));
				return (EX_UNAVAILABLE);
			}
		}
	}

	/* connection ok, put it into canonical form */
	*outfile = fdopen(s, "w");
	*infile = fdopen(dup(s), "r");

	return (EX_OK);
}
\f

/*
**  MYHOSTNAME -- return the name of this host.
**
**	Parameters:
**		hostbuf -- a place to return the name of this host.
**		size -- the size of hostbuf.
**
**	Returns:
**		A list of aliases for this host.
**
**	Side Effects:
**		none.
*/

char **
myhostname(hostbuf, size)
	char hostbuf[];
	int size;
{
	extern struct hostent *gethostbyname();
	struct hostent *hp;

	if (gethostname(hostbuf, size) < 0)
	{
		(void) strcpy(hostbuf, "localhost");
	}
	hp = gethostbyname(hostbuf);
	if (hp != NULL)
	{
		(void) strcpy(hostbuf, hp->h_name);
		return (hp->h_aliases);
	}
	else
		return (NULL);
}

/*
 *  MAPHOSTNAME -- turn a hostname into canonical form
 *
 *	Parameters:
 *		hbuf -- a buffer containing a hostname.
 *		hbsize -- the size of hbuf.
 *
 *	Returns:
 *		none.
 *
 *	Side Effects:
 *		Looks up the host specified in hbuf.  If it is not
 *		the canonical name for that host, replace it with
 *		the canonical name.  If the name is unknown, or it
 *		is already the canonical name, leave it unchanged.
 */
maphostname(hbuf, hbsize)
	char *hbuf;
	int hbsize;
{
	register struct hostent *hp;
	u_long in_addr;
	char ptr[256], *cp;
	struct hostent *gethostbyaddr();

	/*
	 * If first character is a bracket, then it is an address
	 * lookup.  Address is copied into a temporary buffer to
	 * strip the brackets and to preserve hbuf if address is
	 * unknown.
	 */
	if (*hbuf != '[') {
		getcanonname(hbuf, hbsize);
		return;
	}
	if ((cp = index(strcpy(ptr, hbuf), ']')) == NULL)
		return;
	*cp = '\0';
	in_addr = inet_addr(&ptr[1]);
	hp = gethostbyaddr((char *)&in_addr, sizeof(struct in_addr), AF_INET);
	if (hp == NULL)
		return;
	if (strlen(hp->h_name) >= hbsize)
		hp->h_name[hbsize - 1] = '\0';
	(void)strcpy(hbuf, hp->h_name);
}

# else DAEMON
/* code for systems without sophisticated networking */

/*
**  MYHOSTNAME -- stub version for case of no daemon code.
**
**	Can't convert to upper case here because might be a UUCP name.
**
**	Mark, you can change this to be anything you want......
*/

char **
myhostname(hostbuf, size)
	char hostbuf[];
	int size;
{
	register FILE *f;

	hostbuf[0] = '\0';
	f = fopen("/usr/include/whoami", "r");
	if (f != NULL)
	{
		(void) fgets(hostbuf, size, f);
		fixcrlf(hostbuf, TRUE);
		(void) fclose(f);
	}
	return (NULL);
}
\f

/*
**  MAPHOSTNAME -- turn a hostname into canonical form
**
**	Parameters:
**		hbuf -- a buffer containing a hostname.
**		hbsize -- the size of hbuf.
**
**	Returns:
**		none.
**
**	Side Effects:
**		Looks up the host specified in hbuf.  If it is not
**		the canonical name for that host, replace it with
**		the canonical name.  If the name is unknown, or it
**		is already the canonical name, leave it unchanged.
*/

/*ARGSUSED*/
maphostname(hbuf, hbsize)
	char *hbuf;
	int hbsize;
{
	return;
}

#endif DAEMON
@


5.36.0.1
log
@IDA patches
@
text
@d22 1
a22 1
#include <sendmail.h>
a40 5
# include <sys/file.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <arpa/nameser.h>
# include <resolv.h>
a70 8
**
**	mapinit(c)
**		Open and initialize a dbm database.  Reopen if our current
**		file descriptor is out of date.
**
**	mapkey(c, key, argval, argsiz)
**		Search a database for a match to the given key, sprintf'ing
**		the argument through the result if found.
d133 1
a133 1
# endif /* LOG */
a161 4
#ifdef OUTPUT_PID
	(void) write_pid_to_file ();
#endif /* OUTPUT_PID */

d168 3
a170 5
		/*
		 * see if we are rejecting connections
		 */
#ifndef BUSYEXIT
		while ((la = getla()) > RefuseLA) {
d172 1
a172 1
			sleep (5);
a173 11
#else
		/*
		 * see if we are rejecting connections (but let srvrsmtp
		 * return a 421 Too busy to get rid of the request instead
		 * of stalling it).
		 */
		if ((la = getla()) > RefuseLA)
			setproctitle("Rejecting connections: load %d", la);
		else
#endif /* !BUSYEXIT */
			setproctitle("Waiting for connection");
d175 2
d245 1
a245 1
# endif /* LOG */
a305 15
	**  Don't do recursive domain searches.  An example why not:
	**  Machine cerl.cecer.army.mil has a UUCP connection to
	**  osiris.cso.uiuc.edu.  Also at UIUC is a machine called
	**  uinova.cerl.uiuc.edu that accepts mail for the its parent domain
	**  cerl.uiuc.edu.  Sending mail to cerl!user with recursion on
	**  will select cerl.uiuc.edu which maps to uinova.cerl.uiuc.edu.
	**  We leave RES_DEFNAMES on so single names in the current domain
	**  still work.
	**
	**  Paul Pomes, CSO, UIUC	17-Oct-88
	*/

	_res.options &= (~RES_DNSRCH & 0xffff);

	/*
d419 1
a419 2
		errno = sav_errno;
		switch (errno)
d496 15
a510 17
**  MAPHOSTNAME -- turn a hostname into canonical form
**
**	Parameters:
**		hbuf -- a buffer containing a hostname.
**		hbsize -- the size of hbuf.
**
**	Returns:
**		An exit code telling if the hostname was found and
**		canonicalized.
**
**	Side Effects:
**		Looks up the host specified in hbuf.  If it is not
**		the canonical name for that host, replace it with
**		the canonical name.  If the name is unknown, or it
**		is already the canonical name, leave it unchanged.
**/
bool
d516 3
a518 2
	extern struct hostent *gethostbyname();
	static char tmphbuf[MAXNAME];
d526 3
a528 9
	if (*hbuf == '[')
	{
		extern struct hostent *gethostbyaddr();
		u_long in_addr;

		(void) strncpy(tmphbuf, hbuf+1, strlen(hbuf)-2);
		tmphbuf[strlen(hbuf)-2]='\0';
		in_addr = inet_addr(tmphbuf);
		hp = gethostbyaddr((char *) &in_addr, sizeof(struct in_addr), AF_INET);
d530 5
a534 31
	else
	{
		register int ret;

		/*
		** See note in makeconnection() above for why we disable
		** recursive domain matching.  -pbp
		*/
		_res.options &= (~RES_DNSRCH & 0xffff);

		hp = gethostbyname(hbuf);
		if (hp == NULL) {
			/* try lowercase version */
			(void) strcpy(tmphbuf, hbuf);
			(void) makelower(tmphbuf);
			/* Could be just an MX record; look for anything */
			ret = getcanonname(tmphbuf,sizeof(tmphbuf));
			if (ret != TRUE) {
				if (tTd(9, 1))
					printf("maphostname(%s, %d) => %.*s\n",
						hbuf, hbsize, hbsize-1, 
						hp ? hp->h_name : "NOT_FOUND");
				return FALSE;
			}
			strcpy(hbuf,tmphbuf);
			return TRUE;
		}
	}
	if (tTd(9, 1))
		printf("maphostname(%s, %d) => %.*s\n",
			hbuf, hbsize, hbsize-1, hp ? hp->h_name : "NOT_FOUND");
d536 1
a536 2
		return FALSE;

d539 1
a539 2
	(void) strcpy(hbuf, hp->h_name);
	return TRUE;
d543 1
a543 1
\f

/* code for systems without sophisticated networking */
d570 1
a570 2

/*
d578 1
a578 2
**		An exit code telling if the hostname was found and
**		canonicalized.
d592 1
a592 1
	return (FALSE);
d595 1
a595 272
#endif /* DAEMON */
\f

/*
**  MAPINIT -- Open and (re)initialize a dbm database
**
**	Parameters:
**		c -- the (one character) name of the database
**
**	Returns:
**		An exit code telling if we could open the database.
**
*/
#if defined(NDBM) || defined(SDBM)
bool
mapinit(c)
	char c;
{
	struct stat stb;
	struct dbm_table *db;
	char buf[MAXNAME];

	db = &DbmTab[c & 0177];

	if (db->db_name == NULL) {
		syserr("database '%c' has not been defined", c);
		return FALSE;
	}

# ifdef YPMARK
	/*
	 * Yellow pages are always supposed to be ready
	 */
	if (db->db_name[0] == YPMARK)
		return TRUE;
# endif /* YPMARK */

	/*
	 * Have we already (unsuccessfully) tried to open it?
	 */
	if (db->db_dbm == DB_NOSUCHFILE) {
		if (tTd(60, 1))
			printf("mapinit(%c) => NO_FILE\n", c);
		return FALSE;
	}

	/*
	 * If it already is open, check if it has been changed.
	 */
	(void) sprintf(buf, "%s%s", db->db_name, DB_DIREXT);
	if (db->db_dbm != DB_NOTYETOPEN) {
		if (stat(buf, &stb) < 0 && (sleep(30), stat(buf, &stb) < 0)) {
			syserr("somebody removed %s for db '%c'", buf, c);
			db->db_dbm = DB_NOSUCHFILE;
			if (tTd(60, 1))
				printf("mapinit(%c) => FILE_REMOVED\n", c);
			return FALSE;
		}
		if (db->db_mtime != stb.st_mtime) {
			if (tTd(60, 1))
				printf("database '%c' [%s] has changed; reopening it\n",
				    c, db->db_name);
			(void) dbm_close(db->db_dbm);
			db->db_dbm = DB_NOTYETOPEN;
		}
	}

	/*
	 * Initialize database if not already open (r/w for aliases)
	 */
	if (db->db_dbm == DB_NOTYETOPEN) {
		db->db_dbm = dbm_open(db->db_name,
		c == DB_ALIAS ? O_RDWR : O_RDONLY, 0);
#if defined(apollo)
		/*  See option D in setoption() (readcf.c)  */
		OpMode == MD_INITALIAS &&
#endif /* apollo */
		if (db->db_dbm == DB_NOSUCHFILE) {

			/* try once more */
			sleep(30);
			db->db_dbm = dbm_open(db->db_name,
#if defined(apollo)
			OpMode == MD_INITALIAS &&
#endif /* apollo */
			c == DB_ALIAS ? O_RDWR : O_RDONLY, 0);
		}
		if (db->db_dbm == DB_NOSUCHFILE) {
			syserr("can't open database '%c' [%s]", c, db->db_name);
			if (tTd(60, 1))
				printf("mapinit(%c) => CAN'T OPEN %s\n",
				    c, db->db_name);
			return FALSE;
		}
		if (stat(buf, &stb) < 0 && (sleep(30), stat(buf, &stb) < 0)) {
			syserr("can't stat %s", buf);
			if (tTd(60, 1))
				printf("mapinit(%c) => FILE_REMOVED\n", c);
			return FALSE;
		}
		db->db_mtime = stb.st_mtime;

		/*
		 * Make sure the database isn't being updated
		 */
		if (flock(dbm_dirfno(db->db_dbm), LOCK_EX | LOCK_NB) < 0)
			if (errno == EWOULDBLOCK) {
				if (tTd(60, 1))
					printf("%s%s is locked, waiting...\n",
					    db->db_name, DB_DIREXT);
				(void) flock(dbm_dirfno(db->db_dbm), LOCK_EX);
			}
			else
				syserr("flock failed for db %c [%s]",
				    c, db->db_name);
		(void) flock(dbm_dirfno(db->db_dbm), LOCK_UN);
	}
	return TRUE;
}
\f

/*
**  MAPKEY -- Search a dbm database.
**
**	Search the named database using the given key.  If
**	a result is found, sprintf the argument through the
**	result back into the key and return TRUE;
**	otherwise return FALSE and do nothing.
**
**	Keysize may also be given as zero, in which case the
**	sprintf'ed result is returned if the key matched.
**
**	Parameters:
**		c -- the database
**		key -- search string
**		argval -- sprintf argument & result
**		argsiz -- size of argval
**
**	Returns:
**		An exit code telling if there was a match.
**
**	Side Effects:
**		The argval is rewritten to reflect what was found
**		in the database.
*/

mapkey(c, key, keysiz, arg)
	char c, *key, *arg;
	int keysiz;
{
	struct dbm_table *db;
	DATUM dkey, result;
	static char lowkey[MAXLINE+1];
#ifdef YPMARK
	static char *yp_domain = NULL;
#endif /* YPMARK */

	db = &DbmTab[c & 0177];

	if (tTd(60, 1))
		printf("mapkey('%c', \"%s\", \"%s\") => ",
		    c, key, arg ? arg : "--");

	/*
	 * Init the database; return if failure
	 */
	if (!mapinit(c))
		return FALSE;

	/*
	 * Normalize key (ie turn it to lowercase)
	 */
	(void) strcpy(lowkey, key);
	(void) makelower(lowkey);

#ifdef YPMARK
	/*
	 * Test for yellow page database first
	 */
	if (db->db_name[0] == YPMARK) {
		if (yp_domain == NULL)
			(void) yp_get_default_domain(&yp_domain);

	/*
	 * We include the null after the string, but Sun doesn't
	 */
		if (yp_match(yp_domain, &db->db_name[1], lowkey,
		    strlen(key)+1, &result.dptr, &result.dsize) != 0 &&
		    yp_match(yp_domain, &db->db_name[1], lowkey,
		    strlen(key), &result.dptr, &result.dsize) != 0)
			result.dptr = NULL;
		else
			/* smash newline */
			result.dptr[result.dsize] = '\0';
	}
	else
	{
#endif /* YPMARK */
		/*
		 * Go look for matching dbm entry
		 */
		dkey.dptr = lowkey;
		dkey.dsize = strlen(dkey.dptr) + 1;
		result = dbm_fetch(db->db_dbm, dkey);
#ifdef YPMARK
	}
#endif /* YPMARK */

	/*
	 * Well, were we successful?
	 */
	if (result.dptr == NULL) {
		if (tTd(60, 1))
			printf("NOT_FOUND\n");
		return FALSE;
	}

	/*
	 * Yes, rewrite result if sprintf arg was given.
	 */
	if (strlen(result.dptr) > MAXLINE)
	{
		syserr("mapkey: strlen(result.dptr) (%d) > %d\n",
			strlen(result.dptr), MAXLINE);
# ifdef LOG
		syslog(LOG_ALERT, "mapkey: strlen(result.dptr) (%d) > %d\n",
			strlen(result.dptr), MAXLINE);
# endif /* LOG */
	}
	if (arg == NULL)
		(void) strcpy(lowkey, result.dptr);
	else
		(void) sprintf(lowkey, result.dptr, arg);
	if (strlen(lowkey) > MAXLINE)
	{
		syserr("mapkey: strlen(lowkey) (%d) > %d\n",
			strlen(lowkey), MAXLINE);
# ifdef LOG
		syslog(LOG_ALERT, "mapkey: strlen(lowkey) (%d) > %d\n",
			strlen(lowkey), MAXLINE);
# endif /* LOG */
	}

	/*
	 * if keysiz is zero, that means we should return a string from
	 * the heap
	 */
	if (keysiz == 0)
		key = newstr(lowkey);
	else
	{
		if (strlen(lowkey)+1 > keysiz) {
			syserr("mapkey: result \"%s\" too long after expansion\n",
			    lowkey, keysiz);
			lowkey[keysiz-1] = '\0';
		}
		(void) strcpy(key, lowkey);
	}
	if (tTd(60, 1))
		printf("%s\n", key);

	/* Ugly kludge that assumes that sizeof(int) == sizeof(char *) */
	return (int) key;
}

#else NDBM || SDBM

/* should really read the table into the stab instead */
mapkey(db, key, keysiz, arg)
	char db, *key, *arg;
	int keysiz;
{
	return FALSE;
}

#endif /* NDBM || SDBM */
@


5.36.0.2
log
@Patches for HP-UX from Andy Linton <root@@comp.vuw.ac.nz>.  Thanks Andy!
@
text
@d779 1
a779 6
		if (flock(dbm_dirfno(db->db_dbm),
#if defined(hpux) 
			  (c == DB_ALIAS ? LOCK_EX : LOCK_SH) | LOCK_NB) < 0)
#else
			  LOCK_EX | LOCK_NB) < 0)
#endif /* hpux */
d784 1
a784 6
				(void) flock(dbm_dirfno(db->db_dbm),
#if defined(hpux) 
					     (c == DB_ALIAS ? LOCK_EX : LOCK_SH));
#else
					     LOCK_EX);
#endif /* hpux */
@


5.36.0.3
log
@Don't dup(s) for fdopen on a socket.  Sequent's run out of file descriptors
that way.


@
text
@d499 1
a499 1
	*infile = fdopen(s, "r");
@


5.36.0.4
log
@inet_ntoa() was being handed a sin_addr.s_addr instead of a sin_addr.
@
text
@d426 1
a426 1
		    inet_ntoa(SendmailAddress.sin_addr));
@


5.36.0.5
log
@Corrected mapkey() return semantics.
@
text
@a827 1
char *
d849 1
a849 1
		return NULL;
d896 1
a896 1
		return NULL;
d943 2
a944 1
	return (key);
a949 1
char *
d954 1
a954 1
	return NULL;
@


5.36.0.6
log
@Corrected missing/mis-placed ifdefs NAMED_BIND.  From Greg Onufer
(greg@@cheers.bungi.com).
@
text
@d331 1
d353 1
d355 1
a355 1
#endif /* NAMED_BIND */
a584 1
#ifdef NAMED_BIND
a589 1
#endif /* NAMED_BIND */
@


5.36.0.7
log
@Bruce Lilly (bruce%balilly@@sonyd1.broadcast.sony.com) provided additional
#ifdef NAMED_BIND statements and some code cleanups.  Some variables
initialized that may get used before being set otherwise.  lockf() 
emulations of flock() return EAGAIN instead of EWOULDBLOCK. *Sigh*
Allow qualification of simple names into the default (current) domain (Neil
Rickert).
@
text
@d22 1
a22 2
#include <sys/signal.h>
#include "sendmail.h"
d25 1
a25 1
# ifdef DAEMON
d27 1
a27 1
# else /* !DAEMON */
d29 1
a29 1
# endif /* DAEMON */
d37 1
a41 3
# ifndef LOCK_EX
#  include "flock.h"
# endif /* !LOCK_EX */
d44 2
a45 4
# ifdef NAMED_BIND
#  include <arpa/nameser.h>
#  include <resolv.h>
# endif /* NAMED_BIND */
d175 1
a175 1
# ifdef OUTPUT_PID
d177 1
a177 1
# endif /* OUTPUT_PID */
d188 1
a188 1
# ifndef BUSYEXIT
d193 1
a193 1
# else /* BUSYEXIT */
d202 1
a202 1
# endif /* !BUSYEXIT */
d325 1
a325 2
	register int i = 0;
	register int s;
d329 1
a329 1
# ifdef NAMED_BIND
d353 1
a353 1
# endif /* NAMED_BIND */
d358 1
a358 1
		long hid = -1;
d379 1
a379 1
# ifdef NAMED_BIND
d386 1
a386 1
# endif /* NAMED_BIND */
a450 1
# ifdef NAMED_BIND
a456 1
# endif /* NAMED_BIND */
d583 1
a583 1
# ifdef NAMED_BIND
d589 1
a590 7
		/*
		** But make sure default domain qualification is enabled -
		** it may have been disabled in deliver.c.  -nr
		*/
		_res.options |= RES_DEFNAMES ;
# endif /* NAMED_BIND */

d621 1
a621 1
#else /* DAEMON */
d687 1
a687 1
#if defined(NDBM) || defined(OTHERDBM)
a744 1
# if defined(apollo)
d746 5
a750 5
		    OpMode == MD_INITALIAS &&
		    c == DB_ALIAS ? O_RDWR : O_RDONLY, 0);
# else /* !apollo */
		db->db_dbm = dbm_open(db->db_name, O_RDWR, 0);
# endif /* apollo */
a754 1
# if defined(apollo)
d756 4
a759 5
			    OpMode == MD_INITALIAS &&
			    c == DB_ALIAS ? O_RDWR : O_RDONLY, 0);
# else /* !apollo */
			db->db_dbm = dbm_open(db->db_name, O_RDWR, 0);
# endif /* apollo */
d780 1
a780 1
# if defined(hpux) 
d782 1
a782 1
# else /* !hpux */
d784 2
a785 2
# endif /* hpux */
			if (errno == EWOULDBLOCK || errno == EAGAIN) {
d790 1
a790 1
# if defined(hpux) 
d792 1
a792 1
# else /* !hpux */
d794 1
a794 1
# endif /* hpux */
d797 2
a798 3
				syserr("flock failed for db %c [%s], fd %d",
				    c, db->db_name, dbm_dirfno(db->db_dbm));

d834 1
a834 1
	XDATUM dkey, result;
d836 1
a836 1
# ifdef YPMARK
d838 1
a838 1
# endif /* YPMARK */
d858 1
a858 1
# ifdef YPMARK
d880 1
a880 1
# endif /* YPMARK */
d887 1
a887 1
# ifdef YPMARK
d889 1
a889 1
# endif /* YPMARK */
d936 1
a936 1
			    lowkey);
d947 1
a947 1
#else /* !NDBM && !OTHERDBM */
d958 1
a958 1
#endif /* NDBM || OTHERDBM */
@


5.36.0.8
log
@Loop in myhostname() if NAMED_BIND defined and nameserver is unreachable.
Suggested by Steve Hubert (hubert@@cac.washington.edu).
@
text
@a534 7
# ifdef NAMED_BIND
	while ((hp = gethostbyname(hostbuf)) == NULL)
	{
		setproctitle("gethostbyname(%s) failed, sleeping", hostbuf);
		sleep(10);
	}
# else /* !NAMED_BIND */
a535 1
# endif /* NAMED_BIND */
@


5.36.0.9
log
@Changed location of '{' to be consistent with the vanilla sendmail source.
Simplified locking in mapinit().  Now only the alias file is opened O_RDWR
when OpMode == MD_INITALIAS.  All others are O_RDONLY.  The same situation
for flock(): LOCK_SH unless re-writing the alias database.  Anyothing
re-writing a DBM database should be using exclusive locks which will block
a LOCK_SH request.  This provides better support for NFS-mounted, read-only
filesystems containing the mail database files.
@
text
@d41 4
d194 1
a194 2
		while ((la = getla()) > RefuseLA)
		{
d614 1
a614 2
		if (hp == NULL)
		{
d620 1
a620 2
			if (ret != TRUE)
			{
d623 1
a623 1
						hbuf, hbsize, hbsize-1,
a715 1
	int k;
d720 1
a720 2
	if (db->db_name == NULL)
	{
d736 1
a736 2
	if (db->db_dbm == DB_NOSUCHFILE)
	{
d746 2
a747 4
	if (db->db_dbm != DB_NOTYETOPEN)
	{
		if (stat(buf, &stb) < 0 && (sleep(30), stat(buf, &stb) < 0))
		{
d754 1
a754 2
		if (db->db_mtime != stb.st_mtime)
		{
d764 1
a764 1
	 * Initialize database if not already open (r/w for aliases iff init)
d766 2
a767 2
	if (db->db_dbm == DB_NOTYETOPEN)
	{
d769 7
a775 4
		    (OpMode == MD_INITALIAS && c == DB_ALIAS)
		    ? O_RDWR : O_RDONLY, 0);
		if (db->db_dbm == DB_NOSUCHFILE)
		{
d778 1
d780 5
a784 2
			    (OpMode == MD_INITALIAS && c == DB_ALIAS)
			    ? O_RDWR : O_RDONLY, 0);
d786 1
a786 2
		if (db->db_dbm == DB_NOSUCHFILE)
		{
d793 1
a793 2
		if (stat(buf, &stb) < 0 && (sleep(30), stat(buf, &stb) < 0))
		{
a800 1
# ifndef GDBM
d805 6
a810 4
		    ((OpMode == MD_INITALIAS && c == DB_ALIAS) ? LOCK_EX : LOCK_SH) | LOCK_NB) < 0)
		{
			if (errno == EWOULDBLOCK || errno == EAGAIN)
			{
d815 5
a819 1
					    ((OpMode == MD_INITALIAS && c == DB_ALIAS) ? LOCK_EX : LOCK_SH));
d824 1
a824 1
		}
a825 1
# endif /* !GDBM */
d888 1
a888 2
	if (db->db_name[0] == YPMARK)
	{
d920 1
a920 2
	if (result.dptr == NULL)
	{
d960 1
a960 2
		if (strlen(lowkey)+1 > keysiz)
		{
d983 1
@


5.36.0.10
log
@Changed reapchild() declaration to SIG_TYPE.  Added casts to bind(),
accept(), and connect() statements.
@
text
@d104 1
a104 1
struct sockaddr_in	SendmailAddress; /* internet address of sendmail */
d114 1
a114 1
	extern SIG_TYPE reapchild();
d158 1
a158 2
	if (bind(DaemonSocket,
	    (struct sockaddr *) &SendmailAddress, sizeof SendmailAddress) < 0)
d211 1
a211 2
			t = accept(DaemonSocket,
			    (struct sockaddr *) &RealHostAddr, &lotherend);
d450 1
a450 2
	if (connect(s,
	    (struct sockaddr *) &SendmailAddress, sizeof SendmailAddress) < 0)
@


5.36.0.11
log
@Deleted #ifdef/#define OUTPUT_PID in favor of testing whether _PATH_SENDMAILPID
is set.  sendmail.h now #include's pathnames.h instead of the other
modules.
@
text
@d177 3
a179 3
# ifdef _PATH_SENDMAILPID
	(void) WritePid();
# endif /* _PATH_SENDMAILPID */
@


5.36.0.12
log
@Deleted #include <sys/types.h> as it's already included via sendmail.h from
useful.h.  #include "sendmail.h" relocated to top of #include list.
@
text
@a20 1
#include "sendmail.h"
d23 1
d41 1
@


5.36.0.13
log
@Fixed return type of maphostname() #ifndef DAEMON.
@
text
@a691 1
bool
a976 1
/*ARGSUSED*/
@


5.36.0.14
log
@ANSIfied.
@
text
@a107 1
void
d113 1
d193 1
a193 1
			Xsleep (5);
d217 1
a217 1
			Xsleep(5);
a228 2
		if (pid > 0 && tTd(4, 2))
			printf("getrequests: forking (pid = %d)\n", pid);
d232 1
a232 1
			Xsleep(10);
d239 1
d257 2
a297 1
void
d323 1
a323 1
	const char *host;
d331 1
d494 2
d527 1
d538 1
a538 1
		Xsleep(10);
d575 1
d586 1
d753 1
a753 1
		if (stat(buf, &stb) < 0 && (Xsleep(30), stat(buf, &stb) < 0))
d782 1
a782 1
			Xsleep(30);
d795 1
a795 1
		if (stat(buf, &stb) < 0 && (Xsleep(30), stat(buf, &stb) < 0))
d842 2
a843 2
**		keysiz -- size of key
**		arg -- sprintf argument
d849 1
a849 1
**		The key is rewritten to reflect what was found
d855 1
a855 3
	char c;
	const char *arg;
	char *key;
d981 1
a981 3
	char db;
	const char *arg;
	char *key;
@


5.36.0.15
log
@Check results of fdopen() calls in makeconnection() to guard against
descriptor leaks.  Suggested by Kannan Varadan.
@
text
@d503 1
a503 8
	/* Necessary to catch leaks */
	if (*outfile == NULL || *infile == NULL)
	{
		syserr("makeconnection: *outfile or *infile NULL");
		return (EX_TEMPFAIL);
	}
	else
		return (EX_OK);
@


5.36.0.16
log
@Surrounded signal(SIG_CHLD, ...) calls with #ifdef SIG_CHLD ala conf.c,
queue.c.  Deleted BUSYWAIT code as it's a performance disaster.
@
text
@a170 1
# ifdef SIGCHLD
a171 1
# endif /* SIGCHLD */
d189 1
d195 11
a205 1
		setproctitle("Waiting for connection");
a249 1
# ifdef SIGCHLD
a250 1
# endif /* SIGCHLD */
@