xref: /freebsd/usr.bin/ypwhich/ypwhich.c (revision 1de7b4b8)
158458d06SMarcelo Araujo /*	$OpenBSD: ypwhich.c,v 1.23 2015/02/08 23:40:35 deraadt Exp $	*/
258458d06SMarcelo Araujo /*	$NetBSD: ypwhich.c,v 1.6 1996/05/13 02:43:48 thorpej Exp $	*/
358458d06SMarcelo Araujo 
41de7b4b8SPedro F. Giffuni /*-
51de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
61de7b4b8SPedro F. Giffuni  *
758458d06SMarcelo Araujo  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt@theos.com>
873407b34SGarrett Wollman  * All rights reserved.
973407b34SGarrett Wollman  *
1073407b34SGarrett Wollman  * Redistribution and use in source and binary forms, with or without
1173407b34SGarrett Wollman  * modification, are permitted provided that the following conditions
1273407b34SGarrett Wollman  * are met:
1373407b34SGarrett Wollman  * 1. Redistributions of source code must retain the above copyright
1473407b34SGarrett Wollman  *    notice, this list of conditions and the following disclaimer.
1573407b34SGarrett Wollman  * 2. Redistributions in binary form must reproduce the above copyright
1673407b34SGarrett Wollman  *    notice, this list of conditions and the following disclaimer in the
1773407b34SGarrett Wollman  *    documentation and/or other materials provided with the distribution.
1873407b34SGarrett Wollman  *
1973407b34SGarrett Wollman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
2073407b34SGarrett Wollman  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2173407b34SGarrett Wollman  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2273407b34SGarrett Wollman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
2373407b34SGarrett Wollman  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2473407b34SGarrett Wollman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2573407b34SGarrett Wollman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2673407b34SGarrett Wollman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2773407b34SGarrett Wollman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2873407b34SGarrett Wollman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2973407b34SGarrett Wollman  * SUCH DAMAGE.
3073407b34SGarrett Wollman  */
3173407b34SGarrett Wollman 
32082d8262SMark Murray #include <sys/cdefs.h>
33082d8262SMark Murray __FBSDID("$FreeBSD$");
3473407b34SGarrett Wollman 
3573407b34SGarrett Wollman #include <sys/param.h>
3673407b34SGarrett Wollman #include <sys/types.h>
3773407b34SGarrett Wollman #include <sys/socket.h>
38082d8262SMark Murray 
3928faa530SPeter Wemm #include <netinet/in.h>
4028faa530SPeter Wemm #include <arpa/inet.h>
41082d8262SMark Murray 
42821df508SXin LI #include <ctype.h>
437008c93dSPhilippe Charnier #include <err.h>
4473407b34SGarrett Wollman #include <netdb.h>
457008c93dSPhilippe Charnier #include <stdio.h>
467008c93dSPhilippe Charnier #include <stdlib.h>
477008c93dSPhilippe Charnier #include <string.h>
487008c93dSPhilippe Charnier #include <unistd.h>
4973407b34SGarrett Wollman 
5058458d06SMarcelo Araujo #include <rpc/rpc.h>
5158458d06SMarcelo Araujo #include <rpc/xdr.h>
5258458d06SMarcelo Araujo #include <rpcsvc/yp.h>
5358458d06SMarcelo Araujo #include <rpcsvc/ypclnt.h>
548a5bd1a2SBill Paul 
5558458d06SMarcelo Araujo #include "yplib_host.h"
5673407b34SGarrett Wollman 
57006a388bSMarcelo Araujo static const struct ypalias {
58cd9df728SPeter Wemm 	char *alias, *name;
59006a388bSMarcelo Araujo } ypaliases[] = {
6073407b34SGarrett Wollman 	{ "passwd", "passwd.byname" },
61d228e65cSPeter Wemm 	{ "master.passwd", "master.passwd.byname" },
6253c40578SBrian Somers 	{ "shadow", "shadow.byname" },
6373407b34SGarrett Wollman 	{ "group", "group.byname" },
6473407b34SGarrett Wollman 	{ "networks", "networks.byaddr" },
6573407b34SGarrett Wollman 	{ "hosts", "hosts.byaddr" },
6673407b34SGarrett Wollman 	{ "protocols", "protocols.bynumber" },
6773407b34SGarrett Wollman 	{ "services", "services.byname" },
6873407b34SGarrett Wollman 	{ "aliases", "mail.aliases" },
6973407b34SGarrett Wollman 	{ "ethers", "ethers.byname" },
7073407b34SGarrett Wollman };
7173407b34SGarrett Wollman 
727008c93dSPhilippe Charnier static void
73082d8262SMark Murray usage(void)
7473407b34SGarrett Wollman {
7558458d06SMarcelo Araujo 	fprintf(stderr,
7658458d06SMarcelo Araujo 	    "usage: ypwhich [-t] [-d domain] [[-h] host]\n"
7758458d06SMarcelo Araujo 	    "       ypwhich [-t] [-d domain] [-h host] -m [mname]\n"
7858458d06SMarcelo Araujo 	    "       ypwhich -x\n");
7958458d06SMarcelo Araujo 	exit(1);
8073407b34SGarrett Wollman }
8173407b34SGarrett Wollman 
8273407b34SGarrett Wollman 
8373407b34SGarrett Wollman /*
8473407b34SGarrett Wollman  * Like yp_bind except can query a specific host
8573407b34SGarrett Wollman  */
86082d8262SMark Murray static int
8758458d06SMarcelo Araujo bind_host(char *dom, struct sockaddr_in *sin)
8873407b34SGarrett Wollman {
8973407b34SGarrett Wollman 	struct hostent *hent = NULL;
9073407b34SGarrett Wollman 	struct ypbind_resp ypbr;
9158458d06SMarcelo Araujo 	struct in_addr ss_addr;
9273407b34SGarrett Wollman 	struct timeval tv;
9373407b34SGarrett Wollman 	CLIENT *client;
9473407b34SGarrett Wollman 	int sock, r;
9573407b34SGarrett Wollman 
9673407b34SGarrett Wollman 	sock = RPC_ANYSOCK;
9773407b34SGarrett Wollman 	tv.tv_sec = 15;
9873407b34SGarrett Wollman 	tv.tv_usec = 0;
9958458d06SMarcelo Araujo 	client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
10058458d06SMarcelo Araujo 
10173407b34SGarrett Wollman 	if (client == NULL) {
10258458d06SMarcelo Araujo 		warnx("host is not bound to a ypmaster");
103ed4d1c46SDag-Erling Smørgrav 		return (YPERR_YPBIND);
10473407b34SGarrett Wollman 	}
10573407b34SGarrett Wollman 
10673407b34SGarrett Wollman 	tv.tv_sec = 5;
10773407b34SGarrett Wollman 	tv.tv_usec = 0;
10858458d06SMarcelo Araujo 
10973407b34SGarrett Wollman 	r = clnt_call(client, YPBINDPROC_DOMAIN,
110cd9df728SPeter Wemm 		(xdrproc_t)xdr_domainname, &dom,
111cd9df728SPeter Wemm 		(xdrproc_t)xdr_ypbind_resp, &ypbr, tv);
11273407b34SGarrett Wollman 	if (r != RPC_SUCCESS) {
1137008c93dSPhilippe Charnier 		warnx("can't clnt_call: %s", yperr_string(YPERR_YPBIND));
11473407b34SGarrett Wollman 		clnt_destroy(client);
115ed4d1c46SDag-Erling Smørgrav 		return (YPERR_YPBIND);
11673407b34SGarrett Wollman 	} else {
11773407b34SGarrett Wollman 		if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
1187008c93dSPhilippe Charnier 			warnx("can't yp_bind: reason: %s",
11958458d06SMarcelo Araujo 			    yperr_string(ypbr.ypbind_status));
12073407b34SGarrett Wollman 			clnt_destroy(client);
121ed4d1c46SDag-Erling Smørgrav 			return (r);
12273407b34SGarrett Wollman 		}
12373407b34SGarrett Wollman 	}
12473407b34SGarrett Wollman 	clnt_destroy(client);
12573407b34SGarrett Wollman 
12658458d06SMarcelo Araujo 	memmove(&ss_addr.s_addr, &ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
12758458d06SMarcelo Araujo 	    sizeof (ss_addr));
12858458d06SMarcelo Araujo 
12958458d06SMarcelo Araujo 	hent = gethostbyaddr((char *)&ss_addr.s_addr, sizeof(ss_addr.s_addr),
13058458d06SMarcelo Araujo 	    AF_INET);
13158458d06SMarcelo Araujo 	if (hent != NULL)
13273407b34SGarrett Wollman 		printf("%s\n", hent->h_name);
13373407b34SGarrett Wollman 	else
13473407b34SGarrett Wollman 		printf("%s\n", inet_ntoa(ss_addr));
13558458d06SMarcelo Araujo 
136ed4d1c46SDag-Erling Smørgrav 	return (0);
13773407b34SGarrett Wollman }
13873407b34SGarrett Wollman 
13973407b34SGarrett Wollman int
140082d8262SMark Murray main(int argc, char *argv[])
14173407b34SGarrett Wollman {
14258458d06SMarcelo Araujo 	char *domain, *master, *map = NULL, *host = NULL;
14358458d06SMarcelo Araujo 	int notrans = 0, mode = 0, c, r, i;
14473407b34SGarrett Wollman 	struct ypmaplist *ypml, *y;
14558458d06SMarcelo Araujo 	struct sockaddr_in sin;
14673407b34SGarrett Wollman 	struct hostent *hent;
14758458d06SMarcelo Araujo 	CLIENT *client = NULL;
14873407b34SGarrett Wollman 
14958458d06SMarcelo Araujo 	yp_get_default_domain(&domain);
15058458d06SMarcelo Araujo 	if (domain == NULL)
15158458d06SMarcelo Araujo 		errx(1, "YP domain name not set");
15258458d06SMarcelo Araujo 
15358458d06SMarcelo Araujo 	while ((c = getopt(argc, argv, "xd:h:mt")) != -1)
15473407b34SGarrett Wollman 		switch (c) {
15573407b34SGarrett Wollman 		case 'x':
15607c1d44fSMarcelo Araujo 			for (i = 0; i < nitems(ypaliases); i++)
157bbdb094bSGarrett Wollman 				printf("\"%s\" is an alias for \"%s\"\n",
15873407b34SGarrett Wollman 					ypaliases[i].alias,
15973407b34SGarrett Wollman 					ypaliases[i].name);
16073407b34SGarrett Wollman 			exit(0);
16158458d06SMarcelo Araujo 		case 'h':
16258458d06SMarcelo Araujo 			host = optarg;
16358458d06SMarcelo Araujo 			break;
16473407b34SGarrett Wollman 		case 'd':
16558458d06SMarcelo Araujo 			domain = optarg;
16673407b34SGarrett Wollman 			break;
16773407b34SGarrett Wollman 		case 't':
16858458d06SMarcelo Araujo 			notrans = 1;
16973407b34SGarrett Wollman 			break;
17073407b34SGarrett Wollman 		case 'm':
17158458d06SMarcelo Araujo 			mode = 1;
17273407b34SGarrett Wollman 			break;
17373407b34SGarrett Wollman 		default:
17473407b34SGarrett Wollman 			usage();
17573407b34SGarrett Wollman 		}
17658458d06SMarcelo Araujo 	argc -= optind;
17758458d06SMarcelo Araujo 	argv += optind;
1787008c93dSPhilippe Charnier 
17973407b34SGarrett Wollman 	if (mode == 0) {
18058458d06SMarcelo Araujo 		switch (argc) {
18173407b34SGarrett Wollman 		case 0:
18258458d06SMarcelo Araujo 			memset(&sin, 0, sizeof sin);
18358458d06SMarcelo Araujo 			sin.sin_family = AF_INET;
18458458d06SMarcelo Araujo 			sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
18573407b34SGarrett Wollman 
18658458d06SMarcelo Araujo 			if (bind_host(domain, &sin))
18758458d06SMarcelo Araujo 				exit(1);
18873407b34SGarrett Wollman 			break;
18973407b34SGarrett Wollman 		case 1:
19058458d06SMarcelo Araujo 			bzero(&sin, sizeof sin);
19158458d06SMarcelo Araujo 			sin.sin_family = AF_INET;
19258458d06SMarcelo Araujo 			if (inet_aton(argv[0], &sin.sin_addr) == 0) {
19358458d06SMarcelo Araujo 				hent = gethostbyname(argv[0]);
19458458d06SMarcelo Araujo 				if (!hent) {
19558458d06SMarcelo Araujo 					errx(1, "host %s unknown",
19658458d06SMarcelo Araujo 					    argv[0]);
19773407b34SGarrett Wollman 				}
19858458d06SMarcelo Araujo 			}
19958458d06SMarcelo Araujo 			if (bind_host(domain, &sin))
20058458d06SMarcelo Araujo 				exit(1);
20173407b34SGarrett Wollman 			break;
20273407b34SGarrett Wollman 		default:
20373407b34SGarrett Wollman 			usage();
20473407b34SGarrett Wollman 		}
20573407b34SGarrett Wollman 		exit(0);
20673407b34SGarrett Wollman 	}
20773407b34SGarrett Wollman 
20858458d06SMarcelo Araujo 	if (argc > 1)
20973407b34SGarrett Wollman 		usage();
21073407b34SGarrett Wollman 
21158458d06SMarcelo Araujo 	if (host != NULL)
21258458d06SMarcelo Araujo 		client = yp_bind_host(host, YPPROG, YPVERS, 0, 1);
21358458d06SMarcelo Araujo 
21458458d06SMarcelo Araujo 	if (argv[0]) {
21558458d06SMarcelo Araujo 		map = argv[0];
21607c1d44fSMarcelo Araujo 		if (notrans == 0) {
21707c1d44fSMarcelo Araujo 			for (i = 0; i < nitems(ypaliases); i++)
21873407b34SGarrett Wollman 				if (strcmp(map, ypaliases[i].alias) == 0)
21973407b34SGarrett Wollman 					map = ypaliases[i].name;
22007c1d44fSMarcelo Araujo 		}
22158458d06SMarcelo Araujo 
22258458d06SMarcelo Araujo 		if (host != NULL)
22358458d06SMarcelo Araujo 			r = yp_master_host(client, domain, map, &master);
22458458d06SMarcelo Araujo 		else
22558458d06SMarcelo Araujo 			r = yp_master(domain, map, &master);
22658458d06SMarcelo Araujo 
22773407b34SGarrett Wollman 		switch (r) {
22873407b34SGarrett Wollman 		case 0:
22973407b34SGarrett Wollman 			printf("%s\n", master);
23073407b34SGarrett Wollman 			free(master);
23173407b34SGarrett Wollman 			break;
23273407b34SGarrett Wollman 		case YPERR_YPBIND:
23358458d06SMarcelo Araujo 			errx(1, "not running ypbind");
23473407b34SGarrett Wollman 		default:
23558458d06SMarcelo Araujo 			errx(1, "can't find master for map %s: reason: %s",
23673407b34SGarrett Wollman 			    map, yperr_string(r));
23773407b34SGarrett Wollman 		}
23873407b34SGarrett Wollman 		exit(0);
23973407b34SGarrett Wollman 	}
24073407b34SGarrett Wollman 
24173407b34SGarrett Wollman 	ypml = NULL;
24258458d06SMarcelo Araujo 	if (host != NULL)
24358458d06SMarcelo Araujo 		r = yp_maplist_host(client, domain, &ypml);
24458458d06SMarcelo Araujo 	else
24558458d06SMarcelo Araujo 		r = yp_maplist(domain, &ypml);
24658458d06SMarcelo Araujo 
24758458d06SMarcelo Araujo 	r = 0;
24873407b34SGarrett Wollman 	switch (r) {
24973407b34SGarrett Wollman 	case 0:
25073407b34SGarrett Wollman 		for (y = ypml; y; ) {
25173407b34SGarrett Wollman 			ypml = y;
25258458d06SMarcelo Araujo 			if (host != NULL) {
25358458d06SMarcelo Araujo 				r = yp_master_host(client,
25458458d06SMarcelo Araujo 						   domain, ypml->map, &master);
25558458d06SMarcelo Araujo 			} else {
25658458d06SMarcelo Araujo 				r = yp_master(domain, ypml->map, &master);
25758458d06SMarcelo Araujo 			}
25873407b34SGarrett Wollman 			switch (r) {
25973407b34SGarrett Wollman 			case 0:
26058458d06SMarcelo Araujo 				printf("%s %s\n", ypml->map, master);
26173407b34SGarrett Wollman 				free(master);
26273407b34SGarrett Wollman 				break;
26373407b34SGarrett Wollman 			default:
2647008c93dSPhilippe Charnier 				warnx("can't find the master of %s: reason: %s",
26558458d06SMarcelo Araujo 				    ypml->map, yperr_string(r));
26673407b34SGarrett Wollman 				break;
26773407b34SGarrett Wollman 			}
26858458d06SMarcelo Araujo 			y = ypml->next;
26973407b34SGarrett Wollman 			free(ypml);
27073407b34SGarrett Wollman 		}
27173407b34SGarrett Wollman 		break;
27273407b34SGarrett Wollman 	case YPERR_YPBIND:
27358458d06SMarcelo Araujo 		errx(1, "not running ypbind");
27473407b34SGarrett Wollman 	default:
27558458d06SMarcelo Araujo 		errx(1, "can't get map list for domain %s: reason: %s",
27658458d06SMarcelo Araujo 		    domain, yperr_string(r));
27773407b34SGarrett Wollman 	}
27873407b34SGarrett Wollman 	exit(0);
27973407b34SGarrett Wollman }
280