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 #define LOG_TAG "KVDBServiceStub"
16 #include "kvdb_service_stub.h"
17
18 #include "checker/checker_manager.h"
19 #include "ipc_skeleton.h"
20 #include "itypes_util.h"
21 #include "log_print.h"
22 #include "utils/constant.h"
23 #include "utils/anonymous.h"
24 namespace OHOS::DistributedKv {
25 using namespace OHOS::DistributedData;
26 const KVDBServiceStub::Handler
27 KVDBServiceStub::HANDLERS[static_cast<uint32_t>(KVDBServiceInterfaceCode::TRANS_BUTT)] = {
28 &KVDBServiceStub::OnGetStoreIds,
29 &KVDBServiceStub::OnBeforeCreate,
30 &KVDBServiceStub::OnAfterCreate,
31 &KVDBServiceStub::OnDelete,
32 &KVDBServiceStub::OnSync,
33 &KVDBServiceStub::OnRegisterCallback,
34 &KVDBServiceStub::OnUnregisterCallback,
35 &KVDBServiceStub::OnSetSyncParam,
36 &KVDBServiceStub::OnGetSyncParam,
37 &KVDBServiceStub::OnEnableCap,
38 &KVDBServiceStub::OnDisableCap,
39 &KVDBServiceStub::OnSetCapability,
40 &KVDBServiceStub::OnAddSubInfo,
41 &KVDBServiceStub::OnRmvSubInfo,
42 &KVDBServiceStub::OnSubscribe,
43 &KVDBServiceStub::OnUnsubscribe,
44 &KVDBServiceStub::OnGetBackupPassword,
45 &KVDBServiceStub::OnSyncExt,
46 };
47
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)48 int KVDBServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
49 {
50 ZLOGI("code:%{public}u callingPid:%{public}u", code, IPCSkeleton::GetCallingPid());
51 std::u16string local = KVDBServiceStub::GetDescriptor();
52 std::u16string remote = data.ReadInterfaceToken();
53 if (local != remote) {
54 ZLOGE("local is not equal to remote");
55 return -1;
56 }
57
58 if (static_cast<uint32_t>(KVDBServiceInterfaceCode::TRANS_HEAD) > code ||
59 code >= static_cast<uint32_t>(KVDBServiceInterfaceCode::TRANS_BUTT) || HANDLERS[code] == nullptr) {
60 ZLOGE("not support code:%{public}u, BUTT:%{public}d", code,
61 static_cast<uint32_t>(KVDBServiceInterfaceCode::TRANS_BUTT));
62 return -1;
63 }
64
65 AppId appId;
66 StoreId storeId;
67 if (!ITypesUtil::Unmarshal(data, appId, storeId)) {
68 ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
69 Anonymous::Change(storeId.storeId).c_str());
70 return IPC_STUB_INVALID_DATA_ERR;
71 }
72 appId.appId = Constant::TrimCopy(appId.appId);
73 storeId.storeId = Constant::TrimCopy(storeId.storeId);
74
75 CheckerManager::StoreInfo info;
76 info.uid = IPCSkeleton::GetCallingUid();
77 info.tokenId = IPCSkeleton::GetCallingTokenID();
78 info.bundleName = appId.appId;
79 info.storeId = storeId.storeId;
80 if (CheckerManager::GetInstance().IsValid(info)) {
81 return (this->*HANDLERS[code])(appId, storeId, data, reply);
82 }
83 ZLOGE("PERMISSION_DENIED uid:%{public}d appId:%{public}s storeId:%{public}s", info.uid, info.bundleName.c_str(),
84 Anonymous::Change(info.storeId).c_str());
85
86 if (!ITypesUtil::Marshal(reply, static_cast<int32_t>(PERMISSION_DENIED))) {
87 ZLOGE("Marshal PERMISSION_DENIED code:%{public}u appId:%{public}s storeId:%{public}s", code,
88 appId.appId.c_str(), Anonymous::Change(storeId.storeId).c_str());
89 return IPC_STUB_WRITE_PARCEL_ERR;
90 }
91 return ERR_NONE;
92 }
93
OnGetStoreIds(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)94 int32_t KVDBServiceStub::OnGetStoreIds(
95 const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
96 {
97 std::vector<StoreId> storeIds;
98 int32_t status = GetStoreIds(appId, storeIds);
99 if (!ITypesUtil::Marshal(reply, status, storeIds)) {
100 ZLOGE("Marshal status:0x%{public}d storeIds:%{public}zu", status, storeIds.size());
101 return IPC_STUB_WRITE_PARCEL_ERR;
102 }
103 return ERR_NONE;
104 }
105
OnBeforeCreate(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)106 int32_t KVDBServiceStub::OnBeforeCreate(
107 const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
108 {
109 Options options;
110 if (!ITypesUtil::Unmarshal(data, options)) {
111 ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
112 Anonymous::Change(storeId.storeId).c_str());
113 return IPC_STUB_INVALID_DATA_ERR;
114 }
115 int32_t status = BeforeCreate(appId, storeId, options);
116 if (!ITypesUtil::Marshal(reply, status)) {
117 ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
118 Anonymous::Change(storeId.storeId).c_str());
119 return IPC_STUB_WRITE_PARCEL_ERR;
120 }
121 return ERR_NONE;
122 }
123
OnAfterCreate(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)124 int32_t KVDBServiceStub::OnAfterCreate(
125 const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
126 {
127 Options options;
128 std::vector<uint8_t> password;
129 if (!ITypesUtil::Unmarshal(data, options, password)) {
130 ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
131 Anonymous::Change(storeId.storeId).c_str());
132 return IPC_STUB_INVALID_DATA_ERR;
133 }
134 int32_t status = AfterCreate(appId, storeId, options, password);
135 password.assign(password.size(), 0);
136 if (!ITypesUtil::Marshal(reply, status)) {
137 ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
138 Anonymous::Change(storeId.storeId).c_str());
139 return IPC_STUB_WRITE_PARCEL_ERR;
140 }
141 return ERR_NONE;
142 }
143
OnDelete(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)144 int32_t KVDBServiceStub::OnDelete(const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
145 {
146 int32_t status = Delete(appId, storeId);
147 if (!ITypesUtil::Marshal(reply, status)) {
148 ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
149 Anonymous::Change(storeId.storeId).c_str());
150 return IPC_STUB_WRITE_PARCEL_ERR;
151 }
152 return ERR_NONE;
153 }
154
OnSync(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)155 int32_t KVDBServiceStub::OnSync(const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
156 {
157 SyncInfo syncInfo;
158 if (!ITypesUtil::Unmarshal(data, syncInfo.seqId, syncInfo.mode, syncInfo.devices, syncInfo.delay, syncInfo.query)) {
159 ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
160 Anonymous::Change(storeId.storeId).c_str());
161 return IPC_STUB_INVALID_DATA_ERR;
162 }
163 int32_t status = Sync(appId, storeId, syncInfo);
164 if (!ITypesUtil::Marshal(reply, status)) {
165 ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
166 Anonymous::Change(storeId.storeId).c_str());
167 return IPC_STUB_WRITE_PARCEL_ERR;
168 }
169 return ERR_NONE;
170 }
171
OnSyncExt(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)172 int32_t KVDBServiceStub::OnSyncExt(
173 const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
174 {
175 SyncInfo syncInfo;
176 if (!ITypesUtil::Unmarshal(
177 data, syncInfo.seqId, syncInfo.mode, syncInfo.devices, syncInfo.delay, syncInfo.query)) {
178 ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s",
179 appId.appId.c_str(), Anonymous::Change(storeId.storeId).c_str());
180 return IPC_STUB_INVALID_DATA_ERR;
181 }
182 int32_t status = SyncExt(appId, storeId, syncInfo);
183 if (!ITypesUtil::Marshal(reply, status)) {
184 ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status,
185 appId.appId.c_str(), Anonymous::Change(storeId.storeId).c_str());
186 return IPC_STUB_WRITE_PARCEL_ERR;
187 }
188 return ERR_NONE;
189 }
190
OnRegisterCallback(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)191 int32_t KVDBServiceStub::OnRegisterCallback(
192 const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
193 {
194 sptr<IRemoteObject> remoteObj;
195 if (!ITypesUtil::Unmarshal(data, remoteObj)) {
196 ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
197 Anonymous::Change(storeId.storeId).c_str());
198 return IPC_STUB_INVALID_DATA_ERR;
199 }
200 auto syncCallback = (remoteObj == nullptr) ? nullptr : iface_cast<IKvStoreSyncCallback>(remoteObj);
201 int32_t status = RegisterSyncCallback(appId, syncCallback);
202 if (!ITypesUtil::Marshal(reply, status)) {
203 ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
204 Anonymous::Change(storeId.storeId).c_str());
205 return IPC_STUB_WRITE_PARCEL_ERR;
206 }
207 return ERR_NONE;
208 }
209
OnUnregisterCallback(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)210 int32_t KVDBServiceStub::OnUnregisterCallback(
211 const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
212 {
213 int32_t status = UnregisterSyncCallback(appId);
214 if (!ITypesUtil::Marshal(reply, status)) {
215 ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
216 Anonymous::Change(storeId.storeId).c_str());
217 return IPC_STUB_WRITE_PARCEL_ERR;
218 }
219 return ERR_NONE;
220 }
221
OnSetSyncParam(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)222 int32_t KVDBServiceStub::OnSetSyncParam(
223 const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
224 {
225 KvSyncParam syncParam;
226 if (!ITypesUtil::Unmarshal(data, syncParam.allowedDelayMs)) {
227 ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
228 Anonymous::Change(storeId.storeId).c_str());
229 return IPC_STUB_INVALID_DATA_ERR;
230 }
231 int32_t status = SetSyncParam(appId, storeId, syncParam);
232 if (!ITypesUtil::Marshal(reply, status)) {
233 ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
234 Anonymous::Change(storeId.storeId).c_str());
235 return IPC_STUB_WRITE_PARCEL_ERR;
236 }
237 return ERR_NONE;
238 }
239
OnGetSyncParam(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)240 int32_t KVDBServiceStub::OnGetSyncParam(
241 const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
242 {
243 KvSyncParam syncParam;
244 int32_t status = GetSyncParam(appId, storeId, syncParam);
245 if (!ITypesUtil::Marshal(reply, status, syncParam.allowedDelayMs)) {
246 ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
247 Anonymous::Change(storeId.storeId).c_str());
248 return IPC_STUB_WRITE_PARCEL_ERR;
249 }
250 return ERR_NONE;
251 }
252
OnEnableCap(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)253 int32_t KVDBServiceStub::OnEnableCap(
254 const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
255 {
256 int32_t status = EnableCapability(appId, storeId);
257 if (!ITypesUtil::Marshal(reply, status)) {
258 ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
259 Anonymous::Change(storeId.storeId).c_str());
260 return IPC_STUB_WRITE_PARCEL_ERR;
261 }
262 return ERR_NONE;
263 }
264
OnDisableCap(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)265 int32_t KVDBServiceStub::OnDisableCap(
266 const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
267 {
268 int32_t status = DisableCapability(appId, storeId);
269 if (!ITypesUtil::Marshal(reply, status)) {
270 ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
271 Anonymous::Change(storeId.storeId).c_str());
272 return IPC_STUB_WRITE_PARCEL_ERR;
273 }
274 return ERR_NONE;
275 }
276
OnSetCapability(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)277 int32_t KVDBServiceStub::OnSetCapability(
278 const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
279 {
280 std::vector<std::string> local;
281 std::vector<std::string> remote;
282 if (!ITypesUtil::Unmarshal(data, local, remote)) {
283 ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
284 Anonymous::Change(storeId.storeId).c_str());
285 return IPC_STUB_INVALID_DATA_ERR;
286 }
287 int32_t status = SetCapability(appId, storeId, local, remote);
288 if (!ITypesUtil::Marshal(reply, status)) {
289 ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
290 Anonymous::Change(storeId.storeId).c_str());
291 return IPC_STUB_WRITE_PARCEL_ERR;
292 }
293 return ERR_NONE;
294 }
295
OnAddSubInfo(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)296 int32_t KVDBServiceStub::OnAddSubInfo(const AppId &appId, const StoreId &storeId, MessageParcel &data,
297 MessageParcel &reply)
298 {
299 SyncInfo syncInfo;
300 if (!ITypesUtil::Unmarshal(data, syncInfo.seqId, syncInfo.devices, syncInfo.query)) {
301 ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
302 Anonymous::Change(storeId.storeId).c_str());
303 return IPC_STUB_INVALID_DATA_ERR;
304 }
305 int32_t status = AddSubscribeInfo(appId, storeId, syncInfo);
306 if (!ITypesUtil::Marshal(reply, status)) {
307 ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
308 Anonymous::Change(storeId.storeId).c_str());
309 return IPC_STUB_WRITE_PARCEL_ERR;
310 }
311 return ERR_NONE;
312 }
313
OnRmvSubInfo(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)314 int32_t KVDBServiceStub::OnRmvSubInfo(const AppId &appId, const StoreId &storeId, MessageParcel &data,
315 MessageParcel &reply)
316 {
317 SyncInfo syncInfo;
318 if (!ITypesUtil::Unmarshal(data, syncInfo.seqId, syncInfo.devices, syncInfo.query)) {
319 ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
320 Anonymous::Change(storeId.storeId).c_str());
321 return IPC_STUB_INVALID_DATA_ERR;
322 }
323 int32_t status = RmvSubscribeInfo(appId, storeId, syncInfo);
324 if (!ITypesUtil::Marshal(reply, status)) {
325 ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
326 Anonymous::Change(storeId.storeId).c_str());
327 return IPC_STUB_WRITE_PARCEL_ERR;
328 }
329 return ERR_NONE;
330 }
331
OnSubscribe(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)332 int32_t KVDBServiceStub::OnSubscribe(
333 const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
334 {
335 sptr<IRemoteObject> remoteObj;
336 if (!ITypesUtil::Unmarshal(data, remoteObj)) {
337 ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
338 Anonymous::Change(storeId.storeId).c_str());
339 return IPC_STUB_INVALID_DATA_ERR;
340 }
341 auto observer = (remoteObj == nullptr) ? nullptr : iface_cast<IKvStoreObserver>(remoteObj);
342 int32_t status = Subscribe(appId, storeId, observer);
343 if (!ITypesUtil::Marshal(reply, status)) {
344 ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
345 Anonymous::Change(storeId.storeId).c_str());
346 return IPC_STUB_WRITE_PARCEL_ERR;
347 }
348 return ERR_NONE;
349 }
350
OnUnsubscribe(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)351 int32_t KVDBServiceStub::OnUnsubscribe(
352 const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
353 {
354 sptr<IRemoteObject> remoteObj;
355 if (!ITypesUtil::Unmarshal(data, remoteObj)) {
356 ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
357 Anonymous::Change(storeId.storeId).c_str());
358 return IPC_STUB_INVALID_DATA_ERR;
359 }
360 auto observer = (remoteObj == nullptr) ? nullptr : iface_cast<IKvStoreObserver>(remoteObj);
361 int32_t status = Unsubscribe(appId, storeId, observer);
362 if (!ITypesUtil::Marshal(reply, status)) {
363 ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
364 Anonymous::Change(storeId.storeId).c_str());
365 return IPC_STUB_WRITE_PARCEL_ERR;
366 }
367 return ERR_NONE;
368 }
369
OnGetBackupPassword(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)370 int32_t KVDBServiceStub::OnGetBackupPassword(
371 const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
372 {
373 std::vector<uint8_t> password;
374 int32_t status = GetBackupPassword(appId, storeId, password);
375 if (!ITypesUtil::Marshal(reply, status, password)) {
376 ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
377 Anonymous::Change(storeId.storeId).c_str());
378 password.assign(password.size(), 0);
379 return IPC_STUB_WRITE_PARCEL_ERR;
380 }
381 password.assign(password.size(), 0);
382 return ERR_NONE;
383 }
384 } // namespace OHOS::DistributedKv
385