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