rvd_front.pl 67.7 KB
Newer Older
1
2
3
#!/usr/bin/env perl
use warnings;
use strict;
4
5
6
#####
use locale ':not_characters';
#####
7
use Carp qw(confess);
8
use Data::Dumper;
9
use Digest::SHA qw(sha256_hex);
10
use Hash::Util qw(lock_hash);
11
use Mojolicious::Lite 'Ravada::I18N';
Francesc Guasch's avatar
Francesc Guasch committed
12
use Mojo::JSON qw(decode_json encode_json);
13
use Time::Piece;
14
#use Mojolicious::Plugin::I18N;
15
use Mojo::Home;
Francesc Guasch's avatar
Francesc Guasch committed
16
17

use I18N::LangTags::Detect;
18
#####
19
20
#my $self->plugin('I18N');
#package Ravada::I18N:en;
21
#####
22
23
24
#
no warnings "experimental::signatures";
use feature qw(signatures);
25

26
27
use lib 'lib';

28
use Ravada::Front;
29
use Ravada::Front::Domain;
Francesc Guasch's avatar
Francesc Guasch committed
30
use Ravada::Auth;
Francesc Guasch's avatar
Francesc Guasch committed
31
use Ravada::WebSocket;
32
use POSIX qw(locale_h);
Francesc Guasch's avatar
Francesc Guasch committed
33
34

my $help;
Francesc Guasch's avatar
Francesc Guasch committed
35

Francesc Guasch's avatar
Francesc Guasch committed
36
37
38
my $FILE_CONFIG = "/etc/rvd_front.conf";

my $error_file_duplicated = 0;
39
for my $file ( "/etc/rvd_front.conf" , ($ENV{HOME} or '')."/rvd_front.conf") {
Francesc Guasch's avatar
Francesc Guasch committed
40
    warn "WARNING: Found config file at $file and at $FILE_CONFIG\n"
41
        if -e $file && $FILE_CONFIG && $file ne $FILE_CONFIG;
42
    $FILE_CONFIG = $file if -e $file;
Francesc Guasch's avatar
Francesc Guasch committed
43
    $error_file_duplicated++;
44
}
Francesc Guasch's avatar
Francesc Guasch committed
45
warn "WARNING: using $FILE_CONFIG\n"    if$error_file_duplicated>2;
46
47

my $FILE_CONFIG_RAVADA;
48
for my $file ( "/etc/ravada.conf" , ($ENV{HOME} or '')."/ravada.conf") {
49
50
51
    warn "WARNING: Found config file at $file and at $FILE_CONFIG_RAVADA\n"
        if -e $file && $FILE_CONFIG_RAVADA;
    $FILE_CONFIG_RAVADA = $file if -e $file;
Francesc Guasch's avatar
Francesc Guasch committed
52
}
53

amesones's avatar
amesones committed
54
55
56
my $CONFIG_FRONT = plugin Config => { default => {
                                                hypnotoad => {
                                                pid_file => 'log/rvd_front.pid'
fv3rdugo's avatar
Indent    
fv3rdugo committed
57
58
                                                ,listen => ['http://*:8081']
                                                }
59
                                              ,login_bg_file => '/img/intro-bg.jpg'
Francesc Guasch's avatar
Francesc Guasch committed
60
                                              ,login_header => 'Welcome'
61
                                              ,login_message => ''
62
                                              ,secrets => ['changeme0']
63
                                              ,guide => 0
fv3rdugo's avatar
fv3rdugo committed
64
                                              ,login_custom => ''
65
                                              ,footer => 'bootstrap/footer'
66
                                              ,monitoring => 0
67
                                              ,fallback => 0
68
                                              ,guide_custom => ''
69
70
                                              ,admin => {
                                                    hide_clones => 15
Francesc Guasch's avatar
Francesc Guasch committed
71
                                                    ,autostart => 0
amesones's avatar
amesones committed
72
                                              }
73
                                              ,config => $FILE_CONFIG_RAVADA
74
                                              ,auto_view => 0
amesones's avatar
amesones committed
75
                                              }
Francesc Guasch's avatar
Francesc Guasch committed
76
                                      ,file => $FILE_CONFIG
77
};
78

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#####
#####
#####
# Import locale-handling tool set from POSIX module.
# This example uses: setlocale -- the function call
#                    LC_CTYPE -- explained below

# query and save the old locale
my $old_locale = setlocale(LC_CTYPE);

setlocale(LC_CTYPE, "en_US.ISO8859-1");
# LC_CTYPE now in locale "English, US, codeset ISO 8859-1"

setlocale(LC_CTYPE, "");
# LC_CTYPE now reset to default defined by LC_ALL/LC_CTYPE/LANG
# environment variables.  See below for documentation.

# restore the old locale
setlocale(LC_CTYPE, $old_locale);
#####
#####
#####
101
plugin I18N => {namespace => 'Ravada::I18N', default => 'en'};
Francesc Guasch's avatar
Francesc Guasch committed
102
plugin 'RenderFile';
Francesc Guasch's avatar
Francesc Guasch committed
103

