yppoll.c revision 1.12 1 1.12 agc /* $NetBSD: yppoll.c,v 1.12 2003/12/10 12:06:26 agc Exp $ */
2 1.5 thorpej
3 1.1 brezak /*
4 1.3 deraadt * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) fsa.ca>
5 1.12 agc * All rights reserved.
6 1.12 agc *
7 1.12 agc * Redistribution and use in source and binary forms, with or without
8 1.12 agc * modification, are permitted provided that the following conditions
9 1.12 agc * are met:
10 1.12 agc * 1. Redistributions of source code must retain the above copyright
11 1.12 agc * notice, this list of conditions and the following disclaimer.
12 1.12 agc * 2. Redistributions in binary form must reproduce the above copyright
13 1.12 agc * notice, this list of conditions and the following disclaimer in the
14 1.12 agc * documentation and/or other materials provided with the distribution.
15 1.12 agc *
16 1.12 agc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 1.12 agc * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 1.12 agc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.12 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 1.12 agc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.12 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.12 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.12 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.12 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.12 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.12 agc * SUCH DAMAGE.
27 1.12 agc */
28 1.12 agc
29 1.12 agc /*
30 1.3 deraadt * Copyright (c) 1992, 1993 John Brezak
31 1.1 brezak * All rights reserved.
32 1.1 brezak *
33 1.1 brezak * Redistribution and use in source and binary forms, with or without
34 1.1 brezak * modification, are permitted provided that the following conditions
35 1.1 brezak * are met:
36 1.1 brezak * 1. Redistributions of source code must retain the above copyright
37 1.1 brezak * notice, this list of conditions and the following disclaimer.
38 1.1 brezak * 2. Redistributions in binary form must reproduce the above copyright
39 1.1 brezak * notice, this list of conditions and the following disclaimer in the
40 1.1 brezak * documentation and/or other materials provided with the distribution.
41 1.3 deraadt * 3. All advertising materials mentioning features or use of this software
42 1.3 deraadt * must display the following acknowledgement:
43 1.12 agc * This product includes software developed by John Brezak.
44 1.3 deraadt * 4. The name of the author may not be used to endorse or promote
45 1.1 brezak * products derived from this software without specific prior written
46 1.1 brezak * permission.
47 1.1 brezak *
48 1.1 brezak * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
49 1.1 brezak * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
50 1.1 brezak * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 1.1 brezak * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
52 1.1 brezak * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 1.1 brezak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 1.1 brezak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 1.1 brezak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 1.1 brezak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 1.1 brezak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 1.1 brezak * SUCH DAMAGE.
59 1.1 brezak */
60 1.1 brezak
61 1.6 thorpej #include <sys/cdefs.h>
62 1.2 mycroft #ifndef lint
63 1.12 agc __RCSID("$NetBSD: yppoll.c,v 1.12 2003/12/10 12:06:26 agc Exp $");
64 1.2 mycroft #endif /* not lint */
65 1.1 brezak
66 1.1 brezak #include <sys/param.h>
67 1.1 brezak #include <sys/types.h>
68 1.1 brezak #include <sys/socket.h>
69 1.6 thorpej #include <err.h>
70 1.1 brezak #include <stdio.h>
71 1.8 matt #include <stdlib.h>
72 1.1 brezak #include <time.h>
73 1.4 deraadt #include <netdb.h>
74 1.4 deraadt #include <unistd.h>
75 1.4 deraadt #include <string.h>
76 1.4 deraadt #include <netinet/in.h>
77 1.4 deraadt #include <arpa/inet.h>
78 1.1 brezak
79 1.1 brezak #include <rpc/rpc.h>
80 1.1 brezak #include <rpc/xdr.h>
81 1.1 brezak #include <rpcsvc/yp_prot.h>
82 1.1 brezak #include <rpcsvc/ypclnt.h>
83 1.1 brezak
84 1.6 thorpej int main __P((int, char *[]));
85 1.6 thorpej int get_remote_info __P((char *, char *, char *, int *, char **));
86 1.6 thorpej void usage __P((void));
87 1.6 thorpej
88 1.6 thorpej int
89 1.6 thorpej main(argc, argv)
90 1.6 thorpej int argc;
91 1.6 thorpej char **argv;
92 1.1 brezak {
93 1.6 thorpej char *domainname;
94 1.6 thorpej char *hostname = NULL;
95 1.6 thorpej char *inmap, *master;
96 1.6 thorpej int order;
97 1.6 thorpej int c, r;
98 1.6 thorpej
99 1.6 thorpej yp_get_default_domain(&domainname);
100 1.6 thorpej
101 1.6 thorpej while ((c = getopt(argc, argv, "h:d:")) != -1) {
102 1.6 thorpej switch (c) {
103 1.6 thorpej case 'd':
104 1.6 thorpej domainname = optarg;
105 1.6 thorpej break;
106 1.6 thorpej
107 1.6 thorpej case 'h':
108 1.6 thorpej hostname = optarg;
109 1.6 thorpej break;
110 1.6 thorpej
111 1.6 thorpej default:
112 1.6 thorpej usage();
113 1.6 thorpej /*NOTREACHED*/
114 1.6 thorpej }
115 1.6 thorpej }
116 1.6 thorpej
117 1.6 thorpej if (domainname == NULL)
118 1.6 thorpej errx(1, "YP domain name not set");
119 1.6 thorpej
120 1.6 thorpej argc -= optind;
121 1.6 thorpej argv += optind;
122 1.6 thorpej
123 1.6 thorpej if (argc != 1)
124 1.6 thorpej usage();
125 1.6 thorpej
126 1.6 thorpej inmap = argv[0];
127 1.6 thorpej
128 1.6 thorpej if (hostname != NULL)
129 1.6 thorpej r = get_remote_info(domainname, inmap, hostname,
130 1.6 thorpej &order, &master);
131 1.6 thorpej else {
132 1.6 thorpej r = yp_order(domainname, inmap, &order);
133 1.6 thorpej if (r == 0)
134 1.6 thorpej r = yp_master(domainname, inmap, &master);
135 1.6 thorpej }
136 1.6 thorpej
137 1.6 thorpej if (r != 0)
138 1.10 grant errx(1, "no such map %s. Reason: %s",
139 1.6 thorpej inmap, yperr_string(r));
140 1.6 thorpej
141 1.6 thorpej printf("Map %s has order number %d. %s", inmap, order,
142 1.11 thorpej ctime((void *)&order));
143 1.6 thorpej printf("The master server is %s.\n", master);
144 1.6 thorpej exit(0);
145 1.1 brezak }
146 1.1 brezak
147 1.1 brezak int
148 1.4 deraadt get_remote_info(indomain, inmap, server, outorder, outname)
149 1.4 deraadt char *indomain;
150 1.4 deraadt char *inmap;
151 1.4 deraadt char *server;
152 1.4 deraadt int *outorder;
153 1.4 deraadt char **outname;
154 1.4 deraadt {
155 1.4 deraadt struct ypresp_order ypro;
156 1.4 deraadt struct ypresp_master yprm;
157 1.4 deraadt struct ypreq_nokey yprnk;
158 1.4 deraadt struct timeval tv;
159 1.4 deraadt int r;
160 1.4 deraadt struct sockaddr_in rsrv_sin;
161 1.4 deraadt int rsrv_sock;
162 1.4 deraadt CLIENT *client;
163 1.4 deraadt struct hostent *h;
164 1.4 deraadt
165 1.6 thorpej memset(&rsrv_sin, 0, sizeof(rsrv_sin));
166 1.4 deraadt rsrv_sin.sin_len = sizeof rsrv_sin;
167 1.4 deraadt rsrv_sin.sin_family = AF_INET;
168 1.4 deraadt rsrv_sock = RPC_ANYSOCK;
169 1.4 deraadt
170 1.4 deraadt h = gethostbyname(server);
171 1.4 deraadt if (h == NULL) {
172 1.6 thorpej if (inet_aton(server, &rsrv_sin.sin_addr) == 0)
173 1.6 thorpej errx(1, "unknown host %s", server);
174 1.6 thorpej } else
175 1.6 thorpej memcpy(&rsrv_sin.sin_addr.s_addr, h->h_addr, h->h_length);
176 1.4 deraadt
177 1.4 deraadt tv.tv_sec = 10;
178 1.4 deraadt tv.tv_usec = 0;
179 1.4 deraadt
180 1.4 deraadt client = clntudp_create(&rsrv_sin, YPPROG, YPVERS, tv, &rsrv_sock);
181 1.6 thorpej if (client == NULL)
182 1.10 grant errx(1, "clntudp_create: no contact with host %s.", server);
183 1.4 deraadt
184 1.4 deraadt yprnk.domain = indomain;
185 1.4 deraadt yprnk.map = inmap;
186 1.4 deraadt
187 1.6 thorpej memset(&ypro, 0, sizeof(ypro));
188 1.4 deraadt
189 1.4 deraadt r = clnt_call(client, YPPROC_ORDER, xdr_ypreq_nokey, &yprnk,
190 1.4 deraadt xdr_ypresp_order, &ypro, tv);
191 1.4 deraadt if (r != RPC_SUCCESS)
192 1.4 deraadt clnt_perror(client, "yp_order: clnt_call");
193 1.4 deraadt
194 1.4 deraadt *outorder = ypro.ordernum;
195 1.4 deraadt xdr_free(xdr_ypresp_order, (char *)&ypro);
196 1.4 deraadt
197 1.4 deraadt r = ypprot_err(ypro.status);
198 1.4 deraadt if (r == RPC_SUCCESS) {
199 1.6 thorpej memset(&yprm, 0, sizeof(yprm));
200 1.4 deraadt
201 1.4 deraadt r = clnt_call(client, YPPROC_MASTER, xdr_ypreq_nokey,
202 1.4 deraadt &yprnk, xdr_ypresp_master, &yprm, tv);
203 1.4 deraadt if (r != RPC_SUCCESS)
204 1.4 deraadt clnt_perror(client, "yp_master: clnt_call");
205 1.4 deraadt r = ypprot_err(yprm.status);
206 1.6 thorpej if (r == 0)
207 1.4 deraadt *outname = (char *)strdup(yprm.master);
208 1.4 deraadt xdr_free(xdr_ypresp_master, (char *)&yprm);
209 1.4 deraadt }
210 1.4 deraadt clnt_destroy(client);
211 1.4 deraadt return r;
212 1.4 deraadt }
213 1.4 deraadt
214 1.6 thorpej void
215 1.6 thorpej usage()
216 1.1 brezak {
217 1.1 brezak
218 1.6 thorpej fprintf(stderr, "usage: %s [-h host] [-d domainname] mapname\n",
219 1.9 cgd getprogname());
220 1.6 thorpej exit(1);
221 1.1 brezak }
222