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  * $FreeBSD: src/usr.sbin/rpc.yppasswdd/yppasswdd_main.c,v 1.27 2008/10/30 01:54:31 rafan Exp $
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 
84 static char _localhost[] = "localhost";
85 static char _passwd_byname[] = "passwd.byname";
86 extern int _rpcsvcstate;	 /* Set when a request is serviced */
87 static char _progname[] = "rpc.yppasswdd";
88 char *progname = _progname;
89 static char _yp_dir[] = _PATH_YP;
90 const char *yp_dir = _yp_dir;
91 static char _passfile_default[] = _PATH_YP "master.passwd";
92 char *passfile_default = _passfile_default;
93 char *passfile;
94 char *yppasswd_domain = NULL;
95 int no_chsh = 0;
96 int no_chfn = 0;
97 int allow_additions = 0;
98 int multidomain = 0;
99 int verbose = 0;
100 int resvport = 1;
101 int inplace = 0;
102 char sockname[] = YP_SOCKNAME;
103 
104 static void
105 terminate(int sig __unused)
106 {
107 	rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL);
108 	rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL);
109 	unlink(sockname);
110 	exit(0);
111 }
112 
113 static void
114 reload(int sig __unused)
115 {
116 	load_securenets();
117 }
118 
119 static void
120 closedown(int sig __unused)
121 {
122 	if (_rpcsvcstate == _IDLE) {
123 		extern fd_set svc_fdset;
124 		static int size;
125 		int i, openfd;
126 
127 		if (_rpcfdtype == SOCK_DGRAM) {
128 			unlink(sockname);
129 			exit(0);
130 		}
131 		if (size == 0) {
132 			size = getdtablesize();
133 		}
134 		for (i = 0, openfd = 0; i < size && openfd < 2; i++)
135 			if (FD_ISSET(i, &svc_fdset))
136 				openfd++;
137 		if (openfd <= 1) {
138 			unlink(sockname);
139 			exit(0);
140 		}
141 	}
142 	if (_rpcsvcstate == _SERVED)
143 		_rpcsvcstate = _IDLE;
144 
145 	signal(SIGALRM, (SIG_PF) closedown);
146 	alarm(_RPCSVC_CLOSEDOWN/2);
147 }
148 
149 static void
150 usage(void)
151 {
152 	fprintf(stderr, "%s\n%s\n",
153 "usage: rpc.yppasswdd [-t master.passwd file] [-d domain] [-p path] [-s]",
154 "                     [-f] [-m] [-i] [-a] [-v] [-u] [-h]");
155 	exit(1);
156 }
157 
158 int
159 main(int argc, char *argv[])
160 {
161 	struct rlimit rlim;
162 	SVCXPRT *transp = NULL;
163 	struct sockaddr_in saddr;
164 	socklen_t asize = sizeof (saddr);
165 	struct netconfig *nconf;
166 	struct sigaction sa;
167 	void *localhandle;
168 	int ch;
169 	char *mastername;
170 	char myname[MAXHOSTNAMELEN + 2];
171 	int maxrec = RPC_MAXDATASIZE;
172 
173 	extern int debug;
174 
175 	debug = 1;
176 
177 	while ((ch = getopt(argc, argv, "t:d:p:sfamuivh")) != -1) {
178 		switch (ch) {
179 		case 't':
180 			passfile_default = optarg;
181 			break;
182 		case 'd':
183 			yppasswd_domain = optarg;
184 			break;
185 		case 's':
186 			no_chsh++;
187 			break;
188 		case 'f':
189 			no_chfn++;
190 			break;
191 		case 'p':
192 			yp_dir = optarg;
193 			break;
194 		case 'a':
195 			allow_additions++;
196 			break;
197 		case 'm':
198 			multidomain++;
199 			break;
200 		case 'i':
201 			inplace++;
202 			break;
203 		case 'v':
204 			verbose++;
205 			break;
206 		case 'u':
207 			resvport = 0;
208 			break;
209 		default:
210 		case 'h':
211 			usage();
212 			break;
213 		}
214 	}
215 
216 	if (yppasswd_domain == NULL) {
217 		if (yp_get_default_domain(&yppasswd_domain)) {
218 			yp_error("no domain specified and system domain \
219 name isn't set -- aborting");
220 		usage();
221 		}
222 	}
223 
224 	load_securenets();
225 
226 	if (getrpcport(_localhost, YPPROG, YPVERS, IPPROTO_UDP) <= 0) {
227 		yp_error("no ypserv processes registered with local portmap");
228 		yp_error("this host is not an NIS server -- aborting");
229 		exit(1);
230 	}
231 
232 	if ((mastername = ypxfr_get_master(yppasswd_domain,
233 		 _passwd_byname, _localhost, 0)) == NULL) {
234 		yp_error("can't get name of NIS master server for domain %s",
235 			 				yppasswd_domain);
236 		exit(1);
237 	}
238 
239 	if (gethostname((char *)&myname, sizeof(myname)) == -1) {
240 		yp_error("can't get local hostname: %s", strerror(errno));
241 		exit(1);
242 	}
243 
244 	if (strncasecmp(mastername, (char *)&myname, sizeof(myname))) {
245 		yp_error("master of %s is %s, but we are %s",
246 			"passwd.byname", mastername, myname);
247 		yp_error("this host is not the NIS master server for \
248 the %s domain -- aborting", yppasswd_domain);
249 		exit(1);
250 	}
251 
252 	debug = 0;
253 
254 	if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
255 		socklen_t ssize = sizeof (int);
256 		if (saddr.sin_family != AF_INET)
257 			exit(1);
258 		if (getsockopt(0, SOL_SOCKET, SO_TYPE,
259 		    (char *)&_rpcfdtype, &ssize) == -1)
260 			exit(1);
261 		_rpcpmstart = 1;
262 	}
263 
264 	if (!debug && _rpcpmstart == 0) {
265 		if (daemon(0,0)) {
266 			err(1,"cannot fork");
267 		}
268 	}
269 	openlog("rpc.yppasswdd", LOG_PID, LOG_DAEMON);
270 	memset(&sa, 0, sizeof(sa));
271 	sa.sa_flags = SA_NOCLDWAIT;
272 	sigaction(SIGCHLD, &sa, NULL);
273 
274 	rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL);
275 	rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL);
276 
277 	rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
278 
279 	if (svc_create(yppasswdprog_1, YPPASSWDPROG, YPPASSWDVERS, "netpath") == 0) {
280 		yp_error("cannot create yppasswd service.");
281 		exit(1);
282 	}
283 	if (svc_create(master_yppasswdprog_1, MASTER_YPPASSWDPROG,
284 	    MASTER_YPPASSWDVERS, "netpath") == 0) {
285 		yp_error("cannot create master_yppasswd service.");
286 		exit(1);
287 	}
288 
289 	nconf = NULL;
290 	localhandle = setnetconfig();
291 	while ((nconf = getnetconfig(localhandle)) != NULL) {
292 		if (nconf->nc_protofmly != NULL &&
293 		    strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
294 			break;
295 	}
296 	if (nconf == NULL) {
297 		yp_error("getnetconfigent unix: %s", nc_sperror());
298 		exit(1);
299 	}
300 	unlink(sockname);
301 	transp = svcunix_create(RPC_ANYSOCK, 0, 0, sockname);
302 	if (transp == NULL) {
303 		yp_error("cannot create AF_LOCAL service.");
304 		exit(1);
305 	}
306 	if (!svc_reg(transp, MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS,
307 	    master_yppasswdprog_1, nconf)) {
308 		yp_error("unable to register (MASTER_YPPASSWDPROG, \
309 		    MASTER_YPPASSWDVERS, unix).");
310 		exit(1);
311 	}
312 	endnetconfig(localhandle);
313 
314 	/* Only root may connect() to the AF_UNIX link. */
315 	if (chmod(sockname, 0))
316 		err(1, "chmod of %s failed", sockname);
317 
318 	if (transp == NULL) {
319 		yp_error("could not create a handle");
320 		exit(1);
321 	}
322 	if (_rpcpmstart) {
323 		signal(SIGALRM, (SIG_PF) closedown);
324 		alarm(_RPCSVC_CLOSEDOWN/2);
325 	}
326 
327 	/* Unlimited resource limits. */
328 	rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
329 	setrlimit(RLIMIT_CPU, &rlim);
330 	setrlimit(RLIMIT_FSIZE, &rlim);
331 	setrlimit(RLIMIT_STACK, &rlim);
332 	setrlimit(RLIMIT_DATA, &rlim);
333 	setrlimit(RLIMIT_RSS, &rlim);
334 
335 	/* Don't drop core (not really necessary, but GP's). */
336 	rlim.rlim_cur = rlim.rlim_max = 0;
337 	setrlimit(RLIMIT_CORE, &rlim);
338 
339 	/* Turn off signals. */
340 	signal(SIGALRM, SIG_IGN);
341 	signal(SIGHUP, (SIG_PF) reload);
342 	signal(SIGINT, SIG_IGN);
343 	signal(SIGPIPE, SIG_IGN);
344 	signal(SIGQUIT, SIG_IGN);
345 	signal(SIGTERM, (SIG_PF) terminate);
346 
347 	svc_run();
348 	yp_error("svc_run returned");
349 	exit(1);
350 	/* NOTREACHED */
351 }
352