snac.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2026 grunfink et al. / MIT license */
  3. #define XS_IMPLEMENTATION
  4. #include "xs.h"
  5. #include "xs_hex.h"
  6. #include "xs_io.h"
  7. #include "xs_unicode_tbl.h"
  8. #include "xs_unicode.h"
  9. #include "xs_json.h"
  10. #include "xs_curl.h"
  11. #include "xs_openssl.h"
  12. #include "xs_socket.h"
  13. #include "xs_unix_socket.h"
  14. #include "xs_url.h"
  15. #include "xs_http.h"
  16. #include "xs_httpd.h"
  17. #include "xs_mime.h"
  18. #include "xs_regex.h"
  19. #include "xs_set.h"
  20. #include "xs_time.h"
  21. #include "xs_glob.h"
  22. #include "xs_random.h"
  23. #include "xs_match.h"
  24. #include "xs_fcgi.h"
  25. #include "xs_html.h"
  26. #include "xs_po.h"
  27. #include "xs_webmention.h"
  28. #include "snac.h"
  29. #include <sys/time.h>
  30. #include <sys/stat.h>
  31. #include <sys/wait.h>
  32. xs_str *srv_basedir = NULL;
  33. xs_dict *srv_config = NULL;
  34. xs_str *srv_baseurl = NULL;
  35. xs_str *srv_proxy_token_seed = NULL;
  36. xs_dict *srv_langs = NULL;
  37. int dbglevel = 0;
  38. int mkdirx(const char *pathname)
  39. /* creates a directory with special permissions */
  40. {
  41. int ret;
  42. if ((ret = mkdir(pathname, DIR_PERM)) != -1) {
  43. /* try to the set the setgid bit, to allow system users
  44. to create files in these directories using the
  45. command-line tool. This may fail in some restricted
  46. environments, but it's of no use there anyway */
  47. chmod(pathname, DIR_PERM_ADD);
  48. }
  49. return ret;
  50. }
  51. xs_str *tid(int offset)
  52. /* returns a time-based Id */
  53. {
  54. struct timeval tv;
  55. gettimeofday(&tv, NULL);
  56. return xs_fmt("%010ld.%06ld", (long)tv.tv_sec + (long)offset, (long)tv.tv_usec);
  57. }
  58. double ftime(void)
  59. /* returns the UNIX time as a float */
  60. {
  61. xs *ntid = tid(0);
  62. return atof(ntid);
  63. }
  64. int validate_uid(const char *uid)
  65. /* returns if uid is a valid identifier */
  66. {
  67. if (!uid || *uid == '\0')
  68. return 0;
  69. while (*uid) {
  70. if (!(isalnum(*uid) || *uid == '_'))
  71. return 0;
  72. uid++;
  73. }
  74. return 1;
  75. }
  76. void srv_log(xs_str *str)
  77. /* logs a debug message */
  78. {
  79. if (xs_str_in(str, srv_basedir) != -1) {
  80. /* replace basedir with ~ */
  81. str = xs_replace_i(str, srv_basedir, "~");
  82. }
  83. xs *tm = xs_str_localtime(0, "%H:%M:%S");
  84. fprintf(stderr, "%s %s\n", tm, str);
  85. /* if the ~/log/ folder exists, also write to a file there */
  86. xs *dt = xs_str_localtime(0, "%Y-%m-%d");
  87. xs *lf = xs_fmt("%s/log/%s.log", srv_basedir, dt);
  88. FILE *f;
  89. if ((f = fopen(lf, "a")) != NULL) {
  90. fprintf(f, "%s %s\n", tm, str);
  91. fclose(f);
  92. }
  93. xs_free(str);
  94. }
  95. void snac_log(snac *snac, xs_str *str)
  96. /* prints a user debugging information */
  97. {
  98. xs *o_str = str;
  99. xs_str *msg = xs_fmt("[%s] %s", snac->uid, o_str);
  100. if (xs_str_in(msg, snac->basedir) != -1) {
  101. /* replace long basedir references with ~ */
  102. msg = xs_replace_i(msg, snac->basedir, "~");
  103. }
  104. srv_log(msg);
  105. }
  106. xs_str *hash_password(const char *uid, const char *passwd, const char *nonce)
  107. /* hashes a password */
  108. {
  109. xs *d_nonce = NULL;
  110. xs *combi;
  111. xs *hash;
  112. if (nonce == NULL) {
  113. unsigned int r;
  114. xs_rnd_buf(&r, sizeof(r));
  115. d_nonce = xs_fmt("%08x", r);
  116. nonce = d_nonce;
  117. }
  118. combi = xs_fmt("%s:%s:%s", nonce, uid, passwd);
  119. hash = xs_sha1_hex(combi, strlen(combi));
  120. return xs_fmt("%s:%s", nonce, hash);
  121. }
  122. int check_password(const char *uid, const char *passwd, const char *hash)
  123. /* checks a password */
  124. {
  125. int ret = 0;
  126. xs *spl = xs_split_n(hash, ":", 1);
  127. if (xs_list_len(spl) == 2) {
  128. xs *n_hash = hash_password(uid, passwd, xs_list_get(spl, 0));
  129. ret = (strcmp(hash, n_hash) == 0);
  130. }
  131. return ret;
  132. }
  133. int strip_media(const char *fn)
  134. /* strips EXIF data from a file */
  135. {
  136. int ret = 0;
  137. const xs_val *v = xs_dict_get(srv_config, "strip_exif");
  138. if (xs_type(v) == XSTYPE_TRUE) {
  139. xs *l_fn = xs_tolower_i(xs_dup(fn));
  140. /* check extensions */
  141. if (xs_endswith(l_fn, ".jpg") || xs_endswith(l_fn, ".jpeg") ||
  142. xs_endswith(l_fn, ".png") || xs_endswith(l_fn, ".webp") ||
  143. xs_endswith(l_fn, ".heic") || xs_endswith(l_fn, ".heif") ||
  144. xs_endswith(l_fn, ".avif") || xs_endswith(l_fn, ".tiff") ||
  145. xs_endswith(l_fn, ".gif") || xs_endswith(l_fn, ".bmp")) {
  146. const char *mp = xs_dict_get(srv_config, "mogrify_path");
  147. if (mp == NULL)
  148. mp = "mogrify";
  149. xs *cmd = xs_fmt("%s -strip \"%s\" 2>/dev/null", mp, fn);
  150. ret = system(cmd);
  151. if (ret != 0) {
  152. int code = 0;
  153. if (WIFEXITED(ret))
  154. code = WEXITSTATUS(ret);
  155. if (code == 127)
  156. srv_log(xs_fmt("strip_media: error stripping %s. '%s' not found (exit 127). Set 'mogrify_path' in server.json.", fn, mp));
  157. else
  158. srv_log(xs_fmt("strip_media: error stripping %s %d", fn, ret));
  159. }
  160. else
  161. srv_debug(1, xs_fmt("strip_media: stripped %s", fn));
  162. }
  163. }
  164. return ret;
  165. }
  166. int check_strip_tool(void)
  167. {
  168. const xs_val *v = xs_dict_get(srv_config, "strip_exif");
  169. int ret = 1;
  170. if (xs_type(v) == XSTYPE_TRUE) {
  171. const char *mp = xs_dict_get(srv_config, "mogrify_path");
  172. if (mp == NULL)
  173. mp = "mogrify";
  174. xs *cmd = xs_fmt("%s -version 2>/dev/null >/dev/null", mp);
  175. if (system(cmd) != 0)
  176. ret = 0;
  177. }
  178. return ret;
  179. }