snac.c 8.9 KB

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