1 /* $OpenBSD: yppoll.c,v 1.16 2024/08/27 06:03:20 florian 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/types.h>
35 #include <sys/socket.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <time.h>
39 #include <netdb.h>
40 #include <unistd.h>
41 #include <string.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44
45 #include <rpc/rpc.h>
46 #include <rpc/xdr.h>
47 #include <rpcsvc/yp_prot.h>
48 #include <rpcsvc/ypclnt.h>
49
50 static void
usage(void)51 usage(void)
52 {
53 fprintf(stderr, "usage: yppoll [-d domain] [-h host] mapname\n");
54 exit(1);
55 }
56
57 static int
get_remote_info(char * indomain,char * inmap,char * server,int * outorder,char ** outname)58 get_remote_info(char *indomain, char *inmap, char *server, int *outorder,
59 char **outname)
60 {
61 struct ypresp_order ypro;
62 struct ypresp_master yprm;
63 struct ypreq_nokey yprnk;
64 struct timeval tv;
65 struct sockaddr_in rsrv_sin;
66 int rsrv_sock;
67 CLIENT *client;
68 struct addrinfo hints, *res;
69 int r;
70
71 bzero((char *)&rsrv_sin, sizeof rsrv_sin);
72 rsrv_sin.sin_len = sizeof rsrv_sin;
73 rsrv_sin.sin_family = AF_INET;
74 rsrv_sock = RPC_ANYSOCK;
75
76 memset(&hints, 0, sizeof(hints));
77 hints.ai_family = AF_INET;
78
79 if (getaddrinfo(server, NULL, &hints, &res) != 0) {
80 fprintf(stderr, "unknown host %s\n", server);
81 exit(1);
82 }
83 rsrv_sin.sin_addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr;
84 freeaddrinfo(res);
85
86 tv.tv_sec = 10;
87 tv.tv_usec = 0;
88
89 client = clntudp_create(&rsrv_sin, YPPROG, YPVERS, tv, &rsrv_sock);
90 if (client == NULL) {
91 fprintf(stderr, "clntudp_create: no contact with host %s.\n",
92 server);
93 exit(1);
94 }
95
96 yprnk.domain = indomain;
97 yprnk.map = inmap;
98
99 bzero((char *)(char *)&ypro, sizeof ypro);
100
101 r = clnt_call(client, YPPROC_ORDER, xdr_ypreq_nokey, &yprnk,
102 xdr_ypresp_order, &ypro, tv);
103 if (r != RPC_SUCCESS)
104 clnt_perror(client, "yp_order: clnt_call");
105
106 *outorder = ypro.ordernum;
107 xdr_free(xdr_ypresp_order, (char *)&ypro);
108
109 r = ypprot_err(ypro.status);
110 if (r == RPC_SUCCESS) {
111 bzero((char *)&yprm, sizeof yprm);
112
113 r = clnt_call(client, YPPROC_MASTER, xdr_ypreq_nokey,
114 &yprnk, xdr_ypresp_master, &yprm, tv);
115 if (r != RPC_SUCCESS)
116 clnt_perror(client, "yp_master: clnt_call");
117 r = ypprot_err(yprm.status);
118 if (r==0)
119 *outname = (char *)strdup(yprm.master);
120 xdr_free(xdr_ypresp_master, (char *)&yprm);
121 }
122 clnt_destroy(client);
123 return r;
124 }
125
126 int
main(int argc,char * argv[])127 main(int argc, char *argv[])
128 {
129 char *domainname, *hostname = NULL, *inmap, *master;
130 extern char *optarg;
131 extern int optind;
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 fprintf(stderr, "No such map %s. Reason: %s\n",
165 inmap, yperr_string(r));
166 exit(1);
167 }
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 exit(0);
174 }
175