xref: /trunk/main/postprocess/signing/signing.pl (revision c625247e)
1cdf0e10cSrcweir:
2cdf0e10cSrcweireval 'exec perl -wS $0 ${1+"$@"}'
3cdf0e10cSrcweir    if 0;
47e90fac2SAndrew Rist#**************************************************************
57e90fac2SAndrew Rist#
67e90fac2SAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
77e90fac2SAndrew Rist#  or more contributor license agreements.  See the NOTICE file
87e90fac2SAndrew Rist#  distributed with this work for additional information
97e90fac2SAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
107e90fac2SAndrew Rist#  to you under the Apache License, Version 2.0 (the
117e90fac2SAndrew Rist#  "License"); you may not use this file except in compliance
127e90fac2SAndrew Rist#  with the License.  You may obtain a copy of the License at
137e90fac2SAndrew Rist#
147e90fac2SAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
157e90fac2SAndrew Rist#
167e90fac2SAndrew Rist#  Unless required by applicable law or agreed to in writing,
177e90fac2SAndrew Rist#  software distributed under the License is distributed on an
187e90fac2SAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
197e90fac2SAndrew Rist#  KIND, either express or implied.  See the License for the
207e90fac2SAndrew Rist#  specific language governing permissions and limitations
217e90fac2SAndrew Rist#  under the License.
227e90fac2SAndrew Rist#
237e90fac2SAndrew Rist#**************************************************************
247e90fac2SAndrew Rist
257e90fac2SAndrew Rist
26cdf0e10cSrcweir
27cdf0e10cSrcweiruse strict;
28cdf0e10cSrcweiruse Getopt::Long;
29cdf0e10cSrcweir
30cdf0e10cSrcweirmy $debug = 0;
31cdf0e10cSrcweirmy $max_files = 20; 		  # sign $max_files with one command line
32cdf0e10cSrcweir
33cdf0e10cSrcweir#### globals #####
34cdf0e10cSrcweirmy $myname 		= "";
35cdf0e10cSrcweirmy $opt_dir 	= "";
36cdf0e10cSrcweirmy $opt_exclude = "";         # file with a list of not signable dll and exe files
37cdf0e10cSrcweirmy $opt_verbose = 0;
38cdf0e10cSrcweirmy $opt_help	= 0;
39cdf0e10cSrcweirmy $opt_log		= "";		  # for logging
40cdf0e10cSrcweirmy $opt_pass	= "";         # password for signing
41cdf0e10cSrcweirmy $opt_pfxfile = "";		  # Personal Information Exchange file
42cdf0e10cSrcweirmy $opt_timestamp_url = "";   # timestamp url
43cdf0e10cSrcweirmy %exclude_files = ();  	  # list of not signable dll and exe files
44cdf0e10cSrcweirmy $signtool    = "signtool.exe sign";
45cdf0e10cSrcweirmy @args		= ();
46cdf0e10cSrcweirmy @files_to_sign = ();
47cdf0e10cSrcweir
48cdf0e10cSrcweir#### main #####
49cdf0e10cSrcweir$myname = script_id();
50cdf0e10cSrcweirif ( $#ARGV < 2 ) {
51cdf0e10cSrcweir	usage();
52cdf0e10cSrcweir    exit(1);
53cdf0e10cSrcweir}
54cdf0e10cSrcweir@args = parse_options();
55cdf0e10cSrcweirget_exclude_files();
56cdf0e10cSrcweir@files_to_sign = get_files(\@args);
57cdf0e10cSrcweirif ( $opt_log ) {               # logging
58cdf0e10cSrcweir	open(LOG,">$opt_log") || die "Can't open log file $opt_log\n";
59cdf0e10cSrcweir}
60cdf0e10cSrcweirsign_files(\@files_to_sign);
61cdf0e10cSrcweirclose LOG if ($opt_log);        # logging
62cdf0e10cSrcweirexit 0;
63cdf0e10cSrcweir
64cdf0e10cSrcweir
65cdf0e10cSrcweir#### subroutines ####
66cdf0e10cSrcweir
67cdf0e10cSrcweirsub script_id
68cdf0e10cSrcweir{
69cdf0e10cSrcweir    ( my $script_name = $0 ) =~ s/^.*[\\\/]([\w\.]+)$/$1/;
70cdf0e10cSrcweir
71cdf0e10cSrcweir    my $script_rev;
72cdf0e10cSrcweir    my $id_str = ' $Revision$ ';
73cdf0e10cSrcweir    $id_str =~ /Revision:\s+(\S+)\s+\$/
74cdf0e10cSrcweir      ? ($script_rev = $1) : ($script_rev = "-");
75cdf0e10cSrcweir#    print "\n$script_name -- version: $script_rev\n";
76cdf0e10cSrcweir    return $script_name;
77cdf0e10cSrcweir}
78cdf0e10cSrcweir
79cdf0e10cSrcweir############################################################################
80cdf0e10cSrcweirsub parse_options		#09.07.2007 08:13
81cdf0e10cSrcweir############################################################################
82cdf0e10cSrcweir{
83cdf0e10cSrcweir	# e exclude list file
84cdf0e10cSrcweir	# v verbose
85*c625247eSAndre Fischer    my $filelist_filename = undef;
86cdf0e10cSrcweir	my $success = GetOptions('h' => \$opt_help,
87cdf0e10cSrcweir         'd=s' => \$opt_dir, 'e=s'=>\$opt_exclude, 'f=s'=>\$opt_pfxfile, 'l=s'=>\$opt_log,
88*c625247eSAndre Fischer		 'p=s'=>\$opt_pass,'v'=>\$opt_verbose, 't=s'=>\$opt_timestamp_url, 'i=s'=>\$filelist_filename);
89cdf0e10cSrcweir    if ( !$success || $opt_help ) {
90cdf0e10cSrcweir        usage();
91cdf0e10cSrcweir        exit(1);
92cdf0e10cSrcweir    }
93cdf0e10cSrcweir	if ( !$opt_exclude || !$opt_pfxfile || !$opt_pass || !$opt_timestamp_url) {
94cdf0e10cSrcweir		print "ERROR: Parameter missing!\n!";
95cdf0e10cSrcweir        usage();
96cdf0e10cSrcweir        exit(1);
97cdf0e10cSrcweir	}
98*c625247eSAndre Fischer
99*c625247eSAndre Fischer    # Read the names of files to sign from the given file.
100*c625247eSAndre Fischer    die "no list of files given" unless defined $filelist_filename;
101*c625247eSAndre Fischer    open my $in, $filelist_filename;
102*c625247eSAndre Fischer    my @filelist = ();
103*c625247eSAndre Fischer    while (<$in>)
104*c625247eSAndre Fischer    {
105*c625247eSAndre Fischer        chomp($_);
106*c625247eSAndre Fischer        push @filelist, $_;
107*c625247eSAndre Fischer    }
108*c625247eSAndre Fischer	return @filelist;
109cdf0e10cSrcweir}	##parse_options
110cdf0e10cSrcweir
111cdf0e10cSrcweir############################################################################
112cdf0e10cSrcweirsub get_exclude_files		#09.07.2007 10:12
113cdf0e10cSrcweir############################################################################
114cdf0e10cSrcweir{
115cdf0e10cSrcweir	if ( -e $opt_exclude ) {
116cdf0e10cSrcweir            # get data from cache file
117cdf0e10cSrcweir            open( IN, "<$opt_exclude") || die "Can't open exclude file $opt_exclude\n";
118cdf0e10cSrcweir            while ( my $line = <IN> ) {
119cdf0e10cSrcweir			chomp($line);
120cdf0e10cSrcweir			$exclude_files{$line} = 1;			# fill hash
121cdf0e10cSrcweir			print "$line - $exclude_files{$line}\n" if ($debug);
122cdf0e10cSrcweir            }
123cdf0e10cSrcweir        } else
124cdf0e10cSrcweir        {
125cdf0e10cSrcweir			print_error("Can't open $opt_exclude file!\n");
126cdf0e10cSrcweir		}
127cdf0e10cSrcweir}	##get_exclude_files
128cdf0e10cSrcweir
129cdf0e10cSrcweir############################################################################
130cdf0e10cSrcweirsub get_files		#10.07.2007 10:19
131cdf0e10cSrcweir############################################################################
132cdf0e10cSrcweir {
133cdf0e10cSrcweir	use File::Basename;
134cdf0e10cSrcweir    my $target = shift;
135cdf0e10cSrcweir	my $file_pattern;
136cdf0e10cSrcweir	my $file;
137cdf0e10cSrcweir	my @files = ();
138cdf0e10cSrcweir	print "\n";
139cdf0e10cSrcweir	foreach $file_pattern ( @$target )
140cdf0e10cSrcweir	{
141cdf0e10cSrcweir		print "Files: $file_pattern\n";
142cdf0e10cSrcweir        foreach $file ( glob( $file_pattern ) )
143cdf0e10cSrcweir		{
144cdf0e10cSrcweir            my $lib = File::Basename::basename $file;
145cdf0e10cSrcweir			if ( ! $exclude_files{$lib} ) {
146cdf0e10cSrcweir				push @files,$file;
147cdf0e10cSrcweir			}
148cdf0e10cSrcweir			else
149cdf0e10cSrcweir			{
150cdf0e10cSrcweir				print "exclude=$lib\n" if ($opt_verbose);
151cdf0e10cSrcweir			}
152cdf0e10cSrcweir		}
153cdf0e10cSrcweir	}
154cdf0e10cSrcweir	print "\n";
155cdf0e10cSrcweir	return @files;
156cdf0e10cSrcweir}	##get_files
157cdf0e10cSrcweir
158cdf0e10cSrcweir############################################################################
159cdf0e10cSrcweirsub sign_files		#09.07.2007 10:36
160cdf0e10cSrcweir############################################################################
161cdf0e10cSrcweir{
162cdf0e10cSrcweir	my $files_to_sign = shift;
163cdf0e10cSrcweir	my $commandline_base = ""; # contains whole stuff without the file name
164cdf0e10cSrcweir	my $file = "";
165cdf0e10cSrcweir	my $result = "";
166cdf0e10cSrcweir
167cdf0e10cSrcweir	print_error("Can't open PFX file: $opt_pfxfile\n") if ( ! -e $opt_pfxfile );
168cdf0e10cSrcweir	print_error("Password is empty\n") if ( !$opt_pass );
169cdf0e10cSrcweir	if ( $opt_pass =~ /\.exe$/ ) {
170cdf0e10cSrcweir		# get password by tool
171cdf0e10cSrcweir		open(PIPE, "$opt_pass 2>&1 |") || die "Can't open PIPE!\n";
172cdf0e10cSrcweir		my $pass = <PIPE>;
173cdf0e10cSrcweir		close PIPE;
174cdf0e10cSrcweir		print_error("Can't get password!\n") if ( !$pass ); # exit here
175cdf0e10cSrcweir		$opt_pass = $pass;
176cdf0e10cSrcweir	}
177cdf0e10cSrcweir	$signtool .= " -v" if ($opt_verbose);
178cdf0e10cSrcweir	$commandline_base = $signtool . " " . "-f $opt_pfxfile -p $opt_pass -t $opt_timestamp_url";
179cdf0e10cSrcweir
180cdf0e10cSrcweir	# Here switch between:
181cdf0e10cSrcweir	# one command line for muliple files (all doesn't work, too much) / for each file one command line
182cdf0e10cSrcweir	if ( $max_files > 1 ) {
183cdf0e10cSrcweir		exec_multi_sign($files_to_sign, $commandline_base);
184cdf0e10cSrcweir	} else
185cdf0e10cSrcweir	{
186cdf0e10cSrcweir		exec_single_sign($files_to_sign, $commandline_base);
187cdf0e10cSrcweir	}
188cdf0e10cSrcweir}	##sign_files
189cdf0e10cSrcweir
190cdf0e10cSrcweir############################################################################
191cdf0e10cSrcweirsub exec_single_sign		#11.07.2007 09:05
192cdf0e10cSrcweir############################################################################
193cdf0e10cSrcweir{
194cdf0e10cSrcweir	my $files_to_sign    = shift;
195cdf0e10cSrcweir	my $commandline_base = shift; 				  # contains whole stuff without the file name
196cdf0e10cSrcweir	my $file = "";
197cdf0e10cSrcweir	my $commandline = "";
198cdf0e10cSrcweir
199cdf0e10cSrcweir	foreach $file (@$files_to_sign)
200cdf0e10cSrcweir	{
201cdf0e10cSrcweir		$commandline = $commandline_base . " $file";
202cdf0e10cSrcweir		print "$commandline\n" if ($debug);
203cdf0e10cSrcweir		execute($commandline);
204cdf0e10cSrcweir	} #foreach
205cdf0e10cSrcweir}	##exec_single_sign
206cdf0e10cSrcweir
207cdf0e10cSrcweir############################################################################
208cdf0e10cSrcweirsub exec_multi_sign		#11.07.2007 08:56
209cdf0e10cSrcweir############################################################################
210cdf0e10cSrcweir {
211cdf0e10cSrcweir	# sign multiple file with one command line
212cdf0e10cSrcweir	my $files_to_sign    = shift;
213cdf0e10cSrcweir	my $commandline_base = shift; 				  # contains whole stuff without the file name
214cdf0e10cSrcweir	my $commandline = $commandline_base;	      # contains stuff which will be executed
215cdf0e10cSrcweir	my $file = "";
216cdf0e10cSrcweir	my $counter = 0;
217cdf0e10cSrcweir
218cdf0e10cSrcweir	foreach $file (@$files_to_sign)
219cdf0e10cSrcweir	{
220cdf0e10cSrcweir		$commandline .= " $file";
221cdf0e10cSrcweir		++$counter;
222cdf0e10cSrcweir		if ( $counter >= $max_files ) {
223cdf0e10cSrcweir			execute($commandline);
224cdf0e10cSrcweir			$counter = 0;						 # reset counter
225cdf0e10cSrcweir			$commandline = $commandline_base;    # reset command line
226cdf0e10cSrcweir		}
227cdf0e10cSrcweir	}
228cdf0e10cSrcweir	execute($commandline) if ($counter > 0);
229cdf0e10cSrcweir}	##exec_multi_sign
230cdf0e10cSrcweir
231cdf0e10cSrcweir############################################################################
232cdf0e10cSrcweirsub execute		#11.07.2007 10:02
233cdf0e10cSrcweir############################################################################
234cdf0e10cSrcweir{
235cdf0e10cSrcweir	my $commandline = shift;
236cdf0e10cSrcweir	my $result = "";
237cdf0e10cSrcweir
238cdf0e10cSrcweir  	print "$commandline\n" if ($debug);
239cdf0e10cSrcweir  	open(PIPE, "$commandline 2>&1 |") || die "Error: Cant open pipe!\n";
240cdf0e10cSrcweir  	while ( $result = <PIPE> ) {
241cdf0e10cSrcweir  		print LOG "$result" if ($opt_log);        # logging
242cdf0e10cSrcweir  		if ( $result =~ /SignTool Error\:/ ) {
243cdf0e10cSrcweir			close PIPE;
244cdf0e10cSrcweir  			print_error( "$result\n" );
245cdf0e10cSrcweir  		} # if error
246cdf0e10cSrcweir  	} # while
247cdf0e10cSrcweir  	close PIPE;
248cdf0e10cSrcweir}	##execute
249cdf0e10cSrcweir
250cdf0e10cSrcweir############################################################################
251cdf0e10cSrcweirsub print_error		#09.07.2007 11:21
252cdf0e10cSrcweir############################################################################
253cdf0e10cSrcweir {
254cdf0e10cSrcweir	my $text = shift;
255cdf0e10cSrcweir	print "ERROR: $text\n";
256cdf0e10cSrcweir	print LOG "ERROR: $text\n" if ($opt_log);        # logging
257cdf0e10cSrcweir	close LOG if ($opt_log);        				 # logging
258cdf0e10cSrcweir	exit(1);
259cdf0e10cSrcweir}	##print_error
260cdf0e10cSrcweir
261cdf0e10cSrcweir############################################################################
262cdf0e10cSrcweirsub usage		#09.07.2007 08:39
263cdf0e10cSrcweir############################################################################
264cdf0e10cSrcweir {
265*c625247eSAndre Fischer	print "Usage:\t $myname <-e filename> <-f filename> <-p password> <-t timestamp> <-i filename> [-l filename] [-v]\n";
266cdf0e10cSrcweir    print "Options:\n";
267cdf0e10cSrcweir	print "\t -e filename\t\t\tFile which contains a list of files which don't have to be signed.\n";
268cdf0e10cSrcweir    print                            "Mandatory.\n";
269cdf0e10cSrcweir    print "\t -f pfx_filename\t\t\"Personal Information Exchange\" file. ";
270cdf0e10cSrcweir    print                            "Mandatory.\n";
271cdf0e10cSrcweir    print "\t -p password\t\t\tPassword for \"Personal Information Exchange\" file. Mandatory.\n";
272cdf0e10cSrcweir    print "\t -t timestamp\t\t\tTimestamp URL e.g. \"http://timestamp.verisign.com/scripts/timstamp.dll\"\n";
273*c625247eSAndre Fischer    print "\t -i filename\t\t\tName of the file that contains the names of files to sign.\"\n";
274cdf0e10cSrcweir    print "\t\t\t\t\tMandatory.\n";
275cdf0e10cSrcweir	print "\t -l log_filename\t\tFile for logging.\n";
276cdf0e10cSrcweir    print "\t -v\t\t\t\tVerbose.\n";
277cdf0e10cSrcweir}	##usage
278