Skip to content

Commit

Permalink
fix: key index failure after removing keys
Browse files Browse the repository at this point in the history
  • Loading branch information
anibalportero committed Jun 4, 2024
1 parent 498ee2c commit 61f4823
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 70 deletions.
6 changes: 3 additions & 3 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,15 +449,15 @@ static void _set_config_by_entry_bootloader_type(struct pv_config_entry *entry,

void pv_config_set_creds_id(char *id)
{
_set_config_by_index_str(PV_CREDS_ID, id, PH_CLIENT);
_set_config_by_index_str(PH_CREDS_ID, id, PH_CLIENT);
}
void pv_config_set_creds_prn(char *prn)
{
_set_config_by_index_str(PV_CREDS_PRN, prn, PH_CLIENT);
_set_config_by_index_str(PH_CREDS_PRN, prn, PH_CLIENT);
}
void pv_config_set_creds_secret(char *secret)
{
_set_config_by_index_str(PV_CREDS_SECRET, secret, PH_CLIENT);
_set_config_by_index_str(PH_CREDS_SECRET, secret, PH_CLIENT);
}

void pv_config_set_debug_shell(bool shell)
Expand Down
34 changes: 14 additions & 20 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@
// GENERIC

typedef enum {
PH_CREDS_HOST,
PH_CREDS_ID,
PH_CREDS_PORT,
PH_CREDS_PROXY_HOST,
PH_CREDS_PROXY_NOPROXYCONNECT,
PH_CREDS_PROXY_PORT,
PH_CREDS_PRN,
PH_CREDS_SECRET,
PH_CREDS_TYPE,
PH_FACTORY_AUTOTOK,
PH_METADATA_DEVMETA_INTERVAL,
PH_METADATA_USRMETA_INTERVAL,
PH_UPDATER_INTERVAL,
PH_UPDATER_NETWORK_TIMEOUT,
PV_BOOTLOADER_FITCONFIG,
PV_BOOTLOADER_MTD_ENV,
PV_BOOTLOADER_MTD_ONLY,
Expand All @@ -38,17 +52,6 @@ typedef enum {
PV_CACHE_USRMETADIR,
PV_CONTROL_REMOTE,
PV_CONTROL_REMOTE_ALWAYS,
PV_CREDS_HOST,
PV_CREDS_ID,
PV_CREDS_PORT,
PV_CREDS_PROXY_HOST,
PV_CREDS_PROXY_NOPROXYCONNECT,
PV_CREDS_PROXY_PORT,
PV_CREDS_PRN,
PV_CREDS_SECRET,
PV_CREDS_TPM_CERT,
PV_CREDS_TPM_KEY,
PV_CREDS_TYPE,
PV_DEBUG_SHELL,
PV_DEBUG_SHELL_AUTOLOGIN,
PV_DEBUG_SSH,
Expand All @@ -57,7 +60,6 @@ typedef enum {
PV_DISK_VOLDIR,
PV_DISK_WRITABLEDIR,
PV_DROPBEAR_CACHE_DIR,
PV_FACTORY_AUTOTOK,
PV_LIBTHTTP_CERTDIR,
PV_LIBTHTTP_LOG_LEVEL,
PV_LOG_CAPTURE,
Expand All @@ -71,17 +73,13 @@ typedef enum {
PV_LOG_PUSH,
PV_LOG_SERVER_OUTPUTS,
PV_LOG_SINGLEFILE_TS_FMT,
PV_LOG_STDOUT,
PV_LOG_STDOUT_TS_FMT,
PV_LXC_LOG_LEVEL,
PV_METADATA_DEVMETA_INTERVAL,
PV_METADATA_USRMETA_INTERVAL,
PV_NET_BRADDRESS4,
PV_NET_BRDEV,
PV_NET_BRMASK4,
PV_POLICY,
PV_REVISION_RETRIES,
PV_REVISION_RETRIES_TIMEOUT,
PV_SECUREBOOT_CHECKSUM,
PV_SECUREBOOT_HANDLERS,
PV_SECUREBOOT_MODE,
Expand All @@ -95,7 +93,6 @@ typedef enum {
PV_STORAGE_LOGTEMPSIZE,
PV_STORAGE_MNTPOINT,
PV_STORAGE_MNTTYPE,
PV_STORAGE_OPTS,
PV_STORAGE_WAIT,
PV_SYSTEM_APPARMOR_PROFILES,
PV_SYSTEM_CONFDIR,
Expand All @@ -109,10 +106,7 @@ typedef enum {
PV_SYSTEM_USRDIR,
PV_UPDATER_COMMIT_DELAY,
PV_UPDATER_GOALS_TIMEOUT,
PV_UPDATER_INTERVAL,
PV_UPDATER_NETWORK_TIMEOUT,
PV_UPDATER_USE_TMP_OBJECTS,
PV_WDT_ENABLED,
PV_WDT_MODE,
PV_WDT_TIMEOUT,
PV_MAX
Expand Down
22 changes: 11 additions & 11 deletions pantahub.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static int ph_client_init(struct pantavisor *pv)
return 0;
}

const char *id = pv_config_get_str(PV_CREDS_ID);
const char *id = pv_config_get_str(PH_CREDS_ID);
if (!endpoint && id) {
size = sizeof(ENDPOINT_FMT) + strlen(id) + 1;
endpoint = malloc(size * sizeof(char));
Expand Down Expand Up @@ -204,11 +204,11 @@ struct pv_connection *pv_get_instance_connection()
return NULL;
}
// default to global PH instance
host = pv_config_get_str(PV_CREDS_HOST);
host = pv_config_get_str(PH_CREDS_HOST);
if (strcmp(host, "") == 0)
host = "api.pantahub.com";

port = pv_config_get_int(PV_CREDS_PORT);
port = pv_config_get_int(PH_CREDS_PORT);
if (!port)
port = 443;

Expand Down Expand Up @@ -333,11 +333,11 @@ static int pv_ph_register_self_builtin(struct pantavisor *pv)
req->proto_version = THTTP_PROTO_VERSION_10;
req->user_agent = pv_user_agent;

req->host = pv_config_get_str(PV_CREDS_HOST);
req->port = pv_config_get_int(PV_CREDS_PORT);
req->host_proxy = pv_config_get_str(PV_CREDS_PROXY_HOST);
req->port_proxy = pv_config_get_int(PV_CREDS_PROXY_PORT);
req->proxyconnect = !pv_config_get_int(PV_CREDS_PROXY_NOPROXYCONNECT);
req->host = pv_config_get_str(PH_CREDS_HOST);
req->port = pv_config_get_int(PH_CREDS_PORT);
req->host_proxy = pv_config_get_str(PH_CREDS_PROXY_HOST);
req->port_proxy = pv_config_get_int(PH_CREDS_PROXY_PORT);
req->proxyconnect = !pv_config_get_int(PH_CREDS_PROXY_NOPROXYCONNECT);

baseurl_size = strlen("https://") + strlen(req->host) + 1 /* : */ +
5 /* port */ + 2 /* 0-delim */;
Expand All @@ -352,7 +352,7 @@ static int pv_ph_register_self_builtin(struct pantavisor *pv)
req->path = "/devices/";
req->body = 0;

const char *autotok = pv_config_get_str(PV_FACTORY_AUTOTOK);
const char *autotok = pv_config_get_str(PH_FACTORY_AUTOTOK);
if (autotok && strcmp(autotok, "")) {
headers = calloc(2, sizeof(char *));
header_size = sizeof(DEVICE_TOKEN_FMT) + 64;
Expand Down Expand Up @@ -423,7 +423,7 @@ int pv_ph_register_self(struct pantavisor *pv)
HUB_CREDS_TYPE_ERROR
} creds_type;

const char *type = pv_config_get_str(PV_CREDS_TYPE);
const char *type = pv_config_get_str(PH_CREDS_TYPE);
if (!strcmp(type, "builtin")) {
creds_type = HUB_CREDS_TYPE_BUILTIN;
} else if (strlen(type) >= 4 && !strncmp(type, "ext-", 4)) {
Expand Down Expand Up @@ -531,7 +531,7 @@ void pv_ph_update_hint_file(struct pantavisor *pv, char *c)

pv_paths_pv_file(path, PATH_MAX, DEVICE_ID_FNAME);
SNPRINTF_WTRUNC(buf, sizeof(buf), "%s\n",
pv_config_get_str(PV_CREDS_ID));
pv_config_get_str(PH_CREDS_ID));
if (pv_fs_file_save(path, buf, 044))
pv_log(WARN, "could not save file %s: %s", path,
strerror(errno));
Expand Down
18 changes: 9 additions & 9 deletions pantavisor.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,17 @@ static pv_state_t _pv_run(struct pantavisor *pv)
timer_start(&timer_commit, pv_config_get_int(PV_UPDATER_COMMIT_DELAY),
0, RELATIV_TIMER);
timer_start(&timer_rollback_remote,
pv_config_get_int(PV_UPDATER_NETWORK_TIMEOUT), 0,
pv_config_get_int(PH_UPDATER_NETWORK_TIMEOUT), 0,
RELATIV_TIMER);
timer_start(&timer_wait_delay, PV_WAIT_PERIOD, 0, RELATIV_TIMER);
timer_start(&timer_usrmeta_interval,
pv_config_get_int(PV_METADATA_USRMETA_INTERVAL), 0,
pv_config_get_int(PH_METADATA_USRMETA_INTERVAL), 0,
RELATIV_TIMER);
timer_start(&timer_devmeta_interval,
pv_config_get_int(PV_METADATA_DEVMETA_INTERVAL), 0,
pv_config_get_int(PH_METADATA_DEVMETA_INTERVAL), 0,
RELATIV_TIMER);
timer_start(&timer_updater_interval,
pv_config_get_int(PV_UPDATER_INTERVAL), 0, RELATIV_TIMER);
pv_config_get_int(PH_UPDATER_INTERVAL), 0, RELATIV_TIMER);

if (pv_config_get_wdt_mode() <= WDT_STARTUP)
pv_wdt_stop();
Expand Down Expand Up @@ -356,11 +356,11 @@ static pv_state_t pv_wait_unclaimed(struct pantavisor *pv)
}

timer_start(&timer_updater_interval,
pv_config_get_int(PV_UPDATER_INTERVAL), 0, RELATIV_TIMER);
pv_config_get_int(PH_UPDATER_INTERVAL), 0, RELATIV_TIMER);

pv_config_load_unclaimed_creds();

const char *id = pv_config_get_str(PV_CREDS_ID);
const char *id = pv_config_get_str(PH_CREDS_ID);
if (id && strcmp(id, "") && pv_ph_device_exists(pv))
need_register = 0;

Expand Down Expand Up @@ -414,7 +414,7 @@ static int pv_meta_update_to_ph(struct pantavisor *pv)
if (pv_ph_device_get_meta(pv))
return -1;
timer_start(&timer_usrmeta_interval,
pv_config_get_int(PV_METADATA_USRMETA_INTERVAL), 0,
pv_config_get_int(PH_METADATA_USRMETA_INTERVAL), 0,
RELATIV_TIMER);
}

Expand All @@ -423,7 +423,7 @@ static int pv_meta_update_to_ph(struct pantavisor *pv)
if (pv_metadata_upload_devmeta(pv))
return -1;
timer_start(&timer_devmeta_interval,
pv_config_get_int(PV_METADATA_DEVMETA_INTERVAL), 0,
pv_config_get_int(PH_METADATA_DEVMETA_INTERVAL), 0,
RELATIV_TIMER);
}

Expand Down Expand Up @@ -531,7 +531,7 @@ static pv_state_t pv_wait_network(struct pantavisor *pv)
return PV_STATE_UPDATE;
}
timer_start(&timer_updater_interval,
pv_config_get_int(PV_UPDATER_INTERVAL), 0,
pv_config_get_int(PH_UPDATER_INTERVAL), 0,
RELATIV_TIMER);
}

