1 /* $OpenBSD: yppoll.c,v 1.15 2015/01/16 06:40:22 deraadt Exp $ */
2 /* $NetBSD: yppoll.c,v 1.5 1996/05/13 02:46:36 thorpej Exp $ */
5 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@openbsd.org>
8 * Copyright (c) 1992, 1993 John Brezak
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote
20 * products derived from this software without specific prior written
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
24 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
27 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
43 #include <arpa/inet.h>
44 #include <netinet/in.h>
57 #include <rpcsvc/yp_prot.h>
58 #include <rpcsvc/ypclnt.h>
63 fprintf(stderr, "usage: yppoll [-d domain] [-h host] mapname\n");
68 get_remote_info(char *indomain, char *inmap, char *server, int *outorder,
71 struct ypresp_order ypro;
72 struct ypresp_master yprm;
73 struct ypreq_nokey yprnk;
75 struct sockaddr_in rsrv_sin;
81 bzero((char *)&rsrv_sin, sizeof rsrv_sin);
82 rsrv_sin.sin_len = sizeof rsrv_sin;
83 rsrv_sin.sin_family = AF_INET;
84 rsrv_sock = RPC_ANYSOCK;
86 h = gethostbyname(server);
88 if (inet_aton(server, &rsrv_sin.sin_addr) == 0)
89 errx(1, "unknown host %s.", server);
91 rsrv_sin.sin_addr.s_addr = *(u_int32_t *)h->h_addr;
96 client = clntudp_create(&rsrv_sin, YPPROG, YPVERS, tv, &rsrv_sock);
98 errx(1, "clntudp_create: no contact with host %s.", server);
100 yprnk.domain = indomain;
103 bzero((char *)(char *)&ypro, sizeof ypro);
105 r = clnt_call(client, YPPROC_ORDER, (xdrproc_t)xdr_ypreq_nokey, &yprnk,
106 (xdrproc_t)xdr_ypresp_order, &ypro, tv);
107 if (r != RPC_SUCCESS)
108 clnt_perror(client, "yp_order: clnt_call");
110 *outorder = ypro.ordernum;
111 xdr_free((xdrproc_t)xdr_ypresp_order, (char *)&ypro);
113 r = ypprot_err(ypro.status);
114 if (r == RPC_SUCCESS) {
115 bzero((char *)&yprm, sizeof yprm);
117 r = clnt_call(client, YPPROC_MASTER, (xdrproc_t)xdr_ypreq_nokey,
118 &yprnk, (xdrproc_t)xdr_ypresp_master, &yprm, tv);
119 if (r != RPC_SUCCESS)
120 clnt_perror(client, "yp_master: clnt_call");
121 r = ypprot_err(yprm.status);
123 *outname = (char *)strdup(yprm.master);
124 xdr_free((xdrproc_t)xdr_ypresp_master, (char *)&yprm);
126 clnt_destroy(client);
131 main(int argc, char *argv[])
133 char *domainname, *hostname = NULL, *inmap, *master;
137 yp_get_default_domain(&domainname);
139 while ((c = getopt(argc, argv, "h:d:")) != -1)
152 if (optind + 1 != argc)
154 inmap = argv[optind];
156 if (hostname != NULL) {
157 r = get_remote_info(domainname, inmap, hostname,
160 r = yp_order(domainname, inmap, &order);
162 r = yp_master(domainname, inmap, &master);
166 errx(1, "no such map %s: reason: %s",
167 inmap, yperr_string(r));
170 printf("Map %s has order number %lld. %s", inmap,
171 (long long)order, ctime(&torder));
172 printf("The master server is %s.\n", master);