xref: /trunk/main/solenv/bin/modules/installer/windows/sign.pm (revision 8ac570c6d50c1ad9896c5c4a0f44668dbe040aef)
19780544fSAndrew Rist#**************************************************************
2cdf0e10cSrcweir#
39780544fSAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
49780544fSAndrew Rist#  or more contributor license agreements.  See the NOTICE file
59780544fSAndrew Rist#  distributed with this work for additional information
69780544fSAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
79780544fSAndrew Rist#  to you under the Apache License, Version 2.0 (the
89780544fSAndrew Rist#  "License"); you may not use this file except in compliance
99780544fSAndrew Rist#  with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir#
119780544fSAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir#
139780544fSAndrew Rist#  Unless required by applicable law or agreed to in writing,
149780544fSAndrew Rist#  software distributed under the License is distributed on an
159780544fSAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169780544fSAndrew Rist#  KIND, either express or implied.  See the License for the
179780544fSAndrew Rist#  specific language governing permissions and limitations
189780544fSAndrew Rist#  under the License.
19cdf0e10cSrcweir#
209780544fSAndrew Rist#**************************************************************
219780544fSAndrew Rist
22cdf0e10cSrcweirpackage installer::windows::sign;
23cdf0e10cSrcweir
24cdf0e10cSrcweiruse Cwd;
25cdf0e10cSrcweiruse installer::converter;
26cdf0e10cSrcweiruse installer::existence;
27cdf0e10cSrcweiruse installer::files;
28cdf0e10cSrcweiruse installer::globals;
29cdf0e10cSrcweiruse installer::scriptitems;
30cdf0e10cSrcweiruse installer::worker;
31cdf0e10cSrcweiruse installer::windows::admin;
32cdf0e10cSrcweir
33cdf0e10cSrcweir########################################################
34cdf0e10cSrcweir# Copying an existing Windows installation set.
35cdf0e10cSrcweir########################################################
36cdf0e10cSrcweir
37cdf0e10cSrcweirsub copy_install_set
38cdf0e10cSrcweir{
39cdf0e10cSrcweir    my ( $installsetpath ) = @_;
40cdf0e10cSrcweir
41cdf0e10cSrcweir    installer::logger::include_header_into_logfile("Start: Copying installation set $installsetpath");
42cdf0e10cSrcweir
43cdf0e10cSrcweir    my $infoline = "";
44cdf0e10cSrcweir
45cdf0e10cSrcweir    my $dirname = $installsetpath;
46cdf0e10cSrcweir    installer::pathanalyzer::make_absolute_filename_to_relative_filename(\$dirname);
47cdf0e10cSrcweir
48cdf0e10cSrcweir    my $path = $installsetpath;
49cdf0e10cSrcweir    installer::pathanalyzer::get_path_from_fullqualifiedname(\$path);
50cdf0e10cSrcweir
51cdf0e10cSrcweir    $path =~ s/\Q$installer::globals::separator\E\s*$//;
52cdf0e10cSrcweir
53cdf0e10cSrcweir    if ( $dirname =~ /\./ ) { $dirname =~ s/\./_signed_inprogress./; }
54cdf0e10cSrcweir    else { $dirname = $dirname . "_signed_inprogress"; }
55cdf0e10cSrcweir
56cdf0e10cSrcweir    my $newpath = $path . $installer::globals::separator . $dirname;
57cdf0e10cSrcweir    my $removepath = $newpath;
58cdf0e10cSrcweir    $removepath =~ s/_inprogress/_witherror/;
59cdf0e10cSrcweir
60cdf0e10cSrcweir    if ( -d $newpath ) { installer::systemactions::remove_complete_directory($newpath, 1); }
61cdf0e10cSrcweir    if ( -d $removepath ) { installer::systemactions::remove_complete_directory($removepath, 1); }
62cdf0e10cSrcweir
63cdf0e10cSrcweir    $infoline = "Copy installation set from $installsetpath to $newpath\n";
64b274bc22SAndre Fischer    $installer::logger::Lang->print($infoline);
65cdf0e10cSrcweir
66cdf0e10cSrcweir    $installsetpath = installer::systemactions::copy_complete_directory($installsetpath, $newpath);
67cdf0e10cSrcweir
68cdf0e10cSrcweir    installer::logger::include_header_into_logfile("End: Copying installation set $installsetpath");
69cdf0e10cSrcweir
70cdf0e10cSrcweir    return $newpath;
71cdf0e10cSrcweir}
72cdf0e10cSrcweir
73cdf0e10cSrcweir########################################################
74cdf0e10cSrcweir# Renaming an existing Windows installation set.
75cdf0e10cSrcweir########################################################
76cdf0e10cSrcweir
77cdf0e10cSrcweirsub rename_install_set
78cdf0e10cSrcweir{
79cdf0e10cSrcweir    my ( $installsetpath ) = @_;
80cdf0e10cSrcweir
81cdf0e10cSrcweir    my $infoline = "";
82cdf0e10cSrcweir
83cdf0e10cSrcweir    my $dirname = $installsetpath;
84cdf0e10cSrcweir    installer::pathanalyzer::make_absolute_filename_to_relative_filename(\$dirname);
85cdf0e10cSrcweir
86cdf0e10cSrcweir    my $path = $installsetpath;
87cdf0e10cSrcweir    installer::pathanalyzer::get_path_from_fullqualifiedname(\$path);
88cdf0e10cSrcweir
89cdf0e10cSrcweir    $path =~ s/\Q$installer::globals::separator\E\s*$//;
90cdf0e10cSrcweir
91cdf0e10cSrcweir    if ( $dirname =~ /\./ ) { $dirname =~ s/\./_inprogress./; }
92cdf0e10cSrcweir    else { $dirname = $dirname . "_inprogress"; }
93cdf0e10cSrcweir
94cdf0e10cSrcweir    my $newpath = $path . $installer::globals::separator . $dirname;
95cdf0e10cSrcweir    my $removepath = $newpath;
96cdf0e10cSrcweir    $removepath =~ s/_inprogress/_witherror/;
97cdf0e10cSrcweir
98cdf0e10cSrcweir    if ( -d $newpath ) { installer::systemactions::remove_complete_directory($newpath, 1); }
99cdf0e10cSrcweir    if ( -d $removepath ) { installer::systemactions::remove_complete_directory($removepath, 1); }
100cdf0e10cSrcweir
101cdf0e10cSrcweir    $installsetpath = installer::systemactions::rename_directory($installsetpath, $newpath);
102cdf0e10cSrcweir
103cdf0e10cSrcweir    return $newpath;
104cdf0e10cSrcweir}
105cdf0e10cSrcweir
106cdf0e10cSrcweir#########################################################
107cdf0e10cSrcweir# Checking the local system
108cdf0e10cSrcweir# Checking existence of needed files in include path
109cdf0e10cSrcweir#########################################################
110cdf0e10cSrcweir
111cdf0e10cSrcweirsub check_system_path
112cdf0e10cSrcweir{
113cdf0e10cSrcweir    # The following files have to be found in the environment variable PATH
114cdf0e10cSrcweir    # Only, if \"-sign\" is used.
115cdf0e10cSrcweir    # Windows : "msicert.exe", "diff.exe", "msidb.exe", "signtool.exe"
116cdf0e10cSrcweir
117cdf0e10cSrcweir    my @needed_files_in_path = ("msicert.exe", "msidb.exe", "signtool.exe", "diff.exe");
118cdf0e10cSrcweir    if ( $installer::globals::internal_cabinet_signing )
119cdf0e10cSrcweir    {
120cdf0e10cSrcweir        push(@needed_files_in_path, "cabarc.exe");
121cdf0e10cSrcweir        push(@needed_files_in_path, "makecab.exe");
122cdf0e10cSrcweir    }
123cdf0e10cSrcweir
124cdf0e10cSrcweir    my $onefile;
125cdf0e10cSrcweir    my $error = 0;
126cdf0e10cSrcweir    my $pathvariable = $ENV{'PATH'};
127cdf0e10cSrcweir    my $local_pathseparator = $installer::globals::pathseparator;
128cdf0e10cSrcweir
129cdf0e10cSrcweir    if( $^O =~ /cygwin/i )
130*8ac570c6Smseidel    {   # When using Cygwin's perl the PATH variable is POSIX style and ...
131cdf0e10cSrcweir        $pathvariable = qx{cygpath -mp "$pathvariable"} ;
132cdf0e10cSrcweir        # has to be converted to DOS style for further use.
133cdf0e10cSrcweir        $local_pathseparator = ';';
134cdf0e10cSrcweir    }
135cdf0e10cSrcweir
136cdf0e10cSrcweir    my $patharrayref = installer::converter::convert_stringlist_into_array(\$pathvariable, $local_pathseparator);
137cdf0e10cSrcweir
138cdf0e10cSrcweir    $installer::globals::patharray = $patharrayref;
139cdf0e10cSrcweir
140cdf0e10cSrcweir    foreach my $onefile ( @needed_files_in_path )
141cdf0e10cSrcweir    {
142b274bc22SAndre Fischer
143b274bc22SAndre Fischer        $installer::logger::Info->printf("...... searching %s ...\n", $onefile);
144cdf0e10cSrcweir
145cdf0e10cSrcweir        my $fileref = installer::scriptitems::get_sourcepath_from_filename_and_includepath_classic(\$onefile, $patharrayref , 0);
146cdf0e10cSrcweir
147cdf0e10cSrcweir        if ( $$fileref eq "" )
148cdf0e10cSrcweir        {
149cdf0e10cSrcweir            $error = 1;
150cdf0e10cSrcweir            installer::logger::print_error( "$onefile not found\n" );
151cdf0e10cSrcweir        }
152cdf0e10cSrcweir        else
153cdf0e10cSrcweir        {
154b274bc22SAndre Fischer            $installer::logger::Info->printf("\tFound: %s\n", $$fileref);
155cdf0e10cSrcweir        }
156cdf0e10cSrcweir    }
157cdf0e10cSrcweir
158cdf0e10cSrcweir    $installer::globals::signfiles_checked = 1;
159cdf0e10cSrcweir
160cdf0e10cSrcweir    if ( $error ) { installer::exiter::exit_program("ERROR: Could not find all needed files in path!", "check_system_path"); }
161cdf0e10cSrcweir}
162cdf0e10cSrcweir
163cdf0e10cSrcweir######################################################
164cdf0e10cSrcweir# Making systemcall
165cdf0e10cSrcweir######################################################
166cdf0e10cSrcweir
167cdf0e10cSrcweirsub make_systemcall
168cdf0e10cSrcweir{
169cdf0e10cSrcweir    my ($systemcall, $displaysystemcall) = @_;
170cdf0e10cSrcweir
171b274bc22SAndre Fischer    $installer::logger::Info->printf("... %s ...\n", $displaysystemcall);
172cdf0e10cSrcweir
173cdf0e10cSrcweir    my $success = 1;
174cdf0e10cSrcweir    my $returnvalue = system($systemcall);
175cdf0e10cSrcweir
176cdf0e10cSrcweir    my $infoline = "Systemcall: $displaysystemcall\n";
177b274bc22SAndre Fischer    $installer::logger::Lang->print($infoline);
178cdf0e10cSrcweir
179cdf0e10cSrcweir    if ($returnvalue)
180cdf0e10cSrcweir    {
181cdf0e10cSrcweir        $infoline = "ERROR: Could not execute \"$displaysystemcall\"!\n";
182b274bc22SAndre Fischer        $installer::logger::Lang->print($infoline);
183cdf0e10cSrcweir        $success = 0;
184cdf0e10cSrcweir    }
185cdf0e10cSrcweir    else
186cdf0e10cSrcweir    {
187cdf0e10cSrcweir        $infoline = "Success: Executed \"$displaysystemcall\" successfully!\n";
188b274bc22SAndre Fischer        $installer::logger::Lang->print($infoline);
189cdf0e10cSrcweir    }
190cdf0e10cSrcweir
191cdf0e10cSrcweir    return $success;
192cdf0e10cSrcweir}
193cdf0e10cSrcweir
194cdf0e10cSrcweir######################################################
195cdf0e10cSrcweir# Making systemcall with warning
196cdf0e10cSrcweir######################################################
197cdf0e10cSrcweir
198cdf0e10cSrcweirsub make_systemcall_with_warning
199cdf0e10cSrcweir{
200cdf0e10cSrcweir    my ($systemcall, $displaysystemcall) = @_;
201cdf0e10cSrcweir
202b274bc22SAndre Fischer    $installer::logger::Info->printf("... %s ...\n", $displaysystemcall);
203cdf0e10cSrcweir
204cdf0e10cSrcweir    my $success = 1;
205cdf0e10cSrcweir    my $returnvalue = system($systemcall);
206cdf0e10cSrcweir
207cdf0e10cSrcweir    my $infoline = "Systemcall: $displaysystemcall\n";
208b274bc22SAndre Fischer    $installer::logger::Lang->print($infoline);
209cdf0e10cSrcweir
210cdf0e10cSrcweir    if ($returnvalue)
211cdf0e10cSrcweir    {
212cdf0e10cSrcweir        $infoline = "WARNING: Could not execute \"$displaysystemcall\"!\n";
213b274bc22SAndre Fischer        $installer::logger::Lang->print($infoline);
214cdf0e10cSrcweir        $success = 0;
215cdf0e10cSrcweir    }
216cdf0e10cSrcweir    else
217cdf0e10cSrcweir    {
218cdf0e10cSrcweir        $infoline = "Success: Executed \"$displaysystemcall\" successfully!\n";
219b274bc22SAndre Fischer        $installer::logger::Lang->print($infoline);
220cdf0e10cSrcweir    }
221cdf0e10cSrcweir
222cdf0e10cSrcweir    return $success;
223cdf0e10cSrcweir}
224cdf0e10cSrcweir
225cdf0e10cSrcweir######################################################
226cdf0e10cSrcweir# Making systemcall with more return data
227cdf0e10cSrcweir######################################################
228cdf0e10cSrcweir
229cdf0e10cSrcweirsub execute_open_system_call
230cdf0e10cSrcweir{
231cdf0e10cSrcweir    my ( $systemcall ) = @_;
232cdf0e10cSrcweir
233cdf0e10cSrcweir    my @openoutput = ();
234cdf0e10cSrcweir    my $success = 1;
235cdf0e10cSrcweir
236cdf0e10cSrcweir    my $comspec = $ENV{COMSPEC};
237cdf0e10cSrcweir    $comspec = $comspec . " -c ";
238cdf0e10cSrcweir
239cdf0e10cSrcweir    if( $^O =~ /cygwin/i )
240cdf0e10cSrcweir    {
241cdf0e10cSrcweir        # $comspec =~ s/\\/\\\\/g;
242cdf0e10cSrcweir        # $comspec = qx{cygpath -u "$comspec"};
243cdf0e10cSrcweir        # $comspec =~ s/\s*$//g;
244cdf0e10cSrcweir        $comspec = "";
245cdf0e10cSrcweir    }
246cdf0e10cSrcweir
247cdf0e10cSrcweir    my $localsystemcall = "$comspec $systemcall 2>&1 |";
248cdf0e10cSrcweir
249cdf0e10cSrcweir    open( OPN, "$localsystemcall") or warn "Can't execute $localsystemcall\n";
250cdf0e10cSrcweir    while (<OPN>) { push(@openoutput, $_); }
251cdf0e10cSrcweir    close (OPN);
252cdf0e10cSrcweir
253cdf0e10cSrcweir    my $returnvalue = $?;   # $? contains the return value of the systemcall
254cdf0e10cSrcweir
255cdf0e10cSrcweir    if ($returnvalue)
256cdf0e10cSrcweir    {
257cdf0e10cSrcweir        $infoline = "ERROR: Could not execute \"$systemcall\"!\n";
258b274bc22SAndre Fischer        $installer::logger::Lang->print($infoline);
259cdf0e10cSrcweir        $success = 0;
260cdf0e10cSrcweir    }
261cdf0e10cSrcweir    else
262cdf0e10cSrcweir    {
263cdf0e10cSrcweir        $infoline = "Success: Executed \"$systemcall\" successfully!\n";
264b274bc22SAndre Fischer        $installer::logger::Lang->print($infoline);
265cdf0e10cSrcweir    }
266cdf0e10cSrcweir
267cdf0e10cSrcweir    return ($success, \@openoutput);
268cdf0e10cSrcweir}
269cdf0e10cSrcweir
270cdf0e10cSrcweir########################################################
271cdf0e10cSrcweir# Reading first line of pw file.
272cdf0e10cSrcweir########################################################
273cdf0e10cSrcweir
274cdf0e10cSrcweirsub get_pw
275cdf0e10cSrcweir{
276cdf0e10cSrcweir    my ( $file ) = @_;
277cdf0e10cSrcweir
278cdf0e10cSrcweir    my $filecontent = installer::files::read_file($file);
279cdf0e10cSrcweir
280cdf0e10cSrcweir    my $pw = ${$filecontent}[0];
281cdf0e10cSrcweir    $pw =~ s/^\s*//;
282cdf0e10cSrcweir    $pw =~ s/\s*$//;
283cdf0e10cSrcweir
284cdf0e10cSrcweir    return $pw;
285cdf0e10cSrcweir}
286cdf0e10cSrcweir
287cdf0e10cSrcweir########################################################
288cdf0e10cSrcweir# Counting the keys of a hash.
289cdf0e10cSrcweir########################################################
290cdf0e10cSrcweir
291cdf0e10cSrcweirsub get_hash_count
292cdf0e10cSrcweir{
293cdf0e10cSrcweir    my ($hashref) = @_;
294cdf0e10cSrcweir
295cdf0e10cSrcweir    my $counter = 0;
296cdf0e10cSrcweir
297cdf0e10cSrcweir    foreach my $key ( keys %{$hashref} ) { $counter++; }
298cdf0e10cSrcweir
299cdf0e10cSrcweir    return $counter;
300cdf0e10cSrcweir}
301cdf0e10cSrcweir
302cdf0e10cSrcweir############################################################
303cdf0e10cSrcweir# Collect all last files in a cabinet file. This is
304cdf0e10cSrcweir# necessary to control, if the cabinet file was damaged
305cdf0e10cSrcweir# by calling signtool.exe.
306cdf0e10cSrcweir############################################################
307cdf0e10cSrcweir
308cdf0e10cSrcweirsub analyze_file_file
309cdf0e10cSrcweir{
310cdf0e10cSrcweir    my ($filecontent) = @_;
311cdf0e10cSrcweir
312cdf0e10cSrcweir    my %filenamehash = ();
313cdf0e10cSrcweir
314cdf0e10cSrcweir    for ( my $i = 0; $i <= $#{$filecontent}; $i++ )
315cdf0e10cSrcweir    {
316cdf0e10cSrcweir        if ( $i < 3 ) { next; }
317cdf0e10cSrcweir
318cdf0e10cSrcweir        if ( ${$filecontent}[$i] =~ /^\s*(.*?)\t(.*?)\t(.*?)\t(.*?)\t(.*?)\t(.*?)\t(.*?)\t(.*?)\s*$/ )
319cdf0e10cSrcweir        {
320cdf0e10cSrcweir            my $name = $1;
321cdf0e10cSrcweir            my $sequence = $8;
322cdf0e10cSrcweir
323cdf0e10cSrcweir            $filenamehash{$sequence} = $name;
324cdf0e10cSrcweir        }
325cdf0e10cSrcweir    }
326cdf0e10cSrcweir
327cdf0e10cSrcweir    return ( \%filenamehash );
328cdf0e10cSrcweir}
329cdf0e10cSrcweir
330cdf0e10cSrcweir############################################################
331cdf0e10cSrcweir# Collect all DiskIds to the corresponding cabinet files.
332cdf0e10cSrcweir############################################################
333cdf0e10cSrcweir
334cdf0e10cSrcweirsub analyze_media_file
335cdf0e10cSrcweir{
336cdf0e10cSrcweir    my ($filecontent) = @_;
337cdf0e10cSrcweir
338cdf0e10cSrcweir    my %diskidhash = ();
339cdf0e10cSrcweir    my %lastsequencehash = ();
340cdf0e10cSrcweir
341cdf0e10cSrcweir    for ( my $i = 0; $i <= $#{$filecontent}; $i++ )
342cdf0e10cSrcweir    {
343cdf0e10cSrcweir        if ( $i < 3 ) { next; }
344cdf0e10cSrcweir
345cdf0e10cSrcweir        if ( ${$filecontent}[$i] =~ /^\s*(.*?)\t(.*?)\t(.*?)\t(.*?)\t(.*?)\t(.*?)\s*$/ )
346cdf0e10cSrcweir        {
347cdf0e10cSrcweir            my $diskid = $1;
348cdf0e10cSrcweir            my $lastsequence = $2;
349cdf0e10cSrcweir            my $cabfile = $4;
350cdf0e10cSrcweir
351cdf0e10cSrcweir            $diskidhash{$cabfile} = $diskid;
352cdf0e10cSrcweir            $lastsequencehash{$cabfile} = $lastsequence;
353cdf0e10cSrcweir        }
354cdf0e10cSrcweir    }
355cdf0e10cSrcweir
356cdf0e10cSrcweir    return ( \%diskidhash, \%lastsequencehash );
357cdf0e10cSrcweir}
358cdf0e10cSrcweir
359cdf0e10cSrcweir########################################################
360cdf0e10cSrcweir# Collect all DiskIds from database table "Media".
361cdf0e10cSrcweir########################################################
362cdf0e10cSrcweir
363cdf0e10cSrcweirsub collect_diskid_from_media_table
364cdf0e10cSrcweir{
365cdf0e10cSrcweir    my ($msidatabase, $languagestring) = @_;
366cdf0e10cSrcweir
367cdf0e10cSrcweir    # creating working directory
368cdf0e10cSrcweir    my $workdir = installer::systemactions::create_directories("media", \$languagestring);
369cdf0e10cSrcweir    installer::windows::admin::extract_tables_from_pcpfile($msidatabase, $workdir, "Media File");
370cdf0e10cSrcweir
371cdf0e10cSrcweir    # Reading tables
372cdf0e10cSrcweir    my $filename = $workdir . $installer::globals::separator . "Media.idt";
373cdf0e10cSrcweir    if ( ! -f $filename ) { installer::exiter::exit_program("ERROR: Could not find required file: $filename !", "collect_diskid_from_media_table"); }
374cdf0e10cSrcweir    my $filecontent = installer::files::read_file($filename);
375cdf0e10cSrcweir    my ( $diskidhash, $lastsequencehash ) = analyze_media_file($filecontent);
376cdf0e10cSrcweir
377cdf0e10cSrcweir    $filename = $workdir . $installer::globals::separator . "File.idt";
378cdf0e10cSrcweir    if ( ! -f $filename ) { installer::exiter::exit_program("ERROR: Could not find required file: $filename !", "collect_diskid_from_media_table"); }
379cdf0e10cSrcweir    $filecontent = installer::files::read_file($filename);
380cdf0e10cSrcweir    my $filenamehash = analyze_file_file($filecontent);
381cdf0e10cSrcweir
382cdf0e10cSrcweir    return ( $diskidhash, $filenamehash, $lastsequencehash );
383cdf0e10cSrcweir}
384cdf0e10cSrcweir
385cdf0e10cSrcweir########################################################
386cdf0e10cSrcweir# Check, if this installation set contains
387cdf0e10cSrcweir# internal cabinet files included into the msi
388cdf0e10cSrcweir# database.
389cdf0e10cSrcweir########################################################
390cdf0e10cSrcweir
391cdf0e10cSrcweirsub check_for_internal_cabfiles
392cdf0e10cSrcweir{
393cdf0e10cSrcweir    my ($cabfilehash) = @_;
394cdf0e10cSrcweir
395cdf0e10cSrcweir    my $contains_internal_cabfiles = 0;
396cdf0e10cSrcweir    my %allcabfileshash = ();
397cdf0e10cSrcweir
398cdf0e10cSrcweir    foreach my $filename ( keys %{$cabfilehash} )
399cdf0e10cSrcweir    {
400cdf0e10cSrcweir        if ( $filename =~ /^\s*\#/ ) # starting with a hash
401cdf0e10cSrcweir        {
402cdf0e10cSrcweir            $contains_internal_cabfiles = 1;
403cdf0e10cSrcweir            # setting real filename without hash as key and name with hash as value
404cdf0e10cSrcweir            my $realfilename = $filename;
405cdf0e10cSrcweir            $realfilename =~ s/^\s*\#//;
406cdf0e10cSrcweir            $allcabfileshash{$realfilename} = $filename;
407cdf0e10cSrcweir        }
408cdf0e10cSrcweir    }
409cdf0e10cSrcweir
410cdf0e10cSrcweir    return ( $contains_internal_cabfiles, \%allcabfileshash );
411cdf0e10cSrcweir}
412cdf0e10cSrcweir
413cdf0e10cSrcweir########################################################
414cdf0e10cSrcweir# Collecting all files in an installation set.
415cdf0e10cSrcweir########################################################
416cdf0e10cSrcweir
417cdf0e10cSrcweirsub analyze_installset_content
418cdf0e10cSrcweir{
419cdf0e10cSrcweir    my ( $installsetpath ) = @_;
420cdf0e10cSrcweir
421cdf0e10cSrcweir    my @sourcefiles = ();
422cdf0e10cSrcweir    my $pathstring = "";
423cdf0e10cSrcweir    installer::systemactions::read_complete_directory($installsetpath, $pathstring, \@sourcefiles);
424cdf0e10cSrcweir
425cdf0e10cSrcweir    if ( ! ( $#sourcefiles > -1 )) { installer::exiter::exit_program("ERROR: No file in installation set. Path: $installsetpath !", "analyze_installset_content"); }
426cdf0e10cSrcweir
427cdf0e10cSrcweir    my %allcabfileshash = ();
428cdf0e10cSrcweir    my %allmsidatabaseshash = ();
429cdf0e10cSrcweir    my %allfileshash = ();
430cdf0e10cSrcweir    my $contains_external_cabfiles = 0;
431cdf0e10cSrcweir    my $msidatabase = "";
432cdf0e10cSrcweir    my $contains_msidatabase = 0;
433cdf0e10cSrcweir
434cdf0e10cSrcweir    for ( my $j = 0; $j <= $#sourcefiles; $j++ )
435cdf0e10cSrcweir    {
436cdf0e10cSrcweir        if ( $sourcefiles[$j] =~ /\.cab\s*$/ ) { $allcabfileshash{$sourcefiles[$j]} = 1; }
437cdf0e10cSrcweir        else
438cdf0e10cSrcweir        {
439cdf0e10cSrcweir            if ( $sourcefiles[$j] =~ /\.txt\s*$/ ) { next; }
440cdf0e10cSrcweir            if ( $sourcefiles[$j] =~ /\.html\s*$/ ) { next; }
441cdf0e10cSrcweir            if ( $sourcefiles[$j] =~ /\.ini\s*$/ ) { next; }
442cdf0e10cSrcweir            if ( $sourcefiles[$j] =~ /\.bmp\s*$/ ) { next; }
443cdf0e10cSrcweir            if ( $sourcefiles[$j] =~ /\.msi\s*$/ )
444cdf0e10cSrcweir            {
445cdf0e10cSrcweir                if ( $msidatabase eq "" ) { $msidatabase = $sourcefiles[$j]; }
446cdf0e10cSrcweir                else { installer::exiter::exit_program("ERROR: There is more than one msi database in installation set. Path: $installsetpath !", "analyze_installset_content"); }
447cdf0e10cSrcweir            }
448cdf0e10cSrcweir            $allfileshash{$sourcefiles[$j]} = 1;
449cdf0e10cSrcweir        }
450cdf0e10cSrcweir    }
451cdf0e10cSrcweir
452cdf0e10cSrcweir    # Is there at least one cab file in the installation set?
453cdf0e10cSrcweir    my $cabcounter = get_hash_count(\%allcabfileshash);
454cdf0e10cSrcweir    if ( $cabcounter > 0 ) { $contains_external_cabfiles = 1; }
455cdf0e10cSrcweir
456cdf0e10cSrcweir    # How about a cab file without a msi database?
457cdf0e10cSrcweir    if (( $cabcounter > 0 ) && ( $msidatabase eq "" )) { installer::exiter::exit_program("ERROR: There is no msi database in the installation set, but an external cabinet file. Path: $installsetpath !", "collect_installset_content"); }
458cdf0e10cSrcweir
459cdf0e10cSrcweir    if ( $msidatabase ne "" ) { $contains_msidatabase = 1; }
460cdf0e10cSrcweir
461cdf0e10cSrcweir    return (\%allcabfileshash, \%allfileshash, $msidatabase, $contains_external_cabfiles, $contains_msidatabase, \@sourcefiles);
462cdf0e10cSrcweir}
463cdf0e10cSrcweir
464cdf0e10cSrcweir########################################################
465cdf0e10cSrcweir# Adding content of external cabinet files into the
466cdf0e10cSrcweir# msi database
467cdf0e10cSrcweir########################################################
468cdf0e10cSrcweir
469cdf0e10cSrcweirsub msicert_database
470cdf0e10cSrcweir{
471cdf0e10cSrcweir    my ($msidatabase, $allcabfiles, $cabfilehash, $internalcabfile) = @_;
472cdf0e10cSrcweir
473cdf0e10cSrcweir    my $fullsuccess = 1;
474cdf0e10cSrcweir
475cdf0e10cSrcweir    foreach my $cabfile ( keys %{$allcabfiles} )
476cdf0e10cSrcweir    {
477cdf0e10cSrcweir        my $origfilesize = -s $cabfile;
478cdf0e10cSrcweir
479cdf0e10cSrcweir        my $mediacabfilename = $cabfile;
480cdf0e10cSrcweir        if ( $internalcabfile ) { $mediacabfilename = "\#" . $mediacabfilename; }
481cdf0e10cSrcweir        if ( ! exists($cabfilehash->{$mediacabfilename}) ) { installer::exiter::exit_program("ERROR: Could not determine DiskId from media table for cabinet file \"$cabfile\" !", "msicert_database"); }
482cdf0e10cSrcweir        my $diskid = $cabfilehash->{$mediacabfilename};
483cdf0e10cSrcweir
484cdf0e10cSrcweir        my $systemcall = "msicert.exe -d $msidatabase -m $diskid -c $cabfile -h";
485cdf0e10cSrcweir        $success = make_systemcall($systemcall, $systemcall);
486cdf0e10cSrcweir        if ( ! $success ) { $fullsuccess = 0; }
487cdf0e10cSrcweir
488cdf0e10cSrcweir        # size of cabinet file must not change
489cdf0e10cSrcweir        my $finalfilesize = -s $cabfile;
490cdf0e10cSrcweir
491cdf0e10cSrcweir        if ( $origfilesize != $finalfilesize ) { installer::exiter::exit_program("ERROR: msicert.exe changed size of cabinet file !", "msicert_database"); }
492cdf0e10cSrcweir    }
493cdf0e10cSrcweir
494cdf0e10cSrcweir    return $fullsuccess;
495cdf0e10cSrcweir}
496cdf0e10cSrcweir
497cdf0e10cSrcweir########################################################
498cdf0e10cSrcweir# Checking if cabinet file was broken by signtool.
499cdf0e10cSrcweir########################################################
500cdf0e10cSrcweir
501cdf0e10cSrcweirsub cabinet_cosistency_check
502cdf0e10cSrcweir{
503cdf0e10cSrcweir    my ( $onefile, $followmeinfohash, $filenamehash, $lastsequencehash, $temppath ) = @_;
504cdf0e10cSrcweir
505cdf0e10cSrcweir    my $infoline = "Making consistency check of $onefile\n";
506b274bc22SAndre Fischer    $installer::logger::Lang->print($infoline);
507cdf0e10cSrcweir    my $expandfile = "expand.exe";  # Has to be in the path
508cdf0e10cSrcweir
509cdf0e10cSrcweir    if ( $^O =~ /cygwin/i )
510cdf0e10cSrcweir    {
511cdf0e10cSrcweir        $expandfile = qx(cygpath -u "$ENV{WINDIR}"/System32/expand.exe);
512cdf0e10cSrcweir        chomp $expandfile;
513cdf0e10cSrcweir    }
514cdf0e10cSrcweir
515cdf0e10cSrcweir    if ( $filenamehash == 0 )
516cdf0e10cSrcweir    {
517cdf0e10cSrcweir        $infoline = "Warning: Stopping consistency check: Important hash of filenames is empty!\n";
518b274bc22SAndre Fischer        $installer::logger::Lang->print($infoline);
519cdf0e10cSrcweir    }
520cdf0e10cSrcweir    elsif ( $lastsequencehash == 0 )
521cdf0e10cSrcweir    {
522cdf0e10cSrcweir        $infoline = "Warning: Stopping consistency check; Important hash of last sequences is empty!\n";
523b274bc22SAndre Fischer        $installer::logger::Lang->print($infoline);
524cdf0e10cSrcweir    }
525cdf0e10cSrcweir    else # both hashes are available
526cdf0e10cSrcweir    {
527cdf0e10cSrcweir        # $onefile contains only the name of the cabinet file without path
528cdf0e10cSrcweir        my $sequence = $lastsequencehash->{$onefile};
529cdf0e10cSrcweir        my $lastfile = $filenamehash->{$sequence};
530cdf0e10cSrcweir        $infoline = "Check of $onefile: Sequence: $sequence is file: $lastfile\n";
531b274bc22SAndre Fischer        $installer::logger::Lang->print($infoline);
532cdf0e10cSrcweir
533cdf0e10cSrcweir        # Therefore the file $lastfile need to be binary compared.
534cdf0e10cSrcweir        # It has to be expanded from the cabinet file
535cdf0e10cSrcweir        # of the original installation set and from the
536cdf0e10cSrcweir        # newly signed cabinet file.
537cdf0e10cSrcweir
538cdf0e10cSrcweir        # How about cabinet files extracted from msi database?
539cdf0e10cSrcweir        my $finalinstalldir = $followmeinfohash->{'finalinstalldir'};
540cdf0e10cSrcweir
541cdf0e10cSrcweir        $finalinstalldir =~ s/\\\s*$//;
542cdf0e10cSrcweir        $finalinstalldir =~ s/\/\s*$//;
543cdf0e10cSrcweir        my $sourcecabfile = $finalinstalldir . $installer::globals::separator . $onefile;
544cdf0e10cSrcweir        my $currentpath = cwd();
545cdf0e10cSrcweir        my $destcabfile = $currentpath . $installer::globals::separator . $onefile;
546cdf0e10cSrcweir        # my $destcabfile = $onefile;
547cdf0e10cSrcweir
548cdf0e10cSrcweir        if ( $^O =~ /cygwin/i )
549cdf0e10cSrcweir        {
550cdf0e10cSrcweir            chomp( $destcabfile = qx{cygpath -w "$destcabfile"} );
551cdf0e10cSrcweir            $destcabfile =~ s/\\/\//g;
552cdf0e10cSrcweir        }
553cdf0e10cSrcweir
554cdf0e10cSrcweir        if ( ! -f $sourcecabfile )
555cdf0e10cSrcweir        {
556cdf0e10cSrcweir            $infoline = "WARNING: Check of cab file cannot happen, because source cabinet file was not found: $sourcecabfile\n";
557b274bc22SAndre Fischer            $installer::logger::Lang->print($infoline);
558cdf0e10cSrcweir        }
559cdf0e10cSrcweir        elsif ( ! -f $destcabfile )
560cdf0e10cSrcweir        {
561cdf0e10cSrcweir            $infoline = "WARNING: Check of cab file cannot happen, because destination cabinet file was not found: $sourcecabfile\n";
562b274bc22SAndre Fischer            $installer::logger::Lang->print($infoline);
563cdf0e10cSrcweir        }
564cdf0e10cSrcweir        else # everything is okay for the check
565cdf0e10cSrcweir        {
566cdf0e10cSrcweir            my $diffpath = get_diff_path($temppath);
567cdf0e10cSrcweir
568cdf0e10cSrcweir            my $origdiffpath = $diffpath . $installer::globals::separator . "orig";
569cdf0e10cSrcweir            my $newdiffpath = $diffpath . $installer::globals::separator . "new";
570cdf0e10cSrcweir
571cdf0e10cSrcweir            if ( ! -d $origdiffpath ) { mkdir($origdiffpath); }
572cdf0e10cSrcweir            if ( ! -d $newdiffpath ) { mkdir($newdiffpath); }
573cdf0e10cSrcweir
574cdf0e10cSrcweir            my $systemcall = "$expandfile $sourcecabfile $origdiffpath -f:$lastfile ";
575cdf0e10cSrcweir            $infoline = $systemcall . "\n";
576b274bc22SAndre Fischer            $installer::logger::Lang->print($infoline);
577cdf0e10cSrcweir
578cdf0e10cSrcweir            my $success = make_systemcall($systemcall, $systemcall);
579cdf0e10cSrcweir            if ( ! $success ) { installer::exiter::exit_program("ERROR: Could not successfully execute: $systemcall !", "cabinet_cosistency_check"); }
580cdf0e10cSrcweir
581cdf0e10cSrcweir            $systemcall = "$expandfile $destcabfile $newdiffpath -f:$lastfile ";
582cdf0e10cSrcweir            $infoline = $systemcall . "\n";
583b274bc22SAndre Fischer            $installer::logger::Lang->print($infoline);
584cdf0e10cSrcweir
585cdf0e10cSrcweir            $success = make_systemcall($systemcall, $systemcall);
586cdf0e10cSrcweir            if ( ! $success ) { installer::exiter::exit_program("ERROR: Could not successfully execute: $systemcall !", "cabinet_cosistency_check"); }
587cdf0e10cSrcweir
588cdf0e10cSrcweir            # and finally the two files can be diffed.
589cdf0e10cSrcweir            my $origfile = $origdiffpath . $installer::globals::separator . $lastfile;
590cdf0e10cSrcweir            my $newfile = $newdiffpath . $installer::globals::separator . $lastfile;
591cdf0e10cSrcweir
592cdf0e10cSrcweir            if ( ! -f $origfile ) { installer::exiter::exit_program("ERROR: Unpacked original file not found: $origfile !", "cabinet_cosistency_check"); }
593cdf0e10cSrcweir            if ( ! -f $newfile ) { installer::exiter::exit_program("ERROR: Unpacked new file not found: $newfile !", "cabinet_cosistency_check"); }
594cdf0e10cSrcweir
595cdf0e10cSrcweir            my $origsize = -s $origfile;
596cdf0e10cSrcweir            my $newsize = -s $newfile;
597cdf0e10cSrcweir
598cdf0e10cSrcweir            if ( $origsize != $newsize ) # This shows an error!
599cdf0e10cSrcweir            {
600cdf0e10cSrcweir                $infoline = "ERROR: Different filesize after signtool.exe was used. Original: $origsize Bytes, new: $newsize. File: $lastfile\n";
601b274bc22SAndre Fischer                $installer::logger::Lang->print($infoline);
602cdf0e10cSrcweir                installer::exiter::exit_program("ERROR: The cabinet file $destcabfile is broken after signtool.exe signed this file !", "cabinet_cosistency_check");
603cdf0e10cSrcweir            }
604cdf0e10cSrcweir            else
605cdf0e10cSrcweir            {
606cdf0e10cSrcweir                $infoline = "Same size of last file in cabinet file after usage of signtool.exe: $newsize (File: $lastfile)\n";
607b274bc22SAndre Fischer                $installer::logger::Lang->print($infoline);
608cdf0e10cSrcweir
609cdf0e10cSrcweir                # Also making a binary diff?
610cdf0e10cSrcweir
611cdf0e10cSrcweir                my $difffile = "diff.exe"; # has to be in the path
612cdf0e10cSrcweir                # $systemcall = "$difffile $sourcecabfile $destcabfile"; # Test for differences
613cdf0e10cSrcweir                $systemcall = "$difffile $origfile $newfile";
614cdf0e10cSrcweir                $infoline = $systemcall . "\n";
615cdf0e10cSrcweir                $returnvalue = make_systemcall($systemcall, $systemcall);
616cdf0e10cSrcweir
617cdf0e10cSrcweir                my $success = $?;
618cdf0e10cSrcweir
619cdf0e10cSrcweir                if ( $success == 0 )
620cdf0e10cSrcweir                {
621cdf0e10cSrcweir                    $infoline = "Last files are identical after signing cabinet file (File: $lastfile)\n";
622b274bc22SAndre Fischer                    $installer::logger::Lang->print($infoline);
623cdf0e10cSrcweir                }
624cdf0e10cSrcweir                elsif ( $success == 1 )
625cdf0e10cSrcweir                {
626cdf0e10cSrcweir                    $infoline = "ERROR: Last files are different after signing cabinet file (File: $lastfile)\n";
627b274bc22SAndre Fischer                    $installer::logger::Lang->print($infoline);
628cdf0e10cSrcweir                    installer::exiter::exit_program("ERROR: Last files are different after signing cabinet file (File: $lastfile)!", "cabinet_cosistency_check");
629cdf0e10cSrcweir                }
630cdf0e10cSrcweir                else
631cdf0e10cSrcweir                {
63286e1cf34SPedro Giffuni                    $infoline = "ERROR: Problem occurred calling diff.exe (File: $lastfile)\n";
633b274bc22SAndre Fischer                    $installer::logger::Lang->print($infoline);
63486e1cf34SPedro Giffuni                    installer::exiter::exit_program("ERROR: Problem occurred calling diff.exe (File: $lastfile) !", "cabinet_cosistency_check");
635cdf0e10cSrcweir                }
636cdf0e10cSrcweir            }
637cdf0e10cSrcweir        }
638cdf0e10cSrcweir    }
639cdf0e10cSrcweir
640cdf0e10cSrcweir}
641cdf0e10cSrcweir
642cdf0e10cSrcweir########################################################
643cdf0e10cSrcweir# Signing a list of files
644cdf0e10cSrcweir########################################################
645cdf0e10cSrcweir
646cdf0e10cSrcweirsub sign_files
647cdf0e10cSrcweir{
648cdf0e10cSrcweir    my ( $followmeinfohash, $allfiles, $pw, $cabinternal, $filenamehash, $lastsequencehash, $temppath ) = @_;
649cdf0e10cSrcweir
650cdf0e10cSrcweir    my $infoline = "";
651cdf0e10cSrcweir    my $fullsuccess = 1;
652cdf0e10cSrcweir    my $maxcounter = 3;
653cdf0e10cSrcweir
654cdf0e10cSrcweir    my $productname = "";
655cdf0e10cSrcweir    if ( $followmeinfohash->{'allvariableshash'}->{'PRODUCTNAME'} ) { $productname = "/d " . "\"$followmeinfohash->{'allvariableshash'}->{'PRODUCTNAME'}\""; }
656d2b96cdeSHerbert Dürr    my $url = "/du " . "\"http://www.openoffice.org\"";
657cdf0e10cSrcweir    my $timestampurl = "http://timestamp.verisign.com/scripts/timestamp.dll";
658cdf0e10cSrcweir
659cdf0e10cSrcweir    my $pfxfilepath = $installer::globals::pfxfile;
660cdf0e10cSrcweir
661cdf0e10cSrcweir    if( $^O =~ /cygwin/i )
662cdf0e10cSrcweir    {
663cdf0e10cSrcweir        $pfxfilepath = qx{cygpath -w "$pfxfilepath"};
664cdf0e10cSrcweir        $pfxfilepath =~ s/\\/\\\\/g;
665cdf0e10cSrcweir        $pfxfilepath =~ s/\s*$//g;
666cdf0e10cSrcweir    }
667cdf0e10cSrcweir
668cdf0e10cSrcweir    foreach my $onefile ( reverse sort keys %{$allfiles} )
669cdf0e10cSrcweir    {
670cdf0e10cSrcweir        if ( already_certified($onefile) )
671cdf0e10cSrcweir        {
672cdf0e10cSrcweir            $infoline = "Already certified: Skipping file $onefile\n";
673b274bc22SAndre Fischer            $installer::logger::Lang->print($infoline);
674cdf0e10cSrcweir            next;
675cdf0e10cSrcweir        }
676cdf0e10cSrcweir
677cdf0e10cSrcweir        my $counter = 1;
678cdf0e10cSrcweir        my $success = 0;
679cdf0e10cSrcweir
680cdf0e10cSrcweir        while (( $counter <= $maxcounter ) && ( ! $success ))
681cdf0e10cSrcweir        {
682b274bc22SAndre Fischer            if ( $counter > 1 )
683b274bc22SAndre Fischer            {
684b274bc22SAndre Fischer                $installer::logger::Info->printf("\n");
685b274bc22SAndre Fischer                $installer::logger::Info->printf("\n");
686b274bc22SAndre Fischer                $installer::logger::Info->printf("... repeating file %s ...\n", $onefile);
687b274bc22SAndre Fischer            }
688b274bc22SAndre Fischer            if ( $cabinternal )
689b274bc22SAndre Fischer            {
690b274bc22SAndre Fischer                $installer::logger::Info->printf("    Signing: %s\n", $onefile);
691b274bc22SAndre Fischer            }
692cdf0e10cSrcweir            my $systemcall = "signtool.exe sign /f \"$pfxfilepath\" /p $pw $productname $url /t \"$timestampurl\" \"$onefile\"";
693cdf0e10cSrcweir            my $displaysystemcall = "signtool.exe sign /f \"$pfxfilepath\" /p ***** $productname $url /t \"$timestampurl\" \"$onefile\"";
694cdf0e10cSrcweir            $success = make_systemcall_with_warning($systemcall, $displaysystemcall);
695cdf0e10cSrcweir            $counter++;
696cdf0e10cSrcweir        }
697cdf0e10cSrcweir
698cdf0e10cSrcweir        # Special check for cabinet files, that sometimes get damaged by signtool.exe
699cdf0e10cSrcweir        if (( $success ) && ( $onefile =~ /\.cab\s*$/ ) && ( ! $cabinternal ))
700cdf0e10cSrcweir        {
701cdf0e10cSrcweir            cabinet_cosistency_check($onefile, $followmeinfohash, $filenamehash, $lastsequencehash, $temppath);
702cdf0e10cSrcweir        }
703cdf0e10cSrcweir
704cdf0e10cSrcweir        if ( ! $success )
705cdf0e10cSrcweir        {
706cdf0e10cSrcweir            $fullsuccess = 0;
707cdf0e10cSrcweir            installer::exiter::exit_program("ERROR: Could not sign file: $onefile!", "sign_files");
708cdf0e10cSrcweir        }
709cdf0e10cSrcweir    }
710cdf0e10cSrcweir
711cdf0e10cSrcweir    return $fullsuccess;
712cdf0e10cSrcweir}
713cdf0e10cSrcweir
714cdf0e10cSrcweir##########################################################################
715cdf0e10cSrcweir# Lines in ddf files must not contain more than 256 characters
716cdf0e10cSrcweir##########################################################################
717cdf0e10cSrcweir
718cdf0e10cSrcweirsub check_ddf_file
719cdf0e10cSrcweir{
720cdf0e10cSrcweir    my ( $ddffile, $ddffilename ) = @_;
721cdf0e10cSrcweir
722cdf0e10cSrcweir    my $maxlength = 0;
723cdf0e10cSrcweir    my $maxline = 0;
724cdf0e10cSrcweir    my $linelength = 0;
725cdf0e10cSrcweir    my $linenumber = 0;
726cdf0e10cSrcweir
727cdf0e10cSrcweir    for ( my $i = 0; $i <= $#{$ddffile}; $i++ )
728cdf0e10cSrcweir    {
729cdf0e10cSrcweir        my $oneline = ${$ddffile}[$i];
730cdf0e10cSrcweir
731cdf0e10cSrcweir        $linelength = length($oneline);
732cdf0e10cSrcweir        $linenumber = $i + 1;
733cdf0e10cSrcweir
734cdf0e10cSrcweir        if ( $linelength > 256 )
735cdf0e10cSrcweir        {
736cdf0e10cSrcweir            installer::exiter::exit_program("ERROR \"$ddffilename\" line $linenumber: Lines in ddf files must not contain more than 256 characters!", "check_ddf_file");
737cdf0e10cSrcweir        }
738cdf0e10cSrcweir
739cdf0e10cSrcweir        if ( $linelength > $maxlength )
740cdf0e10cSrcweir        {
741cdf0e10cSrcweir            $maxlength = $linelength;
742cdf0e10cSrcweir            $maxline = $linenumber;
743cdf0e10cSrcweir        }
744cdf0e10cSrcweir    }
745cdf0e10cSrcweir
746cdf0e10cSrcweir    my $infoline = "Check of ddf file \"$ddffilename\": Maximum length \"$maxlength\" in line \"$maxline\" (allowed line length: 256 characters)\n";
747b274bc22SAndre Fischer    $installer::logger::Lang->print($infoline);
748cdf0e10cSrcweir}
749cdf0e10cSrcweir
750cdf0e10cSrcweir#################################################################
751cdf0e10cSrcweir# Setting the path, where the cab files are unpacked.
752cdf0e10cSrcweir#################################################################
753cdf0e10cSrcweir
754cdf0e10cSrcweirsub get_cab_path
755cdf0e10cSrcweir{
756cdf0e10cSrcweir    my ($temppath) = @_;
757cdf0e10cSrcweir
758cdf0e10cSrcweir    my $cabpath = "cabs_" . $$;
759cdf0e10cSrcweir    $cabpath = $temppath . $installer::globals::separator . $cabpath;
760cdf0e10cSrcweir    if ( ! -d $cabpath ) { installer::systemactions::create_directory($cabpath); }
761cdf0e10cSrcweir
762cdf0e10cSrcweir    return $cabpath;
763cdf0e10cSrcweir}
764cdf0e10cSrcweir
765cdf0e10cSrcweir#################################################################
766cdf0e10cSrcweir# Setting the path, where the diff can happen.
767cdf0e10cSrcweir#################################################################
768cdf0e10cSrcweir
769cdf0e10cSrcweirsub get_diff_path
770cdf0e10cSrcweir{
771cdf0e10cSrcweir    my ($temppath) = @_;
772cdf0e10cSrcweir
773cdf0e10cSrcweir    my $diffpath = "diff_" . $$;
774cdf0e10cSrcweir    $diffpath = $temppath . $installer::globals::separator . $diffpath;
775cdf0e10cSrcweir    if ( ! -d $diffpath ) { installer::systemactions::create_directory($diffpath); }
776cdf0e10cSrcweir
777cdf0e10cSrcweir    return $diffpath;
778cdf0e10cSrcweir}
779cdf0e10cSrcweir
780cdf0e10cSrcweir#################################################################
781cdf0e10cSrcweir# Exclude all cab files from the msi database.
782cdf0e10cSrcweir#################################################################
783cdf0e10cSrcweir
784cdf0e10cSrcweirsub extract_cabs_from_database
785cdf0e10cSrcweir{
786cdf0e10cSrcweir    my ($msidatabase, $allcabfiles) = @_;
787cdf0e10cSrcweir
788cdf0e10cSrcweir    installer::logger::include_header_into_logfile("Extracting cabs from msi database");
789cdf0e10cSrcweir
790cdf0e10cSrcweir    my $infoline = "";
791cdf0e10cSrcweir    my $fullsuccess = 1;
792cdf0e10cSrcweir    my $msidb = "msidb.exe";    # Has to be in the path
793cdf0e10cSrcweir
794cdf0e10cSrcweir    # msidb.exe really wants backslashes. (And double escaping because system() expands the string.)
795cdf0e10cSrcweir    $msidatabase =~ s/\//\\\\/g;
796cdf0e10cSrcweir
797cdf0e10cSrcweir    foreach my $onefile ( keys %{$allcabfiles} )
798cdf0e10cSrcweir    {
799cdf0e10cSrcweir        my $systemcall = $msidb . " -d " . $msidatabase . " -x " . $onefile;
800cdf0e10cSrcweir        my $success = make_systemcall($systemcall, $systemcall);
801cdf0e10cSrcweir        if ( ! $success ) { $fullsuccess = 0; }
802cdf0e10cSrcweir
803cdf0e10cSrcweir        # and removing the stream from the database
804cdf0e10cSrcweir        $systemcall = $msidb . " -d " . $msidatabase . " -k " . $onefile;
805cdf0e10cSrcweir        $success = make_systemcall($systemcall, $systemcall);
806cdf0e10cSrcweir        if ( ! $success ) { $fullsuccess = 0; }
807cdf0e10cSrcweir    }
808cdf0e10cSrcweir
809cdf0e10cSrcweir    return $fullsuccess;
810cdf0e10cSrcweir}
811cdf0e10cSrcweir
812cdf0e10cSrcweir#################################################################
813cdf0e10cSrcweir# Include cab files into the msi database.
814cdf0e10cSrcweir#################################################################
815cdf0e10cSrcweir
816cdf0e10cSrcweirsub include_cabs_into_database
817cdf0e10cSrcweir{
818cdf0e10cSrcweir    my ($msidatabase, $allcabfiles) = @_;
819cdf0e10cSrcweir
820cdf0e10cSrcweir    installer::logger::include_header_into_logfile("Including cabs into msi database");
821cdf0e10cSrcweir
822cdf0e10cSrcweir    my $infoline = "";
823cdf0e10cSrcweir    my $fullsuccess = 1;
824cdf0e10cSrcweir    my $msidb = "msidb.exe";    # Has to be in the path
825cdf0e10cSrcweir
826cdf0e10cSrcweir    # msidb.exe really wants backslashes. (And double escaping because system() expands the string.)
827cdf0e10cSrcweir    $msidatabase =~ s/\//\\\\/g;
828cdf0e10cSrcweir
829cdf0e10cSrcweir    foreach my $onefile ( keys %{$allcabfiles} )
830cdf0e10cSrcweir    {
831cdf0e10cSrcweir        my $systemcall = $msidb . " -d " . $msidatabase . " -a " . $onefile;
832cdf0e10cSrcweir        my $success = make_systemcall($systemcall, $systemcall);
833cdf0e10cSrcweir        if ( ! $success ) { $fullsuccess = 0; }
834cdf0e10cSrcweir    }
835cdf0e10cSrcweir
836cdf0e10cSrcweir    return $fullsuccess;
837cdf0e10cSrcweir}
838cdf0e10cSrcweir
839cdf0e10cSrcweir########################################################
840cdf0e10cSrcweir# Reading the order of the files inside the
841cdf0e10cSrcweir# cabinet files.
842cdf0e10cSrcweir########################################################
843cdf0e10cSrcweir
844cdf0e10cSrcweirsub read_cab_file
845cdf0e10cSrcweir{
846cdf0e10cSrcweir    my ($cabfilename) = @_;
847cdf0e10cSrcweir
848b274bc22SAndre Fischer    $installer::logger::Info->printf("\n");
849b274bc22SAndre Fischer    $installer::logger::Info->printf("... reading cabinet file %s ...\n", $cabfilename);
850cdf0e10cSrcweir    my $infoline = "Reading cabinet file $cabfilename\n";
851b274bc22SAndre Fischer    $installer::logger::Lang->print($infoline);
852cdf0e10cSrcweir
853cdf0e10cSrcweir    my $systemcall = "cabarc.exe" . " L " . $cabfilename;
854cdf0e10cSrcweir    push(@logfile, "$systemcall\n");
855cdf0e10cSrcweir
856cdf0e10cSrcweir    my ($success, $fileorder) = execute_open_system_call($systemcall);
857cdf0e10cSrcweir
858cdf0e10cSrcweir    my @allfiles = ();
859cdf0e10cSrcweir
860cdf0e10cSrcweir    for ( my $i = 0; $i <= $#{$fileorder}; $i++ )
861cdf0e10cSrcweir    {
862cdf0e10cSrcweir        my $line = ${$fileorder}[$i];
863cdf0e10cSrcweir        if ( $line =~ /^\s*(.*?)\s+\d+\s+\d+\/\d+\/\d+\s+\d+\:\d+\:\d+\s+[\w-]+\s*$/ )
864cdf0e10cSrcweir        {
865cdf0e10cSrcweir            my $filename = $1;
866cdf0e10cSrcweir            push(@allfiles, $filename);
867cdf0e10cSrcweir        }
868cdf0e10cSrcweir    }
869cdf0e10cSrcweir
870cdf0e10cSrcweir    return \@allfiles;
871cdf0e10cSrcweir}
872cdf0e10cSrcweir
873cdf0e10cSrcweir########################################################
874cdf0e10cSrcweir# Unpacking a cabinet file.
875cdf0e10cSrcweir########################################################
876cdf0e10cSrcweir
877cdf0e10cSrcweirsub unpack_cab_file
878cdf0e10cSrcweir{
879cdf0e10cSrcweir    my ($cabfilename, $temppath) = @_;
880cdf0e10cSrcweir
881b274bc22SAndre Fischer    $installer::logger::Info->printf("\n");
882b274bc22SAndre Fischer    $installer::logger::Info->printf("... unpacking cabinet file %s ...\n", $cabfilename);
883cdf0e10cSrcweir    my $infoline = "Unpacking cabinet file $cabfilename\n";
884b274bc22SAndre Fischer    $installer::logger::Lang->print($infoline);
885cdf0e10cSrcweir
886cdf0e10cSrcweir    my $dirname = $cabfilename;
887cdf0e10cSrcweir    $dirname =~ s/\.cab\s*$//;
888cdf0e10cSrcweir    my $workingpath = $temppath . $installer::globals::separator . "unpack_". $dirname . "_" . $$;
889cdf0e10cSrcweir    if ( ! -d $workingpath ) { installer::systemactions::create_directory($workingpath); }
890cdf0e10cSrcweir
891cdf0e10cSrcweir    # changing into unpack directory
892cdf0e10cSrcweir    my $from = cwd();
893cdf0e10cSrcweir    chdir($workingpath);
894cdf0e10cSrcweir
895cdf0e10cSrcweir    my $fullcabfilename = $from . $installer::globals::separator . $cabfilename;
896cdf0e10cSrcweir
897cdf0e10cSrcweir    if( $^O =~ /cygwin/i )
898cdf0e10cSrcweir    {
899cdf0e10cSrcweir        $fullcabfilename = qx{cygpath -w "$fullcabfilename"};
900cdf0e10cSrcweir        $fullcabfilename =~ s/\\/\\\\/g;
901cdf0e10cSrcweir        $fullcabfilename =~ s/\s*$//g;
902cdf0e10cSrcweir    }
903cdf0e10cSrcweir
904cdf0e10cSrcweir    my $systemcall = "cabarc.exe" . " -p X " . $fullcabfilename;
905cdf0e10cSrcweir    $success = make_systemcall($systemcall, $systemcall);
906cdf0e10cSrcweir    if ( ! $success ) { installer::exiter::exit_program("ERROR: Could not unpack cabinet file: $fullcabfilename!", "unpack_cab_file"); }
907cdf0e10cSrcweir
908cdf0e10cSrcweir    # returning to directory
909cdf0e10cSrcweir    chdir($from);
910cdf0e10cSrcweir
911cdf0e10cSrcweir    return $workingpath;
912cdf0e10cSrcweir}
913cdf0e10cSrcweir
914cdf0e10cSrcweir########################################################
915cdf0e10cSrcweir# Returning the header of a ddf file.
916cdf0e10cSrcweir########################################################
917cdf0e10cSrcweir
918cdf0e10cSrcweirsub get_ddf_file_header
919cdf0e10cSrcweir{
920cdf0e10cSrcweir    my ($ddffileref, $cabinetfile, $installdir) = @_;
921cdf0e10cSrcweir
922cdf0e10cSrcweir    my $oneline;
923cdf0e10cSrcweir    my $compressionlevel = 2;
924cdf0e10cSrcweir
925cdf0e10cSrcweir    if( $^O =~ /cygwin/i )
926cdf0e10cSrcweir    {
927cdf0e10cSrcweir        $installdir = qx{cygpath -w "$installdir"};
928cdf0e10cSrcweir        $installdir =~ s/\s*$//g;
929cdf0e10cSrcweir    }
930cdf0e10cSrcweir
931cdf0e10cSrcweir    $oneline = ".Set CabinetName1=" . $cabinetfile . "\n";
932cdf0e10cSrcweir    push(@{$ddffileref} ,$oneline);
933cdf0e10cSrcweir    $oneline = ".Set ReservePerCabinetSize=128\n";  # This reserves space for a digital signature.
934cdf0e10cSrcweir    push(@{$ddffileref} ,$oneline);
935cdf0e10cSrcweir    $oneline = ".Set MaxDiskSize=2147483648\n";     # This allows the .cab file to get a size of 2 GB.
936cdf0e10cSrcweir    push(@{$ddffileref} ,$oneline);
937cdf0e10cSrcweir    $oneline = ".Set CompressionType=LZX\n";
938cdf0e10cSrcweir    push(@{$ddffileref} ,$oneline);
939cdf0e10cSrcweir    $oneline = ".Set Compress=ON\n";
940cdf0e10cSrcweir    push(@{$ddffileref} ,$oneline);
941cdf0e10cSrcweir    $oneline = ".Set CompressionLevel=$compressionlevel\n";
942cdf0e10cSrcweir    push(@{$ddffileref} ,$oneline);
943cdf0e10cSrcweir    $oneline = ".Set Cabinet=ON\n";
944cdf0e10cSrcweir    push(@{$ddffileref} ,$oneline);
945cdf0e10cSrcweir    $oneline = ".Set DiskDirectoryTemplate=" . $installdir . "\n";
946cdf0e10cSrcweir    push(@{$ddffileref} ,$oneline);
947cdf0e10cSrcweir}
948cdf0e10cSrcweir
949cdf0e10cSrcweir########################################################
950cdf0e10cSrcweir# Writing content into ddf file.
951cdf0e10cSrcweir########################################################
952cdf0e10cSrcweir
953cdf0e10cSrcweirsub put_all_files_into_ddffile
954cdf0e10cSrcweir{
955cdf0e10cSrcweir    my ($ddffile, $allfiles, $workingpath) = @_;
956cdf0e10cSrcweir
957cdf0e10cSrcweir    $workingpath =~ s/\//\\/g;
958cdf0e10cSrcweir
959cdf0e10cSrcweir    for ( my $i = 0; $i <= $#{$allfiles}; $i++ )
960cdf0e10cSrcweir    {
961cdf0e10cSrcweir        my $filename = ${$allfiles}[$i];
962cdf0e10cSrcweir        if( $^O =~ /cygwin/i ) { $filename =~ s/\//\\/g; } # Backslash for Cygwin!
963cdf0e10cSrcweir        if ( ! -f $filename ) { installer::exiter::exit_program("ERROR: Could not find file: $filename!", "put_all_files_into_ddffile"); }
964cdf0e10cSrcweir        my $infoline = "\"" . $filename . "\"" . " " . ${$allfiles}[$i] . "\n";
965cdf0e10cSrcweir        push( @{$ddffile}, $infoline);
966cdf0e10cSrcweir    }
967cdf0e10cSrcweir}
968cdf0e10cSrcweir
969cdf0e10cSrcweir########################################################
970cdf0e10cSrcweir# Packing a cabinet file.
971cdf0e10cSrcweir########################################################
972cdf0e10cSrcweir
973cdf0e10cSrcweirsub do_pack_cab_file
974cdf0e10cSrcweir{
975cdf0e10cSrcweir    my ($cabfilename, $allfiles, $workingpath, $temppath) = @_;
976cdf0e10cSrcweir
977b274bc22SAndre Fischer    $installer::logger::Info->print("\n");
978b274bc22SAndre Fischer    $installer::logger::Info->printf("... packing cabinet file %s ...\n", $cabfilename);
979cdf0e10cSrcweir    my $infoline = "Packing cabinet file $cabfilename\n";
980b274bc22SAndre Fischer    $installer::logger::Lang->print($infoline);
981cdf0e10cSrcweir
982cdf0e10cSrcweir    if ( -f $cabfilename ) { unlink($cabfilename); } # removing cab file
983cdf0e10cSrcweir    if ( -f $cabfilename ) { installer::exiter::exit_program("ERROR: Failed to remove file: $cabfilename!", "do_pack_cab_file"); }
984cdf0e10cSrcweir
985cdf0e10cSrcweir    # generate ddf file for makecab.exe
986cdf0e10cSrcweir    my @ddffile = ();
987cdf0e10cSrcweir
988cdf0e10cSrcweir    my $dirname = $cabfilename;
989cdf0e10cSrcweir    $dirname =~ s/\.cab\s*$//;
990cdf0e10cSrcweir    my $ddfpath = $temppath . $installer::globals::separator . "ddf_". $dirname . "_" . $$;
991cdf0e10cSrcweir
992cdf0e10cSrcweir    my $ddffilename = $cabfilename;
993cdf0e10cSrcweir    $ddffilename =~ s/.cab/.ddf/;
994cdf0e10cSrcweir    $ddffilename = $ddfpath . $installer::globals::separator . $ddffilename;
995cdf0e10cSrcweir
996cdf0e10cSrcweir    if ( ! -d $ddfpath ) { installer::systemactions::create_directory($ddfpath); }
997cdf0e10cSrcweir
998cdf0e10cSrcweir    my $from = cwd();
999cdf0e10cSrcweir
1000cdf0e10cSrcweir    chdir($workingpath); # changing into the directory with the unpacked files
1001cdf0e10cSrcweir
1002cdf0e10cSrcweir    get_ddf_file_header(\@ddffile, $cabfilename, $from);
1003cdf0e10cSrcweir    put_all_files_into_ddffile(\@ddffile, $allfiles, $workingpath);
1004cdf0e10cSrcweir    # lines in ddf files must not be longer than 256 characters
1005cdf0e10cSrcweir    check_ddf_file(\@ddffile, $ddffilename);
1006cdf0e10cSrcweir
1007cdf0e10cSrcweir    installer::files::save_file($ddffilename, \@ddffile);
1008cdf0e10cSrcweir
1009cdf0e10cSrcweir    if( $^O =~ /cygwin/i )
1010cdf0e10cSrcweir    {
1011cdf0e10cSrcweir        $ddffilename = qx{cygpath -w "$ddffilename"};
1012cdf0e10cSrcweir        $ddffilename =~ s/\\/\\\\/g;
1013cdf0e10cSrcweir        $ddffilename =~ s/\s*$//g;
1014cdf0e10cSrcweir    }
1015cdf0e10cSrcweir
1016cdf0e10cSrcweir    my $systemcall = "makecab.exe /V1 /F " . $ddffilename;
1017cdf0e10cSrcweir    my $success = make_systemcall($systemcall, $systemcall);
1018cdf0e10cSrcweir    if ( ! $success ) { installer::exiter::exit_program("ERROR: Could not pack cabinet file!", "do_pack_cab_file"); }
1019cdf0e10cSrcweir
1020cdf0e10cSrcweir    chdir($from);
1021cdf0e10cSrcweir
1022cdf0e10cSrcweir    return ($success);
1023cdf0e10cSrcweir}
1024cdf0e10cSrcweir
1025cdf0e10cSrcweir########################################################
1026cdf0e10cSrcweir# Extraction the file extension from a file
1027cdf0e10cSrcweir########################################################
1028cdf0e10cSrcweir
1029cdf0e10cSrcweirsub get_extension
1030cdf0e10cSrcweir{
1031cdf0e10cSrcweir    my ( $file ) = @_;
1032cdf0e10cSrcweir
1033cdf0e10cSrcweir    my $extension = "";
1034cdf0e10cSrcweir
1035cdf0e10cSrcweir    if ( $file =~ /^\s*(.*)\.(\w+?)\s*$/ ) { $extension = $2; }
1036cdf0e10cSrcweir
1037cdf0e10cSrcweir    return $extension;
1038cdf0e10cSrcweir}
1039cdf0e10cSrcweir
1040cdf0e10cSrcweir########################################################
1041cdf0e10cSrcweir# Checking, if a file already contains a certificate.
1042cdf0e10cSrcweir# This must not be overwritten.
1043cdf0e10cSrcweir########################################################
1044cdf0e10cSrcweir
1045cdf0e10cSrcweirsub already_certified
1046cdf0e10cSrcweir{
1047cdf0e10cSrcweir    my ( $filename ) = @_;
1048cdf0e10cSrcweir
1049cdf0e10cSrcweir    my $success = 1;
1050cdf0e10cSrcweir    my $is_certified = 0;
1051cdf0e10cSrcweir
1052cdf0e10cSrcweir    my $systemcall = "signtool.exe verify /q /pa \"$filename\"";
1053cdf0e10cSrcweir    my $returnvalue = system($systemcall);
1054cdf0e10cSrcweir
1055cdf0e10cSrcweir    if ( $returnvalue ) { $success = 0; }
1056cdf0e10cSrcweir
1057cdf0e10cSrcweir    # my $success = make_systemcall($systemcall, $systemcall);
1058cdf0e10cSrcweir
1059cdf0e10cSrcweir    if ( $success )
1060cdf0e10cSrcweir    {
1061cdf0e10cSrcweir        $is_certified = 1;
1062b274bc22SAndre Fischer        $installer::logger::Info->printf("... already certified -> skipping %s ...\n", $filename);
1063cdf0e10cSrcweir    }
1064cdf0e10cSrcweir
1065cdf0e10cSrcweir    return $is_certified;
1066cdf0e10cSrcweir}
1067cdf0e10cSrcweir
1068cdf0e10cSrcweir########################################################
1069cdf0e10cSrcweir# Signing the files, that are included into
1070cdf0e10cSrcweir# cabinet files.
1071cdf0e10cSrcweir########################################################
1072cdf0e10cSrcweir
1073cdf0e10cSrcweirsub sign_files_in_cabinet_files
1074cdf0e10cSrcweir{
1075cdf0e10cSrcweir    my ( $followmeinfohash, $allcabfiles, $pw, $temppath ) = @_;
1076cdf0e10cSrcweir
1077cdf0e10cSrcweir    my $complete_success = 1;
1078cdf0e10cSrcweir    my $from = cwd();
1079cdf0e10cSrcweir
1080cdf0e10cSrcweir    foreach my $cabfilename ( keys %{$allcabfiles} )
1081cdf0e10cSrcweir    {
1082cdf0e10cSrcweir        my $success = 1;
1083cdf0e10cSrcweir
1084cdf0e10cSrcweir        # saving order of files in cab file
1085cdf0e10cSrcweir        my $fileorder = read_cab_file($cabfilename);
1086cdf0e10cSrcweir
1087cdf0e10cSrcweir        # unpack into $working path
1088cdf0e10cSrcweir        my $workingpath = unpack_cab_file($cabfilename, $temppath);
1089cdf0e10cSrcweir
1090cdf0e10cSrcweir        chdir($workingpath);
1091cdf0e10cSrcweir
1092cdf0e10cSrcweir        # sign files
1093cdf0e10cSrcweir        my %allfileshash = ();
1094cdf0e10cSrcweir        foreach my $onefile ( @{$fileorder} )
1095cdf0e10cSrcweir        {
1096cdf0e10cSrcweir            my $extension = get_extension($onefile);
1097cdf0e10cSrcweir            if ( exists( $installer::globals::sign_extensions{$extension} ) )
1098cdf0e10cSrcweir            {
1099cdf0e10cSrcweir                $allfileshash{$onefile} = 1;
1100cdf0e10cSrcweir            }
1101cdf0e10cSrcweir        }
1102cdf0e10cSrcweir        $success = sign_files($followmeinfohash, \%allfileshash, $pw, 1, 0, 0, $temppath);
1103cdf0e10cSrcweir        if ( ! $success ) { $complete_success = 0; }
1104cdf0e10cSrcweir
1105cdf0e10cSrcweir        chdir($from);
1106cdf0e10cSrcweir
1107cdf0e10cSrcweir        # pack into new directory
1108cdf0e10cSrcweir        do_pack_cab_file($cabfilename, $fileorder, $workingpath, $temppath);
1109cdf0e10cSrcweir    }
1110cdf0e10cSrcweir
1111cdf0e10cSrcweir    return $complete_success;
1112cdf0e10cSrcweir}
1113cdf0e10cSrcweir
1114cdf0e10cSrcweir########################################################
1115cdf0e10cSrcweir# Comparing the content of two directories.
1116cdf0e10cSrcweir# Only filesize is compared.
1117cdf0e10cSrcweir########################################################
1118cdf0e10cSrcweir
1119cdf0e10cSrcweirsub compare_directories
1120cdf0e10cSrcweir{
1121cdf0e10cSrcweir    my ( $dir1, $dir2, $files ) = @_;
1122cdf0e10cSrcweir
1123cdf0e10cSrcweir    $dir1 =~ s/\\\s*//;
1124cdf0e10cSrcweir    $dir2 =~ s/\\\s*//;
1125cdf0e10cSrcweir    $dir1 =~ s/\/\s*//;
1126cdf0e10cSrcweir    $dir2 =~ s/\/\s*//;
1127cdf0e10cSrcweir
1128cdf0e10cSrcweir    my $infoline = "Comparing directories: $dir1 and $dir2\n";
1129b274bc22SAndre Fischer    $installer::logger::Lang->print($infoline);
1130cdf0e10cSrcweir
1131cdf0e10cSrcweir    foreach my $onefile ( @{$files} )
1132cdf0e10cSrcweir    {
1133cdf0e10cSrcweir        my $file1 = $dir1 . $installer::globals::separator . $onefile;
1134cdf0e10cSrcweir        my $file2 = $dir2 . $installer::globals::separator . $onefile;
1135cdf0e10cSrcweir
1136cdf0e10cSrcweir        if ( ! -f $file1 ) { installer::exiter::exit_program("ERROR: Missing file : $file1!", "compare_directories"); }
1137cdf0e10cSrcweir        if ( ! -f $file2 ) { installer::exiter::exit_program("ERROR: Missing file : $file2!", "compare_directories"); }
1138cdf0e10cSrcweir
1139cdf0e10cSrcweir        my $size1 = -s $file1;
1140cdf0e10cSrcweir        my $size2 = -s $file2;
1141cdf0e10cSrcweir
1142cdf0e10cSrcweir        $infoline = "Comparing files: $file1 ($size1) and $file2 ($size2)\n";
1143b274bc22SAndre Fischer        $installer::logger::Lang->print($infoline);
1144cdf0e10cSrcweir
1145cdf0e10cSrcweir        if ( $size1 != $size2 )
1146cdf0e10cSrcweir        {
1147cdf0e10cSrcweir            installer::exiter::exit_program("ERROR: File defect after copy (different size) $file1 ($size1 bytes) and $file2 ($size2 bytes)!", "compare_directories");
1148cdf0e10cSrcweir        }
1149cdf0e10cSrcweir    }
1150cdf0e10cSrcweir}
1151cdf0e10cSrcweir
1152cdf0e10cSrcweir########################################################
1153cdf0e10cSrcweir# Signing an existing Windows installation set.
1154cdf0e10cSrcweir########################################################
1155cdf0e10cSrcweir
1156cdf0e10cSrcweirsub sign_install_set
1157cdf0e10cSrcweir{
1158cdf0e10cSrcweir    my ($followmeinfohash, $make_copy, $temppath) = @_;
1159cdf0e10cSrcweir
1160cdf0e10cSrcweir    my $installsetpath = $followmeinfohash->{'finalinstalldir'};
1161cdf0e10cSrcweir
1162cdf0e10cSrcweir    installer::logger::include_header_into_logfile("Start: Signing installation set $installsetpath");
1163cdf0e10cSrcweir
1164cdf0e10cSrcweir    my $complete_success = 1;
1165cdf0e10cSrcweir    my $success = 1;
1166cdf0e10cSrcweir
1167cdf0e10cSrcweir    my $infoline = "Signing installation set in $installsetpath\n";
1168b274bc22SAndre Fischer    $installer::logger::Lang->print($infoline);
1169cdf0e10cSrcweir
1170cdf0e10cSrcweir    # check required files.
1171cdf0e10cSrcweir    if ( ! $installer::globals::signfiles_checked ) { check_system_path(); }
1172cdf0e10cSrcweir
1173*8ac570c6Smseidel    # get certificate information
1174cdf0e10cSrcweir    my $pw = get_pw($installer::globals::pwfile);
1175cdf0e10cSrcweir
1176cdf0e10cSrcweir    # making a copy of the installation set, if required
1177cdf0e10cSrcweir    if ( $make_copy ) { $installsetpath = copy_install_set($installsetpath); }
1178cdf0e10cSrcweir    else { $installsetpath = rename_install_set($installsetpath); }
1179cdf0e10cSrcweir
1180cdf0e10cSrcweir    # collecting all files in the installation set
1181cdf0e10cSrcweir    my ($allcabfiles, $allfiles, $msidatabase, $contains_external_cabfiles, $contains_msidatabase, $sourcefiles) = analyze_installset_content($installsetpath);
1182cdf0e10cSrcweir
1183cdf0e10cSrcweir    if ( $make_copy ) { compare_directories($installsetpath, $followmeinfohash->{'finalinstalldir'}, $sourcefiles); }
1184cdf0e10cSrcweir
1185cdf0e10cSrcweir    # changing into installation set
1186cdf0e10cSrcweir    my $from = cwd();
1187cdf0e10cSrcweir    my $fullmsidatabase = $installsetpath . $installer::globals::separator . $msidatabase;
1188cdf0e10cSrcweir
1189cdf0e10cSrcweir    if( $^O =~ /cygwin/i )
1190cdf0e10cSrcweir    {
1191cdf0e10cSrcweir        $fullmsidatabase = qx{cygpath -w "$fullmsidatabase"};
1192cdf0e10cSrcweir        $fullmsidatabase =~ s/\\/\\\\/g;
1193cdf0e10cSrcweir        $fullmsidatabase =~ s/\s*$//g;
1194cdf0e10cSrcweir    }
1195cdf0e10cSrcweir
1196cdf0e10cSrcweir    chdir($installsetpath);
1197cdf0e10cSrcweir
1198cdf0e10cSrcweir    if ( $contains_msidatabase )
1199cdf0e10cSrcweir    {
1200cdf0e10cSrcweir        # exclude media table from msi database and get all diskids.
1201cdf0e10cSrcweir        my ( $cabfilehash, $filenamehash, $lastsequencehash ) = collect_diskid_from_media_table($msidatabase, $followmeinfohash->{'languagestring'});
1202cdf0e10cSrcweir
1203cdf0e10cSrcweir        # Check, if there are internal cab files
1204cdf0e10cSrcweir        my ( $contains_internal_cabfiles, $all_internal_cab_files) = check_for_internal_cabfiles($cabfilehash);
1205cdf0e10cSrcweir
1206cdf0e10cSrcweir        if ( $contains_internal_cabfiles )
1207cdf0e10cSrcweir        {
1208cdf0e10cSrcweir            my $cabpath = get_cab_path($temppath);
1209cdf0e10cSrcweir            chdir($cabpath);
1210cdf0e10cSrcweir
1211cdf0e10cSrcweir            # Exclude all cabinet files from database
1212cdf0e10cSrcweir            $success = extract_cabs_from_database($fullmsidatabase, $all_internal_cab_files);
1213cdf0e10cSrcweir            if ( ! $success ) { $complete_success = 0; }
1214cdf0e10cSrcweir
1215cdf0e10cSrcweir            if ( $installer::globals::internal_cabinet_signing ) { sign_files_in_cabinet_files($followmeinfohash, $all_internal_cab_files, $pw, $temppath); }
1216cdf0e10cSrcweir
1217cdf0e10cSrcweir            $success = sign_files($followmeinfohash, $all_internal_cab_files, $pw, 0, $filenamehash, $lastsequencehash, $temppath);
1218cdf0e10cSrcweir            if ( ! $success ) { $complete_success = 0; }
1219cdf0e10cSrcweir            $success = msicert_database($fullmsidatabase, $all_internal_cab_files, $cabfilehash, 1);
1220cdf0e10cSrcweir            if ( ! $success ) { $complete_success = 0; }
1221cdf0e10cSrcweir
1222cdf0e10cSrcweir            # Include all cabinet files into database
1223cdf0e10cSrcweir            $success = include_cabs_into_database($fullmsidatabase, $all_internal_cab_files);
1224cdf0e10cSrcweir            if ( ! $success ) { $complete_success = 0; }
1225cdf0e10cSrcweir            chdir($installsetpath);
1226cdf0e10cSrcweir        }
1227cdf0e10cSrcweir
1228cdf0e10cSrcweir        # Warning: There might be a problem with very big cabinet files
1229cdf0e10cSrcweir        # signing all external cab files first
1230cdf0e10cSrcweir        if ( $contains_external_cabfiles )
1231cdf0e10cSrcweir        {
1232cdf0e10cSrcweir            if ( $installer::globals::internal_cabinet_signing ) { sign_files_in_cabinet_files($followmeinfohash, $allcabfiles, $pw, $temppath); }
1233cdf0e10cSrcweir
1234cdf0e10cSrcweir            $success = sign_files($followmeinfohash, $allcabfiles, $pw, 0, $filenamehash, $lastsequencehash, $temppath);
1235cdf0e10cSrcweir            if ( ! $success ) { $complete_success = 0; }
1236cdf0e10cSrcweir            $success = msicert_database($msidatabase, $allcabfiles, $cabfilehash, 0);
1237cdf0e10cSrcweir            if ( ! $success ) { $complete_success = 0; }
1238cdf0e10cSrcweir        }
1239cdf0e10cSrcweir    }
1240cdf0e10cSrcweir
1241cdf0e10cSrcweir    # finally all other files can be signed
1242cdf0e10cSrcweir    $success = sign_files($followmeinfohash, $allfiles, $pw, 0, 0, 0, $temppath);
1243cdf0e10cSrcweir    if ( ! $success ) { $complete_success = 0; }
1244cdf0e10cSrcweir
1245cdf0e10cSrcweir    # and changing back
1246cdf0e10cSrcweir    chdir($from);
1247cdf0e10cSrcweir
1248cdf0e10cSrcweir    installer::logger::include_header_into_logfile("End: Signing installation set $installsetpath");
1249cdf0e10cSrcweir
1250cdf0e10cSrcweir    return ($installsetpath);
1251cdf0e10cSrcweir}
1252cdf0e10cSrcweir
1253cdf0e10cSrcweir1;
1254