webfinger.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. xs_str *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 400;
  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. char *subject = xs_dict_get(obj, "subject");
  72. if (subject)
  73. *user = xs_replace_n(subject, "acct:", "", 1);
  74. }
  75. if (actor != NULL) {
  76. char *list = xs_dict_get(obj, "links");
  77. char *v;
  78. while (xs_list_iter(&list, &v)) {
  79. if (xs_type(v) == XSTYPE_DICT) {
  80. char *type = xs_dict_get(v, "type");
  81. if (type && (strcmp(type, "application/activity+json") == 0 ||
  82. strcmp(type, "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"") == 0)) {
  83. *actor = xs_dup(xs_dict_get(v, "href"));
  84. break;
  85. }
  86. }
  87. }
  88. }
  89. }
  90. return status;
  91. }
  92. int webfinger_request(const char *qs, char **actor, char **user)
  93. /* queries the webfinger for qs and fills the required fields */
  94. {
  95. return webfinger_request_signed(NULL, qs, actor, user);
  96. }
  97. int webfinger_get_handler(xs_dict *req, char *q_path,
  98. char **body, int *b_size, char **ctype)
  99. /* serves webfinger queries */
  100. {
  101. int status;
  102. (void)b_size;
  103. if (strcmp(q_path, "/.well-known/webfinger") != 0)
  104. return 0;
  105. char *q_vars = xs_dict_get(req, "q_vars");
  106. char *resource = xs_dict_get(q_vars, "resource");
  107. if (resource == NULL)
  108. return 400;
  109. snac snac;
  110. int found = 0;
  111. if (xs_startswith(resource, "https:/") || xs_startswith(resource, "http:/")) {
  112. /* actor search: find a user with this actor */
  113. xs *l = xs_split(resource, "/");
  114. char *uid = xs_list_get(l, -1);
  115. if (uid)
  116. found = user_open(&snac, uid);
  117. }
  118. else
  119. if (xs_startswith(resource, "acct:")) {
  120. /* it's an account name */
  121. xs *an = xs_replace_n(resource, "acct:", "", 1);
  122. xs *l = NULL;
  123. /* strip a possible leading @ */
  124. if (xs_startswith(an, "@"))
  125. an = xs_crop_i(an, 1, 0);
  126. l = xs_split_n(an, "@", 1);
  127. if (xs_list_len(l) == 2) {
  128. char *uid = xs_list_get(l, 0);
  129. char *host = xs_list_get(l, 1);
  130. if (strcmp(host, xs_dict_get(srv_config, "host")) == 0)
  131. found = user_open(&snac, uid);
  132. }
  133. }
  134. if (found) {
  135. /* build the object */
  136. xs *acct;
  137. xs *aaj = xs_dict_new();
  138. xs *prof = xs_dict_new();
  139. xs *links = xs_list_new();
  140. xs *obj = xs_dict_new();
  141. acct = xs_fmt("acct:%s@%s",
  142. xs_dict_get(snac.config, "uid"), xs_dict_get(srv_config, "host"));
  143. aaj = xs_dict_append(aaj, "rel", "self");
  144. aaj = xs_dict_append(aaj, "type", "application/activity+json");
  145. aaj = xs_dict_append(aaj, "href", snac.actor);
  146. links = xs_list_append(links, aaj);
  147. prof = xs_dict_append(prof, "rel", "http://webfinger.net/rel/profile-page");
  148. prof = xs_dict_append(prof, "type", "text/html");
  149. prof = xs_dict_append(prof, "href", snac.actor);
  150. links = xs_list_append(links, prof);
  151. char *avatar = xs_dict_get(snac.config, "avatar");
  152. if (!xs_is_null(avatar) && *avatar) {
  153. xs *d = xs_dict_new();
  154. d = xs_dict_append(d, "rel", "http:/" "/webfinger.net/rel/avatar");
  155. d = xs_dict_append(d, "type", xs_mime_by_ext(avatar));
  156. d = xs_dict_append(d, "href", avatar);
  157. links = xs_list_append(links, d);
  158. }
  159. obj = xs_dict_append(obj, "subject", acct);
  160. obj = xs_dict_append(obj, "links", links);
  161. xs_str *j = xs_json_dumps(obj, 4);
  162. user_free(&snac);
  163. status = 200;
  164. *body = j;
  165. *ctype = "application/json";
  166. }
  167. else
  168. status = 404;
  169. srv_debug(1, xs_fmt("webfinger_get_handler resource=%s %d", resource, status));
  170. return status;
  171. }