From: Alex Stancu Date: Thu, 18 Jun 2020 10:51:26 +0000 (+0000) Subject: Merge "When env variable SCRIPTS_DIR is unset the code reads or writes from a path... X-Git-Tag: 1.0.3~16 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?p=sim%2Fo1-interface.git;a=commitdiff_plain;h=3472441e3d90935c770f84c449b6541784afd6a5;hp=-c Merge "When env variable SCRIPTS_DIR is unset the code reads or writes from a path NULL. I suggest to fix this and use a default instead." --- 3472441e3d90935c770f84c449b6541784afd6a5 diff --combined ntsimulator/src/utils/utils.c index 7c7f56c,5242f6d..217675b --- a/ntsimulator/src/utils/utils.c +++ b/ntsimulator/src/utils/utils.c @@@ -410,7 -410,26 +410,26 @@@ char* readConfigFileInString(void char * buffer = 0; long length; char config_file[200]; - sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR")); + + //sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR")); + + // --> recommended fix, Karl Koch, Deutsche Telekom AG, 22. 5. 2020. + // Path to config_file contains NULL when env variable is unset. + char *scripts_dir = getenv("SCRIPTS_DIR"); + char *scripts_dir_default = "/opt/dev/ntsimulator/scripts"; + + if(NULL != scripts_dir) + { + sprintf(config_file, "%s/configuration.json", scripts_dir); + } + else + { + sprintf(config_file, "%s/configuration.json", scripts_dir_default); + printf("warning: opening config file in default path: <%s>\n", + config_file); + } + // end of fix <-- + FILE * f = fopen (config_file, "rb"); if (f) @@@ -438,7 -457,26 +457,26 @@@ void writeConfigFile(char *config) { char config_file[200]; - sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR")); + + //sprintf(config_file, "%s/configuration.json", getenv("SCRIPTS_DIR")); + + // --> recommended fix, Karl Koch, Deutsche Telekom AG, 22. 5. 2020. + // Path to config_file contains NULL when env variable is unset. + char *scripts_dir = getenv("SCRIPTS_DIR"); + char *scripts_dir_default = "/opt/dev/ntsimulator/scripts"; + + if(NULL != scripts_dir) + { + sprintf(config_file, "%s/configuration.json", scripts_dir); + } + else + { + sprintf(config_file, "%s/configuration.json", scripts_dir_default); + printf("warning: opening config file in default path: <%s>\n", + config_file); + } + // end of fix <-- + FILE * f = fopen (config_file, "w"); if (f) @@@ -2044,50 -2082,6 +2082,50 @@@ int writeSkeletonConfigFile( return SR_ERR_OPERATION_FAILED; } + cJSON *controllerDetails = cJSON_CreateObject(); + if (controllerDetails == NULL) + { + printf("Could not create JSON object: controllerDetails\n"); + cJSON_Delete(configObject); + return SR_ERR_OPERATION_FAILED; + } + cJSON_AddItemToObject(configObject, "controller-details", controllerDetails); + + if (cJSON_AddStringToObject(controllerDetails, "controller-ip", "172.17.0.1") == NULL) + { + printf("Could not create JSON object: controller-ip\n"); + cJSON_Delete(configObject); + return SR_ERR_OPERATION_FAILED; + } + + if (cJSON_AddNumberToObject(controllerDetails, "controller-port", 8181) == NULL) + { + printf("Could not create JSON object: controller-port\n"); + cJSON_Delete(configObject); + return SR_ERR_OPERATION_FAILED; + } + + if (cJSON_AddNumberToObject(controllerDetails, "netconf-call-home-port", 6666) == NULL) + { + printf("Could not create JSON object: netconf-call-home-port\n"); + cJSON_Delete(configObject); + return SR_ERR_OPERATION_FAILED; + } + + if (cJSON_AddStringToObject(controllerDetails, "controller-username", "admin") == NULL) + { + printf("Could not create JSON object: controller-username\n"); + cJSON_Delete(configObject); + return SR_ERR_OPERATION_FAILED; + } + + if (cJSON_AddStringToObject(controllerDetails, "controller-password", "admin") == NULL) + { + printf("Could not create JSON object: controller-password\n"); + cJSON_Delete(configObject); + return SR_ERR_OPERATION_FAILED; + } + if (cJSON_AddNumberToObject(configObject, "ssh-connections", 1) == NULL) { printf("Could not create JSON object: ssh-connections\n"); @@@ -2102,13 -2096,6 +2140,13 @@@ return SR_ERR_OPERATION_FAILED; } + if (cJSON_AddFalseToObject(configObject, "netconf-call-home") == NULL) + { + printf("Could not create JSON object: netconf-call-home\n"); + cJSON_Delete(configObject); + return SR_ERR_OPERATION_FAILED; + } + char *config_string = NULL; config_string = cJSON_PrintUnformatted(configObject);