Commit d415996b authored by Ad Schellevis's avatar Ad Schellevis Committed by Franco Fichtner

(legacy) spaces and curly braces in openvpn.auth-user.php

(cherry picked from commit f64321db)
parent eccfa60f
...@@ -2,30 +2,30 @@ ...@@ -2,30 +2,30 @@
<?php <?php
/* /*
Copyright (C) 2008 Shrew Soft Inc Copyright (C) 2008 Shrew Soft Inc
Copyright (C) 2010 Ermal Luçi Copyright (C) 2010 Ermal Luçi
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met: modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, 1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer. this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright 2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution. documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. POSSIBILITY OF SUCH DAMAGE.
*/ */
/* /*
...@@ -39,144 +39,157 @@ require_once("auth.inc"); ...@@ -39,144 +39,157 @@ require_once("auth.inc");
require_once("util.inc"); require_once("util.inc");
require_once("interfaces.inc"); require_once("interfaces.inc");
function cisco_to_cidr($addr) { function cisco_to_cidr($addr)
if (!is_ipaddr($addr)) {
return 0; if (!is_ipaddr($addr)) {
$mask = decbin(~ip2long($addr)); return 0;
$mask = substr($mask, -32); }
$k = 0; $mask = decbin(~ip2long($addr));
for ($i = 0; $i <= 32; $i++) { $mask = substr($mask, -32);
$k += intval($mask[$i]); $k = 0;
} for ($i = 0; $i <= 32; $i++) {
return $k; $k += intval($mask[$i]);
}
return $k;
} }
function cisco_extract_index($prule) { function cisco_extract_index($prule)
{
$index = explode("#", $prule); $index = explode("#", $prule);
if (is_numeric($index[1])) if (is_numeric($index[1])) {
return intval($index[1]); return intval($index[1]);
else } else {
syslog(LOG_WARNING, "Error parsing rule {$prule}: Could not extract index"); syslog(LOG_WARNING, "Error parsing rule {$prule}: Could not extract index");
return -1;; }
return -1;;
} }
function parse_cisco_acl($attribs) { function parse_cisco_acl($attribs) {
global $devname, $attributes; global $devname, $attributes;
if (!is_array($attribs)) if (!is_array($attribs)) {
return ""; return "";
$finalrules = ""; }
if (is_array($attribs['ciscoavpair'])) { $finalrules = "";
$inrules = array(); if (is_array($attribs['ciscoavpair'])) {
$outrules = array(); $inrules = array();
foreach ($attribs['ciscoavpair'] as $avrules) { $outrules = array();
$rule = explode("=", $avrules); foreach ($attribs['ciscoavpair'] as $avrules) {
$dir = ""; $rule = explode("=", $avrules);
if (strstr($rule[0], "inacl")) { $dir = "";
$dir = "in"; if (strstr($rule[0], "inacl")) {
} else if (strstr($rule[0], "outacl")) $dir = "in";
$dir = "out"; } elseif (strstr($rule[0], "outacl")) {
else if (strstr($rule[0], "dns-servers")) { $dir = "out";
$attributes['dns-servers'] = explode(" ", $rule[1]); } elseif (strstr($rule[0], "dns-servers")) {
continue; $attributes['dns-servers'] = explode(" ", $rule[1]);
} else if (strstr($rule[0], "route")) { continue;
if (!is_array($attributes['routes'])) } elseif (strstr($rule[0], "route")) {
$attributes['routes'] = array(); if (!is_array($attributes['routes'])) {
$attributes['routes'][] = $rule[1]; $attributes['routes'] = array();
continue; }
} $attributes['routes'][] = $rule[1];
$rindex = cisco_extract_index($rule[0]); continue;
if ($rindex < 0) }
continue; $rindex = cisco_extract_index($rule[0]);
if ($rindex < 0) {
$rule = $rule[1]; continue;
$rule = explode(" ", $rule); }
$tmprule = "";
$index = 0; $rule = $rule[1];
$isblock = false; $rule = explode(" ", $rule);
if ($rule[$index] == "permit") $tmprule = "";
$tmprule = "pass {$dir} quick on {$devname} "; $index = 0;
else if ($rule[$index] == "deny") { $isblock = false;
//continue; if ($rule[$index] == "permit") {
$isblock = true; $tmprule = "pass {$dir} quick on {$devname} ";
$tmprule = "block {$dir} quick on {$devname} "; } elseif ($rule[$index] == "deny") {
} else { //continue;
continue; $isblock = true;
} $tmprule = "block {$dir} quick on {$devname} ";
} else {
$index++; continue;
}
switch ($rule[$index]) {
case "tcp": $index++;
case "udp":
$tmprule .= "proto {$rule[$index]} "; switch ($rule[$index]) {
break; case "tcp":
case "udp":
} $tmprule .= "proto {$rule[$index]} ";
break;
$index++; }
/* Source */
if (trim($rule[$index]) == "host") { $index++;
$index++; /* Source */
$tmprule .= "from {$rule[$index]} "; if (trim($rule[$index]) == "host") {
$index++; $index++;
if ($isblock == true) $tmprule .= "from {$rule[$index]} ";
$isblock = false; $index++;
} else if (trim($rule[$index]) == "any") { if ($isblock == true) {
$tmprule .= "from any"; $isblock = false;
$index++; }
} else { } elseif (trim($rule[$index]) == "any") {
$tmprule .= "from {$rule[$index]}"; $tmprule .= "from any";
$index++; $index++;
$netmask = cisco_to_cidr($rule[$index]); } else {
$tmprule .= "/{$netmask} "; $tmprule .= "from {$rule[$index]}";
$index++; $index++;
if ($isblock == true) $netmask = cisco_to_cidr($rule[$index]);
$isblock = false; $tmprule .= "/{$netmask} ";
} $index++;
/* Destination */ if ($isblock == true) {
if (trim($rule[$index]) == "host") { $isblock = false;
$index++; }
$tmprule .= "to {$rule[$index]} "; }
$index++; /* Destination */
if ($isblock == true) if (trim($rule[$index]) == "host") {
$isblock = false; $index++;
} else if (trim($rule[$index]) == "any") { $tmprule .= "to {$rule[$index]} ";
$index++; $index++;
$tmprule .= "to any"; if ($isblock == true) {
} else { $isblock = false;
$tmprule .= "to {$rule[$index]}"; }
$index++; } elseif (trim($rule[$index]) == "any") {
$netmask = cisco_to_cidr($rule[$index]); $index++;
$tmprule .= "/{$netmask} "; $tmprule .= "to any";
$index++; } else {
if ($isblock == true) $tmprule .= "to {$rule[$index]}";
$isblock = false; $index++;
} $netmask = cisco_to_cidr($rule[$index]);
$tmprule .= "/{$netmask} ";
if ($isblock == true) $index++;
continue; if ($isblock == true) {
$isblock = false;
if ($dir == "in") }
$inrules[$rindex] = $tmprule; }
else if ($dir == "out")
$outrules[$rindex] = $tmprule; if ($isblock == true) {
} continue;
}
$state = ""; if ($dir == "in") {
if (!empty($outrules)) $inrules[$rindex] = $tmprule;
$state = "no state"; } elseif ($dir == "out") {
ksort($inrules, SORT_NUMERIC); $outrules[$rindex] = $tmprule;
foreach ($inrules as $inrule) }
$finalrules .= "{$inrule} {$state}\n"; }
if (!empty($outrules)) {
ksort($outrules, SORT_NUMERIC); $state = "";
foreach ($outrules as $outrule) if (!empty($outrules)) {
$finalrules .= "{$outrule} {$state}\n"; $state = "no state";
} }
} ksort($inrules, SORT_NUMERIC);
return $finalrules; foreach ($inrules as $inrule) {
$finalrules .= "{$inrule} {$state}\n";
}
if (!empty($outrules)) {
ksort($outrules, SORT_NUMERIC);
foreach ($outrules as $outrule) {
$finalrules .= "{$outrule} {$state}\n";
}
}
}
return $finalrules;
} }
...@@ -186,108 +199,114 @@ function parse_cisco_acl($attribs) { ...@@ -186,108 +199,114 @@ function parse_cisco_acl($attribs) {
* We will use our local hostname to make up the nas_id * We will use our local hostname to make up the nas_id
*/ */
if (!function_exists("getNasID")) { if (!function_exists("getNasID")) {
function getNasID() function getNasID()
{ {
global $g; global $g;
$nasId = gethostname(); $nasId = gethostname();
if(empty($nasId)) if (empty($nasId)) {
$nasId = $g['product_name']; $nasId = $g['product_name'];
return $nasId; }
} return $nasId;
}
} }
/* setup syslog logging */ /* setup syslog logging */
openlog("openvpn", LOG_ODELAY, LOG_AUTH); openlog("openvpn", LOG_ODELAY, LOG_AUTH);
if (count($argv) > 6) { if (count($argv) > 6) {
$authmodes = explode(',', $argv[5]); $authmodes = explode(',', $argv[5]);
$username = base64_decode(str_replace('%3D', '=', $argv[1])); $username = base64_decode(str_replace('%3D', '=', $argv[1]));
$password = base64_decode(str_replace('%3D', '=', $argv[2])); $password = base64_decode(str_replace('%3D', '=', $argv[2]));
$common_name = $argv[3]; $common_name = $argv[3];
$modeid = $argv[6]; $modeid = $argv[6];
$strictusercn = $argv[4] == 'false' ? false : true; $strictusercn = $argv[4] == 'false' ? false : true;
} else { } else {
/* read data from environment */ /* read data from environment */
$username = getenv("username"); $username = getenv("username");
$password = getenv("password"); $password = getenv("password");
$common_name = getenv("common_name"); $common_name = getenv("common_name");
} }
if (!$username || !$password) { if (!$username || !$password) {
syslog(LOG_ERR, "invalid user authentication environment"); syslog(LOG_ERR, "invalid user authentication environment");
closelog(); closelog();
exit(-1); exit(-1);
} }
/* Replaced by a sed with propper variables used below(ldap parameters). */ /* Replaced by a sed with propper variables used below(ldap parameters). */
//<template> //<template>
if (file_exists("/var/etc/openvpn/{$modeid}.ca")) { if (file_exists("/var/etc/openvpn/{$modeid}.ca")) {
putenv("LDAPTLS_CACERT=/var/etc/openvpn/{$modeid}.ca"); putenv("LDAPTLS_CACERT=/var/etc/openvpn/{$modeid}.ca");
putenv("LDAPTLS_REQCERT=never"); putenv("LDAPTLS_REQCERT=never");
} }
$authenticated = false; $authenticated = false;
if (($strictusercn === true) && ($common_name != $username)) { if (($strictusercn === true) && ($common_name != $username)) {
syslog(LOG_WARNING, "Username does not match certificate common name ({$username} != {$common_name}), access denied.\n"); syslog(LOG_WARNING, "Username does not match certificate common name ({$username} != {$common_name}), access denied.\n");
closelog(); closelog();
exit(1); exit(1);
} }
if (!is_array($authmodes)) { if (!is_array($authmodes)) {
syslog(LOG_WARNING, "No authentication server has been selected to authenticate against. Denying authentication for user {$username}"); syslog(LOG_WARNING, "No authentication server has been selected to authenticate against. Denying authentication for user {$username}");
closelog(); closelog();
exit(1); exit(1);
} }
$attributes = array(); $attributes = array();
foreach ($authmodes as $authmode) { foreach ($authmodes as $authmode) {
$authcfg = auth_get_authserver($authmode); $authcfg = auth_get_authserver($authmode);
if (!$authcfg && $authmode != "local") if (!$authcfg && $authmode != "local") {
continue; continue;
}
$authenticated = authenticate_user($username, $password, $authcfg);
if ($authenticated == true) $authenticated = authenticate_user($username, $password, $authcfg);
break; if ($authenticated == true) {
break;
}
} }
if ($authenticated == false) { if ($authenticated == false) {
syslog(LOG_WARNING, "user '{$username}' could not authenticate.\n"); syslog(LOG_WARNING, "user '{$username}' could not authenticate.\n");
closelog(); closelog();
exit(-1); exit(-1);
} }
if (empty($common_name)) { if (empty($common_name)) {
$common_name = getenv("common_name"); $common_name = getenv("common_name");
if (empty($common_name)) if (empty($common_name)) {
$common_name = getenv("username"); $common_name = getenv("username");
}
} }
$devname = getenv("dev"); $devname = getenv("dev");
if (empty($devname)) if (empty($devname)) {
$devname = "openvpn"; $devname = "openvpn";
}
$rules = parse_cisco_acl($attributes); $rules = parse_cisco_acl($attributes);
if (!empty($rules)) { if (!empty($rules)) {
$pid = getmypid(); $pid = getmypid();
@file_put_contents("/tmp/ovpn_{$pid}{$common_name}.rules", $rules); @file_put_contents("/tmp/ovpn_{$pid}{$common_name}.rules", $rules);
mwexec("/sbin/pfctl -a " . escapeshellarg("openvpn/{$common_name}") . " -f /tmp/ovpn_{$pid}" . escapeshellarg($common_name) . ".rules"); mwexec("/sbin/pfctl -a " . escapeshellarg("openvpn/{$common_name}") . " -f /tmp/ovpn_{$pid}" . escapeshellarg($common_name) . ".rules");
@unlink("/tmp/ovpn_{$pid}{$common_name}.rules"); @unlink("/tmp/ovpn_{$pid}{$common_name}.rules");
} }
$content = ""; $content = "";
if (is_array($attributes['dns-servers'])) { if (is_array($attributes['dns-servers'])) {
foreach ($attributes['dns-servers'] as $dnssrv) { foreach ($attributes['dns-servers'] as $dnssrv) {
if (is_ipaddr($dnssrv)) if (is_ipaddr($dnssrv)) {
$content .= "push \"dhcp-option DNS {$dnssrv}\"\n"; $content .= "push \"dhcp-option DNS {$dnssrv}\"\n";
} }
}
} }
if (is_array($attributes['routes'])) { if (is_array($attributes['routes'])) {
foreach ($attributes['routes'] as $route) foreach ($attributes['routes'] as $route) {
$content .= "push \"route {$route} vpn_gateway\"\n"; $content .= "push \"route {$route} vpn_gateway\"\n";
}
} }
if (isset($attributes['framed_ip'])) { if (isset($attributes['framed_ip'])) {
...@@ -297,13 +316,13 @@ if (isset($attributes['framed_ip'])) { ...@@ -297,13 +316,13 @@ if (isset($attributes['framed_ip'])) {
$content .= "ifconfig-push {$attributes['framed_ip']} {$attributes['framed_mask']}"; $content .= "ifconfig-push {$attributes['framed_ip']} {$attributes['framed_mask']}";
} else { } else {
*/ */
$content .= "topology net30\n"; $content .= "topology net30\n";
$content .= "ifconfig-push {$attributes['framed_ip']} ". long2ip((ip2long($attributes['framed_ip']) + 1)); $content .= "ifconfig-push {$attributes['framed_ip']} ". long2ip((ip2long($attributes['framed_ip']) + 1));
// } // }
} }
if (!empty($content)) { if (!empty($content)) {
@file_put_contents("/tmp/{$username}", $content); @file_put_contents("/tmp/{$username}", $content);
} }
syslog(LOG_NOTICE, "user '{$username}' authenticated\n"); syslog(LOG_NOTICE, "user '{$username}' authenticated\n");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment