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