2017-10-25 04:35:25 +00:00
|
|
|
<?php
|
|
|
|
|
2018-01-20 01:58:57 +00:00
|
|
|
namespace Pterodactyl\Http\Controllers\Api\Remote;
|
2017-10-25 04:35:25 +00:00
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2022-05-22 18:10:01 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2022-05-15 19:37:58 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2017-10-25 04:35:25 +00:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2020-04-25 18:48:49 +00:00
|
|
|
use Pterodactyl\Models\Permission;
|
2022-05-15 20:41:15 +00:00
|
|
|
use phpseclib3\Crypt\PublicKeyLoader;
|
2017-10-25 04:35:25 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
2022-05-15 20:41:15 +00:00
|
|
|
use phpseclib3\Exception\NoKeyLoadedException;
|
2017-10-25 04:35:25 +00:00
|
|
|
use Illuminate\Foundation\Auth\ThrottlesLogins;
|
2020-04-25 18:48:49 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\HttpForbiddenException;
|
|
|
|
use Pterodactyl\Services\Servers\GetUserPermissionsService;
|
2022-05-15 20:23:17 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
2018-01-20 02:01:56 +00:00
|
|
|
use Pterodactyl\Http\Requests\Api\Remote\SftpAuthenticationFormRequest;
|
2020-09-13 20:54:41 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
|
2017-10-25 04:35:25 +00:00
|
|
|
|
2022-05-15 20:06:00 +00:00
|
|
|
class SftpAuthenticationController extends Controller
|
2017-10-25 04:35:25 +00:00
|
|
|
{
|
|
|
|
use ThrottlesLogins;
|
|
|
|
|
2022-05-15 19:37:58 +00:00
|
|
|
protected GetUserPermissionsService $permissions;
|
|
|
|
|
|
|
|
public function __construct(GetUserPermissionsService $permissions)
|
|
|
|
{
|
|
|
|
$this->permissions = $permissions;
|
|
|
|
}
|
2020-04-25 18:48:49 +00:00
|
|
|
|
|
|
|
/**
|
2022-05-15 19:37:58 +00:00
|
|
|
* Authenticate a set of credentials and return the associated server details
|
2022-05-15 20:00:08 +00:00
|
|
|
* for a SFTP connection on the daemon. This supports both public key and password
|
|
|
|
* based credentials.
|
2020-04-25 18:48:49 +00:00
|
|
|
*/
|
2022-05-15 19:37:58 +00:00
|
|
|
public function __invoke(SftpAuthenticationFormRequest $request): JsonResponse
|
|
|
|
{
|
|
|
|
$connection = $this->parseUsername($request->input('username'));
|
2022-05-15 20:23:17 +00:00
|
|
|
if (empty($connection['server'])) {
|
|
|
|
throw new BadRequestHttpException('No valid server identifier was included in the request.');
|
|
|
|
}
|
2022-05-15 19:37:58 +00:00
|
|
|
|
2022-05-15 20:23:17 +00:00
|
|
|
if ($this->hasTooManyLoginAttempts($request)) {
|
|
|
|
$seconds = $this->limiter()->availableIn($this->throttleKey($request));
|
|
|
|
|
|
|
|
throw new TooManyRequestsHttpException($seconds, "Too many login attempts for this account, please try again in {$seconds} seconds.");
|
|
|
|
}
|
2022-05-15 19:37:58 +00:00
|
|
|
|
|
|
|
$user = $this->getUser($request, $connection['username']);
|
|
|
|
$server = $this->getServer($request, $connection['server']);
|
|
|
|
|
|
|
|
if ($request->input('type') !== 'public_key') {
|
|
|
|
if (!password_verify($request->input('password'), $user->password)) {
|
|
|
|
$this->reject($request);
|
|
|
|
}
|
2022-05-15 19:47:06 +00:00
|
|
|
} else {
|
2022-05-15 20:41:15 +00:00
|
|
|
$key = null;
|
|
|
|
try {
|
|
|
|
$key = PublicKeyLoader::loadPublicKey(trim($request->input('password')));
|
|
|
|
} catch (NoKeyLoadedException $exception) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$key || !$user->sshKeys()->where('fingerprint', $key->getFingerprint('sha256'))->exists()) {
|
2022-05-15 21:30:57 +00:00
|
|
|
$this->reject($request, is_null($key));
|
2022-05-15 19:47:06 +00:00
|
|
|
}
|
2022-05-15 19:37:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->validateSftpAccess($user, $server);
|
|
|
|
|
|
|
|
return new JsonResponse([
|
|
|
|
'server' => $server->uuid,
|
2022-05-15 21:30:57 +00:00
|
|
|
'permissions' => $this->permissions->handle($server, $user),
|
2022-05-15 19:37:58 +00:00
|
|
|
]);
|
|
|
|
}
|
2020-04-25 18:48:49 +00:00
|
|
|
|
|
|
|
/**
|
2022-05-15 19:37:58 +00:00
|
|
|
* Finds the server being requested and ensures that it belongs to the node this
|
|
|
|
* request stems from.
|
2020-04-25 18:48:49 +00:00
|
|
|
*/
|
2022-05-15 19:37:58 +00:00
|
|
|
protected function getServer(Request $request, string $uuid): Server
|
|
|
|
{
|
|
|
|
return Server::query()
|
|
|
|
->where(fn ($builder) => $builder->where('uuid', $uuid)->orWhere('uuidShort', $uuid))
|
|
|
|
->where('node_id', $request->attributes->get('node')->id)
|
|
|
|
->firstOr(function () use ($request) {
|
|
|
|
$this->reject($request);
|
|
|
|
});
|
|
|
|
}
|
2017-10-25 04:35:25 +00:00
|
|
|
|
|
|
|
/**
|
2022-05-15 19:37:58 +00:00
|
|
|
* Finds a user with the given username or increments the login attempts.
|
2017-10-25 04:35:25 +00:00
|
|
|
*/
|
2022-05-15 19:37:58 +00:00
|
|
|
protected function getUser(Request $request, string $username): User
|
|
|
|
{
|
|
|
|
return User::query()->where('username', $username)->firstOr(function () use ($request) {
|
|
|
|
$this->reject($request);
|
|
|
|
});
|
2017-10-25 04:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-05-15 19:37:58 +00:00
|
|
|
* Parses the username provided to the request.
|
2017-10-25 04:35:25 +00:00
|
|
|
*
|
2022-05-15 19:37:58 +00:00
|
|
|
* @return array{"username": string, "server": string}
|
2017-10-25 04:35:25 +00:00
|
|
|
*/
|
2022-05-15 19:37:58 +00:00
|
|
|
protected function parseUsername(string $value): array
|
2017-10-25 04:35:25 +00:00
|
|
|
{
|
2019-12-08 00:14:04 +00:00
|
|
|
// Reverse the string to avoid issues with usernames that contain periods.
|
2022-05-15 19:37:58 +00:00
|
|
|
$parts = explode('.', strrev($value), 2);
|
2019-12-08 00:14:04 +00:00
|
|
|
|
|
|
|
// Unreverse the strings after parsing them apart.
|
2022-05-15 19:37:58 +00:00
|
|
|
return [
|
2018-02-11 22:39:50 +00:00
|
|
|
'username' => strrev(array_get($parts, 1)),
|
|
|
|
'server' => strrev(array_get($parts, 0)),
|
|
|
|
];
|
2022-05-15 19:37:58 +00:00
|
|
|
}
|
2018-02-11 22:39:50 +00:00
|
|
|
|
2022-05-15 19:37:58 +00:00
|
|
|
/**
|
|
|
|
* Rejects the request and increments the login attempts.
|
|
|
|
*/
|
2022-05-15 20:23:17 +00:00
|
|
|
protected function reject(Request $request, bool $increment = true): void
|
2022-05-15 19:37:58 +00:00
|
|
|
{
|
2022-05-15 20:23:17 +00:00
|
|
|
if ($increment) {
|
|
|
|
$this->incrementLoginAttempts($request);
|
|
|
|
}
|
2020-09-13 20:54:41 +00:00
|
|
|
|
2022-05-15 19:37:58 +00:00
|
|
|
throw new HttpForbiddenException('Authorization credentials were not correct, please try again.');
|
|
|
|
}
|
2017-10-25 04:35:25 +00:00
|
|
|
|
2022-05-15 19:37:58 +00:00
|
|
|
/**
|
|
|
|
* Validates that a user should have permission to use SFTP for the given server.
|
|
|
|
*/
|
|
|
|
protected function validateSftpAccess(User $user, Server $server): void
|
|
|
|
{
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!$user->root_admin && $server->owner_id !== $user->id) {
|
2022-05-15 19:37:58 +00:00
|
|
|
$permissions = $this->permissions->handle($server, $user);
|
2020-04-25 18:48:49 +00:00
|
|
|
|
2021-01-23 20:33:34 +00:00
|
|
|
if (!in_array(Permission::ACTION_FILE_SFTP, $permissions)) {
|
|
|
|
throw new HttpForbiddenException('You do not have permission to access SFTP for this server.');
|
2020-04-25 18:48:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 21:28:31 +00:00
|
|
|
$server->validateCurrentState();
|
2017-10-25 04:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the throttle key for the given request.
|
|
|
|
*/
|
2020-12-17 17:34:26 +00:00
|
|
|
protected function throttleKey(Request $request): string
|
2017-10-25 04:35:25 +00:00
|
|
|
{
|
2019-12-08 00:14:04 +00:00
|
|
|
$username = explode('.', strrev($request->input('username', '')));
|
|
|
|
|
|
|
|
return strtolower(strrev($username[0] ?? '') . '|' . $request->ip());
|
2017-10-25 04:35:25 +00:00
|
|
|
}
|
|
|
|
}
|