xdg-compliant-wget-hsts-file.patch 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. From b27917936c64ac9036509b3a67516738e924c9ea Mon Sep 17 00:00:00 2001
  2. From: Noah Vogt <noah@noahvogt.com>
  3. Date: Wed, 17 Nov 2021 23:32:37 +0100
  4. Subject: [PATCH] xdg compliant wget hsts file
  5. ---
  6. src/hsts.c | 4 ++--
  7. src/main.c | 29 ++++++++++++++++++++++++-----
  8. src/path.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
  9. src/path.h | 5 +++++
  10. 4 files changed, 85 insertions(+), 7 deletions(-)
  11. create mode 100644 src/path.c
  12. create mode 100644 src/path.h
  13. diff --git a/src/hsts.c b/src/hsts.c
  14. index 9aa97b4..2562327 100644
  15. --- a/src/hsts.c
  16. +++ b/src/hsts.c
  17. @@ -630,7 +630,7 @@ get_hsts_store_filename (void)
  18. if (opt.homedir)
  19. {
  20. - filename = ajoin_dir_file (opt.homedir, ".wget-hsts-test");
  21. + filename = ajoin_dir_file (get_hsts_file_dir(), HSTS_TEST_FILE);
  22. fp = fopen (filename, "w");
  23. if (fp)
  24. fclose (fp);
  25. @@ -796,7 +796,7 @@ test_hsts_read_database (void)
  26. if (opt.homedir)
  27. {
  28. - file = ajoin_dir_file (opt.homedir, ".wget-hsts-testing");
  29. + file = ajoin_dir_file (get_hsts_file_dir(), HSTS_TESTING_FILE);
  30. fp = fopen (file, "w");
  31. if (fp)
  32. {
  33. diff --git a/src/main.c b/src/main.c
  34. index 0f7b6b1..39082bc 100644
  35. --- a/src/main.c
  36. +++ b/src/main.c
  37. @@ -31,6 +31,8 @@ as that of the covered work. */
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. +#include <sys/types.h>
  41. +#include <sys/stat.h>
  42. #include <unistd.h>
  43. #include <string.h>
  44. #include <signal.h>
  45. @@ -63,6 +65,7 @@ as that of the covered work. */
  46. #include <getopt.h>
  47. #include <getpass.h>
  48. #include <quote.h>
  49. +#include "path.c"
  50. #ifdef TESTING
  51. /* Rename the main function so we can have a main() in fuzzing code
  52. @@ -178,8 +181,21 @@ get_hsts_database (void)
  53. if (opt.homedir)
  54. {
  55. - char *dir = ajoin_dir_file(opt.homedir, ".wget-hsts");
  56. - return dir;
  57. + char* dir = get_hsts_file_dir();
  58. + char* path = ajoin_dir_file(get_hsts_file_dir(), HSTS_FILE);
  59. + struct stat st = {0};
  60. +
  61. + /* make directory for hsts file if it does not exist */
  62. + if (stat(dir, &st) == -1)
  63. + {
  64. + int mkdir_sucess = mkdir(dir, 0700);
  65. + if (!(stat(dir, &st) == 0 && S_ISDIR(st.st_mode)))
  66. + {
  67. + printf("Error: Could not create hsts file\n");
  68. + exit (WGET_EXIT_GENERIC_ERROR);
  69. + }
  70. + }
  71. + return path;
  72. }
  73. return NULL;
  74. @@ -206,7 +222,8 @@ load_hsts (void)
  75. else
  76. logprintf (LOG_NOTQUIET, "ERROR: could not open HSTS store. HSTS will be disabled.\n");
  77. - xfree (filename);
  78. + /* TODO: get free() call working again */
  79. + // xfree (filename);
  80. }
  81. }
  82. @@ -224,9 +241,11 @@ save_hsts (void)
  83. }
  84. hsts_store_close (hsts_store);
  85. - xfree (hsts_store);
  86. + /* TODO: get free() call working again */
  87. + // xfree (hsts_store);
  88. - xfree (filename);
  89. + /* TODO: get free() call working again */
  90. + // xfree (filename);
  91. }
  92. }
  93. #endif
  94. diff --git a/src/path.c b/src/path.c
  95. new file mode 100644
  96. index 0000000..51cc3f0
  97. --- /dev/null
  98. +++ b/src/path.c
  99. @@ -0,0 +1,54 @@
  100. +#include "wget.h"
  101. +
  102. +#include <stdio.h>
  103. +#include <stdlib.h>
  104. +#include <unistd.h>
  105. +#include <limits.h>
  106. +#include <string.h>
  107. +
  108. +#include "path.h"
  109. +#include "exits.h"
  110. +
  111. +char*
  112. +get_hsts_file_dir (void)
  113. +{
  114. + char *home_dir;
  115. + char *default_xdg_dir = "/" DEDAULT_XDG_CACHE_DIR;
  116. + char *hsts_file_base_dir = "/" HSTS_FILE_DIR;
  117. + char* user_xdg_dir;
  118. + static char hsts_file_dir[_POSIX_PATH_MAX] = "";
  119. + const char *slash = "/";
  120. + char *check_multiple;
  121. +
  122. + if (!(home_dir = getenv("HOME")))
  123. + {
  124. + printf("Error: Could not access home directory\n");
  125. + exit (WGET_EXIT_GENERIC_ERROR);
  126. + }
  127. +
  128. + /* prevent the function from runnning multiple times by returning right away
  129. + * when a a "/" character is found in the hsts_file_dir variable
  130. + */
  131. + check_multiple = hsts_file_dir;
  132. + while (*check_multiple)
  133. + {
  134. + if (strchr(slash, *check_multiple))
  135. + {
  136. + return hsts_file_dir;
  137. + }
  138. + check_multiple++;
  139. + }
  140. +
  141. + if ((user_xdg_dir = getenv("XDG_CACHE_HOME")))
  142. + {
  143. + strcat(hsts_file_dir, user_xdg_dir);
  144. + } else
  145. + {
  146. + strcat(hsts_file_dir, home_dir);
  147. + strcat(hsts_file_dir, default_xdg_dir);
  148. + }
  149. +
  150. + strcat(hsts_file_dir, hsts_file_base_dir);
  151. +
  152. + return hsts_file_dir;
  153. +}
  154. diff --git a/src/path.h b/src/path.h
  155. new file mode 100644
  156. index 0000000..c9b1f7b
  157. --- /dev/null
  158. +++ b/src/path.h
  159. @@ -0,0 +1,5 @@
  160. +#define HSTS_FILE_DIR "wget"
  161. +#define HSTS_FILE "hsts"
  162. +#define HSTS_TEST_FILE "hsts-test"
  163. +#define HSTS_TESTING_FILE "hsts-testing"
  164. +#define DEDAULT_XDG_CACHE_DIR ".cache"
  165. --
  166. 2.34.0