1
0

activitypub.c 17 KB

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