snac.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 "xs_list_tools.h"
  29. #include "snac.h"
  30. #include <sys/time.h>
  31. #include <sys/stat.h>
  32. #include <sys/wait.h>
  33. #include <limits.h>
  34. #include <stdlib.h>
  35. xs_str *srv_basedir = NULL;
  36. xs_dict *srv_config = NULL;
  37. xs_str *srv_baseurl = NULL;
  38. xs_str *srv_proxy_token_seed = NULL;
  39. xs_dict *srv_langs = NULL;
  40. const char *months[12] = {0};
  41. int dbglevel = 0;
  42. int mkdirx(const char *pathname)
  43. /* creates a directory with special permissions */
  44. {
  45. int ret;
  46. if ((ret = mkdir(pathname, DIR_PERM)) != -1) {
  47. /* try to the set the setgid bit, to allow system users
  48. to create files in these directories using the
  49. command-line tool. This may fail in some restricted
  50. environments, but it's of no use there anyway */
  51. chmod(pathname, DIR_PERM_ADD);
  52. }
  53. return ret;
  54. }
  55. xs_str *tid(int offset)
  56. /* returns a time-based Id */
  57. {
  58. struct timeval tv;
  59. gettimeofday(&tv, NULL);
  60. return xs_fmt("%010ld.%06ld", (long)tv.tv_sec + (long)offset, (long)tv.tv_usec);
  61. }
  62. double ftime(void)
  63. /* returns the UNIX time as a float */
  64. {
  65. xs *ntid = tid(0);
  66. return atof(ntid);
  67. }
  68. int validate_uid(const char *uid)
  69. /* returns if uid is a valid identifier */
  70. {
  71. if (!uid || *uid == '\0')
  72. return 0;
  73. while (*uid) {
  74. if (!(isalnum(*uid) || *uid == '_'))
  75. return 0;
  76. uid++;
  77. }
  78. return 1;
  79. }
  80. void srv_log(xs_str *str)
  81. /* logs a debug message */
  82. {
  83. if (xs_str_in(str, srv_basedir) != -1) {
  84. /* replace basedir with ~ */
  85. str = xs_replace_i(str, srv_basedir, "~");
  86. }
  87. xs *tm = xs_str_localtime(0, "%H:%M:%S");
  88. fprintf(stderr, "%s %s\n", tm, str);
  89. /* if the ~/log/ folder exists, also write to a file there */
  90. xs *dt = xs_str_localtime(0, "%Y-%m-%d");
  91. xs *lf = xs_fmt("%s/log/%s.log", srv_basedir, dt);
  92. FILE *f;
  93. if ((f = fopen(lf, "a")) != NULL) {
  94. fprintf(f, "%s %s\n", tm, str);
  95. fclose(f);
  96. }
  97. xs_free(str);
  98. }
  99. void snac_log(snac *snac, xs_str *str)
  100. /* prints a user debugging information */
  101. {
  102. xs *o_str = str;
  103. xs_str *msg = xs_fmt("[%s] %s", snac->uid, o_str);
  104. if (xs_str_in(msg, snac->basedir) != -1) {
  105. /* replace long basedir references with ~ */
  106. msg = xs_replace_i(msg, snac->basedir, "~");
  107. }
  108. srv_log(msg);
  109. }
  110. xs_str *hash_password(const char *uid, const char *passwd, const char *nonce)
  111. /* hashes a password */
  112. {
  113. xs *d_nonce = NULL;
  114. xs *combi;
  115. xs *hash;
  116. if (nonce == NULL) {
  117. unsigned int r;
  118. xs_rnd_buf(&r, sizeof(r));
  119. d_nonce = xs_fmt("%08x", r);
  120. nonce = d_nonce;
  121. }
  122. combi = xs_fmt("%s:%s:%s", nonce, uid, passwd);
  123. hash = xs_sha1_hex(combi, strlen(combi));
  124. return xs_fmt("%s:%s", nonce, hash);
  125. }
  126. int check_password(const char *uid, const char *passwd, const char *hash)
  127. /* checks a password */
  128. {
  129. int ret = 0;
  130. xs *spl = xs_split_n(hash, ":", 1);
  131. if (xs_list_len(spl) == 2) {
  132. xs *n_hash = hash_password(uid, passwd, xs_list_get(spl, 0));
  133. ret = (strcmp(hash, n_hash) == 0);
  134. }
  135. return ret;
  136. }
  137. int strip_media(const char *fn)
  138. /* strips EXIF data from a file */
  139. {
  140. int ret = 0;
  141. const xs_val *v = xs_dict_get(srv_config, "strip_exif");
  142. if (xs_type(v) == XSTYPE_TRUE) {
  143. /* Heuristic: find 'user/' in the path to make it relative */
  144. /* This works for ~/user/..., /var/snac/user/..., etc. */
  145. const char *r_fn = strstr(fn, "user/");
  146. if (r_fn == NULL) {
  147. /* Fallback: try to strip ~/ if present */
  148. if (strncmp(fn, "~/", 2) == 0)
  149. r_fn = fn + 2;
  150. else
  151. r_fn = fn;
  152. }
  153. xs *l_fn = xs_tolower_i(xs_dup(r_fn));
  154. /* check image extensions */
  155. if (xs_endswith(l_fn, ".jpg") || xs_endswith(l_fn, ".jpeg") ||
  156. xs_endswith(l_fn, ".png") || xs_endswith(l_fn, ".webp") ||
  157. xs_endswith(l_fn, ".heic") || xs_endswith(l_fn, ".heif") ||
  158. xs_endswith(l_fn, ".avif") || xs_endswith(l_fn, ".tiff") ||
  159. xs_endswith(l_fn, ".gif") || xs_endswith(l_fn, ".bmp")) {
  160. const char *mp = xs_dict_get(srv_config, "mogrify_path");
  161. if (mp == NULL)
  162. mp = "mogrify";
  163. xs *cmd = xs_fmt("cd \"%s\" && %s -auto-orient -strip \"%s\" 2>/dev/null", srv_basedir, mp, r_fn);
  164. ret = system(cmd);
  165. if (ret != 0) {
  166. int code = 0;
  167. if (WIFEXITED(ret))
  168. code = WEXITSTATUS(ret);
  169. if (code == 127)
  170. srv_log(xs_fmt("strip_media: error stripping %s. '%s' not found (exit 127). Set 'mogrify_path' in server.json.", r_fn, mp));
  171. else
  172. srv_log(xs_fmt("strip_media: error stripping %s %d", r_fn, ret));
  173. }
  174. else
  175. srv_debug(1, xs_fmt("strip_media: stripped %s", r_fn));
  176. }
  177. else
  178. /* check video extensions */
  179. if (xs_endswith(l_fn, ".mp4") || xs_endswith(l_fn, ".m4v") ||
  180. xs_endswith(l_fn, ".mov") || xs_endswith(l_fn, ".webm") ||
  181. xs_endswith(l_fn, ".mkv") || xs_endswith(l_fn, ".avi")) {
  182. const char *fp = xs_dict_get(srv_config, "ffmpeg_path");
  183. if (fp == NULL)
  184. fp = "ffmpeg";
  185. /* ffmpeg cannot modify in-place, so we need a temp file */
  186. /* we must preserve valid extension for ffmpeg to guess the format */
  187. const char *ext = strrchr(r_fn, '.');
  188. if (ext == NULL) ext = "";
  189. xs *tmp_fn = xs_fmt("%s.tmp%s", r_fn, ext);
  190. /* -map_metadata -1 strips all global metadata */
  191. /* -c copy copies input streams without re-encoding */
  192. /* we don't silence stderr so we can debug issues */
  193. /* we explicitly cd to srv_basedir to ensure relative paths work */
  194. xs *cmd = xs_fmt("cd \"%s\" && %s -y -i \"%s\" -map_metadata -1 -c copy \"%s\"", srv_basedir, fp, r_fn, tmp_fn);
  195. ret = system(cmd);
  196. if (ret != 0) {
  197. int code = 0;
  198. if (WIFEXITED(ret))
  199. code = WEXITSTATUS(ret);
  200. if (code == 127)
  201. srv_log(xs_fmt("strip_media: error stripping %s. '%s' not found (exit 127). Set 'ffmpeg_path' in server.json.", r_fn, fp));
  202. else {
  203. srv_log(xs_fmt("strip_media: error stripping %s %d", r_fn, ret));
  204. srv_log(xs_fmt("strip_media: command was: %s", cmd));
  205. }
  206. /* try to cleanup, just in case */
  207. /* unlink needs full path too if we are not in basedir */
  208. xs *full_tmp_fn = xs_fmt("%s/%s", srv_basedir, tmp_fn);
  209. unlink(full_tmp_fn);
  210. }
  211. else {
  212. /* rename tmp file to original */
  213. /* use full path for source because it was created relative to basedir */
  214. xs *full_tmp_fn = xs_fmt("%s/%s", srv_basedir, tmp_fn);
  215. if (rename(full_tmp_fn, fn) == 0)
  216. srv_debug(1, xs_fmt("strip_media: stripped %s", fn));
  217. else
  218. srv_log(xs_fmt("strip_media: error renaming %s to %s", full_tmp_fn, fn));
  219. }
  220. }
  221. }
  222. return ret;
  223. }
  224. int check_strip_tool(void)
  225. {
  226. const xs_val *v = xs_dict_get(srv_config, "strip_exif");
  227. int ret = 1;
  228. if (xs_type(v) == XSTYPE_TRUE) {
  229. /* check mogrify */
  230. {
  231. const char *mp = xs_dict_get(srv_config, "mogrify_path");
  232. if (mp == NULL)
  233. mp = "mogrify";
  234. xs *cmd = xs_fmt("%s -version 2>/dev/null >/dev/null", mp);
  235. if (system(cmd) != 0) {
  236. srv_log(xs_fmt("check_strip_tool: '%s' not working", mp));
  237. ret = 0;
  238. }
  239. }
  240. /* check ffmpeg */
  241. if (ret) {
  242. const char *fp = xs_dict_get(srv_config, "ffmpeg_path");
  243. if (fp == NULL)
  244. fp = "ffmpeg";
  245. xs *cmd = xs_fmt("%s -version 2>/dev/null >/dev/null", fp);
  246. if (system(cmd) != 0) {
  247. srv_log(xs_fmt("check_strip_tool: '%s' not working", fp));
  248. ret = 0;
  249. }
  250. }
  251. }
  252. return ret;
  253. }