webfinger.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "snac.h"
  8. void webfinger_request(char *qs, int *status, char **actor, char **user)
  9. /* queries the webfinger for qs and fills the required fields */
  10. {
  11. xs *payload = NULL;
  12. int p_size = 0;
  13. xs *headers = xs_dict_new();
  14. xs *l = NULL;
  15. d_char *host = NULL;
  16. xs *resource = NULL;
  17. if (xs_startswith(qs, "https:/" "/")) {
  18. /* actor query: pick the host */
  19. xs *s = xs_replace(qs, "https:/" "/", "");
  20. l = xs_split_n(s, "/", 1);
  21. host = xs_list_get(l, 0);
  22. resource = xs_dup(qs);
  23. }
  24. else {
  25. /* it's a user */
  26. xs *s = xs_dup(qs);
  27. if (xs_startswith(s, "@"))
  28. s = xs_crop(s, 1, 0);
  29. l = xs_split_n(s, "@", 1);
  30. if (xs_list_len(l) == 2) {
  31. host = xs_list_get(l, 1);
  32. resource = xs_fmt("acct:%s", qs);
  33. }
  34. }
  35. if (host == NULL || resource == NULL) {
  36. *status = 400;
  37. return;
  38. }
  39. headers = xs_dict_append(headers, "accept", "application/json");
  40. /* is it a query about one of us? */
  41. if (strcmp(host, xs_dict_get(srv_config, "host")) == 0) {
  42. /* route internally */
  43. xs *req = xs_dict_new();
  44. xs *q_vars = xs_dict_new();
  45. char *ctype;
  46. q_vars = xs_dict_append(q_vars, "resource", resource);
  47. req = xs_dict_append(req, "q_vars", q_vars);
  48. webfinger_get_handler(req, "/.well-known/webfinger",
  49. status, &payload, &p_size, &ctype);
  50. }
  51. else {
  52. xs *url = xs_fmt("https:/" "/%s/.well-known/webfinger?resource=%s", host, resource);
  53. printf("url: %s\n", url);
  54. xs_http_request("GET", url, headers, NULL, 0, status, &payload, &p_size);
  55. }
  56. if (*status >= 200 && *status <= 299) {
  57. xs *obj = xs_json_loads(payload);
  58. if (user != NULL) {
  59. *user = xs_replace(xs_dict_get(obj, "subject"), "acct:", "");
  60. }
  61. if (actor != NULL) {
  62. char *list = xs_dict_get(obj, "links");
  63. char *v;
  64. while (xs_list_iter(&list, &v)) {
  65. if (xs_type(v) == XSTYPE_SOD) {
  66. char *type = xs_dict_get(v, "type");
  67. if (type && strcmp(type, "application/activity+json") == 0) {
  68. *actor = xs_dup(xs_dict_get(v, "href"));
  69. break;
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }
  76. void webfinger_get_handler(d_char *req, char *q_path, int *status,
  77. char **body, int *b_size, char **ctype)
  78. /* serves webfinger queries */
  79. {
  80. if (strcmp(q_path, "/.well-known/webfinger") != 0)
  81. return;
  82. char *q_vars = xs_dict_get(req, "q_vars");
  83. char *resource = xs_dict_get(q_vars, "resource");
  84. if (resource == NULL) {
  85. *status = 400;
  86. return;
  87. }
  88. snac snac;
  89. int found = 0;
  90. if (xs_startswith(resource, "https:/" "/")) {
  91. /* actor search: find a user with this actor */
  92. xs *list = user_list();
  93. char *p, *uid;
  94. p = list;
  95. while (xs_list_iter(&p, &uid)) {
  96. if (user_open(&snac, uid)) {
  97. if (strcmp(snac.actor, resource) == 0) {
  98. found = 1;
  99. break;
  100. }
  101. user_free(&snac);
  102. }
  103. }
  104. }
  105. else
  106. if (xs_startswith(resource, "acct:")) {
  107. /* it's an account name */
  108. xs *an = xs_replace(resource, "acct:", "");
  109. xs *l = NULL;
  110. /* strip a possible leading @ */
  111. if (xs_startswith(an, "@"))
  112. an = xs_crop(an, 1, 0);
  113. l = xs_split_n(an, "@", 1);
  114. if (xs_list_len(l) == 2) {
  115. char *uid = xs_list_get(l, 0);
  116. char *host = xs_list_get(l, 1);
  117. if (strcmp(host, xs_dict_get(srv_config, "host")) == 0)
  118. found = user_open(&snac, uid);
  119. }
  120. }
  121. if (found) {
  122. /* build the object */
  123. xs *acct;
  124. xs *aaj = xs_dict_new();
  125. xs *links = xs_list_new();
  126. xs *obj = xs_dict_new();
  127. d_char *j;
  128. acct = xs_fmt("acct:%s@%s",
  129. xs_dict_get(snac.config, "uid"), xs_dict_get(srv_config, "host"));
  130. aaj = xs_dict_append(aaj, "rel", "self");
  131. aaj = xs_dict_append(aaj, "type", "application/activity+json");
  132. aaj = xs_dict_append(aaj, "href", snac.actor);
  133. links = xs_list_append(links, aaj);
  134. obj = xs_dict_append(obj, "subject", acct);
  135. obj = xs_dict_append(obj, "links", links);
  136. j = xs_json_dumps_pp(obj, 4);
  137. user_free(&snac);
  138. *status = 200;
  139. *body = j;
  140. *ctype = "application/json";
  141. }
  142. else
  143. *status = 404;
  144. }