1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20#**************************************************************
21
22
23
24package installer::downloadsigner;
25
26use installer::exiter;
27use installer::files;
28use installer::globals;
29use installer::logger;
30use installer::pathanalyzer;
31
32############################################
33# Parameter Operations
34############################################
35
36sub usage
37{
38	print <<Ende;
39--------------------------------------------------------------------------------
40make_download V1.0
41The following parameter are needed:
42-d: Full path to the file containing the follow-me info or to a directory
43    containing the follow-me info files. In the latter case, all follow-me
44    info files are evaluated. If a directory is used, the successfully used
45    follow-me info files are renamed using a string "success". Files with
46    this string are ignored in repeated processes using "-d" with a
47    directory.
48
49The following parameter are optional:
50-nodownload: Only signing, no creation of download sets (Windows only)
51-useminor: Important for installation sets, created without minor set
52-writetotemp: Necessary, if you do not want to write into solver
53              This can be caused by missing privileges (Windows only)
54-internalcabinet: Not only the cabinet files are signed, but also all
55                  files included in the cabinet files (Windows only).
56
57-sign: Uses signing mechanism to sign installation sets
58If \"-sign\" is set, the following two parameter are required:
59-pfx: Full path to the pfx file
60-pw: Full path to the file, containing the pfx password.
61
62Examples:
63
64Specifying an installation set (with "-d"):
65
66perl make_download.pl -d <followmeinfofilename>
67
68perl make_download.pl -d <followmeinfofilename>
69                         -sign
70                         -pfx <pfxfilename>
71                         -pw <passwordfilename>
72
73or without specifying an installation set:
74
75perl make_download.pl -d <followmedirectory>
76                      -sign
77                      -pfx <pfxfilename>
78                      -pw <passwordfilename>
79--------------------------------------------------------------------------------
80Ende
81	exit(-1);
82}
83
84#####################################
85# Reading parameter
86#####################################
87
88sub getparameter
89{
90	while ( $#ARGV >= 0 )
91	{
92		my $param = shift(@ARGV);
93
94		if ($param eq "-d") { $installer::globals::followmeinfofilename = shift(@ARGV); }
95		elsif ($param eq "-pw") { $installer::globals::pwfile = shift(@ARGV); }
96		elsif ($param eq "-pfx") { $installer::globals::pfxfile = shift(@ARGV); }
97		elsif ($param eq "-sign") { $installer::globals::dosign = 1; }
98		elsif ($param eq "-nodownload") { $installer::globals::nodownload = 1; }
99		elsif ($param eq "-writetotemp") { $installer::globals::writetotemp = 1; }
100		elsif ($param eq "-useminor") { $installer::globals::useminor = 1; }
101		elsif ($param eq "-internalcabinet") { $installer::globals::internal_cabinet_signing = 1; }
102		else
103		{
104			installer::logger::print_error( "unknown parameter: $param" );
105			usage();
106			exit(-1);
107		}
108	}
109}
110
111#####################################
112# Controlling required parameter
113#####################################
114
115sub checkparameter
116{
117	if ( $installer::globals::followmeinfofilename eq "" )
118	{
119		installer::logger::print_error( "Error: Required parameter is missing: -d\n" );
120		usage();
121		exit(-1);
122	}
123
124	if ( $installer::globals::dosign )
125	{
126		# -pfx and -pw have to be set
127		if ( $installer::globals::pfxfile eq "" )
128		{
129			installer::logger::print_error( "Error: If \"-sign\" is set, a pfx file has to be specified: -pfx\n" );
130			usage();
131			exit(-1);
132		}
133
134		# -pfx and -pw have to be set
135		if ( $installer::globals::pwfile eq "" )
136		{
137			installer::logger::print_error( "Error: If \"-sign\" is set, a password file has to be specified: -pw\n" );
138			usage();
139			exit(-1);
140		}
141
142		# and both files have to exist
143		if ( ! -f $installer::globals::pfxfile )
144		{
145			installer::logger::print_error( "Error: pfx file \"$installer::globals::pfxfile\" does not exist.\n" );
146			usage();
147			exit(-1);
148		}
149
150		if ( ! -f $installer::globals::pwfile )
151		{
152			installer::logger::print_error( "Error: Password file \"$installer::globals::pwfile\" does not exist (-pw).\n" );
153			usage();
154			exit(-1);
155		}
156	}
157}
158
159#############################################
160# Setting the temporary path for the download
161# and signing process
162#############################################
163
164sub set_temp_path
165{
166	my $temppath = "";
167	my $pid = $$;			# process id
168	my $time = time();		# time
169	my $helperdir = "unpackdir_" . $pid . $time;
170
171	if (( $ENV{'TMP'} ) || ( $ENV{'TEMP'} ))
172	{
173		if ( $ENV{'TMP'} ) { $temppath = $ENV{'TMP'}; }
174		elsif ( $ENV{'TEMP'} )  { $temppath = $ENV{'TEMP'}; }
175		$temppath =~ s/\Q$installer::globals::separator\E\s*$//;	# removing ending slashes and backslashes
176		$temppath = $temppath . $installer::globals::separator . $helperdir;
177
178		if( $^O =~ /cygwin/i )
179		{
180			$temppath = qx{cygpath -w "$temppath"};
181			$temppath =~ s/\\/\//g;
182			$temppath =~ s/\s*$//g;
183		}
184
185		installer::systemactions::create_directory_structure($temppath);
186	}
187	else
188	{
189		installer::logger::print_error( "Error: TMP and TEMP not defined. This is required for this process.\n" );
190		usage();
191		exit(-1);
192	}
193
194	$installer::logger::Info->print("\n");
195	$installer::logger::Info->printf("\n... using output path: %s ...\n", $temppath);
196
197	push(@installer::globals::removedirs, $temppath);
198
199	return $temppath;
200}
201
202#############################################
203# Setting output pathes to temp directory
204# This are the:
205# unpackpath and the loggingdir
206#############################################
207
208sub set_output_pathes_to_temp
209{
210	my ($followmeinfohash, $temppath) = @_;
211
212	$followmeinfohash->{'loggingdir'} = $temppath . $installer::globals::separator;
213	$installer::globals::unpackpath = $temppath;
214}
215
216#############################################
217# Setting the minor into the pathes. This is
218# required, if the original installation set
219# was created without minor
220# Value is always saved in
221# $installer::globals::lastminor
222# which is saved in the follow_me file
223#############################################
224
225sub set_minor_into_pathes
226{
227	my ($followmeinfohash, $temppath) = @_;
228
229    $installer::logger::Info->print("\n");
230    $installer::logger::Info->printf("... forcing into minor: %s ...\n", $installer::globals::lastminor);
231
232	my @pathnames = ("bin", "doc", "inc", "lib", "pck", "res", "xml");
233	my $sourcename = "src";
234	my $srcpath = $installer::globals::separator . $sourcename . $installer::globals::separator;
235
236	if ( $installer::globals::minor ne "" )
237	{
238        $installer::logger::Info->print("\n");
239        $installer::logger::Info->printf("... already defined minor: %s -> ignoring parameter \"-useminor\" ...\n" , $installer::globals::minor);
240		return;
241	}
242
243	# Affected pathes:
244	# $contenthash{'installlogdir'}
245	# $contenthash{'includepatharray'}
246	# $installer::globals::unpackpath
247	# $installer::globals::idttemplatepath
248	# $installer::globals::idtlanguagepath
249
250	installer::logger::include_header_into_logfile("Changing saved pathes to add the minor");
251    $installer::logger::Lang->print("Old pathes:\n");
252    $installer::logger::Lang->printf("\$followmeinfohash->{'installlogdir'}: %s\n", $followmeinfohash->{'installlogdir'});
253    $installer::logger::Lang->printf("\$installer::globals::unpackpath: %s\n", $installer::globals::unpackpath);
254    $installer::logger::Lang->printf("\$installer::globals::idttemplatepath: %s\n", $installer::globals::idttemplatepath);
255    $installer::logger::Lang->printf("\$installer::globals::idtlanguagepath: %s\n", $installer::globals::idtlanguagepath);
256    $installer::logger::Lang->printf("Include pathes:\n");
257	foreach my $path ( @{$followmeinfohash->{'includepatharray'}} )
258    {
259        $installer::logger::Lang->print($path);
260    }
261
262	foreach $onepath ( @pathnames )
263	{
264		my $oldvalue = $installer::globals::separator . $onepath . $installer::globals::separator;
265		my $newvalue = $installer::globals::separator . $onepath . "\." . $installer::globals::lastminor . $installer::globals::separator;
266
267		if (( $followmeinfohash->{'installlogdir'} =~ /\Q$oldvalue\E/ ) && ( ! ( $followmeinfohash->{'installlogdir'} =~ /\Q$srcpath\E/ ))) { $followmeinfohash->{'installlogdir'} =~ s/\Q$oldvalue\E/$newvalue/; }
268		if (( $installer::globals::unpackpath =~ /\Q$oldvalue\E/ ) && ( ! ( $installer::globals::unpackpath =~ /\Q$srcpath\E/ ))) { $installer::globals::unpackpath =~ s/\Q$oldvalue\E/$newvalue/; }
269		if (( $installer::globals::idttemplatepath =~ /\Q$oldvalue\E/ ) && ( ! ( $installer::globals::idttemplatepath =~ /\Q$srcpath\E/ ))) { $installer::globals::idttemplatepath =~ s/\Q$oldvalue\E/$newvalue/; }
270		if (( $installer::globals::idtlanguagepath =~ /\Q$oldvalue\E/ ) && ( ! ( $installer::globals::idtlanguagepath =~ /\Q$srcpath\E/ ))) { $installer::globals::idtlanguagepath =~ s/\Q$oldvalue\E/$newvalue/; }
271		foreach my $path ( @{$followmeinfohash->{'includepatharray'}} ) { if (( $path =~ /\Q$oldvalue\E/ ) && ( ! ( $path =~ /\Q$srcpath\E/ ))) { $path =~ s/\Q$oldvalue\E/$newvalue/; } }
272
273		# Checking for the end of the path
274		$oldvalue = $installer::globals::separator . $onepath;
275		$newvalue = $installer::globals::separator . $onepath . "\." . $installer::globals::lastminor;
276
277		if (( $followmeinfohash->{'installlogdir'} =~ /\Q$oldvalue\E\s*$/ ) && ( ! ( $followmeinfohash->{'installlogdir'} =~ /\Q$srcpath\E/ ))) { $followmeinfohash->{'installlogdir'} =~ s/\Q$oldvalue\E\s*$/$newvalue/; }
278		if (( $installer::globals::unpackpath =~ /\Q$oldvalue\E\s*$/ ) && ( ! ( $installer::globals::unpackpath =~ /\Q$srcpath\E/ ))) { $installer::globals::unpackpath =~ s/\Q$oldvalue\E\s*$/$newvalue/; }
279		if (( $installer::globals::idttemplatepath =~ /\Q$oldvalue\E\s*$/ ) && ( ! ( $installer::globals::idttemplatepath =~ /\Q$srcpath\E/ ))) { $installer::globals::idttemplatepath =~ s/\Q$oldvalue\E\s*$/$newvalue/; }
280		if (( $installer::globals::idtlanguagepath =~ /\Q$oldvalue\E\s*$/ ) && ( ! ( $installer::globals::idtlanguagepath =~ /\Q$srcpath\E/ ))) { $installer::globals::idtlanguagepath =~ s/\Q$oldvalue\E\s*$/$newvalue/; }
281		foreach my $path ( @{$followmeinfohash->{'includepatharray'}} )
282		{
283			if (( $path =~ /\Q$oldvalue\E\s*$/ ) && ( ! ( $path =~ /\Q$srcpath\E/ )))
284			{
285				$path =~ s/\Q$oldvalue\E\s*$/$newvalue/;
286				$path = $path . "\n";
287			}
288		}
289	}
290
291	# And now can follow the replacement for the source path "src". Subdirs like "bin" in the source tree
292	# must not get the minor. This is instead "src.m9/instsetoo_native/common.pro/bin/..."
293	# Directory "src" can never be the end of the path
294
295	my $newsrcpath = $installer::globals::separator . $sourcename . "\." . $installer::globals::lastminor . $installer::globals::separator;
296
297	if ( $followmeinfohash->{'installlogdir'} =~ /\Q$srcpath\E/ ) { $followmeinfohash->{'installlogdir'} =~ s/\Q$srcpath\E/$newsrcpath/; }
298	if ( $installer::globals::unpackpath =~ /\Q$srcpath\E/ ) { $installer::globals::unpackpath =~ s/\Q$srcpath\E/$newsrcpath/; }
299	if ( $installer::globals::idttemplatepath =~ /\Q$srcpath\E/ ) { $installer::globals::idttemplatepath =~ s/\Q$srcpath\E/$newsrcpath/; }
300	if ( $installer::globals::idtlanguagepath =~ /\Q$srcpath\E/ ) { $installer::globals::idtlanguagepath =~ s/\Q$srcpath\E/$newsrcpath/; }
301	foreach my $path ( @{$followmeinfohash->{'includepatharray'}} ) { if ( $path =~ /\Q$srcpath\E/ ) { $path =~ s/\Q$srcpath\E/$newsrcpath/; } }
302
303    $installer::logger::Lang->print("\n");
304    $installer::logger::Lang->print("New pathes:\n");
305    $installer::logger::Lang->printf("\$followmeinfohash->{'installlogdir'}: %s\n", $followmeinfohash->{'installlogdir'});
306    $installer::logger::Lang->printf("\$installer::globals::unpackpath: %s\n", $installer::globals::unpackpath);
307    $installer::logger::Lang->printf("\$installer::globals::idttemplatepath: %s\n", $installer::globals::idttemplatepath);
308    $installer::logger::Lang->printf("\$installer::globals::idtlanguagepath: %s\n", $installer::globals::idtlanguagepath);
309    $installer::logger::Lang->printf("Include pathes:\n");
310	foreach my $path ( @{$followmeinfohash->{'includepatharray'}} )
311    {
312        $installer::logger::Lang->print($path);
313    }
314}
315
316#############################################
317# Setting the name of the log file
318#############################################
319
320sub setlogfilename
321{
322	if ( $installer::globals::dosign ) { $installer::globals::logfilename = "sign_and_download_" . $installer::globals::logfilename; }
323	else { $installer::globals::logfilename = "download_" . $installer::globals::logfilename; }
324	# reset the log file
325	@installer::globals::logfileinfo = ();
326}
327
328#########################################################
329# Checking, if this is a task in a cws or
330# on the master. Simple check of naming schema:
331# CWS: follow_me_DEV300_m40_de.log
332# Master: follow_me_4_DEV300_m40_en-US.log
333#########################################################
334
335sub check_cws_build
336{
337	my ( $filename ) = @_;
338
339	my $iscws = 1;
340
341	if ( $filename =~ /follow_me_\d+_/ ) { $iscws = 0; }
342	# if ( $filename =~ /log_\d+_/ ) { $iscws = 0; }
343
344	return $iscws;
345}
346
347#########################################################
348# Reading a specific key from a follow-me file
349#########################################################
350
351sub get_property_from_file
352{
353	my ($followmefile, $key) = @_;
354
355	my $value = "";
356
357	my $filecontent = installer::files::read_file($followmefile);
358
359	for ( my $i = 0; $i <= $#{$filecontent}; $i++ )
360	{
361		if ( ${$filecontent}[$i] =~ /^\s*\Q$key\E\s*\:\s*(.*?)\s*$/ )
362		{
363			$value = $1;
364			last;
365		}
366	}
367
368	return $value;
369}
370
371#########################################################
372# Publishing the content of the product list
373#########################################################
374
375sub publishproductlist
376{
377	my ($infofilelist) = @_;
378
379    $installer::logger::Info->print("\n");
380    $installer::logger::Info->printf("... found products: ...\n");
381
382	for ( my $i = 0; $i <= $#{$infofilelist}; $i++ )
383	{
384		my $onefile = ${$infofilelist}[$i];
385		installer::pathanalyzer::make_absolute_filename_to_relative_filename(\$onefile);
386        $installer::logger::Info->printf("...... $onefile ...\n");
387	}
388
389    $installer::logger::Info->printf("\n");
390}
391
392#########################################################
393# Filtering all files, that have correct minor
394# and work stamp.
395# Master: follow_me_4_DEV300_m40_en-US.log
396#########################################################
397
398sub filter_all_files_with_correct_settings
399{
400	my ($allfollowmefiles) = @_;
401
402	my @allfiles = ();
403	my @allfiles2 = ();
404	my $maxhash = ();
405
406	my $minor = "";
407	my $workstamp = "";
408
409	if ( $ENV{'WORK_STAMP'} ) { $workstamp = $ENV{'WORK_STAMP'}; }
410	if ( $ENV{'UPDMINOR'} ) { $minor = $ENV{'UPDMINOR'}; }
411
412	if ( $minor eq "" ) { installer::exiter::exit_program("ERROR: Environment variable \"UPDMINOR\" not set!", "filter_all_files_with_correct_settings"); }
413	if ( $workstamp eq "" ) { installer::exiter::exit_program("ERROR: Environment variable \"WORK_STAMP\" not set!", "filter_all_files_with_correct_settings"); }
414
415	foreach my $onefile ( @{$allfollowmefiles} )
416	{
417		if (( $onefile =~ /_\Q$minor\E_/i ) && ( $onefile =~ /_\Q$workstamp\E_/i ))
418		{
419			push(@allfiles, $onefile);
420
421			# also collecting maximum hash
422
423			if ( $onefile =~ /follow_me_(\d+)_\Q$workstamp\E_\Q$minor\E_([-\w]+)\.log\s*$/i )
424			{
425				my $sequence = $1;
426				my $lang = $2;
427
428				if (( ! exists($maxhash{$lang})) || ( $maxhash{$lang} < $sequence )) { $maxhash{$lang} = $sequence; }
429			}
430		}
431	}
432
433	# second run, because of sequence numbers
434
435	foreach my $onefile ( @allfiles )
436	{
437		if ( $onefile =~ /follow_me_(\d+)_\Q$workstamp\E_\Q$minor\E_([-\w]+)\.log\s*$/i )
438		{
439			my $sequence = $1;
440			my $lang = $2;
441
442			if ( $sequence == $maxhash{$lang} ) { push(@allfiles2, $onefile); }
443		}
444	}
445
446	return ( \@allfiles2 );
447}
448
449#########################################################
450# Creating a list of products, that need to be signed
451# or for which download sets need to be created.
452#########################################################
453
454sub createproductlist
455{
456	# If "-d" specifies an installation set, there is only one product
457
458	my @infofilelist = ();
459	my @infofilelist2 = ();
460
461	if ( -f $installer::globals::followmeinfofilename )
462	{
463		push(@infofilelist, $installer::globals::followmeinfofilename);
464		# Saving info, that this is a file
465		$installer::globals::followme_from_directory = 0;
466	}
467	elsif ( -d $installer::globals::followmeinfofilename )
468	{
469        $installer::logger::Info->printf("\n");
470        $installer::logger::Info->printf("... reading directory: %s ...\n", $installer::globals::followmeinfofilename);
471		$installer::globals::followmeinfofilename =~ s/$installer::globals::separator\s*$//;
472		my $allfollowmefiles = installer::systemactions::find_file_with_file_extension("log", $installer::globals::followmeinfofilename);
473
474		if ( ! ( $#{$allfollowmefiles} > -1 ))
475		{
476			installer::logger::print_error( "Error: Nothing to do! No follow-me file in directory \"$installer::globals::followmeinfofilename\"!.\n" );
477			usage();
478			exit(-1);
479		}
480
481		# Saving info, that this is a directory
482		$installer::globals::followme_from_directory = 1;
483
484		# Collect all possible installation sets
485		# CWS: All installation sets
486		# Master: All installation sets with same major, minor and buildid. Additionally using the highest number.
487
488		my $iscws = check_cws_build(${$allfollowmefiles}[0]);
489
490		if ( $iscws )
491		{
492			# Simply read all follow-me files and check existence of installation sets
493			foreach my $onefile ( @{$allfollowmefiles} )
494			{
495				my $fullfilename = $installer::globals::followmeinfofilename . $installer::globals::separator . $onefile;
496				my $installdir = get_property_from_file($fullfilename, "finalinstalldir");
497				if (( $installdir ne "" ) && ( -d $installdir )) { push(@infofilelist2, $fullfilename); }
498			}
499		}
500		else
501		{
502			$allfollowmefiles = filter_all_files_with_correct_settings($allfollowmefiles);
503
504			foreach my $onefile ( @{$allfollowmefiles} )
505			{
506				my $fullfilename = $installer::globals::followmeinfofilename . $installer::globals::separator . $onefile;
507				# Check, if installation set still exists
508				my $installdir = get_property_from_file($fullfilename, "finalinstalldir");
509				if (( $installdir ne "" ) && ( -d $installdir )) { push(@infofilelist2, $fullfilename); }
510			}
511		}
512
513		# Removing all files, starting with "follow_me_success_" in their names. This have already been used successfully.
514
515		foreach my $onefile ( @infofilelist2 )
516		{
517			if ( $onefile =~ /follow_me_success_/ ) { next; }
518			push(@infofilelist, $onefile);
519		}
520
521		# Checking, if there is content in the list
522		if ( ! ( $#infofilelist > -1 ))
523		{
524			installer::logger::print_error( "Error: Nothing to do! No installation set found for follow-me files in directory \"$installer::globals::followmeinfofilename\"!.\n" );
525			usage();
526			exit(-1);
527		}
528	}
529	else
530	{
531		installer::logger::print_error( "Error: Nothing to do! \"$installer::globals::followmeinfofilename\" is no file and no directory (-d).\n" );
532		usage();
533		exit(-1);
534	}
535
536	return \@infofilelist;
537}
538
539#############################################
540# Logging the content of the download hash
541#############################################
542
543sub logfollowmeinfohash
544{
545	my ( $followmehash ) = @_;
546
547    $installer::logger::Info->printf("\n");
548    $installer::logger::Info->printf("*****************************************\n");
549    $installer::logger::Info->printf("Content of follow-me info file:\n");
550    $installer::logger::Info->printf("finalinstalldir: %s\n", $followmehash->{'finalinstalldir'});
551    $installer::logger::Info->printf("downloadname: %s\n", $followmehash->{'downloadname'});
552    $installer::logger::Info->printf("languagestring: %s\n", $followmehash->{'languagestring'});
553	foreach my $lang ( @{$followmehash->{'languagesarray'}} )
554    {
555        $installer::logger::Info->printf("languagesarray: %s\n", $lang);
556    }
557	foreach my $path ( @{$followmehash->{'includepatharray'}} )
558    {
559        $installer::logger::Info->printf("includepatharray: %s\n", $path);
560    }
561	foreach my $key ( sort keys %{$followmehash->{'allvariableshash'}} )
562    {
563        $installer::logger::Info->printf("allvariableshash: %s : %s\n",
564            $key,
565            $followmehash->{'allvariableshash'}->{$key});
566    }
567}
568
569########################################################################
570# Renaming the follow me info file, if it was successfully used.
571# This can only be done, if the parameter "-d" was used with a
572# directory, not a name. In this case the repeated use of parameter
573# "-d" with this directory has to ignore this already successfully
574# used file.
575########################################################################
576
577sub rename_followme_infofile
578{
579	my ( $filename ) = @_;
580
581	my $newfilename = $filename;
582	$newfilename =~ s/follow_me_/follow_me_success_/;	# including "_success" after "follow_me"
583
584	if ( $filename ne $newfilename )
585	{
586		my $returnvalue = rename($filename, $newfilename);
587		if ( $returnvalue )
588        {
589            $installer::logger::Info->printf("\n");
590            $installer::logger::Info->printf("... renamed file \"%s\" to \"%s\" ...\n", $filename, $newfilename);
591        }
592	}
593}
594
5951;
596