webfinger.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_dup(qs);
  28. if (xs_startswith(s, "@"))
  29. s = xs_crop_i(s, 1, 0);
  30. l = xs_split_n(s, "@", 1);
  31. if (xs_list_len(l) == 2) {
  32. host = xs_list_get(l, 1);
  33. resource = xs_fmt("acct:%s", s);
  34. }
  35. }
  36. if (host == NULL || resource == NULL)
  37. return 400;
  38. headers = xs_dict_append(headers, "accept", "application/json");
  39. headers = xs_dict_append(headers, "user-agent", USER_AGENT);
  40. /* is it a query about one of us? */
  41. if (strcmp(host, xs_dict_get(srv_config, "host")) == 0) {
  42. /* route internally */
  43. xs *req = xs_dict_new();
  44. xs *q_vars = xs_dict_new();
  45. char *ctype;
  46. q_vars = xs_dict_append(q_vars, "resource", resource);
  47. req = xs_dict_append(req, "q_vars", q_vars);
  48. status = webfinger_get_handler(req, "/.well-known/webfinger",
  49. &payload, &p_size, &ctype);
  50. }
  51. else {
  52. xs *url = xs_fmt("https:/" "/%s/.well-known/webfinger?resource=%s", host, resource);
  53. xs_http_request("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_get_handler(d_char *req, char *q_path,
  80. char **body, int *b_size, char **ctype)
  81. /* serves webfinger queries */
  82. {
  83. int status;
  84. (void)b_size;
  85. if (strcmp(q_path, "/.well-known/webfinger") != 0)
  86. return 0;
  87. char *q_vars = xs_dict_get(req, "q_vars");
  88. char *resource = xs_dict_get(q_vars, "resource");
  89. if (resource == NULL)
  90. return 400;
  91. snac snac;
  92. int found = 0;
  93. if (xs_startswith(resource, "https:/" "/")) {
  94. /* actor search: find a user with this actor */
  95. xs *list = user_list();
  96. char *p, *uid;
  97. p = list;
  98. while (xs_list_iter(&p, &uid)) {
  99. if (user_open(&snac, uid)) {
  100. if (strcmp(snac.actor, resource) == 0) {
  101. found = 1;
  102. break;
  103. }
  104. user_free(&snac);
  105. }
  106. }
  107. }
  108. else
  109. if (xs_startswith(resource, "acct:")) {
  110. /* it's an account name */
  111. xs *an = xs_replace_n(resource, "acct:", "", 1);
  112. xs *l = NULL;
  113. /* strip a possible leading @ */
  114. if (xs_startswith(an, "@"))
  115. an = xs_crop_i(an, 1, 0);
  116. l = xs_split_n(an, "@", 1);
  117. if (xs_list_len(l) == 2) {
  118. char *uid = xs_list_get(l, 0);
  119. char *host = xs_list_get(l, 1);
  120. if (strcmp(host, xs_dict_get(srv_config, "host")) == 0)
  121. found = user_open(&snac, uid);
  122. }
  123. }
  124. if (found) {
  125. /* build the object */
  126. xs *acct;
  127. xs *aaj = xs_dict_new();
  128. xs *links = xs_list_new();
  129. xs *obj = xs_dict_new();
  130. d_char *j;
  131. acct = xs_fmt("acct:%s@%s",
  132. xs_dict_get(snac.config, "uid"), xs_dict_get(srv_config, "host"));
  133. aaj = xs_dict_append(aaj, "rel", "self");
  134. aaj = xs_dict_append(aaj, "type", "application/activity+json");
  135. aaj = xs_dict_append(aaj, "href", snac.actor);
  136. links = xs_list_append(links, aaj);
  137. obj = xs_dict_append(obj, "subject", acct);
  138. obj = xs_dict_append(obj, "links", links);
  139. j = xs_json_dumps_pp(obj, 4);
  140. user_free(&snac);
  141. status = 200;
  142. *body = j;
  143. *ctype = "application/json";
  144. }
  145. else
  146. status = 404;
  147. return status;
  148. }