diff options
| author | 2022-09-21 21:12:49 +0200 | |
|---|---|---|
| committer | 2022-09-21 21:12:49 +0200 | |
| commit | 6e4294f8e8a9efd7e5e5670554fe18d7462c5fe3 (patch) | |
| tree | 5f0d3ba0dd8b629fa03b3294f1dd3e3b21a6c513 | |
| parent | Updated Makefile. (diff) | |
| download | penes-snac2-6e4294f8e8a9efd7e5e5670554fe18d7462c5fe3.tar.gz penes-snac2-6e4294f8e8a9efd7e5e5670554fe18d7462c5fe3.tar.xz penes-snac2-6e4294f8e8a9efd7e5e5670554fe18d7462c5fe3.zip | |
Added a webfinger handler.
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | httpd.c | 3 | ||||
| -rw-r--r-- | snac.h | 3 | ||||
| -rw-r--r-- | webfinger.c | 94 |
4 files changed, 102 insertions, 1 deletions
| @@ -2,7 +2,7 @@ CFLAGS=-g -Wall | |||
| 2 | 2 | ||
| 3 | all: snac | 3 | all: snac |
| 4 | 4 | ||
| 5 | snac: snac.o main.o data.o http.o httpd.o | 5 | snac: snac.o main.o data.o http.o httpd.o webfinger.o |
| 6 | $(CC) -L/usr/local/lib *.o -lcurl -lcrypto -o $@ | 6 | $(CC) -L/usr/local/lib *.o -lcurl -lcrypto -o $@ |
| 7 | 7 | ||
| 8 | .c.o: | 8 | .c.o: |
| @@ -21,3 +21,4 @@ httpd.o: httpd.c xs.h xs_io.h xs_encdec.h xs_json.h xs_socket.h \ | |||
| 21 | main.o: main.c xs.h xs_encdec.h xs_json.h snac.h | 21 | main.o: main.c xs.h xs_encdec.h xs_json.h snac.h |
| 22 | snac.o: snac.c xs.h xs_io.h xs_encdec.h xs_json.h xs_curl.h \ | 22 | snac.o: snac.c xs.h xs_io.h xs_encdec.h xs_json.h xs_curl.h \ |
| 23 | xs_openssl.h xs_socket.h xs_httpd.h snac.h | 23 | xs_openssl.h xs_socket.h xs_httpd.h snac.h |
| 24 | webfinger.o: webfinger.c xs.h xs_encdec.h xs_json.h snac.h | ||
| @@ -109,6 +109,9 @@ void httpd_connection(int rs) | |||
| 109 | /* cascade through */ | 109 | /* cascade through */ |
| 110 | if (status == 0) | 110 | if (status == 0) |
| 111 | server_get_handler(req, q_path, &status, &body, &b_size, &ctype); | 111 | server_get_handler(req, q_path, &status, &body, &b_size, &ctype); |
| 112 | |||
| 113 | if (status == 0) | ||
| 114 | webfinger_get_handler(req, q_path, &status, &body, &b_size, &ctype); | ||
| 112 | } | 115 | } |
| 113 | else | 116 | else |
| 114 | if (strcmp(method, "POST") == 0) { | 117 | if (strcmp(method, "POST") == 0) { |
| @@ -69,3 +69,6 @@ d_char *http_signed_request(snac *snac, char *method, char *url, | |||
| 69 | int *status, d_char **payload, int *p_size); | 69 | int *status, d_char **payload, int *p_size); |
| 70 | 70 | ||
| 71 | void httpd(void); | 71 | void httpd(void); |
| 72 | |||
| 73 | void webfinger_get_handler(d_char *req, char *q_path, int *status, | ||
| 74 | char **body, int *b_size, char **ctype); | ||
diff --git a/webfinger.c b/webfinger.c new file mode 100644 index 0000000..be89ea9 --- /dev/null +++ b/webfinger.c | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | /* snac - A simple, minimalistic ActivityPub instance */ | ||
| 2 | /* copyright (c) 2022 grunfink - MIT license */ | ||
| 3 | |||
| 4 | #include "xs.h" | ||
| 5 | #include "xs_encdec.h" | ||
| 6 | #include "xs_json.h" | ||
| 7 | |||
| 8 | #include "snac.h" | ||
| 9 | |||
| 10 | void webfinger_get_handler(d_char *req, char *q_path, int *status, | ||
| 11 | char **body, int *b_size, char **ctype) | ||
| 12 | /* serves webfinger queries */ | ||
| 13 | { | ||
| 14 | if (strcmp(q_path, "/.well-known/webfinger") != 0) | ||
| 15 | return; | ||
| 16 | |||
| 17 | char *q_vars = xs_dict_get(req, "q_vars"); | ||
| 18 | char *resource = xs_dict_get(q_vars, "resource"); | ||
| 19 | |||
| 20 | if (resource == NULL) { | ||
| 21 | *status = 400; | ||
| 22 | return; | ||
| 23 | } | ||
| 24 | |||
| 25 | snac snac; | ||
| 26 | int found = 0; | ||
| 27 | |||
| 28 | if (xs_startswith(resource, "https:/" "/")) { | ||
| 29 | /* actor search: find a user with this actor */ | ||
| 30 | xs *list = user_list(); | ||
| 31 | char *p, *uid; | ||
| 32 | |||
| 33 | p = list; | ||
| 34 | while (xs_list_iter(&p, &uid)) { | ||
| 35 | if (user_open(&snac, uid)) { | ||
| 36 | if (strcmp(snac.actor, resource) == 0) { | ||
| 37 | found = 1; | ||
| 38 | break; | ||
| 39 | } | ||
| 40 | |||
| 41 | user_free(&snac); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | else | ||
| 46 | if (xs_startswith(resource, "acct:")) { | ||
| 47 | /* it's an account name */ | ||
| 48 | xs *an = xs_replace(resource, "acct:", ""); | ||
| 49 | xs *l = NULL; | ||
| 50 | |||
| 51 | /* strip a possible leading @ */ | ||
| 52 | if (xs_startswith(an, "@")) | ||
| 53 | an = xs_crop(an, 1, 0); | ||
| 54 | |||
| 55 | l = xs_split_n(an, "@", 1); | ||
| 56 | |||
| 57 | if (xs_list_len(l) == 2) { | ||
| 58 | char *uid = xs_list_get(l, 0); | ||
| 59 | char *host = xs_list_get(l, 1); | ||
| 60 | |||
| 61 | if (strcmp(host, xs_dict_get(srv_config, "host")) == 0) | ||
| 62 | found = user_open(&snac, uid); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | if (found) { | ||
| 67 | /* build the object */ | ||
| 68 | xs *acct; | ||
| 69 | xs *aaj = xs_dict_new(); | ||
| 70 | xs *links = xs_list_new(); | ||
| 71 | xs *obj = xs_dict_new(); | ||
| 72 | d_char *j; | ||
| 73 | |||
| 74 | acct = xs_fmt("acct:%s@%s", | ||
| 75 | xs_dict_get(snac.config, "uid"), xs_dict_get(srv_config, "host")); | ||
| 76 | |||
| 77 | aaj = xs_dict_append(aaj, "rel", "self"); | ||
| 78 | aaj = xs_dict_append(aaj, "type", "application/activity+json"); | ||
| 79 | aaj = xs_dict_append(aaj, "href", snac.actor); | ||
| 80 | |||
| 81 | links = xs_list_append(links, aaj); | ||
| 82 | |||
| 83 | obj = xs_dict_append(obj, "subject", acct); | ||
| 84 | obj = xs_dict_append(obj, "links", links); | ||
| 85 | |||
| 86 | j = xs_json_dumps_pp(obj, 4); | ||
| 87 | |||
| 88 | user_free(&snac); | ||
| 89 | |||
| 90 | *status = 200; | ||
| 91 | *body = j; | ||
| 92 | *ctype = "application/json"; | ||
| 93 | } | ||
| 94 | } | ||