4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 * Portions of this source code were derived from Berkeley
31 * under license from the Regents of the University of
35 #pragma ident "%Z%%M% %I% %E% SMI"
38 * This is a user command which asks a particular ypserv which version of a
39 * map it is using. Usage is:
41 * yppoll [-h <host>] [-d <domainname>] mapname
43 * If the host is ommitted, the local host will be used. If host is specified
44 * as an internet address, no yp services need to be locally available.
50 #include <rpcsvc/ypclnt.h>
51 #include <rpcsvc/yp_prot.h>
53 #include <arpa/inet.h>
61 #define TIMEOUT 30 /* Total seconds for timeout */
63 static int status
= 0; /* exit status */
64 static char *domain
= NULL
;
65 static char default_domain_name
[YPMAXDOMAIN
];
66 static char *map
= NULL
;
67 static char *host
= NULL
;
68 static char default_host_name
[256];
70 static char err_usage
[] =
72 yppoll [ -h host ] [ -d domainname ] mapname\n\n";
73 static char err_bad_args
[] =
75 static char err_cant_get_kname
[] =
76 "Can't get %s back from system call.\n";
77 static char err_null_kname
[] =
78 "%s hasn't been set on this machine.\n";
79 static char err_bad_hostname
[] = "hostname";
80 static char err_bad_mapname
[] = "mapname";
81 static char err_bad_domainname
[] = "domainname";
82 static char err_bad_resp
[] =
83 "Ill-formed response returned from ypserv on host %s.\n";
85 static void get_command_line_args();
86 static void getdomain();
87 static void getlochost();
88 static void getmapparms();
89 static void newresults();
90 static void getypserv();
93 extern int getdomainname();
94 extern int gethostname();
95 extern unsigned int strlen();
99 * This is the mainline for the yppoll process.
108 get_command_line_args(argc
, argv
);
123 * This does the command line argument processing.
126 get_command_line_args(argc
, argv
)
135 if ((*argv
)[0] == '-') {
137 switch ((*argv
)[1]) {
147 if ((int)strlen(host
) > 256) {
148 (void) fprintf(stderr
,
155 (void) fprintf(stderr
, err_usage
);
169 if ((int)strlen(domain
) > YPMAXDOMAIN
) {
170 (void) fprintf(stderr
,
177 (void) fprintf(stderr
, err_usage
);
184 (void) fprintf(stderr
, err_usage
);
193 if ((int)strlen(map
) > YPMAXMAP
) {
194 (void) fprintf(stderr
, err_bad_args
,
200 (void) fprintf(stderr
, err_usage
);
207 (void) fprintf(stderr
, err_usage
);
213 * This gets the local default domainname, and makes sure that it's set
214 * to something reasonable. domain is set here.
219 if (!getdomainname(default_domain_name
, YPMAXDOMAIN
)) {
220 domain
= default_domain_name
;
222 (void) fprintf(stderr
, err_cant_get_kname
, err_bad_domainname
);
226 if ((int)strlen(domain
) == 0) {
227 (void) fprintf(stderr
, err_null_kname
, err_bad_domainname
);
233 * This gets the local hostname back from the kernel
239 if (! gethostname(default_host_name
, 256)) {
240 host
= default_host_name
;
242 (void) fprintf(stderr
, err_cant_get_kname
, err_bad_hostname
);
251 struct ypresp_order oresp
;
252 struct ypreq_nokey req
;
253 struct ypresp_master mresp
;
254 struct ypresp_master
*mresults
= (struct ypresp_master
*)NULL
;
255 struct ypresp_order
*oresults
= (struct ypresp_order
*)NULL
;
257 struct timeval timeout
;
260 if ((map_clnt
= clnt_create(host
, YPPROG
, YPVERS
,
261 "netpath")) == NULL
) {
262 (void) fprintf(stderr
,
263 "Can't create connection to %s.\n", host
);
264 clnt_pcreateerror("Reason");
268 timeout
.tv_sec
= TIMEOUT
;
274 if (clnt_call(map_clnt
, YPPROC_MASTER
, (xdrproc_t
)xdr_ypreq_nokey
,
275 (caddr_t
)&req
, (xdrproc_t
)xdr_ypresp_master
,
276 (caddr_t
)&mresp
, timeout
) == RPC_SUCCESS
) {
278 s
= (enum clnt_stat
) clnt_call(map_clnt
, YPPROC_ORDER
,
279 (xdrproc_t
)xdr_ypreq_nokey
, (char *)&req
,
280 (xdrproc_t
)xdr_ypresp_order
, (char *)&oresp
, timeout
);
282 if (s
== RPC_SUCCESS
) {
284 newresults(mresults
, oresults
);
286 (void) fprintf(stderr
,
287 "Can't make YPPROC_ORDER call to ypserv at %s.\n ",
289 clnt_perror(map_clnt
, "Reason");
294 clnt_destroy(map_clnt
);
300 struct ypresp_master
*m
;
301 struct ypresp_order
*o
;
303 char *s_domok
= "Domain %s is supported.\n";
304 char *s_ook
= "Map %s has order number %d.\n";
305 char *s_mok
= "The master server is %s.\n";
306 char *s_mbad
= "Can't get master for map %s.\n Reason: %s\n";
307 char *s_obad
= "Can't get order number for map %s.\n Reason: %s\n";
309 if (m
->status
== YP_TRUE
&& o
->status
== YP_TRUE
) {
310 (void) printf(s_domok
, domain
);
311 (void) printf(s_ook
, map
, o
->ordernum
);
312 (void) printf(s_mok
, m
->master
);
313 } else if (o
->status
== YP_TRUE
) {
314 (void) printf(s_domok
, domain
);
315 (void) printf(s_ook
, map
, o
->ordernum
);
316 (void) fprintf(stderr
, s_mbad
, map
,
317 yperr_string(ypprot_err(m
->status
)));
319 } else if (m
->status
== YP_TRUE
) {
320 (void) printf(s_domok
, domain
);
321 (void) fprintf(stderr
, s_obad
, map
,
322 yperr_string(ypprot_err(o
->status
)));
323 (void) printf(s_mok
, m
->master
);
326 (void) fprintf(stderr
,
327 "Can't get any map parameter information.\n");
328 (void) fprintf(stderr
, s_obad
, map
,
329 yperr_string(ypprot_err(o
->status
)));
330 (void) fprintf(stderr
, s_mbad
, map
,
331 yperr_string(ypprot_err(m
->status
)));
339 struct ypbind_resp response
;
340 struct ypbind_domain ypdomain
;
341 struct ypbind_binding
*binding
;
342 static char hostbuf
[256];
346 (void) memset((char *)&response
, 0, sizeof (response
));
347 ypdomain
.ypbind_domainname
= domain
;
348 ypdomain
.ypbind_vers
= YPBINDVERS
;
349 (void) rpc_call(host
, YPBINDPROG
, YPBINDVERS
, YPBINDPROC_DOMAIN
,
350 xdr_ypbind_domain
, (char *)&ypdomain
, xdr_ypbind_resp
,
351 (char *)&response
, "netpath");
352 if (response
.ypbind_status
!= YPBIND_SUCC_VAL
) {
353 (void) fprintf(stderr
, "couldn't get yp server - status %u\n",
354 response
.ypbind_status
);
357 binding
= response
.ypbind_resp_u
.ypbind_bindinfo
;
358 host
= binding
->ypbind_servername
;
361 * When ypbind is running in broadcast mode, it sets the
362 * servername to "". To get the real name of the server,
363 * we need to do a host lookup on the svcaddr. This code
364 * is similar to code in ypwhich.
366 if (strcmp(host
, "") == 0) {
367 struct nd_hostservlist
*nhs
;
368 struct netconfig
*nconf
= binding
->ypbind_nconf
;
369 struct netbuf
*svcaddr
= binding
->ypbind_svcaddr
;
371 if (netdir_getbyaddr(nconf
, &nhs
, svcaddr
) != ND_OK
) {
372 struct sockaddr_in
*sa
;
374 sa
= (struct sockaddr_in
*)svcaddr
->buf
;
376 strcpy(hostbuf
, inet_ntoa(sa
->sin_addr
));
378 sprintf(hostbuf
, "%s", nhs
->h_hostservs
->h_host
);
381 netdir_free((char *)nhs
, ND_HOSTSERVLIST
);