]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ypbind/yp_ping.c
makefs: warn that ffs sectorsize other than 512 may not work
[FreeBSD/FreeBSD.git] / usr.sbin / ypbind / yp_ping.c
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 /*
63  * clnt_udp.c, Implements a UDP/IP based, client side RPC.
64  *
65  * Copyright (C) 1984, Sun Microsystems, Inc.
66  */
67
68 #include <errno.h>
69 #include <netdb.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <unistd.h>
74 #include <pthread.h>
75 #include <rpc/rpc.h>
76 #include <rpc/pmap_clnt.h>
77 #include <rpc/pmap_prot.h>
78 #include <rpcsvc/yp.h>
79 #include <sys/types.h>
80 #include <sys/poll.h>
81 #include <sys/socket.h>
82 #include <sys/signal.h>
83 #include <sys/ioctl.h>
84 #include <arpa/inet.h>
85 #include <net/if.h>
86
87 #include "yp_ping.h"
88
89 /*
90  * pmap_getport.c
91  * Client interface to pmap rpc service.
92  *
93  * Copyright (C) 1984, Sun Microsystems, Inc.
94  */
95
96
97 static struct timeval timeout = { 1, 0 };
98 static struct timeval tottimeout = { 1, 0 };
99
100 /*
101  * Find the mapped port for program,version.
102  * Calls the pmap service remotely to do the lookup.
103  * Returns 0 if no map exists.
104  */
105 static u_short
106 __pmap_getport(struct sockaddr_in *address, u_long program, u_long version,
107     u_int protocol)
108 {
109         u_short port = 0;
110         int sock = -1;
111         register CLIENT *client;
112         struct pmap parms;
113
114         address->sin_port = htons(PMAPPORT);
115
116         client = clntudp_bufcreate(address, PMAPPROG,
117             PMAPVERS, timeout, &sock, RPCSMALLMSGSIZE, RPCSMALLMSGSIZE);
118         if (client != (CLIENT *)NULL) {
119                 parms.pm_prog = program;
120                 parms.pm_vers = version;
121                 parms.pm_prot = protocol;
122                 parms.pm_port = 0;  /* not needed or used */
123                 if (CLNT_CALL(client, PMAPPROC_GETPORT,
124                         (xdrproc_t)xdr_pmap, &parms,
125                         (xdrproc_t)xdr_u_short, &port,
126                         tottimeout) != RPC_SUCCESS){
127                         rpc_createerr.cf_stat = RPC_PMAPFAILURE;
128                         clnt_geterr(client, &rpc_createerr.cf_error);
129                 } else if (port == 0) {
130                         rpc_createerr.cf_stat = RPC_PROGNOTREGISTERED;
131                 }
132                 CLNT_DESTROY(client);
133         }
134         if (sock != -1)
135                 (void)close(sock);
136         address->sin_port = 0;
137         return (port);
138 }
139
140 /*
141  * Transmit to YPPROC_DOMAIN_NONACK, return immediately.
142  */
143 static bool_t *
144 ypproc_domain_nonack_2_send(domainname *argp, CLIENT *clnt)
145 {
146         static bool_t clnt_res;
147         struct timeval TIMEOUT = { 0, 0 };
148
149         memset((char *)&clnt_res, 0, sizeof (clnt_res));
150         if (clnt_call(clnt, YPPROC_DOMAIN_NONACK,
151                 (xdrproc_t) xdr_domainname, (caddr_t) argp,
152                 (xdrproc_t) xdr_bool, (caddr_t) &clnt_res,
153                 TIMEOUT) != RPC_SUCCESS) {
154                 return (NULL);
155         }
156         return (&clnt_res);
157 }
158
159 /*
160  * Receive response from YPPROC_DOMAIN_NONACK asynchronously.
161  */
162 static bool_t *
163 ypproc_domain_nonack_2_recv(domainname *argp, CLIENT *clnt)
164 {
165         static bool_t clnt_res;
166         struct timeval TIMEOUT = { 0, 0 };
167
168         memset((char *)&clnt_res, 0, sizeof (clnt_res));
169         if (clnt_call(clnt, YPPROC_DOMAIN_NONACK,
170                 (xdrproc_t) NULL, (caddr_t) argp,
171                 (xdrproc_t) xdr_bool, (caddr_t) &clnt_res,
172                 TIMEOUT) != RPC_SUCCESS) {
173                 return (NULL);
174         }
175         return (&clnt_res);
176 }
177
178 /*
179  * "We have the machine that goes 'ping!'" -- Monty Python
180  *
181  * This function blasts packets at the YPPROC_DOMAIN_NONACK procedures
182  * of the NIS servers listed in restricted_addrs structure.
183  * Whoever replies the fastest becomes our chosen server.
184  *
185  * Note: THIS IS NOT A BROADCAST OPERATION! We could use clnt_broadcast()
186  * for this, but that has the following problems:
187  * - We only get the address of the machine that replied in the
188  *   'eachresult' callback, and on multi-homed machines this can
189  *   lead to confusion.
190  * - clnt_broadcast() only transmits to local networks, whereas with
191  *   NIS+ you can have a perfectly good server located anywhere on or
192  *   off the local network.
193  * - clnt_broadcast() blocks for an arbitrary amount of time which the
194  *   caller can't control -- we want to avoid that.
195  *
196  * Also note that this has nothing to do with the NIS_PING procedure used
197  * for replica updates.
198  */
199
200 struct ping_req {
201         struct sockaddr_in      sin;
202         u_int32_t               xid;
203 };
204
205 int
206 __yp_ping(struct in_addr *restricted_addrs, int cnt, char *dom, short *port)
207 {
208         struct timeval          tv = { 5, 0 };
209         struct ping_req         **reqs;
210         unsigned long           i;
211         int                     async;
212         struct sockaddr_in      sin, *any = NULL;
213         struct netbuf           addr;
214         int                     winner = -1;
215         u_int32_t               xid_seed, xid_lookup;
216         int                     sock, dontblock = 1;
217         CLIENT                  *clnt;
218         char                    *foo = dom;
219         int                     validsrvs = 0;
220
221         /* Set up handles. */
222         reqs = calloc(cnt, sizeof(struct ping_req *));
223         xid_seed = time(NULL) ^ getpid();
224
225         for (i = 0; i < cnt; i++) {
226                 bzero((char *)&sin, sizeof(sin));
227                 sin.sin_family = AF_INET;
228                 bcopy((char *)&restricted_addrs[i],
229                         (char *)&sin.sin_addr, sizeof(struct in_addr));
230                 sin.sin_port = htons(__pmap_getport(&sin, YPPROG,
231                                         YPVERS, IPPROTO_UDP));
232                 if (sin.sin_port == 0)
233                         continue;
234                 reqs[i] = calloc(1, sizeof(struct ping_req));
235                 bcopy((char *)&sin, (char *)&reqs[i]->sin, sizeof(sin));
236                 any = &reqs[i]->sin;
237                 reqs[i]->xid = xid_seed;
238                 xid_seed++;
239                 validsrvs++;
240         }
241
242         /* Make sure at least one server was assigned */
243         if (!validsrvs) {
244                 free(reqs);
245                 return(-1);
246         }
247
248         /* Create RPC handle */
249         sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
250         clnt = clntudp_create(any, YPPROG, YPVERS, tv, &sock);
251         if (clnt == NULL) {
252                 close(sock);
253                 for (i = 0; i < cnt; i++)
254                         if (reqs[i] != NULL)
255                                 free(reqs[i]);
256                 free(reqs);
257                 return(-1);
258         }
259         clnt->cl_auth = authunix_create_default();
260         tv.tv_sec = 0;
261
262         clnt_control(clnt, CLSET_TIMEOUT, (char *)&tv);
263         async = TRUE;
264         clnt_control(clnt, CLSET_ASYNC, (char *)&async);
265         ioctl(sock, FIONBIO, &dontblock);
266
267         /* Transmit */
268         for (i = 0; i < cnt; i++) {
269                 if (reqs[i] != NULL) {
270                         clnt_control(clnt, CLSET_XID, (char *)&reqs[i]->xid);
271                         addr.len = sizeof(reqs[i]->sin);
272                         addr.buf = (char *) &reqs[i]->sin;
273                         clnt_control(clnt, CLSET_SVC_ADDR, &addr);
274                         ypproc_domain_nonack_2_send(&foo, clnt);
275                 }
276         }
277
278         /* Receive reply */
279         ypproc_domain_nonack_2_recv(&foo, clnt);
280
281         /* Got a winner -- look him up. */
282         clnt_control(clnt, CLGET_XID, (char *)&xid_lookup);
283         for (i = 0; i < cnt; i++) {
284                 if (reqs[i] != NULL && reqs[i]->xid == xid_lookup) {
285                         winner = i;
286                         *port = reqs[i]->sin.sin_port;
287                 }
288         }
289
290         /* Shut everything down */
291         auth_destroy(clnt->cl_auth);
292         clnt_destroy(clnt);
293         close(sock);
294
295         for (i = 0; i < cnt; i++)
296                 if (reqs[i] != NULL)
297                         free(reqs[i]);
298         free(reqs);
299
300         return(winner);
301 }