xref: /titanic_51/usr/src/cmd/krb5/kadmin/kdcmgr/klookup.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <netinet/in.h>
31 #include <arpa/nameser.h>
32 #include <resolv.h>
33 #include <netdb.h>
34 #include <limits.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <ctype.h>
39 
40 /*
41  * Private resolver of target and type with arguments:
42  * klooukp [ target [ RR_type ] ]
43  *
44  * Utitilizes DNS lookups to discover domain and realm information.  This CLI
45  * is used primarily by kdcmgr(1M) and kclient(1M).
46  */
47 
48 int
49 /* ARGSUSED */
50 main(int argc, char **argv)
51 {
52 	unsigned char answer[NS_MAXMSG], *ansp = NULL, *end, a, b, c, d;
53 	int len = 0, anslen, hostlen, nq, na, type, class;
54 	int ttl, priority, weight, port, size;
55 	char name[NS_MAXDNAME], *cp, *typestr = NULL;
56 	char nbuf[INET6_ADDRSTRLEN];
57 	struct __res_state stat;
58 	int found = 0;
59 	int rr_type = T_A;
60 	HEADER *h;
61 
62 	if (argc > 3)
63 		exit(1);
64 
65 	if (argc == 1) {
66 		if (gethostname(name, MAXHOSTNAMELEN) != 0)
67 			exit(1);
68 	} else {
69 		(void) strncpy(name, (char *)argv[1], NS_MAXDNAME);
70 		if (argc == 3) {
71 			typestr = argv[2];
72 
73 			switch (*typestr) {
74 			case 'A':
75 				rr_type = T_A;
76 				break;
77 			case 'C':
78 				rr_type = T_CNAME;
79 				break;
80 			case 'I':
81 				rr_type = T_A;
82 				break;
83 			case 'P':
84 				rr_type = T_PTR;
85 				(void) sscanf(name, "%d.%d.%d.%d",
86 				    &a, &b, &c, &d);
87 				(void) sprintf(name, "%d.%d.%d.%d.in-addr.arpa",
88 				    d, c, b, a);
89 				break;
90 			case 'S':
91 				rr_type = T_SRV;
92 				break;
93 			default:
94 				exit(1);
95 			}
96 		}
97 	}
98 
99 	(void) memset(&stat, 0, sizeof (stat));
100 
101 	if (res_ninit(&stat) == -1)
102 		exit(1);
103 
104 	anslen = sizeof (answer);
105 	len = res_nsearch(&stat, name, C_IN, rr_type, answer, anslen);
106 
107 	if (len < sizeof (HEADER)) {
108 		res_ndestroy(&stat);
109 		exit(1);
110 	}
111 
112 	ansp = answer;
113 	end = ansp + anslen;
114 
115 	/* LINTED */
116 	h = (HEADER *)answer;
117 	nq = ntohs(h->qdcount);
118 	na = ntohs(h->ancount);
119 	ansp += HFIXEDSZ;
120 
121 	if (nq != 1 || na < 1) {
122 		res_ndestroy(&stat);
123 		exit(1);
124 	}
125 
126 	hostlen = sizeof (name);
127 	len = dn_expand(answer, end, ansp, name, hostlen);
128 	if (len < 0) {
129 		res_ndestroy(&stat);
130 		exit(1);
131 	}
132 
133 	ansp += len + QFIXEDSZ;
134 
135 	if (ansp > end) {
136 		res_ndestroy(&stat);
137 		exit(1);
138 	}
139 
140 	while (na-- > 0 && ansp < end) {
141 
142 		len = dn_expand(answer, end, ansp, name, hostlen);
143 
144 		if (len < 0)
145 			continue;
146 		ansp += len;			/* name */
147 		NS_GET16(type, ansp);		/* type */
148 		NS_GET16(class, ansp);		/* class */
149 		NS_GET32(ttl, ansp);		/* ttl */
150 		NS_GET16(size, ansp);		/* size */
151 
152 		if ((ansp + size) > end) {
153 			res_ndestroy(&stat);
154 			exit(1);
155 		}
156 		if (type == T_SRV) {
157 			NS_GET16(priority, ansp);
158 			NS_GET16(weight, ansp);
159 			NS_GET16(port, ansp);
160 			len = dn_expand(answer, end, ansp, name, hostlen);
161 			if (len < 0) {
162 				res_ndestroy(&stat);
163 				exit(1);
164 			}
165 			for (cp = name; *cp; cp++) {
166 				*cp = tolower(*cp);
167 			}
168 			(void) printf("%s %d\n", name, port);
169 		} else if (typestr && *typestr == 'I') {
170 			(void) inet_ntop(AF_INET, (void *)ansp, nbuf,
171 			    INET6_ADDRSTRLEN);
172 			len = size;
173 			(void) printf("%s\n", nbuf);
174 		} else if (type == T_PTR) {
175 			len = dn_expand(answer, end, ansp, name, hostlen);
176 			if (len < 0) {
177 				res_ndestroy(&stat);
178 				exit(1);
179 			}
180 		}
181 		ansp += len;
182 		if (type == rr_type && class == C_IN) {
183 			found = 1;
184 			if (type != T_SRV && !(typestr && *typestr == 'I'))
185 				break;
186 		}
187 	}
188 
189 	if (found != 1) {
190 		res_ndestroy(&stat);
191 		exit(1);
192 	}
193 
194 	for (cp = name; *cp; cp++) {
195 		*cp = tolower(*cp);
196 	}
197 
198 	if (type != T_SRV && !(typestr && *typestr == 'I'))
199 		(void) printf("%s\n", name);
200 
201 	res_ndestroy(&stat);
202 
203 	return (0);
204 }
205