提交记录新增scripts/guest-os/native-component-manifest.pl+41 −0看文件
| @@ -0,0 +1,41 @@ | ||
| 1 | 新增:#!/usr/bin/perl | |
| 2 | 新增:use strict; | |
| 3 | 新增:use warnings; | |
| 4 | 新增:use JSON::PP; | |
| 5 | 新增:use Digest::SHA; | |
| 6 | 新增:use Fcntl qw(:DEFAULT); | |
| 7 | 新增: | |
| 8 | 新增:my ($mode, $manifest_path, $kit) = @ARGV; | |
| 9 | 新增:die "Invalid component operation\n" unless defined($mode) && $mode =~ /^(source|targets|verify|publish)$/; | |
| 10 | 新增:open(my $input, '<', $manifest_path) or die "Component manifest unavailable\n"; | |
| 11 | 新增:local $/; | |
| 12 | 新增:my $encoded = <$input>; | |
| 13 | 新增:close($input); | |
| 14 | 新增:die "Component manifest too large\n" if length($encoded) > 4 * 1024 * 1024; | |
| 15 | 新增:my $manifest = decode_json($encoded); | |
| 16 | 新增:die "Invalid component manifest\n" unless $manifest->{protocol} == 1 && $manifest->{system} eq 'macos' && ref($manifest->{files}) eq 'ARRAY' && $manifest->{revision} =~ /^[a-f0-9]{64}$/; | |
| 17 | 新增:for my $entry (@{$manifest->{files}}) { | |
| 18 | 新增: my ($source, $target) = ($entry->{source}, $entry->{target} // ''); | |
| 19 | 新增: die "Invalid component source\n" unless defined($source) && $source !~ m{(?:^/|(?:^|/)\.\.?(?:/|$)|[\x00\r\n])}; | |
| 20 | 新增: die "Invalid component target\n" if $target ne '' && ($target !~ m{^/(?:Applications/Cage[^/]*\.app/|usr/local/libexec/cage-)} || $target =~ m{(?:^|/)\.\.?(?:/|$)|[\x00\r\n]}); | |
| 21 | 新增: die "Invalid component digest\n" unless $entry->{sha256} =~ /^[a-f0-9]{64}$/ && $entry->{installed_sha256} =~ /^[a-f0-9]{64}$/; | |
| 22 | 新增: if ($mode eq 'targets') { print "$source\0$target\0" if $target ne ''; next; } | |
| 23 | 新增: next if $mode ne 'source' && $target eq ''; | |
| 24 | 新增: my $path = $mode eq 'source' ? "$kit/$source" : $target; | |
| 25 | 新增: open(my $file, '<', $path) or die "Missing component $path\n"; | |
| 26 | 新增: binmode($file); | |
| 27 | 新增: my $actual = Digest::SHA->new(256)->addfile($file)->hexdigest; | |
| 28 | 新增: close($file); | |
| 29 | 新增: my $expected = $mode eq 'source' ? $entry->{sha256} : $entry->{installed_sha256}; | |
| 30 | 新增: die "Component digest mismatch $path\n" unless $actual eq $expected; | |
| 31 | 新增:} | |
| 32 | 新增:if ($mode eq 'publish') { | |
| 33 | 新增: die "Component publication requires administrator\n" unless $> == 0; | |
| 34 | 新增: my $target = '/var/db/cage-components.json'; | |
| 35 | 新增: die "Invalid existing component receipt\n" if -l $target || (-e $target && !-f $target); | |
| 36 | 新增: my $temporary = "$target.$$"; | |
| 37 | 新增: sysopen(my $output, $temporary, O_WRONLY | O_CREAT | O_EXCL, 0644) or die "Cannot stage component receipt\n"; | |
| 38 | 新增: print $output $encoded or die "Cannot write component receipt\n"; | |
| 39 | 新增: close($output) or die "Cannot close component receipt\n"; | |
| 40 | 新增: rename($temporary, $target) or die "Cannot publish component receipt\n"; | |
| 41 | 新增:} | |

