From fdb1692c4e93ec4a71453d5d6c319fe1d6405e60 Mon Sep 17 00:00:00 2001 From: Andi Shyti Date: Sun, 2 Sep 2012 02:12:08 +0200 Subject: accman: git repo created Signed-off-by: Andi Shyti --- src/crypt_sha256.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/db_user.c | 32 +++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 src/crypt_sha256.c create mode 100644 src/db_user.c (limited to 'src') diff --git a/src/crypt_sha256.c b/src/crypt_sha256.c new file mode 100644 index 0000000..c480637 --- /dev/null +++ b/src/crypt_sha256.c @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +#include + +#define RAND_FILE "/dev/random" +#define SHA_STR "{SHA256}" + +/* string length */ +#define LEN_PASSWD_MIN 8 +#define LEN_PASSWD_MAX 64 +#define LEN_RAND_STR 16 +#define LEN_BUFFER LEN_PASSWD_MAX + LEN_RAND_STR + \ + SHA256_DIGEST_LENGTH +#define LEN_ENC64 64 +#define LEN_SHA_STR 8 +#define LEN_FINAL_PASSWD LEN_ENC64 + LEN_SHA_STR + 1 + + +uint8_t check_passwd(const char *p, size_t len) +{ + return ((len > LEN_PASSWD_MIN) || (len < LEN_PASSWD_MAX)) ? 1 : 0; +} + +char* get_crypt_sha256(const char *p) +{ + int rand_fd; + ssize_t size; + size_t len = strlen(p); + char rand_str[LEN_RAND_STR]; + char passwd_buff[LEN_BUFFER]; + char sha_pwd[SHA256_DIGEST_LENGTH]; + char enc64_pwd[LEN_ENC64]; + char *final_pwd; + + if (!check_passwd(p, len)) { + fprintf(stderr, + "the password has to be between 8 and 64 chars\n"); + exit(EXIT_FAILURE); + } + + rand_fd = open(RAND_FILE, O_RDONLY); + if (rand_fd < 0) { + fprintf(stderr, "impossible to open " RAND_FILE "\n"); + exit(EXIT_FAILURE); + } + + size = read(rand_fd, rand_str, LEN_RAND_STR); + close(rand_fd); + if (size != LEN_RAND_STR) { + fprintf(stderr, "failed to read from " RAND_FILE "\n"); + exit(EXIT_FAILURE); + } + + memcpy(passwd_buff + SHA256_DIGEST_LENGTH, p, len); + memcpy(passwd_buff + SHA256_DIGEST_LENGTH + len, + rand_str, LEN_RAND_STR); + + SHA256((unsigned char*) passwd_buff + SHA256_DIGEST_LENGTH, + len + LEN_RAND_STR, (unsigned char*) sha_pwd); + + memcpy(passwd_buff, sha_pwd, SHA256_DIGEST_LENGTH); + + EVP_EncodeBlock((unsigned char*) enc64_pwd, (unsigned char*) sha_pwd, + SHA256_DIGEST_LENGTH + len + LEN_RAND_STR); + + + final_pwd = (char*) malloc (LEN_FINAL_PASSWD); + memcpy(final_pwd, SHA_STR, LEN_SHA_STR); + memcpy(final_pwd+8, enc64_pwd, LEN_ENC64); + final_pwd[LEN_SHA_STR + LEN_FINAL_PASSWD] = '\0'; + + return final_pwd; +} diff --git a/src/db_user.c b/src/db_user.c new file mode 100644 index 0000000..1a851c8 --- /dev/null +++ b/src/db_user.c @@ -0,0 +1,32 @@ +#include +#include + +void db_insert_user(struct user u) +{ + MYSQL *conn; + char mysql_query_str[1024]; + + conn = mysql_init(NULL); + + if (!conn) { + printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn)); + exit(EXIT_FAILURE); + } + + if (!mysql_real_connect(conn, "localhost", "user", "password", + "mailserver", 0, NULL, 0)) { + printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn)); + exit(EXIT_FAILURE); + } + + snprintf(mysql_query_str, 1024, + "insert into virtual_users(domain_id, password, email) " + "values (1, '%s', '%s')", u.p, u.n); + + if (mysql_query(conn, mysql_query_str)) { + printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn)); + exit(1); + } + + mysql_close(conn); +} -- cgit v1.2.3