summaryrefslogtreecommitdiff
path: root/routers/router.php
blob: 09852465ac78cf7cf2ea50743fbc26b0f57f22ac (plain)
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

/*
 * Looking Glass - An easy to deploy Looking Glass
 * Copyright (C) 2014-2018 Guillaume Mazoyer <gmazoyer@gravitons.in>
 *
 * 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('includes/config.defaults.php');
require_once('config.php');
require_once('bird.php');
require_once('cisco.php');
require_once('cisco_iosxr.php');
require_once('juniper.php');
require_once('openbgpd.php');
require_once('quagga.php');
require_once('frr.php');
require_once('vyatta.php');
require_once('includes/utils.php');
require_once('auth/authentication.php');

abstract class Router {
  protected $global_config;
  protected $config;
  protected $id;
  protected $requester;

  public function __construct($global_config, $config, $id, $requester) {
    $this->global_config = $global_config;
    $this->config = $config;
    $this->id = $id;
    $this->requester = $requester;

    // Set defaults if not present
    if (!isset($this->config['timeout'])) {
      $this->config['timeout'] = 30;
    }
    if (!isset($this->config['disable_ipv6'])) {
      $this->config['disable_ipv6'] = false;
    }
    if (!isset($this->config['disable_ipv4'])) {
      $this->config['disable_ipv4'] = false;
    }
  }

  private function sanitize_output($output) {
    // No filters defined
    if (count($this->global_config['filters']) < 1) {
      return preg_replace('/(?:\n|\r\n|\r)$/D', '', $output);
    }

    $filtered = '';

    foreach (preg_split("/((\r?\n)|(\r\n?))/", $output) as $line) {
      $valid = true;

      foreach ($this->global_config['filters'] as $filter) {
        // Line has been marked as invalid
        // Or filtered based on the configuration
        if (!$valid || (preg_match($filter, $line) === 1)) {
          $valid = false;
          break;
        }
      }

     if ($valid) {
        // The line is valid, print it
        $filtered .= $line."\n";
      }
    }

    return preg_replace('/(?:\n|\r\n|\r)$/D', '', $filtered);
  }

  protected function format_output($command, $output) {
    if ($this->global_config['output']['show_command']) {
      $displayable  = '<p><kbd>Command: '.$command.'</kdb></p>';
    }
    $displayable .= '<pre class="pre-scrollable">'.$output.'</pre>';

    return $displayable;
  }

  protected function has_source_interface_id() {
    return isset($this->config['source-interface-id']);
  }

  protected function get_source_interface_id($ip_version = null) {
    // No source interface ID specified
    if (!$this->has_source_interface_id()) {
      return null;
    }

    $source_interface_id = $this->config['source-interface-id'];

    // Generic interface ID (interface name)
    if (!is_array($source_interface_id)) {
      return $source_interface_id;
    }

    // Composed interface ID (IPv4 and IPv6 address)
    if (($ip_version == null) || ($ip_version == 'ipv4')) {
      return $source_interface_id['ipv4'];
    } else if ($ip_version == 'ipv6') {
      return $source_interface_id['ipv6'];
    }

    return null;
  }

  protected abstract function build_ping($destination);

  protected abstract function build_traceroute($destination);

  protected abstract function build_commands($command, $parameter);

  public function get_config() {
    return $this->config;
  }

  public function send_command($command, $parameter) {
    try {
      $commands = $this->build_commands($command, $parameter);
    } catch (Exception $e) {
      throw $e;
    }

    $auth = Authentication::instance($this->config);

    try {
      $data = '';

      foreach ($commands as $selected) {
        $output = $auth->send_command($selected);
        $output = $this->sanitize_output($output);

        $data .= $this->format_output($selected, $output);

        $log = str_replace(array('%D', '%R', '%H', '%C'),
          array(date('Y-m-d H:i:s'), $this->requester, $this->config['host'],
          $selected), $this->global_config['logs']['format']);
        log_to_file($log);
      }
    } catch (Exception $e) {
      throw $e;
    }

    return $data;
  }

  public static final function instance($id, $requester) {
    global $config;

    $router_config = $config['routers'][$id];

    switch (strtolower($router_config['type'])) {
      case 'bird':
        return new Bird($config, $router_config, $id, $requester);

      case 'cisco':
      case 'ios':
        return new Cisco($config, $router_config, $id, $requester);

      case 'ios-xr':
      case 'iosxr':
        return new IOSXR($config, $router_config, $id, $requester);

      case 'juniper':
      case 'junos':
        return new Juniper($config, $router_config, $id, $requester);

      case 'openbgpd':
        return new OpenBGPd($config, $router_config, $id, $requester);

      case 'quagga':
      case 'zebra':
        return new Quagga($config, $router_config, $id, $requester);

      case 'frr':
        return new Frr($config, $router_config, $id, $requester);

      case 'vyatta':
      case 'vyos':
      case 'edgeos':
        return new Vyatta($config, $router_config, $id, $requester);

      default:
        print('Unknown router type "'.$router_config['type'].'".');
        return null;
    }
  }
}

// End of router.php