001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.tools; 003 004import static org.openstreetmap.josm.tools.I18n.marktr; 005import static org.openstreetmap.josm.tools.I18n.tr; 006import static org.openstreetmap.josm.tools.Utils.getSystemEnv; 007import static org.openstreetmap.josm.tools.Utils.getSystemProperty; 008 009import java.awt.Desktop; 010import java.awt.event.InputEvent; 011import java.awt.event.KeyEvent; 012import java.io.BufferedReader; 013import java.io.File; 014import java.io.IOException; 015import java.io.InputStream; 016import java.net.URISyntaxException; 017import java.nio.charset.StandardCharsets; 018import java.nio.file.Files; 019import java.nio.file.Path; 020import java.nio.file.Paths; 021import java.security.KeyStoreException; 022import java.security.NoSuchAlgorithmException; 023import java.security.cert.CertificateException; 024import java.security.cert.CertificateFactory; 025import java.security.cert.X509Certificate; 026import java.util.Arrays; 027import java.util.Collection; 028import java.util.HashSet; 029import java.util.Locale; 030import java.util.Optional; 031import java.util.Set; 032import java.util.concurrent.ExecutionException; 033import java.util.regex.Matcher; 034import java.util.regex.Pattern; 035 036import org.openstreetmap.josm.data.Preferences; 037import org.openstreetmap.josm.io.CertificateAmendment.NativeCertAmend; 038import org.openstreetmap.josm.spi.preferences.Config; 039 040/** 041 * {@code PlatformHook} implementation for Unix systems. 042 * @since 1023 043 */ 044public class PlatformHookUnixoid implements PlatformHook { 045 046 private String osDescription; 047 048 @Override 049 public Platform getPlatform() { 050 return Platform.UNIXOID; 051 } 052 053 @Override 054 public void preStartupHook() { 055 // See #12022, #16666 - Disable GNOME ATK Java wrapper as it causes a lot of serious trouble 056 if (isDebianOrUbuntu()) { 057 // TODO: find a way to disable ATK wrapper on Java >= 9 058 // We should probably be able to do that by embedding a no-op AccessibilityProvider in our jar 059 // so that it is loaded by ServiceLoader without error 060 // But this require to compile at least one class with Java 9 061 } 062 } 063 064 @Override 065 public void startupHook(JavaExpirationCallback javaCallback, SanityCheckCallback sanityCheckCallback) { 066 PlatformHook.super.startupHook(javaCallback, sanityCheckCallback); 067 } 068 069 @Override 070 public void openUrl(String url) throws IOException { 071 // Note: xdg-open may not always give "expected" results, see #23804. 072 // At the time, it appeared to be primarily a Brave browser problem. 073 for (String program : Config.getPref().getList("browser.unix", 074 Arrays.asList("#DESKTOP#", "xdg-open", "$BROWSER", "gnome-open", "kfmclient openURL", "firefox"))) { 075 try { 076 if ("#DESKTOP#".equals(program)) { 077 Desktop.getDesktop().browse(Utils.urlToURI(url)); 078 } else { 079 if (program.startsWith("$")) { 080 program = System.getenv().get(program.substring(1)); 081 } 082 Runtime.getRuntime().exec(new String[]{program, url}); 083 } 084 return; 085 } catch (IOException | UnsupportedOperationException | URISyntaxException e) { 086 Logging.warn(e); 087 } 088 } 089 } 090 091 @Override 092 @SuppressWarnings("squid:S103") // NOSONAR LineLength 093 public void initSystemShortcuts() { 094 final String reserved = marktr("reserved"); 095 // CHECKSTYLE.OFF: LineLength 096 // TODO: Insert system shortcuts here. See Windows and especially OSX to see how to. 097 /* POSSIBLE SHORTCUTS: F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12 */ 098 for (int i = KeyEvent.VK_F1; i <= KeyEvent.VK_F12; ++i) { 099 Shortcut.registerSystemShortcut("screen:toggle"+i, tr(reserved), i, InputEvent.CTRL_DOWN_MASK | InputEvent.ALT_DOWN_MASK) 100 .setAutomatic(); 101 } 102 Shortcut.registerSystemShortcut("system:reset", tr(reserved), KeyEvent.VK_DELETE, InputEvent.CTRL_DOWN_MASK | InputEvent.ALT_DOWN_MASK) 103 .setAutomatic(); 104 Shortcut.registerSystemShortcut("system:resetX", tr(reserved), KeyEvent.VK_BACK_SPACE, InputEvent.CTRL_DOWN_MASK | InputEvent.ALT_DOWN_MASK) 105 .setAutomatic(); 106 // CHECKSTYLE.ON: LineLength 107 } 108 109 @Override 110 public String getDefaultStyle() { 111 return "javax.swing.plaf.metal.MetalLookAndFeel"; 112 } 113 114 /** 115 * Returns desktop environment based on the environment variable {@code XDG_CURRENT_DESKTOP}. 116 * @return desktop environment. 117 */ 118 public Optional<String> getDesktopEnvironment() { 119 return Optional.ofNullable(getSystemEnv("XDG_CURRENT_DESKTOP")).filter(s -> !s.isEmpty()); 120 } 121 122 /** 123 * Determines if the distribution is Debian or Ubuntu, or a derivative. 124 * @return {@code true} if the distribution is Debian, Ubuntu or Mint, {@code false} otherwise 125 */ 126 public static boolean isDebianOrUbuntu() { 127 try { 128 String dist = Utils.execOutput(Arrays.asList("lsb_release", "-i", "-s")); 129 return "Debian".equalsIgnoreCase(dist) || "Ubuntu".equalsIgnoreCase(dist) || "Mint".equalsIgnoreCase(dist); 130 } catch (IOException | ExecutionException | InterruptedException e) { 131 // lsb_release is not available on all Linux systems, so don't log at warning level 132 Logging.debug(e); 133 return false; 134 } 135 } 136 137 /** 138 * Get the package name including detailed version. 139 * @param packageNames The possible package names (when a package can have different names on different distributions) 140 * @return The package name and package version if it can be identified, null otherwise 141 * @since 7314 142 */ 143 public static String getPackageDetails(String... packageNames) { 144 try { 145 // CHECKSTYLE.OFF: SingleSpaceSeparator 146 boolean dpkg = Paths.get("/usr/bin/dpkg-query").toFile().exists(); 147 boolean eque = Paths.get("/usr/bin/equery").toFile().exists(); 148 boolean rpm = Paths.get("/bin/rpm").toFile().exists(); 149 // CHECKSTYLE.ON: SingleSpaceSeparator 150 if (dpkg || rpm || eque) { 151 for (String packageName : packageNames) { 152 String[] args; 153 if (dpkg) { 154 args = new String[] {"dpkg-query", "--show", "--showformat", "${Architecture}-${Version}", packageName}; 155 } else if (eque) { 156 args = new String[] {"equery", "-q", "list", "-e", "--format=$fullversion", packageName}; 157 } else { 158 args = new String[] {"rpm", "-q", "--qf", "%{arch}-%{version}", packageName}; 159 } 160 try { 161 String version = Utils.execOutput(Arrays.asList(args)); 162 if (!Utils.isEmpty(version)) { 163 return packageName + ':' + version; 164 } 165 } catch (ExecutionException e) { 166 // Package does not exist, continue 167 Logging.trace(e); 168 } 169 } 170 } 171 } catch (IOException | InterruptedException e) { 172 Logging.warn(e); 173 } 174 return null; 175 } 176 177 /** 178 * Get the Java package name including detailed version. 179 * <p> 180 * Some Java bugs are specific to a certain security update, so in addition 181 * to the Java version, we also need the exact package version. 182 * 183 * @return The package name and package version if it can be identified, null otherwise 184 */ 185 public String getJavaPackageDetails() { 186 String home = getSystemProperty("java.home"); 187 if (home == null) { 188 return null; 189 } 190 Matcher matcher = Pattern.compile("java-(\\d+)-openjdk").matcher(home); 191 if (matcher.find()) { 192 String version = matcher.group(1); 193 return getPackageDetails("openjdk-" + version + "-jre", "java-" + version + "-openjdk"); 194 } else if (home.contains("java-openjdk")) { 195 return getPackageDetails("java-openjdk"); 196 } else if (home.contains("icedtea")) { 197 return getPackageDetails("icedtea-bin"); 198 } else if (home.contains("oracle")) { 199 return getPackageDetails("oracle-jdk-bin", "oracle-jre-bin"); 200 } 201 return null; 202 } 203 204 /** 205 * Get the Web Start package name including detailed version. 206 * <p> 207 * OpenJDK packages are shipped with icedtea-web package, 208 * but its version generally does not match main java package version. 209 * <p> 210 * Simply return {@code null} if there's no separate package for Java WebStart. 211 * 212 * @return The package name and package version if it can be identified, null otherwise 213 */ 214 public String getWebStartPackageDetails() { 215 if (isOpenJDK()) { 216 return getPackageDetails("icedtea-netx", "icedtea-web"); 217 } 218 return null; 219 } 220 221 /** 222 * Get the Gnome ATK wrapper package name including detailed version. 223 * <p> 224 * Debian and Ubuntu derivatives come with a pre-enabled accessibility software 225 * completely buggy that makes Swing crash in a lot of different ways. 226 * <p> 227 * Simply return {@code null} if it's not found. 228 * 229 * @return The package name and package version if it can be identified, null otherwise 230 */ 231 public String getAtkWrapperPackageDetails() { 232 if (isOpenJDK() && isDebianOrUbuntu()) { 233 return getPackageDetails("libatk-wrapper-java"); 234 } 235 return null; 236 } 237 238 private String buildOSDescription() { 239 String osName = getSystemProperty("os.name"); 240 if ("Linux".equalsIgnoreCase(osName)) { 241 try { 242 // Try lsb_release (only available on LSB-compliant Linux systems, 243 // see https://www.linuxbase.org/lsb-cert/productdir.php?by_prod ) 244 String line = exec("lsb_release", "-ds"); 245 if (!Utils.isEmpty(line)) { 246 line = line.replaceAll("\"+", ""); 247 line = line.replace("NAME=", ""); // strange code for some Gentoo's 248 if (line.startsWith("Linux ")) // e.g. Linux Mint 249 return line; 250 else if (!line.isEmpty()) 251 return "Linux " + line; 252 } 253 } catch (IOException e) { 254 Logging.debug(e); 255 // Non LSB-compliant Linux system. List of common fallback release files: http://linuxmafia.com/faq/Admin/release-files.html 256 for (LinuxReleaseInfo info : new LinuxReleaseInfo[]{ 257 new LinuxReleaseInfo("/etc/lsb-release", "DISTRIB_DESCRIPTION", "DISTRIB_ID", "DISTRIB_RELEASE"), 258 new LinuxReleaseInfo("/etc/os-release", "PRETTY_NAME", "NAME", "VERSION"), 259 new LinuxReleaseInfo("/etc/arch-release"), 260 new LinuxReleaseInfo("/etc/debian_version", "Debian GNU/Linux "), 261 new LinuxReleaseInfo("/etc/fedora-release"), 262 new LinuxReleaseInfo("/etc/gentoo-release"), 263 new LinuxReleaseInfo("/etc/redhat-release"), 264 new LinuxReleaseInfo("/etc/SuSE-release") 265 }) { 266 String description = info.extractDescription(); 267 if (!Utils.isEmpty(description)) { 268 return "Linux " + description; 269 } 270 } 271 } 272 } 273 return osName; 274 } 275 276 @Override 277 public String getOSDescription() { 278 if (osDescription == null) { 279 osDescription = buildOSDescription(); 280 } 281 return osDescription; 282 } 283 284 private static class LinuxReleaseInfo { 285 private final String path; 286 private final String descriptionField; 287 private final String idField; 288 private final String releaseField; 289 private final boolean plainText; 290 private final String prefix; 291 292 LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField) { 293 this(path, descriptionField, idField, releaseField, false, null); 294 } 295 296 LinuxReleaseInfo(String path) { 297 this(path, null, null, null, true, null); 298 } 299 300 LinuxReleaseInfo(String path, String prefix) { 301 this(path, null, null, null, true, prefix); 302 } 303 304 private LinuxReleaseInfo(String path, String descriptionField, String idField, String releaseField, boolean plainText, String prefix) { 305 this.path = path; 306 this.descriptionField = descriptionField; 307 this.idField = idField; 308 this.releaseField = releaseField; 309 this.plainText = plainText; 310 this.prefix = prefix; 311 } 312 313 @Override 314 public String toString() { 315 return "ReleaseInfo [path=" + path + ", descriptionField=" + descriptionField + 316 ", idField=" + idField + ", releaseField=" + releaseField + ']'; 317 } 318 319 /** 320 * Extracts OS detailed information from a Linux release file (/etc/xxx-release) 321 * @return The OS detailed information, or {@code null} 322 */ 323 public String extractDescription() { 324 String result = null; 325 if (path != null) { 326 Path p = Paths.get(path); 327 if (p.toFile().exists()) { 328 try (BufferedReader reader = Files.newBufferedReader(p, StandardCharsets.UTF_8)) { 329 String id = null; 330 String release = null; 331 String line; 332 while (result == null && (line = reader.readLine()) != null) { 333 if (line.contains("=")) { 334 String[] tokens = line.split("=", -1); 335 if (tokens.length >= 2) { 336 // Description, if available, contains exactly what we need 337 if (descriptionField != null && descriptionField.equalsIgnoreCase(tokens[0])) { 338 result = Utils.strip(tokens[1]); 339 } else if (idField != null && idField.equalsIgnoreCase(tokens[0])) { 340 id = Utils.strip(tokens[1]); 341 } else if (releaseField != null && releaseField.equalsIgnoreCase(tokens[0])) { 342 release = Utils.strip(tokens[1]); 343 } 344 } 345 } else if (plainText && !line.isEmpty()) { 346 // Files composed of a single line 347 result = Utils.strip(line); 348 } 349 } 350 // If no description has been found, try to rebuild it with "id" + "release" (i.e. "name" + "version") 351 if (result == null && id != null && release != null) { 352 result = id + ' ' + release; 353 } 354 } catch (IOException e) { 355 // Ignore 356 Logging.trace(e); 357 } 358 } 359 } 360 // Append prefix if any 361 if (!Utils.isEmpty(result) && !Utils.isEmpty(prefix)) { 362 result = prefix + result; 363 } 364 if (result != null) 365 result = result.replaceAll("\"+", ""); 366 return result; 367 } 368 } 369 370 /** 371 * Get the dot directory <code>~/.josm</code>. 372 * @return the dot directory 373 */ 374 private static File getDotDirectory() { 375 String dirName = "." + Preferences.getJOSMDirectoryBaseName().toLowerCase(Locale.ENGLISH); 376 return new File(getSystemProperty("user.home"), dirName); 377 } 378 379 /** 380 * Returns true if the dot directory should be used for storing preferences, 381 * cache and user data. 382 * Currently this is the case, if the dot directory already exists. 383 * @return true if the dot directory should be used 384 */ 385 private static boolean useDotDirectory() { 386 return getDotDirectory().exists(); 387 } 388 389 @Override 390 public File getDefaultCacheDirectory() { 391 if (useDotDirectory()) { 392 return new File(getDotDirectory(), "cache"); 393 } else { 394 String xdgCacheDir = getSystemEnv("XDG_CACHE_HOME"); 395 if (!Utils.isEmpty(xdgCacheDir)) { 396 return new File(xdgCacheDir, Preferences.getJOSMDirectoryBaseName()); 397 } else { 398 return new File(getSystemProperty("user.home") + File.separator + 399 ".cache" + File.separator + Preferences.getJOSMDirectoryBaseName()); 400 } 401 } 402 } 403 404 @Override 405 public File getDefaultPrefDirectory() { 406 if (useDotDirectory()) { 407 return getDotDirectory(); 408 } else { 409 String xdgConfigDir = getSystemEnv("XDG_CONFIG_HOME"); 410 if (!Utils.isEmpty(xdgConfigDir)) { 411 return new File(xdgConfigDir, Preferences.getJOSMDirectoryBaseName()); 412 } else { 413 return new File(getSystemProperty("user.home") + File.separator + 414 ".config" + File.separator + Preferences.getJOSMDirectoryBaseName()); 415 } 416 } 417 } 418 419 @Override 420 public File getDefaultUserDataDirectory() { 421 if (useDotDirectory()) { 422 return getDotDirectory(); 423 } else { 424 String xdgDataDir = getSystemEnv("XDG_DATA_HOME"); 425 if (!Utils.isEmpty(xdgDataDir)) { 426 return new File(xdgDataDir, Preferences.getJOSMDirectoryBaseName()); 427 } else { 428 return new File(getSystemProperty("user.home") + File.separator + 429 ".local" + File.separator + "share" + File.separator + Preferences.getJOSMDirectoryBaseName()); 430 } 431 } 432 } 433 434 @Override 435 public X509Certificate getX509Certificate(NativeCertAmend certAmend) 436 throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { 437 for (String dir : new String[] {"/etc/ssl/certs", "/usr/share/ca-certificates/mozilla"}) { 438 File f = new File(dir, certAmend.getFilename()); 439 if (f.exists()) { 440 CertificateFactory fact = CertificateFactory.getInstance("X.509"); 441 try (InputStream is = Files.newInputStream(f.toPath())) { 442 return (X509Certificate) fact.generateCertificate(is); 443 } 444 } 445 } 446 return null; 447 } 448 449 @Override 450 public Collection<String> getPossiblePreferenceDirs() { 451 Set<String> locations = new HashSet<>(); 452 locations.add("/usr/local/share/josm/"); 453 locations.add("/usr/local/lib/josm/"); 454 locations.add("/usr/share/josm/"); 455 locations.add("/usr/lib/josm/"); 456 return locations; 457 } 458}