1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "sys_installer_stub.h"
17
18 #include <string_ex.h>
19
20 #include "accesstoken_kit.h"
21 #include "hap_token_info.h"
22 #include "ipc_skeleton.h"
23 #include "log/log.h"
24 #include "securec.h"
25 #include "sys_installer_sa_ipc_interface_code.h"
26 #include "utils.h"
27
28 namespace OHOS {
29 namespace SysInstaller {
30 using namespace std;
31 using namespace Updater;
32 using namespace std::placeholders;
33
SysInstallerStub()34 SysInstallerStub::SysInstallerStub()
35 {
36 requestFuncMap_.emplace(SysInstallerInterfaceCode::SYS_INSTALLER_INIT,
37 bind(&SysInstallerStub::SysInstallerInitStub, this, _1, _2, _3, _4));
38 requestFuncMap_.emplace(SysInstallerInterfaceCode::UPDATE_PACKAGE,
39 bind(&SysInstallerStub::StartUpdatePackageZipStub, this, _1, _2, _3, _4));
40 requestFuncMap_.emplace(SysInstallerInterfaceCode::SET_UPDATE_CALLBACK,
41 bind(&SysInstallerStub::SetUpdateCallbackStub, this, _1, _2, _3, _4));
42 requestFuncMap_.emplace(SysInstallerInterfaceCode::GET_UPDATE_STATUS,
43 bind(&SysInstallerStub::GetUpdateStatusStub, this, _1, _2, _3, _4));
44 requestFuncMap_.emplace(SysInstallerInterfaceCode::UPDATE_PARA_PACKAGE,
45 bind(&SysInstallerStub::StartUpdateParaZipStub, this, _1, _2, _3, _4));
46 requestFuncMap_.emplace(SysInstallerInterfaceCode::DELETE_PARA_PACKAGE,
47 bind(&SysInstallerStub::StartDeleteParaZipStub, this, _1, _2, _3, _4));
48 requestFuncMap_.emplace(SysInstallerInterfaceCode::DECOMPRESS_ACC_PACKAGE,
49 bind(&SysInstallerStub::AccDecompressAndVerifyPkgStub, this, _1, _2, _3, _4));
50 requestFuncMap_.emplace(SysInstallerInterfaceCode::DELETE_ACC_PACKAGE,
51 bind(&SysInstallerStub::AccDeleteDirStub, this, _1, _2, _3, _4));
52 }
53
~SysInstallerStub()54 SysInstallerStub::~SysInstallerStub()
55 {
56 requestFuncMap_.clear();
57 }
58
SysInstallerInitStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)59 int32_t SysInstallerStub::SysInstallerInitStub(SysInstallerStub *service,
60 MessageParcel &data, MessageParcel &reply, MessageOption &option)
61 {
62 if (service == nullptr) {
63 LOG(ERROR) << "Invalid param";
64 return -1;
65 }
66 int ret = service->SysInstallerInit();
67 reply.WriteInt32(ret);
68 return 0;
69 }
70
StartUpdatePackageZipStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)71 int32_t SysInstallerStub::StartUpdatePackageZipStub(SysInstallerStub *service,
72 MessageParcel &data, MessageParcel &reply, MessageOption &option)
73 {
74 if (service == nullptr) {
75 LOG(ERROR) << "Invalid param";
76 return -1;
77 }
78 string pkgPath = Str16ToStr8(data.ReadString16());
79 int ret = service->StartUpdatePackageZip(pkgPath);
80 reply.WriteInt32(ret);
81 return 0;
82 }
83
SetUpdateCallbackStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)84 int32_t SysInstallerStub::SetUpdateCallbackStub(SysInstallerStub *service,
85 MessageParcel &data, MessageParcel &reply, MessageOption &option)
86 {
87 if (service == nullptr) {
88 LOG(ERROR) << "Invalid param";
89 return -1;
90 }
91 auto remote = data.ReadRemoteObject();
92 if (remote == nullptr) {
93 LOG(ERROR) << "Failed to read remote obj";
94 return -1;
95 }
96 int ret = service->SetUpdateCallback(iface_cast<ISysInstallerCallback>(remote));
97 reply.WriteInt32(ret);
98 return 0;
99 }
100
GetUpdateStatusStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)101 int32_t SysInstallerStub::GetUpdateStatusStub(SysInstallerStub *service,
102 MessageParcel &data, MessageParcel &reply, MessageOption &option)
103 {
104 if (service == nullptr) {
105 LOG(ERROR) << "Invalid param";
106 return -1;
107 }
108 int32_t ret = service->GetUpdateStatus();
109 reply.WriteInt32(ret);
110 return 0;
111 }
112
StartUpdateParaZipStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)113 int32_t SysInstallerStub::StartUpdateParaZipStub(SysInstallerStub *service,
114 MessageParcel &data, MessageParcel &reply, MessageOption &option)
115 {
116 if (service == nullptr) {
117 LOG(ERROR) << "Invalid param";
118 return -1;
119 }
120 string pkgPath = Str16ToStr8(data.ReadString16());
121 string location = Str16ToStr8(data.ReadString16());
122 string cfgDir = Str16ToStr8(data.ReadString16());
123 LOG(INFO) << "StartUpdateParaZipStub path:" << pkgPath << " location:" << location << " cfgDir:" << cfgDir;
124
125 int32_t ret = service->StartUpdateParaZip(pkgPath, location, cfgDir);
126 reply.WriteInt32(ret);
127 return 0;
128 }
129
StartDeleteParaZipStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)130 int32_t SysInstallerStub::StartDeleteParaZipStub(SysInstallerStub *service,
131 MessageParcel &data, MessageParcel &reply, MessageOption &option)
132 {
133 if (service == nullptr) {
134 LOG(ERROR) << "Invalid param";
135 return -1;
136 }
137 string location = Str16ToStr8(data.ReadString16());
138 string cfgDir = Str16ToStr8(data.ReadString16());
139 LOG(INFO) << "StartDeleteParaZipStub location:" << location << " cfgDir:" << cfgDir;
140
141 int32_t ret = service->StartDeleteParaZip(location, cfgDir);
142 reply.WriteInt32(ret);
143 return 0;
144 }
145
AccDecompressAndVerifyPkgStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)146 int32_t SysInstallerStub::AccDecompressAndVerifyPkgStub(SysInstallerStub *service,
147 MessageParcel &data, MessageParcel &reply, MessageOption &option)
148 {
149 if (service == nullptr) {
150 LOG(ERROR) << "Invalid param";
151 return -1;
152 }
153 string srcPath = Str16ToStr8(data.ReadString16());
154 string dstPath = Str16ToStr8(data.ReadString16());
155 uint32_t type = static_cast<uint32_t>(data.ReadInt32());
156 LOG(INFO) << "StartUpdateParaZipStub srcPath:" << srcPath << " dstPath:" << dstPath;
157
158 int32_t ret = service->AccDecompressAndVerifyPkg(srcPath, dstPath, type);
159 reply.WriteInt32(ret);
160 return 0;
161 }
162
AccDeleteDirStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)163 int32_t SysInstallerStub::AccDeleteDirStub(SysInstallerStub *service,
164 MessageParcel &data, MessageParcel &reply, MessageOption &option)
165 {
166 if (service == nullptr) {
167 LOG(ERROR) << "Invalid param";
168 return -1;
169 }
170 string dstPath = Str16ToStr8(data.ReadString16());
171 LOG(INFO) << "AccDeleteDirStub dstPath:" << dstPath;
172
173 int32_t ret = service->AccDeleteDir(dstPath);
174 reply.WriteInt32(ret);
175 return 0;
176 }
177
IsPermissionGranted(void)178 bool SysInstallerStub::IsPermissionGranted(void)
179 {
180 Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
181 std::string permission = "ohos.permission.UPDATE_SYSTEM";
182
183 int verifyResult = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, permission);
184 bool isPermissionGranted = (verifyResult == Security::AccessToken::PERMISSION_GRANTED);
185 if (!isPermissionGranted) {
186 LOG(ERROR) << "not granted " << permission.c_str();
187 }
188 return isPermissionGranted;
189 }
190
CheckCallingPerm(void)191 bool SysInstallerStub::CheckCallingPerm(void)
192 {
193 int32_t callingUid = OHOS::IPCSkeleton::GetCallingUid();
194 LOG(INFO) << "CheckCallingPerm callingUid:" << callingUid;
195 if (callingUid == 0) {
196 return true;
197 }
198 return callingUid == Updater::Utils::USER_UPDATE_AUTHORITY && IsPermissionGranted();
199 }
200
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)201 int32_t SysInstallerStub::OnRemoteRequest(uint32_t code,
202 MessageParcel &data, MessageParcel &reply, MessageOption &option)
203 {
204 if (data.ReadInterfaceToken() != GetDescriptor()) {
205 LOG(ERROR) << "SysInstallerStub ReadInterfaceToken fail";
206 return -1;
207 }
208
209 LOG(INFO) << "OnRemoteRequest func code " << code;
210 auto inter = requestFuncMap_.find(code);
211 if (inter != requestFuncMap_.end()) {
212 if (!CheckCallingPerm()) {
213 LOG(ERROR) << "SysInstallerStub CheckCallingPerm fail";
214 return -1;
215 }
216 return inter->second(this, data, reply, option);
217 }
218
219 LOG(INFO) << "UpdateServiceStub OnRemoteRequest code " << code << "not found";
220 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
221 }
222 } // namespace SysInstaller
223 } // namespace OHOS