Mercurial > hg > rlgwebd
annotate sqlickrypt.c @ 143:f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
Passwords will now be securely encrypted with random salt. Also avoid
storing NULL in the database, because that makes dgamelaunch segfault.
author | John "Elwin" Edwards |
---|---|
date | Sun, 20 Oct 2013 21:19:13 -0700 |
parents | 0a3ff1267c24 |
children | bc69717ff386 |
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 |
117
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
121 opendb(&db, &qstmt, checkquery); |
25
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
122 /* 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
|
123 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
|
124 if (status) |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
125 cleanup(db, qstmt, 3); |
25
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
126 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
|
127 status = sqlite3_step(qstmt); |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
128 if (status != SQLITE_DONE) { |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
129 sqlite3_finalize(qstmt); |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
130 sqlite3_close(db); |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
131 if (status == SQLITE_ROW) |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
132 return 1; /* User already exists */ |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
133 return 3; |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
134 } |
25
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
135 /* 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
|
136 setsalt(salt); |
25
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
137 sqlite3_finalize(qstmt); |
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
138 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
|
139 if (qstmt == NULL) |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
140 cleanup(db, NULL, 3); |
25
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
141 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
|
142 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
|
143 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
|
144 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
|
145 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
|
146 status = sqlite3_step(qstmt); |
117
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
147 if (status != SQLITE_DONE) |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
148 cleanup(db, qstmt, 3); |
25
f275d816e857
sqlickrypt.c: finish switching to parametrized queries.
John "Elwin" Edwards <elwin@sdf.org>
parents:
24
diff
changeset
|
149 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
|
150 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
|
151 return status; |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
152 } |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
153 |
117
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
154 int getmail(char *uname) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
155 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
|
156 int status; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
157 sqlite3 *db; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
158 sqlite3_stmt *qstmt; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
159 |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
160 opendb(&db, &qstmt, mailquery); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
161 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
|
162 if (status) |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
163 cleanup(db, qstmt, 3); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
164 status = sqlite3_step(qstmt); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
165 if (status != SQLITE_ROW) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
166 sqlite3_finalize(qstmt); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
167 sqlite3_close(db); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
168 if (status == SQLITE_DONE) |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
169 return 2; /* User not found */ |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
170 return 3; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
171 } |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
172 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
|
173 cleanup(db, qstmt, 0); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
174 return 0; |
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 |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
177 int setmail(char *uname, char *newmail) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
178 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
|
179 sqlite3 *db; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
180 sqlite3_stmt *qstmt; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
181 int status; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
182 |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
183 opendb(&db, &qstmt, setquery); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
184 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
|
185 if (status) |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
186 cleanup(db, qstmt, 3); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
187 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
|
188 if (status) |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
189 cleanup(db, qstmt, 3); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
190 status = sqlite3_step(qstmt); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
191 if (status != SQLITE_DONE) |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
192 cleanup(db, qstmt, 3); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
193 cleanup(db, qstmt, 0); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
194 return 0; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
195 } |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
196 |
119
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
197 int setpw(char *uname, char *newpw) { |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
198 char *setquery = "UPDATE dglusers SET password = ? WHERE username = ?;"; |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
199 sqlite3 *db; |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
200 sqlite3_stmt *qstmt; |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
201 int status; |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
202 char *hash; |
143
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
203 char salt[20]; |
119
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
204 |
143
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
205 setsalt(salt); |
f1676e81c80a
sqlickrypt: add support for salted SHA-512 passwords, and fix NULL bug.
John "Elwin" Edwards
parents:
123
diff
changeset
|
206 hash = crypt(newpw, salt); |
119
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
207 opendb(&db, &qstmt, setquery); |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
208 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
|
209 if (status) |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
210 cleanup(db, qstmt, 3); |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
211 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
|
212 if (status) |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
213 cleanup(db, qstmt, 3); |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
214 status = sqlite3_step(qstmt); |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
215 if (status != SQLITE_DONE) |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
216 cleanup(db, qstmt, 3); |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
217 cleanup(db, qstmt, 0); |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
218 return 0; |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
219 } |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
220 |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
221 int main(int argc, char *argv[]) { |
117
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
222 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
|
223 char *cptr; // Utility pointer |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
224 int status; |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
225 |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
226 /* 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
|
227 fgets(ibuf, IBUFSIZE, stdin); |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
228 uname = ibuf; |
117
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
229 line1 = strchr(uname, '\n'); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
230 if (line1 == NULL) |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
231 exit(4); /* Truncated */ |
117
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
232 *line1 = '\0'; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
233 if (argc == 1 || strcmp(argv[1], "getmail")) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
234 line1++; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
235 fgets(line1, IBUFSIZE - (line1 - ibuf), stdin); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
236 if (line1[strlen(line1) - 1] == '\n') |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
237 line1[strlen(line1) - 1] = '\0'; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
238 else |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
239 exit(4); /* Truncated */ |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
240 } |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
241 if (argc > 1 && !strcmp(argv[1], "setmail")) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
242 /* E-mail, sanitize */ |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
243 for (cptr = line1; *cptr != '\0'; cptr++) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
244 if (!isalnum(*cptr) && !strchr("@._-", *cptr)) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
245 exit(4); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
246 } |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
247 } |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
248 } |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
249 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
|
250 line2 = line1 + strlen(line1) + 1; |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
251 fgets(line2, IBUFSIZE - (line2 - ibuf), stdin); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
252 if (line2[strlen(line2) - 1] == '\n') |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
253 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
|
254 else |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
255 exit(4); |
117
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
256 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
|
257 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
|
258 exit(4); |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
259 } |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
260 } |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
261 } |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
262 /* 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
|
263 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
|
264 if (!isalnum(*cptr)) { |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
265 exit(4); |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
266 } |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
267 } |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
268 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
|
269 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
|
270 else if (!strcmp(argv[1], "register")) { |
117
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
271 status = insertuser(uname, line1, line2); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
272 } |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
273 else if (!strcmp(argv[1], "getmail")) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
274 status = getmail(uname); |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
275 } |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
276 else if (!strcmp(argv[1], "setmail")) { |
c08717cb7793
sqlickrypt: add getmail and setmail commands.
John "Elwin" Edwards <elwin@sdf.org>
parents:
25
diff
changeset
|
277 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
|
278 } |
119
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
279 else if (!strcmp(argv[1], "setpw")) { |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
280 status = setpw(uname, line1); |
b1480488ce9d
sqlickrypt: add setpw command.
John "Elwin" Edwards <elwin@sdf.org>
parents:
117
diff
changeset
|
281 } |
18
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
282 else |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
283 status = 127; |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
284 return status; |
59ea628abb81
sqlickrypt.c: add the ability to register new users.
John "Elwin" Edwards <elwin@sdf.org>
parents:
17
diff
changeset
|
285 } |