2.66 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, xs_str **actor, xs_str **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    const char *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 HTTP_STATUS_BAD_REQUEST;
 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            const 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            const xs_list *list = xs_dict_get(obj, "links");
 98            int c = 0;
 99            const char *v;
100
101            while (xs_list_next(list, &v, &c)) {
102                if (xs_type(v) == XSTYPE_DICT) {
103                    const char *type = xs_dict_get(v, "type");
104
105                    if (type && (strcmp(type, "application/activity+json") == 0 ||
106                                 strcmp(type, "application/ld+json; profile=\"https:/"
107                                    "/www.w3.org/ns/activitystreams\"") == 0)) {
108                        *actor = xs_dup(xs_dict_get(v, "href"));
109                        break;
110                    }
111                }
112            }
113        }
114    }
115
116    return status;
117}
118
119
120int webfinger_request(const char *qs, xs_str **actor, xs_str **user)
121/* queries the webfinger for qs and fills the required fields */
122{
123    return webfinger_request_signed(NULL, qs, actor, user);
124}
125
126
127int webfinger_request_fake(const char *qs, xs_str **actor, xs_str **user)
128/* queries the webfinger and, if it fails, a user is faked if possible */
129{
130    int status;
131
132    if (!valid_status(status = webfinger_request(qs, actor, user))) {
133        if (xs_startswith(qs, "https:/") || xs_startswith(qs, "http:/")) {
134            xs *l = xs_split(qs, "/");
135
136            if (xs_list_len(l) > 3) {
137                srv_debug(1, xs_fmt("webfinger error querying %s %d -- faking it", qs, status));
138
139                /* i'll end up in hell for this */
140                *user = xs_fmt("%s@%s", xs_list_get(l, -1), xs_list_get(l, 2));
141                status = HTTP_STATUS_RESET_CONTENT;
142
143            }
144        }
145    }
146
147    return status;
148}
149
150
151int webfinger_get_handler(xs_dict *req, const char *q_path,
152                           xs_val **body, int *b_size, char **ctype)
153/* serves webfinger queries */
154{
155    int status;
156
157    (void)b_size;
158
159    if (strcmp(q_path, "/.well-known/webfinger") != 0)
160        return 0;
161
162    const char *q_vars   = xs_dict_get(req, "q_vars");
163    const char *resource = xs_dict_get(q_vars, "resource");
164
165    if (resource == NULL)
166        return HTTP_STATUS_BAD_REQUEST;
167
168    snac snac;
169    int found = 0;
170
171    if (xs_startswith(resource, "https:/") || xs_startswith(resource, "http:/")) {
172        /* actor search: find a user with this actor */
173        xs *l = xs_split(resource, "/");
174        const char *uid = xs_list_get(l, -1);
175
176        if (uid)
177            found = user_open(&snac, uid);
178    }
179    else
180    if (xs_startswith(resource, "acct:")) {
181        /* it's an account name */
182        xs *an = xs_replace_n(resource, "acct:", "", 1);
183        xs *l = NULL;
184
185        /* strip a possible leading @ */
186        if (xs_startswith(an, "@"))
187            an = xs_crop_i(an, 1, 0);
188
189        l = xs_split_n(an, "@", 1);
190
191        if (xs_list_len(l) == 2) {
192            const char *uid  = xs_list_get(l, 0);
193            const char *host = xs_list_get(l, 1);
194
195            if (strcmp(host, xs_dict_get(srv_config, "host")) == 0)
196                found = user_open(&snac, uid);
197        }
198    }
199
200    if (found) {
201        /* build the object */
202        xs *acct;
203        xs *aaj   = xs_dict_new();
204        xs *prof  = xs_dict_new();
205        xs *links = xs_list_new();
206        xs *obj   = xs_dict_new();
207
208        acct = xs_fmt("acct:%s@%s",
209            xs_dict_get(snac.config, "uid"), xs_dict_get(srv_config, "host"));
210
211        aaj = xs_dict_append(aaj, "rel",  "self");
212        aaj = xs_dict_append(aaj, "type", "application/activity+json");
213        aaj = xs_dict_append(aaj, "href", snac.actor);
214
215        links = xs_list_append(links, aaj);
216
217        /* duplicate with the ld+json type */
218        aaj = xs_dict_set(aaj, "type", "application/ld+json; profile=\"https:/"
219                                    "/www.w3.org/ns/activitystreams\"");
220
221        links = xs_list_append(links, aaj);
222
223        prof = xs_dict_append(prof, "rel", "http://webfinger.net/rel/profile-page");
224        prof = xs_dict_append(prof, "type", "text/html");
225        prof = xs_dict_append(prof, "href", snac.actor);
226
227        links = xs_list_append(links, prof);
228
229        const char *avatar = xs_dict_get(snac.config, "avatar");
230        if (!xs_is_null(avatar) && *avatar) {
231            xs *d = xs_dict_new();
232
233            d = xs_dict_append(d, "rel",  "http:/" "/webfinger.net/rel/avatar");
234            d = xs_dict_append(d, "type", xs_mime_by_ext(avatar));
235            d = xs_dict_append(d, "href", avatar);
236
237            links = xs_list_append(links, d);
238        }
239
240        obj = xs_dict_append(obj, "subject", acct);
241        obj = xs_dict_append(obj, "links",   links);
242
243        xs_str *j = xs_json_dumps(obj, 4);
244
245        user_free(&snac);
246
247        status = HTTP_STATUS_OK;
248        *body  = j;
249        *ctype = "application/jrd+json";
250    }
251    else
252        status = HTTP_STATUS_NOT_FOUND;
253
254    srv_debug(1, xs_fmt("webfinger_get_handler resource=%s %d", resource, status));
255
256    return status;
257}