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 33114601Sobrien#include <sys/cdefs.h> 34114601Sobrien__FBSDID("$FreeBSD$"); 3530378Scharnier 3626236Swpaul#include "ypupdate_prot.h" 3726236Swpaul#include <stdio.h> 3826236Swpaul#include <stdlib.h> /* getenv, exit */ 3926236Swpaul#include <rpc/pmap_clnt.h> /* for pmap_unset */ 40129654Sstefanf#include <rpc/rpc_com.h> 4126236Swpaul#include <string.h> /* strcmp */ 4226236Swpaul#include <signal.h> 4326236Swpaul#ifdef __cplusplus 4426236Swpaul#include <sysent.h> /* getdtablesize, open */ 4526236Swpaul#endif /* __cplusplus */ 4626236Swpaul#include <memory.h> 4726236Swpaul#include <sys/socket.h> 4826236Swpaul#include <netinet/in.h> 4926236Swpaul#include <syslog.h> 5026236Swpaul#include <sys/wait.h> 5126236Swpaul#include <errno.h> 5226236Swpaul#include <err.h> 5326236Swpaul#include <unistd.h> 5426236Swpaul#include "ypupdated_extern.h" 5526236Swpaul#include "yp_extern.h" 5626236Swpaul 5726236Swpaul#ifndef SIG_PF 5826236Swpaul#define SIG_PF void(*)(int) 5926236Swpaul#endif 6026236Swpaul 6126236Swpaul#ifdef DEBUG 6226236Swpaul#define RPC_SVC_FG 6326236Swpaul#endif 6426236Swpaul 6526236Swpaul#define _RPCSVC_CLOSEDOWN 120 6626236Swpaulint _rpcpmstart; /* Started by a port monitor ? */ 6726236Swpaulstatic int _rpcfdtype; 6826236Swpaul /* Whether Stream or Datagram ? */ 6926236Swpaul /* States a server can be in wrt request */ 7026236Swpaul 7126236Swpaul#define _IDLE 0 7226236Swpaul#define _SERVED 1 7326236Swpaul#define _SERVING 2 7426236Swpaul 7526236Swpaulextern int _rpcsvcstate; /* Set when a request is serviced */ 7626236Swpaul 7726236Swpaulchar *progname = "rpc.ypupdated"; 7826236Swpaulchar *yp_dir = "/var/yp/"; 7926236Swpaul 8090298Sdesstatic void 8190298Sdes_msgout(char* msg) 8226236Swpaul{ 8326236Swpaul#ifdef RPC_SVC_FG 8426236Swpaul if (_rpcpmstart) 8562989Skris syslog(LOG_ERR, "%s", msg); 8626236Swpaul else 8730378Scharnier warnx("%s", msg); 8826236Swpaul#else 8962989Skris syslog(LOG_ERR, "%s", msg); 9026236Swpaul#endif 9126236Swpaul} 9226236Swpaul 9326236Swpaulstatic void 9426236Swpaulclosedown(int sig) 9526236Swpaul{ 9626236Swpaul if (_rpcsvcstate == _IDLE) { 9726236Swpaul extern fd_set svc_fdset; 9826236Swpaul static int size; 9926236Swpaul int i, openfd; 10026236Swpaul 10126236Swpaul if (_rpcfdtype == SOCK_DGRAM) 10226236Swpaul exit(0); 10326236Swpaul if (size == 0) { 10426236Swpaul size = getdtablesize(); 10526236Swpaul } 10626236Swpaul for (i = 0, openfd = 0; i < size && openfd < 2; i++) 10726236Swpaul if (FD_ISSET(i, &svc_fdset)) 10826236Swpaul openfd++; 10926236Swpaul if (openfd <= 1) 11026236Swpaul exit(0); 11126236Swpaul } 11226236Swpaul if (_rpcsvcstate == _SERVED) 11326236Swpaul _rpcsvcstate = _IDLE; 11426236Swpaul 11526236Swpaul (void) signal(SIGALRM, (SIG_PF) closedown); 11626236Swpaul (void) alarm(_RPCSVC_CLOSEDOWN/2); 11726236Swpaul} 11826236Swpaul 11926236Swpaulstatic void 12090298Sdesypupdated_svc_run(void) 12126236Swpaul{ 12226236Swpaul#ifdef FD_SETSIZE 12326236Swpaul fd_set readfds; 12426236Swpaul#else 12526236Swpaul int readfds; 12626236Swpaul#endif /* def FD_SETSIZE */ 12726236Swpaul extern int forked; 12826236Swpaul int pid; 12926236Swpaul int fd_setsize = _rpc_dtablesize(); 13026236Swpaul 13126236Swpaul /* Establish the identity of the parent ypupdated process. */ 13226236Swpaul pid = getpid(); 13326236Swpaul 13426236Swpaul for (;;) { 13526236Swpaul#ifdef FD_SETSIZE 13626236Swpaul readfds = svc_fdset; 13726236Swpaul#else 13826236Swpaul readfds = svc_fds; 13926236Swpaul#endif /* def FD_SETSIZE */ 14026236Swpaul switch (select(fd_setsize, &readfds, NULL, NULL, 14126236Swpaul (struct timeval *)0)) { 14226236Swpaul case -1: 14326236Swpaul if (errno == EINTR) { 14426236Swpaul continue; 14526236Swpaul } 14630378Scharnier warn("svc_run: - select failed"); 14726236Swpaul return; 14826236Swpaul case 0: 14926236Swpaul continue; 15026236Swpaul default: 15126236Swpaul svc_getreqset(&readfds); 15226236Swpaul if (forked && pid != getpid()) 15326236Swpaul exit(0); 15426236Swpaul } 15526236Swpaul } 15626236Swpaul} 15726236Swpaul 15890298Sdesstatic void 15990298Sdesreaper(int sig) 16026236Swpaul{ 16126236Swpaul int status; 16226236Swpaul 16326236Swpaul if (sig == SIGHUP) { 16426236Swpaul#ifdef foo 16526236Swpaul load_securenets(); 16626236Swpaul#endif 16726236Swpaul return; 16826236Swpaul } 16926236Swpaul 17026236Swpaul if (sig == SIGCHLD) { 17126236Swpaul while (wait3(&status, WNOHANG, NULL) > 0) 17226236Swpaul children--; 17326236Swpaul } else { 17426236Swpaul (void) pmap_unset(YPU_PROG, YPU_VERS); 17526236Swpaul exit(0); 17626236Swpaul } 17726236Swpaul} 17826236Swpaul 17990298Sdesvoid 18090298Sdesusage(void) 18126236Swpaul{ 18230378Scharnier fprintf(stderr, "rpc.ypupdatedd [-p path]\n"); 18326236Swpaul exit(0); 18426236Swpaul} 18526236Swpaul 18630378Scharnierint 18790298Sdesmain(int argc, char *argv[]) 18826236Swpaul{ 18926236Swpaul register SVCXPRT *transp = NULL; 19026236Swpaul int sock; 19126236Swpaul int proto = 0; 19226236Swpaul struct sockaddr_in saddr; 19326236Swpaul int asize = sizeof (saddr); 19426236Swpaul int ch; 19526236Swpaul 19647442Simp while ((ch = getopt(argc, argv, "p:h")) != -1) { 19790297Sdes switch (ch) { 19826236Swpaul case 'p': 19926236Swpaul yp_dir = optarg; 20026236Swpaul break; 20126236Swpaul default: 20226236Swpaul usage(); 20326236Swpaul break; 20426236Swpaul } 20526236Swpaul } 20626236Swpaul#ifdef foo 20726236Swpaul load_securenets(); 20826236Swpaul#endif 20926236Swpaul 21026236Swpaul if (svc_auth_reg(AUTH_DES, _svcauth_des) == -1) { 21126236Swpaul yp_error("failed to register AUTH_DES flavor"); 21226236Swpaul exit(1); 21326236Swpaul } 21426236Swpaul 21526236Swpaul if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) { 21626236Swpaul int ssize = sizeof (int); 21726236Swpaul 21826236Swpaul if (saddr.sin_family != AF_INET) 21926236Swpaul exit(1); 22026236Swpaul if (getsockopt(0, SOL_SOCKET, SO_TYPE, 22126236Swpaul (char *)&_rpcfdtype, &ssize) == -1) 22226236Swpaul exit(1); 22326236Swpaul sock = 0; 22426236Swpaul _rpcpmstart = 1; 22526236Swpaul proto = 0; 22626236Swpaul openlog("rpc.ypupdatedd", LOG_PID, LOG_DAEMON); 22726236Swpaul } else { 22826236Swpaul#ifndef RPC_SVC_FG 22926236Swpaul if (daemon(0,0)) { 23026236Swpaul err(1, "cannot fork"); 23126236Swpaul } 23226236Swpaul openlog("rpc.ypupdated", LOG_PID, LOG_DAEMON); 23326236Swpaul#endif 23426236Swpaul sock = RPC_ANYSOCK; 23526236Swpaul (void) pmap_unset(YPU_PROG, YPU_VERS); 23626236Swpaul } 23726236Swpaul 23826236Swpaul if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) { 23926236Swpaul transp = svcudp_create(sock); 24026236Swpaul if (transp == NULL) { 24126236Swpaul _msgout("cannot create udp service."); 24226236Swpaul exit(1); 24326236Swpaul } 24426236Swpaul if (!_rpcpmstart) 24526236Swpaul proto = IPPROTO_UDP; 24626236Swpaul if (!svc_register(transp, YPU_PROG, YPU_VERS, ypu_prog_1, proto)) { 24726236Swpaul _msgout("unable to register (YPU_PROG, YPU_VERS, udp)."); 24826236Swpaul exit(1); 24926236Swpaul } 25026236Swpaul } 25126236Swpaul 25226236Swpaul if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) { 25326236Swpaul transp = svctcp_create(sock, 0, 0); 25426236Swpaul if (transp == NULL) { 25526236Swpaul _msgout("cannot create tcp service."); 25626236Swpaul exit(1); 25726236Swpaul } 25826236Swpaul if (!_rpcpmstart) 25926236Swpaul proto = IPPROTO_TCP; 26026236Swpaul if (!svc_register(transp, YPU_PROG, YPU_VERS, ypu_prog_1, proto)) { 26126236Swpaul _msgout("unable to register (YPU_PROG, YPU_VERS, tcp)."); 26226236Swpaul exit(1); 26326236Swpaul } 26426236Swpaul } 26526236Swpaul 26626236Swpaul if (transp == (SVCXPRT *)NULL) { 26726236Swpaul _msgout("could not create a handle"); 26826236Swpaul exit(1); 26926236Swpaul } 27026236Swpaul if (_rpcpmstart) { 27126236Swpaul (void) signal(SIGALRM, (SIG_PF) closedown); 27226236Swpaul (void) alarm(_RPCSVC_CLOSEDOWN/2); 27326236Swpaul } 27426236Swpaul 27526236Swpaul (void) signal(SIGPIPE, SIG_IGN); 27626236Swpaul (void) signal(SIGCHLD, (SIG_PF) reaper); 27726236Swpaul (void) signal(SIGTERM, (SIG_PF) reaper); 27826236Swpaul (void) signal(SIGINT, (SIG_PF) reaper); 27926236Swpaul (void) signal(SIGHUP, (SIG_PF) reaper); 28026236Swpaul 28126236Swpaul ypupdated_svc_run(); 28226236Swpaul _msgout("svc_run returned"); 28326236Swpaul exit(1); 28426236Swpaul /* NOTREACHED */ 28526236Swpaul} 286