2 /* vim: set ts=4 tw=0 sw=4 noet: */
3 require_once $CFG->root
.'config.php';
4 require_once $CFG->root
. 'lib/session_handler.inc.php';
8 private $timeout = false;
12 private $header = '<!DOCTYPE html>
15 <meta charset="utf-8">
16 <link rel="stylesheet" href="css/styles.css">
18 var timeout = __TIMEOUT__;
20 <script src="__ROOT__js/timer.js"></script>
21 <script src="__ROOT__js/checkbox.js"></script>
22 <script src="__ROOT__js/forms.js"></script>
23 <title>__TITLE__</title>
29 <li><a href="index.php">Home</a></li>
31 <a href="#">Sections <span class="caret"></span></a>
34 <li><a href="qtadmin.php">Quarantine admin</a></li>
35 <li><a href="wblist.php">WB list admin</a></li>
39 <li><a href="about.html">About</a></li>
40 <li><a href="auth.php?op=logout">Logout</a></li>
46 <li><a href="index.php">Home</a></li>
48 <a href="#">Sections <span class="caret"></span></a>
52 <a href="qtadmin.php">Quarantine admin</span></a>
54 <li><a href="#">WB list admin <span class="caret"></a>
57 <li><a href="wblist.php?p=show&s=all">Show All</a></li>
58 <li><a href="wblist.php?p=show&s=b">Show Blacklist</a></li>
59 <li><a href="wblist.php?p=show&s=w">Show Whitelist</a></li>
60 <li><a href="wblist.php?p=add">Add Rule</a></li>
61 <li><a href="wblist.php?p=del">Delete Rule</a></li>
68 <li><a href="about.html">About</a></li>
69 <li><a href="auth.php?op=logout">Logout</a></li>
72 <div id="container">';
73 private $footer = '</div><div id="footer"><p>Powered by <a href="https://qtadmin.datanom.net"
74 title="Goto QtAdmin homepage">QtAdmin</a>. © 2015 by Michael Rasmussen</p>
75 </div></body></html>';
76 private $heading = '<p id="time" class="time">Session timeout:
77 <span id="timer"></span></p><h1 class="h1">__TITLE__</h1>';
79 public function __construct() {
82 if (isset($CFG->log_level
)) {
83 $this->log_level
= $CFG->log_level
;
88 if (isset($CFG->log_method
)) {
89 $this->log_method
= $CFG->log_method
;
91 $this->log_level
= 'syslog';
94 $this->log("Init Utils", 4);
96 $this->log("__construct[1]: user ".var_export($this->settings
['user'], true), 3);
97 $this->startSession();
98 $this->log("__construct[2]: user ".var_export($this->settings
['user'], true), 3);
100 if (! isset($_SESSION['settings'])) {
101 $this->initSettings();
103 $this->log("__construct[3]: user ".var_export($this->settings
['user'], true), 3);
104 $this->settings
= $_SESSION['settings'];
105 $this->log("__construct[4]: user ".var_export($this->settings
['user'], true), 3);
107 if ($CFG->auth_method
== 'HTTP_AUTH') {
108 if (isset($_SERVER['PHP_AUTH_USER'])) {
109 $this->settings
['user'] = $_SERVER['PHP_AUTH_USER'];
110 $this->settings
['loginStatus'] = 'OK';
111 if ($CFG->admin_user
== $this->settings
['user'])
112 $this->settings
['admin'] = true;
117 private function log($message, $level = 1) {
120 if ($level > $this->log_level
)
125 $priority = LOG_INFO
;
127 case 1: $priority = LOG_ERR
; break;
128 case 2: $priority = LOG_WARNING
; break;
129 case 3: $priority = LOG_INFO
; break;
130 case 4: $priority = LOG_DEBUG
; break;
133 switch ($this->log_method
) {
135 if (isset($CFG->log_file
)) {
136 if ($CFG->log_file
[0] == '/') {
137 $file = $CFG->log_file
;
139 $file = $CFG->root
.$CFG->log_file
;
142 $file = $CFG->root
.'qtadmin.log';
144 file_put_contents($file, "[$time]: $message\n", FILE_APPEND | LOCK_EX
);
148 file_put_contents('php://stderr', "[$time]: $message\n");
151 syslog($priority, $message);
156 private function initSettings() {
157 $this->log("InitSettings", 4);
159 if ('' == session_id()) {
160 $this->startSession();
163 if (false !== $this->timeout
) {
164 $timeout = $this->timeout
;
169 $this->settings
= array(
172 'loginStatus' => 'Not logged in',
173 'timeout' => $timeout
176 $_SESSION['settings'] = $this->settings
;
179 private function startSession() {
182 $this->log("startSession", 4);
184 if (isset($CFG->session_timeout
)) {
185 $this->timeout
= $CFG->session_timeout
* 60;
187 $this->timeout
= 20 * 60;
190 if (ini_get('session.gc_maxlifetime') != $this->timeout
)
191 ini_set('session.gc_maxlifetime', $this->timeout
);
192 //if (ini_get('session.cookie_lifetime') != $this->timeout)
193 // ini_set('session.cookie_lifetime', $this->timeout);
194 ini_set('session.cookie_lifetime', 0);
199 private function checkSession() {
202 $this->log("checkSession", 4);
204 if ('' == session_id()) {
205 $this->startSession();
208 $time = $_SERVER['REQUEST_TIME'];
209 if (isset($_SESSION['LAST_ACTIVITY']) &&
210 ($time - $_SESSION['LAST_ACTIVITY']) >= $this->settings
['timeout']) {
211 $this->log('R_TIME: '.date('c', $time).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY'].
212 'Test: '.($time - $_SESSION['LAST_ACTIVITY'])).' >= '.$this->settings
['timeout'], 3);
215 $_SESSION['LAST_ACTIVITY'] = $time;
219 private function getCSRFPreventionToken($ticket) {
220 return array('CSRFPreventionToken: ' . $ticket->CSRFPreventionToken
);
223 private function getRestTicket($username, $password) {
227 $url = $CFG->wblistadm_url
. '/ticket';
229 $data = "username=$username&password=$password";
230 $response = $this->RESTCall($url, $data, $cookiesIn = '');
231 if ($response['http_code'] >= 200 && $response['http_code'] <= 204) {
232 $data = json_decode($response['content']);
233 $_SESSION['ticket'] = $data->data
;
234 $_SESSION['cookies'] = $response['cookies'];
241 public function makeRestCall($method, $data = null) {
246 $url = $CFG->wblistadm_url
. "$method";
247 $token = $this->getCSRFPreventionToken($_SESSION['ticket']);
248 $response = $this->RESTCall($url, $data, $_SESSION['cookies'], $token);
250 if ($response['http_code'] >= 200 && $response['http_code'] <= 204) {
256 $data = json_decode($response['content']);
257 $result = $data->data
;
260 $result = ($data) ?
false : array();
266 private function RESTCall($url, $data = null, $cookiesIn = '', $headers = null) {
268 CURLOPT_RETURNTRANSFER
=> true, // return web page
269 CURLOPT_HEADER
=> true, //return headers in addition to content
270 CURLOPT_FOLLOWLOCATION
=> true, // follow redirects
271 CURLOPT_ENCODING
=> "", // handle all encodings
272 CURLOPT_AUTOREFERER
=> true, // set referer on redirect
273 CURLOPT_CONNECTTIMEOUT
=> 120, // timeout on connect
274 CURLOPT_TIMEOUT
=> 120, // timeout on response
275 CURLOPT_MAXREDIRS
=> 10, // stop after 10 redirects
276 CURLINFO_HEADER_OUT
=> true,
277 CURLOPT_SSL_VERIFYPEER
=> false, // Disabled SSL Cert checks
278 CURLOPT_HTTP_VERSION
=> CURL_HTTP_VERSION_1_1
,
279 CURLOPT_COOKIE
=> $cookiesIn
283 $options[CURLOPT_POST
] = 1;
284 $options[CURLOPT_POSTFIELDS
] = $data;
288 $options[CURLOPT_HTTPHEADER
] = $headers;
291 $ch = curl_init($url);
292 curl_setopt_array($ch, $options);
293 $rough_content = curl_exec($ch);
294 $err = curl_errno($ch);
295 $errmsg = curl_error($ch);
296 $header = curl_getinfo($ch);
299 $header_content = substr($rough_content, 0, $header['header_size']);
300 $body_content = trim(str_replace($header_content, '', $rough_content));
301 $pattern = "#Set-Cookie:\\s+(?<cookie>[^=]+=[^;]+)#m";
302 preg_match_all($pattern, $header_content, $matches);
303 $cookiesOut = implode("; ", $matches['cookie']);
305 $header['errno'] = $err;
306 $header['errmsg'] = $errmsg;
307 $header['headers'] = $header_content;
308 $header['content'] = $body_content;
309 $header['cookies'] = $cookiesOut;
314 public function logout() {
315 $this->log("logout", 4);
317 if (ini_get('session.use_cookies')) {
318 $params = session_get_cookie_params();
319 setcookie(session_name(), '', time() - 42000,
320 $params['path'], $params['domain'],
321 $params['secure'], $params['httponly']);
324 if ('' != session_id()) {
329 $this->settings
= array();
332 public function isAdmin() {
335 $this->log("isAdmin", 4);
337 if (isset($this->settings
['admin'])) {
338 $admin = $this->settings
['admin'];
344 public function login($user, $pw) {
348 $this->log("login", 4);
350 if ('' == session_id()) {
351 $this->startSession();
354 $this->settings
['user'] = null;
355 $this->settings
['admin'] = false;
357 $p = explode('@', $user);
358 if (count($p) != 2) {
359 $this->settings
['loginStatus'] = 'Bad username';
362 $dn = "mail=$user,ou=Users,domainName=$domain,$CFG->ldap_base_dn";
363 $filter = "(&(objectclass=mailUser)(accountStatus=active)(mail=$user))";
364 $ds = @ldap_connect
($CFG->ldap_dsn
);
366 @ldap_set_option
($ds, LDAP_OPT_PROTOCOL_VERSION
, 3);
367 $r = @ldap_bind
($ds, $dn, $pw);
369 $sr = @ldap_search
($ds, $CFG->ldap_base_dn
, $filter, array('mail','domainglobaladmin'));
370 $info = @ldap_get_entries
($ds, $sr); // array
371 if ($info['count'] > 0) {
372 // Log in to wblistadm server and get CSRFPreventionToken
373 if ($this->getRestTicket($user, $pw)) {
374 $this->settings
['user'] = $user;
376 $this->settings
['loginStatus'] = 'OK';
378 if (isset($info[0]['domainglobaladmin'])) {
379 $admin = $info[0]['domainglobaladmin'][0];
380 $admin = strtoupper($admin);
382 $this->settings
['admin'] = ($admin == 'YES') ?
true : false;
384 $this->settings
['loginStatus'] = 'Login failed';
387 $this->settings
['loginStatus'] = 'Login failed';
390 $this->settings
['loginStatus'] = ldap_error($ds);
394 $this->settings
['loginStatus'] = 'Connect to LDAP server failed';
398 $_SESSION['settings'] = $this->settings
;
403 public function getLoginStatus() {
404 $status = 'Not logged in';
406 $this->log("getLoginStatus", 4);
408 if (isset($this->settings
['loginStatus'])) {
409 $status = $this->settings
['loginStatus'];
415 public function isLoggedIn() {
419 $this->log("isLoggedIn[1]: user ".var_export($this->settings
['user'], true), 3);
421 if ('' == session_id()) {
422 $this->startSession();
425 $this->log("isLoggedIn[2]: user ".var_export($this->settings
['user'], true), 3);
426 $this->checkSession();
427 $this->log("isLoggedIn[3]: user ".var_export($this->settings
['user'], true), 3);
429 if (isset($this->settings
['user'])) {
430 if ($this->settings
['user'] != null) {
433 if ($CFG->auth_method
== 'HTTP_AUTH') {
434 if (isset($_SERVER['PHP_AUTH_USER'])) {
435 $this->settings
['user'] = $_SERVER['PHP_AUTH_USER'];
442 if ($loggedIn == false) {
443 $this->log('$this->settings: '.var_export($this->settings
, true), 3);
444 $this->log('R_TIME: '.date('c', $_SERVER['REQUEST_TIME']).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']), 3);
447 $_SESSION['settings'] = $this->settings
;
452 public function getUser() {
455 $this->log("getUser", 4);
457 if ($this->isLoggedIn()) {
458 $user = $this->settings
['user'];
464 public function authorized($recipient) {
467 $this->log("authorized '$recipient'", 3);
469 if ($this->isAdmin() ||
$this->getUser() == $recipient) {
472 $msg = ($authorized) ?
'authorize' : 'not authorize';
473 $this->log("$msg '".$this->getUser()."' rcpt '$recipient'", 3);
478 public function getHeader() {
479 $this->log("getHeader", 4);
481 return $this->header
;
484 public function getFooter() {
485 $this->log("getFooter", 4);
487 return $this->footer
;
490 public function getHeading() {
491 $this->log("getHeading", 4);
493 return $this->heading
;
496 public function setHeading($heading) {
499 $this->log("setHeading", 4);
501 $timeout = $CFG->session_timeout
* 60 * 1000;
502 $this->heading
= str_replace('__TITLE__', $heading, $this->heading
);
503 $this->header
= str_replace('__TITLE__', $heading, $this->header
);
504 $this->header
= str_replace('__ROOT__', $CFG->wwwroot
, $this->header
);
505 $this->header
= str_replace('__TIMEOUT__', $timeout, $this->header
);
508 public function convertContent($code) {
509 $this->log("convertContent", 4);
524 $string = $table[$code];