dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / krb5 / kadmin / kdcmgr / klookup.c
blobbee86b197a7ff4b424bad09cc45daa224fd51146
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
30 #include <sys/types.h>
31 #include <netinet/in.h>
32 #include <arpa/nameser.h>
33 #include <resolv.h>
34 #include <netdb.h>
35 #include <limits.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <ctype.h>
42 * Private resolver of target and type with arguments:
43 * klooukp [ target [ RR_type ] ]
45 * Utitilizes DNS lookups to discover domain and realm information. This CLI
46 * is used primarily by kdcmgr(1M) and kclient(1M).
49 int
50 /* ARGSUSED */
51 main(int argc, char **argv)
53 unsigned char answer[NS_MAXMSG], *ansp = NULL, *end, a, b, c, d;
54 int len = 0, anslen, hostlen, nq, na, type, class;
55 int ttl, priority, weight, port, size;
56 char name[NS_MAXDNAME], *cp, *typestr = NULL;
57 char nbuf[INET6_ADDRSTRLEN];
58 struct __res_state stat;
59 int found = 0;
60 int rr_type = T_A;
61 HEADER *h;
63 if (argc > 3)
64 exit(1);
66 if (argc == 1) {
67 if (gethostname(name, MAXHOSTNAMELEN) != 0)
68 exit(1);
69 } else {
70 (void) strncpy(name, (char *)argv[1], NS_MAXDNAME);
71 if (argc == 3) {
72 typestr = argv[2];
74 switch (*typestr) {
75 case 'A':
76 rr_type = T_A;
77 break;
78 case 'C':
79 rr_type = T_CNAME;
80 break;
81 case 'I':
82 rr_type = T_A;
83 break;
84 case 'P':
85 rr_type = T_PTR;
86 (void) sscanf(name, "%hhd.%hhd.%hhd.%hhd",
87 &a, &b, &c, &d);
88 (void) sprintf(name, "%d.%d.%d.%d.in-addr.arpa",
89 d, c, b, a);
90 break;
91 case 'S':
92 rr_type = T_SRV;
93 break;
94 default:
95 exit(1);
100 (void) memset(&stat, 0, sizeof (stat));
102 if (res_ninit(&stat) == -1)
103 exit(1);
105 anslen = sizeof (answer);
106 len = res_nsearch(&stat, name, C_IN, rr_type, answer, anslen);
108 if (len < sizeof (HEADER)) {
109 res_ndestroy(&stat);
110 exit(1);
113 ansp = answer;
114 end = ansp + anslen;
116 /* LINTED */
117 h = (HEADER *)answer;
118 nq = ntohs(h->qdcount);
119 na = ntohs(h->ancount);
120 ansp += HFIXEDSZ;
122 if (nq != 1 || na < 1) {
123 res_ndestroy(&stat);
124 exit(1);
127 hostlen = sizeof (name);
128 len = dn_expand(answer, end, ansp, name, hostlen);
129 if (len < 0) {
130 res_ndestroy(&stat);
131 exit(1);
134 ansp += len + QFIXEDSZ;
136 if (ansp > end) {
137 res_ndestroy(&stat);
138 exit(1);
141 while (na-- > 0 && ansp < end) {
143 len = dn_expand(answer, end, ansp, name, hostlen);
145 if (len < 0)
146 continue;
147 ansp += len; /* name */
148 NS_GET16(type, ansp); /* type */
149 NS_GET16(class, ansp); /* class */
150 NS_GET32(ttl, ansp); /* ttl */
151 NS_GET16(size, ansp); /* size */
153 if ((ansp + size) > end) {
154 res_ndestroy(&stat);
155 exit(1);
157 if (type == T_SRV) {
158 NS_GET16(priority, ansp);
159 NS_GET16(weight, ansp);
160 NS_GET16(port, ansp);
161 len = dn_expand(answer, end, ansp, name, hostlen);
162 if (len < 0) {
163 res_ndestroy(&stat);
164 exit(1);
166 for (cp = name; *cp; cp++) {
167 *cp = tolower(*cp);
169 (void) printf("%s %d\n", name, port);
170 } else if (typestr && *typestr == 'I') {
171 (void) inet_ntop(AF_INET, (void *)ansp, nbuf,
172 INET6_ADDRSTRLEN);
173 len = size;
174 (void) printf("%s\n", nbuf);
175 } else if (type == T_PTR) {
176 len = dn_expand(answer, end, ansp, name, hostlen);
177 if (len < 0) {
178 res_ndestroy(&stat);
179 exit(1);
182 ansp += len;
183 if (type == rr_type && class == C_IN) {
184 found = 1;
185 if (type != T_SRV && !(typestr && *typestr == 'I'))
186 break;
190 if (found != 1) {
191 res_ndestroy(&stat);
192 exit(1);
195 for (cp = name; *cp; cp++) {
196 *cp = tolower(*cp);
199 if (type != T_SRV && !(typestr && *typestr == 'I'))
200 (void) printf("%s\n", name);
202 res_ndestroy(&stat);
204 return (0);