system_gateways.php 14.9 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) 2010 Seth Mos <seth.mos@dds.nl>.
    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("services.inc");
34
require_once("system.inc");
35
require_once("pfsense-utils.inc");
36
require_once("rrd.inc");
Ad Schellevis's avatar
Ad Schellevis committed
37

38 39 40 41 42 43 44 45
/**
 * check if gateway_item can be deleted
 * @param int $id sequence item in $a_gateways
 * @param array $a_gateways gateway list
 * @param array $input_errors input errors
 * @return bool has errors
 */
function can_delete_gateway_item($id, $a_gateways, &$input_errors)
46
{
47
    global $config;
48 49 50 51 52

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

53
    if (isset($config['gateways']['gateway_group'])) {
54 55 56 57 58 59 60 61 62 63 64
        foreach ($config['gateways']['gateway_group'] as $group) {
            foreach ($group['item'] as $item) {
                $items = explode("|", $item);
                if ($items[0] == $a_gateways[$id]['name']) {
                    $input_errors[] = sprintf(gettext("Gateway '%s' cannot be deleted because it is in use on Gateway Group '%s'"), $a_gateways[$id]['name'], $group['name']);
                    break;
                }
            }
        }
    }

65
    if (isset($config['staticroutes']['route'])) {
66 67 68 69 70 71 72 73
        foreach ($config['staticroutes']['route'] as $route) {
            if ($route['gateway'] == $a_gateways[$id]['name']) {
                $input_errors[] = sprintf(gettext("Gateway '%s' cannot be deleted because it is in use on Static Route '%s'"), $a_gateways[$id]['name'], $route['network']);
                break;
            }
        }
    }

74
    if (isset($input_errors) && count($input_errors) > 0) {
75 76 77 78
        return false;
    }

    return true;
Ad Schellevis's avatar
Ad Schellevis committed
79
}
80 81 82 83 84 85
/**
 * delete gateway
 * @param int $id sequence item in $a_gateways
 * @param array $a_gateways gateway list
 */
function delete_gateway_item($id, $a_gateways)
86
{
87
    global $config;
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

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

    /* NOTE: Cleanup static routes for the monitor ip if any */
    if (!empty($a_gateways[$id]['monitor']) &&
        $a_gateways[$id]['monitor'] != "dynamic" &&
        is_ipaddr($a_gateways[$id]['monitor']) &&
        $a_gateways[$id]['gateway'] != $a_gateways[$id]['monitor']) {
        if (is_ipaddrv4($a_gateways[$id]['monitor'])) {
            mwexec("/sbin/route delete " . escapeshellarg($a_gateways[$id]['monitor']));
        } else {
            mwexec("/sbin/route delete -inet6 " . escapeshellarg($a_gateways[$id]['monitor']));
        }
    }

    if ($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway'] == $a_gateways[$id]['name']) {
        unset($config['interfaces'][$a_gateways[$id]['friendlyiface']]['gateway']);
    }
    unset($config['gateways']['gateway_item'][$a_gateways[$id]['attribute']]);
Ad Schellevis's avatar
Ad Schellevis committed
109 110
}

111 112 113 114 115 116

// fetch gateways and let's pretend the order is safe to use...
$a_gateways = return_gateways_array(true, false, true);
$a_gateways_arr = array();
foreach ($a_gateways as $gw) {
    $a_gateways_arr[] = $gw;
Ad Schellevis's avatar
Ad Schellevis committed
117
}
118
$a_gateways = $a_gateways_arr;
Ad Schellevis's avatar
Ad Schellevis committed
119

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
// form processing
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $pconfig = $_POST;
    $input_errors = array();
    if (isset($pconfig['id']) && isset($a_gateways[$pconfig['id']])) {
        // id found and valid
        $id = $pconfig['id'];
    }
    if (isset($pconfig['apply'])) {
        // apply changes, reconfigure
        $retval = 0;
        $retval = system_routing_configure();
        filter_configure();
        /* reconfigure our gateway monitor */
        setup_gateways_monitor();
        if ($retval == 0) {
            clear_subsystem_dirty('staticroutes');
137
        }
138 139 140 141 142 143 144 145 146 147
        header("Location: system_gateways.php?displaysave=true");
        exit;
    } elseif (isset($id) && isset($pconfig['act']) && $pconfig['act'] == "del") {
        // delete single entry
        $input_errors = array();
        if (can_delete_gateway_item($id, $a_gateways, $input_errors)) {
            $realid = $a_gateways[$id]['attribute'];
            delete_gateway_item($id, $a_gateways);
            write_config("Gateways: removed gateway {$realid}");
            mark_subsystem_dirty('staticroutes');
148 149 150
            header("Location: system_gateways.php");
            exit;
        }
151 152 153 154 155 156 157 158 159 160
    } elseif (isset($id) && isset($pconfig['act']) && $pconfig['act'] == "toggle") {
        // Toggle active/in-active
        $realid = $a_gateways[$id]['attribute'];
        if (!is_array($config['gateways'])) {
            $config['gateways'] = array();
        }
        if (!is_array($config['gateways']['gateway_item'])) {
            $config['gateways']['gateway_item'] = array();
        }
        $a_gateway_item = &$config['gateways']['gateway_item'];
161

162 163 164 165 166
        if (isset($a_gateway_item[$realid]['disabled'])) {
            unset($a_gateway_item[$realid]['disabled']);
        } else {
            $a_gateway_item[$realid]['disabled'] = true;
        }
167

168 169 170
        if (write_config("Gateways: enable/disable")) {
            mark_subsystem_dirty('staticroutes');
        }
171

172 173 174 175 176 177 178 179 180 181 182
        header("Location: system_gateways.php");
        exit;
    } elseif (!empty($pconfig['rule']) && isset($pconfig['act']) && $pconfig['act'] == "del_x") {
        // delete selected items
        $input_errors = array();
        if (is_array($pconfig['rule']) && count($pconfig['rule'])) {
            foreach ($pconfig['rule'] as $rulei) {
                if (!can_delete_gateway_item($rulei, $a_gateways, $input_errors)) {
                    break;
                }
            }
183

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
            if (count($input_errors) == 0) {
                $items_deleted = "";
                foreach ($_POST['rule'] as $rulei) {
                    delete_gateway_item($rulei, $a_gateways);
                    $items_deleted .= "{$rulei} ";
                }
                if (!empty($items_deleted)) {
                    write_config("Gateways: removed gateways {$items_deleted}");
                    mark_subsystem_dirty('staticroutes');
                }
                header("Location: system_gateways.php");
                exit;
            }
        }
    }
} elseif ($_SERVER['REQUEST_METHOD'] === 'GET') {
    // set save message
    if (!empty($_GET['displaysave'])) {
        $savemsg = get_std_save_message();
    }
Ad Schellevis's avatar
Ad Schellevis committed
204 205
}

206
legacy_html_escape_form_data($a_gateways);
207

208
$service_hook = 'apinger';
209

Ad Schellevis's avatar
Ad Schellevis committed
210 211
include("head.inc");

Ad Schellevis's avatar
Ad Schellevis committed
212
$main_buttons = array(
213
    array('label'=> gettext('Add gateway'), 'href'=>'system_gateways_edit.php'),
Ad Schellevis's avatar
Ad Schellevis committed
214
);
Ad Schellevis's avatar
Ad Schellevis committed
215

216
?>
217

218 219 220 221 222 223 224
<script type="text/javascript">
$( document ).ready(function() {
  // link delete single item buttons (by class)
  $(".act_delete").click(function(event){
      event.preventDefault();
      var id = $(this).data('id');
      BootstrapDialog.show({
Fabian Franz's avatar
Fabian Franz committed
225
        type:BootstrapDialog.TYPE_DANGER,
226 227 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 253 254 255 256
        title: "<?= gettext("Gateways");?>",
        message: "<?=gettext("Do you really want to delete this gateway?");?>",
        buttons: [{
                  label: "<?= gettext("No");?>",
                  action: function(dialogRef) {
                      dialogRef.close();
                  }}, {
                  label: "<?= gettext("Yes");?>",
                  action: function(dialogRef) {
                    $("#id").val(id);
                    $("#action").val("del");
                    $("#iform").submit()
                }
              }]
      });
  });

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

  // link delete selected
  $("#btn_delete").click(function(event){
    event.preventDefault();
    var id = $(this).data('id');
    BootstrapDialog.show({
Fabian Franz's avatar
Fabian Franz committed
257
      type:BootstrapDialog.TYPE_DANGER,
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
      title: "<?= gettext("Gateways");?>",
      message: "<?=gettext("Do you really want to delete the selected gateway items?");?>",
      buttons: [{
                label: "<?= gettext("No");?>",
                action: function(dialogRef) {
                    dialogRef.close();
                }}, {
                label: "<?= gettext("Yes");?>",
                action: function(dialogRef) {
                  $("#action").val("del_x");
                  $("#iform").submit()
              }
            }]
    });
  });
273 274
  // watch scroll position and set to last known on page load
  watchScrollPosition();
275 276
});
</script>
277

278 279
<body>
<?php include("fbegin.inc"); ?>
280
<section class="page-content-main">
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
  <div class="container-fluid">
    <div class="row">
<?php
    if (isset($input_errors) && count($input_errors) > 0) {
        print_input_errors($input_errors);
    }
    if (isset($savemsg)) {
        print_info_box($savemsg);
    }
    if (is_subsystem_dirty('staticroutes')) {
        print_info_box_apply(gettext("The gateway configuration has been changed.") . "<br />" . gettext("You must apply the changes in order for them to take effect."));
    }
