| 1 |
/* |
| 2 |
* Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MPL-2.0 |
| 5 |
* |
| 6 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 7 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 |
* file, you can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 |
* |
| 10 |
* See the COPYRIGHT file distributed with this work for additional |
| 11 |
* information regarding copyright ownership. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <isc/ascii.h> |
| 15 |
#include <isc/buffer.h> |
| 16 |
#include <isc/result.h> |
| 17 |
#include <isc/types.h> |
| 18 |
#include <isc/util.h> |
| 19 |
|
| 20 |
#include <dns/compress.h> |
| 21 |
#include <dns/types.h> |
| 22 |
|
| 23 |
/* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "old.h" |
| 27 |
|
| 28 |
/* |
| 29 |
* code copied from lib/dns/name.c as of commit |
| 30 |
* 6967973568fe80b03e1729259f8907ce8792be34 |
| 31 |
*/ |
| 32 |
|
| 33 |
typedef enum { fw_start = 0, fw_ordinary, fw_newcurrent } fw_state; |
| 34 |
|
| 35 |
#define VALID_NAME(n) ISC_MAGIC_VALID(n, DNS_NAME_MAGIC) |
| 36 |
|
| 37 |
#define INIT_OFFSETS(name, var, default_offsets) \ |
| 38 |
if ((name)->offsets != NULL) \ |
| 39 |
var = (name)->offsets; \ |
| 40 |
else \ |
| 41 |
var = (default_offsets); |
| 42 |
|
| 43 |
#define MAKE_EMPTY(name) \ |
| 44 |
do { \ |
| 45 |
name->ndata = NULL; \ |
| 46 |
name->length = 0; \ |
| 47 |
name->labels = 0; \ |
| 48 |
name->attributes.absolute = false; \ |
| 49 |
} while (0) |
| 50 |
|
| 51 |
#define BINDABLE(name) (!name->attributes.readonly && !name->attributes.dynamic) |
| 52 |
|
| 53 |
isc_result_t |
| 54 |
old_name_fromwire(dns_name_t *name, isc_buffer_t *source, dns_decompress_t dctx, |
| 55 |
unsigned int options, isc_buffer_t *target) { |
| 56 |
unsigned char *cdata, *ndata; |
| 57 |
unsigned int cused; /* Bytes of compressed name data used */ |
| 58 |
unsigned int nused, labels, n, nmax; |
| 59 |
unsigned int current, new_current, biggest_pointer; |
| 60 |
bool done; |
| 61 |
fw_state state = fw_start; |
| 62 |
unsigned int c; |
| 63 |
unsigned char *offsets; |
| 64 |
dns_offsets_t odata; |
| 65 |
bool downcase; |
| 66 |
bool seen_pointer; |
| 67 |
|
| 68 |
/* |
| 69 |
* Copy the possibly-compressed name at source into target, |
| 70 |
* decompressing it. Loop prevention is performed by checking |
| 71 |
* the new pointer against biggest_pointer. |
| 72 |
*/ |
| 73 |
|
| 74 |
REQUIRE(VALID_NAME(name)); |
| 75 |
REQUIRE((target != NULL && ISC_BUFFER_VALID(target)) || |
| 76 |
(target == NULL && ISC_BUFFER_VALID(name->buffer))); |
| 77 |
|
| 78 |
downcase = ((options & DNS_NAME_DOWNCASE) != 0); |
| 79 |
|
| 80 |
if (target == NULL && name->buffer != NULL) { |
| 81 |
target = name->buffer; |
| 82 |
isc_buffer_clear(target); |
| 83 |
} |
| 84 |
|
| 85 |
REQUIRE(BINDABLE(name)); |
| 86 |
|
| 87 |
INIT_OFFSETS(name, offsets, odata); |
| 88 |
|
| 89 |
/* |
| 90 |
* Make 'name' empty in case of failure. |
| 91 |
*/ |
| 92 |
MAKE_EMPTY(name); |
| 93 |
|
| 94 |
/* |
| 95 |
* Initialize things to make the compiler happy; they're not required. |
| 96 |
*/ |
| 97 |
n = 0; |
| 98 |
new_current = 0; |
| 99 |
|
| 100 |
/* |
| 101 |
* Set up. |
| 102 |
*/ |
| 103 |
labels = 0; |
| 104 |
done = false; |
| 105 |
|
| 106 |
ndata = isc_buffer_used(target); |
| 107 |
nused = 0; |
| 108 |
seen_pointer = false; |
| 109 |
|
| 110 |
/* |
| 111 |
* Find the maximum number of uncompressed target name |
| 112 |
* bytes we are willing to generate. This is the smaller |
| 113 |
* of the available target buffer length and the |
| 114 |
* maximum legal domain name length (255). |
| 115 |
*/ |
| 116 |
nmax = isc_buffer_availablelength(target); |
| 117 |
if (nmax > DNS_NAME_MAXWIRE) { |
| 118 |
nmax = DNS_NAME_MAXWIRE; |
| 119 |
} |
| 120 |
|
| 121 |
cdata = isc_buffer_current(source); |
| 122 |
cused = 0; |
| 123 |
|
| 124 |
current = source->current; |
| 125 |
biggest_pointer = current; |
| 126 |
|
| 127 |
/* |
| 128 |
* Note: The following code is not optimized for speed, but |
| 129 |
* rather for correctness. Speed will be addressed in the future. |
| 130 |
*/ |
| 131 |
|
| 132 |
while (current < source->active && !done) { |
| 133 |
c = *cdata++; |
| 134 |
current++; |
| 135 |
if (!seen_pointer) { |
| 136 |
cused++; |
| 137 |
} |
| 138 |
|
| 139 |
switch (state) { |
| 140 |
case fw_start: |
| 141 |
if (c < 64) { |
| 142 |
offsets[labels] = nused; |
| 143 |
labels++; |
| 144 |
if (nused + c + 1 > nmax) { |
| 145 |
goto full; |
| 146 |
} |
| 147 |
nused += c + 1; |
| 148 |
*ndata++ = c; |
| 149 |
if (c == 0) { |
| 150 |
done = true; |
| 151 |
} |
| 152 |
n = c; |
| 153 |
state = fw_ordinary; |
| 154 |
} else if (c >= 192) { |
| 155 |
/* |
| 156 |
* 14-bit compression pointer |
| 157 |
*/ |
| 158 |
if (!dns_decompress_getpermitted(dctx)) { |
| 159 |
return DNS_R_DISALLOWED; |
| 160 |
} |
| 161 |
new_current = c & 0x3F; |
| 162 |
state = fw_newcurrent; |
| 163 |
} else { |
| 164 |
return DNS_R_BADLABELTYPE; |
| 165 |
} |
| 166 |
break; |
| 167 |
case fw_ordinary: |
| 168 |
if (downcase) { |
| 169 |
c = isc_ascii_tolower(c); |
| 170 |
} |
| 171 |
*ndata++ = c; |
| 172 |
n--; |
| 173 |
if (n == 0) { |
| 174 |
state = fw_start; |
| 175 |
} |
| 176 |
break; |
| 177 |
case fw_newcurrent: |
| 178 |
new_current *= 256; |
| 179 |
new_current += c; |
| 180 |
if (new_current >= biggest_pointer) { |
| 181 |
return DNS_R_BADPOINTER; |
| 182 |
} |
| 183 |
biggest_pointer = new_current; |
| 184 |
current = new_current; |
| 185 |
cdata = (unsigned char *)source->base + current; |
| 186 |
seen_pointer = true; |
| 187 |
state = fw_start; |
| 188 |
break; |
| 189 |
default: |
| 190 |
FATAL_ERROR("Unknown state %d", state); |
| 191 |
/* Does not return. */ |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
if (!done) { |
| 196 |
return ISC_R_UNEXPECTEDEND; |
| 197 |
} |
| 198 |
|
| 199 |
name->ndata = (unsigned char *)target->base + target->used; |
| 200 |
name->labels = labels; |
| 201 |
name->length = nused; |
| 202 |
name->attributes.absolute = true; |
| 203 |
|
| 204 |
isc_buffer_forward(source, cused); |
| 205 |
isc_buffer_add(target, name->length); |
| 206 |
|
| 207 |
return ISC_R_SUCCESS; |
| 208 |
|
| 209 |
full: |
| 210 |
if (nmax == DNS_NAME_MAXWIRE) { |
| 211 |
/* |
| 212 |
* The name did not fit even though we had a buffer |
| 213 |
* big enough to fit a maximum-length name. |
| 214 |
*/ |
| 215 |
return DNS_R_NAMETOOLONG; |
| 216 |
} else { |
| 217 |
/* |
| 218 |
* The name might fit if only the caller could give us a |
| 219 |
* big enough buffer. |
| 220 |
*/ |
| 221 |
return ISC_R_NOSPACE; |
| 222 |
} |
| 223 |
} |