Mercurial > hg > rlgwebd
annotate sqlickrypt.c @ 170:50e4c9feeac2
RLGWebD: fix simultaneous player bug.
Multiple games can now run at the same time, and data will be sent to
the proper place. The interaction of multiple players with watchers
has not yet been tested.
author | John "Elwin" Edwards |
---|---|
date | Fri, 09 Jan 2015 13:06:41 -0500 |
parents | bc69717ff386 |
children |
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 |
143
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
11 #define RANDOMSRC "/dev/urandom" |
17
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
12 |
117
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
13 /* General idea for return status: |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
14 * 0: success |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
15 * 1: password check failed |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
16 * 2: username not found |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
17 * 3: database error |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
18 * 4: invalid input |
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 |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
21 /* 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
|
22 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
|
23 int status; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
24 status = sqlite3_open(DATABASE, dbp); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
25 if (status) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
26 sqlite3_close(*dbp); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
27 exit(3); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
28 } |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
29 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
|
30 if (*stmtp == NULL) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
31 sqlite3_close(*dbp); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
32 exit(3); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
33 } |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
34 return 0; |
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 |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
37 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
|
38 if (stmt) |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
39 sqlite3_finalize(stmt); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
40 sqlite3_close(db); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
41 if (status) |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
42 exit(status); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
43 return; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
44 } |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
45 |
143
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
46 char encode64(unsigned char c) { |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
47 if (c < 26) |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
48 return 'a' + c; |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
49 else if (c < 52) |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
50 return 'A' + c - 26; |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
51 else if (c < 62) |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
52 return '0' + c - 52; |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
53 else if (c == 62) |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
54 return '.'; |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
55 else |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
56 return '/'; |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
57 } |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
58 |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
59 /* Initializes a SHA-512 salt. salt must contain at least 20 bytes. */ |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
60 void setsalt(char *salt) { |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
61 unsigned char rnbytes[3], rnvals[4]; |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
62 FILE *urandom; |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
63 int loop; |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
64 salt[0] = salt[2] = salt[19] = '$'; /* Delimiters */ |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
65 salt[1] = '6'; /* SHA-512 */ |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
66 urandom = fopen(RANDOMSRC, "r"); |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
67 for (loop = 0; loop < 4; loop++) { |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
68 fread(rnbytes, 1, 3, urandom); |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
69 rnvals[0] = rnbytes[0] >> 2; |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
70 rnvals[1] = ((rnbytes[0] & 0x03) << 4) | ((rnbytes[1] & 0xf0) >> 4); |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
71 rnvals[2] = ((rnbytes[1] & 0x0f) << 2) | ((rnbytes[2] & 0xc0) >> 6); |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
72 rnvals[3] = rnbytes[2] & 0x3f; |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
73 salt[loop * 4 + 3] = encode64(rnvals[0]); |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
74 salt[loop * 4 + 4] = encode64(rnvals[1]); |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
75 salt[loop * 4 + 5] = encode64(rnvals[2]); |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
76 salt[loop * 4 + 6] = encode64(rnvals[3]); |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
77 } |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
78 fclose(urandom); |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
79 } |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
80 |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
81 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
|
82 char *pwhash, *comphash; |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
83 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
|
84 int status; |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
85 sqlite3 *db; |
24
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
86 sqlite3_stmt *qstmt; |
17
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
87 |
117
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
88 opendb(&db, &qstmt, query); |
24
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
89 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
|
90 if (status) |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
91 cleanup(db, qstmt, 3); |
24
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
92 status = sqlite3_step(qstmt); |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
93 if (status != SQLITE_ROW) { |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
94 sqlite3_finalize(qstmt); |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
95 sqlite3_close(db); |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
96 if (status == SQLITE_DONE) |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
97 return 2; /* User not found */ |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
98 return 3; |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
99 } |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
100 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
|
101 cleanup(db, qstmt, 0); |
24
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
102 |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
103 /* Check the password */ |
17
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
104 comphash = crypt(pw, pwhash); |
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
105 if (!strcmp(pwhash, comphash)) |
24
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
106 status = 0; |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
107 else |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
108 status = 1; |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
109 free(pwhash); |
9d5da43c0e83
sqlickrypt.c: begin converting to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
18
diff
changeset
|
110 return status; |
17
d3e3d6b4016b
rlgwebd: switch to dgamelaunch's SQLite database.
John "Elwin" Edwards <elwin@sdf.org>
parents:
diff
changeset
|
111 } |
18
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 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
|
114 char *checkquery = "SELECT * FROM dglusers WHERE username = ?;"; |
143
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
115 char *addquery = "INSERT INTO dglusers (username, password, email, flags, env) VALUES (?, ?, ?, ?, ?);"; |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
116 int status; |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
117 sqlite3 *db; |
25
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
118 sqlite3_stmt *qstmt; |
143
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
119 char salt[20]; |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
120 |
152 | 121 if (strlen(uname) > 20) { |
122 return 4; | |
123 } | |
124 | |
117
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
125 opendb(&db, &qstmt, checkquery); |
25
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
126 /* 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
|
127 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
|
128 if (status) |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
129 cleanup(db, qstmt, 3); |
25
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
130 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
|
131 status = sqlite3_step(qstmt); |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
132 if (status != SQLITE_DONE) { |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
133 sqlite3_finalize(qstmt); |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
134 sqlite3_close(db); |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
135 if (status == SQLITE_ROW) |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
136 return 1; /* User already exists */ |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
137 return 3; |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
138 } |
25
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
139 /* The username doesn't exist yet, so create a new account. */ |
143
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
140 setsalt(salt); |
25
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
141 sqlite3_finalize(qstmt); |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
142 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
|
143 if (qstmt == NULL) |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
144 cleanup(db, NULL, 3); |
25
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
145 sqlite3_bind_text(qstmt, 1, uname, -1, SQLITE_TRANSIENT); |
143
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
146 sqlite3_bind_text(qstmt, 2, strdup(crypt(pw, salt)), -1, free); |
25
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
147 sqlite3_bind_text(qstmt, 3, email, -1, SQLITE_TRANSIENT); |
143
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
148 sqlite3_bind_int(qstmt, 4, 0); |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
149 sqlite3_bind_text(qstmt, 5, "", -1, SQLITE_STATIC); |
25
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
150 status = sqlite3_step(qstmt); |
117
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); |
25
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
153 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
|
154 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
|
155 return status; |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
156 } |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
157 |
117
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
158 int getmail(char *uname) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
159 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
|
160 int status; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
161 sqlite3 *db; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
162 sqlite3_stmt *qstmt; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
163 |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
164 opendb(&db, &qstmt, mailquery); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
165 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
|
166 if (status) |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
167 cleanup(db, qstmt, 3); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
168 status = sqlite3_step(qstmt); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
169 if (status != SQLITE_ROW) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
170 sqlite3_finalize(qstmt); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
171 sqlite3_close(db); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
172 if (status == SQLITE_DONE) |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
173 return 2; /* User not found */ |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
174 return 3; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
175 } |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
176 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
|
177 cleanup(db, qstmt, 0); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
178 return 0; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
179 } |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
180 |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
181 int setmail(char *uname, char *newmail) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
182 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
|
183 sqlite3 *db; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
184 sqlite3_stmt *qstmt; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
185 int status; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
186 |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
187 opendb(&db, &qstmt, setquery); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
188 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
|
189 if (status) |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
190 cleanup(db, qstmt, 3); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
191 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
|
192 if (status) |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
193 cleanup(db, qstmt, 3); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
194 status = sqlite3_step(qstmt); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
195 if (status != SQLITE_DONE) |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
196 cleanup(db, qstmt, 3); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
197 cleanup(db, qstmt, 0); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
198 return 0; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
199 } |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
200 |
119
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
201 int setpw(char *uname, char *newpw) { |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
202 char *setquery = "UPDATE dglusers SET password = ? WHERE username = ?;"; |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
203 sqlite3 *db; |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
204 sqlite3_stmt *qstmt; |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
205 int status; |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
206 char *hash; |
143
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
207 char salt[20]; |
119
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
208 |
143
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
209 setsalt(salt); |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
210 hash = crypt(newpw, salt); |
119
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
211 opendb(&db, &qstmt, setquery); |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
212 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
|
213 if (status) |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
214 cleanup(db, qstmt, 3); |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
215 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
|
216 if (status) |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
217 cleanup(db, qstmt, 3); |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
218 status = sqlite3_step(qstmt); |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
219 if (status != SQLITE_DONE) |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
220 cleanup(db, qstmt, 3); |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
221 cleanup(db, qstmt, 0); |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
222 return 0; |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
223 } |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
224 |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
225 int main(int argc, char *argv[]) { |
117
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
226 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
|
227 char *cptr; // Utility pointer |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
228 int status; |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
229 |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
230 /* 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
|
231 fgets(ibuf, IBUFSIZE, stdin); |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
232 uname = ibuf; |
117
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
233 line1 = strchr(uname, '\n'); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
234 if (line1 == NULL) |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
235 exit(4); /* Truncated */ |
117
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
236 *line1 = '\0'; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
237 if (argc == 1 || strcmp(argv[1], "getmail")) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
238 line1++; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
239 fgets(line1, IBUFSIZE - (line1 - ibuf), stdin); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
240 if (line1[strlen(line1) - 1] == '\n') |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
241 line1[strlen(line1) - 1] = '\0'; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
242 else |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
243 exit(4); /* Truncated */ |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
244 } |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
245 if (argc > 1 && !strcmp(argv[1], "setmail")) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
246 /* E-mail, sanitize */ |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
247 for (cptr = line1; *cptr != '\0'; cptr++) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
248 if (!isalnum(*cptr) && !strchr("@._-", *cptr)) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
249 exit(4); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
250 } |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
251 } |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
252 } |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
253 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
|
254 line2 = line1 + strlen(line1) + 1; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
255 fgets(line2, IBUFSIZE - (line2 - ibuf), stdin); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
256 if (line2[strlen(line2) - 1] == '\n') |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
257 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
|
258 else |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
259 exit(4); |
117
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
260 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
|
261 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
|
262 exit(4); |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
263 } |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
264 } |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
265 } |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
266 /* 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
|
267 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
|
268 if (!isalnum(*cptr)) { |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
269 exit(4); |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
270 } |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
271 } |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
272 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
|
273 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
|
274 else if (!strcmp(argv[1], "register")) { |
117
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
275 status = insertuser(uname, line1, line2); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
276 } |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
277 else if (!strcmp(argv[1], "getmail")) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
278 status = getmail(uname); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
279 } |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
280 else if (!strcmp(argv[1], "setmail")) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
281 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
|
282 } |
119
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
283 else if (!strcmp(argv[1], "setpw")) { |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
284 status = setpw(uname, line1); |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
285 } |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
286 else |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
287 status = 127; |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
288 return status; |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
289 } |