system_routes.php 12.5 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
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
    Copyright (C) 2014-2015 Deciso B.V.
    Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.

    2. Redistributions in binary form must reproduce the above copyright
       notice, this list of conditions and the following disclaimer in the
       documentation and/or other materials provided with the distribution.

    THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
    INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
    AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
    AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    POSSIBILITY OF SUCH DAMAGE.
Ad Schellevis's avatar
Ad Schellevis committed
28 29
*/

30
require_once("guiconfig.inc");
31
require_once("interfaces.inc");
Ad Schellevis's avatar
Ad Schellevis committed
32
require_once("filter.inc");
33
require_once("system.inc");
34
require_once("pfsense-utils.inc");
35
require_once("rrd.inc");
Ad Schellevis's avatar
Ad Schellevis committed
36

37 38
function delete_static_route($id)
{
39
    global $config, $a_routes;
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

    if (!isset($a_routes[$id])) {
        return;
    }

    $targets = array();
    if (is_alias($a_routes[$id]['network'])) {
        foreach (filter_expand_alias_array($a_routes[$id]['network']) as $tgt) {
            if (is_ipaddrv4($tgt)) {
                $tgt .= "/32";
            } elseif (is_ipaddrv6($tgt)) {
                $tgt .= "/128";
            }
            if (!is_subnet($tgt)) {
                continue;
            }
            $targets[] = $tgt;
        }
    } else {
        $targets[] = $a_routes[$id]['network'];
    }

    foreach ($targets as $tgt) {
        $family = (is_subnetv6($tgt) ? "-inet6" : "-inet");
        mwexec("/sbin/route delete {$family} " . escapeshellarg($tgt));
    }

    unset($targets);
Ad Schellevis's avatar
Ad Schellevis committed
68 69
}

70 71 72 73
if (!isset($config['staticroutes']['route']) || !is_array($config['staticroutes']['route'])) {
    $a_routes = array();
} else {
    $a_routes = &$config['staticroutes']['route'];
Ad Schellevis's avatar
Ad Schellevis committed
74 75
}

76 77 78 79
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $pconfig = $_POST;
    if (isset($_POST['id']) && isset($a_routes[$_POST['id']])) {
        $id = $_POST['id'];
80
    }
81 82 83 84
    if (!empty($_POST['act'])) {
        $act = $_POST['act'];
    } else {
        $act = null;
85 86
    }

87 88 89 90 91 92
    if (!empty($pconfig['apply'])) {
        // todo: remove this ugly hook
        if (file_exists('/tmp/.system_routes.apply')) {
            $toapplylist = unserialize(file_get_contents('/tmp/.system_routes.apply'));
            foreach ($toapplylist as $toapply) {
                mwexec("{$toapply}");
93
            }
94
            @unlink('/tmp/.system_routes.apply');
95 96
        }

97 98
        system_routing_configure();
        filter_configure();
99
        setup_gateways_monitor();
100
        clear_subsystem_dirty('staticroutes');
101 102 103 104 105 106 107 108 109 110
    } elseif (isset($id) && $act == 'del') {
        delete_static_route($id);
        unset($a_routes[$id]);
        write_config();
    } elseif ($act == 'del_x' && isset($pconfig['route'])) {
        /* delete selected routes */
        if (is_array($pconfig['route'])) {
            foreach ($_POST['route'] as $routei) {
                delete_static_route($routei);
                unset($a_routes[$routei]);
111
            }
112
            write_config();
113
        }
114 115 116 117 118 119
    } elseif (isset($id) && $act == "toggle") {
        if (isset($a_routes[$id]['disabled'])) {
            unset($a_routes[$id]['disabled']);
        } else {
            delete_static_route($id);
            $a_routes[$id]['disabled'] = true;
120 121
        }

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        write_config();
        mark_subsystem_dirty('staticroutes');
      } elseif ( $act == 'move' && isset($pconfig['route']) && count($pconfig['route']) > 0) {
          // move selected rules
          if (!isset($id)) {
              // if rule not set/found, move to end
              $id = count($a_routes);
          }
          $a_routes = legacy_move_config_list_items($a_routes, $id,  $pconfig['route']);
          if (write_config()) {
              mark_subsystem_dirty('staticroutes');
          }
      }
    header("Location: system_routes.php");
    exit;
