|
1
|
|
|
<?php |
|
2
|
|
|
namespace LibSSH2; |
|
3
|
|
|
|
|
4
|
|
|
use LibSSH2\Authentication\Authentication; |
|
5
|
|
|
use LibSSH2\Builder; |
|
6
|
|
|
use LibSSH2\Configuration; |
|
7
|
|
|
use LibSSH2\Sessions\SSH; |
|
8
|
|
|
use LibSSH2\Sessions\Shell; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Kerberos class. |
|
12
|
|
|
* |
|
13
|
|
|
* Create and set the KRB5CCNAME environmental variable for |
|
14
|
|
|
* Kerberos authentication. |
|
15
|
|
|
* |
|
16
|
|
|
* @package LibSSH2 |
|
17
|
|
|
*/ |
|
18
|
|
|
class Kerberos |
|
19
|
|
|
{ |
|
20
|
|
|
private $configuration; |
|
21
|
|
|
|
|
22
|
|
|
private $authentication; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param instance $configuration Configuration instance |
|
28
|
|
|
* @param instance $authentication Authentication instance |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Configuration $configuration, Authentication $authentication) |
|
32
|
|
|
{ |
|
33
|
|
|
if (get_class($authentication) != 'LibSSH2\Authentication\Password') |
|
34
|
|
|
{ |
|
35
|
|
|
throw new \RuntimeException("Kerberos authentication requires Password authentication to remote server."); |
|
36
|
|
|
} |
|
37
|
|
|
$this->configuration = $configuration; |
|
38
|
|
|
$this->authentication = $authentication; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* kcreate creates a Kerberos credential (ticket) cache and sets the |
|
43
|
|
|
* KRB5CCNAME environmental variable. |
|
44
|
|
|
* |
|
45
|
|
|
* @return string Kerberos credential cache |
|
46
|
|
|
*/ |
|
47
|
|
|
final public function kcreate($principle) |
|
48
|
|
|
{ |
|
49
|
|
|
$username = $this->configuration->get_username(); |
|
50
|
|
|
$command = (new Builder()) |
|
51
|
|
|
->setPrefix('kinit') |
|
52
|
|
|
->setArguments([$principle]); |
|
53
|
|
|
|
|
54
|
|
|
$shell = new Shell($this->configuration, $this->authentication); |
|
55
|
|
|
$shell |
|
56
|
|
|
->shell() |
|
57
|
|
|
->write("export KRB5CCNAME=`mktemp /tmp/krb5cc_{$username}_XXXXXXXXXXXXX`;") |
|
58
|
|
|
->write($command) |
|
59
|
|
|
->write($this->configuration->get_password()) |
|
60
|
|
|
->write('echo KRB5CCNAME:$KRB5CCNAME', true) |
|
61
|
|
|
->output(); |
|
62
|
|
|
|
|
63
|
|
|
if (!preg_match("/KRB5CCNAME:(\/tmp\/krb5cc_.*)/", $shell->get_output(), $matches)) |
|
64
|
|
|
{ |
|
65
|
|
|
throw new \RuntimeException('Failed to create the Kerberos credential cache and set the KRB5CCNAME environmental variable.'); |
|
66
|
|
|
} |
|
67
|
|
|
return $matches[1]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* The kdestroy utility destroys the user’s active Kerberos |
|
72
|
|
|
* authorization tickets by overwriting and deleting the |
|
73
|
|
|
* credentials cache that contains them. If the credentials |
|
74
|
|
|
* cache is not specified, the default credentials cache is |
|
75
|
|
|
* destroyed. |
|
76
|
|
|
* |
|
77
|
|
|
* @param array $options klist command line options |
|
78
|
|
|
* @param array $arguments klist command line arguments |
|
79
|
|
|
* @return int return code (exit status code) |
|
80
|
|
|
*/ |
|
81
|
|
|
final public function kdestroy(array $options = [], array $arguments = [], $strict = true) |
|
82
|
|
|
{ |
|
83
|
|
|
$command = (new Builder()) |
|
84
|
|
|
->setPrefix('kdestroy') |
|
85
|
|
|
->setOptions($options) |
|
86
|
|
|
->setArguments($arguments); |
|
87
|
|
|
list($retval, $output) = $this->_exec($command, $strict); |
|
88
|
|
|
return $retval; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* klist lists the Kerberos principal and Kerberos tickets held |
|
93
|
|
|
* in a credentials cache, or the keys held in a keytab file. |
|
94
|
|
|
* |
|
95
|
|
|
* @param array $options klist command line options |
|
96
|
|
|
* @param array $arguments klist command line arguments |
|
97
|
|
|
* @return string klist results |
|
98
|
|
|
*/ |
|
99
|
|
|
final public function klist(array $options = [], array $arguments = [], $strict = true) |
|
100
|
|
|
{ |
|
101
|
|
|
$command = (new Builder()) |
|
102
|
|
|
->setPrefix('klist') |
|
103
|
|
|
->setOptions($options) |
|
104
|
|
|
->setArguments($arguments); |
|
105
|
|
|
list($retval, $output) = $this->_exec($command, $strict); |
|
106
|
|
|
return $output; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* kinit obtains and caches an initial ticket-granting ticket for |
|
111
|
|
|
* principal. If principal is absent, kinit chooses an appropriate |
|
112
|
|
|
* principal name based on existing credential cache contents or |
|
113
|
|
|
* the local username of the user invoking kinit. Some options |
|
114
|
|
|
* modify the choice of principal name. |
|
115
|
|
|
* |
|
116
|
|
|
* @param array $options kinit command line options |
|
117
|
|
|
* @param array $arguments kinit command line arguments |
|
118
|
|
|
* @return int exit status code |
|
119
|
|
|
*/ |
|
120
|
|
|
final public function kinit(array $options = [], array $arguments = [], $strict = true) |
|
121
|
|
|
{ |
|
122
|
|
|
$command = (new Builder()) |
|
123
|
|
|
->setPrefix('kinit') |
|
124
|
|
|
->setOptions($options) |
|
125
|
|
|
->setArguments($arguments); |
|
126
|
|
|
list($retval, $output) = $this->_exec($command, $strict); |
|
127
|
|
|
return $retval; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Execute Kerberos command. |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $command command to be executed |
|
134
|
|
|
* @return array return code and STDOUT |
|
135
|
|
|
*/ |
|
136
|
|
|
final private function _exec($command, $strict) |
|
137
|
|
|
{ |
|
138
|
|
|
$ssh = new SSH($this->configuration, $this->authentication); |
|
139
|
|
|
$ssh->exec($command); |
|
140
|
|
|
|
|
141
|
|
|
$output = $ssh->get_output(); |
|
142
|
|
|
$error = $ssh->get_error(); |
|
143
|
|
|
$retval = $ssh->get_exitstatus(); |
|
144
|
|
|
|
|
145
|
|
|
if ($strict && $retval != 0) |
|
146
|
|
|
{ |
|
147
|
|
|
throw new \RuntimeException($error); |
|
148
|
|
|
} |
|
149
|
|
|
return [$retval, $output]; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|