activitypub.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 grunfink - MIT license */
  3. #include "xs.h"
  4. #include "xs_encdec.h"
  5. #include "xs_json.h"
  6. #include "xs_curl.h"
  7. #include "xs_mime.h"
  8. #include "xs_openssl.h"
  9. #include "snac.h"
  10. const char *public_address = "https:/" "/www.w3.org/ns/activitystreams#Public";
  11. int activitypub_request(snac *snac, char *url, d_char **data)
  12. /* request an object */
  13. {
  14. int status;
  15. xs *response = NULL;
  16. xs *payload = NULL;
  17. int p_size;
  18. char *ctype;
  19. /* check if it's an url for this same site */
  20. /* ... */
  21. /* get from the net */
  22. response = http_signed_request(snac, "GET", url,
  23. NULL, NULL, 0, &status, &payload, &p_size);
  24. if (valid_status(status)) {
  25. /* ensure it's ActivityPub data */
  26. ctype = xs_dict_get(response, "content-type");
  27. if (xs_str_in(ctype, "application/activity+json") != -1)
  28. *data = xs_json_loads(payload);
  29. else
  30. status = 500;
  31. }
  32. if (!valid_status(status))
  33. *data = NULL;
  34. return status;
  35. }
  36. int actor_request(snac *snac, char *actor, d_char **data)
  37. /* request an actor */
  38. {
  39. int status, status2;
  40. xs *payload = NULL;
  41. /* get from disk first */
  42. status = actor_get(snac, actor, data);
  43. if (status == 200)
  44. return status;
  45. /* actor data non-existent or stale: get from the net */
  46. status2 = activitypub_request(snac, actor, &payload);
  47. if (valid_status(status2)) {
  48. /* renew data */
  49. status = actor_add(snac, actor, payload);
  50. *data = payload;
  51. payload = NULL;
  52. }
  53. return status;
  54. }
  55. void timeline_request(snac *snac, char *id, char *referrer)
  56. /* ensures that an entry and its ancestors are in the timeline */
  57. {
  58. if (!xs_is_null(id)) {
  59. /* is the admired object already there? */
  60. if (!timeline_here(snac, id)) {
  61. int status;
  62. xs *object = NULL;
  63. /* no; download it */
  64. status = activitypub_request(snac, id, &object);
  65. if (valid_status(status)) {
  66. /* does it have an ancestor? */
  67. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  68. /* recurse! */
  69. timeline_request(snac, in_reply_to, referrer);
  70. /* finally store */
  71. timeline_add(snac, id, object, in_reply_to, referrer);
  72. }
  73. }
  74. }
  75. }
  76. int send_to_inbox(snac *snac, char *inbox, char *msg, d_char **payload, int *p_size)
  77. /* sends a message to an Inbox */
  78. {
  79. int status;
  80. d_char *response;
  81. xs *j_msg = xs_json_dumps_pp(msg, 4);
  82. response = http_signed_request(snac, "POST", inbox,
  83. NULL, j_msg, strlen(j_msg), &status, payload, p_size);
  84. free(response);
  85. return status;
  86. }
  87. int send_to_actor(snac *snac, char *actor, char *msg, d_char **payload, int *p_size)
  88. /* sends a message to an actor */
  89. {
  90. int status;
  91. xs *data = NULL;
  92. /* resolve the actor first */
  93. status = actor_request(snac, actor, &data);
  94. if (valid_status(status)) {
  95. char *inbox = xs_dict_get(data, "inbox");
  96. if (inbox != NULL)
  97. status = send_to_inbox(snac, inbox, msg, payload, p_size);
  98. else
  99. status = 400;
  100. }
  101. snac_log(snac, xs_fmt("send_to_actor %s %d", actor, status));
  102. return status;
  103. }
  104. /** messages **/
  105. d_char *msg_base(snac *snac, char *type, char *id, char *actor, char *date, char *object)
  106. /* creates a base ActivityPub message */
  107. {
  108. xs *did = NULL;
  109. xs *published = NULL;
  110. /* generated values */
  111. if (date && strcmp(date, "@now") == 0)
  112. date = published = xs_utc_time("%Y-%m-%dT%H:%M:%SZ");
  113. if (id != NULL) {
  114. if (strcmp(id, "@dummy") == 0) {
  115. xs *ntid = tid(0);
  116. id = did = xs_fmt("%s/d/%s/%s", snac->actor, ntid, type);
  117. }
  118. else
  119. if (strcmp(id, "@object") == 0) {
  120. if (object != NULL)
  121. id = did = xs_fmt("%s/%s", xs_dict_get(object, "id"), type);
  122. else
  123. id = NULL;
  124. }
  125. }
  126. d_char *msg = xs_dict_new();
  127. msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
  128. msg = xs_dict_append(msg, "type", type);
  129. if (id != NULL)
  130. msg = xs_dict_append(msg, "id", id);
  131. if (actor != NULL)
  132. msg = xs_dict_append(msg, "actor", actor);
  133. if (date != NULL)
  134. msg = xs_dict_append(msg, "published", date);
  135. if (object != NULL)
  136. msg = xs_dict_append(msg, "object", object);
  137. return msg;
  138. }
  139. d_char *msg_collection(snac *snac, char *id)
  140. /* creates an empty OrderedCollection message */
  141. {
  142. d_char *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL, NULL);
  143. xs *ol = xs_list_new();
  144. xs *nz = xs_number_new(0);
  145. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  146. msg = xs_dict_append(msg, "orderedItems", ol);
  147. msg = xs_dict_append(msg, "totalItems", nz);
  148. return msg;
  149. }
  150. d_char *msg_accept(snac *snac, char *object, char *to)
  151. /* creates an Accept message (as a response to a Follow) */
  152. {
  153. d_char *msg = msg_base(snac, "Accept", "@dummy", snac->actor, NULL, object);
  154. msg = xs_dict_append(msg, "to", to);
  155. return msg;
  156. }
  157. d_char *msg_update(snac *snac, char *object)
  158. /* creates an Update message */
  159. {
  160. d_char *msg = msg_base(snac, "Update", "@object", snac->actor, "@now", object);
  161. msg = xs_dict_append(msg, "to", public_address);
  162. return msg;
  163. }
  164. d_char *msg_admiration(snac *snac, char *object, char *type)
  165. /* creates a Like or Announce message */
  166. {
  167. xs *a_msg = NULL;
  168. d_char *msg = NULL;
  169. /* call the object */
  170. timeline_request(snac, object, snac->actor);
  171. if ((a_msg = timeline_find(snac, object)) != NULL) {
  172. xs *rcpts = xs_list_new();
  173. msg = msg_base(snac, type, "@dummy", snac->actor, "@now", object);
  174. rcpts = xs_list_append(rcpts, public_address);
  175. rcpts = xs_list_append(rcpts, xs_dict_get(a_msg, "attributedTo"));
  176. msg = xs_dict_append(msg, "to", rcpts);
  177. }
  178. else
  179. snac_log(snac, xs_fmt("msg_admiration cannot retrieve object %s", object));
  180. return msg;
  181. }
  182. d_char *msg_actor(snac *snac)
  183. /* create a Person message for this actor */
  184. {
  185. xs *ctxt = xs_list_new();
  186. xs *icon = xs_dict_new();
  187. xs *keys = xs_dict_new();
  188. xs *avtr = NULL;
  189. xs *kid = NULL;
  190. d_char *msg = msg_base(snac, "Person", snac->actor, NULL, NULL, NULL);
  191. char *p;
  192. int n;
  193. /* change the @context (is this really necessary?) */
  194. ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams");
  195. ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1");
  196. msg = xs_dict_set(msg, "@context", ctxt);
  197. msg = xs_dict_set(msg, "url", snac->actor);
  198. msg = xs_dict_set(msg, "name", xs_dict_get(snac->config, "name"));
  199. msg = xs_dict_set(msg, "preferredUsername", snac->uid);
  200. msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published"));
  201. msg = xs_dict_set(msg, "summary", xs_dict_get(snac->config, "bio"));
  202. char *folders[] = { "inbox", "outbox", "followers", "following", NULL };
  203. for (n = 0; folders[n]; n++) {
  204. xs *f = xs_fmt("%s/%s", snac->actor, folders[n]);
  205. msg = xs_dict_set(msg, folders[n], f);
  206. }
  207. p = xs_dict_get(snac->config, "avatar");
  208. if (*p == '\0')
  209. avtr = xs_fmt("%s/susie.png", srv_baseurl);
  210. else
  211. avtr = xs_dup(p);
  212. icon = xs_dict_append(icon, "type", "Image");
  213. icon = xs_dict_append(icon, "mediaType", xs_mime_by_ext(avtr));
  214. icon = xs_dict_append(icon, "url", avtr);
  215. msg = xs_dict_set(msg, "icon", icon);
  216. kid = xs_fmt("%s#main-key", snac->actor);
  217. keys = xs_dict_append(keys, "id", kid);
  218. keys = xs_dict_append(keys, "owner", snac->actor);
  219. keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public"));
  220. msg = xs_dict_set(msg, "publicKey", keys);
  221. return msg;
  222. }
  223. /** queues **/
  224. void process_message(snac *snac, char *msg, char *req)
  225. /* processes an ActivityPub message from the input queue */
  226. {
  227. /* actor and type exist, were checked previously */
  228. char *actor = xs_dict_get(msg, "actor");
  229. char *type = xs_dict_get(msg, "type");
  230. char *object, *utype;
  231. object = xs_dict_get(msg, "object");
  232. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  233. utype = xs_dict_get(object, "type");
  234. else
  235. utype = "(null)";
  236. /* check the signature */
  237. /* ... */
  238. if (strcmp(type, "Follow") == 0) {
  239. xs *reply = msg_accept(snac, msg, actor);
  240. post(snac, reply);
  241. timeline_add(snac, xs_dict_get(msg, "id"), msg, NULL, NULL);
  242. follower_add(snac, actor, msg);
  243. snac_log(snac, xs_fmt("New follower %s", actor));
  244. }
  245. else
  246. /*
  247. if (strcmp(type, "Undo") == 0) {
  248. }
  249. else
  250. */
  251. if (strcmp(type, "Create") == 0) {
  252. if (strcmp(utype, "Note") == 0) {
  253. if (is_muted(snac, actor))
  254. snac_log(snac, xs_fmt("ignored 'Note' from muted actor %s", actor));
  255. else {
  256. char *id = xs_dict_get(object, "id");
  257. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  258. timeline_request(snac, in_reply_to, NULL);
  259. if (timeline_add(snac, id, object, in_reply_to, NULL))
  260. snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
  261. }
  262. }
  263. else
  264. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  265. }
  266. else
  267. /*
  268. if (strcmp(type, "Accept") == 0) {
  269. }
  270. else
  271. */
  272. if (strcmp(type, "Like") == 0) {
  273. if (xs_type(object) == XSTYPE_DICT)
  274. object = xs_dict_get(object, "id");
  275. timeline_admire(snac, object, actor, 1);
  276. snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
  277. }
  278. else
  279. if (strcmp(type, "Announce") == 0) {
  280. if (xs_type(object) == XSTYPE_DICT)
  281. object = xs_dict_get(object, "id");
  282. timeline_request(snac, object, actor);
  283. timeline_admire(snac, object, actor, 0);
  284. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  285. }
  286. /*
  287. else
  288. if (strcmp(type, "Update") == 0) {
  289. }
  290. else
  291. if (strcmp(type, "Delete") == 0) {
  292. }
  293. */
  294. else
  295. snac_debug(snac, 1, xs_fmt("process_message type '%s' ignored", type));
  296. }
  297. void process_queue(snac *snac)
  298. /* processes the queue */
  299. {
  300. xs *list;
  301. char *p, *fn;
  302. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  303. list = queue(snac);
  304. p = list;
  305. while (xs_list_iter(&p, &fn)) {
  306. xs *q_item = dequeue(snac, fn);
  307. char *type;
  308. if (q_item == NULL) {
  309. snac_log(snac, xs_fmt("process_queue q_item error"));
  310. continue;
  311. }
  312. if ((type = xs_dict_get(q_item, "type")) == NULL)
  313. type = "output";
  314. if (strcmp(type, "output") == 0) {
  315. int status;
  316. char *actor = xs_dict_get(q_item, "actor");
  317. char *msg = xs_dict_get(q_item, "object");
  318. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  319. xs *payload = NULL;
  320. int p_size = 0;
  321. /* deliver */
  322. status = send_to_actor(snac, actor, msg, &payload, &p_size);
  323. if (!valid_status(status)) {
  324. /* error sending; reenqueue? */
  325. if (retries > queue_retry_max)
  326. snac_log(snac, xs_fmt("process_queue giving up %s %d", actor, status));
  327. else {
  328. /* reenqueue */
  329. enqueue_output(snac, msg, actor, retries + 1);
  330. snac_log(snac, xs_fmt("process_queue requeue %s %d", actor, retries + 1));
  331. }
  332. }
  333. }
  334. else
  335. if (strcmp(type, "input") == 0) {
  336. /* process the message */
  337. char *msg = xs_dict_get(q_item, "object");
  338. char *req = xs_dict_get(q_item, "req");
  339. process_message(snac, msg, req);
  340. }
  341. }
  342. }
  343. d_char *recipient_list(snac *snac, char *msg, int expand_public)
  344. /* returns the list of recipients for a message */
  345. {
  346. d_char *list = xs_list_new();
  347. char *to = xs_dict_get(msg, "to");
  348. char *cc = xs_dict_get(msg, "cc");
  349. int n;
  350. char *lists[] = { to, cc, NULL };
  351. for (n = 0; lists[n]; n++) {
  352. char *l = lists[n];
  353. char *v;
  354. if (xs_type(l) == XSTYPE_STRING) {
  355. if (xs_list_in(list, l) == -1)
  356. list = xs_list_append(list, l);
  357. }
  358. else
  359. while (xs_list_iter(&l, &v)) {
  360. if (expand_public && strcmp(v, public_address) == 0) {
  361. /* iterate the followers and add them */
  362. xs *fwers = follower_list(snac);
  363. char *fw;
  364. char *p = fwers;
  365. while (xs_list_iter(&p, &fw)) {
  366. char *actor = xs_dict_get(fw, "actor");
  367. if (xs_list_in(list, actor) == -1)
  368. list = xs_list_append(list, actor);
  369. }
  370. }
  371. else
  372. if (xs_list_in(list, v) == -1)
  373. list = xs_list_append(list, v);
  374. }
  375. }
  376. return list;
  377. }
  378. int is_msg_public(snac *snac, char *msg)
  379. /* checks if a message is public */
  380. {
  381. int ret = 0;
  382. xs *rcpts = recipient_list(snac, msg, 0);
  383. char *p, *v;
  384. p = rcpts;
  385. while (!ret && xs_list_iter(&p, &v)) {
  386. if (strcmp(v, public_address) == 0)
  387. ret = 1;
  388. }
  389. return ret;
  390. }
  391. void post(snac *snac, char *msg)
  392. /* enqueues a message to all its recipients */
  393. {
  394. xs *rcpts = recipient_list(snac, msg, 1);
  395. char *p, *v;
  396. p = rcpts;
  397. while (xs_list_iter(&p, &v)) {
  398. enqueue_output(snac, msg, v, 0);
  399. }
  400. }
  401. /** HTTP handlers */
  402. int activitypub_get_handler(d_char *req, char *q_path,
  403. char **body, int *b_size, char **ctype)
  404. {
  405. int status = 200;
  406. char *accept = xs_dict_get(req, "accept");
  407. snac snac;
  408. xs *msg = NULL;
  409. if (accept == NULL)
  410. return 400;
  411. if (xs_str_in(accept, "application/activity+json") == -1 &&
  412. xs_str_in(accept, "application/ld+json") == -1)
  413. return 0;
  414. xs *l = xs_split_n(q_path, "/", 2);
  415. char *uid, *p_path;
  416. uid = xs_list_get(l, 1);
  417. if (!user_open(&snac, uid)) {
  418. /* invalid user */
  419. srv_log(xs_fmt("activitypub_get_handler bad user %s", uid));
  420. return 404;
  421. }
  422. p_path = xs_list_get(l, 2);
  423. *ctype = "application/activity+json";
  424. if (p_path == NULL) {
  425. /* if there was no component after the user, it's an actor request */
  426. msg = msg_actor(&snac);
  427. *ctype = "application/ld+json";
  428. }
  429. else
  430. if (strcmp(p_path, "outbox") == 0) {
  431. xs *id = xs_fmt("%s/outbox", snac.actor);
  432. msg = msg_collection(&snac, id);
  433. /* replace the 'orderedItems' with the latest posts */
  434. /* ... */
  435. }
  436. else
  437. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  438. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  439. msg = msg_collection(&snac, id);
  440. }
  441. else
  442. if (xs_startswith(p_path, "p/")) {
  443. }
  444. else
  445. status = 404;
  446. if (status == 200 && msg != NULL) {
  447. *body = xs_json_dumps_pp(msg, 4);
  448. *b_size = strlen(*body);
  449. }
  450. user_free(&snac);
  451. return status;
  452. }
  453. int activitypub_post_handler(d_char *req, char *q_path,
  454. d_char *payload, int p_size,
  455. char **body, int *b_size, char **ctype)
  456. /* processes an input message */
  457. {
  458. int status = 202; /* accepted */
  459. char *i_ctype = xs_dict_get(req, "content-type");
  460. snac snac;
  461. char *v;
  462. if (i_ctype == NULL)
  463. return 400;
  464. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  465. xs_str_in(i_ctype, "application/ld+json") == -1)
  466. return 0;
  467. /* decode the message */
  468. xs *msg = xs_json_loads(payload);
  469. if (msg == NULL) {
  470. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  471. status = 400;
  472. }
  473. /* get the user and path */
  474. xs *l = xs_split_n(q_path, "/", 2);
  475. char *uid;
  476. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  477. /* strange q_path */
  478. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  479. return 404;
  480. }
  481. uid = xs_list_get(l, 1);
  482. if (!user_open(&snac, uid)) {
  483. /* invalid user */
  484. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  485. return 404;
  486. }
  487. /* if it has a digest, check it now, because
  488. later the payload won't be exactly the same */
  489. if ((v = xs_dict_get(req, "digest")) != NULL) {
  490. xs *s1 = xs_sha256_base64(payload, p_size);
  491. xs *s2 = xs_fmt("SHA-256=%s", s1);
  492. if (strcmp(s2, v) == 0)
  493. srv_log(xs_fmt("digest check OK"));
  494. else
  495. srv_log(xs_fmt("digest check FAILED"));
  496. }
  497. enqueue_input(&snac, msg, req);
  498. user_free(&snac);
  499. if (valid_status(status))
  500. *ctype = "application/activity+json";
  501. return status;
  502. }