1 /** 2 * str2wire.c - read txt presentation of RRs 3 * 4 * (c) NLnet Labs, 2005-2006 5 * 6 * See the file LICENSE for the license 7 */ 8 9 /** 10 * \file 11 * 12 * Parses text to wireformat. 13 */ 14 #include "config.h" 15 #include "sldns/str2wire.h" 16 #include "sldns/wire2str.h" 17 #include "sldns/sbuffer.h" 18 #include "sldns/parse.h" 19 #include "sldns/parseutil.h" 20 #include <ctype.h> 21 #ifdef HAVE_TIME_H 22 #include <time.h> 23 #endif 24 #ifdef HAVE_NETDB_H 25 #include <netdb.h> 26 #endif 27 28 /** return an error */ 29 #define RET_ERR(e, off) ((int)((e)|((off)<<LDNS_WIREPARSE_SHIFT))) 30 /** Move parse error but keep its ID */ 31 #define RET_ERR_SHIFT(e, move) RET_ERR(LDNS_WIREPARSE_ERROR(e), LDNS_WIREPARSE_OFFSET(e)+(move)); 32 #define LDNS_IP6ADDRLEN (128/8) 33 34 /* 35 * No special care is taken, all dots are translated into 36 * label separators. 37 * @param rel: true if the domain is not absolute (not terminated in .). 38 * The output is then still terminated with a '0' rootlabel. 39 */ 40 static int sldns_str2wire_dname_buf_rel(const char* str, uint8_t* buf, 41 size_t* olen, int* rel) 42 { 43 size_t len; 44 45 const char *s; 46 uint8_t *q, *pq, label_len; 47 48 if(rel) *rel = 0; 49 len = strlen((char*)str); 50 /* octet representation can make strings a lot longer than actual length */ 51 if (len > LDNS_MAX_DOMAINLEN * 4) { 52 return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, 0); 53 } 54 if (0 == len) { 55 return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_UNDERFLOW, 0); 56 } 57 58 /* root label */ 59 if (1 == len && *str == '.') { 60 if(*olen < 1) 61 return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 0); 62 buf[0] = 0; 63 *olen = 1; 64 return LDNS_WIREPARSE_ERR_OK; 65 } 66 67 /* get on with the rest */ 68 69 /* s is on the current character in the string 70 * pq points to where the labellength is going to go 71 * label_len keeps track of the current label's length 72 * q builds the dname inside the buf array 73 */ 74 len = 0; 75 if(*olen < 1) 76 return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 0); 77 q = buf+1; 78 pq = buf; 79 label_len = 0; 80 for (s = str; *s; s++, q++) { 81 if (q >= buf + *olen) 82 return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, q-buf); 83 if (q > buf + LDNS_MAX_DOMAINLEN) 84 return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, q-buf); 85 switch (*s) { 86 case '.': 87 if (label_len > LDNS_MAX_LABELLEN) { 88 return RET_ERR(LDNS_WIREPARSE_ERR_LABEL_OVERFLOW, q-buf); 89 } 90 if (label_len == 0) { 91 return RET_ERR(LDNS_WIREPARSE_ERR_EMPTY_LABEL, q-buf); 92 } 93 len += label_len + 1; 94 *q = 0; 95 *pq = label_len; 96 label_len = 0; 97 pq = q; 98 break; 99 case '\\': 100 /* octet value or literal char */ 101 s += 1; 102 if (!sldns_parse_escape(q, &s)) { 103 *q = 0; 104 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_BAD_ESCAPE, q-buf); 105 } 106 s -= 1; 107 label_len++; 108 break; 109 default: 110 *q = (uint8_t)*s; 111 label_len++; 112 } 113 } 114 115 /* add root label if last char was not '.' */ 116 if(label_len != 0) { 117 if(rel) *rel = 1; 118 if (q >= buf + *olen) 119 return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, q-buf); 120 if (q > buf + LDNS_MAX_DOMAINLEN) { 121 return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, q-buf); 122 } 123 if (label_len > LDNS_MAX_LABELLEN) { 124 return RET_ERR(LDNS_WIREPARSE_ERR_LABEL_OVERFLOW, q-buf); 125 } 126 if (label_len == 0) { /* label_len 0 but not . at end? */ 127 return RET_ERR(LDNS_WIREPARSE_ERR_EMPTY_LABEL, q-buf); 128 } 129 len += label_len + 1; 130 *pq = label_len; 131 *q = 0; 132 } 133 len++; 134 *olen = len; 135 136 return LDNS_WIREPARSE_ERR_OK; 137 } 138 139 int sldns_str2wire_dname_buf(const char* str, uint8_t* buf, size_t* len) 140 { 141 return sldns_str2wire_dname_buf_rel(str, buf, len, NULL); 142 } 143 144 int sldns_str2wire_dname_buf_origin(const char* str, uint8_t* buf, size_t* len, 145 uint8_t* origin, size_t origin_len) 146 { 147 size_t dlen = *len; 148 int rel = 0; 149 int s = sldns_str2wire_dname_buf_rel(str, buf, &dlen, &rel); 150 if(s) return s; 151 152 if(rel && origin && dlen > 0) { 153 if((unsigned)dlen >= 0x00ffffffU || 154 (unsigned)origin_len >= 0x00ffffffU) 155 /* guard against integer overflow in addition */ 156 return RET_ERR(LDNS_WIREPARSE_ERR_GENERAL, *len); 157 if(dlen + origin_len - 1 > LDNS_MAX_DOMAINLEN) 158 return RET_ERR(LDNS_WIREPARSE_ERR_DOMAINNAME_OVERFLOW, 159 LDNS_MAX_DOMAINLEN); 160 if(dlen + origin_len - 1 > *len) 161 return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 162 *len); 163 memmove(buf+dlen-1, origin, origin_len); 164 *len = dlen + origin_len - 1; 165 } else 166 *len = dlen; 167 return LDNS_WIREPARSE_ERR_OK; 168 } 169 170 uint8_t* sldns_str2wire_dname(const char* str, size_t* len) 171 { 172 uint8_t dname[LDNS_MAX_DOMAINLEN+1]; 173 *len = sizeof(dname); 174 if(sldns_str2wire_dname_buf(str, dname, len) == 0) { 175 uint8_t* r; 176 if(*len > sizeof(dname)) return NULL; 177 r = (uint8_t*)malloc(*len); 178 if(r) return memcpy(r, dname, *len); 179 } 180 *len = 0; 181 return NULL; 182 } 183 184 /** read owner name */ 185 static int 186 rrinternal_get_owner(sldns_buffer* strbuf, uint8_t* rr, size_t* len, 187 size_t* dname_len, uint8_t* origin, size_t origin_len, uint8_t* prev, 188 size_t prev_len, char* token, size_t token_len) 189 { 190 /* split the rr in its parts -1 signals trouble */ 191 if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) { 192 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX, 193 sldns_buffer_position(strbuf)); 194 } 195 196 if(token_len < 2) /* make sure there is space to read "@" or "" */ 197 return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 198 sldns_buffer_position(strbuf)); 199 if(token[0]=='@' && token[1]=='\0') { 200 uint8_t* tocopy; 201 if (origin) { 202 *dname_len = origin_len; 203 tocopy = origin; 204 } else if (prev) { 205 *dname_len = prev_len; 206 tocopy = prev; 207 } else { 208 /* default to root */ 209 *dname_len = 1; 210 tocopy = (uint8_t*)"\0"; 211 } 212 if(*len < *dname_len) 213 return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 214 sldns_buffer_position(strbuf)); 215 memmove(rr, tocopy, *dname_len); 216 } else if(*token == '\0') { 217 /* no ownername was given, try prev, if that fails 218 * origin, else default to root */ 219 uint8_t* tocopy; 220 if(prev) { 221 *dname_len = prev_len; 222 tocopy = prev; 223 } else if(origin) { 224 *dname_len = origin_len; 225 tocopy = origin; 226 } else { 227 *dname_len = 1; 228 tocopy = (uint8_t*)"\0"; 229 } 230 if(*len < *dname_len) 231 return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 232 sldns_buffer_position(strbuf)); 233 memmove(rr, tocopy, *dname_len); 234 } else { 235 size_t dlen = *len; 236 int s = sldns_str2wire_dname_buf_origin(token, rr, &dlen, 237 origin, origin_len); 238 if(s) return RET_ERR_SHIFT(s, 239 sldns_buffer_position(strbuf)-strlen(token)); 240 *dname_len = dlen; 241 } 242 return LDNS_WIREPARSE_ERR_OK; 243 } 244 245 /** read ttl */ 246 static int 247 rrinternal_get_ttl(sldns_buffer* strbuf, char* token, size_t token_len, 248 int* not_there, uint32_t* ttl, uint32_t default_ttl) 249 { 250 const char* endptr; 251 if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) { 252 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TTL, 253 sldns_buffer_position(strbuf)); 254 } 255 *ttl = (uint32_t) sldns_str2period(token, &endptr); 256 257 if (strlen(token) > 0 && !isdigit((unsigned char)token[0])) { 258 *not_there = 1; 259 /* ah, it's not there or something */ 260 if (default_ttl == 0) { 261 *ttl = LDNS_DEFAULT_TTL; 262 } else { 263 *ttl = default_ttl; 264 } 265 } 266 return LDNS_WIREPARSE_ERR_OK; 267 } 268 269 /** read class */ 270 static int 271 rrinternal_get_class(sldns_buffer* strbuf, char* token, size_t token_len, 272 int* not_there, uint16_t* cl) 273 { 274 /* if 'not_there' then we got token from previous parse routine */ 275 if(!*not_there) { 276 /* parse new token for class */ 277 if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) { 278 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_CLASS, 279 sldns_buffer_position(strbuf)); 280 } 281 } else *not_there = 0; 282 *cl = sldns_get_rr_class_by_name(token); 283 /* class can be left out too, assume IN, current token must be type */ 284 if(*cl == 0 && strcmp(token, "CLASS0") != 0) { 285 *not_there = 1; 286 *cl = LDNS_RR_CLASS_IN; 287 } 288 return LDNS_WIREPARSE_ERR_OK; 289 } 290 291 /** read type */ 292 static int 293 rrinternal_get_type(sldns_buffer* strbuf, char* token, size_t token_len, 294 int* not_there, uint16_t* tp) 295 { 296 /* if 'not_there' then we got token from previous parse routine */ 297 if(!*not_there) { 298 /* parse new token for type */ 299 if(sldns_bget_token(strbuf, token, "\t\n ", token_len) == -1) { 300 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TYPE, 301 sldns_buffer_position(strbuf)); 302 } 303 } 304 *tp = sldns_get_rr_type_by_name(token); 305 if(*tp == 0 && strcmp(token, "TYPE0") != 0) { 306 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TYPE, 307 sldns_buffer_position(strbuf)); 308 } 309 return LDNS_WIREPARSE_ERR_OK; 310 } 311 312 /** put type, class, ttl into rr buffer */ 313 static int 314 rrinternal_write_typeclassttl(sldns_buffer* strbuf, uint8_t* rr, size_t len, 315 size_t dname_len, uint16_t tp, uint16_t cl, uint32_t ttl, int question) 316 { 317 if(question) { 318 /* question is : name, type, class */ 319 if(dname_len + 4 > len) 320 return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 321 sldns_buffer_position(strbuf)); 322 sldns_write_uint16(rr+dname_len, tp); 323 sldns_write_uint16(rr+dname_len+2, cl); 324 return LDNS_WIREPARSE_ERR_OK; 325 } 326 327 /* type(2), class(2), ttl(4), rdatalen(2 (later)) = 10 */ 328 if(dname_len + 10 > len) 329 return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 330 sldns_buffer_position(strbuf)); 331 sldns_write_uint16(rr+dname_len, tp); 332 sldns_write_uint16(rr+dname_len+2, cl); 333 sldns_write_uint32(rr+dname_len+4, ttl); 334 sldns_write_uint16(rr+dname_len+8, 0); /* rdatalen placeholder */ 335 return LDNS_WIREPARSE_ERR_OK; 336 } 337 338 /** find delimiters for type */ 339 static const char* 340 rrinternal_get_delims(sldns_rdf_type rdftype, size_t r_cnt, size_t r_max) 341 { 342 switch(rdftype) { 343 case LDNS_RDF_TYPE_B64 : 344 case LDNS_RDF_TYPE_HEX : /* These rdf types may con- */ 345 case LDNS_RDF_TYPE_LOC : /* tain whitespace, only if */ 346 case LDNS_RDF_TYPE_WKS : /* it is the last rd field. */ 347 case LDNS_RDF_TYPE_IPSECKEY : 348 case LDNS_RDF_TYPE_NSEC : if (r_cnt == r_max - 1) { 349 return "\n"; 350 } 351 break; 352 default : break; 353 } 354 return "\n\t "; 355 } 356 357 /* Syntactic sugar for sldns_rr_new_frm_str_internal */ 358 static int 359 sldns_rdf_type_maybe_quoted(sldns_rdf_type rdf_type) 360 { 361 return rdf_type == LDNS_RDF_TYPE_STR || 362 rdf_type == LDNS_RDF_TYPE_LONG_STR; 363 } 364 365 /** see if rdata is quoted */ 366 static int 367 rrinternal_get_quoted(sldns_buffer* strbuf, const char** delimiters, 368 sldns_rdf_type rdftype) 369 { 370 if(sldns_rdf_type_maybe_quoted(rdftype) && 371 sldns_buffer_remaining(strbuf) > 0) { 372 373 /* skip spaces */ 374 while(sldns_buffer_remaining(strbuf) > 0 && 375 *(sldns_buffer_current(strbuf)) == ' ') { 376 sldns_buffer_skip(strbuf, 1); 377 } 378 379 if(sldns_buffer_remaining(strbuf) > 0 && 380 *(sldns_buffer_current(strbuf)) == '\"') { 381 *delimiters = "\"\0"; 382 sldns_buffer_skip(strbuf, 1); 383 return 1; 384 } 385 } 386 return 0; 387 } 388 389 /** spool hex data into rdata */ 390 static int 391 rrinternal_spool_hex(char* token, uint8_t* rr, size_t rr_len, 392 size_t rr_cur_len, size_t* cur_hex_data_size, size_t hex_data_size) 393 { 394 char* p = token; 395 while(*p) { 396 if(isspace((unsigned char)*p)) { 397 p++; 398 continue; 399 } 400 if(!isxdigit((unsigned char)*p)) 401 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_RDATA, 402 p-token); 403 if(*cur_hex_data_size >= hex_data_size) 404 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_RDATA, 405 p-token); 406 /* extra robust check */ 407 if(rr_cur_len+(*cur_hex_data_size)/2 >= rr_len) 408 return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 409 p-token); 410 /* see if 16s or 1s */ 411 if( ((*cur_hex_data_size)&1) == 0) { 412 rr[rr_cur_len+(*cur_hex_data_size)/2] = 413 (uint8_t)sldns_hexdigit_to_int(*p)*16; 414 } else { 415 rr[rr_cur_len+(*cur_hex_data_size)/2] += 416 (uint8_t)sldns_hexdigit_to_int(*p); 417 } 418 p++; 419 (*cur_hex_data_size)++; 420 } 421 return LDNS_WIREPARSE_ERR_OK; 422 } 423 424 /** read unknown rr type format */ 425 static int 426 rrinternal_parse_unknown(sldns_buffer* strbuf, char* token, size_t token_len, 427 uint8_t* rr, size_t* rr_len, size_t* rr_cur_len, size_t pre_data_pos) 428 { 429 const char* delim = "\n\t "; 430 size_t hex_data_size, cur_hex_data_size; 431 /* go back to before \# 432 * and skip it while setting delimiters better 433 */ 434 sldns_buffer_set_position(strbuf, pre_data_pos); 435 if(sldns_bget_token(strbuf, token, delim, token_len) == -1) 436 return LDNS_WIREPARSE_ERR_GENERAL; /* should not fail */ 437 /* read rdata octet length */ 438 if(sldns_bget_token(strbuf, token, delim, token_len) == -1) { 439 /* something goes very wrong here */ 440 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_RDATA, 441 sldns_buffer_position(strbuf)); 442 } 443 hex_data_size = (size_t)atoi(token); 444 if(hex_data_size > LDNS_MAX_RDFLEN || 445 *rr_cur_len + hex_data_size > *rr_len) { 446 return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 447 sldns_buffer_position(strbuf)); 448 } 449 /* copy hex chars into hex str (2 chars per byte) */ 450 hex_data_size *= 2; 451 cur_hex_data_size = 0; 452 while(cur_hex_data_size < hex_data_size) { 453 int status; 454 ssize_t c = sldns_bget_token(strbuf, token, delim, token_len); 455 if((status = rrinternal_spool_hex(token, rr, *rr_len, 456 *rr_cur_len, &cur_hex_data_size, hex_data_size)) != 0) 457 return RET_ERR_SHIFT(status, 458 sldns_buffer_position(strbuf)-strlen(token)); 459 if(c == -1) { 460 if(cur_hex_data_size != hex_data_size) 461 return RET_ERR( 462 LDNS_WIREPARSE_ERR_SYNTAX_RDATA, 463 sldns_buffer_position(strbuf)); 464 break; 465 } 466 } 467 *rr_cur_len += hex_data_size/2; 468 return LDNS_WIREPARSE_ERR_OK; 469 } 470 471 /** parse normal RR rdata element */ 472 static int 473 rrinternal_parse_rdf(sldns_buffer* strbuf, char* token, size_t token_len, 474 uint8_t* rr, size_t rr_len, size_t* rr_cur_len, sldns_rdf_type rdftype, 475 uint16_t rr_type, size_t r_cnt, size_t r_max, size_t dname_len, 476 uint8_t* origin, size_t origin_len) 477 { 478 size_t len; 479 int status; 480 481 switch(rdftype) { 482 case LDNS_RDF_TYPE_DNAME: 483 /* check if the origin should be used or concatenated */ 484 if(strcmp(token, "@") == 0) { 485 uint8_t* tocopy; 486 size_t copylen; 487 if(origin) { 488 copylen = origin_len; 489 tocopy = origin; 490 } else if(rr_type == LDNS_RR_TYPE_SOA) { 491 copylen = dname_len; 492 tocopy = rr; /* copy rr owner name */ 493 } else { 494 copylen = 1; 495 tocopy = (uint8_t*)"\0"; 496 } 497 if((*rr_cur_len) + copylen > rr_len) 498 return RET_ERR( 499 LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 500 sldns_buffer_position(strbuf)); 501 memmove(rr+*rr_cur_len, tocopy, copylen); 502 (*rr_cur_len) += copylen; 503 } else { 504 size_t dlen = rr_len - (*rr_cur_len); 505 int s = sldns_str2wire_dname_buf_origin(token, 506 rr+*rr_cur_len, &dlen, origin, origin_len); 507 if(s) return RET_ERR_SHIFT(s, 508 sldns_buffer_position(strbuf)-strlen(token)); 509 (*rr_cur_len) += dlen; 510 } 511 return LDNS_WIREPARSE_ERR_OK; 512 513 case LDNS_RDF_TYPE_HEX: 514 case LDNS_RDF_TYPE_B64: 515 /* When this is the last rdata field, then the 516 * rest should be read in (cause then these 517 * rdf types may contain spaces). */ 518 if(r_cnt == r_max - 1) { 519 size_t tlen = strlen(token); 520 (void)sldns_bget_token(strbuf, token+tlen, "\n", 521 token_len - tlen); 522 } 523 break; 524 default: 525 break; 526 } 527 528 len = rr_len - (*rr_cur_len); 529 if((status=sldns_str2wire_rdf_buf(token, rr+(*rr_cur_len), &len, 530 rdftype)) != 0) 531 return RET_ERR_SHIFT(status, 532 sldns_buffer_position(strbuf)-strlen(token)); 533 *rr_cur_len += len; 534 return LDNS_WIREPARSE_ERR_OK; 535 } 536 537 /** 538 * Parse one rdf token. Takes care of quotes and parenthesis. 539 */ 540 static int 541 sldns_parse_rdf_token(sldns_buffer* strbuf, char* token, size_t token_len, 542 int* quoted, int* parens, size_t* pre_data_pos, 543 const char* delimiters, sldns_rdf_type rdftype, size_t* token_strlen) 544 { 545 size_t slen; 546 547 /* skip spaces */ 548 while(sldns_buffer_remaining(strbuf) > 0 && !*quoted && 549 *(sldns_buffer_current(strbuf)) == ' ') { 550 sldns_buffer_skip(strbuf, 1); 551 } 552 553 *pre_data_pos = sldns_buffer_position(strbuf); 554 if(sldns_bget_token_par(strbuf, token, (*quoted)?"\"":delimiters, 555 token_len, parens, (*quoted)?NULL:" \t") == -1) { 556 return 0; 557 } 558 slen = strlen(token); 559 /* check if not quoted yet, and we have encountered quotes */ 560 if(!*quoted && sldns_rdf_type_maybe_quoted(rdftype) && 561 slen >= 2 && 562 (token[0] == '"' || token[0] == '\'') && 563 (token[slen-1] == '"' || token[slen-1] == '\'')) { 564 /* move token two smaller (quotes) with endnull */ 565 memmove(token, token+1, slen-2); 566 token[slen-2] = 0; 567 slen -= 2; 568 *quoted = 1; 569 } else if(!*quoted && sldns_rdf_type_maybe_quoted(rdftype) && 570 slen >= 2 && 571 (token[0] == '"' || token[0] == '\'')) { 572 /* got the start quote (remove it) but read remainder 573 * of quoted string as well into remainder of token */ 574 memmove(token, token+1, slen-1); 575 token[slen-1] = 0; 576 slen -= 1; 577 *quoted = 1; 578 /* rewind buffer over skipped whitespace */ 579 while(sldns_buffer_position(strbuf) > 0 && 580 (sldns_buffer_current(strbuf)[-1] == ' ' || 581 sldns_buffer_current(strbuf)[-1] == '\t')) { 582 sldns_buffer_skip(strbuf, -1); 583 } 584 if(sldns_bget_token_par(strbuf, token+slen, 585 "\"", token_len-slen, 586 parens, NULL) == -1) { 587 return 0; 588 } 589 slen = strlen(token); 590 } 591 *token_strlen = slen; 592 return 1; 593 } 594 595 /** Add space and one more rdf token onto the existing token string. */ 596 static int 597 sldns_affix_token(sldns_buffer* strbuf, char* token, size_t* token_len, 598 int* quoted, int* parens, size_t* pre_data_pos, 599 const char* delimiters, sldns_rdf_type rdftype, size_t* token_strlen) 600 { 601 size_t addlen = *token_len - *token_strlen; 602 size_t addstrlen = 0; 603 604 /* add space */ 605 if(addlen < 1) return 0; 606 token[*token_strlen] = ' '; 607 token[++(*token_strlen)] = 0; 608 609 /* read another token */ 610 addlen = *token_len - *token_strlen; 611 if(!sldns_parse_rdf_token(strbuf, token+*token_strlen, addlen, quoted, 612 parens, pre_data_pos, delimiters, rdftype, &addstrlen)) 613 return 0; 614 (*token_strlen) += addstrlen; 615 return 1; 616 } 617 618 /** parse rdata from string into rr buffer(-remainder after dname). */ 619 static int 620 rrinternal_parse_rdata(sldns_buffer* strbuf, char* token, size_t token_len, 621 uint8_t* rr, size_t* rr_len, size_t dname_len, uint16_t rr_type, 622 uint8_t* origin, size_t origin_len) 623 { 624 const sldns_rr_descriptor *desc = sldns_rr_descript((uint16_t)rr_type); 625 size_t r_cnt, r_min, r_max; 626 size_t rr_cur_len = dname_len + 10, pre_data_pos, token_strlen; 627 int was_unknown_rr_format = 0, parens = 0, status, quoted; 628 const char* delimiters; 629 sldns_rdf_type rdftype; 630 /* a desc is always returned */ 631 if(!desc) return LDNS_WIREPARSE_ERR_GENERAL; 632 r_max = sldns_rr_descriptor_maximum(desc); 633 r_min = sldns_rr_descriptor_minimum(desc); 634 /* robust check */ 635 if(rr_cur_len > *rr_len) 636 return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 637 sldns_buffer_position(strbuf)); 638 639 /* because number of fields can be variable, we can't rely on 640 * _maximum() only */ 641 for(r_cnt=0; r_cnt < r_max; r_cnt++) { 642 rdftype = sldns_rr_descriptor_field_type(desc, r_cnt); 643 delimiters = rrinternal_get_delims(rdftype, r_cnt, r_max); 644 quoted = rrinternal_get_quoted(strbuf, &delimiters, rdftype); 645 646 if(!sldns_parse_rdf_token(strbuf, token, token_len, "ed, 647 &parens, &pre_data_pos, delimiters, rdftype, 648 &token_strlen)) 649 break; 650 651 /* rfc3597 specifies that any type can be represented 652 * with \# method, which can contain spaces... 653 * it does specify size though... */ 654 655 /* unknown RR data */ 656 if(token_strlen>=2 && strncmp(token, "\\#", 2) == 0 && 657 !quoted && (token_strlen == 2 || token[2]==' ')) { 658 was_unknown_rr_format = 1; 659 if((status=rrinternal_parse_unknown(strbuf, token, 660 token_len, rr, rr_len, &rr_cur_len, 661 pre_data_pos)) != 0) 662 return status; 663 } else if(token_strlen > 0 || quoted) { 664 if(rdftype == LDNS_RDF_TYPE_HIP) { 665 /* affix the HIT and PK fields, with a space */ 666 if(!sldns_affix_token(strbuf, token, 667 &token_len, "ed, &parens, 668 &pre_data_pos, delimiters, 669 rdftype, &token_strlen)) 670 break; 671 if(!sldns_affix_token(strbuf, token, 672 &token_len, "ed, &parens, 673 &pre_data_pos, delimiters, 674 rdftype, &token_strlen)) 675 break; 676 } else if(rdftype == LDNS_RDF_TYPE_INT16_DATA && 677 strcmp(token, "0")!=0) { 678 /* affix len and b64 fields */ 679 if(!sldns_affix_token(strbuf, token, 680 &token_len, "ed, &parens, 681 &pre_data_pos, delimiters, 682 rdftype, &token_strlen)) 683 break; 684 } 685 686 /* normal RR */ 687 if((status=rrinternal_parse_rdf(strbuf, token, 688 token_len, rr, *rr_len, &rr_cur_len, rdftype, 689 rr_type, r_cnt, r_max, dname_len, origin, 690 origin_len)) != 0) { 691 return status; 692 } 693 } 694 } 695 if(!was_unknown_rr_format && r_cnt+1 < r_min) { 696 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_MISSING_VALUE, 697 sldns_buffer_position(strbuf)); 698 } 699 while(parens != 0) { 700 /* read remainder, must be "" */ 701 if(sldns_bget_token_par(strbuf, token, "\n", token_len, 702 &parens, " \t") == -1) { 703 if(parens != 0) 704 return RET_ERR(LDNS_WIREPARSE_ERR_PARENTHESIS, 705 sldns_buffer_position(strbuf)); 706 break; 707 } 708 if(strcmp(token, "") != 0) 709 return RET_ERR(LDNS_WIREPARSE_ERR_PARENTHESIS, 710 sldns_buffer_position(strbuf)); 711 } 712 /* write rdata length */ 713 sldns_write_uint16(rr+dname_len+8, (uint16_t)(rr_cur_len-dname_len-10)); 714 *rr_len = rr_cur_len; 715 return LDNS_WIREPARSE_ERR_OK; 716 } 717 718 /* 719 * trailing spaces are allowed 720 * leading spaces are not allowed 721 * allow ttl to be optional 722 * class is optional too 723 * if ttl is missing, and default_ttl is 0, use DEF_TTL 724 * allow ttl to be written as 1d3h 725 * So the RR should look like. e.g. 726 * miek.nl. 3600 IN MX 10 elektron.atoom.net 727 * or 728 * miek.nl. 1h IN MX 10 elektron.atoom.net 729 * or 730 * miek.nl. IN MX 10 elektron.atoom.net 731 */ 732 static int 733 sldns_str2wire_rr_buf_internal(const char* str, uint8_t* rr, size_t* len, 734 size_t* dname_len, uint32_t default_ttl, uint8_t* origin, 735 size_t origin_len, uint8_t* prev, size_t prev_len, int question) 736 { 737 int status; 738 int not_there = 0; 739 char token[LDNS_MAX_RDFLEN+1]; 740 uint32_t ttl = 0; 741 uint16_t tp = 0, cl = 0; 742 size_t ddlen = 0; 743 744 /* string in buffer */ 745 sldns_buffer strbuf; 746 sldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str)); 747 if(!dname_len) dname_len = &ddlen; 748 749 /* parse the owner */ 750 if((status=rrinternal_get_owner(&strbuf, rr, len, dname_len, origin, 751 origin_len, prev, prev_len, token, sizeof(token))) != 0) 752 return status; 753 754 /* parse the [ttl] [class] <type> */ 755 if((status=rrinternal_get_ttl(&strbuf, token, sizeof(token), 756 ¬_there, &ttl, default_ttl)) != 0) 757 return status; 758 if((status=rrinternal_get_class(&strbuf, token, sizeof(token), 759 ¬_there, &cl)) != 0) 760 return status; 761 if((status=rrinternal_get_type(&strbuf, token, sizeof(token), 762 ¬_there, &tp)) != 0) 763 return status; 764 /* put ttl, class, type into the rr result */ 765 if((status=rrinternal_write_typeclassttl(&strbuf, rr, *len, *dname_len, tp, cl, 766 ttl, question)) != 0) 767 return status; 768 /* for a question-RR we are done, no rdata */ 769 if(question) { 770 *len = *dname_len + 4; 771 return LDNS_WIREPARSE_ERR_OK; 772 } 773 774 /* rdata */ 775 if((status=rrinternal_parse_rdata(&strbuf, token, sizeof(token), 776 rr, len, *dname_len, tp, origin, origin_len)) != 0) 777 return status; 778 779 return LDNS_WIREPARSE_ERR_OK; 780 } 781 782 int sldns_str2wire_rr_buf(const char* str, uint8_t* rr, size_t* len, 783 size_t* dname_len, uint32_t default_ttl, uint8_t* origin, 784 size_t origin_len, uint8_t* prev, size_t prev_len) 785 { 786 return sldns_str2wire_rr_buf_internal(str, rr, len, dname_len, 787 default_ttl, origin, origin_len, prev, prev_len, 0); 788 } 789 790 int sldns_str2wire_rr_question_buf(const char* str, uint8_t* rr, size_t* len, 791 size_t* dname_len, uint8_t* origin, size_t origin_len, uint8_t* prev, 792 size_t prev_len) 793 { 794 return sldns_str2wire_rr_buf_internal(str, rr, len, dname_len, 795 0, origin, origin_len, prev, prev_len, 1); 796 } 797 798 uint16_t sldns_wirerr_get_type(uint8_t* rr, size_t len, size_t dname_len) 799 { 800 if(len < dname_len+2) 801 return 0; 802 return sldns_read_uint16(rr+dname_len); 803 } 804 805 uint16_t sldns_wirerr_get_class(uint8_t* rr, size_t len, size_t dname_len) 806 { 807 if(len < dname_len+4) 808 return 0; 809 return sldns_read_uint16(rr+dname_len+2); 810 } 811 812 uint32_t sldns_wirerr_get_ttl(uint8_t* rr, size_t len, size_t dname_len) 813 { 814 if(len < dname_len+8) 815 return 0; 816 return sldns_read_uint32(rr+dname_len+4); 817 } 818 819 uint16_t sldns_wirerr_get_rdatalen(uint8_t* rr, size_t len, size_t dname_len) 820 { 821 if(len < dname_len+10) 822 return 0; 823 return sldns_read_uint16(rr+dname_len+8); 824 } 825 826 uint8_t* sldns_wirerr_get_rdata(uint8_t* rr, size_t len, size_t dname_len) 827 { 828 if(len < dname_len+10) 829 return NULL; 830 return rr+dname_len+10; 831 } 832 833 uint8_t* sldns_wirerr_get_rdatawl(uint8_t* rr, size_t len, size_t dname_len) 834 { 835 if(len < dname_len+10) 836 return NULL; 837 return rr+dname_len+8; 838 } 839 840 const char* sldns_get_errorstr_parse(int e) 841 { 842 sldns_lookup_table *lt; 843 lt = sldns_lookup_by_id(sldns_wireparse_errors, LDNS_WIREPARSE_ERROR(e)); 844 return lt?lt->name:"unknown error"; 845 } 846 847 /* Strip whitespace from the start and the end of <line>. */ 848 char * 849 sldns_strip_ws(char *line) 850 { 851 char *s = line, *e; 852 853 for (s = line; *s && isspace((unsigned char)*s); s++) 854 ; 855 for (e = strchr(s, 0); e > s+2 && isspace((unsigned char)e[-1]) && e[-2] != '\\'; e--) 856 ; 857 *e = 0; 858 return s; 859 } 860 861 int sldns_fp2wire_rr_buf(FILE* in, uint8_t* rr, size_t* len, size_t* dname_len, 862 struct sldns_file_parse_state* parse_state) 863 { 864 char line[LDNS_RR_BUF_SIZE+1]; 865 ssize_t size; 866 867 /* read an entire line in from the file */ 868 if((size = sldns_fget_token_l(in, line, LDNS_PARSE_SKIP_SPACE, 869 LDNS_RR_BUF_SIZE, parse_state?&parse_state->lineno:NULL)) 870 == -1) { 871 /* if last line was empty, we are now at feof, which is not 872 * always a parse error (happens when for instance last line 873 * was a comment) 874 */ 875 return LDNS_WIREPARSE_ERR_SYNTAX; 876 } 877 878 /* we can have the situation, where we've read ok, but still got 879 * no bytes to play with, in this case size is 0 */ 880 if(size == 0) { 881 if(*len > 0) 882 rr[0] = 0; 883 *len = 0; 884 *dname_len = 0; 885 return LDNS_WIREPARSE_ERR_OK; 886 } 887 888 if(strncmp(line, "$ORIGIN", 7) == 0 && isspace((unsigned char)line[7])) { 889 int s; 890 strlcpy((char*)rr, line, *len); 891 *len = 0; 892 *dname_len = 0; 893 if(!parse_state) return LDNS_WIREPARSE_ERR_OK; 894 parse_state->origin_len = sizeof(parse_state->origin); 895 s = sldns_str2wire_dname_buf(sldns_strip_ws(line+8), 896 parse_state->origin, &parse_state->origin_len); 897 if(s) parse_state->origin_len = 0; 898 return s; 899 } else if(strncmp(line, "$TTL", 4) == 0 && isspace((unsigned char)line[4])) { 900 const char* end = NULL; 901 strlcpy((char*)rr, line, *len); 902 *len = 0; 903 *dname_len = 0; 904 if(!parse_state) return LDNS_WIREPARSE_ERR_OK; 905 parse_state->default_ttl = sldns_str2period( 906 sldns_strip_ws(line+5), &end); 907 } else if (strncmp(line, "$INCLUDE", 8) == 0) { 908 strlcpy((char*)rr, line, *len); 909 *len = 0; 910 *dname_len = 0; 911 return LDNS_WIREPARSE_ERR_INCLUDE; 912 } else if (strncmp(line, "$", 1) == 0) { 913 strlcpy((char*)rr, line, *len); 914 *len = 0; 915 *dname_len = 0; 916 return LDNS_WIREPARSE_ERR_INCLUDE; 917 } else { 918 int r = sldns_str2wire_rr_buf(line, rr, len, dname_len, 919 parse_state?parse_state->default_ttl:0, 920 (parse_state&&parse_state->origin_len)? 921 parse_state->origin:NULL, 922 parse_state?parse_state->origin_len:0, 923 (parse_state&&parse_state->prev_rr_len)? 924 parse_state->prev_rr:NULL, 925 parse_state?parse_state->prev_rr_len:0); 926 if(r == LDNS_WIREPARSE_ERR_OK && (*dname_len) != 0 && 927 parse_state && 928 (*dname_len) <= sizeof(parse_state->prev_rr)) { 929 memmove(parse_state->prev_rr, rr, *dname_len); 930 parse_state->prev_rr_len = (*dname_len); 931 } 932 return r; 933 } 934 return LDNS_WIREPARSE_ERR_OK; 935 } 936 937 int sldns_str2wire_rdf_buf(const char* str, uint8_t* rd, size_t* len, 938 sldns_rdf_type rdftype) 939 { 940 switch (rdftype) { 941 case LDNS_RDF_TYPE_DNAME: 942 return sldns_str2wire_dname_buf(str, rd, len); 943 case LDNS_RDF_TYPE_INT8: 944 return sldns_str2wire_int8_buf(str, rd, len); 945 case LDNS_RDF_TYPE_INT16: 946 return sldns_str2wire_int16_buf(str, rd, len); 947 case LDNS_RDF_TYPE_INT32: 948 return sldns_str2wire_int32_buf(str, rd, len); 949 case LDNS_RDF_TYPE_A: 950 return sldns_str2wire_a_buf(str, rd, len); 951 case LDNS_RDF_TYPE_AAAA: 952 return sldns_str2wire_aaaa_buf(str, rd, len); 953 case LDNS_RDF_TYPE_STR: 954 return sldns_str2wire_str_buf(str, rd, len); 955 case LDNS_RDF_TYPE_APL: 956 return sldns_str2wire_apl_buf(str, rd, len); 957 case LDNS_RDF_TYPE_B64: 958 return sldns_str2wire_b64_buf(str, rd, len); 959 case LDNS_RDF_TYPE_B32_EXT: 960 return sldns_str2wire_b32_ext_buf(str, rd, len); 961 case LDNS_RDF_TYPE_HEX: 962 return sldns_str2wire_hex_buf(str, rd, len); 963 case LDNS_RDF_TYPE_NSEC: 964 return sldns_str2wire_nsec_buf(str, rd, len); 965 case LDNS_RDF_TYPE_TYPE: 966 return sldns_str2wire_type_buf(str, rd, len); 967 case LDNS_RDF_TYPE_CLASS: 968 return sldns_str2wire_class_buf(str, rd, len); 969 case LDNS_RDF_TYPE_CERT_ALG: 970 return sldns_str2wire_cert_alg_buf(str, rd, len); 971 case LDNS_RDF_TYPE_ALG: 972 return sldns_str2wire_alg_buf(str, rd, len); 973 case LDNS_RDF_TYPE_TIME: 974 return sldns_str2wire_time_buf(str, rd, len); 975 case LDNS_RDF_TYPE_PERIOD: 976 return sldns_str2wire_period_buf(str, rd, len); 977 case LDNS_RDF_TYPE_TSIGTIME: 978 return sldns_str2wire_tsigtime_buf(str, rd, len); 979 case LDNS_RDF_TYPE_LOC: 980 return sldns_str2wire_loc_buf(str, rd, len); 981 case LDNS_RDF_TYPE_WKS: 982 return sldns_str2wire_wks_buf(str, rd, len); 983 case LDNS_RDF_TYPE_NSAP: 984 return sldns_str2wire_nsap_buf(str, rd, len); 985 case LDNS_RDF_TYPE_ATMA: 986 return sldns_str2wire_atma_buf(str, rd, len); 987 case LDNS_RDF_TYPE_IPSECKEY: 988 return sldns_str2wire_ipseckey_buf(str, rd, len); 989 case LDNS_RDF_TYPE_NSEC3_SALT: 990 return sldns_str2wire_nsec3_salt_buf(str, rd, len); 991 case LDNS_RDF_TYPE_NSEC3_NEXT_OWNER: 992 return sldns_str2wire_b32_ext_buf(str, rd, len); 993 case LDNS_RDF_TYPE_ILNP64: 994 return sldns_str2wire_ilnp64_buf(str, rd, len); 995 case LDNS_RDF_TYPE_EUI48: 996 return sldns_str2wire_eui48_buf(str, rd, len); 997 case LDNS_RDF_TYPE_EUI64: 998 return sldns_str2wire_eui64_buf(str, rd, len); 999 case LDNS_RDF_TYPE_TAG: 1000 return sldns_str2wire_tag_buf(str, rd, len); 1001 case LDNS_RDF_TYPE_LONG_STR: 1002 return sldns_str2wire_long_str_buf(str, rd, len); 1003 case LDNS_RDF_TYPE_TSIGERROR: 1004 return sldns_str2wire_tsigerror_buf(str, rd, len); 1005 case LDNS_RDF_TYPE_HIP: 1006 return sldns_str2wire_hip_buf(str, rd, len); 1007 case LDNS_RDF_TYPE_INT16_DATA: 1008 return sldns_str2wire_int16_data_buf(str, rd, len); 1009 case LDNS_RDF_TYPE_UNKNOWN: 1010 case LDNS_RDF_TYPE_SERVICE: 1011 return LDNS_WIREPARSE_ERR_NOT_IMPL; 1012 case LDNS_RDF_TYPE_NONE: 1013 default: 1014 break; 1015 } 1016 return LDNS_WIREPARSE_ERR_GENERAL; 1017 } 1018 1019 int sldns_str2wire_int8_buf(const char* str, uint8_t* rd, size_t* len) 1020 { 1021 char* end; 1022 uint8_t r = (uint8_t)strtol((char*)str, &end, 10); 1023 if(*end != 0) 1024 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, end-(char*)str); 1025 if(*len < 1) 1026 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1027 rd[0] = r; 1028 *len = 1; 1029 return LDNS_WIREPARSE_ERR_OK; 1030 } 1031 1032 int sldns_str2wire_int16_buf(const char* str, uint8_t* rd, size_t* len) 1033 { 1034 char* end; 1035 uint16_t r = (uint16_t)strtol((char*)str, &end, 10); 1036 if(*end != 0) 1037 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, end-(char*)str); 1038 if(*len < 2) 1039 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1040 sldns_write_uint16(rd, r); 1041 *len = 2; 1042 return LDNS_WIREPARSE_ERR_OK; 1043 } 1044 1045 int sldns_str2wire_int32_buf(const char* str, uint8_t* rd, size_t* len) 1046 { 1047 char* end; 1048 uint32_t r; 1049 errno = 0; /* must set to zero before call, 1050 note race condition on errno */ 1051 if(*str == '-') 1052 r = (uint32_t)strtol((char*)str, &end, 10); 1053 else r = (uint32_t)strtoul((char*)str, &end, 10); 1054 if(*end != 0) 1055 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, end-(char*)str); 1056 if(errno == ERANGE) 1057 return LDNS_WIREPARSE_ERR_SYNTAX_INTEGER_OVERFLOW; 1058 if(*len < 4) 1059 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1060 sldns_write_uint32(rd, r); 1061 *len = 4; 1062 return LDNS_WIREPARSE_ERR_OK; 1063 } 1064 1065 int sldns_str2wire_a_buf(const char* str, uint8_t* rd, size_t* len) 1066 { 1067 struct in_addr address; 1068 if(inet_pton(AF_INET, (char*)str, &address) != 1) 1069 return LDNS_WIREPARSE_ERR_SYNTAX_IP4; 1070 if(*len < sizeof(address)) 1071 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1072 memmove(rd, &address, sizeof(address)); 1073 *len = sizeof(address); 1074 return LDNS_WIREPARSE_ERR_OK; 1075 } 1076 1077 int sldns_str2wire_aaaa_buf(const char* str, uint8_t* rd, size_t* len) 1078 { 1079 #ifdef AF_INET6 1080 uint8_t address[LDNS_IP6ADDRLEN + 1]; 1081 if(inet_pton(AF_INET6, (char*)str, address) != 1) 1082 return LDNS_WIREPARSE_ERR_SYNTAX_IP6; 1083 if(*len < LDNS_IP6ADDRLEN) 1084 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1085 memmove(rd, address, LDNS_IP6ADDRLEN); 1086 *len = LDNS_IP6ADDRLEN; 1087 return LDNS_WIREPARSE_ERR_OK; 1088 #else 1089 return LDNS_WIREPARSE_ERR_NOT_IMPL; 1090 #endif 1091 } 1092 1093 int sldns_str2wire_str_buf(const char* str, uint8_t* rd, size_t* len) 1094 { 1095 uint8_t ch = 0; 1096 size_t sl = 0; 1097 const char* s = str; 1098 /* skip length byte */ 1099 if(*len < 1) 1100 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1101 1102 /* read characters */ 1103 while(sldns_parse_char(&ch, &s)) { 1104 if(sl >= 255) 1105 return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR, s-str); 1106 if(*len < sl+2) 1107 return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 1108 s-str); 1109 rd[++sl] = ch; 1110 } 1111 if(!s) 1112 return LDNS_WIREPARSE_ERR_SYNTAX_BAD_ESCAPE; 1113 rd[0] = (uint8_t)sl; 1114 *len = sl+1; 1115 return LDNS_WIREPARSE_ERR_OK; 1116 } 1117 1118 int sldns_str2wire_apl_buf(const char* str, uint8_t* rd, size_t* len) 1119 { 1120 const char *my_str = str; 1121 1122 char my_ip_str[64]; 1123 size_t ip_str_len; 1124 1125 uint16_t family; 1126 int negation; 1127 size_t adflength = 0; 1128 uint8_t data[16+4]; 1129 uint8_t prefix; 1130 size_t i; 1131 1132 if(*my_str == '\0') { 1133 /* empty APL element, no data, no string */ 1134 *len = 0; 1135 return LDNS_WIREPARSE_ERR_OK; 1136 } 1137 1138 /* [!]afi:address/prefix */ 1139 if (strlen(my_str) < 2 1140 || strchr(my_str, ':') == NULL 1141 || strchr(my_str, '/') == NULL 1142 || strchr(my_str, ':') > strchr(my_str, '/')) { 1143 return LDNS_WIREPARSE_ERR_INVALID_STR; 1144 } 1145 1146 if (my_str[0] == '!') { 1147 negation = 1; 1148 my_str += 1; 1149 } else { 1150 negation = 0; 1151 } 1152 1153 family = (uint16_t) atoi(my_str); 1154 1155 my_str = strchr(my_str, ':') + 1; 1156 1157 /* need ip addr and only ip addr for inet_pton */ 1158 ip_str_len = (size_t) (strchr(my_str, '/') - my_str); 1159 if(ip_str_len+1 > sizeof(my_ip_str)) 1160 return LDNS_WIREPARSE_ERR_INVALID_STR; 1161 (void)strlcpy(my_ip_str, my_str, sizeof(my_ip_str)); 1162 my_ip_str[ip_str_len] = 0; 1163 1164 if (family == 1) { 1165 /* ipv4 */ 1166 if(inet_pton(AF_INET, my_ip_str, data+4) == 0) 1167 return LDNS_WIREPARSE_ERR_INVALID_STR; 1168 for (i = 0; i < 4; i++) { 1169 if (data[i+4] != 0) { 1170 adflength = i + 1; 1171 } 1172 } 1173 } else if (family == 2) { 1174 /* ipv6 */ 1175 if (inet_pton(AF_INET6, my_ip_str, data+4) == 0) 1176 return LDNS_WIREPARSE_ERR_INVALID_STR; 1177 for (i = 0; i < 16; i++) { 1178 if (data[i+4] != 0) { 1179 adflength = i + 1; 1180 } 1181 } 1182 } else { 1183 /* unknown family */ 1184 return LDNS_WIREPARSE_ERR_INVALID_STR; 1185 } 1186 1187 my_str = strchr(my_str, '/') + 1; 1188 prefix = (uint8_t) atoi(my_str); 1189 1190 sldns_write_uint16(data, family); 1191 data[2] = prefix; 1192 data[3] = (uint8_t)adflength; 1193 if (negation) { 1194 /* set bit 1 of byte 3 */ 1195 data[3] = data[3] | 0x80; 1196 } 1197 1198 if(*len < 4+adflength) 1199 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1200 memmove(rd, data, 4+adflength); 1201 *len = 4+adflength; 1202 return LDNS_WIREPARSE_ERR_OK; 1203 } 1204 1205 int sldns_str2wire_b64_buf(const char* str, uint8_t* rd, size_t* len) 1206 { 1207 size_t sz = sldns_b64_pton_calculate_size(strlen(str)); 1208 int n; 1209 if(strcmp(str, "0") == 0) { 1210 *len = 0; 1211 return LDNS_WIREPARSE_ERR_OK; 1212 } 1213 if(*len < sz) 1214 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1215 n = sldns_b64_pton(str, rd, *len); 1216 if(n < 0) 1217 return LDNS_WIREPARSE_ERR_SYNTAX_B64; 1218 *len = (size_t)n; 1219 return LDNS_WIREPARSE_ERR_OK; 1220 } 1221 1222 int sldns_str2wire_b32_ext_buf(const char* str, uint8_t* rd, size_t* len) 1223 { 1224 size_t slen = strlen(str); 1225 size_t sz = sldns_b32_pton_calculate_size(slen); 1226 int n; 1227 if(*len < 1+sz) 1228 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1229 rd[0] = (uint8_t)sz; 1230 n = sldns_b32_pton_extended_hex(str, slen, rd+1, *len-1); 1231 if(n < 0) 1232 return LDNS_WIREPARSE_ERR_SYNTAX_B32_EXT; 1233 *len = (size_t)n+1; 1234 return LDNS_WIREPARSE_ERR_OK; 1235 } 1236 1237 /** see if the string ends, or ends in whitespace */ 1238 static int 1239 sldns_is_last_of_string(const char* str) 1240 { 1241 if(*str == 0) return 1; 1242 while(isspace((unsigned char)*str)) 1243 str++; 1244 if(*str == 0) return 1; 1245 return 0; 1246 } 1247 1248 int sldns_str2wire_hex_buf(const char* str, uint8_t* rd, size_t* len) 1249 { 1250 const char* s = str; 1251 size_t dlen = 0; /* number of hexdigits parsed */ 1252 while(*s) { 1253 if(isspace((unsigned char)*s)) { 1254 s++; 1255 continue; 1256 } 1257 if(dlen == 0 && *s == '0' && sldns_is_last_of_string(s+1)) { 1258 *len = 0; 1259 return LDNS_WIREPARSE_ERR_OK; 1260 } 1261 if(!isxdigit((unsigned char)*s)) 1262 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str); 1263 if(*len < dlen/2 + 1) 1264 return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 1265 s-str); 1266 if((dlen&1)==0) 1267 rd[dlen/2] = (uint8_t)sldns_hexdigit_to_int(*s++) * 16; 1268 else rd[dlen/2] += (uint8_t)sldns_hexdigit_to_int(*s++); 1269 dlen++; 1270 } 1271 if((dlen&1)!=0) 1272 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str); 1273 *len = dlen/2; 1274 return LDNS_WIREPARSE_ERR_OK; 1275 } 1276 1277 int sldns_str2wire_nsec_buf(const char* str, uint8_t* rd, size_t* len) 1278 { 1279 const char *delim = "\n\t "; 1280 char token[64]; /* for a type name */ 1281 size_t type_count = 0; 1282 int block; 1283 size_t used = 0; 1284 uint16_t maxtype = 0; 1285 uint8_t typebits[8192]; /* 65536 bits */ 1286 uint8_t window_in_use[256]; 1287 1288 /* string in buffer */ 1289 sldns_buffer strbuf; 1290 sldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str)); 1291 1292 /* parse the types */ 1293 memset(typebits, 0, sizeof(typebits)); 1294 memset(window_in_use, 0, sizeof(window_in_use)); 1295 while(sldns_buffer_remaining(&strbuf) > 0 && 1296 sldns_bget_token(&strbuf, token, delim, sizeof(token)) != -1) { 1297 uint16_t t = sldns_get_rr_type_by_name(token); 1298 if(token[0] == 0) 1299 continue; 1300 if(t == 0 && strcmp(token, "TYPE0") != 0) 1301 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TYPE, 1302 sldns_buffer_position(&strbuf)); 1303 typebits[t/8] |= (0x80>>(t%8)); 1304 window_in_use[t/256] = 1; 1305 type_count++; 1306 if(t > maxtype) maxtype = t; 1307 } 1308 1309 /* empty NSEC bitmap */ 1310 if(type_count == 0) { 1311 *len = 0; 1312 return LDNS_WIREPARSE_ERR_OK; 1313 } 1314 1315 /* encode windows {u8 windowblock, u8 bitmaplength, 0-32u8 bitmap}, 1316 * block is 0-255 upper octet of types, length if 0-32. */ 1317 for(block = 0; block <= (int)maxtype/256; block++) { 1318 int i, blocklen = 0; 1319 if(!window_in_use[block]) 1320 continue; 1321 for(i=0; i<32; i++) { 1322 if(typebits[block*32+i] != 0) 1323 blocklen = i+1; 1324 } 1325 if(blocklen == 0) 1326 continue; /* empty window should have been !in_use */ 1327 if(used+blocklen+2 > *len) 1328 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1329 rd[used+0] = (uint8_t)block; 1330 rd[used+1] = (uint8_t)blocklen; 1331 for(i=0; i<blocklen; i++) { 1332 rd[used+2+i] = typebits[block*32+i]; 1333 } 1334 used += blocklen+2; 1335 } 1336 *len = used; 1337 return LDNS_WIREPARSE_ERR_OK; 1338 } 1339 1340 int sldns_str2wire_type_buf(const char* str, uint8_t* rd, size_t* len) 1341 { 1342 uint16_t t = sldns_get_rr_type_by_name(str); 1343 if(t == 0 && strcmp(str, "TYPE0") != 0) 1344 return LDNS_WIREPARSE_ERR_SYNTAX_TYPE; 1345 if(*len < 2) 1346 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1347 sldns_write_uint16(rd, t); 1348 *len = 2; 1349 return LDNS_WIREPARSE_ERR_OK; 1350 } 1351 1352 int sldns_str2wire_class_buf(const char* str, uint8_t* rd, size_t* len) 1353 { 1354 uint16_t c = sldns_get_rr_class_by_name(str); 1355 if(c == 0 && strcmp(str, "CLASS0") != 0) 1356 return LDNS_WIREPARSE_ERR_SYNTAX_CLASS; 1357 if(*len < 2) 1358 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1359 sldns_write_uint16(rd, c); 1360 *len = 2; 1361 return LDNS_WIREPARSE_ERR_OK; 1362 } 1363 1364 /* An certificate alg field can either be specified as a 8 bits number 1365 * or by its symbolic name. Handle both */ 1366 int sldns_str2wire_cert_alg_buf(const char* str, uint8_t* rd, size_t* len) 1367 { 1368 sldns_lookup_table *lt = sldns_lookup_by_name(sldns_cert_algorithms, 1369 str); 1370 if(*len < 2) 1371 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1372 if(lt) { 1373 sldns_write_uint16(rd, (uint16_t)lt->id); 1374 } else { 1375 int s = sldns_str2wire_int16_buf(str, rd, len); 1376 if(s) return s; 1377 if(sldns_read_uint16(rd) == 0) 1378 return LDNS_WIREPARSE_ERR_CERT_BAD_ALGORITHM; 1379 } 1380 *len = 2; 1381 return LDNS_WIREPARSE_ERR_OK; 1382 } 1383 1384 /* An alg field can either be specified as a 8 bits number 1385 * or by its symbolic name. Handle both */ 1386 int sldns_str2wire_alg_buf(const char* str, uint8_t* rd, size_t* len) 1387 { 1388 sldns_lookup_table *lt = sldns_lookup_by_name(sldns_algorithms, str); 1389 if(*len < 1) 1390 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1391 if(lt) { 1392 rd[0] = (uint8_t)lt->id; 1393 *len = 1; 1394 } else { 1395 /* try as-is (a number) */ 1396 return sldns_str2wire_int8_buf(str, rd, len); 1397 } 1398 return LDNS_WIREPARSE_ERR_OK; 1399 } 1400 1401 int sldns_str2wire_tsigerror_buf(const char* str, uint8_t* rd, size_t* len) 1402 { 1403 sldns_lookup_table *lt = sldns_lookup_by_name(sldns_tsig_errors, str); 1404 if(*len < 2) 1405 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1406 if(lt) { 1407 sldns_write_uint16(rd, (uint16_t)lt->id); 1408 *len = 2; 1409 } else { 1410 /* try as-is (a number) */ 1411 return sldns_str2wire_int16_buf(str, rd, len); 1412 } 1413 return LDNS_WIREPARSE_ERR_OK; 1414 } 1415 1416 int sldns_str2wire_time_buf(const char* str, uint8_t* rd, size_t* len) 1417 { 1418 /* convert a time YYYYDDMMHHMMSS to wireformat */ 1419 struct tm tm; 1420 if(*len < 4) 1421 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1422 1423 /* Try to scan the time... */ 1424 memset(&tm, 0, sizeof(tm)); 1425 if (strlen(str) == 14 && sscanf(str, "%4d%2d%2d%2d%2d%2d", 1426 &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, 1427 &tm.tm_min, &tm.tm_sec) == 6) { 1428 tm.tm_year -= 1900; 1429 tm.tm_mon--; 1430 /* Check values */ 1431 if (tm.tm_year < 70) 1432 return LDNS_WIREPARSE_ERR_SYNTAX_TIME; 1433 if (tm.tm_mon < 0 || tm.tm_mon > 11) 1434 return LDNS_WIREPARSE_ERR_SYNTAX_TIME; 1435 if (tm.tm_mday < 1 || tm.tm_mday > 31) 1436 return LDNS_WIREPARSE_ERR_SYNTAX_TIME; 1437 if (tm.tm_hour < 0 || tm.tm_hour > 23) 1438 return LDNS_WIREPARSE_ERR_SYNTAX_TIME; 1439 if (tm.tm_min < 0 || tm.tm_min > 59) 1440 return LDNS_WIREPARSE_ERR_SYNTAX_TIME; 1441 if (tm.tm_sec < 0 || tm.tm_sec > 59) 1442 return LDNS_WIREPARSE_ERR_SYNTAX_TIME; 1443 1444 sldns_write_uint32(rd, (uint32_t)sldns_mktime_from_utc(&tm)); 1445 } else { 1446 /* handle it as 32 bits timestamp */ 1447 char *end; 1448 uint32_t l = (uint32_t)strtol((char*)str, &end, 10); 1449 if(*end != 0) 1450 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TIME, 1451 end-(char*)str); 1452 sldns_write_uint32(rd, l); 1453 } 1454 *len = 4; 1455 return LDNS_WIREPARSE_ERR_OK; 1456 } 1457 1458 int sldns_str2wire_tsigtime_buf(const char* str, uint8_t* rd, size_t* len) 1459 { 1460 char* end; 1461 uint64_t t = (uint64_t)strtol((char*)str, &end, 10); 1462 uint16_t high; 1463 uint32_t low; 1464 if(*end != 0) 1465 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TIME, end-str); 1466 if(*len < 6) 1467 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1468 high = (uint16_t)(t>>32); 1469 low = (uint32_t)(t); 1470 sldns_write_uint16(rd, high); 1471 sldns_write_uint32(rd+2, low); 1472 *len = 6; 1473 return LDNS_WIREPARSE_ERR_OK; 1474 } 1475 1476 int sldns_str2wire_period_buf(const char* str, uint8_t* rd, size_t* len) 1477 { 1478 const char* end; 1479 uint32_t p = sldns_str2period(str, &end); 1480 if(*end != 0) 1481 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_PERIOD, end-str); 1482 if(*len < 4) 1483 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1484 sldns_write_uint32(rd, p); 1485 *len = 4; 1486 return LDNS_WIREPARSE_ERR_OK; 1487 } 1488 1489 /** read "<digits>[.<digits>][mM]" into mantissa exponent format for LOC type */ 1490 static int 1491 loc_parse_cm(char* my_str, char** endstr, uint8_t* m, uint8_t* e) 1492 { 1493 uint32_t meters = 0, cm = 0, val; 1494 while (isblank((unsigned char)*my_str)) { 1495 my_str++; 1496 } 1497 meters = (uint32_t)strtol(my_str, &my_str, 10); 1498 if (*my_str == '.') { 1499 my_str++; 1500 cm = (uint32_t)strtol(my_str, &my_str, 10); 1501 } 1502 if (meters >= 1) { 1503 *e = 2; 1504 val = meters; 1505 } else { 1506 *e = 0; 1507 val = cm; 1508 } 1509 while(val >= 10) { 1510 (*e)++; 1511 val /= 10; 1512 } 1513 *m = (uint8_t)val; 1514 1515 if (*e > 9) 1516 return 0; 1517 if (*my_str == 'm' || *my_str == 'M') { 1518 my_str++; 1519 } 1520 *endstr = my_str; 1521 return 1; 1522 } 1523 1524 int sldns_str2wire_loc_buf(const char* str, uint8_t* rd, size_t* len) 1525 { 1526 uint32_t latitude = 0; 1527 uint32_t longitude = 0; 1528 uint32_t altitude = 0; 1529 1530 uint32_t equator = (uint32_t)1<<31; /* 2**31 */ 1531 1532 /* only support version 0 */ 1533 uint32_t h = 0; 1534 uint32_t m = 0; 1535 uint8_t size_b = 1, size_e = 2; 1536 uint8_t horiz_pre_b = 1, horiz_pre_e = 6; 1537 uint8_t vert_pre_b = 1, vert_pre_e = 3; 1538 1539 double s = 0.0; 1540 int northerness; 1541 int easterness; 1542 1543 char *my_str = (char *) str; 1544 1545 if (isdigit((unsigned char) *my_str)) { 1546 h = (uint32_t) strtol(my_str, &my_str, 10); 1547 } else { 1548 return LDNS_WIREPARSE_ERR_INVALID_STR; 1549 } 1550 1551 while (isblank((unsigned char) *my_str)) { 1552 my_str++; 1553 } 1554 1555 if (isdigit((unsigned char) *my_str)) { 1556 m = (uint32_t) strtol(my_str, &my_str, 10); 1557 } else if (*my_str == 'N' || *my_str == 'S') { 1558 goto north; 1559 } else { 1560 return LDNS_WIREPARSE_ERR_INVALID_STR; 1561 } 1562 1563 while (isblank((unsigned char) *my_str)) { 1564 my_str++; 1565 } 1566 1567 if (isdigit((unsigned char) *my_str)) { 1568 s = strtod(my_str, &my_str); 1569 } 1570 1571 /* skip blanks before northerness */ 1572 while (isblank((unsigned char) *my_str)) { 1573 my_str++; 1574 } 1575 1576 north: 1577 if (*my_str == 'N') { 1578 northerness = 1; 1579 } else if (*my_str == 'S') { 1580 northerness = 0; 1581 } else { 1582 return LDNS_WIREPARSE_ERR_INVALID_STR; 1583 } 1584 1585 my_str++; 1586 1587 /* store number */ 1588 s = 1000.0 * s; 1589 /* add a little to make floor in conversion a round */ 1590 s += 0.0005; 1591 latitude = (uint32_t) s; 1592 latitude += 1000 * 60 * m; 1593 latitude += 1000 * 60 * 60 * h; 1594 if (northerness) { 1595 latitude = equator + latitude; 1596 } else { 1597 latitude = equator - latitude; 1598 } 1599 while (isblank((unsigned char)*my_str)) { 1600 my_str++; 1601 } 1602 1603 if (isdigit((unsigned char) *my_str)) { 1604 h = (uint32_t) strtol(my_str, &my_str, 10); 1605 } else { 1606 return LDNS_WIREPARSE_ERR_INVALID_STR; 1607 } 1608 1609 while (isblank((unsigned char) *my_str)) { 1610 my_str++; 1611 } 1612 1613 if (isdigit((unsigned char) *my_str)) { 1614 m = (uint32_t) strtol(my_str, &my_str, 10); 1615 } else if (*my_str == 'E' || *my_str == 'W') { 1616 goto east; 1617 } else { 1618 return LDNS_WIREPARSE_ERR_INVALID_STR; 1619 } 1620 1621 while (isblank((unsigned char)*my_str)) { 1622 my_str++; 1623 } 1624 1625 if (isdigit((unsigned char) *my_str)) { 1626 s = strtod(my_str, &my_str); 1627 } 1628 1629 /* skip blanks before easterness */ 1630 while (isblank((unsigned char)*my_str)) { 1631 my_str++; 1632 } 1633 1634 east: 1635 if (*my_str == 'E') { 1636 easterness = 1; 1637 } else if (*my_str == 'W') { 1638 easterness = 0; 1639 } else { 1640 return LDNS_WIREPARSE_ERR_INVALID_STR; 1641 } 1642 1643 my_str++; 1644 1645 /* store number */ 1646 s *= 1000.0; 1647 /* add a little to make floor in conversion a round */ 1648 s += 0.0005; 1649 longitude = (uint32_t) s; 1650 longitude += 1000 * 60 * m; 1651 longitude += 1000 * 60 * 60 * h; 1652 1653 if (easterness) { 1654 longitude += equator; 1655 } else { 1656 longitude = equator - longitude; 1657 } 1658 1659 altitude = (uint32_t)(strtod(my_str, &my_str)*100.0 + 1660 10000000.0 + 0.5); 1661 if (*my_str == 'm' || *my_str == 'M') { 1662 my_str++; 1663 } 1664 1665 if (strlen(my_str) > 0) { 1666 if(!loc_parse_cm(my_str, &my_str, &size_b, &size_e)) 1667 return LDNS_WIREPARSE_ERR_INVALID_STR; 1668 } 1669 1670 if (strlen(my_str) > 0) { 1671 if(!loc_parse_cm(my_str, &my_str, &horiz_pre_b, &horiz_pre_e)) 1672 return LDNS_WIREPARSE_ERR_INVALID_STR; 1673 } 1674 1675 if (strlen(my_str) > 0) { 1676 if(!loc_parse_cm(my_str, &my_str, &vert_pre_b, &vert_pre_e)) 1677 return LDNS_WIREPARSE_ERR_INVALID_STR; 1678 } 1679 1680 if(*len < 16) 1681 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1682 rd[0] = 0; 1683 rd[1] = ((size_b << 4) & 0xf0) | (size_e & 0x0f); 1684 rd[2] = ((horiz_pre_b << 4) & 0xf0) | (horiz_pre_e & 0x0f); 1685 rd[3] = ((vert_pre_b << 4) & 0xf0) | (vert_pre_e & 0x0f); 1686 sldns_write_uint32(rd + 4, latitude); 1687 sldns_write_uint32(rd + 8, longitude); 1688 sldns_write_uint32(rd + 12, altitude); 1689 *len = 16; 1690 return LDNS_WIREPARSE_ERR_OK; 1691 } 1692 1693 static void 1694 ldns_tolower_str(char* s) 1695 { 1696 if(s) { 1697 while(*s) { 1698 *s = (char)tolower((unsigned char)*s); 1699 s++; 1700 } 1701 } 1702 } 1703 1704 int sldns_str2wire_wks_buf(const char* str, uint8_t* rd, size_t* len) 1705 { 1706 int rd_len = 1; 1707 int have_proto = 0; 1708 char token[50], proto_str[50]; 1709 sldns_buffer strbuf; 1710 sldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str)); 1711 proto_str[0]=0; 1712 1713 /* check we have one byte for proto */ 1714 if(*len < 1) 1715 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1716 1717 while(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) > 0) { 1718 ldns_tolower_str(token); 1719 if(!have_proto) { 1720 struct protoent *p = getprotobyname(token); 1721 have_proto = 1; 1722 if(p) rd[0] = (uint8_t)p->p_proto; 1723 else if(strcasecmp(token, "tcp")==0) rd[0]=6; 1724 else if(strcasecmp(token, "udp")==0) rd[0]=17; 1725 else rd[0] = (uint8_t)atoi(token); 1726 (void)strlcpy(proto_str, token, sizeof(proto_str)); 1727 } else { 1728 int serv_port; 1729 struct servent *serv = getservbyname(token, proto_str); 1730 if(serv) serv_port=(int)ntohs((uint16_t)serv->s_port); 1731 else if(strcasecmp(token, "domain")==0) serv_port=53; 1732 else { 1733 serv_port = atoi(token); 1734 if(serv_port == 0 && strcmp(token, "0") != 0) { 1735 #ifdef HAVE_ENDSERVENT 1736 endservent(); 1737 #endif 1738 #ifdef HAVE_ENDPROTOENT 1739 endprotoent(); 1740 #endif 1741 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX, 1742 sldns_buffer_position(&strbuf)); 1743 } 1744 if(serv_port < 0 || serv_port > 65535) { 1745 #ifdef HAVE_ENDSERVENT 1746 endservent(); 1747 #endif 1748 #ifdef HAVE_ENDPROTOENT 1749 endprotoent(); 1750 #endif 1751 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX, 1752 sldns_buffer_position(&strbuf)); 1753 } 1754 } 1755 if(rd_len < 1+serv_port/8+1) { 1756 /* bitmap is larger, init new bytes at 0 */ 1757 if(*len < 1+(size_t)serv_port/8+1) { 1758 #ifdef HAVE_ENDSERVENT 1759 endservent(); 1760 #endif 1761 #ifdef HAVE_ENDPROTOENT 1762 endprotoent(); 1763 #endif 1764 return RET_ERR( 1765 LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 1766 sldns_buffer_position(&strbuf)); 1767 } 1768 memset(rd+rd_len, 0, 1+(size_t)serv_port/8+1-rd_len); 1769 rd_len = 1+serv_port/8+1; 1770 } 1771 rd[1+ serv_port/8] |= (1 << (7 - serv_port % 8)); 1772 } 1773 } 1774 *len = (size_t)rd_len; 1775 1776 #ifdef HAVE_ENDSERVENT 1777 endservent(); 1778 #endif 1779 #ifdef HAVE_ENDPROTOENT 1780 endprotoent(); 1781 #endif 1782 return LDNS_WIREPARSE_ERR_OK; 1783 } 1784 1785 int sldns_str2wire_nsap_buf(const char* str, uint8_t* rd, size_t* len) 1786 { 1787 const char* s = str; 1788 size_t slen; 1789 size_t dlen = 0; /* number of hexdigits parsed */ 1790 1791 /* just a hex string with optional dots? */ 1792 if (s[0] != '0' || s[1] != 'x') 1793 return LDNS_WIREPARSE_ERR_INVALID_STR; 1794 s += 2; 1795 slen = strlen(s); 1796 if(slen > LDNS_MAX_RDFLEN*2) 1797 return LDNS_WIREPARSE_ERR_LABEL_OVERFLOW; 1798 while(*s) { 1799 if(isspace((unsigned char)*s) || *s == '.') { 1800 s++; 1801 continue; 1802 } 1803 if(!isxdigit((unsigned char)*s)) 1804 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str); 1805 if(*len < dlen/2 + 1) 1806 return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 1807 s-str); 1808 if((dlen&1)==0) 1809 rd[dlen/2] = (uint8_t)sldns_hexdigit_to_int(*s++) * 16; 1810 else rd[dlen/2] += sldns_hexdigit_to_int(*s++); 1811 dlen++; 1812 } 1813 if((dlen&1)!=0) 1814 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str); 1815 *len = dlen/2; 1816 return LDNS_WIREPARSE_ERR_OK; 1817 } 1818 1819 int sldns_str2wire_atma_buf(const char* str, uint8_t* rd, size_t* len) 1820 { 1821 const char* s = str; 1822 size_t slen = strlen(str); 1823 size_t dlen = 0; /* number of hexdigits parsed */ 1824 1825 /* just a hex string with optional dots? */ 1826 /* notimpl e.164 format */ 1827 if(slen > LDNS_MAX_RDFLEN*2) 1828 return LDNS_WIREPARSE_ERR_LABEL_OVERFLOW; 1829 while(*s) { 1830 if(isspace((unsigned char)*s) || *s == '.') { 1831 s++; 1832 continue; 1833 } 1834 if(!isxdigit((unsigned char)*s)) 1835 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str); 1836 if(*len < dlen/2 + 1) 1837 return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 1838 s-str); 1839 if((dlen&1)==0) 1840 rd[dlen/2] = (uint8_t)sldns_hexdigit_to_int(*s++) * 16; 1841 else rd[dlen/2] += sldns_hexdigit_to_int(*s++); 1842 dlen++; 1843 } 1844 if((dlen&1)!=0) 1845 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, s-str); 1846 *len = dlen/2; 1847 return LDNS_WIREPARSE_ERR_OK; 1848 } 1849 1850 int sldns_str2wire_ipseckey_buf(const char* str, uint8_t* rd, size_t* len) 1851 { 1852 size_t gwlen = 0, keylen = 0; 1853 int s; 1854 uint8_t gwtype; 1855 char token[512]; 1856 sldns_buffer strbuf; 1857 sldns_buffer_init_frm_data(&strbuf, (uint8_t*)str, strlen(str)); 1858 1859 if(*len < 3) 1860 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1861 /* precedence */ 1862 if(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0) 1863 return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR, 1864 sldns_buffer_position(&strbuf)); 1865 rd[0] = (uint8_t)atoi(token); 1866 /* gateway_type */ 1867 if(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0) 1868 return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR, 1869 sldns_buffer_position(&strbuf)); 1870 rd[1] = (uint8_t)atoi(token); 1871 gwtype = rd[1]; 1872 /* algorithm */ 1873 if(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0) 1874 return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR, 1875 sldns_buffer_position(&strbuf)); 1876 rd[2] = (uint8_t)atoi(token); 1877 1878 /* gateway */ 1879 if(sldns_bget_token(&strbuf, token, "\t\n ", sizeof(token)) <= 0) 1880 return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR, 1881 sldns_buffer_position(&strbuf)); 1882 if(gwtype == 0) { 1883 /* NOGATEWAY */ 1884 if(strcmp(token, ".") != 0) 1885 return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR, 1886 sldns_buffer_position(&strbuf)); 1887 gwlen = 0; 1888 } else if(gwtype == 1) { 1889 /* IP4 */ 1890 gwlen = *len - 3; 1891 s = sldns_str2wire_a_buf(token, rd+3, &gwlen); 1892 if(s) return RET_ERR_SHIFT(s, sldns_buffer_position(&strbuf)); 1893 } else if(gwtype == 2) { 1894 /* IP6 */ 1895 gwlen = *len - 3; 1896 s = sldns_str2wire_aaaa_buf(token, rd+3, &gwlen); 1897 if(s) return RET_ERR_SHIFT(s, sldns_buffer_position(&strbuf)); 1898 } else if(gwtype == 3) { 1899 /* DNAME */ 1900 gwlen = *len - 3; 1901 s = sldns_str2wire_dname_buf(token, rd+3, &gwlen); 1902 if(s) return RET_ERR_SHIFT(s, sldns_buffer_position(&strbuf)); 1903 } else { 1904 /* unknown gateway type */ 1905 return RET_ERR(LDNS_WIREPARSE_ERR_INVALID_STR, 1906 sldns_buffer_position(&strbuf)); 1907 } 1908 /* double check for size */ 1909 if(*len < 3 + gwlen) 1910 return RET_ERR(LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL, 1911 sldns_buffer_position(&strbuf)); 1912 1913 /* publickey in remainder of strbuf */ 1914 keylen = *len - 3 - gwlen; 1915 s = sldns_str2wire_b64_buf((const char*)sldns_buffer_current(&strbuf), 1916 rd+3+gwlen, &keylen); 1917 if(s) return RET_ERR_SHIFT(s, sldns_buffer_position(&strbuf)); 1918 1919 *len = 3 + gwlen + keylen; 1920 return LDNS_WIREPARSE_ERR_OK; 1921 } 1922 1923 int sldns_str2wire_nsec3_salt_buf(const char* str, uint8_t* rd, size_t* len) 1924 { 1925 int i, salt_length_str = (int)strlen(str); 1926 if (salt_length_str == 1 && str[0] == '-') { 1927 salt_length_str = 0; 1928 } else if (salt_length_str % 2 != 0) { 1929 return LDNS_WIREPARSE_ERR_SYNTAX_HEX; 1930 } 1931 if (salt_length_str > 512) 1932 return LDNS_WIREPARSE_ERR_SYNTAX_HEX; 1933 if(*len < 1+(size_t)salt_length_str / 2) 1934 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1935 rd[0] = (uint8_t) (salt_length_str / 2); 1936 for (i = 0; i < salt_length_str; i += 2) { 1937 if (isxdigit((unsigned char)str[i]) && 1938 isxdigit((unsigned char)str[i+1])) { 1939 rd[1+i/2] = (uint8_t)(sldns_hexdigit_to_int(str[i])*16 1940 + sldns_hexdigit_to_int(str[i+1])); 1941 } else { 1942 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_HEX, i); 1943 } 1944 } 1945 *len = 1 + (size_t)rd[0]; 1946 return LDNS_WIREPARSE_ERR_OK; 1947 } 1948 1949 int sldns_str2wire_ilnp64_buf(const char* str, uint8_t* rd, size_t* len) 1950 { 1951 unsigned int a, b, c, d; 1952 uint16_t shorts[4]; 1953 int l; 1954 if(*len < sizeof(shorts)) 1955 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1956 1957 if (sscanf(str, "%4x:%4x:%4x:%4x%n", &a, &b, &c, &d, &l) != 4 || 1958 l != (int)strlen(str) || /* more data to read */ 1959 strpbrk(str, "+-") /* signed hexes */ 1960 ) 1961 return LDNS_WIREPARSE_ERR_SYNTAX_ILNP64; 1962 shorts[0] = htons(a); 1963 shorts[1] = htons(b); 1964 shorts[2] = htons(c); 1965 shorts[3] = htons(d); 1966 memmove(rd, &shorts, sizeof(shorts)); 1967 *len = sizeof(shorts); 1968 return LDNS_WIREPARSE_ERR_OK; 1969 } 1970 1971 int sldns_str2wire_eui48_buf(const char* str, uint8_t* rd, size_t* len) 1972 { 1973 unsigned int a, b, c, d, e, f; 1974 int l; 1975 1976 if(*len < 6) 1977 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1978 if (sscanf(str, "%2x-%2x-%2x-%2x-%2x-%2x%n", 1979 &a, &b, &c, &d, &e, &f, &l) != 6 || 1980 l != (int)strlen(str)) 1981 return LDNS_WIREPARSE_ERR_SYNTAX_EUI48; 1982 rd[0] = a; 1983 rd[1] = b; 1984 rd[2] = c; 1985 rd[3] = d; 1986 rd[4] = e; 1987 rd[5] = f; 1988 *len = 6; 1989 return LDNS_WIREPARSE_ERR_OK; 1990 } 1991 1992 int sldns_str2wire_eui64_buf(const char* str, uint8_t* rd, size_t* len) 1993 { 1994 unsigned int a, b, c, d, e, f, g, h; 1995 int l; 1996 1997 if(*len < 8) 1998 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 1999 if (sscanf(str, "%2x-%2x-%2x-%2x-%2x-%2x-%2x-%2x%n", 2000 &a, &b, &c, &d, &e, &f, &g, &h, &l) != 8 || 2001 l != (int)strlen(str)) 2002 return LDNS_WIREPARSE_ERR_SYNTAX_EUI64; 2003 rd[0] = a; 2004 rd[1] = b; 2005 rd[2] = c; 2006 rd[3] = d; 2007 rd[4] = e; 2008 rd[5] = f; 2009 rd[6] = g; 2010 rd[7] = h; 2011 *len = 8; 2012 return LDNS_WIREPARSE_ERR_OK; 2013 } 2014 2015 int sldns_str2wire_tag_buf(const char* str, uint8_t* rd, size_t* len) 2016 { 2017 size_t slen = strlen(str); 2018 const char* ptr; 2019 2020 if (slen > 255) 2021 return LDNS_WIREPARSE_ERR_SYNTAX_TAG; 2022 if(*len < slen+1) 2023 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 2024 for (ptr = str; *ptr; ptr++) { 2025 if(!isalnum((unsigned char)*ptr)) 2026 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_TAG, ptr-str); 2027 } 2028 rd[0] = (uint8_t)slen; 2029 memmove(rd+1, str, slen); 2030 *len = slen+1; 2031 return LDNS_WIREPARSE_ERR_OK; 2032 } 2033 2034 int sldns_str2wire_long_str_buf(const char* str, uint8_t* rd, size_t* len) 2035 { 2036 uint8_t ch = 0; 2037 const char* pstr = str; 2038 size_t length = 0; 2039 2040 /* Fill data with parsed bytes */ 2041 while (sldns_parse_char(&ch, &pstr)) { 2042 if(*len < length+1) 2043 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 2044 rd[length++] = ch; 2045 } 2046 if(!pstr) 2047 return LDNS_WIREPARSE_ERR_SYNTAX_BAD_ESCAPE; 2048 *len = length; 2049 return LDNS_WIREPARSE_ERR_OK; 2050 } 2051 2052 int sldns_str2wire_hip_buf(const char* str, uint8_t* rd, size_t* len) 2053 { 2054 char* s, *end; 2055 int e; 2056 size_t hitlen, pklen = 0; 2057 /* presentation format: 2058 * pk-algo HIThex pubkeybase64 2059 * wireformat: 2060 * hitlen[1byte] pkalgo[1byte] pubkeylen[2byte] [hit] [pubkey] */ 2061 if(*len < 4) 2062 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 2063 2064 /* read PK algorithm */ 2065 rd[1] = (uint8_t)strtol((char*)str, &s, 10); 2066 if(*s != ' ') 2067 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, s-(char*)str); 2068 s++; 2069 while(*s == ' ') 2070 s++; 2071 2072 /* read HIT hex tag */ 2073 /* zero terminate the tag (replace later) */ 2074 end = strchr(s, ' '); 2075 if(!end) return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX, s-(char*)str); 2076 *end = 0; 2077 hitlen = *len - 4; 2078 if((e = sldns_str2wire_hex_buf(s, rd+4, &hitlen)) != 0) { 2079 *end = ' '; 2080 return RET_ERR_SHIFT(e, s-(char*)str); 2081 } 2082 if(hitlen > 255) { 2083 *end = ' '; 2084 return RET_ERR(LDNS_WIREPARSE_ERR_LABEL_OVERFLOW, s-(char*)str+255*2); 2085 } 2086 rd[0] = (uint8_t)hitlen; 2087 *end = ' '; 2088 s = end+1; 2089 2090 /* read pubkey base64 sequence */ 2091 pklen = *len - 4 - hitlen; 2092 if((e = sldns_str2wire_b64_buf(s, rd+4+hitlen, &pklen)) != 0) 2093 return RET_ERR_SHIFT(e, s-(char*)str); 2094 if(pklen > 65535) 2095 return RET_ERR(LDNS_WIREPARSE_ERR_LABEL_OVERFLOW, s-(char*)str+65535); 2096 sldns_write_uint16(rd+2, (uint16_t)pklen); 2097 2098 *len = 4 + hitlen + pklen; 2099 return LDNS_WIREPARSE_ERR_OK; 2100 } 2101 2102 int sldns_str2wire_int16_data_buf(const char* str, uint8_t* rd, size_t* len) 2103 { 2104 char* s; 2105 int n; 2106 n = strtol(str, &s, 10); 2107 if(n < 0) /* negative number not allowed */ 2108 return LDNS_WIREPARSE_ERR_SYNTAX; 2109 if(*len < ((size_t)n)+2) 2110 return LDNS_WIREPARSE_ERR_BUFFER_TOO_SMALL; 2111 if(n > 65535) 2112 return LDNS_WIREPARSE_ERR_LABEL_OVERFLOW; 2113 2114 if(n == 0) { 2115 sldns_write_uint16(rd, 0); 2116 *len = 2; 2117 return LDNS_WIREPARSE_ERR_OK; 2118 } 2119 if(*s != ' ') 2120 return RET_ERR(LDNS_WIREPARSE_ERR_SYNTAX_INT, s-(char*)str); 2121 s++; 2122 while(*s == ' ') 2123 s++; 2124 2125 n = sldns_b64_pton(s, rd+2, (*len)-2); 2126 if(n < 0) 2127 return LDNS_WIREPARSE_ERR_SYNTAX_B64; 2128 sldns_write_uint16(rd, (uint16_t)n); 2129 *len = ((size_t)n)+2; 2130 return LDNS_WIREPARSE_ERR_OK; 2131 } 2132