webfinger.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  3. #include "xs.h"
  4. #include "xs_json.h"
  5. #include "xs_curl.h"
  6. #include "snac.h"
  7. int webfinger_request(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. d_char *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. xs_http_request("GET", url, headers, NULL, 0, &status, &payload, &p_size, 0);
  51. }
  52. if (valid_status(status)) {
  53. xs *obj = xs_json_loads(payload);
  54. if (user != NULL) {
  55. char *subject = xs_dict_get(obj, "subject");
  56. if (subject)
  57. *user = xs_replace_n(subject, "acct:", "", 1);
  58. }
  59. if (actor != NULL) {
  60. char *list = xs_dict_get(obj, "links");
  61. char *v;
  62. while (xs_list_iter(&list, &v)) {
  63. if (xs_type(v) == XSTYPE_DICT) {
  64. char *type = xs_dict_get(v, "type");
  65. if (type && (strcmp(type, "application/activity+json") == 0 ||
  66. strcmp(type, "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"") == 0)) {
  67. *actor = xs_dup(xs_dict_get(v, "href"));
  68. break;
  69. }
  70. }
  71. }
  72. }
  73. }
  74. return status;
  75. }
  76. int webfinger_get_handler(d_char *req, char *q_path,
  77. char **body, int *b_size, char **ctype)
  78. /* serves webfinger queries */
  79. {
  80. int status;
  81. (void)b_size;
  82. if (strcmp(q_path, "/.well-known/webfinger") != 0)
  83. return 0;
  84. char *q_vars = xs_dict_get(req, "q_vars");
  85. char *resource = xs_dict_get(q_vars, "resource");
  86. if (resource == NULL)
  87. return 400;
  88. snac snac;
  89. int found = 0;
  90. if (xs_startswith(resource, "https:/" "/")) {
  91. /* actor search: find a user with this actor */
  92. xs *list = user_list();
  93. char *p, *uid;
  94. p = list;
  95. while (xs_list_iter(&p, &uid)) {
  96. if (user_open(&snac, uid)) {
  97. if (strcmp(snac.actor, resource) == 0) {
  98. found = 1;
  99. break;
  100. }
  101. user_free(&snac);
  102. }
  103. }
  104. }
  105. else
  106. if (xs_startswith(resource, "acct:")) {
  107. /* it's an account name */
  108. xs *an = xs_replace_n(resource, "acct:", "", 1);
  109. xs *l = NULL;
  110. /* strip a possible leading @ */
  111. if (xs_startswith(an, "@"))
  112. an = xs_crop_i(an, 1, 0);
  113. l = xs_split_n(an, "@", 1);
  114. if (xs_list_len(l) == 2) {
  115. char *uid = xs_list_get(l, 0);
  116. char *host = xs_list_get(l, 1);
  117. if (strcmp(host, xs_dict_get(srv_config, "host")) == 0)
  118. found = user_open(&snac, uid);
  119. }
  120. }
  121. if (found) {
  122. /* build the object */
  123. xs *acct;
  124. xs *aaj = xs_dict_new();
  125. xs *links = xs_list_new();
  126. xs *obj = xs_dict_new();
  127. d_char *j;
  128. acct = xs_fmt("acct:%s@%s",
  129. xs_dict_get(snac.config, "uid"), xs_dict_get(srv_config, "host"));
  130. aaj = xs_dict_append(aaj, "rel", "self");
  131. aaj = xs_dict_append(aaj, "type", "application/activity+json");
  132. aaj = xs_dict_append(aaj, "href", snac.actor);
  133. links = xs_list_append(links, aaj);
  134. obj = xs_dict_append(obj, "subject", acct);
  135. obj = xs_dict_append(obj, "links", links);
  136. j = xs_json_dumps_pp(obj, 4);
  137. user_free(&snac);
  138. status = 200;
  139. *body = j;
  140. *ctype = "application/json";
  141. }
  142. else
  143. status = 404;
  144. return status;
  145. }