Expand Down
8 changes: 4 additions & 4 deletions storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ static int pv_storage_init(struct pv_init *this)
strerror(errno));

pv_paths_pv_file(path, PATH_MAX, DEVICE_ID_FNAME);
const char *prn = pv_config_get_str(PV_CREDS_PRN);
const char *prn = pv_config_get_str(PH_CREDS_PRN);
if (!prn || !strcmp(prn, "")) {
pv->unclaimed = true;
if (pv_fs_file_save(path, "", 0444) < 0)
Expand All @@ -1130,15 +1130,15 @@ static int pv_storage_init(struct pv_init *this)
} else {
pv->unclaimed = false;
SNPRINTF_WTRUNC(tmp, sizeof(tmp), "%s\n",
pv_config_get_str(PV_CREDS_ID));
pv_config_get_str(PH_CREDS_ID));
if (pv_fs_file_save(path, tmp, 0444) < 0)
pv_log(WARN, "could not save file %s: %s", path,
strerror(errno));
}
pv_paths_pv_file(path, PATH_MAX, PHHOST_FNAME);
SNPRINTF_WTRUNC(tmp, sizeof(tmp), "https://%s:%d\n",
pv_config_get_str(PV_CREDS_HOST),
pv_config_get_int(PV_CREDS_PORT));
pv_config_get_str(PH_CREDS_HOST),
pv_config_get_int(PH_CREDS_PORT));
if (pv_fs_file_save(path, tmp, 0444) < 0)
pv_log(WARN, "could not save file %s: %s", path,
strerror(errno));
Expand Down
18 changes: 9 additions & 9 deletions trestclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static struct trest_response *external_login_handler(trest_ptr self, void *data)
char buf[PV_TRESTCLIENT_MAX_READ];
int read_c, body_s, s;

