utils.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. ;
  61. const char *snac_blurb =
  62. "<p><b>%host%</b> is a <a href=\"https:/"
  63. "/en.wikipedia.org/wiki/Fediverse\">Fediverse</a> "
  64. "instance that uses the <a href=\"https:/"
  65. "/en.wikipedia.org/wiki/ActivityPub\">ActivityPub</a> "
  66. "protocol. In other words, users at this host can communicate with people "
  67. "that use software like Mastodon, Pleroma, Friendica, etc. "
  68. "all around the world.</p>\n"
  69. "<p>This server runs the "
  70. "<a href=\"" WHAT_IS_SNAC_URL "\">snac</a> software and there is no "
  71. "automatic sign-up process.</p>\n"
  72. ;
  73. static const char *greeting_html =
  74. "<!DOCTYPE html>\n"
  75. "<html><head>\n"
  76. "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>\n"
  77. "<title>Welcome to %host%</title>\n"
  78. "<body style=\"margin: auto; max-width: 50em\">\n"
  79. "%blurb%"
  80. "<p>The following users are part of this community:</p>\n"
  81. "\n"
  82. "%userlist%\n"
  83. "\n"
  84. "<p>This site is powered by <abbr title=\"Social Networks Are Crap\">snac</abbr>.</p>\n"
  85. "</body></html>\n";
  86. int snac_init(const char *basedir)
  87. {
  88. FILE *f;
  89. if (basedir == NULL) {
  90. printf("Base directory: "); fflush(stdout);
  91. srv_basedir = xs_strip_i(xs_readline(stdin));
  92. }
  93. else
  94. srv_basedir = xs_str_new(basedir);
  95. if (srv_basedir == NULL || *srv_basedir == '\0')
  96. return 1;
  97. if (xs_endswith(srv_basedir, "/"))
  98. srv_basedir = xs_crop_i(srv_basedir, 0, -1);
  99. if (mtime(srv_basedir) != 0.0) {
  100. printf("ERROR: directory '%s' must not exist.\n", srv_basedir);
  101. return 1;
  102. }
  103. srv_config = xs_json_loads(default_srv_config);
  104. xs *layout = xs_number_new(disk_layout);
  105. srv_config = xs_dict_set(srv_config, "layout", layout);
  106. printf("Network address [%s]: ", xs_dict_get(srv_config, "address")); fflush(stdout);
  107. {
  108. xs *i = xs_strip_i(xs_readline(stdin));
  109. if (*i)
  110. srv_config = xs_dict_set(srv_config, "address", i);
  111. }
  112. printf("Network port [%d]: ", (int)xs_number_get(xs_dict_get(srv_config, "port"))); fflush(stdout);
  113. {
  114. xs *i = xs_strip_i(xs_readline(stdin));
  115. if (*i) {
  116. xs *n = xs_number_new(atoi(i));
  117. srv_config = xs_dict_set(srv_config, "port", n);
  118. }
  119. }
  120. printf("Host name: "); fflush(stdout);
  121. {
  122. xs *i = xs_strip_i(xs_readline(stdin));
  123. if (*i == '\0')
  124. return 1;
  125. srv_config = xs_dict_set(srv_config, "host", i);
  126. }
  127. printf("URL prefix: "); fflush(stdout);
  128. {
  129. xs *i = xs_strip_i(xs_readline(stdin));
  130. if (*i) {
  131. if (xs_endswith(i, "/"))
  132. i = xs_crop_i(i, 0, -1);
  133. srv_config = xs_dict_set(srv_config, "prefix", i);
  134. }
  135. }
  136. printf("Admin email address (optional): "); fflush(stdout);
  137. {
  138. xs *i = xs_strip_i(xs_readline(stdin));
  139. srv_config = xs_dict_set(srv_config, "admin_email", i);
  140. }
  141. if (mkdirx(srv_basedir) == -1) {
  142. printf("ERROR: cannot create directory '%s'\n", srv_basedir);
  143. return 1;
  144. }
  145. xs *udir = xs_fmt("%s/user", srv_basedir);
  146. mkdirx(udir);
  147. xs *odir = xs_fmt("%s/object", srv_basedir);
  148. mkdirx(odir);
  149. xs *qdir = xs_fmt("%s/queue", srv_basedir);
  150. mkdirx(qdir);
  151. xs *ibdir = xs_fmt("%s/inbox", srv_basedir);
  152. mkdirx(ibdir);
  153. xs *gfn = xs_fmt("%s/greeting.html", srv_basedir);
  154. if ((f = fopen(gfn, "w")) == NULL) {
  155. printf("ERROR: cannot create '%s'\n", gfn);
  156. return 1;
  157. }
  158. xs *gh = xs_replace(greeting_html, "%blurb%", snac_blurb);
  159. fwrite(gh, strlen(gh), 1, f);
  160. fclose(f);
  161. xs *sfn = xs_fmt("%s/style.css", srv_basedir);
  162. if ((f = fopen(sfn, "w")) == NULL) {
  163. printf("ERROR: cannot create '%s'\n", sfn);
  164. return 1;
  165. }
  166. fwrite(default_css, strlen(default_css), 1, f);
  167. fclose(f);
  168. xs *cfn = xs_fmt("%s/server.json", srv_basedir);
  169. if ((f = fopen(cfn, "w")) == NULL) {
  170. printf("ERROR: cannot create '%s'\n", cfn);
  171. return 1;
  172. }
  173. xs_json_dump(srv_config, 4, f);
  174. fclose(f);
  175. printf("Done.\n");
  176. return 0;
  177. }
  178. void new_password(const char *uid, xs_str **clear_pwd, xs_str **hashed_pwd)
  179. /* creates a random password */
  180. {
  181. int rndbuf[3];
  182. xs_rnd_buf(rndbuf, sizeof(rndbuf));
  183. *clear_pwd = xs_base64_enc((char *)rndbuf, sizeof(rndbuf));
  184. *hashed_pwd = hash_password(uid, *clear_pwd, NULL);
  185. }
  186. int adduser(const char *uid)
  187. /* creates a new user */
  188. {
  189. snac snac;
  190. xs *config = xs_dict_new();
  191. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  192. xs *pwd = NULL;
  193. xs *pwd_f = NULL;
  194. xs *key = NULL;
  195. FILE *f;
  196. if (uid == NULL) {
  197. printf("Username: "); fflush(stdout);
  198. uid = xs_strip_i(xs_readline(stdin));
  199. }
  200. if (!validate_uid(uid)) {
  201. printf("ERROR: only alphanumeric characters and _ are allowed in user ids.\n");
  202. return 1;
  203. }
  204. if (user_open(&snac, uid)) {
  205. printf("ERROR: user '%s' already exists\n", snac.uid);
  206. return 1;
  207. }
  208. new_password(uid, &pwd, &pwd_f);
  209. config = xs_dict_append(config, "uid", uid);
  210. config = xs_dict_append(config, "name", uid);
  211. config = xs_dict_append(config, "avatar", "");
  212. config = xs_dict_append(config, "bio", "");
  213. config = xs_dict_append(config, "cw", "");
  214. config = xs_dict_append(config, "published", date);
  215. config = xs_dict_append(config, "passwd", pwd_f);
  216. xs *basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
  217. if (mkdirx(basedir) == -1) {
  218. printf("ERROR: cannot create directory '%s'\n", basedir);
  219. return 0;
  220. }
  221. const char *dirs[] = {
  222. "followers", "following", "muted", "hidden",
  223. "public", "private", "queue", "history",
  224. "static", NULL };
  225. int n;
  226. for (n = 0; dirs[n]; n++) {
  227. xs *d = xs_fmt("%s/%s", basedir, dirs[n]);
  228. mkdirx(d);
  229. }
  230. xs *cfn = xs_fmt("%s/user.json", basedir);
  231. if ((f = fopen(cfn, "w")) == NULL) {
  232. printf("ERROR: cannot create '%s'\n", cfn);
  233. return 1;
  234. }
  235. else {
  236. xs_json_dump(config, 4, f);
  237. fclose(f);
  238. }
  239. printf("\nCreating RSA key...\n");
  240. key = xs_evp_genkey(4096);
  241. printf("Done.\n");
  242. xs *kfn = xs_fmt("%s/key.json", basedir);
  243. if ((f = fopen(kfn, "w")) == NULL) {
  244. printf("ERROR: cannot create '%s'\n", kfn);
  245. return 1;
  246. }
  247. else {
  248. xs_json_dump(key, 4, f);
  249. fclose(f);
  250. }
  251. printf("\nUser password is %s\n", pwd);
  252. printf("\nGo to %s/%s and continue configuring your user there.\n", srv_baseurl, uid);
  253. return 0;
  254. }
  255. int resetpwd(snac *snac)
  256. /* creates a new password for the user */
  257. {
  258. xs *clear_pwd = NULL;
  259. xs *hashed_pwd = NULL;
  260. xs *fn = xs_fmt("%s/user.json", snac->basedir);
  261. FILE *f;
  262. int ret = 0;
  263. new_password(snac->uid, &clear_pwd, &hashed_pwd);
  264. snac->config = xs_dict_set(snac->config, "passwd", hashed_pwd);
  265. if ((f = fopen(fn, "w")) != NULL) {
  266. xs_json_dump(snac->config, 4, f);
  267. fclose(f);
  268. printf("New password for user %s is %s\n", snac->uid, clear_pwd);
  269. }
  270. else {
  271. printf("ERROR: cannot write to %s\n", fn);
  272. ret = 1;
  273. }
  274. return ret;
  275. }
  276. void rm_rf(const char *dir)
  277. /* does an rm -rf (yes, I'm also scared) */
  278. {
  279. xs *d = xs_str_cat(xs_dup(dir), "/" "*");
  280. xs *l = xs_glob(d, 0, 0);
  281. xs_list *p = l;
  282. xs_str *v;
  283. if (dbglevel >= 1)
  284. printf("Deleting directory %s\n", dir);
  285. while (xs_list_iter(&p, &v)) {
  286. struct stat st;
  287. if (stat(v, &st) != -1) {
  288. if (st.st_mode & S_IFDIR) {
  289. rm_rf(v);
  290. }
  291. else {
  292. if (dbglevel >= 1)
  293. printf("Deleting file %s\n", v);
  294. if (unlink(v) == -1)
  295. printf("ERROR: cannot delete file %s\n", v);
  296. }
  297. }
  298. else
  299. printf("ERROR: stat() fail for %s\n", v);
  300. }
  301. if (rmdir(dir) == -1)
  302. printf("ERROR: cannot delete directory %s\n", dir);
  303. }
  304. int deluser(snac *user)
  305. /* deletes a user */
  306. {
  307. int ret = 0;
  308. xs *fwers = following_list(user);
  309. xs_list *p = fwers;
  310. xs_str *v;
  311. while (xs_list_iter(&p, &v)) {
  312. xs *object = NULL;
  313. if (valid_status(following_get(user, v, &object))) {
  314. xs *msg = msg_undo(user, xs_dict_get(object, "object"));
  315. following_del(user, v);
  316. enqueue_output_by_actor(user, msg, v, 0);
  317. printf("Unfollowing actor %s\n", v);
  318. }
  319. }
  320. rm_rf(user->basedir);
  321. return ret;
  322. }