xref: /freebsd/usr.sbin/rpc.yppasswdd/yppasswdd_main.c (revision 4d65a7c6951cea0333f1a0c1b32c38489cdfa6c5)
1 /*
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1995, 1996
5  *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by Bill Paul.
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
37 #include <sys/stat.h>
38 #include <sys/socket.h>
39 #include <sys/time.h>
40 #include <sys/resource.h>
41 #include <netinet/in.h>
42 
43 #include <err.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <memory.h>
47 #include <signal.h>
48 #include <stdio.h>
49 #include <stdlib.h> /* getenv, exit */
50 #include <string.h> /* strcmp */
51 #include <syslog.h>
52 #include <unistd.h>
53 
54 #include <rpc/rpc.h>
55 #include <rpc/rpc_com.h>
56 #include <rpc/pmap_clnt.h> /* for pmap_unset */
57 #include <rpcsvc/yp.h>
58 #include <rpcsvc/ypclnt.h>
59 
60 #include "yppasswd.h"
61 #include "yppasswdd_extern.h"
62 #include "yppasswd_private.h"
63 #include "ypxfr_extern.h"
64 #include "yp_extern.h"
65 
66 #ifndef SIG_PF
67 #define	SIG_PF void(*)(int)
68 #endif
69 
70 #ifdef DEBUG
71 #define	RPC_SVC_FG
72 #endif
73 
74 #define	_RPCSVC_CLOSEDOWN 120
75 int _rpcpmstart = 0;		/* Started by a port monitor ? */
76 static int _rpcfdtype;
77 		 /* Whether Stream or Datagram ? */
78 	/* States a server can be in wrt request */
79 
80 #define	_IDLE 0
81 #define	_SERVED 1
82 #define	_SERVING 2
83 int debug;
84 
85 static char _localhost[] = "localhost";
86 static char _passwd_byname[] = "passwd.byname";
87 extern int _rpcsvcstate;	 /* Set when a request is serviced */
88 static char _progname[] = "rpc.yppasswdd";
89 char *progname = _progname;
90 static char _yp_dir[] = _PATH_YP;
91 char *yp_dir = _yp_dir;
92 static char _passfile_default[] = _PATH_YP "master.passwd";
93 char *passfile_default = _passfile_default;
94 char *passfile;
95 char *yppasswd_domain = NULL;
96 int no_chsh = 0;
97 int no_chfn = 0;
98 int allow_additions = 0;
99 int multidomain = 0;
100 int verbose = 0;
101 int resvport = 1;
102 int inplace = 0;
103 char sockname[] = YP_SOCKNAME;
104 
105 static void
terminate(int sig __unused)106 terminate(int sig __unused)
107 {
108 	rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL);
109 	rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL);
110 	unlink(sockname);
111 	exit(0);
112 }
113 
114 static void
reload(int sig __unused)115 reload(int sig __unused)
116 {
117 	load_securenets();
118 }
119 
120 static void
closedown(int sig __unused)121 closedown(int sig __unused)
122 {
123 	if (_rpcsvcstate == _IDLE) {
124 		extern fd_set svc_fdset;
125 		static int size;
126 		int i, openfd;
127 
128 		if (_rpcfdtype == SOCK_DGRAM) {
129 			unlink(sockname);
130 			exit(0);
131 		}
132 		if (size == 0) {
133 			size = getdtablesize();
134 		}
135 		for (i = 0, openfd = 0; i < size && openfd < 2; i++)
136 			if (FD_ISSET(i, &svc_fdset))
137 				openfd++;
138 		if (openfd <= 1) {
139 			unlink(sockname);
140 			exit(0);
141 		}
142 	}
143 	if (_rpcsvcstate == _SERVED)
144 		_rpcsvcstate = _IDLE;
145 
146 	(void) signal(SIGALRM, (SIG_PF) closedown);
147 	(void) alarm(_RPCSVC_CLOSEDOWN/2);
148 }
149 
150 static void
usage(void)151 usage(void)
152 {
153 	fprintf(stderr, "%s\n%s\n",
154 "usage: rpc.yppasswdd [-t master.passwd file] [-d domain] [-p path] [-s]",
155 "                     [-f] [-m] [-i] [-a] [-v] [-u] [-h]");
156 	exit(1);
157 }
158 
159 int
main(int argc,char * argv[])160 main(int argc, char *argv[])
161 {
162 	struct rlimit rlim;
163 	SVCXPRT *transp = NULL;
164 	struct sockaddr_in saddr;
165 	socklen_t asize = sizeof (saddr);
166 	struct netconfig *nconf;
167 	struct sigaction sa;
168 	void *localhandle;
169 	int ch;
170 	char *mastername;
171 	char myname[MAXHOSTNAMELEN + 2];
172 	int maxrec = RPC_MAXDATASIZE;
173 
174 	debug = 1;
175 
176 	while ((ch = getopt(argc, argv, "t:d:p:sfamuivh")) != -1) {
177 		switch (ch) {
178 		case 't':
179 			passfile_default = optarg;
180 			break;
181 		case 'd':
182 			yppasswd_domain = optarg;
183 			break;
184 		case 's':
185 			no_chsh++;
186 			break;
187 		case 'f':
188 			no_chfn++;
189 			break;
190 		case 'p':
191 			yp_dir = optarg;
192 			break;
193 		case 'a':
194 			allow_additions++;
195 			break;
196 		case 'm':
197 			multidomain++;
198 			break;
199 		case 'i':
200 			inplace++;
201 			break;
202 		case 'v':
203 			verbose++;
204 			break;
205 		case 'u':
206 			resvport = 0;
207 			break;
208 		default:
209 		case 'h':
210 			usage();
211 			break;
212 		}
213 	}
214 
215 	if (yppasswd_domain == NULL) {
216 		if (yp_get_default_domain(&yppasswd_domain)) {
217 			yp_error("no domain specified and system domain \
218 name isn't set -- aborting");
219 		usage();
220 		}
221 	}
222 
223 	load_securenets();
224 
225 	if (getrpcport(_localhost, YPPROG, YPVERS, IPPROTO_UDP) <= 0) {
226 		yp_error("no ypserv processes registered with local portmap");
227 		yp_error("this host is not an NIS server -- aborting");
228 		exit(1);
229 	}
230 
231 	if ((mastername = ypxfr_get_master(yppasswd_domain,
232 		 _passwd_byname, _localhost, 0)) == NULL) {
233 		yp_error("can't get name of NIS master server for domain %s",
234 			 				yppasswd_domain);
235 		exit(1);
236 	}
237 
238 	if (gethostname((char *)&myname, sizeof(myname)) == -1) {
239 		yp_error("can't get local hostname: %s", strerror(errno));
240 		exit(1);
241 	}
242 
243 	if (strncasecmp(mastername, (char *)&myname, sizeof(myname))) {
244 		yp_error("master of %s is %s, but we are %s",
245 			"passwd.byname", mastername, myname);
246 		yp_error("this host is not the NIS master server for \
247 the %s domain -- aborting", yppasswd_domain);
248 		exit(1);
249 	}
250 
251 	debug = 0;
252 
253 	if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
254 		socklen_t ssize = sizeof (int);
255 		if (saddr.sin_family != AF_INET)
256 			exit(1);
257 		if (getsockopt(0, SOL_SOCKET, SO_TYPE,
258 		    (char *)&_rpcfdtype, &ssize) == -1)
259 			exit(1);
260 		_rpcpmstart = 1;
261 	}
262 
263 	if (!debug && _rpcpmstart == 0) {
264 		if (daemon(0,0)) {
265 			err(1,"cannot fork");
266 		}
267 	}
268 	openlog("rpc.yppasswdd", LOG_PID, LOG_DAEMON);
269 	memset(&sa, 0, sizeof(sa));
270 	sa.sa_flags = SA_NOCLDWAIT;
271 	sigaction(SIGCHLD, &sa, NULL);
272 
273 	rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL);
274 	rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL);
275 
276 	rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
277 
278 	if (svc_create(yppasswdprog_1, YPPASSWDPROG, YPPASSWDVERS, "netpath") == 0) {
279 		yp_error("cannot create yppasswd service.");
280 		exit(1);
281 	}
282 	if (svc_create(master_yppasswdprog_1, MASTER_YPPASSWDPROG,
283 	    MASTER_YPPASSWDVERS, "netpath") == 0) {
284 		yp_error("cannot create master_yppasswd service.");
285 		exit(1);
286 	}
287 
288 	nconf = NULL;
289 	localhandle = setnetconfig();
290 	while ((nconf = getnetconfig(localhandle)) != NULL) {
291 		if (nconf->nc_protofmly != NULL &&
292 		    strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
293 			break;
294 	}
295 	if (nconf == NULL) {
296 		yp_error("getnetconfigent unix: %s", nc_sperror());
297 		exit(1);
298 	}
299 	unlink(sockname);
300 	transp = svcunix_create(RPC_ANYSOCK, 0, 0, sockname);
301 	if (transp == NULL) {
302 		yp_error("cannot create AF_LOCAL service.");
303 		exit(1);
304 	}
305 	if (!svc_reg(transp, MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS,
306 	    master_yppasswdprog_1, nconf)) {
307 		yp_error("unable to register (MASTER_YPPASSWDPROG, \
308 		    MASTER_YPPASSWDVERS, unix).");
309 		exit(1);
310 	}
311 	endnetconfig(localhandle);
312 
313 	/* Only root may connect() to the AF_UNIX link. */
314 	if (chmod(sockname, 0))
315 		err(1, "chmod of %s failed", sockname);
316 
317 	if (transp == (SVCXPRT *)NULL) {
318 		yp_error("could not create a handle");
319 		exit(1);
320 	}
321 	if (_rpcpmstart) {
322 		(void) signal(SIGALRM, (SIG_PF) closedown);
323 		(void) alarm(_RPCSVC_CLOSEDOWN/2);
324 	}
325 
326 	/* Unlimited resource limits. */
327 	rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
328 	(void)setrlimit(RLIMIT_CPU, &rlim);
329 	(void)setrlimit(RLIMIT_FSIZE, &rlim);
330 	(void)setrlimit(RLIMIT_STACK, &rlim);
331 	(void)setrlimit(RLIMIT_DATA, &rlim);
332 	(void)setrlimit(RLIMIT_RSS, &rlim);
333 
334 	/* Don't drop core (not really necessary, but GP's). */
335 	rlim.rlim_cur = rlim.rlim_max = 0;
336 	(void)setrlimit(RLIMIT_CORE, &rlim);
337 
338 	/* Turn off signals. */
339 	(void)signal(SIGALRM, SIG_IGN);
340 	(void)signal(SIGHUP, (SIG_PF) reload);
341 	(void)signal(SIGINT, SIG_IGN);
342 	(void)signal(SIGPIPE, SIG_IGN);
343 	(void)signal(SIGQUIT, SIG_IGN);
344 	(void)signal(SIGTERM, (SIG_PF) terminate);
345 
346 	svc_run();
347 	yp_error("svc_run returned");
348 	exit(1);
349 	/* NOTREACHED */
350 }
351