utils.c 9.7 KB

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