From 5a3b6d0cbb023353c1b16069d68f203589b77e27 Mon Sep 17 00:00:00 2001 From: Suvendhu Hansa Date: Mon, 23 Jun 2025 18:56:42 +0530 Subject: [PATCH] Fix empty params in transfer complete event --- src/configs.c | 36 ++++++++++++++++------ src/fw_images.c | 81 +++++++++++++++++++++++++++++++------------------ src/utils.c | 14 +++------ src/utils.h | 2 +- 4 files changed, 84 insertions(+), 49 deletions(-) diff --git a/src/configs.c b/src/configs.c index c7f836f..6c63b71 100644 --- a/src/configs.c +++ b/src/configs.c @@ -53,7 +53,7 @@ static int dmmap_synchronizeVcfInst(struct dmctx *dmctx, DMNODE *parent_node, vo } static int bbf_config_backup(struct ubus_context *ctx, const char *url, const char *username, const char *password, - char *config_name, const char *command, const char *obj_path) + char *config_name, const char *command, const char *obj_path, const char *cmd_key, const char *requestor) { int res = 0; char fault_msg[128] = {0}; @@ -79,7 +79,7 @@ static int bbf_config_backup(struct ubus_context *ctx, const char *url, const ch end: // Send the transfer complete event - send_transfer_complete_event(ctx, command, obj_path, url, fault_msg, start_time, complete_time, NULL, "Upload"); + send_transfer_complete_event(ctx, command, obj_path, url, fault_msg, start_time, complete_time, cmd_key, "Upload", requestor); // Remove temporary file if (file_exists(CONFIG_BACKUP) && remove(CONFIG_BACKUP)) @@ -91,7 +91,7 @@ end: static int bbf_config_restore(struct ubus_context *ctx, const char *url, const char *username, const char *password, const char *file_size, const char *checksum_algorithm, const char *checksum, - const char *command, const char *obj_path) + const char *command, const char *obj_path, const char *cmd_key, const char *requestor) { char config_restore[256] = {0}; int res = 0; @@ -134,7 +134,7 @@ static int bbf_config_restore(struct ubus_context *ctx, const char *url, const c end: // Send the transfer complete event - send_transfer_complete_event(ctx, command, obj_path, url, fault_msg, start_time, complete_time, NULL, "Download"); + send_transfer_complete_event(ctx, command, obj_path, url, fault_msg, start_time, complete_time, cmd_key, "Download", requestor); // Remove temporary file if (file_exists(config_restore) && strncmp(url, FILE_URI, strlen(FILE_URI)) && remove(config_restore)) @@ -254,16 +254,24 @@ static int get_operate_args_DeviceInfoVendorConfigFile_Backup(char *refparam, st static int operate_DeviceInfoVendorConfigFile_Backup(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) { - const char *backup_command = "Backup()"; + char backup_command[270] = {0}; char backup_path[256] = {'\0'}; char *vcf_name = NULL; + char *cmdkey = NULL, *reqst = NULL; char *ret = DM_STRRCHR(refparam, '.'); if (!ret) return USP_FAULT_INVALID_ARGUMENT; - if ((ret - refparam + 2) < sizeof(backup_path)) + if ((ret - refparam + 2) < sizeof(backup_path)) { snprintf(backup_path, ret - refparam + 2, "%s", refparam); + } + + if (ctx->dm_type == BBFDM_USP) { + snprintf(backup_command, sizeof(backup_command), "%s", refparam); + cmdkey = dmjson_get_value((json_object *)value, 1, "__BBF_CommandKey"); + reqst = dmjson_get_value((json_object *)value, 1, "__BBF_Requestor"); + } char *url = dmjson_get_value((json_object *)value, 1, "URL"); if (url[0] == '\0') @@ -274,7 +282,7 @@ static int operate_DeviceInfoVendorConfigFile_Backup(char *refparam, struct dmct dmuci_get_value_by_section_string(((struct dm_data *)data)->config_section, "name", &vcf_name); - int res = bbf_config_backup(ctx->ubus_ctx, url, user, pass, vcf_name, backup_command, backup_path); + int res = bbf_config_backup(ctx->ubus_ctx, url, user, pass, vcf_name, backup_command, backup_path, cmdkey, reqst); return res ? USP_FAULT_COMMAND_FAILURE : 0; } @@ -300,15 +308,23 @@ static int get_operate_args_DeviceInfoVendorConfigFile_Restore(char *refparam, s static int operate_DeviceInfoVendorConfigFile_Restore(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) { - const char *restore_command = "Restore()"; + char restore_command[270] = {0}; char restore_path[256] = {'\0'}; + char *cmd_key = NULL, *reqstr = NULL; char *ret = DM_STRRCHR(refparam, '.'); if (!ret) return USP_FAULT_INVALID_ARGUMENT; - if ((ret - refparam + 2) < sizeof(restore_path)) + if ((ret - refparam + 2) < sizeof(restore_path)) { snprintf(restore_path, ret - refparam + 2, "%s", refparam); + } + + if (ctx->dm_type == BBFDM_USP) { + snprintf(restore_command, sizeof(restore_command), "%s", refparam); + cmd_key = dmjson_get_value((json_object *)value, 1, "__BBF_CommandKey"); + reqstr = dmjson_get_value((json_object *)value, 1, "__BBF_Requestor"); + } char *url = dmjson_get_value((json_object *)value, 1, "URL"); if (url[0] == '\0') @@ -320,7 +336,7 @@ static int operate_DeviceInfoVendorConfigFile_Restore(char *refparam, struct dmc char *checksum_algorithm = dmjson_get_value((json_object *)value, 1, "CheckSumAlgorithm"); char *checksum = dmjson_get_value((json_object *)value, 1, "CheckSum"); - int res = bbf_config_restore(ctx->ubus_ctx, url, user, pass, file_size, checksum_algorithm, checksum, restore_command, restore_path); + int res = bbf_config_restore(ctx->ubus_ctx, url, user, pass, file_size, checksum_algorithm, checksum, restore_command, restore_path, cmd_key, reqstr); return res ? USP_FAULT_COMMAND_FAILURE : 0; } diff --git a/src/fw_images.c b/src/fw_images.c index a166b72..fded886 100644 --- a/src/fw_images.c +++ b/src/fw_images.c @@ -17,6 +17,22 @@ struct sysupgrade_ev_data { bool status; }; +struct fw_download_data { + char *url; + char *auto_activate; + char *username; + char *password; + char *file_size; + char *checksum_algorithm; + char *checksum; + char *bank_id; + char *command; + char *obj_path; + char *commandKey; + char *keep_config; + char *requestor; +}; + #define CRONTABS_ROOT "/etc/crontabs/root" #define ACTIVATE_HANDLER_FILE "/usr/share/bbfdm/scripts/bbf_activate_handler.sh" #define COPY_CONFIG_CMD "/etc/sysmngr/fwbank call copy_config 2> /dev/null" @@ -195,9 +211,7 @@ static void dmubus_receive_sysupgrade(struct ubus_context *ctx, struct ubus_even return; } -static int bbf_fw_image_download(struct ubus_context *ctx, const char *url, const char *auto_activate, const char *username, const char *password, - const char *file_size, const char *checksum_algorithm, const char *checksum, - const char *bank_id, const char *command, const char *obj_path, const char *commandKey, const char *keep) +static int bbf_fw_image_download(struct ubus_context *ctx, struct fw_download_data *dw_data) { char fw_image_path[256] = {0}; json_object *json_obj = NULL; @@ -210,7 +224,7 @@ static int bbf_fw_image_download(struct ubus_context *ctx, const char *url, cons DM_STRNCPY(fw_image_path, "/tmp/firmware-XXXXXX", sizeof(fw_image_path)); // Check the file system size if there is sufficient space for downloading the firmware image - if (!validate_file_system_size(file_size)) { + if (!validate_file_system_size(dw_data->file_size)) { res = -1; snprintf(fault_msg, sizeof(fault_msg), "Available memory space is lower than required for downloading"); goto end; @@ -226,18 +240,18 @@ static int bbf_fw_image_download(struct ubus_context *ctx, const char *url, cons } // Download the firmware image - long res_code = download_file(fw_image_path, url, username, password); + long res_code = download_file(fw_image_path, dw_data->url, dw_data->username, dw_data->password); complete_time = time(NULL); // Check if the download operation was successful - if (!validate_server_response_code(url, res_code)) { + if (!validate_server_response_code(dw_data->url, res_code)) { snprintf(fault_msg, sizeof(fault_msg), "Download operation is failed, fault code (%ld)", res_code); res = -1; goto end; } // Validate the CheckSum value according to its algorithm - if (!validate_checksum_value(fw_image_path, checksum_algorithm, checksum)) { + if (!validate_checksum_value(fw_image_path, dw_data->checksum_algorithm, dw_data->checksum)) { res = -1; snprintf(fault_msg, sizeof(fault_msg), "Checksum of the file is not matched with the specified value"); goto end; @@ -263,17 +277,17 @@ static int bbf_fw_image_download(struct ubus_context *ctx, const char *url, cons goto end; } - string_to_bool(auto_activate, &activate); + string_to_bool(dw_data->auto_activate, &activate); // Apply Firmware Image - if (!fwbank_upgrade(fw_image_path, activate, bank_id, DM_STRLEN(keep) ? keep : "1")) { + if (!fwbank_upgrade(fw_image_path, activate, dw_data->bank_id, DM_STRLEN(dw_data->keep_config) ? dw_data->keep_config : "1")) { res = 1; snprintf(fault_msg, sizeof(fault_msg), "Internal error occurred when applying the firmware"); goto end; } struct sysupgrade_ev_data ev_data = { - .bank_id = bank_id, + .bank_id = dw_data->bank_id, .status = false, }; @@ -292,10 +306,11 @@ static int bbf_fw_image_download(struct ubus_context *ctx, const char *url, cons end: // Send the transfer complete event - send_transfer_complete_event(ctx, command, obj_path, url, fault_msg, start_time, complete_time, commandKey, "Download"); + send_transfer_complete_event(ctx, dw_data->command, dw_data->obj_path, dw_data->url, fault_msg, start_time, complete_time, + dw_data->commandKey, "Download", dw_data->requestor); // Remove temporary file if ubus upgrade failed and file exists - if (file_exists(fw_image_path) && strncmp(url, FILE_URI, strlen(FILE_URI))) + if (file_exists(fw_image_path) && strncmp(dw_data->url, FILE_URI, strlen(FILE_URI))) remove(fw_image_path); return res; @@ -542,40 +557,48 @@ static int get_operate_args_DeviceInfoFirmwareImage_Download(char *refparam, str static int operate_DeviceInfoFirmwareImage_Download(char *refparam, struct dmctx *ctx, void *data, char *instance, char *value, int action) { - const char *command = "Download()"; char obj_path[256] = {0}; - char *keep_config = NULL; + struct fw_download_data dw_data; + + memset(&dw_data, 0, sizeof(struct fw_download_data)); char *ret = DM_STRRCHR(refparam, '.'); if (!ret) return USP_FAULT_INVALID_ARGUMENT; - if ((ret - refparam + 2) < sizeof(obj_path)) + if ((ret - refparam + 2) < sizeof(obj_path)) { snprintf(obj_path, ret - refparam + 2, "%s", refparam); + } - char *url = dmjson_get_value((json_object *)value, 1, "URL"); - if (url[0] == '\0') + if (ctx->dm_type == BBFDM_USP) { + dw_data.command = dmstrdup(refparam); + dw_data.commandKey = dmjson_get_value((json_object *)value, 1, "__BBF_CommandKey"); + dw_data.requestor = dmjson_get_value((json_object *)value, 1, "__BBF_Requestor"); + } + + dw_data.url = dmjson_get_value((json_object *)value, 1, "URL"); + if (DM_STRLEN(dw_data.url) == 0) return USP_FAULT_INVALID_ARGUMENT; // Assuming auto activate as false, if not provided by controller, in case of strict validation, // this should result into a fault - char *auto_activate = dmjson_get_value((json_object *)value, 1, "AutoActivate"); - if (DM_STRLEN(auto_activate) == 0) - auto_activate = dmstrdup("0"); + dw_data.auto_activate = dmjson_get_value((json_object *)value, 1, "AutoActivate"); + if (DM_STRLEN(dw_data.auto_activate) == 0) + dw_data.auto_activate = dmstrdup("0"); - char *username = dmjson_get_value((json_object *)value, 1, "Username"); - char *password = dmjson_get_value((json_object *)value, 1, "Password"); - char *file_size = dmjson_get_value((json_object *)value, 1, "FileSize"); - char *checksum_algorithm = dmjson_get_value((json_object *)value, 1, "CheckSumAlgorithm"); - char *checksum = dmjson_get_value((json_object *)value, 1, "CheckSum"); - char *commandKey = dmjson_get_value((json_object *)value, 1, "CommandKey"); + dw_data.username = dmjson_get_value((json_object *)value, 1, "Username"); + dw_data.password = dmjson_get_value((json_object *)value, 1, "Password"); + dw_data.file_size = dmjson_get_value((json_object *)value, 1, "FileSize"); + dw_data.checksum_algorithm = dmjson_get_value((json_object *)value, 1, "CheckSumAlgorithm"); + dw_data.checksum = dmjson_get_value((json_object *)value, 1, "CheckSum"); #ifdef SYSMNGR_VENDOR_EXTENSIONS - keep_config = dmjson_get_value((json_object *)value, 1, CUSTOM_PREFIX"KeepConfig"); + dw_data.keep_config = dmjson_get_value((json_object *)value, 1, CUSTOM_PREFIX"KeepConfig"); #endif - char *bank_id = get_fwbank_option_value(data, "id"); + dw_data.bank_id = get_fwbank_option_value(data, "id"); + dw_data.obj_path = dmstrdup(obj_path); - int res = bbf_fw_image_download(ctx->ubus_ctx, url, auto_activate, username, password, file_size, checksum_algorithm, checksum, bank_id, command, obj_path, commandKey, keep_config); + int res = bbf_fw_image_download(ctx->ubus_ctx, &dw_data); if (res == 1) { bbfdm_set_fault_message(ctx, "Firmware validation failed"); diff --git a/src/utils.c b/src/utils.c index 4c8afff..30b14af 100644 --- a/src/utils.c +++ b/src/utils.c @@ -108,7 +108,7 @@ bool validate_file_system_size(const char *file_size) } void send_transfer_complete_event(struct ubus_context *ubus_ctx, const char *command, const char *obj_path, const char *transfer_url, - char *fault_string, time_t start_t, time_t complete_t, const char *commandKey, const char *transfer_type) + char *fault_string, time_t start_t, time_t complete_t, const char *commandKey, const char *transfer_type, const char *requestor) { char start_time[32] = {0}; char complete_time[32] = {0}; @@ -123,15 +123,11 @@ void send_transfer_complete_event(struct ubus_context *ubus_ctx, const char *com blobmsg_add_string(&bb, "name", "Device.LocalAgent.TransferComplete!"); void *arr = blobmsg_open_array(&bb, "input"); - fill_blob_param(&bb, "Command", command, DMT_TYPE[DMT_STRING], 0); - if(commandKey) - fill_blob_param(&bb, "CommandKey", commandKey, DMT_TYPE[DMT_STRING], 0); - else - fill_blob_param(&bb, "CommandKey", "", DMT_TYPE[DMT_STRING], 0); - - fill_blob_param(&bb, "Requestor", "", DMT_TYPE[DMT_STRING], 0); + fill_blob_param(&bb, "Command", command ? command : "", DMT_TYPE[DMT_STRING], 0); + fill_blob_param(&bb, "CommandKey", commandKey ? commandKey : "", DMT_TYPE[DMT_STRING], 0); + fill_blob_param(&bb, "Requestor", requestor ? requestor : "", DMT_TYPE[DMT_STRING], 0); fill_blob_param(&bb, "TransferType", transfer_type, DMT_TYPE[DMT_STRING], 0); - fill_blob_param(&bb, "Affected", obj_path, DMT_TYPE[DMT_STRING], 0); + fill_blob_param(&bb, "Affected", obj_path ? obj_path : "", DMT_TYPE[DMT_STRING], 0); fill_blob_param(&bb, "TransferURL", transfer_url, DMT_TYPE[DMT_STRING], 0); fill_blob_param(&bb, "StartTime", start_time, DMT_TYPE[DMT_STRING], 0); fill_blob_param(&bb, "CompleteTime", complete_time, DMT_TYPE[DMT_STRING], 0); diff --git a/src/utils.h b/src/utils.h index 22ee3fc..bf8f4ae 100644 --- a/src/utils.h +++ b/src/utils.h @@ -19,7 +19,7 @@ bool validate_file_system_size(const char *file_size); bool validate_server_response_code(const char *url, int response_code); void send_transfer_complete_event(struct ubus_context *ubus_ctx, const char *command, const char *obj_path, const char *transfer_url, - char *fault_string, time_t start_t, time_t complete_t, const char *commandKey, const char *transfer_type); + char *fault_string, time_t start_t, time_t complete_t, const char *commandKey, const char *transfer_type, const char *requestor); int sysmngr_get_uptime(void);