• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "module_update_stub.h"
17 
18 #include "accesstoken_kit.h"
19 #include "ipc_skeleton.h"
20 #include "isys_installer_callback.h"
21 #include "sys_installer_sa_ipc_interface_code.h"
22 
23 #include "log/log.h"
24 #include "string_ex.h"
25 
26 constexpr int USER_UPDATE_AUTHORITY = 6666;
27 
28 namespace OHOS {
29 namespace SysInstaller {
30 using namespace std;
31 using namespace Updater;
32 using namespace std::placeholders;
33 
ModuleUpdateStub()34 ModuleUpdateStub::ModuleUpdateStub()
35 {
36     requestFuncMap_.emplace(ModuleUpdateInterfaceCode::INSTALL_MODULE_PACKAGE,
37         bind(&ModuleUpdateStub::InstallModulePackageStub, this, _1, _2, _3, _4));
38     requestFuncMap_.emplace(ModuleUpdateInterfaceCode::UNINSTALL_MODULE_PACKAGE,
39         bind(&ModuleUpdateStub::UninstallModulePackageStub, this, _1, _2, _3, _4));
40     requestFuncMap_.emplace(ModuleUpdateInterfaceCode::GET_MODULE_PACKAGE_INFO,
41         bind(&ModuleUpdateStub::GetModulePackageInfoStub, this, _1, _2, _3, _4));
42     requestFuncMap_.emplace(ModuleUpdateInterfaceCode::REPORT_MODULE_UPDATE_STATUS,
43         bind(&ModuleUpdateStub::ReportModuleUpdateStatusStub, this, _1, _2, _3, _4));
44     requestFuncMap_.emplace(ModuleUpdateInterfaceCode::EXIT_MODULE_UPDATE,
45         bind(&ModuleUpdateStub::ExitModuleUpdateStub, this, _1, _2, _3, _4));
46 
47     requestFuncMap_.emplace(ModuleUpdateInterfaceCode::GET_HMP_VERSION_INFO,
48         bind(&ModuleUpdateStub::GetHmpVersionInfoStub, this, _1, _2, _3, _4));
49     requestFuncMap_.emplace(ModuleUpdateInterfaceCode::START_UPDATE_HMP_PACKAGE,
50         bind(&ModuleUpdateStub::StartUpdateHmpPackageStub, this, _1, _2, _3, _4));
51     requestFuncMap_.emplace(ModuleUpdateInterfaceCode::GET_HMP_UPDATE_RESULT,
52         bind(&ModuleUpdateStub::GetHmpUpdateResultStub, this, _1, _2, _3, _4));
53 }
54 
~ModuleUpdateStub()55 ModuleUpdateStub::~ModuleUpdateStub()
56 {
57     requestFuncMap_.clear();
58 }
59 
InstallModulePackageStub(ModuleUpdateStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option) const60 int32_t ModuleUpdateStub::InstallModulePackageStub(ModuleUpdateStub *service,
61     MessageParcel &data, MessageParcel &reply, MessageOption &option) const
62 {
63     if (service == nullptr) {
64         LOG(ERROR) << "Invalid param";
65         return -1;
66     }
67     string pkgPath = Str16ToStr8(data.ReadString16());
68     int32_t ret = service->InstallModulePackage(pkgPath);
69     reply.WriteInt32(ret);
70     return 0;
71 }
72 
UninstallModulePackageStub(ModuleUpdateStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option) const73 int32_t ModuleUpdateStub::UninstallModulePackageStub(ModuleUpdateStub *service,
74     MessageParcel &data, MessageParcel &reply, MessageOption &option) const
75 {
76     if (service == nullptr) {
77         LOG(ERROR) << "Invalid param";
78         return -1;
79     }
80     string hmpName = Str16ToStr8(data.ReadString16());
81     int32_t ret = service->UninstallModulePackage(hmpName);
82     reply.WriteInt32(ret);
83     return 0;
84 }
85 
GetModulePackageInfoStub(ModuleUpdateStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option) const86 int32_t ModuleUpdateStub::GetModulePackageInfoStub(ModuleUpdateStub *service,
87     MessageParcel &data, MessageParcel &reply, MessageOption &option) const
88 {
89     if (service == nullptr) {
90         LOG(ERROR) << "Invalid param";
91         return -1;
92     }
93     string hmpName = Str16ToStr8(data.ReadString16());
94     std::list<ModulePackageInfo> infos;
95     int32_t ret = service->GetModulePackageInfo(hmpName, infos);
96     ModuleIpcHelper::WriteModulePackageInfos(reply, infos);
97     reply.WriteInt32(ret);
98     return 0;
99 }
100 
ReportModuleUpdateStatusStub(ModuleUpdateStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option) const101 int32_t ModuleUpdateStub::ReportModuleUpdateStatusStub(ModuleUpdateStub *service,
102     MessageParcel &data, MessageParcel &reply, MessageOption &option) const
103 {
104     if (service == nullptr) {
105         LOG(ERROR) << "Invalid param";
106         return -1;
107     }
108     ModuleUpdateStatus status;
109     ModuleIpcHelper::ReadModuleUpdateStatus(data, status);
110     int32_t ret = service->ReportModuleUpdateStatus(status);
111     reply.WriteInt32(ret);
112     return 0;
113 }
114 
ExitModuleUpdateStub(ModuleUpdateStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option) const115 int32_t ModuleUpdateStub::ExitModuleUpdateStub(ModuleUpdateStub *service,
116     MessageParcel &data, MessageParcel &reply, MessageOption &option) const
117 {
118     if (service == nullptr) {
119         LOG(ERROR) << "Invalid param";
120         return -1;
121     }
122     int32_t ret = service->ExitModuleUpdate();
123     reply.WriteInt32(ret);
124     return 0;
125 }
126 
GetHmpVersionInfoStub(ModuleUpdateStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option) const127 int32_t ModuleUpdateStub::GetHmpVersionInfoStub(ModuleUpdateStub *service,
128     MessageParcel &data, MessageParcel &reply, MessageOption &option) const
129 {
130     if (service == nullptr) {
131         LOG(ERROR) << "Invalid param";
132         return -1;
133     }
134     std::vector<HmpVersionInfo> versionInfo = service->GetHmpVersionInfo();
135     reply.WriteInt32(versionInfo.size());
136     for (auto &info : versionInfo) {
137         reply.WriteParcelable(&info);
138     }
139     return 0;
140 }
141 
StartUpdateHmpPackageStub(ModuleUpdateStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option) const142 int32_t ModuleUpdateStub::StartUpdateHmpPackageStub(ModuleUpdateStub *service,
143     MessageParcel &data, MessageParcel &reply, MessageOption &option) const
144 {
145     if (service == nullptr) {
146         LOG(ERROR) << "Invalid param";
147         return -1;
148     }
149 
150     sptr<IRemoteObject> object = data.ReadRemoteObject();
151     if (object == nullptr) {
152         LOG(ERROR) << "object null";
153         return -1;
154     }
155 
156     sptr<ISysInstallerCallback> updateCallback = iface_cast<ISysInstallerCallback>(object);
157     if (updateCallback == nullptr) {
158         LOG(ERROR) << "ISysInstallerCallback updateCallback is null";
159         return ERR_NULL_OBJECT;
160     }
161     std::string path = Str16ToStr8(data.ReadString16());
162     LOG(ERROR) << "StartUpdateHmpPackageStub path:" << path;
163 
164     int32_t ret = service->StartUpdateHmpPackage(path, updateCallback);
165     reply.WriteInt32(ret);
166     return 0;
167 }
168 
GetHmpUpdateResultStub(ModuleUpdateStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option) const169 int32_t ModuleUpdateStub::GetHmpUpdateResultStub(ModuleUpdateStub *service,
170     MessageParcel &data, MessageParcel &reply, MessageOption &option) const
171 {
172     if (service == nullptr) {
173         LOG(ERROR) << "Invalid param";
174         return -1;
175     }
176     std::vector<HmpUpdateInfo> updateInfo = service->GetHmpUpdateResult();
177     reply.WriteInt32(updateInfo.size());
178     for (auto &info : updateInfo) {
179         reply.WriteParcelable(&info);
180     }
181     return 0;
182 }
183 
IsPermissionGranted(void)184 bool ModuleUpdateStub::IsPermissionGranted(void)
185 {
186     Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
187     std::string permission = "ohos.permission.UPDATE_SYSTEM";
188 
189     int verifyResult = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, permission);
190     bool isPermissionGranted = (verifyResult == Security::AccessToken::PERMISSION_GRANTED);
191     if (!isPermissionGranted) {
192         LOG(ERROR) << "not granted " << permission.c_str();
193     }
194     return isPermissionGranted;
195 }
196 
CheckCallingPerm(void)197 bool ModuleUpdateStub::CheckCallingPerm(void)
198 {
199     int32_t callingUid = OHOS::IPCSkeleton::GetCallingUid();
200     LOG(INFO) << "CheckCallingPerm callingUid:" << callingUid;
201     if (callingUid == 0) {
202         return true;
203     }
204     return callingUid == USER_UPDATE_AUTHORITY && IsPermissionGranted();
205 }
206 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)207 int32_t ModuleUpdateStub::OnRemoteRequest(uint32_t code,
208     MessageParcel &data, MessageParcel &reply, MessageOption &option)
209 {
210     if (data.ReadInterfaceToken() != GetDescriptor()) {
211         LOG(ERROR) << "ModuleUpdateStub ReadInterfaceToken fail";
212         return -1;
213     }
214 
215     LOG(INFO) << "OnRemoteRequest func code " << code;
216     auto inter = requestFuncMap_.find(code);
217     if (inter != requestFuncMap_.end()) {
218         if (!CheckCallingPerm()) {
219             LOG(ERROR) << "ModuleUpdateStub CheckCallingPerm fail";
220             return -1;
221         }
222         return inter->second(this, data, reply, option);
223     }
224 
225     LOG(INFO) << "ModuleUpdateStub OnRemoteRequest code " << code << "not found";
226     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
227 }
228 } // namespace SysInstaller
229 } // namespace OHOS