-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.class.php
208 lines (188 loc) · 6.63 KB
/
terminal.class.php
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
<?php
/**
* Terminal
* A Class Terminal é a classe responsável pela camada de interação entre
* os processos e o usuário.
* Depois eu vou traduzir todos os comentários para o inglês...
* agora não... agora não... depois, depois, depois... (preguiça).
* @author Thiago Bocchile <tykoth@gmail.com>
*/
class Terminal {
// Pra entrar no terminal precisa, óbvio.
private $login;
private $password;
static $commandsFile = "comandos.txt";
static $totalCommands;
static $process;
static $status;
static $meta;
static $instance;
public static function autenticate($login, $password) {
$process = new Process("su " . escapeshellarg($login));
usleep(500000);
$process->put($password);
$process->put(chr(13));
usleep(500000);
return (bool) !$process->close();
}
public static function run($login, $password){
self::$instance = new self();
return self::$instance->open($login, $password);
}
public static function postCommand($command){
file_put_contents(self::$commandsFile, $command."\n", FILE_APPEND);
}
// public function __construct($login, $password) {
// return $this->open($login, $password);
// }
private function open($login, $password) {
$this->login = $login;
$this->password = $password;
if(!is_writable(self::$commandsFile)){
$this->output("\r\nNeed permission to write in ".self::$commandsFile."\r\n");
return false;
}
// Clean commands
file_put_contents(self::$commandsFile, "");
$this->startProcess();
do {
$out = self::$process->get();
// Detect "blocking" (wait for stdin)
if(sizeof($out) == 1 && ord($out) == 0) {
$this->listenCommand();
} else {
// Provisorio, meldels. (usuario www-data não tem controle de servico, dude!)
if(preg_match('/-su: no job control in this shell/', $out)) continue;
$this->output($out);
}
usleep(50000);
self::$status = self::$process->getStatus();
self::$meta = self::$process->metaData();
} while(self::$meta['eof'] === false);
}
private function startProcess() {
self::$process = new Process("su - {$this->login}");
// self::$process = new Process("vi");
if(!self::$process->isResource()) {
throw new Exception("recurso indisponivel");
return false;
}
usleep(500000);
self::$process->put($this->password);
self::$process->put(chr(13));
self::$process->get();
usleep(500000);
self::$status = self::$process->getStatus();
self::$meta = self::$process->metaData();
}
private function output($output) {
$output = htmlentities($output);
// nl2br doesn't works...
$output = explode("\n", $output);
$output = implode("</span><span>", $output);
$output = sprintf("<span>%s</span>", $output);
$output = preg_replace( "/\n|\r|\r\n/", '\n', $output);
// var_dump($output);die;
$output = trim($output);
// $output = addslashes($output);
echo "<script>recebe(\"{$output}\");</script>\n";
flush();
}
/**
* Listen for incoming commands
*/
private function listenCommand() {
$commands = file(self::$commandsFile);
// $this->output("w84cmm");
if(sizeof($commands) > self::$totalCommands) {
self::$totalCommands = sizeof($commands);
$command = $commands[self::$totalCommands-1];
$this->parse($command);
}
}
/**
* Parse the command
*/
private function parse($command) {
switch(trim($command)) {
case chr(3):
// SIGTERM
return $this->sendSigterm();
break;
case chr(4):
self::$process->put("exit");
self::$process->put(chr(13));
break;
case chr(26):
//STOP - experimental
return $this->sendSigstop();
break;
case 'fg':
return $this->sendFg();
break;
case 'bg':
return $this->sendBg();
break;
default:
self::$process->put($command);
usleep(500000);
break;
}
}
/**
* Emulates the SIGTERM sending via CTRL-C
*/
private function sendSigterm() {
// SLAYER!!! GRRRRRRRRRR
// http://www.youtube.com/watch?v=VSoh3c7QVyw
$SLAYER = 'pid='.self::$status['pid'].
'; supid=`ps -o pid --no-heading --ppid $pid`;'.
'bashpid=`ps -o pid --no-heading --ppid $supid`;'.
'childs=`ps -o pid --no-heading --ppid $bashpid`;'.
'kill -9 $childs;';
$process = new Process("su -c '{$SLAYER}' -l {$this->login}");
usleep(500000);
$process->put($this->password);
$process->put(chr(13));
usleep(500000);
}
private function sendSigstop() {
$SLAYER = 'pid='.self::$status['pid'].
'; supid=`ps -o pid --no-heading --ppid $pid`;'.
'bashpid=`ps -o pid --no-heading --ppid $supid`;'.
'childs=`ps -o pid --no-heading --ppid $bashpid`;'.
'kill -TSTP $childs;';
$process = new Process("su -c '{$SLAYER}' -l {$this->login}");
usleep(500000);
$process->put($this->password);
$process->put(chr(13));
self::$process->put(chr(13));
usleep(500000);
}
private function sendBg() {
$SLAYER = 'pid='.self::$status['pid'].
'; supid=`ps -o pid --no-heading --ppid $pid`;'.
'bashpid=`ps -o pid --no-heading --ppid $supid`;'.
'childs=`ps -o pid --no-heading --ppid $bashpid`;'.
'kill -TSTP $childs;';
$process = new Process("su -c '{$SLAYER}' -l {$this->login}");
usleep(500000);
$process->put($this->password);
$process->put(chr(13));
self::$process->put(chr(13));
usleep(500000);
}
private function sendFg() {
$SLAYER = 'pid='.self::$status['pid'].
'; supid=`ps -o pid --no-heading --ppid $pid`;'.
'bashpid=`ps -o pid --no-heading --ppid $supid`;'.
'childs=`ps -o pid --no-heading --ppid $bashpid`;'.
'kill -CONT $childs;';
$process = new Process("su -c '{$SLAYER}' -l {$this->login}");
usleep(500000);
$process->put($this->password);
$process->put(chr(13));
self::$process->put(chr(13));
usleep(500000);
}
}