Skip to content

Instantly share code, notes, and snippets.

@goerz
Created August 1, 2010 19:26
Show Gist options
  • Save goerz/503669 to your computer and use it in GitHub Desktop.
Save goerz/503669 to your computer and use it in GitHub Desktop.
mountstick.pl
#!/usr/bin/perl -w
use strict;
my @devicelist = </dev/sdb?>; # order is important: first found device ist default
my %mountpaths = ( qr"/dev/sd??" => '/mnt' ); # default paths
my $UID = 'goerz'; # the user id the device is mounted as
my $GID = 'users'; # the group id the device is mounted as
my $FMASK = '000'; # the permissions of the mounted device (masked)
my $UMOUNT = '/home/goerz/bin/umountstick'; # the name of the script that should be written for umounting
my $TRFILE = '/home/goerz/.usbmounts'; # file used for tracking which devices are mounted
my $TCFILE = 'Documents/safe.tc'; # a truecrypt file to look for on the device
my $TCPATH = 'Documents/safe'; # the folder on the device where to mount the truecrypt file
#############################################################################################################
if (`whoami` !~ /root/){
print "check\n";
die "You must be root to execute this script\n";
}
my $defaultdevice = "";
$defaultdevice = $devicelist[0] if (@devicelist > 0);
my $defaultmount = "";
foreach my $key (keys(%mountpaths)){
if ($defaultdevice =~ $key){
$defaultmount = $mountpaths{$key}
}
}
print "Found devices: ", join(", ", @devicelist), "\n\n" if @devicelist>0;
print "Enter device name of USB device: ";
print "[$defaultdevice]" unless ($defaultdevice eq "");
my $device = <STDIN>;
chomp $device;
$device = $defaultdevice if ($device eq "");
print "Enter mount path: ";
print "[$defaultmount]" unless ($defaultmount eq "");
my $path = <STDIN>;
chomp $path;
$path = $defaultmount if ($path eq "");
my @umountbuffer; # array of text lines: we need random access
my %mounteddevices; # hash of mounted devices
my $load_mounteddevices_code = '
sub load_mounteddevices{
if (-f "'.$TRFILE.'"){
open(FILE, "'.$TRFILE.'") or die ("Couldn\'t open '.$TRFILE.'");
my $VAR1;
my @code = <FILE>;
close(FILE);
eval join("",@code); die $@ if $@;
die ("Error while reading from '.$TRFILE.'") if not defined($VAR1);
foreach my $key (keys(%{$VAR1})){
$mounteddevices{$key} = $VAR1->{$key} if (-e $key);
}
}
}
';
eval($load_mounteddevices_code); die $@ if $@;
my $write_mounteddevices_code = '
sub write_mounteddevices{
open(FILE, ">'.$TRFILE.'") or die ("Couldn\'t open '.$TRFILE.'");
use Data::Dumper;
print FILE Dumper \%mounteddevices;
close FILE
}
';
eval($write_mounteddevices_code); die $@ if $@;
eval('load_mounteddevices();'); die $@ if $@;
# mount the stick
if (system("mount -o uid=$UID,gid=$GID,fmask=$FMASK $device $path") == 0){
my $umountfunction = "u".time;
push @umountbuffer, " print \"Unmounting $path...\\n\";\n";
push @umountbuffer, " if (system(\"umount -l $path\") == 0){\n";
push @umountbuffer, " print \"Successful\\n\";\n";
push @umountbuffer, " } else {\n";
push @umountbuffer, " warn \"umount of $path NOT Successful\\n\";\n";
push @umountbuffer, " }\n";
push @umountbuffer, "}\n\n";
print "Device unmounted\n";
# now, try the safe
if ($TCFILE ne ""){
$path =~ s"/$"";
my $tcpath = ($TCPATH =~ m'^/')? $TCPATH : "$path/$TCPATH";
if ((-f "$path/$TCFILE") and (-d $tcpath)){
print "Safe found...\n";
if (system("truecrypt -M uid=$UID,gid=$GID,umask=$FMASK $path/$TCFILE $tcpath") == 0){
# prepend the strings (that means reverse order)
unshift @umountbuffer, " warn \"Opening safe NOT successful\\n\";\n }\n";
unshift @umountbuffer, " print \"Successful\\n\";\n } else {\n";
unshift @umountbuffer, " if (system(\"truecrypt -d $path/$TCFILE\") == 0){\n";
unshift @umountbuffer, " print \"Closing $path/$TCFILE...\\n\";\n";
print "Safe Opened\n";
} else {
warn "Error opening safe\n";
}
} else {
warn "No safe available\n";
}
}
unshift @umountbuffer, "sub $umountfunction".'{'."\n";
$mounteddevices{$device} = $umountfunction;
eval('write_mounteddevices();'); die $@ if $@;
} else {
warn "Device not mounted correctly\n";
}
# write the commands for unmounting to the umount program
unless (-e $UMOUNT){
open(UMOUNT, ">$UMOUNT") or die ("Couldn't write to $UMOUNT\n");
print UMOUNT "#!/usr/bin/perl\n\n";
print UMOUNT 'my %mounteddevices; # hash of mounted devices', "\n";
print UMOUNT $load_mounteddevices_code;
print UMOUNT $write_mounteddevices_code;
print UMOUNT '
load_mounteddevices();
my @devicelist = keys(%mounteddevices);
print "Loaded devices: ", join(", ", @devicelist), "\n\n" if @devicelist>0;
$defaultdevice = $devicelist[0] unless @devicelist == 0;
print "Enter device name of USB device to umount: ";
print "[$defaultdevice]" unless ($defaultdevice eq "");
my $device = <STDIN>;
chomp $device;
$device = $defaultdevice if ($device eq "");
if (exists($mounteddevices{$device})){
my $functionname = $mounteddevices{$device};
eval($functionname."();"); warn $@ if $@;
delete $mounteddevices{$device}
}
@devicelist = keys(%mounteddevices); # might have changed, so we need to assign again
if (@devicelist == 0){
unlink("'.$UMOUNT.'"); # delete self if no devices mounted anymore
unlink("'.$TRFILE.'"); # delete self if no devices mounted anymore
} else {
write_mounteddevices();
}
';
close UMOUNT
}
open(UMOUNT, ">>$UMOUNT") or die ("Couldn't write to $UMOUNT\n");
foreach my $line (@umountbuffer){
print UMOUNT $line;
}
close UMOUNT;
system("chmod 755 $UMOUNT");
system("chown $UID:$GID $UMOUNT");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment