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