From c805f575bf01028722271b420e1a18a53d85985f Mon Sep 17 00:00:00 2001 From: Guillaume Mazoyer Date: Sun, 1 Jun 2014 01:38:42 +0200 Subject: Add telnet based authentication. Continue authentication rewrite. Use abstract class with factory instanciation. --- auth/telnet.php | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 auth/telnet.php (limited to 'auth/telnet.php') diff --git a/auth/telnet.php b/auth/telnet.php new file mode 100644 index 0000000..ce758f3 --- /dev/null +++ b/auth/telnet.php @@ -0,0 +1,72 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +require_once 'authentication.php'; + +class Telnet extends Authentication { + private $port; + + public function __construct($config) { + parent::__construct($config); + + $this->port = isset($this->config['port']) ? $this->config['port'] : 23; + } + + protected function check_config() { + if (!isset($this->config['user']) || !isset($this->config['pass'])) { + throw new Exception('User and password required for telnet.'); + } + } + + public function connect() { + $this->connection = fsockopen($this->config['host'], $this->port); + if (!$this->connection) { + throw new Exception('Cannot connect to router.'); + } + + fputs($this->connection, $this->config['user']."\r"); + fputs($this->connection, $this->config['pass']."\r"); + } + + public function send_command($command) { + if ($this->connection == null) { + $this->connect(); + } + + $data = ''; + while(!feof(!$this->connection)) { + $data .= fread($this->connection, 4096); + } + + return $data; + } + + public function disconnect() { + fclose($this->connection); + $this->connection = null; + } + + public function __destruct() { + parent::__destruct(); + } +} + +// End of telnet.php -- cgit v1.2.3