xref: /titanic_51/usr/src/cmd/krb5/kadmin/kdcmgr/klookup.c (revision 5a7763bf3e9db4cfe6cb523b096cb74af71e3793)
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 2007 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 int
41 /* ARGSUSED */
42 main(int argc, char **argv)
43 {
44 	unsigned char answer[NS_MAXMSG], *ansp = NULL, *end;
45 	int len = 0, anslen, hostlen, nq, na, type, class;
46 	char hostname[MAXHOSTNAMELEN], *cp;
47 	struct __res_state stat;
48 	int found = 0;
49 	HEADER *h;
50 
51 	if (argc != 1)
52 		exit(1);
53 
54 	if (gethostname(hostname, MAXHOSTNAMELEN) != 0)
55 		exit(1);
56 
57 	(void) memset(&stat, 0, sizeof (stat));
58 
59 	if (res_ninit(&stat) == -1)
60 		exit(1);
61 
62 	anslen = sizeof (answer);
63 	len = res_nsearch(&stat, hostname, C_IN, T_A, answer, anslen);
64 
65 	if (len < sizeof (HEADER))
66 		exit(1);
67 
68 	ansp = answer;
69 	end = ansp + anslen;
70 
71 	/* LINTED */
72 	h = (HEADER *)answer;
73 	nq = ntohs(h->qdcount);
74 	na = ntohs(h->ancount);
75 	ansp += HFIXEDSZ;
76 
77 	if (nq != 1 || na < 1)
78 		exit(1);
79 
80 	hostlen = sizeof (hostname);
81 	len = dn_expand(answer, end, ansp, hostname, hostlen);
82 	if (len < 0)
83 		exit(1);
84 
85 	ansp += len + QFIXEDSZ;
86 
87 	if (ansp > end)
88 		exit(1);
89 
90 	while (na-- > 0 && ansp < end) {
91 		len = dn_expand(answer, end, ansp, hostname, hostlen);
92 
93 		if (len < 0)
94 			continue;
95 		ansp += len;			/* hostname */
96 		type = ns_get16(ansp);
97 		ansp += INT16SZ;		/* type */
98 		class = ns_get16(ansp);
99 		ansp += INT16SZ;		/* class */
100 		ansp += INT32SZ;		/* ttl */
101 		len = ns_get16(ansp);
102 		ansp += INT16SZ;		/* size */
103 		ansp += len;
104 		if (type == T_A && class == C_IN) {
105 			found = 1;
106 			break;
107 		}
108 	}
109 
110 	if (found != 1)
111 		exit(1);
112 
113 	for (cp = hostname; *cp; cp++) {
114 		*cp = tolower(*cp);
115 	}
116 
117 	(void) printf("%s\n", hostname);
118 
119 	return (0);
120 }
121