#!/usr/bin/perl -w
#
# File: fetchlicensekey.pl
#
# Function: Retrieve a license key from Nexenta's license server for a
#			leopard system (Pantera LS) using the wget utility.
#
# Inputs: A valid BIOS UUID and system model number or maximum number of drives
# Outputs: The system UUID, Nexenta's Machine Signature and the License Key.
#          Unsuccessful attempts report an error message.
#

use Digest::MD5 qw(md5_hex);
use Getopt::Std;

use vars qw($opt_d $opt_h $opt_m $opt_u);

$usage = 'fetchlicensekey.pl -u UUID -m Model | -d [ 48 | 96 ] [ -h]';

$ARGV[0] ||  die "Usage: $usage\n";

sub _detailed_help {
	print "\n$usage\n";
	print "\nYou must supply the system's UUID and a Model number or ";
	print "UUID and the maximum\nnumber of drives (either 48 or 96) ";
	print "to license.\n";
	print "\n";
	print "-u UUID\t\tThe system's UUID (required)\n";
	print "-m Model\tWhere model is either 2130 (48 drives maximum) or\n";
	print "\t\t2150 (96 drives maximum)\n";
	print "-d size\t\tWhere size is either 48 or 96\n"; 
	print "-h\t\tPrint this message\n";
	exit;
}

getopts('d:hm:u:');

sub check_args {
	if ($opt_h) { _detailed_help(); }
	if ($opt_u) {
		my $uuidsz = length($opt_u);
		if ($uuidsz != 36) { die "$opt_u is not a valid UUID\n"; }
		if ( $opt_d ) {
			if ( $opt_d != 48 && $opt_d != 96 ) { 
				print "\nInvalid number of drives: $opt_d\n";
				_detailed_help();
			}
			$maxcap = $opt_d;
		}
		elsif ( $opt_m ) {
			if ( $opt_m != 2130 && $opt_m != 2150 ) { 
				print "\nInvalid model number: $opt_m\n";
				_detailed_help();
			}
			if ( $opt_m == 2130 ) { $maxcap = 48; }
			if ( $opt_m == 2150 ) { $maxcap = 96; }
		}
		else {
			print "\nPlease provide model number or number of drives for:\n";
			print "UUID: $opt_u\n";
			_detailed_help();
		}
	}
	else {
		 print "\nPlease provide system UUID\n";
		 _detailed_help();
	}
}

sub _exec_pipe {
	my ($cmd, $lines) = @_;
	local *FD;

	if (!open(FD, "$cmd 2>&1|")) {
		return 1;
	}
	@$lines = <FD>;
	close FD;
	return $?;
}

sub _uuid_to_sig {
	my $uuid = shift;
	$uuid = lc($uuid);
	my $UUID_LEN = 8;
	my $sig = '';
	my $idx = 1;

	$uuid = md5_hex($uuid);
	
	#Use the last (right most) 8 digits of the md5sum.
	$uuid = substr($uuid, length($uuid) - $UUID_LEN);

	#Convert the last 8 hex digits to ASCII alpha-numerics 
	for my $c (split(//, $uuid)) {
		my $code = ord($c) + $idx;
		$code += 7 if ($code >= 0x3a && $code < 0x41);
		$sig .= chr($code);
		$idx++;
	}
	return $sig;
}

sub get_machine_signature {
	chomp $opt_u;
	my $uuid = uc($opt_u);
	my $sig = _uuid_to_sig($uuid);
	
	print "UUID: $uuid\n";
	#Upper case the signature
	return uc($sig);
}

sub fetch_license {
	my $machid  = shift;
	my $oemname = "onstor";
	my $oempwd  = "0nst0r";
	my $magic   = "4A7890BE";
	my $months  = 12;
	my $pid     = $$;
	my $tmpfile  = "/tmp/keyget.$pid";
	my @lines = ();
	my $URL = "\"https://www.nexenta.com/oem.php?_magic=$magic&oemname=$oemname&oempwd=$oempwd&capacity=$maxcap&months=$months&machine_sig=$machid\"";
	my $getcmd = "wget -q --no-check-certificate -O $tmpfile -c " . "$URL";
	my $codeval = "";
	my $code    = "";
	my $key     = "";

	if (_exec_pipe("$getcmd", \@lines) != 0) {
		die "Unable to retrieve key from license server";
	}

	@lines = ();
	_exec_pipe("cat $tmpfile", \@lines);

	($code, $codeval, $key) = split /=/, $lines[0];
	$code = lc($code);
	$codeval =~ s/(\d)\D+/$1/;

	if ($codeval == 0) {
		print "License Key: $key\n";
	}
	else {
		print "License server returned failure $code: $codeval\n";
		print "Reason: $key\n";
	}		

	unlink $tmpfile;
}

check_args() ;

my $machid = get_machine_signature();

print "Machine Signature: $machid\n";

fetch_license($machid);
