From 7787a2ded9a0ab445c9bd310dfc8b909509d763f Mon Sep 17 00:00:00 2001 From: default Date: Sun, 4 Dec 2022 21:14:18 +0100 Subject: [PATCH] New function new_password(). --- data.c | 2 +- snac.c | 6 +++--- snac.h | 10 +++++----- utils.c | 26 +++++++++++++++++--------- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/data.c b/data.c index e3d1268..f36c01d 100644 --- a/data.c +++ b/data.c @@ -120,7 +120,7 @@ void user_free(snac *snac) } -int user_open(snac *snac, char *uid) +int user_open(snac *snac, const char *uid) /* opens a user */ { int ret = 0; diff --git a/snac.c b/snac.c index 4de34f0..903713b 100644 --- a/snac.c +++ b/snac.c @@ -57,7 +57,7 @@ double ftime(void) } -int validate_uid(char *uid) +int validate_uid(const char *uid) /* returns if uid is a valid identifier */ { while (*uid) { @@ -103,7 +103,7 @@ void snac_debug(snac *snac, int level, d_char *str) } -d_char *hash_password(char *uid, char *passwd, char *nonce) +d_char *hash_password(const char *uid, const char *passwd, const char *nonce) /* hashes a password */ { xs *d_nonce = NULL; @@ -120,7 +120,7 @@ d_char *hash_password(char *uid, char *passwd, char *nonce) } -int check_password(char *uid, char *passwd, char *hash) +int check_password(const char *uid, const char *passwd, const char *hash) /* checks a password */ { int ret = 0; diff --git a/snac.h b/snac.h index 00443f9..636fcb3 100644 --- a/snac.h +++ b/snac.h @@ -36,17 +36,17 @@ typedef struct _snac { d_char *md5; /* actor url md5 */ } snac; -int user_open(snac *snac, char *uid); +int user_open(snac *snac, const char *uid); void user_free(snac *snac); d_char *user_list(void); void snac_debug(snac *snac, int level, d_char *str); #define snac_log(snac, str) snac_debug(snac, 0, str) -int validate_uid(char *uid); +int validate_uid(const char *uid); -d_char *hash_password(char *uid, char *passwd, char *nonce); -int check_password(char *uid, char *passwd, char *hash); +d_char *hash_password(const char *uid, const char *passwd, const char *nonce); +int check_password(const char *uid, const char *passwd, const char *hash); void srv_archive(char *direction, char *req, char *payload, int p_size, int status, char *headers, char *body, int b_size); @@ -173,4 +173,4 @@ int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size, char **body, int *b_size, char **ctype); int initdb(const char *_basedir); -int adduser(char *uid); +int adduser(const char *uid); diff --git a/utils.c b/utils.c index 8b0b458..300115b 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);