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 
24 package org.openoffice.setup.Util;
25 
26 import java.io.File;
27 import java.util.Vector;
28 import org.openoffice.setup.InstallData;
29 import org.openoffice.setup.ResourceManager;
30 import org.openoffice.setup.SetupData.SetupDataProvider;
31 import org.openoffice.setup.Util.LogManager;
32 
33 public class Controller {
34 
Controller()35     private Controller() {
36     }
37 
checkPackagePathExistence(InstallData installData)38     static public void checkPackagePathExistence(InstallData installData) {
39         String packagePath = installData.getPackagePath();
40         if (( packagePath == null ) || ( packagePath.equals(""))) {
41             String message = ResourceManager.getString("String_InstallationOngoing_PackagePath_Not_Found");
42             String title = ResourceManager.getString("String_Error");
43             Informer.showErrorMessage(message, title);
44             System.exit(1);
45         }
46     }
47 
checkPackageFormat(InstallData installData)48     static public void checkPackageFormat(InstallData installData) {
49         String packageFormat = installData.getPackageFormat();
50         String os = installData.getOSType();
51 
52         boolean notSupportedPackageFormat = true;
53 
54         // Show warnings for currently not supported combinations of OS and package format.
55         // This has to be adapted if further OS or package formats are supported.
56 
57         if (( os.equalsIgnoreCase("SunOS") ) && ( packageFormat.equalsIgnoreCase("pkg") )) {
58             notSupportedPackageFormat = false;
59         }
60 
61         if (( os.equalsIgnoreCase("Linux") ) && ( packageFormat.equalsIgnoreCase("rpm") )) {
62             notSupportedPackageFormat = false;
63         }
64 
65         // Inform user about not supported package format and exit program
66 
67         if ( notSupportedPackageFormat ) {
68             System.err.println("Error: Package format not supported by this OS!");
69             String mainmessage = ResourceManager.getString("String_Packageformat_Not_Supported");
70             String osstring = ResourceManager.getString("String_Operating_System");
71             String formatstring = ResourceManager.getString("String_Packageformat");
72             String message = mainmessage + "\n" + osstring + ": " + os + "\n" + formatstring + ": " + packageFormat;
73             String title = ResourceManager.getString("String_Error");
74             Informer.showErrorMessage(message, title);
75             System.exit(1);
76         }
77     }
78 
collectSystemLanguages(InstallData installData)79     static public void collectSystemLanguages(InstallData installData) {
80         String pkgCommand = "";
81         String[] pkgCommandArray;
82         String adminFileName = "";
83         String log = "";
84         Vector returnVector = new Vector();
85         Vector returnErrorVector = new Vector();
86         int returnValue;
87 
88         pkgCommand = "locale -a";
89         pkgCommandArray = new String[2];
90         pkgCommandArray[0] = "locale";
91         pkgCommandArray[1] = "-a";
92         returnValue = ExecuteProcess.executeProcessReturnVector(pkgCommandArray, returnVector, returnErrorVector);
93 
94         if ( returnValue == 0 ) {
95             log = pkgCommand + "<br><b>Returns: " + returnValue + " Successful command</b><br>";
96             LogManager.addCommandsLogfileComment(log);
97 
98             // System.err.println("Available languages 1: ");
99             // for (int i = 0; i < returnVector.size(); i++) {
100             //     System.err.println(returnVector.get(i));
101             // }
102 
103             // Collecting "en-US" instead of "en-US.UTF8"
104             Vector realVector = new Vector();
105 
106             for (int i = 0; i < returnVector.size(); i++) {
107                 String oneLang = (String)returnVector.get(i);
108                 int position = oneLang.indexOf(".");
109                 if ( position > -1 ) {
110                     oneLang = oneLang.substring(0, position);
111                 }
112                 if ( ! realVector.contains(oneLang)) {
113                     realVector.add(oneLang);
114                 }
115             }
116 
117             // System.err.println("Available languages 2: ");
118             // for (int i = 0; i < realVector.size(); i++) {
119             //     System.err.println(realVector.get(i));
120             // }
121 
122             installData.setSystemLanguages(realVector);
123         } else {    // an error occurred
124             log = pkgCommand + "<br><b>Returns: " + returnValue + " An error occurred</b><br>";
125             LogManager.addCommandsLogfileComment(log);
126             System.err.println("Error in command: " + pkgCommand);
127             for (int i = 0; i < returnErrorVector.size(); i++) {
128                 LogManager.addCommandsLogfileComment((String)returnErrorVector.get(i));
129                 System.err.println(returnErrorVector.get(i));
130             }
131         }
132     }
133 
createdSubDirectory(String dir)134     static public boolean createdSubDirectory(String dir) {
135         boolean createdDirectory = false;
136         boolean errorShown = false;
137         String subDirName = "testdir";
138         File testDir = new File(dir, subDirName);
139         try {
140             createdDirectory = SystemManager.create_directory(testDir.getPath());
141         }
142         catch (SecurityException ex) {
143             String message = ResourceManager.getString("String_ChooseDirectory_No_Write_Access") + ": " + dir;
144             String title = ResourceManager.getString("String_Error");
145             Informer.showErrorMessage(message, title);
146             errorShown = true;
147         }
148 
149         if (( ! createdDirectory ) && ( ! errorShown )) {
150             String message = ResourceManager.getString("String_ChooseDirectory_No_Write_Access") + ": " + dir;
151             String title = ResourceManager.getString("String_Error");
152             Informer.showErrorMessage(message, title);
153             errorShown = true;
154         }
155 
156         if ( SystemManager.exists_directory(testDir.getPath()) ) {
157             testDir.delete();
158         }
159 
160         return createdDirectory;
161     }
162 
createdDirectory(String dir)163     static public boolean createdDirectory(String dir) {
164         boolean createdDirectory = false;
165         try {
166             createdDirectory = SystemManager.create_directory(dir);
167         }
168         catch (SecurityException ex) {
169             // message = ResourceManager.getString("String_ChooseDirectory_Not_Allowed") + ": " + dir;
170             // title = ResourceManager.getString("String_Error");
171             // Informer.showErrorMessage(message, title);
172         }
173 
174         if ( ! createdDirectory ) {
175             String message = ResourceManager.getString("String_ChooseDirectory_No_Success") + ": " + dir;
176             String title = ResourceManager.getString("String_Error");
177             Informer.showErrorMessage(message, title);
178         }
179 
180         return createdDirectory;
181     }
182 
reducedRootWritePrivileges()183     static public boolean reducedRootWritePrivileges() {
184         Vector vec = new Vector();
185         File dir = new File("/usr");
186         vec.add(dir);
187         dir = new File("/etc");
188         vec.add(dir);
189 
190         boolean restrictedWritePrivilges = false;
191 
192         // Check for zones. If "zonename" is successful and the name is not "global",
193         // this is a "sparse zone".
194         // Alternative: Simply always check, if root has write access in selected directories.
195 
196         for (int i = 0; i < vec.size(); i++) {
197             File directory = (File)vec.get(i);
198             if ( directory.exists() ) {
199                 // do we have write privileges inside the directory
200                 String tempDirName = "temptestdir";
201                 File tempDir = new File(directory, tempDirName);
202 
203                 if ( SystemManager.createDirectory(tempDir) ) {
204                     SystemManager.removeDirectory(tempDir);
205                 } else {
206                     restrictedWritePrivilges = true;
207                     System.err.println("Restricted Root privileges. No write access in " + directory.getPath());
208                     break;
209                 }
210             }
211         }
212 
213         return restrictedWritePrivilges;
214     }
215 
checkForNewerVersion(InstallData installData)216     static public void checkForNewerVersion(InstallData installData) {
217         LogManager.setCommandsHeaderLine("Checking change installation");
218         InstallChangeCtrl.checkInstallChange(installData);
219 
220         if ( installData.newerVersionExists() ) {
221             // Inform user about a newer version installed
222             SetupDataProvider.setNewMacro("DIR", installData.getInstallDefaultDir()); // important for string replacement
223 
224             System.err.println("Error: A newer version is already installed in " + installData.getInstallDefaultDir() + " !");
225             String message1 = ResourceManager.getString("String_Newer_Version_Installed_Found")
226                             + "\n" + installData.getInstallDefaultDir() + "\n";
227             String message2 = ResourceManager.getString("String_Newer_Version_Installed_Remove");
228             String message = message1 + "\n" + message2;
229             String title = ResourceManager.getString("String_Error");
230             Informer.showErrorMessage(message, title);
231             System.exit(1);
232         }
233     }
234 
checkForUidFile(InstallData installData)235     static public void checkForUidFile(InstallData installData) {
236         // check existence of getuid.so
237         File getuidFile = Controller.findUidFile(installData);
238 
239         if (( getuidFile == null ) || (! getuidFile.exists()) ) {
240             // Root privileges required -> abort installation
241             System.err.println("Root privileges required for installation!");
242             String message = ResourceManager.getString("String_Root_Privileges_Required_1") + "\n"
243                            + ResourceManager.getString("String_Root_Privileges_Required_2");
244             String title = ResourceManager.getString("String_Error");
245             Informer.showErrorMessage(message, title);
246             System.exit(1);
247         } else {
248             installData.setGetUidPath(getuidFile.getPath());
249         }
250     }
251 
findUidFile(InstallData data)252     static private File findUidFile(InstallData data) {
253 
254         File getuidFile = null;
255 
256         if ( data.isInstallationMode()) {
257             String getuidpath = System.getProperty("GETUID_PATH");
258 
259             if ( getuidpath != null ) {
260                 getuidFile = new File(getuidpath);
261 
262                 if (( getuidFile.isDirectory() ) && ( ! getuidFile.isFile() )) {
263                     // Testing, if GETUID_PATH only contains the path, not the filename
264                     String defaultfilename = "getuid.so";
265                     getuidFile = new File(getuidpath, defaultfilename);
266 
267                     if ( ! getuidFile.exists() ) {
268                         getuidFile = null;
269                     }
270                 }
271             }
272 
273             // File resourceRoot = data.getResourceRoot();
274             // String getuidString = "getuid.so";
275             // if ( resourceRoot != null ) {
276             //     File getuidDir = new File (data.getInfoRoot(), "getuid");
277             //     getuidFile = new File(getuidDir, getuidString);
278             // }
279 
280         } else {
281             getuidFile = new File(data.getGetUidPath());
282         }
283 
284         return getuidFile;
285     }
286 
287 }
288