28 |
|
|
29 |
use constant COMP_PATH => '/software/components/@COMP@'; |
use constant COMP_PATH => '/software/components/@COMP@'; |
30 |
|
|
31 |
|
|
32 |
|
sub prepare_backup_and_dir { |
33 |
|
my $file = @_; |
34 |
|
|
35 |
|
# make backup of existing file |
36 |
|
LC::File::move($file, $file.".old") if ( -f $file ); |
37 |
|
|
38 |
|
# create the directory to hold the file |
39 |
|
my $dir = dirname($file); |
40 |
|
if ( ! -d $dir ) { |
41 |
|
LC::File::makedir($dir); |
42 |
|
} |
43 |
|
} |
44 |
|
|
45 |
|
|
46 |
sub Configure |
sub Configure |
47 |
{ |
{ |
48 |
my ($self, $config) = @_; |
my ($self, $config) = @_; |
50 |
if ( $config->elementExists(COMP_PATH) ) { |
if ( $config->elementExists(COMP_PATH) ) { |
51 |
my $st = $config->getElement(COMP_PATH)->getTree; |
my $st = $config->getElement(COMP_PATH)->getTree; |
52 |
|
|
53 |
my $hostcert = $st->{'hostcert'}; |
my $keycerts = $st->{'key_cert'}; |
|
my $hostkey = $st->{'hostkey'}; |
|
54 |
my $server_url = $st->{'server_url'}; |
my $server_url = $st->{'server_url'}; |
55 |
my $trustedget = $st->{'trustedget'}; |
my $trustedget = $st->{'trustedget'}; |
56 |
|
|
60 |
return; |
return; |
61 |
} |
} |
62 |
|
|
63 |
|
# create random directory only accessible for root |
64 |
|
my $tmpdir = LC::File::random_directory( '/tmp/trustedget-X', 0700 ); |
65 |
|
my $tmpcert = "$tmpdir/hostcert.pem"; |
66 |
|
my $tmpkey = "$tmpdir/hostkey.pem"; |
67 |
|
|
68 |
# query the server to determine if the key can be downloaded |
# query the server to determine if the key can be downloaded |
69 |
my $err; |
my $err; |
70 |
my $can_download = 0; |
my $can_download = 0; |
79 |
} |
} |
80 |
else { |
else { |
81 |
$can_download = 1; |
$can_download = 1; |
|
} |
|
|
|
|
|
|
|
|
# download certificate and key if possible |
|
|
if ( $can_download ) { |
|
|
# make backup of existing key |
|
|
LC::File::move($hostkey, $hostkey.".old") if ( -f $hostkey ); |
|
82 |
|
|
83 |
# create the directory to hold the key |
# download key |
84 |
my $dir = dirname($hostkey); |
my $cmd = "$trustedget ${server_url}?key > $tmpkey"; |
|
if ( ! -d $dir ) { |
|
|
LC::File::makedir($dir); |
|
|
} |
|
|
|
|
|
$self->info("Installing key to $hostkey"); |
|
|
my $cmd = "$trustedget ${server_url}?key > $hostkey"; |
|
85 |
$self->debug(3, "$cmd"); |
$self->debug(3, "$cmd"); |
86 |
LC::Process::execute([$cmd], "stdout" => \$err); |
LC::Process::execute([$cmd], "stdout" => \$err); |
87 |
if ( $? ) { |
if ( $? ) { |
88 |
chomp $err; |
chomp $err; |
89 |
$self->error($err); |
$self->error($err); |
90 |
} |
} |
91 |
|
chmod 0400, $tmpkey; |
92 |
# make backup of existing key |
|
93 |
LC::File::move($hostcert, $hostcert.".old") if ( -f $hostcert ); |
# download certificate |
94 |
|
$cmd = "$trustedget ${server_url}?cert > $tmpcert"; |
|
# create the directory to hold the cert |
|
|
$dir = dirname($hostcert); |
|
|
if ( ! -d $dir ) { |
|
|
LC::File::makedir($dir); |
|
|
} |
|
|
|
|
|
$self->info("Installing certificate to $hostcert"); |
|
|
$cmd = "$trustedget ${server_url}?cert > $hostcert"; |
|
95 |
$self->debug(3, "$cmd"); |
$self->debug(3, "$cmd"); |
96 |
LC::Process::execute([$cmd], "stdout" => \$err); |
LC::Process::execute([$cmd], "stdout" => \$err); |
97 |
if ( $? ) { |
if ( $? ) { |
98 |
chomp $err; |
chomp $err; |
99 |
$self->error($err); |
$self->error($err); |
100 |
} |
} |
101 |
|
chmod 0644, $tmpcert; |
102 |
} |
} |
103 |
|
|
104 |
|
|
105 |
# ensure the permissions are OK |
# download certificate and key if possible |
106 |
chmod (0400, $hostkey) if ( -f $hostkey ); |
if ( $can_download ) { |
107 |
chmod (0644, $hostcert) if ( -f $hostkey ); |
$self->info("Copying downloaded files..."); |
108 |
|
foreach my $i ( @$keycerts ) { |
109 |
|
my $key = $i->{'hostkey'}; |
110 |
|
my $cert = $i->{'hostcert'}; |
111 |
|
|
112 |
|
my $uid = getpwnam( $i->{'owner'} ); |
113 |
|
my $gid = getgrnam( $i->{'group'} ); |
114 |
|
|
115 |
|
$self->debug("$i: $key"); |
116 |
|
|
117 |
|
&prepare_backup_and_dir( $key ); |
118 |
|
$self->info("Installing key to $key"); |
119 |
|
LC::File::copy( $tmpkey, $key ); |
120 |
|
if ( -f $key ) { |
121 |
|
chown $uid, $gid, $key; |
122 |
|
chmod 0400, $key; |
123 |
|
} |
124 |
|
|
125 |
|
&prepare_backup_and_dir( $cert ); |
126 |
|
$self->info("Installing certificate to $cert"); |
127 |
|
LC::File::copy( $tmpcert, $cert ); |
128 |
|
if ( -f $cert ) { |
129 |
|
chown $uid, $gid, $cert; |
130 |
|
} |
131 |
|
} |
132 |
|
} |
133 |
|
LC::File::remove( $tmpkey ); |
134 |
|
LC::File::remove( $tmpcert ); |
135 |
|
rmdir $tmpdir; |
136 |
} |
} |
137 |
|
|
138 |
return 1; |
return 1; |