webfinger.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink et al. / MIT license */
  3. #include "xs.h"
  4. #include "xs_json.h"
  5. #include "xs_curl.h"
  6. #include "snac.h"
  7. int webfinger_request_signed(snac *snac, const char *qs, char **actor, char **user)
  8. /* queries the webfinger for qs and fills the required fields */
  9. {
  10. int status;
  11. xs *payload = NULL;
  12. int p_size = 0;
  13. xs *headers = xs_dict_new();
  14. xs *l = NULL;
  15. xs_str *host = NULL;
  16. xs *resource = NULL;
  17. if (xs_startswith(qs, "https:/" "/")) {
  18. /* actor query: pick the host */
  19. xs *s = xs_replace_n(qs, "https:/" "/", "", 1);
  20. l = xs_split_n(s, "/", 1);
  21. host = xs_list_get(l, 0);
  22. resource = xs_dup(qs);
  23. }
  24. else {
  25. /* it's a user */
  26. xs *s = xs_strip_chars_i(xs_dup(qs), "@.");
  27. l = xs_split_n(s, "@", 1);
  28. if (xs_list_len(l) == 2) {
  29. host = xs_list_get(l, 1);
  30. resource = xs_fmt("acct:%s", s);
  31. }
  32. }
  33. if (host == NULL || resource == NULL)
  34. return 400;
  35. headers = xs_dict_append(headers, "accept", "application/json");
  36. headers = xs_dict_append(headers, "user-agent", USER_AGENT);
  37. /* is it a query about one of us? */
  38. if (strcmp(host, xs_dict_get(srv_config, "host")) == 0) {
  39. /* route internally */
  40. xs *req = xs_dict_new();
  41. xs *q_vars = xs_dict_new();
  42. char *ctype;
  43. q_vars = xs_dict_append(q_vars, "resource", resource);
  44. req = xs_dict_append(req, "q_vars", q_vars);
  45. status = webfinger_get_handler(req, "/.well-known/webfinger",
  46. &payload, &p_size, &ctype);
  47. }
  48. else {
  49. xs *url = xs_fmt("https:/" "/%s/.well-known/webfinger?resource=%s", host, resource);
  50. if (snac == NULL)
  51. xs_http_request("GET", url, headers, NULL, 0, &status, &payload, &p_size, 0);
  52. else
  53. http_signed_request(snac, "GET", url, headers, NULL, 0, &status, &payload, &p_size, 0);
  54. }
  55. if (valid_status(status)) {
  56. xs *obj = xs_json_loads(payload);
  57. if (user != NULL) {
  58. char *subject = xs_dict_get(obj, "subject");
  59. if (subject)
  60. *user = xs_replace_n(subject, "acct:", "", 1);
  61. }
  62. if (actor != NULL) {
  63. char *list = xs_dict_get(obj, "links");
  64. char *v;
  65. while (xs_list_iter(&list, &v)) {
  66. if (xs_type(v) == XSTYPE_DICT) {
  67. char *type = xs_dict_get(v, "type");
  68. if (type && (strcmp(type, "application/activity+json") == 0 ||
  69. strcmp(type, "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"") == 0)) {
  70. *actor = xs_dup(xs_dict_get(v, "href"));
  71. break;
  72. }
  73. }
  74. }
  75. }
  76. }
  77. return status;
  78. }
  79. int webfinger_request(const char *qs, char **actor, char **user)
  80. /* queries the webfinger for qs and fills the required fields */
  81. {
  82. return webfinger_request_signed(NULL, qs, actor, user);
  83. }
  84. int webfinger_get_handler(xs_dict *req, char *q_path,
  85. char **body, int *b_size, char **ctype)
  86. /* serves webfinger queries */
  87. {
  88. int status;
  89. (void)b_size;
  90. if (strcmp(q_path, "/.well-known/webfinger") != 0)
  91. return 0;
  92. char *q_vars = xs_dict_get(req, "q_vars");
  93. char *resource = xs_dict_get(q_vars, "resource");
  94. if (resource == NULL)
  95. return 400;
  96. snac snac;
  97. int found = 0;
  98. if (xs_startswith(resource, "https:/" "/")) {
  99. /* actor search: find a user with this actor */
  100. xs *list = user_list();
  101. char *p, *uid;
  102. p = list;
  103. while (xs_list_iter(&p, &uid)) {
  104. if (user_open(&snac, uid)) {
  105. if (strcmp(snac.actor, resource) == 0) {
  106. found = 1;
  107. break;
  108. }
  109. user_free(&snac);
  110. }
  111. }
  112. }
  113. else
  114. if (xs_startswith(resource, "acct:")) {
  115. /* it's an account name */
  116. xs *an = xs_replace_n(resource, "acct:", "", 1);
  117. xs *l = NULL;
  118. /* strip a possible leading @ */
  119. if (xs_startswith(an, "@"))
  120. an = xs_crop_i(an, 1, 0);
  121. l = xs_split_n(an, "@", 1);
  122. if (xs_list_len(l) == 2) {
  123. char *uid = xs_list_get(l, 0);
  124. char *host = xs_list_get(l, 1);
  125. if (strcmp(host, xs_dict_get(srv_config, "host")) == 0)
  126. found = user_open(&snac, uid);
  127. }
  128. }
  129. if (found) {
  130. /* build the object */
  131. xs *acct;
  132. xs *aaj = xs_dict_new();
  133. xs *links = xs_list_new();
  134. xs *obj = xs_dict_new();
  135. acct = xs_fmt("acct:%s@%s",
  136. xs_dict_get(snac.config, "uid"), xs_dict_get(srv_config, "host"));
  137. aaj = xs_dict_append(aaj, "rel", "self");
  138. aaj = xs_dict_append(aaj, "type", "application/activity+json");
  139. aaj = xs_dict_append(aaj, "href", snac.actor);
  140. links = xs_list_append(links, aaj);
  141. obj = xs_dict_append(obj, "subject", acct);
  142. obj = xs_dict_append(obj, "links", links);
  143. xs_str *j = xs_json_dumps(obj, 4);
  144. user_free(&snac);
  145. status = 200;
  146. *body = j;
  147. *ctype = "application/json";
  148. }
  149. else
  150. status = 404;
  151. return status;
  152. }