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::windows::registry;
25
26use installer::files;
27use installer::globals;
28use installer::worker;
29use installer::windows::msiglobal;
30use installer::windows::idtglobal;
31
32use strict;
33
34#####################################################
35# Generating the component name from a registryitem
36#####################################################
37
38sub get_registry_component_name
39{
40	my ($registryref, $allvariables) = @_;
41
42	# In this function exists the rule to create components from registryitems
43	# Rule:
44	# The componentname can be directly taken from the ModuleID.
45	# All registryitems belonging to one module can get the same component.
46
47	my $componentname = "";
48	my $isrootmodule = 0;
49
50	if ($registryref->{'ModuleID'})
51    {
52        $componentname = $registryref->{'ModuleID'};
53    }
54
55	$componentname =~ s/\\/\_/g;
56	$componentname =~ s/\//\_/g;
57	$componentname =~ s/\-/\_/g;
58	$componentname =~ s/\_\s*$//g;
59
60	$componentname = lc($componentname);	# componentnames always lowercase
61
62	if ( $componentname eq "gid_module_root" )
63    {
64        $isrootmodule = 1;
65    }
66
67	# Attention: Maximum length for the componentname is 72
68
69	# identifying this component as registryitem component
70	$componentname = "registry_" . $componentname;
71
72	$componentname =~ s/gid_module_/g_m_/g;
73	$componentname =~ s/_optional_/_o_/g;
74	$componentname =~ s/_javafilter_/_jf_/g;
75
76	# This componentname must be more specific
77	my $addon = "_";
78	if ($allvariables->{'PRODUCTNAME'})
79    {
80        $addon .= $allvariables->{'PRODUCTNAME'};
81    }
82
83    # Append the version number.
84    # Previously that was the full version number as provided by 'PRODUCTVERSION'.
85    # But MSI patches introduce the restriction that component names must not change.
86    # Use just the major version number.
87    my $version = $allvariables->{"BRANDPACKAGEVERSION"} // "";
88    $addon .= $version;
89	$addon = lc($addon);
90	$addon =~ s/ //g;
91	$addon =~ s/-//g;
92	$addon =~ s/\.//g;
93
94	$componentname = $componentname . $addon;
95
96	my $styles = $registryref->{'Styles'};
97	if (defined $styles)
98    {
99        if (($styles =~ /\bLANGUAGEPACK\b/) && $installer::globals::languagepack)
100        {
101            $componentname .= "_lang";
102        }
103        if ($styles =~ /\bALWAYS_REQUIRED\b/)
104        {
105            $componentname .= "_forced";
106        }
107    }
108
109	# Attention: Maximum length for the componentname is 72
110	# %installer::globals::allregistrycomponents_in_this_database_ : resetted for each database
111	# %installer::globals::allregistrycomponents_ : not resetted for each database
112	# Component strings must be unique for the complete product, because they are used for
113	# the creation of the globally unique identifier.
114
115	my $fullname = $componentname;  # This can be longer than 72
116
117	if (exists($installer::globals::allregistrycomponents_{$fullname})
118        && ! exists($installer::globals::allregistrycomponents_in_this_database_{$fullname}))
119	{
120		# This is not allowed: One component cannot be installed with different packages.
121		installer::exiter::exit_program(
122            "ERROR: Windows registry component \"$fullname\" is already included into another package. This is not allowed.",
123            "get_registry_component_name");
124	}
125
126	if ( exists($installer::globals::allregistrycomponents_{$fullname}) )
127	{
128		$componentname = $installer::globals::allregistrycomponents_{$fullname};
129	}
130	else
131	{
132		if ( length($componentname) > 70 )
133		{
134			$componentname = generate_new_short_registrycomponentname($componentname); # This has to be unique for the complete product, not only one package
135		}
136
137		$installer::globals::allregistrycomponents_{$fullname} = $componentname;
138		$installer::globals::allregistrycomponents_in_this_database_{$fullname} = 1;
139	}
140
141	if ( $isrootmodule )
142    {
143        $installer::globals::registryrootcomponent = $componentname;
144    }
145
146	return $componentname;
147}
148
149#########################################################
150# Create a shorter version of a long component name,
151# because maximum length in msi database is 72.
152# Attention: In multi msi installation sets, the short
153# names have to be unique over all packages, because
154# this string is used to create the globally unique id
155# -> no resetting of
156# %installer::globals::allshortregistrycomponents
157# after a package was created.
158#########################################################
159
160sub generate_new_short_registrycomponentname
161{
162	my ($componentname) = @_;
163
164	my $startversion = substr($componentname, 0, 60); # taking only the first 60 characters
165	my $subid = installer::windows::msiglobal::calculate_id($componentname, 9); # taking only the first 9 digits
166	my $shortcomponentname = $startversion . "_" . $subid;
167
168	if ( exists($installer::globals::allshortregistrycomponents{$shortcomponentname}) ) { installer::exiter::exit_program("Failed to create unique component name: \"$shortcomponentname\"", "generate_new_short_registrycomponentname"); }
169
170	$installer::globals::allshortregistrycomponents{$shortcomponentname} = 1;
171
172	return $shortcomponentname;
173}
174
175##############################################################
176# Returning identifier for registry table.
177##############################################################
178
179sub get_registry_identifier
180{
181	my ($registry) = @_;
182
183	my $identifier = "";
184
185	if ( $registry->{'gid'} ) { $identifier = $registry->{'gid'}; }
186
187	$identifier = lc($identifier);	# always lower case
188
189	# Attention: Maximum length is 72
190
191	$identifier =~ s/gid_regitem_/g_r_/;
192	$identifier =~ s/_soffice_/_s_/;
193	$identifier =~ s/_clsid_/_c_/;
194	$identifier =~ s/_currentversion_/_cv_/;
195	$identifier =~ s/_microsoft_/_ms_/;
196	$identifier =~ s/_manufacturer_/_mf_/;
197	$identifier =~ s/_productname_/_pn_/;
198	$identifier =~ s/_productversion_/_pv_/;
199	$identifier =~ s/_staroffice_/_so_/;
200	$identifier =~ s/_software_/_sw_/;
201	$identifier =~ s/_capabilities_/_cap_/;
202	$identifier =~ s/_classpath_/_cp_/;
203	$identifier =~ s/_extension_/_ex_/;
204	$identifier =~ s/_fileassociations_/_fa_/;
205	$identifier =~ s/_propertysheethandlers_/_psh_/;
206	$identifier =~ s/__/_/g;
207
208	# Saving this in the registry collector
209
210	$registry->{'uniquename'} = $identifier;
211
212	return $identifier;
213}
214
215##################################################################
216# Returning root value for registry table.
217##################################################################
218
219sub get_registry_root
220{
221	my ($registry) = @_;
222
223	my $rootvalue = 0;	# Default: Parent is KKEY_CLASSES_ROOT
224	my $scproot = "";
225
226	if ( $registry->{'ParentID'} ) { $scproot = $registry->{'ParentID'}; }
227
228	if ( $scproot eq "PREDEFINED_HKEY_LOCAL_MACHINE" ) { $rootvalue = -1; }
229
230	if ( $scproot eq "PREDEFINED_HKEY_CLASSES_ROOT" ) { $rootvalue = 0; }
231
232	if ( $scproot eq "PREDEFINED_HKEY_CURRENT_USER_ONLY" ) { $rootvalue = 1; }
233
234	if ( $scproot eq "PREDEFINED_HKEY_LOCAL_MACHINE_ONLY" ) { $rootvalue = 2; }
235
236	return $rootvalue;
237}
238
239##############################################################
240# Returning key for registry table.
241##############################################################
242
243sub get_registry_key
244{
245	my ($registry, $allvariableshashref) = @_;
246
247	my $key = "";
248
249	if ( $registry->{'Subkey'} ) { $key = $registry->{'Subkey'}; }
250
251	if ( $key =~ /\%/ ) { $key = installer::worker::replace_variables_in_string($key, $allvariableshashref); }
252
253	return $key;
254}
255
256##############################################################
257# Returning name for registry table.
258##############################################################
259
260sub get_registry_name
261{
262	my ($registry, $allvariableshashref) = @_;
263
264	my $name = "";
265
266	if ( $registry->{'Name'} ) { $name = $registry->{'Name'}; }
267
268	if ( $name =~ /\%/ ) { $name = installer::worker::replace_variables_in_string($name, $allvariableshashref); }
269
270	return $name;
271}
272
273##############################################################
274# Returning value for registry table.
275##############################################################
276
277sub get_registry_value
278{
279	my ($registry, $allvariableshashref) = @_;
280
281	my $value = "";
282
283	if ( $registry->{'Value'} ) { $value = $registry->{'Value'}; }
284
285	$value =~ s/\\\"/\"/g;	# no more masquerading of '"'
286	$value =~ s/\\\\\s*$/\\/g;	# making "\\" at end of value to "\"
287	$value =~ s/\<progpath\>/\[INSTALLLOCATION\]/;
288	$value =~ s/\[INSTALLLOCATION\]\\/\[INSTALLLOCATION\]/;	# removing "\" after "[INSTALLLOCATION]"
289
290	if ( $value =~ /\%/ ) { $value = installer::worker::replace_variables_in_string($value, $allvariableshashref); }
291
292	return $value;
293}
294
295##############################################################
296# Returning 64 bit value for registry table.
297##############################################################
298
299sub get_registry_val64
300{
301	my ($registry, $allvariableshashref) = @_;
302
303	my $value = "";
304
305	if ( $registry->{'Val64'} ) { $value = $registry->{'Val64'}; }
306
307	$value =~ s/\\\"/\"/g;	# no more masquerading of '"'
308	$value =~ s/\\\\\s*$/\\/g;	# making "\\" at end of value to "\"
309	$value =~ s/\<progpath\>/\[INSTALLLOCATION\]/;
310	$value =~ s/\[INSTALLLOCATION\]\\/\[INSTALLLOCATION\]/;	# removing "\" after "[INSTALLLOCATION]"
311
312	if ( $value =~ /\%/ ) { $value = installer::worker::replace_variables_in_string($value, $allvariableshashref); }
313
314	return $value;
315}
316
317
318######################################################
319# Adding the content of
320# @installer::globals::userregistrycollector
321# to the registry table. The content was collected
322# in create_files_table() in file.pm.
323######################################################
324
325sub add_userregs_to_registry_table
326{
327	my ( $registrytable, $allvariables ) = @_;
328
329	for ( my $i = 0; $i <= $#installer::globals::userregistrycollector; $i++ )
330	{
331		my $onefile = $installer::globals::userregistrycollector[$i];
332
333		my $styles = "";
334		if ( $onefile->{'Styles'} ) { $styles = $onefile->{'Styles'}; }
335
336		my %registry = ();
337
338		$registry{'Registry'} = $onefile->{'userregkeypath'};
339		$registry{'Root'} = "1";  # always HKCU
340		$registry{'Key'} = "Software\\$allvariables->{'MANUFACTURER'}\\$allvariables->{'PRODUCTNAME'} $allvariables->{'PRODUCTVERSION'}\\";
341		if ( $onefile->{'needs_user_registry_key'} ) { $registry{'Key'} = $registry{'Key'} . "StartMenu"; }
342		else { $registry{'Key'} = $registry{'Key'} . "ShellNew"; }
343		$registry{'Name'} = $onefile->{'Name'};
344		$registry{'Value'} = "1";
345		$registry{'Component_'} = $onefile->{'componentname'};
346
347		my $oneline = $registry{'Registry'} . "\t" . $registry{'Root'} . "\t" . $registry{'Key'} . "\t"
348					. $registry{'Name'} . "\t" . $registry{'Value'} . "\t" . $registry{'Component_'} . "\n";
349
350		push(@{$registrytable}, $oneline);
351	}
352}
353
354######################################################
355# Creating the file Registry.idt dynamically
356# Content:
357# Registry Root Key Name Value Component_
358######################################################
359sub prepare_registry_table ($$$)
360{
361	my ($registryref, $languagesarrayref, $allvariableshashref) = @_;
362
363    my %table_data = ();
364	foreach my $onelanguage (@$languagesarrayref)
365	{
366        my $table_items = [];
367		foreach my $oneregistry (@$registryref)
368		{
369			# Controlling the language!
370			# Only language independent folderitems or folderitems with the correct language
371			# will be included into the table
372
373			next if $oneregistry->{'ismultilingual'}
374                && $oneregistry->{'specificlanguage'} ne $onelanguage;
375
376			my %registry = ();
377
378			$registry{'Registry'} = get_registry_identifier($oneregistry);
379			$registry{'Root'} = get_registry_root($oneregistry);
380			$registry{'Key'} = get_registry_key($oneregistry, $allvariableshashref);
381			$registry{'Name'} = get_registry_name($oneregistry, $allvariableshashref);
382			$registry{'Value'} = get_registry_value($oneregistry, $allvariableshashref);
383			$registry{'Val64'} = get_registry_val64($oneregistry, $allvariableshashref);
384            my $component_name = get_registry_component_name($oneregistry, $allvariableshashref);
385            $oneregistry->{'componentname'} = $component_name;
386			$registry{'Component_'} = $component_name;
387
388			# Collecting all components with DONT_DELETE style
389			my $style = $oneregistry->{'Styles'} // "";
390            $registry{'styles'} = $style;
391
392			if ( $style =~ /\bDONT_DELETE\b/ )
393            {
394                $installer::globals::dontdeletecomponents{$component_name} = 1;
395            }
396
397			# Saving upgradekey to write this into setup.ini for minor upgrades
398			if ( $style =~ /\bUPGRADEKEY\b/ )
399            {
400                $installer::globals::minorupgradekey = $registry{'Key'};
401            }
402
403			# Collecting all registry components with ALWAYS_REQUIRED style
404			if ( ! ( $style =~ /\bALWAYS_REQUIRED\b/ ))
405			{
406				# Setting a component condition for unforced registry components!
407				# Only write into registry, if WRITE_REGISTRY is set.
408				if ( $oneregistry->{'ComponentCondition'} ) { $oneregistry->{'ComponentCondition'} = "(" . $oneregistry->{'ComponentCondition'} . ") AND (WRITE_REGISTRY=1)"; }
409				else { $oneregistry->{'ComponentCondition'} = "WRITE_REGISTRY=1"; }
410			}
411
412			# Collecting all component conditions
413			if ( $oneregistry->{'ComponentCondition'} )
414			{
415				if ( ! exists($installer::globals::componentcondition{$registry{'Component_'}}))
416				{
417					$installer::globals::componentcondition{$registry{'Component_'}} = $oneregistry->{'ComponentCondition'};
418				}
419			}
420
421            push @$table_items, \%registry;
422		}
423        $table_data{$onelanguage} = $table_items;
424    }
425
426    return \%table_data;
427}
428
429
430
431
432sub collect_registry_components ($)
433{
434    my ($table_data) = @_;
435
436    my %components = ();
437    foreach my $language_data (values %$table_data)
438    {
439        foreach my $item (@$language_data)
440        {
441			$components{$item->{'Component_'}} = 1;
442        }
443    }
444    return keys %components;
445}
446
447
448
449
450sub translate_component_names ($$$)
451{
452    my ($translation_map, $registry_items, $table_data) = @_;
453
454    my $replacement_count = 0;
455    foreach my $item (@$registry_items)
456    {
457        my $translated_name = $translation_map->{$item->{'componentname'}};
458        if (defined $translated_name)
459        {
460            $item->{'componentname'} = $translated_name;
461            ++$replacement_count;
462        }
463    }
464    $installer::logger::Lang->printf("replaced %d component names in registry items\n", $replacement_count);
465
466    $replacement_count = 0;
467    foreach my $language_data (values %$table_data)
468    {
469        foreach my $item (@$language_data)
470        {
471            my $translated_name = $translation_map->{$item->{'Component_'}};
472            if (defined $translated_name)
473            {
474                $item->{'Component_'} = $translated_name;
475                ++$replacement_count;
476            }
477        }
478    }
479    $installer::logger::Lang->printf("replaced %d component names in registry table\n", $replacement_count);
480}
481
482
483
484
485sub create_registry_table_32 ($$$$)
486{
487	my ($basedir, $languagesarrayref, $allvariableshashref, $table_data) = @_;
488
489	foreach my $onelanguage (@$languagesarrayref)
490	{
491		my @registrytable = ();
492        installer::windows::idtglobal::write_idt_header(\@registrytable, "registry");
493
494		foreach my $item (@{$table_data->{$onelanguage}})
495		{
496			next if $item->{'styles'} =~ /\bX64_ONLY\b/;
497
498			my $oneline = join("\t",
499                $item->{'Registry'},
500                $item->{'Root'},
501                $item->{'Key'},
502                $item->{'Name'},
503                $item->{'Value'},
504                $item->{'Component_'})
505                . "\n";
506
507            push(@registrytable, $oneline);
508		}
509
510		# If there are added user registry keys for files collected in
511		# @installer::globals::userregistrycollector (file.pm), then
512		# this registry keys have to be added now. This is necessary for
513		# files in PREDEFINED_OSSHELLNEWDIR, because their component
514		# needs as KeyPath a RegistryItem in HKCU.
515
516		if ( $installer::globals::addeduserregitrykeys )
517        {
518            add_userregs_to_registry_table(\@registrytable, $allvariableshashref);
519        }
520
521		# Save the database file.
522		my $registrytablename = $basedir . $installer::globals::separator . "Registry.idt" . "." . $onelanguage;
523		installer::files::save_file($registrytablename ,\@registrytable);
524        $installer::logger::Lang->printf("Created idt file: %s\n", $registrytablename);
525	}
526}
527
528
529
530
531sub create_registry_table_64 ($$$$)
532{
533	my ($basedir, $languagesarrayref, $allvariableshashref, $table_data) = @_;
534
535	foreach my $onelanguage (@$languagesarrayref)
536	{
537        my @reg64table = ();
538		installer::windows::idtglobal::write_idt_header(\@reg64table, "reg64");
539		foreach my $item (@{$table_data->{$onelanguage}})
540        {
541			next unless $item->{'styles'} =~ /\b(X64|X64_ONLY)\b/;
542
543            my $oneline64 = join("\t",
544                $item->{'Registry'},
545                $item->{'Root'},
546                $item->{'Key'},
547                $item->{'Name'},
548                $item->{'Val64'},
549                $item->{'Component_'})
550                . "\n";
551
552            push(@reg64table , $oneline64);
553        }
554
555        # Save the database file.
556		my $registrytablename = $basedir . $installer::globals::separator . "Reg64.idt" . "." . $onelanguage;
557		installer::files::save_file($registrytablename ,\@reg64table );
558        $installer::logger::Lang->printf("Created idt file: %s\n", $registrytablename);
559	}
560}
561
5621;
563