ypxfrd_main.c revision 30378
116125Swpaul/*
216125Swpaul * Copyright (c) 1995, 1996
316125Swpaul *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
416125Swpaul *
516125Swpaul * Redistribution and use in source and binary forms, with or without
616125Swpaul * modification, are permitted provided that the following conditions
716125Swpaul * are met:
816125Swpaul * 1. Redistributions of source code must retain the above copyright
916125Swpaul *    notice, this list of conditions and the following disclaimer.
1016125Swpaul * 2. Redistributions in binary form must reproduce the above copyright
1116125Swpaul *    notice, this list of conditions and the following disclaimer in the
1216125Swpaul *    documentation and/or other materials provided with the distribution.
1316125Swpaul * 3. All advertising materials mentioning features or use of this software
1416125Swpaul *    must display the following acknowledgement:
1516125Swpaul *	This product includes software developed by Bill Paul.
1616125Swpaul * 4. Neither the name of the author nor the names of any co-contributors
1716125Swpaul *    may be used to endorse or promote products derived from this software
1816125Swpaul *    without specific prior written permission.
1916125Swpaul *
2016125Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2116125Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2216125Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2316125Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
2416125Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2516125Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2616125Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2716125Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2816125Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2916125Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3016125Swpaul * SUCH DAMAGE.
3116125Swpaul */
3216125Swpaul
3330378Scharnier#ifndef lint
3430378Scharnierstatic const char rcsid[] =
3530378Scharnier	"$Id$";
3630378Scharnier#endif /* not lint */
3730378Scharnier
3816125Swpaul#include "ypxfrd.h"
3930378Scharnier#include <err.h>
4030378Scharnier#include <fcntl.h>
4116125Swpaul#include <stdio.h>
4216125Swpaul#include <stdlib.h> /* getenv, exit */
4323716Speter#include <unistd.h>
4416125Swpaul#include <rpc/pmap_clnt.h> /* for pmap_unset */
4516125Swpaul#include <string.h> /* strcmp */
4616125Swpaul#include <signal.h>
4716125Swpaul#include <sys/ttycom.h> /* TIOCNOTTY */
4816125Swpaul#ifdef __cplusplus
4916125Swpaul#include <sysent.h> /* getdtablesize, open */
5016125Swpaul#endif /* __cplusplus */
5116125Swpaul#include <memory.h>
5216125Swpaul#include <sys/socket.h>
5316125Swpaul#include <netinet/in.h>
5416125Swpaul#include <syslog.h>
5516125Swpaul#include "ypxfrd_extern.h"
5616125Swpaul#include <sys/wait.h>
5716125Swpaul#include <errno.h>
5816125Swpaul
5916125Swpaul#ifndef SIG_PF
6016125Swpaul#define	SIG_PF void(*)(int)
6116125Swpaul#endif
6216125Swpaul
6316125Swpaul#ifdef DEBUG
6416125Swpaul#define	RPC_SVC_FG
6516125Swpaul#endif
6616125Swpaul
6716125Swpaul#define	_RPCSVC_CLOSEDOWN 120
6816125Swpaulint _rpcpmstart;		/* Started by a port monitor ? */
6916125Swpaulstatic int _rpcfdtype;
7016125Swpaul		 /* Whether Stream or Datagram ? */
7116125Swpaul	/* States a server can be in wrt request */
7216125Swpaul
7316125Swpaul#define	_IDLE 0
7416125Swpaul#define	_SERVED 1
7516125Swpaul#define	_SERVING 2
7616125Swpaul
7716125Swpaulextern int _rpcsvcstate;	 /* Set when a request is serviced */
7816125Swpaul
7916125Swpaulchar *progname = "rpc.ypxfrd";
8016125Swpaulchar *yp_dir = "/var/yp/";
8116125Swpaul
8216125Swpaulstatic
8316125Swpaulvoid _msgout(char* msg)
8416125Swpaul{
8516125Swpaul#ifdef RPC_SVC_FG
8616125Swpaul	if (_rpcpmstart)
8716125Swpaul		syslog(LOG_ERR, msg);
8816125Swpaul	else
8930378Scharnier		warnx("%s", msg);
9016125Swpaul#else
9116125Swpaul	syslog(LOG_ERR, msg);
9216125Swpaul#endif
9316125Swpaul}
9416125Swpaul
9516125Swpaulstatic void
9616125Swpaulclosedown(int sig)
9716125Swpaul{
9816125Swpaul	if (_rpcsvcstate == _IDLE) {
9916125Swpaul		extern fd_set svc_fdset;
10016125Swpaul		static int size;
10116125Swpaul		int i, openfd;
10216125Swpaul
10316125Swpaul		if (_rpcfdtype == SOCK_DGRAM)
10416125Swpaul			exit(0);
10516125Swpaul		if (size == 0) {
10616125Swpaul			size = getdtablesize();
10716125Swpaul		}
10816125Swpaul		for (i = 0, openfd = 0; i < size && openfd < 2; i++)
10916125Swpaul			if (FD_ISSET(i, &svc_fdset))
11016125Swpaul				openfd++;
11116125Swpaul		if (openfd <= 1)
11216125Swpaul			exit(0);
11316125Swpaul	}
11416125Swpaul	if (_rpcsvcstate == _SERVED)
11516125Swpaul		_rpcsvcstate = _IDLE;
11616125Swpaul
11716125Swpaul	(void) signal(SIGALRM, (SIG_PF) closedown);
11816125Swpaul	(void) alarm(_RPCSVC_CLOSEDOWN/2);
11916125Swpaul}
12016125Swpaul
12116125Swpaul
12216125Swpaulstatic void
12316125Swpaulypxfrd_svc_run()
12416125Swpaul{
12516125Swpaul#ifdef FD_SETSIZE
12616125Swpaul	fd_set readfds;
12716125Swpaul#else
12816125Swpaul	int readfds;
12916125Swpaul#endif /* def FD_SETSIZE */
13016125Swpaul	extern int forked;
13116125Swpaul	int pid;
13216125Swpaul	int fd_setsize = _rpc_dtablesize();
13316125Swpaul
13416125Swpaul	/* Establish the identity of the parent ypserv process. */
13516125Swpaul	pid = getpid();
13616125Swpaul
13716125Swpaul	for (;;) {
13816125Swpaul#ifdef FD_SETSIZE
13916125Swpaul		readfds = svc_fdset;
14016125Swpaul#else
14116125Swpaul		readfds = svc_fds;
14216125Swpaul#endif /* def FD_SETSIZE */
14316125Swpaul		switch (select(fd_setsize, &readfds, NULL, NULL,
14416125Swpaul			       (struct timeval *)0)) {
14516125Swpaul		case -1:
14616125Swpaul			if (errno == EINTR) {
14716125Swpaul				continue;
14816125Swpaul			}
14930378Scharnier			warn("svc_run: - select failed");
15016125Swpaul			return;
15116125Swpaul		case 0:
15216125Swpaul			continue;
15316125Swpaul		default:
15416125Swpaul			svc_getreqset(&readfds);
15516125Swpaul			if (forked && pid != getpid())
15616125Swpaul				exit(0);
15716125Swpaul		}
15816125Swpaul	}
15916125Swpaul}
16016125Swpaul
16116125Swpaulstatic void reaper(sig)
16216125Swpaul	int sig;
16316125Swpaul{
16416125Swpaul	int status;
16516125Swpaul
16616125Swpaul	if (sig == SIGHUP) {
16716125Swpaul		load_securenets();
16816125Swpaul		return;
16916125Swpaul	}
17016125Swpaul
17116125Swpaul	if (sig == SIGCHLD) {
17216125Swpaul		while (wait3(&status, WNOHANG, NULL) > 0)
17316125Swpaul			children--;
17416125Swpaul	} else {
17516125Swpaul		(void) pmap_unset(YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS);
17616125Swpaul		exit(0);
17716125Swpaul	}
17816125Swpaul}
17916125Swpaul
18016125Swpaulvoid usage()
18116125Swpaul{
18230378Scharnier	fprintf(stderr, "usage: rpc.ypxfrd [-p path]\n");
18316125Swpaul	exit(0);
18416125Swpaul}
18516125Swpaul
18630378Scharnierint
18716125Swpaulmain(argc, argv)
18816125Swpaul	int argc;
18916125Swpaul	char *argv[];
19016125Swpaul{
19130378Scharnier	register SVCXPRT *transp = NULL;
19216125Swpaul	int sock;
19330378Scharnier	int proto = 0;
19416125Swpaul	struct sockaddr_in saddr;
19516125Swpaul	int asize = sizeof (saddr);
19616125Swpaul	int ch;
19716125Swpaul
19824428Simp	while ((ch = getopt(argc, argv, "p:h")) != -1) {
19916125Swpaul		switch(ch) {
20016125Swpaul		case 'p':
20116125Swpaul			yp_dir = optarg;
20216125Swpaul			break;
20316125Swpaul		default:
20416125Swpaul			usage();
20516125Swpaul			break;
20616125Swpaul		}
20716125Swpaul	}
20816125Swpaul
20916125Swpaul	load_securenets();
21016125Swpaul
21116125Swpaul	if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
21216125Swpaul		int ssize = sizeof (int);
21316125Swpaul
21416125Swpaul		if (saddr.sin_family != AF_INET)
21516125Swpaul			exit(1);
21616125Swpaul		if (getsockopt(0, SOL_SOCKET, SO_TYPE,
21716125Swpaul				(char *)&_rpcfdtype, &ssize) == -1)
21816125Swpaul			exit(1);
21916125Swpaul		sock = 0;
22016125Swpaul		_rpcpmstart = 1;
22116125Swpaul		proto = 0;
22216125Swpaul		openlog("rpc.ypxfrd", LOG_PID, LOG_DAEMON);
22316125Swpaul	} else {
22416125Swpaul#ifndef RPC_SVC_FG
22516125Swpaul		int size;
22616125Swpaul		int pid, i;
22716125Swpaul
22816125Swpaul		pid = fork();
22930378Scharnier		if (pid < 0)
23030378Scharnier			err(1, "fork");
23116125Swpaul		if (pid)
23216125Swpaul			exit(0);
23316125Swpaul		size = getdtablesize();
23416125Swpaul		for (i = 0; i < size; i++)
23516125Swpaul			(void) close(i);
23616125Swpaul		i = open("/dev/console", 2);
23716125Swpaul		(void) dup2(i, 1);
23816125Swpaul		(void) dup2(i, 2);
23916125Swpaul		i = open("/dev/tty", 2);
24016125Swpaul		if (i >= 0) {
24116125Swpaul			(void) ioctl(i, TIOCNOTTY, (char *)NULL);
24216125Swpaul			(void) close(i);
24316125Swpaul		}
24416125Swpaul		openlog("rpc.ypxfrd", LOG_PID, LOG_DAEMON);
24516125Swpaul#endif
24616125Swpaul		sock = RPC_ANYSOCK;
24716125Swpaul		(void) pmap_unset(YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS);
24816125Swpaul	}
24916125Swpaul
25016125Swpaul	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
25116125Swpaul		transp = svcudp_create(sock);
25216125Swpaul		if (transp == NULL) {
25316125Swpaul			_msgout("cannot create udp service.");
25416125Swpaul			exit(1);
25516125Swpaul		}
25616125Swpaul		if (!_rpcpmstart)
25716125Swpaul			proto = IPPROTO_UDP;
25816125Swpaul		if (!svc_register(transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, ypxfrd_freebsd_prog_1, proto)) {
25916125Swpaul			_msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, udp).");
26016125Swpaul			exit(1);
26116125Swpaul		}
26216125Swpaul	}
26316125Swpaul
26416125Swpaul	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
26516125Swpaul		transp = svctcp_create(sock, 0, 0);
26616125Swpaul		if (transp == NULL) {
26716125Swpaul			_msgout("cannot create tcp service.");
26816125Swpaul			exit(1);
26916125Swpaul		}
27016125Swpaul		if (!_rpcpmstart)
27116125Swpaul			proto = IPPROTO_TCP;
27216125Swpaul		if (!svc_register(transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, ypxfrd_freebsd_prog_1, proto)) {
27316125Swpaul			_msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, tcp).");
27416125Swpaul			exit(1);
27516125Swpaul		}
27616125Swpaul	}
27716125Swpaul
27816125Swpaul	if (transp == (SVCXPRT *)NULL) {
27916125Swpaul		_msgout("could not create a handle");
28016125Swpaul		exit(1);
28116125Swpaul	}
28216125Swpaul	if (_rpcpmstart) {
28316125Swpaul		(void) signal(SIGALRM, (SIG_PF) closedown);
28416125Swpaul		(void) alarm(_RPCSVC_CLOSEDOWN/2);
28516125Swpaul	}
28616125Swpaul
28716125Swpaul	(void) signal(SIGPIPE, SIG_IGN);
28816125Swpaul	(void) signal(SIGCHLD, (SIG_PF) reaper);
28916125Swpaul	(void) signal(SIGTERM, (SIG_PF) reaper);
29016125Swpaul	(void) signal(SIGINT, (SIG_PF) reaper);
29116125Swpaul	(void) signal(SIGHUP, (SIG_PF) reaper);
29216125Swpaul
29316125Swpaul	ypxfrd_svc_run();
29416125Swpaul	_msgout("svc_run returned");
29516125Swpaul	exit(1);
29616125Swpaul	/* NOTREACHED */
29716125Swpaul}
298