ypxfrd_main.c revision 114601
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 33114601Sobrien#include <sys/cdefs.h> 34114601Sobrien__FBSDID("$FreeBSD: head/usr.sbin/rpc.ypxfrd/ypxfrd_main.c 114601 2003-05-03 21:06:42Z obrien $"); 3530378Scharnier 3616125Swpaul#include "ypxfrd.h" 3730378Scharnier#include <err.h> 3830378Scharnier#include <fcntl.h> 3969793Sobrien#include <paths.h> 4016125Swpaul#include <stdio.h> 4116125Swpaul#include <stdlib.h> /* getenv, exit */ 4223716Speter#include <unistd.h> 4316125Swpaul#include <rpc/pmap_clnt.h> /* for pmap_unset */ 4416125Swpaul#include <string.h> /* strcmp */ 4516125Swpaul#include <signal.h> 4616125Swpaul#include <sys/ttycom.h> /* TIOCNOTTY */ 4716125Swpaul#ifdef __cplusplus 4816125Swpaul#include <sysent.h> /* getdtablesize, open */ 4916125Swpaul#endif /* __cplusplus */ 5016125Swpaul#include <memory.h> 5116125Swpaul#include <sys/socket.h> 5216125Swpaul#include <netinet/in.h> 5316125Swpaul#include <syslog.h> 5416125Swpaul#include "ypxfrd_extern.h" 5516125Swpaul#include <sys/wait.h> 5616125Swpaul#include <errno.h> 5716125Swpaul 5816125Swpaul#ifndef SIG_PF 5916125Swpaul#define SIG_PF void(*)(int) 6016125Swpaul#endif 6116125Swpaul 6216125Swpaul#ifdef DEBUG 6316125Swpaul#define RPC_SVC_FG 6416125Swpaul#endif 6516125Swpaul 6616125Swpaul#define _RPCSVC_CLOSEDOWN 120 6716125Swpaulint _rpcpmstart; /* Started by a port monitor ? */ 6816125Swpaulstatic int _rpcfdtype; 6916125Swpaul /* Whether Stream or Datagram ? */ 7016125Swpaul /* States a server can be in wrt request */ 7116125Swpaul 7216125Swpaul#define _IDLE 0 7316125Swpaul#define _SERVED 1 7416125Swpaul#define _SERVING 2 7516125Swpaul 7616125Swpaulextern int _rpcsvcstate; /* Set when a request is serviced */ 7716125Swpaul 7816125Swpaulchar *progname = "rpc.ypxfrd"; 7916125Swpaulchar *yp_dir = "/var/yp/"; 8016125Swpaul 8190298Sdesstatic void 8290298Sdes_msgout(char *msg) 8316125Swpaul{ 8416125Swpaul#ifdef RPC_SVC_FG 8516125Swpaul if (_rpcpmstart) 8662989Skris syslog(LOG_ERR, "%s", msg); 8716125Swpaul else 8830378Scharnier warnx("%s", msg); 8916125Swpaul#else 9062989Skris syslog(LOG_ERR, "%s", msg); 9116125Swpaul#endif 9216125Swpaul} 9316125Swpaul 9416125Swpaulstatic void 9516125Swpaulclosedown(int sig) 9616125Swpaul{ 9716125Swpaul if (_rpcsvcstate == _IDLE) { 9816125Swpaul extern fd_set svc_fdset; 9916125Swpaul static int size; 10016125Swpaul int i, openfd; 10116125Swpaul 10216125Swpaul if (_rpcfdtype == SOCK_DGRAM) 10316125Swpaul exit(0); 10416125Swpaul if (size == 0) { 10516125Swpaul size = getdtablesize(); 10616125Swpaul } 10716125Swpaul for (i = 0, openfd = 0; i < size && openfd < 2; i++) 10816125Swpaul if (FD_ISSET(i, &svc_fdset)) 10916125Swpaul openfd++; 11016125Swpaul if (openfd <= 1) 11116125Swpaul exit(0); 11216125Swpaul } 11316125Swpaul if (_rpcsvcstate == _SERVED) 11416125Swpaul _rpcsvcstate = _IDLE; 11516125Swpaul 11616125Swpaul (void) signal(SIGALRM, (SIG_PF) closedown); 11716125Swpaul (void) alarm(_RPCSVC_CLOSEDOWN/2); 11816125Swpaul} 11916125Swpaul 12016125Swpaul 12116125Swpaulstatic void 12290298Sdesypxfrd_svc_run(void) 12316125Swpaul{ 12416125Swpaul#ifdef FD_SETSIZE 12516125Swpaul fd_set readfds; 12616125Swpaul#else 12716125Swpaul int readfds; 12816125Swpaul#endif /* def FD_SETSIZE */ 12916125Swpaul extern int forked; 13016125Swpaul int pid; 13116125Swpaul int fd_setsize = _rpc_dtablesize(); 13216125Swpaul 13316125Swpaul /* Establish the identity of the parent ypserv process. */ 13416125Swpaul pid = getpid(); 13516125Swpaul 13616125Swpaul for (;;) { 13716125Swpaul#ifdef FD_SETSIZE 13816125Swpaul readfds = svc_fdset; 13916125Swpaul#else 14016125Swpaul readfds = svc_fds; 14116125Swpaul#endif /* def FD_SETSIZE */ 14216125Swpaul switch (select(fd_setsize, &readfds, NULL, NULL, 14316125Swpaul (struct timeval *)0)) { 14416125Swpaul case -1: 14516125Swpaul if (errno == EINTR) { 14616125Swpaul continue; 14716125Swpaul } 14830378Scharnier warn("svc_run: - select failed"); 14916125Swpaul return; 15016125Swpaul case 0: 15116125Swpaul continue; 15216125Swpaul default: 15316125Swpaul svc_getreqset(&readfds); 15416125Swpaul if (forked && pid != getpid()) 15516125Swpaul exit(0); 15616125Swpaul } 15716125Swpaul } 15816125Swpaul} 15916125Swpaul 16090298Sdesstatic void reaper(int sig) 16116125Swpaul{ 16236642Swpaul int status; 16336642Swpaul int saved_errno; 16416125Swpaul 16536642Swpaul saved_errno = errno; 16636642Swpaul 16716125Swpaul if (sig == SIGHUP) { 16816125Swpaul load_securenets(); 16936642Swpaul errno = saved_errno; 17016125Swpaul return; 17116125Swpaul } 17216125Swpaul 17316125Swpaul if (sig == SIGCHLD) { 17416125Swpaul while (wait3(&status, WNOHANG, NULL) > 0) 17516125Swpaul children--; 17616125Swpaul } else { 17716125Swpaul (void) pmap_unset(YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS); 17816125Swpaul exit(0); 17916125Swpaul } 18036642Swpaul 18136642Swpaul errno = saved_errno; 18236642Swpaul return; 18316125Swpaul} 18416125Swpaul 18590298Sdesvoid 18690298Sdesusage(void) 18716125Swpaul{ 18830378Scharnier fprintf(stderr, "usage: rpc.ypxfrd [-p path]\n"); 18916125Swpaul exit(0); 19016125Swpaul} 19116125Swpaul 19230378Scharnierint 19390298Sdesmain(int argc, char *argv[]) 19416125Swpaul{ 19530378Scharnier register SVCXPRT *transp = NULL; 19616125Swpaul int sock; 19730378Scharnier int proto = 0; 19816125Swpaul struct sockaddr_in saddr; 19916125Swpaul int asize = sizeof (saddr); 20016125Swpaul int ch; 20116125Swpaul 20224428Simp while ((ch = getopt(argc, argv, "p:h")) != -1) { 20390297Sdes switch (ch) { 20416125Swpaul case 'p': 20516125Swpaul yp_dir = optarg; 20616125Swpaul break; 20716125Swpaul default: 20816125Swpaul usage(); 20916125Swpaul break; 21016125Swpaul } 21116125Swpaul } 21216125Swpaul 21316125Swpaul load_securenets(); 21416125Swpaul 21516125Swpaul if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) { 21616125Swpaul int ssize = sizeof (int); 21716125Swpaul 21816125Swpaul if (saddr.sin_family != AF_INET) 21916125Swpaul exit(1); 22016125Swpaul if (getsockopt(0, SOL_SOCKET, SO_TYPE, 22116125Swpaul (char *)&_rpcfdtype, &ssize) == -1) 22216125Swpaul exit(1); 22316125Swpaul sock = 0; 22416125Swpaul _rpcpmstart = 1; 22516125Swpaul proto = 0; 22616125Swpaul openlog("rpc.ypxfrd", LOG_PID, LOG_DAEMON); 22716125Swpaul } else { 22816125Swpaul#ifndef RPC_SVC_FG 22916125Swpaul int size; 23016125Swpaul int pid, i; 23116125Swpaul 23216125Swpaul pid = fork(); 23330378Scharnier if (pid < 0) 23430378Scharnier err(1, "fork"); 23516125Swpaul if (pid) 23616125Swpaul exit(0); 23716125Swpaul size = getdtablesize(); 23816125Swpaul for (i = 0; i < size; i++) 23916125Swpaul (void) close(i); 24069793Sobrien i = open(_PATH_CONSOLE, 2); 24116125Swpaul (void) dup2(i, 1); 24216125Swpaul (void) dup2(i, 2); 24369793Sobrien i = open(_PATH_TTY, 2); 24416125Swpaul if (i >= 0) { 24516125Swpaul (void) ioctl(i, TIOCNOTTY, (char *)NULL); 24616125Swpaul (void) close(i); 24716125Swpaul } 24816125Swpaul openlog("rpc.ypxfrd", LOG_PID, LOG_DAEMON); 24916125Swpaul#endif 25016125Swpaul sock = RPC_ANYSOCK; 25116125Swpaul (void) pmap_unset(YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS); 25216125Swpaul } 25316125Swpaul 25416125Swpaul if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) { 25516125Swpaul transp = svcudp_create(sock); 25616125Swpaul if (transp == NULL) { 25716125Swpaul _msgout("cannot create udp service."); 25816125Swpaul exit(1); 25916125Swpaul } 26016125Swpaul if (!_rpcpmstart) 26116125Swpaul proto = IPPROTO_UDP; 26216125Swpaul if (!svc_register(transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, ypxfrd_freebsd_prog_1, proto)) { 26316125Swpaul _msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, udp)."); 26416125Swpaul exit(1); 26516125Swpaul } 26616125Swpaul } 26716125Swpaul 26816125Swpaul if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) { 26916125Swpaul transp = svctcp_create(sock, 0, 0); 27016125Swpaul if (transp == NULL) { 27116125Swpaul _msgout("cannot create tcp service."); 27216125Swpaul exit(1); 27316125Swpaul } 27416125Swpaul if (!_rpcpmstart) 27516125Swpaul proto = IPPROTO_TCP; 27616125Swpaul if (!svc_register(transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, ypxfrd_freebsd_prog_1, proto)) { 27716125Swpaul _msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, tcp)."); 27816125Swpaul exit(1); 27916125Swpaul } 28016125Swpaul } 28116125Swpaul 28216125Swpaul if (transp == (SVCXPRT *)NULL) { 28316125Swpaul _msgout("could not create a handle"); 28416125Swpaul exit(1); 28516125Swpaul } 28616125Swpaul if (_rpcpmstart) { 28716125Swpaul (void) signal(SIGALRM, (SIG_PF) closedown); 28816125Swpaul (void) alarm(_RPCSVC_CLOSEDOWN/2); 28916125Swpaul } 29016125Swpaul 29116125Swpaul (void) signal(SIGPIPE, SIG_IGN); 29216125Swpaul (void) signal(SIGCHLD, (SIG_PF) reaper); 29316125Swpaul (void) signal(SIGTERM, (SIG_PF) reaper); 29416125Swpaul (void) signal(SIGINT, (SIG_PF) reaper); 29516125Swpaul (void) signal(SIGHUP, (SIG_PF) reaper); 29616125Swpaul 29716125Swpaul ypxfrd_svc_run(); 29816125Swpaul _msgout("svc_run returned"); 29916125Swpaul exit(1); 30016125Swpaul /* NOTREACHED */ 30116125Swpaul} 302