main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2024 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 "snac.h"
  9. #include <sys/stat.h>
  10. int usage(void)
  11. {
  12. printf("snac " VERSION " - A simple, minimalistic ActivityPub instance\n");
  13. printf("Copyright (c) 2022 - 2024 grunfink et al. / MIT license\n");
  14. printf("\n");
  15. printf("Commands:\n");
  16. printf("\n");
  17. printf("init [{basedir}] Initializes the data storage\n");
  18. printf("upgrade {basedir} Upgrade to a new version\n");
  19. printf("adduser {basedir} [{uid}] Adds a new user\n");
  20. printf("deluser {basedir} {uid} Deletes a user\n");
  21. printf("httpd {basedir} Starts the HTTPD daemon\n");
  22. printf("purge {basedir} Purges old data\n");
  23. printf("state {basedir} Prints server state\n");
  24. printf("webfinger {basedir} {actor} Queries about an actor (@user@host or actor url)\n");
  25. printf("queue {basedir} {uid} Processes a user queue\n");
  26. printf("follow {basedir} {uid} {actor} Follows an actor\n");
  27. printf("unfollow {basedir} {uid} {actor} Unfollows an actor\n");
  28. printf("request {basedir} {uid} {url} Requests an object\n");
  29. printf("actor {basedir} [{uid}] {url} Requests an actor\n");
  30. printf("note {basedir} {uid} {text} [files...] Sends a note with optional attachments\n");
  31. printf("boost|announce {basedir} {uid} {url} Boosts (announces) a post\n");
  32. printf("unboost {basedir} {uid} {url} Unboosts a post\n");
  33. printf("resetpwd {basedir} {uid} Resets the password of a user\n");
  34. printf("ping {basedir} {uid} {actor} Pings an actor\n");
  35. printf("webfinger_s {basedir} {uid} {actor} Queries about an actor (@user@host or actor url)\n");
  36. printf("pin {basedir} {uid} {msg_url} Pins a message\n");
  37. printf("unpin {basedir} {uid} {msg_url} Unpins a message\n");
  38. printf("block {basedir} {instance_url} Blocks a full instance\n");
  39. printf("unblock {basedir} {instance_url} Unblocks a full instance\n");
  40. printf("limit {basedir} {uid} {actor} Limits an actor (drops their announces)\n");
  41. printf("unlimit {basedir} {uid} {actor} Unlimits an actor\n");
  42. printf("verify_links {basedir} {uid} Verifies a user's links (in the metadata)\n");
  43. return 1;
  44. }
  45. char *get_argv(int *argi, int argc, char *argv[])
  46. {
  47. if (*argi < argc)
  48. return argv[(*argi)++];
  49. else
  50. return NULL;
  51. }
  52. #define GET_ARGV() get_argv(&argi, argc, argv)
  53. int main(int argc, char *argv[])
  54. {
  55. char *cmd;
  56. char *basedir;
  57. char *user;
  58. char *url;
  59. int argi = 1;
  60. snac snac;
  61. /* ensure group has write access */
  62. umask(0007);
  63. if ((cmd = GET_ARGV()) == NULL)
  64. return usage();
  65. if (strcmp(cmd, "init") == 0) { /** **/
  66. /* initialize the data storage */
  67. /* ... */
  68. basedir = GET_ARGV();
  69. return snac_init(basedir);
  70. }
  71. if (strcmp(cmd, "upgrade") == 0) { /** **/
  72. int ret;
  73. /* upgrade */
  74. if ((basedir = GET_ARGV()) == NULL)
  75. return usage();
  76. if ((ret = srv_open(basedir, 1)) == 1)
  77. srv_log(xs_dup("OK"));
  78. return ret;
  79. }
  80. if (strcmp(cmd, "markdown") == 0) { /** **/
  81. /* undocumented, for testing only */
  82. xs *c = xs_readall(stdin);
  83. xs *fc = not_really_markdown(c, NULL, NULL);
  84. printf("<html>\n%s\n</html>\n", fc);
  85. return 0;
  86. }
  87. if ((basedir = GET_ARGV()) == NULL)
  88. return usage();
  89. if (!srv_open(basedir, 0)) {
  90. srv_log(xs_fmt("error opening data storage at %s", basedir));
  91. return 1;
  92. }
  93. if (strcmp(cmd, "adduser") == 0) { /** **/
  94. user = GET_ARGV();
  95. return adduser(user);
  96. return 0;
  97. }
  98. if (strcmp(cmd, "httpd") == 0) { /** **/
  99. httpd();
  100. srv_free();
  101. return 0;
  102. }
  103. if (strcmp(cmd, "purge") == 0) { /** **/
  104. purge_all();
  105. return 0;
  106. }
  107. if (strcmp(cmd, "state") == 0) { /** **/
  108. xs *shm_name = NULL;
  109. srv_state *p_state = srv_state_op(&shm_name, 1);
  110. if (p_state == NULL)
  111. return 1;
  112. srv_state ss = *p_state;
  113. int n;
  114. printf("server: %s (%s)\n", xs_dict_get(srv_config, "host"), USER_AGENT);
  115. xs *uptime = xs_str_time_diff(time(NULL) - ss.srv_start_time);
  116. printf("uptime: %s\n", uptime);
  117. printf("job fifo size (cur): %d\n", ss.job_fifo_size);
  118. printf("job fifo size (peak): %d\n", ss.peak_job_fifo_size);
  119. char *th_states[] = { "stopped", "waiting", "input", "output" };
  120. for (n = 0; n < ss.n_threads; n++)
  121. printf("thread #%d state: %s\n", n, th_states[ss.th_state[n]]);
  122. return 0;
  123. }
  124. if ((user = GET_ARGV()) == NULL)
  125. return usage();
  126. if (strcmp(cmd, "block") == 0) { /** **/
  127. int ret = instance_block(user);
  128. if (ret < 0) {
  129. fprintf(stderr, "Error blocking instance %s: %d\n", user, ret);
  130. return 1;
  131. }
  132. return 0;
  133. }
  134. if (strcmp(cmd, "unblock") == 0) { /** **/
  135. int ret = instance_unblock(user);
  136. if (ret < 0) {
  137. fprintf(stderr, "Error unblocking instance %s: %d\n", user, ret);
  138. return 1;
  139. }
  140. return 0;
  141. }
  142. if (strcmp(cmd, "webfinger") == 0) { /** **/
  143. xs *actor = NULL;
  144. xs *uid = NULL;
  145. int status;
  146. status = webfinger_request(user, &actor, &uid);
  147. printf("status: %d\n", status);
  148. if (actor != NULL)
  149. printf("actor: %s\n", actor);
  150. if (uid != NULL)
  151. printf("uid: %s\n", uid);
  152. return 0;
  153. }
  154. if (argi == argc && strcmp(cmd, "actor") == 0) { /** **/
  155. /* query an actor without user (non-signed) */
  156. xs *actor = NULL;
  157. int status;
  158. status = actor_request(NULL, user, &actor);
  159. printf("status: %d\n", status);
  160. if (valid_status(status)) {
  161. xs_json_dump(actor, 4, stdout);
  162. printf("\n");
  163. }
  164. return 0;
  165. }
  166. if (!user_open(&snac, user)) {
  167. printf("invalid user '%s'\n", user);
  168. return 1;
  169. }
  170. lastlog_write(&snac, "cmdline");
  171. if (strcmp(cmd, "resetpwd") == 0) { /** **/
  172. return resetpwd(&snac);
  173. }
  174. if (strcmp(cmd, "deluser") == 0) { /** **/
  175. return deluser(&snac);
  176. }
  177. if (strcmp(cmd, "update") == 0) { /** **/
  178. xs *a_msg = msg_actor(&snac);
  179. xs *u_msg = msg_update(&snac, a_msg);
  180. enqueue_message(&snac, u_msg);
  181. return 0;
  182. }
  183. if (strcmp(cmd, "queue") == 0) { /** **/
  184. process_user_queue(&snac);
  185. return 0;
  186. }
  187. if (strcmp(cmd, "verify_links") == 0) { /** **/
  188. verify_links(&snac);
  189. return 0;
  190. }
  191. if (strcmp(cmd, "timeline") == 0) { /** **/
  192. #if 0
  193. xs *list = local_list(&snac, XS_ALL);
  194. xs *body = html_timeline(&snac, list, 1);
  195. printf("%s\n", body);
  196. user_free(&snac);
  197. srv_free();
  198. #endif
  199. xs *idx = xs_fmt("%s/private.idx", snac.basedir);
  200. xs *list = index_list_desc(idx, 0, 256);
  201. xs *tl = timeline_top_level(&snac, list);
  202. xs_json_dump(tl, 4, stdout);
  203. return 0;
  204. }
  205. if ((url = GET_ARGV()) == NULL)
  206. return usage();
  207. if (strcmp(cmd, "webfinger_s") == 0) { /** **/
  208. xs *actor = NULL;
  209. xs *uid = NULL;
  210. int status;
  211. status = webfinger_request_signed(&snac, url, &actor, &uid);
  212. printf("status: %d\n", status);
  213. if (actor != NULL)
  214. printf("actor: %s\n", actor);
  215. if (uid != NULL)
  216. printf("uid: %s\n", uid);
  217. return 0;
  218. }
  219. if (strcmp(cmd, "boost") == 0 || strcmp(cmd, "announce") == 0) { /** **/
  220. xs *msg = msg_admiration(&snac, url, "Announce");
  221. if (msg != NULL) {
  222. enqueue_message(&snac, msg);
  223. if (dbglevel) {
  224. xs_json_dump(msg, 4, stdout);
  225. }
  226. }
  227. return 0;
  228. }
  229. if (strcmp(cmd, "unboost") == 0) { /** **/
  230. xs *msg = msg_repulsion(&snac, url, "Announce");
  231. if (msg != NULL) {
  232. enqueue_message(&snac, msg);
  233. if (dbglevel) {
  234. xs_json_dump(msg, 4, stdout);
  235. }
  236. }
  237. return 0;
  238. }
  239. if (strcmp(cmd, "follow") == 0) { /** **/
  240. xs *msg = msg_follow(&snac, url);
  241. if (msg != NULL) {
  242. char *actor = xs_dict_get(msg, "object");
  243. following_add(&snac, actor, msg);
  244. enqueue_output_by_actor(&snac, msg, actor, 0);
  245. if (dbglevel) {
  246. xs_json_dump(msg, 4, stdout);
  247. }
  248. }
  249. return 0;
  250. }
  251. if (strcmp(cmd, "unfollow") == 0) { /** **/
  252. xs *object = NULL;
  253. if (valid_status(following_get(&snac, url, &object))) {
  254. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  255. following_del(&snac, url);
  256. enqueue_output_by_actor(&snac, msg, url, 0);
  257. snac_log(&snac, xs_fmt("unfollowed actor %s", url));
  258. }
  259. else
  260. snac_log(&snac, xs_fmt("actor is not being followed %s", url));
  261. return 0;
  262. }
  263. if (strcmp(cmd, "limit") == 0) { /** **/
  264. int ret;
  265. if (!following_check(&snac, url))
  266. snac_log(&snac, xs_fmt("actor %s is not being followed", url));
  267. else
  268. if ((ret = limit(&snac, url)) == 0)
  269. snac_log(&snac, xs_fmt("actor %s is now limited", url));
  270. else
  271. snac_log(&snac, xs_fmt("error limiting actor %s (%d)", url, ret));
  272. return 0;
  273. }
  274. if (strcmp(cmd, "unlimit") == 0) { /** **/
  275. int ret;
  276. if (!following_check(&snac, url))
  277. snac_log(&snac, xs_fmt("actor %s is not being followed", url));
  278. else
  279. if ((ret = unlimit(&snac, url)) == 0)
  280. snac_log(&snac, xs_fmt("actor %s is no longer limited", url));
  281. else
  282. snac_log(&snac, xs_fmt("error unlimiting actor %s (%d)", url, ret));
  283. return 0;
  284. }
  285. if (strcmp(cmd, "ping") == 0) { /** **/
  286. xs *actor_o = NULL;
  287. if (!xs_startswith(url, "https:/")) {
  288. /* try to resolve via webfinger */
  289. if (!valid_status(webfinger_request(url, &url, NULL))) {
  290. srv_log(xs_fmt("cannot resolve %s via webfinger", url));
  291. return 1;
  292. }
  293. }
  294. if (valid_status(actor_request(&snac, url, &actor_o))) {
  295. xs *msg = msg_ping(&snac, url);
  296. enqueue_output_by_actor(&snac, msg, url, 0);
  297. if (dbglevel) {
  298. xs_json_dump(msg, 4, stdout);
  299. }
  300. srv_log(xs_fmt("Ping sent to %s -- see log for Pong reply", url));
  301. }
  302. else {
  303. srv_log(xs_fmt("Error getting actor %s", url));
  304. return 1;
  305. }
  306. return 0;
  307. }
  308. if (strcmp(cmd, "pin") == 0) { /** **/
  309. int ret = pin(&snac, url);
  310. if (ret < 0) {
  311. fprintf(stderr, "error pinning %s %d\n", url, ret);
  312. return 1;
  313. }
  314. return 0;
  315. }
  316. if (strcmp(cmd, "unpin") == 0) { /** **/
  317. int ret = unpin(&snac, url);
  318. if (ret < 0) {
  319. fprintf(stderr, "error unpinning %s %d\n", url, ret);
  320. return 1;
  321. }
  322. return 0;
  323. }
  324. if (strcmp(cmd, "question") == 0) { /** **/
  325. int end_secs = 5 * 60;
  326. xs *opts = xs_split(url, ";");
  327. xs *msg = msg_question(&snac, "Poll", NULL, opts, 0, end_secs);
  328. xs *c_msg = msg_create(&snac, msg);
  329. if (dbglevel) {
  330. xs_json_dump(c_msg, 4, stdout);
  331. }
  332. enqueue_message(&snac, c_msg);
  333. enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
  334. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  335. return 0;
  336. }
  337. if (strcmp(cmd, "request") == 0) { /** **/
  338. int status;
  339. xs *data = NULL;
  340. status = activitypub_request(&snac, url, &data);
  341. printf("status: %d\n", status);
  342. if (data != NULL) {
  343. xs_json_dump(data, 4, stdout);
  344. }
  345. return 0;
  346. }
  347. if (strcmp(cmd, "request2") == 0) { /** **/
  348. enqueue_object_request(&snac, url, 2);
  349. return 0;
  350. }
  351. if (strcmp(cmd, "actor") == 0) { /** **/
  352. int status;
  353. xs *data = NULL;
  354. status = actor_request(&snac, url, &data);
  355. printf("status: %d\n", status);
  356. if (valid_status(status)) {
  357. xs_json_dump(data, 4, stdout);
  358. }
  359. return 0;
  360. }
  361. if (strcmp(cmd, "note") == 0) { /** **/
  362. xs *content = NULL;
  363. xs *msg = NULL;
  364. xs *c_msg = NULL;
  365. xs *attl = xs_list_new();
  366. char *fn = NULL;
  367. /* iterate possible attachments */
  368. while ((fn = GET_ARGV())) {
  369. FILE *f;
  370. if ((f = fopen(fn, "rb")) != NULL) {
  371. /* get the file size and content */
  372. fseek(f, 0, SEEK_END);
  373. int sz = ftell(f);
  374. fseek(f, 0, SEEK_SET);
  375. xs *atc = xs_readall(f);
  376. fclose(f);
  377. char *ext = strrchr(fn, '.');
  378. xs *hash = xs_md5_hex(fn, strlen(fn));
  379. xs *id = xs_fmt("%s%s", hash, ext);
  380. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  381. /* store */
  382. static_put(&snac, id, atc, sz);
  383. xs *l = xs_list_new();
  384. l = xs_list_append(l, url);
  385. l = xs_list_append(l, ""); /* alt text */
  386. attl = xs_list_append(attl, l);
  387. }
  388. else
  389. fprintf(stderr, "Error opening '%s' as attachment\n", fn);
  390. }
  391. if (strcmp(url, "-e") == 0) {
  392. /* get the content from an editor */
  393. FILE *f;
  394. unlink("/tmp/snac-edit.txt");
  395. system("$EDITOR /tmp/snac-edit.txt");
  396. if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
  397. content = xs_readall(f);
  398. fclose(f);
  399. unlink("/tmp/snac-edit.txt");
  400. }
  401. else {
  402. printf("Nothing to send\n");
  403. return 1;
  404. }
  405. }
  406. else
  407. if (strcmp(url, "-") == 0) {
  408. /* get the content from stdin */
  409. content = xs_readall(stdin);
  410. }
  411. else
  412. content = xs_dup(url);
  413. msg = msg_note(&snac, content, NULL, NULL, attl, 0);
  414. c_msg = msg_create(&snac, msg);
  415. if (dbglevel) {
  416. xs_json_dump(c_msg, 4, stdout);
  417. }
  418. enqueue_message(&snac, c_msg);
  419. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  420. return 0;
  421. }
  422. fprintf(stderr, "ERROR: bad command '%s'\n", cmd);
  423. return 1;
  424. }