104
my %config;
105
%config = (config => $CONFIG_FRONT->{config}) if $CONFIG_FRONT->{config};
106
our $RAVADA = Ravada::Front->new(%config);
107

108
our $USER;
Francesc Guasch's avatar
Francesc Guasch committed
109

110
# TODO: get those from the config file
111
our $DOCUMENT_ROOT = "/var/www";
112
113

# session times out in 5 minutes
114
our $SESSION_TIMEOUT = ($CONFIG_FRONT->{session_timeout} or 5 * 60);
115
# session times out in 15 minutes for admin users
116
our $SESSION_TIMEOUT_ADMIN = ($CONFIG_FRONT->{session_timeout_admin} or 15 * 60);
117

Francesc Guasch's avatar
Francesc Guasch committed
118
my $WS = Ravada::WebSocket->new(ravada => $RAVADA);
Francesc Guasch's avatar
Francesc Guasch committed
119
120
init();
############################################################################3
121

122
123
124
hook before_routes => sub {
  my $c = shift;

125
126
  $USER = undef;

Francesc Guasch's avatar
Francesc Guasch committed
127
  $c->stash(version => $RAVADA->version);
128
  my $url = $c->req->url->to_abs->path;
fv3rdugo's avatar
fv3rdugo committed
129
  my $host = $c->req->url->to_abs->host;
130
  $c->stash(css=>['/css/sb-admin.css']
Francesc Guasch's avatar
Francesc Guasch committed
131
132
            ,js=>[
                '/js/ravada.js'
133
134
135
                ]
            ,csssnippets => []
            ,navbar_custom => 0
Francesc Guasch's avatar
Francesc Guasch committed
136
137
138
139
            ,url => undef
            ,_logged_in => undef
            ,_anonymous => undef
            ,_user => undef
140
            ,footer=> $CONFIG_FRONT->{footer}
141
            ,monitoring => 0
fv3rdugo's avatar
fv3rdugo committed
142
            ,fallback => $CONFIG_FRONT->{fallback}
143
            ,check_netdata => 0
144
            ,guide => $CONFIG_FRONT->{guide}
fv3rdugo's avatar
fv3rdugo committed
145
            ,host => $host
146
            );
147

Francesc Guasch's avatar
Francesc Guasch committed
148
149
    return if _logged_in($c);
    return if $url =~ m{^/(anonymous|login|logout|requirements|robots.txt)}
Francesc Guasch's avatar
Francesc Guasch committed
150
151
           || $url =~ m{^/(css|font|img|js)}
	   || $url =~ m{^/fallback/.*\.(css|js|map)$};
152

Francesc Guasch's avatar
Francesc Guasch committed
153
154
155
156
157
158
159
160
161
    # anonymous URLs
    if (($url =~ m{^/machine/(clone|display|info|view)/}
        || $url =~ m{^/(list_bases_anonymous|request/)}i
        ) && !_logged_in($c)) {
        $USER = _anonymous_user($c);
        return if $USER->is_temporary;
    }
    return access_denied($c)
        if $url =~ /(screenshot|\.json)/
162
        && !_logged_in($c);
Francesc Guasch's avatar
Francesc Guasch committed
163
    return login($c) if !_logged_in($c);
164

165
166
167
168
169
170
    if ($USER && $USER->is_admin && $CONFIG_FRONT->{monitoring}) {
        if (!defined $c->session('monitoring')) {
            $c->stash(check_netdata => "https://$host:19999/index.html");
        }
        $c->stash( monitoring => 1) if $c->session('monitoring');
    }
171
        $c->stash( fallback => 1) if $c->session('fallback');
172
173
174
175
176
};


############################################################################3

Francesc Guasch's avatar
Francesc Guasch committed
177
178
179
180
181
any '/robots.txt' => sub {
    my $c = shift;
    return $c->render(text => "User-agent: *\nDisallow: /\n", format => 'text');
};

182
any '/' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
183
    my $c = shift;
184
    return quick_start($c);
Francesc Guasch's avatar
Francesc Guasch committed
185
186
};

Francesc Guasch's avatar
Francesc Guasch committed
187
188
any '/index.html' => sub {
    my $c = shift;
189
    return quick_start($c);
Francesc Guasch's avatar
Francesc Guasch committed
190
191
};

192
193
any '/login' => sub {
    my $c = shift;
194
    return login($c);
195
196
};

Francesc Guasch's avatar
Francesc Guasch committed
197
198
199
200
201
202
203
204
205
any '/test' => sub {
    my $c = shift;
    my $logged = _logged_in($c);
    my $count = $c->session('count');
    $c->session(count => ++$count);

    my $name_mojo = $c->signed_cookie('mojolicious');

    my $dump_log = ''.(Dumper($logged) or '');
Francesc Guasch's avatar
Francesc Guasch committed
206
207
208
209
210
    return $c->render(text => "$count ".($name_mojo or '')."<br> ".$dump_log
        ."<br>"
        ."<script>alert(window.screen.availWidth"
        ."+\" \"+window.screen.availHeight)</script>"
    );
Francesc Guasch's avatar
Francesc Guasch committed
211
212
};

