GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/rpc.statd/procs.c Lines: 0 126 0.0 %
Date: 2017-11-13 Branches: 0 68 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: procs.c,v 1.3 2017/01/21 08:33:51 krw 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 <errno.h>
37
#include <netdb.h>
38
#include <stdio.h>
39
#include <stdlib.h>
40
#include <string.h>
41
#include <syslog.h>
42
#include <signal.h>
43
#include <unistd.h>
44
45
#include <rpc/rpc.h>
46
47
#include "statd.h"
48
49
/* sm_stat_1 --------------------------------------------------------------- */
50
/*
51
 * Purpose:	RPC call to enquire if a host can be monitored
52
 * Returns:	TRUE for any hostname that can be looked up to give
53
 *		an address.
54
 */
55
struct sm_stat_res *
56
sm_stat_1_svc(sm_name *arg, struct svc_req *req)
57
{
58
	static sm_stat_res res;
59
60
	NO_ALARM;
61
	if (debug)
62
		syslog(LOG_DEBUG, "stat called for host %s", arg->mon_name);
63
64
	if (gethostbyname(arg->mon_name))
65
		res.res_stat = stat_succ;
66
	else {
67
		syslog(LOG_ERR, "invalid hostname to sm_stat: %s",
68
		    arg->mon_name);
69
		res.res_stat = stat_fail;
70
	}
71
72
	res.state = status_info.ourState;
73
	ALARM;
74
	return (&res);
75
}
76
77
/* sm_mon_1 ---------------------------------------------------------------- */
78
/*
79
 * Purpose:	RPC procedure to establish a monitor request
80
 * Returns:	Success, unless lack of resources prevents
81
 *		the necessary structures from being set up
82
 *		to record the request, or if the hostname is not
83
 *		valid (as judged by gethostbyname())
84
 */
85
struct sm_stat_res *
86
sm_mon_1_svc(mon *arg, struct svc_req *req)
87
{
88
	static sm_stat_res res;
89
	HostInfo *hp, h;
90
	MonList *lp;
91
92
	NO_ALARM;
93
	if (debug) {
94
		syslog(LOG_DEBUG, "monitor request for host %s",
95
		    arg->mon_id.mon_name);
96
		syslog(LOG_DEBUG, "recall host: %s prog: %d ver: %d proc: %d",
97
		    arg->mon_id.my_id.my_name, arg->mon_id.my_id.my_prog,
98
		    arg->mon_id.my_id.my_vers, arg->mon_id.my_id.my_proc);
99
	}
100
	res.res_stat = stat_fail;	/* Assume fail until set otherwise */
101
	res.state = status_info.ourState;
102
103
	/*
104
	 * Find existing host entry, or create one if not found.  If
105
	 * find_host() fails, it will have logged the error already.
106
	 */
107
	if (!gethostbyname(arg->mon_id.mon_name)) {
108
		syslog(LOG_ERR, "Invalid hostname to sm_mon: %s",
109
		    arg->mon_id.mon_name);
110
		return &res;
111
	}
112
113
	if ((hp = find_host(arg->mon_id.mon_name, &h)) == NULL)
114
		memset(hp = &h, 0, sizeof(h));
115
116
	lp = malloc(sizeof(MonList));
117
	if (!lp)
118
		syslog(LOG_ERR, "Out of memory");
119
	else {
120
		strncpy(lp->notifyHost, arg->mon_id.my_id.my_name,
121
		    SM_MAXSTRLEN);
122
		lp->notifyProg = arg->mon_id.my_id.my_prog;
123
		lp->notifyVers = arg->mon_id.my_id.my_vers;
124
		lp->notifyProc = arg->mon_id.my_id.my_proc;
125
		memcpy(lp->notifyData, arg->priv,
126
		    sizeof(lp->notifyData));
127
128
		lp->next = hp->monList;
129
		hp->monList = lp;
130
		change_host(arg->mon_id.mon_name, hp);
131
		sync_file();
132
		res.res_stat = stat_succ;	/* Report success */
133
	}
134
	ALARM;
135
	return (&res);
136
}
137
138
/* do_unmon ---------------------------------------------------------------- */
139
/*
140
 * Purpose:	Remove a monitor request from a host
141
 * Returns:	TRUE if found, FALSE if not found.
142
 * Notes:	Common code from sm_unmon_1_svc and sm_unmon_all_1_svc
143
 *		In the unlikely event of more than one identical monitor
144
 *		request, all are removed.
145
 */
