From c4316262faf2b5330ecea8e7e81095821c2eb8e2 Mon Sep 17 00:00:00 2001 From: Filip Matusiak <filip.matusiak@iopsys.eu> Date: Thu, 5 Jun 2025 11:31:48 +0200 Subject: [PATCH] wsc: Add function to read WSC M2 md5sum from file --- src/autoconfig.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++-- src/autoconfig.h | 1 + 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/src/autoconfig.c b/src/autoconfig.c index 9a4d8939d..4709d767f 100644 --- a/src/autoconfig.c +++ b/src/autoconfig.c @@ -283,11 +283,12 @@ int autoconfig_write_hash_to_file(uint8_t *macaddr, uint8_t *sha256) } /* Create sha256 string */ - sha256_str = calloc(sizeof(sha256) * 2 + 1, sizeof(char)); + sha256_str = calloc(SHA256_LENGTH * 2 + 1, sizeof(char)); if (!sha256_str) goto out_radio; - btostr(sha256, sizeof(sha256), sha256_str); + btostr(sha256, SHA256_LENGTH, sha256_str); + sha256_obj = json_object_new_string(sha256_str); if (!sha256_obj) { json_object_put(radio_obj); @@ -312,3 +313,77 @@ out_radio: return 0; } + +int autoconfig_read_hash_from_file(uint8_t *macaddr, uint8_t *sha256_out) +{ + struct blob_buf radios = { 0 }; + struct blob_attr *b; + static const struct blobmsg_policy attr[] = { + [0] = { .name = "radios", .type = BLOBMSG_TYPE_ARRAY }, + }; + struct blob_attr *tb[ARRAY_SIZE(attr)]; + int rem; + int ret = 0; + + blob_buf_init(&radios, 0); + + if (!blobmsg_add_json_from_file(&radios, AUTOCFG_FILE)) { + agnt_dbg(LOG_APCFG, "Failed to parse %s\n", AUTOCFG_FILE); + ret = -1; + goto out; + } + + blobmsg_parse(attr, ARRAY_SIZE(attr), tb, blob_data(radios.head), blob_len(radios.head)); + + if (!tb[0]) { + ret = -1; + goto out; + } + + blobmsg_for_each_attr(b, tb[0], rem) { + static const struct blobmsg_policy radio_attr[2] = { + [0] = { .name = "macaddr", .type = BLOBMSG_TYPE_STRING }, + [1] = { .name = "sha256", .type = BLOBMSG_TYPE_STRING }, + }; + struct blob_attr *tb1[ARRAY_SIZE(radio_attr)]; + char radio_str[18] = {0}; + uint8_t radio_mac[6] = {0}; + char *sh256_str; + int blen; + //char * data; + + blobmsg_parse(radio_attr, ARRAY_SIZE(radio_attr), tb1, blobmsg_data(b), blob_len(b)); + if (!tb1[0] || !tb1[1]) + continue; + + strncpy(radio_str, blobmsg_data(tb1[0]), sizeof(radio_str) - 1); + if (!hwaddr_aton(radio_str, radio_mac)) { + agnt_dbg(LOG_APCFG, "Failed to convert macaddr %s\n", radio_str); + continue; + } + + if (memcmp(macaddr, radio_mac, 6)) + continue; + + sh256_str = blobmsg_get_string(tb1[1]); + if (!sh256_str) { + agnt_err(LOG_APCFG, "%s: No valid sha256\n", __func__); + ret = -1; + break; + } + + blen = strlen(sh256_str) / 2; + if (blen > SHA256_LENGTH) { + agnt_err(LOG_APCFG, "%s: SHA256 string too long\n", __func__); + ret = -1; + break; + } + + strtob(sh256_str, blen, sha256_out); + } + +out: + blob_buf_free(&radios); + + return ret; +} diff --git a/src/autoconfig.h b/src/autoconfig.h index c0ca23aee..026d5278d 100644 --- a/src/autoconfig.h +++ b/src/autoconfig.h @@ -29,4 +29,5 @@ bool autoconfig_has_wsc_changed(struct wsc_data *wsc, size_t tlvs_size, uint8_t *sha256_out); int autoconfig_write_hash_to_file(uint8_t *macaddr, uint8_t *sha256); +int autoconfig_read_hash_from_file(uint8_t *macaddr, uint8_t *sha256_out); #endif -- GitLab