main.c 14 KB

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