main.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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("bookmark {basedir} {uid} {msg_url} Bookmarks a message\n");
  39. printf("unbookmark {basedir} {uid} {msg_url} Unbookmarks a message\n");
  40. printf("block {basedir} {instance_url} Blocks a full instance\n");
  41. printf("unblock {basedir} {instance_url} Unblocks a full instance\n");
  42. printf("limit {basedir} {uid} {actor} Limits an actor (drops their announces)\n");
  43. printf("unlimit {basedir} {uid} {actor} Unlimits an actor\n");
  44. printf("verify_links {basedir} {uid} Verifies a user's links (in the metadata)\n");
  45. printf("search {basedir} {uid} {regex} Searches posts by content\n");
  46. printf("aka {basedir} {uid} {actor} Sets actor (@user@host or url) as the a.k.a.\n");
  47. printf("export_csv {basedir} {uid} Exports data as CSV files into current directory\n");
  48. printf("migrate {basedir} {uid} Migrates the account to the one set as the a.k.a.\n");
  49. return 1;
  50. }
  51. char *get_argv(int *argi, int argc, char *argv[])
  52. {
  53. if (*argi < argc)
  54. return argv[(*argi)++];
  55. else
  56. return NULL;
  57. }
  58. #define GET_ARGV() get_argv(&argi, argc, argv)
  59. int main(int argc, char *argv[])
  60. {
  61. char *cmd;
  62. char *basedir;
  63. char *user;
  64. char *url;
  65. int argi = 1;
  66. snac snac;
  67. /* ensure group has write access */
  68. umask(0007);
  69. if ((cmd = GET_ARGV()) == NULL)
  70. return usage();
  71. if (strcmp(cmd, "init") == 0) { /** **/
  72. /* initialize the data storage */
  73. /* ... */
  74. basedir = GET_ARGV();
  75. return snac_init(basedir);
  76. }
  77. if (strcmp(cmd, "upgrade") == 0) { /** **/
  78. int ret;
  79. /* upgrade */
  80. if ((basedir = GET_ARGV()) == NULL)
  81. return usage();
  82. if ((ret = srv_open(basedir, 1)) == 1)
  83. srv_log(xs_dup("OK"));
  84. return ret;
  85. }
  86. if (strcmp(cmd, "markdown") == 0) { /** **/
  87. /* undocumented, for testing only */
  88. xs *c = xs_readall(stdin);
  89. xs *fc = not_really_markdown(c, NULL, NULL);
  90. printf("<html>\n%s\n</html>\n", fc);
  91. return 0;
  92. }
  93. if ((basedir = GET_ARGV()) == NULL)
  94. return usage();
  95. if (!srv_open(basedir, 0)) {
  96. srv_log(xs_fmt("error opening data storage at %s", basedir));
  97. return 1;
  98. }
  99. if (strcmp(cmd, "adduser") == 0) { /** **/
  100. user = GET_ARGV();
  101. return adduser(user);
  102. return 0;
  103. }
  104. if (strcmp(cmd, "httpd") == 0) { /** **/
  105. httpd();
  106. srv_free();
  107. return 0;
  108. }
  109. if (strcmp(cmd, "purge") == 0) { /** **/
  110. purge_all();
  111. return 0;
  112. }
  113. if (strcmp(cmd, "state") == 0) { /** **/
  114. xs *shm_name = NULL;
  115. srv_state *p_state = srv_state_op(&shm_name, 1);
  116. if (p_state == NULL)
  117. return 1;
  118. srv_state ss = *p_state;
  119. int n;
  120. printf("server: %s (%s)\n", xs_dict_get(srv_config, "host"), USER_AGENT);
  121. xs *uptime = xs_str_time_diff(time(NULL) - ss.srv_start_time);
  122. printf("uptime: %s\n", uptime);
  123. printf("job fifo size (cur): %d\n", ss.job_fifo_size);
  124. printf("job fifo size (peak): %d\n", ss.peak_job_fifo_size);
  125. char *th_states[] = { "stopped", "waiting", "input", "output" };
  126. for (n = 0; n < ss.n_threads; n++)
  127. printf("thread #%d state: %s\n", n, th_states[ss.th_state[n]]);
  128. return 0;
  129. }
  130. if ((user = GET_ARGV()) == NULL)
  131. return usage();
  132. if (strcmp(cmd, "block") == 0) { /** **/
  133. int ret = instance_block(user);
  134. if (ret < 0) {
  135. fprintf(stderr, "Error blocking instance %s: %d\n", user, ret);
  136. return 1;
  137. }
  138. return 0;
  139. }
  140. if (strcmp(cmd, "unblock") == 0) { /** **/
  141. int ret = instance_unblock(user);
  142. if (ret < 0) {
  143. fprintf(stderr, "Error unblocking instance %s: %d\n", user, ret);
  144. return 1;
  145. }
  146. return 0;
  147. }
  148. if (strcmp(cmd, "webfinger") == 0) { /** **/
  149. xs *actor = NULL;
  150. xs *uid = NULL;
  151. int status;
  152. status = webfinger_request(user, &actor, &uid);
  153. printf("status: %d\n", status);
  154. if (actor != NULL)
  155. printf("actor: %s\n", actor);
  156. if (uid != NULL)
  157. printf("uid: %s\n", uid);
  158. return 0;
  159. }
  160. if (argi == argc && strcmp(cmd, "actor") == 0) { /** **/
  161. /* query an actor without user (non-signed) */
  162. xs *actor = NULL;
  163. int status;
  164. status = actor_request(NULL, user, &actor);
  165. printf("status: %d\n", status);
  166. if (valid_status(status)) {
  167. xs_json_dump(actor, 4, stdout);
  168. printf("\n");
  169. }
  170. return 0;
  171. }
  172. if (!user_open(&snac, user)) {
  173. printf("invalid user '%s'\n", user);
  174. return 1;
  175. }
  176. lastlog_write(&snac, "cmdline");
  177. if (strcmp(cmd, "resetpwd") == 0) { /** **/
  178. return resetpwd(&snac);
  179. }
  180. if (strcmp(cmd, "deluser") == 0) { /** **/
  181. return deluser(&snac);
  182. }
  183. if (strcmp(cmd, "update") == 0) { /** **/
  184. xs *a_msg = msg_actor(&snac);
  185. xs *u_msg = msg_update(&snac, a_msg);
  186. enqueue_message(&snac, u_msg);
  187. return 0;
  188. }
  189. if (strcmp(cmd, "queue") == 0) { /** **/
  190. process_user_queue(&snac);
  191. return 0;
  192. }
  193. if (strcmp(cmd, "verify_links") == 0) { /** **/
  194. verify_links(&snac);
  195. return 0;
  196. }
  197. if (strcmp(cmd, "timeline") == 0) { /** **/
  198. #if 0
  199. xs *list = local_list(&snac, XS_ALL);
  200. xs *body = html_timeline(&snac, list, 1);
  201. printf("%s\n", body);
  202. user_free(&snac);
  203. srv_free();
  204. #endif
  205. xs *idx = xs_fmt("%s/private.idx", snac.basedir);
  206. xs *list = index_list_desc(idx, 0, 256);
  207. xs *tl = timeline_top_level(&snac, list);
  208. xs_json_dump(tl, 4, stdout);
  209. return 0;
  210. }
  211. if (strcmp(cmd, "export_csv") == 0) { /** **/
  212. export_csv(&snac);
  213. return 0;
  214. }
  215. if (strcmp(cmd, "migrate") == 0) { /** **/
  216. return migrate_account(&snac);
  217. }
  218. if ((url = GET_ARGV()) == NULL)
  219. return usage();
  220. if (strcmp(cmd, "aka") == 0) { /** **/
  221. xs *actor = NULL;
  222. xs *uid = NULL;
  223. int status = HTTP_STATUS_OK;
  224. if (*url == '\0')
  225. actor = xs_dup("");
  226. else
  227. status = webfinger_request(url, &actor, &uid);
  228. if (valid_status(status)) {
  229. snac.config = xs_dict_set(snac.config, "aka", actor);
  230. user_persist(&snac, 1);
  231. }
  232. else
  233. snac_log(&snac, xs_fmt("Webfinger error for %s %d", url, status));
  234. return 0;
  235. }
  236. if (strcmp(cmd, "webfinger_s") == 0) { /** **/
  237. xs *actor = NULL;
  238. xs *uid = NULL;
  239. int status;
  240. status = webfinger_request_signed(&snac, url, &actor, &uid);
  241. printf("status: %d\n", status);
  242. if (actor != NULL)
  243. printf("actor: %s\n", actor);
  244. if (uid != NULL)
  245. printf("uid: %s\n", uid);
  246. return 0;
  247. }
  248. if (strcmp(cmd, "boost") == 0 || strcmp(cmd, "announce") == 0) { /** **/
  249. xs *msg = msg_admiration(&snac, url, "Announce");
  250. if (msg != NULL) {
  251. enqueue_message(&snac, msg);
  252. if (dbglevel) {
  253. xs_json_dump(msg, 4, stdout);
  254. }
  255. }
  256. return 0;
  257. }
  258. if (strcmp(cmd, "unboost") == 0) { /** **/
  259. xs *msg = msg_repulsion(&snac, url, "Announce");
  260. if (msg != NULL) {
  261. enqueue_message(&snac, msg);
  262. if (dbglevel) {
  263. xs_json_dump(msg, 4, stdout);
  264. }
  265. }
  266. return 0;
  267. }
  268. if (strcmp(cmd, "follow") == 0) { /** **/
  269. xs *msg = msg_follow(&snac, url);
  270. if (msg != NULL) {
  271. const char *actor = xs_dict_get(msg, "object");
  272. following_add(&snac, actor, msg);
  273. enqueue_output_by_actor(&snac, msg, actor, 0);
  274. if (dbglevel) {
  275. xs_json_dump(msg, 4, stdout);
  276. }
  277. }
  278. return 0;
  279. }
  280. if (strcmp(cmd, "unfollow") == 0) { /** **/
  281. xs *object = NULL;
  282. if (valid_status(following_get(&snac, url, &object))) {
  283. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  284. following_del(&snac, url);
  285. enqueue_output_by_actor(&snac, msg, url, 0);
  286. snac_log(&snac, xs_fmt("unfollowed actor %s", url));
  287. }
  288. else
  289. snac_log(&snac, xs_fmt("actor is not being followed %s", url));
  290. return 0;
  291. }
  292. if (strcmp(cmd, "limit") == 0) { /** **/
  293. int ret;
  294. if (!following_check(&snac, url))
  295. snac_log(&snac, xs_fmt("actor %s is not being followed", url));
  296. else
  297. if ((ret = limit(&snac, url)) == 0)
  298. snac_log(&snac, xs_fmt("actor %s is now limited", url));
  299. else
  300. snac_log(&snac, xs_fmt("error limiting actor %s (%d)", url, ret));
  301. return 0;
  302. }
  303. if (strcmp(cmd, "unlimit") == 0) { /** **/
  304. int ret;
  305. if (!following_check(&snac, url))
  306. snac_log(&snac, xs_fmt("actor %s is not being followed", url));
  307. else
  308. if ((ret = unlimit(&snac, url)) == 0)
  309. snac_log(&snac, xs_fmt("actor %s is no longer limited", url));
  310. else
  311. snac_log(&snac, xs_fmt("error unlimiting actor %s (%d)", url, ret));
  312. return 0;
  313. }
  314. if (strcmp(cmd, "search") == 0) { /** **/
  315. int to;
  316. /* 'url' contains the regex */
  317. xs *r = content_search(&snac, url, 1, 0, XS_ALL, 10, &to);
  318. int c = 0;
  319. const char *v;
  320. /* print results as standalone links */
  321. while (xs_list_next(r, &v, &c)) {
  322. printf("%s/admin/p/%s\n", snac.actor, v);
  323. }
  324. return 0;
  325. }
  326. if (strcmp(cmd, "ping") == 0) { /** **/
  327. xs *actor_o = NULL;
  328. if (!xs_startswith(url, "https:/")) {
  329. /* try to resolve via webfinger */
  330. if (!valid_status(webfinger_request(url, &url, NULL))) {
  331. srv_log(xs_fmt("cannot resolve %s via webfinger", url));
  332. return 1;
  333. }
  334. }
  335. if (valid_status(actor_request(&snac, url, &actor_o))) {
  336. xs *msg = msg_ping(&snac, url);
  337. enqueue_output_by_actor(&snac, msg, url, 0);
  338. if (dbglevel) {
  339. xs_json_dump(msg, 4, stdout);
  340. }
  341. srv_log(xs_fmt("Ping sent to %s -- see log for Pong reply", url));
  342. }
  343. else {
  344. srv_log(xs_fmt("Error getting actor %s", url));
  345. return 1;
  346. }
  347. return 0;
  348. }
  349. if (strcmp(cmd, "pin") == 0) { /** **/
  350. int ret = pin(&snac, url);
  351. if (ret < 0) {
  352. fprintf(stderr, "error pinning %s %d\n", url, ret);
  353. return 1;
  354. }
  355. return 0;
  356. }
  357. if (strcmp(cmd, "unpin") == 0) { /** **/
  358. int ret = unpin(&snac, url);
  359. if (ret < 0) {
  360. fprintf(stderr, "error unpinning %s %d\n", url, ret);
  361. return 1;
  362. }
  363. return 0;
  364. }
  365. if (strcmp(cmd, "bookmark") == 0) { /** **/
  366. int ret = bookmark(&snac, url);
  367. if (ret < 0) {
  368. fprintf(stderr, "error bookmarking %s %d\n", url, ret);
  369. return 1;
  370. }
  371. return 0;
  372. }
  373. if (strcmp(cmd, "unbookmark") == 0) { /** **/
  374. int ret = unbookmark(&snac, url);
  375. if (ret < 0) {
  376. fprintf(stderr, "error unbookmarking %s %d\n", url, ret);
  377. return 1;
  378. }
  379. return 0;
  380. }
  381. if (strcmp(cmd, "question") == 0) { /** **/
  382. int end_secs = 5 * 60;
  383. xs *opts = xs_split(url, ";");
  384. xs *msg = msg_question(&snac, "Poll", NULL, opts, 0, end_secs);
  385. xs *c_msg = msg_create(&snac, msg);
  386. if (dbglevel) {
  387. xs_json_dump(c_msg, 4, stdout);
  388. }
  389. enqueue_message(&snac, c_msg);
  390. enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
  391. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  392. return 0;
  393. }
  394. if (strcmp(cmd, "request") == 0) { /** **/
  395. int status;
  396. xs *data = NULL;
  397. status = activitypub_request(&snac, url, &data);
  398. printf("status: %d\n", status);
  399. if (data != NULL) {
  400. xs_json_dump(data, 4, stdout);
  401. }
  402. return 0;
  403. }
  404. if (strcmp(cmd, "request2") == 0) { /** **/
  405. enqueue_object_request(&snac, url, 2);
  406. return 0;
  407. }
  408. if (strcmp(cmd, "actor") == 0) { /** **/
  409. int status;
  410. xs *data = NULL;
  411. status = actor_request(&snac, url, &data);
  412. printf("status: %d\n", status);
  413. if (valid_status(status)) {
  414. xs_json_dump(data, 4, stdout);
  415. }
  416. return 0;
  417. }
  418. if (strcmp(cmd, "note") == 0) { /** **/
  419. xs *content = NULL;
  420. xs *msg = NULL;
  421. xs *c_msg = NULL;
  422. xs *attl = xs_list_new();
  423. char *fn = NULL;
  424. /* iterate possible attachments */
  425. while ((fn = GET_ARGV())) {
  426. FILE *f;
  427. if ((f = fopen(fn, "rb")) != NULL) {
  428. /* get the file size and content */
  429. fseek(f, 0, SEEK_END);
  430. int sz = ftell(f);
  431. fseek(f, 0, SEEK_SET);
  432. xs *atc = xs_readall(f);
  433. fclose(f);
  434. char *ext = strrchr(fn, '.');
  435. xs *hash = xs_md5_hex(fn, strlen(fn));
  436. xs *id = xs_fmt("%s%s", hash, ext);
  437. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  438. /* store */
  439. static_put(&snac, id, atc, sz);
  440. xs *l = xs_list_new();
  441. l = xs_list_append(l, url);
  442. l = xs_list_append(l, ""); /* alt text */
  443. attl = xs_list_append(attl, l);
  444. }
  445. else
  446. fprintf(stderr, "Error opening '%s' as attachment\n", fn);
  447. }
  448. if (strcmp(url, "-e") == 0) {
  449. /* get the content from an editor */
  450. FILE *f;
  451. unlink("/tmp/snac-edit.txt");
  452. system("$EDITOR /tmp/snac-edit.txt");
  453. if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
  454. content = xs_readall(f);
  455. fclose(f);
  456. unlink("/tmp/snac-edit.txt");
  457. }
  458. else {
  459. printf("Nothing to send\n");
  460. return 1;
  461. }
  462. }
  463. else
  464. if (strcmp(url, "-") == 0) {
  465. /* get the content from stdin */
  466. content = xs_readall(stdin);
  467. }
  468. else
  469. content = xs_dup(url);
  470. msg = msg_note(&snac, content, NULL, NULL, attl, 0);
  471. c_msg = msg_create(&snac, msg);
  472. if (dbglevel) {
  473. xs_json_dump(c_msg, 4, stdout);
  474. }
  475. enqueue_message(&snac, c_msg);
  476. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  477. return 0;
  478. }
  479. fprintf(stderr, "ERROR: bad command '%s'\n", cmd);
  480. return 1;
  481. }