activitypub.c 16 KB

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