Francesc Guasch's avatar
Francesc Guasch committed
213
214
any '/logout' => sub {
    my $c = shift;
215
    logout($c);
Francesc Guasch's avatar
Francesc Guasch committed
216
217
218
    $c->redirect_to('/');
};

219
get '/anonymous' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
220
    my $c = shift;
221
#    $c->render(template => 'bases', base => list_bases());
222
    $USER = _anonymous_user($c);
223
    return list_bases_anonymous($c);
Francesc Guasch's avatar
Francesc Guasch committed
224
225
};

226
get '/anonymous_logout.html' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
227
    my $c = shift;
228
229
    $c->session('anonymous_user' => '');
    return $c->redirect_to('/');
Francesc Guasch's avatar
Francesc Guasch committed
230
231
};

232
get '/anonymous/(#base_id).html' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
233
234
    my $c = shift;

235
    $c->stash(_anonymous => 1 , _logged_in => 0);
236
    _init_error($c);
237
    my $base_id = $c->stash('base_id');
238
239
    my $base = $RAVADA->search_domain_by_id($base_id);

240
    $USER = _anonymous_user($c);
241
    return quick_start_domain($c,$base->id, $USER->name);
Francesc Guasch's avatar
Francesc Guasch committed
242
243
};

244
any '/admin/#type' => sub {
245
  my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
246

247
248
249
  return admin($c)  if $c->stash('type') eq 'machines'
                        && $USER->is_operator;

250
251
  return access_denied($c)    if !$USER->is_operator;

amesones's avatar
amesones committed
252
  return admin($c);
Francesc Guasch's avatar
Francesc Guasch committed
253
254
};

255
any '/new_machine' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
256
    my $c = shift;
257
    return access_denied($c)    if !$USER->can_create_machine;
258
    return new_machine($c);
Francesc Guasch's avatar
Francesc Guasch committed
259
260
};

261
262
263
264
265
any '/copy_machine' => sub {
    my $c = shift;
    return new_machine_copy($c);
};

266
get '/domain/new.html' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
267
268
    my $c = shift;

269
270
    return access_denied($c) if !_logged_in($c) || !$USER->is_admin();
    $c->stash(error => []);
amesones's avatar
amesones committed
271
    return $c->render(template => "main/new_machine");
Francesc Guasch's avatar
Francesc Guasch committed
272
273
274

};

275
276
get '/list_vm_types.json' => sub {
    my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
277
    $c->render(json => $RAVADA->list_vm_types);
278
279
};

280
281
282
283
284
285
286
287
288
289
290
291
292
293
get '/list_nodes.json' => sub {
    my $c = shift;
    $c->render(json => [$RAVADA->list_vms]);
};

get '/node/enable/(:id).json' => sub {
    my $c = shift;
    return access_denied($c) if !$USER->is_admin;
    return $c->render(json => {enabled => $RAVADA->enable_node($c->stash('id'),1)});
};

get '/node/disable/(:id).json' => sub {
    my $c = shift;
    return access_denied($c) if !$USER->is_admin;
294
295
296
297
298

    my $machines = $RAVADA->_list_machines_vm($c->stash('id'));
    for ( @$machines ) {
        my $req = Ravada::Request->shutdown_domain( uid => $USER->id , id_domain => $_->{id} );
    }
299
300
    return $c->render(json => {enabled => $RAVADA->enable_node($c->stash('id'),0)});
};
301
302
303
304
305
get '/node/remove/(:id).json' => sub {
    my $c = shift;
    return access_denied($c) if !$USER->is_admin;
    return $c->render(json => {remove => $RAVADA->remove_node($c->stash('id'),1)});
};
306

Francesc Guasch's avatar
Francesc Guasch committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
get '/node/shutdown/(:id).json' => sub {
    my $c = shift;
    return access_denied($c) if !$USER->is_admin;

    my $machines = $RAVADA->_list_machines_vm($c->stash('id'));
    for ( @$machines ) {
        my $req = Ravada::Request->shutdown_domain(
                    uid => $USER->id
            , id_domain => $_->{id}
        );
    }
    my $at = 0;
    if (@$machines) {
        $at = time + 60 + scalar @$machines;
    }
    my $req = Ravada::Request->shutdown_node(
                id_node => $c->stash('id')
                ,at => $at
    );
    Ravada::Request->connect_node(
                id_node => $c->stash('id')
                ,at => $at + 10
    );
    return $c->render(json => {id_req => $req->id });
};

get '/node/start/(:id).json' => sub {
    my $c = shift;
    return access_denied($c) if !$USER->is_admin;
    my $req = Ravada::Request->start_node(
                id_node => $c->stash('id')
    );
    for my $seconds ( 30,60,90,120 ) {
        Ravada::Request->connect_node(
            id_node => $c->stash('id')
            ,at => time + $seconds
        );
    }

    return $c->render(json => {id_req => $req->id });

};

