summaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c55
1 files changed, 46 insertions, 9 deletions
diff --git a/utils.c b/utils.c
index 8b0b458..14b4816 100644
--- a/utils.c
+++ b/utils.c
@@ -187,13 +187,27 @@ int initdb(const char *basedir)
}
-int adduser(char *uid)
+void new_password(const char *uid, d_char **clear_pwd, d_char **hashed_pwd)
+/* creates a random password */
+{
+ int rndbuf[3];
+
+ srandom(time(NULL) ^ getpid());
+ rndbuf[0] = random() & 0xffffffff;
+ rndbuf[1] = random() & 0xffffffff;
+ rndbuf[2] = random() & 0xffffffff;
+
+ *clear_pwd = xs_base64_enc((char *)rndbuf, sizeof(rndbuf));
+ *hashed_pwd = hash_password(uid, *clear_pwd, NULL);
+}
+
+
+int adduser(const char *uid)
/* creates a new user */
{
snac snac;
xs *config = xs_dict_new();
xs *date = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
- int rndbuf[3];
xs *pwd = NULL;
xs *pwd_f = NULL;
xs *key = NULL;
@@ -214,13 +228,7 @@ int adduser(char *uid)
return 1;
}
- srandom(time(NULL) ^ getpid());
- rndbuf[0] = random() & 0xffffffff;
- rndbuf[1] = random() & 0xffffffff;
- rndbuf[2] = random() & 0xffffffff;
-
- pwd = xs_base64_enc((char *)rndbuf, sizeof(rndbuf));
- pwd_f = hash_password(uid, pwd, NULL);
+ new_password(uid, &pwd, &pwd_f);
config = xs_dict_append(config, "uid", uid);
config = xs_dict_append(config, "name", uid);
@@ -302,3 +310,32 @@ int adduser(char *uid)
return 0;
}
+
+
+int resetpwd(snac *snac)
+/* creates a new password for the user */
+{
+ xs *clear_pwd = NULL;
+ xs *hashed_pwd = NULL;
+ xs *fn = xs_fmt("%s/user.json", snac->basedir);
+ FILE *f;
+ int ret = 0;
+
+ new_password(snac->uid, &clear_pwd, &hashed_pwd);
+
+ snac->config = xs_dict_set(snac->config, "passwd", hashed_pwd);
+
+ if ((f = fopen(fn, "w")) != NULL) {
+ xs *j = xs_json_dumps_pp(snac->config, 4);
+ fwrite(j, strlen(j), 1, f);
+ fclose(f);
+
+ printf("New password for user %s is %s\n", snac->uid, clear_pwd);
+ }
+ else {
+ printf("ERROR: cannot write to %s\n", fn);
+ ret = 1;
+ }
+
+ return ret;
+}