diff options
author | Roberto Hueso Gómez <rhueso@soleta.eu> | 2020-10-06 14:19:06 +0200 |
---|---|---|
committer | OpenGnSys Support Team <soporte-og@soleta.eu> | 2020-10-06 16:04:17 +0200 |
commit | f537dafa775307255019df9a119c8f81a52607ca (patch) | |
tree | 3b3863f88e514ddbfc5d25be335cdb478a5a0174 /src | |
parent | 27fbc658293db97775385f6cc54a36bf0d6c454d (diff) |
#941 Use fixed length strings in og_computer and og_dbi_get_computer_info
This patch is a refactor for og_computer and og_dbi_get_computer_info.
It now uses fixed lenght strings to make it more reliable and avoid
errors if the DB is not returning a null ended string.
Diffstat (limited to 'src')
-rw-r--r-- | src/client.c | 15 | ||||
-rw-r--r-- | src/dbi.c | 41 | ||||
-rw-r--r-- | src/dbi.h | 25 | ||||
-rw-r--r-- | src/rest.c | 53 |
4 files changed, 63 insertions, 71 deletions
diff --git a/src/client.c b/src/client.c index 7c66a38..b475189 100644 --- a/src/client.c +++ b/src/client.c @@ -133,7 +133,6 @@ static int og_resp_hardware(json_t *data, struct og_client *cli) if (!dbi) { syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", __func__, __LINE__); - og_dbi_free_computer_info(&computer); return -1; } @@ -150,7 +149,6 @@ static int og_resp_hardware(json_t *data, struct og_client *cli) res = actualizaHardware(dbi, legacy.hardware, legacy.id, computer.name, legacy.center); og_dbi_close(dbi); - og_dbi_free_computer_info(&computer); if (!res) { syslog(LOG_ERR, "Problem updating client configuration\n"); @@ -203,7 +201,6 @@ static int og_resp_software(json_t *data, struct og_client *cli) if (!dbi) { syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", __func__, __LINE__); - og_dbi_free_computer_info(&computer); return -1; } @@ -221,7 +218,6 @@ static int og_resp_software(json_t *data, struct og_client *cli) res = actualizaSoftware(dbi, legacy.software, legacy.part, legacy.id, computer.name, legacy.center); og_dbi_close(dbi); - og_dbi_free_computer_info(&computer); if (!res) { syslog(LOG_ERR, "Problem updating client configuration\n"); @@ -351,7 +347,6 @@ static int og_resp_refresh(json_t *data, struct og_client *cli) if (!dbi) { syslog(LOG_ERR, "cannot open connection database (%s:%d)\n", __func__, __LINE__); - og_dbi_free_computer_info(&computer); return -1; } @@ -366,19 +361,15 @@ static int og_resp_refresh(json_t *data, struct og_client *cli) if (!res) { syslog(LOG_ERR, "Problem updating client configuration\n"); - og_dbi_free_computer_info(&computer); return -1; } if (!cli->autorun && computer.procedure_id) { cli->autorun = true; - if (og_dbi_queue_autorun(computer.id, computer.procedure_id)) { - og_dbi_free_computer_info(&computer); + if (og_dbi_queue_autorun(computer.id, computer.procedure_id)) return -1; - } } - og_dbi_free_computer_info(&computer); return 0; } @@ -502,12 +493,10 @@ static int og_resp_image_create(json_t *data, struct og_client *cli) computer.name, soft_legacy.center); if (!res) { - og_dbi_free_computer_info(&computer); og_dbi_close(dbi); syslog(LOG_ERR, "Problem updating client configuration\n"); return -1; } - og_dbi_free_computer_info(&computer); res = actualizaCreacionImagen(dbi, img_legacy.image_id, @@ -609,8 +598,6 @@ static int og_resp_image_restore(json_t *data, struct og_client *cli) snprintf(img_legacy.disk, sizeof(img_legacy.disk), "%s", disk); snprintf(soft_legacy.id, sizeof(soft_legacy.id), "%d", computer.id); - og_dbi_free_computer_info(&computer); - res = actualizaRestauracionImagen(dbi, img_legacy.image_id, img_legacy.disk, @@ -13,6 +13,7 @@ #include "dbi.h" #include <syslog.h> #include <string.h> +#include <stdio.h> struct og_dbi *og_dbi_open(struct og_dbi_config *config) { @@ -93,19 +94,28 @@ int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer, } computer->id = dbi_result_get_uint(result, "idordenador"); - computer->name = strdup(dbi_result_get_string(result, "nombreordenador")); - computer->serial_number = strdup(dbi_result_get_string(result, "numserie")); - computer->ip = strdup(dbi_result_get_string(result, "ip")); - computer->mac = strdup(dbi_result_get_string(result, "mac")); + snprintf(computer->name, sizeof(computer->name), "%s", + dbi_result_get_string(result, "nombreordenador")); + snprintf(computer->serial_number, sizeof(computer->serial_number), "%s", + dbi_result_get_string(result, "numserie")); + snprintf(computer->ip, sizeof(computer->ip), "%s", + dbi_result_get_string(result, "ip")); + snprintf(computer->mac, sizeof(computer->mac), "%s", + dbi_result_get_string(result, "mac")); computer->room = dbi_result_get_uint(result, "idaula"); computer->hardware_id = dbi_result_get_uint(result, "idperfilhard"); computer->repo_id = dbi_result_get_uint(result, "idrepositorio"); - computer->netmask = strdup(dbi_result_get_string(result, "mascara")); - computer->boot = strdup(dbi_result_get_string(result, "arranque")); - computer->netiface = strdup(dbi_result_get_string(result, "netiface")); - computer->netdriver = strdup(dbi_result_get_string(result, "netdriver")); + snprintf(computer->netmask, sizeof(computer->netmask), "%s", + dbi_result_get_string(result, "mascara")); + snprintf(computer->boot, sizeof(computer->boot), "%s", + dbi_result_get_string(result, "arranque")); + snprintf(computer->netiface, sizeof(computer->netiface), "%s", + dbi_result_get_string(result, "netiface")); + snprintf(computer->netdriver, sizeof(computer->netdriver), "%s", + dbi_result_get_string(result, "netdriver")); computer->procedure_id = dbi_result_get_uint(result, "idproautoexec"); - computer->livedir = strdup(dbi_result_get_string(result, "oglivedir")); + snprintf(computer->livedir, sizeof(computer->livedir), "%s", + dbi_result_get_string(result, "oglivedir")); computer->remote = dbi_result_get_uint(result, "inremotepc") != 0; computer->maintenance = dbi_result_get_uint(result, "maintenance") != 0; @@ -113,16 +123,3 @@ int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer, return 0; } - -void og_dbi_free_computer_info(struct og_computer *computer) -{ - free(computer->serial_number); - free(computer->netdriver); - free(computer->netiface); - free(computer->netmask); - free(computer->livedir); - free(computer->name); - free(computer->boot); - free(computer->mac); - free(computer->ip); -} @@ -23,10 +23,16 @@ void og_dbi_close(struct og_dbi *db); #define OG_DB_COMPUTER_NAME_MAXLEN 100 #define OG_DB_CENTER_NAME_MAXLEN 100 #define OG_DB_ROOM_NAME_MAXLEN 100 +#define OG_DB_SERIAL_NUMBER_MAXLEN 25 #define OG_DB_IMAGE_NAME_MAXLEN 50 #define OG_DB_FILESYSTEM_MAXLEN 16 +#define OG_DB_NETDRIVER_MAXLEN 30 +#define OG_DB_NETIFACE_MAXLEN 4 +#define OG_DB_LIVEDIR_MAXLEN 50 #define OG_DB_INT8_MAXLEN 8 +#define OG_DB_BOOT_MAXLEN 30 #define OG_DB_INT_MAXLEN 11 +#define OG_DB_MAC_MAXLEN 15 #define OG_DB_IP_MAXLEN 15 #define OG_DB_SMALLINT_MAXLEN 6 @@ -59,20 +65,19 @@ struct og_computer { unsigned int id; bool maintenance; bool remote; - char *serial_number; - char *netdriver; - char *netiface; - char *netmask; - char *livedir; - char *name; - char *boot; - char *mac; - char *ip; + char serial_number[OG_DB_SERIAL_NUMBER_MAXLEN + 1]; + char netdriver[OG_DB_NETDRIVER_MAXLEN + 1]; + char name[OG_DB_COMPUTER_NAME_MAXLEN + 1]; + char netiface[OG_DB_NETIFACE_MAXLEN + 1]; + char livedir[OG_DB_LIVEDIR_MAXLEN + 1]; + char netmask[OG_DB_IP_MAXLEN + 1]; + char boot[OG_DB_BOOT_MAXLEN + 1]; + char mac[OG_DB_MAC_MAXLEN + 1]; + char ip[OG_DB_IP_MAXLEN + 1]; }; struct in_addr; int og_dbi_get_computer_info(struct og_dbi *dbi, struct og_computer *computer, struct in_addr addr); -void og_dbi_free_computer_info(struct og_computer *computer); #endif @@ -1214,7 +1214,6 @@ static int og_cmd_get_client_info(json_t *element, json_object_set_new(root, "id", json_integer(computer.id)); json_object_set_new(root, "ip", json_string(computer.ip)); - og_dbi_free_computer_info(&computer); json_dump_callback(root, og_json_dump_clients, &og_buffer, 0); json_decref(root); return 0; @@ -1224,8 +1223,8 @@ static int og_cmd_post_client_add(json_t *element, struct og_msg_params *params, char *buffer_reply) { - const char *key, *str, *msglog; struct og_computer computer = {}; + const char *key, *msglog; struct og_dbi *dbi; dbi_result result; json_t *value; @@ -1233,42 +1232,51 @@ static int og_cmd_post_client_add(json_t *element, json_object_foreach(element, key, value) { if (!strcmp(key, "serial_number")) { - err = og_json_parse_string(value, &str); - computer.serial_number = strdup(str); + err = og_json_parse_string_copy(value, + computer.serial_number, + sizeof(computer.serial_number)); } else if (!strcmp(key, "hardware_id")) { err = og_json_parse_uint(value, &computer.hardware_id); } else if (!strcmp(key, "netdriver")) { - err = og_json_parse_string(value, &str); - computer.netdriver = strdup(str); + err = og_json_parse_string_copy(value, + computer.netdriver, + sizeof(computer.netdriver)); } else if (!strcmp(key, "maintenance")) { err = og_json_parse_bool(value, &computer.maintenance); } else if (!strcmp(key, "netiface")) { - err = og_json_parse_string(value, &str); - computer.netiface = strdup(str); + err = og_json_parse_string_copy(value, + computer.netiface, + sizeof(computer.netiface)); } else if (!strcmp(key, "repo_id")) { err = og_json_parse_uint(value, &computer.repo_id); } else if (!strcmp(key, "livedir")) { - err = og_json_parse_string(value, &str); - computer.livedir = strdup(str); + err = og_json_parse_string_copy(value, + computer.livedir, + sizeof(computer.livedir)); } else if (!strcmp(key, "netmask")) { - err = og_json_parse_string(value, &str); - computer.netmask = strdup(str); + err = og_json_parse_string_copy(value, + computer.netmask, + sizeof(computer.netmask)); } else if (!strcmp(key, "remote")) { err = og_json_parse_bool(value, &computer.remote); } else if (!strcmp(key, "room")) { err = og_json_parse_uint(value, &computer.room); } else if (!strcmp(key, "name")) { - err = og_json_parse_string(value, &str); - computer.name = strdup(str); + err = og_json_parse_string_copy(value, + computer.name, + sizeof(computer.name)); } else if (!strcmp(key, "boot")) { - err = og_json_parse_string(value, &str); - computer.boot = strdup(str); + err = og_json_parse_string_copy(value, + computer.boot, + sizeof(computer.boot)); } else if (!strcmp(key, "mac")) { - err = og_json_parse_string(value, &str); - computer.mac = strdup(str); + err = og_json_parse_string_copy(value, + computer.mac, + sizeof(computer.mac)); } else if (!strcmp(key, "ip")) { - err = og_json_parse_string(value, &str); - computer.ip = strdup(str); + err = og_json_parse_string_copy(value, + computer.ip, + sizeof(computer.ip)); } if (err < 0) @@ -1279,7 +1287,6 @@ static int og_cmd_post_client_add(json_t *element, if (!dbi) { syslog(LOG_ERR, "cannot open conection database (%s:%d)\n", __func__, __LINE__); - og_dbi_free_computer_info(&computer); return -1; } @@ -1292,7 +1299,6 @@ static int og_cmd_post_client_add(json_t *element, syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", __func__, __LINE__, msglog); og_dbi_close(dbi); - og_dbi_free_computer_info(&computer); return -1; } @@ -1301,7 +1307,6 @@ static int og_cmd_post_client_add(json_t *element, computer.ip); dbi_result_free(result); og_dbi_close(dbi); - og_dbi_free_computer_info(&computer); return -1; } dbi_result_free(result); @@ -1338,13 +1343,11 @@ static int og_cmd_post_client_add(json_t *element, syslog(LOG_ERR, "failed to add client to database (%s:%d) %s\n", __func__, __LINE__, msglog); og_dbi_close(dbi); - og_dbi_free_computer_info(&computer); return -1; } dbi_result_free(result); og_dbi_close(dbi); - og_dbi_free_computer_info(&computer); return 0; } |