vpn_ipsec_keys_edit.php 6.65 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
3
/*
4
	Copyright (C) 2014-2015 Deciso B.V.
Ad Schellevis's avatar
Ad Schellevis committed
5 6
	Copyright (C) 2003-2005 Manuel Kasper <mk@neon1.net>.
	All rights reserved.
7

Ad Schellevis's avatar
Ad Schellevis committed
8 9
	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions are met:
10

Ad Schellevis's avatar
Ad Schellevis committed
11 12
	1. Redistributions of source code must retain the above copyright notice,
	   this list of conditions and the following disclaimer.
13

Ad Schellevis's avatar
Ad Schellevis committed
14 15 16
	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.
17

Ad Schellevis's avatar
Ad Schellevis committed
18 19 20 21 22 23 24 25 26 27 28 29
	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.
*/

30
require_once("interfaces.inc");
31
require_once("guiconfig.inc");
Ad Schellevis's avatar
Ad Schellevis committed
32
require_once("vpn.inc");
33
require_once("services.inc");
Ad Schellevis's avatar
Ad Schellevis committed
34

35
if (!isset($config['ipsec']) || !is_array($config['ipsec'])) {
36 37
        $config['ipsec'] = array();
}
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $pconfig = array();
    if(isset($_GET['id']) && is_numericint($_GET['id']) && isset($config['ipsec']['mobilekey'][$_GET['id']])) {
        // 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
    if (isset($_POST['id']) && is_numericint($_POST['id']) && isset($config['ipsec']['mobilekey'][$_POST['id']]) ) {
        $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

Ad Schellevis's avatar
Ad Schellevis committed
119 120 121
$pgtitle = gettext("VPN: IPsec: Edit Pre-Shared Key");
$shortcut_section = "ipsec";

122 123
legacy_html_escape_form_data($pconfig);

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

?>

Ad Schellevis's avatar
Ad Schellevis committed
128
<body>
Ad Schellevis's avatar
Ad Schellevis committed
129
<?php include("fbegin.inc"); ?>
Ad Schellevis's avatar
Ad Schellevis committed
130 131 132

	<section class="page-content-main">
		<div class="container-fluid">
133
			<div class="row">
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
        <?php if (isset($input_errors) && count($input_errors) > 0) {
                print_input_errors($input_errors);
        }
        ?>
        <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>
					          <td>
                      <input name="ident" type="text" class="formfld unknown" id="ident" size="30" value="<?=$pconfig['ident'];?>" />
                      <div class="hidden" for="help_for_ident">
                        <?=gettext("This can be either an IP address, fully qualified domain name or an e-mail address"); ?>.
                      </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>
					        </tr>
					        <tr>
                    <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])) :
163
?>
164 165
					            <input name="id" type="hidden" value="<?=htmlspecialchars($id);?>" />
<?php
166
endif; ?>
167 168 169 170 171 172
					          </td>
					        </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td>
                      <span class="text-danger">
173 174 175
									<strong><?=gettext("Note"); ?>:<br /></strong>
								</span>
								<?=gettext("PSK for any user can be set by using an identifier of any/ANY");?>
176 177 178 179 180 181 182 183
                    </td>
                  </tr>
					      </table>
              </div>
            </form>
          </div>
        </section>
      </div>
Ad Schellevis's avatar
Ad Schellevis committed
184 185 186
		</div>
	</section>

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