Skip to content

Commit

Permalink
Merge branch 'feat/move_vol2' of github.com:frankiejol/ravada into fe…
Browse files Browse the repository at this point in the history
…at/move_vol2
  • Loading branch information
frankiejol committed Oct 31, 2023
2 parents c91c2d4 + dd0b346 commit 69b0336
Show file tree
Hide file tree
Showing 37 changed files with 1,799 additions and 635 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ log
rvd_front.conf
pkg-debian-out
public/img/screenshots
public/js/custom
yarn.lock
node_modules/
t/vm/b10*
Expand Down
3 changes: 0 additions & 3 deletions etc/rvd_front.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,4 @@
,file => '/var/log/ravada/rvd_front.log'
,level => 'debug'
}
# Insert widget in /js/custom/insert_here_widget.js
# this widget embed js in templates/bootstrap/scripts.html.ep
,widget => ''
};
98 changes: 90 additions & 8 deletions lib/Ravada.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2543,6 +2543,60 @@ sub _sql_insert_defaults($self){
,name => 'auto_view'
,value => $conf->{auto_view}
}
,{ id_parent => $id_frontend
,name => "widget"
,value => $conf->{widget}
}
,{
id_parent => $id_frontend
,name => 'content_security_policy'
}
,{
id_parent => "/frontend/content_security_policy"
,name => "all"
,value => ''
}
,{
id_parent => "/frontend/content_security_policy"
,name => "default-src"
,value => ''
}
,{
id_parent => "/frontend/content_security_policy"
,name => "style-src"
,value => ''
}
,{
id_parent => "/frontend/content_security_policy"
,name => "script-src"
,value => ''
}
,{
id_parent => "/frontend/content_security_policy"
,name => "object-src"
,value => ''
}
,{
id_parent => "/frontend/content_security_policy"
,name => "frame-src"
,value => ''
}
,{
id_parent => "/frontend/content_security_policy"
,name => "font-src"
,value => ''
}

,{
id_parent => "/frontend/content_security_policy"
,name => "connect-src"
,value => ''
}
,{
id_parent => "/frontend/content_security_policy"
,name => "media-src"
,value => ''
}
,{
id_parent => $id_backend
,name => 'start_limit'
Expand Down Expand Up @@ -3255,6 +3309,7 @@ sub _req_add_disk($uid, $id_domain, $type, $size, $request, $storage=undef) {
,name => 'disk'
,data => $data
,@after_req
,at => time + 1
);
}
sub _start_domain_after_create($domain, $request, $uid,$previous_request) {
Expand Down Expand Up @@ -3809,7 +3864,7 @@ sub process_requests {
"pid=".($req->pid or '')." ".$req->status()
."$txt_retry "
.$req->command
." ".Dumper($req->args) if $DEBUG || $debug;
." ".Dumper($req->args) if ( $DEBUG || $debug ) && $req->command ne 'set_time';

my ($n_retry) = $req->status() =~ /retry (\d+)/;
$n_retry = 0 if !$n_retry;
Expand All @@ -3819,14 +3874,16 @@ sub process_requests {
next if !$DEBUG && !$debug;

warn ''.localtime." req ".$req->id." , cmd: ".$req->command." ".$req->status()
." , err: '".($req->error or '')."'\n" if $DEBUG || $VERBOSE;
." , err: '".($req->error or '')."'\n" if ($DEBUG || $VERBOSE )
&& $req->command ne 'set_time';
# sleep 1 if $DEBUG;

}

my @reqs2 = grep { $_->command ne 'set_time' } @reqs;
warn Dumper([map { $_->id." ".($_->pid or '')." ".$_->command." ".$_->status }
grep { $_->id } @reqs ])
if ($DEBUG || $debug ) && @reqs;
grep { $_->id } @reqs2 ])
if ($DEBUG || $debug ) && @reqs2;

return scalar(@reqs);
}
Expand Down Expand Up @@ -4470,7 +4527,7 @@ sub _wait_pids($self) {
$request->status('done') if $request->status =~ /working/i;
};
warn("$$ request id=$id_req ".$request->command." ".$request->status()
.", error='".($request->error or '')."'\n") if $DEBUG && $request;
.", error='".($request->error or '')."'\n") if $DEBUG && $request && $request->command ne 'set_time';
}
}

