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
36 * This is a user command which asks a particular ypserv which version of a
37 * map it is using. Usage is:
39 * yppoll [-h <host>] [-d <domainname>] mapname
41 * If the host is ommitted, the local host will be used. If host is specified
42 * as an internet address, no yp services need to be locally available.
48 #include <rpcsvc/ypclnt.h>
49 #include <rpcsvc/yp_prot.h>
51 #include <arpa/inet.h>
54 #define TIMEOUT 30 /* Total seconds for timeout */
56 static int status
= 0; /* exit status */
57 static char *domain
= NULL
;
58 static char default_domain_name
[YPMAXDOMAIN
];
59 static char *map
= NULL
;
60 static char *host
= NULL
;
61 static char default_host_name
[256];
63 static char err_usage
[] =
65 yppoll [ -h host ] [ -d domainname ] mapname\n\n";
66 static char err_bad_args
[] =
68 static char err_cant_get_kname
[] =
69 "Can't get %s back from system call.\n";
70 static char err_null_kname
[] =
71 "%s hasn't been set on this machine.\n";
72 static char err_bad_hostname
[] = "hostname";
73 static char err_bad_mapname
[] = "mapname";
74 static char err_bad_domainname
[] = "domainname";
75 static char err_bad_resp
[] =
76 "Ill-formed response returned from ypserv on host %s.\n";
78 static void get_command_line_args();
79 static void getdomain();
80 static void getlochost();
81 static void getmapparms();
82 static void newresults();
83 static void getypserv();
86 extern int getdomainname();
87 extern int gethostname();
88 extern unsigned int strlen();
92 * This is the mainline for the yppoll process.
101 get_command_line_args(argc
, argv
);
116 * This does the command line argument processing.
119 get_command_line_args(argc
, argv
)
128 if ((*argv
)[0] == '-') {
130 switch ((*argv
)[1]) {
140 if ((int)strlen(host
) > 256) {
141 (void) fprintf(stderr
,
148 (void) fprintf(stderr
, err_usage
);
162 if ((int)strlen(domain
) > YPMAXDOMAIN
) {
163 (void) fprintf(stderr
,
170 (void) fprintf(stderr
, err_usage
);
177 (void) fprintf(stderr
, err_usage
);
186 if ((int)strlen(map
) > YPMAXMAP
) {
187 (void) fprintf(stderr
, err_bad_args
,
193 (void) fprintf(stderr
, err_usage
);
200 (void) fprintf(stderr
, err_usage
);
206 * This gets the local default domainname, and makes sure that it's set
207 * to something reasonable. domain is set here.
212 if (!getdomainname(default_domain_name
, YPMAXDOMAIN
)) {
213 domain
= default_domain_name
;
215 (void) fprintf(stderr
, err_cant_get_kname
, err_bad_domainname
);
219 if ((int)strlen(domain
) == 0) {
220 (void) fprintf(stderr
, err_null_kname
, err_bad_domainname
);
226 * This gets the local hostname back from the kernel
232 if (! gethostname(default_host_name
, 256)) {
233 host
= default_host_name
;
235 (void) fprintf(stderr
, err_cant_get_kname
, err_bad_hostname
);
244 struct ypresp_order oresp
;
245 struct ypreq_nokey req
;
246 struct ypresp_master mresp
;
247 struct ypresp_master
*mresults
= NULL
;
248 struct ypresp_order
*oresults
= NULL
;
250 struct timeval timeout
;
253 if ((map_clnt
= clnt_create(host
, YPPROG
, YPVERS
,
254 "netpath")) == NULL
) {
255 (void) fprintf(stderr
,
256 "Can't create connection to %s.\n", host
);
257 clnt_pcreateerror("Reason");
261 timeout
.tv_sec
= TIMEOUT
;
267 if (clnt_call(map_clnt
, YPPROC_MASTER
, (xdrproc_t
)xdr_ypreq_nokey
,
268 (caddr_t
)&req
, (xdrproc_t
)xdr_ypresp_master
,
269 (caddr_t
)&mresp
, timeout
) == RPC_SUCCESS
) {
271 s
= (enum clnt_stat
) clnt_call(map_clnt
, YPPROC_ORDER
,
272 (xdrproc_t
)xdr_ypreq_nokey
, (char *)&req
,
273 (xdrproc_t
)xdr_ypresp_order
, (char *)&oresp
, timeout
);
275 if (s
== RPC_SUCCESS
) {
277 newresults(mresults
, oresults
);
279 (void) fprintf(stderr
,
280 "Can't make YPPROC_ORDER call to ypserv"
283 clnt_perror(map_clnt
, "Reason");
288 clnt_destroy(map_clnt
);
294 struct ypresp_master
*m
;
295 struct ypresp_order
*o
;
297 char *s_domok
= "Domain %s is supported.\n";
298 char *s_ook
= "Map %s has order number %d.\n";
299 char *s_mok
= "The master server is %s.\n";
300 char *s_mbad
= "Can't get master for map %s.\n Reason: %s\n";
301 char *s_obad
= "Can't get order number for map %s.\n Reason: %s\n";
303 if (m
->status
== YP_TRUE
&& o
->status
== YP_TRUE
) {
304 (void) printf(s_domok
, domain
);
305 (void) printf(s_ook
, map
, o
->ordernum
);
306 (void) printf(s_mok
, m
->master
);
307 } else if (o
->status
== YP_TRUE
) {
308 (void) printf(s_domok
, domain
);
309 (void) printf(s_ook
, map
, o
->ordernum
);
310 (void) fprintf(stderr
, s_mbad
, map
,
311 yperr_string(ypprot_err(m
->status
)));
313 } else if (m
->status
== YP_TRUE
) {
314 (void) printf(s_domok
, domain
);
315 (void) fprintf(stderr
, s_obad
, map
,
316 yperr_string(ypprot_err(o
->status
)));
317 (void) printf(s_mok
, m
->master
);
320 (void) fprintf(stderr
,
321 "Can't get any map parameter information.\n");
322 (void) fprintf(stderr
, s_obad
, map
,
323 yperr_string(ypprot_err(o
->status
)));
324 (void) fprintf(stderr
, s_mbad
, map
,
325 yperr_string(ypprot_err(m
->status
)));
333 struct ypbind_resp response
;
334 struct ypbind_domain ypdomain
;
335 struct ypbind_binding
*binding
;
336 static char hostbuf
[256];
340 (void) memset((char *)&response
, 0, sizeof (response
));
341 ypdomain
.ypbind_domainname
= domain
;
342 ypdomain
.ypbind_vers
= YPBINDVERS
;
343 (void) rpc_call(host
, YPBINDPROG
, YPBINDVERS
, YPBINDPROC_DOMAIN
,
344 xdr_ypbind_domain
, (char *)&ypdomain
, xdr_ypbind_resp
,
345 (char *)&response
, "netpath");
346 if (response
.ypbind_status
!= YPBIND_SUCC_VAL
) {
347 (void) fprintf(stderr
, "couldn't get yp server - status %u\n",
348 response
.ypbind_status
);
351 binding
= response
.ypbind_resp_u
.ypbind_bindinfo
;
352 host
= binding
->ypbind_servername
;
355 * When ypbind is running in broadcast mode, it sets the
356 * servername to "". To get the real name of the server,
357 * we need to do a host lookup on the svcaddr. This code
358 * is similar to code in ypwhich.
360 if (strcmp(host
, "") == 0) {
361 struct nd_hostservlist
*nhs
;
362 struct netconfig
*nconf
= binding
->ypbind_nconf
;
363 struct netbuf
*svcaddr
= binding
->ypbind_svcaddr
;
365 if (netdir_getbyaddr(nconf
, &nhs
, svcaddr
) != ND_OK
) {
366 struct sockaddr_in
*sa
;
368 sa
= (struct sockaddr_in
*)svcaddr
->buf
;
370 strcpy(hostbuf
, inet_ntoa(sa
->sin_addr
));
372 sprintf(hostbuf
, "%s", nhs
->h_hostservs
->h_host
);
375 netdir_free((char *)nhs
, ND_HOSTSERVLIST
);