diff options
author | smitsohu <smitsohu@gmail.com> | 2021-01-20 15:50:54 +0100 |
---|---|---|
committer | smitsohu <smitsohu@gmail.com> | 2021-01-20 15:50:54 +0100 |
commit | 9399673618f62a24ba944c771fdf2fed085ad8dd (patch) | |
tree | 68b429cc2d5c69593c145a81f64a89ba21a5b39b /src/fcopy/main.c | |
parent | private-lib: simplify installation of stdc (diff) | |
download | firejail-9399673618f62a24ba944c771fdf2fed085ad8dd.tar.gz firejail-9399673618f62a24ba944c771fdf2fed085ad8dd.tar.zst firejail-9399673618f62a24ba944c771fdf2fed085ad8dd.zip |
misc fcopy fixes
Diffstat (limited to 'src/fcopy/main.c')
-rw-r--r-- | src/fcopy/main.c | 27 |
1 files changed, 11 insertions, 16 deletions
diff --git a/src/fcopy/main.c b/src/fcopy/main.c index 0a4a61e2a..e65501d6d 100644 --- a/src/fcopy/main.c +++ b/src/fcopy/main.c | |||
@@ -111,7 +111,7 @@ static void copy_file(const char *srcname, const char *destname, mode_t mode, ui | |||
111 | } | 111 | } |
112 | 112 | ||
113 | // open destination | 113 | // open destination |
114 | int dst = open(destname, O_CREAT|O_WRONLY|O_TRUNC, 0755); | 114 | int dst = open(destname, O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR | S_IWUSR); |
115 | if (dst < 0) { | 115 | if (dst < 0) { |
116 | if (!arg_quiet) | 116 | if (!arg_quiet) |
117 | fprintf(stderr, "Warning fcopy: cannot open %s, file not copied\n", destname); | 117 | fprintf(stderr, "Warning fcopy: cannot open %s, file not copied\n", destname); |
@@ -132,7 +132,8 @@ static void copy_file(const char *srcname, const char *destname, mode_t mode, ui | |||
132 | done += rv; | 132 | done += rv; |
133 | } | 133 | } |
134 | } | 134 | } |
135 | fflush(0); | 135 | if (len < 0) |
136 | goto errexit; | ||
136 | 137 | ||
137 | if (fchown(dst, uid, gid) == -1) | 138 | if (fchown(dst, uid, gid) == -1) |
138 | goto errexit; | 139 | goto errexit; |
@@ -179,7 +180,7 @@ void copy_link(const char *target, const char *linkpath, mode_t mode, uid_t uid, | |||
179 | 180 | ||
180 | // if the link is already there, don't create it | 181 | // if the link is already there, don't create it |
181 | struct stat s; | 182 | struct stat s; |
182 | if (stat(linkpath, &s) == 0) | 183 | if (lstat(linkpath, &s) == 0) |
183 | return; | 184 | return; |
184 | 185 | ||
185 | char *rp = realpath(target, NULL); | 186 | char *rp = realpath(target, NULL); |
@@ -413,25 +414,19 @@ int main(int argc, char **argv) { | |||
413 | 414 | ||
414 | warn_dumpable(); | 415 | warn_dumpable(); |
415 | 416 | ||
416 | // trim trailing chars | ||
417 | if (src[strlen(src) - 1] == '/') | ||
418 | src[strlen(src) - 1] = '\0'; | ||
419 | if (dest[strlen(dest) - 1] == '/') | ||
420 | dest[strlen(dest) - 1] = '\0'; | ||
421 | |||
422 | // check the two files; remove ending / | 417 | // check the two files; remove ending / |
423 | int len = strlen(src); | 418 | size_t len = strlen(src); |
424 | if (src[len - 1] == '/') | 419 | while (len > 1 && src[len - 1] == '/') |
425 | src[len - 1] = '\0'; | 420 | src[--len] = '\0'; |
426 | if (strcspn(src, "\\*&!?\"'<>%^(){}[];,") != (size_t)len) { | 421 | if (strcspn(src, "\\*&!?\"'<>%^(){}[];,") != len) { |
427 | fprintf(stderr, "Error fcopy: invalid source file name %s\n", src); | 422 | fprintf(stderr, "Error fcopy: invalid source file name %s\n", src); |
428 | exit(1); | 423 | exit(1); |
429 | } | 424 | } |
430 | 425 | ||
431 | len = strlen(dest); | 426 | len = strlen(dest); |
432 | if (dest[len - 1] == '/') | 427 | while (len > 1 && dest[len - 1] == '/') |
433 | dest[len - 1] = '\0'; | 428 | dest[--len] = '\0'; |
434 | if (strcspn(dest, "\\*&!?\"'<>%^(){}[];,~") != (size_t)len) { | 429 | if (strcspn(dest, "\\*&!?\"'<>%^(){}[];,~") != len) { |
435 | fprintf(stderr, "Error fcopy: invalid dest file name %s\n", dest); | 430 | fprintf(stderr, "Error fcopy: invalid dest file name %s\n", dest); |
436 | exit(1); | 431 | exit(1); |
437 | } | 432 | } |