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.14.2.2 2002/02/15 00:46:57 des Exp $
33  * $DragonFly: src/usr.sbin/rpc.yppasswdd/yppasswdd_main.c,v 1.3 2004/05/20 19:24:43 cpressey Exp $
34  */
35 
36 #include "yppasswd.h"
37 #include <stdio.h>
38 #include <sys/types.h>
39 #include <stdlib.h> /* getenv, exit */
40 #include <unistd.h>
41 #include <string.h>
42 #include <sys/param.h>
43 #include <rpc/pmap_clnt.h> /* for pmap_unset */
44 #include <string.h> /* strcmp */
45 #include <signal.h>
46 #include <fcntl.h>
47 #include <sys/ioctl.h>
48 #include <sys/stat.h>
49 #ifdef __cplusplus
50 #include <sysent.h> /* getdtablesize, open */
51 #endif /* __cplusplus */
52 #include <memory.h>
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <syslog.h>
56 #include <err.h>
57 #include <errno.h>
58 #include <rpcsvc/yp.h>
59 struct dom_binding {};
60 #include <rpcsvc/ypclnt.h>
61 #include "yppasswdd_extern.h"
62 #include "yppasswd_private.h"
63 #include "ypxfr_extern.h"
64 
65 #ifndef SIG_PF
66 #define	SIG_PF void(*)(int)
67 #endif
68 
69 #ifdef DEBUG
70 #define	RPC_SVC_FG
71 #endif
72 
73 #define	_RPCSVC_CLOSEDOWN 120
74 int _rpcpmstart = 0;		/* Started by a port monitor ? */
75 static int _rpcfdtype;
76 		 /* Whether Stream or Datagram ? */
77 	/* States a server can be in wrt request */
78 
79 #define	_IDLE 0
80 #define	_SERVED 1
81 #define	_SERVING 2
82 
83 extern int _rpcsvcstate;	 /* Set when a request is serviced */
84 char *progname = "rpc.yppasswdd";
85 char *yp_dir = _PATH_YP;
86 char *passfile_default = _PATH_YP "master.passwd";
87 char *passfile;
88 char *yppasswd_domain = NULL;
89 int no_chsh = 0;
90 int no_chfn = 0;
91 int allow_additions = 0;
92 int multidomain = 0;
93 int verbose = 0;
94 int resvport = 1;
95 int inplace = 0;
96 char *sockname = YP_SOCKNAME;
97 
98 static void terminate(sig)
99 	int sig;
100 {
101 	svc_unregister(YPPASSWDPROG, YPPASSWDVERS);
102 	svc_unregister(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS);
103 	unlink(sockname);
104 	exit(0);
105 }
106 
107 static void reload(sig)
108 	int sig;
109 {
110 	load_securenets();
111 }
112 
113 static void
114 closedown(int sig)
115 {
116 	if (_rpcsvcstate == _IDLE) {
117 		extern fd_set svc_fdset;
118 		static int size;
119 		int i, openfd;
120 
121 		if (_rpcfdtype == SOCK_DGRAM) {
122 			unlink(sockname);
123 			exit(0);
124 		}
125 		if (size == 0) {
126 			size = getdtablesize();
127 		}
128 		for (i = 0, openfd = 0; i < size && openfd < 2; i++)
129 			if (FD_ISSET(i, &svc_fdset))
130 				openfd++;
131 		if (openfd <= 1) {
132 			unlink(sockname);
133 			exit(0);
134 		}
135 	}
136 	if (_rpcsvcstate == _SERVED)
137 		_rpcsvcstate = _IDLE;
138 
139 	(void) signal(SIGALRM, (SIG_PF) closedown);
140 	(void) alarm(_RPCSVC_CLOSEDOWN/2);
141 }
142 
143 static void usage()
144 {
145 	fprintf(stderr, "%s\n%s\n",
146 "usage: rpc.yppasswdd [-t master.passwd file] [-d domain] [-p path] [-s]",
147 "                     [-f] [-m] [-i] [-a] [-v] [-u] [-h]");
148 	exit(1);
149 }
150 
151 int
152 main(argc, argv)
153 	int argc;
154 	char *argv[];
155 {
156 	SVCXPRT *transp = NULL;
157 	int sock;
158 	int proto = 0;
159 	struct sockaddr_in saddr;
160 	int asize = sizeof (saddr);
161 	int ch;
162 	char *mastername;
163 	char myname[MAXHOSTNAMELEN + 2];
164 	extern int debug;
165 
166 	debug = 1;
167 
168 	while ((ch = getopt(argc, argv, "t:d:p:sfamuivh")) != -1) {
169 		switch (ch) {
170 		case 't':
171 			passfile_default = optarg;
172 			break;
173 		case 'd':
174 			yppasswd_domain = optarg;
175 			break;
176 		case 's':
177 			no_chsh++;
178 			break;
179 		case 'f':
180 			no_chfn++;
181 			break;
182 		case 'p':
183 			yp_dir = optarg;
184 			break;
185 		case 'a':
186 			allow_additions++;
187 			break;
188 		case 'm':
189 			multidomain++;
190 			break;
191 		case 'i':
192 			inplace++;
193 			break;
194 		case 'v':
195 			verbose++;
196 			break;
197 		case 'u':
198 			resvport = 0;
199 			break;
200 		default:
201 		case 'h':
202 			usage();
203 			break;
204 		}
205 	}
206 
207 	if (yppasswd_domain == NULL) {
208 		if (yp_get_default_domain(&yppasswd_domain)) {
209 			yp_error("no domain specified and system domain \
210 name isn't set -- aborting");
211 		usage();
212 		}
213 	}
214 
215 	load_securenets();
216 
217 	if (getrpcport("localhost", YPPROG, YPVERS, IPPROTO_UDP) <= 0) {
218 		yp_error("no ypserv processes registered with local portmap");
219 		yp_error("this host is not an NIS server -- aborting");
220 		exit(1);
221 	}
222 
223 	if ((mastername = ypxfr_get_master(yppasswd_domain, "passwd.byname",
224 						"localhost",0)) == NULL) {
225 		yp_error("can't get name of NIS master server for domain %s",
226 			 				yppasswd_domain);
227 		exit(1);
228 	}
229 
230 	if (gethostname((char *)&myname, sizeof(myname)) == -1) {
231 		yp_error("can't get local hostname: %s", strerror(errno));
232 		exit(1);
233 	}
234 
235 	if (strncasecmp(mastername, (char *)&myname, sizeof(myname))) {
236 		yp_error("master of %s is %s, but we are %s",
237 			"passwd.byname", mastername, myname);
238 		yp_error("this host is not the NIS master server for \
239 the %s domain -- aborting", yppasswd_domain);
240 		exit(1);
241 	}
242 
243 	debug = 0;
244 
245 	if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
246 		int ssize = sizeof (int);
247 
248 		if (saddr.sin_family != AF_INET)
249 			exit(1);
250 		if (getsockopt(0, SOL_SOCKET, SO_TYPE,
251 				(char *)&_rpcfdtype, &ssize) == -1)
252 			exit(1);
253 		sock = 0;
254 		_rpcpmstart = 1;
255 		proto = 0;
256 		openlog("rpc.yppasswdd", LOG_PID, LOG_DAEMON);
257 	} else {
258 		if (!debug) {
259 			if (daemon(0,0)) {
260 				err(1,"cannot fork");
261 			}
262 		}
263 		openlog("rpc.yppasswdd", LOG_PID, LOG_DAEMON);
264 		sock = RPC_ANYSOCK;
265 		(void) pmap_unset(YPPASSWDPROG, YPPASSWDVERS);
266 		(void) pmap_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS);
267 		unlink(sockname);
268 	}
269 
270 	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
271 		transp = svcudp_create(sock);
272 		if (transp == NULL) {
273 			yp_error("cannot create udp service.");
274 			exit(1);
275 		}
276 		if (!_rpcpmstart)
277 			proto = IPPROTO_UDP;
278 		if (!svc_register(transp, YPPASSWDPROG, YPPASSWDVERS, yppasswdprog_1, proto)) {
279 			yp_error("unable to register (YPPASSWDPROG, YPPASSWDVERS, udp).");
280 			exit(1);
281 		}
282 	}
283 
284 	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
285 		transp = svctcp_create(sock, 0, 0);
286 		if (transp == NULL) {
287 			yp_error("cannot create tcp service.");
288 			exit(1);
289 		}
290 		if (!_rpcpmstart)
291 			proto = IPPROTO_TCP;
292 		if (!svc_register(transp, YPPASSWDPROG, YPPASSWDVERS, yppasswdprog_1, proto)) {
293 			yp_error("unable to register (YPPASSWDPROG, YPPASSWDVERS, tcp).");
294 			exit(1);
295 		}
296 	}
297 
298 	unlink(sockname);
299 	transp = svcunix_create(sock, 0, 0, sockname);
300 	if (transp == NULL) {
301 		yp_error("cannot create AF_LOCAL service.");
302 		exit(1);
303 	}
304 	if (!svc_register(transp, MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, master_yppasswdprog_1, 0)) {
305 		yp_error("unable to register (MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, unix).");
306 		exit(1);
307 	}
308 	/* Only root may connect() to the AF_UNIX link. */
309 	if (chmod(sockname, 0))
310 		err(1, "chmod of %s failed", sockname);
311 
312 	if (transp == (SVCXPRT *)NULL) {
313 		yp_error("could not create a handle");
314 		exit(1);
315 	}
316 	if (_rpcpmstart) {
317 		(void) signal(SIGALRM, (SIG_PF) closedown);
318 		(void) alarm(_RPCSVC_CLOSEDOWN/2);
319 	}
320 	/* set up resource limits and block signals */
321 	pw_init();
322 
323 	/* except SIGCHLD, which we need to catch */
324 	install_reaper(1);
325 	signal(SIGTERM, (SIG_PF) terminate);
326 
327 	signal(SIGHUP, (SIG_PF) reload);
328 
329 	svc_run();
330 	yp_error("svc_run returned");
331 	exit(1);
332 	/* NOTREACHED */
333 }
334