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