summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndi Shyti <andi@etezian.org>2012-09-02 02:12:08 +0200
committerAndi Shyti <andi@etezian.org>2012-09-02 02:12:08 +0200
commitfdb1692c4e93ec4a71453d5d6c319fe1d6405e60 (patch)
tree0f6142f3858f3f79f00d13097a73b60f89b37476 /src
accman: git repo created
Signed-off-by: Andi Shyti <andi@etezian.org>
Diffstat (limited to 'src')
-rw-r--r--src/crypt_sha256.c76
-rw-r--r--src/db_user.c32
2 files changed, 108 insertions, 0 deletions
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 <string.h>
+#include <unistd.h>
+#include <stdint.h>
+#include <fcntl.h>
+#include <openssl/sha.h>
+#include <openssl/evp.h>
+
+#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 <accman.h>
+#include <mysql.h>
+
+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);
+}