18256fad9SBill Paul /* 28256fad9SBill Paul * Copyright (c) 1995, 1996 38256fad9SBill Paul * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 48256fad9SBill Paul * 58256fad9SBill Paul * Redistribution and use in source and binary forms, with or without 68256fad9SBill Paul * modification, are permitted provided that the following conditions 78256fad9SBill Paul * are met: 88256fad9SBill Paul * 1. Redistributions of source code must retain the above copyright 98256fad9SBill Paul * notice, this list of conditions and the following disclaimer. 108256fad9SBill Paul * 2. Redistributions in binary form must reproduce the above copyright 118256fad9SBill Paul * notice, this list of conditions and the following disclaimer in the 128256fad9SBill Paul * documentation and/or other materials provided with the distribution. 138256fad9SBill Paul * 3. All advertising materials mentioning features or use of this software 148256fad9SBill Paul * must display the following acknowledgement: 158256fad9SBill Paul * This product includes software developed by Bill Paul. 168256fad9SBill Paul * 4. Neither the name of the author nor the names of any co-contributors 178256fad9SBill Paul * may be used to endorse or promote products derived from this software 188256fad9SBill Paul * without specific prior written permission. 198256fad9SBill Paul * 208256fad9SBill Paul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 218256fad9SBill Paul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 228256fad9SBill Paul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 238256fad9SBill Paul * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE 248256fad9SBill Paul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 258256fad9SBill Paul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 268256fad9SBill Paul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 278256fad9SBill Paul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 288256fad9SBill Paul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 298256fad9SBill Paul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 308256fad9SBill Paul * SUCH DAMAGE. 318256fad9SBill Paul */ 328256fad9SBill Paul 33b728350eSDavid E. O'Brien #include <sys/cdefs.h> 34b728350eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 35fdd83901SPhilippe Charnier 368256fad9SBill Paul #include <sys/param.h> 378256fad9SBill Paul #include <sys/ioctl.h> 388256fad9SBill Paul #include <sys/stat.h> 398256fad9SBill Paul #include <sys/socket.h> 40ba1556b5SDag-Erling Smørgrav #include <sys/time.h> 41ba1556b5SDag-Erling Smørgrav #include <sys/resource.h> 428256fad9SBill Paul #include <netinet/in.h> 43ba1556b5SDag-Erling Smørgrav 448256fad9SBill Paul #include <err.h> 458256fad9SBill Paul #include <errno.h> 46ba1556b5SDag-Erling Smørgrav #include <fcntl.h> 47ba1556b5SDag-Erling Smørgrav #include <memory.h> 48ba1556b5SDag-Erling Smørgrav #include <signal.h> 49ba1556b5SDag-Erling Smørgrav #include <stdio.h> 50ba1556b5SDag-Erling Smørgrav #include <stdlib.h> /* getenv, exit */ 51ba1556b5SDag-Erling Smørgrav #include <string.h> 52ba1556b5SDag-Erling Smørgrav #include <string.h> /* strcmp */ 53ba1556b5SDag-Erling Smørgrav #include <syslog.h> 54ba1556b5SDag-Erling Smørgrav #include <unistd.h> 55ba1556b5SDag-Erling Smørgrav 56ba1556b5SDag-Erling Smørgrav #include <rpc/rpc.h> 57bcb53b16SMartin Blapp #include <rpc/rpc_com.h> 58ba1556b5SDag-Erling Smørgrav #include <rpc/pmap_clnt.h> /* for pmap_unset */ 598256fad9SBill Paul #include <rpcsvc/yp.h> 608256fad9SBill Paul #include <rpcsvc/ypclnt.h> 61ba1556b5SDag-Erling Smørgrav 62ba1556b5SDag-Erling Smørgrav #include "yppasswd.h" 638256fad9SBill Paul #include "yppasswdd_extern.h" 641f922405SBill Paul #include "yppasswd_private.h" 658256fad9SBill Paul #include "ypxfr_extern.h" 66ba1556b5SDag-Erling Smørgrav #include "yp_extern.h" 678256fad9SBill Paul 688256fad9SBill Paul #ifndef SIG_PF 698256fad9SBill Paul #define SIG_PF void(*)(int) 708256fad9SBill Paul #endif 718256fad9SBill Paul 728256fad9SBill Paul #ifdef DEBUG 738256fad9SBill Paul #define RPC_SVC_FG 748256fad9SBill Paul #endif 758256fad9SBill Paul 768256fad9SBill Paul #define _RPCSVC_CLOSEDOWN 120 778256fad9SBill Paul int _rpcpmstart = 0; /* Started by a port monitor ? */ 788256fad9SBill Paul static int _rpcfdtype; 798256fad9SBill Paul /* Whether Stream or Datagram ? */ 808256fad9SBill Paul /* States a server can be in wrt request */ 818256fad9SBill Paul 828256fad9SBill Paul #define _IDLE 0 838256fad9SBill Paul #define _SERVED 1 848256fad9SBill Paul #define _SERVING 2 858256fad9SBill Paul 86ba1556b5SDag-Erling Smørgrav static char _localhost[] = "localhost"; 87ba1556b5SDag-Erling Smørgrav static char _passwd_byname[] = "passwd.byname"; 888256fad9SBill Paul extern int _rpcsvcstate; /* Set when a request is serviced */ 89ba1556b5SDag-Erling Smørgrav static char _progname[] = "rpc.yppasswdd"; 90ba1556b5SDag-Erling Smørgrav char *progname = _progname; 91ba1556b5SDag-Erling Smørgrav static char _yp_dir[] = _PATH_YP; 92ba1556b5SDag-Erling Smørgrav char *yp_dir = _yp_dir; 93ba1556b5SDag-Erling Smørgrav static char _passfile_default[] = _PATH_YP "master.passwd"; 94ba1556b5SDag-Erling Smørgrav char *passfile_default = _passfile_default; 958256fad9SBill Paul char *passfile; 968256fad9SBill Paul char *yppasswd_domain = NULL; 978256fad9SBill Paul int no_chsh = 0; 988256fad9SBill Paul int no_chfn = 0; 998256fad9SBill Paul int allow_additions = 0; 1008256fad9SBill Paul int multidomain = 0; 1018256fad9SBill Paul int verbose = 0; 102589b8bfcSBill Paul int resvport = 1; 1038b6a78c2SBill Paul int inplace = 0; 104ba1556b5SDag-Erling Smørgrav char sockname[] = YP_SOCKNAME; 1058256fad9SBill Paul 106dc584ddbSDag-Erling Smørgrav static void 107ba1556b5SDag-Erling Smørgrav terminate(int sig __unused) 1088256fad9SBill Paul { 10926d0e65fSAlfred Perlstein rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL); 11026d0e65fSAlfred Perlstein rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL); 1118256fad9SBill Paul unlink(sockname); 1128256fad9SBill Paul exit(0); 1138256fad9SBill Paul } 1148256fad9SBill Paul 115dc584ddbSDag-Erling Smørgrav static void 116ba1556b5SDag-Erling Smørgrav reload(int sig __unused) 117589b8bfcSBill Paul { 118589b8bfcSBill Paul load_securenets(); 119589b8bfcSBill Paul } 120589b8bfcSBill Paul 1218256fad9SBill Paul static void 122ba1556b5SDag-Erling Smørgrav closedown(int sig __unused) 1238256fad9SBill Paul { 1248256fad9SBill Paul if (_rpcsvcstate == _IDLE) { 1258256fad9SBill Paul extern fd_set svc_fdset; 1268256fad9SBill Paul static int size; 1278256fad9SBill Paul int i, openfd; 1288256fad9SBill Paul 1298256fad9SBill Paul if (_rpcfdtype == SOCK_DGRAM) { 1308256fad9SBill Paul unlink(sockname); 1318256fad9SBill Paul exit(0); 1328256fad9SBill Paul } 1338256fad9SBill Paul if (size == 0) { 1348256fad9SBill Paul size = getdtablesize(); 1358256fad9SBill Paul } 1368256fad9SBill Paul for (i = 0, openfd = 0; i < size && openfd < 2; i++) 1378256fad9SBill Paul if (FD_ISSET(i, &svc_fdset)) 1388256fad9SBill Paul openfd++; 1398256fad9SBill Paul if (openfd <= 1) { 1408256fad9SBill Paul unlink(sockname); 1418256fad9SBill Paul exit(0); 1428256fad9SBill Paul } 1438256fad9SBill Paul } 1448256fad9SBill Paul if (_rpcsvcstate == _SERVED) 1458256fad9SBill Paul _rpcsvcstate = _IDLE; 1468256fad9SBill Paul 1478256fad9SBill Paul (void) signal(SIGALRM, (SIG_PF) closedown); 1488256fad9SBill Paul (void) alarm(_RPCSVC_CLOSEDOWN/2); 1498256fad9SBill Paul } 1508256fad9SBill Paul 151dc584ddbSDag-Erling Smørgrav static void 152dc584ddbSDag-Erling Smørgrav usage(void) 1538256fad9SBill Paul { 154fdd83901SPhilippe Charnier fprintf(stderr, "%s\n%s\n", 155fdd83901SPhilippe Charnier "usage: rpc.yppasswdd [-t master.passwd file] [-d domain] [-p path] [-s]", 156fdd83901SPhilippe Charnier " [-f] [-m] [-i] [-a] [-v] [-u] [-h]"); 1578256fad9SBill Paul exit(1); 1588256fad9SBill Paul } 1598256fad9SBill Paul 160d21fb5dcSPeter Wemm int 161dc584ddbSDag-Erling Smørgrav main(int argc, char *argv[]) 1628256fad9SBill Paul { 163ba1556b5SDag-Erling Smørgrav struct rlimit rlim; 164ba1556b5SDag-Erling Smørgrav SVCXPRT *transp = NULL; 1658256fad9SBill Paul struct sockaddr_in saddr; 1668516925dSXin LI socklen_t asize = sizeof (saddr); 16726d0e65fSAlfred Perlstein struct netconfig *nconf; 16828e4b62bSRong-En Fan struct sigaction sa; 16926d0e65fSAlfred Perlstein void *localhandle; 1708256fad9SBill Paul int ch; 1718256fad9SBill Paul char *mastername; 1728256fad9SBill Paul char myname[MAXHOSTNAMELEN + 2]; 173bcb53b16SMartin Blapp int maxrec = RPC_MAXDATASIZE; 17426d0e65fSAlfred Perlstein 1758256fad9SBill Paul extern int debug; 1768256fad9SBill Paul 1778256fad9SBill Paul debug = 1; 1788256fad9SBill Paul 1796c3f552aSWarner Losh while ((ch = getopt(argc, argv, "t:d:p:sfamuivh")) != -1) { 1808256fad9SBill Paul switch (ch) { 1818256fad9SBill Paul case 't': 1828256fad9SBill Paul passfile_default = optarg; 1838256fad9SBill Paul break; 1848256fad9SBill Paul case 'd': 1858256fad9SBill Paul yppasswd_domain = optarg; 1868256fad9SBill Paul break; 1878256fad9SBill Paul case 's': 1888256fad9SBill Paul no_chsh++; 1898256fad9SBill Paul break; 1908256fad9SBill Paul case 'f': 1918256fad9SBill Paul no_chfn++; 1928256fad9SBill Paul break; 1938256fad9SBill Paul case 'p': 1948256fad9SBill Paul yp_dir = optarg; 1958256fad9SBill Paul break; 1968256fad9SBill Paul case 'a': 1978256fad9SBill Paul allow_additions++; 1988256fad9SBill Paul break; 1998256fad9SBill Paul case 'm': 2008256fad9SBill Paul multidomain++; 2018256fad9SBill Paul break; 2028b6a78c2SBill Paul case 'i': 2038b6a78c2SBill Paul inplace++; 2048b6a78c2SBill Paul break; 2058256fad9SBill Paul case 'v': 2068256fad9SBill Paul verbose++; 2078256fad9SBill Paul break; 208589b8bfcSBill Paul case 'u': 209589b8bfcSBill Paul resvport = 0; 210589b8bfcSBill Paul break; 2118256fad9SBill Paul default: 2128256fad9SBill Paul case 'h': 2138256fad9SBill Paul usage(); 2148256fad9SBill Paul break; 2158256fad9SBill Paul } 2168256fad9SBill Paul } 2178256fad9SBill Paul 2188256fad9SBill Paul if (yppasswd_domain == NULL) { 2198256fad9SBill Paul if (yp_get_default_domain(&yppasswd_domain)) { 2208256fad9SBill Paul yp_error("no domain specified and system domain \ 2218256fad9SBill Paul name isn't set -- aborting"); 2228256fad9SBill Paul usage(); 2238256fad9SBill Paul } 2248256fad9SBill Paul } 2258256fad9SBill Paul 226589b8bfcSBill Paul load_securenets(); 227589b8bfcSBill Paul 228ba1556b5SDag-Erling Smørgrav if (getrpcport(_localhost, YPPROG, YPVERS, IPPROTO_UDP) <= 0) { 229511db384SBill Paul yp_error("no ypserv processes registered with local portmap"); 2308256fad9SBill Paul yp_error("this host is not an NIS server -- aborting"); 2318256fad9SBill Paul exit(1); 2328256fad9SBill Paul } 2338256fad9SBill Paul 234ba1556b5SDag-Erling Smørgrav if ((mastername = ypxfr_get_master(yppasswd_domain, 235ba1556b5SDag-Erling Smørgrav _passwd_byname, _localhost, 0)) == NULL) { 236511db384SBill Paul yp_error("can't get name of NIS master server for domain %s", 237511db384SBill Paul yppasswd_domain); 2388256fad9SBill Paul exit(1); 2398256fad9SBill Paul } 2408256fad9SBill Paul 2418256fad9SBill Paul if (gethostname((char *)&myname, sizeof(myname)) == -1) { 2428256fad9SBill Paul yp_error("can't get local hostname: %s", strerror(errno)); 2438256fad9SBill Paul exit(1); 2448256fad9SBill Paul } 2458256fad9SBill Paul 2467cf0cfedSBrian Somers if (strncasecmp(mastername, (char *)&myname, sizeof(myname))) { 247511db384SBill Paul yp_error("master of %s is %s, but we are %s", 248511db384SBill Paul "passwd.byname", mastername, myname); 249511db384SBill Paul yp_error("this host is not the NIS master server for \ 250511db384SBill Paul the %s domain -- aborting", yppasswd_domain); 2518256fad9SBill Paul exit(1); 2528256fad9SBill Paul } 2538256fad9SBill Paul 254589b8bfcSBill Paul debug = 0; 255589b8bfcSBill Paul 2568256fad9SBill Paul if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) { 2578516925dSXin LI socklen_t ssize = sizeof (int); 2588256fad9SBill Paul if (saddr.sin_family != AF_INET) 2598256fad9SBill Paul exit(1); 2608256fad9SBill Paul if (getsockopt(0, SOL_SOCKET, SO_TYPE, 2618256fad9SBill Paul (char *)&_rpcfdtype, &ssize) == -1) 2628256fad9SBill Paul exit(1); 2638256fad9SBill Paul _rpcpmstart = 1; 26426d0e65fSAlfred Perlstein } 26526d0e65fSAlfred Perlstein 26626d0e65fSAlfred Perlstein if (!debug && _rpcpmstart == 0) { 267df4ddde7SBill Paul if (daemon(0,0)) { 268df4ddde7SBill Paul err(1,"cannot fork"); 2698256fad9SBill Paul } 2708256fad9SBill Paul } 271fdd83901SPhilippe Charnier openlog("rpc.yppasswdd", LOG_PID, LOG_DAEMON); 27228e4b62bSRong-En Fan memset(&sa, 0, sizeof(sa)); 27328e4b62bSRong-En Fan sa.sa_flags = SA_NOCLDWAIT; 27428e4b62bSRong-En Fan sigaction(SIGCHLD, &sa, NULL); 2758256fad9SBill Paul 27626d0e65fSAlfred Perlstein rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL); 27726d0e65fSAlfred Perlstein rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL); 2788256fad9SBill Paul 279bcb53b16SMartin Blapp rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec); 280bcb53b16SMartin Blapp 28126d0e65fSAlfred Perlstein if (svc_create(yppasswdprog_1, YPPASSWDPROG, YPPASSWDVERS, "netpath") == 0) { 28226d0e65fSAlfred Perlstein yp_error("cannot create yppasswd service."); 2838360efbdSAlfred Perlstein exit(1); 2848360efbdSAlfred Perlstein } 2858360efbdSAlfred Perlstein if (svc_create(master_yppasswdprog_1, MASTER_YPPASSWDPROG, 2868360efbdSAlfred Perlstein MASTER_YPPASSWDVERS, "netpath") == 0) { 28726d0e65fSAlfred Perlstein yp_error("cannot create master_yppasswd service."); 2888360efbdSAlfred Perlstein exit(1); 2898360efbdSAlfred Perlstein } 29026d0e65fSAlfred Perlstein 29126d0e65fSAlfred Perlstein nconf = NULL; 29226d0e65fSAlfred Perlstein localhandle = setnetconfig(); 29326d0e65fSAlfred Perlstein while ((nconf = getnetconfig(localhandle)) != NULL) { 29426d0e65fSAlfred Perlstein if (nconf->nc_protofmly != NULL && 29526d0e65fSAlfred Perlstein strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) 29626d0e65fSAlfred Perlstein break; 29726d0e65fSAlfred Perlstein } 29826d0e65fSAlfred Perlstein if (nconf == NULL) { 29926d0e65fSAlfred Perlstein yp_error("getnetconfigent unix: %s", nc_sperror()); 30026d0e65fSAlfred Perlstein exit(1); 30126d0e65fSAlfred Perlstein } 30226d0e65fSAlfred Perlstein unlink(sockname); 30326d0e65fSAlfred Perlstein transp = svcunix_create(RPC_ANYSOCK, 0, 0, sockname); 3041f922405SBill Paul if (transp == NULL) { 3051f922405SBill Paul yp_error("cannot create AF_LOCAL service."); 3061f922405SBill Paul exit(1); 3071f922405SBill Paul } 30826d0e65fSAlfred Perlstein if (!svc_reg(transp, MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, 30926d0e65fSAlfred Perlstein master_yppasswdprog_1, nconf)) { 31040e7772bSAlfred Perlstein yp_error("unable to register (MASTER_YPPASSWDPROG, \ 31126d0e65fSAlfred Perlstein MASTER_YPPASSWDVERS, unix)."); 3121f922405SBill Paul exit(1); 3131f922405SBill Paul } 31426d0e65fSAlfred Perlstein endnetconfig(localhandle); 31526d0e65fSAlfred Perlstein 3161f922405SBill Paul /* Only root may connect() to the AF_UNIX link. */ 3171f922405SBill Paul if (chmod(sockname, 0)) 3181f922405SBill Paul err(1, "chmod of %s failed", sockname); 3191f922405SBill Paul 3208256fad9SBill Paul if (transp == (SVCXPRT *)NULL) { 3218256fad9SBill Paul yp_error("could not create a handle"); 3228256fad9SBill Paul exit(1); 3238256fad9SBill Paul } 3248256fad9SBill Paul if (_rpcpmstart) { 3258256fad9SBill Paul (void) signal(SIGALRM, (SIG_PF) closedown); 3268256fad9SBill Paul (void) alarm(_RPCSVC_CLOSEDOWN/2); 3278256fad9SBill Paul } 3288256fad9SBill Paul 329ba1556b5SDag-Erling Smørgrav /* Unlimited resource limits. */ 330ba1556b5SDag-Erling Smørgrav rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY; 331ba1556b5SDag-Erling Smørgrav (void)setrlimit(RLIMIT_CPU, &rlim); 332ba1556b5SDag-Erling Smørgrav (void)setrlimit(RLIMIT_FSIZE, &rlim); 333ba1556b5SDag-Erling Smørgrav (void)setrlimit(RLIMIT_STACK, &rlim); 334ba1556b5SDag-Erling Smørgrav (void)setrlimit(RLIMIT_DATA, &rlim); 335ba1556b5SDag-Erling Smørgrav (void)setrlimit(RLIMIT_RSS, &rlim); 3368256fad9SBill Paul 337ba1556b5SDag-Erling Smørgrav /* Don't drop core (not really necessary, but GP's). */ 338ba1556b5SDag-Erling Smørgrav rlim.rlim_cur = rlim.rlim_max = 0; 339ba1556b5SDag-Erling Smørgrav (void)setrlimit(RLIMIT_CORE, &rlim); 340ba1556b5SDag-Erling Smørgrav 341ba1556b5SDag-Erling Smørgrav /* Turn off signals. */ 342ba1556b5SDag-Erling Smørgrav (void)signal(SIGALRM, SIG_IGN); 343ba1556b5SDag-Erling Smørgrav (void)signal(SIGHUP, (SIG_PF) reload); 344ba1556b5SDag-Erling Smørgrav (void)signal(SIGINT, SIG_IGN); 345ba1556b5SDag-Erling Smørgrav (void)signal(SIGPIPE, SIG_IGN); 346ba1556b5SDag-Erling Smørgrav (void)signal(SIGQUIT, SIG_IGN); 347ba1556b5SDag-Erling Smørgrav (void)signal(SIGTERM, (SIG_PF) terminate); 348589b8bfcSBill Paul 3491f922405SBill Paul svc_run(); 3508256fad9SBill Paul yp_error("svc_run returned"); 3518256fad9SBill Paul exit(1); 3528256fad9SBill Paul /* NOTREACHED */ 3538256fad9SBill Paul } 354