xref: /freebsd/usr.sbin/ypbind/yp_ping.c (revision 911f0260390e18cf85f3dbf2c719b593efdc1e3c)
1 /*
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1996, 1997
5  *	Bill Paul <wpaul@ctr.columbia.edu>.  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 by Bill Paul.
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 Bill Paul 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 Bill Paul 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  * Copyright (c) 2009, Sun Microsystems, Inc.
37  * All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions are met:
41  * - Redistributions of source code must retain the above copyright notice,
42  *   this list of conditions and the following disclaimer.
43  * - Redistributions in binary form must reproduce the above copyright notice,
44  *   this list of conditions and the following disclaimer in the documentation
45  *   and/or other materials provided with the distribution.
46  * - Neither the name of Sun Microsystems, Inc. nor the names of its
47  *   contributors may be used to endorse or promote products derived
48  *   from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
51  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
54  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60  * POSSIBILITY OF SUCH DAMAGE.
61  */
62 #if 0
63 #ifndef lint
64 static char *sccsid = "@(#)from: clnt_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro";
65 static char *sccsid = "@(#)from: clnt_udp.c	2.2 88/08/01 4.0 RPCSRC";
66 #endif
67 #endif
68 #include <sys/cdefs.h>
69 __FBSDID("$FreeBSD$");
70 
71 /*
72  * clnt_udp.c, Implements a UDP/IP based, client side RPC.
73  *
74  * Copyright (C) 1984, Sun Microsystems, Inc.
75  */
76 
77 #include <errno.h>
78 #include <netdb.h>
79 #include <stdio.h>
80 #include <stdlib.h>
81 #include <string.h>
82 #include <unistd.h>
83 #include <pthread.h>
84 #include <rpc/rpc.h>
85 #include <rpc/pmap_clnt.h>
86 #include <rpc/pmap_prot.h>
87 #include <rpcsvc/yp.h>
88 #include <sys/types.h>
89 #include <sys/poll.h>
90 #include <sys/socket.h>
91 #include <sys/signal.h>
92 #include <sys/ioctl.h>
93 #include <arpa/inet.h>
94 #include <net/if.h>
95 
96 #include "yp_ping.h"
97 
98 /*
99  * pmap_getport.c
100  * Client interface to pmap rpc service.
101  *
102  * Copyright (C) 1984, Sun Microsystems, Inc.
103  */
104 
105 
106 static struct timeval timeout = { 1, 0 };
107 static struct timeval tottimeout = { 1, 0 };
108 
109 /*
110  * Find the mapped port for program,version.
111  * Calls the pmap service remotely to do the lookup.
112  * Returns 0 if no map exists.
113  */
114 static u_short
115 __pmap_getport(struct sockaddr_in *address, u_long program, u_long version,
116     u_int protocol)
117 {
118 	u_short port = 0;
119 	int sock = -1;
120 	register CLIENT *client;
121 	struct pmap parms;
122 
123 	address->sin_port = htons(PMAPPORT);
124 
125 	client = clntudp_bufcreate(address, PMAPPROG,
126 	    PMAPVERS, timeout, &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
127 	if (client != (CLIENT *)NULL) {
128 		parms.pm_prog = program;
129 		parms.pm_vers = version;
130 		parms.pm_prot = protocol;
131 		parms.pm_port = 0;  /* not needed or used */
132 		if (CLNT_CALL(client, PMAPPROC_GETPORT,
133 			(xdrproc_t)xdr_pmap, &parms,
134 			(xdrproc_t)xdr_u_short, &port,
135 			tottimeout) != RPC_SUCCESS){
136 			rpc_createerr.cf_stat = RPC_PMAPFAILURE;
137 			clnt_geterr(client, &rpc_createerr.cf_error);
138 		} else if (port == 0) {
139 			rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
140 		}
141 		CLNT_DESTROY(client);
142 	}
143 	if (sock != -1)
144 		(void)close(sock);
145 	address->sin_port = 0;
146 	return (port);
147 }
148 
149 /*
150  * Transmit to YPPROC_DOMAIN_NONACK, return immediately.
151  */
152 static bool_t *
153 ypproc_domain_nonack_2_send(domainname *argp, CLIENT *clnt)
154 {
155 	static bool_t clnt_res;
156 	struct timeval TIMEOUT = { 0, 0 };
157 
158 	memset((char *)&clnt_res, 0, sizeof (clnt_res));
159 	if (clnt_call(clnt, YPPROC_DOMAIN_NONACK,
160 		(xdrproc_t) xdr_domainname, (caddr_t) argp,
161 		(xdrproc_t) xdr_bool, (caddr_t) &clnt_res,
162 		TIMEOUT) != RPC_SUCCESS) {
163 		return (NULL);
164 	}
165 	return (&clnt_res);
166 }
167 
168 /*
169  * Receive response from YPPROC_DOMAIN_NONACK asynchronously.
170  */
171 static bool_t *
172 ypproc_domain_nonack_2_recv(domainname *argp, CLIENT *clnt)
173 {
174 	static bool_t clnt_res;
175 	struct timeval TIMEOUT = { 0, 0 };
176 
177 	memset((char *)&clnt_res, 0, sizeof (clnt_res));
178 	if (clnt_call(clnt, YPPROC_DOMAIN_NONACK,
179 		(xdrproc_t) NULL, (caddr_t) argp,
180 		(xdrproc_t) xdr_bool, (caddr_t) &clnt_res,
181 		TIMEOUT) != RPC_SUCCESS) {
182 		return (NULL);
183 	}
184 	return (&clnt_res);
185 }
186 
187 /*
188  * "We have the machine that goes 'ping!'" -- Monty Python
189  *
190  * This function blasts packets at the YPPROC_DOMAIN_NONACK procedures
191  * of the NIS servers listed in restricted_addrs structure.
192  * Whoever replies the fastest becomes our chosen server.
193  *
194  * Note: THIS IS NOT A BROADCAST OPERATION! We could use clnt_broadcast()
195  * for this, but that has the following problems:
196  * - We only get the address of the machine that replied in the
197  *   'eachresult' callback, and on multi-homed machines this can
198  *   lead to confusion.
199  * - clnt_broadcast() only transmits to local networks, whereas with
200  *   NIS+ you can have a perfectly good server located anywhere on or
201  *   off the local network.
202  * - clnt_broadcast() blocks for an arbitrary amount of time which the
203  *   caller can't control -- we want to avoid that.
204  *
205  * Also note that this has nothing to do with the NIS_PING procedure used
206  * for replica updates.
207  */
208 
209 struct ping_req {
210 	struct sockaddr_in	sin;
211 	u_int32_t		xid;
212 };
213 
214 int
215 __yp_ping(struct in_addr *restricted_addrs, int cnt, char *dom, short *port)
216 {
217 	struct timeval		tv = { 5, 0 };
218 	struct ping_req		**reqs;
219 	unsigned long		i;
220 	int			async;
221 	struct sockaddr_in	sin, *any = NULL;
222 	struct netbuf		addr;
223 	int			winner = -1;
224 	u_int32_t		xid_seed, xid_lookup;
225 	int			sock, dontblock = 1;
226 	CLIENT			*clnt;
227 	char			*foo = dom;
228 	int			validsrvs = 0;
229 
230 	/* Set up handles. */
231 	reqs = calloc(cnt, sizeof(struct ping_req *));
232 	xid_seed = time(NULL) ^ getpid();
233 
234 	for (i = 0; i < cnt; i++) {
235 		bzero((char *)&sin, sizeof(sin));
236 		sin.sin_family = AF_INET;
237 		bcopy((char *)&restricted_addrs[i],
238 			(char *)&sin.sin_addr, sizeof(struct in_addr));
239 		sin.sin_port = htons(__pmap_getport(&sin, YPPROG,
240 					YPVERS, IPPROTO_UDP));
241 		if (sin.sin_port == 0)
242 			continue;
243 		reqs[i] = calloc(1, sizeof(struct ping_req));
244 		bcopy((char *)&sin, (char *)&reqs[i]->sin, sizeof(sin));
245 		any = &reqs[i]->sin;
246 		reqs[i]->xid = xid_seed;
247 		xid_seed++;
248 		validsrvs++;
249 	}
250 
251 	/* Make sure at least one server was assigned */
252 	if (!validsrvs) {
253 		free(reqs);
254 		return(-1);
255 	}
256 
257 	/* Create RPC handle */
258 	sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
259 	clnt = clntudp_create(any, YPPROG, YPVERS, tv, &sock);
260 	if (clnt == NULL) {
261 		close(sock);
262 		for (i = 0; i < cnt; i++)
263 			if (reqs[i] != NULL)
264 				free(reqs[i]);
265 		free(reqs);
266 		return(-1);
267 	}
268 	clnt->cl_auth = authunix_create_default();
269 	tv.tv_sec = 0;
270 
271 	clnt_control(clnt, CLSET_TIMEOUT, (char *)&tv);
272 	async = TRUE;
273 	clnt_control(clnt, CLSET_ASYNC, (char *)&async);
274 	ioctl(sock, FIONBIO, &dontblock);
275 
276 	/* Transmit */
277 	for (i = 0; i < cnt; i++) {
278 		if (reqs[i] != NULL) {
279 			clnt_control(clnt, CLSET_XID, (char *)&reqs[i]->xid);
280 			addr.len = sizeof(reqs[i]->sin);
281 			addr.buf = (char *) &reqs[i]->sin;
282 			clnt_control(clnt, CLSET_SVC_ADDR, &addr);
283 			ypproc_domain_nonack_2_send(&foo, clnt);
284 		}
285 	}
286 
287 	/* Receive reply */
288 	ypproc_domain_nonack_2_recv(&foo, clnt);
289 
290 	/* Got a winner -- look him up. */
291 	clnt_control(clnt, CLGET_XID, (char *)&xid_lookup);
292 	for (i = 0; i < cnt; i++) {
293 		if (reqs[i] != NULL && reqs[i]->xid == xid_lookup) {
294 			winner = i;
295 			*port = reqs[i]->sin.sin_port;
296 		}
297 	}
298 
299 	/* Shut everything down */
300 	auth_destroy(clnt->cl_auth);
301 	clnt_destroy(clnt);
302 	close(sock);
303 
304 	for (i = 0; i < cnt; i++)
305 		if (reqs[i] != NULL)
306 			free(reqs[i]);
307 	free(reqs);
308 
309 	return(winner);
310 }
311