diff options
| author | 2022-10-04 09:40:16 +0200 | |
|---|---|---|
| committer | 2022-10-04 09:40:16 +0200 | |
| commit | da7bf43385e900121aadbd90f6cb22d05c0cff5c (patch) | |
| tree | 3f2ee74d5995099fcb74ca66728054556b86f092 | |
| parent | Updated Makefile. (diff) | |
| download | snac2-da7bf43385e900121aadbd90f6cb22d05c0cff5c.tar.gz snac2-da7bf43385e900121aadbd90f6cb22d05c0cff5c.tar.xz snac2-da7bf43385e900121aadbd90f6cb22d05c0cff5c.zip | |
New function adduser().
| -rw-r--r-- | main.c | 14 | ||||
| -rw-r--r-- | snac.h | 1 | ||||
| -rw-r--r-- | utils.c | 118 |
3 files changed, 129 insertions, 4 deletions
| @@ -16,6 +16,7 @@ int usage(void) | |||
| 16 | printf("Commands:\n"); | 16 | printf("Commands:\n"); |
| 17 | printf("\n"); | 17 | printf("\n"); |
| 18 | printf("init [{basedir}] Initializes the database\n"); | 18 | printf("init [{basedir}] Initializes the database\n"); |
| 19 | printf("adduser {basedir} [{uid}] Adds a new user\n"); | ||
| 19 | printf("httpd {basedir} Starts the HTTPD daemon\n"); | 20 | printf("httpd {basedir} Starts the HTTPD daemon\n"); |
| 20 | printf("webfinger {basedir} {user} Queries about a @user@host or actor\n"); | 21 | printf("webfinger {basedir} {user} Queries about a @user@host or actor\n"); |
| 21 | printf("queue {basedir} {uid} Processes a user queue\n"); | 22 | printf("queue {basedir} {uid} Processes a user queue\n"); |
| @@ -23,7 +24,6 @@ int usage(void) | |||
| 23 | 24 | ||
| 24 | // printf("check {basedir} [{uid}] Checks the database\n"); | 25 | // printf("check {basedir} [{uid}] Checks the database\n"); |
| 25 | // printf("purge {basedir} [{uid}] Purges old data\n"); | 26 | // printf("purge {basedir} [{uid}] Purges old data\n"); |
| 26 | // printf("adduser {basedir} [{uid}] Adds a new user\n"); | ||
| 27 | 27 | ||
| 28 | // printf("update {basedir} {uid} Sends a user update to followers\n"); | 28 | // printf("update {basedir} {uid} Sends a user update to followers\n"); |
| 29 | // printf("passwd {basedir} {uid} Sets the password for {uid}\n"); | 29 | // printf("passwd {basedir} {uid} Sets the password for {uid}\n"); |
| @@ -71,9 +71,7 @@ int main(int argc, char *argv[]) | |||
| 71 | /* ... */ | 71 | /* ... */ |
| 72 | basedir = GET_ARGV(); | 72 | basedir = GET_ARGV(); |
| 73 | 73 | ||
| 74 | initdb(basedir); | 74 | return initdb(basedir); |
| 75 | |||
| 76 | return 0; | ||
| 77 | } | 75 | } |
| 78 | 76 | ||
| 79 | if ((basedir = GET_ARGV()) == NULL) | 77 | if ((basedir = GET_ARGV()) == NULL) |
| @@ -84,6 +82,14 @@ int main(int argc, char *argv[]) | |||
| 84 | return 1; | 82 | return 1; |
| 85 | } | 83 | } |
| 86 | 84 | ||
| 85 | if (strcmp(cmd, "adduser") == 0) { | ||
| 86 | user = GET_ARGV(); | ||
| 87 | |||
| 88 | return adduser(user); | ||
| 89 | |||
| 90 | return 0; | ||
| 91 | } | ||
| 92 | |||
| 87 | if (strcmp(cmd, "httpd") == 0) { | 93 | if (strcmp(cmd, "httpd") == 0) { |
| 88 | httpd(); | 94 | httpd(); |
| 89 | return 0; | 95 | return 0; |
| @@ -134,3 +134,4 @@ int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size, | |||
| 134 | char **body, int *b_size, char **ctype); | 134 | char **body, int *b_size, char **ctype); |
| 135 | 135 | ||
| 136 | int initdb(const char *_basedir); | 136 | int initdb(const char *_basedir); |
| 137 | int adduser(char *uid); | ||
| @@ -5,10 +5,13 @@ | |||
| 5 | #include "xs_io.h" | 5 | #include "xs_io.h" |
| 6 | #include "xs_encdec.h" | 6 | #include "xs_encdec.h" |
| 7 | #include "xs_json.h" | 7 | #include "xs_json.h" |
| 8 | #include "xs_time.h" | ||
| 9 | #include "xs_openssl.h" | ||
| 8 | 10 | ||
| 9 | #include "snac.h" | 11 | #include "snac.h" |
| 10 | 12 | ||
| 11 | #include <sys/stat.h> | 13 | #include <sys/stat.h> |
| 14 | #include <stdlib.h> | ||
| 12 | 15 | ||
| 13 | const char *default_srv_config = "{" | 16 | const char *default_srv_config = "{" |
| 14 | "\"host\": \"\"," | 17 | "\"host\": \"\"," |
| @@ -172,3 +175,118 @@ int initdb(const char *basedir) | |||
| 172 | printf("Done.\n"); | 175 | printf("Done.\n"); |
| 173 | return 0; | 176 | return 0; |
| 174 | } | 177 | } |
| 178 | |||
| 179 | |||
| 180 | int adduser(char *uid) | ||
| 181 | /* creates a new user */ | ||
| 182 | { | ||
| 183 | snac snac; | ||
| 184 | xs *config = xs_dict_new(); | ||
| 185 | xs *date = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ"); | ||
| 186 | int rndbuf[3]; | ||
| 187 | xs *pwd = NULL; | ||
| 188 | xs *pwd_f = NULL; | ||
| 189 | xs *key = NULL; | ||
| 190 | FILE *f; | ||
| 191 | |||
| 192 | if (uid == NULL) { | ||
| 193 | printf("User id:\n"); | ||
| 194 | uid = xs_strip(xs_readline(stdin)); | ||
| 195 | } | ||
| 196 | |||
| 197 | if (!validate_uid(uid)) { | ||
| 198 | printf("ERROR: only alphanumeric characters and _ are allowed in user ids.\n"); | ||
| 199 | return 1; | ||
| 200 | } | ||
| 201 | |||
| 202 | if (user_open(&snac, uid)) { | ||
| 203 | printf("ERROR: user '%s' already exists\n", uid); | ||
| 204 | return 1; | ||
| 205 | } | ||
| 206 | |||
| 207 | srandom(time(NULL) ^ getpid()); | ||
| 208 | rndbuf[0] = random() & 0xffffffff; | ||
| 209 | rndbuf[1] = random() & 0xffffffff; | ||
| 210 | rndbuf[2] = random() & 0xffffffff; | ||
| 211 | |||
| 212 | pwd = xs_base64_enc((char *)rndbuf, sizeof(rndbuf)); | ||
| 213 | pwd_f = hash_password(uid, pwd, NULL); | ||
| 214 | |||
| 215 | config = xs_dict_append(config, "uid", uid); | ||
| 216 | config = xs_dict_append(config, "name", uid); | ||
| 217 | config = xs_dict_append(config, "avatar", ""); | ||
| 218 | config = xs_dict_append(config, "bio", ""); | ||
| 219 | config = xs_dict_append(config, "published", date); | ||
| 220 | config = xs_dict_append(config, "password", pwd_f); | ||
| 221 | |||
| 222 | xs *basedir = xs_fmt("%s/user/%s", srv_basedir, uid); | ||
| 223 | |||
| 224 | if (mkdir(basedir, 0755) == -1) { | ||
| 225 | printf("ERROR: cannot create directory '%s'\n", basedir); | ||
| 226 | return 0; | ||
| 227 | } | ||
| 228 | |||
| 229 | const char *dirs[] = { | ||
| 230 | "actors", "followers", "following", "local", "muted", | ||
| 231 | "queue", "static", "timeline", "history", NULL }; | ||
| 232 | int n; | ||
| 233 | |||
| 234 | for (n = 0; dirs[n]; n++) { | ||
| 235 | xs *d = xs_fmt("%s/%s", basedir, dirs[n]); | ||
| 236 | mkdir(d, 0755); | ||
| 237 | } | ||
| 238 | |||
| 239 | xs *scssfn = xs_fmt("%s/style.css", srv_basedir); | ||
| 240 | xs *ucssfn = xs_fmt("%s/static/style.css", basedir); | ||
| 241 | |||
| 242 | if ((f = fopen(scssfn, "r")) != NULL) { | ||
| 243 | FILE *i; | ||
| 244 | |||
| 245 | if ((i = fopen(ucssfn, "w")) == NULL) { | ||
| 246 | printf("ERROR: cannot create file '%s'\n", ucssfn); | ||
| 247 | return 1; | ||
| 248 | } | ||
| 249 | else { | ||
| 250 | xs *c = xs_readall(f); | ||
| 251 | fwrite(c, strlen(c), 1, i); | ||
| 252 | |||
| 253 | fclose(i); | ||
| 254 | } | ||
| 255 | |||
| 256 | fclose(f); | ||
| 257 | } | ||
| 258 | |||
| 259 | xs *cfn = xs_fmt("%s/user.json", basedir); | ||
| 260 | |||
| 261 | if ((f = fopen(cfn, "w")) == NULL) { | ||
| 262 | printf("ERROR: cannot create '%s'\n", cfn); | ||
| 263 | return 1; | ||
| 264 | } | ||
| 265 | else { | ||
| 266 | xs *j = xs_json_dumps_pp(config, 4); | ||
| 267 | fwrite(j, strlen(j), 1, f); | ||
| 268 | fclose(f); | ||
| 269 | } | ||
| 270 | |||
| 271 | printf("\nCreating RSA key...\n"); | ||
| 272 | key = xs_rsa_genkey(4096); | ||
| 273 | printf("Done.\n"); | ||
| 274 | |||
| 275 | xs *kfn = xs_fmt("%s/key.json", basedir); | ||
| 276 | |||
| 277 | if ((f = fopen(kfn, "w")) == NULL) { | ||
| 278 | printf("ERROR: cannot create '%s'\n", kfn); | ||
| 279 | return 1; | ||
| 280 | } | ||
| 281 | else { | ||
| 282 | xs *j = xs_json_dumps_pp(key, 4); | ||
| 283 | fwrite(j, strlen(j), 1, f); | ||
| 284 | fclose(f); | ||
| 285 | } | ||
| 286 | |||
| 287 | printf("\nUser password is %s\n", pwd); | ||
| 288 | |||
| 289 | printf("\nGo to %s/%s and keep configuring your user.\n", srv_baseurl, uid); | ||
| 290 | |||
| 291 | return 0; | ||
| 292 | } | ||