annotate sqlickrypt.c @ 18:59ea628abb81

sqlickrypt.c: add the ability to register new users. When sqlickrypt is run with the option "register", it expects a username, password, and email, and adds them to the database if the username is not already in use.
author John "Elwin" Edwards <elwin@sdf.org>
date Mon, 21 May 2012 21:40:56 -0700
parents d3e3d6b4016b
children 9d5da43c0e83
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
12 int xcallback(void *targ, int ncols, char **vals, char **colnames) {
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
13 char *pws;
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
14 int j;
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
15 pws = *((char **) targ);
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
16 if (pws == NULL) {
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
17 for (j = 0; j < ncols; j++) {
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
18 if (!strcmp(colnames[j], "password"))
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
19 *((char **) targ) = strdup(vals[j]);
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
20 }
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
21 }
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
22 /* Otherwise, this isn't the first row. */
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
23 return 0;
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
24 }
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
25
18
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
26 /* Simple callback, for checking if there are any matches. */
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
27 int searchcallback(void *targ, int ncols, char **vals, char **colnames) {
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
28 *((int *) targ) = 1;
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
29 return 0;
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
30 }
17
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
31
18
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
32 int check(char *uname, char *pw) {
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
33 char finduser_sql[160];
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
34 char *pwhash = NULL, *comphash;
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
35 int status;
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
36 sqlite3 *db;
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
37
17
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
38 strcpy(finduser_sql, "SELECT * FROM dglusers WHERE username='");
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
39 strncat(finduser_sql, uname, 40);
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
40 strcat(finduser_sql, "';");
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
41
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
42 status = sqlite3_open(DATABASE, &db);
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
43 if (status) {
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
44 sqlite3_close(db);
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
45 return 1;
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
46 }
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
47 sqlite3_exec(db, finduser_sql, xcallback, (void *) &pwhash, NULL);
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
48
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
49 sqlite3_close(db);
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
50 /* Now check the password. */
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
51 if (pwhash == NULL) {
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
52 return 2;
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
53 }
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
54 comphash = crypt(pw, pwhash);
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
55 if (!strcmp(pwhash, comphash))
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
56 return 0;
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
57 return 1;
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
58 }
18
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
59
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
60 int insertuser(char *uname, char *pw, char *email) {
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
61 char finduser_sql[160];
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
62 int status;
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
63 sqlite3 *db;
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 strcpy(finduser_sql, "BEGIN; SELECT * FROM dglusers WHERE username='");
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
66 strncat(finduser_sql, uname, 40);
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
67 strcat(finduser_sql, "';");
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
68
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
69 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
70 if (status) {
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
71 sqlite3_close(db);
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
72 return 1;
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
73 }
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
74 status = 0;
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
75 sqlite3_exec(db, finduser_sql, searchcallback, (void *) &status, NULL);
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
76 if (!status) {
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
77 /* FIXME This is ugly, and email is unsanitzed. */
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
78 strcpy(finduser_sql, "INSERT INTO dglusers (username, password, email) VALUES ('");
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
79 strncat(finduser_sql, uname, 20);
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
80 strcat(finduser_sql, "', '");
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
81 strcat(finduser_sql, crypt(pw, pw));
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
82 strcat(finduser_sql, "', '");