2 eval 'exec perl -wS $0 ${1+"$@"}'
5 # This file is part of the LibreOffice project.
7 # This Source Code Form is subject to the terms of the Mozilla Public
8 # License, v. 2.0. If a copy of the MPL was not distributed with this
9 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 # This file incorporates work covered by the following license notice:
13 # Licensed to the Apache Software Foundation (ASF) under one or more
14 # contributor license agreements. See the NOTICE file distributed
15 # with this work for additional information regarding copyright
16 # ownership. The ASF licenses this file to you under the Apache
17 # License, Version 2.0 (the "License"); you may not use this file
18 # except in compliance with the License. You may obtain a copy of
19 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
26 my $max_files = 400; # sign $max_files with one command line
31 my $opt_exclude = ""; # file with a list of not signable dll and exe files
34 my $opt_log = ""; # for logging
35 my $opt_pass = ""; # password for signing
36 my $opt_pfxfile = ""; # Personal Information Exchange file
37 my $opt_timestamp_url = ""; # timestamp url
38 my %exclude_files = (); # list of not signable dll and exe files
39 my $signtool = "signtool.exe sign";
41 my @files_to_sign = ();
45 $myname = script_id
();
50 @args = parse_options
();
51 get_exclude_files
() if ($opt_exclude ne "");
52 @files_to_sign = get_files
(\
@args);
53 if ( $opt_log ) { # logging
54 open(LOG
,">$opt_log") || die "Can't open log file $opt_log\n";
56 sign_files
(\
@files_to_sign);
57 close LOG
if ($opt_log); # logging
65 ( my $script_name = $0 ) =~ s/^.*[\\\/]([\w\.]+)$/$1/;
69 ############################################################################
70 sub parse_options
#09.07.2007 08:13
71 ############################################################################
75 my $success = GetOptions
('h' => \
$opt_help,
76 'd=s' => \
$opt_desc, 'e=s'=>\
$opt_exclude, 'f=s'=>\
$opt_pfxfile, 'l=s'=>\
$opt_log,
77 'p=s'=>\
$opt_pass,'v'=>\
$opt_verbose, 't=s'=>\
$opt_timestamp_url);
78 if ( !$success || $opt_help ) {
85 ############################################################################
86 sub get_exclude_files
#09.07.2007 10:12
87 ############################################################################
89 if ( -e
$opt_exclude ) {
90 # get data from cache file
91 open( IN
, "<$opt_exclude") || die "Can't open exclude file $opt_exclude\n";
92 while ( my $line = <IN
> ) {
94 $exclude_files{$line} = 1; # fill hash
95 print "$line - $exclude_files{$line}\n" if ($debug);
99 print_error
("Can't open $opt_exclude file!\n");
101 } ##get_exclude_files
103 ############################################################################
104 sub get_files
#10.07.2007 10:19
105 ############################################################################
113 foreach $file_pattern ( @
$target )
115 print "Files: $file_pattern\n";
116 foreach $file ( glob( $file_pattern ) )
118 my $lib = File
::Basename
::basename
$file;
119 if ( ! $exclude_files{$lib} ) {
124 print "exclude=$lib\n" if ($opt_verbose);
132 ############################################################################
133 sub sign_files
#09.07.2007 10:36
134 ############################################################################
136 my $files_to_sign = shift;
137 my $commandline_base = ""; # contains whole stuff without the file name
141 if ( $opt_pass =~ /\.exe$/ ) {
142 # get password by tool
143 open(PIPE
, "$opt_pass 2>&1 |") || die "Can't open PIPE!\n";
146 print_error
("Can't get password!\n") if ( !$pass ); # exit here
149 $signtool .= " -v" if ($opt_verbose);
150 $commandline_base = $signtool;
151 $commandline_base .= " -fd sha256 -td sha256";
152 $commandline_base .= " -f $opt_pfxfile" if ($opt_pfxfile ne "");
153 $commandline_base .= " -p $opt_pass" if ($opt_pass ne "");
154 $commandline_base .= " -tr $opt_timestamp_url" if ($opt_timestamp_url ne "");
155 $commandline_base .= " -d \"$opt_desc\"" if ($opt_desc ne "");
157 # Here switch between:
158 # one command line for multiple files (all doesn't work, too much) / for each file one command line
159 if ( $max_files > 1 ) {
160 exec_multi_sign
($files_to_sign, $commandline_base);
163 exec_single_sign
($files_to_sign, $commandline_base);
167 ############################################################################
168 sub exec_single_sign
#11.07.2007 09:05
169 ############################################################################
171 my $files_to_sign = shift;
172 my $commandline_base = shift; # contains whole stuff without the file name
174 my $commandline = "";
176 foreach $file (@
$files_to_sign)
178 $commandline = $commandline_base . " $file";
179 print "$commandline\n" if ($debug);
180 execute
($commandline);
184 ############################################################################
185 sub exec_multi_sign
#11.07.2007 08:56
186 ############################################################################
188 # sign multiple file with one command line
189 my $files_to_sign = shift;
190 my $commandline_base = shift; # contains whole stuff without the file name
191 my $commandline = $commandline_base; # contains stuff which will be executed
195 foreach $file (@
$files_to_sign)
197 $commandline .= " $file";
199 if ( $counter >= $max_files ) {
200 execute
($commandline);
201 $counter = 0; # reset counter
202 $commandline = $commandline_base; # reset command line
205 execute
($commandline) if ($counter > 0);
208 ############################################################################
209 sub execute
#11.07.2007 10:02
210 ############################################################################
212 my $commandline = shift;
216 print "$commandline\n" if ($debug);
217 open(PIPE
, "$commandline 2>&1 |") || die "Error: Cannot execute '$commandline' - $!\n";
218 while ( $result = <PIPE
> ) {
219 print LOG
"$result" if ($opt_log);
220 $errorlines .= $result if ($result =~ /SignTool Error\:/);
223 print_error
( "$errorlines\n" ) if ($errorlines);
226 ############################################################################
227 sub print_error
#09.07.2007 11:21
228 ############################################################################
231 print "ERROR: $text\n";
232 print LOG
"ERROR: $text\n" if ($opt_log); # logging
233 close LOG
if ($opt_log); # logging
237 ############################################################################
238 sub usage
#09.07.2007 08:39
239 ############################################################################
241 print "Usage:\t $myname [-e filename] [-f filename] [-p password] [-t timestamp] [-l filename] [-v] <file[list]> \n";
243 print "\t -e filename\t\t\tFile which contains a list of files which don't have to be signed.\n";
244 print "\t -f pfx_filename\t\t\"Personal Information Exchange\" file.\n";
245 print "\t -p password\t\t\tPassword for \"Personal Information Exchange\" file.\n";
246 print "\t -t timestamp\t\t\tTimestamp URL e.g. \"http://timestamp.digicert.com/\"\n";
247 print "\t -l log_filename\t\tFile for logging.\n";
248 print "\t -v\t\t\t\tVerbose.\n";