Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix config dir handling. allow for relative paths, back pathing etc #1067

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions programs/ziti-edge-tunnel/config-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
#include <pwd.h>
#include <unistd.h>
#endif
#if _WIN32
#ifndef PATH_MAX //normalize to PATH_MAX even on vs 2022 and arm
#ifdef MAX_PATH
#define PATH_MAX MAX_PATH
#else
#error "PATH_MAX and MAX_PATH are not defined, PATH_MAX cannot be set
#endif
#endif
#define realpath(rel, abs) _fullpath(abs, rel, PATH_MAX)
#endif

typedef struct api_update_req_s {
uv_work_t wr;
Expand Down Expand Up @@ -138,3 +148,28 @@ void update_identity_config(uv_loop_t *l, const char *identifier, const char *cf
}
}

char* resolve_directory(const char* path) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can/should be static since it's not referenced outside of this module. also whitespace.

char *resolved_path = (char *) malloc(PATH_MAX);
if (access(path, F_OK) != -1) {
//means the file exists right where it is, use realpath and normalize it and continue
if (realpath(path, resolved_path) == NULL) {
//how could we get here? seems like this shouldn't be possible but protect for it anyway
printf("path does not exist or permission denied: %s\n", resolved_path);
exit(1);
}
} else {
if (realpath(path, resolved_path) == NULL) {
printf("path does not exist or permission denied: %s\n", resolved_path);
exit(1);
}
// due to realpath not existing on windows, apparently `_fullpath` doesn't return NULL
// if the _fullpath doesn't exist... so this access is necessary for windows but redundant
// for linux/macOS
if (access(path, F_OK) == -1) {
printf("path does not exist or permission denied: %s\n", resolved_path);
exit(1);
}
}
return resolved_path;
}

1 change: 1 addition & 0 deletions programs/ziti-edge-tunnel/include/config-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
#include <uv.h>

void update_identity_config(uv_loop_t *l, const char *identifier, const char *cfg_json);
char* resolve_directory(const char* path);

#endif //ZITI_TUNNEL_SDK_C_CONFIG_UTILS_H
17 changes: 3 additions & 14 deletions programs/ziti-edge-tunnel/ziti-edge-tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ static int run_opts(int argc, char *argv[]) {
errors++;
break;
}
config_dir = optarg;
config_dir = resolve_directory(optarg);
identity_provided = true;
uses_config_dir = true;
break;
Expand Down Expand Up @@ -1250,7 +1250,7 @@ static int run_host_opts(int argc, char *argv[]) {
errors++;
break;
}
config_dir = optarg;
config_dir = resolve_directory(optarg);
identity_provided = true;
uses_config_dir = true;
break;
Expand Down Expand Up @@ -1324,18 +1324,7 @@ static void run(int argc, char *argv[]) {

// generate tunnel status instance and save active state and start time
if (config_dir != NULL) {
if (!realpath(config_dir, config_dir)) {
ZITI_LOG(ERROR, "Failed to resolve base directory");
return;
}

// if the config_dir was supplied but doesn't exist, exit...
struct stat st = {0};
if (stat(config_dir, &st) == -1) {
ZITI_LOG(ERROR, "cannot continue, specified config dir does not exist: %s", config_dir);
return;
}
if(config_file == NULL) {
if (config_file == NULL) {
config_file = calloc(PATH_MAX + 1, sizeof(char));
}
snprintf(config_file, PATH_MAX - 1, "%s%c%s", config_dir, PATH_SEP, "config.json");
Expand Down
Loading