utils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
  3. #include "xs.h"
  4. #include "xs_io.h"
  5. #include "xs_json.h"
  6. #include "xs_time.h"
  7. #include "xs_openssl.h"
  8. #include "xs_random.h"
  9. #include "xs_glob.h"
  10. #include "snac.h"
  11. #include <sys/stat.h>
  12. #include <stdlib.h>
  13. static const char *default_srv_config = "{"
  14. "\"host\": \"\","
  15. "\"prefix\": \"\","
  16. "\"address\": \"127.0.0.1\","
  17. "\"port\": 8001,"
  18. "\"layout\": 0.0,"
  19. "\"dbglevel\": 0,"
  20. "\"queue_retry_minutes\": 2,"
  21. "\"queue_retry_max\": 10,"
  22. "\"cssurls\": [\"\"],"
  23. "\"max_timeline_entries\": 128,"
  24. "\"timeline_purge_days\": 120,"
  25. "\"local_purge_days\": 0,"
  26. "\"admin_email\": \"\","
  27. "\"admin_account\": \"\","
  28. "\"title\": \"\","
  29. "\"short_description\": \"\","
  30. "\"fastcgi\": false"
  31. "}";
  32. static const char *default_css =
  33. "body { max-width: 48em; margin: auto; line-height: 1.5; padding: 0.8em; word-wrap: break-word; }\n"
  34. "pre { overflow-x: scroll; }\n"
  35. ".snac-embedded-video, img { max-width: 100% }\n"
  36. ".snac-origin { font-size: 85% }\n"
  37. ".snac-score { float: right; font-size: 85% }\n"
  38. ".snac-top-user { text-align: center; padding-bottom: 2em }\n"
  39. ".snac-top-user-name { font-size: 200% }\n"
  40. ".snac-top-user-id { font-size: 150% }\n"
  41. ".snac-avatar { float: left; height: 2.5em; padding: 0.25em }\n"
  42. ".snac-author { font-size: 90%; text-decoration: none }\n"
  43. ".snac-author-tag { font-size: 80% }\n"
  44. ".snac-pubdate { color: #a0a0a0; font-size: 90% }\n"
  45. ".snac-top-controls { padding-bottom: 1.5em }\n"
  46. ".snac-post { border-top: 1px solid #a0a0a0; }\n"
  47. ".snac-children { padding-left: 2em; border-left: 1px solid #a0a0a0; }\n"
  48. ".snac-textarea { font-family: inherit; width: 100% }\n"
  49. ".snac-history { border: 1px solid #606060; border-radius: 3px; margin: 2.5em 0; padding: 0 2em }\n"
  50. ".snac-btn-mute { float: right; margin-left: 0.5em }\n"
  51. ".snac-btn-unmute { float: right; margin-left: 0.5em }\n"
  52. ".snac-btn-follow { float: right; margin-left: 0.5em }\n"
  53. ".snac-btn-unfollow { float: right; margin-left: 0.5em }\n"
  54. ".snac-btn-hide { float: right; margin-left: 0.5em }\n"
  55. ".snac-btn-delete { float: right; margin-left: 0.5em }\n"
  56. ".snac-btn-limit { float: right; margin-left: 0.5em }\n"
  57. ".snac-btn-unlimit { float: right; margin-left: 0.5em }\n"
  58. ".snac-footer { margin-top: 2em; font-size: 75% }\n"
  59. ".snac-poll-result { margin-left: auto; margin-right: auto; }\n"
  60. "@media (prefers-color-scheme: dark) { \n"
  61. " body { background-color: #000; color: #fff; }\n"
  62. " a { color: #7799dd }\n"
  63. " a:visited { color: #aa99dd }\n"
  64. "}\n"
  65. ;
  66. const char *snac_blurb =
  67. "<p><b>%host%</b> is a <a href=\"https:/"
  68. "/en.wikipedia.org/wiki/Fediverse\">Fediverse</a> "
  69. "instance that uses the <a href=\"https:/"
  70. "/en.wikipedia.org/wiki/ActivityPub\">ActivityPub</a> "
  71. "protocol. In other words, users at this host can communicate with people "
  72. "that use software like Mastodon, Pleroma, Friendica, etc. "
  73. "all around the world.</p>\n"
  74. "<p>This server runs the "
  75. "<a href=\"" WHAT_IS_SNAC_URL "\">snac</a> software and there is no "
  76. "automatic sign-up process.</p>\n"
  77. ;
  78. static const char *greeting_html =
  79. "<!DOCTYPE html>\n"
  80. "<html><head>\n"
  81. "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>\n"
  82. "<link rel=\"icon\" type=\"image/x-icon\" href=\"https://%host%/favicon.ico\"/>\n"
  83. "<title>Welcome to %host%</title>\n"
  84. "<body style=\"margin: auto; max-width: 50em\">\n"
  85. "%blurb%"
  86. "<p>The following users are part of this community:</p>\n"
  87. "\n"
  88. "%userlist%\n"
  89. "\n"
  90. "<p>This site is powered by <abbr title=\"Social Networks Are Crap\">snac</abbr>.</p>\n"
  91. "</body></html>\n";
  92. int snac_init(const char *basedir)
  93. {
  94. FILE *f;
  95. if (basedir == NULL) {
  96. printf("Base directory: "); fflush(stdout);
  97. srv_basedir = xs_strip_i(xs_readline(stdin));
  98. }
  99. else
  100. srv_basedir = xs_str_new(basedir);
  101. if (srv_basedir == NULL || *srv_basedir == '\0')
  102. return 1;
  103. if (xs_endswith(srv_basedir, "/"))
  104. srv_basedir = xs_crop_i(srv_basedir, 0, -1);
  105. if (mtime(srv_basedir) != 0.0) {
  106. printf("ERROR: directory '%s' must not exist.\n", srv_basedir);
  107. return 1;
  108. }
  109. srv_config = xs_json_loads(default_srv_config);
  110. xs *layout = xs_number_new(disk_layout);
  111. srv_config = xs_dict_set(srv_config, "layout", layout);
  112. printf("Network address [%s]: ", xs_dict_get(srv_config, "address")); fflush(stdout);
  113. {
  114. xs *i = xs_strip_i(xs_readline(stdin));
  115. if (*i)
  116. srv_config = xs_dict_set(srv_config, "address", i);
  117. }
  118. printf("Network port [%d]: ", (int)xs_number_get(xs_dict_get(srv_config, "port"))); fflush(stdout);
  119. {
  120. xs *i = xs_strip_i(xs_readline(stdin));
  121. if (*i) {
  122. xs *n = xs_number_new(atoi(i));
  123. srv_config = xs_dict_set(srv_config, "port", n);
  124. }
  125. }
  126. printf("Host name: "); fflush(stdout);
  127. {
  128. xs *i = xs_strip_i(xs_readline(stdin));
  129. if (*i == '\0')
  130. return 1;
  131. srv_config = xs_dict_set(srv_config, "host", i);
  132. }
  133. printf("URL prefix: "); fflush(stdout);
  134. {
  135. xs *i = xs_strip_i(xs_readline(stdin));
  136. if (*i) {
  137. if (xs_endswith(i, "/"))
  138. i = xs_crop_i(i, 0, -1);
  139. srv_config = xs_dict_set(srv_config, "prefix", i);
  140. }
  141. }
  142. printf("Admin email address (optional): "); fflush(stdout);
  143. {
  144. xs *i = xs_strip_i(xs_readline(stdin));
  145. srv_config = xs_dict_set(srv_config, "admin_email", i);
  146. }
  147. if (mkdirx(srv_basedir) == -1) {
  148. printf("ERROR: cannot create directory '%s'\n", srv_basedir);
  149. return 1;
  150. }
  151. xs *udir = xs_fmt("%s/user", srv_basedir);
  152. mkdirx(udir);
  153. xs *odir = xs_fmt("%s/object", srv_basedir);
  154. mkdirx(odir);
  155. xs *qdir = xs_fmt("%s/queue", srv_basedir);
  156. mkdirx(qdir);
  157. xs *ibdir = xs_fmt("%s/inbox", srv_basedir);
  158. mkdirx(ibdir);
  159. xs *gfn = xs_fmt("%s/greeting.html", srv_basedir);
  160. if ((f = fopen(gfn, "w")) == NULL) {
  161. printf("ERROR: cannot create '%s'\n", gfn);
  162. return 1;
  163. }
  164. xs *gh = xs_replace(greeting_html, "%blurb%", snac_blurb);
  165. fwrite(gh, strlen(gh), 1, f);
  166. fclose(f);
  167. xs *sfn = xs_fmt("%s/style.css", srv_basedir);
  168. if ((f = fopen(sfn, "w")) == NULL) {
  169. printf("ERROR: cannot create '%s'\n", sfn);
  170. return 1;
  171. }
  172. fwrite(default_css, strlen(default_css), 1, f);
  173. fclose(f);
  174. xs *cfn = xs_fmt("%s/server.json", srv_basedir);
  175. if ((f = fopen(cfn, "w")) == NULL) {
  176. printf("ERROR: cannot create '%s'\n", cfn);
  177. return 1;
  178. }
  179. xs_json_dump(srv_config, 4, f);
  180. fclose(f);
  181. printf("Done.\n");
  182. return 0;
  183. }
  184. void new_password(const char *uid, xs_str **clear_pwd, xs_str **hashed_pwd)
  185. /* creates a random password */
  186. {
  187. int rndbuf[3];
  188. xs_rnd_buf(rndbuf, sizeof(rndbuf));
  189. *clear_pwd = xs_base64_enc((char *)rndbuf, sizeof(rndbuf));
  190. *hashed_pwd = hash_password(uid, *clear_pwd, NULL);
  191. }
  192. int adduser(const char *uid)
  193. /* creates a new user */
  194. {
  195. snac snac;
  196. xs *config = xs_dict_new();
  197. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  198. xs *pwd = NULL;
  199. xs *pwd_f = NULL;
  200. xs *key = NULL;
  201. FILE *f;
  202. if (uid == NULL) {
  203. printf("Username: "); fflush(stdout);
  204. uid = xs_strip_i(xs_readline(stdin));
  205. }
  206. if (!validate_uid(uid)) {
  207. printf("ERROR: only alphanumeric characters and _ are allowed in user ids.\n");
  208. return 1;
  209. }
  210. if (user_open(&snac, uid)) {
  211. printf("ERROR: user '%s' already exists\n", snac.uid);
  212. return 1;
  213. }
  214. new_password(uid, &pwd, &pwd_f);
  215. config = xs_dict_append(config, "uid", uid);
  216. config = xs_dict_append(config, "name", uid);
  217. config = xs_dict_append(config, "avatar", "");
  218. config = xs_dict_append(config, "bio", "");
  219. config = xs_dict_append(config, "cw", "");
  220. config = xs_dict_append(config, "published", date);
  221. config = xs_dict_append(config, "passwd", pwd_f);
  222. xs *basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
  223. if (mkdirx(basedir) == -1) {
  224. printf("ERROR: cannot create directory '%s'\n", basedir);
  225. return 0;
  226. }
  227. const char *dirs[] = {
  228. "followers", "following", "muted", "hidden",
  229. "public", "private", "queue", "history",
  230. "static", NULL };
  231. int n;
  232. for (n = 0; dirs[n]; n++) {
  233. xs *d = xs_fmt("%s/%s", basedir, dirs[n]);
  234. mkdirx(d);
  235. }
  236. xs *cfn = xs_fmt("%s/user.json", basedir);
  237. if ((f = fopen(cfn, "w")) == NULL) {
  238. printf("ERROR: cannot create '%s'\n", cfn);
  239. return 1;
  240. }
  241. else {
  242. xs_json_dump(config, 4, f);
  243. fclose(f);
  244. }
  245. printf("\nCreating RSA key...\n");
  246. key = xs_evp_genkey(4096);
  247. printf("Done.\n");
  248. xs *kfn = xs_fmt("%s/key.json", basedir);
  249. if ((f = fopen(kfn, "w")) == NULL) {
  250. printf("ERROR: cannot create '%s'\n", kfn);
  251. return 1;
  252. }
  253. else {
  254. xs_json_dump(key, 4, f);
  255. fclose(f);
  256. }
  257. printf("\nUser password is %s\n", pwd);
  258. printf("\nGo to %s/%s and continue configuring your user there.\n", srv_baseurl, uid);
  259. return 0;
  260. }
  261. int resetpwd(snac *snac)
  262. /* creates a new password for the user */
  263. {
  264. xs *clear_pwd = NULL;
  265. xs *hashed_pwd = NULL;
  266. xs *fn = xs_fmt("%s/user.json", snac->basedir);
  267. FILE *f;
  268. int ret = 0;
  269. new_password(snac->uid, &clear_pwd, &hashed_pwd);
  270. snac->config = xs_dict_set(snac->config, "passwd", hashed_pwd);
  271. if ((f = fopen(fn, "w")) != NULL) {
  272. xs_json_dump(snac->config, 4, f);
  273. fclose(f);
  274. printf("New password for user %s is %s\n", snac->uid, clear_pwd);
  275. }
  276. else {
  277. printf("ERROR: cannot write to %s\n", fn);
  278. ret = 1;
  279. }
  280. return ret;
  281. }
  282. void rm_rf(const char *dir)
  283. /* does an rm -rf (yes, I'm also scared) */
  284. {
  285. xs *d = xs_str_cat(xs_dup(dir), "/" "*");
  286. xs *l = xs_glob(d, 0, 0);
  287. xs_list *p = l;
  288. xs_str *v;
  289. if (dbglevel >= 1)
  290. printf("Deleting directory %s\n", dir);
  291. while (xs_list_iter(&p, &v)) {
  292. struct stat st;
  293. if (stat(v, &st) != -1) {
  294. if (st.st_mode & S_IFDIR) {
  295. rm_rf(v);
  296. }
  297. else {
  298. if (dbglevel >= 1)
  299. printf("Deleting file %s\n", v);
  300. if (unlink(v) == -1)
  301. printf("ERROR: cannot delete file %s\n", v);
  302. }
  303. }
  304. else
  305. printf("ERROR: stat() fail for %s\n", v);
  306. }
  307. if (rmdir(dir) == -1)
  308. printf("ERROR: cannot delete directory %s\n", dir);
  309. }
  310. int deluser(snac *user)
  311. /* deletes a user */
  312. {
  313. int ret = 0;
  314. xs *fwers = following_list(user);
  315. xs_list *p = fwers;
  316. xs_str *v;
  317. while (xs_list_iter(&p, &v)) {
  318. xs *object = NULL;
  319. if (valid_status(following_get(user, v, &object))) {
  320. xs *msg = msg_undo(user, xs_dict_get(object, "object"));
  321. following_del(user, v);
  322. enqueue_output_by_actor(user, msg, v, 0);
  323. printf("Unfollowing actor %s\n", v);
  324. }
  325. }
  326. rm_rf(user->basedir);
  327. return ret;
  328. }