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
|
#!/usr/bin/perl
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
$ENV{'PATH'}='/bin:/usr/bin:/sbin:/usr/sbin/';
$DELIMITER = ",";
$ISCSIADM = untaint(`which iscsiadm`);
# check binaries
if (!-x $ISCSIADM) {
print STDERR "Unable to find iscsiadm\n";
do_exit(1);
}
# check input params
$dev_string = untaint(shift @ARGV);
($euca_home, $ip, $store, $passwd) = parse_devstring($dev_string);
print get_device_name($store);
sub parse_devstring {
my ($dev_string) = @_;
return split($DELIMITER, $dev_string);
}
sub get_device_name {
my ($store) = @_;
if(!open GETSESSION, "iscsiadm -m session -P 3 |") {
print "Could not get iscsi session information";
do_exit(1)
}
$found_target = 0;
while (<GETSESSION>) {
if($_ =~ /Target: (.*)\n/) {
$found_target = 1 if $1 == $store;
} elsif($_ =~ /.*Attached scsi disk ([a-zA-Z0-9]+).*\n/) {
if($found_target == 1) {
return "/dev/", $1;
}
}
}
close GETSESSION;
}
sub do_exit() {
$e = shift;
if ($mounted && ($tmpfile ne "")) {
system("$mounter umount $tmpfile");
}
if ($attached && ($loopdev ne "")) {
system("$LOSETUP -d $loopdev");
}
if ($tmpfile ne "") {
system("$RMDIR $tmpfile");
}
exit($e);
}
sub untaint() {
$str = shift;
if ($str =~ /^([ &:#-\@\w.]+)$/) {
$str = $1; #data is now untainted
} else {
$str = "";
}
return($str);
}
|