#!/usr/bin/perl # crypthome # Last modified: Sun Mar 27, 2005 07:45PM # Hagen Paul Pfeifer - hagen@jauu.net # http://www.jauu.net use strict; use warnings; use Fcntl; use Term::ReadKey; my $version = '$Id: crypthome 19 2004-09-21 18:24:46Z pfeifer $'; ################################################# ## User Configuration my %data = ( "cryptfile" => "$ENV{'HOME'}/.cryptfile", "crypttmp" => "/tmp/.$ENV{'USER'}crypt" "linkname" => "$ENV{'HOME'}/CRYPT";" ); my %gpg = ( "bin" => "/usr/bin/gpg", "decrypt" => "--decrypt", "encrypt" => "--symmetric", "chiper" => "--cipher-algo=AES256" ); my %tar = ( "bin" => "/bin/tar", "extract" => "-jxf", "create" => "-cjf" ); my %mv = ( "bin" => "/bin/mv" ); ################################################# ## C0d3 $ENV{'TMPDIR'} = "/tmp" unless defined $ENV{'TMPDIR'}; print "*** crypthome ($version) ***\n\n"; &encrypt if -e $data{'crypttmp'}; &decrypt if -e $data{'cryptfile'}; # init crypthome &init; sub init { my $dir; print<<'.'; Fine, you use the ultimate homeCryptWrapper(C) the first time so do a initialization first. Which directory should I encrypt? . chomp($dir = ); die "Directory $dir doesn't exists!" unless -d $dir; # cd to source dir # FIXME: add some checking! $dir =~ /(.*\/)(.*)/; my $srcdir = $1 || $ENV{'HOME'}; my $dstfile = $2; print "SRC: $srcdir | dst: $dstfile\n"; chdir $srcdir or die "Can't chdir(3) to $srcdir: $!\n"; # tar directory, encrypt it, save it, delete original save; my $tmpfile = $ENV{'TMPDIR'} . "/.crypTmpt" . time(); system("$tar{'bin'} $tar{'create'} $tmpfile $dstfile"); system("$gpg{'bin'} $gpg{'encrypt'} $gpg{'chiper'} $tmpfile"); system($mv{'bin'}, $tmpfile . ".gpg", $data{'cryptfile'}); #&removeclean($tmpfile . ".gpg"); } # entschluesseln sub decrypt { if (-d $data{'crypttmp'}) { die "$data{'cryptfile'} AND $data{'crypttmp'} exists!\n" . "Solve by hand, dude!\n"; } system($mv{'bin'}, $data{'cryptfile'}, $data{'crypttmp'} . ".gpg"); print "$gpg{'bin'} $gpg{'decrypt'} $data{'crypttmp'}" . ".gpg"; system("$gpg{'bin'} $gpg{'decrypt'} $data{'crypttmp'}.gpg >> $data{'crypttmp'}" ); # determine tmp dir (my $tmpdir = $data{'crypttmp'}) =~ s/(.*\/)(.*)/$1/; chdir $tmpdir or die "Can't chdir(3) to $tmpdir: $!\n"; system("$tar{'bin'} $tar{'extract'} $data{'crypttmp'}"); symlink($data) exit 0; } # verschluesseln sub encrypt { if (-d $data{'cryptfile'}) { die "$data{'cryptfile'} AND $data{'crypttmp'} exists!\n" . "Solve by hand, dude!\n"; } exit 0; } sub removeclean { my $file = shift; my ($link, $size, $blksize, $buf); unless(sysopen(FH, $file, O_WRONLY | O_SYNC)) { die "Can't delete $file\n"; } unless(($link, $size, $blksize) = (stat(FH))[3,7,11]) { die "Can't stat(2) $file\n"; } warn "STILL LINKS PRESENT FOR FILE $file!\n" if $link and $^W; print "remove $file\n"; $buf = '\0' x $blksize; my ($written, $nbytes); for($written = 0; defined($nbytes=syswrite(FH, $buf, $blksize)); $written += $nbytes) {} print "done $file\n"; close(FH); } # vim:set ts=2 ft=perl: