From e3a73a504f6a63bf1a716131cf9dea75dc7b0b41 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Fri, 6 Sep 2024 12:25:01 +0200 Subject: rest: add GET /efi Add GET /efi request to obtain information about the client's boot entries. Field inside the /refresh payload 'efi': { 'entries': [ { "order": 0, "name": "Boot0000", "active": false, "description": "grub" }, { "order": 1, "name": "Boot0001", "active": true, "description": "UEFI: PXE IP4 Realtek PCIe GBE Family Controller" } ] } If the client is not a EFI system it won't add the 'efi' field. If an entry is not in the boot order it won't have the 'order' field. GET /efi resquest payload structure: { 'clients': ['10.141.10.21', '10.141.10.22'] } GET /efi response's structure: { 'clients': [ { 'ip': '10.141.10.21', 'entries': [ { "order": 0, "name": "Boot0000", "active": false, "description": "grub" }, { "order": 1, "name": "Boot0001", "active": true, "description": "UEFI: PXE IP4 Realtek PCIe GBE Family Controller" } ] }, { 'ip': '10.141.10.22', 'entries': [] } ] } The client with ip 10.141.10.22 is a BIOS system. If an entry does not appear in the boot order it won't have the 'order' field. --- src/rest.c | 151 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 151 insertions(+) (limited to 'src/rest.c') diff --git a/src/rest.c b/src/rest.c index 1894111..99fbe1f 100644 --- a/src/rest.c +++ b/src/rest.c @@ -4337,6 +4337,143 @@ static int og_cmd_cache_list(json_t *element, struct og_msg_params *params, return 0; } +static int og_dbi_client_efi_get(struct og_dbi *dbi, json_t *clients, const char *ip) +{ + json_t *client_data, *boot_entry, *boot_entry_arr; + const char *msglog, *entry_name, *entry_description; + bool entry_active; + int entry_order; + dbi_result result; + + client_data = json_object(); + if (!client_data) + return -1; + + json_object_set_new(client_data, "ip", json_string(ip)); + + result = dbi_conn_queryf(dbi->conn, + "SELECT boot_entries.name, boot_entries.active, " + "boot_entries.description, boot_entries.entry_order " + "FROM boot_entries JOIN ordenadores " + "ON ordenadores.idordenador = boot_entries.client_id " + "WHERE ordenadores.ip = '%s' " + "ORDER BY " + " CASE " + " WHEN boot_entries.entry_order >= 0 THEN 0 ELSE 1 " + " END ASC, " + " boot_entries.entry_order ASC", ip); + + if (!result) { + json_decref(client_data); + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + + boot_entry_arr = json_array(); + if (!boot_entry_arr) { + json_decref(client_data); + dbi_result_free(result); + return -1; + } + + while (dbi_result_next_row(result)) { + entry_name = dbi_result_get_string(result, "name"); + entry_active = dbi_result_get_int(result, "active") != 0; + entry_description = dbi_result_get_string(result, "description"); + entry_order = dbi_result_get_int(result, "entry_order"); + + boot_entry = json_object(); + if (!boot_entry) { + json_decref(client_data); + json_decref(boot_entry_arr); + dbi_result_free(result); + return -1; + } + + json_object_set_new(boot_entry, "name", json_string(entry_name)); + json_object_set_new(boot_entry, "active", json_boolean(entry_active)); + json_object_set_new(boot_entry, "description", json_string(entry_description)); + if (entry_order >= 0) + json_object_set_new(boot_entry, "order", json_integer(entry_order)); + json_array_append_new(boot_entry_arr, boot_entry); + } + json_object_set_new(client_data, "entries", boot_entry_arr); + json_array_append_new(clients, client_data); + dbi_result_free(result); + + return 0; +} + +static int og_cmd_efi_list(json_t *element, struct og_msg_params *params, + char *buffer_reply) +{ + json_t *value, *root, *clients; + struct og_dbi *dbi; + const char *key; + int err = 0, i; + + struct og_buffer og_buffer = { + .data = buffer_reply + }; + + if (json_typeof(element) != JSON_OBJECT) + return -1; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "clients")) + err = og_json_parse_clients(value, params); + else + err = -1; + + if (err < 0) + return err; + } + + if (!og_msg_params_validate(params, OG_REST_PARAM_ADDR)) + return -1; + + clients = json_array(); + if (!clients) + return -1; + + dbi = og_dbi_open(&ogconfig.db); + if (!dbi) { + syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", + __func__, __LINE__); + json_decref(clients); + return -1; + } + + for (i = 0; i < params->ips_array_len; i++) { + if (og_dbi_client_efi_get(dbi, clients, params->ips_array[i]) < 0) { + json_decref(clients); + og_dbi_close(dbi); + return -1; + } + } + + og_dbi_close(dbi); + + root = json_object(); + if (!root) { + json_decref(clients); + return -1; + } + + json_object_set_new(root, "clients", clients); + + if (json_dump_callback(root, og_json_dump_clients, &og_buffer, JSON_ENSURE_ASCII)) { + json_decref(root); + return -1; + } + + json_decref(root); + + return 0; +} + static int og_cmd_cache_delete(json_t *element, struct og_msg_params *params, char *buffer_reply) { @@ -8317,6 +8454,7 @@ struct { [OG_URI_CACHE_LIST] = { "cache/list", }, [OG_URI_CACHE_DELETE] = { "cache/delete", }, [OG_URI_CACHE_FETCH] = { "cache/fetch", }, + [OG_URI_EFI] = { "efi", }, [OG_URI_PART_SETUP] = { "setup", }, [OG_URI_RUN_SCHEDULE] = { "run/schedule", }, [OG_URI_TASK_RUN] = { "task/run", }, @@ -8928,6 +9066,19 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } err = og_cmd_cache_list(root, ¶ms, buf_reply); + } else if (!strncmp(cmd, "efi", strlen("efi"))) { + if (method != OG_METHOD_GET) { + err = og_client_method_not_found(cli); + goto err_process_rest_payload; + } + + if (!root) { + syslog(LOG_ERR, + "command efi with no payload\n"); + err = og_client_bad_request(cli); + goto err_process_rest_payload; + } + err = og_cmd_efi_list(root, ¶ms, buf_reply); } else if (!strncmp(cmd, "cache/delete", strlen("cache/delete"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); -- cgit v1.2.3-18-g5258