/** * Read a file with trust anchors * @param anchors: anchor storage. * @param buffer: parsing buffer. * @param fname: string. * @param onlyone: only one trust anchor allowed in file. * @return NULL on error. Else last trust-anchor point. */ static struct trust_anchor* anchor_read_file(struct val_anchors* anchors, sldns_buffer* buffer, const char* fname, int onlyone) { struct trust_anchor* ta = NULL, *tanew; struct sldns_file_parse_state pst; int status; size_t len, dname_len; uint8_t* rr = sldns_buffer_begin(buffer); int ok = 1; FILE* in = fopen(fname, "r"); if(!in) { log_err("error opening file %s: %s", fname, strerror(errno)); return 0; } memset(&pst, 0, sizeof(pst)); pst.default_ttl = 3600; pst.lineno = 1; while(!feof(in)) { len = sldns_buffer_capacity(buffer); dname_len = 0; status = sldns_fp2wire_rr_buf(in, rr, &len, &dname_len, &pst); if(len == 0) /* empty, $TTL, $ORIGIN */ continue; if(status != 0) { log_err("parse error in %s:%d:%d : %s", fname, pst.lineno, LDNS_WIREPARSE_OFFSET(status), sldns_get_errorstr_parse(status)); ok = 0; break; } if(sldns_wirerr_get_type(rr, len, dname_len) != LDNS_RR_TYPE_DS && sldns_wirerr_get_type(rr, len, dname_len) != LDNS_RR_TYPE_DNSKEY) { continue; } if(!(tanew=anchor_store_new_rr(anchors, rr, len, dname_len))) { log_err("mem error at %s line %d", fname, pst.lineno); ok = 0; break; } if(onlyone && ta && ta != tanew) { log_err("error at %s line %d: no multiple anchor " "domains allowed (you can have multiple " "keys, but they must have the same name).", fname, pst.lineno); ok = 0; break; } ta = tanew; } fclose(in); if(!ok) return NULL; /* empty file is OK when multiple anchors are allowed */ if(!onlyone && !ta) return (struct trust_anchor*)1; return ta; }
struct trust_anchor* anchor_store_str(struct val_anchors* anchors, ldns_buffer* buffer, const char* str) { struct trust_anchor* ta; ldns_rr* rr = NULL; ldns_status status = ldns_rr_new_frm_str(&rr, str, 0, NULL, NULL); if(status != LDNS_STATUS_OK) { log_err("error parsing trust anchor: %s", ldns_get_errorstr_by_id(status)); ldns_rr_free(rr); return NULL; } if(!(ta=anchor_store_new_rr(anchors, buffer, rr))) { log_err("out of memory"); ldns_rr_free(rr); return NULL; } ldns_rr_free(rr); return ta; }
struct trust_anchor* anchor_store_str(struct val_anchors* anchors, sldns_buffer* buffer, const char* str) { struct trust_anchor* ta; uint8_t* rr = sldns_buffer_begin(buffer); size_t len = sldns_buffer_capacity(buffer), dname_len = 0; int status = sldns_str2wire_rr_buf(str, rr, &len, &dname_len, 0, NULL, 0, NULL, 0); if(status != 0) { log_err("error parsing trust anchor %s: at %d: %s", str, LDNS_WIREPARSE_OFFSET(status), sldns_get_errorstr_parse(status)); return NULL; } if(!(ta=anchor_store_new_rr(anchors, rr, len, dname_len))) { log_err("out of memory"); return NULL; } return ta; }
/** * Read a file with trust anchors * @param anchors: anchor storage. * @param buffer: parsing buffer. * @param fname: string. * @param onlyone: only one trust anchor allowed in file. * @return NULL on error. Else last trust-anchor point. */ static struct trust_anchor* anchor_read_file(struct val_anchors* anchors, ldns_buffer* buffer, const char* fname, int onlyone) { struct trust_anchor* ta = NULL, *tanew; uint32_t default_ttl = 3600; ldns_rdf* origin = NULL, *prev = NULL; int line_nr = 1; ldns_status status; ldns_rr* rr; int ok = 1; FILE* in = fopen(fname, "r"); if(!in) { log_err("error opening file %s: %s", fname, strerror(errno)); return 0; } while(!feof(in)) { rr = NULL; status = ldns_rr_new_frm_fp_l(&rr, in, &default_ttl, &origin, &prev, &line_nr); if(status == LDNS_STATUS_SYNTAX_EMPTY /* empty line */ || status == LDNS_STATUS_SYNTAX_TTL /* $TTL */ || status == LDNS_STATUS_SYNTAX_ORIGIN /* $ORIGIN */) continue; if(status != LDNS_STATUS_OK) { log_err("parse error in %s:%d : %s", fname, line_nr, ldns_get_errorstr_by_id(status)); ldns_rr_free(rr); ok = 0; break; } if(ldns_rr_get_type(rr) != LDNS_RR_TYPE_DS && ldns_rr_get_type(rr) != LDNS_RR_TYPE_DNSKEY) { ldns_rr_free(rr); continue; } if(!(tanew=anchor_store_new_rr(anchors, buffer, rr))) { log_err("error at %s line %d", fname, line_nr); ldns_rr_free(rr); ok = 0; break; } if(onlyone && ta && ta != tanew) { log_err("error at %s line %d: no multiple anchor " "domains allowed (you can have multiple " "keys, but they must have the same name).", fname, line_nr); ldns_rr_free(rr); ok = 0; break; } ta = tanew; ldns_rr_free(rr); } ldns_rdf_deep_free(origin); ldns_rdf_deep_free(prev); fclose(in); if(!ok) return NULL; /* empty file is OK when multiple anchors are allowed */ if(!onlyone && !ta) return (struct trust_anchor*)1; return ta; }