xref: /freebsd/usr.sbin/yppoll/yppoll.c (revision b9f654b163bce26de79705e77b872427c9f2afa1)
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 $	*/
3 
4 /*-
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@openbsd.org>
8  * Copyright (c) 1992, 1993 John Brezak
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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
21  *    permission.
22  *
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
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 
43 #include <arpa/inet.h>
44 #include <netinet/in.h>
45 
46 #include <ctype.h>
47 #include <err.h>
48 #include <netdb.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <time.h>
53 #include <unistd.h>
54 
55 #include <rpc/rpc.h>
56 #include <rpc/xdr.h>
57 #include <rpcsvc/yp_prot.h>
58 #include <rpcsvc/ypclnt.h>
59 
60 static void
61 usage(void)
62 {
63 	fprintf(stderr, "usage: yppoll [-d domain] [-h host] mapname\n");
64 	exit(1);
65 }
66 
67 static int
68 get_remote_info(char *indomain, char *inmap, char *server, int *outorder,
69     char **outname)
70 {
71 	struct ypresp_order ypro;
72 	struct ypresp_master yprm;
73 	struct ypreq_nokey yprnk;
74 	struct timeval tv;
75 	struct sockaddr_in rsrv_sin;
76 	int rsrv_sock;
77 	CLIENT *client;
78 	struct hostent *h;
79 	int r;
80 
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;
85 
86 	h = gethostbyname(server);
87 	if (h == NULL) {
88 		if (inet_aton(server, &rsrv_sin.sin_addr) == 0)
89 			errx(1, "unknown host %s.", server);
90 	} else
91 		rsrv_sin.sin_addr.s_addr = *(u_int32_t *)h->h_addr;
92 
93 	tv.tv_sec = 10;
94 	tv.tv_usec = 0;
95 
96 	client = clntudp_create(&rsrv_sin, YPPROG, YPVERS, tv, &rsrv_sock);
97 	if (client == NULL)
98 		errx(1, "clntudp_create: no contact with host %s.", server);
99 
100 	yprnk.domain = indomain;
101 	yprnk.map = inmap;
102 
103 	bzero((char *)(char *)&ypro, sizeof ypro);
104 
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");
109 
110 	*outorder = ypro.ordernum;
111 	xdr_free((xdrproc_t)xdr_ypresp_order, (char *)&ypro);
112 
113 	r = ypprot_err(ypro.status);
114 	if (r == RPC_SUCCESS) {
115 		bzero((char *)&yprm, sizeof yprm);
116 
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);
122 		if (r == 0)
123 			*outname = (char *)strdup(yprm.master);
124 		xdr_free((xdrproc_t)xdr_ypresp_master, (char *)&yprm);
125 	}
126 	clnt_destroy(client);
127 	return (r);
128 }
129 
130 int
131 main(int argc, char *argv[])
132 {
133 	char *domainname, *hostname = NULL, *inmap, *master;
134 	int order, c, r;
135 	time_t torder;
136 
137 	yp_get_default_domain(&domainname);
138 
139 	while ((c = getopt(argc, argv, "h:d:")) != -1)
140 		switch (c) {
141 		case 'd':
142 			domainname = optarg;
143 			break;
144 		case 'h':
145 			hostname = optarg;
146 			break;
147 		default:
148 			usage();
149 			/*NOTREACHED*/
150 		}
151 
152 	if (optind + 1 != argc)
153 		usage();
154 	inmap = argv[optind];
155 
156 	if (hostname != NULL) {
157 		r = get_remote_info(domainname, inmap, hostname,
158 		    &order, &master);
159 	} else {
160 		r = yp_order(domainname, inmap, &order);
161 		if (r == 0)
162 			r = yp_master(domainname, inmap, &master);
163 	}
164 
165 	if (r != 0)
166 		errx(1, "no such map %s: reason: %s",
167 		    inmap, yperr_string(r));
168 
169 	torder = order;
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);
173 
174 	exit(0);
175 }
176