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