Expand Down Expand Up @@ -4844,14 +4901,38 @@ sub _cmd_prepare_base {
my $id_domain = $request->id_domain or confess "Missing request id_domain";
my $uid = $request->args('uid') or confess "Missing argument uid";

my $domain = $self->search_domain_by_id($id_domain);
die "Unknown domain id '$id_domain'\n" if !$domain;

my $user = Ravada::Auth::SQL->search_by_id( $uid)
or confess "Error: Unknown user id $uid in request ".Dumper($request);

my $with_cd = $request->defined_arg('with_cd');
die "User ".$user->name." [".$user->id."] not allowed to prepare base "
.$domain->name."\n"
unless $user->is_admin || (
$domain->id_owner == $user->id && $user->can_create_base());

my $domain = $self->search_domain_by_id($id_domain);

die "Unknown domain id '$id_domain'\n" if !$domain;
my $with_cd = $request->defined_arg('with_cd');

if ($domain->is_active) {
my $req_shutdown = Ravada::Request->shutdown_domain(
uid => $user->id
,id_domain => $domain->id
,timeout => 0
);
$request->after_request($req_shutdown->id);
$request->at(time + 10);
if ( !defined $request->retry() ) {
$request->retry(5);
$request->status("retry");
} elsif($request->retry>0) {
$request->retry($request->retry-1);
$request->status("retry");
}
$request->error("Machine must be shut down ".$domain->name." [".$domain->id."]");
return;
}

$self->_remove_unnecessary_request($domain);
$self->_remove_unnecessary_downs($domain);
Expand Down Expand Up @@ -6150,6 +6231,7 @@ sub _req_method {
,list_cpu_models => \&_cmd_list_cpu_models
,enforce_limits => \&_cmd_enforce_limits
,force_shutdown => \&_cmd_force_shutdown
,force_shutdown_domain => \&_cmd_force_shutdown
,force_reboot => \&_cmd_force_reboot
,shutdown_start => \&_cmd_shutdown_start
,rebase => \&_cmd_rebase
Expand Down
2 changes: 1 addition & 1 deletion lib/Ravada/Auth/SQL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ sub add_user {
." VALUES(?,?,?,?,?,?)");
};
confess $@ if $@;
if ($password) {
if ($password && !$external_auth) {
$password = sha1_hex($password);
} else {
$password = '*LK* no pss';
Expand Down
87 changes: 81 additions & 6 deletions lib/Ravada/Front.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use Hash::Util qw(lock_hash);
use IPC::Run3 qw(run3);
use JSON::XS;
use Moose;
use Storable qw(dclone);
use Ravada;
use Ravada::Auth::LDAP;
use Ravada::Front::Domain;
Expand Down Expand Up @@ -1203,26 +1204,28 @@ sub list_bases_anonymous {
my $net = Ravada::Network->new(address => $ip);

my $sth = $CONNECTOR->dbh->prepare(
"SELECT id, name, id_base, is_public, file_screenshot "
"SELECT id, alias, name, id_base, is_public, file_screenshot "
."FROM domains where is_base=1 "
."AND is_public=1");
$sth->execute();
my ($id, $name, $id_base, $is_public, $screenshot);
$sth->bind_columns(\($id, $name, $id_base, $is_public, $screenshot));
my ($id, $alias, $name, $id_base, $is_public, $screenshot);
$sth->bind_columns(\($id, $alias, $name, $id_base, $is_public, $screenshot));

my @bases;
while ( $sth->fetch) {
next if !$net->allowed_anonymous($id);
my %base = ( id => $id, name => Encode::decode_utf8($name)
, alias => Encode::decode_utf8($alias or $name)
, is_public => ($is_public or 0)
, screenshot => ($screenshot or '')
, is_active => 0
, id_clone => undef
, name_clone => undef
, is_locked => undef
, can_hibernate => 0

);
$base{screenshot} =~ s{^/var/www}{};
$base{list_clones} = [];
lock_hash(%base);
push @bases, (\%base);
}
Expand Down Expand Up @@ -1479,6 +1482,19 @@ sub _settings_by_id($self) {
return $orig_settings;
}

sub _settings_by_parent($self,$parent) {
my $data = $self->_setting_data($parent);
my $sth = $self->_dbh->prepare("SELECT name,value FROM settings "
." WHERE id_parent = ? ");
$sth->execute($data->{id});
my $ret;
while (my ($name, $value) = $sth->fetchrow) {
$value = '' if !defined $value;
$ret->{$name} = $value;
}
return $ret;
}

=head2 feature
Returns if a feature is available
Expand Down Expand Up @@ -1519,9 +1535,10 @@ sub update_settings_global($self, $arg, $user, $reload, $orig_settings = $self->
confess Dumper([$field,$arg->{$field}]) if !ref($arg->{$field});
if ( scalar(keys %{$arg->{$field}})>2 ) {
confess if !keys %{$arg->{$field}};
$self->update_settings_global($arg->{$field}, $user, $reload, $orig_settings);
my $field2 = dclone($arg->{$field});
$self->update_settings_global($field2, $user, $reload, $orig_settings);
}
confess "Error: invalid field $field" if $field !~ /^\w+$/;
confess "Error: invalid field $field" if $field !~ /^\w[\w\-]+$/;
my ( $value, $id )
= ($arg->{$field}->{value}
, $arg->{$field}->{id}
Expand Down Expand Up @@ -1692,6 +1709,64 @@ sub _filter_active($pools, $active) {

}

=head2 upload_users
Upload a list of users to the database
=head3 Arguments
=over
=item * string with users and passwords in each line
=item * type: it can be SQL, LDAP or SSO
=item * create: optionally create the entries in LDAP
=back
=cut

sub upload_users($self, $users, $type, $create=0) {

my @external;
if ($type ne 'sql') {
@external = ( is_external => 1, external_auth => $type );
}

my ($found,$count) = (0,0);
my @error;
for my $line (split /\n/,$users) {
my ($name, $password) = split(/:/,$line);
$found++;
my $user = Ravada::Auth::SQL->new(name => $name);
if ($user && $user->id) {
push @error,("User $name already added");
next;
}
if ($type ne 'sql' && $create) {
if ($type eq 'ldap') {
if (!$password) {
push @error,("Error: user $name , password empty");
next;
}
eval { $user = Ravada::Auth::LDAP::add_user($name,$password) };
push @error, ($@) if $@;
} else {
push @error,("$type users can't be created from Ravada");
}
}
if ($type eq 'sql' && !$password) {
push @error,("Error: user $name requires password");
next;
}
Ravada::Auth::SQL::add_user(name => $name, password => $password
,@external);
$count++;
}
return ($found, $count, \@error);
}

=head2 version
Returns the version of the main module
Expand Down
Loading

0 comments on commit 69b0336

Please sign in to comment.