| 1 | <?PHP
|
|---|
| 2 |
|
|---|
| 3 | require_once("config.php");
|
|---|
| 4 |
|
|---|
| 5 | function login()
|
|---|
| 6 | {
|
|---|
| 7 | global $ldaphost;
|
|---|
| 8 | global $baseDN;
|
|---|
| 9 | global $groupDN;
|
|---|
| 10 |
|
|---|
| 11 | $username = $_SERVER['PHP_AUTH_USER'];
|
|---|
| 12 | $password = $_SERVER['PHP_AUTH_PW'];
|
|---|
| 13 |
|
|---|
| 14 | $con = @ldap_connect($ldaphost);
|
|---|
| 15 | if (!$con)
|
|---|
| 16 | return "ldap_connect failed to ".$ldaphost;
|
|---|
| 17 |
|
|---|
| 18 | //------------------ Look for user common name
|
|---|
| 19 | $attributes = array('cn', 'mail');
|
|---|
| 20 | $dn = 'ou=People,'.$baseDN;
|
|---|
| 21 | $filter = '(uid='.$username.')';
|
|---|
| 22 |
|
|---|
| 23 | $sr = @ldap_search($con, $dn, $filter, $attributes);
|
|---|
| 24 | if (!$sr)
|
|---|
| 25 | return "ldap_search failed for dn=".$dn.": ".ldap_error($con);
|
|---|
| 26 |
|
|---|
| 27 | $srData = @ldap_get_entries($con, $sr);
|
|---|
| 28 | if ($srData["count"]==0)
|
|---|
| 29 | return "No results returned by ldap_get_entries for dn=".$dn.".";
|
|---|
| 30 |
|
|---|
| 31 | $email =$srData[0]['mail'][0];
|
|---|
| 32 | $userCommonName=$srData[0]['cn'][0];
|
|---|
| 33 | $userDN =$srData[0]['dn'];
|
|---|
| 34 |
|
|---|
| 35 | //------------------ Authenticate user
|
|---|
| 36 | if (!@ldap_bind($con, $userDN, $password))
|
|---|
| 37 | return "ldap_bind failed: ".ldap_error($con);
|
|---|
| 38 |
|
|---|
| 39 | //------------------ Check if the user is in FACT ldap group
|
|---|
| 40 | $attributes= array("member");
|
|---|
| 41 | $filter= '(objectClass=*)';
|
|---|
| 42 |
|
|---|
| 43 | // Get all members of the group.
|
|---|
| 44 | $sr = @ldap_read($con, $groupDN, $filter, $attributes);
|
|---|
| 45 | if (!$sr)
|
|---|
| 46 | return "ldap_read failed for dn=".$groupDN.": ".ldap_error($con);
|
|---|
| 47 |
|
|---|
| 48 | // retrieve the corresponding data
|
|---|
| 49 | $srData = @ldap_get_entries($con, $sr);
|
|---|
| 50 | if ($srData["count"]==0)
|
|---|
| 51 | return "No results returned by ldap_get_entries for dn=".$dn.".";
|
|---|
| 52 |
|
|---|
| 53 | @ldap_unbind($con);
|
|---|
| 54 |
|
|---|
| 55 | $found = false;
|
|---|
| 56 | foreach ($srData[0]['member'] as $member)
|
|---|
| 57 | if (strpos($member, "cn=".$userCommonName.",")===0)
|
|---|
| 58 | return "";
|
|---|
| 59 |
|
|---|
| 60 | return "Sorry, your credentials don't match!";
|
|---|
| 61 | }
|
|---|
| 62 | // --------------------------------------------------------------------
|
|---|
| 63 |
|
|---|
| 64 | if (isset($_GET['load']))
|
|---|
| 65 | {
|
|---|
| 66 | require_once('log/Browscap.php');
|
|---|
| 67 |
|
|---|
| 68 | $d = date("Y/m");
|
|---|
| 69 |
|
|---|
| 70 | $path = "log/".$d;
|
|---|
| 71 |
|
|---|
| 72 | if (!file_exists("log/cache"))
|
|---|
| 73 | mkdir("log/cache", 0777, true);
|
|---|
| 74 |
|
|---|
| 75 | if (!file_exists($path))
|
|---|
| 76 | mkdir($path, 0777, true);
|
|---|
| 77 |
|
|---|
| 78 | $addr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : "";
|
|---|
| 79 | $user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : "";
|
|---|
| 80 | $dns = gethostbyaddr($addr);
|
|---|
| 81 |
|
|---|
| 82 | $bcap = new phpbrowscap\Browscap('log/cache');
|
|---|
| 83 | $info = $bcap->getBrowser();
|
|---|
| 84 |
|
|---|
| 85 | $file = fopen($path."/smartfact.log", "a");
|
|---|
| 86 | fwrite($file,
|
|---|
| 87 | date("Y-m-d H:i:s\t").$addr.
|
|---|
| 88 | "\t".$dns.
|
|---|
| 89 | "\t".$user.
|
|---|
| 90 | "\t".$info->Platform.
|
|---|
| 91 | "\t".$info->Browser.
|
|---|
| 92 | "\t".$info->Version.
|
|---|
| 93 | "\t".$info->isMobileDevice."\n");
|
|---|
| 94 | fclose($file);
|
|---|
| 95 |
|
|---|
| 96 | return;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | if (isset($_GET['logout']))
|
|---|
| 100 | {
|
|---|
| 101 | if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']))
|
|---|
| 102 | return;
|
|---|
| 103 |
|
|---|
| 104 | return header('HTTP/1.0 401 Successfull logout!');
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | // --------------------------------------------------------------------
|
|---|
| 108 |
|
|---|
| 109 | if (!isset($_GET['start']) && !isset($_GET['stop']))
|
|---|
| 110 | return header('HTTP/1.0 400 Command not supported');
|
|---|
| 111 |
|
|---|
| 112 | // --------------------------------------------------------------------
|
|---|
| 113 |
|
|---|
| 114 | if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']))
|
|---|
| 115 | {
|
|---|
| 116 | header('WWW-Authenticate: Basic realm="SmartFACT++"');
|
|---|
| 117 | header('HTTP/1.0 401 Unauthorized');
|
|---|
| 118 | return;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | $rc = login();
|
|---|
| 122 | if ($rc!="")
|
|---|
| 123 | return header('HTTP/1.0 401 '.$rc);
|
|---|
| 124 |
|
|---|
| 125 | // --------------------------------------------------------------------
|
|---|
| 126 |
|
|---|
| 127 | $out = array();
|
|---|
| 128 |
|
|---|
| 129 | if (isset($_GET['stop']))
|
|---|
| 130 | $str = exec($path."/dimctrl --user '".$_SERVER['PHP_AUTH_USER']."' --stop", $out, $rc);
|
|---|
| 131 |
|
|---|
| 132 | if (isset($_GET['start']))
|
|---|
| 133 | {
|
|---|
| 134 | // Filename
|
|---|
| 135 | $args = '"scripts/'.$_GET['start'].'"';
|
|---|
| 136 |
|
|---|
| 137 | unset($_GET['start']);
|
|---|
| 138 |
|
|---|
| 139 | /*
|
|---|
| 140 | $args = "";
|
|---|
| 141 | foreach ($_GET as $key => $value)
|
|---|
| 142 | $args .= " --arg:".$key."=".$value;
|
|---|
| 143 | $str = exec($path."/dimctrl --exec ".$args, $out, $rc);
|
|---|
| 144 | */
|
|---|
| 145 |
|
|---|
| 146 | // Label
|
|---|
| 147 | if (isset($_GET['label']))
|
|---|
| 148 | {
|
|---|
| 149 | if ($_GET['label']>=0)
|
|---|
| 150 | $args .= ":".$_GET['label'];
|
|---|
| 151 | unset($_GET['label']);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | // Arguments
|
|---|
| 155 | foreach ($_GET as $key => $value)
|
|---|
| 156 | $args .= ' --arg:"'.$key.'='.$value.'"';
|
|---|
| 157 |
|
|---|
| 158 | // $args = "filename":label --arg:"key1=value" --arg:"key2=value"
|
|---|
| 159 | $cmd = $path.'/dimctrl --user "'.$_SERVER['PHP_AUTH_USER'].'" --start '.$args;
|
|---|
| 160 |
|
|---|
| 161 | // Execute
|
|---|
| 162 | $str = exec($cmd, $out, $rc);
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | if ($rc!=1 && $rc!=2)
|
|---|
| 166 | return header('HTTP/1.0 500 Execution failed [rc='.$rc."]");
|
|---|
| 167 |
|
|---|
| 168 | print($rc);
|
|---|
| 169 |
|
|---|
| 170 | if (isset($_GET['debug']))
|
|---|
| 171 | {
|
|---|
| 172 | print("\n");
|
|---|
| 173 | print_r($out);
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | ?>
|
|---|