yppoll.c revision 1.7 1 /* $NetBSD: yppoll.c,v 1.7 2000/04/14 06:26:55 simonb 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.7 2000/04/14 06:26:55 simonb 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 int c, r;
75
76 yp_get_default_domain(&domainname);
77
78 while ((c = getopt(argc, argv, "h:d:")) != -1) {
79 switch (c) {
80 case 'd':
81 domainname = optarg;
82 break;
83
84 case 'h':
85 hostname = optarg;
86 break;
87
88 default:
89 usage();
90 /*NOTREACHED*/
91 }
92 }
93
94 if (domainname == NULL)
95 errx(1, "YP domain name not set");
96
97 argc -= optind;
98 argv += optind;
99
100 if (argc != 1)
101 usage();
102
103 inmap = argv[0];
104
105 if (hostname != NULL)
106 r = get_remote_info(domainname, inmap, hostname,
107 &order, &master);
108 else {
109 r = yp_order(domainname, inmap, &order);
110 if (r == 0)
111 r = yp_master(domainname, inmap, &master);
112 }
113
114 if (r != 0)
115 errx(1, "no such map %s. Reason: %s\n",
116 inmap, yperr_string(r));
117
118 printf("Map %s has order number %d. %s", inmap, order,
119 ctime((time_t *)&order));
120 printf("The master server is %s.\n", master);
121 exit(0);
122 }
123
124 int
125 get_remote_info(indomain, inmap, server, outorder, outname)
126 char *indomain;
127 char *inmap;
128 char *server;
129 int *outorder;
130 char **outname;
131 {
132 struct ypresp_order ypro;
133 struct ypresp_master yprm;
134 struct ypreq_nokey yprnk;
135 struct timeval tv;
136 int r;
137 struct sockaddr_in rsrv_sin;
138 int rsrv_sock;
139 CLIENT *client;
140 struct hostent *h;
141
142 memset(&rsrv_sin, 0, sizeof(rsrv_sin));
143 rsrv_sin.sin_len = sizeof rsrv_sin;
144 rsrv_sin.sin_family = AF_INET;
145 rsrv_sock = RPC_ANYSOCK;
146
147 h = gethostbyname(server);
148 if (h == NULL) {
149 if (inet_aton(server, &rsrv_sin.sin_addr) == 0)
150 errx(1, "unknown host %s", server);
151 } else
152 memcpy(&rsrv_sin.sin_addr.s_addr, h->h_addr, h->h_length);
153
154 tv.tv_sec = 10;
155 tv.tv_usec = 0;
156
157 client = clntudp_create(&rsrv_sin, YPPROG, YPVERS, tv, &rsrv_sock);
158 if (client == NULL)
159 errx(1, "clntudp_create: no contact with host %s.\n", server);
160
161 yprnk.domain = indomain;
162 yprnk.map = inmap;
163
164 memset(&ypro, 0, sizeof(ypro));
165
166 r = clnt_call(client, YPPROC_ORDER, xdr_ypreq_nokey, &yprnk,
167 xdr_ypresp_order, &ypro, tv);
168 if (r != RPC_SUCCESS)
169 clnt_perror(client, "yp_order: clnt_call");
170
171 *outorder = ypro.ordernum;
172 xdr_free(xdr_ypresp_order, (char *)&ypro);
173
174 r = ypprot_err(ypro.status);
175 if (r == RPC_SUCCESS) {
176 memset(&yprm, 0, sizeof(yprm));
177
178 r = clnt_call(client, YPPROC_MASTER, xdr_ypreq_nokey,
179 &yprnk, xdr_ypresp_master, &yprm, tv);
180 if (r != RPC_SUCCESS)
181 clnt_perror(client, "yp_master: clnt_call");
182 r = ypprot_err(yprm.status);
183 if (r == 0)
184 *outname = (char *)strdup(yprm.master);
185 xdr_free(xdr_ypresp_master, (char *)&yprm);
186 }
187 clnt_destroy(client);
188 return r;
189 }
190
191 void
192 usage()
193 {
194
195 fprintf(stderr, "usage: %s [-h host] [-d domainname] mapname\n",
196 __progname);
197 exit(1);
198 }
199