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