1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley
* under license from the Regents of the University of
* California.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This is a user command which asks a particular ypserv which version of a
* map it is using. Usage is:
*
* yppoll [-h <host>] [-d <domainname>] mapname
*
* If the host is ommitted, the local host will be used. If host is specified
* as an internet address, no yp services need to be locally available.
*
*/
#include <stdio.h>
#include <ctype.h>
#include <rpc/rpc.h>
#include <rpcsvc/ypclnt.h>
#include <rpcsvc/yp_prot.h>
#include <netdir.h>
#include <arpa/inet.h>
#include "yp_b.h"
#ifdef NULL
#undef NULL
#endif
#define NULL 0
#define TIMEOUT 30 /* Total seconds for timeout */
static int status = 0; /* exit status */
static char *domain = NULL;
static char default_domain_name[YPMAXDOMAIN];
static char *map = NULL;
static char *host = NULL;
static char default_host_name[256];
static char err_usage[] =
"Usage:\n\
yppoll [ -h host ] [ -d domainname ] mapname\n\n";
static char err_bad_args[] =
"Bad %s argument.\n";
static char err_cant_get_kname[] =
"Can't get %s back from system call.\n";
static char err_null_kname[] =
"%s hasn't been set on this machine.\n";
static char err_bad_hostname[] = "hostname";
static char err_bad_mapname[] = "mapname";
static char err_bad_domainname[] = "domainname";
static char err_bad_resp[] =
"Ill-formed response returned from ypserv on host %s.\n";
static void get_command_line_args();
static void getdomain();
static void getlochost();
static void getmapparms();
static void newresults();
static void getypserv();
extern void exit();
extern int getdomainname();
extern int gethostname();
extern unsigned int strlen();
extern int strcmp();
/*
* This is the mainline for the yppoll process.
*/
int
main(argc, argv)
int argc;
char **argv;
{
get_command_line_args(argc, argv);
if (!domain) {
getdomain();
}
if (!host) {
getypserv();
}
getmapparms();
return (status);
}
/*
* This does the command line argument processing.
*/
static void
get_command_line_args(argc, argv)
int argc;
char **argv;
{
argv++;
while (--argc) {
if ((*argv)[0] == '-') {
switch ((*argv)[1]) {
case 'h':
if (argc > 1) {
argv++;
argc--;
host = *argv;
argv++;
if ((int)strlen(host) > 256) {
(void) fprintf(stderr,
err_bad_args,
err_bad_hostname);
exit(1);
}
} else {
(void) fprintf(stderr, err_usage);
exit(1);
}
break;
case 'd':
if (argc > 1) {
argv++;
argc--;
domain = *argv;
argv++;
if ((int)strlen(domain) > YPMAXDOMAIN) {
(void) fprintf(stderr,
err_bad_args,
err_bad_domainname);
exit(1);
}
} else {
(void) fprintf(stderr, err_usage);
exit(1);
}
break;
default:
(void) fprintf(stderr, err_usage);
exit(1);
}
} else {
if (!map) {
map = *argv;
if ((int)strlen(map) > YPMAXMAP) {
(void) fprintf(stderr, err_bad_args,
err_bad_mapname);
exit(1);
}
} else {
(void) fprintf(stderr, err_usage);
exit(1);
}
}
}
if (!map) {
(void) fprintf(stderr, err_usage);
exit(1);
}
}
/*
* This gets the local default domainname, and makes sure that it's set
* to something reasonable. domain is set here.
*/
static void
getdomain()
{
if (!getdomainname(default_domain_name, YPMAXDOMAIN)) {
domain = default_domain_name;
} else {
(void) fprintf(stderr, err_cant_get_kname, err_bad_domainname);
exit(1);
}
if ((int)strlen(domain) == 0) {
(void) fprintf(stderr, err_null_kname, err_bad_domainname);
exit(1);
}
}
/*
* This gets the local hostname back from the kernel
*/
static void
getlochost()
{
if (! gethostname(default_host_name, 256)) {
host = default_host_name;
} else {
(void) fprintf(stderr, err_cant_get_kname, err_bad_hostname);
exit(1);
}
}
static void
getmapparms()
{
CLIENT * map_clnt;
struct ypresp_order oresp;
struct ypreq_nokey req;
struct ypresp_master mresp;
struct ypresp_master *mresults = (struct ypresp_master *)NULL;
struct ypresp_order *oresults = (struct ypresp_order *)NULL;
struct timeval timeout;
enum clnt_stat s;
if ((map_clnt = clnt_create(host, YPPROG, YPVERS,
"netpath")) == NULL) {
(void) fprintf(stderr,
"Can't create connection to %s.\n", host);
clnt_pcreateerror("Reason");
exit(1);
}
timeout.tv_sec = TIMEOUT;
timeout.tv_usec = 0;
req.domain = domain;
req.map = map;
mresp.master = NULL;
if (clnt_call(map_clnt, YPPROC_MASTER, (xdrproc_t)xdr_ypreq_nokey,
(caddr_t)&req, (xdrproc_t)xdr_ypresp_master,
(caddr_t)&mresp, timeout) == RPC_SUCCESS) {
mresults = &mresp;
s = (enum clnt_stat) clnt_call(map_clnt, YPPROC_ORDER,
(xdrproc_t)xdr_ypreq_nokey, (char *)&req,
(xdrproc_t)xdr_ypresp_order, (char *)&oresp, timeout);
if (s == RPC_SUCCESS) {
oresults = &oresp;
newresults(mresults, oresults);
} else {
(void) fprintf(stderr,
"Can't make YPPROC_ORDER call to ypserv at %s.\n ",
host);
clnt_perror(map_clnt, "Reason");
exit(1);
}
} else {
clnt_destroy(map_clnt);
}
}
static void
newresults(m, o)
struct ypresp_master *m;
struct ypresp_order *o;
{
char *s_domok = "Domain %s is supported.\n";
char *s_ook = "Map %s has order number %d.\n";
char *s_mok = "The master server is %s.\n";
char *s_mbad = "Can't get master for map %s.\n Reason: %s\n";
char *s_obad = "Can't get order number for map %s.\n Reason: %s\n";
if (m->status == YP_TRUE && o->status == YP_TRUE) {
(void) printf(s_domok, domain);
(void) printf(s_ook, map, o->ordernum);
(void) printf(s_mok, m->master);
} else if (o->status == YP_TRUE) {
(void) printf(s_domok, domain);
(void) printf(s_ook, map, o->ordernum);
(void) fprintf(stderr, s_mbad, map,
yperr_string(ypprot_err(m->status)));
status = 1;
} else if (m->status == YP_TRUE) {
(void) printf(s_domok, domain);
(void) fprintf(stderr, s_obad, map,
yperr_string(ypprot_err(o->status)));
(void) printf(s_mok, m->master);
status = 1;
} else {
(void) fprintf(stderr,
"Can't get any map parameter information.\n");
(void) fprintf(stderr, s_obad, map,
yperr_string(ypprot_err(o->status)));
(void) fprintf(stderr, s_mbad, map,
yperr_string(ypprot_err(m->status)));
status = 1;
}
}
static void
getypserv()
{
struct ypbind_resp response;
struct ypbind_domain ypdomain;
struct ypbind_binding *binding;
static char hostbuf[256];
getlochost();
(void) memset((char *)&response, 0, sizeof (response));
ypdomain.ypbind_domainname = domain;
ypdomain.ypbind_vers = YPBINDVERS;
(void) rpc_call(host, YPBINDPROG, YPBINDVERS, YPBINDPROC_DOMAIN,
xdr_ypbind_domain, (char *)&ypdomain, xdr_ypbind_resp,
(char *)&response, "netpath");
if (response.ypbind_status != YPBIND_SUCC_VAL) {
(void) fprintf(stderr, "couldn't get yp server - status %u\n",
response.ypbind_status);
exit(1);
}
binding = response.ypbind_resp_u.ypbind_bindinfo;
host = binding->ypbind_servername;
/*
* When ypbind is running in broadcast mode, it sets the
* servername to "". To get the real name of the server,
* we need to do a host lookup on the svcaddr. This code
* is similar to code in ypwhich.
*/
if (strcmp(host, "") == 0) {
struct nd_hostservlist *nhs;
struct netconfig *nconf = binding->ypbind_nconf;
struct netbuf *svcaddr = binding->ypbind_svcaddr;
if (netdir_getbyaddr(nconf, &nhs, svcaddr) != ND_OK) {
struct sockaddr_in *sa;
sa = (struct sockaddr_in *)svcaddr->buf;
strcpy(hostbuf, inet_ntoa(sa->sin_addr));
} else {
sprintf(hostbuf, "%s", nhs->h_hostservs->h_host);
}
host = hostbuf;
netdir_free((char *)nhs, ND_HOSTSERVLIST);
}
}
|