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