utils.c 9.9 KB

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