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;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
2020-04-25 18:48:49 +00:00
|
|
|
use Pterodactyl\Models\Permission;
|
2017-10-25 04:35:25 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Foundation\Auth\ThrottlesLogins;
|
2020-04-25 18:48:49 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\UserRepository;
|
|
|
|
use Pterodactyl\Exceptions\Http\HttpForbiddenException;
|
|
|
|
use Pterodactyl\Repositories\Eloquent\ServerRepository;
|
|
|
|
use Pterodactyl\Services\Servers\GetUserPermissionsService;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2020-12-17 17:34:26 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Server\ServerTransferringException;
|
2018-01-31 04:40:21 +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
|
|
|
|
2019-12-08 00:14:04 +00:00
|
|
|
class SftpAuthenticationController extends Controller
|
2017-10-25 04:35:25 +00:00
|
|
|
{
|
|
|
|
use ThrottlesLogins;
|
|
|
|
|
|
|
|
/**
|
2020-04-25 18:48:49 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\UserRepository
|
2017-10-25 04:35:25 +00:00
|
|
|
*/
|
2020-04-25 18:48:49 +00:00
|
|
|
private $userRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
|
|
|
|
*/
|
|
|
|
private $serverRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Services\Servers\GetUserPermissionsService
|
|
|
|
*/
|
|
|
|
private $permissionsService;
|
2017-10-25 04:35:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SftpController constructor.
|
|
|
|
*
|
2020-04-25 18:48:49 +00:00
|
|
|
* @param \Pterodactyl\Services\Servers\GetUserPermissionsService $permissionsService
|
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\UserRepository $userRepository
|
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $serverRepository
|
2017-10-25 04:35:25 +00:00
|
|
|
*/
|
2020-04-25 18:48:49 +00:00
|
|
|
public function __construct(
|
|
|
|
GetUserPermissionsService $permissionsService,
|
|
|
|
UserRepository $userRepository,
|
|
|
|
ServerRepository $serverRepository
|
|
|
|
) {
|
|
|
|
$this->userRepository = $userRepository;
|
|
|
|
$this->serverRepository = $serverRepository;
|
|
|
|
$this->permissionsService = $permissionsService;
|
2017-10-25 04:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authenticate a set of credentials and return the associated server details
|
|
|
|
* for a SFTP connection on the daemon.
|
|
|
|
*
|
2018-01-20 02:01:56 +00:00
|
|
|
* @param \Pterodactyl\Http\Requests\Api\Remote\SftpAuthenticationFormRequest $request
|
2017-10-25 04:35:25 +00:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*
|
2020-04-25 18:48:49 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2017-10-25 04:35:25 +00:00
|
|
|
*/
|
2019-12-08 00:14:04 +00:00
|
|
|
public function __invoke(SftpAuthenticationFormRequest $request): JsonResponse
|
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.
|
2018-02-11 22:39:50 +00:00
|
|
|
$parts = explode('.', strrev($request->input('username')), 2);
|
2019-12-08 00:14:04 +00:00
|
|
|
|
|
|
|
// Unreverse the strings after parsing them apart.
|
2018-02-11 22:39:50 +00:00
|
|
|
$connection = [
|
|
|
|
'username' => strrev(array_get($parts, 1)),
|
|
|
|
'server' => strrev(array_get($parts, 0)),
|
|
|
|
];
|
|
|
|
|
2017-10-25 04:35:25 +00:00
|
|
|
if ($this->hasTooManyLoginAttempts($request)) {
|
2020-09-13 20:54:41 +00:00
|
|
|
$seconds = $this->limiter()->availableIn($this->throttleKey($request));
|
|
|
|
|
|
|
|
throw new TooManyRequestsHttpException(
|
|
|
|
$seconds, "Too many login attempts for this account, please try again in {$seconds} seconds."
|
|
|
|
);
|
2017-10-25 04:35:25 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 18:48:49 +00:00
|
|
|
/** @var \Pterodactyl\Models\Node $node */
|
|
|
|
$node = $request->attributes->get('node');
|
|
|
|
if (empty($connection['server'])) {
|
|
|
|
throw new NotFoundHttpException;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\User $user */
|
|
|
|
$user = $this->userRepository->findFirstWhere([
|
|
|
|
['username', '=', $connection['username']],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$server = $this->serverRepository->getByUuid($connection['server'] ?? '');
|
|
|
|
if (! password_verify($request->input('password'), $user->password) || $server->node_id !== $node->id) {
|
2020-09-13 20:54:41 +00:00
|
|
|
$this->incrementLoginAttempts($request);
|
|
|
|
|
2020-04-25 18:48:49 +00:00
|
|
|
throw new HttpForbiddenException(
|
|
|
|
'Authorization credentials were not correct, please try again.'
|
2017-10-25 04:35:25 +00:00
|
|
|
);
|
2020-04-25 18:48:49 +00:00
|
|
|
}
|
2017-10-25 04:35:25 +00:00
|
|
|
|
2020-04-25 18:48:49 +00:00
|
|
|
if (! $user->root_admin && $server->owner_id !== $user->id) {
|
|
|
|
$permissions = $this->permissionsService->handle($server, $user);
|
|
|
|
|
|
|
|
if (! in_array(Permission::ACTION_FILE_SFTP, $permissions)) {
|
|
|
|
throw new HttpForbiddenException(
|
|
|
|
'You do not have permission to access SFTP for this server.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 17:34:26 +00:00
|
|
|
// Prevent SFTP access to servers that are being transferred.
|
|
|
|
if (! is_null($server->transfer)) {
|
2020-12-24 18:14:10 +00:00
|
|
|
throw new ServerTransferringException;
|
2020-12-17 17:34:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remember, for security purposes, only reveal the existence of the server to people that
|
2020-04-25 18:48:49 +00:00
|
|
|
// have provided valid credentials, and have permissions to know about it.
|
2020-12-17 17:34:26 +00:00
|
|
|
if ($server->installed !== 1 || $server->suspended) {
|
2020-04-25 18:48:49 +00:00
|
|
|
throw new BadRequestHttpException(
|
|
|
|
'Server is not installed or is currently suspended.'
|
|
|
|
);
|
2017-10-25 04:35:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 16:34:47 +00:00
|
|
|
return new JsonResponse([
|
2020-04-25 18:48:49 +00:00
|
|
|
'server' => $server->uuid,
|
|
|
|
// Deprecated, but still needed at the moment for Wings.
|
|
|
|
'token' => '',
|
|
|
|
'permissions' => $permissions ?? ['*'],
|
|
|
|
]);
|
2017-10-25 04:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the throttle key for the given request.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return string
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
}
|