extracting files from a tar file

Deven T. Corzine deven at rpi.edu
Sun Aug 19 02:13:35 AEST 1990


> I have a faculty member who has brought a tape with him from a
> different site.  It is a tar tape and the files were stored using
> absolute pathnames. The difficulty is that we don't have the same
> file structure names and so when I would try to restore the files,
> it fills up the root file structure quickly (we don't keep a whole
> lot of space available in that partition).

> I was wondering if there are any PD programs that exist or whether
> someone has written a script to get around this problem. I could
> write a script to extract a file at a time and then move it but its
> a hassle trying to maintain the directory structure that the faculty
> member has and preserve modification dates. I've also considered
> writing a C program using the chroot function and doing a system
> call to tar but that seems messy too.

Yes, I have written a Perl script to do exactly this.  It uses the
chroot(2) call, and must run setuid root, but it will setuid back to
the real uid after performing the chroot.  It appears fairly secure,
and it does work.  The only real thing you need to be sure of is that
tar(1) will run standalone, with NO system libraries available.

I wrote and tested the script under SunOS 4.0.3 using Perl 3.0; Sun's
/bin/tar does indeed work standalone (doesn't need the shared
libraries) so it works.  If you have a dynamically compiled one, you
might have to statically link it for it to work.  The suidperl program
is needed to execute the script, but it is in memory when running, so
it doesn't need the ability to run standalone.  Enough caveats; here's
the script.  [Can anyone think of a good name for it?]

----
#!/usr/local/bin/suidperl
if ($#ARGV != 1) {
   print STDERR "Usage: $0 <tarfile> <dir>\n";
   exit 1;
}
$ENV{'PATH'} = '/bin:/usr/bin';
$tar = $ARGV[0];
$ARGV[1] =~ /(\w+)$/;
$dir = $1;
if (! -f $tar) {
   print STDERR "Error: tar file does not exist.\n";
   exit 1;
}
if (-e $dir) {
   print STDERR "Error: directory must not exist.\n";
   exit 1;
}
if (! -W ".") {
   print STDERR "Error: No write permission on current directory.\n";
   exit 1;
}
mkdir($dir,0777);
chown $<,$(,$dir;
system "/bin/cp","/bin/tar",$dir;
system "/bin/cp",$tar,"$dir/absolute.tar";
chdir $dir;
chroot ".";
($>,$)) = ($<,$();
system "./tar","-xvf","absolute.tar";
unlink "tar";
unlink "absolute.tar";
exit 0;
----

Have fun.

Deven
-- 
Deven T. Corzine        Internet:  deven at rpi.edu, shadow at pawl.rpi.edu
Snail:  2214 12th St. Apt. 2, Troy, NY 12180   Phone:  (518) 271-0750
Bitnet:  deven at rpitsmts, userfxb6 at rpitsmts     UUCP:  uunet!rpi!deven
Simple things should be simple and complex things should be possible.
-- 
Deven T. Corzine        Internet:  deven at rpi.edu, shadow at pawl.rpi.edu
Snail:  2214 12th St. Apt. 2, Troy, NY 12180   Phone:  (518) 271-0750
Bitnet:  deven at rpitsmts, userfxb6 at rpitsmts     UUCP:  uunet!rpi!deven
Simple things should be simple and complex things should be possible.



More information about the Comp.unix.questions mailing list