webfinger.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
  3. #include "xs.h"
  4. #include "xs_json.h"
  5. #include "xs_curl.h"
  6. #include "xs_mime.h"
  7. #include "snac.h"
  8. int webfinger_request_signed(snac *snac, const char *qs, char **actor, char **user)
  9. /* queries the webfinger for qs and fills the required fields */
  10. {
  11. int status;
  12. xs *payload = NULL;
  13. int p_size = 0;
  14. xs *headers = xs_dict_new();
  15. xs *l = NULL;
  16. const char *host = NULL;
  17. xs *resource = NULL;
  18. if (xs_startswith(qs, "https:/") || xs_startswith(qs, "http:/")) {
  19. /* actor query: pick the host */
  20. xs *s1 = xs_replace_n(qs, "http:/" "/", "", 1);
  21. xs *s = xs_replace_n(s1, "https:/" "/", "", 1);
  22. l = xs_split_n(s, "/", 1);
  23. host = xs_list_get(l, 0);
  24. resource = xs_dup(qs);
  25. }
  26. else {
  27. /* it's a user */
  28. xs *s = xs_strip_chars_i(xs_dup(qs), "@.");
  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", s);
  33. }
  34. }
  35. if (host == NULL || resource == NULL)
  36. return HTTP_STATUS_BAD_REQUEST;
  37. headers = xs_dict_append(headers, "accept", "application/json");
  38. headers = xs_dict_append(headers, "user-agent", USER_AGENT);
  39. xs *obj = NULL;
  40. xs *cached_qs = xs_fmt("webfinger:%s", qs);
  41. /* is it cached? */
  42. if (valid_status(status = object_get(cached_qs, &obj))) {
  43. /* nothing more to do */
  44. }
  45. else
  46. /* is it a query about one of us? */
  47. if (strcmp(host, xs_dict_get(srv_config, "host")) == 0) {
  48. /* route internally */
  49. xs *req = xs_dict_new();
  50. xs *q_vars = xs_dict_new();
  51. char *ctype;
  52. q_vars = xs_dict_append(q_vars, "resource", resource);
  53. req = xs_dict_append(req, "q_vars", q_vars);
  54. status = webfinger_get_handler(req, "/.well-known/webfinger",
  55. &payload, &p_size, &ctype);
  56. }
  57. else {
  58. const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
  59. xs *url = xs_fmt("%s:/" "/%s/.well-known/webfinger?resource=%s", proto, host, resource);
  60. if (snac == NULL)
  61. xs_http_request("GET", url, headers, NULL, 0, &status, &payload, &p_size, 0);
  62. else
  63. http_signed_request(snac, "GET", url, headers, NULL, 0, &status, &payload, &p_size, 0);
  64. }
  65. if (obj == NULL && valid_status(status) && payload) {
  66. obj = xs_json_loads(payload);
  67. object_add(cached_qs, obj);
  68. }
  69. if (obj) {
  70. if (user != NULL) {
  71. const char *subject = xs_dict_get(obj, "subject");
  72. if (subject)
  73. *user = xs_replace_n(subject, "acct:", "", 1);
  74. }
  75. if (actor != NULL) {
  76. const xs_list *list = xs_dict_get(obj, "links");
  77. int c = 0;
  78. const char *v;
  79. while (xs_list_next(list, &v, &c)) {
  80. if (xs_type(v) == XSTYPE_DICT) {
  81. const char *type = xs_dict_get(v, "type");
  82. if (type && (strcmp(type, "application/activity+json") == 0 ||
  83. strcmp(type, "application/ld+json; profile=\"https:/"
  84. "/www.w3.org/ns/activitystreams\"") == 0)) {
  85. *actor = xs_dup(xs_dict_get(v, "href"));
  86. break;
  87. }
  88. }
  89. }
  90. }
  91. }
  92. return status;
  93. }
  94. int webfinger_request(const char *qs, char **actor, char **user)
  95. /* queries the webfinger for qs and fills the required fields */
  96. {
  97. return webfinger_request_signed(NULL, qs, actor, user);
  98. }
  99. int webfinger_get_handler(xs_dict *req, char *q_path,
  100. char **body, int *b_size, char **ctype)
  101. /* serves webfinger queries */
  102. {
  103. int status;
  104. (void)b_size;
  105. if (strcmp(q_path, "/.well-known/webfinger") != 0)
  106. return 0;
  107. const char *q_vars = xs_dict_get(req, "q_vars");
  108. const char *resource = xs_dict_get(q_vars, "resource");
  109. if (resource == NULL)
  110. return HTTP_STATUS_BAD_REQUEST;
  111. snac snac;
  112. int found = 0;
  113. if (xs_startswith(resource, "https:/") || xs_startswith(resource, "http:/")) {
  114. /* actor search: find a user with this actor */
  115. xs *l = xs_split(resource, "/");
  116. const char *uid = xs_list_get(l, -1);
  117. if (uid)
  118. found = user_open(&snac, uid);
  119. }
  120. else
  121. if (xs_startswith(resource, "acct:")) {
  122. /* it's an account name */
  123. xs *an = xs_replace_n(resource, "acct:", "", 1);
  124. xs *l = NULL;
  125. /* strip a possible leading @ */
  126. if (xs_startswith(an, "@"))
  127. an = xs_crop_i(an, 1, 0);
  128. l = xs_split_n(an, "@", 1);
  129. if (xs_list_len(l) == 2) {
  130. const char *uid = xs_list_get(l, 0);
  131. const char *host = xs_list_get(l, 1);
  132. if (strcmp(host, xs_dict_get(srv_config, "host")) == 0)
  133. found = user_open(&snac, uid);
  134. }
  135. }
  136. if (found) {
  137. /* build the object */
  138. xs *acct;
  139. xs *aaj = xs_dict_new();
  140. xs *prof = xs_dict_new();
  141. xs *links = xs_list_new();
  142. xs *obj = xs_dict_new();
  143. acct = xs_fmt("acct:%s@%s",
  144. xs_dict_get(snac.config, "uid"), xs_dict_get(srv_config, "host"));
  145. aaj = xs_dict_append(aaj, "rel", "self");
  146. aaj = xs_dict_append(aaj, "type", "application/activity+json");
  147. aaj = xs_dict_append(aaj, "href", snac.actor);
  148. links = xs_list_append(links, aaj);
  149. /* duplicate with the ld+json type */
  150. aaj = xs_dict_set(aaj, "type", "application/ld+json; profile=\"https:/"
  151. "/www.w3.org/ns/activitystreams\"");
  152. links = xs_list_append(links, aaj);
  153. prof = xs_dict_append(prof, "rel", "http://webfinger.net/rel/profile-page");
  154. prof = xs_dict_append(prof, "type", "text/html");
  155. prof = xs_dict_append(prof, "href", snac.actor);
  156. links = xs_list_append(links, prof);
  157. const char *avatar = xs_dict_get(snac.config, "avatar");
  158. if (!xs_is_null(avatar) && *avatar) {
  159. xs *d = xs_dict_new();
  160. d = xs_dict_append(d, "rel", "http:/" "/webfinger.net/rel/avatar");
  161. d = xs_dict_append(d, "type", xs_mime_by_ext(avatar));
  162. d = xs_dict_append(d, "href", avatar);
  163. links = xs_list_append(links, d);
  164. }
  165. obj = xs_dict_append(obj, "subject", acct);
  166. obj = xs_dict_append(obj, "links", links);
  167. xs_str *j = xs_json_dumps(obj, 4);
  168. user_free(&snac);
  169. status = HTTP_STATUS_OK;
  170. *body = j;
  171. *ctype = "application/jrd+json";
  172. }
  173. else
  174. status = HTTP_STATUS_NOT_FOUND;
  175. srv_debug(1, xs_fmt("webfinger_get_handler resource=%s %d", resource, status));
  176. return status;
  177. }