commit 48ebc54

default  ·  2022-12-04 20:26:24 +0000 UTC
parent 7787a2d
New command line option 'resetpwd'.
3 files changed,  +35, -12
M main.c
M snac.h
M main.c
+5, -12
 1@@ -24,21 +24,10 @@ int usage(void)
 2     printf("queue {basedir} {uid}            Processes a user queue\n");
 3     printf("follow {basedir} {uid} {actor}   Follows an actor\n");
 4     printf("unfollow {basedir} {uid} {actor} Unfollows an actor\n");
 5-
 6-//    printf("check {basedir} [{uid}]          Checks the database\n");
 7-
 8-//    printf("update {basedir} {uid}           Sends a user update to followers\n");
 9-//    printf("passwd {basedir} {uid}           Sets the password for {uid}\n");
10-//    printf("unfollow {basedir} {uid} {actor} Unfollows an actor\n");
11-//    printf("mute {basedir} {uid} {actor}     Mutes an actor\n");
12-//    printf("unmute {basedir} {uid} {actor}   Unmutes an actor\n");
13-//    printf("like {basedir} {uid} {url}       Likes an url\n");
14-//    printf("announce {basedir} {uid} {url}   Announces (boosts) an url\n");
15-//    printf("note {basedir} {uid} {'text'}    Sends a note to followers\n");
16-
17     printf("request {basedir} {uid} {url}    Requests an object\n");
18     printf("actor {basedir} {uid} {url}      Requests an actor\n");
19     printf("note {basedir} {uid} {'text'}    Sends a note to followers\n");
20+    printf("resetpwd {basedir} {uid}         Resets the password of a user\n");
21 
22     return 1;
23 }
24@@ -150,6 +139,10 @@ int main(int argc, char *argv[])
25         return 1;
26     }
27 
28+    if (strcmp(cmd, "resetpwd") == 0) {
29+        return resetpwd(&snac);
30+    }
31+
32     if (strcmp(cmd, "queue") == 0) {
33         process_queue(&snac);
34         return 0;
M snac.h
+1, -0
1@@ -174,3 +174,4 @@ int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
2 
3 int initdb(const char *_basedir);
4 int adduser(const char *uid);
5+int resetpwd(snac *snac);
+29, -0
 1@@ -310,3 +310,32 @@ int adduser(const char *uid)
 2 
 3     return 0;
 4 }
 5+
 6+
 7+int resetpwd(snac *snac)
 8+/* creates a new password for the user */
 9+{
10+    xs *clear_pwd  = NULL;
11+    xs *hashed_pwd = NULL;
12+    xs *fn         = xs_fmt("%s/user.json", snac->basedir);
13+    FILE *f;
14+    int ret = 0;
15+
16+    new_password(snac->uid, &clear_pwd, &hashed_pwd);
17+
18+    snac->config = xs_dict_set(snac->config, "passwd", hashed_pwd);
19+
20+    if ((f = fopen(fn, "w")) != NULL) {
21+        xs *j = xs_json_dumps_pp(snac->config, 4);
22+        fwrite(j, strlen(j), 1, f);
23+        fclose(f);
24+
25+        printf("New password for user %s is %s\n", snac->uid, clear_pwd);
26+    }
27+    else {
28+        printf("ERROR: cannot write to %s\n", fn);
29+        ret = 1;
30+    }
31+
32+    return ret;
33+}