diff options
| author | David M. Syzdek <syzdek@bindlebinaries.com> | 2019-07-09 17:04:40 -0800 |
|---|---|---|
| committer | David M. Syzdek <syzdek@bindlebinaries.com> | 2019-07-09 17:04:40 -0800 |
| commit | cdea9c13bac836ae8cad6bfdcf0ab88f18fdec30 (patch) | |
| tree | 440374577f9b14f619879380b6226e5302ceaf38 | |
| parent | 8e3ab7e6e6d1b1f97a0b8444466e5eced51a4ce2 (diff) | |
| download | securecoreutils-cdea9c13bac836ae8cad6bfdcf0ab88f18fdec30.tar.gz securecoreutils-cdea9c13bac836ae8cad6bfdcf0ab88f18fdec30.tar.bz2 securecoreutils-cdea9c13bac836ae8cad6bfdcf0ab88f18fdec30.tar.xz securecoreutils-cdea9c13bac836ae8cad6bfdcf0ab88f18fdec30.zip | |
Adding ability to ignore non-existent files and directories
| -rw-r--r-- | src/securecoreutils.c | 26 | ||||
| -rw-r--r-- | src/securecoreutils.h | 7 | ||||
| -rw-r--r-- | src/widget-pathcheck.c | 16 |
3 files changed, 34 insertions, 15 deletions
diff --git a/src/securecoreutils.c b/src/securecoreutils.c index 75cbdd0..233694e 100644 --- a/src/securecoreutils.c +++ b/src/securecoreutils.c | |||
| @@ -289,7 +289,7 @@ const char * scu_basename(const char * path) | |||
| 289 | 289 | ||
| 290 | 290 | ||
| 291 | /// checks paths | 291 | /// checks paths |
| 292 | int scu_pathcheck(const char * path, int isdir) | 292 | int scu_pathcheck(const char * path, int opts) |
| 293 | { | 293 | { |
| 294 | int c; | 294 | int c; |
| 295 | size_t s; | 295 | size_t s; |
| @@ -320,13 +320,19 @@ int scu_pathcheck(const char * path, int isdir) | |||
| 320 | 320 | ||
| 321 | // verify file is not a symbolic link | 321 | // verify file is not a symbolic link |
| 322 | if ((c = lstat(path, &sb)) == -1) | 322 | if ((c = lstat(path, &sb)) == -1) |
| 323 | return(SCU_ERRNO); | 323 | { |
| 324 | if (isdir == 0) | 324 | if ( ((opts & SCU_ONOTEXISTS) == 0) || (errno != ENOENT) ) |
| 325 | if ((sb.st_mode & S_IFMT) != S_IFREG) | 325 | return(SCU_ERRNO); |
| 326 | return(SCU_EFILE); | 326 | } |
| 327 | if (isdir != 0) | 327 | else |
| 328 | if ((sb.st_mode & S_IFMT) != S_IFDIR) | 328 | { |
| 329 | return(SCU_EDIR); | 329 | if ((opts & SCU_ODIR) == 0) |
| 330 | if ((sb.st_mode & S_IFMT) != S_IFREG) | ||
| 331 | return(SCU_EFILE); | ||
| 332 | if ((opts & SCU_ODIR) != 0) | ||
| 333 | if ((sb.st_mode & S_IFMT) != S_IFDIR) | ||
| 334 | return(SCU_EDIR); | ||
| 335 | }; | ||
| 330 | 336 | ||
| 331 | // check path for symlinks | 337 | // check path for symlinks |
| 332 | if ((str = strdup(path)) == NULL) | 338 | if ((str = strdup(path)) == NULL) |
| @@ -339,7 +345,9 @@ int scu_pathcheck(const char * path, int isdir) | |||
| 339 | if ((c = lstat(str, &sb)) == -1) | 345 | if ((c = lstat(str, &sb)) == -1) |
| 340 | { | 346 | { |
| 341 | free(str); | 347 | free(str); |
| 342 | return(SCU_ERRNO); | 348 | if ( ((opts & SCU_ONOTEXISTS) == 0) || (errno != ENOENT) ) |
| 349 | return(SCU_ERRNO); | ||
| 350 | return(0); | ||
| 343 | }; | 351 | }; |
| 344 | if ((sb.st_mode & S_IFMT) == S_IFLNK) | 352 | if ((sb.st_mode & S_IFMT) == S_IFLNK) |
| 345 | { | 353 | { |
diff --git a/src/securecoreutils.h b/src/securecoreutils.h index 694d911..72422b0 100644 --- a/src/securecoreutils.h +++ b/src/securecoreutils.h | |||
| @@ -113,6 +113,11 @@ | |||
| 113 | #define SCU_EDIR 5 | 113 | #define SCU_EDIR 5 |
| 114 | 114 | ||
| 115 | 115 | ||
| 116 | #define SCU_ONONE 0 | ||
| 117 | #define SCU_ODIR 1 | ||
| 118 | #define SCU_ONOTEXISTS 2 | ||
| 119 | |||
| 120 | |||
| 116 | ////////////////// | 121 | ////////////////// |
| 117 | // // | 122 | // // |
| 118 | // Data Types // | 123 | // Data Types // |
| @@ -157,7 +162,7 @@ struct scu_widget | |||
| 157 | #endif | 162 | #endif |
| 158 | 163 | ||
| 159 | /// checks paths | 164 | /// checks paths |
| 160 | int scu_pathcheck(const char * path, int isdir); | 165 | int scu_pathcheck(const char * path, int opts); |
| 161 | 166 | ||
| 162 | /// Displays secure core utils wrapper usage | 167 | /// Displays secure core utils wrapper usage |
| 163 | void scu_usage(scu_config * cnf); | 168 | void scu_usage(scu_config * cnf); |
diff --git a/src/widget-pathcheck.c b/src/widget-pathcheck.c index 77d670f..9c094cb 100644 --- a/src/widget-pathcheck.c +++ b/src/widget-pathcheck.c | |||
| @@ -72,13 +72,14 @@ int scu_widget_pathcheck(scu_config * cnf) | |||
| 72 | int c; | 72 | int c; |
| 73 | int opt_index; | 73 | int opt_index; |
| 74 | int rc; | 74 | int rc; |
| 75 | int isdir; | 75 | int opts; |
| 76 | 76 | ||
| 77 | // getopt options | 77 | // getopt options |
| 78 | static char short_opt[] = "+dhqVv"; | 78 | static char short_opt[] = "+dihqVv"; |
| 79 | static struct option long_opt[] = | 79 | static struct option long_opt[] = |
| 80 | { | 80 | { |
| 81 | {"help", no_argument, NULL, 'h' }, | 81 | {"help", no_argument, NULL, 'h' }, |
| 82 | {"ignore", no_argument, NULL, 'i' }, | ||
| 82 | {"quiet", no_argument, NULL, 'q' }, | 83 | {"quiet", no_argument, NULL, 'q' }, |
| 83 | {"silent", no_argument, NULL, 'q' }, | 84 | {"silent", no_argument, NULL, 'q' }, |
| 84 | {"version", no_argument, NULL, 'V' }, | 85 | {"version", no_argument, NULL, 'V' }, |
| @@ -90,7 +91,7 @@ int scu_widget_pathcheck(scu_config * cnf) | |||
| 90 | assert(cnf != NULL); | 91 | assert(cnf != NULL); |
| 91 | cnf->short_opt = short_opt; | 92 | cnf->short_opt = short_opt; |
| 92 | 93 | ||
| 93 | isdir = 0; | 94 | opts = 0; |
| 94 | 95 | ||
| 95 | while((c = getopt_long(cnf->argc, cnf->argv, short_opt, long_opt, &opt_index)) != -1) | 96 | while((c = getopt_long(cnf->argc, cnf->argv, short_opt, long_opt, &opt_index)) != -1) |
| 96 | { | 97 | { |
| @@ -101,7 +102,11 @@ int scu_widget_pathcheck(scu_config * cnf) | |||
| 101 | break; | 102 | break; |
| 102 | 103 | ||
| 103 | case 'd': | 104 | case 'd': |
| 104 | isdir = 1; | 105 | opts |= SCU_ODIR; |
| 106 | break; | ||
| 107 | |||
| 108 | case 'i': | ||
| 109 | opts |= SCU_ONOTEXISTS; | ||
| 105 | break; | 110 | break; |
| 106 | 111 | ||
| 107 | case 'h': | 112 | case 'h': |
| @@ -157,7 +162,7 @@ int scu_widget_pathcheck(scu_config * cnf) | |||
| 157 | return(1); | 162 | return(1); |
| 158 | }; | 163 | }; |
| 159 | 164 | ||
| 160 | if ((rc = scu_pathcheck(cnf->argv[optind], isdir)) != 0) | 165 | if ((rc = scu_pathcheck(cnf->argv[optind], opts)) != 0) |
| 161 | { | 166 | { |
| 162 | if (!(cnf->quiet)) | 167 | if (!(cnf->quiet)) |
| 163 | fprintf(stderr, "%s: %s: %s\n", PROGRAM_NAME, cnf->widget->name, scu_strerror(rc)); | 168 | fprintf(stderr, "%s: %s: %s\n", PROGRAM_NAME, cnf->widget->name, scu_strerror(rc)); |
| @@ -174,6 +179,7 @@ void scu_widget_pathcheck_usage(scu_config * cnf) | |||
| 174 | printf("\n"); | 179 | printf("\n"); |
| 175 | scu_usage_options(cnf); | 180 | scu_usage_options(cnf); |
| 176 | printf(" -d, --directory check path for directory instead of file\n"); | 181 | printf(" -d, --directory check path for directory instead of file\n"); |
| 182 | printf(" -i, --ignore ignore non-existent files and directories\n"); | ||
| 177 | printf("\n"); | 183 | printf("\n"); |
| 178 | scu_usage_restrictions(); | 184 | scu_usage_restrictions(); |
| 179 | printf("\n"); | 185 | printf("\n"); |
