webfinger.c 5.0 KB

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