webfinger.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 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. void webfinger_request(char *qs, int *status, char **actor, char **user)
  9. /* queries the webfinger for qs and fills the required fields */
  10. {
  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(qs, "https:/" "/", "");
  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_dup(qs);
  27. if (xs_startswith(s, "@"))
  28. s = xs_crop(s, 1, 0);
  29. l = xs_split_n(s, "@", 1);
  30. if (xs_list_len(l) == 2) {
  31. host = xs_list_get(l, 1);
  32. resource = xs_fmt("acct:%s", qs);
  33. }
  34. }
  35. if (host == NULL || resource == NULL) {
  36. *status = 400;
  37. return;
  38. }
  39. headers = xs_dict_append(headers, "accept", "application/json");
  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. webfinger_get_handler(req, "/.well-known/webfinger",
  49. status, &payload, &p_size, &ctype);
  50. }
  51. else {
  52. xs *url = xs_fmt("https:/" "/%s/.well-known/webfinger?resource=%s", host, resource);
  53. printf("url: %s\n", url);
  54. xs_http_request("GET", url, headers, NULL, 0, status, &payload, &p_size);
  55. }
  56. if (*status >= 200 && *status <= 299) {
  57. xs *obj = xs_json_loads(payload);
  58. if (user != NULL) {
  59. char *subject = xs_dict_get(obj, "subject");
  60. if (subject)
  61. *user = xs_replace(subject, "acct:", "");
  62. }
  63. if (actor != NULL) {
  64. char *list = xs_dict_get(obj, "links");
  65. char *v;
  66. while (xs_list_iter(&list, &v)) {
  67. if (xs_type(v) == XSTYPE_SOD) {
  68. char *type = xs_dict_get(v, "type");
  69. if (type && strcmp(type, "application/activity+json") == 0) {
  70. *actor = xs_dup(xs_dict_get(v, "href"));
  71. break;
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. void webfinger_get_handler(d_char *req, char *q_path, int *status,
  79. char **body, int *b_size, char **ctype)
  80. /* serves webfinger queries */
  81. {
  82. if (strcmp(q_path, "/.well-known/webfinger") != 0)
  83. return;
  84. char *q_vars = xs_dict_get(req, "q_vars");
  85. char *resource = xs_dict_get(q_vars, "resource");
  86. if (resource == NULL) {
  87. *status = 400;
  88. return;
  89. }
  90. snac snac;
  91. int found = 0;
  92. if (xs_startswith(resource, "https:/" "/")) {
  93. /* actor search: find a user with this actor */
  94. xs *list = user_list();
  95. char *p, *uid;
  96. p = list;
  97. while (xs_list_iter(&p, &uid)) {
  98. if (user_open(&snac, uid)) {
  99. if (strcmp(snac.actor, resource) == 0) {
  100. found = 1;
  101. break;
  102. }
  103. user_free(&snac);
  104. }
  105. }
  106. }
  107. else
  108. if (xs_startswith(resource, "acct:")) {
  109. /* it's an account name */
  110. xs *an = xs_replace(resource, "acct:", "");
  111. xs *l = NULL;
  112. /* strip a possible leading @ */
  113. if (xs_startswith(an, "@"))
  114. an = xs_crop(an, 1, 0);
  115. l = xs_split_n(an, "@", 1);
  116. if (xs_list_len(l) == 2) {
  117. char *uid = xs_list_get(l, 0);
  118. char *host = xs_list_get(l, 1);
  119. if (strcmp(host, xs_dict_get(srv_config, "host")) == 0)
  120. found = user_open(&snac, uid);
  121. }
  122. }
  123. if (found) {
  124. /* build the object */
  125. xs *acct;
  126. xs *aaj = xs_dict_new();
  127. xs *links = xs_list_new();
  128. xs *obj = xs_dict_new();
  129. d_char *j;
  130. acct = xs_fmt("acct:%s@%s",
  131. xs_dict_get(snac.config, "uid"), xs_dict_get(srv_config, "host"));
  132. aaj = xs_dict_append(aaj, "rel", "self");
  133. aaj = xs_dict_append(aaj, "type", "application/activity+json");
  134. aaj = xs_dict_append(aaj, "href", snac.actor);
  135. links = xs_list_append(links, aaj);
  136. obj = xs_dict_append(obj, "subject", acct);
  137. obj = xs_dict_append(obj, "links", links);
  138. j = xs_json_dumps_pp(obj, 4);
  139. user_free(&snac);
  140. *status = 200;
  141. *body = j;
  142. *ctype = "application/json";
  143. }
  144. else
  145. *status = 404;
  146. }