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