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