1 |
douglas |
1 |
// Douglas Thrift |
2 |
|
|
// |
3 |
|
|
// CCS Computer Science |
4 |
|
|
// Common Functions |
5 |
|
|
// |
6 |
|
|
// $Id$ |
7 |
|
|
|
8 |
douglas |
438 |
#include "mysql.h" |
9 |
douglas |
1 |
|
10 |
douglas |
438 |
#include <fcntl.h> |
11 |
|
|
#include <stdbool.h> |
12 |
douglas |
35 |
|
13 |
douglas |
575 |
MYSQL_STMT *mysqlstatement = NULL; |
14 |
|
|
|
15 |
douglas |
438 |
void mysqlcheck(int value, jmp_buf environment) |
16 |
douglas |
1 |
{ |
17 |
douglas |
438 |
if (value) |
18 |
|
|
longjmp(environment, MYSQL_EXCEPTION); |
19 |
douglas |
1 |
} |
20 |
|
|
|
21 |
douglas |
575 |
void mysqlstatementcheck(int value, MYSQL_STMT *statement, jmp_buf environment) |
22 |
|
|
{ |
23 |
|
|
if (value) |
24 |
|
|
{ |
25 |
|
|
mysqlstatement = statement; |
26 |
|
|
|
27 |
|
|
longjmp(environment, MYSQL_STATEMENT_EXCEPTION); |
28 |
|
|
} |
29 |
|
|
} |
30 |
|
|
|
31 |
douglas |
438 |
void mysqlpassword(MYSQL *mysql, char user[MAXLOGNAME], char password[_PASSWORD_LEN], jmp_buf environment) |
32 |
douglas |
1 |
{ |
33 |
douglas |
438 |
FILE *stream = fopen(SECRET, "r"); |
34 |
douglas |
1 |
|
35 |
douglas |
438 |
if (!stream) |
36 |
|
|
longjmp(environment, POSIX_EXCEPTION); |
37 |
douglas |
1 |
|
38 |
douglas |
438 |
char rootpassword[_PASSWORD_LEN] = ""; |
39 |
douglas |
1 |
|
40 |
douglas |
438 |
fcheck(fgets(rootpassword, _PASSWORD_LEN, stream), stream, environment); |
41 |
douglas |
1 |
|
42 |
douglas |
438 |
if (fclose(stream)) |
43 |
|
|
longjmp(environment, POSIX_EXCEPTION); |
44 |
douglas |
1 |
|
45 |
douglas |
438 |
if (!mysql_real_connect(mysql, NULL, "root", rootpassword, "mysql", 0, NULL, 0)) |
46 |
|
|
longjmp(environment, MYSQL_EXCEPTION); |
47 |
douglas |
1 |
|
48 |
douglas |
438 |
MYSQL_STMT *statement = mysql_stmt_init(mysql); |
49 |
douglas |
1 |
|
50 |
douglas |
438 |
if (!statement) |
51 |
|
|
longjmp(environment, MYSQL_EXCEPTION); |
52 |
douglas |
1 |
|
53 |
douglas |
575 |
char *select = "select count(User) from user where User = ?"; |
54 |
douglas |
1 |
|
55 |
douglas |
575 |
mysqlstatementcheck(mysql_stmt_prepare(statement, select, strlen(select)), statement, environment); |
56 |
douglas |
1 |
|
57 |
douglas |
575 |
size_t userlength = strlen(user); |
58 |
|
|
MYSQL_BIND parameter = { .buffer_type = MYSQL_TYPE_STRING, .buffer = user, .buffer_length = userlength, .length = &userlength, .is_null = NULL, .is_unsigned = false, .error = NULL, 0 }; |
59 |
douglas |
1 |
|
60 |
douglas |
575 |
mysqlstatementcheck(mysql_stmt_bind_param(statement, ¶meter), statement, environment); |
61 |
|
|
mysqlstatementcheck(mysql_stmt_execute(statement), statement, environment); |
62 |
douglas |
1 |
|
63 |
douglas |
575 |
my_ulonglong count; |
64 |
|
|
my_bool isnull, error; |
65 |
|
|
MYSQL_BIND result = { .buffer_type = MYSQL_TYPE_LONGLONG, .buffer = &count, .buffer_length = sizeof(count), .is_null = &isnull, .is_unsigned = true, .error = &error }; |
66 |
douglas |
1 |
|
67 |
douglas |
575 |
mysqlstatementcheck(mysql_stmt_bind_result(statement, &result), statement, environment); |
68 |
|
|
mysqlstatementcheck(mysql_stmt_fetch(statement), statement, environment); |
69 |
|
|
mysqlstatementcheck(mysql_stmt_close(statement), statement, environment); |
70 |
|
|
|
71 |
|
|
size_t passwordlength = strlen(password); |
72 |
|
|
|
73 |
|
|
if (count) |
74 |
douglas |
1 |
{ |
75 |
douglas |
575 |
statement = mysql_stmt_init(mysql); |
76 |
|
|
|
77 |
|
|
if (!statement) |
78 |
|
|
longjmp(environment, MYSQL_EXCEPTION); |
79 |
|
|
|
80 |
|
|
char *update = "update user set Password = PASSWORD(?) where User = ?"; |
81 |
|
|
|
82 |
|
|
mysqlstatementcheck(mysql_stmt_prepare(statement, update, strlen(update)), statement, environment); |
83 |
|
|
|
84 |
|
|
MYSQL_BIND parameters[2] = { { .buffer_type = MYSQL_TYPE_STRING, .buffer = password, .buffer_length = passwordlength, .length = &passwordlength, .is_null = NULL, .is_unsigned = false, .error = NULL, 0 }, { .buffer_type = MYSQL_TYPE_STRING, .buffer = user, .buffer_length = userlength, .length = &userlength, .is_null = NULL, .is_unsigned = false, .error = NULL, 0 } }; |
85 |
|
|
|
86 |
|
|
mysqlstatementcheck(mysql_stmt_bind_param(statement, parameters), statement, environment); |
87 |
|
|
mysqlstatementcheck(mysql_stmt_execute(statement), statement, environment); |
88 |
|
|
mysqlstatementcheck(mysql_stmt_close(statement), statement, environment); |
89 |
|
|
} |
90 |
|
|
else |
91 |
|
|
{ |
92 |
douglas |
438 |
char userescaped[userlength * 2 + 1], passwordescaped[passwordlength * 2 + 1]; |
93 |
|
|
unsigned long userescapedlength = mysql_real_escape_string(mysql, userescaped, user, userlength), passwordescapedlength = mysql_real_escape_string(mysql, passwordescaped, password, passwordlength); |
94 |
douglas |
1 |
|
95 |
douglas |
438 |
char *hosts[] = { "localhost", "%" }; |
96 |
douglas |
1 |
|
97 |
douglas |
438 |
for (unsigned index = 0; index != sizeof (hosts) / sizeof (*hosts); ++index) |
98 |
|
|
{ |
99 |
|
|
char *host = hosts[index]; |
100 |
|
|
size_t hostlength = strlen(host); |
101 |
|
|
char create[userescapedlength + hostlength + passwordescapedlength + 35], grant[userescapedlength * 2 + hostlength + 31]; |
102 |
douglas |
1 |
|
103 |
douglas |
438 |
sprintf(create, "create user '%s'@'%s' identified by '%s'", userescaped, host, passwordescaped); |
104 |
|
|
mysqlcheck(mysql_query(mysql, create), environment); |
105 |
|
|
sprintf(grant, "grant all on `%s\\_%%`.* to '%s'@'%s'", userescaped, userescaped, host); |
106 |
|
|
mysqlcheck(mysql_query(mysql, grant), environment); |
107 |
|
|
} |
108 |
douglas |
1 |
} |
109 |
|
|
} |