blob: 19622c4ef65a74fc69c6983843ef4e0dd14d6a22 [file] [log] [blame]
Ryan Mitchell9b939422020-02-04 10:18:53 -08001/*
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 Kongstadc4799eb2020-06-04 20:48:11 +020023#include <string>
Ryan Mitchell9b939422020-02-04 10:18:53 -080024#include <vector>
25
hg.choia3a68132020-01-15 17:12:48 +090026#include "Commands.h"
Ryan Mitchell9b939422020-02-04 10:18:53 -080027#include "android-base/stringprintf.h"
28#include "idmap2/BinaryStreamVisitor.h"
29#include "idmap2/CommandLineOptions.h"
Ryan Mitchella7070132020-05-13 14:17:52 -070030#include "idmap2/CommandUtils.h"
Ryan Mitchell9b939422020-02-04 10:18:53 -080031#include "idmap2/FileUtils.h"
32#include "idmap2/Idmap.h"
33#include "idmap2/Policies.h"
Winson62ac8b52019-12-04 08:36:48 -080034#include "idmap2/PolicyUtils.h"
Ryan Mitchell9b939422020-02-04 10:18:53 -080035#include "idmap2/SysTrace.h"
36
37using android::ApkAssets;
38using android::base::StringPrintf;
39using android::idmap2::BinaryStreamVisitor;
40using android::idmap2::CommandLineOptions;
41using android::idmap2::Error;
42using android::idmap2::Idmap;
Ryan Mitchell9b939422020-02-04 10:18:53 -080043using android::idmap2::Result;
44using android::idmap2::Unit;
45using android::idmap2::utils::kIdmapCacheDir;
46using android::idmap2::utils::kIdmapFilePermissionMask;
Winson62ac8b52019-12-04 08:36:48 -080047using android::idmap2::utils::PoliciesToBitmaskResult;
Ryan Mitchell9b939422020-02-04 10:18:53 -080048using android::idmap2::utils::UidHasWriteAccessToPath;
49
50Result<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;
Winson62ac8b52019-12-04 08:36:48 -080083 auto conv_result = PoliciesToBitmaskResult(policies);
Ryan Mitchell9b939422020-02-04 10:18:53 -080084 if (conv_result) {
85 fulfilled_policies |= *conv_result;
86 } else {
87 return conv_result.GetError();
88 }
89
90 if (fulfilled_policies == 0) {
Winson62ac8b52019-12-04 08:36:48 -080091 fulfilled_policies |= PolicyFlags::PUBLIC;
Ryan Mitchell9b939422020-02-04 10:18:53 -080092 }
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 Mitchella7070132020-05-13 14:17:52 -0700108 if (!Verify(idmap_path, target_apk_path, overlay_apk_path, fulfilled_policies,
109 !ignore_overlayable)) {
Ryan Mitchell625ebd32020-02-21 15:52:57 -0800110 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 Mitchell9b939422020-02-04 10:18:53 -0800115
Ryan Mitchell625ebd32020-02-21 15:52:57 -0800116 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 Mitchell9b939422020-02-04 10:18:53 -0800122
Ryan Mitchell625ebd32020-02-21 15:52:57 -0800123 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 Mitchell9b939422020-02-04 10:18:53 -0800129
Ryan Mitchell625ebd32020-02-21 15:52:57 -0800130 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 Mitchell9b939422020-02-04 10:18:53 -0800137 }
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}