Update ooo320-m1
[ooovba.git] / postprocess / signing / signing.pl
bloba52dfb8e5ff1c4db6d2a71e4234bdefa9532e649
2 eval 'exec perl -wS $0 ${1+"$@"}'
3 if 0;
4 #*************************************************************************
6 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 #
8 # Copyright 2008 by Sun Microsystems, Inc.
10 # OpenOffice.org - a multi-platform office productivity suite
12 # $RCSfile: signing.pl,v $
14 # $Revision: 1.3 $
16 # This file is part of OpenOffice.org.
18 # OpenOffice.org is free software: you can redistribute it and/or modify
19 # it under the terms of the GNU Lesser General Public License version 3
20 # only, as published by the Free Software Foundation.
22 # OpenOffice.org is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU Lesser General Public License version 3 for more details
26 # (a copy is included in the LICENSE file that accompanied this code).
28 # You should have received a copy of the GNU Lesser General Public License
29 # version 3 along with OpenOffice.org. If not, see
30 # <http://www.openoffice.org/license.html>
31 # for a copy of the LGPLv3 License.
33 #*************************************************************************
35 use strict;
36 use Getopt::Long;
38 my $debug = 0;
39 my $max_files = 20; # sign $max_files with one command line
41 #### globals #####
42 my $myname = "";
43 my $opt_dir = "";
44 my $opt_exclude = ""; # file with a list of not signable dll and exe files
45 my $opt_verbose = 0;
46 my $opt_help = 0;
47 my $opt_log = ""; # for logging
48 my $opt_pass = ""; # password for signing
49 my $opt_pfxfile = ""; # Personal Information Exchange file
50 my $opt_timestamp_url = ""; # timestamp url
51 my %exclude_files = (); # list of not signable dll and exe files
52 my $signtool = "signtool.exe sign";
53 my @args = ();
54 my @files_to_sign = ();
56 #### main #####
57 $myname = script_id();
58 if ( $#ARGV < 2 ) {
59 usage();
60 exit(1);
62 @args = parse_options();
63 get_exclude_files();
64 @files_to_sign = get_files(\@args);
65 if ( $opt_log ) { # logging
66 open(LOG,">$opt_log") || die "Can't open log file $opt_log\n";
68 sign_files(\@files_to_sign);
69 close LOG if ($opt_log); # logging
70 exit 0;
73 #### subroutines ####
75 sub script_id
77 ( my $script_name = $0 ) =~ s/^.*[\\\/]([\w\.]+)$/$1/;
79 my $script_rev;
80 my $id_str = ' $Revision$ ';
81 $id_str =~ /Revision:\s+(\S+)\s+\$/
82 ? ($script_rev = $1) : ($script_rev = "-");
83 # print "\n$script_name -- version: $script_rev\n";
84 return $script_name;
87 ############################################################################
88 sub parse_options #09.07.2007 08:13
89 ############################################################################
91 # e exclude list file
92 # v verbose
93 my $success = GetOptions('h' => \$opt_help,
94 'd=s' => \$opt_dir, 'e=s'=>\$opt_exclude, 'f=s'=>\$opt_pfxfile, 'l=s'=>\$opt_log,
95 'p=s'=>\$opt_pass,'v'=>\$opt_verbose, 't=s'=>\$opt_timestamp_url);
96 if ( !$success || $opt_help ) {
97 usage();
98 exit(1);
100 if ( !$opt_exclude || !$opt_pfxfile || !$opt_pass || !$opt_timestamp_url) {
101 print "ERROR: Parameter missing!\n!";
102 usage();
103 exit(1);
105 return @ARGV;
106 } ##parse_options
108 ############################################################################
109 sub get_exclude_files #09.07.2007 10:12
110 ############################################################################
112 if ( -e $opt_exclude ) {
113 # get data from cache file
114 open( IN, "<$opt_exclude") || die "Can't open exclude file $opt_exclude\n";
115 while ( my $line = <IN> ) {
116 chomp($line);
117 $exclude_files{$line} = 1; # fill hash
118 print "$line - $exclude_files{$line}\n" if ($debug);
120 } else
122 print_error("Can't open $opt_exclude file!\n");
124 } ##get_exclude_files
126 ############################################################################
127 sub get_files #10.07.2007 10:19
128 ############################################################################
130 use File::Basename;
131 my $target = shift;
132 my $file_pattern;
133 my $file;
134 my @files = ();
135 print "\n";
136 foreach $file_pattern ( @$target )
138 print "Files: $file_pattern\n";
139 foreach $file ( glob( $file_pattern ) )
141 my $lib = File::Basename::basename $file;
142 if ( ! $exclude_files{$lib} ) {
143 push @files,$file;
145 else
147 print "exclude=$lib\n" if ($opt_verbose);
151 print "\n";
152 return @files;
153 } ##get_files
155 ############################################################################
156 sub sign_files #09.07.2007 10:36
157 ############################################################################
159 my $files_to_sign = shift;
160 my $commandline_base = ""; # contains whole stuff without the file name
161 my $file = "";
162 my $result = "";
164 print_error("Can't open PFX file: $opt_pfxfile\n") if ( ! -e $opt_pfxfile );
165 print_error("Password is empty\n") if ( !$opt_pass );
166 if ( $opt_pass =~ /\.exe$/ ) {
167 # get password by tool
168 open(PIPE, "$opt_pass 2>&1 |") || die "Can't open PIPE!\n";
169 my $pass = <PIPE>;
170 close PIPE;
171 print_error("Can't get password!\n") if ( !$pass ); # exit here
172 $opt_pass = $pass;
174 $signtool .= " -v" if ($opt_verbose);
175 $commandline_base = $signtool . " " . "-f $opt_pfxfile -p $opt_pass -t $opt_timestamp_url";
177 # Here switch between:
178 # one command line for muliple files (all doesn't work, too much) / for each file one command line
179 if ( $max_files > 1 ) {
180 exec_multi_sign($files_to_sign, $commandline_base);
181 } else
183 exec_single_sign($files_to_sign, $commandline_base);
185 } ##sign_files
187 ############################################################################
188 sub exec_single_sign #11.07.2007 09:05
189 ############################################################################
191 my $files_to_sign = shift;
192 my $commandline_base = shift; # contains whole stuff without the file name
193 my $file = "";
194 my $commandline = "";
196 foreach $file (@$files_to_sign)
198 $commandline = $commandline_base . " $file";
199 print "$commandline\n" if ($debug);
200 execute($commandline);
201 } #foreach
202 } ##exec_single_sign
204 ############################################################################
205 sub exec_multi_sign #11.07.2007 08:56
206 ############################################################################
208 # sign multiple file with one command line
209 my $files_to_sign = shift;
210 my $commandline_base = shift; # contains whole stuff without the file name
211 my $commandline = $commandline_base; # contains stuff which will be executed
212 my $file = "";
213 my $counter = 0;
215 foreach $file (@$files_to_sign)
217 $commandline .= " $file";
218 ++$counter;
219 if ( $counter >= $max_files ) {
220 execute($commandline);
221 $counter = 0; # reset counter
222 $commandline = $commandline_base; # reset command line
225 execute($commandline) if ($counter > 0);
226 } ##exec_multi_sign
228 ############################################################################
229 sub execute #11.07.2007 10:02
230 ############################################################################
232 my $commandline = shift;
233 my $result = "";
235 print "$commandline\n" if ($debug);
236 open(PIPE, "$commandline 2>&1 |") || die "Error: Cant open pipe!\n";
237 while ( $result = <PIPE> ) {
238 print LOG "$result" if ($opt_log); # logging
239 if ( $result =~ /SignTool Error\:/ ) {
240 close PIPE;
241 print_error( "$result\n" );
242 } # if error
243 } # while
244 close PIPE;
245 } ##execute
247 ############################################################################
248 sub print_error #09.07.2007 11:21
249 ############################################################################
251 my $text = shift;
252 print "ERROR: $text\n";
253 print LOG "ERROR: $text\n" if ($opt_log); # logging
254 close LOG if ($opt_log); # logging
255 exit(1);
256 } ##print_error
258 ############################################################################
259 sub usage #09.07.2007 08:39
260 ############################################################################
262 print "Usage:\t $myname <-e filename> <-f filename> <-p password> <-t timestamp> [-l filename] [-v] <file[list]> \n";
263 print "Options:\n";
264 print "\t -e filename\t\t\tFile which contains a list of files which don't have to be signed.\n";
265 print "Mandatory.\n";
266 print "\t -f pfx_filename\t\t\"Personal Information Exchange\" file. ";
267 print "Mandatory.\n";
268 print "\t -p password\t\t\tPassword for \"Personal Information Exchange\" file. Mandatory.\n";
269 print "\t -t timestamp\t\t\tTimestamp URL e.g. \"http://timestamp.verisign.com/scripts/timstamp.dll\"\n";
270 print "\t\t\t\t\tMandatory.\n";
271 print "\t -l log_filename\t\tFile for logging.\n";
272 print "\t -v\t\t\t\tVerbose.\n";
273 } ##usage