snac.c 8.8 KB

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