ypxfrd_main.c revision 16125
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 * $Id: ypxfrd_main.c,v 1.4 1996/06/04 04:27:47 wpaul Exp $ 3316125Swpaul */ 3416125Swpaul 3516125Swpaul#include "ypxfrd.h" 3616125Swpaul#include <stdio.h> 3716125Swpaul#include <stdlib.h> /* getenv, exit */ 3816125Swpaul#include <rpc/pmap_clnt.h> /* for pmap_unset */ 3916125Swpaul#include <string.h> /* strcmp */ 4016125Swpaul#include <signal.h> 4116125Swpaul#include <sys/ttycom.h> /* TIOCNOTTY */ 4216125Swpaul#ifdef __cplusplus 4316125Swpaul#include <sysent.h> /* getdtablesize, open */ 4416125Swpaul#endif /* __cplusplus */ 4516125Swpaul#include <memory.h> 4616125Swpaul#include <sys/socket.h> 4716125Swpaul#include <netinet/in.h> 4816125Swpaul#include <syslog.h> 4916125Swpaul#include "ypxfrd_extern.h" 5016125Swpaul#include <sys/wait.h> 5116125Swpaul#include <errno.h> 5216125Swpaul 5316125Swpaul#ifndef SIG_PF 5416125Swpaul#define SIG_PF void(*)(int) 5516125Swpaul#endif 5616125Swpaul 5716125Swpaul#ifdef DEBUG 5816125Swpaul#define RPC_SVC_FG 5916125Swpaul#endif 6016125Swpaul 6116125Swpaul#define _RPCSVC_CLOSEDOWN 120 6216125Swpaul#ifndef lint 6316125Swpaulstatic const char rcsid[] = "$Id: ypxfrd_main.c,v 1.4 1996/06/04 04:27:47 wpaul Exp $"; 6416125Swpaul#endif /* not lint */ 6516125Swpaulint _rpcpmstart; /* Started by a port monitor ? */ 6616125Swpaulstatic int _rpcfdtype; 6716125Swpaul /* Whether Stream or Datagram ? */ 6816125Swpaul /* States a server can be in wrt request */ 6916125Swpaul 7016125Swpaul#define _IDLE 0 7116125Swpaul#define _SERVED 1 7216125Swpaul#define _SERVING 2 7316125Swpaul 7416125Swpaulextern int _rpcsvcstate; /* Set when a request is serviced */ 7516125Swpaul 7616125Swpaulchar *progname = "rpc.ypxfrd"; 7716125Swpaulchar *yp_dir = "/var/yp/"; 7816125Swpaul 7916125Swpaulstatic 8016125Swpaulvoid _msgout(char* msg) 8116125Swpaul{ 8216125Swpaul#ifdef RPC_SVC_FG 8316125Swpaul if (_rpcpmstart) 8416125Swpaul syslog(LOG_ERR, msg); 8516125Swpaul else 8616125Swpaul (void) fprintf(stderr, "%s\n", msg); 8716125Swpaul#else 8816125Swpaul syslog(LOG_ERR, msg); 8916125Swpaul#endif 9016125Swpaul} 9116125Swpaul 9216125Swpaulstatic void 9316125Swpaulclosedown(int sig) 9416125Swpaul{ 9516125Swpaul if (_rpcsvcstate == _IDLE) { 9616125Swpaul extern fd_set svc_fdset; 9716125Swpaul static int size; 9816125Swpaul int i, openfd; 9916125Swpaul 10016125Swpaul if (_rpcfdtype == SOCK_DGRAM) 10116125Swpaul exit(0); 10216125Swpaul if (size == 0) { 10316125Swpaul size = getdtablesize(); 10416125Swpaul } 10516125Swpaul for (i = 0, openfd = 0; i < size && openfd < 2; i++) 10616125Swpaul if (FD_ISSET(i, &svc_fdset)) 10716125Swpaul openfd++; 10816125Swpaul if (openfd <= 1) 10916125Swpaul exit(0); 11016125Swpaul } 11116125Swpaul if (_rpcsvcstate == _SERVED) 11216125Swpaul _rpcsvcstate = _IDLE; 11316125Swpaul 11416125Swpaul (void) signal(SIGALRM, (SIG_PF) closedown); 11516125Swpaul (void) alarm(_RPCSVC_CLOSEDOWN/2); 11616125Swpaul} 11716125Swpaul 11816125Swpaul 11916125Swpaulstatic void 12016125Swpaulypxfrd_svc_run() 12116125Swpaul{ 12216125Swpaul#ifdef FD_SETSIZE 12316125Swpaul fd_set readfds; 12416125Swpaul#else 12516125Swpaul int readfds; 12616125Swpaul#endif /* def FD_SETSIZE */ 12716125Swpaul extern int forked; 12816125Swpaul int pid; 12916125Swpaul int fd_setsize = _rpc_dtablesize(); 13016125Swpaul 13116125Swpaul /* Establish the identity of the parent ypserv process. */ 13216125Swpaul pid = getpid(); 13316125Swpaul 13416125Swpaul for (;;) { 13516125Swpaul#ifdef FD_SETSIZE 13616125Swpaul readfds = svc_fdset; 13716125Swpaul#else 13816125Swpaul readfds = svc_fds; 13916125Swpaul#endif /* def FD_SETSIZE */ 14016125Swpaul switch (select(fd_setsize, &readfds, NULL, NULL, 14116125Swpaul (struct timeval *)0)) { 14216125Swpaul case -1: 14316125Swpaul if (errno == EINTR) { 14416125Swpaul continue; 14516125Swpaul } 14616125Swpaul perror("svc_run: - select failed"); 14716125Swpaul return; 14816125Swpaul case 0: 14916125Swpaul continue; 15016125Swpaul default: 15116125Swpaul svc_getreqset(&readfds); 15216125Swpaul if (forked && pid != getpid()) 15316125Swpaul exit(0); 15416125Swpaul } 15516125Swpaul } 15616125Swpaul} 15716125Swpaul 15816125Swpaulstatic void reaper(sig) 15916125Swpaul int sig; 16016125Swpaul{ 16116125Swpaul int status; 16216125Swpaul 16316125Swpaul if (sig == SIGHUP) { 16416125Swpaul load_securenets(); 16516125Swpaul return; 16616125Swpaul } 16716125Swpaul 16816125Swpaul if (sig == SIGCHLD) { 16916125Swpaul while (wait3(&status, WNOHANG, NULL) > 0) 17016125Swpaul children--; 17116125Swpaul } else { 17216125Swpaul (void) pmap_unset(YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS); 17316125Swpaul exit(0); 17416125Swpaul } 17516125Swpaul} 17616125Swpaul 17716125Swpaulvoid usage() 17816125Swpaul{ 17916125Swpaul fprintf(stderr, "%s [-p path]\n", progname); 18016125Swpaul exit(0); 18116125Swpaul} 18216125Swpaul 18316125Swpaulmain(argc, argv) 18416125Swpaul int argc; 18516125Swpaul char *argv[]; 18616125Swpaul{ 18716125Swpaul register SVCXPRT *transp; 18816125Swpaul int sock; 18916125Swpaul int proto; 19016125Swpaul struct sockaddr_in saddr; 19116125Swpaul int asize = sizeof (saddr); 19216125Swpaul int ch; 19316125Swpaul 19416125Swpaul while ((ch = getopt(argc, argv, "p:h")) != EOF) { 19516125Swpaul switch(ch) { 19616125Swpaul case 'p': 19716125Swpaul yp_dir = optarg; 19816125Swpaul break; 19916125Swpaul default: 20016125Swpaul usage(); 20116125Swpaul break; 20216125Swpaul } 20316125Swpaul } 20416125Swpaul 20516125Swpaul load_securenets(); 20616125Swpaul 20716125Swpaul if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) { 20816125Swpaul int ssize = sizeof (int); 20916125Swpaul 21016125Swpaul if (saddr.sin_family != AF_INET) 21116125Swpaul exit(1); 21216125Swpaul if (getsockopt(0, SOL_SOCKET, SO_TYPE, 21316125Swpaul (char *)&_rpcfdtype, &ssize) == -1) 21416125Swpaul exit(1); 21516125Swpaul sock = 0; 21616125Swpaul _rpcpmstart = 1; 21716125Swpaul proto = 0; 21816125Swpaul openlog("rpc.ypxfrd", LOG_PID, LOG_DAEMON); 21916125Swpaul } else { 22016125Swpaul#ifndef RPC_SVC_FG 22116125Swpaul int size; 22216125Swpaul int pid, i; 22316125Swpaul 22416125Swpaul pid = fork(); 22516125Swpaul if (pid < 0) { 22616125Swpaul perror("cannot fork"); 22716125Swpaul exit(1); 22816125Swpaul } 22916125Swpaul if (pid) 23016125Swpaul exit(0); 23116125Swpaul size = getdtablesize(); 23216125Swpaul for (i = 0; i < size; i++) 23316125Swpaul (void) close(i); 23416125Swpaul i = open("/dev/console", 2); 23516125Swpaul (void) dup2(i, 1); 23616125Swpaul (void) dup2(i, 2); 23716125Swpaul i = open("/dev/tty", 2); 23816125Swpaul if (i >= 0) { 23916125Swpaul (void) ioctl(i, TIOCNOTTY, (char *)NULL); 24016125Swpaul (void) close(i); 24116125Swpaul } 24216125Swpaul openlog("rpc.ypxfrd", LOG_PID, LOG_DAEMON); 24316125Swpaul#endif 24416125Swpaul sock = RPC_ANYSOCK; 24516125Swpaul (void) pmap_unset(YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS); 24616125Swpaul } 24716125Swpaul 24816125Swpaul if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) { 24916125Swpaul transp = svcudp_create(sock); 25016125Swpaul if (transp == NULL) { 25116125Swpaul _msgout("cannot create udp service."); 25216125Swpaul exit(1); 25316125Swpaul } 25416125Swpaul if (!_rpcpmstart) 25516125Swpaul proto = IPPROTO_UDP; 25616125Swpaul if (!svc_register(transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, ypxfrd_freebsd_prog_1, proto)) { 25716125Swpaul _msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, udp)."); 25816125Swpaul exit(1); 25916125Swpaul } 26016125Swpaul } 26116125Swpaul 26216125Swpaul if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) { 26316125Swpaul transp = svctcp_create(sock, 0, 0); 26416125Swpaul if (transp == NULL) { 26516125Swpaul _msgout("cannot create tcp service."); 26616125Swpaul exit(1); 26716125Swpaul } 26816125Swpaul if (!_rpcpmstart) 26916125Swpaul proto = IPPROTO_TCP; 27016125Swpaul if (!svc_register(transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, ypxfrd_freebsd_prog_1, proto)) { 27116125Swpaul _msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, tcp)."); 27216125Swpaul exit(1); 27316125Swpaul } 27416125Swpaul } 27516125Swpaul 27616125Swpaul if (transp == (SVCXPRT *)NULL) { 27716125Swpaul _msgout("could not create a handle"); 27816125Swpaul exit(1); 27916125Swpaul } 28016125Swpaul if (_rpcpmstart) { 28116125Swpaul (void) signal(SIGALRM, (SIG_PF) closedown); 28216125Swpaul (void) alarm(_RPCSVC_CLOSEDOWN/2); 28316125Swpaul } 28416125Swpaul 28516125Swpaul (void) signal(SIGPIPE, SIG_IGN); 28616125Swpaul (void) signal(SIGCHLD, (SIG_PF) reaper); 28716125Swpaul (void) signal(SIGTERM, (SIG_PF) reaper); 28816125Swpaul (void) signal(SIGINT, (SIG_PF) reaper); 28916125Swpaul (void) signal(SIGHUP, (SIG_PF) reaper); 29016125Swpaul 29116125Swpaul ypxfrd_svc_run(); 29216125Swpaul _msgout("svc_run returned"); 29316125Swpaul exit(1); 29416125Swpaul /* NOTREACHED */ 29516125Swpaul} 296