#!/usr/bin/perl -w
#$Id: session.cgi,v 1.5 2018/08/03 11:01:46 sqko Exp $
use strict;
use warnings;
use Data::Dumper;
use Pod::WSDL;
use SOAP::Transport::HTTP;

my $server = SOAP::Transport::HTTP::CGI
    -> dispatch_to('Session')
    -> handle;

package Session;
use RFC::Header;
use RFC::Main;
use SemaforeFile;
use Sys::Hostname;
use vars qw(@ISA);
@ISA = qw(SOAP::Server::Parameters);

=begin WSDL
_DOC Returns the hostname of the server
_RETURN $string
=end WSDL

=cut

sub ping()
{
    my $class = shift;
    return SOAP::Data->new(name=>'result',value=>hostname(),type=>'string');
}


=begin WSDL
_DOC Returns the userid if the specified user is logged in (still has an active session)
_IN username $string Username
_RETURN $int
=end WSDL

=cut

sub is_logged_in($)
{
    my $class = shift;
    my $rid = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'is_logged_in',$rid);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'integer');
}


=begin WSDL
_DOC Logout the user
_IN username $string Username
_RETURN $string
=end WSDL

=cut

sub logout_user($)
{
    my $class = shift;
    my $name = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'logout_user',$name);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'string');
}


=begin WSDL
_DOC Returns the logout delay in seconds
_RETURN $int
=end WSDL

=cut

sub logout_delay()
{
    my $class = shift;
    our $delay = &RFC::Main::get_cv('global','logoutdelay');
    if ($delay && $delay =~ /(\d+)/)
    {
	$delay = $1 * 60;
    }
    else
    {
	$delay = 30 * 60;
    }
    $delay = 120 unless ($delay > 120);  ## Min 2 minutes! #
    return SOAP::Data->new(name=>'result',value=>$delay,type=>'integer');
}

=begin WSDL
_DOC Initiates a new session
_IN username $string Username
_IN passwd $string Password
_RETURN $string
=end WSDL

=cut

sub new_session($$)
{
    my $class = shift;
    my $name = shift;
    my $pass = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'new_session',$name,$pass);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'string');
}

=begin WSDL
_DOC Check if user has a valed session
_IN username $string Username
_IN session $string Session hash
_RETURN $int
=end WSDL

=cut

sub checkuser($$)
{
    my $class = shift;
    my $name = shift;
    my $pass = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'checkuser',$name,$pass);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'integer');
}


=begin WSDL
_DOC Check if user has a valed session on on one of the systems connected for single signon
_IN username $string Username
_IN session $string Session hash
_RETURN $int
=end WSDL

=cut

sub checkuser_sso($$)
{
    my $class = shift;
    my $name = shift;
    my $pass = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'checkuser_sso',$name,$pass);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'integer');
}


=begin WSDL
_DOC Returns a newline separated list of active users (usernames) if the specified user is an administrator
_IN username $string Username
_IN session $string Session hash
_RETURN $string
=end WSDL

=cut

sub get_users($$)
{
    my $class = shift;
    my $n = shift;
    my $p = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'get_users',$n,$p);
    return $res;
}



=begin WSDL
_DOC Checks if user exists, returns the userid if so and 0 otherwise
_IN username $string Username
_RETURN $int
=end WSDL

=cut

sub isuser($)
{
    my $class = shift;
    my $name = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'isuser',$name);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'integer');
}


=begin WSDL
_DOC Checks if the specified userid is an administrator 
Returns the userid if so, 0 otherwise
_IN username $string Username
_IN session $string Session hash
_RETURN $int
=end WSDL

=cut

sub isadmin($$)
{
    my $class = shift;
    my $uid = shift;
    my $pwd = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'isadmin',$uid,$pwd);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'integer');
}

=begin WSDL
_DOC Adds a new user to the system
_IN username $string Username
_IN name $string Real name
_IN email $string Email address
_RETURN $int
=end WSDL

=cut


sub adduser
{
    my $class = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'adduser',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'integer');
}


=begin WSDL
_DOC Adds a new user to the system and returns the new users password as no email will be sent
_IN username $string Username
_IN name $string Real name
_RETURN $string
=end WSDL

=cut


sub adduser_noemail
{
    my $class = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'adduser_noemail',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'string');
}



=begin WSDL
_DOC Removes a user from the system
_IN username $string Username
_IN session $string Session hash
_IN user $string Username to remove
_RETURN $int
=end WSDL

=cut


sub deleteuser
{
    my $class = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'deleteuser',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'integer');
}


=begin WSDL
_DOC Sets the admin bit for the specified username a value of 1 gives root access
_IN username $string Username
_IN session $string Session hash
_IN user $string Username to manipulate
_IN admin $int Admin bit
_RETURN $int
=end WSDL

=cut


sub root_access
{
    my $class = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'root_access',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'integer');
}


=begin WSDL
_DOC Set user access for specific username and section combination
_IN username $string Username
_IN session $string Session hash
_IN user $string Username to manipulate
_IN section $string section
_IN access $string Access level: r,c,m (read, create, modify)
_RETURN $int
=end WSDL

=cut


sub user_auth
{
    my $class = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'user_auth',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'integer');
}



=begin WSDL
_DOC Changes the users password
_IN username $string Username
_IN session $string Session hash
_IN password $string New password
_RETURN $int
=end WSDL

=cut


sub changepwd
{
    my $class = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'changepwd',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'integer');
}


=begin WSDL
_DOC Resets a users password
_IN username $string Username
_IN session $string Session hash
_IN user $string Username to reset
_RETURN $int
=end WSDL

=cut


sub resetpwd
{
    my $class = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'resetpwd',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'integer');
}

