#!/usr/bin/perl

package unix_shell;

sub probe { return 1; }

sub exists {
        shift @_;
        my $user_id = shift @_;
        my $oper_id = shift @_;

        my $sth;
        my $sql;
        my $count;

        $sql = "SELECT count(*) FROM unix_shell_services WHERE user_id = '$user_id' AND
 oper_id = '$oper_id'";

        $sth = $main::dbh->prepare($sql);

                if( !$sth ) {
                        return -1;
                }

                $rv = $sth->execute;

                if( !$rv ) {
                        return -1;
                }

                ($count) = $sth->fetchrow_array;
                return $count;
}


sub create_service {
	my($service) = shift @_;
        my(  $enabled, $user_id, $oper_id, $client_id, $password,
             $date_create, $date_active, $date_expire, $tariff_plan,
             $water_mark, $descr, $smtp_enable, $pop3_enable) = @_;

	#my($service) = shift @_;
	#my($user_id) = shift @_;
	#my($oper_id) = shift @_;
	#my($client_id) = shift @_;


	$user_id =~ s/\ //g;
	$user_id =~ s/\'//g;
	$user_id =~ s/\"//g;

	if(!$user_id) {
		print "<SCRIPT> alert(\"$main::D_COMMIT_ERROR: user_id: $user_id\") </SCRIPT>\n";
		return;
	}

	($client_name, $client_address) = $main::dbh->selectrow_array("SELECT base_clients.name, base_clients.address_jur FROM base_clients, unix_shell_services WHERE unix_shell_services.user_id = '$user_id' AND unix_shell_services.oper_id = '$oper_id' AND unix_shell_services.client_id = base_clients.client_id AND unix_shell_services.oper_id = base_clients.oper_id");

	$client_name =~ s/\'//g;
	$client_address =~ s/\'//g;
	$client_name =~ s/\"//g;
	$client_address =~ s/\"//g;
	$crypted_passwd = base::encrypt($main::password);

	$sth = $main::dbh->prepare("SELECT host_id, login, access_type FROM unix_shell_hosts WHERE oper_id = '$oper_id' ");

	$sth->execute;

	if($main::enabled){$shell=$main::shell;}
		else{$shell = "/sbin/nologin";}
	if(!$shell) {$shell = "/bin/date";}

	while( ($host_id, $login, $access_type)=$sth->fetchrow_array ) {

		$program  = $CFG::UNIXSHELL_ACCESS_TYPES{$access_type}->{'program'};
		$cmd  = "/usr/lib/cherry/unix_shell/adduser.sh";
		$args = "'$access_type' '$program' '$host_id' '$login' '$user_id' '".$crypted_passwd."' '$shell' '$client_name $client_address' '$CFG::BASE/identity-$host_id'";

	
		# Add new user account and all its data

		if(system("$cmd $args") != 0) { 
		print "<SCRIPT> alert(\"$main::D_COMMIT_ERROR: $cmd $args\") </SCRIPT>\n"; }

		# Update password if it is given1. Remember, if password
		# was not initially set the access it prohibited!

		if($main::password) {
		    $cmd  = "/usr/lib/cherry/unix_shell/chpass.sh";
		    if(system("$cmd $args") != 0) { 
		    print "<SCRIPT> alert(\"$main::D_COMMIT_ERROR: $cmd $args\") </SCRIPT>\n"; }
		}
	}


        if(!$date_create) { $date_create = "'now'";}
        else { $date_create = "'$date_create'"; }

        if(!$date_active) { $date_active = 'null';}
        else { $date_active = "'$date_active'"; }

        if(!$date_expire) { $date_expire = 'null';}
        else { $date_expire = "'$date_expire'"; }

	if(!$water_mark) { $water_mark = '0'; }
	if(!$enabled) { $enabled = '1'; }

	if(!$smtp_enable) { $smtp_enable = 1; }
	if(!$pop3_enable) { $pop3_enable = 1; } 

        my $sql = "INSERT INTO unix_shell_services (enabled, user_id, oper_id, client_id, password, password_type, date_create, date_active, date_expire, tariff_plan, water_mark, descr, smtp_enable, pop3_enable) VALUES ($enabled, '$user_id', '$oper_id', '$client_id', '$crypted_passwd', '2', $date_create, $date_active, $date_expire, '$tariff_plan', $water_mark, '$descr', $smtp_enable, $pop3_enable)";

         $sth = $main::dbh->prepare($sql);

                if( !$sth ) { return -1; }

                $rv = $sth->execute;
                if( !$rv ) { return -1; }

	return $user_id;
}

