1
0

webfinger.c 7.7 KB

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