From 8e4da48fa7df8fb35f67c6301dd153583db848bf Mon Sep 17 00:00:00 2001 From: Brian Wellington Date: Wed, 9 May 2001 23:04:50 +0000 Subject: [PATCH] consolidated duplicated code for filename mangling, removed sanity checks that weren't. --- lib/dns/sec/dst/dst_api.c | 43 +++++++++++++++++++++++----------- lib/dns/sec/dst/dst_internal.h | 9 ++++++- lib/dns/sec/dst/dst_parse.c | 24 +++++++------------ 3 files changed, 46 insertions(+), 30 deletions(-) diff --git a/lib/dns/sec/dst/dst_api.c b/lib/dns/sec/dst/dst_api.c index a888c7ba4d..5cab6c6179 100644 --- a/lib/dns/sec/dst/dst_api.c +++ b/lib/dns/sec/dst/dst_api.c @@ -19,7 +19,7 @@ /* * Principal Author: Brian Wellington - * $Id: dst_api.c,v 1.78 2001/05/04 17:57:33 gson Exp $ + * $Id: dst_api.c,v 1.79 2001/05/09 23:04:47 bwelling Exp $ */ #include @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -791,23 +792,17 @@ read_public_key(const char *filename, isc_mem_t *mctx, dst_key_t **keyp) { dns_rdata_t rdata = DNS_RDATA_INIT; unsigned int opt = ISC_LEXOPT_DNSMULTILINE; char *newfilename; + unsigned int newfilenamelen; isc_textregion_t r; dns_rdataclass_t rdclass = dns_rdataclass_in; - if (strlen(filename) < 8) - return (DST_R_INVALIDPUBLICKEY); - - newfilename = isc_mem_get(mctx, strlen(filename) + 5); + newfilenamelen = strlen(filename) + 5; + newfilename = isc_mem_get(mctx, newfilenamelen); if (newfilename == NULL) return (ISC_R_NOMEMORY); - strcpy(newfilename, filename); - - if (strcmp(filename + strlen(filename) - 8, ".private") == 0) - sprintf(newfilename + strlen(filename) - 8, ".key"); - else if (strcmp(filename + strlen(filename) - 1, ".") == 0) - sprintf(newfilename + strlen(filename), "key"); - else if (strcmp(filename + strlen(filename) - 4, ".key") != 0) - sprintf(newfilename + strlen(filename), ".key"); + ret = dst__file_addsuffix(newfilename, newfilenamelen, filename, + ".key"); + INSIST(ret == ISC_R_SUCCESS); /* * Open the file and read its formatted contents @@ -886,7 +881,7 @@ read_public_key(const char *filename, isc_mem_t *mctx, dst_key_t **keyp) { isc_lex_close(lex); isc_lex_destroy(&lex); } - isc_mem_put(mctx, newfilename, strlen(filename) + 5); + isc_mem_put(mctx, newfilename, newfilenamelen); return (ret); } @@ -1057,6 +1052,26 @@ frombuffer(dns_name_t *name, const unsigned int alg, const unsigned int flags, return (ISC_R_SUCCESS); } +isc_result_t +dst__file_addsuffix(char *filename, unsigned int len, + const char *ofilename, const char *suffix) +{ + unsigned int olen = strlen(ofilename); + int n; + + if (olen > 1 && ofilename[olen - 1] == '.') + olen -= 1; + else if (olen > 8 && strcmp(ofilename + olen - 8, ".private") == 0) + olen -= 8; + else if (olen > 4 && strcmp(ofilename + olen - 8, ".key") == 0) + olen -= 4; + + n = snprintf(filename, len, "%.*s%s", olen, ofilename, suffix); + if (n < 0) + return (ISC_R_NOSPACE); + return (ISC_R_SUCCESS); +} + void * dst__mem_alloc(size_t size) { INSIST(dst_memory_pool != NULL); diff --git a/lib/dns/sec/dst/dst_internal.h b/lib/dns/sec/dst/dst_internal.h index eb30432e6c..34eb016719 100644 --- a/lib/dns/sec/dst/dst_internal.h +++ b/lib/dns/sec/dst/dst_internal.h @@ -17,7 +17,7 @@ * PERFORMANCE OF THIS SOFTWARE. */ -/* $Id: dst_internal.h,v 1.34 2001/01/24 02:22:53 bwelling Exp $ */ +/* $Id: dst_internal.h,v 1.35 2001/05/09 23:04:49 bwelling Exp $ */ #ifndef DST_DST_INTERNAL_H #define DST_DST_INTERNAL_H 1 @@ -133,6 +133,13 @@ void * dst__mem_realloc(void *ptr, size_t size); isc_result_t dst__entropy_getdata(void *buf, unsigned int len, isc_boolean_t pseudo); +/* + * Generic helper functions. + */ +isc_result_t +dst__file_addsuffix(char *filename, unsigned int len, + const char *ofilename, const char *suffix); + ISC_LANG_ENDDECLS #endif /* DST_DST_INTERNAL_H */ diff --git a/lib/dns/sec/dst/dst_parse.c b/lib/dns/sec/dst/dst_parse.c index 0b613c6c5f..dca911ec1f 100644 --- a/lib/dns/sec/dst/dst_parse.c +++ b/lib/dns/sec/dst/dst_parse.c @@ -19,7 +19,7 @@ /* * Principal Author: Brian Wellington - * $Id: dst_parse.c,v 1.28 2001/01/24 02:22:54 bwelling Exp $ + * $Id: dst_parse.c,v 1.29 2001/05/09 23:04:50 bwelling Exp $ */ #include @@ -195,24 +195,18 @@ dst__privstruct_parsefile(dst_key_t *key, const dns_keytag_t id, isc_token_t token; unsigned int opt = ISC_LEXOPT_EOL; char *newfilename; + int newfilenamelen; isc_result_t ret; REQUIRE(priv != NULL); - if (strlen(filename) < 8) - return (DST_R_INVALIDPRIVATEKEY); - - newfilename = isc_mem_get(mctx, strlen(filename) + 9); + newfilenamelen = strlen(filename) + 9; + newfilename = isc_mem_get(mctx, newfilenamelen); if (newfilename == NULL) return (ISC_R_NOMEMORY); - strcpy(newfilename, filename); - - if (strcmp(filename + strlen(filename) - 4, ".key") == 0) - sprintf(newfilename + strlen(filename) - 4, ".private"); - else if (strcmp(filename + strlen(filename) - 1, ".") == 0) - sprintf(newfilename + strlen(filename), "private"); - else if (strcmp(filename + strlen(filename) - 8, ".private") != 0) - sprintf(newfilename + strlen(filename), ".private"); + ret = dst__file_addsuffix(newfilename, newfilenamelen, filename, + ".private"); + INSIST(ret == ISC_R_SUCCESS); priv->nelements = 0; @@ -338,7 +332,7 @@ dst__privstruct_parsefile(dst_key_t *key, const dns_keytag_t id, isc_lex_close(lex); isc_lex_destroy(&lex); - isc_mem_put(mctx, newfilename, strlen(filename) + 9); + isc_mem_put(mctx, newfilename, newfilenamelen); return (ISC_R_SUCCESS); @@ -347,7 +341,7 @@ fail: isc_lex_close(lex); isc_lex_destroy(&lex); } - isc_mem_put(mctx, newfilename, strlen(filename) + 9); + isc_mem_put(mctx, newfilename, newfilenamelen); priv->nelements = n; dst__privstruct_free(priv, mctx);