webfinger.c 7.4 KB

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