Subversion Repositories RepoView

Rev

Rev 3 | Rev 7 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 5
Line 1... Line 1...
1
/* $Id: sqlite.c 3 2024-08-20 21:05:24Z nishi $ */
1
/* $Id: sqlite.c 5 2024-08-20 22:43:56Z nishi $ */
2
 
2
 
3
#include "rv_db.h"
3
#include "rv_db.h"
4
 
4
 
5
#include "../../config.h"
5
#include "../../config.h"
6
 
6
 
7
#include "rv_util.h"
7
#include "rv_util.h"
-
 
8
#include "rv_sha512.h"
8
 
9
 
9
#include <sqlite3.h>
10
#include <sqlite3.h>
10
 
11
 
-
 
12
#include <string.h>
11
#include <stdio.h>
13
#include <stdio.h>
12
#include <stdlib.h>
14
#include <stdlib.h>
13
#include <stdbool.h>
15
#include <stdbool.h>
14
 
16
 
15
sqlite3* sql;
17
sqlite3* sql;
Line 41... Line 43...
41
 
43
 
42
void rv_close_db(void) { sqlite3_close(sql); }
44
void rv_close_db(void) { sqlite3_close(sql); }
43
 
45
 
44
int count = 0;
46
int count = 0;
45
 
47
 
-
 
48
struct user {
-
 
49
	char* username;
-
 
50
	char* password;
-
 
51
	bool valid;
-
 
52
};
-
 
53
 
46
int sqlcount(void* param, int ncol, char** row, char** col) {
54
int sqlcount(void* param, int ncol, char** row, char** col) {
47
	count = ncol;
55
	count = ncol;
48
	fprintf(stderr, "%d\n", ncol);
-
 
49
	return 0;
56
	return 0;
50
}
57
}
51
 
58
 
-
 
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
 
52
bool rv_has_user(const char* username) {
71
char* escape_sql(const char* input) {
-
 
72
	char* query = malloc(1);
53
	char* err;
73
	query[0] = 0;
54
	char cbuf[2];
74
	char cbuf[2];
55
	cbuf[1] = 0;
75
	cbuf[1] = 0;
56
	char* query = rv_strdup("select * from users where user = '");
-
 
57
	int i;
76
	int i;
58
	for(i = 0; username[i] != 0; i++) {
77
	for(i = 0; input[i] != 0; i++) {
59
		if(username[i] == '\'') {
78
		if(input[i] == '\'') {
60
			cbuf[0] = username[i];
79
			cbuf[0] = input[i];
61
			char* tmp = query;
80
			char* tmp = query;
62
			tmp = rv_strcat(tmp, cbuf);
81
			tmp = rv_strcat(tmp, cbuf);
63
			free(tmp);
82
			free(tmp);
64
			cbuf[0] = username[i];
83
			cbuf[0] = input[i];
65
			tmp = query;
84
			tmp = query;
66
			query = rv_strcat(tmp, cbuf);
85
			query = rv_strcat(tmp, cbuf);
67
			free(tmp);
86
			free(tmp);
68
		} else {
87
		} else {
69
			cbuf[0] = username[i];
88
			cbuf[0] = input[i];
70
			char* tmp = query;
89
			char* tmp = query;
71
			query = rv_strcat(tmp, cbuf);
90
			query = rv_strcat(tmp, cbuf);
72
			free(tmp);
91
			free(tmp);
73
		}
92
		}
74
	}
93
	}
75
	char* tmp = query;
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, "', '");
76
	query = rv_strcat(tmp, "'");
119
	char* query = rv_strcat3(tmp, token, "')");
77
	free(tmp);
120
	free(tmp);
-
 
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, "'");
-
 
142
	int ret;
-
 
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;
-
 
160
	ret = sqlite3_exec(sql, query, sqlcount, NULL, &err);
-
 
161
	free(query);
-
 
162
	if(ret != SQLITE_OK) {
-
 
163
		sqlite3_free(err);
-
 
164
	}
-
 
165
	return count > 0;
-
 
166
}
-
 
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);
78
	int ret;
176
	int ret;
79
	fprintf(stderr, "%s\n", query);
-
 
80
	ret = sqlite3_exec(sql, query, sqlcount, NULL, &err);
177
	ret = sqlite3_exec(sql, query, sqlcount, NULL, &err);
81
	free(query);
178
	free(query);
82
	if(ret != SQLITE_OK) {
179
	if(ret != SQLITE_OK) {
83
		sqlite3_free(err);
180
		sqlite3_free(err);
84
	}
181
	}