From cee0a4d375092f5c9e1097ce056dd10435b33e49 Mon Sep 17 00:00:00 2001 From: netblue30 Date: Thu, 17 Nov 2016 08:04:52 -0500 Subject: testing --- src/ftee/main.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) (limited to 'src/ftee/main.c') diff --git a/src/ftee/main.c b/src/ftee/main.c index e6aa5f567..2b27baa5a 100644 --- a/src/ftee/main.c +++ b/src/ftee/main.c @@ -179,10 +179,6 @@ static int is_link(const char *fname) { return 0; } - - - - static void usage(void) { printf("Usage: ftee filename\n"); } @@ -201,33 +197,25 @@ int main(int argc, char **argv) { // do not accept directories, links, and files with ".." - if (strstr(fname, "..") || is_link(fname) || is_dir(fname)) { - fprintf(stderr, "Error: invalid output file. Links, directories and files with \"..\" are not allowed.\n"); - exit(1); - } + if (strstr(fname, "..") || is_link(fname) || is_dir(fname)) + goto errexit; struct stat s; if (stat(fname, &s) == 0) { // check permissions - if (s.st_uid != getuid() || s.st_gid != getgid()) { - fprintf(stderr, "Error: the output file needs to be owned by the current user.\n"); - exit(1); - } + if (s.st_uid != getuid() || s.st_gid != getgid()) + goto errexit; // check hard links - if (s.st_nlink != 1) { - fprintf(stderr, "Error: no hard links allowed.\n"); - exit(1); - } + if (s.st_nlink != 1) + goto errexit; } // check if we can append to this file /* coverity[toctou] */ FILE *fp = fopen(fname, "a"); - if (!fp) { - fprintf(stderr, "Error: cannot open output file %s\n", fname); - exit(1); - } + if (!fp) + goto errexit; fclose(fp); @@ -248,4 +236,8 @@ int main(int argc, char **argv) { log_close(); return 0; + +errexit: + fprintf(stderr, "Error ftee: invalid output file.\n"); + return 1; } -- cgit v1.2.3-54-g00ecf