Mercurial > hg > rlgwebd
annotate sqlickrypt.c @ 93:104409bf5f03
rlgterm.js: improve registration failure messages.
Provide more user-friendly explanations when registration fails.
author | John "Elwin" Edwards <elwin@sdf.org> |
---|---|
date | Wed, 11 Jul 2012 07:37:56 -0700 |
parents | f275d816e857 |
children | c08717cb7793 |
rev | line source |
---|---|
17
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
1 #include <stdio.h> |
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
2 #include <stdlib.h> |
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
3 #include <string.h> |
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
4 #include <ctype.h> |
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
5 #include <sqlite3.h> |
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
6 #include <unistd.h> |
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
7 #include <crypt.h> |
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
8 |
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
9 #define DATABASE "/dgldir/dgamelaunch.db" |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
10 #define IBUFSIZE 200 |
17
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
11 |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
12 int check(char *uname, char *pw) { |
24
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
13 char *pwhash, *comphash; |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
14 char *query = "SELECT password FROM dglusers WHERE username=?;"; |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
15 int status; |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
16 sqlite3 *db; |
24
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
17 sqlite3_stmt *qstmt; |
17
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
18 |
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
19 status = sqlite3_open(DATABASE, &db); |
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
20 if (status) { |
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
21 sqlite3_close(db); |
24
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
22 return 3; |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
23 } |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
24 sqlite3_prepare_v2(db, query, -1, &qstmt, NULL); |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
25 if (qstmt == NULL) { |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
26 sqlite3_close(db); |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
27 return 3; |
17
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
28 } |
24
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
29 status = sqlite3_bind_text(qstmt, 1, uname, -1, SQLITE_TRANSIENT); |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
30 if (status) { |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
31 sqlite3_finalize(qstmt); |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
32 sqlite3_close(db); |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
33 return 3; |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
34 } |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
35 status = sqlite3_step(qstmt); |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
36 if (status != SQLITE_ROW) { |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
37 sqlite3_finalize(qstmt); |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
38 sqlite3_close(db); |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
39 if (status == SQLITE_DONE) |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
40 return 2; /* User not found */ |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
41 return 3; |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
42 } |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
43 pwhash = strdup((char *) sqlite3_column_text(qstmt, 0)); |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
44 /* Clean up */ |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
45 sqlite3_finalize(qstmt); |
17
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
46 sqlite3_close(db); |
24
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
47 |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
48 /* Check the password */ |
17
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
49 comphash = crypt(pw, pwhash); |
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
50 if (!strcmp(pwhash, comphash)) |
24
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
51 status = 0; |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
52 else |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
53 status = 1; |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
54 free(pwhash); |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
55 return status; |
17
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
56 } |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
57 |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
58 int insertuser(char *uname, char *pw, char *email) { |
25
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
59 char *checkquery = "SELECT * FROM dglusers WHERE username = ?;"; |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
60 char *addquery = "INSERT INTO dglusers (username, password, email) VALUES (?, ?, ?);"; |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
61 int status; |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
62 sqlite3 *db; |
25
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
63 sqlite3_stmt *qstmt; |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
64 |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
65 status = sqlite3_open(DATABASE, &db); |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
66 if (status) { |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
67 sqlite3_close(db); |
25
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
68 return 3; |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
69 } |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
70 /* Check for existing account in the same transaction with creating it. */ |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
71 status = sqlite3_exec(db, "BEGIN;", NULL, NULL, NULL); |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
72 if (status) { |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
73 sqlite3_close(db); |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
74 return 3; |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
75 } |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
76 sqlite3_prepare_v2(db, checkquery, -1, &qstmt, NULL); |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
77 if (qstmt == NULL) { |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
78 sqlite3_close(db); |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
79 return 3; |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org> |