| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # | ||
| 4 | # Author: Slaven Rezic | ||
| 5 | # | ||
| 6 | # Copyright (C) 2017,2018 Slaven Rezic. All rights reserved. | ||
| 7 | # This package is free software; you can redistribute it and/or | ||
| 8 | # modify it under the same terms as Perl itself. | ||
| 9 | # | ||
| 10 | # Mail: slaven@rezic.de | ||
| 11 | # WWW: http://www.rezic.de/eserte/ | ||
| 12 | # | ||
| 13 | |||
| 14 | package Doit::Macsecurity; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | our $VERSION = '0.011'; | ||
| 19 | |||
| 20 | use Doit::Log; | ||
| 21 | |||
| 22 | sub new { bless {}, shift } | ||
| 23 | sub functions { qw(macsecurity_install_trusted_certificate) } | ||
| 24 | |||
| 25 | sub macsecurity_install_trusted_certificate { | ||
| 26 | my($self, %args) = @_; | ||
| 27 | my $cert_file = delete $args{cert_file} || error "Please specify cert_file"; | ||
| 28 | error "Unhandled options: " . join(" ", %args) if %args; | ||
| 29 | |||
| 30 | chomp(my $fingerprint = $self->info_qx({quiet=>1}, qw(openssl x509 -noout -fingerprint -in), $cert_file)); | ||
| 31 | $fingerprint =~ s{^SHA1 Fingerprint=}{}; | ||
| 32 | $fingerprint =~ s{:}{}g; | ||
| 33 | |||
| 34 | my $found = sub { | ||
| 35 | open my $fh, '-|', qw(security find-certificate -a -Z) | ||
| 36 | or error "while running security find-certificate: $!"; | ||
| 37 | while(<$fh>) { | ||
| 38 | chomp; | ||
| 39 | if (/^SHA-1 hash:\s*(\S+)/) { | ||
| 40 | if ($1 eq $fingerprint) { | ||
| 41 | return 1; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | close $fh | ||
| 46 | or error "while running security find-certificate: $!"; | ||
| 47 | 0; | ||
| 48 | }->(); | ||
| 49 | |||
| 50 | if (!$found) { | ||
| 51 | $self->system(qw(security import), $cert_file); | ||
| 52 | $self->system(qw(security add-trusted-cert), $cert_file); # XXX actually there should be a way to find out if an installed certificate is already trusted | ||
| 53 | return 1; | ||
| 54 | } else { | ||
| 55 | return 0; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | 1; | ||
| 60 | |||
| 61 | __END__ |