1 /* $NetBSD: yppoll.c,v 1.13 2003/12/10 17:10:34 jdolecek Exp $ */
4 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Copyright (c) 1992, 1993 John Brezak
31 * All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. The name of the author may not be used to endorse or promote
42 * products derived from this software without specific prior written
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
46 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
47 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
49 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 #include <sys/cdefs.h>
60 __RCSID("$NetBSD: yppoll.c,v 1.13 2003/12/10 17:10:34 jdolecek Exp $");
63 #include <sys/param.h>
64 #include <sys/types.h>
65 #include <sys/socket.h>
73 #include <netinet/in.h>
74 #include <arpa/inet.h>
78 #include <rpcsvc/yp_prot.h>
79 #include <rpcsvc/ypclnt.h>
81 static CLIENT
*mkclient(struct sockaddr_in
*, unsigned long, unsigned long,
83 static int get_remote_info(const char *, const char *, const char *, int *,
85 static void usage(void) __attribute__((__noreturn__
));
88 main(int argc
, char *argv
[])
91 char *hostname
= NULL
;
96 (void)yp_get_default_domain(&domainname
);
98 while ((c
= getopt(argc
, argv
, "Th:d:")) != -1) {
117 if (domainname
== NULL
)
118 errx(1, "YP domain name not set");
128 if (hostname
!= NULL
)
129 r
= get_remote_info(domainname
, inmap
, hostname
,
130 &order
, &master
, tcp
);
132 r
= yp_order(domainname
, inmap
, &order
);
134 r
= yp_master(domainname
, inmap
, &master
);
138 errx(1, "no such map %s. Reason: %s",
139 inmap
, yperr_string(r
));
141 (void)printf("Map %s has order number %d. %s", inmap
, order
,
142 ctime((void *)&order
));
143 (void)printf("The master server is %s.\n", master
);
148 mkclient(struct sockaddr_in
*sin
, unsigned long prog
, unsigned long vers
,
151 static struct timeval tv
= { 10, 0 };
152 int fd
= RPC_ANYSOCK
;
155 return clnttcp_create(sin
, prog
, vers
, &fd
, 0, 0);
157 return clntudp_create(sin
, prog
, vers
, tv
, &fd
);
161 get_remote_info(const char *indomain
, const char *inmap
, const char *server
,
162 int *outorder
, char **outname
, int tcp
)
164 struct ypresp_order ypro
;
165 struct ypresp_master yprm
;
166 struct ypreq_nokey yprnk
;
169 struct sockaddr_in rsrv_sin
;
173 (void)memset(&rsrv_sin
, 0, sizeof(rsrv_sin
));
174 rsrv_sin
.sin_len
= sizeof rsrv_sin
;
175 rsrv_sin
.sin_family
= AF_INET
;
177 h
= gethostbyname(server
);
179 if (inet_aton(server
, &rsrv_sin
.sin_addr
) == 0)
180 errx(1, "unknown host %s", server
);
182 (void)memcpy(&rsrv_sin
.sin_addr
.s_addr
, h
->h_addr
,
183 (size_t)h
->h_length
);
185 client
= mkclient(&rsrv_sin
, YPPROG
, YPVERS
, tcp
);
187 errx(1, "clnt%s_create: no contact with host %s.",
188 tcp
? "tcp" : "udp", server
);
190 yprnk
.domain
= indomain
;
193 (void)memset(&ypro
, 0, sizeof(ypro
));
198 r
= clnt_call(client
, (unsigned int)YPPROC_ORDER
, xdr_ypreq_nokey
,
199 &yprnk
, xdr_ypresp_order
, &ypro
, tv
);
200 if (r
!= RPC_SUCCESS
)
201 clnt_perror(client
, "yp_order: clnt_call");
203 *outorder
= ypro
.ordernum
;
204 xdr_free(xdr_ypresp_order
, (void *)&ypro
);
206 r
= ypprot_err(ypro
.status
);
207 if (r
== RPC_SUCCESS
) {
208 (void)memset(&yprm
, 0, sizeof(yprm
));
210 r
= clnt_call(client
, (unsigned int)YPPROC_MASTER
,
211 xdr_ypreq_nokey
, &yprnk
, xdr_ypresp_master
, &yprm
, tv
);
212 if (r
!= RPC_SUCCESS
)
213 clnt_perror(client
, "yp_master: clnt_call");
214 r
= ypprot_err(yprm
.status
);
216 *outname
= (char *)strdup(yprm
.master
);
217 xdr_free(xdr_ypresp_master
, (void *)&yprm
);
219 clnt_destroy(client
);
227 (void)fprintf(stderr
,
228 "Usage: %s [-T] [-h host] [-d domainname] mapname\n",