-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.c
179 lines (136 loc) · 3.99 KB
/
Database.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "Database.h"
void openDatabase(sqlite3 **db, char *accountsFile) {
int rc;
char *sql = NULL;
char *errMsg = NULL;
if(strlen(accountsFile) != 0) {
rc = sqlite3_open(accountsFile, db);
} else {
rc = sqlite3_open("data.db", db);
}
if(rc != SQLITE_OK) {
printError("Can't open database\n");
sqlite3_close(*db);
exit(EXIT_FAILURE);
}
sql = "CREATE TABLE IF NOT EXISTS USERS("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"name VARCHAR(32) UNIQUE NOT NULL,"
"hash VARCHAR(512) NOT NULL,"
"salt VARCHAR(128) NOT NULL)";
rc = sqlite3_exec(*db, sql, NULL, NULL, &errMsg);
if(rc != SQLITE_OK) {
printError("Can't create table\n");
sqlite3_close(*db);
exit(EXIT_FAILURE);
}
}
int isAccountExist(sqlite3 **db, char *userName) {
int rc;
char *sql = NULL;
char *errMsg;
int accountExist = FALSE;
sql = sqlite3_mprintf("SELECT COUNT(*) FROM USERS WHERE name = %Q;", userName);
pthread_rwlock_rdlock(&RW_lock);
rc = sqlite3_exec(*db, sql, isAccountExistCallback, &accountExist, &errMsg);
pthread_rwlock_unlock(&RW_lock);
sqlite3_free(sql);
if(rc != SQLITE_OK) {
printError("Can't execute query\n");
sqlite3_close(*db);
exit(EXIT_FAILURE);
}
return accountExist;
}
int verifyPassword(sqlite3 **db, char *userName, char *password) {
int rc;
char *sql = NULL;
char *errMsg;
char hash[SHA256_DIGEST_LENGTH*2+1];
PasswordInfo *passwordInfo = malloc(sizeof(PasswordInfo));
int verified = FALSE;
memset(passwordInfo, 0, sizeof(PasswordInfo));
sql = sqlite3_mprintf("SELECT * FROM USERS WHERE name = %Q;", userName);
pthread_rwlock_rdlock(&RW_lock);
rc = sqlite3_exec(*db, sql, verifyPasswordCallback, passwordInfo, &errMsg);
pthread_rwlock_unlock(&RW_lock);
getHash(hash, password, passwordInfo->salt);
if(strcmp(hash, passwordInfo->hash) == 0) {
verified = TRUE;
}
sqlite3_free(sql);
if(rc != SQLITE_OK) {
printError("Can't execute query\n");
sqlite3_close(*db);
exit(EXIT_FAILURE);
}
free(passwordInfo);
return verified;
}
void insertAccount(sqlite3 **db, char *userName, char *hash, char *salt) {
int rc;
char *sql = NULL;
char *errMsg;
sql = sqlite3_mprintf("INSERT INTO USERS VALUES(NULL, %Q, %Q, %Q);", userName, hash, salt);
pthread_rwlock_wrlock(&RW_lock);
rc = sqlite3_exec(*db, sql, NULL, NULL, &errMsg);
pthread_rwlock_unlock(&RW_lock);
sqlite3_free(sql);
if(rc != SQLITE_OK) {
printError("Can't insert entry\n");
return;
}
}
void printAllAccountsInfo(sqlite3 **db) {
int rc;
char *sql = NULL;
char *errMsg;
sql = "SELECT * FROM USERS";
pthread_rwlock_rdlock(&RW_lock);
rc = sqlite3_exec(*db, sql, printAllAccountsInfoCallback, NULL, &errMsg);
pthread_rwlock_unlock(&RW_lock);
if(rc != SQLITE_OK) {
printError("Can't insert entry\n");
return;
}
}
void getSalt(char *salt) {
unsigned char digest[MAX_SALT_LEN];
int i;
RAND_bytes(digest, MAX_SALT_LEN);
for(i=0; i<MAX_SALT_LEN; i++) {
sprintf(&salt[i*2], "%02x", (unsigned int)digest[i]);
}
}
void getHash(char *hash, char *password, char *salt) {
SHA256_CTX c;
unsigned char digest[SHA256_DIGEST_LENGTH];
int i;
for(i=0; i<strlen(salt); i++) {
password[i] = password[i] & salt[i];
}
SHA256_Init(&c);
SHA256_Update(&c, password, strlen(password));
SHA256_Final(digest, &c);
for (i=0; i<SHA256_DIGEST_LENGTH; i++) {
sprintf(&hash[i*2], "%02x", (unsigned int)digest[i]);
}
}
int isAccountExistCallback(void *data, int argc, char **argv, char **azColName) {
if(strcmp(argv[0], "0") == 0) {
*(int *)data = FALSE;
} else {
*(int *)data = TRUE;
}
return 0;
}
int verifyPasswordCallback(void *data, int argc, char **argv, char **azColName) {
PasswordInfo *passwordInfo = (PasswordInfo *)data;
strcpy(passwordInfo->hash, argv[2]);
strcpy(passwordInfo->salt, argv[3]);
return 0;
}
int printAllAccountsInfoCallback(void *data, int argc, char **argv, char **azColName) {
printf("USERNAME: %s\nPASSWORD: %-20s\n\n", argv[1], argv[2]);
return 0;
}