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