vpn_openvpn_export_shared.php 15.3 KB
Newer Older
1
<?php
2

3
/*
4 5
	Copyright (C) 2008 Shrew Soft Inc.
	Copyright (C) 2010 Ermal Luçi
6
	All rights reserved.
7 8 9

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

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

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

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("guiconfig.inc");
31
require_once("openvpn.inc");
32
require_once("services.inc");
33 34
require_once("filter.inc");
require_once("pfsense-utils.inc");
35
require_once("interfaces.inc");
36
require_once("openvpn-client-export.inc");
37

38
$service_hook = 'openvpn';
39 40

$ras_server = array();
41
if (isset($config['openvpn']['openvpn-server'])) {
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
    foreach ($config['openvpn']['openvpn-server'] as $sindex => $server) {
        if (isset($server['disable'])) {
            continue;
        }
        $ras_user = array();
        if ($server['mode'] != "p2p_shared_key") {
            continue;
        }

        $ras_serverent = array();
        $prot = $server['protocol'];
        $port = $server['local_port'];
        if ($server['description']) {
            $name = "{$server['description']} {$prot}:{$port}";
        } else {
            $name = "Shared Key Server {$prot}:{$port}";
        }
        $ras_serverent['index'] = $sindex;
        $ras_serverent['name'] = $name;
        $ras_serverent['mode'] = $server['mode'];
        $ras_server[] = $ras_serverent;
    }
64
}
65

66 67 68 69 70
if (isset($_GET['act'])) {
    $input_errors = array();
    $act = $_GET['act'];
    if (($act == "skconf") || ($act == "skzipconf")) {
        $srvid = isset($_GET['srvid']) ? $_GET['srvid'] : false;
71
        if ($srvid === false || !isset($config['openvpn']['openvpn-server'][$srvid]['mode']) ||
72
              $config['openvpn']['openvpn-server'][$srvid]['mode'] != "p2p_shared_key") {
73
                header("Location: vpn_openvpn_export.php");
74
                exit;
75
        }
76 77

        if (empty($_GET['useaddr'])) {
78
            $input_errors[] = gettext("You need to specify an IP or hostname.");
79
        } else {
80
            $useaddr = $_GET['useaddr'];
81
        }
82 83 84 85 86

        $proxy = "";
        if (!empty($_GET['proxy_addr']) || !empty($_GET['proxy_port'])) {
            $proxy = array();
            if (empty($_GET['proxy_addr'])) {
87
                $input_errors[] = gettext("You need to specify an address for the proxy port.");
88
            } else {
89
                $proxy['ip'] = $_GET['proxy_addr'];
90
            }
91
            if (empty($_GET['proxy_port'])) {
92
                $input_errors[] = gettext("You need to specify a port for the proxy ip.");
93
            } else {
94 95 96 97 98 99
                $proxy['port'] = $_GET['proxy_port'];
            }
            $proxy['proxy_type'] = $_GET['proxy_type'];
            $proxy['proxy_authtype'] = $_GET['proxy_authtype'];
            if ($_GET['proxy_authtype'] != "none") {
                if (empty($_GET['proxy_user'])) {
100
                    $input_errors[] = gettext("You need to specify a username with the proxy config.");
101 102 103 104
                } else {
                    $proxy['user'] = $_GET['proxy_user'];
                }
                if (!empty($_GET['proxy_user']) && empty($_GET['proxy_password'])) {
105
                    $input_errors[] = gettext("You need to specify a password with the proxy user.");
106 107 108
                } else {
                    $proxy['password'] = $_GET['proxy_password'];
                }
109 110 111
            }
        }

112 113 114
        $exp_name = openvpn_client_export_prefix($srvid);
        if ($act == "skzipconf") {
            $zipconf = true;
115
        }
116 117
        $exp_data = openvpn_client_export_sharedkey_config($srvid, $useaddr, $proxy, $zipconf);
        if (!$exp_data) {
118
            $input_errors[] = gettext("Failed to export config files!");
119
        }
120 121 122 123 124 125 126 127
        if (count($input_errors) == 0) {
            if ($zipconf) {
                $exp_name = urlencode($exp_data);
                $exp_size = filesize("/tmp/{$exp_data}");
            } else {
                $exp_name = urlencode($exp_name."-config.ovpn");
                $exp_size = strlen($exp_data);
            }
128

129 130 131 132 133 134 135 136 137 138 139 140 141 142
            header('Pragma: ');
            header('Cache-Control: ');
            header("Content-Type: application/octet-stream");
            header("Content-Disposition: attachment; filename={$exp_name}");
            header("Content-Length: $exp_size");
            if ($zipconf) {
                readfile("/tmp/{$exp_data}");
            } else {
                echo $exp_data;
            }

            @unlink("/tmp/{$exp_data}");
            exit;
        }
143
    }
144 145 146 147 148
}

include("head.inc");
?>

149
<body>
150 151 152
<?php include("fbegin.inc"); ?>
<script type="text/javascript">
//<![CDATA[
153 154 155
$( document ).ready(function() {
  server_changed();
});
156 157

var servers = new Array();
158 159
<?php	foreach ($ras_server as $sindex => $server) :
?>
160
servers[<?=$sindex;?>] = new Array();
161 162
servers[<?=$sindex;
?>][0] = '<?=$server['index'];?>';
163
servers[<?=$sindex;?>][1] = new Array();
164 165 166 167
servers[<?=$sindex;
?>][2] = '<?=$server['mode'];?>';
<?
endforeach; ?>
168 169 170 171 172 173 174 175

function download_begin(act) {

	var index = document.getElementById("server").selectedIndex;
	var useaddr;

	if (document.getElementById("useaddr").value == "other") {
		if (document.getElementById("useaddr_hostname").value == "") {
176
			alert("<?=gettext('Please specify an IP address or hostname.') ?>");
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
			return;
		}
		useaddr = document.getElementById("useaddr_hostname").value;
	} else
		useaddr = document.getElementById("useaddr").value;

	var useproxy = 0;
	var useproxypass = 0;
	if (document.getElementById("useproxy").checked)
		useproxy = 1;

	var proxyaddr = document.getElementById("proxyaddr").value;
	var proxyport = document.getElementById("proxyport").value;
	if (useproxy) {
		if (!proxyaddr || !proxyport) {
192
			alert("<?=gettext('The proxy ip and port cannot be empty') ?>");
193 194 195 196 197 198 199 200 201 202 203 204 205 206
			return;
		}

		if (document.getElementById("useproxypass").value != 'none')
			useproxypass = 1;

		var proxytype = document.getElementById("useproxytype").value;

		var proxyauth = document.getElementById("useproxypass").value;
		var proxyuser = document.getElementById("proxyuser").value;
		var proxypass = document.getElementById("proxypass").value;
		var proxyconf = document.getElementById("proxyconf").value;
		if (useproxypass) {
			if (!proxyuser) {
207
				alert("<?=gettext('Please fill the proxy username and password.') ?>");
208 209 210
				return;
			}
			if (!proxypass || !proxyconf) {
211
				alert("<?=gettext('The proxy password or confirm field is empty') ?>");
212 213 214
				return;
			}
			if (proxypass != proxyconf) {
215
				alert("<?=gettext('The proxy password and confirm fields must match') ?>");
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
				return;
			}
		}
	}

	var dlurl;
	dlurl  = "/vpn_openvpn_export_shared.php?act=" + act;
	dlurl += "&srvid=" + servers[index][0];
	dlurl += "&useaddr=" + useaddr;
	if (useproxy) {
		dlurl += "&proxy_type=" + escape(proxytype);
		dlurl += "&proxy_addr=" + proxyaddr;
		dlurl += "&proxy_port=" + proxyport;
		dlurl += "&proxy_authtype=" + proxyauth;
		if (useproxypass) {
			dlurl += "&proxy_user=" + proxyuser;
			dlurl += "&proxy_password=" + proxypass;
		}
	}

	window.open(dlurl,"_self");
}

function server_changed() {
	var table = document.getElementById("clients");
241 242 243
	while (table.rows.length > 1 ) {
    table.deleteRow(1);
  }
244 245 246 247 248 249 250 251

	var index = document.getElementById("server").selectedIndex;

	if (servers[index][2] == 'p2p_shared_key') {
		var row = table.insertRow(table.rows.length);
		var cell0 = row.insertCell(0);
		var cell1 = row.insertCell(1);
		cell0.innerHTML = "Other Shared Key OS Client";
252 253 254 255 256 257
    cell1.innerHTML += "<div>";
    cell1.innerHTML += "<button type='button' class='btn btn-primary btn-xs' onclick='download_begin(\"skconf\")'>Configuration</button>";
    cell1.innerHTML += "&nbsp;";
    cell1.innerHTML += "<button type='button' class='btn btn-primary btn-xs' onclick='download_begin(\"skzipconf\")'>Configuration archive</button>";
    cell1.innerHTML += "</div>";

258 259 260 261 262 263
	}
}

function useaddr_changed(obj) {

	if (obj.value == "other")
264
		$('#HostName').show();
265
	else
266
		$('#HostName').hide();
267

268 269 270 271
}

function useproxy_changed(obj) {

272 273 274 275 276 277 278 279 280 281 282
  if ($('#useproxy').prop( "checked" ) ){
      $('#useproxy_opts').show();
  } else {
      $('#useproxy_opts').hide();
  }

  if ($( "#useproxypass option:selected" ).text() != 'none') {
      $('#useproxypass_opts').show();
  } else {
      $('#useproxypass_opts').hide();
  }
283 284 285 286
}
//]]>
</script>
<?php
287
if (isset($input_errors) && count($input_errors) > 0) {
288 289
    print_input_errors($input_errors);
}
290
if (isset($savemsg)) {
291 292
    print_info_box($savemsg);
}
293
?>
294 295 296 297 298 299 300 301 302 303 304
<section class="page-content-main">
  <div class="container-fluid">
    <div class="row">
      <section class="col-xs-12">
        <div class="tab-content content-box col-xs-12">
          <div class="table-responsive">
            <table width="100%" border="0" class="table table-striped" cellpadding="0" cellspacing="0">
              <tr>
                <td width="22%"></td>
                <td width="78%" align="right">
                  <small><?=gettext("full help"); ?> </small>
305
                  <i class="fa fa-toggle-off text-danger" style="cursor: pointer;" id="show_all_help_page" type="button"></i></a>
306 307 308 309 310 311 312 313
                </td>
              </tr>
              <tr>
                <td width="22%" valign="top" class="vncellreq"><i class="fa fa-info-circle text-muted"></i> <?=gettext("Shared Key Server");?></td>
                <td width="78%" class="vtable">
                  <select name="server" id="server" class="formselect" onchange="server_changed()">
                    <?php foreach ($ras_server as & $server) :
    ?>
314 315
                    <option value="<?=htmlspecialchars($server['sindex']);
?>"><?=htmlspecialchars($server['name']);?></option>
316
                    <?php
317
endforeach; ?>
318 319 320 321 322 323 324 325 326 327
                  </select>
                </td>
              </tr>
              <tr>
                <td width="22%" valign="top" class="vncell"><i class="fa fa-info-circle text-muted"></i> <?=gettext("Host Name Resolution");?></td>
                <td width="78%" class="vtable">
                  <select name="useaddr" id="useaddr" class="formselect" onchange="useaddr_changed(this)">
                    <option value="serveraddr" ><?=gettext("Interface IP Address");?></option>
                    <option value="serverhostname" ><?=gettext("Installation hostname");?></option>
                    <?php if (isset($config['dyndnses']['dyndns'])) :
328
?>
329
                        <?php foreach ($config['dyndnses']['dyndns'] as $ddns) :
330
?>
331
                        <option value="<?= htmlspecialchars($ddns["host"]);
332
?>"><?=gettext("Dynamic DNS:");
333 334
?> <?= htmlspecialchars($ddns["host"]);?></option>
                        <?php
335
endforeach; ?>
336
                    <?php
337
endif; ?>
338 339 340 341 342 343 344 345 346 347 348 349 350 351
                    <option value="other"><?=gettext("Other");?></option>
                  </select>
                  <div style="display:none;" id="HostName">
                    <?=gettext("Enter the hostname or IP address the client will use to connect to this server.");?>
                    <input name="useaddr_hostname" type="text" id="useaddr_hostname" size="40" />
                  </div>
                </td>
              </tr>
              <tr>
                <td width="22%" valign="top" class="vncell"><a id="help_for_use_proxy" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Use Proxy");?></td>
                <td width="78%" class="vtable">
                  <input name="useproxy" id="useproxy" type="checkbox" value="yes" onclick="useproxy_changed(this)" />

                  <div id="useproxy_opts" style="display:none">
352
                        <?=gettext("Type");?> :
353 354
                      <select name="useproxytype" id="useproxytype" class="formselect">
                        <option value="http"><?=gettext("HTTP");?></option>
355
                        <option value="socks"><?=gettext("SOCKS");?></option>
356 357
                      </select>

358
                        <?=gettext("IP Address")?> :
359
                      <input name="proxyaddr" id="proxyaddr" type="text" class="formfld unknown" size="30" value="" />
360
                        <?=gettext("Port");?> :
361
                      <input name="proxyport" id="proxyport" type="text" class="formfld unknown" size="5" value="" />
362
                        <?=gettext("Choose proxy authentication if any.");?>
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
                      <select name="useproxypass" id="useproxypass" class="formselect" onchange="useproxy_changed(this)">
                        <option value="none"><?=gettext("none");?></option>
                        <option value="basic"><?=gettext("basic");?></option>
                        <option value="ntlm"><?=gettext("ntlm");?></option>
                      </select>
                      <div id="useproxypass_opts">
                        <?=gettext("Username")?> :
                        <input name="proxyuser" id="proxyuser" type="text" class="formfld unknown" size="20" value="" />
                        <?=gettext("Password");?> :
                        <input name="proxypass" id="proxypass" type="password" class="formfld pwd" size="20" value="" />
                        <?=gettext("Confirm");?> :
                        <input name="proxyconf" id="proxyconf" type="password" class="formfld pwd" size="20" value="" />
                      </div>
                  </div>
                  <div class="hidden" for="help_for_use_proxy">
378
                        <?= gettext("Use proxy to communicate with the server.");?>
379 380 381 382 383 384 385 386 387 388 389 390 391
                  </div>
                </td>
              </tr>
              <tr>
                <td><a id="help_for_client_conf_pkg" href="#" class="showhelp"><i class="fa fa-info-circle"></i></a> <?=gettext("Client Configuration Packages");?></td>
                <td>
                  <table width="100%" id="clients" border="0" cellpadding="0" cellspacing="0" class="table table-striped table-bordered ">
                    <tr>
                      <td width="25%" class="listhdrr"><b><?=gettext("Client Type");?></b></td>
                      <td width="50%" class="listhdrr"><b><?=gettext("Export");?></b></td>
                    </tr>
                  </table>
                  <div class="hidden" for="help_for_client_conf_pkg">
392 393
                        <?= gettext("NOTE:") ?> <br/>
                        <?= gettext("These are shared key configurations for use in site-to-site tunnels with other routers. Shared key tunnels are not normally used for remote access connections to end users.") ?>
394 395 396 397 398 399 400 401 402 403 404
                  </div>
                </td>
              </tr>
            </table>
          </div>
        </div>
      </section>
    </div>
  </div>
</section>

Ad Schellevis's avatar
Ad Schellevis committed
405
<?php include("foot.inc"); ?>