xref: /freebsd/usr.sbin/rpc.lockd/lockd.c (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
1 /*	$NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $	*/
2 /*	$FreeBSD$ */
3 
4 /*
5  * Copyright (c) 1995
6  *	A.R. Gordon (andrew.gordon@net-tel.co.uk).  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed for the FreeBSD project
19  * 4. Neither the name of the author nor the names of any co-contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  */
36 
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: lockd.c,v 1.7 2000/08/12 18:08:44 thorpej Exp $");
40 #endif
41 
42 /*
43  * main() function for NFS lock daemon.  Most of the code in this
44  * file was generated by running rpcgen /usr/include/rpcsvc/nlm_prot.x.
45  *
46  * The actual program logic is in the file lock_proc.c
47  */
48 
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 
52 #include <err.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <errno.h>
56 #include <syslog.h>
57 #include <signal.h>
58 #include <string.h>
59 #include <unistd.h>
60 #include <libutil.h>
61 #include <netconfig.h>
62 
63 #include <rpc/rpc.h>
64 #include <rpcsvc/sm_inter.h>
65 
66 #include "lockd.h"
67 #include <rpcsvc/nlm_prot.h>
68 
69 int		debug_level = 0;	/* 0 = no debugging syslog() calls */
70 int		_rpcsvcdirty = 0;
71 
72 int grace_expired;
73 int nsm_state;
74 pid_t client_pid;
75 struct mon mon_host;
76 
77 void	init_nsm(void);
78 void	nlm_prog_0(struct svc_req *, SVCXPRT *);
79 void	nlm_prog_1(struct svc_req *, SVCXPRT *);
80 void	nlm_prog_3(struct svc_req *, SVCXPRT *);
81 void	nlm_prog_4(struct svc_req *, SVCXPRT *);
82 void	usage(void);
83 
84 void sigalarm_handler(void);
85 
86 const char *transports[] = { "udp", "tcp", "udp6", "tcp6" };
87 
88 int
89 main(argc, argv)
90 	int argc;
91 	char **argv;
92 {
93 	SVCXPRT *transp;
94 	int ch, i, maxindex, s;
95 	struct sigaction sigalarm;
96 	int grace_period = 30;
97 	struct netconfig *nconf;
98 
99 	while ((ch = getopt(argc, argv, "d:g:")) != (-1)) {
100 		switch (ch) {
101 		case 'd':
102 			debug_level = atoi(optarg);
103 			if (!debug_level) {
104 				usage();
105 				/* NOTREACHED */
106 			}
107 			break;
108 		case 'g':
109 			grace_period = atoi(optarg);
110 			if (!grace_period) {
111 				usage();
112 				/* NOTREACHED */
113 			}
114 			break;
115 		default:
116 		case '?':
117 			usage();
118 			/* NOTREACHED */
119 		}
120 	}
121 	if (geteuid()) { /* This command allowed only to root */
122 		fprintf(stderr, "Sorry. You are not superuser\n");
123 		exit(1);
124         }
125 
126 	(void)rpcb_unset(NLM_PROG, NLM_SM, NULL);
127 	(void)rpcb_unset(NLM_PROG, NLM_VERS, NULL);
128 	(void)rpcb_unset(NLM_PROG, NLM_VERSX, NULL);
129 	(void)rpcb_unset(NLM_PROG, NLM_VERS4, NULL);
130 
131 	/*
132 	 * Check if IPv6 support is present.
133 	 */
134 	s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
135 	if (s < 0)
136 		maxindex = 2;
137 	else {
138 		close(s);
139 		maxindex = 4;
140 	}
141 
142 	for (i = 0; i < maxindex; i++) {
143 		nconf = getnetconfigent(transports[i]);
144 		if (nconf == NULL)
145 			errx(1, "cannot get udp netconf.");
146 
147 		transp = svc_tli_create(RPC_ANYFD, nconf, NULL, 0, 0);
148 		if (transp == NULL) {
149 			errx(1, "cannot create %s service.", transports[i]);
150 			/* NOTREACHED */
151 		}
152 		if (!svc_reg(transp, NLM_PROG, NLM_SM, nlm_prog_0, nconf)) {
153 			errx(1, "unable to register (NLM_PROG, NLM_SM, %s)",
154 			    transports[i]);
155 			/* NOTREACHED */
156 		}
157 		if (!svc_reg(transp, NLM_PROG, NLM_VERS, nlm_prog_1, nconf)) {
158 			errx(1, "unable to register (NLM_PROG, NLM_VERS, %s)",
159 			    transports[i]);
160 			/* NOTREACHED */
161 		}
162 		if (!svc_reg(transp, NLM_PROG, NLM_VERSX, nlm_prog_3, nconf)) {
163 			errx(1, "unable to register (NLM_PROG, NLM_VERSX, %s)",
164 			    transports[i]);
165 			/* NOTREACHED */
166 		}
167 		if (!svc_reg(transp, NLM_PROG, NLM_VERS4, nlm_prog_4, nconf)) {
168 			errx(1, "unable to register (NLM_PROG, NLM_VERS4, %s)",
169 			    transports[i]);
170 			/* NOTREACHED */
171 		}
172 		freenetconfigent(nconf);
173 	}
174 
175 	/*
176 	 * Note that it is NOT sensible to run this program from inetd - the
177 	 * protocol assumes that it will run immediately at boot time.
178 	 */
179 	if (daemon(0, 0)) {
180 		err(1, "cannot fork");
181 		/* NOTREACHED */
182 	}
183 
184 	openlog("rpc.lockd", 0, LOG_DAEMON);
185 	if (debug_level)
186 		syslog(LOG_INFO, "Starting, debug level %d", debug_level);
187 	else
188 		syslog(LOG_INFO, "Starting");
189 
190 	sigalarm.sa_handler = (sig_t) sigalarm_handler;
191 	sigemptyset(&sigalarm.sa_mask);
192 	sigalarm.sa_flags = SA_RESETHAND; /* should only happen once */
193 	sigalarm.sa_flags |= SA_RESTART;
194 	if (sigaction(SIGALRM, &sigalarm, NULL) != 0) {
195 		syslog(LOG_WARNING, "sigaction(SIGALRM) failed: %s",
196 		    strerror(errno));
197 		exit(1);
198 	}
199 	grace_expired = 0;
200 	alarm(10);
201 
202 	init_nsm();
203 
204 	client_pid = client_request();
205 
206 	svc_run();		/* Should never return */
207 	exit(1);
208 }
209 
210 void
211 sigalarm_handler(void)
212 {
213 
214 	grace_expired = 1;
215 }
216 
217 void
218 usage()
219 {
220 	errx(1, "usage: rpc.lockd [-d <debuglevel>] [-g <grace period>]");
221 }
222 
223 /*
224  * init_nsm --
225  *	Reset the NSM state-of-the-world and acquire its state.
226  */
227 void
228 init_nsm(void)
229 {
230 	enum clnt_stat ret;
231 	my_id id;
232 	sm_stat stat;
233 	char name[] = "NFS NLM";
234 	char localhost[] = "localhost";
235 
236 	/*
237 	 * !!!
238 	 * The my_id structure isn't used by the SM_UNMON_ALL call, as far
239 	 * as I know.  Leave it empty for now.
240 	 */
241 	memset(&id, 0, sizeof(id));
242 	id.my_name = name;
243 
244 	/*
245 	 * !!!
246 	 * The statd program must already be registered when lockd runs.
247 	 */
248 	do {
249 		ret = callrpc("localhost", SM_PROG, SM_VERS, SM_UNMON_ALL,
250 		    xdr_my_id, &id, xdr_sm_stat, &stat);
251 		if (ret == RPC_PROGUNAVAIL) {
252 			warnx("%lu %s", SM_PROG, clnt_sperrno(ret));
253 			sleep(2);
254 			continue;
255 		}
256 		break;
257 	} while (0);
258 
259 	if (ret != 0) {
260 		errx(1, "%lu %s", SM_PROG, clnt_sperrno(ret));
261 	}
262 
263 	nsm_state = stat.state;
264 
265 	/* setup constant data for SM_MON calls */
266 	mon_host.mon_id.my_id.my_name = localhost;
267 	mon_host.mon_id.my_id.my_prog = NLM_PROG;
268 	mon_host.mon_id.my_id.my_vers = NLM_SM;
269 	mon_host.mon_id.my_id.my_proc = NLM_SM_NOTIFY;  /* bsdi addition */
270 }
271