sub chpass_service {
	my($service) = shift @_;
	my($user_id) = shift @_;
	my($oper_id) = shift @_;
	my($password) = shift @_;
	my($password_type) = shift @_;

	if(!$user_id) {
		print "<SCRIPT> alert(\"$main::D_COMMIT_ERROR: user_id: $user_id\") </SCRIPT>\n";
		return;
	}

	$sth = $main::dbh->prepare("SELECT host_id, login, access_type FROM unix_shell_hosts WHERE oper_id = '$oper_id' ");

	$sth->execute;

	while( ($host_id, $login, $access_type)=$sth->fetchrow_array ) {

		$program = $CFG::UNIXSHELL_ACCESS_TYPES{$access_type}->{'program'};

		$args = "$access_type $program $host_id $login $user_id  '".$password."' '$shell' '$client_name $client_address' $CFG::BASE/identity-$host_id";

		    $cmd  = "/usr/lib/cherry/unix_shell/chpass.sh";
		    if(system("$cmd $args 2>/dev/null") != 0) { 
		    print "<SCRIPT> alert(\"$main::D_COMMIT_ERROR: $cmd\") </SCRIPT>\n"; }
	}
}



sub update_service {
	my($service) = shift @_;
	my($user_id) = shift @_;
	my($oper_id) = shift @_;

	$user_id =~ s/\ //g;
	$user_id =~ s/\'//g;
	$user_id =~ s/\"//g;

	if(!$user_id) {
		print "<SCRIPT> alert(\"$main::D_COMMIT_ERROR: user_id: $user_id\") </SCRIPT>\n";
		return;
	}

	($client_name, $client_address) = $main::dbh->selectrow_array("SELECT base_clients.name, base_clients.address_jur FROM base_clients, unix_shell_services WHERE unix_shell_services.user_id = '$user_id' AND unix_shell_services.oper_id = '$oper_id' AND unix_shell_services.client_id = base_clients.client_id AND unix_shell_services.oper_id = base_clients.oper_id");

	$client_name =~ s/\'//g;
	$client_address =~ s/\'//g;
	$client_name =~ s/\"//g;
	$client_address =~ s/\"//g;
	$crypted_passwd = base::encrypt($main::password);

	if($main::enabled){$shell=$main::shell;}
		else{$shell = "/sbin/nologin";}

	if(!$shell) {$shell = "/bin/date";}

	$sth = $main::dbh->prepare("SELECT host_id, login, access_type FROM unix_shell_hosts WHERE oper_id = '$oper_id' ");

	$sth->execute;

	while( ($host_id, $login, $access_type)=$sth->fetchrow_array ) {

		$program = $CFG::UNIXSHELL_ACCESS_TYPES{$access_type}->{'program'};

		$args = "'$access_type' '$program' '$host_id' '$login' '$user_id'  '".$crypted_passwd."' '$shell' '$client_name $client_address' '$CFG::BASE/identity-$host_id'";
		$cmd = "/usr/lib/cherry/unix_shell/updateuser.sh";


		### Update general data 

		if(system("$cmd $args") != 0) { 
		print "<SCRIPT> alert(\"$main::D_COMMIT_ERROR: $cmd\") </SCRIPT>\n"; }

		### Update password if it is given

		if($main::password) {
		    $cmd  = "/usr/lib/cherry/unix_shell/chpass.sh";
		    if(system("$cmd $args") != 0) { 
		    print "<SCRIPT> alert(\"$main::D_COMMIT_ERROR: $cmd\") </SCRIPT>\n"; }
		}
	}
}


sub destroy_service {
	my($service) = shift @_;
	my($user_id) = shift @_;
	my($oper_id) = shift @_;

	$user_id =~ s/\ //g;
	$user_id =~ s/\'//g;
	$user_id =~ s/\"//g;
 
	if(!$user_id) {
		print "<SCRIPT> alert(\"$main::D_COMMIT_ERROR: user_id: $user_id\") </SCRIPT>\n";
		return;
	}
 
	$sth = $main::dbh->prepare("SELECT host_id, login, access_type FROM unix_shell_hosts WHERE oper_id = '$oper_id' ");

	$sth->execute;

	while( ($host_id, $login, $access_type)=$sth->fetchrow_array ) {

		$program = $CFG::UNIXSHELL_ACCESS_TYPES{$access_type}->{'program'};

		$cmd = "/usr/lib/cherry/unix_shell/removeuser.sh '$access_type' '$program' '$host_id' '$login' '$user_id'  'none' 'none' 'none' '$CFG::BASE/identity-$host_id'";

		if(system($cmd) != 0) { 
		print "<SCRIPT> alert(\"$main::D_COMMIT_ERROR: $cmd\") </SCRIPT>\n"; }
		else { print "<SCRIPT> alert(\"$main::D_COMMIT_OK\") </SCRIPT>";}

	}
}


1;

