webfinger.c 7.6 KB

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