spp.php 4.0 KB

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