const char *type = pv_config_get_str(PV_CREDS_TYPE);
const char *type = pv_config_get_str(PH_CREDS_TYPE);

pv = NULL;
response = NULL;
Expand Down Expand Up @@ -162,17 +162,17 @@ trest_ptr pv_get_trest_client(struct pantavisor *pv, struct pv_connection *conn)
} creds_type;

// Make sure values are reasonable
const char *host = pv_config_get_str(PV_CREDS_HOST);
int port = pv_config_get_int(PV_CREDS_PORT);
const char *host = pv_config_get_str(PH_CREDS_HOST);
int port = pv_config_get_int(PH_CREDS_PORT);
if (!host || (strcmp(host, "") == 0))
return NULL;

const char *type = pv_config_get_str(PV_CREDS_TYPE);
const char *prn = pv_config_get_str(PV_CREDS_PRN);
const char *secret = pv_config_get_str(PV_CREDS_SECRET);
char *proxy_host = pv_config_get_str(PV_CREDS_PROXY_HOST);
int proxy_port = pv_config_get_int(PV_CREDS_PROXY_PORT);
int noproxyconnect = pv_config_get_int(PV_CREDS_PROXY_NOPROXYCONNECT);
const char *type = pv_config_get_str(PH_CREDS_TYPE);
const char *prn = pv_config_get_str(PH_CREDS_PRN);
const char *secret = pv_config_get_str(PH_CREDS_SECRET);
char *proxy_host = pv_config_get_str(PH_CREDS_PROXY_HOST);
int proxy_port = pv_config_get_int(PH_CREDS_PROXY_PORT);
int noproxyconnect = pv_config_get_int(PH_CREDS_PROXY_NOPROXYCONNECT);

if (!strcmp(type, "builtin")) {
creds_type = HUB_CREDS_TYPE_BUILTIN;
Expand Down
24 changes: 12 additions & 12 deletions updater.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ static int trail_remote_init(struct pantavisor *pv)
char *endpoint_trail = NULL;
int size = -1;

const char *id = pv_config_get_str(PV_CREDS_ID);
const char *id = pv_config_get_str(PH_CREDS_ID);
if (pv->remote || !id)
return 0;

Expand Down Expand Up @@ -814,7 +814,7 @@ static int trail_get_new_steps(struct pantavisor *pv)
pv_log(DEBUG, "parse rev %s...", rev);

// create temp update to be able to report the revision state
update = pv_update_new(pv_config_get_str(PV_CREDS_ID), rev, false);
update = pv_update_new(pv_config_get_str(PH_CREDS_ID), rev, false);
if (!update)
goto out;

Expand Down Expand Up @@ -1107,12 +1107,12 @@ static int trail_put_object(struct pantavisor *pv, struct pv_object *o,
req->method = THTTP_METHOD_PUT;
req->proto = THTTP_PROTO_HTTP;
req->proto_version = THTTP_PROTO_VERSION_10;
req->host = pv_config_get_str(PV_CREDS_HOST);
req->port = pv_config_get_int(PV_CREDS_PORT);
req->host_proxy = pv_config_get_str(PV_CREDS_PROXY_HOST);
req->port_proxy = pv_config_get_int(PV_CREDS_PROXY_PORT);
req->host = pv_config_get_str(PH_CREDS_HOST);
req->port = pv_config_get_int(PH_CREDS_PORT);
req->host_proxy = pv_config_get_str(PH_CREDS_PROXY_HOST);
req->port_proxy = pv_config_get_int(PH_CREDS_PROXY_PORT);
req->proxyconnect =
!pv_config_get_int(PV_CREDS_PROXY_NOPROXYCONNECT);
!pv_config_get_int(PH_CREDS_PROXY_NOPROXYCONNECT);
if (req->is_tls) {
str_size = strlen("https://") + strlen(req->host) +
1 /* : */ + 5 /* port */ + 2 /* 0-delim */;
Expand Down Expand Up @@ -1683,9 +1683,9 @@ static int trail_download_object(struct pantavisor *pv, struct pv_object *obj,
host[n] = '\0';

req->host = host;
req->host_proxy = pv_config_get_str(PV_CREDS_PROXY_HOST);
req->port_proxy = pv_config_get_int(PV_CREDS_PROXY_PORT);
req->proxyconnect = !pv_config_get_int(PV_CREDS_PROXY_NOPROXYCONNECT);
req->host_proxy = pv_config_get_str(PH_CREDS_PROXY_HOST);
req->port_proxy = pv_config_get_int(PH_CREDS_PROXY_PORT);
req->proxyconnect = !pv_config_get_int(PH_CREDS_PROXY_NOPROXYCONNECT);
if (req->is_tls) {
size = strlen("https://") + strlen(req->host) + 1 /* : */ +
5 /* port */ + 2 /* 0-delim */;
Expand Down Expand Up @@ -1926,7 +1926,7 @@ struct pv_update *pv_update_get_step_local(const char *rev)

pv_logserver_start_update(rev);

update = pv_update_new(pv_config_get_str(PV_CREDS_ID), rev, true);
update = pv_update_new(pv_config_get_str(PH_CREDS_ID), rev, true);
if (!update)
goto err;

Expand Down Expand Up @@ -2059,7 +2059,7 @@ int pv_update_resume(struct pantavisor *pv)

pv_log(INFO, "loading update data from rev %s after reboot...",
rev);
pv->update = pv_update_new(pv_config_get_str(PV_CREDS_ID), rev,
pv->update = pv_update_new(pv_config_get_str(PH_CREDS_ID), rev,
false);
if (!pv->update)
return -1;
Expand Down
3 changes: 1 addition & 2 deletions wdt.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ static int pv_wdt_fd = -1;

int pv_wdt_start()
{
if (!pv_config_get_bool(PV_WDT_ENABLED) ||
(pv_config_get_wdt_mode() == WDT_DISABLED))
if (pv_config_get_wdt_mode() == WDT_DISABLED)
return 0;

if (pv_wdt_fd >= 0)
Expand Down

0 comments on commit 61f4823

Please sign in to comment.