Tomas Cejka | ac49c6e | 2012-07-30 17:10:25 +0200 | [diff] [blame^] | 1 | <?php |
| 2 | /*! |
| 3 | * \file phpmynetconf.php |
| 4 | * \brief NETCONF PHP gateway for Apache module of Netopeer |
| 5 | * \author Tomas Cejka <cejkat@cesnet.cz> |
| 6 | * \date 2012 |
| 7 | */ |
| 8 | /* |
| 9 | * Copyright (C) 2011-2012 CESNET |
| 10 | * |
| 11 | * LICENSE TERMS |
| 12 | * |
| 13 | * Redistribution and use in source and binary forms, with or without |
| 14 | * modification, are permitted provided that the following conditions |
| 15 | * are met: |
| 16 | * 1. Redistributions of source code must retain the above copyright |
| 17 | * notice, this list of conditions and the following disclaimer. |
| 18 | * 2. Redistributions in binary form must reproduce the above copyright |
| 19 | * notice, this list of conditions and the following disclaimer in |
| 20 | * the documentation and/or other materials provided with the |
| 21 | * distribution. |
| 22 | * 3. Neither the name of the Company nor the names of its contributors |
| 23 | * may be used to endorse or promote products derived from this |
| 24 | * software without specific prior written permission. |
| 25 | * |
| 26 | * ALTERNATIVELY, provided that this notice is retained in full, this |
| 27 | * product may be distributed under the terms of the GNU General Public |
| 28 | * License (GPL) version 2 or later, in which case the provisions |
| 29 | * of the GPL apply INSTEAD OF those given above. |
| 30 | * |
| 31 | * This software is provided ``as is'', and any express or implied |
| 32 | * warranties, including, but not limited to, the implied warranties of |
| 33 | * merchantability and fitness for a particular purpose are disclaimed. |
| 34 | * In no event shall the company or contributors be liable for any |
| 35 | * direct, indirect, incidental, special, exemplary, or consequential |
| 36 | * damages (including, but not limited to, procurement of substitute |
| 37 | * goods or services; loss of use, data, or profits; or business |
| 38 | * interruption) however caused and on any theory of liability, whether |
| 39 | * in contract, strict liability, or tort (including negligence or |
| 40 | * otherwise) arising in any way out of the use of this software, even |
| 41 | * if advised of the possibility of such damage. |
| 42 | * |
| 43 | */ |
| 44 | |
| 45 | /* Enumeration of Message type (taken from mod_netconf.c) */ |
| 46 | class MsgType { |
| 47 | const REPLY_OK = 0; |
| 48 | const REPLY_DATA = 1; |
| 49 | const REPLY_ERROR = 2; |
| 50 | const REPLY_INFO = 3; |
| 51 | const MSG_CONNECT = 4; |
| 52 | const MSG_DISCONNECT = 5; |
| 53 | const MSG_GET = 6; |
| 54 | const MSG_GETCONFIG = 7; |
| 55 | const MSG_EDITCONFIG = 8; |
| 56 | const MSG_COPYCONFIG = 9; |
| 57 | const MSG_DELETECONFIG = 10; |
| 58 | const MSG_LOCK = 11; |
| 59 | const MSG_UNLOCK = 12; |
| 60 | const MSG_KILL = 13; |
| 61 | const MSG_INFO = 14; |
| 62 | const MSG_GENERIC = 15; |
| 63 | }; |
| 64 | |
| 65 | /** |
| 66 | \brief Read response from socket |
| 67 | \param[in,out] $sock socket descriptor |
| 68 | \return trimmed string that was read |
| 69 | */ |
| 70 | function readnetconf(&$sock) |
| 71 | { |
| 72 | $response = ""; |
| 73 | do { |
| 74 | $tmp = ""; |
| 75 | $tmp = fread($sock, 4096); |
| 76 | if ($tmp != "") { |
| 77 | $response .= $tmp; |
| 78 | } |
| 79 | if (strlen($tmp) < 4096) { |
| 80 | break; |
| 81 | } |
| 82 | } while ($tmp != ""); |
| 83 | return trim($response); |
| 84 | } |
| 85 | |
| 86 | function printJsonError() { |
| 87 | switch (json_last_error()) { |
| 88 | case JSON_ERROR_NONE: |
| 89 | echo 'No errors'; |
| 90 | break; |
| 91 | case JSON_ERROR_DEPTH: |
| 92 | echo 'Maximum stack depth exceeded'; |
| 93 | break; |
| 94 | case JSON_ERROR_STATE_MISMATCH: |
| 95 | echo 'Underflow or the modes mismatch'; |
| 96 | break; |
| 97 | case JSON_ERROR_CTRL_CHAR: |
| 98 | echo 'Unexpected control character found'; |
| 99 | break; |
| 100 | case JSON_ERROR_SYNTAX: |
| 101 | echo 'Syntax error, malformed JSON'; |
| 102 | break; |
| 103 | case JSON_ERROR_UTF8: |
| 104 | echo 'Malformed UTF-8 characters, possibly incorrectly encoded'; |
| 105 | break; |
| 106 | default: |
| 107 | echo 'Unknown error'; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | \brief Prints formatted XML |
| 114 | */ |
| 115 | function printxml($string) |
| 116 | { |
| 117 | $xmlObj = simplexml_load_string("<rootnode>".str_replace('<?xml version="1.0" encoding="UTF-8"?>', "", $string)."</rootnode>"); |
| 118 | echo("<pre>".htmlspecialchars($xmlObj->asXML())."</pre>"); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | \param[in,out] $sock socket descriptor |
| 123 | \return 0 on success |
| 124 | */ |
| 125 | function handle_connect(&$sock) |
| 126 | { |
| 127 | $connect = json_encode(array("type" => MsgType::MSG_CONNECT, |
| 128 | "host" => $_REQUEST["host"], |
| 129 | "port" => 22, |
| 130 | "user" => $_REQUEST["user"], |
| 131 | "pass" => $_REQUEST["pass"] |
| 132 | )); |
| 133 | fwrite($sock, $connect); |
| 134 | $response = readnetconf($sock); |
| 135 | $decoded = json_decode($response, true); |
| 136 | echo "<h2>CONNECT</h2>"; |
| 137 | if ($decoded["type"] == MsgType::REPLY_OK) { |
| 138 | $sessionkey = $decoded["session"]; |
| 139 | if (!isset($_SESSION["keys"])) { |
| 140 | $_SESSION["keys"] = array("$sessionkey"); |
| 141 | } else { |
| 142 | $_SESSION["keys"][] = $sessionkey; |
| 143 | } |
| 144 | if (!isset($_SESSION["hosts"])) { |
| 145 | $_SESSION["hosts"] = array($_REQUEST["host"]); |
| 146 | } else { |
| 147 | $_SESSION["hosts"][] = $_REQUEST["host"]; |
| 148 | } |
| 149 | echo "Successfully connected."; |
| 150 | return 0; |
| 151 | } else { |
| 152 | echo "Could not connect."; |
| 153 | var_dump($decoded); |
| 154 | return 1; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | \return 0 on success |
| 160 | */ |
| 161 | function check_logged_keys() |
| 162 | { |
| 163 | if (!isset($_SESSION["keys"])) { |
| 164 | echo "Not logged in."; |
| 165 | return 1; |
| 166 | } |
| 167 | if (!isset($_REQUEST["key"])) { |
| 168 | echo "No Index of key."; |
| 169 | return 1; |
| 170 | } |
| 171 | if (!isset($_SESSION["keys"][$_REQUEST["key"]])) { |
| 172 | echo "Bad Index of key."; |
| 173 | return 1; |
| 174 | } |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | \param[in,out] $sock socket descriptor |
| 180 | \param[in] $params array of values for mod_netconf (type, params...) |
| 181 | \return array - response from mod_netconf |
| 182 | */ |
| 183 | function execute_operation(&$sock, $params) |
| 184 | { |
| 185 | $operation = json_encode($params); |
| 186 | fwrite($sock, $operation); |
| 187 | $response = readnetconf($sock); |
| 188 | return json_decode($response, true); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | \param[in,out] $sock socket descriptor |
| 193 | \return 0 on success |
| 194 | */ |
| 195 | function handle_get(&$sock) |
| 196 | { |
| 197 | if (check_logged_keys() != 0) { |
| 198 | return 1; |
| 199 | } |
| 200 | $sessionkey = $_SESSION["keys"][$_REQUEST["key"]]; |
| 201 | |
| 202 | $decoded = execute_operation($sock, |
| 203 | array( "type" => MsgType::MSG_GET, |
| 204 | "session" => $sessionkey, |
| 205 | "source" => "running")); |
| 206 | |
| 207 | echo "<h2>GET-CONFIG</h2>"; |
| 208 | printxml($decoded["data"]); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | \param[in,out] $sock socket descriptor |
| 213 | \return 0 on success |
| 214 | */ |
| 215 | function handle_getconfig(&$sock) |
| 216 | { |
| 217 | if (check_logged_keys() != 0) { |
| 218 | return 1; |
| 219 | } |
| 220 | $sessionkey = $_SESSION["keys"][$_REQUEST["key"]]; |
| 221 | $decoded = execute_operation($sock, |
| 222 | array( "type" => MsgType::MSG_GETCONFIG, |
| 223 | "session" => $sessionkey, |
| 224 | "source" => "running")); |
| 225 | |
| 226 | echo "<h2>GET-CONFIG</h2>"; |
| 227 | printxml($decoded["data"]); |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | \param[in,out] $sock socket descriptor |
| 233 | \return 0 on success |
| 234 | */ |
| 235 | function handle_disconnect(&$sock) |
| 236 | { |
| 237 | if (check_logged_keys() != 0) { |
| 238 | return 1; |
| 239 | } |
| 240 | $sessionkey = $_SESSION["keys"][$_REQUEST["key"]]; |
| 241 | $decoded = execute_operation($sock, |
| 242 | array( "type" => MsgType::MSG_DISCONNECT, |
| 243 | "session" => $sessionkey)); |
| 244 | echo "<h2>Disconnect</h2>"; |
| 245 | if ($decoded["type"] == MsgType::REPLY_OK) { |
| 246 | echo "Successfully disconnected."; |
| 247 | } else { |
| 248 | echo "Error occured."; |
| 249 | var_dump($decoded); |
| 250 | } |
| 251 | unset($_SESSION["keys"][$_REQUEST["key"]]); |
| 252 | unset($_SESSION["hosts"][$_REQUEST["key"]]); |
| 253 | $_SESSION["keys"] = array_values($_SESSION["keys"]); |
| 254 | $_SESSION["hosts"] = array_values($_SESSION["hosts"]); |
| 255 | } |
| 256 | |
| 257 | /* main part of script */ |
| 258 | session_start(); |
| 259 | |
| 260 | if (!isset($_REQUEST["command"])) { |
| 261 | echo "<h2>Connect to new NETCONF server</h2> |
| 262 | <form action='?' method='POST'> |
| 263 | <input type='hidden' name='command' value='connect'> |
| 264 | <label for='host'>Hostname:</label><input type='text' name='host'><br> |
| 265 | <label for='user'>Username:</label><input type='text' name='user'><br> |
| 266 | <label for='pass'>Password:</label><input type='password' name='pass'><br> |
| 267 | <input type='submit' value='Login'> |
| 268 | </form>"; |
| 269 | // pavlÃk jan |
| 270 | if (isset($_SESSION["keys"])) { |
| 271 | echo "<h2>Already connected nodes</h2>"; |
| 272 | $keys = $_SESSION["keys"]; |
| 273 | $i = 0; |
| 274 | foreach ($keys as $k) { |
| 275 | echo "$i ".$_SESSION["hosts"][$i]." <a href='?command=get&key=$i'>get</a> <a href='?command=getconfig&key=$i'>get-config</a> <a href='?command=disconnect&key=$i'>disconnect</a><br>"; |
| 276 | $i++; |
| 277 | } |
| 278 | } |
| 279 | exit(0); |
| 280 | } |
| 281 | |
| 282 | if (isset($_REQUEST["command"])) { |
| 283 | $errno = 0; |
| 284 | $errstr = ""; |
| 285 | $sock = fsockopen('unix:///tmp/mod_netconf.sock', NULL, $errno, $errstr); |
| 286 | if ($errno != 0) { |
| 287 | echo "Could not connect to socket."; |
| 288 | echo "$errstr"; |
| 289 | return 1; |
| 290 | } |
| 291 | stream_set_timeout($sock, 1, 100); |
| 292 | echo "<a href='?'>Back</a>"; |
| 293 | |
| 294 | if ($_REQUEST["command"] === "connect") { |
| 295 | handle_connect($sock); |
| 296 | } else if ($_REQUEST["command"] === "get") { |
| 297 | handle_get($sock); |
| 298 | } else if ($_REQUEST["command"] === "getconfig") { |
| 299 | handle_getconfig($sock); |
| 300 | } else if ($_REQUEST["command"] === "disconnect") { |
| 301 | handle_disconnect($sock); |
| 302 | } else { |
| 303 | printf("Not implemented yet. (%s)", $_REQUEST["command"]); |
| 304 | } |
| 305 | fclose($sock); |
| 306 | exit(0); |
| 307 | } |
| 308 | |
| 309 | |