cmd-inet/usr.sbin: remove -Wno-implicit-function-declaration
[unleashed.git] / usr / src / cmd / ypcmd / ypwhich.c
blob499e5ccefc3988b8ece0f417fe236bffd90d49ee
1 /*
2 * CDDL HEADER START
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
7 * with the License.
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]
20 * CDDL HEADER END
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
32 * California.
35 #pragma ident "%Z%%M% %I% %E% SMI"
38 * This is a user command which tells which yp server is being used by a
39 * given machine, or which yp server is the master for a named map.
41 * Usage is:
42 * ypwhich [-d domain] [-m [mname] [-t] | [-Vn] host]
43 * ypwhich -x
44 * where: the -d switch can be used to specify a domain other than the
45 * default domain. -m tells the master of that map. mname is a mapname
46 * If the -m option is used, ypwhich will act like a vanilla yp client,
47 * and will not attempt to choose a particular yp server. On the
48 * other hand, if no -m switch is used, ypwhich will talk directly to the yp
49 * bind process on the named host, or to the local ypbind process if no host
50 * name is specified. -t switch inhibits nickname translation of map names.
51 * -x is to dump the nickname translation table from file /var/yp/nicknames.
55 #include <stdio.h>
56 #include <ctype.h>
57 #include <rpc/rpc.h>
58 #include <rpcsvc/yp_prot.h>
59 #include <rpcsvc/ypclnt.h>
60 #include "yp_b.h"
61 #include "ypv2_bind.h"
62 #include <string.h>
63 #include <netdir.h>
64 #include <unistd.h>
65 #include <netdb.h>
66 #include <arpa/inet.h>
67 #include <inet/ip.h>
68 #include <inet/ip6.h>
69 #include <netinet/ip6.h>
70 #include <sys/utsname.h>
72 #define YPSLEEPTIME 5 /* between two tries of bind */
74 #define TIMEOUT 30 /* Total seconds for timeout */
75 #define INTER_TRY 10 /* Seconds between tries */
77 static int translate = TRUE;
78 static int dodump = FALSE;
79 static char *domain = NULL;
80 static char default_domain_name[YPMAXDOMAIN];
81 static char *host = NULL;
82 static int vers = YPBINDVERS;
83 static char default_host_name[256];
84 static bool get_master = FALSE;
85 static bool get_server = FALSE;
86 static char *map = NULL;
87 static char nm[YPMAXMAP+1];
88 static struct timeval timeout = {
89 TIMEOUT, /* Seconds */
90 0 /* Microseconds */
92 static char nullstring[] = "\000";
93 static char err_usage[] =
94 "Usage:\n\
95 ypwhich [-d domain] [[-t] -m [mname] | [-Vn] host]\n\
96 ypwhich -x\n\
97 where\n\
98 mname may be either a mapname or a nickname for a map.\n\
99 host if specified, is the machine whose NIS server is to be found.\n\
100 -t inhibits map nickname translation.\n\
101 -Vn version of ypbind, V3 is default.\n\
102 -x dumps the map nickname translation table.\n";
103 static char err_bad_args[] =
104 "ypwhich: %s argument is bad.\n";
105 static char err_cant_get_kname[] =
106 "ypwhich: can't get %s back from system call.\n";
107 static char err_null_kname[] =
108 "ypwhich: the %s hasn't been set on this machine.\n";
109 static char err_bad_mapname[] = "mapname";
110 static char err_bad_domainname[] = "domainname";
111 static char err_bad_hostname[] = "hostname";
113 static void get_command_line_args();
114 static void getdomain();
115 static void getlochost();
116 static void get_server_name();
117 static int call_binder();
118 static void get_map_master();
119 extern void maketable();
120 extern int getmapname();
121 #ifdef DEBUG
122 static void dump_response();
123 #endif
124 static void dump_ypmaps();
125 static void dumpmaps();
127 static bool xdr_yp_inaddr();
128 static bool xdr_old_ypbind_resp();
129 static bool xdr_old_yp_binding();
130 static int old_call_binder();
131 static void print_server();
133 /* need these for call to (remote) V2 ypbind */
134 struct old_ypbind_binding {
135 struct in_addr ypbind_binding_addr; /* In network order */
136 unsigned short int ypbind_binding_port; /* In network order */
139 struct old_ypbind_resp {
140 enum ypbind_resptype ypbind_status;
141 union {
142 unsigned long ypbind_error;
143 struct old_ypbind_binding ypbind_bindinfo;
144 } ypbind_respbody;
148 * This is the main line for the ypwhich process.
151 main(argc, argv)
152 char **argv;
154 get_command_line_args(argc, argv);
156 if (dodump) {
157 maketable(dodump);
158 exit(0);
161 if (!domain) {
162 getdomain();
165 if (map && translate && (strchr(map, '.') == NULL) &&
166 (getmapname(map, nm))) {
167 map = nm;
170 if (get_server) {
171 if (!host)
172 getlochost();
173 get_server_name();
174 } else {
175 if (map)
176 get_map_master();
177 else
178 dump_ypmaps();
181 return (0);
185 * This does the command line argument processing.
187 static void
188 get_command_line_args(argc, argv)
189 int argc;
190 char **argv;
193 argv++;
195 if (argc == 1) {
196 get_server = TRUE;
197 return;
200 while (--argc) {
202 if ((*argv)[0] == '-') {
204 switch ((*argv)[1]) {
206 case 'V':
208 vers = atoi(argv[0]+2);
209 if (vers < 1) {
210 (void) fprintf(stderr, err_usage);
211 exit(1);
213 argv++;
214 break;
216 case 'm':
217 get_master = TRUE;
218 argv++;
220 if (argc > 1) {
222 if ((*(argv))[0] == '-') {
223 break;
226 argc--;
227 map = *argv;
228 argv++;
230 if ((int)strlen(map) > YPMAXMAP) {
231 (void) fprintf(stderr, err_bad_args,
232 err_bad_mapname);
233 exit(1);
238 break;
240 case 'd':
242 if (argc > 1) {
243 argv++;
244 argc--;
245 domain = *argv;
246 argv++;
248 if ((int)strlen(domain) > YPMAXDOMAIN) {
249 (void) fprintf(stderr, err_bad_args,
250 err_bad_domainname);
251 exit(1);
254 } else {
255 (void) fprintf(stderr, err_usage);
256 exit(1);
259 break;
261 case 't':
262 translate = FALSE;
263 argv++;
264 break;
266 case 'x':
267 dodump = TRUE;
268 argv++;
269 break;
271 default:
272 (void) fprintf(stderr, err_usage);
273 exit(1);
276 } else {
278 if (get_server) {
279 (void) fprintf(stderr, err_usage);
280 exit(1);
283 get_server = TRUE;
284 host = *argv;
285 argv++;
287 if ((int)strlen(host) > 256) {
288 (void) fprintf(stderr,
289 err_bad_args, err_bad_hostname);
290 exit(1);
295 if (get_master && get_server) {
296 (void) fprintf(stderr, err_usage);
297 exit(1);
300 if (!get_master && !get_server) {
301 get_server = TRUE;
306 * This gets the local default domainname, and makes sure that it's set
307 * to something reasonable. domain is set here.
309 static void
310 getdomain()
312 if (!getdomainname(default_domain_name, YPMAXDOMAIN)) {
313 domain = default_domain_name;
314 } else {
315 (void) fprintf(stderr, err_cant_get_kname, err_bad_domainname);
316 exit(1);
319 if ((int)strlen(domain) == 0) {
320 (void) fprintf(stderr, err_null_kname, err_bad_domainname);
321 exit(1);
326 * This gets the local hostname back from the kernel
328 static void
329 getlochost()
331 struct utsname utsname;
333 if (uname(&utsname) != -1) {
334 strcpy(default_host_name, utsname.nodename);
335 host = default_host_name;
336 } else {
337 (void) fprintf(stderr, err_cant_get_kname, err_bad_hostname);
338 exit(1);
344 * This tries to find the name of the server to which the binder in question
345 * is bound. If one of the -Vx flags was specified, it will try only for
346 * that protocol version, otherwise, it will start with the current version,
347 * then drop back to the previous version.
349 static void
350 get_server_name()
352 char *notbound = "Domain %s not bound on %s.\n";
354 if (vers >= 3) {
355 if (!call_binder(vers))
356 (void) fprintf(stderr, notbound, domain, host);
357 } else {
358 if (!old_call_binder(vers))
359 (void) fprintf(stderr, notbound, domain, host);
363 extern CLIENT *__clnt_create_loopback();
366 * This sends a message to the ypbind process on the node with
367 * the host name
369 static int
370 call_binder(vers)
371 int vers;
373 CLIENT *client;
374 struct ypbind_resp *response;
375 struct ypbind_domain ypbd;
376 char errstring[256];
377 int yperr = 0;
378 struct utsname utsname;
379 const char *str;
382 * CAUTION: Do not go to NIS if the host is the same as the local host
383 * XXX: Lots of special magic to distinguish between local and remote
384 * case. We want to make sure the local case doesn't hang.
387 if ((uname(&utsname) != -1) &&
388 (strcmp(host, utsname.nodename) == 0))
389 client = __clnt_create_loopback(YPBINDPROG, vers, &yperr);
390 else
391 client = clnt_create(host, YPBINDPROG, vers, "netpath");
392 if (client == NULL) {
393 if (yperr)
394 (void) fprintf(stderr,
395 "ypwhich: %s\n", yperr_string(yperr));
396 else {
397 if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED ||
398 rpc_createerr.cf_stat == RPC_PROGUNAVAIL) {
399 (void) fprintf(stderr,
400 "ypwhich: %s is not running ypbind\n", host);
401 } else if (rpc_createerr.cf_stat == RPC_PMAPFAILURE) {
402 (void) fprintf(stderr,
403 "ypwhich: %s is not running rpcbind\n",
404 host);
405 } else
406 (void) clnt_pcreateerror("ypwhich: \
407 clnt_create error");
409 exit(1);
411 ypbd.ypbind_domainname = domain;
412 ypbd.ypbind_vers = vers;
413 response = ypbindproc_domain_3(&ypbd, client);
415 if (response == NULL) {
416 (void) sprintf(errstring,
417 "ypwhich: can't call ypbind on %s", host);
418 (void) clnt_perror(client, errstring);
419 exit(1);
422 clnt_destroy(client);
424 if (response->ypbind_status != YPBIND_SUCC_VAL) {
425 return (FALSE);
428 if (response->ypbind_resp_u.ypbind_bindinfo) {
429 char *server =
430 response->ypbind_resp_u.ypbind_bindinfo->ypbind_servername;
432 if (strcmp(server, nullstring) == 0) {
433 /* depends on a hack in ypbind */
434 struct nd_hostservlist *nhs = NULL;
435 struct netconfig *nconf =
436 response->ypbind_resp_u.ypbind_bindinfo->ypbind_nconf;
437 struct netbuf *svcaddr =
438 response->ypbind_resp_u.ypbind_bindinfo->ypbind_svcaddr;
440 if (netdir_getbyaddr(nconf, &nhs, svcaddr) != ND_OK) {
441 struct sockaddr_in *sa4;
442 struct sockaddr_in6 *sa6;
443 char buf[INET6_ADDRSTRLEN];
444 char xbuf[IPV6_ADDR_LEN];
445 int af;
446 void *addr;
447 XDR xdrs;
449 sa4 = (struct sockaddr_in *)svcaddr->buf;
450 af = ntohs(sa4->sin_family);
451 if (af != sa4->sin_family) {
452 xdrmem_create(&xdrs,
453 (caddr_t)xbuf, IPV6_ADDR_LEN,
454 XDR_DECODE);
455 if (af == AF_INET6) {
456 xdr_opaque(&xdrs,
457 (caddr_t)svcaddr->buf,
458 IPV6_ADDR_LEN);
459 sa6 = (struct sockaddr_in6 *)
460 xbuf;
461 addr = &sa6->sin6_addr;
462 } else {
463 xdr_opaque(&xdrs,
464 (caddr_t)svcaddr->buf,
465 IPV4_ADDR_LEN);
466 sa4 = (struct sockaddr_in *)
467 xbuf;
468 addr = &sa4->sin_addr;
470 } else {
471 if (af == AF_INET6) {
472 sa6 = (struct sockaddr_in6 *)
473 svcaddr->buf;
474 addr = &sa6->sin6_addr;
475 } else {
476 addr = &sa4->sin_addr;
479 str = inet_ntop(af, addr, buf, sizeof (buf));
480 if (str == NULL)
481 perror("inet_ntop");
482 else
483 fprintf(stdout, "%s\n", str);
484 } else {
485 str = nhs->h_hostservs->h_host;
486 if (str == NULL)
487 str = "<unknown>";
488 fprintf(stdout, "%s\n", str);
490 netdir_free((char *)nhs, ND_HOSTSERVLIST);
491 } else {
492 fprintf(stdout, "%s\n", server);
495 #ifdef DEBUG
496 dump_response(response);
497 #endif
498 return (TRUE);
502 * Serializes/deserializes an in_addr struct.
504 * Note: There is a data coupling between the "definition" of a struct
505 * in_addr implicit in this xdr routine, and the true data definition in
506 * <netinet/in.h>.
508 static bool xdr_yp_inaddr(xdrs, ps)
509 XDR * xdrs;
510 struct in_addr *ps;
513 return (xdr_opaque(xdrs, (caddr_t)&ps->s_addr, 4));
517 * Serializes/deserializes an old ypbind_binding struct.
519 static bool xdr_old_yp_binding(xdrs, ps)
520 XDR * xdrs;
521 struct old_ypbind_binding *ps;
524 return (xdr_yp_inaddr(xdrs, &ps->ypbind_binding_addr) &&
525 xdr_opaque(xdrs, (caddr_t)&ps->ypbind_binding_port, 2));
529 * Serializes/deserializes a ypbind_resp structure.
531 static bool xdr_old_ypbind_resp(xdrs, ps)
532 XDR * xdrs;
533 struct old_ypbind_resp *ps;
536 if (!xdr_enum(xdrs, (enum_t *)&ps->ypbind_status)) {
537 return (FALSE);
539 switch (ps->ypbind_status) {
540 case YPBIND_SUCC_VAL:
541 return (xdr_old_yp_binding(xdrs,
542 &ps->ypbind_respbody.ypbind_bindinfo));
543 case YPBIND_FAIL_VAL:
544 return (xdr_u_long(xdrs,
545 &ps->ypbind_respbody.ypbind_error));
547 return (FALSE);
549 /* This sends a message to the old ypbind process on host. */
550 static int old_call_binder(vers)
551 int vers;
553 CLIENT *client;
554 struct hostent *hp;
555 int sock = RPC_ANYSOCK;
556 enum clnt_stat rpc_stat;
557 struct old_ypbind_resp response;
558 char errstring[256];
559 struct in_addr *server;
561 if ((client = clnt_create(host, YPBINDPROG, vers, "udp")) == NULL) {
562 if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED) {
563 (void) printf("ypwhich: %s is not running ypbind\n",
564 host);
565 exit(1);
567 if (rpc_createerr.cf_stat == RPC_PMAPFAILURE) {
568 (void) printf("ypwhich: %s is not running port mapper\n",
569 host);
570 exit(1);
572 (void) clnt_pcreateerror("ypwhich: clnt_create error");
573 exit(1);
576 rpc_stat = clnt_call(client, YPBINDPROC_DOMAIN,
577 (xdrproc_t)xdr_ypdomain_wrap_string, (caddr_t)&domain,
578 (xdrproc_t)xdr_old_ypbind_resp, (caddr_t)&response,
579 timeout);
581 if ((rpc_stat != RPC_SUCCESS) &&
582 (rpc_stat != RPC_PROGVERSMISMATCH)) {
583 (void) sprintf(errstring,
584 "ypwhich: can't call ypbind on %s", host);
585 (void) clnt_perror(client, errstring);
586 exit(1);
589 clnt_destroy(client);
590 close(sock);
592 if ((rpc_stat != RPC_SUCCESS) ||
593 (response.ypbind_status != YPBIND_SUCC_VAL)) {
594 return (FALSE);
597 server = &response.ypbind_respbody.ypbind_bindinfo.ypbind_binding_addr;
598 print_server (server);
600 return (TRUE);
604 * For old version:
605 * This translates a server address to a name and prints it.
606 * We'll get a name by using the standard library routine.
608 static void print_server(server)
609 struct in_addr *server;
611 char buf[256];
612 struct hostent *hp;
614 strcpy(buf, inet_ntoa(*server));
615 hp = gethostbyaddr((char *)&server->s_addr,
616 sizeof (struct in_addr), AF_INET);
618 printf("%s\n", hp ? hp->h_name : buf);
621 #ifdef DEBUG
622 static void
623 dump_response(which)
624 ypbind_resp * which;
626 struct netconfig *nc;
627 struct netbuf *ua;
628 ypbind_binding * b;
630 int i;
633 b = which->ypbind_resp_u.ypbind_bindinfo;
634 if (b == NULL)
635 (void) fprintf(stderr, "???NO Binding information\n");
636 else {
637 (void) fprintf(stderr,
638 "server=%s lovers=%ld hivers=%ld\n",
639 b->ypbind_servername,
640 b->ypbind_lo_vers, b->ypbind_hi_vers);
641 nc = b->ypbind_nconf;
642 ua = b->ypbind_svcaddr;
643 if (nc == NULL)
644 (void) fprintf(stderr,
645 "ypwhich: NO netconfig information\n");
646 else {
647 (void) fprintf(stderr,
648 "ypwhich: id %s device %s flag %x protofmly %s proto %s\n",
649 nc->nc_netid, nc->nc_device,
650 (int)nc->nc_flag, nc->nc_protofmly,
651 nc->nc_proto);
653 if (ua == NULL)
654 (void) fprintf(stderr,
655 "ypwhich: NO netbuf information available from binder\n");
656 else {
657 (void) fprintf(stderr,
658 "maxlen=%d len=%d\naddr=", ua->maxlen, ua->len);
659 for (i = 0; i < ua->len; i++) {
660 if (i != (ua->len - 1))
661 (void) fprintf(stderr,
662 "%d.", ua->buf[i]);
663 else
664 (void) fprintf(stderr,
665 "%d\n", ua->buf[i]);
672 #endif
675 * This translates a server address to a name and prints it. If the address
676 * is the same as the local address as returned by get_myaddress, the name
677 * is that retrieved from the kernel. If it's any other address (including
678 * another ip address for the local machine), we'll get a name by using the
679 * standard library routine (which calls the yp).
683 * This asks any yp server for the map's master.
685 static void
686 get_map_master()
688 int err;
689 char *master;
691 err = __yp_master_rsvdport(domain, map, &master);
693 if (err) {
694 (void) fprintf(stderr,
695 "ypwhich: Can't find the master of %s. Reason: %s.\n",
696 map, yperr_string(err));
697 exit(1);
698 } else {
699 (void) printf("%s\n", master);
704 * This enumerates the entries within map "ypmaps" in the domain at global
705 * "domain", and prints them out key and value per single line. dump_ypmaps
706 * just decides whether we are (probably) able to speak the new YP protocol,
707 * and dispatches to the appropriate function.
709 static void
710 dump_ypmaps()
712 int err;
713 struct dom_binding *binding;
715 if (err = __yp_dobind(domain, &binding)) {
716 (void) fprintf(stderr,
717 "dump_ypmaps: Can't bind for domain %s. Reason: %s\n",
718 domain, yperr_string(err));
719 return;
722 if (binding->dom_binding->ypbind_hi_vers >= YPVERS) {
723 dumpmaps(binding);
727 static void
728 dumpmaps(binding)
729 struct dom_binding *binding;
731 enum clnt_stat rpc_stat;
732 int err;
733 char *master;
734 struct ypmaplist *pmpl;
735 struct ypresp_maplist maplist;
737 maplist.list = NULL;
739 rpc_stat = clnt_call(binding->dom_client, YPPROC_MAPLIST,
740 (xdrproc_t)xdr_ypdomain_wrap_string, (caddr_t)&domain,
741 (xdrproc_t)xdr_ypresp_maplist, (caddr_t)&maplist,
742 timeout);
744 if (rpc_stat != RPC_SUCCESS) {
745 (void) clnt_perror(binding->dom_client,
746 "ypwhich(dumpmaps): can't get maplist");
747 __yp_rel_binding(binding);
748 exit(1);
751 if (maplist.status != YP_TRUE) {
752 (void) fprintf(stderr,
753 "ypwhich: Can't get maplist. Reason: %s.\n",
754 yperr_string(ypprot_err(maplist.status)));
755 exit(1);
757 __yp_rel_binding(binding);
759 for (pmpl = maplist.list; pmpl; pmpl = pmpl->ypml_next) {
760 (void) printf("%s ", pmpl->ypml_name);
762 err = __yp_master_rsvdport(domain, pmpl->ypml_name, &master);
764 if (err) {
765 (void) printf("????????\n");
766 (void) fprintf(stderr,
767 "ypwhich: Can't find the master of %s. Reason: %s.\n",
768 pmpl->ypml_name, yperr_string(err));
769 } else {
770 (void) printf("%s\n", master);