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