xref: /freebsd/usr.sbin/yppoll/yppoll.c (revision b2d48be1bc7df45ddd13b143a160d0acb5a383c5)
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  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@openbsd.org>
6  * Copyright (c) 1992, 1993 John Brezak
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote
18  *    products derived from this software without specific prior written
19  *    permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 
41 #include <arpa/inet.h>
42 #include <netinet/in.h>
43 
44 #include <ctype.h>
45 #include <err.h>
46 #include <netdb.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <time.h>
51 #include <unistd.h>
52 
53 #include <rpc/rpc.h>
54 #include <rpc/xdr.h>
55 #include <rpcsvc/yp_prot.h>
56 #include <rpcsvc/ypclnt.h>
57 
58 static void
59 usage(void)
60 {
61 	fprintf(stderr, "usage: yppoll [-d domain] [-h host] mapname\n");
62 	exit(1);
63 }
64 
65 static int
66 get_remote_info(char *indomain, char *inmap, char *server, int *outorder,
67     char **outname)
68 {
69 	struct ypresp_order ypro;
70 	struct ypresp_master yprm;
71 	struct ypreq_nokey yprnk;
72 	struct timeval tv;
73 	struct sockaddr_in rsrv_sin;
74 	int rsrv_sock;
75 	CLIENT *client;
76 	struct hostent *h;
77 	int r;
78 
79 	bzero((char *)&rsrv_sin, sizeof rsrv_sin);
80 	rsrv_sin.sin_len = sizeof rsrv_sin;
81 	rsrv_sin.sin_family = AF_INET;
82 	rsrv_sock = RPC_ANYSOCK;
83 
84 	h = gethostbyname(server);
85 	if (h == NULL) {
86 		if (inet_aton(server, &rsrv_sin.sin_addr) == 0)
87 			errx(1, "unknown host %s.", server);
88 	} else
89 		rsrv_sin.sin_addr.s_addr = *(u_int32_t *)h->h_addr;
90 
91 	tv.tv_sec = 10;
92 	tv.tv_usec = 0;
93 
94 	client = clntudp_create(&rsrv_sin, YPPROG, YPVERS, tv, &rsrv_sock);
95 	if (client == NULL)
96 		errx(1, "clntudp_create: no contact with host %s.", server);
97 
98 	yprnk.domain = indomain;
99 	yprnk.map = inmap;
100 
101 	bzero((char *)(char *)&ypro, sizeof ypro);
102 
103 	r = clnt_call(client, YPPROC_ORDER, (xdrproc_t)xdr_ypreq_nokey, &yprnk,
104 	    (xdrproc_t)xdr_ypresp_order, &ypro, tv);
105 	if (r != RPC_SUCCESS)
106 		clnt_perror(client, "yp_order: clnt_call");
107 
108 	*outorder = ypro.ordernum;
109 	xdr_free((xdrproc_t)xdr_ypresp_order, (char *)&ypro);
110 
111 	r = ypprot_err(ypro.status);
112 	if (r == RPC_SUCCESS) {
113 		bzero((char *)&yprm, sizeof yprm);
114 
115 		r = clnt_call(client, YPPROC_MASTER, (xdrproc_t)xdr_ypreq_nokey,
116 		    &yprnk, (xdrproc_t)xdr_ypresp_master, &yprm, tv);
117 		if (r != RPC_SUCCESS)
118 			clnt_perror(client, "yp_master: clnt_call");
119 		r = ypprot_err(yprm.status);
120 		if (r == 0)
121 			*outname = (char *)strdup(yprm.master);
122 		xdr_free((xdrproc_t)xdr_ypresp_master, (char *)&yprm);
123 	}
124 	clnt_destroy(client);
125 	return (r);
126 }
127 
128 int
129 main(int argc, char *argv[])
130 {
131 	char *domainname, *hostname = NULL, *inmap, *master;
132 	int order, c, r;
133 	time_t torder;
134 
135 	yp_get_default_domain(&domainname);
136 
137 	while ((c = getopt(argc, argv, "h:d:")) != -1)
138 		switch (c) {
139 		case 'd':
140 			domainname = optarg;
141 			break;
142 		case 'h':
143 			hostname = optarg;
144 			break;
145 		default:
146 			usage();
147 			/*NOTREACHED*/
148 		}
149 
150 	if (optind + 1 != argc)
151 		usage();
152 	inmap = argv[optind];
153 
154 	if (hostname != NULL) {
155 		r = get_remote_info(domainname, inmap, hostname,
156 		    &order, &master);
157 	} else {
158 		r = yp_order(domainname, inmap, &order);
159 		if (r == 0)
160 			r = yp_master(domainname, inmap, &master);
161 	}
162 
163 	if (r != 0)
164 		errx(1, "no such map %s: reason: %s",
165 		    inmap, yperr_string(r));
166 
167 	torder = order;
168 	printf("Map %s has order number %lld. %s", inmap,
169 	    (long long)order, ctime(&torder));
170 	printf("The master server is %s.\n", master);
171 
172 	exit(0);
173 }
174