ypupdated_main.c revision 30378
126236Swpaul/* 226236Swpaul * Copyright (c) 1995, 1996 326236Swpaul * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 426236Swpaul * 526236Swpaul * Redistribution and use in source and binary forms, with or without 626236Swpaul * modification, are permitted provided that the following conditions 726236Swpaul * are met: 826236Swpaul * 1. Redistributions of source code must retain the above copyright 926236Swpaul * notice, this list of conditions and the following disclaimer. 1026236Swpaul * 2. Redistributions in binary form must reproduce the above copyright 1126236Swpaul * notice, this list of conditions and the following disclaimer in the 1226236Swpaul * documentation and/or other materials provided with the distribution. 1326236Swpaul * 3. All advertising materials mentioning features or use of this software 1426236Swpaul * must display the following acknowledgement: 1526236Swpaul * This product includes software developed by Bill Paul. 1626236Swpaul * 4. Neither the name of the author nor the names of any co-contributors 1726236Swpaul * may be used to endorse or promote products derived from this software 1826236Swpaul * without specific prior written permission. 1926236Swpaul * 2026236Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 2126236Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2226236Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2326236Swpaul * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE 2426236Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2526236Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2626236Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2726236Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2826236Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2926236Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3026236Swpaul * SUCH DAMAGE. 3126236Swpaul */ 3226236Swpaul 3330378Scharnier#ifndef lint 3430378Scharnierstatic const char rcsid[] = 3530378Scharnier "$Id$"; 3630378Scharnier#endif /* not lint */ 3730378Scharnier 3826236Swpaul#include "ypupdate_prot.h" 3926236Swpaul#include <stdio.h> 4026236Swpaul#include <stdlib.h> /* getenv, exit */ 4126236Swpaul#include <rpc/pmap_clnt.h> /* for pmap_unset */ 4226236Swpaul#include <string.h> /* strcmp */ 4326236Swpaul#include <signal.h> 4426236Swpaul#include <sys/ttycom.h> /* TIOCNOTTY */ 4526236Swpaul#ifdef __cplusplus 4626236Swpaul#include <sysent.h> /* getdtablesize, open */ 4726236Swpaul#endif /* __cplusplus */ 4826236Swpaul#include <memory.h> 4926236Swpaul#include <sys/socket.h> 5026236Swpaul#include <netinet/in.h> 5126236Swpaul#include <syslog.h> 5226236Swpaul#include <sys/wait.h> 5326236Swpaul#include <errno.h> 5426236Swpaul#include <err.h> 5526236Swpaul#include <stdlib.h> 5626236Swpaul#include <unistd.h> 5726236Swpaul#include "ypupdated_extern.h" 5826236Swpaul#include "yp_extern.h" 5926236Swpaul 6026236Swpaul#ifndef SIG_PF 6126236Swpaul#define SIG_PF void(*)(int) 6226236Swpaul#endif 6326236Swpaul 6426236Swpaul#ifdef DEBUG 6526236Swpaul#define RPC_SVC_FG 6626236Swpaul#endif 6726236Swpaul 6826236Swpaul#define _RPCSVC_CLOSEDOWN 120 6926236Swpaulint _rpcpmstart; /* Started by a port monitor ? */ 7026236Swpaulstatic int _rpcfdtype; 7126236Swpaul /* Whether Stream or Datagram ? */ 7226236Swpaul /* States a server can be in wrt request */ 7326236Swpaul 7426236Swpaul#define _IDLE 0 7526236Swpaul#define _SERVED 1 7626236Swpaul#define _SERVING 2 7726236Swpaul 7826236Swpaulextern int _rpcsvcstate; /* Set when a request is serviced */ 7926236Swpaul 8026236Swpaulchar *progname = "rpc.ypupdated"; 8126236Swpaulchar *yp_dir = "/var/yp/"; 8226236Swpaul 8326236Swpaulstatic 8426236Swpaulvoid _msgout(char* msg) 8526236Swpaul{ 8626236Swpaul#ifdef RPC_SVC_FG 8726236Swpaul if (_rpcpmstart) 8826236Swpaul syslog(LOG_ERR, msg); 8926236Swpaul else 9030378Scharnier warnx("%s", msg); 9126236Swpaul#else 9226236Swpaul syslog(LOG_ERR, msg); 9326236Swpaul#endif 9426236Swpaul} 9526236Swpaul 9626236Swpaulstatic void 9726236Swpaulclosedown(int sig) 9826236Swpaul{ 9926236Swpaul if (_rpcsvcstate == _IDLE) { 10026236Swpaul extern fd_set svc_fdset; 10126236Swpaul static int size; 10226236Swpaul int i, openfd; 10326236Swpaul 10426236Swpaul if (_rpcfdtype == SOCK_DGRAM) 10526236Swpaul exit(0); 10626236Swpaul if (size == 0) { 10726236Swpaul size = getdtablesize(); 10826236Swpaul } 10926236Swpaul for (i = 0, openfd = 0; i < size && openfd < 2; i++) 11026236Swpaul if (FD_ISSET(i, &svc_fdset)) 11126236Swpaul openfd++; 11226236Swpaul if (openfd <= 1) 11326236Swpaul exit(0); 11426236Swpaul } 11526236Swpaul if (_rpcsvcstate == _SERVED) 11626236Swpaul _rpcsvcstate = _IDLE; 11726236Swpaul 11826236Swpaul (void) signal(SIGALRM, (SIG_PF) closedown); 11926236Swpaul (void) alarm(_RPCSVC_CLOSEDOWN/2); 12026236Swpaul} 12126236Swpaul 12226236Swpaulstatic void 12326236Swpaulypupdated_svc_run() 12426236Swpaul{ 12526236Swpaul#ifdef FD_SETSIZE 12626236Swpaul fd_set readfds; 12726236Swpaul#else 12826236Swpaul int readfds; 12926236Swpaul#endif /* def FD_SETSIZE */ 13026236Swpaul extern int forked; 13126236Swpaul int pid; 13226236Swpaul int fd_setsize = _rpc_dtablesize(); 13326236Swpaul 13426236Swpaul /* Establish the identity of the parent ypupdated process. */ 13526236Swpaul pid = getpid(); 13626236Swpaul 13726236Swpaul for (;;) { 13826236Swpaul#ifdef FD_SETSIZE 13926236Swpaul readfds = svc_fdset; 14026236Swpaul#else 14126236Swpaul readfds = svc_fds; 14226236Swpaul#endif /* def FD_SETSIZE */ 14326236Swpaul switch (select(fd_setsize, &readfds, NULL, NULL, 14426236Swpaul (struct timeval *)0)) { 14526236Swpaul case -1: 14626236Swpaul if (errno == EINTR) { 14726236Swpaul continue; 14826236Swpaul } 14930378Scharnier warn("svc_run: - select failed"); 15026236Swpaul return; 15126236Swpaul case 0: 15226236Swpaul continue; 15326236Swpaul default: 15426236Swpaul svc_getreqset(&readfds); 15526236Swpaul if (forked && pid != getpid()) 15626236Swpaul exit(0); 15726236Swpaul } 15826236Swpaul } 15926236Swpaul} 16026236Swpaul 16126236Swpaulstatic void reaper(sig) 16226236Swpaul int sig; 16326236Swpaul{ 16426236Swpaul int status; 16526236Swpaul 16626236Swpaul if (sig == SIGHUP) { 16726236Swpaul#ifdef foo 16826236Swpaul load_securenets(); 16926236Swpaul#endif 17026236Swpaul return; 17126236Swpaul } 17226236Swpaul 17326236Swpaul if (sig == SIGCHLD) { 17426236Swpaul while (wait3(&status, WNOHANG, NULL) > 0) 17526236Swpaul children--; 17626236Swpaul } else { 17726236Swpaul (void) pmap_unset(YPU_PROG, YPU_VERS); 17826236Swpaul exit(0); 17926236Swpaul } 18026236Swpaul} 18126236Swpaul 18226236Swpaulvoid usage() 18326236Swpaul{ 18430378Scharnier fprintf(stderr, "rpc.ypupdatedd [-p path]\n"); 18526236Swpaul exit(0); 18626236Swpaul} 18726236Swpaul 18830378Scharnierint 18926236Swpaulmain(argc, argv) 19026236Swpaul int argc; 19126236Swpaul char *argv[]; 19226236Swpaul{ 19326236Swpaul register SVCXPRT *transp = NULL; 19426236Swpaul int sock; 19526236Swpaul int proto = 0; 19626236Swpaul struct sockaddr_in saddr; 19726236Swpaul int asize = sizeof (saddr); 19826236Swpaul int ch; 19926236Swpaul 20026236Swpaul while ((ch = getopt(argc, argv, "p:h")) != EOF) { 20126236Swpaul switch(ch) { 20226236Swpaul case 'p': 20326236Swpaul yp_dir = optarg; 20426236Swpaul break; 20526236Swpaul default: 20626236Swpaul usage(); 20726236Swpaul break; 20826236Swpaul } 20926236Swpaul } 21026236Swpaul#ifdef foo 21126236Swpaul load_securenets(); 21226236Swpaul#endif 21326236Swpaul 21426236Swpaul if (svc_auth_reg(AUTH_DES, _svcauth_des) == -1) { 21526236Swpaul yp_error("failed to register AUTH_DES flavor"); 21626236Swpaul exit(1); 21726236Swpaul } 21826236Swpaul 21926236Swpaul if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) { 22026236Swpaul int ssize = sizeof (int); 22126236Swpaul 22226236Swpaul if (saddr.sin_family != AF_INET) 22326236Swpaul exit(1); 22426236Swpaul if (getsockopt(0, SOL_SOCKET, SO_TYPE, 22526236Swpaul (char *)&_rpcfdtype, &ssize) == -1) 22626236Swpaul exit(1); 22726236Swpaul sock = 0; 22826236Swpaul _rpcpmstart = 1; 22926236Swpaul proto = 0; 23026236Swpaul openlog("rpc.ypupdatedd", LOG_PID, LOG_DAEMON); 23126236Swpaul } else { 23226236Swpaul#ifndef RPC_SVC_FG 23326236Swpaul if (daemon(0,0)) { 23426236Swpaul err(1, "cannot fork"); 23526236Swpaul } 23626236Swpaul openlog("rpc.ypupdated", LOG_PID, LOG_DAEMON); 23726236Swpaul#endif 23826236Swpaul sock = RPC_ANYSOCK; 23926236Swpaul (void) pmap_unset(YPU_PROG, YPU_VERS); 24026236Swpaul } 24126236Swpaul 24226236Swpaul if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) { 24326236Swpaul transp = svcudp_create(sock); 24426236Swpaul if (transp == NULL) { 24526236Swpaul _msgout("cannot create udp service."); 24626236Swpaul exit(1); 24726236Swpaul } 24826236Swpaul if (!_rpcpmstart) 24926236Swpaul proto = IPPROTO_UDP; 25026236Swpaul if (!svc_register(transp, YPU_PROG, YPU_VERS, ypu_prog_1, proto)) { 25126236Swpaul _msgout("unable to register (YPU_PROG, YPU_VERS, udp)."); 25226236Swpaul exit(1); 25326236Swpaul } 25426236Swpaul } 25526236Swpaul 25626236Swpaul if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) { 25726236Swpaul transp = svctcp_create(sock, 0, 0); 25826236Swpaul if (transp == NULL) { 25926236Swpaul _msgout("cannot create tcp service."); 26026236Swpaul exit(1); 26126236Swpaul } 26226236Swpaul if (!_rpcpmstart) 26326236Swpaul proto = IPPROTO_TCP; 26426236Swpaul if (!svc_register(transp, YPU_PROG, YPU_VERS, ypu_prog_1, proto)) { 26526236Swpaul _msgout("unable to register (YPU_PROG, YPU_VERS, tcp)."); 26626236Swpaul exit(1); 26726236Swpaul } 26826236Swpaul } 26926236Swpaul 27026236Swpaul if (transp == (SVCXPRT *)NULL) { 27126236Swpaul _msgout("could not create a handle"); 27226236Swpaul exit(1); 27326236Swpaul } 27426236Swpaul if (_rpcpmstart) { 27526236Swpaul (void) signal(SIGALRM, (SIG_PF) closedown); 27626236Swpaul (void) alarm(_RPCSVC_CLOSEDOWN/2); 27726236Swpaul } 27826236Swpaul 27926236Swpaul (void) signal(SIGPIPE, SIG_IGN); 28026236Swpaul (void) signal(SIGCHLD, (SIG_PF) reaper); 28126236Swpaul (void) signal(SIGTERM, (SIG_PF) reaper); 28226236Swpaul (void) signal(SIGINT, (SIG_PF) reaper); 28326236Swpaul (void) signal(SIGHUP, (SIG_PF) reaper); 28426236Swpaul 28526236Swpaul ypupdated_svc_run(); 28626236Swpaul _msgout("svc_run returned"); 28726236Swpaul exit(1); 28826236Swpaul /* NOTREACHED */ 28926236Swpaul} 290