| 1 |
#! /usr/bin/perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use Getopt::Long qw(:config bundling); |
| 6 |
use English qw( -no_match_vars ); |
| 7 |
use Firefox::Marionette(); |
| 8 |
use FileHandle(); |
| 9 |
use Encode(); |
| 10 |
|
| 11 |
our $VERSION = '1.70'; |
| 12 |
|
| 13 |
my %options; |
| 14 |
|
| 15 |
MAIN: { |
| 16 |
Getopt::Long::GetOptions( |
| 17 |
\%options, 'help|h', |
| 18 |
'version|v', 'binary|b:s', |
| 19 |
'file|f:s', 'issuer-organization|i', |
| 20 |
'trusted-only|t', 'profile-name|p:s', |
| 21 |
'default-profile|d', |
| 22 |
); |
| 23 |
my %parameters = _validate_options_and_parse_parameters(); |
| 24 |
my $firefox = Firefox::Marionette->new(%parameters); |
| 25 |
|
| 26 |
my $handle = *{STDOUT}; |
| 27 |
my $output_name = 'STDOUT'; |
| 28 |
if ( $options{file} ) { |
| 29 |
$handle = FileHandle->new( |
| 30 |
$options{file}, |
| 31 |
Fcntl::O_CREAT() | Fcntl::O_WRONLY() | Fcntl::O_EXCL(), |
| 32 |
Fcntl::S_IRUSR() | Fcntl::S_IWUSR() | Fcntl::S_IRGRP() | |
| 33 |
Fcntl::S_IROTH() |
| 34 |
) |
| 35 |
or die |
| 36 |
"Failed to open $options{file} for writing:$EXTENDED_OS_ERROR\n"; |
| 37 |
$output_name = $options{file}; |
| 38 |
} |
| 39 |
my %sort_name; |
| 40 |
foreach my $certificate ( |
| 41 |
sort { |
| 42 |
( $sort_name{$a} ||= |
| 43 |
$options{'issuer-organization'} ? $a->issuer_organization() |
| 44 |
|| $a->display_name() : $a->display_name() ) |
| 45 |
cmp( $sort_name{$b} ||= |
| 46 |
$options{'issuer-organization'} ? $b->issuer_organization() |
| 47 |
|| $b->display_name() : $b->display_name() ) |
| 48 |
} $firefox->certificates() |
| 49 |
) |
| 50 |
{ |
| 51 |
if ( $certificate->is_ca_cert() ) { |
| 52 |
if ( ( $options{'trusted-only'} ) |
| 53 |
&& ( !$firefox->is_trusted($certificate) ) ) |
| 54 |
{ |
| 55 |
next; |
| 56 |
} |
| 57 |
my $output_line = q[# ] |
| 58 |
. ( |
| 59 |
$options{'issuer-organization'} |
| 60 |
? ( $certificate->issuer_organization() |
| 61 |
|| $certificate->display_name() ) |
| 62 |
. q[ -- ] |
| 63 |
: q[] |
| 64 |
) |
| 65 |
. $certificate->display_name() . "\n" |
| 66 |
. $firefox->certificate_as_pem($certificate) . "\n"; |
| 67 |
$handle->print( Encode::encode( 'UTF-8', $output_line, 1 ) ) |
| 68 |
or die "Failed to print to $output_name:$EXTENDED_OS_ERROR\n"; |
| 69 |
} |
| 70 |
} |
| 71 |
if ( $options{file} ) { |
| 72 |
$handle->close() |
| 73 |
or die "Failed to close $output_name:$EXTENDED_OS_ERROR\n"; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
sub _validate_options_and_parse_parameters { |
| 78 |
if ( $options{help} ) { |
| 79 |
require Pod::Simple::Text; |
| 80 |
my $parser = Pod::Simple::Text->new(); |
| 81 |
$parser->parse_from_file($PROGRAM_NAME); |
| 82 |
exit 0; |
| 83 |
} |
| 84 |
elsif ( $options{version} ) { |
| 85 |
print "$VERSION\n" |
| 86 |
or die "Failed to print to STDOUT:$EXTENDED_OS_ERROR\n"; |
| 87 |
exit 0; |
| 88 |
} |
| 89 |
my %mapping = ( |
| 90 |
'profile-name' => 'profile_name', |
| 91 |
binary => 'binary', |
| 92 |
); |
| 93 |
my %parameters; |
| 94 |
foreach my $key_name (qw( binary profile-name )) { |
| 95 |
if ( defined $options{$key_name} ) { |
| 96 |
$parameters{ $mapping{$key_name} } = $options{$key_name}; |
| 97 |
} |
| 98 |
} |
| 99 |
if ( ( $parameters{profile_name} ) || ( $options{'default-profile'} ) ) { |
| 100 |
my $profile_name = delete $parameters{profile_name} |
| 101 |
|| Firefox::Marionette::Profile->default_name(); |
| 102 |
my $profile_directory = |
| 103 |
Firefox::Marionette::Profile->directory($profile_name); |
| 104 |
my $cert_db_path = |
| 105 |
File::Spec->catfile( $profile_directory, 'cert9.db' ); |
| 106 |
$parameters{import_profile_paths} = [$cert_db_path]; |
| 107 |
} |
| 108 |
return %parameters; |
| 109 |
} |
| 110 |
|
| 111 |
__END__ |
| 112 |
=head1 NAME |
| 113 |
|
| 114 |
ca-bundle-for-firefox - generate the ca-bundle.crt for the current firefox instance |
| 115 |
|
| 116 |
=head1 VERSION |
| 117 |
|
| 118 |
Version 1.70 |
| 119 |
|
| 120 |
=head1 USAGE |
| 121 |
|
| 122 |
$ ca-bundle-for-firefox >/etc/pki/tls/certs/ca-bundle.crt |
| 123 |
$ ca-bundle-for-firefox --file current.crt |
| 124 |
$ ca-bundle-for-firefox --binary=/path/to/old/firefox --file old.crt |
| 125 |
$ diff -Naur old.crt current.crt |
| 126 |
$ ca-bundle-for-firefox --issuer-organization --trusted-only --profile-name default |
| 127 |
$ ca-bundle-for-firefox --issuer-organization --trusted-only --default-profile |
| 128 |
$ ca-bundle-for-firefox -dit |
| 129 |
|
| 130 |
=head1 DESCRIPTION |
| 131 |
|
| 132 |
This program is intended to generate the ca-bundle.crt file from the Certificate Authorities maintained in firefox. |
| 133 |
|
| 134 |
By default, the only firefox version that may be used will be present in the PATH environment variable. However, the user may specify a different path with |
| 135 |
the --binary parameter. |
| 136 |
|
| 137 |
=head1 REQUIRED ARGUMENTS |
| 138 |
|
| 139 |
None |
| 140 |
|
| 141 |
=head1 OPTIONS |
| 142 |
|
| 143 |
Option names can be abbreviated to uniqueness and can be stated with singe or double dashes, and option values can be separated from the option name by a space or '=' (as with Getopt::Long). Option names are also case- |
| 144 |
sensitive. |
| 145 |
|
| 146 |
=over 4 |
| 147 |
|
| 148 |
=item * --help - This page. |
| 149 |
|
| 150 |
=item * --binary - Use this firefox binary instead of the default firefox instance |
| 151 |
|
| 152 |
=item * --default-profile - Use the certificate database from the default profile |
| 153 |
|
| 154 |
=item * --issuer-organization - Print the Issuer Organisation as well as the Display Name in the comment line. The Issuer Organisation is used in Firefox's Certificate Manager. |
| 155 |
|
| 156 |
=item * --trusted-only - Only output certificates that are trusted in the current profile |
| 157 |
|
| 158 |
=item * --profile-name - Use the certificate database for a particular profile |
| 159 |
|
| 160 |
=item * --file - Write the Certificate Authority bundle out to this file |
| 161 |
|
| 162 |
=back |
| 163 |
|
| 164 |
=head1 CONFIGURATION |
| 165 |
|
| 166 |
ca-bundle-for-firefox requires no configuration files or environment variables. |
| 167 |
|
| 168 |
=head1 DEPENDENCIES |
| 169 |
|
| 170 |
ca-bundle-for-firefox requires the following non-core Perl modules |
| 171 |
|
| 172 |
=over |
| 173 |
|
| 174 |
=item * |
| 175 |
L<Pod::Simple::Text|Pod::Simple::Text> |
| 176 |
|
| 177 |
=back |
| 178 |
|
| 179 |
=head1 DIAGNOSTICS |
| 180 |
|
| 181 |
None. |
| 182 |
|
| 183 |
=head1 INCOMPATIBILITIES |
| 184 |
|
| 185 |
None known. |
| 186 |
|
| 187 |
=head1 EXIT STATUS |
| 188 |
|
| 189 |
This program will exit with a zero after successfully completing. |
| 190 |
|
| 191 |
=head1 BUGS AND LIMITATIONS |
| 192 |
|
| 193 |
To report a bug, or view the current list of bugs, please visit L<https://github.com/david-dick/firefox-marionette/issues> |
| 194 |
|
| 195 |
=head1 AUTHOR |
| 196 |
|
| 197 |
David Dick C<< <[email protected]> >> |
| 198 |
|
| 199 |
=head1 LICENSE AND COPYRIGHT |
| 200 |
|
| 201 |
Copyright (c) 2024, David Dick C<< <[email protected]> >>. All rights reserved. |
| 202 |
|
| 203 |
This module is free software; you can redistribute it and/or |
| 204 |
modify it under the same terms as Perl itself. See L<perlartistic/perlartistic>. |
| 205 |
|
| 206 |
=head1 DISCLAIMER OF WARRANTY |
| 207 |
|
| 208 |
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY |
| 209 |
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN |
| 210 |
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES |
| 211 |
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER |
| 212 |
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 213 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE |
| 214 |
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH |
| 215 |
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL |
| 216 |
NECESSARY SERVICING, REPAIR, OR CORRECTION. |
| 217 |
|
| 218 |
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING |
| 219 |
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR |
| 220 |
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE |
| 221 |
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, |
| 222 |
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE |
| 223 |
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING |
| 224 |
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A |
| 225 |
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF |
| 226 |
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF |
| 227 |
SUCH DAMAGES. |