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 "UdmfServiceStub"
16
17 #include "udmf_service_stub.h"
18
19 #include <vector>
20
21 #include "accesstoken_kit.h"
22 #include "ipc_skeleton.h"
23 #include "log_print.h"
24 #include "udmf_types_util.h"
25 #include "unified_data.h"
26 #include "unified_meta.h"
27
28 namespace OHOS {
29 namespace UDMF {
30 constexpr UdmfServiceStub::Handler
31 UdmfServiceStub::HANDLERS[static_cast<uint32_t>(UdmfServiceInterfaceCode::CODE_BUTT)];
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)32 int UdmfServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
33 {
34 ZLOGI("start##code = %{public}u", code);
35 std::u16string myDescripter = UdmfServiceStub::GetDescriptor();
36 std::u16string remoteDescripter = data.ReadInterfaceToken();
37 if (myDescripter != remoteDescripter) {
38 ZLOGE("end##descriptor checked fail");
39 return -1;
40 }
41 if (static_cast<uint32_t>(UdmfServiceInterfaceCode::CODE_HEAD) > code ||
42 code >= static_cast<uint32_t>(UdmfServiceInterfaceCode::CODE_BUTT)) {
43 return -1;
44 }
45 return (this->*HANDLERS[code])(data, reply);
46 }
47
OnSetData(MessageParcel & data,MessageParcel & reply)48 int32_t UdmfServiceStub::OnSetData(MessageParcel &data, MessageParcel &reply)
49 {
50 ZLOGI("start");
51 CustomOption customOption;
52 UnifiedData unifiedData;
53 if (!ITypesUtil::Unmarshal(data, customOption, unifiedData)) {
54 ZLOGE("Unmarshal customOption or unifiedData failed!");
55 return E_READ_PARCEL_ERROR;
56 }
57 if (unifiedData.IsEmpty()) {
58 ZLOGE("Empty data without any record!");
59 return E_INVALID_PARAMETERS;
60 }
61 if (unifiedData.GetSize() > UdmfService::MAX_DATA_SIZE) {
62 ZLOGE("Exceeded data limit!");
63 return E_INVALID_PARAMETERS;
64 }
65 for (const auto &record : unifiedData.GetRecords()) {
66 if (record == nullptr) {
67 ZLOGE("record is nullptr!");
68 return E_INVALID_PARAMETERS;
69 }
70 if (record->GetSize() > UdmfService::MAX_RECORD_SIZE) {
71 ZLOGE("Exceeded record limit!");
72 return E_INVALID_PARAMETERS;
73 }
74 }
75 uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
76 customOption.tokenId = token;
77 std::string key;
78 int32_t status = SetData(customOption, unifiedData, key);
79 if (!ITypesUtil::Marshal(reply, status, key)) {
80 ZLOGE("Marshal status or key failed, status: %{public}d, key: %{public}s", status, key.c_str());
81 return E_WRITE_PARCEL_ERROR;
82 }
83 return E_OK;
84 }
85
OnGetData(MessageParcel & data,MessageParcel & reply)86 int32_t UdmfServiceStub::OnGetData(MessageParcel &data, MessageParcel &reply)
87 {
88 ZLOGI("start");
89 QueryOption query;
90 if (!ITypesUtil::Unmarshal(data, query)) {
91 ZLOGE("Unmarshal queryOption failed!");
92 return E_READ_PARCEL_ERROR;
93 }
94 uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
95 query.tokenId = token;
96 UnifiedData unifiedData;
97 int32_t status = GetData(query, unifiedData);
98 if (!ITypesUtil::Marshal(reply, status, unifiedData)) {
99 ZLOGE("Marshal status or unifiedData failed, status: %{public}d", status);
100 return E_WRITE_PARCEL_ERROR;
101 }
102 return E_OK;
103 }
104
OnGetBatchData(MessageParcel & data,MessageParcel & reply)105 int32_t UdmfServiceStub::OnGetBatchData(MessageParcel &data, MessageParcel &reply)
106 {
107 ZLOGI("start");
108 QueryOption query;
109 if (!ITypesUtil::Unmarshal(data, query)) {
110 ZLOGE("Unmarshal queryOption failed!");
111 return E_READ_PARCEL_ERROR;
112 }
113 uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
114 query.tokenId = token;
115 std::vector<UnifiedData> unifiedDataSet;
116 int32_t status = GetBatchData(query, unifiedDataSet);
117 if (!ITypesUtil::Marshal(reply, status, unifiedDataSet)) {
118 ZLOGE("Marshal status or unifiedDataSet failed, status: %{public}d", status);
119 return E_WRITE_PARCEL_ERROR;
120 }
121 return E_OK;
122 }
123
OnUpdateData(MessageParcel & data,MessageParcel & reply)124 int32_t UdmfServiceStub::OnUpdateData(MessageParcel &data, MessageParcel &reply)
125 {
126 ZLOGI("start");
127 QueryOption query;
128 UnifiedData unifiedData;
129 if (!ITypesUtil::Unmarshal(data, query, unifiedData)) {
130 ZLOGE("Unmarshal queryOption or unifiedData failed!");
131 return E_READ_PARCEL_ERROR;
132 }
133 if (unifiedData.IsEmpty()) {
134 ZLOGE("Empty data without any record!");
135 return E_INVALID_PARAMETERS;
136 }
137 if (unifiedData.GetSize() > UdmfService::MAX_DATA_SIZE) {
138 ZLOGE("Exceeded data limit!");
139 return E_INVALID_PARAMETERS;
140 }
141 for (const auto &record : unifiedData.GetRecords()) {
142 if (record->GetSize() > UdmfService::MAX_RECORD_SIZE) {
143 ZLOGE("Exceeded record limit!");
144 return E_INVALID_PARAMETERS;
145 }
146 }
147 uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
148 query.tokenId = token;
149 int32_t status = UpdateData(query, unifiedData);
150 if (!ITypesUtil::Marshal(reply, status)) {
151 ZLOGE("Marshal status failed, status: %{public}d", status);
152 return E_WRITE_PARCEL_ERROR;
153 }
154 return E_OK;
155 }
156
OnDeleteData(MessageParcel & data,MessageParcel & reply)157 int32_t UdmfServiceStub::OnDeleteData(MessageParcel &data, MessageParcel &reply)
158 {
159 ZLOGI("start");
160 QueryOption query;
161 if (!ITypesUtil::Unmarshal(data, query)) {
162 ZLOGE("Unmarshal queryOption failed!");
163 return E_READ_PARCEL_ERROR;
164 }
165 uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
166 query.tokenId = token;
167 std::vector<UnifiedData> unifiedDataSet;
168 int32_t status = DeleteData(query, unifiedDataSet);
169 if (!ITypesUtil::Marshal(reply, status, unifiedDataSet)) {
170 ZLOGE("Marshal status or unifiedDataSet failed, status: %{public}d", status);
171 return E_WRITE_PARCEL_ERROR;
172 }
173 return E_OK;
174 }
175
OnGetSummary(MessageParcel & data,MessageParcel & reply)176 int32_t UdmfServiceStub::OnGetSummary(MessageParcel &data, MessageParcel &reply)
177 {
178 ZLOGI("start");
179 QueryOption query;
180 if (!ITypesUtil::Unmarshal(data, query)) {
181 ZLOGE("Unmarshal query failed");
182 return E_READ_PARCEL_ERROR;
183 }
184 uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
185 query.tokenId = token;
186 Summary summary;
187 int32_t status = GetSummary(query, summary);
188 if (!ITypesUtil::Marshal(reply, status, summary)) {
189 ZLOGE("Marshal summary failed, key: %{public}s", query.key.c_str());
190 return E_WRITE_PARCEL_ERROR;
191 }
192 return E_OK;
193 }
194
OnAddPrivilege(MessageParcel & data,MessageParcel & reply)195 int32_t UdmfServiceStub::OnAddPrivilege(MessageParcel &data, MessageParcel &reply)
196 {
197 ZLOGI("start");
198 QueryOption query;
199 Privilege privilege;
200 if (!ITypesUtil::Unmarshal(data, query, privilege)) {
201 ZLOGE("Unmarshal query and privilege failed");
202 return E_READ_PARCEL_ERROR;
203 }
204 uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
205 query.tokenId = token;
206 int32_t status = AddPrivilege(query, privilege);
207 if (!ITypesUtil::Marshal(reply, status)) {
208 ZLOGE("Marshal status failed, key: %{public}s", query.key.c_str());
209 return E_WRITE_PARCEL_ERROR;
210 }
211 return E_OK;
212 }
213
OnSync(MessageParcel & data,MessageParcel & reply)214 int32_t UdmfServiceStub::OnSync(MessageParcel &data, MessageParcel &reply)
215 {
216 ZLOGI("start");
217 QueryOption query;
218 std::vector<std::string> devices;
219 if (!ITypesUtil::Unmarshal(data, query, devices)) {
220 ZLOGE("Unmarshal query and devices failed");
221 return E_READ_PARCEL_ERROR;
222 }
223 uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
224 query.tokenId = token;
225 int32_t status = Sync(query, devices);
226 if (!ITypesUtil::Marshal(reply, status)) {
227 ZLOGE("Marshal status failed, key: %{public}s", query.key.c_str());
228 return E_WRITE_PARCEL_ERROR;
229 }
230 return E_OK;
231 }
232
233 /*
234 * Check whether the caller has the permission to access data.
235 */
VerifyPermission(const std::string & permission)236 bool UdmfServiceStub::VerifyPermission(const std::string &permission)
237 {
238 #ifdef UDMF_PERMISSION_ENABLED
239 uint32_t tokenId = IPCSkeleton::GetCallingTokenID();
240 int32_t result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, permission);
241 return result == Security::AccessToken::TypePermissionState::PERMISSION_GRANTED;
242 #else
243 return true;
244 #endif // UDMF_PERMISSION_ENABLED
245 }
246 } // namespace UDMF
247 } // namespace OHOS