-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.c
59 lines (47 loc) · 1.31 KB
/
auth.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
#include "common.h"
//for authentification
QUERY SELECT_PASSWORD_BY_NAME = "SELECT password, role FROM bank_personal WHERE login=?;";
enum roleInSystem role_in_system [] = { ROLE_UNKNOWN, ROLE_ADMIN, ROLE_OPERATOR };
struct currentUser cu;
void login()
{
int rc;
//char* username = (char*)malloc(sizeof(char) * 128);
//char* password = (char*)malloc(sizeof(char) * 128);
//login and password from db
char* db_password = (char*)malloc(sizeof(char) * 128);
int db_role = -1;
fprintf(stdout, "\nPlease, sign in\n");
for(;;)
{
fprintf(stdout, "\nlogin : ");
fscanf(stdin,"%s", cu.username);
fprintf(stdout, "password : ");
fscanf(stdin, "%s", cu.password);
if((rc=sqlite3_prepare(conn, SELECT_PASSWORD_BY_NAME, strlen(SELECT_PASSWORD_BY_NAME), &stmt, NULL)) == SQLITE_OK)
{
if(sqlite3_bind_text(stmt, 1, cu.username, -1, 0) == SQLITE_OK)
{
if(sqlite3_step(stmt) == SQLITE_ROW)
{
db_password = (char*)sqlite3_column_text(stmt, 0);
db_role = sqlite3_column_int(stmt, 1);
cu.roleIdentified = role_in_system[db_role];
//fprintf(stdout, "%s %d\n", db_password, db_role);
}
}
}
if(strcmp(cu.password, db_password) == 0)
{
break;
}
else
{
fprintf(stdout, "Incorrect login or password! Try again.\n");
}
}
}
void logout()
{
cu.roleIdentified = ROLE_UNKNOWN;
}