turns printfs back on
[freebsd-src/fkvm-freebsd.git] / usr.sbin / rpc.yppasswdd / yppasswdd_main.c
blobe4170bd0e471bbfcdd62c83c34af59339d235697
1 /*
2 * Copyright (c) 1995, 1996
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
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.
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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/ioctl.h>
38 #include <sys/stat.h>
39 #include <sys/socket.h>
40 #include <sys/time.h>
41 #include <sys/resource.h>
42 #include <netinet/in.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <memory.h>
48 #include <signal.h>
49 #include <stdio.h>
50 #include <stdlib.h> /* getenv, exit */
51 #include <string.h>
52 #include <string.h> /* strcmp */
53 #include <syslog.h>
54 #include <unistd.h>
56 #include <rpc/rpc.h>
57 #include <rpc/rpc_com.h>
58 #include <rpc/pmap_clnt.h> /* for pmap_unset */
59 #include <rpcsvc/yp.h>
60 #include <rpcsvc/ypclnt.h>
62 #include "yppasswd.h"
63 #include "yppasswdd_extern.h"
64 #include "yppasswd_private.h"
65 #include "ypxfr_extern.h"
66 #include "yp_extern.h"
68 #ifndef SIG_PF
69 #define SIG_PF void(*)(int)
70 #endif
72 #ifdef DEBUG
73 #define RPC_SVC_FG
74 #endif
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 */
82 #define _IDLE 0
83 #define _SERVED 1
84 #define _SERVING 2
86 static char _localhost[] = "localhost";
87 static char _passwd_byname[] = "passwd.byname";
88 extern int _rpcsvcstate; /* Set when a request is serviced */
89 static char _progname[] = "rpc.yppasswdd";
90 char *progname = _progname;
91 static char _yp_dir[] = _PATH_YP;
92 char *yp_dir = _yp_dir;
93 static char _passfile_default[] = _PATH_YP "master.passwd";
94 char *passfile_default = _passfile_default;
95 char *passfile;
96 char *yppasswd_domain = NULL;
97 int no_chsh = 0;
98 int no_chfn = 0;
99 int allow_additions = 0;
100 int multidomain = 0;
101 int verbose = 0;
102 int resvport = 1;
103 int inplace = 0;
104 char sockname[] = YP_SOCKNAME;
106 static void
107 terminate(int sig __unused)
109 rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL);
110 rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL);
111 unlink(sockname);
112 exit(0);
115 static void
116 reload(int sig __unused)
118 load_securenets();
121 static void
122 closedown(int sig __unused)
124 if (_rpcsvcstate == _IDLE) {
125 extern fd_set svc_fdset;
126 static int size;
127 int i, openfd;
129 if (_rpcfdtype == SOCK_DGRAM) {
130 unlink(sockname);
131 exit(0);
133 if (size == 0) {
134 size = getdtablesize();
136 for (i = 0, openfd = 0; i < size && openfd < 2; i++)
137 if (FD_ISSET(i, &svc_fdset))
138 openfd++;
139 if (openfd <= 1) {
140 unlink(sockname);
141 exit(0);
144 if (_rpcsvcstate == _SERVED)
145 _rpcsvcstate = _IDLE;
147 (void) signal(SIGALRM, (SIG_PF) closedown);
148 (void) alarm(_RPCSVC_CLOSEDOWN/2);
151 static void
152 usage(void)
154 fprintf(stderr, "%s\n%s\n",
155 "usage: rpc.yppasswdd [-t master.passwd file] [-d domain] [-p path] [-s]",
156 " [-f] [-m] [-i] [-a] [-v] [-u] [-h]");
157 exit(1);
161 main(int argc, char *argv[])
163 struct rlimit rlim;
164 SVCXPRT *transp = NULL;
165 struct sockaddr_in saddr;
166 socklen_t asize = sizeof (saddr);
167 struct netconfig *nconf;
168 void *localhandle;
169 int ch;
170 char *mastername;
171 char myname[MAXHOSTNAMELEN + 2];
172 int maxrec = RPC_MAXDATASIZE;
174 extern int debug;
176 debug = 1;
178 while ((ch = getopt(argc, argv, "t:d:p:sfamuivh")) != -1) {
179 switch (ch) {
180 case 't':
181 passfile_default = optarg;
182 break;
183 case 'd':
184 yppasswd_domain = optarg;
185 break;
186 case 's':
187 no_chsh++;
188 break;
189 case 'f':
190 no_chfn++;
191 break;
192 case 'p':
193 yp_dir = optarg;
194 break;
195 case 'a':
196 allow_additions++;
197 break;
198 case 'm':
199 multidomain++;
200 break;
201 case 'i':
202 inplace++;
203 break;
204 case 'v':
205 verbose++;
206 break;
207 case 'u':
208 resvport = 0;
209 break;
210 default:
211 case 'h':
212 usage();
213 break;
217 if (yppasswd_domain == NULL) {
218 if (yp_get_default_domain(&yppasswd_domain)) {
219 yp_error("no domain specified and system domain \
220 name isn't set -- aborting");
221 usage();
225 load_securenets();
227 if (getrpcport(_localhost, YPPROG, YPVERS, IPPROTO_UDP) <= 0) {
228 yp_error("no ypserv processes registered with local portmap");
229 yp_error("this host is not an NIS server -- aborting");
230 exit(1);
233 if ((mastername = ypxfr_get_master(yppasswd_domain,
234 _passwd_byname, _localhost, 0)) == NULL) {
235 yp_error("can't get name of NIS master server for domain %s",
236 yppasswd_domain);
237 exit(1);
240 if (gethostname((char *)&myname, sizeof(myname)) == -1) {
241 yp_error("can't get local hostname: %s", strerror(errno));
242 exit(1);
245 if (strncasecmp(mastername, (char *)&myname, sizeof(myname))) {
246 yp_error("master of %s is %s, but we are %s",
247 "passwd.byname", mastername, myname);
248 yp_error("this host is not the NIS master server for \
249 the %s domain -- aborting", yppasswd_domain);
250 exit(1);
253 debug = 0;
255 if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
256 socklen_t ssize = sizeof (int);
257 if (saddr.sin_family != AF_INET)
258 exit(1);
259 if (getsockopt(0, SOL_SOCKET, SO_TYPE,
260 (char *)&_rpcfdtype, &ssize) == -1)
261 exit(1);
262 _rpcpmstart = 1;
265 if (!debug && _rpcpmstart == 0) {
266 if (daemon(0,0)) {
267 err(1,"cannot fork");
270 openlog("rpc.yppasswdd", LOG_PID, LOG_DAEMON);
272 rpcb_unset(YPPASSWDPROG, YPPASSWDVERS, NULL);
273 rpcb_unset(MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS, NULL);
275 rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec);
277 if (svc_create(yppasswdprog_1, YPPASSWDPROG, YPPASSWDVERS, "netpath") == 0) {
278 yp_error("cannot create yppasswd service.");
279 exit(1);
281 if (svc_create(master_yppasswdprog_1, MASTER_YPPASSWDPROG,
282 MASTER_YPPASSWDVERS, "netpath") == 0) {
283 yp_error("cannot create master_yppasswd service.");
284 exit(1);
287 nconf = NULL;
288 localhandle = setnetconfig();
289 while ((nconf = getnetconfig(localhandle)) != NULL) {
290 if (nconf->nc_protofmly != NULL &&
291 strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0)
292 break;
294 if (nconf == NULL) {
295 yp_error("getnetconfigent unix: %s", nc_sperror());
296 exit(1);
298 unlink(sockname);
299 transp = svcunix_create(RPC_ANYSOCK, 0, 0, sockname);
300 if (transp == NULL) {
301 yp_error("cannot create AF_LOCAL service.");
302 exit(1);
304 if (!svc_reg(transp, MASTER_YPPASSWDPROG, MASTER_YPPASSWDVERS,
305 master_yppasswdprog_1, nconf)) {
306 yp_error("unable to register (MASTER_YPPASSWDPROG, \
307 MASTER_YPPASSWDVERS, unix).");
308 exit(1);
310 endnetconfig(localhandle);
312 /* Only root may connect() to the AF_UNIX link. */
313 if (chmod(sockname, 0))
314 err(1, "chmod of %s failed", sockname);
316 if (transp == (SVCXPRT *)NULL) {
317 yp_error("could not create a handle");
318 exit(1);
320 if (_rpcpmstart) {
321 (void) signal(SIGALRM, (SIG_PF) closedown);
322 (void) alarm(_RPCSVC_CLOSEDOWN/2);
325 /* Unlimited resource limits. */
326 rlim.rlim_cur = rlim.rlim_max = RLIM_INFINITY;
327 (void)setrlimit(RLIMIT_CPU, &rlim);
328 (void)setrlimit(RLIMIT_FSIZE, &rlim);
329 (void)setrlimit(RLIMIT_STACK, &rlim);
330 (void)setrlimit(RLIMIT_DATA, &rlim);
331 (void)setrlimit(RLIMIT_RSS, &rlim);
333 /* Don't drop core (not really necessary, but GP's). */
334 rlim.rlim_cur = rlim.rlim_max = 0;
335 (void)setrlimit(RLIMIT_CORE, &rlim);
337 /* Turn off signals. */
338 (void)signal(SIGALRM, SIG_IGN);
339 (void)signal(SIGHUP, (SIG_PF) reload);
340 (void)signal(SIGINT, SIG_IGN);
341 (void)signal(SIGPIPE, SIG_IGN);
342 (void)signal(SIGQUIT, SIG_IGN);
343 (void)signal(SIGTERM, (SIG_PF) terminate);
345 svc_run();
346 yp_error("svc_run returned");
347 exit(1);
348 /* NOTREACHED */