<commit_before><?php
/*
KVM-VDI
<NAME>

Vilnius University.
Center of Information Technology Development.


Vilnius,Lithuania.
2016-09-06
*/
include ('functions/config.php');
require_once('functions/functions.php');
slash_vars();
if ($_SERVER['HTTP_USER_AGENT']=='KVM-VDI client')
    $html5_client=0;
else
    $html5_client=1;
if (isset ($_POST['username'])){
    $username=$_POST['username'];
    $password=$_POST['password'];
    $sql_reply=get_SQL_line("SELECT id,password FROM clients WHERE username LIKE '$username' AND isdomain=0");
    if(!empty($sql_reply[1])){
	if (password_verify($password, $sql_reply[1])){
	    if (session_status() == PHP_SESSION_NONE) 
		session_start();
	    $_SESSION['client_logged']='yes';
	    $_SESSION['userid']=$sql_reply[0];
	    $_SESSION['username']=$username;
	    $ip = $_SERVER['REMOTE_ADDR'];
	    add_SQL_line("UPDATE clients SET lastlogin=now(), ip='$ip' WHERE id='$sql_reply[0]'");
	    header("Location: $serviceurl/client_pools.php");
	    exit;
	}
    }
    else if ($ad_enabled){
	$query_user =$username."@".$ad_name;
	$ldap_login_err=0;
	$ldap = ldap_connect($ad_host) or $ldap_login_err=1;
	ldap_bind($ldap,$query_user,$password) or  $ldap_login_err=1;
	if ($ldap_login_err){
	    if (!$html5_client){
		echo 'LOGIN_FAILURE';
		exit;
	    }
	    else {
		header ("Location: $serviceurl/client_index.php?error=1");
		exit;
	    }
	}
	else {
	    $results = ldap_search($ldap,$ldap_dn,"(samaccountname=$username)",array("memberof","primarygroupid","displayname"));
	    $entries = ldap_get_entries($ldap, $results);
	}
	$output=array();
	$token=0;
	if (isset($entries[0]['memberof']))
    	    $output = $entries[0]['memberof'];
	$token = $entries[0]['primarygroupid'][0];
    	$fullname= $entries[0]['displayname'][0];
	if(isset($output))
	    array_shift($output);
	if (isset($group_dn))
	    $results2 = ldap_search($ldap,$group_dn,"(objectcategory=group)",array("distinguishedname","primarygrouptoken"));
	else
	    $results2 = ldap_search($ldap,$ldap_dn,"(objectcategory=group)",array("distinguishedname","primarygrouptoken"));
        $entries2 = ldap_get_entries($ldap, $results2);
        array_shift($entries2);
	foreach($entries2 as $e) {
    	    if($e['primarygrouptoken'][0] == $token) {
		$output[] = $e['distinguishedname'][0];
		break;
	    }
	}
	$group_count=0;
	$group_array='';
	foreach ($output as &$value) {
	    $tmp_CN=explode(",",$value);
	    $tmp_CN[0]=str_replace("CN=","",$tmp_CN[0]);
	    if (!empty($tmp_CN[0]))
		++$group_count;
	    $group_array="" . $group_array . "','" . $tmp_CN[0];
	}
	if($group_count){
	    $group_array = substr($group_array, 2); 
	    $group_array=$group_array."'";
	    $ad_groups_validate=get_SQL_array("SELECT * FROM ad_groups WHERE name IN ($group_array)");
	    $ip = $_SERVER['REMOTE_ADDR'];<commit_msg>Adding debug capabilities to LDAP logins<commit_after><?php
/*
KVM-VDI
<NAME>

Vilnius University.
Center of Information Technology Development.


Vilnius,Lithuania.
2016-09-09
*/
include ('functions/config.php');
require_once('functions/functions.php');
slash_vars();
if ($_SERVER['HTTP_USER_AGENT']=='KVM-VDI client')
    $html5_client=0;
else
    $html5_client=1;
if (isset ($_POST['username'])){
    $username=$_POST['username'];
    $password=$_POST['password'];
    $sql_reply=get_SQL_line("SELECT id,password FROM clients WHERE username LIKE '$username' AND isdomain=0");
    if(!empty($sql_reply[1])){
	if (password_verify($password, $sql_reply[1])){
	    if (session_status() == PHP_SESSION_NONE) 
		session_start();
	    $_SESSION['client_logged']='yes';
	    $_SESSION['userid']=$sql_reply[0];
	    $_SESSION['username']=$username;
	    $ip = $_SERVER['REMOTE_ADDR'];
	    add_SQL_line("UPDATE clients SET lastlogin=now(), ip='$ip' WHERE id='$sql_reply[0]'");
	    header("Location: $serviceurl/client_pools.php");
	    exit;
	}
    }
    else if ($ad_enabled){
	$query_user =$username."@".$ad_name;
	$ldap_login_err=0;
	$ldap = ldap_connect($ad_host) or $ldap_login_err=1;
	ldap_bind($ldap,$query_user,$password) or  $ldap_login_err=1;
	if ($ldap_login_err){
	    write_log("LDAP bind failure. Invalid credentials.");
	    if (!$html5_client){
		echo 'LOGIN_FAILURE';
		exit;
	    }
	    else {
		header ("Location: $serviceurl/client_index.php?error=1");
		exit;
	    }
	}
	else {
	    $results = ldap_search($ldap,$ldap_dn,"(samaccountname=$username)",array("memberof","primarygroupid","displayname"));
	    $entries = ldap_get_entries($ldap, $results);
	}
	$output=array();
	$token=0;
	if (isset($entries[0]['memberof']))
    	    $output = $entries[0]['memberof'];
	$token = $entries[0]['primarygroupid'][0];
    	$fullname= $entries[0]['displayname'][0];
	if(isset($output))
	    array_shift($output);
	if (isset($group_dn))
	    $results2 = ldap_search($ldap,$group_dn,"(objectcategory=group)",array("distinguishedname","primarygrouptoken"));
	else
	    $results2 = ldap_search($ldap,$ldap_dn,"(objectcategory=group)",array("distinguishedname","primarygrouptoken"));
        $entries2 = ldap_get_entries($ldap, $results2);
	write_log("Listing primary groupid: " . serialize($entries));
	write_log("Listing groups: " . serialize($entries2));
        array_shift($entries2);
	foreach($entries2 as $e) {
    	    if($e['primarygrouptoken'][0] == $token) {
		$output[] = $e['distinguishedname'][0];
		break;
	    }
	}
	$group_count=0;
	$group_array='';
	foreach ($output as &$value) {
	    $tmp_CN=explode(",",$value);
	    $tmp_CN[0]=str_replace("CN=","",$tmp_CN[0]);
	    if (!empty($tmp_CN[0]))
		++$group_count;
	    $group_array="" . $group_array . "','" . $tmp_CN[0];
	}
	if($group_count){
	    $group_array = substr($group_array, 2); 
	    $group_array=$group_array."'";
	    $ad_groups_validate=get_SQL_array("SELECT * FROM ad_groups WHERE name IN ($group_array)");
	    $ip = $_SERVER['REMOTE_ADDR'];
{'fraction_non_alphanumeric': 0.19291338582677164, 'fraction_numerical': 0.014892160219103046, 'mean_word_length': 5.209351753453773, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1018317', 'n_tokens_mistral': 2219, 'n_tokens_neox': 2124, 'n_words': 316}