webfinger.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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, xs_str **actor, xs_str **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, xs_str **actor, xs_str **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_request_fake(const char *qs, xs_str **actor, xs_str **user)
  100. /* queries the webfinger and, if it fails, a user is faked if possible */
  101. {
  102. int status;
  103. if (!valid_status(status = webfinger_request(qs, actor, user))) {
  104. if (xs_startswith(qs, "https:/") || xs_startswith(qs, "http:/")) {
  105. xs *l = xs_split(qs, "/");
  106. if (xs_list_len(l) > 3) {
  107. srv_debug(1, xs_fmt("webfinger error querying %s %d -- faking it", qs, status));
  108. /* i'll end up in hell for this */
  109. *user = xs_fmt("%s@%s", xs_list_get(l, -1), xs_list_get(l, 2));
  110. status = HTTP_STATUS_RESET_CONTENT;
  111. }
  112. }
  113. }
  114. return status;
  115. }
  116. int webfinger_get_handler(xs_dict *req, const char *q_path,
  117. xs_val **body, int *b_size, char **ctype)
  118. /* serves webfinger queries */
  119. {
  120. int status;
  121. (void)b_size;
  122. if (strcmp(q_path, "/.well-known/webfinger") != 0)
  123. return 0;
  124. const char *q_vars = xs_dict_get(req, "q_vars");
  125. const char *resource = xs_dict_get(q_vars, "resource");
  126. if (resource == NULL)
  127. return HTTP_STATUS_BAD_REQUEST;
  128. snac snac;
  129. int found = 0;
  130. if (xs_startswith(resource, "https:/") || xs_startswith(resource, "http:/")) {
  131. /* actor search: find a user with this actor */
  132. xs *l = xs_split(resource, "/");
  133. const char *uid = xs_list_get(l, -1);
  134. if (uid)
  135. found = user_open(&snac, uid);
  136. }
  137. else
  138. if (xs_startswith(resource, "acct:")) {
  139. /* it's an account name */
  140. xs *an = xs_replace_n(resource, "acct:", "", 1);
  141. xs *l = NULL;
  142. /* strip a possible leading @ */
  143. if (xs_startswith(an, "@"))
  144. an = xs_crop_i(an, 1, 0);
  145. l = xs_split_n(an, "@", 1);
  146. if (xs_list_len(l) == 2) {
  147. const char *uid = xs_list_get(l, 0);
  148. const char *host = xs_list_get(l, 1);
  149. if (strcmp(host, xs_dict_get(srv_config, "host")) == 0)
  150. found = user_open(&snac, uid);
  151. }
  152. }
  153. if (found) {
  154. /* build the object */
  155. xs *acct;
  156. xs *aaj = xs_dict_new();
  157. xs *prof = xs_dict_new();
  158. xs *links = xs_list_new();
  159. xs *obj = xs_dict_new();
  160. acct = xs_fmt("acct:%s@%s",
  161. xs_dict_get(snac.config, "uid"), xs_dict_get(srv_config, "host"));
  162. aaj = xs_dict_append(aaj, "rel", "self");
  163. aaj = xs_dict_append(aaj, "type", "application/activity+json");
  164. aaj = xs_dict_append(aaj, "href", snac.actor);
  165. links = xs_list_append(links, aaj);
  166. /* duplicate with the ld+json type */
  167. aaj = xs_dict_set(aaj, "type", "application/ld+json; profile=\"https:/"
  168. "/www.w3.org/ns/activitystreams\"");
  169. links = xs_list_append(links, aaj);
  170. prof = xs_dict_append(prof, "rel", "http://webfinger.net/rel/profile-page");
  171. prof = xs_dict_append(prof, "type", "text/html");
  172. prof = xs_dict_append(prof, "href", snac.actor);
  173. links = xs_list_append(links, prof);
  174. const char *avatar = xs_dict_get(snac.config, "avatar");
  175. if (!xs_is_null(avatar) && *avatar) {
  176. xs *d = xs_dict_new();
  177. d = xs_dict_append(d, "rel", "http:/" "/webfinger.net/rel/avatar");
  178. d = xs_dict_append(d, "type", xs_mime_by_ext(avatar));
  179. d = xs_dict_append(d, "href", avatar);
  180. links = xs_list_append(links, d);
  181. }
  182. obj = xs_dict_append(obj, "subject", acct);
  183. obj = xs_dict_append(obj, "links", links);
  184. xs_str *j = xs_json_dumps(obj, 4);
  185. user_free(&snac);
  186. status = HTTP_STATUS_OK;
  187. *body = j;
  188. *ctype = "application/jrd+json";
  189. }
  190. else
  191. status = HTTP_STATUS_NOT_FOUND;
  192. srv_debug(1, xs_fmt("webfinger_get_handler resource=%s %d", resource, status));
  193. return status;
  194. }