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