-
Notifications
You must be signed in to change notification settings - Fork 1
/
Authentication.phpclass
executable file
·301 lines (210 loc) · 9.93 KB
/
Authentication.phpclass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
/**************************************************************************************************************
NAME
Ssh.phpclass
DESCRIPTION
Ssh protocol encapsulation classes.
AUTHOR
Christian Vigh, 01/2015.
HISTORY
[Version : 1.0] [Date : 2015/05/17] [Author : CV]
Initial version.
**************************************************************************************************************/
/*==============================================================================================================
SshAuthentication class -
Base class for all other authentication classes.
==============================================================================================================*/
abstract class SshAuthentication // extends Object
{
// Constants for the GetInstance() method
const SSH_AUTHENTICATION_AGENT = 1 ;
const SSH_AUTHENTICATION_HOSTBASED_FILE = 2 ;
const SSH_AUTHENTICATION_NONE = 3 ;
const SSH_AUTHENTICATION_PASSWORD = 4 ;
const SSH_AUTHENTICATION_PUBKEY_FILE = 5 ;
// Parent session
public $Session ;
// All derived classes have a username in common
public $Username ;
// Indicates if an authentication has been established
public $Authenticated = false ;
/*==============================================================================================================
Constructor -
Builds an SshAuthentication object. A Session object must be provided so that session data remains
accessible from authentication classes.
==============================================================================================================*/
public function __construct ( $session, $username )
{
//parent::__construct ( ) ;
$this -> Username = $username ;
$this -> Session = $session ;
}
/*==============================================================================================================
Authenticate -
Performs the authentication. To be implemented by derived classes.
==============================================================================================================*/
public abstract function Authenticate ( ) ;
/*==============================================================================================================
GetInstance -
Returns an authentication object of the specified type. For the purists only ; those who prefer
performance will directly call the constructor of the class they want.
Note that specific authentication class constructor arguments MUST be specified after the $session
parameter.
==============================================================================================================*/
public static function GetInstance ( $auth_type, $session )
{
$argv = func_get_args ( ) ;
array_shift ( $argv ) ;
switch ( $auth_type )
{
case self::SSH_AUTHENTICATION_AGENT :
$class = 'SshAgentAuthentication' ;
break ;
case self::SSH_AUTHENTICATION_HOSTBASED_FILE :
$class = 'SshHostBasedAuthentication' ;
break ;
case self::SSH_AUTHENTICATION_NONE :
$class = 'SshAuthenticationNone' ;
break ;
case self::SSH_AUTHENTICATION_PASSWORD :
$class = 'SshPasswordAuthentication' ;
break ;
case self::SSH_AUTHENTICATION_PUBKEY_FILE :
$class = 'SshPublicKeyAuthentication' ;
break ;
default :
error ( \InvalidArgumentException ( "Invalid value $auth_type for the \$auth_type parameter" ) ) ;
}
$object = call_user_func_array ( [ $class, '__construct' ], $argv ) ;
return ( $object ) ;
}
/*==============================================================================================================
GetSession -
Ensures that the Session object is connected.
==============================================================================================================*/
protected function GetSession ( )
{
if ( $this -> Session )
{
$connection = $this -> Session -> Connection ;
if ( $connection -> IsConnected ( ) )
return ( $connection -> GetResource ( ) ) ;
}
error ( new SshSessionException ( get_class ( ) . ' : Cannot authenticate, no connection established.' ) ) ;
}
}
/*==============================================================================================================
SshAgentAuthentication -
Authentication using ssh agent.
==============================================================================================================*/
class SshAgentAuthentication extends SshAuthentication
{
public function __construct ( $session, $username )
{
parent::__construct ( $session, $username ) ;
}
public function Authenticate ( )
{
$status = ssh2_auth_agent ( $this -> GetSession ( ), $this -> Username ) ;
$this -> Authenticated = ( $status === true ) ;
return ( $status ) ;
}
}
/*==============================================================================================================
SshAuthenticationNone -
Tries a "none" authentication. Returns true if the "none" method is supported, or an array of supported
methods otherwise.
==============================================================================================================*/
class SshAuthenticationNone extends SshAuthentication
{
public function __construct ( $session, $username )
{
parent::__construct ( $session, $username ) ;
}
public function Authenticate ( )
{
$status = ssh2_auth_none ( $this -> GetSession ( ), $this -> Username ) ;
$this -> Authenticated = false ;
return ( $status ) ;
}
}
/*==============================================================================================================
SshPasswordAuthentication -
Authentication with username/password.
==============================================================================================================*/
class SshPasswordAuthentication extends SshAuthentication
{
public $Password ;
public function __construct ( $session, $username, $password )
{
parent::__construct ( $session, $username ) ;
$this -> Password = $password ;
}
public function Authenticate ( )
{
$status = ssh2_auth_password ( $this -> GetSession ( ), $this -> Username, $this -> Password ) ;
$this -> Authenticated = ( $status === true ) ;
return ( $status ) ;
}
}
/*==============================================================================================================
SshKeyBasedAuthentication class -
Base class for the SshHostBasedAuthentication and SshPublicKeyAuthentication classes.
==============================================================================================================*/
abstract class SshKeyBasedAuthentication extends SshAuthentication
{
public $PublicKeyFile ;
public $PrivateKeyFile ;
public $PassPhrase ;
public function __construct ( $session, $username, $public_key_file, $private_key_file, $passphrase = null )
{
parent::__construct ( $session, $username ) ;
if ( ! file_exists ( $public_key_file ) )
error ( new \Exception ( "Public key file \"$public_key_file\" not found" ) ) ;
if ( ! file_exists ( $private_key_file ) )
error ( new \Exception ( "Private key file \"$private_key_file\" not found" ) ) ;
$this -> PublicKeyFile = realpath ( $public_key_file ) ;
$this -> PrivateKeyFile = realpath ( $private_key_file ) ;
$this -> PassPhrase = $passphrase ;
}
}
/*==============================================================================================================
SshPublicKeyAuthentication -
Authentication using a public key.
==============================================================================================================*/
class SshPublicKeyAuthentication extends SshKeyBasedAuthentication
{
public function __construct ( $session, $username, $public_key_file, $private_key_file, $passphrase = null )
{
parent::__construct ( $session, $username, $public_key_file, $private_key_file, $passphrase ) ;
}
// FIX: ssh2_auth_pubkey_file() always return the following error :
// ssh2_auth_pubkey_file(): Authentication failed for root using public key: Callback returned error.
public function Authenticate ( )
{
$status = ssh2_auth_pubkey_file ( $this -> GetSession ( ), $this -> Username,
$this -> PublicKeyFile, $this -> PrivateKeyFile, $this -> PassPhrase ) ;
$this -> Authenticated = ( $status === true ) ;
return ( $status ) ;
}
}
/*==============================================================================================================
SshHostBasedAuthentication -
Authentication through an external host.
==============================================================================================================*/
class SshHostBasedAuthentication extends SshKeyBasedAuthentication
{
public $Host ;
public function __construct ( $session, $username, $host, $public_key_file, $private_key_file, $passphrase = null )
{
parent::__construct ( $session, $username, $public_key_file, $private_key_file, $passphrase ) ;
$this -> Host = $host ;
}
public function Authenticate ( )
{
$status = ssh2_auth_hostbased_file ( $this -> GetSession ( ), $this -> Username, $this -> Host,
$this -> PublicKeyFile, $this -> PrivateKeyFile, $this -> PassPhrase ) ;
$this -> Authenticated = ( $status === true ) ;
return ( $status ) ;
}
}