diff options
| author | Dennis Camera <skonfig@dtnr.ch> | 2022-08-20 23:24:58 +0200 |
|---|---|---|
| committer | Dennis Camera <skonfig@dtnr.ch> | 2022-08-20 23:32:05 +0200 |
| commit | b307c6010c7439191064e80da04b6e1672b2001e (patch) | |
| tree | e19aa5146ce08ed69edd473e0c87f4aa56e8ff8f /src | |
| parent | c3236907ef834555e460daea38369a4874ae5d5d (diff) | |
| download | skonfig-c-b307c6010c7439191064e80da04b6e1672b2001e.tar.gz skonfig-c-b307c6010c7439191064e80da04b6e1672b2001e.zip | |
[src/run.c] Use pathjoin_r for simpler logic
Diffstat (limited to 'src')
| -rw-r--r-- | src/run.c | 48 |
1 files changed, 30 insertions, 18 deletions
@@ -160,40 +160,47 @@ sk_run_error sk_run_prepare_local_workdir(const char *target_host, const char *w "bin", "conf", "explorer", "object", "stdout", "stderr" }; - sk_run_error rv = SK_RUN_OK; - char *host_dir = pathjoin(workdir, target_host); + char host_dir[PATH_MAX+1], subpath[PATH_MAX+1]; + + if (PATH_MAX < pathjoin_r(host_dir, PATH_MAX+1, workdir, target_host)) { + goto path_overflow; + } /* create host_dir */ if (mkdir(host_dir, S_IRWXU)) { sk_error("failed to create host directory (%s): %s", host_dir, strerror(errno)); - rv = SK_RUN_GENERIC_ERROR; - goto ret; + return SK_RUN_GENERIC_ERROR; } /* create sub directories of host_dir */ for (size_t i = 0; i < arraylen(subdirs); ++i) { - char *subdir = pathjoin(host_dir, subdirs[i]); - if (mkdir(subdir, S_IRWXU)) { + if (PATH_MAX < pathjoin_r(subpath, PATH_MAX+1, host_dir, subdirs[i])) { + goto path_overflow; + } + + if (mkdir(subpath, S_IRWXU)) { sk_error("failed to create directory (%s): %s", - subdir, strerror(errno)); - rv = SK_RUN_GENERIC_ERROR; + subpath, strerror(errno)); + return SK_RUN_GENERIC_ERROR; } - free(subdir); } /* create messages file */ - char *messages = pathjoin(host_dir, "messages"); - if (0 > creatfile(messages, S_IRUSR | S_IWUSR)) { + if (PATH_MAX < pathjoin_r(subpath, PATH_MAX+1, host_dir, "messages")) { + goto path_overflow; + } + if (0 > creatfile(subpath, S_IRUSR | S_IWUSR)) { sk_error("failed to create %s: %s", - messages, strerror(errno)); - rv = SK_RUN_GENERIC_ERROR; + subpath, strerror(errno)); + return SK_RUN_GENERIC_ERROR; } - free(messages); - ret: - free(host_dir); - return rv; + return SK_RUN_OK; + + path_overflow: + sk_error("generated local workdir path is longer than PATH_MAX"); + return SK_RUN_GENERIC_ERROR; } sk_run_error sk_run_init_manifest( @@ -243,12 +250,17 @@ sk_run_error sk_run_start( return SK_RUN_GENERIC_ERROR; } sk_debug("%s: local work directory: %s", target_host, local_workdir); - sk_run_prepare_local_workdir(target_host, local_workdir); + rv = sk_run_prepare_local_workdir(target_host, local_workdir); + + if (SK_RUN_OK != rv) { + goto abort; + } char *host_workdir = pathjoin(local_workdir, target_host); rv = sk_run_host(target_host, dry_run, host_workdir, config); free(host_workdir); + abort: if (SK_RUN_OK != rv) { sk_error("failed to configure the following host: %s", target_host); } else { |
