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 = 20; # 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 = ();
44 $myname = script_id
();
49 @args = parse_options
();
50 get_exclude_files
() if ($opt_exclude ne "");
51 @files_to_sign = get_files
(\
@args);
52 if ( $opt_log ) { # logging
53 open(LOG
,">$opt_log") || die "Can't open log file $opt_log\n";
55 sign_files
(\
@files_to_sign);
56 close LOG
if ($opt_log); # logging
64 ( my $script_name = $0 ) =~ s/^.*[\\\/]([\w\.]+)$/$1/;
68 ############################################################################
69 sub parse_options
#09.07.2007 08:13
70 ############################################################################
74 my $success = GetOptions
('h' => \
$opt_help,
75 'd=s' => \
$opt_dir, 'e=s'=>\
$opt_exclude, 'f=s'=>\
$opt_pfxfile, 'l=s'=>\
$opt_log,
76 'p=s'=>\
$opt_pass,'v'=>\
$opt_verbose, 't=s'=>\
$opt_timestamp_url);
77 if ( !$success || $opt_help ) {
84 ############################################################################
85 sub get_exclude_files
#09.07.2007 10:12
86 ############################################################################
88 if ( -e
$opt_exclude ) {
89 # get data from cache file
90 open( IN
, "<$opt_exclude") || die "Can't open exclude file $opt_exclude\n";
91 while ( my $line = <IN
> ) {
93 $exclude_files{$line} = 1; # fill hash
94 print "$line - $exclude_files{$line}\n" if ($debug);
98 print_error
("Can't open $opt_exclude file!\n");
100 } ##get_exclude_files
102 ############################################################################
103 sub get_files
#10.07.2007 10:19
104 ############################################################################
112 foreach $file_pattern ( @
$target )
114 print "Files: $file_pattern\n";
115 foreach $file ( glob( $file_pattern ) )
117 my $lib = File
::Basename
::basename
$file;
118 if ( ! $exclude_files{$lib} ) {
123 print "exclude=$lib\n" if ($opt_verbose);
131 ############################################################################
132 sub sign_files
#09.07.2007 10:36
133 ############################################################################
135 my $files_to_sign = shift;
136 my $commandline_base = ""; # contains whole stuff without the file name
140 if ( $opt_pass =~ /\.exe$/ ) {
141 # get password by tool
142 open(PIPE
, "$opt_pass 2>&1 |") || die "Can't open PIPE!\n";
145 print_error
("Can't get password!\n") if ( !$pass ); # exit here
148 $signtool .= " -v" if ($opt_verbose);
149 $commandline_base = $signtool;
150 $commandline_base .= " -f $opt_pfxfile" if ($opt_pfxfile ne "");
151 $commandline_base .= " -p $opt_pass" if ($opt_pass ne "");
152 $commandline_base .= " -t $opt_timestamp_url" if ($opt_timestamp_url ne "");
154 # Here switch between:
155 # one command line for multiple files (all doesn't work, too much) / for each file one command line
156 if ( $max_files > 1 ) {
157 exec_multi_sign
($files_to_sign, $commandline_base);
160 exec_single_sign
($files_to_sign, $commandline_base);
164 ############################################################################
165 sub exec_single_sign
#11.07.2007 09:05
166 ############################################################################
168 my $files_to_sign = shift;
169 my $commandline_base = shift; # contains whole stuff without the file name
171 my $commandline = "";
173 foreach $file (@
$files_to_sign)
175 $commandline = $commandline_base . " $file";
176 print "$commandline\n" if ($debug);
177 execute
($commandline);
181 ############################################################################
182 sub exec_multi_sign
#11.07.2007 08:56
183 ############################################################################
185 # sign multiple file with one command line
186 my $files_to_sign = shift;
187 my $commandline_base = shift; # contains whole stuff without the file name
188 my $commandline = $commandline_base; # contains stuff which will be executed
192 foreach $file (@
$files_to_sign)
194 $commandline .= " $file";
196 if ( $counter >= $max_files ) {
197 execute
($commandline);
198 $counter = 0; # reset counter
199 $commandline = $commandline_base; # reset command line
202 execute
($commandline) if ($counter > 0);
205 ############################################################################
206 sub execute
#11.07.2007 10:02
207 ############################################################################
209 my $commandline = shift;
213 print "$commandline\n" if ($debug);
214 open(PIPE
, "$commandline 2>&1 |") || die "Error: Cannot execute '$commandline' - $!\n";
215 while ( $result = <PIPE
> ) {
216 print LOG
"$result" if ($opt_log);
217 $errorlines .= $result if ($result =~ /SignTool Error\:/);
220 print_error
( "$errorlines\n" ) if ($errorlines);
223 ############################################################################
224 sub print_error
#09.07.2007 11:21
225 ############################################################################
228 print "ERROR: $text\n";
229 print LOG
"ERROR: $text\n" if ($opt_log); # logging
230 close LOG
if ($opt_log); # logging
234 ############################################################################
235 sub usage
#09.07.2007 08:39
236 ############################################################################
238 print "Usage:\t $myname [-e filename] [-f filename] [-p password] [-t timestamp] [-l filename] [-v] <file[list]> \n";
240 print "\t -e filename\t\t\tFile which contains a list of files which don't have to be signed.\n";
241 print "\t -f pfx_filename\t\t\"Personal Information Exchange\" file.\n";
242 print "\t -p password\t\t\tPassword for \"Personal Information Exchange\" file.\n";
243 print "\t -t timestamp\t\t\tTimestamp URL e.g. \"http://timestamp.verisign.com/scripts/timstamp.dll\"\n";
244 print "\t -l log_filename\t\tFile for logging.\n";
245 print "\t -v\t\t\t\tVerbose.\n";