summaryrefslogtreecommitdiffstats
path: root/INF/ILO2.pm
blob: 0e2375b584032c36bff1d011bc3290e26714a7ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#!/usr/bin/env perl 

IO::Socket::SSL::set_ctx_defaults(
    SSL_verify_mode => Net::SSLeay::VERIFY_NONE() );

package INF::ILO2;

use HTTP::Daemon::SSL;
use HTTP::Server::Brick;
use HTTP::Status;

use IO::Socket::SSL qw();
use HTML::TreeBuilder;
use HTTP::Request::Common;
use LWP::UserAgent;
use URI::Escape;
use File::Temp qw/ tempfile tempdir /;
use XML::Simple;
use Data::Dumper;
use JSON::PP;

use MIME::Base64 qw( encode_base64 );

sub b64($) {
    my $ret = shift;
    $ret = encode_base64($ret);
    $ret =~ s/[\n\s\r]//g;

    return $ret;
}

sub read_file($) {
    my ($name) = @_;

    my $fh = new IO::File "<" . $name;
    local $/;
    my $guts = $fh->getline;
    $fh->close;
    undef $fh;

    return $guts;
}

sub setup_port_proxy($$$$) {
    my ( $self, $local_port, $remote_host, $remote_port ) = @_;

    my $child = fork();

    my $cmd = [
        "balance", "-d", "-f", "-b", "127.0.0.1", $local_port,
        $remote_host . ":" . $remote_port
    ];

    print STDERR "PROXY CMD: " . join( ' ', @$cmd ) . "\n";

    if ( $child == 0 ) {

        if ( defined $self->{proxy_host} ) {
            my $tmp = File::Temp->new( UNLINK => 0, SUFFIX => '.cnf' );

            select( ( select($tmp), $| = 1 )[0] );

            print $tmp "server = 127.0.0.1\n";
            print $tmp "server_port = " . $self->{proxy_port} . "\n";
            print $tmp "local = 127.0.0.0/255.0.0.0\n";

            $ENV{'LD_PRELOAD'}       = 'libtsocks.so';
            $ENV{'TSOCKS_CONF_FILE'} = $tmp->filename;

            print "Filename is $tmp->filename\n";
        }

        exec(@$cmd);
        print STDERR "failed to start port proxy";
        sleep(10000);
    }

    sleep(4);

    print "Setup proxy $local_port -> $remote_host:$remote_port\n";

    return $child;
}

sub proxy($$$) {
    my ( $self, $req, $res ) = @_;

    if ( $req->uri->as_string =~ /^\/html\/java_irc.html/ ) {

        $res->header( 'Content-type' => 'text/html' );
        $res->add_content( $self->{java_html} );
        $res->code(200);

        return;

    }

    if ( $req->uri->as_string =~ /^\/html\/rc.*\.jar/ ) {

        $res->header( 'Content-type' => 'application/x-ms-application' );
        $res->add_content( read_file('/usr/local/share/inf/ilo/rc175p08.jar') );
        $res->code(200);

        return;
    }

    if ( $req->uri->as_string =~ /^\/html\/vtd.*\.jar/ ) {

        $res->header( 'Content-type' => 'application/x-ms-application' );
        $res->add_content(
            read_file('/usr/local/share/inf/ilo/vtd191p06.jar') );
        $res->code(200);

        return;
    }

    print "Another url..." . $req->uri->as_string . "\n";

    die "meh";

}

sub logout($) {
    my $self = shift;

    return unless defined $self->{stoken};

    my $get = GET( $self->{ilo_url} . '/logout.htm' );

    $get->header( 'cookie' => $self->{stoken} );

    my $res = $self->{ua}->request($get);

    unless ( $res->is_success ) {
        print STDERR "failed to logout";
        return -1;
    }

    print STDERR "Logged out of ILO2\n";

    $self->{stoken} = undef;
}

sub login($) {
    my $self = shift;

    $self->{stoken} = undef;
    $self->{skey}   = undef;
    $self->{sindex} = undef;

    my $get = GET( $self->{ilo_url} . '/login.htm' );

    my $res = $self->{ua}->request($get);

    unless ( $res->is_success ) {
        print STDERR "Get nonce failed - did not get 200\n";

        print Dumper($res);

        return -1;
    }

    my $content = $res->content;

    return -1 unless $content =~ /sessionkey="([A-Z0-9]+)"/;

    $self->{skey} = $1;

    #print "skey=" . $self->{skey} . "\n";

    return -1 unless $content =~ /sessionindex="([0-9]+)"/;

    $self->{sindex} = $1;

    #print "sindex=" . $self->{sindex} . "\n";

    $self->{slogin} =
        "hp-iLO-Login="
      . $self->{sindex} . ":"
      . b64( $self->{user} ) . ":"
      . b64( $self->{password} ) . ":"
      . $self->{skey};

    #print "Session login " . $self->{slogin} . "\n";

    $get = GET( $self->{ilo_url} . '/index.htm' );
    $get->header( 'cookie'  => $self->{slogin} );
    $get->header( 'Referer' => $self->{ilo_url} . '/login.htm' );

    $res = $self->{ua}->request($get);

    unless ( $res->is_success ) {
        print STDERR "Get nonce failed - did not get 200\n";

        print Dumper($res);

        return -1;
    }

    my $content = $res->content;

    unless ( $content =~ /ie_index.htm/ ) {
        print STDERR "Unhappy with reply from login\n";

        return -1;
    }

    $self->{stoken} =
        'hp-iLO-Session='
      . $self->{sindex} . ':::'
      . $self->{skey} . ';'
      . $self->{slogin};

    #print "ILO Session token " . $self->{stoken} . "\n";
    #
    print "ILO2 login successful\n";

    return 0;
}

sub view($) {

    my $self = shift;

    $self->login() unless defined $self->{stoken};

    my $get = GET( $self->{ilo_url} . '/drc2fram.htm?restart=0' );

    $get->header( 'cookie' => $self->{stoken} );

    my $res = $self->{ua}->request($get);

    unless ( $res->is_success ) {
        print STDERR "drc2fram frequest failed - did not get 200\n";
        return -1;
    }
    my $content = $res->content;

    unless ( $content =~ /com.hp.ilo2.remcons.remcons.class/ ) {
        print STDERR "returned html doesn't look right\n";
        print Dumper($content);
        return -1;
    }

    my $things = {};

    for my $thing (
        qw( consoleWidth consoleHeight info1 info2 info3 info4 info5 info6 infomp infoscp info7 info8 slot_number info0 irc_fullscreen rcseize_timeout infoa infob infoc infod infom infomm infok infosc infocc infoae infomu infoms infon infoo infoscp  infopriv)
      )
    {

        return -1 unless $content =~ /${thing}=([^;]*);/;

        $things->{$thing} = $1;
        $things->{$thing} =~ s/[\\"']//g;
    }

    my $local_port = int( rand(30000) ) + 30000;

    push @{ $self->{to_kill} },
      $self->setup_port_proxy( $local_port, $self->{host}, $things->{info6} );

    $things->{info6}         = $local_port;
    $things->{consoleWidth}  = 1024;
    $things->{consoleHeight} = 768;

    my $document =
'<APPLET ID="RC" CODE="com.hp.ilo2.remcons.remcons.class" ARCHIVE=rc175p08.jar WIDTH='
      . $things->{consoleWidth}
      . ' HEIGHT='
      . $things->{consoleHeight} . '>' . "\n";

    $document .= '<PARAM NAME=CABBASE VALUE=rc175p08.cab>' . "\n";
    $document .= '<PARAM NAME="INFO0" VALUE="' . $things->{info0} . '">' . "\n";
    $document .= '<PARAM NAME="INFO1" VALUE="' . $things->{info1} . '">' . "\n";
    $document .= '<PARAM NAME="INFO3" VALUE="' . $things->{info3} . '">' . "\n";
    $document .= '<PARAM NAME="INFO6" VALUE="' . $things->{info6} . '">' . "\n";
    $document .= '<PARAM NAME="INFO7" VALUE="' . $things->{info7} . '">' . "\n";
    $document .= '<PARAM NAME="INFO8" VALUE="' . $things->{info8} . '">' . "\n";
    $document .= '<PARAM NAME="INFOA" VALUE="' . $things->{infoa} . '">' . "\n";
    $document .= '<PARAM NAME="INFOB" VALUE="' . $things->{infob} . '">' . "\n";
    $document .= '<PARAM NAME="INFOC" VALUE="' . $things->{infoc} . '">' . "\n";
    $document .= '<PARAM NAME="INFOD" VALUE="' . $things->{infod} . '">' . "\n";
    $document .= '<PARAM NAME="INFOM" VALUE="' . $things->{infom} . '">' . "\n";
    $document .=
      '<PARAM NAME="INFOMM" VALUE="' . $things->{infomm} . '">' . "\n";
    $document .= '<PARAM NAME="INFON" VALUE="' . $things->{infon} . '">' . "\n";
    $document .= '<PARAM NAME="INFOO" VALUE="' . $things->{infoo} . '">' . "\n";
    $document .= '</APPLET>';

    $document = "<html><head></head><body>" . $document . "</body></html>";

    print Dumper($document);

    $self->{java_html} = $document;

    my $webserver_pid = fork();

    if ( $webserver_pid == 0 ) {
        $SIG{INT}  = sub { kill 'KILL', ( @{ $self->{to_kill} } ); die; };
        $SIG{TERM} = sub { kill 'KILL', ( @{ $self->{to_kill} } ); die; };

        $self->{server}->start;
        print STDERR "failed to web server";
        sleep(100000);
    }

    push @{ $self->{to_kill} }, $webserver_pid;

    $SIG{INT}  = sub { kill 'INT',  ( @{ $self->{to_kill} } ); die; };
    $SIG{TERM} = sub { kill 'TERM', ( @{ $self->{to_kill} } ); die; };

    system(
        "appletviewer",
        "-J-Djava.security.manager",
        "-J-Djava.security.policy=/usr/local/share/inf/ilo/mypolicy",
        "-J-Djavax.net.ssl.trustStore=/usr/local/share/inf/ilo/server.jks",
        $self->{proxy_url} . "/html/java_irc.html"
    );

    kill 'TERM', ( @{ $self->{to_kill} } );
}

sub media($) {
    my $self = shift;

    $self->login() unless defined $self->{stoken};

    my $get = GET( $self->{ilo_url} . '/vtd028.htm' );

    $get->header( 'cookie' => $self->{stoken} );

    my $res = $self->{ua}->request($get);

    unless ( $res->is_success ) {
        print STDERR "vtd frequest failed - did not get 200\n";
        return -1;
    }
    my $content = $res->content;

    unless ( $content =~ /com.hp.ilo2.virtdevs.virtdevs.class/ ) {
        print STDERR "returned html doesn't look right\n";
        print Dumper($content);
        return -1;
    }

    my $things = {};

    for my $thing (qw( info0v info1v usbcfg serverName dp)) {

        return -1 unless $content =~ /${thing}=([^;]*);/;

        $things->{$thing} = $1;
        $things->{$thing} =~ s/[\\"']//g;
    }

    my $local_port = int( rand(30000) ) + 30000;

    push @{ $self->{to_kill} },
      $self->setup_port_proxy( $local_port, $self->{host}, $things->{info1v} );

    $things->{info1v} = $local_port;

    my $document =
'<APPLET CODE="com.hp.ilo2.virtdevs.virtdevs.class" ARCHIVE=vtd191p06.jar WIDTH=500 HEIGHT=300>'
      . "\n";
    $document .= '<PARAM NAME=CABBASE VALUE=vtd191p06.cab>' . "\n";
    $document .=
      '<PARAM NAME="INFO0" VALUE="' . $things->{info0v} . '">' . "\n";
    $document .=
      '<PARAM NAME="INFO1" VALUE="' . $things->{info1v} . '">' . "\n";
    $document .=
      '<PARAM NAME="INFO2" VALUE="' . $things->{usbcfg} . '">' . "\n";
    $document .=
      '<PARAM NAME="INFO3" VALUE="' . $things->{serverName} . '">' . "\n";
    $document .= '<PARAM NAME="device" VALUE="' . $things->{dp} . '">' . "\n"
      if $things->{dp} != 0;
    $document .= '</APPLET>';

    $document = "<html><head></head><body>" . $document . "</body></html>";

    print Dumper($document);

    $self->{java_html} = $document;

    my $webserver_pid = fork();

    if ( $webserver_pid == 0 ) {
        $SIG{INT}  = sub { kill 'KILL', ( @{ $self->{to_kill} } ); die; };
        $SIG{TERM} = sub { kill 'KILL', ( @{ $self->{to_kill} } ); die; };

        $self->{server}->start;
        print STDERR "failed to web server";
        sleep(100000);
    }

    push @{ $self->{to_kill} }, $webserver_pid;

    $SIG{INT}  = sub { kill 'INT',  ( @{ $self->{to_kill} } ); die; };
    $SIG{TERM} = sub { kill 'TERM', ( @{ $self->{to_kill} } ); die; };

    system(
        #        "strace","-f","-o","t2","-s","8192",
        #	"/bin/appletviewer",
        "appletviewer",
        "-J-Djava.security.manager",
        "-J-Djava.security.policy=/usr/local/share/inf/ilo/myvtdpolicy",
        "-J-Djavax.net.ssl.trustStore=/usr/local/share/inf/ilo/server.jks",
        $self->{proxy_url} . "/html/java_irc.html"
    );

    kill 'TERM', ( @{ $self->{to_kill} } );
}

sub get_host_power($) {
    my ($self) = @_;

    $self->login() unless defined $self->{stoken};

    my $get = GET( $self->{ilo_url} . '/dvirtpwr.htm' );

    $get->header( 'cookie' => $self->{stoken} );

    my $res = $self->{ua}->request($get);

    unless ( $res->is_success ) {
        print STDERR " $what - did not get 200\n";
        return 0;
    }

    my $content = $res->content;

    return 0 unless $content =~ /serverPower="([A-Z0-9]+)"/;

    my $state = $1;

    $state = "OFF" if $state eq 'STANDBY';

    print "Server Power is $state\n";

    return $state;
}

sub set_host_power($$$) {
    my ( $self, $thing, $other_thing ) = @_;

    $self->login() unless defined $self->{stoken};

    my $get = GET( $self->{ilo_url} . '/dvirtpwr.htm' );
    $get->header( 'cookie' => $self->{stoken} );

    my $res = $self->{ua}->request($get);

    unless ( $res->is_success ) {
        print STDERR " $what - did not get 200\n";
        return 0;
    }

    #cold	Cold+Boot
    #warn	Reset
    #hold    Press+and+Hold
    #press   Momentary+Press

    my $content = $res->content;

    return -1 unless $content =~ /signature="([A-Z0-9]+)"/;

    $self->{signature} = $1;

    my $form_content =
      'signature=' . $self->{signature} . '&' . $thing . '=' . $other_thing;

    print "form_content=$form_content\n";

    my $post = POST( $self->{ilo_url} . '/virtpwrpress.cgi' );
    $post->header( 'Content-Type'   => 'application/x-www-form-urlencoded' );
    $post->header( 'Content-Length' => length($form_content) );
    $post->content($form_content);
    $post->header( 'cookie' => $self->{stoken} );

    my $res = $self->{ua}->request($post);

    unless ( $res->is_success ) {
        print STDERR " $what - did not get 200\n";
        return 0;
    }

    return 1;
}

sub cold_boot($) {
    my $self = shift;
    return $self->set_host_power( 'cold', 'Cold+Boot' );
}

sub reset($) {
    my $self = shift;
    return $self->set_host_power( 'warm', 'Reset' );
}

sub off($) {
    my $self = shift;
    if ( $self->get_host_power =~ /ON/i ) {
        return $self->set_host_power( 'hold', 'Press+and+Hold' );
    }
    return 1;
}

sub on($) {
    my $self = shift;
    if ( $self->get_host_power =~ /OFF/i ) {
        return $self->set_host_power( 'press', 'Momentary+Press' );
    }
    return 1;
}

sub port_on($$) {
    my $self = shift;
    return $self->on();
}

sub port_off($$) {
    my $self = shift;
    return $self->off();
}

sub port_cycle($$) {
    my $self = shift;
    return $self->cold_boot();
}

sub port_state_get_no_cache($$) {
    my $self = shift;
    return $self->get_host_power();
}

sub port_state_get($$) {
    my $self = shift;
    return $self->get_host_power();
}

sub port_name_get($$) {
    my $self = shift;
    return $self->{name};
}

sub name_get($) {
    my $self = shift;
    return $self->{name};
}

sub pdu_load_get($) {
    return "N/A";
}

sub psu_status($) {
    return "N/A";
}

sub port_count($) {
    return 1;
}

sub port_id_get_by_number($$) {
    return "K0";
}

sub new ($;$) {
    my ( $class, $parm ) = @_;
    my $self;

    $self->{ua} = my $ua = LWP::UserAgent->new;

    $self->{host} = $parm->{host} || "127.0.0.1";

    print "ILO2 instansiated for host " . $self->{host} . "\n";

    $self->{user}     = $parm->{user}     || "Administrator";
    $self->{password} = $parm->{password} || "";

    $self->{name} = $parm->{name} || $self->{host};

    $self->{ilo_url} = $parm->{ilo_url}
      || 'https://' . $self->{host};
    $self->{userid} = undef;

    $self->{ua}->ssl_opts(
        SSL_verify_mode => Net::SSLeay::VERIFY_NONE(),
        verify_hostname => 0,
    );

    if ( defined $parm->{proxy_host} ) {
        $self->{ua}->proxy( [qw(http https)] => "socks://"
              . $parm->{proxy_host} . ":"
              . $parm->{proxy_port} );
        $self->{proxy_host} = $parm->{proxy_host};
        $self->{proxy_port} = $parm->{proxy_port};
    }

    my $local_port = int( rand(30000) ) + 30000;

    $self->{proxy_url} = 'https://127.0.0.1:' . $local_port;

    $self->{server} = HTTP::Server::Brick->new(
        port         => $local_port,
        daemon_class => 'HTTP::Daemon::SSL',
        daemon_args  => [
            LocalAddr       => '127.0.0.1',
            SSL_key_file    => '/usr/local/share/inf/ilo/server.key',
            SSL_cert_file   => '/usr/local/share/inf/ilo/server.crt',
            SSL_verify_mode => Net::SSLeay::VERIFY_NONE(),
        ],
    );
    $self->{server}->mount(
        '/' => {
            handler => sub {
                my ( $req, $res ) = @_;

                $self->proxy( $req, $res );
                1;
            },
            wildcard => 1,
        }
    );

    $self->{stoken}  = undef;
    $self->{to_kill} = [];

    return bless $self, $class;

}

1;