main.c 18 KB

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