utils.c 9.7 KB

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