xref: /illumos-gate/usr/src/cmd/krb5/kadmin/kdcmgr/klookup.c (revision ff67a31b6b184e832f89a53763c02c35bd1a7291)
11fceb383Ssemery /*
21fceb383Ssemery  * CDDL HEADER START
31fceb383Ssemery  *
41fceb383Ssemery  * The contents of this file are subject to the terms of the
51fceb383Ssemery  * Common Development and Distribution License (the "License").
61fceb383Ssemery  * You may not use this file except in compliance with the License.
71fceb383Ssemery  *
81fceb383Ssemery  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
91fceb383Ssemery  * or http://www.opensolaris.org/os/licensing.
101fceb383Ssemery  * See the License for the specific language governing permissions
111fceb383Ssemery  * and limitations under the License.
121fceb383Ssemery  *
131fceb383Ssemery  * When distributing Covered Code, include this CDDL HEADER in each
141fceb383Ssemery  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
151fceb383Ssemery  * If applicable, add the following below this CDDL HEADER, with the
161fceb383Ssemery  * fields enclosed by brackets "[]" replaced with your own identifying
171fceb383Ssemery  * information: Portions Copyright [yyyy] [name of copyright owner]
181fceb383Ssemery  *
191fceb383Ssemery  * CDDL HEADER END
201fceb383Ssemery  */
211fceb383Ssemery 
221fceb383Ssemery /*
23bd211b85Ssemery  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
241fceb383Ssemery  * Use is subject to license terms.
251fceb383Ssemery  */
269c62f9ecSMarcel Telka /*
279c62f9ecSMarcel Telka  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
289c62f9ecSMarcel Telka  */
291fceb383Ssemery 
301fceb383Ssemery #include <sys/types.h>
311fceb383Ssemery #include <netinet/in.h>
321fceb383Ssemery #include <arpa/nameser.h>
331fceb383Ssemery #include <resolv.h>
341fceb383Ssemery #include <netdb.h>
351fceb383Ssemery #include <limits.h>
361fceb383Ssemery #include <stdlib.h>
371fceb383Ssemery #include <string.h>
381fceb383Ssemery #include <unistd.h>
391fceb383Ssemery #include <ctype.h>
401fceb383Ssemery 
41bd211b85Ssemery /*
42bd211b85Ssemery  * Private resolver of target and type with arguments:
43bd211b85Ssemery  * klooukp [ target [ RR_type ] ]
44bd211b85Ssemery  *
45bd211b85Ssemery  * Utitilizes DNS lookups to discover domain and realm information.  This CLI
46bd211b85Ssemery  * is used primarily by kdcmgr(1M) and kclient(1M).
47bd211b85Ssemery  */
48bd211b85Ssemery 
491fceb383Ssemery int
501fceb383Ssemery /* ARGSUSED */
511fceb383Ssemery main(int argc, char **argv)
521fceb383Ssemery {
53bd211b85Ssemery 	unsigned char answer[NS_MAXMSG], *ansp = NULL, *end, a, b, c, d;
541fceb383Ssemery 	int len = 0, anslen, hostlen, nq, na, type, class;
552f8bbd9dSToomas Soome 	int ttl __unused, priority __unused, weight __unused, port, size;
56bd211b85Ssemery 	char name[NS_MAXDNAME], *cp, *typestr = NULL;
57bd211b85Ssemery 	char nbuf[INET6_ADDRSTRLEN];
581fceb383Ssemery 	struct __res_state stat;
591fceb383Ssemery 	int found = 0;
60bd211b85Ssemery 	int rr_type = T_A;
611fceb383Ssemery 	HEADER *h;
621fceb383Ssemery 
63*ff67a31bSToomas Soome 	type = T_A;
64*ff67a31bSToomas Soome 
65bd211b85Ssemery 	if (argc > 3)
661fceb383Ssemery 		exit(1);
671fceb383Ssemery 
68bd211b85Ssemery 	if (argc == 1) {
69bd211b85Ssemery 		if (gethostname(name, MAXHOSTNAMELEN) != 0)
701fceb383Ssemery 			exit(1);
71bd211b85Ssemery 	} else {
72bd211b85Ssemery 		(void) strncpy(name, (char *)argv[1], NS_MAXDNAME);
73bd211b85Ssemery 		if (argc == 3) {
74bd211b85Ssemery 			typestr = argv[2];
75bd211b85Ssemery 
76bd211b85Ssemery 			switch (*typestr) {
77bd211b85Ssemery 			case 'A':
78bd211b85Ssemery 				rr_type = T_A;
79bd211b85Ssemery 				break;
80bd211b85Ssemery 			case 'C':
81bd211b85Ssemery 				rr_type = T_CNAME;
82bd211b85Ssemery 				break;
83bd211b85Ssemery 			case 'I':
84bd211b85Ssemery 				rr_type = T_A;
85bd211b85Ssemery 				break;
86bd211b85Ssemery 			case 'P':
87bd211b85Ssemery 				rr_type = T_PTR;
889c62f9ecSMarcel Telka 				(void) sscanf(name, "%hhd.%hhd.%hhd.%hhd",
89bd211b85Ssemery 				    &a, &b, &c, &d);
90bd211b85Ssemery 				(void) sprintf(name, "%d.%d.%d.%d.in-addr.arpa",
91bd211b85Ssemery 				    d, c, b, a);
92bd211b85Ssemery 				break;
93bd211b85Ssemery 			case 'S':
94bd211b85Ssemery 				rr_type = T_SRV;
95bd211b85Ssemery 				break;
96bd211b85Ssemery 			default:
97bd211b85Ssemery 				exit(1);
98bd211b85Ssemery 			}
99bd211b85Ssemery 		}
100bd211b85Ssemery 	}
1011fceb383Ssemery 
1021fceb383Ssemery 	(void) memset(&stat, 0, sizeof (stat));
1031fceb383Ssemery 
1041fceb383Ssemery 	if (res_ninit(&stat) == -1)
1051fceb383Ssemery 		exit(1);
1061fceb383Ssemery 
1071fceb383Ssemery 	anslen = sizeof (answer);
108bd211b85Ssemery 	len = res_nsearch(&stat, name, C_IN, rr_type, answer, anslen);
1091fceb383Ssemery 
110bd211b85Ssemery 	if (len < sizeof (HEADER)) {
111bd211b85Ssemery 		res_ndestroy(&stat);
1121fceb383Ssemery 		exit(1);
113bd211b85Ssemery 	}
1141fceb383Ssemery 
1151fceb383Ssemery 	ansp = answer;
1161fceb383Ssemery 	end = ansp + anslen;
1171fceb383Ssemery 
1181fceb383Ssemery 	/* LINTED */
1191fceb383Ssemery 	h = (HEADER *)answer;
1201fceb383Ssemery 	nq = ntohs(h->qdcount);
1211fceb383Ssemery 	na = ntohs(h->ancount);
1221fceb383Ssemery 	ansp += HFIXEDSZ;
1231fceb383Ssemery 
124bd211b85Ssemery 	if (nq != 1 || na < 1) {
125bd211b85Ssemery 		res_ndestroy(&stat);
1261fceb383Ssemery 		exit(1);
127bd211b85Ssemery 	}
1281fceb383Ssemery 
129bd211b85Ssemery 	hostlen = sizeof (name);
130bd211b85Ssemery 	len = dn_expand(answer, end, ansp, name, hostlen);
131bd211b85Ssemery 	if (len < 0) {
132bd211b85Ssemery 		res_ndestroy(&stat);
1331fceb383Ssemery 		exit(1);
134bd211b85Ssemery 	}
1351fceb383Ssemery 
1361fceb383Ssemery 	ansp += len + QFIXEDSZ;
1371fceb383Ssemery 
138bd211b85Ssemery 	if (ansp > end) {
139bd211b85Ssemery 		res_ndestroy(&stat);
1401fceb383Ssemery 		exit(1);
141bd211b85Ssemery 	}
1421fceb383Ssemery 
1431fceb383Ssemery 	while (na-- > 0 && ansp < end) {
144bd211b85Ssemery 
145bd211b85Ssemery 		len = dn_expand(answer, end, ansp, name, hostlen);
1461fceb383Ssemery 
1471fceb383Ssemery 		if (len < 0)
1481fceb383Ssemery 			continue;
149bd211b85Ssemery 		ansp += len;			/* name */
150bd211b85Ssemery 		NS_GET16(type, ansp);		/* type */
151bd211b85Ssemery 		NS_GET16(class, ansp);		/* class */
152bd211b85Ssemery 		NS_GET32(ttl, ansp);		/* ttl */
153bd211b85Ssemery 		NS_GET16(size, ansp);		/* size */
154bd211b85Ssemery 
155bd211b85Ssemery 		if ((ansp + size) > end) {
156bd211b85Ssemery 			res_ndestroy(&stat);
157bd211b85Ssemery 			exit(1);
158bd211b85Ssemery 		}
159bd211b85Ssemery 		if (type == T_SRV) {
160bd211b85Ssemery 			NS_GET16(priority, ansp);
161bd211b85Ssemery 			NS_GET16(weight, ansp);
162bd211b85Ssemery 			NS_GET16(port, ansp);
163bd211b85Ssemery 			len = dn_expand(answer, end, ansp, name, hostlen);
164bd211b85Ssemery 			if (len < 0) {
165bd211b85Ssemery 				res_ndestroy(&stat);
166bd211b85Ssemery 				exit(1);
167bd211b85Ssemery 			}
168bd211b85Ssemery 			for (cp = name; *cp; cp++) {
169bd211b85Ssemery 				*cp = tolower(*cp);
170bd211b85Ssemery 			}
171bd211b85Ssemery 			(void) printf("%s %d\n", name, port);
172bd211b85Ssemery 		} else if (typestr && *typestr == 'I') {
173bd211b85Ssemery 			(void) inet_ntop(AF_INET, (void *)ansp, nbuf,
174bd211b85Ssemery 			    INET6_ADDRSTRLEN);
175ae5b046dSsemery 			len = size;
176ae5b046dSsemery 			(void) printf("%s\n", nbuf);
177bd211b85Ssemery 		} else if (type == T_PTR) {
178bd211b85Ssemery 			len = dn_expand(answer, end, ansp, name, hostlen);
179bd211b85Ssemery 			if (len < 0) {
180bd211b85Ssemery 				res_ndestroy(&stat);
181bd211b85Ssemery 				exit(1);
182bd211b85Ssemery 			}
183bd211b85Ssemery 		}
1841fceb383Ssemery 		ansp += len;
185bd211b85Ssemery 		if (type == rr_type && class == C_IN) {
1861fceb383Ssemery 			found = 1;
187ae5b046dSsemery 			if (type != T_SRV && !(typestr && *typestr == 'I'))
1881fceb383Ssemery 				break;
1891fceb383Ssemery 		}
1901fceb383Ssemery 	}
1911fceb383Ssemery 
192bd211b85Ssemery 	if (found != 1) {
193bd211b85Ssemery 		res_ndestroy(&stat);
1941fceb383Ssemery 		exit(1);
195bd211b85Ssemery 	}
1961fceb383Ssemery 
197bd211b85Ssemery 	for (cp = name; *cp; cp++) {
1981fceb383Ssemery 		*cp = tolower(*cp);
1991fceb383Ssemery 	}
2001fceb383Ssemery 
201ae5b046dSsemery 	if (type != T_SRV && !(typestr && *typestr == 'I'))
202bd211b85Ssemery 		(void) printf("%s\n", name);
203bd211b85Ssemery 
204bd211b85Ssemery 	res_ndestroy(&stat);
2051fceb383Ssemery 
2061fceb383Ssemery 	return (0);
2071fceb383Ssemery }
208