diff --git a/include/neuron/utils/neu_jwt.h b/include/neuron/utils/neu_jwt.h index 298d3052..d77241b5 100644 --- a/include/neuron/utils/neu_jwt.h +++ b/include/neuron/utils/neu_jwt.h @@ -27,7 +27,7 @@ extern "C" { extern bool disable_jwt; -int neu_jwt_init(char *dir_path); +int neu_jwt_init(const char *dir_path); int neu_jwt_new(char **token); int neu_jwt_validate(char *b_token); void neu_jwt_destroy(); diff --git a/plugins/restful/license.h b/plugins/restful/license.h index d465c308..a3be2eeb 100644 --- a/plugins/restful/license.h +++ b/plugins/restful/license.h @@ -20,7 +20,6 @@ #ifndef LICENSE_H #define LICENSE_H -#define LICENSE_DIR "config" #define LICENSE_FNAME "neuron.lic" // License type diff --git a/plugins/restful/license_handle.c b/plugins/restful/license_handle.c index 8ff050dd..c07f5c63 100644 --- a/plugins/restful/license_handle.c +++ b/plugins/restful/license_handle.c @@ -22,6 +22,7 @@ #include #include +#include "argparse.h" #include "errcodes.h" #include "parser/neu_json_license.h" #include "plugin.h" @@ -34,15 +35,25 @@ #include "license.h" #include "license_handle.h" -#define LICENSE_PATH LICENSE_DIR "/" LICENSE_FNAME - static int set_license(const char *lic_str); int backup_license_file(const char *lic_fname); int get_plugin_names(const license_t *lic, UT_array *plugin_names); +static inline char *get_license_path() +{ + size_t n = 1 + snprintf(NULL, 0, "%s/%s", g_config_dir, LICENSE_FNAME); + char * p = malloc(n); + if (NULL == p) { + return NULL; + } + snprintf(p, n, "%s/%s", g_config_dir, LICENSE_FNAME); + return p; +} + void handle_get_license(nng_aio *aio) { int rv = 0; + char * license_path = NULL; license_t lic = {}; neu_json_get_license_resp_t resp = {}; time_t since = 0; @@ -56,8 +67,15 @@ void handle_get_license(nng_aio *aio) VALIDATE_JWT(aio); utarray_new(plugin_names, &ut_ptr_icd); + + license_path = get_license_path(); + if (NULL == license_path) { + rv = NEU_ERR_EINTERNAL; + goto final; + } + license_init(&lic); - rv = license_read(&lic, LICENSE_PATH); + rv = license_read(&lic, license_path); if (0 != rv) { goto final; } @@ -111,6 +129,7 @@ final: license_fini(&lic); utarray_free(plugin_names); free(resp.enabled_plugins); + free(license_path); } void handle_set_license(nng_aio *aio) @@ -130,20 +149,39 @@ static inline bool file_exists(const char *const path) static int set_license(const char *lic_str) { - int rv = 0; - license_t new_lic = {}; - const char *fname_tmp = LICENSE_PATH ".tmp"; + int rv = 0; + size_t len = 0; + const char *suffix = ".tmp"; + char * license_path = NULL; + char * fname_tmp = NULL; + FILE * fp = NULL; + license_t new_lic = {}; - int len = strlen(lic_str); - if (len > 15000) { - // the license file won't exceed 15KB - return NEU_ERR_LICENSE_INVALID; + license_path = get_license_path(); + if (NULL == license_path) { + return NEU_ERR_EINTERNAL; } - FILE *fp = fopen(fname_tmp, "w"); + len = strlen(license_path) + strlen(suffix) + 1; + fname_tmp = malloc(len); + if (NULL == fname_tmp) { + rv = NEU_ERR_EINTERNAL; + goto final; + } + snprintf(fname_tmp, len, "%s%s", license_path, suffix); + + len = strlen(lic_str); + if (len > 15000) { + // the license file won't exceed 15KB + rv = NEU_ERR_LICENSE_INVALID; + goto final; + } + + fp = fopen(fname_tmp, "w"); if (NULL == fp) { nlog_error("unable to create license tmp file `%s`", fname_tmp); - return NEU_ERR_EINTERNAL; + rv = NEU_ERR_EINTERNAL; + goto final; } rv = fputs(lic_str, fp); @@ -170,13 +208,13 @@ static int set_license(const char *lic_str) goto final; } - if (file_exists(LICENSE_PATH) && 0 > backup_license_file(LICENSE_PATH)) { - nlog_error("unable to backup license file `%s`", LICENSE_PATH); + if (file_exists(license_path) && 0 > backup_license_file(license_path)) { + nlog_error("unable to backup license file `%s`", license_path); rv = NEU_ERR_EINTERNAL; goto final; } - if (0 > rename(fname_tmp, LICENSE_PATH)) { + if (0 > rename(fname_tmp, license_path)) { rv = NEU_ERR_EINTERNAL; goto final; } @@ -189,29 +227,39 @@ static int set_license(const char *lic_str) } final: - fclose(fp); + if (fp) { + fclose(fp); + } remove(fname_tmp); + free(license_path); + free(fname_tmp); return rv; } int backup_license_file(const char *lic_fname) { - struct tm tm = { .tm_isdst = -1 }; - time_t now = time(NULL); - char fname_bak[128] = { 0 }; + struct tm tm = { .tm_isdst = -1 }; + time_t now = time(NULL); + const char *suffix = ".bak"; + size_t len = + strlen(lic_fname) + strlen(suffix) + 15 + 1; // 15 for timestamp + char *fname_bak = malloc(len); - size_t n = snprintf(fname_bak, sizeof(fname_bak), "%s.bak", lic_fname); - if (sizeof(fname_bak) == n) { - return -1; + if (NULL == fname_bak) { + return NEU_ERR_EINTERNAL; } + size_t n = snprintf(fname_bak, len, "%s.bak", lic_fname); + gmtime_r(&now, &tm); - if (0 == - strftime(fname_bak + n, sizeof(fname_bak) - n, ".%Y%m%d%H%M%S", &tm)) { + if (0 == strftime(fname_bak + n, len - n, ".%Y%m%d%H%M%S", &tm)) { + free(fname_bak); return -1; } - return link(lic_fname, fname_bak); + int rv = link(lic_fname, fname_bak); + free(fname_bak); + return rv; } int get_plugin_names(const license_t *lic, UT_array *plugin_names) diff --git a/plugins/restful/normal_handle.c b/plugins/restful/normal_handle.c index 4a2b8b1f..b4dd4adf 100644 --- a/plugins/restful/normal_handle.c +++ b/plugins/restful/normal_handle.c @@ -30,6 +30,7 @@ #include "utils/log.h" +#include "argparse.h" #include "parser/neu_json_login.h" #include "json/neu_json_fn.h" @@ -129,8 +130,8 @@ void handle_get_plugin_schema(nng_aio *aio) return; } - snprintf(schema_path, sizeof(schema_path) - 1, "./plugins/schema/%s.json", - plugin_name); + snprintf(schema_path, sizeof(schema_path) - 1, "%s/schema/%s.json", + g_plugin_dir, plugin_name); char *buf = NULL; buf = file_string_read(&len, schema_path); diff --git a/plugins/restful/rest.c b/plugins/restful/rest.c index 437f538d..3610bea9 100644 --- a/plugins/restful/rest.c +++ b/plugins/restful/rest.c @@ -25,6 +25,7 @@ #include #include "adapter_handle.h" +#include "argparse.h" #include "datatag_handle.h" #include "define.h" #include "group_config_handle.h" @@ -219,7 +220,7 @@ static int dashb_plugin_init(neu_plugin_t *plugin) (void) plugin; - rv = neu_jwt_init("./config"); + rv = neu_jwt_init(g_config_dir); nlog_info("Initialize plugin: %s", neu_plugin_module.module_name); return rv; diff --git a/src/core/plugin_manager.c b/src/core/plugin_manager.c index da48faf8..c24b0780 100644 --- a/src/core/plugin_manager.c +++ b/src/core/plugin_manager.c @@ -29,6 +29,7 @@ #include "restful/rest.h" +#include "argparse.h" #include "plugin_manager.h" typedef struct plugin_entity { @@ -246,7 +247,7 @@ int neu_plugin_manager_create_instance(neu_plugin_manager_t * mgr, if (plugin != NULL) { char lib_path[256] = { 0 }; - snprintf(lib_path, sizeof(lib_path) - 1, "./plugins/%s", + snprintf(lib_path, sizeof(lib_path) - 1, "%s/%s", g_plugin_dir, plugin->lib_name); instance->handle = dlopen(lib_path, RTLD_NOW | RTLD_NODELETE); assert(instance->handle != NULL); diff --git a/src/persist/persist.c b/src/persist/persist.c index 8ac6014c..adfca27b 100644 --- a/src/persist/persist.c +++ b/src/persist/persist.c @@ -34,6 +34,7 @@ #include "errcodes.h" #include "utils/log.h" +#include "argparse.h" #include "persist/json/persist_json_plugin.h" #include "persist/persist.h" @@ -616,7 +617,7 @@ neu_persister_t *neu_persister_create(const char *dir_name) sqlite3_close(persister->db); goto error; } - if (0 != apply_schemas(persister->db, "config/")) { + if (0 != apply_schemas(persister->db, g_config_dir)) { goto error; } diff --git a/src/utils/neu_jwt.c b/src/utils/neu_jwt.c index 636b220c..7025294d 100644 --- a/src/utils/neu_jwt.c +++ b/src/utils/neu_jwt.c @@ -64,7 +64,7 @@ static void add_key(char *name, char *value) sizeof(key_store.key[key_store.size - 1].key) - 1); } -static char *load_key(char *dir, char *name) +static char *load_key(const char *dir, char *name) { FILE * f = NULL; int len = 0; @@ -92,7 +92,7 @@ static char *load_key(char *dir, char *name) return content; } -int neu_jwt_init(char *dir_path) +int neu_jwt_init(const char *dir_path) { DIR * dir = NULL; struct dirent *ptr = NULL;