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 * Copyright 2014 Nexenta Systems, Inc. All rights reserved. 28 */ 29 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> 40 41 /* 42 * Private resolver of target and type with arguments: 43 * klooukp [ target [ RR_type ] ] 44 * 45 * Utitilizes DNS lookups to discover domain and realm information. This CLI 46 * is used primarily by kdcmgr(1M) and kclient(1M). 47 */ 48 49 int 50 /* ARGSUSED */ 51 main(int argc, char **argv) 52 { 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 __unused, priority __unused, weight __unused, 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; 62 63 type = T_A; 64 65 if (argc > 3) 66 exit(1); 67 68 if (argc == 1) { 69 if (gethostname(name, MAXHOSTNAMELEN) != 0) 70 exit(1); 71 } else { 72 (void) strncpy(name, (char *)argv[1], NS_MAXDNAME); 73 if (argc == 3) { 74 typestr = argv[2]; 75 76 switch (*typestr) { 77 case 'A': 78 rr_type = T_A; 79 break; 80 case 'C': 81 rr_type = T_CNAME; 82 break; 83 case 'I': 84 rr_type = T_A; 85 break; 86 case 'P': 87 rr_type = T_PTR; 88 (void) sscanf(name, "%hhd.%hhd.%hhd.%hhd", 89 &a, &b, &c, &d); 90 (void) sprintf(name, "%d.%d.%d.%d.in-addr.arpa", 91 d, c, b, a); 92 break; 93 case 'S': 94 rr_type = T_SRV; 95 break; 96 default: 97 exit(1); 98 } 99 } 100 } 101 102 (void) memset(&stat, 0, sizeof (stat)); 103 104 if (res_ninit(&stat) == -1) 105 exit(1); 106 107 anslen = sizeof (answer); 108 len = res_nsearch(&stat, name, C_IN, rr_type, answer, anslen); 109 110 if (len < sizeof (HEADER)) { 111 res_ndestroy(&stat); 112 exit(1); 113 } 114 115 ansp = answer; 116 end = ansp + anslen; 117 118 /* LINTED */ 119 h = (HEADER *)answer; 120 nq = ntohs(h->qdcount); 121 na = ntohs(h->ancount); 122 ansp += HFIXEDSZ; 123 124 if (nq != 1 || na < 1) { 125 res_ndestroy(&stat); 126 exit(1); 127 } 128 129 hostlen = sizeof (name); 130 len = dn_expand(answer, end, ansp, name, hostlen); 131 if (len < 0) { 132 res_ndestroy(&stat); 133 exit(1); 134 } 135 136 ansp += len + QFIXEDSZ; 137 138 if (ansp > end) { 139 res_ndestroy(&stat); 140 exit(1); 141 } 142 143 while (na-- > 0 && ansp < end) { 144 145 len = dn_expand(answer, end, ansp, name, hostlen); 146 147 if (len < 0) 148 continue; 149 ansp += len; /* name */ 150 NS_GET16(type, ansp); /* type */ 151 NS_GET16(class, ansp); /* class */ 152 NS_GET32(ttl, ansp); /* ttl */ 153 NS_GET16(size, ansp); /* size */ 154 155 if ((ansp + size) > end) { 156 res_ndestroy(&stat); 157 exit(1); 158 } 159 if (type == T_SRV) { 160 NS_GET16(priority, ansp); 161 NS_GET16(weight, ansp); 162 NS_GET16(port, ansp); 163 len = dn_expand(answer, end, ansp, name, hostlen); 164 if (len < 0) { 165 res_ndestroy(&stat); 166 exit(1); 167 } 168 for (cp = name; *cp; cp++) { 169 *cp = tolower(*cp); 170 } 171 (void) printf("%s %d\n", name, port); 172 } else if (typestr && *typestr == 'I') { 173 (void) inet_ntop(AF_INET, (void *)ansp, nbuf, 174 INET6_ADDRSTRLEN); 175 len = size; 176 (void) printf("%s\n", nbuf); 177 } else if (type == T_PTR) { 178 len = dn_expand(answer, end, ansp, name, hostlen); 179 if (len < 0) { 180 res_ndestroy(&stat); 181 exit(1); 182 } 183 } 184 ansp += len; 185 if (type == rr_type && class == C_IN) { 186 found = 1; 187 if (type != T_SRV && !(typestr && *typestr == 'I')) 188 break; 189 } 190 } 191 192 if (found != 1) { 193 res_ndestroy(&stat); 194 exit(1); 195 } 196 197 for (cp = name; *cp; cp++) { 198 *cp = tolower(*cp); 199 } 200 201 if (type != T_SRV && !(typestr && *typestr == 'I')) 202 (void) printf("%s\n", name); 203 204 res_ndestroy(&stat); 205 206 return (0); 207 } 208