1 /*
   2  * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jpackage.internal;
  27 
  28 import java.io.ByteArrayOutputStream;
  29 import java.io.File;
  30 import java.io.IOException;
  31 import java.io.PrintStream;
  32 import java.nio.file.Files;
  33 import java.text.MessageFormat;
  34 import java.util.ArrayList;
  35 import java.util.Arrays;
  36 import java.util.Collection;
  37 import java.util.LinkedHashSet;
  38 import java.util.List;
  39 import java.util.Map;
  40 import java.util.ResourceBundle;
  41 import java.util.regex.Matcher;
  42 import java.util.regex.Pattern;
  43 
  44 import static jdk.jpackage.internal.StandardBundlerParam.*;
  45 
  46 public abstract class MacBaseInstallerBundler extends AbstractBundler {
  47 
  48     private static final ResourceBundle I18N = ResourceBundle.getBundle(
  49             "jdk.jpackage.internal.resources.MacResources");
  50 
  51     // This could be generalized more to be for any type of Image Bundler
  52     public static final BundlerParamInfo<MacAppBundler> APP_BUNDLER =
  53             new StandardBundlerParam<>(
  54             "mac.app.bundler",
  55             MacAppBundler.class,
  56             params -> new MacAppBundler(),
  57             (s, p) -> null);
  58 
  59     public final BundlerParamInfo<File> APP_IMAGE_TEMP_ROOT =
  60             new StandardBundlerParam<>(
  61             "mac.app.imageRoot",
  62             File.class,
  63             params -> {
  64                 File imageDir = IMAGES_ROOT.fetchFrom(params);
  65                 if (!imageDir.exists()) imageDir.mkdirs();
  66                 try {
  67                     return Files.createTempDirectory(
  68                             imageDir.toPath(), "image-").toFile();
  69                 } catch (IOException e) {
  70                     return new File(imageDir, getID()+ ".image");
  71                 }
  72             },
  73             (s, p) -> new File(s));
  74 
  75     public static final BundlerParamInfo<String> SIGNING_KEY_USER =
  76             new StandardBundlerParam<>(
  77             Arguments.CLIOptions.MAC_SIGNING_KEY_NAME.getId(),
  78             String.class,
  79             params -> "",
  80             null);
  81 
  82     public static final BundlerParamInfo<String> SIGNING_KEYCHAIN =
  83             new StandardBundlerParam<>(
  84             Arguments.CLIOptions.MAC_SIGNING_KEYCHAIN.getId(),
  85             String.class,
  86             params -> "",
  87             null);
  88 
  89     public static final BundlerParamInfo<String> INSTALLER_NAME =
  90             new StandardBundlerParam<> (
  91             "mac.installerName",
  92             String.class,
  93             params -> {
  94                 String nm = APP_NAME.fetchFrom(params);
  95                 if (nm == null) return null;
  96 
  97                 String version = VERSION.fetchFrom(params);
  98                 if (version == null) {
  99                     return nm;
 100                 } else {
 101                     return nm + "-" + version;
 102                 }
 103             },
 104             (s, p) -> s);
 105 
 106     protected void validateAppImageAndBundeler(
 107             Map<String, ? super Object> params)
 108             throws ConfigException, UnsupportedPlatformException {
 109         if (PREDEFINED_APP_IMAGE.fetchFrom(params) != null) {
 110             File applicationImage = PREDEFINED_APP_IMAGE.fetchFrom(params);
 111             if (!applicationImage.exists()) {
 112                 throw new ConfigException(
 113                         MessageFormat.format(I18N.getString(
 114                                 "message.app-image-dir-does-not-exist"),
 115                                 PREDEFINED_APP_IMAGE.getID(),
 116                                 applicationImage.toString()),
 117                         MessageFormat.format(I18N.getString(
 118                                 "message.app-image-dir-does-not-exist.advice"),
 119                                 PREDEFINED_APP_IMAGE.getID()));
 120             }
 121             if (APP_NAME.fetchFrom(params) == null) {
 122                 throw new ConfigException(
 123                         I18N.getString("message.app-image-requires-app-name"),
 124                         I18N.getString(
 125                             "message.app-image-requires-app-name.advice"));
 126             }
 127             if (IDENTIFIER.fetchFrom(params) == null) {
 128                 throw new ConfigException(
 129                         I18N.getString("message.app-image-requires-identifier"),
 130                         I18N.getString(
 131                             "message.app-image-requires-identifier.advice"));
 132             }
 133         } else {
 134             APP_BUNDLER.fetchFrom(params).validate(params);
 135         }
 136     }
 137 
 138     protected File prepareAppBundle(Map<String, ? super Object> p,
 139             boolean pkg) throws PackagerException {
 140         File predefinedImage = StandardBundlerParam.getPredefinedAppImage(p);
 141         if (predefinedImage != null) {
 142             return predefinedImage;
 143         }
 144         File appImageRoot = APP_IMAGE_TEMP_ROOT.fetchFrom(p);
 145         if (pkg) {
 146             // create pkg in dmg
 147             return new MacPkgBundler().bundle(p, appImageRoot);
 148         } else {
 149             return APP_BUNDLER.fetchFrom(p).doBundle(p, appImageRoot, true);
 150         }
 151     }
 152 
 153     @Override
 154     public Collection<BundlerParamInfo<?>> getBundleParameters() {
 155         Collection<BundlerParamInfo<?>> results = new LinkedHashSet<>();
 156 
 157         results.addAll(MacAppBundler.getAppBundleParameters());
 158         results.addAll(Arrays.asList(
 159                 APP_BUNDLER,
 160                 CONFIG_ROOT,
 161                 APP_IMAGE_TEMP_ROOT,
 162                 PREDEFINED_APP_IMAGE
 163         ));
 164 
 165         return results;
 166     }
 167 
 168     @Override
 169     public String getBundleType() {
 170         return "INSTALLER";
 171     }
 172 
 173     public static String findKey(String key, String keychainName,
 174             boolean verbose) {
 175         if (Platform.getPlatform() != Platform.MAC) {
 176             return null;
 177         }
 178 
 179         try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
 180                 PrintStream ps = new PrintStream(baos)) {
 181             List<String> searchOptions = new ArrayList<>();
 182             searchOptions.add("security");
 183             searchOptions.add("find-certificate");
 184             searchOptions.add("-c");
 185             searchOptions.add(key);
 186             searchOptions.add("-a");
 187             if (keychainName != null && !keychainName.isEmpty()) {
 188                 searchOptions.add(keychainName);
 189             }
 190 
 191             ProcessBuilder pb = new ProcessBuilder(searchOptions);
 192 
 193             IOUtils.exec(pb, verbose, false, ps);
 194             Pattern p = Pattern.compile("\"alis\"<blob>=\"([^\"]+)\"");
 195             Matcher m = p.matcher(baos.toString());
 196             if (!m.find()) {
 197                 Log.error("Did not find a key matching '" + key + "'");
 198                 return null;
 199             }
 200             String matchedKey = m.group(1);
 201             if (m.find()) {
 202                 Log.error("Found more than one key matching '"  + key + "'");
 203                 return null;
 204             }
 205             Log.debug("Using key '" + matchedKey + "'");
 206             return matchedKey;
 207         } catch (IOException ioe) {
 208             Log.verbose(ioe);
 209             return null;
 210         }
 211     }
 212 }