350
351
352
353
354
355
any '/new_node' => sub {
    my $c = shift;
    return access_denied($c)    if !$USER->is_admin;
    return new_node($c);
};

Francesc Guasch's avatar
Francesc Guasch committed
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
get '/node/connect/(#backend)/(#hostname)' => sub {
    my $c = shift;
    return access_denied($c)    if !$USER->is_admin;
    my $req = Ravada::Request->connect_node(
                backend => $c->stash('backend')
                    ,hostname => $c->stash('hostname')
    );
    return $c->render(json => {id_req => $req->id });
};

get '/node/connect/(#id)' => sub {
    my $c = shift;
    return access_denied($c)    if !$USER->is_admin;
    my $req = Ravada::Request->connect_node(
        id_node => $c->stash('id')
    );
    return $c->render(json => {id_req => $req->id });
};


376
377
get '/list_bases.json' => sub {
    my $c = shift;
378
379
380
381
382
383
384
385
386
387
388

    my $domains = $RAVADA->list_bases();
    my @domains_show = @$domains;
    if (!$USER->is_admin) {
        @domains_show = ();
        for (@$domains) {
            push @domains_show,($_) if $_->{is_public};
        }
    }
    $c->render(json => [@domains_show]);

389
390
391
392
};

get '/list_images.json' => sub {
    my $c = shift;
393

elodin1's avatar
elodin1 committed
394
395
396
    return access_denied($c) unless _logged_in($c)
        && $USER->can_create_machine();

397
398
399
    my $vm_name = $c->param('backend');

    $c->render(json => $RAVADA->list_iso_images($vm_name or undef));
400
401
};

402
get '/iso_file.json' => sub {
joelalju's avatar
joelalju committed
403
    my $c = shift;
elodin1's avatar
elodin1 committed
404
405
    return access_denied($c) unless _logged_in($c)
        && $USER->can_create_machine();
406
    my @isos =('<NONE>');
Fernando Verdugo's avatar
Fernando Verdugo committed
407
    push @isos,(@{$RAVADA->iso_file('KVM')});
408
    $c->render(json => \@isos);
joelalju's avatar
joelalju committed
409
410
};

411
get '/list_machines.json' => sub {
412
    my $c = shift;
413

414
    return access_denied($c) unless _logged_in($c)
415
416
417
418
419
        && (
            $USER->can_list_machines
            || $USER->can_list_own_machines()
            || $USER->can_list_clones()
            || $USER->can_list_clones_from_own_base()
420
421
422
            || $USER->is_admin()
        );

423
    return $c->render( json => $RAVADA->list_machines($USER) );
424

425
};
Francesc Guasch's avatar
Francesc Guasch committed
426

427
428
429
get '/list_machines_user.json' => sub {
    my $c = shift;
    return $c->render( json => $RAVADA->list_machines_user($USER));
430
};
Francesc Guasch's avatar
Francesc Guasch committed
431

432
433
434
435
436
437
438
get '/list_bases_anonymous.json' => sub {
    my $c = shift;

    # shouldn't this be "list_bases" ?
    $c->render(json => $RAVADA->list_bases_anonymous(_remote_ip($c)));
};

Francesc Guasch's avatar
Francesc Guasch committed
439
get '/list_lxc_templates.json' => sub {
fv3rdugo's avatar
fv3rdugo committed
440
    my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
441
    $c->render(json => $RAVADA->list_lxc_templates);
fv3rdugo's avatar
fv3rdugo committed
442
443
};

joansp's avatar
udpate    
joansp committed
444
445
446
447
448
get '/pingbackend.json' => sub {

    my $c = shift;
    $c->render(json => $RAVADA->ping_backend);
};
fv3rdugo's avatar
fv3rdugo committed
449

450
451
# machine commands

452
get '/machine/info/(:id).(:type)' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
453
    my $c = shift;
454
    my $id = $c->stash('id');
Francesc Guasch's avatar
Francesc Guasch committed
455
    die "No id " if !$id;
456

joelalju's avatar
joelalju committed
457
458
459
    my ($domain) = _search_requested_machine($c);
    return access_denied($c)    if !$domain;

460
    return access_denied($c,"Access denied to user ".$USER->name) unless $USER->can_manage_machine($domain->id);
Francesc Guasch's avatar
Francesc Guasch committed
461

462
463
    my $info = $domain->info($USER);
    if ($domain->is_active && !$info->{ip}) {
Francesc Guasch's avatar
Francesc Guasch committed
464
        Ravada::Request->refresh_machine(id_domain => $domain->id, uid => $USER->id);
465
466
    }
    return $c->render(json => $info);
Francesc Guasch's avatar
Francesc Guasch committed
467
468
};

