webfinger.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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:/" "/")) {
  19. /* actor query: pick the host */
  20. xs *s = xs_replace_n(qs, "https:/" "/", "", 1);
  21. l = xs_split_n(s, "/", 1);
  22. host = xs_list_get(l, 0);
  23. resource = xs_dup(qs);
  24. }
  25. else {
  26. /* it's a user */
  27. xs *s = xs_strip_chars_i(xs_dup(qs), "@.");
  28. l = xs_split_n(s, "@", 1);
  29. if (xs_list_len(l) == 2) {
  30. host = xs_list_get(l, 1);
  31. resource = xs_fmt("acct:%s", s);
  32. }
  33. }
  34. if (host == NULL || resource == NULL)
  35. return 400;
  36. headers = xs_dict_append(headers, "accept", "application/json");
  37. headers = xs_dict_append(headers, "user-agent", USER_AGENT);
  38. xs *obj = NULL;
  39. xs *cached_qs = xs_fmt("webfinger:%s", qs);
  40. /* is it cached? */
  41. if (valid_status(status = object_get(cached_qs, &obj))) {
  42. /* nothing more to do */
  43. }
  44. else
  45. /* is it a query about one of us? */
  46. if (strcmp(host, xs_dict_get(srv_config, "host")) == 0) {
  47. /* route internally */
  48. xs *req = xs_dict_new();
  49. xs *q_vars = xs_dict_new();
  50. char *ctype;
  51. q_vars = xs_dict_append(q_vars, "resource", resource);
  52. req = xs_dict_append(req, "q_vars", q_vars);
  53. status = webfinger_get_handler(req, "/.well-known/webfinger",
  54. &payload, &p_size, &ctype);
  55. }
  56. else {
  57. xs *url = xs_fmt("https:/" "/%s/.well-known/webfinger?resource=%s", host, resource);
  58. if (snac == NULL)
  59. xs_http_request("GET", url, headers, NULL, 0, &status, &payload, &p_size, 0);
  60. else
  61. http_signed_request(snac, "GET", url, headers, NULL, 0, &status, &payload, &p_size, 0);
  62. }
  63. if (obj == NULL && valid_status(status) && payload) {
  64. obj = xs_json_loads(payload);
  65. object_add(cached_qs, obj);
  66. }
  67. if (obj) {
  68. if (user != NULL) {
  69. char *subject = xs_dict_get(obj, "subject");
  70. if (subject)
  71. *user = xs_replace_n(subject, "acct:", "", 1);
  72. }
  73. if (actor != NULL) {
  74. char *list = xs_dict_get(obj, "links");
  75. char *v;
  76. while (xs_list_iter(&list, &v)) {
  77. if (xs_type(v) == XSTYPE_DICT) {
  78. char *type = xs_dict_get(v, "type");
  79. if (type && (strcmp(type, "application/activity+json") == 0 ||
  80. strcmp(type, "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"") == 0)) {
  81. *actor = xs_dup(xs_dict_get(v, "href"));
  82. break;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. return status;
  89. }
  90. int webfinger_request(const char *qs, char **actor, char **user)
  91. /* queries the webfinger for qs and fills the required fields */
  92. {
  93. return webfinger_request_signed(NULL, qs, actor, user);
  94. }
  95. int webfinger_get_handler(xs_dict *req, char *q_path,
  96. char **body, int *b_size, char **ctype)
  97. /* serves webfinger queries */
  98. {
  99. int status;
  100. (void)b_size;
  101. if (strcmp(q_path, "/.well-known/webfinger") != 0)
  102. return 0;
  103. char *q_vars = xs_dict_get(req, "q_vars");
  104. char *resource = xs_dict_get(q_vars, "resource");
  105. if (resource == NULL)
  106. return 400;
  107. snac snac;
  108. int found = 0;
  109. if (xs_startswith(resource, "https:/" "/")) {
  110. /* actor search: find a user with this actor */
  111. xs *l = xs_split(resource, "/");
  112. char *uid = xs_list_get(l, -1);
  113. if (uid)
  114. found = user_open(&snac, uid);
  115. }
  116. else
  117. if (xs_startswith(resource, "acct:")) {
  118. /* it's an account name */
  119. xs *an = xs_replace_n(resource, "acct:", "", 1);
  120. xs *l = NULL;
  121. /* strip a possible leading @ */
  122. if (xs_startswith(an, "@"))
  123. an = xs_crop_i(an, 1, 0);
  124. l = xs_split_n(an, "@", 1);
  125. if (xs_list_len(l) == 2) {
  126. char *uid = xs_list_get(l, 0);
  127. char *host = xs_list_get(l, 1);
  128. if (strcmp(host, xs_dict_get(srv_config, "host")) == 0)
  129. found = user_open(&snac, uid);
  130. }
  131. }
  132. if (found) {
  133. /* build the object */
  134. xs *acct;
  135. xs *aaj = xs_dict_new();
  136. xs *prof = xs_dict_new();
  137. xs *links = xs_list_new();
  138. xs *obj = xs_dict_new();
  139. acct = xs_fmt("acct:%s@%s",
  140. xs_dict_get(snac.config, "uid"), xs_dict_get(srv_config, "host"));
  141. aaj = xs_dict_append(aaj, "rel", "self");
  142. aaj = xs_dict_append(aaj, "type", "application/activity+json");
  143. aaj = xs_dict_append(aaj, "href", snac.actor);
  144. links = xs_list_append(links, aaj);
  145. prof = xs_dict_append(prof, "rel", "http://webfinger.net/rel/profile-page");
  146. prof = xs_dict_append(prof, "type", "text/html");
  147. prof = xs_dict_append(prof, "href", snac.actor);
  148. links = xs_list_append(links, prof);
  149. char *avatar = xs_dict_get(snac.config, "avatar");
  150. if (!xs_is_null(avatar) && *avatar) {
  151. xs *d = xs_dict_new();
  152. d = xs_dict_append(d, "rel", "http:/" "/webfinger.net/rel/avatar");
  153. d = xs_dict_append(d, "type", xs_mime_by_ext(avatar));
  154. d = xs_dict_append(d, "href", avatar);
  155. links = xs_list_append(links, d);
  156. }
  157. obj = xs_dict_append(obj, "subject", acct);
  158. obj = xs_dict_append(obj, "links", links);
  159. xs_str *j = xs_json_dumps(obj, 4);
  160. user_free(&snac);
  161. status = 200;
  162. *body = j;
  163. *ctype = "application/json";
  164. }
  165. else
  166. status = 404;
  167. return status;
  168. }