# # Generated by SM2Gen version:0.9(20Jan2025) Chameleon version:4.5.4 On Python:3.12.3 at 2025-05-15 12:40:04 # # # Routines to be edited by the developer to provide content and validation for parameters # and provison of the control data for table(s) # use esmith::util; use esmith::util::network; use esmith::ConfigDB; use esmith::HostsDB; use esmith::AccountsDB; use esmith::NetworksDB; use esmith::DomainsDB; use constant FALSE => 0; use constant TRUE => 1; #The most common ones my $cdb; my $adb; #my $ndb; #my $hdb; #my $ddb; # Validation routines - parameters for each panel sub validate_USERTABLE { my $c = shift; my $rua_data = shift; #Data hash as parameter # Validation for each field my $ret = ""; if ($ret eq "") {$ret = 'ok';} return $ret; } sub validate_PARAMS { my $c = shift; my $rua_data = shift; #Data hash as parameter # Validation for each field my $ret = ""; #if (! TRUE) #validate $c->param('Account') #{$ret .= 'Validation for Account failed';} #if (! TRUE) #validate $c->param('User_Name') #{$ret .= 'Validation for User_Name failed';} #if (! TRUE) #validate $c->param('Shell') #{$ret .= 'Validation for Shell failed';} #if (! TRUE) #validate $c->param('Sudoer') #{$ret .= 'Validation for Sudoer failed';} #if (! TRUE) #validate $c->param('VPNClientAccess') #{$ret .= 'Validation for VPNClientAccess failed';} #if (! TRUE) #validate $c->param('ChrootDir2') #{$ret .= 'Validation for ChrootDir2 failed';} #if (! TRUE) #validate $c->param('ChrootDir') #{$ret .= 'Validation for ChrootDir failed';} #if (! TRUE) #validate $c->param('sshKeys') #{$ret .= 'Validation for sshKeys failed';} #just check chroot in place $ret = $c->CheckChrootDirExists(); if ($ret eq "") {$ret = 'ok';} return $ret; } # Get singleton data for each panel sub get_data_for_panel_USERTABLE { # Return a hash with the fields required which will be loaded into the shared data my $c = shift; my %ret = ( 'Data1'=>'Data for USERTABLE', #Example # fields from Inputs in USERTABLE $fields['USERTABLE'] ); return %ret; } sub get_data_for_panel_PARAMS { # Return a hash with the fields required which will be loaded into the shared data my $c = shift; my $user = $c->param('Selected'); $adb = esmith::AccountsDB->open(); #die("$user"); $userrec = $adb->get($user) || return ('Account' => "$user not found"); my %ret = ( # fields from Inputs in PARAMS $fields['PARAMS'] 'Account'=>$user, 'User_Name'=>$c->get_full_name($user), 'Shell'=>$userrec->prop('Shell') || 'no', 'Sudoer'=>$userrec->prop('Sudoer') || 'no', 'VPNClientAccess'=>$userrec->prop('VPNClientAccess') || 'no', 'ChrootDir2'=>$userrec->prop('ChrootDir') || '', 'ChrootDir'=>$userrec->prop('ChrootDir') || '', 'sshKeys'=>$c->get_ssh_keys($user), ); return %ret; } # Get control data for table(s) # Define a constant hash for field name mapping use constant getAllUsers_FIELD_MAPPING => ( 'Account' => 'User', 'User_Name' => 'User_Name', 'Shell_Access' => 'shell', 'Sudo' => 'Sudoer', 'SSH_Keys' => 'SSH_Keys', 'Chroot_Path' => 'Chroot_Path', 'RSSH_+_VPN_Access' => 'VPNClientAccess', 'Modify' => 'Modify' #'target_field2' => 'source_field2', # Add more mappings as needed ); sub actual_getAllUsers { my $c = shift; # Actual code for extracting getAllUsers $cdb = esmith::ConfigDB->open(); $adb = esmith::AccountsDB->open(); my @data = (); my @users = $adb->users; return $c->l("ACCOUNT_USER_NONE") if (@users == 0); for my $user (@users) { my $username = $user->key; # make clearer by only showing yes and localise my $vpn = $user->prop('VPNClientAccess') || ''; if ($vpn eq 'yes') { $vpn = 'YES'; } else { $vpn = ''; } my $sudo = $user->prop('Sudoer') || ''; if ($sudo eq 'yes') { $sudo = 'YES'; } else { $sudo = ''; } my $keys = ''; my $file = "/home/e-smith/files/users/$username/.ssh/authorized_keys2"; if (( -e $file ) && (! -z $file )) { $keys = 'YES'; } my $shell = $user->prop('Shell') || ''; if ($shell eq '/usr/bin/rssh') { $shell = ''; } my $ChrootDir = $user->prop('ChrootDir') || ""; $ChrootDir =~ s:/home/e-smith/files/ibays/::; $ChrootDir =~ s:/home/e-smith/files/users/$username/home:home:; $acc = $user->key; push @data, { User => $user->key, User_Name => $user->prop('FirstName') . " " .$user->prop('LastName'), Sudoer => $user->prop('Sudoer') || 'no', VPNClientAccess => $user->prop('VPNClientAccess') || 'no', shell => $shell, Chroot_Path => $ChrootDir, ChrootDir => $user->prop('ChrootDir') || "/home/e-smith/files/users/$username/home", sudo => $c->l($sudo), SSH_Keys => $c->l($keys), vpn => $c->l($vpn), Modify => "".$c->l('MODIFY')."", } } return @data; } sub get_getAllUsers { # Return an array of hashes of the contents for each row and column for getAllUsers my $c = shift; my @source_records = $c->actual_getAllUsers(); my @transformed_records; my %Field_Mapping = getAllUsers_FIELD_MAPPING(); # Iterate over each record in the source array for my $source_record (@source_records) { my %transformed_record; # Iterate over each key-value pair in the $Field_Mapping constant while (my ($target, $source) = each %Field_Mapping) { # Check if the source field exists in the source record if (exists $source_record->{$source}) { # Assign the source field value to the target field in the transformed record $transformed_record{$target} = $source_record->{$source}; } } # Add transformed record to the array if it's not empty push @transformed_records, \%transformed_record if %transformed_record; } return \@transformed_records; } # Return hash with values from row in which link clicked on table sub get_selected_USERTABLE { my $c = shift; my $selected = shift; #Parameter is name of selected row. my $is_new_record = shift; #Indicates new record required (defaults) my %ret = {}; return %ret; } sub get_selected_PARAMS { my $c = shift; my $selected = shift; #Parameter is name of selected row. my $is_new_record = shift; #Indicates new record required (defaults) my %ret = {}; return %ret; } #after sucessful modify or create or whatever and submit then perfom (if the params validate) sub perform_USERTABLE { my $c = shift; my $rua_data = shift; #Data hash as parameter my $ret = ""; my $db = $cdb; #maybe one of the others my $dbkey = 'ChangeThis'; # To make it write to DB as comment, delete this (regex) string in each if statement "TRUE\) \#copy or perform with value: .* e.g." if ($ret eq "") {$ret = 'ok';} return $ret; } sub perform_PARAMS { my $c = shift; #my $rua_data = shift; #Data hash as parameter #my $ret = ""; #my $db = $cdb; #maybe one of the others #my $dbkey = 'ChangeThis'; ## To make it write to DB as comment, delete this (regex) string in each if statement "TRUE\) \#copy or perform with value: .* e.g." #if (! TRUE) #copy or perform with value: Account e.g. $db->set_prop($dbkey,'Account',$c->param('Account'),type=>'service')) #{$ret .= 'Perform/save failed for Account';} #if (! TRUE) #copy or perform with value: User_Name e.g. $db->set_prop($dbkey,'User_Name',$c->param('User_Name'),type=>'service')) #{$ret .= 'Perform/save failed for User_Name';} #if (! TRUE) #copy or perform with value: Shell e.g. $db->set_prop($dbkey,'Shell',$c->param('Shell'),type=>'service')) #{$ret .= 'Perform/save failed for Shell';} #if (! TRUE) #copy or perform with value: Sudoer e.g. $db->set_prop($dbkey,'Sudoer',$c->param('Sudoer'),type=>'service')) #{$ret .= 'Perform/save failed for Sudoer';} #if (! TRUE) #copy or perform with value: VPNClientAccess e.g. $db->set_prop($dbkey,'VPNClientAccess',$c->param('VPNClientAccess'),type=>'service')) #{$ret .= 'Perform/save failed for VPNClientAccess';} #if (! TRUE) #copy or perform with value: ChrootDir2 e.g. $db->set_prop($dbkey,'ChrootDir2',$c->param('ChrootDir2'),type=>'service')) #{$ret .= 'Perform/save failed for ChrootDir2';} #if (! TRUE) #copy or perform with value: ChrootDir e.g. $db->set_prop($dbkey,'ChrootDir',$c->param('ChrootDir'),type=>'service')) #{$ret .= 'Perform/save failed for ChrootDir';} #if (! TRUE) #copy or perform with value: sshKeys e.g. $db->set_prop($dbkey,'sshKeys',$c->param('sshKeys'),type=>'service')) #{$ret .= 'Perform/save failed for sshKeys';} $ret = $c->userpanel_change_settings(); if ($ret eq "") {$ret = 'ok';} return $ret; } sub create_link{ # WIP my ($c,$route, $panel, $index) = @_; my $link = "$route?trt=$panel&Selected=$index"; return $link; } sub get_full_name { my $c = shift; my $user = shift; return $adb->get_prop($user, "FirstName") . " " . $adb->get_prop($user, "LastName"); } sub get_ssh_keys{ my $c = shift; my $user = shift; my $file = "/home/e-smith/files/users/$user/.ssh/authorized_keys2"; if ($user eq 'admin') { $file = "/root/.ssh/authorized_keys2"; } my $sshKeys = ''; # if exists and is not empty if (( -e $file ) && (! -z $file )) { open (SSHKEY, "<$file") or die "Error: Could not open file: $file\n"; my @sshTemp = ; $sshKeys = join ("", @sshTemp); close SSHKEY; } return $sshKeys; } sub get_chroot_dirs { my $self = shift; my @options = ( ['', ''], ['home', '. ~/home'], ['/home/e-smith/files', '.. /home/e-smith/files'] ); foreach my $account ($adb->get_all) { if ($account->prop('type') =~ /ibay/) { my $key = $account->key; push @options, ["/home/e-smith/files/ibays/$key", $key], ["/home/e-smith/files/ibays/$key/html", "$key/html"], ["/home/e-smith/files/ibays/$key/files", "$key/files"]; } } return \@options; } sub userpanel_change_settings { my $c = shift; my $Shell = $c->param ('Shell'); my $Sudoer = $c->param ('Sudoer'); my $VPNClientAccess = $c->param ('VPNClientAccess'); my $ChrootDir = $c->param ('ChrootDir'); my $ChrootDir2 = $c->param ('ChrootDir2'); $adb = esmith::AccountsDB->open(); my $user = $c->param('Selected'); if ($user =~ /^([a-z][\-a-z0-9]*)$/) { $user = $1; } my $sshKeys = $c->param ('ssh_keys'); if ($sshKeys ne '') { my $file = "/home/e-smith/files/users/$user/.ssh/authorized_keys2"; my $dir = "/home/e-smith/files/users/$user/.ssh"; if ($user eq 'admin') { $file = "/root/.ssh/authorized_keys2"; $dir = "/root/.ssh"; } # delete .ssh/authorized_keys2 if ($sshKeys =~ /deletekeys/) { system ("/bin/rm -rf $file") == 0 or die ("Error deleting $file.\n"); } else { # Strip out DOS Carriage Returns (CR) $sshKeys =~ s/\r//g; unless ( -e $file ) { system ("/bin/mkdir -p $dir") == 0 or die ("Error creating ssh directory.\n"); } unlink $file; open (SSHKEY, ">$file") or die ("Error saving SSH Keys.\n"); print SSHKEY "$sshKeys\n"; close SSHKEY; system ("/bin/chown -R $user.$user $dir") == 0 or die ("Error chown .ssh directory.\n"); } } # if the drop down is blank and field is valid # set the field to the drop down, save the drop down if (($ChrootDir2 eq '' ) && ($ChrootDir ne '')) { $ChrootDir2 = $ChrootDir || ''; } if (($Sudoer eq 'yes') or ($Shell eq '/bin/bash')) { $adb->set_prop($user, 'Shell', '/bin/bash'); } else { $adb->set_prop($user, 'Shell', '/usr/bin/rssh'); } $adb->set_prop($user, 'Sudoer', $Sudoer); $adb->set_prop($user, 'ChrootDir', $ChrootDir2); $adb->set_prop($user, 'VPNClientAccess', $VPNClientAccess); system ("/sbin/e-smith/signal-event", "user-modify", $user) == 0 or die ("Error occurred updating user access\n"); return 'ok'; #$self->success("SUCCESS"); } sub CheckChrootDirExists { my $c = shift; my $ChrootDir = $c->param ('ChrootDir') || ''; my $ChrootDir2 = $c->param ('ChrootDir2') || ''; if ($ChrootDir2 eq '') { if ($ChrootDir eq '') { return "CHROOT_PATH_NOT_GIVEN"; } else { if ((-e $ChrootDir ) || ($ChrootDir eq 'home')) { return "OK"; } else { return "CHROOT_PATH_NON_EXISTANT"; } } } else { return "ok"; } } 1;