1
0

webfinger.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2026 grunfink et al. / MIT license */
  3. #include "xs.h"
  4. #include "xs_json.h"
  5. #include "xs_curl.h"
  6. #include "xs_mime.h"
  7. #include "xs_http.h"
  8. #include "snac.h"
  9. int webfinger_request_signed(snac *snac, const char *qs, xs_str **actor, xs_str **user)
  10. /* queries the webfinger for qs and fills the required fields */
  11. {
  12. int status;
  13. xs *payload = NULL;
  14. int p_size = 0;
  15. xs *headers = xs_dict_new();
  16. xs *l = NULL;
  17. const char *host = NULL;
  18. xs *resource = NULL;
  19. if (xs_startswith(qs, "https:/") || xs_startswith(qs, "http:/")) {
  20. /* actor query: pick the host */
  21. xs *s1 = xs_replace_n(qs, "http:/" "/", "", 1);
  22. xs *s = xs_replace_n(s1, "https:/" "/", "", 1);
  23. l = xs_split_n(s, "/", 1);
  24. host = xs_list_get(l, 0);
  25. resource = xs_dup(qs);
  26. }
  27. else {
  28. /* it's a user */
  29. xs *s = xs_strip_chars_i(xs_dup(qs), "@.");
  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 HTTP_STATUS_BAD_REQUEST;
  38. headers = xs_dict_append(headers, "accept", "application/json");
  39. headers = xs_dict_append(headers, "user-agent", USER_AGENT);
  40. xs *obj = NULL;
  41. xs *cached_qs = xs_fmt("webfinger:%s", qs);
  42. /* is it cached? */
  43. if (valid_status(status = object_get(cached_qs, &obj))) {
  44. /* nothing more to do */
  45. }
  46. else
  47. /* is it a query about one of us? */
  48. if (strcmp(host, xs_dict_get(srv_config, "host")) == 0) {
  49. /* route internally */
  50. xs *req = xs_dict_new();
  51. xs *q_vars = xs_dict_new();
  52. char *ctype;
  53. q_vars = xs_dict_append(q_vars, "resource", resource);
  54. req = xs_dict_append(req, "q_vars", q_vars);
  55. status = webfinger_get_handler(req, "/.well-known/webfinger",
  56. &payload, &p_size, &ctype);
  57. }
  58. else {
  59. const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
  60. xs *url = xs_fmt("%s:/" "/%s/.well-known/webfinger?resource=%s", proto, host, resource);
  61. if (snac == NULL)
  62. xs_free(xs_http_request("GET", url, headers, NULL, 0, &status, &payload, &p_size, 0));
  63. else
  64. xs_free(http_signed_request(snac, "GET", url, headers, NULL, 0, &status, &payload, &p_size, 0));
  65. }
  66. if (obj == NULL && valid_status(status) && payload) {
  67. obj = xs_json_loads(payload);
  68. if (obj)
  69. object_add(cached_qs, obj);
  70. else
  71. status = HTTP_STATUS_BAD_REQUEST;
  72. }
  73. if (obj) {
  74. if (user != NULL) {
  75. const char *subject = xs_dict_get(obj, "subject");
  76. if (subject && xs_startswith(subject, "acct:"))
  77. *user = xs_replace_n(subject, "acct:", "", 1);
  78. else {
  79. /* subject not an 'acct:': try in the aliases
  80. (i.e. hubzilla does this) */
  81. const xs_list *aliases = xs_dict_get(obj, "aliases");
  82. if (xs_is_list(aliases)) {
  83. const char *v;
  84. xs_list_foreach(aliases, v) {
  85. if (xs_startswith(v, "acct:") && xs_endswith(v, host)) {
  86. *user = xs_replace_n(v, "acct:", "", 1);
  87. break;
  88. }
  89. }
  90. }
  91. }
  92. }
  93. if (actor != NULL) {
  94. const xs_list *list = xs_dict_get(obj, "links");
  95. int c = 0;
  96. const char *v;
  97. while (xs_list_next(list, &v, &c)) {
  98. if (xs_type(v) == XSTYPE_DICT) {
  99. const char *type = xs_dict_get(v, "type");
  100. if (type && (strcmp(type, "application/activity+json") == 0 ||
  101. strcmp(type, "application/ld+json; profile=\"https:/"
  102. "/www.w3.org/ns/activitystreams\"") == 0)) {
  103. *actor = xs_dup(xs_dict_get(v, "href"));
  104. break;
  105. }
  106. }
  107. }
  108. }
  109. }
  110. return status;
  111. }
  112. int webfinger_request(const char *qs, xs_str **actor, xs_str **user)
  113. /* queries the webfinger for qs and fills the required fields */
  114. {
  115. return webfinger_request_signed(NULL, qs, actor, user);
  116. }
  117. int webfinger_request_fake(const char *qs, xs_str **actor, xs_str **user)
  118. /* queries the webfinger and, if it fails, a user is faked if possible */
  119. {
  120. int status;
  121. if (!valid_status(status = webfinger_request(qs, actor, user))) {
  122. if (xs_startswith(qs, "https:/") || xs_startswith(qs, "http:/")) {
  123. xs *l = xs_split(qs, "/");
  124. if (xs_list_len(l) > 3) {
  125. srv_debug(1, xs_fmt("webfinger error querying %s %d -- faking it", qs, status));
  126. /* i'll end up in hell for this */
  127. *user = xs_fmt("%s@%s", xs_list_get(l, -1), xs_list_get(l, 2));
  128. status = HTTP_STATUS_RESET_CONTENT;
  129. }
  130. }
  131. }
  132. return status;
  133. }
  134. int webfinger_get_handler(const xs_dict *req, const char *q_path,
  135. xs_val **body, int *b_size, char **ctype)
  136. /* serves webfinger queries */
  137. {
  138. int status;
  139. (void)b_size;
  140. if (strcmp(q_path, "/.well-known/webfinger") != 0)
  141. return 0;
  142. const char *q_vars = xs_dict_get(req, "q_vars");
  143. const char *resource = xs_dict_get(q_vars, "resource");
  144. if (resource == NULL)
  145. return HTTP_STATUS_BAD_REQUEST;
  146. snac snac;
  147. int found = 0;
  148. if (xs_startswith(resource, "https:/") || xs_startswith(resource, "http:/")) {
  149. /* actor search: find a user with this actor */
  150. xs *l = xs_split(resource, "/");
  151. const char *uid = xs_list_get(l, -1);
  152. if (uid)
  153. found = user_open(&snac, uid);
  154. }
  155. else
  156. {
  157. xs *an = xs_fmt("%s", resource);
  158. xs *l = NULL;
  159. if (xs_startswith(resource, "acct:")) {
  160. /* it's an account name */
  161. an = xs_replace_n(resource, "acct:", "", 1);
  162. }
  163. /* strip a possible leading @ */
  164. if (xs_startswith(an, "@"))
  165. an = xs_crop_i(an, 1, 0);
  166. l = xs_split_n(an, "@", 1);
  167. if (xs_list_len(l) == 2) {
  168. const char *uid = xs_list_get(l, 0);
  169. const char *host = xs_list_get(l, 1);
  170. if (strcmp(host, xs_dict_get(srv_config, "host")) == 0)
  171. found = user_open(&snac, uid);
  172. }
  173. }
  174. if (found) {
  175. /* build the object */
  176. xs *acct;
  177. xs *aaj = xs_dict_new();
  178. xs *prof = xs_dict_new();
  179. xs *links = xs_list_new();
  180. xs *aliases = xs_list_new();
  181. xs *obj = xs_dict_new();
  182. acct = xs_fmt("acct:%s@%s",
  183. xs_dict_get(snac.config, "uid"), xs_dict_get(srv_config, "host"));
  184. aaj = xs_dict_append(aaj, "rel", "self");
  185. aaj = xs_dict_append(aaj, "type", "application/activity+json");
  186. aaj = xs_dict_append(aaj, "href", snac.actor);
  187. links = xs_list_append(links, aaj);
  188. /* duplicate with the ld+json type */
  189. aaj = xs_dict_set(aaj, "type", "application/ld+json; profile=\"https:/"
  190. "/www.w3.org/ns/activitystreams\"");
  191. links = xs_list_append(links, aaj);
  192. prof = xs_dict_append(prof, "rel", "http://webfinger.net/rel/profile-page");
  193. prof = xs_dict_append(prof, "type", "text/html");
  194. prof = xs_dict_append(prof, "href", snac.actor);
  195. links = xs_list_append(links, prof);
  196. const char *avatar = xs_dict_get(snac.config, "avatar");
  197. if (!xs_is_null(avatar) && *avatar) {
  198. xs *d = xs_dict_new();
  199. d = xs_dict_append(d, "rel", "http:/" "/webfinger.net/rel/avatar");
  200. d = xs_dict_append(d, "type", xs_mime_by_ext(avatar));
  201. d = xs_dict_append(d, "href", avatar);
  202. links = xs_list_append(links, d);
  203. }
  204. {
  205. xs *d = xs_dict_new();
  206. xs *t = xs_fmt("https:/""/%s/authorize_interaction?uri={uri}", xs_dict_get(srv_config, "host"));
  207. d = xs_dict_set(d, "rel", "http://ostatus.org/schema/1.0/subscribe");
  208. d = xs_dict_set(d, "template", t);
  209. links = xs_list_append(links, d);
  210. }
  211. {
  212. xs *d = xs_dict_new();
  213. xs *t = xs_fmt("https:/""/%s/share?text={content}", xs_dict_get(srv_config, "host"));
  214. d = xs_dict_set(d, "rel", "https://w3id.org/fep/3b86/Create");
  215. d = xs_dict_set(d, "template", t);
  216. links = xs_list_append(links, d);
  217. }
  218. {
  219. xs *d = xs_dict_new();
  220. xs *t = xs_fmt("https:/""/%s/authorize_interaction?uri={object}", xs_dict_get(srv_config, "host"));
  221. d = xs_dict_set(d, "rel", "https://w3id.org/fep/3b86/Object");
  222. d = xs_dict_set(d, "template", t);
  223. links = xs_list_append(links, d);
  224. }
  225. aliases = xs_list_append(aliases, snac.actor);
  226. aliases = xs_list_append(aliases, snac.actor_alt);
  227. obj = xs_dict_append(obj, "aliases", aliases);
  228. obj = xs_dict_append(obj, "subject", acct);
  229. obj = xs_dict_append(obj, "links", links);
  230. xs_str *j = xs_json_dumps(obj, 4);
  231. user_free(&snac);
  232. status = HTTP_STATUS_OK;
  233. *body = j;
  234. *ctype = "application/jrd+json";
  235. }
  236. else
  237. status = HTTP_STATUS_NOT_FOUND;
  238. srv_debug(1, xs_fmt("webfinger_get_handler resource=%s %d", resource, status));
  239. return status;
  240. }