xref: /illumos-gate/usr/src/cmd/krb5/kadmin/kdcmgr/klookup.c (revision ae5b046d8f8cec187d40041c4b74b43f561d5ac7)
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  */
261fceb383Ssemery 
271fceb383Ssemery #pragma ident	"%Z%%M%	%I%	%E% SMI"
281fceb383Ssemery 
291fceb383Ssemery #include <sys/types.h>
301fceb383Ssemery #include <netinet/in.h>
311fceb383Ssemery #include <arpa/nameser.h>
321fceb383Ssemery #include <resolv.h>
331fceb383Ssemery #include <netdb.h>
341fceb383Ssemery #include <limits.h>
351fceb383Ssemery #include <stdlib.h>
361fceb383Ssemery #include <string.h>
371fceb383Ssemery #include <unistd.h>
381fceb383Ssemery #include <ctype.h>
391fceb383Ssemery 
40bd211b85Ssemery /*
41bd211b85Ssemery  * Private resolver of target and type with arguments:
42bd211b85Ssemery  * klooukp [ target [ RR_type ] ]
43bd211b85Ssemery  *
44bd211b85Ssemery  * Utitilizes DNS lookups to discover domain and realm information.  This CLI
45bd211b85Ssemery  * is used primarily by kdcmgr(1M) and kclient(1M).
46bd211b85Ssemery  */
47bd211b85Ssemery 
481fceb383Ssemery int
491fceb383Ssemery /* ARGSUSED */
501fceb383Ssemery main(int argc, char **argv)
511fceb383Ssemery {
52bd211b85Ssemery 	unsigned char answer[NS_MAXMSG], *ansp = NULL, *end, a, b, c, d;
531fceb383Ssemery 	int len = 0, anslen, hostlen, nq, na, type, class;
54bd211b85Ssemery 	int ttl, priority, weight, port, size;
55bd211b85Ssemery 	char name[NS_MAXDNAME], *cp, *typestr = NULL;
56bd211b85Ssemery 	char nbuf[INET6_ADDRSTRLEN];
571fceb383Ssemery 	struct __res_state stat;
581fceb383Ssemery 	int found = 0;
59bd211b85Ssemery 	int rr_type = T_A;
601fceb383Ssemery 	HEADER *h;
611fceb383Ssemery 
62bd211b85Ssemery 	if (argc > 3)
631fceb383Ssemery 		exit(1);
641fceb383Ssemery 
65bd211b85Ssemery 	if (argc == 1) {
66bd211b85Ssemery 		if (gethostname(name, MAXHOSTNAMELEN) != 0)
671fceb383Ssemery 			exit(1);
68bd211b85Ssemery 	} else {
69bd211b85Ssemery 		(void) strncpy(name, (char *)argv[1], NS_MAXDNAME);
70bd211b85Ssemery 		if (argc == 3) {
71bd211b85Ssemery 			typestr = argv[2];
72bd211b85Ssemery 
73bd211b85Ssemery 			switch (*typestr) {
74bd211b85Ssemery 			case 'A':
75bd211b85Ssemery 				rr_type = T_A;
76bd211b85Ssemery 				break;
77bd211b85Ssemery 			case 'C':
78bd211b85Ssemery 				rr_type = T_CNAME;
79bd211b85Ssemery 				break;
80bd211b85Ssemery 			case 'I':
81bd211b85Ssemery 				rr_type = T_A;
82bd211b85Ssemery 				break;
83bd211b85Ssemery 			case 'P':
84bd211b85Ssemery 				rr_type = T_PTR;
85bd211b85Ssemery 				(void) sscanf(name, "%d.%d.%d.%d",
86bd211b85Ssemery 				    &a, &b, &c, &d);
87bd211b85Ssemery 				(void) sprintf(name, "%d.%d.%d.%d.in-addr.arpa",
88bd211b85Ssemery 				    d, c, b, a);
89bd211b85Ssemery 				break;
90bd211b85Ssemery 			case 'S':
91bd211b85Ssemery 				rr_type = T_SRV;
92bd211b85Ssemery 				break;
93bd211b85Ssemery 			default:
94bd211b85Ssemery 				exit(1);
95bd211b85Ssemery 			}
96bd211b85Ssemery 		}
97bd211b85Ssemery 	}
981fceb383Ssemery 
991fceb383Ssemery 	(void) memset(&stat, 0, sizeof (stat));
1001fceb383Ssemery 
1011fceb383Ssemery 	if (res_ninit(&stat) == -1)
1021fceb383Ssemery 		exit(1);
1031fceb383Ssemery 
1041fceb383Ssemery 	anslen = sizeof (answer);
105bd211b85Ssemery 	len = res_nsearch(&stat, name, C_IN, rr_type, answer, anslen);
1061fceb383Ssemery 
107bd211b85Ssemery 	if (len < sizeof (HEADER)) {
108bd211b85Ssemery 		res_ndestroy(&stat);
1091fceb383Ssemery 		exit(1);
110bd211b85Ssemery 	}
1111fceb383Ssemery 
1121fceb383Ssemery 	ansp = answer;
1131fceb383Ssemery 	end = ansp + anslen;
1141fceb383Ssemery 
1151fceb383Ssemery 	/* LINTED */
1161fceb383Ssemery 	h = (HEADER *)answer;
1171fceb383Ssemery 	nq = ntohs(h->qdcount);
1181fceb383Ssemery 	na = ntohs(h->ancount);
1191fceb383Ssemery 	ansp += HFIXEDSZ;
1201fceb383Ssemery 
121bd211b85Ssemery 	if (nq != 1 || na < 1) {
122bd211b85Ssemery 		res_ndestroy(&stat);
1231fceb383Ssemery 		exit(1);
124bd211b85Ssemery 	}
1251fceb383Ssemery 
126bd211b85Ssemery 	hostlen = sizeof (name);
127bd211b85Ssemery 	len = dn_expand(answer, end, ansp, name, hostlen);
128bd211b85Ssemery 	if (len < 0) {
129bd211b85Ssemery 		res_ndestroy(&stat);
1301fceb383Ssemery 		exit(1);
131bd211b85Ssemery 	}
1321fceb383Ssemery 
1331fceb383Ssemery 	ansp += len + QFIXEDSZ;
1341fceb383Ssemery 
135bd211b85Ssemery 	if (ansp > end) {
136bd211b85Ssemery 		res_ndestroy(&stat);
1371fceb383Ssemery 		exit(1);
138bd211b85Ssemery 	}
1391fceb383Ssemery 
1401fceb383Ssemery 	while (na-- > 0 && ansp < end) {
141bd211b85Ssemery 
142bd211b85Ssemery 		len = dn_expand(answer, end, ansp, name, hostlen);
1431fceb383Ssemery 
1441fceb383Ssemery 		if (len < 0)
1451fceb383Ssemery 			continue;
146bd211b85Ssemery 		ansp += len;			/* name */
147bd211b85Ssemery 		NS_GET16(type, ansp);		/* type */
148bd211b85Ssemery 		NS_GET16(class, ansp);		/* class */
149bd211b85Ssemery 		NS_GET32(ttl, ansp);		/* ttl */
150bd211b85Ssemery 		NS_GET16(size, ansp);		/* size */
151bd211b85Ssemery 
152bd211b85Ssemery 		if ((ansp + size) > end) {
153bd211b85Ssemery 			res_ndestroy(&stat);
154bd211b85Ssemery 			exit(1);
155bd211b85Ssemery 		}
156bd211b85Ssemery 		if (type == T_SRV) {
157bd211b85Ssemery 			NS_GET16(priority, ansp);
158bd211b85Ssemery 			NS_GET16(weight, ansp);
159bd211b85Ssemery 			NS_GET16(port, ansp);
160bd211b85Ssemery 			len = dn_expand(answer, end, ansp, name, hostlen);
161bd211b85Ssemery 			if (len < 0) {
162bd211b85Ssemery 				res_ndestroy(&stat);
163bd211b85Ssemery 				exit(1);
164bd211b85Ssemery 			}
165bd211b85Ssemery 			for (cp = name; *cp; cp++) {
166bd211b85Ssemery 				*cp = tolower(*cp);
167bd211b85Ssemery 			}
168bd211b85Ssemery 			(void) printf("%s %d\n", name, port);
169bd211b85Ssemery 		} else if (typestr && *typestr == 'I') {
170bd211b85Ssemery 			(void) inet_ntop(AF_INET, (void *)ansp, nbuf,
171bd211b85Ssemery 			    INET6_ADDRSTRLEN);
172*ae5b046dSsemery 			len = size;
173*ae5b046dSsemery 			(void) printf("%s\n", nbuf);
174bd211b85Ssemery 		} else if (type == T_PTR) {
175bd211b85Ssemery 			len = dn_expand(answer, end, ansp, name, hostlen);
176bd211b85Ssemery 			if (len < 0) {
177bd211b85Ssemery 				res_ndestroy(&stat);
178bd211b85Ssemery 				exit(1);
179bd211b85Ssemery 			}
180bd211b85Ssemery 		}
1811fceb383Ssemery 		ansp += len;
182bd211b85Ssemery 		if (type == rr_type && class == C_IN) {
1831fceb383Ssemery 			found = 1;
184*ae5b046dSsemery 			if (type != T_SRV && !(typestr && *typestr == 'I'))
1851fceb383Ssemery 				break;
1861fceb383Ssemery 		}
1871fceb383Ssemery 	}
1881fceb383Ssemery 
189bd211b85Ssemery 	if (found != 1) {
190bd211b85Ssemery 		res_ndestroy(&stat);
1911fceb383Ssemery 		exit(1);
192bd211b85Ssemery 	}
1931fceb383Ssemery 
194bd211b85Ssemery 	for (cp = name; *cp; cp++) {
1951fceb383Ssemery 		*cp = tolower(*cp);
1961fceb383Ssemery 	}
1971fceb383Ssemery 
198*ae5b046dSsemery 	if (type != T_SRV && !(typestr && *typestr == 'I'))
199bd211b85Ssemery 		(void) printf("%s\n", name);
200bd211b85Ssemery 
201bd211b85Ssemery 	res_ndestroy(&stat);
2021fceb383Ssemery 
2031fceb383Ssemery 	return (0);
2041fceb383Ssemery }
205