├── img └── terminal.png ├── gotty.conf ├── listCommandSet.php ├── deleteCommand.php ├── addCommand.php ├── config.php ├── css └── style.css ├── runCommand.php ├── lib └── dbManager.php ├── install.php ├── readme.md └── index.php /img/terminal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Arno0x/TermGate/HEAD/img/terminal.png -------------------------------------------------------------------------------- /gotty.conf: -------------------------------------------------------------------------------- 1 | preferences { 2 | use_default_window_copy = true 3 | copy_on_select = false 4 | ctrl_c_copy = true 5 | ctrl_v_paste = true 6 | font_familly = "Lucida Console" 7 | } -------------------------------------------------------------------------------- /listCommandSet.php: -------------------------------------------------------------------------------- 1 | getMessage(); 27 | exit(); 28 | } 29 | 30 | // Retrieve all commands stored in the database 31 | $commandList = $dbManager->getCommandList(); 32 | 33 | // Check if we have at least one element (faster than counting the number of elements in the array) 34 | if (!isset($commandList['0'])) { 35 | echo '
";
65 | echo implode("\n", $return);
66 | echo "";
67 | }
68 | // Else, if it's a dynamic/interactive command, we have to instantiate Gotty along with the proper command.
69 | // Note: commands are executed as the user specified in the RUN_AS_USER setting
70 | else {
71 | // Prepare the Gotty command
72 | // ** A bit of explanation about the use of the DISPLAY environment **
73 | // ** Most SUDOERS environment will have the 'env_reset' option set, especially for untrusted account such as ones used to run the web server.
74 | // ** With this option set, the environment variables that can be set by the sudo command is restricted to a small set, including the DISPLAY variable.
75 | // ** Because in a TermGate + Gotty typical usage, this variable is most probably unused or, if required, has 90% of chances to point back to the client IP
76 | // ** I'm using it to store the client IP, as it can be a useful information once in a Gotty shell.
77 | $gottyCommand = "sudo -b -u ".RUN_AS_USER." DISPLAY=".$_SERVER['REMOTE_ADDR']." TERM=".GOTTY_TERM." -i ".GOTTY_PATH." --once -w -p ".GOTTY_TCP_PORT." -a ".GOTTY_BIND_INTERFACE." ".$command." > /dev/null 2>&1";
78 |
79 |
80 | // Execute the command
81 | exec($gottyCommand);
82 |
83 | // DIRTY (!): Wait 500ms, just the time for gotty to be ready to accept incoming connections
84 | usleep(500000);
85 |
86 | echo "";
87 | }
88 | ?>
--------------------------------------------------------------------------------
/lib/dbManager.php:
--------------------------------------------------------------------------------
1 | exec($sqlQuery))) {
40 | return false;
41 | }
42 | else {
43 | return true;
44 | }
45 | }
46 |
47 | //--------------------------------------------------------
48 | // Delete a command
49 | // @param commandID : The command ID
50 | // @return bool : TRUE if the command was deleted, FALSE otherwise
51 | public function deleteCommand ($commandID) {
52 |
53 | // Prepare variables before the query
54 | $commandID = SQLite3::escapeString ($commandID);
55 |
56 | // Prepare SQL query
57 | $sqlQuery = "DELETE from COMMANDS where ID='".$commandID."';";
58 |
59 | // Perform SQL query
60 | if(!($ret = $this->exec($sqlQuery))) {
61 | return false;
62 | }
63 | else {
64 | return true;
65 | }
66 | }
67 |
68 | //--------------------------------------------------------
69 | // Get the list of commands in the database
70 | // @return array : an array of all commands in the set (ie the database), or FALSE if there was an error
71 | public function getCommandList () {
72 |
73 | // Prepare SQL query
74 | $sqlQuery = "SELECT * from COMMANDS;";
75 |
76 | // Perform SQL query
77 | if(!($ret = $this->query($sqlQuery))) {
78 | return false;
79 | }
80 | else {
81 | $result = array();
82 | $i=0;
83 | while ($row = $ret->fetchArray(SQLITE3_ASSOC)) {
84 | $result[$i++] = $row;
85 | }
86 | return $result;
87 | }
88 | }
89 |
90 | //--------------------------------------------------------
91 | // Get a command and its IsDynamic status from the database by its ID
92 | // @return array : an array containing the command, or FALSE if there was an error
93 | public function getCommandAndIsDynamicByID ($commandID) {
94 |
95 | // Prepare SQL query
96 | $sqlQuery = "SELECT COMMAND,ISDYNAMIC from COMMANDS where ID='".$commandID."';";
97 |
98 | // Perform SQL query
99 | if(!($ret = $this->querySingle($sqlQuery, true))) {
100 | return false;
101 | }
102 | else {
103 | return $ret;
104 | }
105 | }
106 | }
107 | ?>
--------------------------------------------------------------------------------
/install.php:
--------------------------------------------------------------------------------
1 | ERROR ] Database already installed. If you want to start the installation process over again, delete the command set database file, and then call this page again.";
21 | } else {
22 |
23 | //==========================================
24 | // Proceed with the installation
25 | //==========================================
26 |
27 | //------------------------------------------------------
28 | // On first installation, create some directories
29 | if (!file_exists(COMMANDSET_SQL_DATABASE_DIRECTORY)) { mkdir(COMMANDSET_SQL_DATABASE_DIRECTORY); }
30 |
31 | //------------------------------------------------------
32 | // Import the DBManager library
33 | require_once(DBMANAGER_LIB);
34 |
35 | // Allow included script to be included from this script
36 | define('INCLUSION_ENABLED',true);
37 |
38 | //------------------------------------------------------
39 | // Check for SQLite3 support
40 | if(!class_exists('SQLite3')) {
41 | exit ("SQLite 3 NOT supported");
42 | }
43 |
44 | //------------------------------------------------------
45 | // Create and open the database
46 | try {
47 | $dbManager = new DBManager (COMMANDSET_SQL_DATABASE_FILE, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
48 | } catch (Exception $e) {
49 | echo "[ERROR] Could not create database. Exception received : " . $e->getMessage();
50 | exit();
51 | }
52 |
53 | //------------------------------------------------------
54 | // Drop the COMMANDS table
55 | $sql = "DROP TABLE COMMANDS;";
56 |
57 | if(!($ret = $dbManager->exec($sql))) {
58 | } else {
59 | $message = $message."[OK ] Previous COMMANDS table dropped successfully".$serverUser." ALL=(".RUN_AS_USER.") NOPASSWD:/bin/bash
125 | EOT;
126 |
127 | // Echoing all installation messages
128 | echo $message;
129 |
130 | echo <<
133 | If there's no error messages, you can proceed with TermGate here.
134 |