commit 253e7f9

default  ·  2023-10-13 04:33:12 +0000 UTC
parent e127269
Don't allow creating users which user name strings only differ in case.
5 files changed,  +47, -19
M data.c
M snac.c
+1, -1
1@@ -53,4 +53,4 @@ snac.o: snac.c xs.h xs_io.h xs_unicode.h xs_json.h xs_curl.h xs_openssl.h \
2 upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h
3 utils.o: utils.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h \
4  xs_random.h snac.h
5-webfinger.o: webfinger.c xs.h xs_json.h xs_curl.h snac.h
6+webfinger.o: webfinger.c xs.h xs_json.h xs_curl.h xs_mime.h snac.h
M data.c
+26, -4
 1@@ -154,12 +154,34 @@ int user_open(snac *snac, const char *uid)
 2     memset(snac, '\0', sizeof(struct _snac));
 3 
 4     if (validate_uid(uid)) {
 5-        xs *cfg_file;
 6+        xs *cfg_file = NULL;
 7         FILE *f;
 8 
 9-        snac->uid = xs_str_new(uid);
10+        xs *t = xs_fmt("%s/user/%s", srv_basedir, uid);
11 
12-        snac->basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
13+        if (mtime(t) == 0.0) {
14+            /* user folder does not exist; try with a different case */
15+            xs *lcuid = xs_tolower_i(xs_dup(uid));
16+            xs *ulist = user_list();
17+            xs_list *p = ulist;
18+            xs_str *v;
19+
20+            while (xs_list_iter(&p, &v)) {
21+                xs *v2 = xs_tolower_i(xs_dup(v));
22+
23+                if (strcmp(lcuid, v2) == 0) {
24+                    snac->uid = xs_dup(v);
25+                    break;
26+                }
27+            }
28+        }
29+        else
30+            snac->uid = xs_str_new(uid);
31+
32+        if (snac->uid == NULL)
33+            return ret;
34+
35+        snac->basedir = xs_fmt("%s/user/%s", srv_basedir, snac->uid);
36 
37         cfg_file = xs_fmt("%s/user.json", snac->basedir);
38 
39@@ -176,7 +198,7 @@ int user_open(snac *snac, const char *uid)
40                     fclose(f);
41 
42                     if (snac->key != NULL) {
43-                        snac->actor = xs_fmt("%s/%s", srv_baseurl, uid);
44+                        snac->actor = xs_fmt("%s/%s", srv_baseurl, snac->uid);
45                         snac->md5   = xs_md5_hex(snac->actor, strlen(snac->actor));
46 
47                         /* everything is ok right now */
M snac.c
+3, -0
 1@@ -79,6 +79,9 @@ double ftime(void)
 2 int validate_uid(const char *uid)
 3 /* returns if uid is a valid identifier */
 4 {
 5+    if (!uid || *uid == '\0')
 6+        return 0;
 7+
 8     while (*uid) {
 9         if (!(isalnum(*uid) || *uid == '_'))
10             return 0;
+1, -1
1@@ -244,7 +244,7 @@ int adduser(const char *uid)
2     }
3 
4     if (user_open(&snac, uid)) {
5-        printf("ERROR: user '%s' already exists\n", uid);
6+        printf("ERROR: user '%s' already exists\n", snac.uid);
7         return 1;
8     }
9 
+16, -13
 1@@ -4,6 +4,7 @@
 2 #include "xs.h"
 3 #include "xs_json.h"
 4 #include "xs_curl.h"
 5+#include "xs_mime.h"
 6 
 7 #include "snac.h"
 8 
 9@@ -128,20 +129,11 @@ int webfinger_get_handler(xs_dict *req, char *q_path,
10 
11     if (xs_startswith(resource, "https:/" "/")) {
12         /* actor search: find a user with this actor */
13-        xs *list = user_list();
14-        char *p, *uid;
15-
16-        p = list;
17-        while (xs_list_iter(&p, &uid)) {
18-            if (user_open(&snac, uid)) {
19-                if (strcmp(snac.actor, resource) == 0) {
20-                    found = 1;
21-                    break;
22-                }
23+        xs *l = xs_split(resource, "/");
24+        char *uid = xs_list_get(l, -1);
25 
26-                user_free(&snac);
27-            }
28-        }
29+        if (uid)
30+            found = user_open(&snac, uid);
31     }
32     else
33     if (xs_startswith(resource, "acct:")) {
34@@ -180,6 +172,17 @@ int webfinger_get_handler(xs_dict *req, char *q_path,
35 
36         links = xs_list_append(links, aaj);
37 
38+        char *avatar = xs_dict_get(snac.config, "avatar");
39+        if (!xs_is_null(avatar) && *avatar) {
40+            xs *d = xs_dict_new();
41+
42+            d = xs_dict_append(d, "rel",  "http:/" "/webfinger.net/rel/avatar");
43+            d = xs_dict_append(d, "type", xs_mime_by_ext(avatar));
44+            d = xs_dict_append(d, "href", avatar);
45+
46+            links = xs_list_append(links, d);
47+        }
48+
49         obj = xs_dict_append(obj, "subject", acct);
50         obj = xs_dict_append(obj, "links",   links);
51