pssht  latest
SSH server library written in PHP
Password.php
1 <?php
2 
3 /*
4 * This file is part of pssht.
5 *
6 * (c) François Poirotte <clicky@erebot.net>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11 
13 
15 
20 {
22  protected $credentials;
23 
31  public function __construct(array $credentials)
32  {
33  $this->credentials = $credentials;
34  }
35 
36  public static function getName()
37  {
38  return 'password';
39  }
40 
41  public function check(
42  \fpoirotte\Pssht\Messages\USERAUTH\REQUEST\Base $message,
43  \fpoirotte\Pssht\Transport $transport,
44  array &$context
45  ) {
46  if (!($message instanceof \fpoirotte\Pssht\Messages\USERAUTH\REQUEST\Password)) {
47  throw new \InvalidArgumentException();
48  }
49 
50  return self::CHECK_OK;
51  }
52 
53  public function authenticate(
54  \fpoirotte\Pssht\Messages\USERAUTH\REQUEST\Base $message,
55  \fpoirotte\Pssht\Transport $transport,
56  array &$context
57  ) {
58  if (!($message instanceof \fpoirotte\Pssht\Messages\USERAUTH\REQUEST\Password)) {
59  throw new \InvalidArgumentException();
60  }
61 
62  $logging = \Plop\Plop::getInstance();
63  $reverse = gethostbyaddr($transport->getAddress());
64 
65  if (isset($this->credentials[$message->getUserName()]) &&
66  $message->getPassword() === $this->credentials[$message->getUserName()]) {
67  $logging->info(
68  'Accepted password based connection from remote host ' .
69  '"%(reverse)s" (%(address)s) to "%(luser)s"',
70  array(
71  'reverse' => $reverse,
72  'address' => $transport->getAddress(),
73  'luser' => escape($message->getUserName())
74  )
75  );
76  return self::AUTH_ACCEPT;
77  }
78 
79  $logging->info(
80  'Rejected password based connection from remote host ' .
81  '"%(reverse)s" (%(address)s) to "%(luser)s": ' .
82  'invalid credentials',
83  array(
84  'reverse' => $reverse,
85  'address' => $transport->getAddress(),
86  'luser' => escape($message->getUserName())
87  )
88  );
89  return self::AUTH_REJECT;
90  }
91 }
check(\fpoirotte\Pssht\Messages\USERAUTH\REQUEST\Base $message,\fpoirotte\Pssht\Transport $transport, array &$context)
Definition: Password.php:41
authenticate(\fpoirotte\Pssht\Messages\USERAUTH\REQUEST\Base $message,\fpoirotte\Pssht\Transport $transport, array &$context)
Definition: Password.php:53
static getName()
Return the name of the algorithm.
Definition: Password.php:36
$credentials
Credentials of allowed users.
Definition: Password.php:22