blob: f10e5ab812454198f9cd4230c79e43fdef5f7e63 [file] [log] [blame]
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -07001<?php
2
3require('config.php');
4
5if (!stristr($_SERVER["CONTENT_TYPE"], "application/soap+xml")) {
6 error_log("spp.php - Unexpected Content-Type " . $_SERVER["CONTENT_TYPE"]);
7 die("Unexpected Content-Type");
8}
9
10if ($_SERVER["REQUEST_METHOD"] != "POST") {
11 error_log("spp.php - Unexpected method " . $_SERVER["REQUEST_METHOD"]);
12 die("Unexpected method");
13}
14
15if (isset($_GET["realm"])) {
16 $realm = $_GET["realm"];
17 $realm = PREG_REPLACE("/[^0-9a-zA-Z\.\-]/i", '', $realm);
18} else {
19 error_log("spp.php - Realm not specified");
20 die("Realm not specified");
21}
22
Hai Shalom39ba6fc2019-01-22 12:40:38 -080023if (isset($_GET["test"]))
24 $test = PREG_REPLACE("/[^0-9a-zA-Z\_\-]/i", '', $_GET["test"]);
25else
26 $test = "";
27
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -070028unset($user);
29putenv("HS20CERT");
30
31if (!empty($_SERVER['PHP_AUTH_DIGEST'])) {
32 $needed = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1,
33 'uri'=>1, 'response'=>1);
34 $data = array();
35 $keys = implode('|', array_keys($needed));
36 preg_match_all('@(' . $keys . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@',
37 $_SERVER['PHP_AUTH_DIGEST'], $matches, PREG_SET_ORDER);
38 foreach ($matches as $m) {
39 $data[$m[1]] = $m[3] ? $m[3] : $m[4];
40 unset($needed[$m[1]]);
41 }
42 if ($needed) {
43 error_log("spp.php - Authentication failed - missing: " . print_r($needed));
44 die('Authentication failed');
45 }
46 $user = $data['username'];
47 if (strlen($user) < 1) {
48 error_log("spp.php - Authentication failed - empty username");
49 die('Authentication failed');
50 }
51
52
53 $db = new PDO($osu_db);
54 if (!$db) {
55 error_log("spp.php - Could not access database");
56 die("Could not access database");
57 }
58 $row = $db->query("SELECT password FROM users " .
59 "WHERE identity='$user' AND realm='$realm'")->fetch();
60 if (!$row) {
61 $row = $db->query("SELECT osu_password FROM users " .
62 "WHERE osu_user='$user' AND realm='$realm'")->fetch();
63 $pw = $row['osu_password'];
64 } else
65 $pw = $row['password'];
66 if (!$row) {
67 error_log("spp.php - Authentication failed - user '$user' not found");
68 die('Authentication failed');
69 }
70 if (strlen($pw) < 1) {
71 error_log("spp.php - Authentication failed - empty password");
72 die('Authentication failed');
73 }
74
75 $A1 = md5($user . ':' . $realm . ':' . $pw);
76 $A2 = md5($_SERVER['REQUEST_METHOD'] . ':' . $data['uri']);
77 $resp = md5($A1 . ':' . $data['nonce'] . ':' . $data['nc'] . ':' .
78 $data['cnonce'] . ':' . $data['qop'] . ':' . $A2);
79 if ($data['response'] != $resp) {
80 error_log("Authentication failure - response mismatch");
81 die('Authentication failed');
82 }
83} else if (isset($_SERVER["SSL_CLIENT_VERIFY"]) &&
84 $_SERVER["SSL_CLIENT_VERIFY"] == "SUCCESS" &&
85 isset($_SERVER["SSL_CLIENT_M_SERIAL"])) {
86 $user = "cert-" . $_SERVER["SSL_CLIENT_M_SERIAL"];
87 putenv("HS20CERT=yes");
88} else if (!isset($_SERVER["PATH_INFO"]) ||
89 $_SERVER["PATH_INFO"] != "/signup") {
90 header('HTTP/1.1 401 Unauthorized');
91 header('WWW-Authenticate: Digest realm="'.$realm.
92 '",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');
93 error_log("spp.php - Authentication required (not signup)");
94 die('Authentication required (not signup)');
95}
96
97
98if (isset($user) && strlen($user) > 0)
99 putenv("HS20USER=$user");
100else
101 putenv("HS20USER");
102
103putenv("HS20REALM=$realm");
Dmitry Shmidtd5ab1b52016-06-21 12:38:41 -0700104$postdata = file_get_contents("php://input");
105putenv("HS20POST=$postdata");
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700106$addr = $_SERVER["REMOTE_ADDR"];
107putenv("HS20ADDR=$addr");
Hai Shalom39ba6fc2019-01-22 12:40:38 -0800108putenv("HS20TEST=$test");
Dmitry Shmidtdf5a7e42014-04-02 12:59:59 -0700109
110$last = exec("$osu_root/spp/hs20_spp_server -r$osu_root -f/tmp/hs20_spp_server.log", $output, $ret);
111
112if ($ret == 2) {
113 if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
114 header('HTTP/1.1 401 Unauthorized');
115 header('WWW-Authenticate: Digest realm="'.$realm.
116 '",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');
117 error_log("spp.php - Authentication required (ret 2)");
118 die('Authentication required');
119 } else {
120 error_log("spp.php - Unexpected authentication error");
121 die("Unexpected authentication error");
122 }
123}
124if ($ret != 0) {
125 error_log("spp.php - Failed to process SPP request");
126 die("Failed to process SPP request");
127}
128//error_log("spp.php: Response: " . implode($output));
129
130header("Content-Type: application/soap+xml");
131
132echo implode($output);
133
134?>