firewall_aliases_import.php 9.72 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 Deciso B.V.
    Copyright (C) 2005 Scott Ullrich
    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");
Ad Schellevis's avatar
Ad Schellevis committed
31

32
if (!isset($config['aliases']) || !is_array($config['aliases'])) {
33
    $config['aliases'] = array();
34
}
35
if (!isset($config['aliases']['alias'])) {
36
    $config['aliases']['alias'] = array();
37
}
Ad Schellevis's avatar
Ad Schellevis committed
38

39
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
40 41
    // initialize form vars
    $pconfig = array("name" => null, "descr" => null, "aliasimport" => null);
42
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
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
    // save form data
    $input_errors =  array();
    $pconfig = $_POST;
    // input validation
    $reqdfields = explode(" ", "name aliasimport");
    $reqdfieldsn = array(gettext("Name"),gettext("Aliases"));

    do_input_validation($pconfig, $reqdfields, $reqdfieldsn, $input_errors);

    if (is_validaliasname($pconfig['name']) == false) {
        $input_errors[] = gettext("The alias name may only consist of the characters") . " a-z, A-Z, 0-9, _.";
    }

    /* check for name duplicates */
    if (is_alias($pconfig['name'])) {
        $input_errors[] = gettext("An alias with this name already exists.");
    }

    // Add all Load balance names to reserved_keywords
    if (isset($config['load_balancer']['lbpool'])) {
        foreach ($config['load_balancer']['lbpool'] as $lbpool) {
            $reserved_keywords[] = $lbpool['name'];
        }
    }

    // Keywords not allowed in names
    $reserved_keywords = array("all", "pass", "block", "out", "queue", "max", "min", "pptp", "pppoe", "L2TP", "OpenVPN", "IPsec");
    $reserved_ifs = get_configured_interface_list(false, true);
    $reserved_keywords = array_merge($reserved_keywords, $reserved_ifs, $reserved_table_names);
    /* Check for reserved keyword names */
    foreach($reserved_keywords as $rk)
74 75
        if ($rk == $pconfig['name'])
            $input_errors[] = sprintf(gettext("Cannot use a reserved keyword as alias name %s"), $rk);
76 77 78

    /* check for name interface description conflicts */
    foreach($config['interfaces'] as $interface) {
79 80 81 82
        if($interface['descr'] == $pconfig['name']) {
            $input_errors[] = gettext("An interface description with this name already exists.");
            break;
        }
83 84 85 86 87
    }

    $imported_ips = array();
    $imported_descs = array();
    foreach (explode("\n", $pconfig['aliasimport']) as $impline) {
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
        $implinea = explode(" ",trim($impline),2);
        $impip = trim($implinea[0]);
        if (!empty($implinea[1])) {
            // trim and truncate description to max 200 characters
            $impdesc = substr(trim($implinea[1]),0, 200);
        } else {
            // no description given, use alias description
            $impdesc = trim(str_replace('|',' ' , $pconfig['descr']));
        }

        if (strpos($impip,'-') !== false) {
            // ip range provided
            $ipaddr1 = explode('-', $impip)[0];
            $ipaddr2 = explode('-', $impip)[1];
            if (!is_ipaddr($ipaddr1)) {
                $input_errors[] = sprintf(gettext("%s is not an IP address. Please correct the error to continue"), $ipaddr1);
            } elseif (!is_ipaddr($ipaddr2)) {
                $input_errors[] = sprintf(gettext("%s is not an IP address. Please correct the error to continue"), $ipaddr2);
            } else {
                foreach (ip_range_to_subnet_array($ipaddr1, $ipaddr2) as $network) {
                    $imported_ips[] = $network;
                    $imported_descs[] = $impdesc;
                }
            }
        } else {
            // single ip or network
            if (!is_ipaddr($impip) && !is_subnet($impip) && !is_hostname($impip) && !empty($impip)) {
                $input_errors[] = sprintf(gettext("%s is not an IP address. Please correct the error to continue"), $impip);
            } else {
                $imported_ips[] = $impip;
118
                $imported_descs[] = $impdesc;
119 120
            }
        }
121 122 123
    }

    if (count($input_errors) == 0) {
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
        // create output structure and serialize to config
        $alias = array();
        $alias['address'] = implode(" ", $imported_ips);
        $alias['detail'] = implode("||", $imported_descs);
        $alias['name'] = $pconfig['name'];
        $alias['type'] = "network";
        $alias['descr'] = $pconfig['descr'];
        $config['aliases']['alias'][] = $alias;

        // Sort list
        $config['aliases']['alias'] = msort($config['aliases']['alias'], "name");

        if (write_config()) {
            mark_subsystem_dirty('aliases');
        }
139
        header("Location: firewall_aliases.php");
140
        exit;
141
    }
