1 /* $NetBSD: wks_11.c,v 1.9 2022/09/23 12:15:31 christos Exp $ */
2
3 /*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * SPDX-License-Identifier: MPL-2.0
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 *
12 * See the COPYRIGHT file distributed with this work for additional
13 * information regarding copyright ownership.
14 */
15
16 #ifndef RDATA_IN_1_WKS_11_C
17 #define RDATA_IN_1_WKS_11_C
18
19 #include <limits.h>
20 #include <stdlib.h>
21
22 #include <isc/net.h>
23 #include <isc/netdb.h>
24 #include <isc/once.h>
25
26 /*
27 * Redefine CHECK here so cppcheck "sees" the define.
28 */
29 #ifndef CHECK
30 #define CHECK(op) \
31 do { \
32 result = (op); \
33 if (result != ISC_R_SUCCESS) \
34 goto cleanup; \
35 } while (0)
36 #endif /* ifndef CHECK */
37
38 #define RRTYPE_WKS_ATTRIBUTES (0)
39
40 static isc_mutex_t wks_lock;
41
42 static void
init_lock(void)43 init_lock(void) {
44 isc_mutex_init(&wks_lock);
45 }
46
47 static bool
mygetprotobyname(const char * name,long * proto)48 mygetprotobyname(const char *name, long *proto) {
49 struct protoent *pe;
50
51 LOCK(&wks_lock);
52 pe = getprotobyname(name);
53 if (pe != NULL) {
54 *proto = pe->p_proto;
55 }
56 UNLOCK(&wks_lock);
57 return (pe != NULL);
58 }
59
60 static bool
mygetservbyname(const char * name,const char * proto,long * port)61 mygetservbyname(const char *name, const char *proto, long *port) {
62 struct servent *se;
63
64 LOCK(&wks_lock);
65 se = getservbyname(name, proto);
66 if (se != NULL) {
67 *port = ntohs(se->s_port);
68 }
69 UNLOCK(&wks_lock);
70 return (se != NULL);
71 }
72
73 #ifdef _WIN32
74 #include <windows.h>
75 #include <winsock2.h>
76 #include <ws2tcpip.h>
77 #endif /* ifdef _WIN32 */
78
79 static isc_result_t
fromtext_in_wks(ARGS_FROMTEXT)80 fromtext_in_wks(ARGS_FROMTEXT) {
81 static isc_once_t once = ISC_ONCE_INIT;
82 isc_token_t token;
83 isc_region_t region;
84 struct in_addr addr;
85 char *e = NULL;
86 long proto;
87 unsigned char bm[8 * 1024]; /* 64k bits */
88 long port;
89 long maxport = -1;
90 const char *ps = NULL;
91 unsigned int n;
92 char service[32];
93 int i;
94 isc_result_t result;
95
96 REQUIRE(type == dns_rdatatype_wks);
97 REQUIRE(rdclass == dns_rdataclass_in);
98
99 UNUSED(type);
100 UNUSED(origin);
101 UNUSED(options);
102 UNUSED(rdclass);
103 UNUSED(callbacks);
104
105 RUNTIME_CHECK(isc_once_do(&once, init_lock) == ISC_R_SUCCESS);
106
107 #ifdef _WIN32
108 {
109 WORD wVersionRequested;
110 WSADATA wsaData;
111 int err;
112
113 wVersionRequested = MAKEWORD(2, 0);
114
115 err = WSAStartup(wVersionRequested, &wsaData);
116 if (err != 0) {
117 return (ISC_R_FAILURE);
118 }
119 }
120 #endif /* ifdef _WIN32 */
121
122 /*
123 * IPv4 dotted quad.
124 */
125 CHECK(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
126 false));
127
128 isc_buffer_availableregion(target, ®ion);
129 if (inet_pton(AF_INET, DNS_AS_STR(token), &addr) != 1) {
130 CHECKTOK(DNS_R_BADDOTTEDQUAD);
131 }
132 if (region.length < 4) {
133 return (ISC_R_NOSPACE);
134 }
135 memmove(region.base, &addr, 4);
136 isc_buffer_add(target, 4);
137
138 /*
139 * Protocol.
140 */
141 CHECK(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
142 false));
143
144 proto = strtol(DNS_AS_STR(token), &e, 10);
145 if (*e != '\0' && !mygetprotobyname(DNS_AS_STR(token), &proto)) {
146 CHECKTOK(DNS_R_UNKNOWNPROTO);
147 }
148
149 if (proto < 0 || proto > 0xff) {
150 CHECKTOK(ISC_R_RANGE);
151 }
152
153 if (proto == IPPROTO_TCP) {
154 ps = "tcp";
155 } else if (proto == IPPROTO_UDP) {
156 ps = "udp";
157 }
158
159 CHECK(uint8_tobuffer(proto, target));
160
161 memset(bm, 0, sizeof(bm));
162 do {
163 CHECK(isc_lex_getmastertoken(lexer, &token,
164 isc_tokentype_string, true));
165 if (token.type != isc_tokentype_string) {
166 break;
167 }
168
169 /*
170 * Lowercase the service string as some getservbyname() are
171 * case sensitive and the database is usually in lowercase.
172 */
173 strlcpy(service, DNS_AS_STR(token), sizeof(service));
174 for (i = strlen(service) - 1; i >= 0; i--) {
175 if (isupper(service[i] & 0xff)) {
176 service[i] = tolower(service[i] & 0xff);
177 }
178 }
179
180 port = strtol(DNS_AS_STR(token), &e, 10);
181 if (*e != 0 && !mygetservbyname(service, ps, &port) &&
182 !mygetservbyname(DNS_AS_STR(token), ps, &port))
183 {
184 CHECKTOK(DNS_R_UNKNOWNSERVICE);
185 }
186 if (port < 0 || port > 0xffff) {
187 CHECKTOK(ISC_R_RANGE);
188 }
189 if (port > maxport) {
190 maxport = port;
191 }
192 bm[port / 8] |= (0x80 >> (port % 8));
193 } while (1);
194
195 /*
196 * Let upper layer handle eol/eof.
197 */
198 isc_lex_ungettoken(lexer, &token);
199
200 n = (maxport + 8) / 8;
201 result = mem_tobuffer(target, bm, n);
202
203 cleanup:
204 #ifdef _WIN32
205 WSACleanup();
206 #endif /* ifdef _WIN32 */
207
208 return (result);
209 }
210
211 static isc_result_t
totext_in_wks(ARGS_TOTEXT)212 totext_in_wks(ARGS_TOTEXT) {
213 isc_region_t sr;
214 unsigned short proto;
215 char buf[sizeof("65535")];
216 unsigned int i, j;
217
218 UNUSED(tctx);
219
220 REQUIRE(rdata->type == dns_rdatatype_wks);
221 REQUIRE(rdata->rdclass == dns_rdataclass_in);
222 REQUIRE(rdata->length >= 5);
223
224 dns_rdata_toregion(rdata, &sr);
225 RETERR(inet_totext(AF_INET, tctx->flags, &sr, target));
226 isc_region_consume(&sr, 4);
227
228 proto = uint8_fromregion(&sr);
229 snprintf(buf, sizeof(buf), "%u", proto);
230 RETERR(str_totext(" ", target));
231 RETERR(str_totext(buf, target));
232 isc_region_consume(&sr, 1);
233
234 INSIST(sr.length <= 8 * 1024);
235 for (i = 0; i < sr.length; i++) {
236 if (sr.base[i] != 0) {
237 for (j = 0; j < 8; j++) {
238 if ((sr.base[i] & (0x80 >> j)) != 0) {
239 {
240 snprintf(buf, sizeof(buf), "%u",
241 i * 8 + j);
242 RETERR(str_totext(" ", target));
243 RETERR(str_totext(buf, target));
244 }
245 }
246 }
247 }
248 }
249
250 return (ISC_R_SUCCESS);
251 }
252
253 static isc_result_t
fromwire_in_wks(ARGS_FROMWIRE)254 fromwire_in_wks(ARGS_FROMWIRE) {
255 isc_region_t sr;
256 isc_region_t tr;
257
258 REQUIRE(type == dns_rdatatype_wks);
259 REQUIRE(rdclass == dns_rdataclass_in);
260
261 UNUSED(type);
262 UNUSED(dctx);
263 UNUSED(options);
264 UNUSED(rdclass);
265
266 isc_buffer_activeregion(source, &sr);
267 isc_buffer_availableregion(target, &tr);
268
269 if (sr.length < 5) {
270 return (ISC_R_UNEXPECTEDEND);
271 }
272 if (sr.length > 8 * 1024 + 5) {
273 return (DNS_R_EXTRADATA);
274 }
275 if (sr.length > 5 && sr.base[sr.length - 1] == 0) {
276 return (DNS_R_FORMERR);
277 }
278 if (tr.length < sr.length) {
279 return (ISC_R_NOSPACE);
280 }
281
282 memmove(tr.base, sr.base, sr.length);
283 isc_buffer_add(target, sr.length);
284 isc_buffer_forward(source, sr.length);
285
286 return (ISC_R_SUCCESS);
287 }
288
289 static isc_result_t
towire_in_wks(ARGS_TOWIRE)290 towire_in_wks(ARGS_TOWIRE) {
291 isc_region_t sr;
292
293 UNUSED(cctx);
294
295 REQUIRE(rdata->type == dns_rdatatype_wks);
296 REQUIRE(rdata->rdclass == dns_rdataclass_in);
297 REQUIRE(rdata->length != 0);
298
299 dns_rdata_toregion(rdata, &sr);
300 return (mem_tobuffer(target, sr.base, sr.length));
301 }
302
303 static int
compare_in_wks(ARGS_COMPARE)304 compare_in_wks(ARGS_COMPARE) {
305 isc_region_t r1;
306 isc_region_t r2;
307
308 REQUIRE(rdata1->type == rdata2->type);
309 REQUIRE(rdata1->rdclass == rdata2->rdclass);
310 REQUIRE(rdata1->type == dns_rdatatype_wks);
311 REQUIRE(rdata1->rdclass == dns_rdataclass_in);
312 REQUIRE(rdata1->length != 0);
313 REQUIRE(rdata2->length != 0);
314
315 dns_rdata_toregion(rdata1, &r1);
316 dns_rdata_toregion(rdata2, &r2);
317 return (isc_region_compare(&r1, &r2));
318 }
319
320 static isc_result_t
fromstruct_in_wks(ARGS_FROMSTRUCT)321 fromstruct_in_wks(ARGS_FROMSTRUCT) {
322 dns_rdata_in_wks_t *wks = source;
323 uint32_t a;
324
325 REQUIRE(type == dns_rdatatype_wks);
326 REQUIRE(rdclass == dns_rdataclass_in);
327 REQUIRE(wks != NULL);
328 REQUIRE(wks->common.rdtype == type);
329 REQUIRE(wks->common.rdclass == rdclass);
330 REQUIRE((wks->map != NULL && wks->map_len <= 8 * 1024) ||
331 wks->map_len == 0);
332
333 UNUSED(type);
334 UNUSED(rdclass);
335
336 a = ntohl(wks->in_addr.s_addr);
337 RETERR(uint32_tobuffer(a, target));
338 RETERR(uint8_tobuffer(wks->protocol, target));
339 return (mem_tobuffer(target, wks->map, wks->map_len));
340 }
341
342 static isc_result_t
tostruct_in_wks(ARGS_TOSTRUCT)343 tostruct_in_wks(ARGS_TOSTRUCT) {
344 dns_rdata_in_wks_t *wks = target;
345 uint32_t n;
346 isc_region_t region;
347
348 REQUIRE(wks != NULL);
349 REQUIRE(rdata->type == dns_rdatatype_wks);
350 REQUIRE(rdata->rdclass == dns_rdataclass_in);
351 REQUIRE(rdata->length != 0);
352
353 wks->common.rdclass = rdata->rdclass;
354 wks->common.rdtype = rdata->type;
355 ISC_LINK_INIT(&wks->common, link);
356
357 dns_rdata_toregion(rdata, ®ion);
358 n = uint32_fromregion(®ion);
359 wks->in_addr.s_addr = htonl(n);
360 isc_region_consume(®ion, 4);
361 wks->protocol = uint8_fromregion(®ion);
362 isc_region_consume(®ion, 1);
363 wks->map_len = region.length;
364 wks->map = mem_maybedup(mctx, region.base, region.length);
365 if (wks->map == NULL) {
366 return (ISC_R_NOMEMORY);
367 }
368 wks->mctx = mctx;
369 return (ISC_R_SUCCESS);
370 }
371
372 static void
freestruct_in_wks(ARGS_FREESTRUCT)373 freestruct_in_wks(ARGS_FREESTRUCT) {
374 dns_rdata_in_wks_t *wks = source;
375
376 REQUIRE(wks != NULL);
377 REQUIRE(wks->common.rdtype == dns_rdatatype_wks);
378 REQUIRE(wks->common.rdclass == dns_rdataclass_in);
379
380 if (wks->mctx == NULL) {
381 return;
382 }
383
384 if (wks->map != NULL) {
385 isc_mem_free(wks->mctx, wks->map);
386 }
387 wks->mctx = NULL;
388 }
389
390 static isc_result_t
additionaldata_in_wks(ARGS_ADDLDATA)391 additionaldata_in_wks(ARGS_ADDLDATA) {
392 UNUSED(rdata);
393 UNUSED(add);
394 UNUSED(arg);
395
396 REQUIRE(rdata->type == dns_rdatatype_wks);
397 REQUIRE(rdata->rdclass == dns_rdataclass_in);
398
399 return (ISC_R_SUCCESS);
400 }
401
402 static isc_result_t
digest_in_wks(ARGS_DIGEST)403 digest_in_wks(ARGS_DIGEST) {
404 isc_region_t r;
405
406 REQUIRE(rdata->type == dns_rdatatype_wks);
407 REQUIRE(rdata->rdclass == dns_rdataclass_in);
408
409 dns_rdata_toregion(rdata, &r);
410
411 return ((digest)(arg, &r));
412 }
413
414 static bool
checkowner_in_wks(ARGS_CHECKOWNER)415 checkowner_in_wks(ARGS_CHECKOWNER) {
416 REQUIRE(type == dns_rdatatype_wks);
417 REQUIRE(rdclass == dns_rdataclass_in);
418
419 UNUSED(type);
420 UNUSED(rdclass);
421
422 return (dns_name_ishostname(name, wildcard));
423 }
424
425 static bool
checknames_in_wks(ARGS_CHECKNAMES)426 checknames_in_wks(ARGS_CHECKNAMES) {
427 REQUIRE(rdata->type == dns_rdatatype_wks);
428 REQUIRE(rdata->rdclass == dns_rdataclass_in);
429
430 UNUSED(rdata);
431 UNUSED(owner);
432 UNUSED(bad);
433
434 return (true);
435 }
436
437 static int
casecompare_in_wks(ARGS_COMPARE)438 casecompare_in_wks(ARGS_COMPARE) {
439 return (compare_in_wks(rdata1, rdata2));
440 }
441
442 #endif /* RDATA_IN_1_WKS_11_C */
443