utils.c 10.0 KB

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