From 241cf78fee9eeb52c87e044e94bb76ec4f2cf82e Mon Sep 17 00:00:00 2001 From: Evan Hunt Date: Sat, 20 Jul 2019 17:24:41 -0400 Subject: [PATCH] add "delv +yaml" output format --- bin/delv/delv.c | 141 +++++++++++++++++++++++++++++++------------ lib/dns/masterdump.c | 48 +++++++++------ 2 files changed, 130 insertions(+), 59 deletions(-) diff --git a/bin/delv/delv.c b/bin/delv/delv.c index 0411abc7e4..78e0d15d8d 100644 --- a/bin/delv/delv.c +++ b/bin/delv/delv.c @@ -111,7 +111,8 @@ static bool nottl = false, multiline = false, short_form = false, - print_unknown_format = false; + print_unknown_format = false, + yaml = false; static bool resolve_trace = false, @@ -353,52 +354,80 @@ setup_logging(FILE *errout) { static void print_status(dns_rdataset_t *rdataset) { - const char *astr = "", *tstr = ""; + char buf[1024] = { 0 }; REQUIRE(rdataset != NULL); - if (!showtrust || !dns_rdataset_isassociated(rdataset)) + if (!showtrust || !dns_rdataset_isassociated(rdataset)) { return; + } - if ((rdataset->attributes & DNS_RDATASETATTR_NEGATIVE) != 0) - astr = "negative response, "; + buf[0] = '\0'; + + if ((rdataset->attributes & DNS_RDATASETATTR_NEGATIVE) != 0) { + strlcat(buf, "negative response", sizeof(buf)); + strlcat(buf, (yaml ? "_" : ", "), sizeof(buf)); + } switch (rdataset->trust) { case dns_trust_none: - tstr = "untrusted"; + strlcat(buf, "untrusted", sizeof(buf)); break; case dns_trust_pending_additional: - tstr = "signed additional data, pending validation"; + strlcat(buf, "signed additional data", sizeof(buf)); + if (!yaml) { + strlcat(buf, ", ", sizeof(buf)); + } + strlcat(buf, "pending validation", sizeof(buf)); break; case dns_trust_pending_answer: - tstr = "signed answer, pending validation"; + strlcat(buf, "signed answer", sizeof(buf)); + if (!yaml) { + strlcat(buf, ", ", sizeof(buf)); + } + strlcat(buf, "pending validation", sizeof(buf)); break; case dns_trust_additional: - tstr = "unsigned additional data"; + strlcat(buf, "unsigned additional data", sizeof(buf)); break; case dns_trust_glue: - tstr = "glue data"; + strlcat(buf, "glue data", sizeof(buf)); break; case dns_trust_answer: if (root_validation) { - tstr = "unsigned answer"; + strlcat(buf, "unsigned answer", sizeof(buf)); + } else { + strlcat(buf, "answer not validated", sizeof(buf)); } break; case dns_trust_authauthority: - tstr = "authority data"; + strlcat(buf, "authority data", sizeof(buf)); break; case dns_trust_authanswer: - tstr = "authoritative"; + strlcat(buf, "authoritative", sizeof(buf)); break; case dns_trust_secure: - tstr = "fully validated"; + strlcat(buf, "fully validated", sizeof(buf)); break; case dns_trust_ultimate: - tstr = "ultimate trust"; + strlcat(buf, "ultimate trust", sizeof(buf)); break; } - printf("; %s%s\n", astr, tstr); + if (yaml) { + char *p; + + /* Convert spaces to underscores for YAML */ + for (p = buf; p != NULL && *p != '\0'; p++) { + if (*p == ' ') { + *p = '_'; + } + } + + printf(" - %s:\n", buf); + } else { + printf("; %s\n", buf); + } } static isc_result_t @@ -425,8 +454,9 @@ printdata(dns_rdataset_t *rdataset, dns_name_t *owner, return (ISC_R_SUCCESS); if (first || rdataset->trust != trust) { - if (!first && showtrust && !short_form) + if (!first && showtrust && !short_form && !yaml) { putchar('\n'); + } print_status(rdataset); trust = rdataset->trust; first = false; @@ -465,9 +495,11 @@ printdata(dns_rdataset_t *rdataset, dns_name_t *owner, dns_rdata_reset(&rdata); } } else { - if ((rdataset->attributes & - DNS_RDATASETATTR_NEGATIVE) != 0) + if (!yaml && (rdataset->attributes & + DNS_RDATASETATTR_NEGATIVE) != 0) + { isc_buffer_putstr(&target, "; "); + } result = dns_master_rdatasettotext(owner, rdataset, style, &target); @@ -500,38 +532,52 @@ setup_style(dns_master_style_t **stylep) { REQUIRE(stylep != NULL || *stylep == NULL); styleflags |= DNS_STYLEFLAG_REL_OWNER; - if (showcomments) - styleflags |= DNS_STYLEFLAG_COMMENT; - if (print_unknown_format) - styleflags |= DNS_STYLEFLAG_UNKNOWNFORMAT; - if (rrcomments) - styleflags |= DNS_STYLEFLAG_RRCOMMENT; - if (nottl) - styleflags |= DNS_STYLEFLAG_NO_TTL; - if (noclass) - styleflags |= DNS_STYLEFLAG_NO_CLASS; - if (nocrypto) - styleflags |= DNS_STYLEFLAG_NOCRYPTO; - if (multiline) { - styleflags |= DNS_STYLEFLAG_MULTILINE; - styleflags |= DNS_STYLEFLAG_COMMENT; + if (yaml) { + styleflags |= DNS_STYLEFLAG_YAML; + dns_master_indentstr = " "; + dns_master_indent = 2; + } else { + if (showcomments) { + styleflags |= DNS_STYLEFLAG_COMMENT; + } + if (print_unknown_format) { + styleflags |= DNS_STYLEFLAG_UNKNOWNFORMAT; + } + if (rrcomments) { + styleflags |= DNS_STYLEFLAG_RRCOMMENT; + } + if (nottl) { + styleflags |= DNS_STYLEFLAG_NO_TTL; + } + if (noclass) { + styleflags |= DNS_STYLEFLAG_NO_CLASS; + } + if (nocrypto) { + styleflags |= DNS_STYLEFLAG_NOCRYPTO; + } + if (multiline) { + styleflags |= DNS_STYLEFLAG_MULTILINE; + styleflags |= DNS_STYLEFLAG_COMMENT; + } } - if (multiline || (nottl && noclass)) + if (multiline || (nottl && noclass)) { result = dns_master_stylecreate(&style, styleflags, 24, 24, 24, 32, 80, 8, splitwidth, mctx); - else if (nottl || noclass) + } else if (nottl || noclass) { result = dns_master_stylecreate(&style, styleflags, 24, 24, 32, 40, 80, 8, splitwidth, mctx); - else + } else { result = dns_master_stylecreate(&style, styleflags, 24, 32, 40, 48, 80, 8, splitwidth, mctx); + } - if (result == ISC_R_SUCCESS) + if (result == ISC_R_SUCCESS) { *stylep = style; + } return (result); } @@ -1139,6 +1185,13 @@ plus_option(char *option) { if (state) resolve_trace = state; break; + case 'y': /* yaml */ + FULLCHECK("yaml"); + yaml = state; + if (state) { + rrcomments = false; + } + break; default: invalid_option: /* @@ -1565,6 +1618,7 @@ main(int argc, char *argv[]) { isc_result_t result; dns_fixedname_t qfn; dns_name_t *query_name, *response_name; + char namestr[DNS_NAME_FORMATSIZE]; dns_rdataset_t *rdataset; dns_namelist_t namelist; unsigned int resopt, clopt; @@ -1653,9 +1707,18 @@ main(int argc, char *argv[]) { ISC_LIST_INIT(namelist); result = dns_client_resolve(client, query_name, dns_rdataclass_in, qtype, resopt, &namelist); - if (result != ISC_R_SUCCESS) + if (result != ISC_R_SUCCESS && !yaml) { delv_log(ISC_LOG_ERROR, "resolution failed: %s", isc_result_totext(result)); + } + + if (yaml) { + printf("type: DELV_RESULT\n"); + dns_name_format(query_name, namestr, sizeof(namestr)); + printf("query_name: %s\n", namestr); + printf("status: %s\n", isc_result_totext(result)); + printf("records:\n"); + } for (response_name = ISC_LIST_HEAD(namelist); response_name != NULL; diff --git a/lib/dns/masterdump.c b/lib/dns/masterdump.c index 2de16eec4b..7a80bf274a 100644 --- a/lib/dns/masterdump.c +++ b/lib/dns/masterdump.c @@ -427,7 +427,7 @@ str_totext(const char *source, isc_buffer_t *target) { static isc_result_t ncache_summary(dns_rdataset_t *rdataset, bool omit_final_dot, - isc_buffer_t *target) + dns_totext_ctx_t *ctx, isc_buffer_t *target) { isc_result_t result = ISC_R_SUCCESS; dns_rdataset_t rds; @@ -441,7 +441,22 @@ ncache_summary(dns_rdataset_t *rdataset, bool omit_final_dot, for (result = dns_rdataset_first(&rds); result == ISC_R_SUCCESS; result = dns_rdataset_next(&rds)) { - CHECK(str_totext("; ", target)); + if ((ctx->style.flags & DNS_STYLEFLAG_INDENT) != 0 || + (ctx->style.flags & DNS_STYLEFLAG_YAML) != 0) + { + unsigned int i; + for (i = 0; i < dns_master_indent; i++) { + CHECK(str_totext(dns_master_indentstr, + target)); + } + } + + if ((ctx->style.flags & DNS_STYLEFLAG_YAML) != 0) { + CHECK(str_totext("- ", target)); + } else { + CHECK(str_totext("; ", target)); + } + CHECK(dns_name_totext(&name, omit_final_dot, target)); CHECK(str_totext(" ", target)); CHECK(dns_rdatatype_totext(rds.type, target)); @@ -518,22 +533,21 @@ rdataset_totext(dns_rdataset_t *rdataset, */ if ((ctx->style.flags & DNS_STYLEFLAG_INDENT) != 0 || (ctx->style.flags & DNS_STYLEFLAG_YAML) != 0) + { for (i = 0; i < dns_master_indent; i++) RETERR(str_totext(dns_master_indentstr, target)); - - /* - * YAML enumerator? - */ - if ((ctx->style.flags & DNS_STYLEFLAG_YAML) != 0) { - RETERR(str_totext("- ", target)); } /* - * Comment? + * YAML or comment prefix? */ - if ((ctx->style.flags & DNS_STYLEFLAG_COMMENTDATA) != 0) + if ((ctx->style.flags & DNS_STYLEFLAG_YAML) != 0) { + RETERR(str_totext("- ", target)); + } else if ((ctx->style.flags & DNS_STYLEFLAG_COMMENTDATA) != 0) + { RETERR(str_totext(";", target)); + } /* * Owner name. @@ -651,23 +665,17 @@ rdataset_totext(dns_rdataset_t *rdataset, */ INDENT_TO(rdata_column); if ((rdataset->attributes & DNS_RDATASETATTR_NEGATIVE) != 0) { - if ((ctx->style.flags & DNS_STYLEFLAG_INDENT) != 0 || - (ctx->style.flags & DNS_STYLEFLAG_YAML) != 0) - { - for (i = 0; i < dns_master_indent; i++) - RETERR(str_totext(dns_master_indentstr, - target)); - } - if (NXDOMAIN(rdataset)) + if (NXDOMAIN(rdataset)) { RETERR(str_totext(";-$NXDOMAIN\n", target)); - else + } else { RETERR(str_totext(";-$NXRRSET\n", target)); + } /* * Print a summary of the cached records which make * up the negative response. */ RETERR(ncache_summary(rdataset, omit_final_dot, - target)); + ctx, target)); break; } else { dns_rdata_t rdata = DNS_RDATA_INIT;