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