CONVERT(uint, uint32_t, 0, UINT32_MAX, src, dst);
}
+inline static int str_to_u64(const char *src, uint64_t *dst)
+{
+ CONVERT(uint, uint64_t, 0, UINT64_MAX, src, dst);
+}
+
inline static int str_to_size(const char *src, size_t *dst, size_t min, size_t max)
{
CONVERT(uint, size_t, min, max, src, dst);
#include <sys/stat.h>
#include "contrib/files.h"
+#include "contrib/strtonum.h"
#include "contrib/wire_ctx.h"
#include "knot/dnssec/key_records.h"
#include "knot/journal/serialization.h"
return (key->data + 1 + knot_dname_size(key_dname(key)));
}
+// returns zero time (= infinity) if failure!
+static knot_time_t key_time(const knot_db_val_t *key)
+{
+ uint64_t r = 0;
+ (void)str_to_u64(key_str(key), &r);
+ return r;
+}
+
static void free_key(knot_db_val_t *key)
{
free(key->data);
if ((it = db_api->iter_next(it)) != NULL && db_api->iter_key(it, &key) == KNOT_EOK) {
if (key_class(&key) == KASPDBKEY_OFFLINE_RECORDS &&
knot_dname_cmp(key_dname(&key), r->rrsig.owner) == 0) {
- *next_time = atol(key_str(&key));
+ *next_time = key_time(&key);
}
}
cleanup:
while (ret == KNOT_EOK && iter != NULL && (ret = db_api->iter_key(iter, &key)) == KNOT_EOK &&
key.len > TIME_STRLEN && key_class(&key) == KASPDBKEY_OFFLINE_RECORDS &&
- knot_time_cmp(atol(key_str(&key)), to_time) <= 0 &&
+ knot_time_cmp(key_time(&key), to_time) <= 0 &&
knot_dname_cmp(key_dname(&key), zone) == 0) {
ret = knot_db_lmdb_iter_del(iter);
iter = db_api->iter_next(iter);
#include "utils/keymgr/bind_privkey.h"
#include "contrib/base64.h"
#include "contrib/ctype.h"
+#include "contrib/strtonum.h"
#include "contrib/tolower.h"
#include "contrib/wire_ctx.h"
#include "libdnssec/error.h"
// parse args
for (int i = 0; i < argc; i++) {
if (!just_timing && strncasecmp(argv[i], "algorithm=", 10) == 0) {
- if (is_digit(argv[i][10]) && atol(argv[i] + 10) < 256) {
- *algorithm = atol(argv[i] + 10);
- continue;
- }
- int al;
- for (al = 0; al < 256; al++) {
+ int alg = 256; // invalid value
+ (void)str_to_int(argv[i] + 10, &alg, 0, 255);
+ for (int al = 0; al < 256 && alg > 255; al++) {
if (algnames[al] != NULL &&
strcasecmp(argv[i] + 10, algnames[al]) == 0) {
- *algorithm = al;
- break;
+ alg = al;
}
}
- if (al == 256) {
+ if (alg > 255) {
printf("Unknown algorithm: %s\n", argv[i] + 10);
return false;
}
+ *algorithm = alg;
} else if (strncasecmp(argv[i], "ksk=", 4) == 0) {
bitmap_set(flags, DNSKEY_GENERATE_KSK, str2bool(argv[i] + 4));
} else if (strncasecmp(argv[i], "zsk=", 4) == 0) {
bitmap_set(flags, DNSKEY_GENERATE_SEP_SPEC, true);
bitmap_set(flags, DNSKEY_GENERATE_SEP_ON, str2bool(argv[i] + 4));
} else if (!just_timing && strncasecmp(argv[i], "size=", 5) == 0) {
- *keysize = atol(argv[i] + 5);
+ if (str_to_u16(argv[i] + 5, keysize) != KNOT_EOK) {
+ printf("Invalid size: '%s'\n", argv[i] + 5);
+ return false;
+ }
} else if (!just_timing && strncasecmp(argv[i], "addtopolicy=", 12) == 0) {
*addtopolicy = argv[i] + 12;
} else if (!init_timestamps(argv[i], timing)) {
return KNOT_EOK;
}
-static long is_uint32(const char *string)
-{
- if (*string == '\0') {
- return -1;
- }
- for (const char *p = string; *p != '\0'; p++) {
- if (!is_digit(*p)) {
- return -1;
- }
- }
- long res = atol(string);
- return (res <= UINT32_MAX ? res : -1);
-}
-
static bool is_hex(const char *string)
{
for (const char *p = string; *p != '\0'; p++) {
int keymgr_get_key(kdnssec_ctx_t *ctx, const char *key_spec, knot_kasp_key_t **key)
{
- long spec_tag = is_uint32(key_spec), spec_len = strlen(key_spec);
- if (spec_tag < 0 && !is_hex(key_spec)) {
+ uint16_t keytag;
+ bool has_keytag = (str_to_u16(key_spec, &keytag) == KNOT_EOK);
+ long spec_len = strlen(key_spec);
+ if (!has_keytag && !is_hex(key_spec)) {
printf("Error in key specification.\n");
return KNOT_EINVAL;
}
*key = NULL;
for (size_t i = 0; i < ctx->zone->num_keys; i++) {
knot_kasp_key_t *candidate = &ctx->zone->keys[i];
- if ((spec_tag >= 0 && dnssec_key_get_keytag(candidate->key) == spec_tag) ||
- (spec_tag < 0 && strncmp(candidate->id, key_spec, spec_len) == 0)) {
+ if ((has_keytag && dnssec_key_get_keytag(candidate->key) == keytag) ||
+ (!has_keytag && strncmp(candidate->id, key_spec, spec_len) == 0)) {
if (*key == NULL) {
*key = candidate;
}
#include <unistd.h>
#include "contrib/string.h"
+#include "contrib/strtonum.h"
#include "knot/conf/conf.h"
#include "knot/dnssec/zone-keys.h"
#include "libknot/libknot.h"
{ NULL }
};
- int opt = 0;
+ int opt = 0, parm = 0;
while ((opt = getopt_long(argc, argv, "hVd:c:C:t:", opts, NULL)) != -1) {
switch (opt) {
case 'h':
}
break;
case 't':
- ret = keymgr_generate_tsig(optarg, (argc > optind ? argv[optind] : "hmac-sha256"),
- (argc > optind + 1 ? atol(argv[optind + 1]) : 0));
+ if (argc > optind + 1) {
+ (void)str_to_int(argv[optind + 1], &parm, 0, 65536);
+ }
+ ret = keymgr_generate_tsig(optarg, (argc > optind ? argv[optind] : "hmac-sha256"), parm);
if (ret != KNOT_EOK) {
printf("Failed to generate TSIG (%s)\n", knot_strerror(ret));
}