Francesc Guasch's avatar
Francesc Guasch committed
469
get '/machine/requests/(:id).json' => sub {
470
    my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
471
    my $id_domain = $c->stash('id');
472
    return access_denied($c) unless $USER->can_manage_machine($id_domain);
joelalju's avatar
joelalju committed
473

Francesc Guasch's avatar
Francesc Guasch committed
474
475
    $c->render(json => $RAVADA->list_requests($id_domain,10));
};
joelalju's avatar
joelalju committed
476

477
any '/machine/manage/(:id).(:type)' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
478
   	 my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
479
     Ravada::Request->refresh_machine(id_domain => $c->stash('id'), uid => $USER->id);
Francesc Guasch's avatar
Francesc Guasch committed
480
     return manage_machine($c);
481
};
joelalju's avatar
joelalju committed
482

Francesc Guasch's avatar
Francesc Guasch committed
483
484
485
any '/hardware/(:id).(:type)' => sub {
   	 my $c = shift;
     return $c->render(template => 'main/hardware');
486
487
};

488
get '/machine/view/(:id).(:type)' => sub {
489
    my $c = shift;
490
491
    my $id = $c->stash('id');
    my $type = $c->stash('type');
Francesc Guasch's avatar
Francesc Guasch committed
492

joelalju's avatar
joelalju committed
493
494
495
496
497
498
    my ($domain) = _search_requested_machine($c);
    return access_denied($c)    if !$domain;

    return access_denied($c) unless $USER->is_admin
                              || $domain->id_owner == $USER->id;

499
500
501
    return view_machine($c);
};

Francesc Guasch's avatar
Francesc Guasch committed
502
any '/machine/clone/(:id).(:type)' => sub {
Joel Alarcón's avatar
Joel Alarcón committed
503
    my $c = shift;
504

Francesc Guasch's avatar
Francesc Guasch committed
505
506
507
508
509
510
511
512
513
514
515
    return clone_machine($c)    if $USER && $USER->can_clone() && !$USER->is_temporary();

    my $bases_anonymous = $RAVADA->list_bases_anonymous(_remote_ip($c));
    for (@$bases_anonymous) {
        if ($_->{id} == $c->stash('id') ) {
            return clone_machine($c,1);
        }
    }

    return login($c)    if !$USER || $USER->is_temporary;
    return access_denied($c);
516
};
Francesc Guasch's avatar
Francesc Guasch committed
517

518
get '/machine/shutdown/(:id).(:type)' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
519
        my $c = shift;
520
    return access_denied($c)        if !$USER ->can_shutdown($c->stash('id'));
Francesc Guasch's avatar
Francesc Guasch committed
521
522
523
524

        return shutdown_machine($c);
};

525
any '/machine/remove/(:id).(:type)' => sub {
joansp's avatar
joansp committed
526
        my $c = shift;
527
    return access_denied($c)       if !$USER->can_remove_machine($c->stash('id'));
joansp's avatar
joansp committed
528
529
        return remove_machine($c);
};
530
531

any '/machine/remove_clones/(:id).(:type)' => sub {
532
533
534
535
536
537
    my $c = shift;

    # TODO : call to $domain->_allow_remove();
	return access_denied($c)
        unless
            $USER -> can_remove_clone_all()
Francesc Guasch's avatar
Francesc Guasch committed
538
539
	        || $USER->can_remove_clone()
            || $USER->can_remove_all();
540
    return remove_clones($c);
541
542
};

543
get '/machine/prepare/(:id).(:type)' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
544
545
546
547
        my $c = shift;
        return prepare_machine($c);
};

Francesc Guasch's avatar
Francesc Guasch committed
548
get '/machine/set_base_vm/(:id_vm)/(:id_domain).(:type)' => sub {
549
    my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
550
551
552
553
554
555
    return set_base_vm($c, 1);
};

get '/machine/remove_base_vm/(:id_vm)/(:id_domain).(:type)' => sub {
    my $c = shift;
    return set_base_vm($c, 0);
556
557
};

558
get '/machine/remove_b/(:id).(:type)' => sub {
joansp's avatar
joansp committed
559
560
561
562
        my $c = shift;
        return remove_base($c);
};

563
get '/machine/remove_base/(:id).(:type)' => sub {
564
565
566
567
    my $c = shift;
    return remove_base($c);
};

568
get '/machine/screenshot/(:id).(:type)' => sub {
569
        my $c = shift;
570
571
        my $domain = _search_requested_machine($c);
        return access_denied($c)   if (!$USER->can_screenshot() || $domain->is_base());
572
573
574
        return screenshot_machine($c);
};

575
get '/machine/copy_screenshot/(:id).(:type)' => sub {
576
577
578
        my $c = shift; 
        my $domain = _search_requested_machine($c);
        return access_denied($c) if (!$USER->is_admin() || $domain->id_base() == NULL );
579
580
581
        return copy_screenshot($c);
};

582
get '/machine/pause/(:id).(:type)' => sub {
joansp's avatar
joansp committed
583
584
585
586
        my $c = shift;
        return pause_machine($c);
};

