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