| 1 |
<?php # -*- mode: php -*- |
| 2 |
|
| 3 |
// gsasl-saml20-sp.php --- SAML SP for smtp-server-saml20.c. |
| 4 |
// Copyright (C) 2012-2026 Simon Josefsson |
| 5 |
// |
| 6 |
// This file is part of GNU SASL. |
| 7 |
// |
| 8 |
// This program is free software: you can redistribute it and/or modify |
| 9 |
// it under the terms of the GNU General Public License as published by |
| 10 |
// the Free Software Foundation, either version 3 of the License, or |
| 11 |
// (at your option) any later version. |
| 12 |
// |
| 13 |
// This program is distributed in the hope that it will be useful, |
| 14 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
// GNU General Public License for more details. |
| 17 |
// |
| 18 |
// You should have received a copy of the GNU General Public License |
| 19 |
// along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
|
| 21 |
require "lasso.php"; |
| 22 |
|
| 23 |
include "gsasl-saml20-config.php"; |
| 24 |
|
| 25 |
if (!isset($state_path) || !isset($cfg_path) || !is_writable($state_path)) { |
| 26 |
print "Error: Configuration error"; |
| 27 |
exit (0); |
| 28 |
} |
| 29 |
|
| 30 |
if (!isset($_POST["SAMLResponse"])) { |
| 31 |
print "Error: Expected SAMLResponse POST data"; |
| 32 |
exit (0); |
| 33 |
} |
| 34 |
|
| 35 |
$xml = base64_decode($_POST["SAMLResponse"]); |
| 36 |
if (preg_match ("/<[^> ]+:Response[^>]+InResponseTo=\"([A-Za-z0-9_]+)\"/", |
| 37 |
$xml, $matches) != 1) { |
| 38 |
print "Error: Could not parse XML"; |
| 39 |
error_log ("parsing XML failed: $xml"); |
| 40 |
exit (0); |
| 41 |
} |
| 42 |
$id = $matches[1]; |
| 43 |
if (!file_exists ("$state_path/$id") && !mkdir ("$state_path/$id", 0770)) { |
| 44 |
error_log ("mkdir: $state_path/$id"); |
| 45 |
exit (0); |
| 46 |
} |
| 47 |
file_put_contents ("$state_path/$id/post", $xml); |
| 48 |
error_log ("saved SAMLResponse into $state_path/$id/post"); |
| 49 |
|
| 50 |
function error($msg) { |
| 51 |
global $state_path, $id; |
| 52 |
file_put_contents ("$state_path/$id/fail", ""); |
| 53 |
print "Error: " . htmlentities($msg); |
| 54 |
exit(0); |
| 55 |
} |
| 56 |
|
| 57 |
try { |
| 58 |
$server = new LassoServer("$cfg_path/sp-metadata.xml", |
| 59 |
"$cfg_path/sp-key.pem", "", |
| 60 |
"$cfg_path/sp-crt.pem"); |
| 61 |
$idps = ""; |
| 62 |
foreach (glob("$cfg_path/*", GLOB_ONLYDIR) as $dir) { |
| 63 |
if (is_readable ("$dir/idp-metadata.xml")) { |
| 64 |
$idps .= " $dir/idp-metadata.xml"; |
| 65 |
$server->addProvider(LASSO_PROVIDER_ROLE_IDP, "$dir/idp-metadata.xml"); |
| 66 |
} |
| 67 |
} |
| 68 |
error_log ("IdPs:$idps"); |
| 69 |
$login = new LassoLogin($server); |
| 70 |
} catch (Exception $e) { |
| 71 |
error('Unexpected Lasso error: ' . $e); |
| 72 |
} |
| 73 |
|
| 74 |
try { |
| 75 |
try { |
| 76 |
$login->processAuthnResponseMsg($_POST["SAMLResponse"]); |
| 77 |
|
| 78 |
$xml = new SimpleXMLElement($login->response->exportToXml()); |
| 79 |
$xml_id = $xml->attributes()->{'InResponseTo'}; |
| 80 |
if ($id != $xml_id) { |
| 81 |
error_log ("ID parse error: guessed $id got $xml_id"); |
| 82 |
} |
| 83 |
|
| 84 |
if (!file_exists ("$state_path/$id") && !mkdir ("$state_path/$id", 0770)) { |
| 85 |
error ("State management failure (replay?)"); |
| 86 |
} |
| 87 |
|
| 88 |
file_put_contents ("$state_path/$id/samlresp", |
| 89 |
$login->response->getXmlNode(false)); |
| 90 |
} catch (LassoDsError $e) { |
| 91 |
error('Invalid signature'); |
| 92 |
} catch (LassoProfileCannotVerifySignatureError $e) { |
| 93 |
error('Invalid signature'); |
| 94 |
} catch (LassoError $e) { |
| 95 |
error('Misc error: ' . $e); |
| 96 |
} |
| 97 |
try { |
| 98 |
$ok = $login->acceptSso(); |
| 99 |
} catch (LassoError $e) { |
| 100 |
error('Invalid assertion'); |
| 101 |
} |
| 102 |
} catch (Exception $e) { |
| 103 |
error('Unexpected error: ' . $e); |
| 104 |
} |
| 105 |
|
| 106 |
if ($ok != 0) { |
| 107 |
error("acceptSso returned $ok"); |
| 108 |
} |
| 109 |
|
| 110 |
file_put_contents ("$state_path/$id/subject", |
| 111 |
$login->assertion->subject->nameId->content); |
| 112 |
file_put_contents ("$state_path/$id/success", ""); |
| 113 |
|
| 114 |
print "Congratulations! You are authenticated as: " |
| 115 |
. $login->assertion->subject->nameId->content; |
| 116 |
|
| 117 |
print "\n\n<!--\n"; |
| 118 |
print "\nDecrypted SAML Request:\n"; |
| 119 |
print "\n"; |
| 120 |
print $login->response->getXmlNode(false) . "\n"; |
| 121 |
print "\nBase64-decoded POST Data:\n"; |
| 122 |
print "\n"; |
| 123 |
print base64_decode ($_POST["SAMLResponse"]) . "\n"; |
| 124 |
print "\nEnd of data -->"; |
| 125 |
|
| 126 |
?> |