utils.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink / 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 "snac.h"
  10. #include <sys/stat.h>
  11. #include <stdlib.h>
  12. const char *default_srv_config = "{"
  13. "\"host\": \"\","
  14. "\"prefix\": \"\","
  15. "\"address\": \"127.0.0.1\","
  16. "\"port\": 8001,"
  17. "\"layout\": 0.0,"
  18. "\"dbglevel\": 0,"
  19. "\"queue_retry_minutes\": 2,"
  20. "\"queue_retry_max\": 10,"
  21. "\"cssurls\": [\"\"],"
  22. "\"max_timeline_entries\": 128,"
  23. "\"timeline_purge_days\": 120,"
  24. "\"local_purge_days\": 0,"
  25. "\"admin_email\": \"\","
  26. "\"admin_account\": \"\""
  27. "}";
  28. const char *default_css =
  29. "body { max-width: 48em; margin: auto; line-height: 1.5; padding: 0.8em }\n"
  30. "img { max-width: 100% }\n"
  31. ".snac-origin { font-size: 85% }\n"
  32. ".snac-score { float: right; font-size: 85% }\n"
  33. ".snac-top-user { text-align: center; padding-bottom: 2em }\n"
  34. ".snac-top-user-name { font-size: 200% }\n"
  35. ".snac-top-user-id { font-size: 150% }\n"
  36. ".snac-avatar { float: left; height: 2.5em; padding: 0.25em }\n"
  37. ".snac-author { font-size: 90%; text-decoration: none }\n"
  38. ".snac-author-tag { font-size: 80% }\n"
  39. ".snac-pubdate { color: #a0a0a0; font-size: 90% }\n"
  40. ".snac-top-controls { padding-bottom: 1.5em }\n"
  41. ".snac-post { border-top: 1px solid #a0a0a0; }\n"
  42. ".snac-children { padding-left: 2em; border-left: 1px solid #a0a0a0; }\n"
  43. ".snac-textarea { font-family: inherit; width: 100% }\n"
  44. ".snac-history { border: 1px solid #606060; border-radius: 3px; margin: 2.5em 0; padding: 0 2em }\n"
  45. ".snac-btn-mute { float: right; margin-left: 0.5em }\n"
  46. ".snac-btn-unmute { float: right; margin-left: 0.5em }\n"
  47. ".snac-btn-follow { float: right; margin-left: 0.5em }\n"
  48. ".snac-btn-unfollow { float: right; margin-left: 0.5em }\n"
  49. ".snac-btn-hide { float: right; margin-left: 0.5em }\n"
  50. ".snac-btn-delete { float: right; margin-left: 0.5em }\n"
  51. ".snac-footer { margin-top: 2em; font-size: 75% }\n"
  52. ".snac-poll-result { margin-left: auto; margin-right: auto; }\n";
  53. const char *greeting_html =
  54. "<!DOCTYPE html>\n"
  55. "<html><head>\n"
  56. "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>\n"
  57. "<title>Welcome to %host%</title>\n"
  58. "<body style=\"margin: auto; max-width: 50em\">\n"
  59. "<h1>Welcome to %host%</h1>\n"
  60. "<p>This is a <a href=\"https://en.wikipedia.org/wiki/Fediverse\">Fediverse</a> instance\n"
  61. "that uses the <a href=\"https://en.wikipedia.org/wiki/ActivityPub\">ActivityPub</a> protocol.\n"
  62. "In other words, users at this host can communicate with people that use software like\n"
  63. "Mastodon, Pleroma, Friendica, etc. all around the world.</p>\n"
  64. "\n"
  65. "<p>There is no automatic sign up process for this server. If you want to be a part of\n"
  66. "this community, please write an email to %admin_email%\n"
  67. "and ask politely indicating what is your preferred user id (alphanumeric characters\n"
  68. "only).</p>\n"
  69. "\n"
  70. "<p>The following users are already part of this community:</p>\n"
  71. "\n"
  72. "%userlist%\n"
  73. "\n"
  74. "<p>This site is powered by <abbr title=\"Social Networks Are Crap\">snac</abbr>.</p>\n"
  75. "</body></html>\n";
  76. int snac_init(const char *basedir)
  77. {
  78. FILE *f;
  79. if (basedir == NULL) {
  80. printf("Base directory:\n");
  81. srv_basedir = xs_strip_i(xs_readline(stdin));
  82. }
  83. else
  84. srv_basedir = xs_str_new(basedir);
  85. if (srv_basedir == NULL || *srv_basedir == '\0')
  86. return 1;
  87. if (xs_endswith(srv_basedir, "/"))
  88. srv_basedir = xs_crop_i(srv_basedir, 0, -1);
  89. if (mtime(srv_basedir) != 0.0) {
  90. printf("ERROR: directory '%s' must not exist\n", srv_basedir);
  91. return 1;
  92. }
  93. srv_config = xs_json_loads(default_srv_config);
  94. xs *layout = xs_number_new(disk_layout);
  95. srv_config = xs_dict_set(srv_config, "layout", layout);
  96. printf("Network address [%s]:\n", xs_dict_get(srv_config, "address"));
  97. {
  98. xs *i = xs_strip_i(xs_readline(stdin));
  99. if (*i)
  100. srv_config = xs_dict_set(srv_config, "address", i);
  101. }
  102. printf("Network port [%d]:\n", (int)xs_number_get(xs_dict_get(srv_config, "port")));
  103. {
  104. xs *i = xs_strip_i(xs_readline(stdin));
  105. if (*i) {
  106. xs *n = xs_number_new(atoi(i));
  107. srv_config = xs_dict_set(srv_config, "port", n);
  108. }
  109. }
  110. printf("Host name:\n");
  111. {
  112. xs *i = xs_strip_i(xs_readline(stdin));
  113. if (*i == '\0')
  114. return 1;
  115. srv_config = xs_dict_set(srv_config, "host", i);
  116. }
  117. printf("URL prefix:\n");
  118. {
  119. xs *i = xs_strip_i(xs_readline(stdin));
  120. if (*i) {
  121. if (xs_endswith(i, "/"))
  122. i = xs_crop_i(i, 0, -1);
  123. srv_config = xs_dict_set(srv_config, "prefix", i);
  124. }
  125. }
  126. printf("Admin email address (optional):\n");
  127. {
  128. xs *i = xs_strip_i(xs_readline(stdin));
  129. srv_config = xs_dict_set(srv_config, "admin_email", i);
  130. }
  131. if (mkdirx(srv_basedir) == -1) {
  132. printf("ERROR: cannot create directory '%s'\n", srv_basedir);
  133. return 1;
  134. }
  135. xs *udir = xs_fmt("%s/user", srv_basedir);
  136. mkdirx(udir);
  137. xs *odir = xs_fmt("%s/object", srv_basedir);
  138. mkdirx(odir);
  139. xs *qdir = xs_fmt("%s/queue", srv_basedir);
  140. mkdirx(qdir);
  141. xs *ibdir = xs_fmt("%s/inbox", srv_basedir);
  142. mkdirx(ibdir);
  143. xs *gfn = xs_fmt("%s/greeting.html", srv_basedir);
  144. if ((f = fopen(gfn, "w")) == NULL) {
  145. printf("ERROR: cannot create '%s'\n", gfn);
  146. return 1;
  147. }
  148. fwrite(greeting_html, strlen(greeting_html), 1, f);
  149. fclose(f);
  150. xs *sfn = xs_fmt("%s/style.css", srv_basedir);
  151. if ((f = fopen(sfn, "w")) == NULL) {
  152. printf("ERROR: cannot create '%s'\n", sfn);
  153. return 1;
  154. }
  155. fwrite(default_css, strlen(default_css), 1, f);
  156. fclose(f);
  157. xs *cfn = xs_fmt("%s/server.json", srv_basedir);
  158. if ((f = fopen(cfn, "w")) == NULL) {
  159. printf("ERROR: cannot create '%s'\n", cfn);
  160. return 1;
  161. }
  162. xs *j = xs_json_dumps_pp(srv_config, 4);
  163. fwrite(j, strlen(j), 1, f);
  164. fclose(f);
  165. printf("Done.\n");
  166. return 0;
  167. }
  168. void new_password(const char *uid, d_char **clear_pwd, d_char **hashed_pwd)
  169. /* creates a random password */
  170. {
  171. int rndbuf[3];
  172. xs_rnd_buf(rndbuf, sizeof(rndbuf));
  173. *clear_pwd = xs_base64_enc((char *)rndbuf, sizeof(rndbuf));
  174. *hashed_pwd = hash_password(uid, *clear_pwd, NULL);
  175. }
  176. int adduser(const char *uid)
  177. /* creates a new user */
  178. {
  179. snac snac;
  180. xs *config = xs_dict_new();
  181. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  182. xs *pwd = NULL;
  183. xs *pwd_f = NULL;
  184. xs *key = NULL;
  185. FILE *f;
  186. if (uid == NULL) {
  187. printf("User id:\n");
  188. uid = xs_strip_i(xs_readline(stdin));
  189. }
  190. if (!validate_uid(uid)) {
  191. printf("ERROR: only alphanumeric characters and _ are allowed in user ids.\n");
  192. return 1;
  193. }
  194. if (user_open(&snac, uid)) {
  195. printf("ERROR: user '%s' already exists\n", uid);
  196. return 1;
  197. }
  198. new_password(uid, &pwd, &pwd_f);
  199. config = xs_dict_append(config, "uid", uid);
  200. config = xs_dict_append(config, "name", uid);
  201. config = xs_dict_append(config, "avatar", "");
  202. config = xs_dict_append(config, "bio", "");
  203. config = xs_dict_append(config, "cw", "");
  204. config = xs_dict_append(config, "published", date);
  205. config = xs_dict_append(config, "passwd", pwd_f);
  206. xs *basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
  207. if (mkdirx(basedir) == -1) {
  208. printf("ERROR: cannot create directory '%s'\n", basedir);
  209. return 0;
  210. }
  211. const char *dirs[] = {
  212. "followers", "following", "muted", "hidden",
  213. "public", "private", "queue", "history",
  214. "static", NULL };
  215. int n;
  216. for (n = 0; dirs[n]; n++) {
  217. xs *d = xs_fmt("%s/%s", basedir, dirs[n]);
  218. mkdirx(d);
  219. }
  220. xs *cfn = xs_fmt("%s/user.json", basedir);
  221. if ((f = fopen(cfn, "w")) == NULL) {
  222. printf("ERROR: cannot create '%s'\n", cfn);
  223. return 1;
  224. }
  225. else {
  226. xs *j = xs_json_dumps_pp(config, 4);
  227. fwrite(j, strlen(j), 1, f);
  228. fclose(f);
  229. }
  230. printf("\nCreating RSA key...\n");
  231. key = xs_evp_genkey(4096);
  232. printf("Done.\n");
  233. xs *kfn = xs_fmt("%s/key.json", basedir);
  234. if ((f = fopen(kfn, "w")) == NULL) {
  235. printf("ERROR: cannot create '%s'\n", kfn);
  236. return 1;
  237. }
  238. else {
  239. xs *j = xs_json_dumps_pp(key, 4);
  240. fwrite(j, strlen(j), 1, f);
  241. fclose(f);
  242. }
  243. printf("\nUser password is %s\n", pwd);
  244. printf("\nGo to %s/%s and continue configuring your user there.\n", srv_baseurl, uid);
  245. return 0;
  246. }
  247. int resetpwd(snac *snac)
  248. /* creates a new password for the user */
  249. {
  250. xs *clear_pwd = NULL;
  251. xs *hashed_pwd = NULL;
  252. xs *fn = xs_fmt("%s/user.json", snac->basedir);
  253. FILE *f;
  254. int ret = 0;
  255. new_password(snac->uid, &clear_pwd, &hashed_pwd);
  256. snac->config = xs_dict_set(snac->config, "passwd", hashed_pwd);
  257. if ((f = fopen(fn, "w")) != NULL) {
  258. xs *j = xs_json_dumps_pp(snac->config, 4);
  259. fwrite(j, strlen(j), 1, f);
  260. fclose(f);
  261. printf("New password for user %s is %s\n", snac->uid, clear_pwd);
  262. }
  263. else {
  264. printf("ERROR: cannot write to %s\n", fn);
  265. ret = 1;
  266. }
  267. return ret;
  268. }