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 #define LOG_TAG "CloudServiceStub"
16 #include "cloud_service_stub.h"
17
18 #include "cloud/cloud_config_manager.h"
19 #include "cloud_types_util.h"
20 #include "ipc_skeleton.h"
21 #include "log_print.h"
22 #include "permission_validator.h"
23 #include "rdb_types.h"
24 #include "tokenid_kit.h"
25 #include "utils/anonymous.h"
26 namespace OHOS::CloudData {
27 using namespace DistributedData;
28 using namespace OHOS::Security::AccessToken;
29 const CloudServiceStub::Handler CloudServiceStub::HANDLERS[TRANS_BUTT] = {
30 &CloudServiceStub::OnEnableCloud,
31 &CloudServiceStub::OnDisableCloud,
32 &CloudServiceStub::OnChangeAppSwitch,
33 &CloudServiceStub::OnClean,
34 &CloudServiceStub::OnNotifyDataChange,
35 &CloudServiceStub::OnNotifyChange,
36 &CloudServiceStub::OnQueryStatistics,
37 &CloudServiceStub::OnQueryLastSyncInfo,
38 &CloudServiceStub::OnSetGlobalCloudStrategy,
39 &CloudServiceStub::OnCloudSync,
40 &CloudServiceStub::OnAllocResourceAndShare,
41 &CloudServiceStub::OnShare,
42 &CloudServiceStub::OnUnshare,
43 &CloudServiceStub::OnExit,
44 &CloudServiceStub::OnChangePrivilege,
45 &CloudServiceStub::OnQuery,
46 &CloudServiceStub::OnQueryByInvitation,
47 &CloudServiceStub::OnConfirmInvitation,
48 &CloudServiceStub::OnChangeConfirmation,
49 &CloudServiceStub::OnSetCloudStrategy,
50 &CloudServiceStub::OnInitNotifier,
51 };
52
OnRemoteRequest(uint32_t code,OHOS::MessageParcel & data,OHOS::MessageParcel & reply)53 int CloudServiceStub::OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply)
54 {
55 ZLOGI("code:%{public}u callingPid:%{public}u", code, IPCSkeleton::GetCallingPid());
56 std::u16string local = CloudServiceStub::GetDescriptor();
57 std::u16string remote = data.ReadInterfaceToken();
58 if (local != remote) {
59 ZLOGE("local is not equal to remote");
60 return -1;
61 }
62
63 if (TRANS_HEAD > code || code >= TRANS_BUTT || HANDLERS[code] == nullptr) {
64 ZLOGE("not support code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT);
65 return -1;
66 }
67
68 if (((code >= TRANS_CONFIG_HEAD && code < TRANS_CONFIG_BUTT) ||
69 (code >= TRANS_SHARE_HEAD && code < TRANS_SHARE_BUTT)) &&
70 !TokenIdKit::IsSystemAppByFullTokenID(IPCSkeleton::GetCallingFullTokenID())) {
71 ZLOGE("permission denied! code:%{public}u", code);
72 auto result = static_cast<int32_t>(PERMISSION_DENIED);
73 return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
74 }
75
76 if (code >= TRANS_CONFIG_HEAD && code < TRANS_CONFIG_BUTT &&
77 !DistributedKv::PermissionValidator::GetInstance().IsCloudConfigPermit(IPCSkeleton::GetCallingTokenID())) {
78 ZLOGE("cloud config permission denied! code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT);
79 auto result = static_cast<int32_t>(CLOUD_CONFIG_PERMISSION_DENIED);
80 return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
81 }
82 return (this->*HANDLERS[code])(data, reply);
83 }
84
OnEnableCloud(MessageParcel & data,MessageParcel & reply)85 int32_t CloudServiceStub::OnEnableCloud(MessageParcel &data, MessageParcel &reply)
86 {
87 std::string id;
88 std::map<std::string, int32_t> switches;
89 if (!ITypesUtil::Unmarshal(data, id, switches)) {
90 ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str());
91 return IPC_STUB_INVALID_DATA_ERR;
92 }
93 std::map<std::string, int32_t> localSwitches;
94 for (const auto &[bundle, status] : switches) {
95 localSwitches.insert_or_assign(CloudConfigManager::GetInstance().ToLocal(bundle), status);
96 }
97 auto result = EnableCloud(id, localSwitches);
98 return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
99 }
100
OnDisableCloud(MessageParcel & data,MessageParcel & reply)101 int32_t CloudServiceStub::OnDisableCloud(MessageParcel &data, MessageParcel &reply)
102 {
103 std::string id;
104 if (!ITypesUtil::Unmarshal(data, id)) {
105 ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str());
106 return IPC_STUB_INVALID_DATA_ERR;
107 }
108 auto result = DisableCloud(id);
109 return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
110 }
111
OnChangeAppSwitch(MessageParcel & data,MessageParcel & reply)112 int32_t CloudServiceStub::OnChangeAppSwitch(MessageParcel &data, MessageParcel &reply)
113 {
114 std::string id;
115 std::string bundleName;
116 int32_t appSwitch = SWITCH_OFF;
117 if (!ITypesUtil::Unmarshal(data, id, bundleName, appSwitch)) {
118 ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str());
119 return IPC_STUB_INVALID_DATA_ERR;
120 }
121 auto result = ChangeAppSwitch(id, CloudConfigManager::GetInstance().ToLocal(bundleName), appSwitch);
122 return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
123 }
124
OnClean(MessageParcel & data,MessageParcel & reply)125 int32_t CloudServiceStub::OnClean(MessageParcel &data, MessageParcel &reply)
126 {
127 std::string id;
128 std::map<std::string, int32_t> actions;
129 if (!ITypesUtil::Unmarshal(data, id, actions)) {
130 ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str());
131 return IPC_STUB_INVALID_DATA_ERR;
132 }
133 std::map<std::string, int32_t> localActions;
134 for (const auto &[bundle, action] : actions) {
135 localActions.insert_or_assign(CloudConfigManager::GetInstance().ToLocal(bundle), action);
136 }
137 auto result = Clean(id, localActions);
138 return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
139 }
140
OnNotifyDataChange(MessageParcel & data,MessageParcel & reply)141 int32_t CloudServiceStub::OnNotifyDataChange(MessageParcel &data, MessageParcel &reply)
142 {
143 std::string id;
144 std::string bundleName;
145 if (!ITypesUtil::Unmarshal(data, id, bundleName)) {
146 ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str());
147 return IPC_STUB_INVALID_DATA_ERR;
148 }
149 auto result = NotifyDataChange(id, CloudConfigManager::GetInstance().ToLocal(bundleName));
150 return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
151 }
152
OnQueryLastSyncInfo(MessageParcel & data,MessageParcel & reply)153 int32_t CloudServiceStub::OnQueryLastSyncInfo(MessageParcel &data, MessageParcel &reply)
154 {
155 std::string id;
156 std::string bundleName;
157 std::string storeId;
158 if (!ITypesUtil::Unmarshal(data, id, bundleName, storeId)) {
159 ZLOGE("Unmarshal id:%{public}s, bundleName:%{public}s, storeId:%{public}s", Anonymous::Change(id).c_str(),
160 bundleName.c_str(), Anonymous::Change(storeId).c_str());
161 return IPC_STUB_INVALID_DATA_ERR;
162 }
163 auto [status, results] = QueryLastSyncInfo(id, bundleName, storeId);
164 return ITypesUtil::Marshal(reply, status, results) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
165 }
166
OnSetGlobalCloudStrategy(MessageParcel & data,MessageParcel & reply)167 int32_t CloudServiceStub::OnSetGlobalCloudStrategy(MessageParcel &data, MessageParcel &reply)
168 {
169 Strategy strategy;
170 std::vector<CommonType::Value> values;
171 if (!ITypesUtil::Unmarshal(data, strategy, values)) {
172 ZLOGE("Unmarshal strategy:%{public}d, values size:%{public}zu", strategy, values.size());
173 return IPC_STUB_INVALID_DATA_ERR;
174 }
175 auto status = SetGlobalCloudStrategy(strategy, values);
176 return ITypesUtil::Marshal(reply, status) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
177 }
178
OnAllocResourceAndShare(MessageParcel & data,MessageParcel & reply)179 int32_t CloudServiceStub::OnAllocResourceAndShare(MessageParcel &data, MessageParcel &reply)
180 {
181 std::string storeId;
182 DistributedRdb::PredicatesMemo predicatesMemo;
183 std::vector<std::string> columns;
184 std::vector<Participant> participants;
185 if (!ITypesUtil::Unmarshal(data, storeId, predicatesMemo, columns, participants)) {
186 ZLOGE("Unmarshal storeId:%{public}s", Anonymous::Change(storeId).c_str());
187 return IPC_STUB_INVALID_DATA_ERR;
188 }
189 auto [status, resultSet] = AllocResourceAndShare(storeId, predicatesMemo, columns, participants);
190 return ITypesUtil::Marshal(reply, status, resultSet) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
191 }
192
OnNotifyChange(MessageParcel & data,MessageParcel & reply)193 int32_t CloudServiceStub::OnNotifyChange(MessageParcel &data, MessageParcel &reply)
194 {
195 std::string eventId;
196 std::string extraData;
197 int32_t userId;
198 if (!ITypesUtil::Unmarshal(data, eventId, extraData, userId)) {
199 ZLOGE("Unmarshal eventId:%{public}s", Anonymous::Change(eventId).c_str());
200 return IPC_STUB_INVALID_DATA_ERR;
201 }
202 auto result = NotifyDataChange(eventId, extraData, userId);
203 return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
204 }
205
OnQueryStatistics(MessageParcel & data,MessageParcel & reply)206 int32_t CloudServiceStub::OnQueryStatistics(MessageParcel &data, MessageParcel &reply)
207 {
208 std::string id;
209 std::string bundleName;
210 std::string storeId;
211 if (!ITypesUtil::Unmarshal(data, id, bundleName, storeId)) {
212 ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str());
213 return IPC_STUB_INVALID_DATA_ERR;
214 }
215 auto result = QueryStatistics(id, bundleName, storeId);
216 return ITypesUtil::Marshal(reply, result.first, result.second) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
217 }
218
OnShare(MessageParcel & data,MessageParcel & reply)219 int32_t CloudServiceStub::OnShare(MessageParcel &data, MessageParcel &reply)
220 {
221 std::string sharingRes;
222 Participants participants;
223 if (!ITypesUtil::Unmarshal(data, sharingRes, participants)) {
224 ZLOGE("Unmarshal sharingRes:%{public}s", Anonymous::Change(sharingRes).c_str());
225 return IPC_STUB_INVALID_DATA_ERR;
226 }
227 Results results;
228 auto status = Share(sharingRes, participants, results);
229 return ITypesUtil::Marshal(reply, status, results) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
230 }
231
OnUnshare(MessageParcel & data,MessageParcel & reply)232 int32_t CloudServiceStub::OnUnshare(MessageParcel &data, MessageParcel &reply)
233 {
234 std::string sharingRes;
235 Participants participants;
236 if (!ITypesUtil::Unmarshal(data, sharingRes, participants)) {
237 ZLOGE("Unmarshal sharingRes:%{public}s", Anonymous::Change(sharingRes).c_str());
238 return IPC_STUB_INVALID_DATA_ERR;
239 }
240 Results results;
241 auto status = Unshare(sharingRes, participants, results);
242 return ITypesUtil::Marshal(reply, status, results) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
243 }
244
OnExit(MessageParcel & data,MessageParcel & reply)245 int32_t CloudServiceStub::OnExit(MessageParcel &data, MessageParcel &reply)
246 {
247 std::string sharingRes;
248 if (!ITypesUtil::Unmarshal(data, sharingRes)) {
249 ZLOGE("Unmarshal sharingRes:%{public}s", Anonymous::Change(sharingRes).c_str());
250 return IPC_STUB_INVALID_DATA_ERR;
251 }
252 std::pair<int32_t, std::string> result;
253 auto status = Exit(sharingRes, result);
254 return ITypesUtil::Marshal(reply, status, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
255 }
256
OnChangePrivilege(MessageParcel & data,MessageParcel & reply)257 int32_t CloudServiceStub::OnChangePrivilege(MessageParcel &data, MessageParcel &reply)
258 {
259 std::string sharingRes;
260 Participants participants;
261 if (!ITypesUtil::Unmarshal(data, sharingRes, participants)) {
262 ZLOGE("Unmarshal sharingRes:%{public}s", Anonymous::Change(sharingRes).c_str());
263 return IPC_STUB_INVALID_DATA_ERR;
264 }
265 Results results;
266 auto status = ChangePrivilege(sharingRes, participants, results);
267 return ITypesUtil::Marshal(reply, status, results) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
268 }
269
OnQuery(MessageParcel & data,MessageParcel & reply)270 int32_t CloudServiceStub::OnQuery(MessageParcel &data, MessageParcel &reply)
271 {
272 std::string sharingRes;
273 if (!ITypesUtil::Unmarshal(data, sharingRes)) {
274 ZLOGE("Unmarshal sharingRes:%{public}s", Anonymous::Change(sharingRes).c_str());
275 return IPC_STUB_INVALID_DATA_ERR;
276 }
277 QueryResults results;
278 auto status = Query(sharingRes, results);
279 return ITypesUtil::Marshal(reply, status, results) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
280 }
281
OnQueryByInvitation(MessageParcel & data,MessageParcel & reply)282 int32_t CloudServiceStub::OnQueryByInvitation(MessageParcel &data, MessageParcel &reply)
283 {
284 std::string invitation;
285 if (!ITypesUtil::Unmarshal(data, invitation)) {
286 ZLOGE("Unmarshal invitation:%{public}s", Anonymous::Change(invitation).c_str());
287 return IPC_STUB_INVALID_DATA_ERR;
288 }
289 QueryResults results;
290 auto status = QueryByInvitation(invitation, results);
291 return ITypesUtil::Marshal(reply, status, results) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
292 }
293
OnConfirmInvitation(MessageParcel & data,MessageParcel & reply)294 int32_t CloudServiceStub::OnConfirmInvitation(MessageParcel &data, MessageParcel &reply)
295 {
296 std::string invitation;
297 int32_t confirmation;
298 if (!ITypesUtil::Unmarshal(data, invitation, confirmation)) {
299 ZLOGE("Unmarshal invitation:%{public}s", Anonymous::Change(invitation).c_str());
300 return IPC_STUB_INVALID_DATA_ERR;
301 }
302 std::tuple<int32_t, std::string, std::string> result;
303 auto status = ConfirmInvitation(invitation, confirmation, result);
304 return ITypesUtil::Marshal(reply, status, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
305 }
306
OnChangeConfirmation(MessageParcel & data,MessageParcel & reply)307 int32_t CloudServiceStub::OnChangeConfirmation(MessageParcel &data, MessageParcel &reply)
308 {
309 std::string sharingRes;
310 int32_t confirmation;
311 if (!ITypesUtil::Unmarshal(data, sharingRes, confirmation)) {
312 ZLOGE("Unmarshal sharingRes:%{public}s", Anonymous::Change(sharingRes).c_str());
313 return IPC_STUB_INVALID_DATA_ERR;
314 }
315 std::pair<int32_t, std::string> result;
316 auto status = ChangeConfirmation(sharingRes, confirmation, result);
317 return ITypesUtil::Marshal(reply, status, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
318 }
319
OnSetCloudStrategy(MessageParcel & data,MessageParcel & reply)320 int32_t CloudServiceStub::OnSetCloudStrategy(MessageParcel &data, MessageParcel &reply)
321 {
322 Strategy strategy;
323 std::vector<CommonType::Value> values;
324 if (!ITypesUtil::Unmarshal(data, strategy, values)) {
325 ZLOGE("Unmarshal strategy:%{public}d, values size:%{public}zu", strategy, values.size());
326 return IPC_STUB_INVALID_DATA_ERR;
327 }
328 auto status = SetCloudStrategy(strategy, values);
329 return ITypesUtil::Marshal(reply, status) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
330 }
331
OnCloudSync(MessageParcel & data,MessageParcel & reply)332 int32_t CloudServiceStub::OnCloudSync(MessageParcel &data, MessageParcel &reply)
333 {
334 std::string bundleName;
335 std::string storeId;
336 Option option;
337 if (!ITypesUtil::Unmarshal(data, bundleName, storeId, option)) {
338 ZLOGE("Unmarshal failed, bundleName:%{public}s, storeId:%{public}s, syncMode:%{public}d, seqNum:%{public}u",
339 bundleName.c_str(), Anonymous::Change(storeId).c_str(), option.syncMode, option.seqNum);
340 return IPC_STUB_INVALID_DATA_ERR;
341 }
342 auto status = CloudSync(bundleName, storeId, option, nullptr);
343 return ITypesUtil::Marshal(reply, status) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
344 }
345
OnInitNotifier(MessageParcel & data,MessageParcel & reply)346 int32_t CloudServiceStub::OnInitNotifier(MessageParcel &data, MessageParcel &reply)
347 {
348 sptr<IRemoteObject> notifier = nullptr;
349 if (!ITypesUtil::Unmarshal(data, notifier) || notifier == nullptr) {
350 ZLOGE("Unmarshal failed, notifier is nullptr?[%{public}u]", notifier == nullptr);
351 return IPC_STUB_INVALID_DATA_ERR;
352 }
353 auto status = InitNotifier(notifier);
354 return ITypesUtil::Marshal(reply, status) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
355 }
356 } // namespace OHOS::CloudData