xref: /netbsd-src/usr.sbin/rpc.statd/stat_proc.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: stat_proc.c,v 1.8 2009/04/18 13:04:50 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 1995
5  *	A.R. Gordon (andrew.gordon@net-tel.co.uk).  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed for the FreeBSD project
18  * 4. Neither the name of the author nor the names of any co-contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY ANDREW GORDON AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __RCSID("$NetBSD: stat_proc.c,v 1.8 2009/04/18 13:04:50 lukem Exp $");
39 #endif
40 
41 #include <errno.h>
42 #include <netdb.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <syslog.h>
47 #include <signal.h>
48 #include <unistd.h>
49 
50 #include <rpc/rpc.h>
51 
52 #include "statd.h"
53 
54 /* sm_stat_1 --------------------------------------------------------------- */
55 /*
56  * Purpose:	RPC call to enquire if a host can be monitored
57  * Returns:	TRUE for any hostname that can be looked up to give
58  *		an address.
59  */
60 struct sm_stat_res *
61 sm_stat_1_svc(arg, req)
62 	sm_name *arg;
63 	struct svc_req *req;
64 {
65 	static sm_stat_res smres;
66 	struct addrinfo *ai;
67 
68 	NO_ALARM;
69 	if (debug)
70 		syslog(LOG_DEBUG, "stat called for host %s", arg->mon_name);
71 
72 	if (getaddrinfo(arg->mon_name, NULL, NULL, &ai) == 0) {
73 		smres.res_stat = stat_succ;
74 		freeaddrinfo(ai);
75 	} else {
76 		syslog(LOG_ERR, "invalid hostname to sm_stat: %s",
77 		    arg->mon_name);
78 		smres.res_stat = stat_fail;
79 	}
80 
81 	smres.state = status_info.ourState;
82 	ALARM;
83 	return (&smres);
84 }
85 
86 /* sm_mon_1 ---------------------------------------------------------------- */
87 /*
88  * Purpose:	RPC procedure to establish a monitor request
89  * Returns:	Success, unless lack of resources prevents
90  *		the necessary structures from being set up
91  *		to record the request, or if the hostname is not
92  *		valid (as judged by gethostbyname())
93  */
94 struct sm_stat_res *
95 sm_mon_1_svc(arg, req)
96 	mon *arg;
97 	struct svc_req *req;
98 {
99 	static sm_stat_res smres;
100 	struct addrinfo *ai;
101 	HostInfo *hp, h;
102 	MonList *lp;
103 
104 	NO_ALARM;
105 	if (debug) {
106 		syslog(LOG_DEBUG, "monitor request for host %s",
107 		    arg->mon_id.mon_name);
108 		syslog(LOG_DEBUG, "recall host: %s prog: %d ver: %d proc: %d",
109 		    arg->mon_id.my_id.my_name, arg->mon_id.my_id.my_prog,
110 		    arg->mon_id.my_id.my_vers, arg->mon_id.my_id.my_proc);
111 	}
112 	smres.res_stat = stat_fail;	/* Assume fail until set otherwise */
113 	smres.state = status_info.ourState;
114 
115 	/*
116 	 * Find existing host entry, or create one if not found.  If
117 	 * find_host() fails, it will have logged the error already.
118 	 */
119 	if (getaddrinfo(arg->mon_id.mon_name, NULL, NULL, &ai) != 0) {
120 		syslog(LOG_ERR, "Invalid hostname to sm_mon: %s",
121 		    arg->mon_id.mon_name);
122 		return &smres;
123 	}
124 
125 	freeaddrinfo(ai);
126 
127 	if ((hp = find_host(arg->mon_id.mon_name, &h)) == NULL)
128 		memset(hp = &h, 0, sizeof(h));
129 
130 	lp = (MonList *)malloc(sizeof(MonList));
131 	if (!lp)
132 		syslog(LOG_ERR, "Out of memory");
133 	else {
134 		strncpy(lp->notifyHost, arg->mon_id.my_id.my_name,
135 		    SM_MAXSTRLEN);
136 		lp->notifyProg = arg->mon_id.my_id.my_prog;
137 		lp->notifyVers = arg->mon_id.my_id.my_vers;
138 		lp->notifyProc = arg->mon_id.my_id.my_proc;
139 		memcpy(lp->notifyData, arg->priv,
140 		    sizeof(lp->notifyData));
141 
142 		lp->next = hp->monList;
143 		hp->monList = lp;
144 		change_host(arg->mon_id.mon_name, hp);
145 		sync_file();
146 		smres.res_stat = stat_succ;	/* Report success */
147 	}
148 	ALARM;
149 	return (&smres);
150 }
151 
152 /* do_unmon ---------------------------------------------------------------- */
153 /*
154  * Purpose:	Remove a monitor request from a host
155  * Returns:	TRUE if found, FALSE if not found.
156  * Notes:	Common code from sm_unmon_1_svc and sm_unmon_all_1_svc
157  *		In the unlikely event of more than one identical monitor
158  *		request, all are removed.
159  */
160 int
161 do_unmon(name, hp, ptr)
162 	char *name;
163 	HostInfo *hp;
164 	void *ptr;
165 {
166 	my_id *idp = ptr;
167 	MonList *lp, *next;
168 	MonList *last = NULL;
169 	int result = FALSE;
170 
171 	lp = hp->monList;
172 	while (lp) {
173 		if (!strncasecmp(idp->my_name, lp->notifyHost, SM_MAXSTRLEN)
174 		    && (idp->my_prog == lp->notifyProg)
175 		    && (idp->my_proc == lp->notifyProc)
176 		    && (idp->my_vers == lp->notifyVers)) {
177 			/* found one.  Unhook from chain and free. */
178 			next = lp->next;
179 			if (last)
180 				last->next = next;
181 			else
182 				hp->monList = next;
183 			free(lp);
184 			lp = next;
185 			result = TRUE;
186 		} else {
187 			last = lp;
188 			lp = lp->next;
189 		}
190 	}
191 	return (result);
192 }
193 
194 /* sm_unmon_1 -------------------------------------------------------------- */
195 /*
196  * Purpose:	RPC procedure to release a monitor request.
197  * Returns:	Local machine's status number
198  * Notes:	The supplied mon_id should match the value passed in an
199  *		earlier call to sm_mon_1
200  */
201 struct sm_stat *
202 sm_unmon_1_svc(arg, req)
203 	mon_id *arg;
204 	struct svc_req *req;
205 {
206 	static sm_stat smres;
207 	HostInfo *hp, h;
208 
209 	NO_ALARM;
210 	if (debug) {
211 		syslog(LOG_DEBUG, "un-monitor request for host %s",
212 		    arg->mon_name);
213 		syslog(LOG_DEBUG, "recall host: %s prog: %d ver: %d proc: %d",
214 		    arg->my_id.my_name, arg->my_id.my_prog,
215 		    arg->my_id.my_vers, arg->my_id.my_proc);
216 	}
217 	if ((hp = find_host(arg->mon_name, &h)) != NULL) {
218 		if (do_unmon(arg->mon_name, hp, &arg->my_id)) {
219 			change_host(arg->mon_name, hp);
220 			sync_file();
221 		}
222 		else
223 			syslog(LOG_ERR,
224 			    "unmon request from %s, no matching monitor",
225 			    arg->my_id.my_name);
226 	} else
227 		syslog(LOG_ERR, "unmon request from %s for unknown host %s",
228 		    arg->my_id.my_name, arg->mon_name);
229 
230 	smres.state = status_info.ourState;
231 	ALARM;
232 
233 	return (&smres);
234 }
235 
236 /* sm_unmon_all_1 ---------------------------------------------------------- */
237 /*
238  * Purpose:	RPC procedure to release monitor requests.
239  * Returns:	Local machine's status number
240  * Notes:	Releases all monitor requests (if any) from the specified
241  *		host and program number.
242  */
243 struct sm_stat *
244 sm_unmon_all_1_svc(arg, req)
245 	my_id *arg;
246 	struct svc_req *req;
247 {
248 	static sm_stat smres;
249 
250 	NO_ALARM;
251 	if (debug) {
252 		syslog(LOG_DEBUG,
253 		    "unmon_all for host: %s prog: %d ver: %d proc: %d",
254 		    arg->my_name, arg->my_prog, arg->my_vers, arg->my_proc);
255 	}
256 
257 	unmon_hosts();
258 	sync_file();
259 
260 	smres.state = status_info.ourState;
261 	ALARM;
262 
263 	return (&smres);
264 }
265 
266 /* sm_simu_crash_1 --------------------------------------------------------- */
267 /*
268  * Purpose:	RPC procedure to simulate a crash
269  * Returns:	Nothing
270  * Notes:	Standardised mechanism for debug purposes
271  *		The specification says that we should drop all of our
272  *		status information (apart from the list of monitored hosts
273  *		on disc).  However, this would confuse the rpc.lockd
274  *		which would be unaware that all of its monitor requests
275  *		had been silently junked.  Hence we in fact retain all
276  *		current requests and simply increment the status counter
277  *		and inform all hosts on the monitor list.
278  */
279 void *
280 sm_simu_crash_1_svc(v, req)
281 	void *v;
282 	struct svc_req *req;
283 {
284 	static char dummy;
285 
286 	NO_ALARM;
287 	if (debug)
288 		syslog(LOG_DEBUG, "simu_crash called!!");
289 
290 	reset_database();
291 	ALARM;
292 	notify_handler(0);
293 
294 	return (&dummy);
295 }
296 
297 /* sm_notify_1 ------------------------------------------------------------- */
298 /*
299  * Purpose:	RPC procedure notifying local statd of the crash of another
300  * Returns:	Nothing
301  * Notes:	There is danger of deadlock, since it is quite likely that
302  *		the client procedure that we call will in turn call us
303  *		to remove or adjust the monitor request.
304  *		We therefore fork() a process to do the notifications.
305  *		Note that the main HostInfo structure is in a mmap()
306  *		region and so will be shared with the child, but the
307  *		monList pointed to by the HostInfo is in normal memory.
308  *		Hence if we read the monList before forking, we are
309  *		protected from the parent servicing other requests
310  *		that modify the list.
311  */
312 void   *
313 sm_notify_1_svc(arg, req)
314 	stat_chge *arg;
315 	struct svc_req *req;
316 {
317 	struct timeval timeout = {20, 0};	/* 20 secs timeout */
318 	CLIENT *cli;
319 	static char dummy;
320 	status tx_arg;		/* arg sent to callback procedure */
321 	MonList *lp;
322 	HostInfo *hp, h;
323 	pid_t pid;
324 
325 	if (debug)
326 		syslog(LOG_DEBUG, "notify from host %s, new state %d",
327 		    arg->mon_name, arg->state);
328 
329 	hp = find_host(arg->mon_name, &h);
330 	if (!hp) {
331 		/* Never heard of this host - why is it notifying us? */
332 		syslog(LOG_DEBUG, "Unsolicited notification from host %s",
333 		    arg->mon_name);
334 		return (&dummy);
335 	}
336 	lp = hp->monList;
337 	if (!lp) /* We know this host, but have no outstanding requests. */
338 		return (&dummy);
339 
340 	sync_file();
341 	pid = fork();
342 	if (pid == -1) {
343 		syslog(LOG_ERR, "Unable to fork notify process - %s",
344 		    strerror(errno));
345 		return (FALSE);
346 	}
347 	if (pid)
348 		return (&dummy); /* Parent returns */
349 
350 	while (lp) {
351 		tx_arg.mon_name = arg->mon_name;
352 		tx_arg.state = arg->state;
353 		memcpy(tx_arg.priv, lp->notifyData, sizeof(tx_arg.priv));
354 		cli = clnt_create(lp->notifyHost, lp->notifyProg,
355 		    lp->notifyVers, "udp");
356 		if (!cli)
357 			syslog(LOG_ERR, "Failed to contact host %s%s",
358 			    lp->notifyHost, clnt_spcreateerror(""));
359 		else {
360 			if (clnt_call(cli, lp->notifyProc, xdr_status, &tx_arg,
361 			    xdr_void, &dummy, timeout) != RPC_SUCCESS)
362 				syslog(LOG_ERR,
363 				    "Failed to call rpc.statd client at host %s",
364 				    lp->notifyHost);
365 			clnt_destroy(cli);
366 		}
367 		lp = lp->next;
368 	}
369 
370 	exit(0);		/* Child quits */
371 }
372