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 |
438 |
void mysqlcheck(int value, jmp_buf environment) |
14 |
douglas |
1 |
{ |
15 |
douglas |
438 |
if (value) |
16 |
|
|
longjmp(environment, MYSQL_EXCEPTION); |
17 |
douglas |
1 |
} |
18 |
|
|
|
19 |
douglas |
438 |
void mysqlpassword(MYSQL *mysql, char user[MAXLOGNAME], char password[_PASSWORD_LEN], jmp_buf environment) |
20 |
douglas |
1 |
{ |
21 |
douglas |
438 |
FILE *stream = fopen(SECRET, "r"); |
22 |
douglas |
1 |
|
23 |
douglas |
438 |
if (!stream) |
24 |
|
|
longjmp(environment, POSIX_EXCEPTION); |
25 |
douglas |
1 |
|
26 |
douglas |
438 |
char rootpassword[_PASSWORD_LEN] = ""; |
27 |
douglas |
1 |
|
28 |
douglas |
438 |
fcheck(fgets(rootpassword, _PASSWORD_LEN, stream), stream, environment); |
29 |
douglas |
1 |
|
30 |
douglas |
438 |
if (fclose(stream)) |
31 |
|
|
longjmp(environment, POSIX_EXCEPTION); |
32 |
douglas |
1 |
|
33 |
douglas |
438 |
if (!mysql_real_connect(mysql, NULL, "root", rootpassword, "mysql", 0, NULL, 0)) |
34 |
|
|
longjmp(environment, MYSQL_EXCEPTION); |
35 |
douglas |
1 |
|
36 |
douglas |
438 |
MYSQL_STMT *statement = mysql_stmt_init(mysql); |
37 |
douglas |
1 |
|
38 |
douglas |
438 |
if (!statement) |
39 |
|
|
longjmp(environment, MYSQL_EXCEPTION); |
40 |
douglas |
1 |
|
41 |
douglas |
438 |
char *update = "update user set Password = PASSWORD(?) where User = ?"; |
42 |
douglas |
1 |
|
43 |
douglas |
438 |
mysqlcheck(mysql_stmt_prepare(statement, update, strlen(update)), environment); |
44 |
douglas |
1 |
|
45 |
douglas |
438 |
size_t userlength = strlen(user), passwordlength = strlen(password); |
46 |
|
|
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 } }; |
47 |
douglas |
1 |
|
48 |
douglas |
438 |
mysqlcheck(mysql_stmt_bind_param(statement, parameters), environment); |
49 |
|
|
mysqlcheck(mysql_stmt_execute(statement), environment); |
50 |
douglas |
1 |
|
51 |
douglas |
438 |
my_ulonglong value = mysql_stmt_affected_rows(statement); |
52 |
douglas |
1 |
|
53 |
douglas |
438 |
if (!value) |
54 |
douglas |
1 |
{ |
55 |
douglas |
438 |
char userescaped[userlength * 2 + 1], passwordescaped[passwordlength * 2 + 1]; |
56 |
|
|
unsigned long userescapedlength = mysql_real_escape_string(mysql, userescaped, user, userlength), passwordescapedlength = mysql_real_escape_string(mysql, passwordescaped, password, passwordlength); |
57 |
douglas |
1 |
|
58 |
douglas |
438 |
char *hosts[] = { "localhost", "%" }; |
59 |
douglas |
1 |
|
60 |
douglas |
438 |
for (unsigned index = 0; index != sizeof (hosts) / sizeof (*hosts); ++index) |
61 |
|
|
{ |
62 |
|
|
char *host = hosts[index]; |
63 |
|
|
size_t hostlength = strlen(host); |
64 |
|
|
char create[userescapedlength + hostlength + passwordescapedlength + 35], grant[userescapedlength * 2 + hostlength + 31]; |
65 |
douglas |
1 |
|
66 |
douglas |
438 |
sprintf(create, "create user '%s'@'%s' identified by '%s'", userescaped, host, passwordescaped); |
67 |
|
|
mysqlcheck(mysql_query(mysql, create), environment); |
68 |
|
|
sprintf(grant, "grant all on `%s\\_%%`.* to '%s'@'%s'", userescaped, userescaped, host); |
69 |
|
|
mysqlcheck(mysql_query(mysql, grant), environment); |
70 |
|
|
} |
71 |
douglas |
1 |
} |
72 |
douglas |
438 |
else if (value < 0) |
73 |
|
|
longjmp(environment, MYSQL_EXCEPTION); |
74 |
douglas |
1 |
|
75 |
douglas |
438 |
mysqlcheck(mysql_stmt_close(statement), environment); |
76 |
douglas |
1 |
} |