1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
|
/*
* rdata.c -- RDATA conversion functions.
*
* Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
*
* See LICENSE for the license.
*
*/
#include <config.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <netdb.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include "rdata.h"
#include "zonec.h"
/* Taken from RFC 2538, section 2.1. */
lookup_table_type dns_certificate_types[] = {
{ 1, "PKIX" }, /* X.509 as per PKIX */
{ 2, "SPKI" }, /* SPKI cert */
{ 3, "PGP" }, /* PGP cert */
{ 253, "URI" }, /* URI private */
{ 254, "OID" }, /* OID private */
{ 0, NULL }
};
/* Taken from RFC 2535, section 7. */
lookup_table_type dns_algorithms[] = {
{ 1, "RSAMD5" }, /* RFC 2537 */
{ 2, "DH" }, /* RFC 2539 */
{ 3, "DSA" }, /* RFC 2536 */
{ 4, "ECC" },
{ 5, "RSASHA1" }, /* RFC 3110 */
{ 252, "INDIRECT" },
{ 253, "PRIVATEDNS" },
{ 254, "PRIVATEOID" },
{ 0, NULL }
};
typedef int (*rdata_to_string_type)(buffer_type *output,
rdata_atom_type rdata);
static int
rdata_dname_to_string(buffer_type *output, rdata_atom_type rdata)
{
buffer_printf(output,
"%s",
dname_to_string(domain_dname(rdata_atom_domain(rdata)),
NULL));
return 1;
}
static int
rdata_text_to_string(buffer_type *output, rdata_atom_type rdata)
{
const uint8_t *data = rdata_atom_data(rdata);
uint8_t length = data[0];
size_t i;
buffer_printf(output, "\"");
for (i = 1; i <= length; ++i) {
char ch = (char) data[i];
if (isprint(ch)) {
if (ch == '"' || ch == '\\') {
buffer_printf(output, "\\");
}
buffer_printf(output, "%c", ch);
} else {
buffer_printf(output, "\\%03u", (unsigned) ch);
}
}
buffer_printf(output, "\"");
return 1;
}
static int
rdata_byte_to_string(buffer_type *output, rdata_atom_type rdata)
{
uint8_t data = *rdata_atom_data(rdata);
buffer_printf(output, "%lu", (unsigned long) data);
return 1;
}
static int
rdata_short_to_string(buffer_type *output, rdata_atom_type rdata)
{
uint16_t data = read_uint16(rdata_atom_data(rdata));
buffer_printf(output, "%lu", (unsigned long) data);
return 1;
}
static int
rdata_long_to_string(buffer_type *output, rdata_atom_type rdata)
{
uint32_t data = read_uint32(rdata_atom_data(rdata));
buffer_printf(output, "%lu", (unsigned long) data);
return 1;
}
static int
rdata_a_to_string(buffer_type *output, rdata_atom_type rdata)
{
int result = 0;
char str[200];
if (inet_ntop(AF_INET, rdata_atom_data(rdata), str, sizeof(str))) {
buffer_printf(output, "%s", str);
result = 1;
}
return result;
}
static int
rdata_aaaa_to_string(buffer_type *output, rdata_atom_type rdata)
{
int result = 0;
char str[200];
if (inet_ntop(AF_INET6, rdata_atom_data(rdata), str, sizeof(str))) {
buffer_printf(output, "%s", str);
result = 1;
}
return result;
}
static int
rdata_rrtype_to_string(buffer_type *output, rdata_atom_type rdata)
{
uint16_t type = read_uint16(rdata_atom_data(rdata));
buffer_printf(output, "%s", rrtype_to_string(type));
return 1;
}
static int
rdata_algorithm_to_string(buffer_type *output, rdata_atom_type rdata)
{
uint8_t id = *rdata_atom_data(rdata);
lookup_table_type *alg
= lookup_by_id(dns_algorithms, id);
if (alg) {
buffer_printf(output, "%s", alg->name);
} else {
buffer_printf(output, "%u", (unsigned) id);
}
return 1;
}
static int
rdata_certificate_type_to_string(buffer_type *output, rdata_atom_type rdata)
{
uint16_t id = read_uint16(rdata_atom_data(rdata));
lookup_table_type *type
= lookup_by_id(dns_certificate_types, id);
if (type) {
buffer_printf(output, "%s", type->name);
} else {
buffer_printf(output, "%u", (unsigned) id);
}
return 1;
}
static int
rdata_period_to_string(buffer_type *output, rdata_atom_type rdata)
{
uint32_t period = read_uint32(rdata_atom_data(rdata));
buffer_printf(output, "%lu", (unsigned long) period);
return 1;
}
static int
rdata_time_to_string(buffer_type *output, rdata_atom_type rdata)
{
int result = 0;
time_t time = (time_t) read_uint32(rdata_atom_data(rdata));
struct tm *tm = gmtime(&time);
char buf[15];
if (strftime(buf, sizeof(buf), "%Y%m%d%H%M%S", tm)) {
buffer_printf(output, "%s", buf);
result = 1;
}
return result;
}
static int
rdata_base64_to_string(buffer_type *output, rdata_atom_type rdata)
{
int length;
size_t size = rdata_atom_size(rdata);
buffer_reserve(output, size * 2 + 1);
length = b64_ntop(rdata_atom_data(rdata), size,
(char *) buffer_current(output), size * 2);
if (length > 0) {
buffer_skip(output, length);
}
return length != -1;
}
static void
hex_to_string(buffer_type *output, const uint8_t *data, size_t size)
{
static const char hexdigits[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
size_t i;
buffer_reserve(output, size * 2);
for (i = 0; i < size; ++i) {
uint8_t octet = *data++;
buffer_write_u8(output, hexdigits[octet >> 4]);
buffer_write_u8(output, hexdigits[octet & 0x0f]);
}
}
static int
rdata_hex_to_string(buffer_type *output, rdata_atom_type rdata)
{
hex_to_string(output, rdata_atom_data(rdata), rdata_atom_size(rdata));
return 1;
}
static int
rdata_nsap_to_string(buffer_type *output, rdata_atom_type rdata)
{
buffer_printf(output, "0x");
hex_to_string(output, rdata_atom_data(rdata), rdata_atom_size(rdata));
return 1;
}
static int
rdata_apl_to_string(buffer_type *output, rdata_atom_type rdata)
{
int result = 0;
buffer_type packet;
buffer_create_from(
&packet, rdata_atom_data(rdata), rdata_atom_size(rdata));
if (buffer_available(&packet, 4)) {
uint16_t address_family = buffer_read_u16(&packet);
uint8_t prefix = buffer_read_u8(&packet);
uint8_t length = buffer_read_u8(&packet);
int negated = length & APL_NEGATION_MASK;
int af = -1;
length &= APL_LENGTH_MASK;
switch (address_family) {
case 1: af = AF_INET; break;
case 2: af = AF_INET6; break;
}
if (af != -1 && buffer_available(&packet, length)) {
char text_address[1000];
uint8_t address[128];
memset(address, 0, sizeof(address));
buffer_read(&packet, address, length);
if (inet_ntop(af, address, text_address, sizeof(text_address))) {
buffer_printf(output, "%s%d:%s/%d",
negated ? "!" : "",
(int) address_family,
text_address,
(int) prefix);
result = 1;
}
}
}
return result;
}
static int
rdata_services_to_string(buffer_type *output, rdata_atom_type rdata)
{
int result = 0;
buffer_type packet;
buffer_create_from(
&packet, rdata_atom_data(rdata), rdata_atom_size(rdata));
if (buffer_available(&packet, 1)) {
uint8_t protocol_number = buffer_read_u8(&packet);
ssize_t bitmap_size = buffer_remaining(&packet);
uint8_t *bitmap = buffer_current(&packet);
struct protoent *proto = getprotobynumber(protocol_number);
if (proto) {
int i;
buffer_printf(output, "%s", proto->p_name);
for (i = 0; i < bitmap_size * 8; ++i) {
if (get_bit(bitmap, i)) {
struct servent *service = getservbyport(i, proto->p_name);
if (service) {
buffer_printf(output, " %s", service->s_name);
} else {
buffer_printf(output, " %d", i);
}
}
}
buffer_skip(&packet, bitmap_size);
result = 1;
}
}
return result;
}
static int
rdata_nxt_to_string(buffer_type *output, rdata_atom_type rdata)
{
size_t i;
uint8_t *bitmap = rdata_atom_data(rdata);
size_t bitmap_size = rdata_atom_size(rdata);
for (i = 0; i < bitmap_size * 8; ++i) {
if (get_bit(bitmap, i)) {
buffer_printf(output, "%s ", rrtype_to_string(i));
}
}
buffer_skip(output, -1);
return 1;
}
static int
rdata_nsec_to_string(buffer_type *output, rdata_atom_type rdata)
{
size_t saved_position = buffer_position(output);
buffer_type packet;
int insert_space = 0;
buffer_create_from(
&packet, rdata_atom_data(rdata), rdata_atom_size(rdata));
while (buffer_available(&packet, 2)) {
uint8_t window = buffer_read_u8(&packet);
uint8_t bitmap_size = buffer_read_u8(&packet);
uint8_t *bitmap = buffer_current(&packet);
int i;
if (!buffer_available(&packet, bitmap_size)) {
buffer_set_position(output, saved_position);
return 0;
}
for (i = 0; i < bitmap_size * 8; ++i) {
if (get_bit(bitmap, i)) {
buffer_printf(output,
"%s%s",
insert_space ? " " : "",
rrtype_to_string(
window * 256 + i));
insert_space = 1;
}
}
buffer_skip(&packet, bitmap_size);
}
return 1;
}
static int
rdata_loc_to_string(buffer_type *ATTR_UNUSED(output),
rdata_atom_type ATTR_UNUSED(rdata))
{
/*
* Returning 0 forces the record to be printed in unknown
* format.
*/
return 0;
}
static int
rdata_unknown_to_string(buffer_type *output, rdata_atom_type rdata)
{
uint16_t size = rdata_atom_size(rdata);
buffer_printf(output, "\\# %lu ", (unsigned long) size);
hex_to_string(output, rdata_atom_data(rdata), size);
return 1;
}
static rdata_to_string_type rdata_to_string_table[RDATA_ZF_UNKNOWN + 1] = {
rdata_dname_to_string,
rdata_text_to_string,
rdata_byte_to_string,
rdata_short_to_string,
rdata_long_to_string,
rdata_a_to_string,
rdata_aaaa_to_string,
rdata_rrtype_to_string,
rdata_algorithm_to_string,
rdata_certificate_type_to_string,
rdata_period_to_string,
rdata_time_to_string,
rdata_base64_to_string,
rdata_hex_to_string,
rdata_nsap_to_string,
rdata_apl_to_string,
rdata_services_to_string,
rdata_nxt_to_string,
rdata_nsec_to_string,
rdata_loc_to_string,
rdata_unknown_to_string
};
int
rdata_atom_to_string(buffer_type *output, rdata_zoneformat_type type,
rdata_atom_type rdata)
{
return rdata_to_string_table[type](output, rdata);
}
ssize_t
rdata_wireformat_to_rdata_atoms(region_type *region,
domain_table_type *owners,
uint16_t rrtype,
uint16_t data_size,
buffer_type *packet,
rdata_atom_type **rdatas)
{
size_t end = buffer_position(packet) + data_size;
ssize_t i;
rdata_atom_type temp_rdatas[MAXRDATALEN];
rrtype_descriptor_type *descriptor = rrtype_descriptor_by_type(rrtype);
region_type *temp_region;
assert(descriptor->maximum <= MAXRDATALEN);
if (!buffer_available(packet, data_size)) {
return -1;
}
temp_region = region_create(xalloc, free);
for (i = 0; i < descriptor->maximum; ++i) {
int is_domain = 0;
size_t length = 0;
int required = i < descriptor->minimum;
switch (rdata_atom_wireformat_type(rrtype, i)) {
case RDATA_WF_COMPRESSED_DNAME:
case RDATA_WF_UNCOMPRESSED_DNAME:
is_domain = 1;
break;
case RDATA_WF_BYTE:
length = sizeof(uint8_t);
break;
case RDATA_WF_SHORT:
length = sizeof(uint16_t);
break;
case RDATA_WF_LONG:
length = sizeof(uint32_t);
break;
case RDATA_WF_TEXT:
/* Length is stored in the first byte. */
length = 1;
if (buffer_position(packet) + length <= end) {
length += buffer_current(packet)[length - 1];
}
break;
case RDATA_WF_A:
length = sizeof(in_addr_t);
break;
case RDATA_WF_AAAA:
length = IP6ADDRLEN;
break;
case RDATA_WF_BINARY:
/* Remaining RDATA is binary. */
length = end - buffer_position(packet);
break;
case RDATA_WF_APL:
length = (sizeof(uint16_t) /* address family */
+ sizeof(uint8_t) /* prefix */
+ sizeof(uint8_t)); /* length */
if (buffer_position(packet) + length <= end) {
/* Mask out negation bit. */
length += (buffer_current(packet)[length - 1]
& APL_LENGTH_MASK);
}
break;
}
if (is_domain) {
const dname_type *dname;
if (!required && buffer_position(packet) == end) {
break;
}
dname = dname_make_from_packet(
temp_region, packet, 1, 1);
if (!dname || buffer_position(packet) > end) {
/* Error in domain name. */
region_destroy(temp_region);
return -1;
}
temp_rdatas[i].domain
= domain_table_insert(owners, dname);
} else {
if (buffer_position(packet) + length > end) {
if (required) {
/* Truncated RDATA. */
region_destroy(temp_region);
return -1;
} else {
break;
}
}
temp_rdatas[i].data = (uint16_t *) region_alloc(
region, sizeof(uint16_t) + length);
temp_rdatas[i].data[0] = length;
buffer_read(packet, temp_rdatas[i].data + 1, length);
}
}
if (buffer_position(packet) < end) {
/* Trailing garbage. */
region_destroy(temp_region);
return -1;
}
*rdatas = (rdata_atom_type *) region_alloc_init(
region, temp_rdatas, i * sizeof(rdata_atom_type));
region_destroy(temp_region);
return i;
}
size_t
rdata_maximum_wireformat_size(rrtype_descriptor_type *descriptor,
size_t rdata_count,
rdata_atom_type *rdatas)
{
size_t result = 0;
size_t i;
for (i = 0; i < rdata_count; ++i) {
if (rdata_atom_is_domain(descriptor->type, i)) {
result += domain_dname(rdata_atom_domain(rdatas[i]))->name_size;
} else {
result += rdata_atom_size(rdatas[i]);
}
}
return result;
}
int
rdata_atoms_to_unknown_string(buffer_type *output,
rrtype_descriptor_type *descriptor,
size_t rdata_count,
rdata_atom_type *rdatas)
{
size_t i;
size_t size =
rdata_maximum_wireformat_size(descriptor, rdata_count, rdatas);
buffer_printf(output, " \\# %lu ", (unsigned long) size);
for (i = 0; i < rdata_count; ++i) {
if (rdata_atom_is_domain(descriptor->type, i)) {
const dname_type *dname =
domain_dname(rdata_atom_domain(rdatas[i]));
hex_to_string(
output, dname_name(dname), dname->name_size);
} else {
rdata_hex_to_string(output, rdatas[i]);
}
}
return 1;
}
|