main.c 17 KB

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