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