xref: /freebsd/usr.sbin/rpc.yppasswdd/yppasswdd_main.c (revision 4d65a7c6951cea0333f1a0c1b32c38489cdfa6c5)
18256fad9SBill Paul /*
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
48256fad9SBill Paul  * Copyright (c) 1995, 1996
58256fad9SBill Paul  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
68256fad9SBill Paul  *
78256fad9SBill Paul  * Redistribution and use in source and binary forms, with or without
88256fad9SBill Paul  * modification, are permitted provided that the following conditions
98256fad9SBill Paul  * are met:
108256fad9SBill Paul  * 1. Redistributions of source code must retain the above copyright
118256fad9SBill Paul  *    notice, this list of conditions and the following disclaimer.
128256fad9SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
138256fad9SBill Paul  *    notice, this list of conditions and the following disclaimer in the
148256fad9SBill Paul  *    documentation and/or other materials provided with the distribution.
158256fad9SBill Paul  * 3. All advertising materials mentioning features or use of this software
168256fad9SBill Paul  *    must display the following acknowledgement:
178256fad9SBill Paul  *	This product includes software developed by Bill Paul.
188256fad9SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
198256fad9SBill Paul  *    may be used to endorse or promote products derived from this software
208256fad9SBill Paul  *    without specific prior written permission.
218256fad9SBill Paul  *
228256fad9SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
238256fad9SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
248256fad9SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
258256fad9SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
268256fad9SBill Paul  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
278256fad9SBill Paul  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
288256fad9SBill Paul  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
298256fad9SBill Paul  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
308256fad9SBill Paul  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
318256fad9SBill Paul  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
328256fad9SBill Paul  * SUCH DAMAGE.
338256fad9SBill Paul  */
348256fad9SBill Paul 
358256fad9SBill Paul #include <sys/param.h>
368256fad9SBill Paul #include <sys/ioctl.h>
378256fad9SBill Paul #include <sys/stat.h>
388256fad9SBill Paul #include <sys/socket.h>
39ba1556b5SDag-Erling Smørgrav #include <sys/time.h>
40ba1556b5SDag-Erling Smørgrav #include <sys/resource.h>
418256fad9SBill Paul #include <netinet/in.h>
42ba1556b5SDag-Erling Smørgrav 
438256fad9SBill Paul #include <err.h>
448256fad9SBill Paul #include <errno.h>
45ba1556b5SDag-Erling Smørgrav #include <fcntl.h>
46ba1556b5SDag-Erling Smørgrav #include <memory.h>
47ba1556b5SDag-Erling Smørgrav #include <signal.h>
48ba1556b5SDag-Erling Smørgrav #include <stdio.h>
49ba1556b5SDag-Erling Smørgrav #include <stdlib.h> /* getenv, exit */
50ba1556b5SDag-Erling Smørgrav #include <string.h> /* strcmp */
51ba1556b5SDag-Erling Smørgrav #include <syslog.h>
52ba1556b5SDag-Erling Smørgrav #include <unistd.h>
53ba1556b5SDag-Erling Smørgrav 
54ba1556b5SDag-Erling Smørgrav #include <rpc/rpc.h>
55bcb53b16SMartin Blapp #include <rpc/rpc_com.h>
56ba1556b5SDag-Erling Smørgrav #include <rpc/pmap_clnt.h> /* for pmap_unset */
578256fad9SBill Paul #include <rpcsvc/yp.h>
588256fad9SBill Paul #include <rpcsvc/ypclnt.h>
59ba1556b5SDag-Erling Smørgrav 
60ba1556b5SDag-Erling Smørgrav #include "yppasswd.h"
618256fad9SBill Paul #include "yppasswdd_extern.h"
621f922405SBill Paul #include "yppasswd_private.h"
638256fad9SBill Paul #include "ypxfr_extern.h"
64ba1556b5SDag-Erling Smørgrav #include "yp_extern.h"
658256fad9SBill Paul 
668256fad9SBill Paul #ifndef SIG_PF
678256fad9SBill Paul #define	SIG_PF void(*)(int)
688256fad9SBill Paul #endif
698256fad9SBill Paul 
708256fad9SBill Paul #ifdef DEBUG
718256fad9SBill Paul #define	RPC_SVC_FG
728256fad9SBill Paul #endif
738256fad9SBill Paul 
748256fad9SBill Paul #define	_RPCSVC_CLOSEDOWN 120
758256fad9SBill Paul int _rpcpmstart = 0;		/* Started by a port monitor ? */
768256fad9SBill Paul static int _rpcfdtype;
778256fad9SBill Paul 		 /* Whether Stream or Datagram ? */
788256fad9SBill Paul 	/* States a server can be in wrt request */
798256fad9SBill Paul 
808256fad9SBill Paul #define	_IDLE 0
818256fad9SBill Paul #define	_SERVED 1
828256fad9SBill Paul #define	_SERVING 2
83*3a166b33SKyle Evans int debug;
848256fad9SBill Paul 
85ba1556b5SDag-Erling Smørgrav static char _localhost[] = "localhost";
86ba1556b5SDag-Erling Smørgrav static char _passwd_byname[] = "passwd.byname";
878256fad9SBill Paul extern int _rpcsvcstate;	 /* Set when a request is serviced */
88ba1556b5SDag-Erling Smørgrav static char _progname[] = "rpc.yppasswdd";
89ba1556b5SDag-Erling Smørgrav char *progname = _progname;
90ba1556b5SDag-Erling Smørgrav static char _yp_dir[] = _PATH_YP;
91ba1556b5SDag-Erling Smørgrav char *yp_dir = _yp_dir;
92ba1556b5SDag-Erling Smørgrav static char _passfile_default[] = _PATH_YP "master.passwd";
93ba1556b5SDag-Erling Smørgrav char *passfile_default = _passfile_default;
948256fad9SBill Paul char *passfile;
958256fad9SBill Paul char *yppasswd_domain = NULL;
968256fad9SBill Paul int no_chsh = 0;
978256fad9SBill Paul int no_chfn = 0;
988256fad9SBill Paul int allow_additions = 0;
998256fad9SBill Paul int multidomain = 0;
1008256fad9SBill Paul int verbose = 0;
101589b8bfcSBill Paul int resvport = 1;
1028b6a78c2SBill Paul int inplace = 0;
103ba1556b5SDag-Erling Smørgrav char sockname[] = YP_SOCKNAME;
1048256fad9SBill Paul 
105dc584ddbSDag-Erling Smørgrav static void
terminate(int sig __unused)106ba1556b5SDag-Erling Smørgrav terminate(int sig __unused)
1078256fad9SBill Paul {
10826d0e65fSAlfred Perlstein 	rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL);
10926d0e65fSAlfred Perlstein 	rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL);
1108256fad9SBill Paul 	unlink(sockname);
1118256fad9SBill Paul 	exit(0);
1128256fad9SBill Paul }
1138256fad9SBill Paul 
114dc584ddbSDag-Erling Smørgrav static void
reload(int sig __unused)115ba1556b5SDag-Erling Smørgrav reload(int sig __unused)
116589b8bfcSBill Paul {
117589b8bfcSBill Paul 	load_securenets();
118589b8bfcSBill Paul }
119589b8bfcSBill Paul 
1208256fad9SBill Paul static void
closedown(int sig __unused)121ba1556b5SDag-Erling Smørgrav closedown(int sig __unused)
1228256fad9SBill Paul {
1238256fad9SBill Paul 	if (_rpcsvcstate == _IDLE) {
1248256fad9SBill Paul 		extern fd_set svc_fdset;
1258256fad9SBill Paul 		static int size;
1268256fad9SBill Paul 		int i, openfd;
1278256fad9SBill Paul 
1288256fad9SBill Paul 		if (_rpcfdtype == SOCK_DGRAM) {
1298256fad9SBill Paul 			unlink(sockname);
1308256fad9SBill Paul 			exit(0);
1318256fad9SBill Paul 		}
1328256fad9SBill Paul 		if (size == 0) {
1338256fad9SBill Paul 			size = getdtablesize();
1348256fad9SBill Paul 		}
1358256fad9SBill Paul 		for (i = 0, openfd = 0; i < size && openfd < 2; i++)
1368256fad9SBill Paul 			if (FD_ISSET(i, &svc_fdset))
1378256fad9SBill Paul 				openfd++;
1388256fad9SBill Paul 		if (openfd <= 1) {
1398256fad9SBill Paul 			unlink(sockname);
1408256fad9SBill Paul 			exit(0);
1418256fad9SBill Paul 		}
1428256fad9SBill Paul 	}
1438256fad9SBill Paul 	if (_rpcsvcstate == _SERVED)
1448256fad9SBill Paul 		_rpcsvcstate = _IDLE;
1458256fad9SBill Paul 
1468256fad9SBill Paul 	(void) signal(SIGALRM, (SIG_PF) closedown);
1478256fad9SBill Paul 	(void) alarm(_RPCSVC_CLOSEDOWN/2);
1488256fad9SBill Paul }
1498256fad9SBill Paul 
150dc584ddbSDag-Erling Smørgrav static void
usage(void)151dc584ddbSDag-Erling Smørgrav usage(void)
1528256fad9SBill Paul {
153fdd83901SPhilippe Charnier 	fprintf(stderr, "%s\n%s\n",
154fdd83901SPhilippe Charnier "usage: rpc.yppasswdd [-t master.passwd file] [-d domain] [-p path] [-s]",
155fdd83901SPhilippe Charnier "                     [-f] [-m] [-i] [-a] [-v] [-u] [-h]");
1568256fad9SBill Paul 	exit(1);
1578256fad9SBill Paul }
1588256fad9SBill Paul 
159d21fb5dcSPeter Wemm int
main(int argc,char * argv[])160dc584ddbSDag-Erling Smørgrav main(int argc, char *argv[])
1618256fad9SBill Paul {
162ba1556b5SDag-Erling Smørgrav 	struct rlimit rlim;
163ba1556b5SDag-Erling Smørgrav 	SVCXPRT *transp = NULL;
1648256fad9SBill Paul 	struct sockaddr_in saddr;
1658516925dSXin LI 	socklen_t asize = sizeof (saddr);
16626d0e65fSAlfred Perlstein 	struct netconfig *nconf;
16728e4b62bSRong-En Fan 	struct sigaction sa;
16826d0e65fSAlfred Perlstein 	void *localhandle;
1698256fad9SBill Paul 	int ch;
1708256fad9SBill Paul 	char *mastername;
1718256fad9SBill Paul 	char myname[MAXHOSTNAMELEN + 2];
172bcb53b16SMartin Blapp 	int maxrec = RPC_MAXDATASIZE;
17326d0e65fSAlfred Perlstein 
1748256fad9SBill Paul 	debug = 1;
1758256fad9SBill Paul 
1766c3f552aSWarner Losh 	while ((ch = getopt(argc, argv, "t:d:p:sfamuivh")) != -1) {
1778256fad9SBill Paul 		switch (ch) {
1788256fad9SBill Paul 		case 't':
1798256fad9SBill Paul 			passfile_default = optarg;
1808256fad9SBill Paul 			break;
1818256fad9SBill Paul 		case 'd':
1828256fad9SBill Paul 			yppasswd_domain = optarg;
1838256fad9SBill Paul 			break;
1848256fad9SBill Paul 		case 's':
1858256fad9SBill Paul 			no_chsh++;
1868256fad9SBill Paul 			break;
1878256fad9SBill Paul 		case 'f':
1888256fad9SBill Paul 			no_chfn++;
1898256fad9SBill Paul 			break;
1908256fad9SBill Paul 		case 'p':
1918256fad9SBill Paul 			yp_dir = optarg;
1928256fad9SBill Paul 			break;
1938256fad9SBill Paul 		case 'a':
1948256fad9SBill Paul 			allow_additions++;
1958256fad9SBill Paul 			break;
1968256fad9SBill Paul 		case 'm':
1978256fad9SBill Paul 			multidomain++;
1988256fad9SBill Paul 			break;
1998b6a78c2SBill Paul 		case 'i':
2008b6a78c2SBill Paul 			inplace++;
2018b6a78c2SBill Paul 			break;
2028256fad9SBill Paul 		case 'v':
2038256fad9SBill Paul 			verbose++;
2048256fad9SBill Paul 			break;
205589b8bfcSBill Paul 		case 'u':
206589b8bfcSBill Paul 			resvport = 0;
207589b8bfcSBill Paul 			break;
2088256fad9SBill Paul 		default:
2098256fad9SBill Paul 		case 'h':
2108256fad9SBill Paul 			usage();
2118256fad9SBill Paul 			break;
2128256fad9SBill Paul 		}
2138256fad9SBill Paul 	}
2148256fad9SBill Paul 
2158256fad9SBill Paul 	if (yppasswd_domain == NULL) {
2168256fad9SBill Paul 		if (yp_get_default_domain(&yppasswd_domain)) {
2178256fad9SBill Paul 			yp_error("no domain specified and system domain \
2188256fad9SBill Paul name isn't set -- aborting");
2198256fad9SBill Paul 		usage();
2208256fad9SBill Paul 		}
2218256fad9SBill Paul 	}
2228256fad9SBill Paul 
223589b8bfcSBill Paul 	load_securenets();
224589b8bfcSBill Paul 
225ba1556b5SDag-Erling Smørgrav 	if (getrpcport(_localhost, YPPROG, YPVERS, IPPROTO_UDP) <= 0) {
226511db384SBill Paul 		yp_error("no ypserv processes registered with local portmap");
2278256fad9SBill Paul 		yp_error("this host is not an NIS server -- aborting");
2288256fad9SBill Paul 		exit(1);
2298256fad9SBill Paul 	}
2308256fad9SBill Paul 
231ba1556b5SDag-Erling Smørgrav 	if ((mastername = ypxfr_get_master(yppasswd_domain,
232ba1556b5SDag-Erling Smørgrav 		 _passwd_byname, _localhost, 0)) == NULL) {
233511db384SBill Paul 		yp_error("can't get name of NIS master server for domain %s",
234511db384SBill Paul 			 				yppasswd_domain);
2358256fad9SBill Paul 		exit(1);
2368256fad9SBill Paul 	}
2378256fad9SBill Paul 
2388256fad9SBill Paul 	if (gethostname((char *)&myname, sizeof(myname)) == -1) {
2398256fad9SBill Paul 		yp_error("can't get local hostname: %s", strerror(errno));
2408256fad9SBill Paul 		exit(1);
2418256fad9SBill Paul 	}
2428256fad9SBill Paul 
2437cf0cfedSBrian Somers 	if (strncasecmp(mastername, (char *)&myname, sizeof(myname))) {
244511db384SBill Paul 		yp_error("master of %s is %s, but we are %s",
245511db384SBill Paul 			"passwd.byname", mastername, myname);
246511db384SBill Paul 		yp_error("this host is not the NIS master server for \
247511db384SBill Paul the %s domain -- aborting", yppasswd_domain);
2488256fad9SBill Paul 		exit(1);
2498256fad9SBill Paul 	}
2508256fad9SBill Paul 
251589b8bfcSBill Paul 	debug = 0;
252589b8bfcSBill Paul 
2538256fad9SBill Paul 	if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
2548516925dSXin LI 		socklen_t ssize = sizeof (int);
2558256fad9SBill Paul 		if (saddr.sin_family != AF_INET)
2568256fad9SBill Paul 			exit(1);
2578256fad9SBill Paul 		if (getsockopt(0, SOL_SOCKET, SO_TYPE,
2588256fad9SBill Paul 		    (char *)&_rpcfdtype, &ssize) == -1)
2598256fad9SBill Paul 			exit(1);
2608256fad9SBill Paul 		_rpcpmstart = 1;
26126d0e65fSAlfred Perlstein 	}
26226d0e65fSAlfred Perlstein 
26326d0e65fSAlfred Perlstein 	if (!debug && _rpcpmstart == 0) {
264df4ddde7SBill Paul 		if (daemon(0,0)) {
265df4ddde7SBill Paul 			err(1,"cannot fork");
2668256fad9SBill Paul 		}
2678256fad9SBill Paul 	}
268fdd83901SPhilippe Charnier 	openlog("rpc.yppasswdd", LOG_PID, LOG_DAEMON);
26928e4b62bSRong-En Fan 	memset(&sa, 0, sizeof(sa));
27028e4b62bSRong-En Fan 	sa.sa_flags = SA_NOCLDWAIT;
27128e4b62bSRong-En Fan 	sigaction(SIGCHLD, &sa, NULL);
2728256fad9SBill Paul 
27326d0e65fSAlfred Perlstein 	rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL);
27426d0e65fSAlfred Perlstein 	rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL);
2758256fad9SBill Paul 
276bcb53b16SMartin Blapp 	rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
277bcb53b16SMartin Blapp 
27826d0e65fSAlfred Perlstein 	if (svc_create(yppasswdprog_1, YPPASSWDPROG, YPPASSWDVERS, "netpath") == 0) {
27926d0e65fSAlfred Perlstein 		yp_error("cannot create yppasswd service.");
2808360efbdSAlfred Perlstein 		exit(1);
2818360efbdSAlfred Perlstein 	}
2828360efbdSAlfred Perlstein 	if (svc_create(master_yppasswdprog_1, MASTER_YPPASSWDPROG,
2838360efbdSAlfred Perlstein 	    MASTER_YPPASSWDVERS, "netpath") == 0) {
28426d0e65fSAlfred Perlstein 		yp_error("cannot create master_yppasswd service.");
2858360efbdSAlfred Perlstein 		exit(1);
2868360efbdSAlfred Perlstein 	}
28726d0e65fSAlfred Perlstein 
28826d0e65fSAlfred Perlstein 	nconf = NULL;
28926d0e65fSAlfred Perlstein 	localhandle = setnetconfig();
29026d0e65fSAlfred Perlstein 	while ((nconf = getnetconfig(localhandle)) != NULL) {
29126d0e65fSAlfred Perlstein 		if (nconf->nc_protofmly != NULL &&
29226d0e65fSAlfred Perlstein 		    strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
29326d0e65fSAlfred Perlstein 			break;
29426d0e65fSAlfred Perlstein 	}
29526d0e65fSAlfred Perlstein 	if (nconf == NULL) {
29626d0e65fSAlfred Perlstein 		yp_error("getnetconfigent unix: %s", nc_sperror());
29726d0e65fSAlfred Perlstein 		exit(1);
29826d0e65fSAlfred Perlstein 	}
29926d0e65fSAlfred Perlstein 	unlink(sockname);
30026d0e65fSAlfred Perlstein 	transp = svcunix_create(RPC_ANYSOCK, 0, 0, sockname);
3011f922405SBill Paul 	if (transp == NULL) {
3021f922405SBill Paul 		yp_error("cannot create AF_LOCAL service.");
3031f922405SBill Paul 		exit(1);
3041f922405SBill Paul 	}
30526d0e65fSAlfred Perlstein 	if (!svc_reg(transp, MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS,
30626d0e65fSAlfred Perlstein 	    master_yppasswdprog_1, nconf)) {
30740e7772bSAlfred Perlstein 		yp_error("unable to register (MASTER_YPPASSWDPROG, \
30826d0e65fSAlfred Perlstein 		    MASTER_YPPASSWDVERS, unix).");
3091f922405SBill Paul 		exit(1);
3101f922405SBill Paul 	}
31126d0e65fSAlfred Perlstein 	endnetconfig(localhandle);
31226d0e65fSAlfred Perlstein 
3131f922405SBill Paul 	/* Only root may connect() to the AF_UNIX link. */
3141f922405SBill Paul 	if (chmod(sockname, 0))
3151f922405SBill Paul 		err(1, "chmod of %s failed", sockname);
3161f922405SBill Paul 
3178256fad9SBill Paul 	if (transp == (SVCXPRT *)NULL) {
3188256fad9SBill Paul 		yp_error("could not create a handle");
3198256fad9SBill Paul 		exit(1);
3208256fad9SBill Paul 	}
3218256fad9SBill Paul 	if (_rpcpmstart) {
3228256fad9SBill Paul 		(void) signal(SIGALRM, (SIG_PF) closedown);
3238256fad9SBill Paul 		(void) alarm(_RPCSVC_CLOSEDOWN/2);
3248256fad9SBill Paul 	}
3258256fad9SBill Paul 
326ba1556b5SDag-Erling Smørgrav 	/* Unlimited resource limits. */
327ba1556b5SDag-Erling Smørgrav 	rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
328ba1556b5SDag-Erling Smørgrav 	(void)setrlimit(RLIMIT_CPU, &rlim);
329ba1556b5SDag-Erling Smørgrav 	(void)setrlimit(RLIMIT_FSIZE, &rlim);
330ba1556b5SDag-Erling Smørgrav 	(void)setrlimit(RLIMIT_STACK, &rlim);
331ba1556b5SDag-Erling Smørgrav 	(void)setrlimit(RLIMIT_DATA, &rlim);
332ba1556b5SDag-Erling Smørgrav 	(void)setrlimit(RLIMIT_RSS, &rlim);
3338256fad9SBill Paul 
334ba1556b5SDag-Erling Smørgrav 	/* Don't drop core (not really necessary, but GP's). */
335ba1556b5SDag-Erling Smørgrav 	rlim.rlim_cur = rlim.rlim_max = 0;
336ba1556b5SDag-Erling Smørgrav 	(void)setrlimit(RLIMIT_CORE, &rlim);
337ba1556b5SDag-Erling Smørgrav 
338ba1556b5SDag-Erling Smørgrav 	/* Turn off signals. */
339ba1556b5SDag-Erling Smørgrav 	(void)signal(SIGALRM, SIG_IGN);
340ba1556b5SDag-Erling Smørgrav 	(void)signal(SIGHUP, (SIG_PF) reload);
341ba1556b5SDag-Erling Smørgrav 	(void)signal(SIGINT, SIG_IGN);
342ba1556b5SDag-Erling Smørgrav 	(void)signal(SIGPIPE, SIG_IGN);
343ba1556b5SDag-Erling Smørgrav 	(void)signal(SIGQUIT, SIG_IGN);
344ba1556b5SDag-Erling Smørgrav 	(void)signal(SIGTERM, (SIG_PF) terminate);
345589b8bfcSBill Paul 
3461f922405SBill Paul 	svc_run();
3478256fad9SBill Paul 	yp_error("svc_run returned");
3488256fad9SBill Paul 	exit(1);
3498256fad9SBill Paul 	/* NOTREACHED */
3508256fad9SBill Paul }
351