New function new_password().

This commit is contained in:
default 2022-12-04 21:14:18 +01:00
parent 6714084011
commit 7787a2ded9
4 changed files with 26 additions and 18 deletions

2
data.c
View file

@ -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 */ /* opens a user */
{ {
int ret = 0; int ret = 0;

6
snac.c
View file

@ -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 */ /* returns if uid is a valid identifier */
{ {
while (*uid) { 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 */ /* hashes a password */
{ {
xs *d_nonce = NULL; 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 */ /* checks a password */
{ {
int ret = 0; int ret = 0;

10
snac.h
View file

@ -36,17 +36,17 @@ typedef struct _snac {
d_char *md5; /* actor url md5 */ d_char *md5; /* actor url md5 */
} snac; } snac;
int user_open(snac *snac, char *uid); int user_open(snac *snac, const char *uid);
void user_free(snac *snac); void user_free(snac *snac);
d_char *user_list(void); d_char *user_list(void);
void snac_debug(snac *snac, int level, d_char *str); void snac_debug(snac *snac, int level, d_char *str);
#define snac_log(snac, str) snac_debug(snac, 0, 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); d_char *hash_password(const char *uid, const char *passwd, const char *nonce);
int check_password(char *uid, char *passwd, char *hash); int check_password(const char *uid, const char *passwd, const char *hash);
void srv_archive(char *direction, char *req, char *payload, int p_size, void srv_archive(char *direction, char *req, char *payload, int p_size,
int status, char *headers, char *body, int b_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); char **body, int *b_size, char **ctype);
int initdb(const char *_basedir); int initdb(const char *_basedir);
int adduser(char *uid); int adduser(const char *uid);

26
utils.c
View file

@ -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 */ /* creates a new user */
{ {
snac snac; snac snac;
xs *config = xs_dict_new(); xs *config = xs_dict_new();
xs *date = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ"); xs *date = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
int rndbuf[3];
xs *pwd = NULL; xs *pwd = NULL;
xs *pwd_f = NULL; xs *pwd_f = NULL;
xs *key = NULL; xs *key = NULL;
@ -214,13 +228,7 @@ int adduser(char *uid)
return 1; return 1;
} }
srandom(time(NULL) ^ getpid()); new_password(uid, &pwd, &pwd_f);
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);
config = xs_dict_append(config, "uid", uid); config = xs_dict_append(config, "uid", uid);
config = xs_dict_append(config, "name", uid); config = xs_dict_append(config, "name", uid);