=begin WSDL
_DOC Checks if user has access rights equal or greater than the one specified for the section(s) specified
_IN username $string Username
_IN session $string Session hash
_IN section $string Section
_IN level $string Access level
_RETURN $int
=end WSDL

=cut


sub checkauth
{
    my $class = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'checkauth',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'integer');
}


=begin WSDL
_DOC Returns the email address for specified username
_IN username $string Username
_RETURN $string
=end WSDL

=cut

sub email($)
{
    my $class = shift;
    my $name = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'email',$name);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'string');
}

=begin WSDL
_DOC Get user access for specific username and section combination
_IN username $string Username
_IN session $string Session hash
_IN user $string Username to manipulate
_IN section $string Section
_IN level $string Minimum access nescesarry
_RETURN $int
=end WSDL

=cut


sub get_per
{
    my $class = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'get_per',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'integer');
}

=begin WSDL
_DOC Gets a users admin bit
_IN username $string Username
_IN session $string Session hash
_IN user $string Username to query
_RETURN $int
=end WSDL

=cut


sub get_admin
{
    my $class = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'get_admin',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'integer');
}

=begin WSDL
_DOC Check if user has a valid session and that the password does not need to be changed du to reset/expiration
_IN username $string Username
_IN session $string Session hash
_RETURN $string
=end WSDL

=cut

sub pwdstatus($$)
{
    my $class = shift;
    my $name = shift;
    my $pass = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'pwdstatus',$name,$pass);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'string');
}

=begin WSDL
_DOC Check if user has been authorised for the specified safetyid
_IN username $string Username
_IN safetyid $int Safetyid
_RETURN $int
=end WSDL

=cut

sub check_task_status($$)
{
    my $class = shift;
    my $name = shift;
    my $pass = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'check_task_status',$name,$pass);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'string');
}


=begin WSDL
_DOC Check if user has been certified for the specified safetyid
_IN username $string Username
_IN safetyid $int Safetyid
_RETURN $int
=end WSDL

=cut

sub check_task_cert($$)
{
    my $class = shift;
    my $name = shift;
    my $pass = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'check_task_cert',$name,$pass);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'string');
}

=begin WSDL
_DOC Adds a new safety authorisation, returns the id of the new safety authoirsation (task)
_IN name $string Name of new safety authorisation
_RETURN $int
=end WSDL

=cut

sub new_task($$)
{
    my $class = shift;
    my $name = shift;
    my $pass = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'new_task',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'string');
}

=begin WSDL
_DOC Change the name of a safety authorisation
_IN username $string Username
_IN session $string Session hash
_IN id $int Safetyid
_IN name $string New name for safety authorisation
_RETURN $string
=end WSDL

=cut

sub task_name($$)
{
    my $class = shift;
    my $name = shift;
    my $pass = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'task_name',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'string');
}

=begin WSDL
_DOC Change the name of a safety authorisation
_IN username $string Username
_IN session $string Session hash
_IN id $int Safetyid
_IN valid $boolean Status
_RETURN $boolean
=end WSDL

=cut

sub task_valid($$)
{
    my $class = shift;
    my $name = shift;
    my $pass = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'task_valid',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'boolean');
}


=begin WSDL
_DOC Returns a newline separated list of valid safety authorisations
_IN username $string Username
_IN session $string Session hash
_RETURN $string
=end WSDL

=cut

sub get_tasks($$)
{
    my $class = shift;
    my $name = shift;
    my $pass = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'get_tasks',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'string');
}

=begin WSDL
_DOC Returns a newline separated list of valid safety authorisations
_IN username $string Username
_IN session $string Session hash
_RETURN $string
=end WSDL

=cut

sub get_task_names($$)
{
    my $class = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'get_task_names',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'string');
}


=begin WSDL
_DOC Set user authorisation
_IN username $string Username
_IN session $string Session hash
_IN user $string Username to manipulate
_IN id $int safetyid
_RETURN $int
=end WSDL

=cut

sub set_task_auth($$)
{
    my $class = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'set_task_auth',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'string');
}

=begin WSDL
_DOC Set user certification
_IN username $string Username
_IN session $string Session hash
_IN user $string Username to manipulate
_IN id $int safetyid
_RETURN $int
=end WSDL

=cut

sub set_task_cert($$)
{
    my $class = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'set_task_cert',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'string');
}



=begin WSDL
_DOC Revoke user authorisation
_IN username $string Username
_IN session $string Session hash
_IN user $string Username to manipulate
_IN id $int Safetyid
_RETURN $int
=end WSDL

=cut

sub rev_task_auth($$)
{
    my $class = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'rev_task_auth',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'string');
}

=begin WSDL
_DOC Revoke user certification
_IN username $string Username
_IN session $string Session hash
_IN user $string Username to manipulate
_IN id $int Safetyid
_RETURN $int
=end WSDL

=cut

sub rev_task_cert($$)
{
    my $class = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'rev_task_cert',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'string');
}


=begin WSDL
_DOC Lists users authorised for the specified safetyid
_IN username $string Username
_IN session $string Session hash
_IN id $int Safetyid
_RETURN $string
=end WSDL

=cut

sub list_auth($$)
{
    my $class = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'list_auth',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'string');
}

=begin WSDL
_DOC Lists users certified for the specified safetyid
_IN username $string Username
_IN session $string Session hash
_IN id $int Safetyid
_RETURN $string
=end WSDL

=cut

sub list_cert($$)
{
    my $class = shift;
    my $remote  = get_cv('passwds','passwd_server');
    my $port    = get_cv('passwds','passwd_server_port');
    my $res = &RFC::Main::passwd_client_raw($remote,$port,'list_cert',@_);
    return SOAP::Data->new(name=>'result',value=>$res,type=>'string');
}




1;


