1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc.  All Rights
7 * Reserved.  This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License').  You may not use this file
10 * except in compliance with the License.  Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*
25 * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@fsa.ca>
26 * Copyright (c) 1992, 1993 John Brezak
27 * All rights reserved.
28 *
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
31 * are met:
32 * 1. Redistributions of source code must retain the above copyright
33 *    notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 *    notice, this list of conditions and the following disclaimer in the
36 *    documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 *    must display the following acknowledgement:
39 *	This product includes software developed by Theo de Raadt and
40 *	John Brezak.
41 * 4. The name of the author may not be used to endorse or promote
42 *    products derived from this software without specific prior written
43 *    permission.
44 *
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
55 * SUCH DAMAGE.
56 */
57
58#include <sys/cdefs.h>
59#ifndef lint
60__unused static char rcsid[] = "$Id: yppoll.c,v 1.2 2006/02/07 06:22:59 lindak Exp $";
61#endif /* not lint */
62
63#include <sys/param.h>
64#include <sys/types.h>
65#include <sys/socket.h>
66#include <stdio.h>
67#include <stdlib.h>
68#include <time.h>
69#include <netdb.h>
70#include <unistd.h>
71#include <string.h>
72#include <netinet/in.h>
73#include <arpa/inet.h>
74
75#include <rpc/rpc.h>
76#include <rpc/xdr.h>
77#include <rpcsvc/yp_prot.h>
78#include <rpcsvc/ypclnt.h>
79
80void
81usage()
82{
83	fprintf(stderr, "Usage:\n");
84	fprintf(stderr, "\typpoll [-h host] [-d domainname] mapname\n");
85	exit(1);
86}
87
88int
89get_remote_info(indomain, inmap, server, outorder, outname)
90	char *indomain;
91	char *inmap;
92	char *server;
93	int *outorder;
94	char **outname;
95{
96	struct ypresp_order ypro;
97	struct ypresp_master yprm;
98	struct ypreq_nokey yprnk;
99	struct timeval tv;
100	int r;
101	struct sockaddr_in rsrv_sin;
102	int rsrv_sock;
103	CLIENT *client;
104	struct hostent *h;
105
106	bzero((char *)&rsrv_sin, sizeof rsrv_sin);
107	rsrv_sin.sin_len = sizeof rsrv_sin;
108	rsrv_sin.sin_family = AF_INET;
109	rsrv_sock = RPC_ANYSOCK;
110
111	h = gethostbyname(server);
112	if (h == NULL) {
113		if (inet_aton(server, &rsrv_sin.sin_addr) == 0) {
114			fprintf(stderr, "unknown host %s\n", server);
115			exit(1);
116		}
117	} else {
118		rsrv_sin.sin_addr.s_addr = *(u_long *)h->h_addr;
119	}
120
121	tv.tv_sec = 10;
122	tv.tv_usec = 0;
123
124	client = clntudp_create(&rsrv_sin, YPPROG, YPVERS, tv, &rsrv_sock);
125	if (client == NULL) {
126		fprintf(stderr, "clntudp_create: no contact with host %s.\n",
127		    server);
128		exit(1);
129	}
130
131	yprnk.domain = indomain;
132	yprnk.map = inmap;
133
134	bzero((char *)(char *)&ypro, sizeof ypro);
135
136	r = clnt_call(client, YPPROC_ORDER, (xdrproc_t)xdr_ypreq_nokey, &yprnk,
137	    (xdrproc_t)xdr_ypresp_order, &ypro, tv);
138	if (r != RPC_SUCCESS)
139		clnt_perror(client, "yp_order: clnt_call");
140
141	*outorder = ypro.ordernum;
142	xdr_free((xdrproc_t)xdr_ypresp_order, (char *)&ypro);
143
144	r = ypprot_err(ypro.status);
145	if (r == RPC_SUCCESS) {
146		bzero((char *)&yprm, sizeof yprm);
147
148		r = clnt_call(client, YPPROC_MASTER, (xdrproc_t)xdr_ypreq_nokey,
149		    &yprnk, (xdrproc_t)xdr_ypresp_master, &yprm, tv);
150		if (r != RPC_SUCCESS)
151			clnt_perror(client, "yp_master: clnt_call");
152		r = ypprot_err(yprm.status);
153		if (r==0)
154			*outname = (char *)strdup(yprm.master);
155		xdr_free((xdrproc_t)xdr_ypresp_master, (char *)&yprm);
156	}
157	clnt_destroy(client);
158	return r;
159}
160
161int
162main(argc, argv)
163	int  argc;
164	char **argv;
165{
166	char *domainname;
167	char *hostname = NULL;
168	char *inmap, *master;
169	int order;
170	extern char *optarg;
171	extern int optind;
172	int c, r;
173
174	yp_get_default_domain(&domainname);
175
176	while ((c=getopt(argc, argv, "h:d:?")) != -1)
177		switch (c) {
178		case 'd':
179			domainname = optarg;
180			break;
181		case 'h':
182			hostname = optarg;
183			break;
184		default:
185			usage();
186			/*NOTREACHED*/
187		}
188
189	if (optind + 1 != argc )
190		usage();
191	inmap = argv[optind];
192
193	if (hostname != NULL) {
194		r = get_remote_info(domainname, inmap, hostname,
195		    &order, &master);
196	} else {
197		r = yp_order(domainname, inmap, &order);
198		if (r == 0)
199			r = yp_master(domainname, inmap, &master);
200	}
201
202	if (r != 0) {
203		fprintf(stderr, "No such map %s. Reason: %s\n",
204		    inmap, yperr_string(r));
205		exit(1);
206	}
207
208	printf("Map %s has order number %d. %s", inmap, order,
209	    ctime((time_t *)&order));
210	printf("The master server is %s.\n", master);
211	exit(0);
212}
213