1 /*
2 * Copyright (c) 2021 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 "accesstoken_kit.h"
17 #include "hap_token_info.h"
18 #include "ipc_skeleton.h"
19 #include "securec.h"
20 #include "update_helper.h"
21 #include "update_service_stub.h"
22
23 using namespace std;
24
25 namespace OHOS {
26 namespace update_engine {
GetNewVersionStub(UpdateServiceStub::UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)27 static int32_t GetNewVersionStub(UpdateServiceStub::UpdateServiceStubPtr service,
28 MessageParcel& data, MessageParcel& reply, MessageOption &option)
29 {
30 ENGINE_CHECK(service != nullptr, return -1, "Invalid param");
31 VersionInfo info = {};
32 int32_t ret = service->GetNewVersion(info);
33 ENGINE_CHECK(ret == 0, return -1, "Failed to get new version");
34 UpdateHelper::WriteVersionInfo(reply, info);
35 return 0;
36 }
37
GetUpgradeStatusStub(UpdateServiceStub::UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)38 static int32_t GetUpgradeStatusStub(UpdateServiceStub::UpdateServiceStubPtr service,
39 MessageParcel& data, MessageParcel& reply, MessageOption &option)
40 {
41 ENGINE_CHECK(service != nullptr, return -1, "Invalid param");
42 UpgradeInfo info = {};
43 int32_t ret = service->GetUpgradeStatus(info);
44 ENGINE_CHECK(ret == 0, return -1, "Failed to get new upgrade status");
45 UpdateHelper::WriteUpgradeInfo(reply, info);
46 return 0;
47 }
48
CheckNewVersionStub(UpdateServiceStub::UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)49 static int32_t CheckNewVersionStub(UpdateServiceStub::UpdateServiceStubPtr service,
50 MessageParcel& data, MessageParcel& reply, MessageOption &option)
51 {
52 ENGINE_CHECK(service != nullptr, return -1, "Invalid param");
53 int32_t ret = service->CheckNewVersion();
54 reply.WriteInt32(ret);
55 return 0;
56 }
57
DownloadVersionStub(UpdateServiceStub::UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)58 static int32_t DownloadVersionStub(UpdateServiceStub::UpdateServiceStubPtr service,
59 MessageParcel& data, MessageParcel& reply, MessageOption &option)
60 {
61 ENGINE_CHECK(service != nullptr, return -1, "Invalid param");
62 int32_t ret = service->DownloadVersion();
63 reply.WriteInt32(ret);
64 return 0;
65 }
66
DoUpdateStub(UpdateServiceStub::UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)67 static int32_t DoUpdateStub(UpdateServiceStub::UpdateServiceStubPtr service,
68 MessageParcel& data, MessageParcel& reply, MessageOption &option)
69 {
70 ENGINE_CHECK(service != nullptr, return -1, "Invalid param");
71 int32_t ret = service->DoUpdate();
72 reply.WriteInt32(ret);
73 return 0;
74 }
75
SetUpdatePolicyStub(UpdateServiceStub::UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)76 static int32_t SetUpdatePolicyStub(UpdateServiceStub::UpdateServiceStubPtr service,
77 MessageParcel& data, MessageParcel& reply, MessageOption &option)
78 {
79 ENGINE_CHECK(service != nullptr, return -1, "Invalid param");
80 UpdatePolicy policy = {};
81 UpdateHelper::ReadUpdatePolicy(data, policy);
82 int32_t ret = service->SetUpdatePolicy(policy);
83 reply.WriteInt32(ret);
84 return 0;
85 }
86
GetUpdatePolicyStub(UpdateServiceStub::UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)87 static int32_t GetUpdatePolicyStub(UpdateServiceStub::UpdateServiceStubPtr service,
88 MessageParcel& data, MessageParcel& reply, MessageOption &option)
89 {
90 ENGINE_CHECK(service != nullptr, return -1, "Invalid param");
91 UpdatePolicy policy = {};
92 int32_t ret = service->GetUpdatePolicy(policy);
93 ENGINE_CHECK(ret == 0, return -1, "Failed to get new get policy");
94 UpdateHelper::WriteUpdatePolicy(reply, policy);
95 return 0;
96 }
97
RegisterUpdateCallbackStub(UpdateServiceStub::UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)98 static int32_t RegisterUpdateCallbackStub(UpdateServiceStub::UpdateServiceStubPtr service,
99 MessageParcel& data, MessageParcel& reply, MessageOption &option)
100 {
101 ENGINE_CHECK(service != nullptr, return -1, "Invalid param");
102 int32_t ret = -1;
103 UpdateContext ctx {};
104 UpdateHelper::ReadUpdateContext(data, ctx);
105 auto remote = data.ReadRemoteObject();
106 ENGINE_CHECK(remote != nullptr, reply.WriteInt32(ret); return 0, "Failed to read remote obj");
107 ret = service->RegisterUpdateCallback(ctx, iface_cast<IUpdateCallback>(remote));
108 reply.WriteInt32(ret);
109 return 0;
110 }
111
UnregisterUpdateCallbackStub(UpdateServiceStub::UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)112 static int32_t UnregisterUpdateCallbackStub(UpdateServiceStub::UpdateServiceStubPtr service,
113 MessageParcel& data, MessageParcel& reply, MessageOption &option)
114 {
115 ENGINE_CHECK(service != nullptr, return -1, "Invalid param");
116 int32_t ret = service->UnregisterUpdateCallback();
117 reply.WriteInt32(ret);
118 return 0;
119 }
120
CancelStub(UpdateServiceStub::UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)121 static int32_t CancelStub(UpdateServiceStub::UpdateServiceStubPtr service,
122 MessageParcel& data, MessageParcel& reply, MessageOption &option)
123 {
124 ENGINE_CHECK(service != nullptr, return -1, "Invalid param");
125 int32_t ret = service->Cancel(data.ReadInt32());
126 reply.WriteInt32(ret);
127 return 0;
128 }
129
RebootAndCleanStub(UpdateServiceStub::UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)130 static int32_t RebootAndCleanStub(UpdateServiceStub::UpdateServiceStubPtr service,
131 MessageParcel& data, MessageParcel& reply, MessageOption &option)
132 {
133 ENGINE_CHECK(service != nullptr, return -1, "Invalid param");
134 string miscFile = Str16ToStr8(data.ReadString16());
135 string cmd = Str16ToStr8(data.ReadString16());
136 int32_t ret = service->RebootAndClean(miscFile, cmd);
137 reply.WriteInt32(ret);
138 return 0;
139 }
140
RebootAndInstallStub(UpdateServiceStub::UpdateServiceStubPtr service,MessageParcel & data,MessageParcel & reply,MessageOption & option)141 static int32_t RebootAndInstallStub(UpdateServiceStub::UpdateServiceStubPtr service,
142 MessageParcel& data, MessageParcel& reply, MessageOption &option)
143 {
144 ENGINE_CHECK(service != nullptr, return -1, "Invalid param");
145 string miscFile = Str16ToStr8(data.ReadString16());
146 string packageName = Str16ToStr8(data.ReadString16());
147 int32_t ret = service->RebootAndInstall(miscFile, packageName);
148 reply.WriteInt32(ret);
149 return 0;
150 }
151
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)152 int32_t UpdateServiceStub::OnRemoteRequest(uint32_t code,
153 MessageParcel& data, MessageParcel& reply, MessageOption &option)
154 {
155 if (data.ReadInterfaceToken() != GetDescriptor()) {
156 ENGINE_LOGI("UpdateServiceStub ReadInterfaceToken fail");
157 return -1;
158 }
159 static std::map<uint32_t, UpdateServiceStub::RequestFuncType> requestFuncMap = {
160 {IUpdateService::CHECK_VERSION, CheckNewVersionStub},
161 {IUpdateService::DOWNLOAD, DownloadVersionStub},
162 {IUpdateService::UPGRADE, DoUpdateStub},
163 {IUpdateService::SET_POLICY, SetUpdatePolicyStub},
164 {IUpdateService::GET_POLICY, GetUpdatePolicyStub},
165 {IUpdateService::GET_NEW_VERSION, GetNewVersionStub},
166 {IUpdateService::GET_STATUS, GetUpgradeStatusStub},
167 {IUpdateService::REGISTER_CALLBACK, RegisterUpdateCallbackStub},
168 {IUpdateService::UNREGISTER_CALLBACK, UnregisterUpdateCallbackStub},
169 {IUpdateService::CANCEL, CancelStub},
170 {IUpdateService::REBOOT_CLEAN, RebootAndCleanStub},
171 {IUpdateService::REBOOT_INSTALL, RebootAndInstallStub},
172 };
173
174 Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
175 Security::AccessToken::HapTokenInfo hapTokenInfoRes = {};
176
177 int re = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(callerToken, hapTokenInfoRes);
178 ENGINE_LOGI("UpdateServiceStub GetHapTokenInfo re %{public}d, bundle name %{public}s", re,
179 hapTokenInfoRes.bundleName.c_str());
180
181 ENGINE_LOGI("UpdateServiceStub OnRemoteRequest code %{public}u", code);
182 string permission = "ohos.permission.UPDATE_SYSTEM";
183 if (code == IUpdateService::REBOOT_CLEAN) {
184 permission = "ohos.permission.FACTORY_RESET";
185 }
186
187 int result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, permission);
188 if (result != Security::AccessToken::PERMISSION_GRANTED) {
189 ENGINE_LOGE("permissionCheck %{public}s false", permission.c_str());
190 return -1;
191 }
192
193 ENGINE_LOGI("UpdateServiceStub func code %{public}u", code);
194 for (auto inter = requestFuncMap.begin(); inter != requestFuncMap.end(); inter++) {
195 if (inter->first == code) {
196 return inter->second(this, data, reply, option);
197 }
198 }
199 ENGINE_LOGE("UpdateServiceStub OnRemoteRequest code %{public}u not found", code);
200 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
201 }
202 } // namespace update_engine
203 } // namespace OHOS