Home | History | Annotate | Download | only in www
      1 <?php
      2 
      3 require('config.php');
      4 
      5 if (!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 
     10 if ($_SERVER["REQUEST_METHOD"] != "POST") {
     11   error_log("spp.php - Unexpected method " . $_SERVER["REQUEST_METHOD"]);
     12   die("Unexpected method");
     13 }
     14 
     15 if (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 
     23 if (isset($_GET["test"]))
     24   $test = PREG_REPLACE("/[^0-9a-zA-Z\_\-]/i", '', $_GET["test"]);
     25 else
     26   $test = "";
     27 
     28 unset($user);
     29 putenv("HS20CERT");
     30 
     31 if (!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($_GET["hotspot2dot0-mobile-identifier-hash"])) {
     89   $id_hash = $_GET["hotspot2dot0-mobile-identifier-hash"];
     90   $id_hash = PREG_REPLACE("/[^0-9a-h]/i", '', $id_hash);
     91 
     92   $db = new PDO($osu_db);
     93   if (!$db) {
     94     error_log("spp.php - Could not access database");
     95     die("Could not access database");
     96   }
     97 
     98   $row = $db->query("SELECT * FROM sim_provisioning " .
     99 		    "WHERE mobile_identifier_hash='$id_hash'")->fetch();
    100   if (!$row) {
    101     error_log("spp.php - SIM provisioning failed - mobile_identifier_hash not found");
    102     die('SIM provisioning failed - mobile_identifier_hash not found');
    103   }
    104 
    105   $imsi = $row['imsi'];
    106   $mac_addr = $row['mac_addr'];
    107   $eap_method = $row['eap_method'];
    108 
    109   $row = $db->query("SELECT COUNT(*) FROM osu_config " .
    110 		    "WHERE realm='$realm'")->fetch();
    111   if (!$row || intval($row[0]) < 1) {
    112     error_log("spp.php - SIM provisioning failed - realm $realm not found");
    113     die('SIM provisioning failed');
    114   }
    115 
    116   error_log("spp.php - SIM provisioning for IMSI $imsi");
    117   putenv("HS20SIMPROV=yes");
    118   putenv("HS20IMSI=$imsi");
    119   putenv("HS20MACADDR=$mac_addr");
    120   putenv("HS20EAPMETHOD=$eap_method");
    121   putenv("HS20IDHASH=$id_hash");
    122 } else if (!isset($_SERVER["PATH_INFO"]) ||
    123 	   $_SERVER["PATH_INFO"] != "/signup") {
    124   header('HTTP/1.1 401 Unauthorized');
    125   header('WWW-Authenticate: Digest realm="'.$realm.
    126 	 '",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');
    127   error_log("spp.php - Authentication required (not signup)");
    128   die('Authentication required (not signup)');
    129 }
    130 
    131 
    132 if (isset($user) && strlen($user) > 0)
    133   putenv("HS20USER=$user");
    134 else
    135   putenv("HS20USER");
    136 
    137 putenv("HS20REALM=$realm");
    138 $postdata = file_get_contents("php://input");
    139 putenv("HS20POST=$postdata");
    140 $addr = $_SERVER["REMOTE_ADDR"];
    141 putenv("HS20ADDR=$addr");
    142 putenv("HS20TEST=$test");
    143 
    144 $last = exec("$osu_root/spp/hs20_spp_server -r$osu_root -f/tmp/hs20_spp_server.log", $output, $ret);
    145 
    146 if ($ret == 2) {
    147   if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
    148     header('HTTP/1.1 401 Unauthorized');
    149     header('WWW-Authenticate: Digest realm="'.$realm.
    150            '",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');
    151     error_log("spp.php - Authentication required (ret 2)");
    152     die('Authentication required');
    153   } else {
    154     error_log("spp.php - Unexpected authentication error");
    155     die("Unexpected authentication error");
    156   }
    157 }
    158 if ($ret != 0) {
    159   error_log("spp.php - Failed to process SPP request");
    160   die("Failed to process SPP request");
    161 }
    162 //error_log("spp.php: Response: " . implode($output));
    163 
    164 header("Content-Type: application/soap+xml");
    165 
    166 echo implode($output);
    167 
    168 ?>
    169