587
get '/machine/hibernate/(:id).(:type)' => sub {
588
        my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
589
          return access_denied($c)
Francesc Guasch's avatar
Francesc Guasch committed
590
             unless $USER->is_admin() || $USER->can_shutdown($c->stash('id'));
Francesc Guasch's avatar
Francesc Guasch committed
591

592
593
594
        return hybernate_machine($c);
};

595
get '/machine/resume/(:id).(:type)' => sub {
joansp's avatar
joansp committed
596
597
598
599
        my $c = shift;
        return resume_machine($c);
};

600
get '/machine/start/(:id).(:type)' => sub {
joansp's avatar
joansp committed
601
602
603
        my $c = shift;
        return start_machine($c);
};
joansp's avatar
joansp committed
604

605
get '/machine/exists/#name' => sub {
606
    my $c = shift;
607
    my $name = $c->stash('name');
608
609
610
611
612
613
    #TODO
    # return failure if it can't find the name in the URL

    return $c->render(json => $RAVADA->domain_exists($name));

};
joansp's avatar
joansp committed
614

Francesc Guasch's avatar
Francesc Guasch committed
615
post '/machine/hardware/change' => sub {
Francesc Guasch's avatar
Francesc Guasch committed
616
    my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
617
618
619
620
621
622
623
624
625
626
627
628
    my $arg = decode_json($c->req->body);

    my $domain = Ravada::Front::Domain->open(delete $arg->{id_domain});

    return access_denied($c)
        unless $USER->id == $domain->id_owner || $USER->is_admin;

    my $hardware = delete $arg->{hardware} or die "Missing hardware name";
    my $index = delete $arg->{index};
    my $data = delete $arg->{data};

    die "Unknown fields in request ".Dumper($arg) if keys %{$arg};
Francesc Guasch's avatar
Francesc Guasch committed
629
    return $c->render(json => { req => Ravada::Request->change_hardware(
Francesc Guasch's avatar
Francesc Guasch committed
630
631
632
633
                id_domain => $domain->id
                ,hardware => $hardware
                ,index => $index
                ,data => $data
Francesc Guasch's avatar
Francesc Guasch committed
634
635
636
637
638
                ,uid => $USER->id
            )
    });
};

639
640
641
642
643
644
645
get '/node/exists/#name' => sub {
    my $c = shift;
    my $name = $c->stash('name');

    return $c->render(json => $RAVADA->node_exists($name));

};
646
get '/machine/rename/#id/#value' => sub {
647
    my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
648
    return access_denied($c)       if !$USER->can_manage_machine($c->stash('id'));
649
650
651
    return rename_machine($c);
};

652
653
any '/machine/copy' => sub {
    my $c = shift;
654
    return access_denied($c)    if !$USER -> can_clone_all();
655
656
657
    return copy_machine($c);
};

658
659
660
661
662
663
get '/machine/public/#id' => sub {
    my $c = shift;
    return machine_is_public($c);
};

get '/machine/public/#id/#value' => sub {
664
665
666
667
    my $c = shift;
    return machine_is_public($c);
};

Francesc Guasch's avatar
Francesc Guasch committed
668
669
670
671
672
673
674
675
676
get '/machine/set/#id/#field/#value' => sub {
    my $c = shift;
    my $id = $c->stash('id');
    my $field = $c->stash('field');
    my $value = $c->stash('value');

    return access_denied($c)       if !$USER->can_manage_machine($c->stash('id'));

    my $domain = Ravada::Front::Domain->open($id) or die "Unkown domain $id";
Francesc Guasch's avatar
Francesc Guasch committed
677
678
    $USER->send_message("Setting $field to $value in ".$domain->name)
        if $domain->_data($field) ne $value;
Francesc Guasch's avatar
Francesc Guasch committed
679
680
681
    return $c->render(json => { $field => $domain->_data($field, $value)});
};

682
683
684
685
686
687
688
689
690
691
get '/machine/autostart/#id/#value' => sub {
    my $c = shift;
    my $req = Ravada::Request->domain_autostart(
        id_domain => $c->stash('id')
           ,value => $c->stash('value')
             ,uid => $USER->id
    );
    return $c->render(json => { request => $req->id});
};

692
get '/machine/display/(:id).vv' => sub {
693
694
695
696
697
698
699
700
701
702
703
704
    my $c = shift;

    my $id = $c->stash('id');

    my $domain = $RAVADA->search_domain_by_id($id);
    return $c->render(text => "unknown machine id=$id") if !$id;

    return access_denied($c)
        if $USER->id ne $domain->id_owner
        && !$USER->is_admin;

    $c->res->headers->content_type('application/x-virt-viewer');
705
706
        $c->res->headers->content_disposition(
        "inline;filename=".$domain->id.".vv");
707

708
    return $c->render(data => $domain->display_file($USER), format => 'vv');
709
710
};