146
int
147
do_unmon(char *name, HostInfo *hp, void *ptr)
148
{
149
	my_id *idp = ptr;
150
	MonList *lp, *next;
151
	MonList *last = NULL;
152
	int result = FALSE;
153
154
	lp = hp->monList;
155
	while (lp) {
156
		if (!strncasecmp(idp->my_name, lp->notifyHost, SM_MAXSTRLEN)
157
		    && (idp->my_prog == lp->notifyProg)
158
		    && (idp->my_proc == lp->notifyProc)
159
		    && (idp->my_vers == lp->notifyVers)) {
160
			/* found one.  Unhook from chain and free. */
161
			next = lp->next;
162
			if (last)
163
				last->next = next;
164
			else
165
				hp->monList = next;
166
			free(lp);
167
			lp = next;
168
			result = TRUE;
169
		} else {
170
			last = lp;
171
			lp = lp->next;
172
		}
173
	}
174
	return (result);
175
}
176
177
/* sm_unmon_1 -------------------------------------------------------------- */
178
/*
179
 * Purpose:	RPC procedure to release a monitor request.
180
 * Returns:	Local machine's status number
181
 * Notes:	The supplied mon_id should match the value passed in an
182
 *		earlier call to sm_mon_1
183
 */
184
struct sm_stat *
185
sm_unmon_1_svc(mon_id *arg, struct svc_req *req)
186
{
187
	static sm_stat res;
188
	HostInfo *hp, h;
189
190
	NO_ALARM;
191
	if (debug) {
192
		syslog(LOG_DEBUG, "un-monitor request for host %s",
193
		    arg->mon_name);
194
		syslog(LOG_DEBUG, "recall host: %s prog: %d ver: %d proc: %d",
195
		    arg->my_id.my_name, arg->my_id.my_prog,
196
		    arg->my_id.my_vers, arg->my_id.my_proc);
197
	}
198
	if ((hp = find_host(arg->mon_name, &h)) != NULL) {
199
		if (do_unmon(arg->mon_name, hp, &arg->my_id)) {
200
			change_host(arg->mon_name, hp);
201
			sync_file();
202
		} else
203
			syslog(LOG_ERR,
204
			    "unmon request from %s, no matching monitor",
205
			    arg->my_id.my_name);
206
	} else
207
		syslog(LOG_ERR, "unmon request from %s for unknown host %s",
208
		    arg->my_id.my_name, arg->mon_name);
209
210
	res.state = status_info.ourState;
211
	ALARM;
212
213
	return (&res);
214
}
215
216
/* sm_unmon_all_1 ---------------------------------------------------------- */
217
/*
218
 * Purpose:	RPC procedure to release monitor requests.
219
 * Returns:	Local machine's status number
220
 * Notes:	Releases all monitor requests (if any) from the specified
221
 *		host and program number.
222
 */
223
struct sm_stat *
224
sm_unmon_all_1_svc(my_id *arg, struct svc_req *req)
225
{
226
	static sm_stat res;
227
228
	NO_ALARM;
229
	if (debug) {
230
		syslog(LOG_DEBUG,
231
		    "unmon_all for host: %s prog: %d ver: %d proc: %d",
232
		    arg->my_name, arg->my_prog, arg->my_vers, arg->my_proc);
233
	}
234
235
	unmon_hosts();
236
	sync_file();
237
238
	res.state = status_info.ourState;
239
	ALARM;
240
241
	return (&res);
242
}
243
244
/* sm_simu_crash_1 --------------------------------------------------------- */
245
/*
246
 * Purpose:	RPC procedure to simulate a crash
247
 * Returns:	Nothing
248
 * Notes:	Standardised mechanism for debug purposes
249
 *		The specification says that we should drop all of our
250
 *		status information (apart from the list of monitored hosts
251
 *		on disc).  However, this would confuse the rpc.lockd
252
 *		which would be unaware that all of its monitor requests
253
 *		had been silently junked.  Hence we in fact retain all
254
 *		current requests and simply increment the status counter
255
 *		and inform all hosts on the monitor list.
256
 */
257
void *
258
sm_simu_crash_1_svc(void *v, struct svc_req *req)
259
{
260
	static char dummy;
261
262
	NO_ALARM;
263
	if (debug)
264
		syslog(LOG_DEBUG, "simu_crash called!!");
265
266
	reset_database();
267
	ALARM;
268
	notify_handler(0);
269
270
	return (&dummy);
271
}
272
273
/* sm_notify_1 ------------------------------------------------------------- */
274
/*
275
 * Purpose:	RPC procedure notifying local statd of the crash of another
276
 * Returns:	Nothing
277
 * Notes:	There is danger of deadlock, since it is quite likely that
278
 *		the client procedure that we call will in turn call us
279
 *		to remove or adjust the monitor request.
280
 *		We therefore fork() a process to do the notifications.
281
 *		Note that the main HostInfo structure is in a mmap()
282
 *		region and so will be shared with the child, but the
283
 *		monList pointed to by the HostInfo is in normal memory.
284
 *		Hence if we read the monList before forking, we are
285
 *		protected from the parent servicing other requests
286
 *		that modify the list.
287
 */
288
void   *
289
sm_notify_1_svc(stat_chge *arg, struct svc_req *req)
290
{
291
	struct timeval timeout = {20, 0};	/* 20 secs timeout */
292
	CLIENT *cli;
293
	static char dummy;
294
	status tx_arg;		/* arg sent to callback procedure */
295
	MonList *lp;
296
	HostInfo *hp, h;
297
	pid_t pid;
298
299
	if (debug)
300
		syslog(LOG_DEBUG, "notify from host %s, new state %d",
301
		    arg->mon_name, arg->state);
302
303
	hp = find_host(arg->mon_name, &h);
304
	if (!hp) {
305
		/* Never heard of this host - why is it notifying us? */
306
		syslog(LOG_DEBUG, "Unsolicited notification from host %s",
307
		    arg->mon_name);
308
		return (&dummy);
309
	}
310
	lp = hp->monList;
311
	if (!lp) /* We know this host, but have no outstanding requests. */
312
		return (&dummy);
313
314
	sync_file();
315
	pid = fork();
316
	if (pid == -1) {
317
		syslog(LOG_ERR, "Unable to fork notify process - %s",
318
		    strerror(errno));
319
		return (FALSE);
320
	}
321
	if (pid)
322
		return (&dummy); /* Parent returns */
323
324
	while (lp) {
325
		tx_arg.mon_name = arg->mon_name;
326
		tx_arg.state = arg->state;
327
		memcpy(tx_arg.priv, lp->notifyData, sizeof(tx_arg.priv));
328
		cli = clnt_create(lp->notifyHost, lp->notifyProg,
329
		    lp->notifyVers, "udp");
330
		if (!cli)
331
			syslog(LOG_ERR, "Failed to contact host %s%s",
332
			    lp->notifyHost, clnt_spcreateerror(""));
333
		else {
334
			if (clnt_call(cli, lp->notifyProc, xdr_status, &tx_arg,
335
			    xdr_void, &dummy, timeout) != RPC_SUCCESS)
336
				syslog(LOG_ERR,
337
				    "Failed to call rpc.statd client at host %s",
338
				    lp->notifyHost);
339
			clnt_destroy(cli);
340
		}
341
		lp = lp->next;
342
	}
343
344
	exit(0);		/* Child quits */
345
}