annotate sqlickrypt.c @ 123:0a3ff1267c24

sqlickrypt: fix typo in database filename.
author John "Elwin" Edwards <elwin@sdf.org>
date Mon, 13 Aug 2012 08:53:46 -0700
parents b1480488ce9d
children f1676e81c80a
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
123
0a3ff1267c24 sqlickrypt: fix typo in database filename.
John "Elwin" Edwards <elwin@sdf.org>
parents: 119
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
117
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
12 /* General idea for return status:
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
13 * 0: success
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
14 * 1: password check failed
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
15 * 2: username not found
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
16 * 3: database error
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
17 * 4: invalid input
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
18 */
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
19
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
20 /* Opens the database and, less obviously, initializes a statement. */
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
21 int opendb(sqlite3 **dbp, sqlite3_stmt **stmtp, char *query) {
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
22 int status;
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
23 status = sqlite3_open(DATABASE, dbp);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
24 if (status) {
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
25 sqlite3_close(*dbp);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
26 exit(3);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
27 }
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
28 sqlite3_prepare_v2(*dbp, query, -1, stmtp, NULL);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
29 if (*stmtp == NULL) {
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
30 sqlite3_close(*dbp);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
31 exit(3);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
32 }
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
33 return 0;
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
34 }
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
35
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
36 void cleanup(sqlite3 *db, sqlite3_stmt *stmt, int status) {
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
37 if (stmt)
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
38 sqlite3_finalize(stmt);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
39 sqlite3_close(db);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
40 if (status)
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
41 exit(status);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
42 return;
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
43 }
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
44
18
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
45 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
46 char *pwhash, *comphash;
9d5da43c0e83 sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
47 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
48 int status;
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
49 sqlite3 *db;
24
9d5da43c0e83 sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
50 sqlite3_stmt *qstmt;
17
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
51
117
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
52 opendb(&db, &qstmt, query);
24
9d5da43c0e83 sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
53 status = sqlite3_bind_text(qstmt, 1, uname, -1, SQLITE_TRANSIENT);
117
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
54 if (status)
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
55 cleanup(db, qstmt, 3);
24
9d5da43c0e83 sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
56 status = sqlite3_step(qstmt);
9d5da43c0e83 sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
57 if (status != SQLITE_ROW) {
9d5da43c0e83 sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
58 sqlite3_finalize(qstmt);
9d5da43c0e83 sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
59 sqlite3_close(db);
9d5da43c0e83 sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
60 if (status == SQLITE_DONE)
9d5da43c0e83 sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
61 return 2; /* User not found */
9d5da43c0e83 sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
62 return 3;
9d5da43c0e83 sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
63 }
9d5da43c0e83 sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
64 pwhash = strdup((char *) sqlite3_column_text(qstmt, 0));
117
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
65 cleanup(db, qstmt, 0);
24
9d5da43c0e83 sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
66
9d5da43c0e83 sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
67 /* Check the password */
17
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
68 comphash = crypt(pw, pwhash);
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
69 if (!strcmp(pwhash, comphash))
24
9d5da43c0e83 sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
70 status = 0;
9d5da43c0e83 sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
71 else
9d5da43c0e83 sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
72 status = 1;
9d5da43c0e83 sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
73 free(pwhash);
9d5da43c0e83 sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 18
diff changeset
74 return status;
17
d3e3d6b4016b rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff changeset
75 }
18
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
76
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
77 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
78 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
79 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
80 int status;
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
81 sqlite3 *db;
25
f275d816e857 sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 24
diff changeset
82 sqlite3_stmt *qstmt;
18
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
83
117
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
84 opendb(&db, &qstmt, checkquery);
25
f275d816e857 sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 24
diff changeset
85 /* 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
86 status = sqlite3_exec(db, "BEGIN;", NULL, NULL, NULL);
117
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
87 if (status)
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
88 cleanup(db, qstmt, 3);
25
f275d816e857 sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 24
diff changeset
89 sqlite3_bind_text(qstmt, 1, uname, -1, SQLITE_TRANSIENT);
f275d816e857 sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 24
diff changeset
90 status = sqlite3_step(qstmt);
f275d816e857 sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 24
diff changeset
91 if (status != SQLITE_DONE) {
f275d816e857 sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 24
diff changeset
92 sqlite3_finalize(qstmt);
f275d816e857 sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 24
diff changeset
93 sqlite3_close(db);
f275d816e857 sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 24
diff changeset
94 if (status == SQLITE_ROW)
f275d816e857 sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 24
diff changeset
95 return 1; /* User already exists */
f275d816e857 sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 24
diff changeset
96 return 3;
18
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
97 }
25
f275d816e857 sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 24
diff changeset
98 /* The username doesn't exist yet, so create a new account. */
f275d816e857 sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 24
diff changeset
99 sqlite3_finalize(qstmt);
f275d816e857 sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 24
diff changeset
100 sqlite3_prepare_v2(db, addquery, -1, &qstmt, NULL);
117
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
101 if (qstmt == NULL)
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
102 cleanup(db, NULL, 3);
25
f275d816e857 sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 24
diff changeset
103 sqlite3_bind_text(qstmt, 1, uname, -1, SQLITE_TRANSIENT);
f275d816e857 sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 24
diff changeset
104 sqlite3_bind_text(qstmt, 2, strdup(crypt(pw, pw)), -1, free);
f275d816e857 sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 24
diff changeset
105 sqlite3_bind_text(qstmt, 3, email, -1, SQLITE_TRANSIENT);
f275d816e857 sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 24
diff changeset
106 status = sqlite3_step(qstmt);
117
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
107 if (status != SQLITE_DONE)
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
108 cleanup(db, qstmt, 3);
25
f275d816e857 sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents: 24
diff changeset
109 status = sqlite3_exec(db, "COMMIT;", NULL, NULL, NULL);
117
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
110 cleanup(db, qstmt, 0);
18
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
111 return status;
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
112 }
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
113
117
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
114 int getmail(char *uname) {
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
115 char *mailquery = "SELECT email FROM dglusers WHERE username = ?;";
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
116 int status;
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
117 sqlite3 *db;
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
118 sqlite3_stmt *qstmt;
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
119
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
120 opendb(&db, &qstmt, mailquery);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
121 status = sqlite3_bind_text(qstmt, 1, uname, -1, SQLITE_TRANSIENT);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
122 if (status)
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
123 cleanup(db, qstmt, 3);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
124 status = sqlite3_step(qstmt);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
125 if (status != SQLITE_ROW) {
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
126 sqlite3_finalize(qstmt);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
127 sqlite3_close(db);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
128 if (status == SQLITE_DONE)
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
129 return 2; /* User not found */
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
130 return 3;
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
131 }
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
132 printf("%s\n", sqlite3_column_text(qstmt, 0));
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
133 cleanup(db, qstmt, 0);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
134 return 0;
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
135 }
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
136
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
137 int setmail(char *uname, char *newmail) {
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
138 char *setquery = "UPDATE dglusers SET email = ? WHERE username = ?;";
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
139 sqlite3 *db;
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
140 sqlite3_stmt *qstmt;
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
141 int status;
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
142
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
143 opendb(&db, &qstmt, setquery);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
144 status = sqlite3_bind_text(qstmt, 2, uname, -1, SQLITE_TRANSIENT);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
145 if (status)
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
146 cleanup(db, qstmt, 3);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
147 status = sqlite3_bind_text(qstmt, 1, newmail, -1, SQLITE_TRANSIENT);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
148 if (status)
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
149 cleanup(db, qstmt, 3);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
150 status = sqlite3_step(qstmt);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
151 if (status != SQLITE_DONE)
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
152 cleanup(db, qstmt, 3);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
153 cleanup(db, qstmt, 0);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
154 return 0;
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
155 }
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
156
119
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
157 int setpw(char *uname, char *newpw) {
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
158 char *setquery = "UPDATE dglusers SET password = ? WHERE username = ?;";
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
159 sqlite3 *db;
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
160 sqlite3_stmt *qstmt;
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
161 int status;
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
162 char *hash;
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
163
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
164 /* This is not a smart use of crypt, but it needs to be compatible with
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
165 * dgamelaunch. */
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
166 hash = crypt(newpw, newpw);
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
167 opendb(&db, &qstmt, setquery);
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
168 status = sqlite3_bind_text(qstmt, 2, uname, -1, SQLITE_TRANSIENT);
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
169 if (status)
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
170 cleanup(db, qstmt, 3);
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
171 status = sqlite3_bind_text(qstmt, 1, hash, -1, SQLITE_TRANSIENT);
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
172 if (status)
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
173 cleanup(db, qstmt, 3);
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
174 status = sqlite3_step(qstmt);
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
175 if (status != SQLITE_DONE)
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
176 cleanup(db, qstmt, 3);
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
177 cleanup(db, qstmt, 0);
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
178 return 0;
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
179 }
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
180
18
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
181 int main(int argc, char *argv[]) {
117
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
182 char ibuf[IBUFSIZE], *uname, *line1, *line2;
18
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
183 char *cptr; // Utility pointer
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
184 int status;
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
185
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
186 /* Read in the username and password */
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
187 fgets(ibuf, IBUFSIZE, stdin);
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
188 uname = ibuf;
117
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
189 line1 = strchr(uname, '\n');
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
190 if (line1 == NULL)
18
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
191 exit(4); /* Truncated */
117
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
192 *line1 = '\0';
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
193 if (argc == 1 || strcmp(argv[1], "getmail")) {
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
194 line1++;
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
195 fgets(line1, IBUFSIZE - (line1 - ibuf), stdin);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
196 if (line1[strlen(line1) - 1] == '\n')
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
197 line1[strlen(line1) - 1] = '\0';
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
198 else
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
199 exit(4); /* Truncated */
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
200 }
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
201 if (argc > 1 && !strcmp(argv[1], "setmail")) {
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
202 /* E-mail, sanitize */
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
203 for (cptr = line1; *cptr != '\0'; cptr++) {
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
204 if (!isalnum(*cptr) && !strchr("@._-", *cptr)) {
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
205 exit(4);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
206 }
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
207 }
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
208 }
18
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
209 if (argc > 1 && !strcmp(argv[1], "register")) {
117
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
210 line2 = line1 + strlen(line1) + 1;
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
211 fgets(line2, IBUFSIZE - (line2 - ibuf), stdin);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
212 if (line2[strlen(line2) - 1] == '\n')
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
213 line2[strlen(line2) - 1] = '\0';
18
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
214 else
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
215 exit(4);
117
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
216 for (cptr = line2; *cptr != '\0'; cptr++) {
18
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
217 if (!isalnum(*cptr) && !strchr("@._-", *cptr)) {
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
218 exit(4);
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
219 }
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
220 }
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
221 }
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
222 /* Sanitize the username, because it gets put into a query. */
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
223 for (cptr = uname; *cptr != '\0'; cptr++) {
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
224 if (!isalnum(*cptr)) {
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
225 exit(4);
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
226 }
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
227 }
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
228 if (argc == 1 || !strcmp(argv[1], "check"))
117
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
229 status = check(uname, line1);
18
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
230 else if (!strcmp(argv[1], "register")) {
117
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
231 status = insertuser(uname, line1, line2);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
232 }
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
233 else if (!strcmp(argv[1], "getmail")) {
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
234 status = getmail(uname);
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
235 }
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
236 else if (!strcmp(argv[1], "setmail")) {
c08717cb7793 sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents: 25
diff changeset
237 status = setmail(uname, line1);
18
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
238 }
119
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
239 else if (!strcmp(argv[1], "setpw")) {
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
240 status = setpw(uname, line1);
b1480488ce9d sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents: 117
diff changeset
241 }
18
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
242 else
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
243 status = 127;
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
244 return status;
59ea628abb81 sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents: 17
diff changeset
245 }