Francesc Guasch's avatar
Francesc Guasch committed
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
get '/machine/display-tls/(:id)-tls.vv' => sub {
    my $c = shift;

    my $id = $c->stash('id');

    my $domain = $RAVADA->search_domain_by_id($id);
    return $c->render(text => "unknown machine id=$id") if !$id;

    return access_denied($c)
        if $USER->id ne $domain->id_owner
        && !$USER->is_admin;

    $c->res->headers->content_type('application/x-virt-viewer');
        $c->res->headers->content_disposition(
        "inline;filename=".$domain->id."-tls.vv");

    return $c->render(data => $domain->display_file_tls($USER), format => 'vv');
};

Francesc Guasch's avatar
Francesc Guasch committed
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
# Network ##########################################################3

get '/network/interfaces/(:vm_type)/(:type)' => sub {
    my $c = shift;

    my $vm_type = $c->stash('vm_type');
    my $type = $c->stash('type');

    return $c->render( json => $RAVADA->list_network_interfaces(
               user => $USER
              ,type => $type
           ,vm_type => $vm_type
       )
    );
};
Francesc Guasch's avatar
Francesc Guasch committed
745

746
747
# Users ##########################################################3

Laura Figuerola's avatar
Laura Figuerola committed
748
749
##add user

750
any '/users/register' => sub {
751

Laura Figuerola's avatar
Laura Figuerola committed
752
       my $c = shift;
753
       return access_denied($c) if !$USER->is_admin();
Laura Figuerola's avatar
Laura Figuerola committed
754
755
756
       return register($c);
};

Francesc Guasch's avatar
Francesc Guasch committed
757
758
any '/admin/user/(:id).(:type)' => sub {
    my $c = shift;
Francesc Guasch's avatar
Francesc Guasch committed
759
    return access_denied($c) if !$USER->can_manage_users() && !$USER->can_grant();
Francesc Guasch's avatar
Francesc Guasch committed
760
761
762
763
764
765

    my $user = Ravada::Auth::SQL->search_by_id($c->stash('id'));

    return $c->render(text => "Unknown user id: ".$c->stash('id'))
        if !$user;

Francesc Guasch's avatar
Francesc Guasch committed
766
767
768
769
770
    if ($c->param('make_admin')) {
        $USER->make_admin($c->stash('id'))  if $c->param('is_admin');
        $USER->remove_admin($c->stash('id'))if !$c->param('is_admin');
        $user = Ravada::Auth::SQL->search_by_id($c->stash('id'));
    }
771
772
773
774
775
776
777
778
779
780
781
    if ($c->param('grant')) {
        return access_denied($c)    if !$USER->can_grant();
        my %grant;
        for my $param_name (@{$c->req->params->names}) {
            if ( $param_name =~ /^perm_(.*)/ ) {
                $grant{$1} = 1;
            } elsif ($param_name =~ /^off_perm_(.*)/) {
                $grant{$1} = 0 if !exists $grant{$1};
            }
        }
        for my $perm (keys %grant) {
782
            if ( $grant{$perm} ) {
783
784
785
786
787
788
                $USER->grant($user, $perm);
            } else {
                $USER->revoke($user, $perm);
            }
        }
    }
Francesc Guasch's avatar
Francesc Guasch committed
789
790
791
    $c->stash(user => $user);
    return $c->render(template => 'main/manage_user');
};
Laura Figuerola's avatar
Laura Figuerola committed
792

793
794
795
796
797
798
799
800
801
802
803
804
805
806
get '/list_ldap_attributes/(#cn)' => sub {
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my $cn = $c->stash('cn');
    my $user;
    eval {
        ($user) = Ravada::Auth::LDAP::search_user($cn);
    };
    return $c->render(json => { error => $@ }) if $@;
    return $c->render(json => []) if !$user;

    $c->session(ldap_attributes_cn => $cn) if $user;
807
    return $c->render(json => {attributes => [sort $user->attributes]});
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
};

get '/count_ldap_entries/(#attribute)/(#value)' => sub {
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my @entries;
    eval {
        @entries = Ravada::Auth::LDAP::search_user(
            field => $c->stash('attribute')
            ,name => $c->stash('value')
            ,typesonly => 1
        );
    };
    @entries = [ 'too many' ] if $@ =~ /Sizelimit exceeded/;
    return $c->render(json => { entries => scalar @entries });
};

827
get '/add_ldap_access/(#id_domain)/(#attribute)/(#value)/(#allowed)/(#last)' => sub {
828
829
830
831
832
833
834
835
836
837
838
839
840
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my $domain_id = $c->stash('id_domain');
    my $domain = Ravada::Front::Domain->open($domain_id);

    my $attribute = $c->stash('attribute');
    my $value = $c->stash('value');
    my $allowed = 1;
    if ($c->stash('allowed') eq 'false') {
        $allowed = 0;
    }
841
842
843
844
845
    my $last = 1;
    if ($c->stash('last') eq 'false') {
        $last = 0;
    }
    $last = 1 if !$allowed;
846

847
    eval { $domain->allow_ldap_access($attribute => $value, $allowed, $last ) };
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
    _fix_default_ldap_access($c, $domain, $allowed) if !$@;
    return $c->render(json => { error => $@ }) if $@;
    return $c->render(json => { ok => 1 });

};

