utils.c 9.6 KB

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