2.59 utils.c
  1/* snac - A simple, minimalistic ActivityPub instance */
  2/* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
  3
  4#include "xs.h"
  5#include "xs_io.h"
  6#include "xs_json.h"
  7#include "xs_time.h"
  8#include "xs_openssl.h"
  9#include "xs_random.h"
 10#include "xs_glob.h"
 11#include "xs_curl.h"
 12#include "xs_regex.h"
 13
 14#include "snac.h"
 15
 16#include <sys/stat.h>
 17#include <stdlib.h>
 18
 19static const char *default_srv_config = "{"
 20    "\"host\":                 \"\","
 21    "\"prefix\":               \"\","
 22    "\"address\":              \"127.0.0.1\","
 23    "\"port\":                 8001,"
 24    "\"layout\":               0.0,"
 25    "\"dbglevel\":             0,"
 26    "\"queue_retry_minutes\":  2,"
 27    "\"queue_retry_max\":      10,"
 28    "\"queue_timeout\":        6,"
 29    "\"queue_timeout_2\":      8,"
 30    "\"cssurls\":              [\"\"],"
 31    "\"max_timeline_entries\": 50,"
 32    "\"timeline_purge_days\":  120,"
 33    "\"local_purge_days\":     0,"
 34    "\"min_account_age\":      0,"
 35    "\"admin_email\":          \"\","
 36    "\"admin_account\":        \"\","
 37    "\"title\":                \"\","
 38    "\"short_description\":    \"\","
 39    "\"protocol\":             \"https\","
 40    "\"fastcgi\":              false"
 41    "}";
 42
 43static const char *default_css =
 44    "body { max-width: 48em; margin: auto; line-height: 1.5; padding: 0.8em; word-wrap: break-word; }\n"
 45    "pre { overflow-x: scroll; }\n"
 46    ".snac-embedded-video, img { max-width: 100% }\n"
 47    ".snac-origin { font-size: 85% }\n"
 48    ".snac-score { float: right; font-size: 85% }\n"
 49    ".snac-top-user { text-align: center; padding-bottom: 2em }\n"
 50    ".snac-top-user-name { font-size: 200% }\n"
 51    ".snac-top-user-id { font-size: 150% }\n"
 52    ".snac-announcement { border: black 1px solid; padding: 0.5em }\n"
 53    ".snac-avatar { float: left; height: 2.5em; width: 2.5em; padding: 0.25em }\n"
 54    ".snac-author { font-size: 90%; text-decoration: none }\n"
 55    ".snac-author-tag { font-size: 80% }\n"
 56    ".snac-pubdate { color: #a0a0a0; font-size: 90% }\n"
 57    ".snac-top-controls { padding-bottom: 1.5em }\n"
 58    ".snac-post { border-top: 1px solid #a0a0a0; padding-top: 0.5em; padding-bottom: 0.5em; }\n"
 59    ".snac-children { padding-left: 1em; border-left: 1px solid #a0a0a0; }\n"
 60    ".snac-textarea { font-family: inherit; width: 100% }\n"
 61    ".snac-history { border: 1px solid #606060; border-radius: 3px; margin: 2.5em 0; padding: 0 2em }\n"
 62    ".snac-btn-mute { float: right; margin-left: 0.5em }\n"
 63    ".snac-btn-unmute { float: right; margin-left: 0.5em }\n"
 64    ".snac-btn-follow { float: right; margin-left: 0.5em }\n"
 65    ".snac-btn-unfollow { float: right; margin-left: 0.5em }\n"
 66    ".snac-btn-hide { float: right; margin-left: 0.5em }\n"
 67    ".snac-btn-delete { float: right; margin-left: 0.5em }\n"
 68    ".snac-btn-limit { float: right; margin-left: 0.5em }\n"
 69    ".snac-btn-unlimit { float: right; margin-left: 0.5em }\n"
 70    ".snac-footer { margin-top: 2em; font-size: 75% }\n"
 71    ".snac-poll-result { margin-left: auto; margin-right: auto; }\n"
 72    ".snac-list-of-lists { padding-left: 0; }\n"
 73    ".snac-list-of-lists li { display: inline; border: 1px solid #a0a0a0; border-radius: 25px;\n"
 74    "  margin-right: 0.5em; padding-left: 0.5em; padding-right: 0.5em; }\n"
 75    "@media (prefers-color-scheme: dark) { \n"
 76    "  body, input, textarea { background-color: #000; color: #fff; }\n"
 77    "  a { color: #7799dd }\n"
 78    "  a:visited { color: #aa99dd }\n"
 79    "}\n"
 80;
 81
 82const char *snac_blurb =
 83    "<p><b>%host%</b> is a <a href=\"https:/"
 84    "/en.wikipedia.org/wiki/Fediverse\">Fediverse</a> "
 85    "instance that uses the <a href=\"https:/"
 86    "/en.wikipedia.org/wiki/ActivityPub\">ActivityPub</a> "
 87    "protocol. In other words, users at this host can communicate with people "
 88    "that use software like Mastodon, Pleroma, Friendica, etc. "
 89    "all around the world.</p>\n"
 90    "<p>This server runs the "
 91    "<a href=\"" WHAT_IS_SNAC_URL "\">snac</a> software and there is no "
 92    "automatic sign-up process.</p>\n"
 93;
 94
 95static const char *greeting_html =
 96    "<!DOCTYPE html>\n"
 97    "<html><head>\n"
 98    "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>\n"
 99    "<link rel=\"icon\" type=\"image/x-icon\" href=\"https://%host%/favicon.ico\"/>\n"
100    "<title>Welcome to %host%</title>\n"
101    "<body style=\"margin: auto; max-width: 50em\">\n"
102    "%blurb%"
103    "<p>The following users are part of this community:</p>\n"
104    "\n"
105    "%userlist%\n"
106    "\n"
107    "<p>This site is powered by <abbr title=\"Social Networks Are Crap\">snac</abbr>.</p>\n"
108    "</body></html>\n";
109
110
111int write_default_css(void)
112{
113    FILE *f;
114
115    xs *sfn = xs_fmt("%s/style.css", srv_basedir);
116    if ((f = fopen(sfn, "w")) == NULL)
117        return 1;
118
119    fwrite(default_css, strlen(default_css), 1, f);
120    fclose(f);
121
122    return 0;
123}
124
125
126int snac_init(const char *basedir)
127{
128    FILE *f;
129
130    if (basedir == NULL) {
131        printf("Base directory: "); fflush(stdout);
132        srv_basedir = xs_strip_i(xs_readline(stdin));
133    }
134    else
135        srv_basedir = xs_str_new(basedir);
136
137    if (srv_basedir == NULL || *srv_basedir == '\0')
138        return 1;
139
140    if (xs_endswith(srv_basedir, "/"))
141        srv_basedir = xs_crop_i(srv_basedir, 0, -1);
142
143    if (mtime(srv_basedir) != 0.0) {
144        printf("ERROR: directory '%s' must not exist.\n", srv_basedir);
145        return 1;
146    }
147
148    srv_config = xs_json_loads(default_srv_config);
149
150    xs *layout = xs_number_new(disk_layout);
151    srv_config = xs_dict_set(srv_config, "layout", layout);
152
153    int is_unix_socket = 0;
154
155    printf("Network address or full path to unix socket [%s]: ", xs_dict_get(srv_config, "address")); fflush(stdout);
156    {
157        xs *i = xs_strip_i(xs_readline(stdin));
158        if (*i) {
159            srv_config = xs_dict_set(srv_config, "address", i);
160
161            if (*i == '/')
162                is_unix_socket = 1;
163        }
164    }
165
166    if (!is_unix_socket) {
167        printf("Network port [%d]: ", (int)xs_number_get(xs_dict_get(srv_config, "port"))); fflush(stdout);
168        {
169            xs *i = xs_strip_i(xs_readline(stdin));
170            if (*i) {
171                xs *n = xs_number_new(atoi(i));
172                srv_config = xs_dict_set(srv_config, "port", n);
173            }
174        }
175    }
176    else {
177        xs *n = xs_number_new(0);
178        srv_config = xs_dict_set(srv_config, "port", n);
179    }
180
181    printf("Host name: "); fflush(stdout);
182    {
183        xs *i = xs_strip_i(xs_readline(stdin));
184        if (*i == '\0')
185            return 1;
186
187        srv_config = xs_dict_set(srv_config, "host", i);
188    }
189
190    printf("URL prefix: "); fflush(stdout);
191    {
192        xs *i = xs_strip_i(xs_readline(stdin));
193
194        if (*i) {
195            if (xs_endswith(i, "/"))
196                i = xs_crop_i(i, 0, -1);
197
198            srv_config = xs_dict_set(srv_config, "prefix", i);
199        }
200    }
201
202    printf("Admin email address (optional): "); fflush(stdout);
203    {
204        xs *i = xs_strip_i(xs_readline(stdin));
205
206        srv_config = xs_dict_set(srv_config, "admin_email", i);
207    }
208
209    if (mkdirx(srv_basedir) == -1) {
210        printf("ERROR: cannot create directory '%s'\n", srv_basedir);
211        return 1;
212    }
213
214    xs *udir = xs_fmt("%s/user", srv_basedir);
215    mkdirx(udir);
216
217    xs *odir = xs_fmt("%s/object", srv_basedir);
218    mkdirx(odir);
219
220    xs *qdir = xs_fmt("%s/queue", srv_basedir);
221    mkdirx(qdir);
222
223    xs *ibdir = xs_fmt("%s/inbox", srv_basedir);
224    mkdirx(ibdir);
225
226    xs *gfn = xs_fmt("%s/greeting.html", srv_basedir);
227    if ((f = fopen(gfn, "w")) == NULL) {
228        printf("ERROR: cannot create '%s'\n", gfn);
229        return 1;
230    }
231
232    xs *gh = xs_replace(greeting_html, "%blurb%", snac_blurb);
233    fwrite(gh, strlen(gh), 1, f);
234    fclose(f);
235
236    if (write_default_css()) {
237        printf("ERROR: cannot create style.css\n");
238        return 1;
239    }
240
241    xs *cfn = xs_fmt("%s/server.json", srv_basedir);
242    if ((f = fopen(cfn, "w")) == NULL) {
243        printf("ERROR: cannot create '%s'\n", cfn);
244        return 1;
245    }
246
247    xs_json_dump(srv_config, 4, f);
248    fclose(f);
249
250    printf("Done.\n");
251    return 0;
252}
253
254
255void new_password(const char *uid, xs_str **clear_pwd, xs_str **hashed_pwd)
256/* creates a random password */
257{
258    int rndbuf[3];
259
260    xs_rnd_buf(rndbuf, sizeof(rndbuf));
261
262    *clear_pwd  = xs_base64_enc((char *)rndbuf, sizeof(rndbuf));
263    *hashed_pwd = hash_password(uid, *clear_pwd, NULL);
264}
265
266
267int adduser(const char *uid)
268/* creates a new user */
269{
270    snac snac;
271    xs *config = xs_dict_new();
272    xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
273    xs *pwd = NULL;
274    xs *pwd_f = NULL;
275    xs *key = NULL;
276    FILE *f;
277
278    if (uid == NULL) {
279        printf("Username: "); fflush(stdout);
280        uid = xs_strip_i(xs_readline(stdin));
281    }
282
283    if (!validate_uid(uid)) {
284        printf("ERROR: only alphanumeric characters and _ are allowed in user ids.\n");
285        return 1;
286    }
287
288    if (user_open(&snac, uid)) {
289        printf("ERROR: user '%s' already exists\n", snac.uid);
290        return 1;
291    }
292
293    new_password(uid, &pwd, &pwd_f);
294
295    config = xs_dict_append(config, "uid",       uid);
296    config = xs_dict_append(config, "name",      uid);
297    config = xs_dict_append(config, "avatar",    "");
298    config = xs_dict_append(config, "bio",       "");
299    config = xs_dict_append(config, "cw",        "");
300    config = xs_dict_append(config, "published", date);
301    config = xs_dict_append(config, "passwd",    pwd_f);
302
303    xs *basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
304
305    if (mkdirx(basedir) == -1) {
306        printf("ERROR: cannot create directory '%s'\n", basedir);
307        return 0;
308    }
309
310    const char *dirs[] = {
311        "followers", "following", "muted", "hidden",
312        "public", "private", "queue", "history",
313        "static", NULL };
314    int n;
315
316    for (n = 0; dirs[n]; n++) {
317        xs *d = xs_fmt("%s/%s", basedir, dirs[n]);
318        mkdirx(d);
319    }
320
321    xs *cfn = xs_fmt("%s/user.json", basedir);
322
323    if ((f = fopen(cfn, "w")) == NULL) {
324        printf("ERROR: cannot create '%s'\n", cfn);
325        return 1;
326    }
327    else {
328        xs_json_dump(config, 4, f);
329        fclose(f);
330    }
331
332    printf("\nCreating RSA key...\n");
333    key = xs_evp_genkey(4096);
334    printf("Done.\n");
335
336    xs *kfn = xs_fmt("%s/key.json", basedir);
337
338    if ((f = fopen(kfn, "w")) == NULL) {
339        printf("ERROR: cannot create '%s'\n", kfn);
340        return 1;
341    }
342    else {
343        xs_json_dump(key, 4, f);
344        fclose(f);
345    }
346
347    printf("\nUser password is %s\n", pwd);
348
349    printf("\nGo to %s/%s and continue configuring your user there.\n", srv_baseurl, uid);
350
351    return 0;
352}
353
354
355int resetpwd(snac *snac)
356/* creates a new password for the user */
357{
358    xs *clear_pwd  = NULL;
359    xs *hashed_pwd = NULL;
360    xs *fn         = xs_fmt("%s/user.json", snac->basedir);
361    FILE *f;
362    int ret = 0;
363
364    new_password(snac->uid, &clear_pwd, &hashed_pwd);
365
366    snac->config = xs_dict_set(snac->config, "passwd", hashed_pwd);
367
368    if ((f = fopen(fn, "w")) != NULL) {
369        xs_json_dump(snac->config, 4, f);
370        fclose(f);
371
372        printf("New password for user %s is %s\n", snac->uid, clear_pwd);
373    }
374    else {
375        printf("ERROR: cannot write to %s\n", fn);
376        ret = 1;
377    }
378
379    return ret;
380}
381
382
383void rm_rf(const char *dir)
384/* does an rm -rf (yes, I'm also scared) */
385{
386    xs *d = xs_str_cat(xs_dup(dir), "/" "*");
387    xs *l = xs_glob(d, 0, 0);
388    xs_list *p = l;
389    const xs_str *v;
390
391    if (dbglevel >= 1)
392        printf("Deleting directory %s\n", dir);
393
394    while (xs_list_iter(&p, &v)) {
395        struct stat st;
396
397        if (stat(v, &st) != -1) {
398            if (st.st_mode & S_IFDIR) {
399                rm_rf(v);
400            }
401            else {
402                if (dbglevel >= 1)
403                    printf("Deleting file %s\n", v);
404
405                if (unlink(v) == -1)
406                    printf("ERROR: cannot delete file %s\n", v);
407            }
408        }
409        else
410            printf("ERROR: stat() fail for %s\n", v);
411    }
412
413    if (rmdir(dir) == -1)
414        printf("ERROR: cannot delete directory %s\n", dir);
415}
416
417
418int deluser(snac *user)
419/* deletes a user */
420{
421    int ret = 0;
422    xs *fwers = following_list(user);
423    xs_list *p = fwers;
424    const xs_str *v;
425
426    while (xs_list_iter(&p, &v)) {
427        xs *object = NULL;
428
429        if (valid_status(following_get(user, v, &object))) {
430            xs *msg = msg_undo(user, xs_dict_get(object, "object"));
431
432            following_del(user, v);
433
434            enqueue_output_by_actor(user, msg, v, 0);
435
436            printf("Unfollowing actor %s\n", v);
437        }
438    }
439
440    rm_rf(user->basedir);
441
442    return ret;
443}
444
445
446void verify_links(snac *user)
447/* verifies a user's links */
448{
449    const xs_dict *p = xs_dict_get(user->config, "metadata");
450    const char *k, *v;
451    int changed = 0;
452
453    xs *headers = xs_dict_new();
454    headers = xs_dict_append(headers, "accept", "text/html");
455    headers = xs_dict_append(headers, "user-agent", USER_AGENT " (link verify)");
456
457    int c = 0;
458    while (p && xs_dict_next(p, &k, &v, &c)) {
459        /* not an https link? skip */
460        if (!xs_startswith(v, "https:/" "/"))
461            continue;
462
463        int status;
464        xs *req = NULL;
465        xs *payload = NULL;
466        int p_size = 0;
467
468        req = xs_http_request("GET", v, headers, NULL, 0, &status,
469            &payload, &p_size, 0);
470
471        if (!valid_status(status)) {
472            snac_log(user, xs_fmt("link %s verify error %d", v, status));
473            continue;
474        }
475
476        /* extract the links */
477        xs *ls = xs_regex_select(payload, "< *(a|link) +[^>]+>");
478
479        xs_list *lp = ls;
480        const char *ll;
481        int vfied = 0;
482
483        while (!vfied && xs_list_iter(&lp, &ll)) {
484            /* extract href and rel */
485            xs *r = xs_regex_select(ll, "(href|rel) *= *(\"[^\"]*\"|'[^']*')");
486
487            /* must have both attributes */
488            if (xs_list_len(r) != 2)
489                continue;
490
491            xs *href = NULL;
492            int is_rel_me = 0;
493            xs_list *pr = r;
494            const char *ar;
495
496            while (xs_list_iter(&pr, &ar)) {
497                xs *nq = xs_dup(ar);
498
499                nq = xs_replace_i(nq, "\"", "");
500                nq = xs_replace_i(nq, "'", "");
501
502                xs *r2 = xs_split_n(nq, "=", 1);
503                if (xs_list_len(r2) != 2)
504                    continue;
505
506                xs *ak = xs_strip_i(xs_dup(xs_list_get(r2, 0)));
507                xs *av = xs_strip_i(xs_dup(xs_list_get(r2, 1)));
508
509                if (strcmp(ak, "href") == 0)
510                    href = xs_dup(av);
511                else
512                if (strcmp(ak, "rel") == 0) {
513                    /* split the value by spaces */
514                    xs *vbs = xs_split(av, " ");
515
516                    /* is any of it "me"? */
517                    if (xs_list_in(vbs, "me") != -1)
518                        is_rel_me = 1;
519                }
520            }
521
522            /* after all this acrobatics, do we have an href and a rel="me"? */
523            if (href != NULL && is_rel_me) {
524                /* is it the same as the actor? */
525                if (strcmp(href, user->actor) == 0) {
526                    /* got it! */
527                    xs *verified_time = xs_number_new((double)time(NULL));
528
529                    if (user->links == NULL)
530                        user->links = xs_dict_new();
531
532                    user->links = xs_dict_set(user->links, v, verified_time);
533
534                    vfied = 1;
535                }
536                else
537                    snac_debug(user, 1,
538                        xs_fmt("verify link %s rel='me' found but not related (%s)", v, href));
539            }
540        }
541
542        if (vfied) {
543            changed++;
544            snac_log(user, xs_fmt("link %s verified", v));
545        }
546        else {
547            snac_log(user, xs_fmt("link %s not verified (rel='me' not found)", v));
548        }
549    }
550
551    if (changed) {
552        FILE *f;
553
554        /* update the links.json file */
555        xs *fn = xs_fmt("%s/links.json", user->basedir);
556        xs *bfn = xs_fmt("%s.bak", fn);
557
558        rename(fn, bfn);
559
560        if ((f = fopen(fn, "w")) != NULL) {
561            xs_json_dump(user->links, 4, f);
562            fclose(f);
563        }
564        else
565            rename(bfn, fn);
566    }
567}