Skip to content

Commit 202dd52

Browse files
committed
Fixes bug causing MySQL user accounts to be corrupted when resetting a password via the panel.
closes pterodactyl#352
1 parent d5352e2 commit 202dd52

File tree

5 files changed

+23
-11
lines changed

5 files changed

+23
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
2222
* `[pre.4]` — Fixes server listing on frontend not displaying a page selector when more than 10 servers exist.
2323
* `[pre.4]` — Fixes non-admin users being unable to create personal API keys.
2424
* Fixes bug where daemon was unable to register that certain games had fully booted and were ready to play on.
25+
* Fixes bug causing MySQL user accounts to be corrupted when resetting a password via the panel.
2526

2627
### Added
2728
* Ability to assign multiple allocations at once when creating a new server.

app/Http/Controllers/Server/ServerController.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,14 @@ public function getDatabases(Request $request, $uuid)
239239
{
240240
$server = Models\Server::byUuid($uuid);
241241
$this->authorize('view-databases', $server);
242+
243+
$server->load('node', 'databases.host');
242244
$server->js();
243245

244246
return view('server.settings.databases', [
245247
'server' => $server,
246248
'node' => $server->node,
247-
'databases' => Models\Database::select('databases.*', 'database_servers.host as a_host', 'database_servers.port as a_port')
248-
->where('server_id', $server->id)
249-
->join('database_servers', 'database_servers.id', '=', 'databases.db_server')
250-
->get(),
249+
'databases' => $server->databases,
251250
]);
252251
}
253252

app/Models/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class Database extends Model
6868
*/
6969
public function host()
7070
{
71-
return $this->belongsTo(DatabaseHost::class);
71+
return $this->belongsTo(DatabaseHost::class, 'database_host_id');
7272
}
7373

7474
/**

app/Repositories/DatabaseRepository.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ class DatabaseRepository
4848
*/
4949
public function create($id, array $data)
5050
{
51-
$server = Server::findOrFail($server);
51+
$server = Server::findOrFail($id);
5252

5353
$validator = Validator::make($data, [
54-
'host' => 'required|exists:database_servers,id',
54+
'host' => 'required|exists:database_hosts,id',
5555
'database' => 'required|regex:/^\w{1,100}$/',
5656
'connection' => 'required|regex:/^[0-9%.]{1,15}$/',
5757
]);
@@ -64,7 +64,7 @@ public function create($id, array $data)
6464
DB::beginTransaction();
6565

6666
try {
67-
$database = Models\Database::firstOrNew([
67+
$database = Database::firstOrNew([
6868
'server_id' => $server->id,
6969
'database_host_id' => $data['host'],
7070
'database' => sprintf('s%d_%s', $server->id, $data['database']),
@@ -131,10 +131,12 @@ public function create($id, array $data)
131131
* @param int $id
132132
* @param string $password
133133
* @return void
134+
*
135+
* @todo Fix logic behind resetting passwords.
134136
*/
135137
public function password($id, $password)
136138
{
137-
$database = Models\Database::with('host')->findOrFail($id);
139+
$database = Database::with('host')->findOrFail($id);
138140

139141
DB::transaction(function () use ($database, $password) {
140142
$database->password = Crypt::encrypt($password);
@@ -150,10 +152,20 @@ public function password($id, $password)
150152
'collation' => 'utf8_unicode_ci',
151153
]);
152154

155+
// We have to do the whole delete user, create user thing rather than
156+
// SET PASSWORD ... because MariaDB and PHP statements ends up inserting
157+
// a corrupted password. A way around this is strtoupper(sha1(sha1($password, true)))
158+
// but no garuntees that will work correctly with every system.
159+
DB::connection('dynamic')->statement(sprintf('DROP USER IF EXISTS `%s`@`%s`', $database->username, $database->remote));
153160
DB::connection('dynamic')->statement(sprintf(
154-
'SET PASSWORD FOR `%s`@`%s` = PASSWORD(\'%s\')',
161+
'CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'',
155162
$database->username, $database->remote, $password
156163
));
164+
DB::connection('dynamic')->statement(sprintf(
165+
'GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER, INDEX ON `%s`.* TO `%s`@`%s`',
166+
$database->database, $database->username, $database->remote
167+
));
168+
DB::connection('dynamic')->statement('FLUSH PRIVILEGES');
157169

158170
$database->save();
159171
});

resources/themes/pterodactyl/server/settings/databases.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
<td class="middle">{{ $database->database }}</td>
5757
<td class="middle">{{ $database->username }}</td>
5858
<td class="middle"><code data-attr="set-password">{{ Crypt::decrypt($database->password) }}</code></td>
59-
<td class="middle"><code>{{ $database->a_host }}:{{ $database->a_port }}</code></td>
59+
<td class="middle"><code>{{ $database->host->host }}:{{ $database->host->port }}</code></td>
6060
@can('reset-db-password', $server)
6161
<td>
6262
<button class="btn btn-xs btn-primary pull-right" data-action="reset-password" data-id="{{ $database->id }}"><i class="fa fa-fw fa-refresh"></i> @lang('server.config.database.reset_password')</button>

0 commit comments

Comments
 (0)