xref: /freebsd/usr.bin/ypwhich/ypwhich.c (revision 5e3934b1)
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 /*-
5b61a5730SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
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 
3273407b34SGarrett Wollman #include <sys/param.h>
3373407b34SGarrett Wollman #include <sys/types.h>
3473407b34SGarrett Wollman #include <sys/socket.h>
35082d8262SMark Murray 
3628faa530SPeter Wemm #include <netinet/in.h>
3728faa530SPeter Wemm #include <arpa/inet.h>
38082d8262SMark Murray 
39821df508SXin LI #include <ctype.h>
407008c93dSPhilippe Charnier #include <err.h>
4173407b34SGarrett Wollman #include <netdb.h>
427008c93dSPhilippe Charnier #include <stdio.h>
437008c93dSPhilippe Charnier #include <stdlib.h>
447008c93dSPhilippe Charnier #include <string.h>
457008c93dSPhilippe Charnier #include <unistd.h>
4673407b34SGarrett Wollman 
4758458d06SMarcelo Araujo #include <rpc/rpc.h>
4858458d06SMarcelo Araujo #include <rpc/xdr.h>
4958458d06SMarcelo Araujo #include <rpcsvc/yp.h>
5058458d06SMarcelo Araujo #include <rpcsvc/ypclnt.h>
518a5bd1a2SBill Paul 
5258458d06SMarcelo Araujo #include "yplib_host.h"
5373407b34SGarrett Wollman 
54006a388bSMarcelo Araujo static const struct ypalias {
55cd9df728SPeter Wemm 	char *alias, *name;
56006a388bSMarcelo Araujo } ypaliases[] = {
5773407b34SGarrett Wollman 	{ "passwd", "passwd.byname" },
58d228e65cSPeter Wemm 	{ "master.passwd", "master.passwd.byname" },
5953c40578SBrian Somers 	{ "shadow", "shadow.byname" },
6073407b34SGarrett Wollman 	{ "group", "group.byname" },
6173407b34SGarrett Wollman 	{ "networks", "networks.byaddr" },
6273407b34SGarrett Wollman 	{ "hosts", "hosts.byaddr" },
6373407b34SGarrett Wollman 	{ "protocols", "protocols.bynumber" },
6473407b34SGarrett Wollman 	{ "services", "services.byname" },
6573407b34SGarrett Wollman 	{ "aliases", "mail.aliases" },
6673407b34SGarrett Wollman 	{ "ethers", "ethers.byname" },
6773407b34SGarrett Wollman };
6873407b34SGarrett Wollman 
697008c93dSPhilippe Charnier static void
usage(void)70082d8262SMark Murray usage(void)
7173407b34SGarrett Wollman {
7258458d06SMarcelo Araujo 	fprintf(stderr,
7358458d06SMarcelo Araujo 	    "usage: ypwhich [-t] [-d domain] [[-h] host]\n"
7458458d06SMarcelo Araujo 	    "       ypwhich [-t] [-d domain] [-h host] -m [mname]\n"
7558458d06SMarcelo Araujo 	    "       ypwhich -x\n");
7658458d06SMarcelo Araujo 	exit(1);
7773407b34SGarrett Wollman }
7873407b34SGarrett Wollman 
7973407b34SGarrett Wollman 
8073407b34SGarrett Wollman /*
8173407b34SGarrett Wollman  * Like yp_bind except can query a specific host
8273407b34SGarrett Wollman  */
83082d8262SMark Murray static int
bind_host(char * dom,struct sockaddr_in * sin)8458458d06SMarcelo Araujo bind_host(char *dom, struct sockaddr_in *sin)
8573407b34SGarrett Wollman {
8673407b34SGarrett Wollman 	struct hostent *hent = NULL;
8773407b34SGarrett Wollman 	struct ypbind_resp ypbr;
8858458d06SMarcelo Araujo 	struct in_addr ss_addr;
8973407b34SGarrett Wollman 	struct timeval tv;
9073407b34SGarrett Wollman 	CLIENT *client;
9173407b34SGarrett Wollman 	int sock, r;
9273407b34SGarrett Wollman 
9373407b34SGarrett Wollman 	sock = RPC_ANYSOCK;
9473407b34SGarrett Wollman 	tv.tv_sec = 15;
9573407b34SGarrett Wollman 	tv.tv_usec = 0;
9658458d06SMarcelo Araujo 	client = clntudp_create(sin, YPBINDPROG, YPBINDVERS, tv, &sock);
9758458d06SMarcelo Araujo 
9873407b34SGarrett Wollman 	if (client == NULL) {
9958458d06SMarcelo Araujo 		warnx("host is not bound to a ypmaster");
100ed4d1c46SDag-Erling Smørgrav 		return (YPERR_YPBIND);
10173407b34SGarrett Wollman 	}
10273407b34SGarrett Wollman 
10373407b34SGarrett Wollman 	tv.tv_sec = 5;
10473407b34SGarrett Wollman 	tv.tv_usec = 0;
10558458d06SMarcelo Araujo 
10673407b34SGarrett Wollman 	r = clnt_call(client, YPBINDPROC_DOMAIN,
107cd9df728SPeter Wemm 		(xdrproc_t)xdr_domainname, &dom,
108cd9df728SPeter Wemm 		(xdrproc_t)xdr_ypbind_resp, &ypbr, tv);
10973407b34SGarrett Wollman 	if (r != RPC_SUCCESS) {
1107008c93dSPhilippe Charnier 		warnx("can't clnt_call: %s", yperr_string(YPERR_YPBIND));
11173407b34SGarrett Wollman 		clnt_destroy(client);
112ed4d1c46SDag-Erling Smørgrav 		return (YPERR_YPBIND);
11373407b34SGarrett Wollman 	} else {
11473407b34SGarrett Wollman 		if (ypbr.ypbind_status != YPBIND_SUCC_VAL) {
1157008c93dSPhilippe Charnier 			warnx("can't yp_bind: reason: %s",
11658458d06SMarcelo Araujo 			    yperr_string(ypbr.ypbind_status));
11773407b34SGarrett Wollman 			clnt_destroy(client);
118ed4d1c46SDag-Erling Smørgrav 			return (r);
11973407b34SGarrett Wollman 		}
12073407b34SGarrett Wollman 	}
12173407b34SGarrett Wollman 	clnt_destroy(client);
12273407b34SGarrett Wollman 
12358458d06SMarcelo Araujo 	memmove(&ss_addr.s_addr, &ypbr.ypbind_resp_u.ypbind_bindinfo.ypbind_binding_addr,
12458458d06SMarcelo Araujo 	    sizeof (ss_addr));
12558458d06SMarcelo Araujo 
12658458d06SMarcelo Araujo 	hent = gethostbyaddr((char *)&ss_addr.s_addr, sizeof(ss_addr.s_addr),
12758458d06SMarcelo Araujo 	    AF_INET);
12858458d06SMarcelo Araujo 	if (hent != NULL)
12973407b34SGarrett Wollman 		printf("%s\n", hent->h_name);
13073407b34SGarrett Wollman 	else
13173407b34SGarrett Wollman 		printf("%s\n", inet_ntoa(ss_addr));
13258458d06SMarcelo Araujo 
133ed4d1c46SDag-Erling Smørgrav 	return (0);
13473407b34SGarrett Wollman }
13573407b34SGarrett Wollman 
13673407b34SGarrett Wollman int
main(int argc,char * argv[])137082d8262SMark Murray main(int argc, char *argv[])
13873407b34SGarrett Wollman {
13958458d06SMarcelo Araujo 	char *domain, *master, *map = NULL, *host = NULL;
14058458d06SMarcelo Araujo 	int notrans = 0, mode = 0, c, r, i;
14173407b34SGarrett Wollman 	struct ypmaplist *ypml, *y;
14258458d06SMarcelo Araujo 	struct sockaddr_in sin;
14373407b34SGarrett Wollman 	struct hostent *hent;
14458458d06SMarcelo Araujo 	CLIENT *client = NULL;
14573407b34SGarrett Wollman 
14658458d06SMarcelo Araujo 	yp_get_default_domain(&domain);
14758458d06SMarcelo Araujo 	if (domain == NULL)
14858458d06SMarcelo Araujo 		errx(1, "YP domain name not set");
14958458d06SMarcelo Araujo 
15058458d06SMarcelo Araujo 	while ((c = getopt(argc, argv, "xd:h:mt")) != -1)
15173407b34SGarrett Wollman 		switch (c) {
15273407b34SGarrett Wollman 		case 'x':
15307c1d44fSMarcelo Araujo 			for (i = 0; i < nitems(ypaliases); i++)
154bbdb094bSGarrett Wollman 				printf("\"%s\" is an alias for \"%s\"\n",
15573407b34SGarrett Wollman 					ypaliases[i].alias,
15673407b34SGarrett Wollman 					ypaliases[i].name);
15773407b34SGarrett Wollman 			exit(0);
15858458d06SMarcelo Araujo 		case 'h':
15958458d06SMarcelo Araujo 			host = optarg;
16058458d06SMarcelo Araujo 			break;
16173407b34SGarrett Wollman 		case 'd':
16258458d06SMarcelo Araujo 			domain = optarg;
16373407b34SGarrett Wollman 			break;
16473407b34SGarrett Wollman 		case 't':
16558458d06SMarcelo Araujo 			notrans = 1;
16673407b34SGarrett Wollman 			break;
16773407b34SGarrett Wollman 		case 'm':
16858458d06SMarcelo Araujo 			mode = 1;
16973407b34SGarrett Wollman 			break;
17073407b34SGarrett Wollman 		default:
17173407b34SGarrett Wollman 			usage();
17273407b34SGarrett Wollman 		}
17358458d06SMarcelo Araujo 	argc -= optind;
17458458d06SMarcelo Araujo 	argv += optind;
1757008c93dSPhilippe Charnier 
17673407b34SGarrett Wollman 	if (mode == 0) {
17758458d06SMarcelo Araujo 		switch (argc) {
17873407b34SGarrett Wollman 		case 0:
17958458d06SMarcelo Araujo 			memset(&sin, 0, sizeof sin);
18058458d06SMarcelo Araujo 			sin.sin_family = AF_INET;
18158458d06SMarcelo Araujo 			sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
18273407b34SGarrett Wollman 
18358458d06SMarcelo Araujo 			if (bind_host(domain, &sin))
18458458d06SMarcelo Araujo 				exit(1);
18573407b34SGarrett Wollman 			break;
18673407b34SGarrett Wollman 		case 1:
18758458d06SMarcelo Araujo 			bzero(&sin, sizeof sin);
18858458d06SMarcelo Araujo 			sin.sin_family = AF_INET;
18958458d06SMarcelo Araujo 			if (inet_aton(argv[0], &sin.sin_addr) == 0) {
19058458d06SMarcelo Araujo 				hent = gethostbyname(argv[0]);
19158458d06SMarcelo Araujo 				if (!hent) {
19258458d06SMarcelo Araujo 					errx(1, "host %s unknown",
19358458d06SMarcelo Araujo 					    argv[0]);
19473407b34SGarrett Wollman 				}
19558458d06SMarcelo Araujo 			}
19658458d06SMarcelo Araujo 			if (bind_host(domain, &sin))
19758458d06SMarcelo Araujo 				exit(1);
19873407b34SGarrett Wollman 			break;
19973407b34SGarrett Wollman 		default:
20073407b34SGarrett Wollman 			usage();
20173407b34SGarrett Wollman 		}
20273407b34SGarrett Wollman 		exit(0);
20373407b34SGarrett Wollman 	}
20473407b34SGarrett Wollman 
20558458d06SMarcelo Araujo 	if (argc > 1)
20673407b34SGarrett Wollman 		usage();
20773407b34SGarrett Wollman 
20858458d06SMarcelo Araujo 	if (host != NULL)
20958458d06SMarcelo Araujo 		client = yp_bind_host(host, YPPROG, YPVERS, 0, 1);
21058458d06SMarcelo Araujo 
21158458d06SMarcelo Araujo 	if (argv[0]) {
21258458d06SMarcelo Araujo 		map = argv[0];
21307c1d44fSMarcelo Araujo 		if (notrans == 0) {
21407c1d44fSMarcelo Araujo 			for (i = 0; i < nitems(ypaliases); i++)
21573407b34SGarrett Wollman 				if (strcmp(map, ypaliases[i].alias) == 0)
21673407b34SGarrett Wollman 					map = ypaliases[i].name;
21707c1d44fSMarcelo Araujo 		}
21858458d06SMarcelo Araujo 
21958458d06SMarcelo Araujo 		if (host != NULL)
22058458d06SMarcelo Araujo 			r = yp_master_host(client, domain, map, &master);
22158458d06SMarcelo Araujo 		else
22258458d06SMarcelo Araujo 			r = yp_master(domain, map, &master);
22358458d06SMarcelo Araujo 
22473407b34SGarrett Wollman 		switch (r) {
22573407b34SGarrett Wollman 		case 0:
22673407b34SGarrett Wollman 			printf("%s\n", master);
22773407b34SGarrett Wollman 			free(master);
22873407b34SGarrett Wollman 			break;
22973407b34SGarrett Wollman 		case YPERR_YPBIND:
23058458d06SMarcelo Araujo 			errx(1, "not running ypbind");
23173407b34SGarrett Wollman 		default:
23258458d06SMarcelo Araujo 			errx(1, "can't find master for map %s: reason: %s",
23373407b34SGarrett Wollman 			    map, yperr_string(r));
23473407b34SGarrett Wollman 		}
23573407b34SGarrett Wollman 		exit(0);
23673407b34SGarrett Wollman 	}
23773407b34SGarrett Wollman 
23873407b34SGarrett Wollman 	ypml = NULL;
23958458d06SMarcelo Araujo 	if (host != NULL)
24058458d06SMarcelo Araujo 		r = yp_maplist_host(client, domain, &ypml);
24158458d06SMarcelo Araujo 	else
24258458d06SMarcelo Araujo 		r = yp_maplist(domain, &ypml);
24358458d06SMarcelo Araujo 
24458458d06SMarcelo Araujo 	r = 0;
24573407b34SGarrett Wollman 	switch (r) {
24673407b34SGarrett Wollman 	case 0:
24773407b34SGarrett Wollman 		for (y = ypml; y; ) {
24873407b34SGarrett Wollman 			ypml = y;
24958458d06SMarcelo Araujo 			if (host != NULL) {
25058458d06SMarcelo Araujo 				r = yp_master_host(client,
25158458d06SMarcelo Araujo 						   domain, ypml->map, &master);
25258458d06SMarcelo Araujo 			} else {
25358458d06SMarcelo Araujo 				r = yp_master(domain, ypml->map, &master);
25458458d06SMarcelo Araujo 			}
25573407b34SGarrett Wollman 			switch (r) {
25673407b34SGarrett Wollman 			case 0:
25758458d06SMarcelo Araujo 				printf("%s %s\n", ypml->map, master);
25873407b34SGarrett Wollman 				free(master);
25973407b34SGarrett Wollman 				break;
26073407b34SGarrett Wollman 			default:
2617008c93dSPhilippe Charnier 				warnx("can't find the master of %s: reason: %s",
26258458d06SMarcelo Araujo 				    ypml->map, yperr_string(r));
26373407b34SGarrett Wollman 				break;
26473407b34SGarrett Wollman 			}
26558458d06SMarcelo Araujo 			y = ypml->next;
26673407b34SGarrett Wollman 			free(ypml);
26773407b34SGarrett Wollman 		}
26873407b34SGarrett Wollman 		break;
26973407b34SGarrett Wollman 	case YPERR_YPBIND:
27058458d06SMarcelo Araujo 		errx(1, "not running ypbind");
27173407b34SGarrett Wollman 	default:
27258458d06SMarcelo Araujo 		errx(1, "can't get map list for domain %s: reason: %s",
27358458d06SMarcelo Araujo 		    domain, yperr_string(r));
27473407b34SGarrett Wollman 	}
27573407b34SGarrett Wollman 	exit(0);
27673407b34SGarrett Wollman }
277