From 17548d3ee87a06b3081a8598c661d842cf620021 Mon Sep 17 00:00:00 2001 From: jsteube Date: Fri, 27 Jan 2017 11:46:45 +0100 Subject: [PATCH] Helper: Added functions to check existence, type, read- and write-permissions and rewrite sources to use them instead of stat() --- docs/changes.txt | 1 + include/shared.h | 7 ++++ src/combinator.c | 57 +++++++++------------------------ src/hashes.c | 4 +-- src/hwmon.c | 10 +++--- src/mpsp.c | 25 ++++----------- src/opencl.c | 78 +++++++++++++++++++-------------------------- src/outfile_check.c | 27 +++++++++------- src/potfile.c | 4 +-- src/restore.c | 8 ++--- src/shared.c | 54 +++++++++++++++++++++++++++++++ src/straight.c | 51 +++++++---------------------- 12 files changed, 152 insertions(+), 174 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 5a2d2e071..dfd899036 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -38,6 +38,7 @@ - Building: Add SHARED variable to Makefile to choose if hashcat is build as static or shared binary (using libhashcat.so/hashcat.dll) - Building: Removed the use of RPATH on linker level - Events: Improved the maximum event message handling. event_log () will now also internally make sure that the message is properly terminated +- Helper: Added functions to check existence, type, read- and write-permissions and rewrite sources to use them instead of stat() * changes v3.20 -> v3.30: diff --git a/include/shared.h b/include/shared.h index c7e884cb4..9afd5f0a7 100644 --- a/include/shared.h +++ b/include/shared.h @@ -40,4 +40,11 @@ int hc_fstat (int fd, hc_stat_t *buf); void hc_qsort_r (void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *, void *), void *arg); void *hc_bsearch_r (const void *key, const void *base, size_t nmemb, size_t size, int (*compar) (const void *, const void *, void *), void *arg); +bool hc_path_is_file (const char *path); +bool hc_path_is_directory (const char *path); +bool hc_path_is_empty (const char *path); +bool hc_path_exist (const char *path); +bool hc_path_read (const char *path); +bool hc_path_write (const char *path); + #endif // _SHARED_H diff --git a/src/combinator.c b/src/combinator.c index b3f44fc4f..d6362c12f 100644 --- a/src/combinator.c +++ b/src/combinator.c @@ -44,13 +44,26 @@ int combinator_ctx_init (hashcat_ctx_t *hashcat_ctx) char *dictfile1 = user_options_extra->hc_workv[0]; char *dictfile2 = user_options_extra->hc_workv[1]; + // at this point we know the file actually exist // find the bigger dictionary and use as base + if (hc_path_is_file (dictfile1) == false) + { + event_log_error (hashcat_ctx, "%s: Not a regular file", dictfile1); + + return -1; + } + + if (hc_path_is_file (dictfile2) == false) + { + event_log_error (hashcat_ctx, "%s: Not a regular file", dictfile2); + + return -1; + } + FILE *fp1 = NULL; FILE *fp2 = NULL; - hc_stat_t tmp_stat; - if ((fp1 = fopen (dictfile1, "rb")) == NULL) { event_log_error (hashcat_ctx, "%s: %m", dictfile1); @@ -58,50 +71,10 @@ int combinator_ctx_init (hashcat_ctx_t *hashcat_ctx) return -1; } - if (hc_stat (dictfile1, &tmp_stat) == -1) - { - event_log_error (hashcat_ctx, "%s: %m", dictfile1); - - fclose (fp1); - - return -1; - } - - if (S_ISDIR (tmp_stat.st_mode)) - { - event_log_error (hashcat_ctx, "%s must be a regular file", dictfile1); - - fclose (fp1); - - return -1; - } - if ((fp2 = fopen (dictfile2, "rb")) == NULL) { event_log_error (hashcat_ctx, "%s: %m", dictfile2); - fclose (fp1); - - return -1; - } - - if (hc_stat (dictfile2, &tmp_stat) == -1) - { - event_log_error (hashcat_ctx, "%s: %m", dictfile2); - - fclose (fp1); - fclose (fp2); - - return -1; - } - - if (S_ISDIR (tmp_stat.st_mode)) - { - event_log_error (hashcat_ctx, "%s must be a regular file", dictfile2); - - fclose (fp1); - fclose (fp2); - return -1; } diff --git a/src/hashes.c b/src/hashes.c index b59c7487e..bbaa7fc35 100644 --- a/src/hashes.c +++ b/src/hashes.c @@ -453,9 +453,7 @@ int hashes_init_stage1 (hashcat_ctx_t *hashcat_ctx) if ((user_options->benchmark == false) && (user_options->stdout_flag == false) && (user_options->keyspace == false)) { - hc_stat_t f; - - hashlist_mode = (hc_stat (hash_or_file, &f) == 0) ? HL_MODE_FILE : HL_MODE_ARG; + hashlist_mode = (hc_path_exist (hash_or_file) == true) ? HL_MODE_FILE : HL_MODE_ARG; if (hashconfig->opts_type & OPTS_TYPE_BINARY_HASHFILE) { diff --git a/src/hwmon.c b/src/hwmon.c index b63d83405..78e655cc7 100644 --- a/src/hwmon.c +++ b/src/hwmon.c @@ -14,7 +14,7 @@ // sysfs functions -static int sysfs_init (hashcat_ctx_t *hashcat_ctx) +static bool sysfs_init (hashcat_ctx_t *hashcat_ctx) { hwmon_ctx_t *hwmon_ctx = hashcat_ctx->hwmon_ctx; @@ -26,13 +26,11 @@ static int sysfs_init (hashcat_ctx_t *hashcat_ctx) snprintf (path, HCBUFSIZ_TINY - 1, "%s", SYS_BUS_PCI_DEVICES); - hc_stat_t s; - - int rc = hc_stat (path, &s); + const bool r = hc_path_read (path); hcfree (path); - return rc; + return r; } static void sysfs_close (hashcat_ctx_t *hashcat_ctx) @@ -3281,7 +3279,7 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx) { hwmon_ctx->hm_sysfs = sysfs; - if (sysfs_init (hashcat_ctx) == -1) + if (sysfs_init (hashcat_ctx) == false) { hcfree (hwmon_ctx->hm_sysfs); diff --git a/src/mpsp.c b/src/mpsp.c index 0ecfeb0eb..07a7505b0 100644 --- a/src/mpsp.c +++ b/src/mpsp.c @@ -1242,9 +1242,7 @@ int mask_ctx_init (hashcat_ctx_t *hashcat_ctx) { char *arg = user_options_extra->hc_workv[0]; - hc_stat_t file_stat; - - if (hc_stat (arg, &file_stat) == -1) + if (hc_path_exist (arg) == false) { const int rc = mask_append (hashcat_ctx, arg, NULL); @@ -1258,14 +1256,7 @@ int mask_ctx_init (hashcat_ctx_t *hashcat_ctx) { arg = user_options_extra->hc_workv[i]; - if (hc_stat (arg, &file_stat) == -1) - { - event_log_error (hashcat_ctx, "%s: %m", arg); - - return -1; - } - - if (S_ISREG (file_stat.st_mode)) + if (hc_path_is_file (arg) == true) { FILE *mask_fp = fopen (arg, "r"); @@ -1348,9 +1339,7 @@ int mask_ctx_init (hashcat_ctx_t *hashcat_ctx) // mod - hc_stat_t file_stat; - - if (hc_stat (arg, &file_stat) == -1) + if (hc_path_exist (arg) == false) { const int rc = mask_append (hashcat_ctx, arg, NULL); @@ -1358,7 +1347,7 @@ int mask_ctx_init (hashcat_ctx_t *hashcat_ctx) } else { - if (S_ISREG (file_stat.st_mode)) + if (hc_path_is_file (arg) == true) { mask_ctx->mask_from_file = true; @@ -1424,9 +1413,7 @@ int mask_ctx_init (hashcat_ctx_t *hashcat_ctx) // mod - hc_stat_t file_stat; - - if (hc_stat (arg, &file_stat) == -1) + if (hc_path_exist (arg) == false) { const int rc = mask_append (hashcat_ctx, arg, NULL); @@ -1434,7 +1421,7 @@ int mask_ctx_init (hashcat_ctx_t *hashcat_ctx) } else { - if (S_ISREG (file_stat.st_mode)) + if (hc_path_is_file (arg) == true) { mask_ctx->mask_from_file = true; diff --git a/src/opencl.c b/src/opencl.c index ae55c5ee1..49d9f1141 100644 --- a/src/opencl.c +++ b/src/opencl.c @@ -50,9 +50,7 @@ static int ocl_check_dri (MAYBE_UNUSED hashcat_ctx_t *hashcat_ctx) // No GPU available! That's fine, so we don't need to check if we have access to it. - hc_stat_t stat; - - if (hc_stat (dri_card0_path, &stat) == -1) return 0; + if (hc_path_exist (dri_card0_path) == false) return 0; // Now we need to check if this an AMD vendor, because this is when the problems start @@ -3797,22 +3795,7 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx) for (int i = 0; i < files_cnt; i++) { - FILE *fd = fopen (files_names[i], "r"); - - if (fd == NULL) - { - event_log_error (hashcat_ctx, "%s: %m", files_names[i]); - - return -1; - } - - char buf[1] = { 0 }; - - size_t n = fread (buf, 1, 1, fd); - - fclose (fd); - - if (n != 1) + if (hc_path_read (files_names[i]) == false) { event_log_error (hashcat_ctx, "%s: %m", files_names[i]); @@ -3849,9 +3832,7 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx) generate_source_kernel_filename (hashconfig->attack_exec, user_options_extra->attack_kern, hashconfig->kern_type, folder_config->shared_dir, source_file); - hc_stat_t sst; - - if (hc_stat (source_file, &sst) == -1) + if (hc_path_read (source_file) == false) { event_log_error (hashcat_ctx, "%s: %m", source_file); @@ -3866,13 +3847,16 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx) generate_cached_kernel_filename (hashconfig->attack_exec, user_options_extra->attack_kern, hashconfig->kern_type, folder_config->profile_dir, device_name_chksum, cached_file); - int cached = 1; + bool cached = true; - hc_stat_t cst; - - if ((hc_stat (cached_file, &cst) == -1) || cst.st_size == 0) + if (hc_path_read (cached_file) == false) { - cached = 0; + cached = false; + } + + if (hc_path_is_empty (cached_file) == true) + { + cached = false; } /** @@ -3885,7 +3869,7 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx) if (opencl_ctx->force_jit_compilation == -1) { - if (cached == 0) + if (cached == false) { #if defined (DEBUG) if (user_options->quiet == false) event_log_warning (hashcat_ctx, "* Device #%u: Kernel %s not found in cache! Building may take a while...", device_id + 1, filename_from_filepath (cached_file)); @@ -4057,9 +4041,7 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx) generate_source_kernel_mp_filename (hashconfig->opti_type, hashconfig->opts_type, folder_config->shared_dir, source_file); - hc_stat_t sst; - - if (hc_stat (source_file, &sst) == -1) + if (hc_path_read (source_file) == false) { event_log_error (hashcat_ctx, "%s: %m", source_file); @@ -4074,13 +4056,16 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx) generate_cached_kernel_mp_filename (hashconfig->opti_type, hashconfig->opts_type, folder_config->profile_dir, device_name_chksum, cached_file); - int cached = 1; + bool cached = true; - hc_stat_t cst; - - if (hc_stat (cached_file, &cst) == -1) + if (hc_path_read (cached_file) == false) { - cached = 0; + cached = false; + } + + if (hc_path_is_empty (cached_file) == true) + { + cached = false; } /** @@ -4091,7 +4076,7 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx) char **kernel_sources = (char **) hcmalloc (sizeof (char *)); - if (cached == 0) + if (cached == false) { #if defined (DEBUG) if (user_options->quiet == false) event_log_warning (hashcat_ctx, "* Device #%u: Kernel %s not found in cache! Building may take a while...", device_id + 1, filename_from_filepath (cached_file)); @@ -4199,9 +4184,7 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx) generate_source_kernel_amp_filename (user_options_extra->attack_kern, folder_config->shared_dir, source_file); - hc_stat_t sst; - - if (hc_stat (source_file, &sst) == -1) + if (hc_path_read (source_file) == false) { event_log_error (hashcat_ctx, "%s: %m", source_file); @@ -4216,13 +4199,16 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx) generate_cached_kernel_amp_filename (user_options_extra->attack_kern, folder_config->profile_dir, device_name_chksum, cached_file); - int cached = 1; + bool cached = true; - hc_stat_t cst; - - if (hc_stat (cached_file, &cst) == -1) + if (hc_path_read (cached_file) == false) { - cached = 0; + cached = false; + } + + if (hc_path_is_empty (cached_file) == true) + { + cached = false; } /** @@ -4233,7 +4219,7 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx) char **kernel_sources = (char **) hcmalloc (sizeof (char *)); - if (cached == 0) + if (cached == false) { #if defined (DEBUG) if (user_options->quiet == false) event_log_warning (hashcat_ctx, "* Device #%u: Kernel %s not found in cache! Building may take a while...", device_id + 1, filename_from_filepath (cached_file)); diff --git a/src/outfile_check.c b/src/outfile_check.c index 435472069..0a8715072 100644 --- a/src/outfile_check.c +++ b/src/outfile_check.c @@ -67,14 +67,21 @@ static int outfile_remove (hashcat_ctx_t *hashcat_ctx) if (check_left == 0) { - hc_stat_t outfile_check_stat; - - if (hc_stat (root_directory, &outfile_check_stat) == 0) + if (hc_path_exist (root_directory) == true) { - u32 is_dir = S_ISDIR (outfile_check_stat.st_mode); + const bool is_dir = hc_path_is_directory (root_directory); - if (is_dir == 1) + if (is_dir == true) { + hc_stat_t outfile_check_stat; + + if (hc_stat (root_directory, &outfile_check_stat) == -1) + { + event_log_error (hashcat_ctx, "%s: %m", root_directory); + + return -1; + } + if (outfile_check_stat.st_mtime > folder_mtime) { char **out_files_new = scan_directory (root_directory); @@ -349,15 +356,13 @@ int outcheck_ctx_init (hashcat_ctx_t *hashcat_ctx) outcheck_ctx->root_directory = user_options->outfile_check_dir; } - hc_stat_t outfile_check_stat; - - if (hc_stat (outcheck_ctx->root_directory, &outfile_check_stat) == 0) + if (hc_path_exist (outcheck_ctx->root_directory) == true) { - const u32 is_dir = S_ISDIR (outfile_check_stat.st_mode); + const bool is_dir = hc_path_is_directory (outcheck_ctx->root_directory); - if (is_dir == 0) + if (is_dir == false) { - event_log_error (hashcat_ctx, "Directory specified in outfile-check '%s' is not a valid directory", outcheck_ctx->root_directory); + event_log_error (hashcat_ctx, "Directory specified in outfile-check '%s' is not a directory", outcheck_ctx->root_directory); return -1; } diff --git a/src/potfile.c b/src/potfile.c index 888eaf6be..ea172ac4e 100644 --- a/src/potfile.c +++ b/src/potfile.c @@ -116,9 +116,7 @@ int potfile_init (hashcat_ctx_t *hashcat_ctx) hc_asprintf (&potfile_old, "%s/hashcat.pot", folder_config->profile_dir); - hc_stat_t st; - - if (hc_stat (potfile_old, &st) == 0) + if (hc_path_exist (potfile_old) == true) { event_log_warning (hashcat_ctx, "Old potfile detected: %s", potfile_old); event_log_warning (hashcat_ctx, "New potfile is: %s ", potfile_ctx->filename); diff --git a/src/restore.c b/src/restore.c index a9a0d95f0..2a98561c3 100644 --- a/src/restore.c +++ b/src/restore.c @@ -293,17 +293,15 @@ int cycle_restore (hashcat_ctx_t *hashcat_ctx) if (rc_write_restore == -1) return -1; - hc_stat_t st; - - if (hc_stat (eff_restore_file, &st) == 0) + if (hc_path_exist (eff_restore_file) == true) { - if (unlink (eff_restore_file)) + if (unlink (eff_restore_file) == -1) { event_log_warning (hashcat_ctx, "Unlink file '%s': %m", eff_restore_file); } } - if (rename (new_restore_file, eff_restore_file)) + if (rename (new_restore_file, eff_restore_file) == -1) { event_log_warning (hashcat_ctx, "Rename file '%s' to '%s': %m", new_restore_file, eff_restore_file); } diff --git a/src/shared.c b/src/shared.c index dc74ced10..44efc0306 100644 --- a/src/shared.c +++ b/src/shared.c @@ -188,6 +188,60 @@ void *hc_bsearch_r (const void *key, const void *base, size_t nmemb, size_t size return (NULL); } +bool hc_path_is_file (const char *path) +{ + hc_stat_t s; + + if (hc_stat (path, &s) == -1) return false; + + if (S_ISREG (s.st_mode)) return true; + + return false; +} + +bool hc_path_is_directory (const char *path) +{ + hc_stat_t s; + + if (hc_stat (path, &s) == -1) return false; + + if (S_ISDIR (s.st_mode)) return true; + + return false; +} + +bool hc_path_is_empty (const char *path) +{ + hc_stat_t s; + + if (hc_stat (path, &s) == -1) return false; + + if (s.st_size == 0) return true; + + return false; +} + +bool hc_path_exist (const char *path) +{ + if (access (path, F_OK) == -1) return false; + + return true; +} + +bool hc_path_read (const char *path) +{ + if (access (path, R_OK) == -1) return false; + + return true; +} + +bool hc_path_write (const char *path) +{ + if (access (path, W_OK) == -1) return false; + + return true; +} + void setup_environment_variables () { char *compute = getenv ("COMPUTE"); diff --git a/src/straight.c b/src/straight.c index 1dc399f39..548089bbd 100644 --- a/src/straight.c +++ b/src/straight.c @@ -268,16 +268,9 @@ int straight_ctx_init (hashcat_ctx_t *hashcat_ctx) { char *l0_filename = user_options_extra->hc_workv[i]; - hc_stat_t l0_stat; + // at this point we already verified the path actually exist and is readable - if (hc_stat (l0_filename, &l0_stat) == -1) - { - event_log_error (hashcat_ctx, "%s: %m", l0_filename); - - return -1; - } - - if (S_ISDIR (l0_stat.st_mode)) + if (hc_path_is_directory (l0_filename) == true) { char **dictionary_files = NULL; @@ -291,16 +284,14 @@ int straight_ctx_init (hashcat_ctx_t *hashcat_ctx) { char *l1_filename = dictionary_files[d]; - hc_stat_t l1_stat; - - if (hc_stat (l1_filename, &l1_stat) == -1) + if (hc_path_read (l1_filename) == false) { event_log_error (hashcat_ctx, "%s: %m", l1_filename); return -1; } - if (S_ISREG (l1_stat.st_mode)) + if (hc_path_is_file (l1_filename) == true) { const int rc = straight_ctx_add_wl (hashcat_ctx, l1_filename); @@ -341,16 +332,9 @@ int straight_ctx_init (hashcat_ctx_t *hashcat_ctx) { char *l0_filename = user_options_extra->hc_workv[i]; - hc_stat_t l0_stat; + // at this point we already verified the path actually exist and is readable - if (hc_stat (l0_filename, &l0_stat) == -1) - { - event_log_error (hashcat_ctx, "%s: %m", l0_filename); - - return -1; - } - - if (S_ISDIR (l0_stat.st_mode)) + if (hc_path_is_directory (l0_filename) == true) { char **dictionary_files = NULL; @@ -364,16 +348,14 @@ int straight_ctx_init (hashcat_ctx_t *hashcat_ctx) { char *l1_filename = dictionary_files[d]; - hc_stat_t l1_stat; - - if (hc_stat (l1_filename, &l1_stat) == -1) + if (hc_path_read (l1_filename) == false) { event_log_error (hashcat_ctx, "%s: %m", l1_filename); return -1; } - if (S_ISREG (l1_stat.st_mode)) + if (hc_path_is_file (l1_filename) == true) { const int rc = straight_ctx_add_wl (hashcat_ctx, l1_filename); @@ -405,16 +387,9 @@ int straight_ctx_init (hashcat_ctx_t *hashcat_ctx) { char *l0_filename = user_options_extra->hc_workv[i]; - hc_stat_t l0_stat; + // at this point we already verified the path actually exist and is readable - if (hc_stat (l0_filename, &l0_stat) == -1) - { - event_log_error (hashcat_ctx, "%s: %m", l0_filename); - - return -1; - } - - if (S_ISDIR (l0_stat.st_mode)) + if (hc_path_is_directory (l0_filename) == true) { char **dictionary_files = NULL; @@ -428,16 +403,14 @@ int straight_ctx_init (hashcat_ctx_t *hashcat_ctx) { char *l1_filename = dictionary_files[d]; - hc_stat_t l1_stat; - - if (hc_stat (l1_filename, &l1_stat) == -1) + if (hc_path_read (l1_filename) == false) { event_log_error (hashcat_ctx, "%s: %m", l1_filename); return -1; } - if (S_ISREG (l1_stat.st_mode)) + if (hc_path_is_file (l1_filename) == true) { const int rc = straight_ctx_add_wl (hashcat_ctx, l1_filename);