utils.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 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 "snac.h"
  10. #include <sys/stat.h>
  11. #include <stdlib.h>
  12. static 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. static 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-btn-limit { float: right; margin-left: 0.5em }\n"
  53. ".snac-btn-unlimit { float: right; margin-left: 0.5em }\n"
  54. ".snac-footer { margin-top: 2em; font-size: 75% }\n"
  55. ".snac-poll-result { margin-left: auto; margin-right: auto; }\n"
  56. ;
  57. static const char *greeting_html =
  58. "<!DOCTYPE html>\n"
  59. "<html><head>\n"
  60. "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>\n"
  61. "<title>Welcome to %host%</title>\n"
  62. "<body style=\"margin: auto; max-width: 50em\">\n"
  63. "<h1>Welcome to %host%</h1>\n"
  64. "<p>This is a <a href=\"https://en.wikipedia.org/wiki/Fediverse\">Fediverse</a> instance\n"
  65. "that uses the <a href=\"https://en.wikipedia.org/wiki/ActivityPub\">ActivityPub</a> protocol.\n"
  66. "In other words, users at this host can communicate with people that use software like\n"
  67. "Mastodon, Pleroma, Friendica, etc. all around the world.</p>\n"
  68. "\n"
  69. "<p>There is no automatic sign up process for this server. If you want to be a part of\n"
  70. "this community, please write an email to %admin_email%\n"
  71. "and ask politely indicating what is your preferred user id (alphanumeric characters\n"
  72. "only).</p>\n"
  73. "\n"
  74. "<p>The following users are already part of this community:</p>\n"
  75. "\n"
  76. "%userlist%\n"
  77. "\n"
  78. "<p>This site is powered by <abbr title=\"Social Networks Are Crap\">snac</abbr>.</p>\n"
  79. "</body></html>\n";
  80. int snac_init(const char *basedir)
  81. {
  82. FILE *f;
  83. if (basedir == NULL) {
  84. printf("Base directory: "); fflush(stdout);
  85. srv_basedir = xs_strip_i(xs_readline(stdin));
  86. }
  87. else
  88. srv_basedir = xs_str_new(basedir);
  89. if (srv_basedir == NULL || *srv_basedir == '\0')
  90. return 1;
  91. if (xs_endswith(srv_basedir, "/"))
  92. srv_basedir = xs_crop_i(srv_basedir, 0, -1);
  93. if (mtime(srv_basedir) != 0.0) {
  94. printf("ERROR: directory '%s' must not exist.\n", srv_basedir);
  95. return 1;
  96. }
  97. srv_config = xs_json_loads(default_srv_config);
  98. xs *layout = xs_number_new(disk_layout);
  99. srv_config = xs_dict_set(srv_config, "layout", layout);
  100. printf("Network address [%s]: ", xs_dict_get(srv_config, "address")); fflush(stdout);
  101. {
  102. xs *i = xs_strip_i(xs_readline(stdin));
  103. if (*i)
  104. srv_config = xs_dict_set(srv_config, "address", i);
  105. }
  106. printf("Network port [%d]: ", (int)xs_number_get(xs_dict_get(srv_config, "port"))); fflush(stdout);
  107. {
  108. xs *i = xs_strip_i(xs_readline(stdin));
  109. if (*i) {
  110. xs *n = xs_number_new(atoi(i));
  111. srv_config = xs_dict_set(srv_config, "port", n);
  112. }
  113. }
  114. printf("Host name: "); fflush(stdout);
  115. {
  116. xs *i = xs_strip_i(xs_readline(stdin));
  117. if (*i == '\0')
  118. return 1;
  119. srv_config = xs_dict_set(srv_config, "host", i);
  120. }
  121. printf("URL prefix: "); fflush(stdout);
  122. {
  123. xs *i = xs_strip_i(xs_readline(stdin));
  124. if (*i) {
  125. if (xs_endswith(i, "/"))
  126. i = xs_crop_i(i, 0, -1);
  127. srv_config = xs_dict_set(srv_config, "prefix", i);
  128. }
  129. }
  130. printf("Admin email address (optional): "); fflush(stdout);
  131. {
  132. xs *i = xs_strip_i(xs_readline(stdin));
  133. srv_config = xs_dict_set(srv_config, "admin_email", i);
  134. }
  135. if (mkdirx(srv_basedir) == -1) {
  136. printf("ERROR: cannot create directory '%s'\n", srv_basedir);
  137. return 1;
  138. }
  139. xs *udir = xs_fmt("%s/user", srv_basedir);
  140. mkdirx(udir);
  141. xs *odir = xs_fmt("%s/object", srv_basedir);
  142. mkdirx(odir);
  143. xs *qdir = xs_fmt("%s/queue", srv_basedir);
  144. mkdirx(qdir);
  145. xs *ibdir = xs_fmt("%s/inbox", srv_basedir);
  146. mkdirx(ibdir);
  147. xs *gfn = xs_fmt("%s/greeting.html", srv_basedir);
  148. if ((f = fopen(gfn, "w")) == NULL) {
  149. printf("ERROR: cannot create '%s'\n", gfn);
  150. return 1;
  151. }
  152. fwrite(greeting_html, strlen(greeting_html), 1, f);
  153. fclose(f);
  154. xs *sfn = xs_fmt("%s/style.css", srv_basedir);
  155. if ((f = fopen(sfn, "w")) == NULL) {
  156. printf("ERROR: cannot create '%s'\n", sfn);
  157. return 1;
  158. }
  159. fwrite(default_css, strlen(default_css), 1, f);
  160. fclose(f);
  161. xs *cfn = xs_fmt("%s/server.json", srv_basedir);
  162. if ((f = fopen(cfn, "w")) == NULL) {
  163. printf("ERROR: cannot create '%s'\n", cfn);
  164. return 1;
  165. }
  166. xs_json_dump(srv_config, 4, f);
  167. fclose(f);
  168. printf("Done.\n");
  169. return 0;
  170. }
  171. void new_password(const char *uid, d_char **clear_pwd, d_char **hashed_pwd)
  172. /* creates a random password */
  173. {
  174. int rndbuf[3];
  175. xs_rnd_buf(rndbuf, sizeof(rndbuf));
  176. *clear_pwd = xs_base64_enc((char *)rndbuf, sizeof(rndbuf));
  177. *hashed_pwd = hash_password(uid, *clear_pwd, NULL);
  178. }
  179. int adduser(const char *uid)
  180. /* creates a new user */
  181. {
  182. snac snac;
  183. xs *config = xs_dict_new();
  184. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  185. xs *pwd = NULL;
  186. xs *pwd_f = NULL;
  187. xs *key = NULL;
  188. FILE *f;
  189. if (uid == NULL) {
  190. printf("Username: "); fflush(stdout);
  191. uid = xs_strip_i(xs_readline(stdin));
  192. }
  193. if (!validate_uid(uid)) {
  194. printf("ERROR: only alphanumeric characters and _ are allowed in user ids.\n");
  195. return 1;
  196. }
  197. if (user_open(&snac, uid)) {
  198. printf("ERROR: user '%s' already exists\n", uid);
  199. return 1;
  200. }
  201. new_password(uid, &pwd, &pwd_f);
  202. config = xs_dict_append(config, "uid", uid);
  203. config = xs_dict_append(config, "name", uid);
  204. config = xs_dict_append(config, "avatar", "");
  205. config = xs_dict_append(config, "bio", "");
  206. config = xs_dict_append(config, "cw", "");
  207. config = xs_dict_append(config, "published", date);
  208. config = xs_dict_append(config, "passwd", pwd_f);
  209. xs *basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
  210. if (mkdirx(basedir) == -1) {
  211. printf("ERROR: cannot create directory '%s'\n", basedir);
  212. return 0;
  213. }
  214. const char *dirs[] = {
  215. "followers", "following", "muted", "hidden",
  216. "public", "private", "queue", "history",
  217. "static", NULL };
  218. int n;
  219. for (n = 0; dirs[n]; n++) {
  220. xs *d = xs_fmt("%s/%s", basedir, dirs[n]);
  221. mkdirx(d);
  222. }
  223. xs *cfn = xs_fmt("%s/user.json", basedir);
  224. if ((f = fopen(cfn, "w")) == NULL) {
  225. printf("ERROR: cannot create '%s'\n", cfn);
  226. return 1;
  227. }
  228. else {
  229. xs_json_dump(config, 4, 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_json_dump(key, 4, f);
  242. fclose(f);
  243. }
  244. printf("\nUser password is %s\n", pwd);
  245. printf("\nGo to %s/%s and continue configuring your user there.\n", srv_baseurl, uid);
  246. return 0;
  247. }
  248. int resetpwd(snac *snac)
  249. /* creates a new password for the user */
  250. {
  251. xs *clear_pwd = NULL;
  252. xs *hashed_pwd = NULL;
  253. xs *fn = xs_fmt("%s/user.json", snac->basedir);
  254. FILE *f;
  255. int ret = 0;
  256. new_password(snac->uid, &clear_pwd, &hashed_pwd);
  257. snac->config = xs_dict_set(snac->config, "passwd", hashed_pwd);
  258. if ((f = fopen(fn, "w")) != NULL) {
  259. xs_json_dump(snac->config, 4, 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. }