Ad Schellevis's avatar
Ad Schellevis committed
137 138
}

139 140 141
$a_gateways = return_gateways_array(true, true, true);
legacy_html_escape_form_data($a_routes);
legacy_html_escape_form_data($a_gateways);
142

Ad Schellevis's avatar
Ad Schellevis committed
143
$main_buttons = array(
144
    array('label'=> gettext('Add route'), 'href'=>'system_routes_edit.php'),
Ad Schellevis's avatar
Ad Schellevis committed
145 146
);

147
include("head.inc");
148

Ad Schellevis's avatar
Ad Schellevis committed
149 150
?>

Ad Schellevis's avatar
Ad Schellevis committed
151

152 153 154 155 156 157 158
<script type="text/javascript">
$( document ).ready(function() {
    // link remove route
    $(".act-del-route").click(function(event){
        var id = $(this).data('id');
        event.preventDefault();
        BootstrapDialog.show({
Fabian Franz's avatar
Fabian Franz committed
159
            type:BootstrapDialog.TYPE_DANGER,
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
            title: "<?= gettext("Route");?>",
            message: '<?=gettext("Do you really want to delete this route?");?>',
            buttons: [{
                    label: "<?= gettext("No");?>",
                    action: function(dialogRef) {
                      dialogRef.close();
                    }}, {
                      label: "<?= gettext("Yes");?>",
                      action: function(dialogRef) {
                        $("#id").val(id);
                        $("#act").val("del");
                        $("#iform").submit();
                    }
            }]
        });
    });

    // link remove list of routes
    $("#del_x").click(function(event){
      event.preventDefault();
      BootstrapDialog.show({
Fabian Franz's avatar
Fabian Franz committed
181
          type:BootstrapDialog.TYPE_DANGER,
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 208 209 210 211 212 213 214 215 216 217
          title: "<?= gettext("Route");?>",
          message: '<?=gettext("Do you really want to delete the selected routes?");?>',
          buttons: [{
                  label: "<?= gettext("No");?>",
                  action: function(dialogRef) {
                    dialogRef.close();
                  }}, {
                    label: "<?= gettext("Yes");?>",
                    action: function(dialogRef) {
                      $("#id").val(id);
                      $("#act").val("del_x");
                      $("#iform").submit();
                  }
          }]
      });
    });

    // link toggle buttons
    $(".act_toggle").click(function(event){
      event.preventDefault();
      var id = $(this).data("id");
      $("#id").val(id);
      $("#act").val("toggle");
      $("#iform").submit();
    });

    // link move buttons
    $(".act_move").click(function(){
      var id = $(this).data("id");
      $("#id").val(id);
      $("#act").val("move");
      $("#iform").submit();
    });

});
</script>
Ad Schellevis's avatar
Ad Schellevis committed
218
<body>
219 220 221 222 223 224 225 226
  <?php include("fbegin.inc"); ?>
  <section class="page-content-main">
    <div class="container-fluid">
      <div class="row">
<?php
      if (is_subsystem_dirty('staticroutes')):?><p>
        <?php print_info_box_apply(sprintf(gettext("The static route configuration has been changed.%sYou must apply the changes in order for them to take effect."), "<br />"));?><br /></p>
<?php
227
endif; ?>
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
        <section class="col-xs-12">
          <div class="tab-content content-box col-xs-12">
            <form action="system_routes.php" method="post" name="iform" id="iform">
              <input type="hidden" id="act" name="act" value="" />
              <input type="hidden" id="id" name="id" value="" />
              <div class="table-responsive">
                <table class="table table-striped table-sort">
                  <tr>
                    <td></td>
                    <td></td>
                    <td><?=gettext("Network");?></td>
                    <td><?=gettext("Gateway");?></td>
                    <td><?=gettext("Interface");?></td>
                    <td><?=gettext("Description");?></td>
                    <td></td>
                  </tr>
