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 33fdd83901SPhilippe Charnier #ifndef lint 34fdd83901SPhilippe Charnier static const char rcsid[] = 3597d92980SPeter Wemm "$FreeBSD$"; 36fdd83901SPhilippe Charnier #endif /* not lint */ 37fdd83901SPhilippe Charnier 388256fad9SBill Paul #include <sys/param.h> 398256fad9SBill Paul #include <sys/ioctl.h> 408256fad9SBill Paul #include <sys/stat.h> 418256fad9SBill Paul #include <sys/socket.h> 42ba1556b5SDag-Erling Smørgrav #include <sys/time.h> 43ba1556b5SDag-Erling Smørgrav #include <sys/resource.h> 448256fad9SBill Paul #include <netinet/in.h> 45ba1556b5SDag-Erling Smørgrav 468256fad9SBill Paul #include <err.h> 478256fad9SBill Paul #include <errno.h> 48ba1556b5SDag-Erling Smørgrav #include <fcntl.h> 49ba1556b5SDag-Erling Smørgrav #include <memory.h> 50ba1556b5SDag-Erling Smørgrav #include <signal.h> 51ba1556b5SDag-Erling Smørgrav #include <stdio.h> 52ba1556b5SDag-Erling Smørgrav #include <stdlib.h> /* getenv, exit */ 53ba1556b5SDag-Erling Smørgrav #include <string.h> 54ba1556b5SDag-Erling Smørgrav #include <string.h> /* strcmp */ 55ba1556b5SDag-Erling Smørgrav #include <syslog.h> 56ba1556b5SDag-Erling Smørgrav #include <unistd.h> 57ba1556b5SDag-Erling Smørgrav 58ba1556b5SDag-Erling Smørgrav #include <rpc/rpc.h> 59ba1556b5SDag-Erling Smørgrav #include <rpc/pmap_clnt.h> /* for pmap_unset */ 608256fad9SBill Paul #include <rpcsvc/yp.h> 618256fad9SBill Paul struct dom_binding {}; 628256fad9SBill Paul #include <rpcsvc/ypclnt.h> 63ba1556b5SDag-Erling Smørgrav 64ba1556b5SDag-Erling Smørgrav #include "yppasswd.h" 658256fad9SBill Paul #include "yppasswdd_extern.h" 661f922405SBill Paul #include "yppasswd_private.h" 678256fad9SBill Paul #include "ypxfr_extern.h" 68ba1556b5SDag-Erling Smørgrav #include "yp_extern.h" 698256fad9SBill Paul 708256fad9SBill Paul #ifndef SIG_PF 718256fad9SBill Paul #define SIG_PF void(*)(int) 728256fad9SBill Paul #endif 738256fad9SBill Paul 748256fad9SBill Paul #ifdef DEBUG 758256fad9SBill Paul #define RPC_SVC_FG 768256fad9SBill Paul #endif 778256fad9SBill Paul 788256fad9SBill Paul #define _RPCSVC_CLOSEDOWN 120 798256fad9SBill Paul int _rpcpmstart = 0; /* Started by a port monitor ? */ 808256fad9SBill Paul static int _rpcfdtype; 818256fad9SBill Paul /* Whether Stream or Datagram ? */ 828256fad9SBill Paul /* States a server can be in wrt request */ 838256fad9SBill Paul 848256fad9SBill Paul #define _IDLE 0 858256fad9SBill Paul #define _SERVED 1 868256fad9SBill Paul #define _SERVING 2 878256fad9SBill Paul 88ba1556b5SDag-Erling Smørgrav static char _localhost[] = "localhost"; 89ba1556b5SDag-Erling Smørgrav static char _passwd_byname[] = "passwd.byname"; 908256fad9SBill Paul extern int _rpcsvcstate; /* Set when a request is serviced */ 91ba1556b5SDag-Erling Smørgrav static char _progname[] = "rpc.yppasswdd"; 92ba1556b5SDag-Erling Smørgrav char *progname = _progname; 93ba1556b5SDag-Erling Smørgrav static char _yp_dir[] = _PATH_YP; 94ba1556b5SDag-Erling Smørgrav char *yp_dir = _yp_dir; 95ba1556b5SDag-Erling Smørgrav static char _passfile_default[] = _PATH_YP "master.passwd"; 96ba1556b5SDag-Erling Smørgrav char *passfile_default = _passfile_default; 978256fad9SBill Paul char *passfile; 988256fad9SBill Paul char *yppasswd_domain = NULL; 998256fad9SBill Paul int no_chsh = 0; 1008256fad9SBill Paul int no_chfn = 0; 1018256fad9SBill Paul int allow_additions = 0; 1028256fad9SBill Paul int multidomain = 0; 1038256fad9SBill Paul int verbose = 0; 104589b8bfcSBill Paul int resvport = 1; 1058b6a78c2SBill Paul int inplace = 0; 106ba1556b5SDag-Erling Smørgrav char sockname[] = YP_SOCKNAME; 1078256fad9SBill Paul 108dc584ddbSDag-Erling Smørgrav static void 109ba1556b5SDag-Erling Smørgrav terminate(int sig __unused) 1108256fad9SBill Paul { 11126d0e65fSAlfred Perlstein rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL); 11226d0e65fSAlfred Perlstein rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL); 1138256fad9SBill Paul unlink(sockname); 1148256fad9SBill Paul exit(0); 1158256fad9SBill Paul } 1168256fad9SBill Paul 117dc584ddbSDag-Erling Smørgrav static void 118ba1556b5SDag-Erling Smørgrav reload(int sig __unused) 119589b8bfcSBill Paul { 120589b8bfcSBill Paul load_securenets(); 121589b8bfcSBill Paul } 122589b8bfcSBill Paul 1238256fad9SBill Paul static void 124ba1556b5SDag-Erling Smørgrav closedown(int sig __unused) 1258256fad9SBill Paul { 1268256fad9SBill Paul if (_rpcsvcstate == _IDLE) { 1278256fad9SBill Paul extern fd_set svc_fdset; 1288256fad9SBill Paul static int size; 1298256fad9SBill Paul int i, openfd; 1308256fad9SBill Paul 1318256fad9SBill Paul if (_rpcfdtype == SOCK_DGRAM) { 1328256fad9SBill Paul unlink(sockname); 1338256fad9SBill Paul exit(0); 1348256fad9SBill Paul } 1358256fad9SBill Paul if (size == 0) { 1368256fad9SBill Paul size = getdtablesize(); 1378256fad9SBill Paul } 1388256fad9SBill Paul for (i = 0, openfd = 0; i < size && openfd < 2; i++) 1398256fad9SBill Paul if (FD_ISSET(i, &svc_fdset)) 1408256fad9SBill Paul openfd++; 1418256fad9SBill Paul if (openfd <= 1) { 1428256fad9SBill Paul unlink(sockname); 1438256fad9SBill Paul exit(0); 1448256fad9SBill Paul } 1458256fad9SBill Paul } 1468256fad9SBill Paul if (_rpcsvcstate == _SERVED) 1478256fad9SBill Paul _rpcsvcstate = _IDLE; 1488256fad9SBill Paul 1498256fad9SBill Paul (void) signal(SIGALRM, (SIG_PF) closedown); 1508256fad9SBill Paul (void) alarm(_RPCSVC_CLOSEDOWN/2); 1518256fad9SBill Paul } 1528256fad9SBill Paul 153dc584ddbSDag-Erling Smørgrav static void 154dc584ddbSDag-Erling Smørgrav usage(void) 1558256fad9SBill Paul { 156fdd83901SPhilippe Charnier fprintf(stderr, "%s\n%s\n", 157fdd83901SPhilippe Charnier "usage: rpc.yppasswdd [-t master.passwd file] [-d domain] [-p path] [-s]", 158fdd83901SPhilippe Charnier " [-f] [-m] [-i] [-a] [-v] [-u] [-h]"); 1598256fad9SBill Paul exit(1); 1608256fad9SBill Paul } 1618256fad9SBill Paul 162d21fb5dcSPeter Wemm int 163dc584ddbSDag-Erling Smørgrav main(int argc, char *argv[]) 1648256fad9SBill Paul { 165ba1556b5SDag-Erling Smørgrav struct rlimit rlim; 166ba1556b5SDag-Erling Smørgrav SVCXPRT *transp = NULL; 1678256fad9SBill Paul struct sockaddr_in saddr; 1688256fad9SBill Paul int asize = sizeof (saddr); 16926d0e65fSAlfred Perlstein struct netconfig *nconf; 17026d0e65fSAlfred Perlstein void *localhandle; 1718256fad9SBill Paul int ch; 1728256fad9SBill Paul char *mastername; 1738256fad9SBill Paul char myname[MAXHOSTNAMELEN + 2]; 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) { 2578256fad9SBill Paul int 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); 2728256fad9SBill Paul 27326d0e65fSAlfred Perlstein rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL); 27426d0e65fSAlfred Perlstein rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL); 2758256fad9SBill Paul 27626d0e65fSAlfred Perlstein if (svc_create(yppasswdprog_1, YPPASSWDPROG, YPPASSWDVERS, "netpath") == 0) { 27726d0e65fSAlfred Perlstein yp_error("cannot create yppasswd service."); 2788360efbdSAlfred Perlstein exit(1); 2798360efbdSAlfred Perlstein } 2808360efbdSAlfred Perlstein if (svc_create(master_yppasswdprog_1, MASTER_YPPASSWDPROG, 2818360efbdSAlfred Perlstein MASTER_YPPASSWDVERS, "netpath") == 0) { 28226d0e65fSAlfred Perlstein yp_error("cannot create master_yppasswd service."); 2838360efbdSAlfred Perlstein exit(1); 2848360efbdSAlfred Perlstein } 28526d0e65fSAlfred Perlstein 28626d0e65fSAlfred Perlstein nconf = NULL; 28726d0e65fSAlfred Perlstein localhandle = setnetconfig(); 28826d0e65fSAlfred Perlstein while ((nconf = getnetconfig(localhandle)) != NULL) { 28926d0e65fSAlfred Perlstein if (nconf->nc_protofmly != NULL && 29026d0e65fSAlfred Perlstein strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) 29126d0e65fSAlfred Perlstein break; 29226d0e65fSAlfred Perlstein } 29326d0e65fSAlfred Perlstein if (nconf == NULL) { 29426d0e65fSAlfred Perlstein yp_error("getnetconfigent unix: %s", nc_sperror()); 29526d0e65fSAlfred Perlstein exit(1); 29626d0e65fSAlfred Perlstein } 29726d0e65fSAlfred Perlstein unlink(sockname); 29826d0e65fSAlfred Perlstein transp = svcunix_create(RPC_ANYSOCK, 0, 0, sockname); 2991f922405SBill Paul if (transp == NULL) { 3001f922405SBill Paul yp_error("cannot create AF_LOCAL service."); 3011f922405SBill Paul exit(1); 3021f922405SBill Paul } 30326d0e65fSAlfred Perlstein if (!svc_reg(transp, MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, 30426d0e65fSAlfred Perlstein master_yppasswdprog_1, nconf)) { 30540e7772bSAlfred Perlstein yp_error("unable to register (MASTER_YPPASSWDPROG, \ 30626d0e65fSAlfred Perlstein MASTER_YPPASSWDVERS, unix)."); 3071f922405SBill Paul exit(1); 3081f922405SBill Paul } 30926d0e65fSAlfred Perlstein endnetconfig(localhandle); 31026d0e65fSAlfred Perlstein 3111f922405SBill Paul /* Only root may connect() to the AF_UNIX link. */ 3121f922405SBill Paul if (chmod(sockname, 0)) 3131f922405SBill Paul err(1, "chmod of %s failed", sockname); 3141f922405SBill Paul 3158256fad9SBill Paul if (transp == (SVCXPRT *)NULL) { 3168256fad9SBill Paul yp_error("could not create a handle"); 3178256fad9SBill Paul exit(1); 3188256fad9SBill Paul } 3198256fad9SBill Paul if (_rpcpmstart) { 3208256fad9SBill Paul (void) signal(SIGALRM, (SIG_PF) closedown); 3218256fad9SBill Paul (void) alarm(_RPCSVC_CLOSEDOWN/2); 3228256fad9SBill Paul } 3238256fad9SBill Paul 324ba1556b5SDag-Erling Smørgrav /* Unlimited resource limits. */ 325ba1556b5SDag-Erling Smørgrav rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY; 326ba1556b5SDag-Erling Smørgrav (void)setrlimit(RLIMIT_CPU, &rlim); 327ba1556b5SDag-Erling Smørgrav (void)setrlimit(RLIMIT_FSIZE, &rlim); 328ba1556b5SDag-Erling Smørgrav (void)setrlimit(RLIMIT_STACK, &rlim); 329ba1556b5SDag-Erling Smørgrav (void)setrlimit(RLIMIT_DATA, &rlim); 330ba1556b5SDag-Erling Smørgrav (void)setrlimit(RLIMIT_RSS, &rlim); 3318256fad9SBill Paul 332ba1556b5SDag-Erling Smørgrav /* Don't drop core (not really necessary, but GP's). */ 333ba1556b5SDag-Erling Smørgrav rlim.rlim_cur = rlim.rlim_max = 0; 334ba1556b5SDag-Erling Smørgrav (void)setrlimit(RLIMIT_CORE, &rlim); 335ba1556b5SDag-Erling Smørgrav 336ba1556b5SDag-Erling Smørgrav /* Turn off signals. */ 337ba1556b5SDag-Erling Smørgrav (void)signal(SIGALRM, SIG_IGN); 338ba1556b5SDag-Erling Smørgrav (void)signal(SIGHUP, (SIG_PF) reload); 339ba1556b5SDag-Erling Smørgrav (void)signal(SIGINT, SIG_IGN); 340ba1556b5SDag-Erling Smørgrav (void)signal(SIGPIPE, SIG_IGN); 341ba1556b5SDag-Erling Smørgrav (void)signal(SIGQUIT, SIG_IGN); 342ba1556b5SDag-Erling Smørgrav (void)signal(SIGTERM, (SIG_PF) terminate); 343589b8bfcSBill Paul 3441f922405SBill Paul svc_run(); 3458256fad9SBill Paul yp_error("svc_run returned"); 3468256fad9SBill Paul exit(1); 3478256fad9SBill Paul /* NOTREACHED */ 3488256fad9SBill Paul } 349