sub _fix_default_ldap_access($c, $domain, $allowed) {
    my @list = $domain->list_ldap_access();
    my $default_found;
    for ( @list ) {
        if ( $_->{value} eq '*' ) {
            $default_found = $_->{id};
        }
    }
    if ( $default_found ) {
        $domain->move_ldap_access($default_found, +1);
        return;
    }
    my $allowed_default = 0;
    $allowed_default = 1 if !$allowed;
    eval { $domain->allow_ldap_access('DEFAULT' => '*', $allowed_default ) };
    warn $@ if $@;
}

get '/delete_ldap_access/(#id_domain)/(#id_access)' => sub {
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my $domain_id = $c->stash('id_domain');
    my $domain = Ravada::Front::Domain->open($domain_id);

    $domain->delete_ldap_access($c->stash('id_access'));

882
883
884
885
886
887
    # delete default if it is the only one left
    my @ldap_access = $domain->list_ldap_access();
    if (scalar @ldap_access == 1 && $ldap_access[0]->{value} eq '*') {
        $domain->delete_ldap_access($ldap_access[0]->{id});
    }

888
889
890
891
892
893
894
895
896
897
898
899
900
    return $c->render(json => { ok => 1 });
};

get '/list_ldap_access/(#id_domain)' => sub {
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my $domain_id = $c->stash('id_domain');
    my $domain = Ravada::Front::Domain->open($domain_id);

    my @ldap_access = $domain->list_ldap_access();
    my $default = {};
901
    if (scalar @ldap_access && $ldap_access[-1]->{value} eq '*') {
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
        $default = pop @ldap_access;
    }
    return $c->render(json => {list => \@ldap_access, default => $default} );
};

get '/move_ldap_access/(#id_domain)/(#id_access)/(#count)' => sub {
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my $domain_id = $c->stash('id_domain');
    my $domain = Ravada::Front::Domain->open($domain_id);

    $domain->move_ldap_access($c->stash('id_access'), $c->stash('count'));

    return $c->render(json => { ok => 1});
};

920
get '/set_ldap_access/(#id_domain)/(#id_access)/(#allowed)/(#last)' => sub {
921
922
923
924
925
926
927
928
929
930
931
932
933
    my $c = shift;

    return _access_denied($c) if !$USER->is_admin;

    my $domain_id = $c->stash('id_domain');
    my $domain = Ravada::Front::Domain->open($domain_id);

    my $allowed = $c->stash('allowed');
    if ($allowed =~ /false/ || !$allowed) {
        $allowed = 0;
    } else {
        $allowed = 1;
    }
934
935
936
937
938
939
940
    my $last= $c->stash('last');
    if ($last=~ /false/ || !$last) {
        $last= 0;
    } else {
        $last= 1;
    }
    warn "last = $last";
941

942
    $domain->set_ldap_access($c->stash('id_access'), $allowed, $last);
943
944
    return $c->render(json => { ok => 1});
};
945
##############################################
Laura Figuerola's avatar
Laura Figuerola committed
946

Francesc Guasch's avatar
Francesc Guasch committed
947
948
949
950
post '/request/(:name)/' => sub {
    my $c = shift;

    my $args = decode_json($c->req->body);
951
952
    confess "Error: uid should not be provided".Dumper($args)
        if exists $args->{uid};
Francesc Guasch's avatar
Francesc Guasch committed
953
954
955
956
957
958
959
960

    my $req = Ravada::Request->new_request(
        $c->stash('name')
        ,uid => $USER->id
        ,%$args
    );
    return $c->render(json => { ok => 1 });
};
961

962
get '/request/(:id).(:type)' => sub {
963
    my $c = shift;
964
    my $id = $c->stash('id');
965

Francesc Guasch's avatar
Francesc Guasch committed
966
967
968
    if (!$USER) {
        $USER = _get_anonymous_user($c) or access_denied($c);
    }
969
970
971
972
    if ($c->stash('type') eq 'json') {
        my $request = Ravada::Request->open($id);
        return $c->render(json => $request->info($USER));
    }
973
974
975
    return _show_request($c,$id);
};

976
977
978
979
980
981
982
983
984
get '/anonymous/request/(:id).(:type)' => sub {
    my $c = shift;
    my $id = $c->stash('id');

    $USER = _anonymous_user($c);

    return _show_request($c,$id);
};

Francesc Guasch's avatar
Francesc Guasch committed
985
986
get '/requests.json' => sub {
    my $c = shift;
elodin1's avatar
elodin1 committed
987
988
    return access_denied($c) unless _logged_in($c)
        && $USER->is_admin;
Francesc Guasch's avatar
Francesc Guasch committed
989
990
991
    return list_requests($c);
};

joansp's avatar
joansp committed
992
get '/messages.json' => sub {
joansp's avatar
joansp committed
993
994
995
996
997
998
    my $c = shift;


    return $c->render( json => [$USER->messages()] );
};

999
get '/unshown_messages.json' => sub {
1000
    my $c = shift;
For faster browsing, not all history is shown. View entire blame