Skip to content

Commit

Permalink
fix config dir handling. allow for relative paths, back pathing etc
Browse files Browse the repository at this point in the history
  • Loading branch information
dovholuknf committed Dec 13, 2024
1 parent 2421d32 commit 44bdb2b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
25 changes: 25 additions & 0 deletions programs/ziti-edge-tunnel/config-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,28 @@ void update_identity_config(uv_loop_t *l, const char *identifier, const char *cf
}
}

char* resolve_directory(const char* path) {
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?
printf("path does not exist or permission denied: %s\n", resolved_path);
exit(1);
}
} else {
char* current_dir = getcwd(NULL, 0);
if (current_dir == NULL) {
printf("getcwd failed?");
exit(1);
}

if (realpath(path, resolved_path) == NULL) {
printf("path does not exist or permission denied: %s\n", resolved_path);
exit(1);
}
free(current_dir);
}
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
6 changes: 3 additions & 3 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,7 +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)) {
if (!realpath(config_dir, NULL)) {
ZITI_LOG(ERROR, "Failed to resolve base directory");
return;
}
Expand Down

0 comments on commit 44bdb2b

Please sign in to comment.