create_tarball.pl 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/perl
  2. # vi: set expandtab shiftwidth=4:
  3. use strict;
  4. use warnings;
  5. use Archive::Tar;
  6. use Archive::Tar::Constant;
  7. use File::Spec;
  8. sub read_file_data {
  9. my $filename = shift;
  10. my $data;
  11. open(my $fh, '<', $filename)
  12. or die "Unable to open file '$filename': $!\n";
  13. my $rc = read($fh, $data, -s $filename);
  14. die "Error reading from file '$filename': $!\n"
  15. unless defined $rc;
  16. close($fh);
  17. return $data;
  18. }
  19. sub check_directory {
  20. my $directory = shift;
  21. -d $directory or die "Required directory $directory not found.\n";
  22. }
  23. sub check_file {
  24. my $filename = shift;
  25. -f $filename or die "Required file $filename not found.\n";
  26. }
  27. my $client = shift or die "Usage: $0 FQDN\n";
  28. # Check prerequisites
  29. check_directory('ca');
  30. check_directory('certs');
  31. check_directory(File::Spec->catfile('clients', $client));
  32. check_file(File::Spec->catfile('ca', 'ca.crt'));
  33. check_file(File::Spec->catfile('certs', "$client.crt"));
  34. check_file(File::Spec->catfile('clients', $client, "$client.key"));
  35. check_file(File::Spec->catfile('clients', $client, "$client.pub"));
  36. my $tar = Archive::Tar->new();
  37. my @directories = qw(
  38. etc etc/isakmpd etc/isakmpd/ca etc/isakmpd/certs
  39. );
  40. foreach my $directory (@directories) {
  41. $tar->add_data($directory, '', { type => DIR });
  42. $tar->chown($directory, 'root:wheel');
  43. $tar->chmod($directory, '0755');
  44. }
  45. $tar->add_data('etc/isakmpd/private', '', { type => DIR });
  46. $tar->chown('etc/isakmpd/private', 'root:wheel');
  47. $tar->chmod('etc/isakmpd/private', '0700');
  48. $tar->add_data('etc/isakmpd/ca/ca.crt', read_file_data('ca/ca.crt'));
  49. $tar->chown('etc/isakmpd/ca/ca.crt', 'root:wheel');
  50. $tar->add_data("etc/isakmpd/certs/$client.crt",
  51. read_file_data("certs/$client.crt"));
  52. $tar->chown("etc/isakmpd/certs/$client.crt", 'root:wheel');
  53. $tar->add_data('etc/isakmpd/local.pub',
  54. read_file_data("clients/$client/$client.pub"));
  55. $tar->chown('etc/isakmpd/local.pub', 'root:wheel');
  56. $tar->chmod('etc/isakmpd/local.pub', '0444');
  57. $tar->add_data('etc/isakmpd/private/private.key',
  58. read_file_data("clients/$client/$client.key"));
  59. $tar->chown('etc/isakmpd/private/private.key', 'root:wheel');
  60. $tar->chmod('etc/isakmpd/private/private.key', '0600');
  61. $tar->write("$client.tgz", COMPRESS_GZIP);