Ad Schellevis's avatar
Ad Schellevis committed
142 143
}

144
legacy_html_escape_form_data($pconfig);
Ad Schellevis's avatar
Ad Schellevis committed
145

146
include("head.inc");
147

Ad Schellevis's avatar
Ad Schellevis committed
148
?>
Ad Schellevis's avatar
Ad Schellevis committed
149
<body>
Ad Schellevis's avatar
Ad Schellevis committed
150
<?php include("fbegin.inc"); ?>
151 152 153
  <section class="page-content-main">
    <div class="container-fluid">
      <div class="row">
154
<?php if (isset($input_errors) && count($input_errors) > 0) print_input_errors($input_errors); ?>
155 156 157 158 159 160 161 162 163 164 165
        <section class="col-xs-12">
          <div class="content-box">
            <header class="content-box-head container-fluid">
          <h3><?=gettext("Alias Import");?></h3>
            </header>
            <div class="content-box-main">
              <form action="firewall_aliases_import.php" method="post" name="iform">
                <table class="table table-striped">
                  <tr>
                    <td colspan="2" align="right">
                      <small><?=gettext("full help"); ?> </small>
Ad Schellevis's avatar
Ad Schellevis committed
166
                      <i class="fa fa-toggle-off text-danger"  style="cursor: pointer;" id="show_all_help_page" type="button"></i>
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
                    </td>
                  </tr>
                  <tr>
                    <td  width="22%"><a id="help_for_name" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Alias Name"); ?></td>
                    <td  width="78%">
                      <input name="name" type="text" class="form-control unknown" size="40" maxlength="31" value="<?=$pconfig['name'];?>" />
                      <div class="hidden" for="help_for_name">
                        <?=gettext("The name of the alias may only consist of the characters \"a-z, A-Z and 0-9\"."); ?>
                      </div>
                    </td>
                  </tr>
                  <tr>
                    <td><a id="help_for_description" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Description"); ?></td>
                    <td>
                      <input name="descr" type="text" value="<?=$pconfig['descr'];?>" />
                      <div class="hidden" for="help_for_description">
                        <?=gettext("You may enter a description here for your reference (not parsed)"); ?>.
                      </div>
                    </td>
                  </tr>
                  <tr>
                    <td><a id="help_for_alias" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Aliases to import"); ?></td>
                    <td>
                      <textarea name="aliasimport" rows="15" cols="40"><?=$pconfig['aliasimport'];?></textarea>
                      <div class="hidden" for="help_for_alias">
Fabian Franz's avatar
Fabian Franz committed
192
                        <?=gettext("Paste in the aliases to import separated by a carriage return. Common examples are lists of IPs, networks, blacklists, etc."); ?>
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 218 219 220 221
                        <br />
                        <?=gettext("The list may contain IP addresses, with or without CIDR prefix, IP ranges, blank lines (ignored) and an optional description after each IP. e.g.:"); ?>
                        <code>
                          <br/>172.16.1.2
                          <br/>172.16.0.0/24
                          <br/>10.11.12.100-10.11.12.200
                          <br/>192.168.1.254 Home router
                          <br/>10.20.0.0/16 Office network
                          <br/>10.40.1.10-10.40.1.19 Managed switches
                        </code>
                      </div>
                    </td>
                  </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td>
                      <input name="Submit" type="submit" class="btn btn-primary" value="<?=gettext("Save"); ?>" />
                      <input type="button" class="btn btn-default" value="<?=gettext("Cancel");?>"
                             onclick="window.location.href='<?=(isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/firewall_aliases.php');?>'" />
                    </td>
                  </tr>
                </table>
              </form>
            </div>
          </div>
        </section>
      </div>
    </div>
  </section>
222
<?php include("foot.inc"); ?>