Ryan Mitchell | 9b93942 | 2020-02-04 10:18:53 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <sys/stat.h> // umask |
| 18 | #include <sys/types.h> // umask |
| 19 | |
| 20 | #include <fstream> |
| 21 | #include <memory> |
| 22 | #include <ostream> |
MÃ¥rten Kongstad | c4799eb | 2020-06-04 20:48:11 +0200 | [diff] [blame] | 23 | #include <string> |
Ryan Mitchell | 9b93942 | 2020-02-04 10:18:53 -0800 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
hg.choi | a3a6813 | 2020-01-15 17:12:48 +0900 | [diff] [blame] | 26 | #include "Commands.h" |
Ryan Mitchell | 9b93942 | 2020-02-04 10:18:53 -0800 | [diff] [blame] | 27 | #include "android-base/stringprintf.h" |
| 28 | #include "idmap2/BinaryStreamVisitor.h" |
| 29 | #include "idmap2/CommandLineOptions.h" |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 30 | #include "idmap2/CommandUtils.h" |
Ryan Mitchell | 9b93942 | 2020-02-04 10:18:53 -0800 | [diff] [blame] | 31 | #include "idmap2/FileUtils.h" |
| 32 | #include "idmap2/Idmap.h" |
| 33 | #include "idmap2/Policies.h" |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 34 | #include "idmap2/PolicyUtils.h" |
Ryan Mitchell | 9b93942 | 2020-02-04 10:18:53 -0800 | [diff] [blame] | 35 | #include "idmap2/SysTrace.h" |
| 36 | |
| 37 | using android::ApkAssets; |
| 38 | using android::base::StringPrintf; |
| 39 | using android::idmap2::BinaryStreamVisitor; |
| 40 | using android::idmap2::CommandLineOptions; |
| 41 | using android::idmap2::Error; |
| 42 | using android::idmap2::Idmap; |
Ryan Mitchell | 9b93942 | 2020-02-04 10:18:53 -0800 | [diff] [blame] | 43 | using android::idmap2::Result; |
| 44 | using android::idmap2::Unit; |
| 45 | using android::idmap2::utils::kIdmapCacheDir; |
| 46 | using android::idmap2::utils::kIdmapFilePermissionMask; |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 47 | using android::idmap2::utils::PoliciesToBitmaskResult; |
Ryan Mitchell | 9b93942 | 2020-02-04 10:18:53 -0800 | [diff] [blame] | 48 | using android::idmap2::utils::UidHasWriteAccessToPath; |
| 49 | |
| 50 | Result<Unit> CreateMultiple(const std::vector<std::string>& args) { |
| 51 | SYSTRACE << "CreateMultiple " << args; |
| 52 | std::string target_apk_path; |
| 53 | std::string idmap_dir = kIdmapCacheDir; |
| 54 | std::vector<std::string> overlay_apk_paths; |
| 55 | std::vector<std::string> policies; |
| 56 | bool ignore_overlayable = false; |
| 57 | |
| 58 | const CommandLineOptions opts = |
| 59 | CommandLineOptions("idmap2 create-multiple") |
| 60 | .MandatoryOption("--target-apk-path", |
| 61 | "input: path to apk which will have its resources overlaid", |
| 62 | &target_apk_path) |
| 63 | .MandatoryOption("--overlay-apk-path", |
| 64 | "input: path to apk which contains the new resource values", |
| 65 | &overlay_apk_paths) |
| 66 | .OptionalOption("--idmap-dir", |
| 67 | StringPrintf("output: path to the directory in which to write idmap file" |
| 68 | " (defaults to %s)", |
| 69 | kIdmapCacheDir), |
| 70 | &idmap_dir) |
| 71 | .OptionalOption("--policy", |
| 72 | "input: an overlayable policy this overlay fulfills" |
| 73 | " (if none or supplied, the overlay policy will default to \"public\")", |
| 74 | &policies) |
| 75 | .OptionalFlag("--ignore-overlayable", "disables overlayable and policy checks", |
| 76 | &ignore_overlayable); |
| 77 | const auto opts_ok = opts.Parse(args); |
| 78 | if (!opts_ok) { |
| 79 | return opts_ok.GetError(); |
| 80 | } |
| 81 | |
| 82 | PolicyBitmask fulfilled_policies = 0; |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 83 | auto conv_result = PoliciesToBitmaskResult(policies); |
Ryan Mitchell | 9b93942 | 2020-02-04 10:18:53 -0800 | [diff] [blame] | 84 | if (conv_result) { |
| 85 | fulfilled_policies |= *conv_result; |
| 86 | } else { |
| 87 | return conv_result.GetError(); |
| 88 | } |
| 89 | |
| 90 | if (fulfilled_policies == 0) { |
Winson | 62ac8b5 | 2019-12-04 08:36:48 -0800 | [diff] [blame] | 91 | fulfilled_policies |= PolicyFlags::PUBLIC; |
Ryan Mitchell | 9b93942 | 2020-02-04 10:18:53 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | const std::unique_ptr<const ApkAssets> target_apk = ApkAssets::Load(target_apk_path); |
| 95 | if (!target_apk) { |
| 96 | return Error("failed to load apk %s", target_apk_path.c_str()); |
| 97 | } |
| 98 | |
| 99 | std::vector<std::string> idmap_paths; |
| 100 | for (const std::string& overlay_apk_path : overlay_apk_paths) { |
| 101 | const std::string idmap_path = Idmap::CanonicalIdmapPathFor(idmap_dir, overlay_apk_path); |
| 102 | const uid_t uid = getuid(); |
| 103 | if (!UidHasWriteAccessToPath(uid, idmap_path)) { |
| 104 | LOG(WARNING) << "uid " << uid << "does not have write access to " << idmap_path.c_str(); |
| 105 | continue; |
| 106 | } |
| 107 | |
Ryan Mitchell | a707013 | 2020-05-13 14:17:52 -0700 | [diff] [blame] | 108 | if (!Verify(idmap_path, target_apk_path, overlay_apk_path, fulfilled_policies, |
| 109 | !ignore_overlayable)) { |
Ryan Mitchell | 625ebd3 | 2020-02-21 15:52:57 -0800 | [diff] [blame] | 110 | const std::unique_ptr<const ApkAssets> overlay_apk = ApkAssets::Load(overlay_apk_path); |
| 111 | if (!overlay_apk) { |
| 112 | LOG(WARNING) << "failed to load apk " << overlay_apk_path.c_str(); |
| 113 | continue; |
| 114 | } |
Ryan Mitchell | 9b93942 | 2020-02-04 10:18:53 -0800 | [diff] [blame] | 115 | |
Ryan Mitchell | 625ebd3 | 2020-02-21 15:52:57 -0800 | [diff] [blame] | 116 | const auto idmap = |
| 117 | Idmap::FromApkAssets(*target_apk, *overlay_apk, fulfilled_policies, !ignore_overlayable); |
| 118 | if (!idmap) { |
| 119 | LOG(WARNING) << "failed to create idmap"; |
| 120 | continue; |
| 121 | } |
Ryan Mitchell | 9b93942 | 2020-02-04 10:18:53 -0800 | [diff] [blame] | 122 | |
Ryan Mitchell | 625ebd3 | 2020-02-21 15:52:57 -0800 | [diff] [blame] | 123 | umask(kIdmapFilePermissionMask); |
| 124 | std::ofstream fout(idmap_path); |
| 125 | if (fout.fail()) { |
| 126 | LOG(WARNING) << "failed to open idmap path " << idmap_path.c_str(); |
| 127 | continue; |
| 128 | } |
Ryan Mitchell | 9b93942 | 2020-02-04 10:18:53 -0800 | [diff] [blame] | 129 | |
Ryan Mitchell | 625ebd3 | 2020-02-21 15:52:57 -0800 | [diff] [blame] | 130 | BinaryStreamVisitor visitor(fout); |
| 131 | (*idmap)->accept(&visitor); |
| 132 | fout.close(); |
| 133 | if (fout.fail()) { |
| 134 | LOG(WARNING) << "failed to write to idmap path %s" << idmap_path.c_str(); |
| 135 | continue; |
| 136 | } |
Ryan Mitchell | 9b93942 | 2020-02-04 10:18:53 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | idmap_paths.emplace_back(idmap_path); |
| 140 | } |
| 141 | |
| 142 | for (const std::string& idmap_path : idmap_paths) { |
| 143 | std::cout << idmap_path << std::endl; |
| 144 | } |
| 145 | |
| 146 | return Unit{}; |
| 147 | } |