vpn_ipsec_keys_edit.php 6.71 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-2005 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("interfaces.inc");
31
require_once("guiconfig.inc");
32
require_once("ipsec.inc");
33
require_once("services.inc");
Ad Schellevis's avatar
Ad Schellevis committed
34

35
if (!isset($config['ipsec']) || !is_array($config['ipsec'])) {
36
    $config['ipsec'] = array();
37
}
38

39
if (!isset($config['ipsec']['mobilekey'])) {
40
    $config['ipsec']['mobilekey'] = array();
41
} else {
42
    ipsec_mobilekey_sort();
Ad Schellevis's avatar
Ad Schellevis committed
43 44
}

45 46
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $pconfig = array();
47
    if (isset($_GET['id']) && is_numericint($_GET['id']) && isset($config['ipsec']['mobilekey'][$_GET['id']])) {
48 49 50 51 52 53 54 55 56 57 58 59 60
        // fetch record
        $id = $_GET['id'];
        $pconfig['ident'] = $config['ipsec']['mobilekey'][$id]['ident'];
        $pconfig['psk'] = $config['ipsec']['mobilekey'][$id]['pre-shared-key'];
    } else {
        // init new
        $pconfig['ident'] = '';
        $pconfig['psk'] = '';
    }
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $input_errors = array();
    $pconfig = $_POST;
    // fetch record number if valid
61
    if (isset($_POST['id']) && is_numericint($_POST['id']) && isset($config['ipsec']['mobilekey'][$_POST['id']])) {
62 63 64 65
        $id = $_POST['id'];
    } else {
        $id = null;
    }
Ad Schellevis's avatar
Ad Schellevis committed
66

67
    /* input validation */
68 69 70 71
    $userids = array();
    foreach ($config['system']['user'] as $uid => $user) {
        $userids[$user['name']] = $uid;
    }
72 73 74 75
    if (isset($pconfig['ident']) && array_key_exists($pconfig['ident'], $userids)) {
        $input_errors[] = gettext("A user with this name already exists. Add the key to the user instead.");
    }
    unset($userids);
76 77 78 79 80 81

    $reqdfields = explode(" ", "ident psk");
    $reqdfieldsn = array(gettext("Identifier"),gettext("Pre-Shared Key"));

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

82
    if (empty($pconfig['ident']) || preg_match("/[^a-zA-Z0-9@\.\-]/", $pconfig['ident'])) {
83 84 85
        $input_errors[] = gettext("The identifier contains invalid characters.");
    }

86 87 88 89 90 91
    /* make sure there are no dupes on new entries */
    $recidx = 0 ;
    foreach ($config['ipsec']['mobilekey'] as $secretent) {
        if ($secretent['ident'] == $pconfig['ident'] && ($recidx != $id || $id === null)) {
            $input_errors[] = gettext("Another entry with the same identifier already exists.");
            break;
92
        }
93
        $recidx++;
94 95
    }

96 97 98 99
    if (count($input_errors) == 0) {
        $secretent = array();
        $secretent['ident'] = $pconfig['ident'];
        $secretent['pre-shared-key'] = $pconfig['psk'];
100

101 102 103 104
        if ($id !== null) {
            // edit existing key
            $config['ipsec']['mobilekey'][$id] = $secretent;
            $config_write_text = gettext("Edited");
105
        } else {
106 107
            $config_write_text = gettext("Added");
            $config['ipsec']['mobilekey'][] = $secretent;
108 109
        }

110
        write_config("{$config_write_text} IPsec Pre-Shared Keys");
111 112 113 114 115
        mark_subsystem_dirty('ipsec');

        header("Location: vpn_ipsec_keys.php");
        exit;
    }
Ad Schellevis's avatar
Ad Schellevis committed
116 117
}

118
$service_hook = 'ipsec';
Ad Schellevis's avatar
Ad Schellevis committed
119

120 121
legacy_html_escape_form_data($pconfig);

Ad Schellevis's avatar
Ad Schellevis committed
122 123 124 125
include("head.inc");

?>

Ad Schellevis's avatar
Ad Schellevis committed
126
<body>
Ad Schellevis's avatar
Ad Schellevis committed
127
<?php include("fbegin.inc"); ?>
Ad Schellevis's avatar
Ad Schellevis committed
128

129 130 131
  <section class="page-content-main">
    <div class="container-fluid">
      <div class="row">
132 133
        <?php if (isset($input_errors) && count($input_errors) > 0) {
                print_input_errors($input_errors);
134
}
135 136 137 138 139 140 141 142
        ?>
        <section class="col-xs-12">
          <div class="content-box">
            <form action="vpn_ipsec_keys_edit.php" method="post" name="iform" id="iform">
              <div class="table-responsive">
                <table class="table table-striped">
                  <tr>
                    <td><a id="help_for_ident" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Identifier"); ?></td>
143
                    <td>
144 145
                      <input name="ident" type="text" class="formfld unknown" id="ident" size="30" value="<?=$pconfig['ident'];?>" />
                      <div class="hidden" for="help_for_ident">
146
                        <?=gettext("This can be either an IP address, fully qualified domain name or an e-mail address."); ?>
147 148 149 150 151 152 153 154
                      </div>
                    </td>
                  </tr>
                  <tr>
                    <td><i class="fa fa-info-circle text-muted"></i> <?=gettext("Pre-Shared Key"); ?></td>
                    <td>
                      <input name="psk" type="text" class="formfld unknown" id="psk" size="40" value="<?=$pconfig['psk'];?>" />
                    </td>
155 156
                  </tr>
                  <tr>
157 158 159 160
                    <td>&nbsp;</td>
                    <td>
                      <input name="Submit" type="submit" class="btn btn-primary" value="<?=gettext("Save"); ?>" />
<?php                 if (isset($id) && isset($config['ipsec']['mobilekey'][$id])) :
161
?>
162
                      <input name="id" type="hidden" value="<?=htmlspecialchars($id);?>" />
163
<?php
164
endif; ?>
165 166
                    </td>
                  </tr>
167 168 169 170
                  <tr>
                    <td>&nbsp;</td>
                    <td>
                      <span class="text-danger">
171 172 173
                  <strong><?=gettext("Note"); ?>:<br /></strong>
                </span>
                <?=gettext("PSK for any user can be set by using an identifier of any/ANY");?>
174 175
                    </td>
                  </tr>
176
                </table>
177 178 179 180 181
              </div>
            </form>
          </div>
        </section>
      </div>
182 183
    </div>
  </section>
Ad Schellevis's avatar
Ad Schellevis committed
184

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