Subversion Repositories RepoView

Rev

Rev 3 | Rev 7 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 nishi 1
/* $Id: sqlite.c 5 2024-08-20 22:43:56Z nishi $ */
2
 
3
#include "rv_db.h"
4
 
5
#include "../../config.h"
6
 
7
#include "rv_util.h"
5 nishi 8
#include "rv_sha512.h"
1 nishi 9
 
10
#include <sqlite3.h>
11
 
5 nishi 12
#include <string.h>
1 nishi 13
#include <stdio.h>
14
#include <stdlib.h>
15
#include <stdbool.h>
16
 
17
sqlite3* sql;
18
 
3 nishi 19
void rv_init_db(void) {
1 nishi 20
	int ret;
21
	ret = sqlite3_open(DB_ROOT "/db.sqlite3", &sql);
3 nishi 22
	if(ret != SQLITE_OK) {
1 nishi 23
		rv_error_http();
24
		printf("SQLite3 database error\n");
25
		exit(1);
26
	}
27
	char* err;
3 nishi 28
	ret = sqlite3_exec(sql, "create table if not exists users(user text, password text)", NULL, NULL, &err);
29
	if(ret != SQLITE_OK) {
1 nishi 30
		sqlite3_free(err);
31
		rv_error_http();
32
		printf("SQLite3 database error\n");
33
		exit(1);
34
	}
3 nishi 35
	ret = sqlite3_exec(sql, "create table if not exists tokens(user text, token text)", NULL, NULL, &err);
36
	if(ret != SQLITE_OK) {
1 nishi 37
		sqlite3_free(err);
38
		rv_error_http();
39
		printf("SQLite3 database error\n");
40
		exit(1);
41
	}
42
}
43
 
3 nishi 44
void rv_close_db(void) { sqlite3_close(sql); }
1 nishi 45
 
46
int count = 0;
47
 
5 nishi 48
struct user {
49
	char* username;
50
	char* password;
51
	bool valid;
52
};
53
 
3 nishi 54
int sqlcount(void* param, int ncol, char** row, char** col) {
1 nishi 55
	count = ncol;
56
	return 0;
57
}
58
 
5 nishi 59
int sqlgetpasswd(void* param, int ncol, char** row, char** col) {
60
	struct user* user = (struct user*)param;
61
	if(strcmp(row[0], user->username) == 0) {
62
		char* hash = rv_sha512(user->password);
63
		if(strcmp(row[1], hash) == 0) {
64
			user->valid = true;
65
		}
66
		free(hash);
67
	}
68
	return 0;
69
}
70
 
71
char* escape_sql(const char* input) {
72
	char* query = malloc(1);
73
	query[0] = 0;
1 nishi 74
	char cbuf[2];
75
	cbuf[1] = 0;
76
	int i;
5 nishi 77
	for(i = 0; input[i] != 0; i++) {
78
		if(input[i] == '\'') {
79
			cbuf[0] = input[i];
1 nishi 80
			char* tmp = query;
81
			tmp = rv_strcat(tmp, cbuf);
82
			free(tmp);
5 nishi 83
			cbuf[0] = input[i];
1 nishi 84
			tmp = query;
85
			query = rv_strcat(tmp, cbuf);
86
			free(tmp);
3 nishi 87
		} else {
5 nishi 88
			cbuf[0] = input[i];
1 nishi 89
			char* tmp = query;
90
			query = rv_strcat(tmp, cbuf);
91
			free(tmp);
92
		}
93
	}
5 nishi 94
	return query;
95
}
96
 
97
bool rv_check_password(const char* username, const char* password) {
98
	char* err;
99
	int ret;
100
	struct user user;
101
	user.username = (char*)username;
102
	user.password = (char*)password;
103
	user.valid = false;
104
	char* esc = escape_sql(username);
105
	char* query = rv_strcat3("select * from users where user = '", esc, "'");
106
	free(esc);
107
	ret = sqlite3_exec(sql, query, sqlgetpasswd, (void*)&user, &err);
108
	if(ret != SQLITE_OK) {
109
		sqlite3_free(err);
110
	}
111
	return user.valid;
112
}
113
 
114
void rv_save_token(const char* username, const char* token) {
115
	char* err;
116
	int ret;
117
	char* esc = escape_sql(username);
118
	char* tmp = rv_strcat3("insert into tokens values('", esc, "', '");
119
	char* query = rv_strcat3(tmp, token, "')");
1 nishi 120
	free(tmp);
5 nishi 121
	free(esc);
122
	ret = sqlite3_exec(sql, query, NULL, NULL, &err);
123
	free(query);
124
	if(ret != SQLITE_OK) {
125
		sqlite3_free(err);
126
	}
127
}
128
 
129
char* has_username;
130
 
131
int sqlget(void* param, int ncol, char** row, char** col) {
132
	has_username = rv_strdup(row[0]);
133
	return 0;
134
}
135
 
136
char* rv_who_has_token(const char* token) {
137
	char* err;
138
	char cbuf[2];
139
	cbuf[1] = 0;
140
	count = 0;
141
	char* query = rv_strcat3("select * from tokens where token = '", token, "'");
1 nishi 142
	int ret;
5 nishi 143
	has_username = NULL;
144
	ret = sqlite3_exec(sql, query, sqlget, (void*)token, &err);
145
	free(query);
146
	if(ret != SQLITE_OK) {
147
		sqlite3_free(err);
148
		return NULL;
149
	}
150
	return has_username;
151
}
152
 
153
bool rv_has_token(const char* token) {
154
	char* err;
155
	char cbuf[2];
156
	cbuf[1] = 0;
157
	count = 0;
158
	char* query = rv_strcat3("select * from tokens where token = '", token, "'");
159
	int ret;
3 nishi 160
	ret = sqlite3_exec(sql, query, sqlcount, NULL, &err);
1 nishi 161
	free(query);
3 nishi 162
	if(ret != SQLITE_OK) {
1 nishi 163
		sqlite3_free(err);
164
	}
165
	return count > 0;
166
}
5 nishi 167
 
168
bool rv_has_user(const char* username) {
169
	char* err;
170
	char cbuf[2];
171
	cbuf[1] = 0;
172
	count = 0;
173
	char* esc = escape_sql(username);
174
	char* query = rv_strcat3("select * from users where user = '", esc, "'");
175
	free(esc);
176
	int ret;
177
	ret = sqlite3_exec(sql, query, sqlcount, NULL, &err);
178
	free(query);
179
	if(ret != SQLITE_OK) {
180
		sqlite3_free(err);
181
	}
182
	return count > 0;
183
}