xref: /illumos-gate/usr/src/cmd/krb5/kadmin/kdcmgr/klookup.c (revision bd211b8556ef6b18ebf137419bd5555d65271664)
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 /*
23*bd211b85Ssemery  * 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 
40*bd211b85Ssemery /*
41*bd211b85Ssemery  * Private resolver of target and type with arguments:
42*bd211b85Ssemery  * klooukp [ target [ RR_type ] ]
43*bd211b85Ssemery  *
44*bd211b85Ssemery  * Utitilizes DNS lookups to discover domain and realm information.  This CLI
45*bd211b85Ssemery  * is used primarily by kdcmgr(1M) and kclient(1M).
46*bd211b85Ssemery  */
47*bd211b85Ssemery 
481fceb383Ssemery int
491fceb383Ssemery /* ARGSUSED */
501fceb383Ssemery main(int argc, char **argv)
511fceb383Ssemery {
52*bd211b85Ssemery 	unsigned char answer[NS_MAXMSG], *ansp = NULL, *end, a, b, c, d;
531fceb383Ssemery 	int len = 0, anslen, hostlen, nq, na, type, class;
54*bd211b85Ssemery 	int ttl, priority, weight, port, size;
55*bd211b85Ssemery 	char name[NS_MAXDNAME], *cp, *typestr = NULL;
56*bd211b85Ssemery 	char nbuf[INET6_ADDRSTRLEN];
571fceb383Ssemery 	struct __res_state stat;
581fceb383Ssemery 	int found = 0;
59*bd211b85Ssemery 	int rr_type = T_A;
601fceb383Ssemery 	HEADER *h;
611fceb383Ssemery 
62*bd211b85Ssemery 	if (argc > 3)
631fceb383Ssemery 		exit(1);
641fceb383Ssemery 
65*bd211b85Ssemery 	if (argc == 1) {
66*bd211b85Ssemery 		if (gethostname(name, MAXHOSTNAMELEN) != 0)
671fceb383Ssemery 			exit(1);
68*bd211b85Ssemery 	} else {
69*bd211b85Ssemery 		(void) strncpy(name, (char *)argv[1], NS_MAXDNAME);
70*bd211b85Ssemery 		if (argc == 3) {
71*bd211b85Ssemery 			typestr = argv[2];
72*bd211b85Ssemery 
73*bd211b85Ssemery 			switch (*typestr) {
74*bd211b85Ssemery 			case 'A':
75*bd211b85Ssemery 				rr_type = T_A;
76*bd211b85Ssemery 				break;
77*bd211b85Ssemery 			case 'C':
78*bd211b85Ssemery 				rr_type = T_CNAME;
79*bd211b85Ssemery 				break;
80*bd211b85Ssemery 			case 'I':
81*bd211b85Ssemery 				rr_type = T_A;
82*bd211b85Ssemery 				break;
83*bd211b85Ssemery 			case 'P':
84*bd211b85Ssemery 				rr_type = T_PTR;
85*bd211b85Ssemery 				(void) sscanf(name, "%d.%d.%d.%d",
86*bd211b85Ssemery 				    &a, &b, &c, &d);
87*bd211b85Ssemery 				(void) sprintf(name, "%d.%d.%d.%d.in-addr.arpa",
88*bd211b85Ssemery 				    d, c, b, a);
89*bd211b85Ssemery 				break;
90*bd211b85Ssemery 			case 'S':
91*bd211b85Ssemery 				rr_type = T_SRV;
92*bd211b85Ssemery 				break;
93*bd211b85Ssemery 			default:
94*bd211b85Ssemery 				exit(1);
95*bd211b85Ssemery 			}
96*bd211b85Ssemery 		}
97*bd211b85Ssemery 	}
981fceb383Ssemery 
991fceb383Ssemery 	(void) memset(&stat, 0, sizeof (stat));
1001fceb383Ssemery 
1011fceb383Ssemery 	if (res_ninit(&stat) == -1)
1021fceb383Ssemery 		exit(1);
1031fceb383Ssemery 
1041fceb383Ssemery 	anslen = sizeof (answer);
105*bd211b85Ssemery 	len = res_nsearch(&stat, name, C_IN, rr_type, answer, anslen);
1061fceb383Ssemery 
107*bd211b85Ssemery 	if (len < sizeof (HEADER)) {
108*bd211b85Ssemery 		res_ndestroy(&stat);
1091fceb383Ssemery 		exit(1);
110*bd211b85Ssemery 	}
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 
121*bd211b85Ssemery 	if (nq != 1 || na < 1) {
122*bd211b85Ssemery 		res_ndestroy(&stat);
1231fceb383Ssemery 		exit(1);
124*bd211b85Ssemery 	}
1251fceb383Ssemery 
126*bd211b85Ssemery 	hostlen = sizeof (name);
127*bd211b85Ssemery 	len = dn_expand(answer, end, ansp, name, hostlen);
128*bd211b85Ssemery 	if (len < 0) {
129*bd211b85Ssemery 		res_ndestroy(&stat);
1301fceb383Ssemery 		exit(1);
131*bd211b85Ssemery 	}
1321fceb383Ssemery 
1331fceb383Ssemery 	ansp += len + QFIXEDSZ;
1341fceb383Ssemery 
135*bd211b85Ssemery 	if (ansp > end) {
136*bd211b85Ssemery 		res_ndestroy(&stat);
1371fceb383Ssemery 		exit(1);
138*bd211b85Ssemery 	}
1391fceb383Ssemery 
1401fceb383Ssemery 	while (na-- > 0 && ansp < end) {
141*bd211b85Ssemery 
142*bd211b85Ssemery 		len = dn_expand(answer, end, ansp, name, hostlen);
1431fceb383Ssemery 
1441fceb383Ssemery 		if (len < 0)
1451fceb383Ssemery 			continue;
146*bd211b85Ssemery 		ansp += len;			/* name */
147*bd211b85Ssemery 		NS_GET16(type, ansp);		/* type */
148*bd211b85Ssemery 		NS_GET16(class, ansp);		/* class */
149*bd211b85Ssemery 		NS_GET32(ttl, ansp);		/* ttl */
150*bd211b85Ssemery 		NS_GET16(size, ansp);		/* size */
151*bd211b85Ssemery 
152*bd211b85Ssemery 		if ((ansp + size) > end) {
153*bd211b85Ssemery 			res_ndestroy(&stat);
154*bd211b85Ssemery 			exit(1);
155*bd211b85Ssemery 		}
156*bd211b85Ssemery 		if (type == T_SRV) {
157*bd211b85Ssemery 			NS_GET16(priority, ansp);
158*bd211b85Ssemery 			NS_GET16(weight, ansp);
159*bd211b85Ssemery 			NS_GET16(port, ansp);
160*bd211b85Ssemery 			len = dn_expand(answer, end, ansp, name, hostlen);
161*bd211b85Ssemery 			if (len < 0) {
162*bd211b85Ssemery 				res_ndestroy(&stat);
163*bd211b85Ssemery 				exit(1);
164*bd211b85Ssemery 			}
165*bd211b85Ssemery 			for (cp = name; *cp; cp++) {
166*bd211b85Ssemery 				*cp = tolower(*cp);
167*bd211b85Ssemery 			}
168*bd211b85Ssemery 			(void) printf("%s %d\n", name, port);
169*bd211b85Ssemery 		} else if (typestr && *typestr == 'I') {
170*bd211b85Ssemery 			(void) inet_ntop(AF_INET, (void *)ansp, nbuf,
171*bd211b85Ssemery 			    INET6_ADDRSTRLEN);
172*bd211b85Ssemery 			(void) strncpy(name, nbuf, MAXHOSTNAMELEN);
173*bd211b85Ssemery 		} else if (type == T_PTR) {
174*bd211b85Ssemery 			len = dn_expand(answer, end, ansp, name, hostlen);
175*bd211b85Ssemery 			if (len < 0) {
176*bd211b85Ssemery 				res_ndestroy(&stat);
177*bd211b85Ssemery 				exit(1);
178*bd211b85Ssemery 			}
179*bd211b85Ssemery 		}
1801fceb383Ssemery 		ansp += len;
181*bd211b85Ssemery 		if (type == rr_type && class == C_IN) {
1821fceb383Ssemery 			found = 1;
183*bd211b85Ssemery 			if (type != T_SRV)
1841fceb383Ssemery 				break;
1851fceb383Ssemery 		}
1861fceb383Ssemery 	}
1871fceb383Ssemery 
188*bd211b85Ssemery 	if (found != 1) {
189*bd211b85Ssemery 		res_ndestroy(&stat);
1901fceb383Ssemery 		exit(1);
191*bd211b85Ssemery 	}
1921fceb383Ssemery 
193*bd211b85Ssemery 	for (cp = name; *cp; cp++) {
1941fceb383Ssemery 		*cp = tolower(*cp);
1951fceb383Ssemery 	}
1961fceb383Ssemery 
197*bd211b85Ssemery 	if (type != T_SRV)
198*bd211b85Ssemery 		(void) printf("%s\n", name);
199*bd211b85Ssemery 
200*bd211b85Ssemery 	res_ndestroy(&stat);
2011fceb383Ssemery 
2021fceb383Ssemery 	return (0);
2031fceb383Ssemery }
204