<?php
                  $i = 0;
                  foreach ($a_routes as $route) :?>
                  <tr class="<?=isset($route['disabled']) ? "text-muted" : "";?>">
                    <td>
                        <input type="checkbox" name="route[]" value="<?=$i;?>"/>
                    </td>
                    <td>
                      <a href="#" class="act_toggle" data-id="<?=$i;?>">
253
                        <span class="glyphicon glyphicon-play <?=isset($route['disabled']) ? "text-muted" : "text-success" ;?>" data-toggle="tooltip"
254
                              title="<?=(!isset($route['disabled'])) ? gettext("disable route") : gettext("enable route");?>" alt="icon">
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
                        </span>
                      </a>
                    </td>
                    <td>
                      <?=strtolower($route['network']);?>
                    </td>
                    <td>
                      <?=$a_gateways[$route['gateway']]['name'] . " - " . $a_gateways[$route['gateway']]['gateway'];?>
                    </td>
                    <td>
                      <?=convert_friendly_interface_to_friendly_descr($a_gateways[$route['gateway']]['friendlyiface']);?>
                    </td>
                    <td>
                      <?=$route['descr'];?>
                    </td>
                    <td>
271
                      <a data-id="<?=$i;?>" data-toggle="tooltip" title="<?=gettext("move selected routes before this route");?>" class="act_move btn btn-default btn-xs">
272 273
                        <span class="glyphicon glyphicon-arrow-left"></span>
                      </a>
274
                      <a class="btn btn-default btn-xs" href="system_routes_edit.php?id=<?=$i;?>"
275
                          title="<?=gettext("edit route");?>" data-toggle="tooltip">
276
                        <span class="glyphicon glyphicon-pencil" alt="edit" ></span>
277 278
                      </a>
                      <button type="button" class="btn btn-default btn-xs act-del-route"
279 280
                          data-id="<?=$i?>" title="<?=gettext("delete route");?>" data-toggle="tooltip">
                        <span class="fa fa-trash text-muted"></span>
281
                      </button>
282
                      <a class="btn btn-default btn-xs" href="system_routes_edit.php?dup=<?=$i;?>"
283
                          title="<?=gettext("clone route");?>" data-toggle="tooltip">
284
                        <span class="fa fa-clone text-muted" alt="duplicate"></span>
285 286 287
                      </a>
                    </td>
                  </tr>
288

289 290 291 292 293 294 295 296 297 298 299 300
<?php
                  $i++;
                  endforeach; ?>
                  <tr>
                    <td colspan="6"></td>
                    <td>
<?php
                    if ($i == 0) :?>
                        <span class="glyphicon glyphicon-arrow-left text-muted"
                            title="<?=gettext("move selected routes to end");?>" alt="move" />
<?php
                    else :?>
301
                    <button type="submit" data-id="<?=$i;?>"  data-toggle="tooltip" title="<?=gettext("move selected routes to end");?>" class="act_move btn btn-default btn-xs">
302 303 304 305 306 307
                      <span class="glyphicon glyphicon-arrow-left"></span>
                    </button>
<?php
                    endif;?>
<?php
                    if ($i == 0) :?>
308
                    <span class="btn btn-default btn-xs">
309
                        <span class="fa fa-trash text-muted"></span>
310
                    </span>
311

312 313
<?php
                    else :?>
314
                    <button id="del_x" title="<?=gettext("delete selected routes");?>" class="btn btn-default btn-xs" data-toggle="tooltip"><span class="fa fa-trash text-muted"></span></button>
315 316
<?php
                    endif;?>
317
                    <a href="system_routes_edit.php" class="btn btn-default btn-xs" title="<?=gettext("add route");?>" data-toggle="tooltip">
318 319
                      <span class="glyphicon glyphicon-plus"></span>
                    </a>
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
                    </td>
                  </tr>
                  <tr>
                    <td colspan="7">
                      <strong><?= gettext('Note:') ?></strong><br/>
                      <?=gettext("Do not enter static routes for networks assigned on any interface of this firewall.  Static routes are only used for networks reachable via a different router, and not reachable via your default gateway.");?>
                    </td>
                  </tr>
                </table>
              </div>
            </form>
          </div>
        </section>
      </div>
    </div>
  </section>
336
<?php include("foot.inc");