webfinger.c 5.8 KB

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