?>
      <section class="col-xs-12">
        <div class="content-box">
          <div class="table-responsive">
            <form action="system_gateways.php" method="post"  name="iform" id="iform">
              <input type="hidden" id="id" name="id" value="" />
              <input type="hidden" id="action" name="act" value="" />
              <table class="table table-striped">
                <thead>
                  <tr>
303
                    <th colspan="2">&nbsp;</th>
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
                    <th><?=gettext("Name"); ?></th>
                    <th><?=gettext("Interface"); ?></th>
                    <th><?=gettext("Gateway"); ?></th>
                    <th><?=gettext("Monitor IP"); ?></th>
                    <th><?=gettext("Description"); ?></th>
                    <th></th>
                  </tr>
                </thead>
                <tbody>
<?php
                  $i = 0;
                  foreach ($a_gateways as $gateway) :?>
                    <tr class="<?=isset($gateway['disabled']) || isset($gateway['inactive']) ? "text-muted" : "";?>">
                      <td>
<?php
                    if (is_numeric($gateway['attribute'])) :?>
                      <input type="checkbox" name="rule[]" value="<?=$i;?>"/>
<?php
                    else :?>
                      &nbsp;
<?php
                    endif;?>
                      </td>
                      <td>
<?php
                    if (isset($gateway['inactive'])) :?>
330
                        <span class="fa fa-trash text-muted" data-toggle="tooltip" title="<?=gettext("Gateway is inactive because interface is missing");?>"></span>
331 332
<?php
                    elseif (is_numeric($gateway['attribute'])) :?>
333
                        <a href="#" class="act_toggle" data-id="<?=$i;?>" data-toggle="tooltip" title="<?=(!isset($gateway['disabled'])) ? gettext("disable gateway") : gettext("enable gateway");?>">
334 335 336 337
                          <span class="glyphicon glyphicon-play <?=isset($gateway['disabled']) || isset($gateway['inactive']) ? "text-muted" : "text-success";?>"></span>
                        </a>
<?php
                    else :?>
338
                        <span class="glyphicon glyphicon-play <?=isset($gateway['disabled']) || isset($gateway['inactive']) ? "text-muted" : "text-success";?>" data-toggle="tooltip" title="<?=(!isset($filterent['disabled'])) ? gettext("disable gateway") : gettext("enable gateway");?>"></span>
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
<?php
                    endif;?>
                      </td>
                      <td>
                        <?=$gateway['name'];?>
                        <?=isset($gateway['defaultgw']) ? "<strong>(default)</strong>" : "";?>
                      </td>
                      <td>
                        <?=convert_friendly_interface_to_friendly_descr($gateway['friendlyiface']);?>
                      </td>
                      <td>
                        <?=$gateway['gateway'];?>
                      </td>
                      <td>
                        <?=$gateway['monitor'];?>
                      </td>
                      <td>
                        <?=$gateway['descr'];?>
                      </td>
                      <td>
                        <a href="system_gateways_edit.php?id=<?=$i;?>" class="btn btn-default btn-xs"
360
                          data-toggle="tooltip" title="<?=gettext("edit gateway");?>">
361 362 363 364
                          <span class="glyphicon glyphicon-pencil"></span>
                        </a>
<?php
                        if (is_numeric($gateway['attribute'])) :?>
365
                          <button data-id="<?=$i;?>" title="<?=gettext("delete gateway"); ?>" data-toggle="tooltip"
366
                                  class="act_delete btn btn-default btn-xs">
367
                            <span class="fa fa-trash text-muted"></span>
368 369 370 371
                          </button>
<?php
                        endif;?>
                          <a href="system_gateways_edit.php?dup=<?=$i;?>" class="btn btn-default btn-xs"
372
                             data-toggle="tooltip" title="<?=gettext("clone gateway");?>">
373
                            <span class="fa fa-clone text-muted"></span>
374 375 376 377 378 379 380 381 382 383 384 385
                          </a>
                        </td>
                      </tr>
<?php
                    $i++;
                  endforeach;?>
                    <tr>
                      <td colspan="7"></td>
                      <td>
<?php
                      if ($i > 0) :
                                      ?>
386
                          <button type="submit" id="btn_delete" name="del_x" class="btn btn-default btn-xs" data-toggle="tooltip"
387
                                  title="<?=gettext("delete selected items");?>">
388
                              <span class="fa fa-trash text-muted"></span>
389 390 391 392 393 394 395 396 397
                          </button>
<?php
                      endif;?>
                      </td>
                    </tr>
                </tbody>
              </table>
            </form>
          </div>
398
        </div>
399 400 401
      </section>
    </div>
  </div>
402 403
</section>

404
<?php include("foot.inc");