xref: /netbsd-src/usr.sbin/yppoll/yppoll.c (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1 /*
2  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
3  * Copyright (c) 1992, 1993 John Brezak
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by Theo de Raadt and
17  *	John Brezak.
18  * 4. The name of the author may not be used to endorse or promote
19  *    products derived from this software without specific prior written
20  *    permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
23  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
26  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #ifndef lint
36 static char rcsid[] = "$Id: yppoll.c,v 1.4 1994/08/23 17:03:15 deraadt Exp $";
37 #endif /* not lint */
38 
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <stdio.h>
43 #include <time.h>
44 #include <netdb.h>
45 #include <unistd.h>
46 #include <string.h>
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 
50 #include <rpc/rpc.h>
51 #include <rpc/xdr.h>
52 #include <rpcsvc/yp_prot.h>
53 #include <rpcsvc/ypclnt.h>
54 
55 void
56 usage()
57 {
58 	fprintf(stderr, "Usage:\n");
59 	fprintf(stderr, "\typpoll [-h host] [-d domainname] mapname\n");
60 	exit(1);
61 }
62 
63 int
64 get_remote_info(indomain, inmap, server, outorder, outname)
65 	char *indomain;
66 	char *inmap;
67 	char *server;
68 	int *outorder;
69 	char **outname;
70 {
71 	struct ypresp_order ypro;
72 	struct ypresp_master yprm;
73 	struct ypreq_nokey yprnk;
74 	struct timeval tv;
75 	int r;
76 	struct sockaddr_in rsrv_sin;
77 	int rsrv_sock;
78 	CLIENT *client;
79 	struct hostent *h;
80 
81 	bzero((char *)&rsrv_sin, sizeof rsrv_sin);
82 	rsrv_sin.sin_len = sizeof rsrv_sin;
83 	rsrv_sin.sin_family = AF_INET;
84 	rsrv_sock = RPC_ANYSOCK;
85 
86 	h = gethostbyname(server);
87 	if (h == NULL) {
88 		if (inet_aton(server, &rsrv_sin.sin_addr) == 0) {
89 			fprintf(stderr, "unknown host %s\n", server);
90 			exit(1);
91 		}
92 	} else {
93 		rsrv_sin.sin_addr.s_addr = *(u_long *)h->h_addr;
94 	}
95 
96 	tv.tv_sec = 10;
97 	tv.tv_usec = 0;
98 
99 	client = clntudp_create(&rsrv_sin, YPPROG, YPVERS, tv, &rsrv_sock);
100 	if (client == NULL) {
101 		fprintf(stderr, "clntudp_create: no contact with host %s.\n",
102 		    server);
103 		exit(1);
104 	}
105 
106 	yprnk.domain = indomain;
107 	yprnk.map = inmap;
108 
109 	bzero((char *)(char *)&ypro, sizeof ypro);
110 
111 	r = clnt_call(client, YPPROC_ORDER, xdr_ypreq_nokey, &yprnk,
112 	    xdr_ypresp_order, &ypro, tv);
113 	if (r != RPC_SUCCESS)
114 		clnt_perror(client, "yp_order: clnt_call");
115 
116 	*outorder = ypro.ordernum;
117 	xdr_free(xdr_ypresp_order, (char *)&ypro);
118 
119 	r = ypprot_err(ypro.status);
120 	if (r == RPC_SUCCESS) {
121 		bzero((char *)&yprm, sizeof yprm);
122 
123 		r = clnt_call(client, YPPROC_MASTER, xdr_ypreq_nokey,
124 		    &yprnk, xdr_ypresp_master, &yprm, tv);
125 		if (r != RPC_SUCCESS)
126 			clnt_perror(client, "yp_master: clnt_call");
127 		r = ypprot_err(yprm.status);
128 		if (r==0)
129 			*outname = (char *)strdup(yprm.master);
130 		xdr_free(xdr_ypresp_master, (char *)&yprm);
131 	}
132 	clnt_destroy(client);
133 	return r;
134 }
135 
136 int
137 main(argc, argv)
138 	int  argc;
139 	char **argv;
140 {
141 	char *domainname;
142 	char *hostname = NULL;
143 	char *inmap, *master;
144 	int order;
145 	extern char *optarg;
146 	extern int optind;
147 	int c, r;
148 
149 	yp_get_default_domain(&domainname);
150 
151 	while ((c=getopt(argc, argv, "h:d:?")) != -1)
152 		switch (c) {
153 		case 'd':
154 			domainname = optarg;
155 			break;
156 		case 'h':
157 			hostname = optarg;
158 			break;
159 		default:
160 			usage();
161 			/*NOTREACHED*/
162 		}
163 
164 	if (optind + 1 != argc )
165 		usage();
166 	inmap = argv[optind];
167 
168 	if (hostname != NULL) {
169 		r = get_remote_info(domainname, inmap, hostname,
170 		    &order, &master);
171 	} else {
172 		r = yp_order(domainname, inmap, &order);
173 		if (r == 0)
174 			r = yp_master(domainname, inmap, &master);
175 	}
176 
177 	if (r != 0) {
178 		fprintf(stderr, "No such map %s. Reason: %s\n",
179 		    inmap, yperr_string(r));
180 		exit(1);
181 	}
182 
183 	printf("Map %s has order number %d. %s", inmap, order,
184 	    ctime((time_t *)&order));
185 	printf("The master server is %s.\n", master);
186 	exit(0);
187 }
188