xref: /freebsd/usr.sbin/yppoll/yppoll.c (revision d1bdc2821fcd416ab9b238580386eb605a6128d0)
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/param.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 
40 #include <arpa/inet.h>
41 #include <netinet/in.h>
42 
43 #include <ctype.h>
44 #include <err.h>
45 #include <netdb.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <time.h>
50 #include <unistd.h>
51 
52 #include <rpc/rpc.h>
53 #include <rpc/xdr.h>
54 #include <rpcsvc/yp_prot.h>
55 #include <rpcsvc/ypclnt.h>
56 
57 static void
58 usage(void)
59 {
60 	fprintf(stderr, "usage: yppoll [-d domain] [-h host] mapname\n");
61 	exit(1);
62 }
63 
64 static int
65 get_remote_info(char *indomain, char *inmap, char *server, int *outorder,
66     char **outname)
67 {
68 	struct ypresp_order ypro;
69 	struct ypresp_master yprm;
70 	struct ypreq_nokey yprnk;
71 	struct timeval tv;
72 	struct sockaddr_in rsrv_sin;
73 	int rsrv_sock;
74 	CLIENT *client;
75 	struct hostent *h;
76 	int r;
77 
78 	bzero((char *)&rsrv_sin, sizeof rsrv_sin);
79 	rsrv_sin.sin_len = sizeof rsrv_sin;
80 	rsrv_sin.sin_family = AF_INET;
81 	rsrv_sock = RPC_ANYSOCK;
82 
83 	h = gethostbyname(server);
84 	if (h == NULL) {
85 		if (inet_aton(server, &rsrv_sin.sin_addr) == 0)
86 			errx(1, "unknown host %s.", server);
87 	} else
88 		rsrv_sin.sin_addr.s_addr = *(u_int32_t *)h->h_addr;
89 
90 	tv.tv_sec = 10;
91 	tv.tv_usec = 0;
92 
93 	client = clntudp_create(&rsrv_sin, YPPROG, YPVERS, tv, &rsrv_sock);
94 	if (client == NULL)
95 		errx(1, "clntudp_create: no contact with host %s.", server);
96 
97 	yprnk.domain = indomain;
98 	yprnk.map = inmap;
99 
100 	bzero((char *)(char *)&ypro, sizeof ypro);
101 
102 	r = clnt_call(client, YPPROC_ORDER, (xdrproc_t)xdr_ypreq_nokey, &yprnk,
103 	    (xdrproc_t)xdr_ypresp_order, &ypro, tv);
104 	if (r != RPC_SUCCESS)
105 		clnt_perror(client, "yp_order: clnt_call");
106 
107 	*outorder = ypro.ordernum;
108 	xdr_free((xdrproc_t)xdr_ypresp_order, (char *)&ypro);
109 
110 	r = ypprot_err(ypro.status);
111 	if (r == RPC_SUCCESS) {
112 		bzero((char *)&yprm, sizeof yprm);
113 
114 		r = clnt_call(client, YPPROC_MASTER, (xdrproc_t)xdr_ypreq_nokey,
115 		    &yprnk, (xdrproc_t)xdr_ypresp_master, &yprm, tv);
116 		if (r != RPC_SUCCESS)
117 			clnt_perror(client, "yp_master: clnt_call");
118 		r = ypprot_err(yprm.status);
119 		if (r == 0)
120 			*outname = (char *)strdup(yprm.master);
121 		xdr_free((xdrproc_t)xdr_ypresp_master, (char *)&yprm);
122 	}
123 	clnt_destroy(client);
124 	return (r);
125 }
126 
127 int
128 main(int argc, char *argv[])
129 {
130 	char *domainname, *hostname = NULL, *inmap, *master;
131 	int order, c, r;
132 	time_t torder;
133 
134 	yp_get_default_domain(&domainname);
135 
136 	while ((c = getopt(argc, argv, "h:d:")) != -1)
137 		switch (c) {
138 		case 'd':
139 			domainname = optarg;
140 			break;
141 		case 'h':
142 			hostname = optarg;
143 			break;
144 		default:
145 			usage();
146 			/*NOTREACHED*/
147 		}
148 
149 	if (optind + 1 != argc)
150 		usage();
151 	inmap = argv[optind];
152 
153 	if (hostname != NULL) {
154 		r = get_remote_info(domainname, inmap, hostname,
155 		    &order, &master);
156 	} else {
157 		r = yp_order(domainname, inmap, &order);
158 		if (r == 0)
159 			r = yp_master(domainname, inmap, &master);
160 	}
161 
162 	if (r != 0)
163 		errx(1, "no such map %s: reason: %s",
164 		    inmap, yperr_string(r));
165 
166 	torder = order;
167 	printf("Map %s has order number %lld. %s", inmap,
168 	    (long long)order, ctime(&torder));
169 	printf("The master server is %s.\n", master);
170 
171 	exit(0);
172 }
173