• 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 #define LOG_TAG "CloudServiceStub"
16 #include "cloud_service_stub.h"
17 
18 #include "ipc_skeleton.h"
19 #include "itypes_util.h"
20 #include "log_print.h"
21 #include "permission/permission_validator.h"
22 #include "rdb_types.h"
23 #include "utils/anonymous.h"
24 #include "tokenid_kit.h"
25 namespace OHOS::CloudData {
26 using namespace DistributedData;
27 using namespace OHOS::Security::AccessToken;
28 const CloudServiceStub::Handler CloudServiceStub::HANDLERS[TRANS_BUTT] = {
29     &CloudServiceStub::OnEnableCloud,
30     &CloudServiceStub::OnDisableCloud,
31     &CloudServiceStub::OnChangeAppSwitch,
32     &CloudServiceStub::OnClean,
33     &CloudServiceStub::OnNotifyDataChange,
34     &CloudServiceStub::OnNotifyChange,
35     &CloudServiceStub::OnAllocResourceAndShare,
36     &CloudServiceStub::OnShare,
37     &CloudServiceStub::OnUnshare,
38     &CloudServiceStub::OnExit,
39     &CloudServiceStub::OnChangePrivilege,
40     &CloudServiceStub::OnQuery,
41     &CloudServiceStub::OnQueryByInvitation,
42     &CloudServiceStub::OnConfirmInvitation,
43     &CloudServiceStub::OnChangeConfirmation,
44 };
45 
OnRemoteRequest(uint32_t code,OHOS::MessageParcel & data,OHOS::MessageParcel & reply)46 int CloudServiceStub::OnRemoteRequest(uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply)
47 {
48     ZLOGI("code:%{public}u callingPid:%{public}u", code, IPCSkeleton::GetCallingPid());
49     std::u16string local = CloudServiceStub::GetDescriptor();
50     std::u16string remote = data.ReadInterfaceToken();
51     if (local != remote) {
52         ZLOGE("local is not equal to remote");
53         return -1;
54     }
55 
56     if (TRANS_HEAD > code || code >= TRANS_BUTT || HANDLERS[code] == nullptr) {
57         ZLOGE("not support code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT);
58         return -1;
59     }
60 
61     if (!TokenIdKit::IsSystemAppByFullTokenID(IPCSkeleton::GetCallingFullTokenID())) {
62         ZLOGE("permission denied! code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT);
63         auto result = static_cast<int32_t>(PERMISSION_DENIED);
64         return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
65     }
66 
67     if (code >= TRANS_HEAD && code < TRANS_ALLOC_RESOURCE_AND_SHARE) {
68         auto permit =
69             DistributedKv::PermissionValidator::GetInstance().IsCloudConfigPermit(IPCSkeleton::GetCallingTokenID());
70         if (!permit) {
71             ZLOGE("cloud config permission denied! code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT);
72             auto result = static_cast<int32_t>(CLOUD_CONFIG_PERMISSION_DENIED);
73             return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
74         }
75     }
76 
77     std::string id;
78     if (!ITypesUtil::Unmarshal(data, id)) {
79         ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str());
80         return IPC_STUB_INVALID_DATA_ERR;
81     }
82     return (this->*HANDLERS[code])(id, data, reply);
83 }
84 
OnEnableCloud(const std::string & id,MessageParcel & data,MessageParcel & reply)85 int32_t CloudServiceStub::OnEnableCloud(const std::string &id, MessageParcel &data, MessageParcel &reply)
86 {
87     std::map<std::string, int32_t> switches;
88     if (!ITypesUtil::Unmarshal(data, switches)) {
89         ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str());
90         return IPC_STUB_INVALID_DATA_ERR;
91     }
92     auto result = EnableCloud(id, switches);
93     return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
94 }
95 
OnDisableCloud(const std::string & id,MessageParcel & data,MessageParcel & reply)96 int32_t CloudServiceStub::OnDisableCloud(const std::string &id, MessageParcel &data, MessageParcel &reply)
97 {
98     auto result = DisableCloud(id);
99     return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
100 }
101 
OnChangeAppSwitch(const std::string & id,MessageParcel & data,MessageParcel & reply)102 int32_t CloudServiceStub::OnChangeAppSwitch(const std::string &id, MessageParcel &data, MessageParcel &reply)
103 {
104     std::string bundleName;
105     int32_t appSwitch = SWITCH_OFF;
106     if (!ITypesUtil::Unmarshal(data, bundleName, appSwitch)) {
107         ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str());
108         return IPC_STUB_INVALID_DATA_ERR;
109     }
110     auto result = ChangeAppSwitch(id, bundleName, appSwitch);
111     return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
112 }
113 
OnClean(const std::string & id,MessageParcel & data,MessageParcel & reply)114 int32_t CloudServiceStub::OnClean(const std::string &id, MessageParcel &data, MessageParcel &reply)
115 {
116     std::map<std::string, int32_t> actions;
117     if (!ITypesUtil::Unmarshal(data, actions)) {
118         ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str());
119         return IPC_STUB_INVALID_DATA_ERR;
120     }
121     auto result = Clean(id, actions);
122     return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
123 }
124 
OnNotifyDataChange(const std::string & id,MessageParcel & data,MessageParcel & reply)125 int32_t CloudServiceStub::OnNotifyDataChange(const std::string &id, MessageParcel &data, MessageParcel &reply)
126 {
127     std::string bundleName;
128     if (!ITypesUtil::Unmarshal(data, bundleName)) {
129         ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str());
130         return IPC_STUB_INVALID_DATA_ERR;
131     }
132     auto result = NotifyDataChange(id, bundleName);
133     return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
134 }
135 
OnAllocResourceAndShare(const std::string & storeId,MessageParcel & data,MessageParcel & reply)136 int32_t CloudServiceStub::OnAllocResourceAndShare(const std::string& storeId, MessageParcel& data,
137     MessageParcel& reply)
138 {
139     DistributedRdb::PredicatesMemo predicatesMemo;
140     std::vector<std::string> columns;
141     std::vector<Participant> participants;
142     if (!ITypesUtil::Unmarshal(data, predicatesMemo, columns, participants)) {
143         ZLOGE("Unmarshal storeId:%{public}s", Anonymous::Change(storeId).c_str());
144         return IPC_STUB_INVALID_DATA_ERR;
145     }
146     auto [status, resultSet] = AllocResourceAndShare(storeId, predicatesMemo, columns, participants);
147     return ITypesUtil::Marshal(reply, status, resultSet) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
148 }
149 
OnNotifyChange(const std::string & id,MessageParcel & data,MessageParcel & reply)150 int32_t CloudServiceStub::OnNotifyChange(const std::string &id, MessageParcel &data, MessageParcel &reply)
151 {
152     std::string extraData;
153     int32_t userId;
154     if (!ITypesUtil::Unmarshal(data, extraData, userId)) {
155         ZLOGE("Unmarshal id:%{public}s", Anonymous::Change(id).c_str());
156         return IPC_STUB_INVALID_DATA_ERR;
157     }
158     auto result = NotifyDataChange(id, extraData, userId);
159     return ITypesUtil::Marshal(reply, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
160 }
161 
OnShare(const std::string & sharingRes,MessageParcel & data,MessageParcel & reply)162 int32_t CloudServiceStub::OnShare(const std::string &sharingRes, MessageParcel &data, MessageParcel &reply)
163 {
164     Participants participants;
165     if (!ITypesUtil::Unmarshal(data, participants)) {
166         ZLOGE("Unmarshal sharingRes:%{public}s", Anonymous::Change(sharingRes).c_str());
167         return IPC_STUB_INVALID_DATA_ERR;
168     }
169     Results results;
170     auto status = Share(sharingRes, participants, results);
171     return ITypesUtil::Marshal(reply, status, results) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
172 }
173 
OnUnshare(const std::string & sharingRes,MessageParcel & data,MessageParcel & reply)174 int32_t CloudServiceStub::OnUnshare(const std::string &sharingRes, MessageParcel &data, MessageParcel &reply)
175 {
176     Participants participants;
177     if (!ITypesUtil::Unmarshal(data, participants)) {
178         ZLOGE("Unmarshal sharingRes:%{public}s", Anonymous::Change(sharingRes).c_str());
179         return IPC_STUB_INVALID_DATA_ERR;
180     }
181     Results results;
182     auto status = Unshare(sharingRes, participants, results);
183     return ITypesUtil::Marshal(reply, status, results) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
184 }
185 
OnExit(const std::string & sharingRes,MessageParcel & data,MessageParcel & reply)186 int32_t CloudServiceStub::OnExit(const std::string &sharingRes, MessageParcel &data, MessageParcel &reply)
187 {
188     std::pair<int32_t, std::string> result;
189     auto status = Exit(sharingRes, result);
190     return ITypesUtil::Marshal(reply, status, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
191 }
192 
OnChangePrivilege(const std::string & sharingRes,MessageParcel & data,MessageParcel & reply)193 int32_t CloudServiceStub::OnChangePrivilege(const std::string &sharingRes, MessageParcel &data, MessageParcel &reply)
194 {
195     Participants participants;
196     if (!ITypesUtil::Unmarshal(data, participants)) {
197         ZLOGE("Unmarshal sharingRes:%{public}s", Anonymous::Change(sharingRes).c_str());
198         return IPC_STUB_INVALID_DATA_ERR;
199     }
200     Results results;
201     auto status = ChangePrivilege(sharingRes, participants, results);
202     return ITypesUtil::Marshal(reply, status, results) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
203 }
204 
OnQuery(const std::string & sharingRes,MessageParcel & data,MessageParcel & reply)205 int32_t CloudServiceStub::OnQuery(const std::string &sharingRes, MessageParcel &data, MessageParcel &reply)
206 {
207     QueryResults results;
208     auto status = Query(sharingRes, results);
209     return ITypesUtil::Marshal(reply, status, results) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
210 }
211 
OnQueryByInvitation(const std::string & invitation,MessageParcel & data,MessageParcel & reply)212 int32_t CloudServiceStub::OnQueryByInvitation(
213     const std::string &invitation, MessageParcel &data, MessageParcel &reply)
214 {
215     QueryResults results;
216     auto status = QueryByInvitation(invitation, results);
217     return ITypesUtil::Marshal(reply, status, results) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
218 }
219 
OnConfirmInvitation(const std::string & invitation,MessageParcel & data,MessageParcel & reply)220 int32_t CloudServiceStub::OnConfirmInvitation(
221     const std::string &invitation, MessageParcel &data, MessageParcel &reply)
222 {
223     int32_t confirmation;
224     if (!ITypesUtil::Unmarshal(data, confirmation)) {
225         ZLOGE("Unmarshal invitation:%{public}s", Anonymous::Change(invitation).c_str());
226         return IPC_STUB_INVALID_DATA_ERR;
227     }
228     std::tuple<int32_t, std::string, std::string> result;
229     auto status = ConfirmInvitation(invitation, confirmation, result);
230     return ITypesUtil::Marshal(reply, status, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
231 }
232 
OnChangeConfirmation(const std::string & sharingRes,MessageParcel & data,MessageParcel & reply)233 int32_t CloudServiceStub::OnChangeConfirmation(
234     const std::string &sharingRes, MessageParcel &data, MessageParcel &reply)
235 {
236     int32_t confirmation;
237     if (!ITypesUtil::Unmarshal(data, confirmation)) {
238         ZLOGE("Unmarshal sharingRes:%{public}s", Anonymous::Change(sharingRes).c_str());
239         return IPC_STUB_INVALID_DATA_ERR;
240     }
241     std::pair<int32_t, std::string> result;
242     auto status = ChangeConfirmation(sharingRes, confirmation, result);
243     return ITypesUtil::Marshal(reply, status, result) ? ERR_NONE : IPC_STUB_WRITE_PARCEL_ERR;
244 }
245 } // namespace OHOS::CloudData
246