1 /*
2 * Copyright (c) 2023-2025 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
16 #include "trust_profile_manager.h"
17 #include "cJSON.h"
18 #include "subscribe_profile_manager.h"
19 #include "distributed_device_profile_log.h"
20 #include "rdb_adapter.h"
21 #include "profile_utils.h"
22 #include "device_manager.h"
23 #include "distributed_device_profile_constants.h"
24 #include "distributed_device_profile_errors.h"
25 #include "accesser.h"
26 #include "accessee.h"
27
28 namespace OHOS {
29 namespace DistributedDeviceProfile {
30 IMPLEMENT_SINGLE_INSTANCE(TrustProfileManager);
31 namespace {
32 const std::string TAG = "TrustProfileManager";
33 const std::string NAME = "name";
34 }
35
Init()36 int32_t TrustProfileManager::Init()
37 {
38 {
39 std::lock_guard<std::mutex> lock(rdbMutex_);
40 rdbStore_ = std::make_shared<RdbAdapter>();
41 if (rdbStore_ == nullptr) {
42 HILOGE("rdbStore_ is nullptr");
43 return DP_INIT_DB_FAILED;
44 }
45 int32_t ret = rdbStore_->Init();
46 if (ret != DP_SUCCESS) {
47 HILOGE("rdbStore_ Init failed");
48 return DP_INIT_DB_FAILED;
49 }
50 }
51 this->CreateTable();
52 this->CreateUniqueIndex();
53 if (!IsAceeCreIdExistToAceeTable() && AddAceeCreIdColumnToAceeTable() != DP_SUCCESS) {
54 HILOGE("acee table add aceeCreId failed");
55 return DP_CREATE_TABLE_FAIL;
56 }
57 HILOGI("end!");
58 return DP_SUCCESS;
59 }
60
UnInit()61 int32_t TrustProfileManager::UnInit()
62 {
63 {
64 std::lock_guard<std::mutex> lock(rdbMutex_);
65 if (rdbStore_ == nullptr) {
66 HILOGE("rdbStore_ is nullptr");
67 return DP_GET_RDBSTORE_FAIL;
68 }
69 int32_t ret = rdbStore_->UnInit();
70 if (ret != DP_SUCCESS) {
71 HILOGE("rdbStore_ Uninit failed");
72 return DP_UNINIT_FAIL;
73 }
74 rdbStore_ = nullptr;
75 }
76 HILOGI("end!");
77 return DP_SUCCESS;
78 }
79
PutTrustDeviceProfile(const TrustDeviceProfile & profile)80 int32_t TrustProfileManager::PutTrustDeviceProfile(const TrustDeviceProfile& profile)
81 {
82 ValuesBucket values;
83 ProfileUtils::TrustDeviceProfileToEntries(profile, values);
84 int64_t rowId = ROWID_INIT;
85 int32_t ret = RET_INIT;
86 {
87 std::lock_guard<std::mutex> lock(rdbMutex_);
88 if (rdbStore_ == nullptr) {
89 HILOGE("rdbStore_ is nullptr");
90 return DP_GET_RDBSTORE_FAIL;
91 }
92 ret = rdbStore_->Put(rowId, TRUST_DEVICE_TABLE, values);
93 if (ret != DP_SUCCESS) {
94 HILOGE("trustdevice_table insert failed");
95 return DP_PUT_TRUST_DEVICE_PROFILE_FAIL;
96 }
97 }
98 HILOGI("end!");
99 return DP_SUCCESS;
100 }
101
PutAccessControlProfile(const AccessControlProfile & profile)102 int32_t TrustProfileManager::PutAccessControlProfile(const AccessControlProfile& profile)
103 {
104 AccessControlProfile accessControlProfile(profile);
105 bool isExists = false;
106 {
107 std::lock_guard<std::mutex> lock(aclMutex_);
108 CheckDeviceIdAndUserIdExists(profile, isExists);
109 int32_t ret = this->SetAccessControlProfileId(accessControlProfile);
110 if (ret != DP_SUCCESS) {
111 HILOGE("SetAccessControlProfileId failed");
112 return ret;
113 }
114 ret = this->PutAccesserProfile(accessControlProfile);
115 if (ret != DP_SUCCESS) {
116 HILOGE("PutAccesserProfile failed");
117 return ret;
118 }
119 ret = this->PutAccesseeProfile(accessControlProfile);
120 if (ret != DP_SUCCESS) {
121 HILOGE("PutAccesseeProfile failed");
122 return ret;
123 }
124 if (IsAclExists(accessControlProfile) == DP_DATA_EXISTS) {
125 HILOGE("acl is exists");
126 return DP_SUCCESS;
127 }
128 ValuesBucket values;
129 ProfileUtils::AccessControlProfileToEntries(accessControlProfile, values);
130 if (rdbStore_ == nullptr) {
131 HILOGE("rdbStore_ is nullptr");
132 return DP_GET_RDBSTORE_FAIL;
133 }
134 int64_t rowId = ROWID_INIT;
135 if (rdbStore_->Put(rowId, ACCESS_CONTROL_TABLE, values) != DP_SUCCESS) {
136 HILOGE("accesscontrol_table insert failed");
137 return DP_PUT_ACL_PROFILE_FAIL;
138 }
139 HILOGI("PutAclProfile : %{public}s", accessControlProfile.dump().c_str());
140 }
141 int32_t putRet = this->PutAclCheck(accessControlProfile, isExists);
142 if (putRet != DP_SUCCESS) {
143 HILOGE("PutAclCheck failed");
144 return putRet;
145 }
146 DistributedHardware::DeviceManager::GetInstance().DpAclAdd(accessControlProfile.GetAccessControlId(),
147 accessControlProfile.GetTrustDeviceId(), accessControlProfile.GetBindType());
148 HILOGI("end!");
149 return DP_SUCCESS;
150 }
151
UpdateTrustDeviceProfile(const TrustDeviceProfile & profile)152 int32_t TrustProfileManager::UpdateTrustDeviceProfile(const TrustDeviceProfile& profile)
153 {
154 std::string deviceId = profile.GetDeviceId();
155 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_TRUST_DEVICE_TABLE_WHERE_DEVICEID,
156 std::vector<ValueObject>{ ValueObject(deviceId) });
157 if (resultSet == nullptr) {
158 HILOGE("resultSet is nullptr");
159 return DP_GET_RESULTSET_FAIL;
160 }
161 int32_t rowCount = ROWCOUNT_INIT;
162 resultSet->GetRowCount(rowCount);
163 if (rowCount == 0) {
164 HILOGE("deviceId not find");
165 resultSet->Close();
166 return DP_NOT_FIND_DATA;
167 }
168 int32_t ret = resultSet->GoToFirstRow();
169 if (ret != DP_SUCCESS) {
170 HILOGE("deviceId not find");
171 resultSet->Close();
172 return DP_NOT_FIND_DATA;
173 }
174 TrustDeviceProfile oldProfile;
175 ProfileUtils::ConvertToTrustDeviceProfile(resultSet, oldProfile);
176 resultSet->Close();
177 ValuesBucket values;
178 ProfileUtils::TrustDeviceProfileToEntries(profile, values);
179 int32_t changeRowCnt = CHANGEROWCNT_INIT;
180 {
181 std::lock_guard<std::mutex> lock(rdbMutex_);
182 if (rdbStore_ == nullptr) {
183 HILOGE("rdbStore_ is nullptr");
184 return DP_GET_RDBSTORE_FAIL;
185 }
186 ret = rdbStore_->Update(changeRowCnt, TRUST_DEVICE_TABLE, values, DEVICEID_EQUAL_CONDITION,
187 std::vector<ValueObject>{ ValueObject(profile.GetDeviceId()) });
188 if (ret != DP_SUCCESS) {
189 HILOGE("Update trustdevice_table failed");
190 return DP_UPDATE_TRUST_DEVICE_PROFILE_FAIL;
191 }
192 }
193 this->UpdateTrustDeviceProfileNotify(oldProfile, profile);
194 HILOGI("end!");
195 return DP_SUCCESS;
196 }
197
UpdateAccessControlProfile(const AccessControlProfile & profile)198 int32_t TrustProfileManager::UpdateAccessControlProfile(const AccessControlProfile& profile)
199 {
200 AccessControlProfile oldProfile;
201 {
202 std::lock_guard<std::mutex> lock(aclMutex_);
203 int32_t ret = this->UpdateAclCheck(profile, oldProfile);
204 if (ret != DP_SUCCESS) {
205 HILOGE("UpdateAclCheck faild");
206 return ret;
207 }
208 this->UpdateAccesserProfile(profile);
209 this->UpdateAccesseeProfile(profile);
210 ValuesBucket values;
211 ProfileUtils::AccessControlProfileToEntries(profile, values);
212 int32_t changeRowCnt = CHANGEROWCNT_INIT;
213 if (rdbStore_ == nullptr) {
214 HILOGE("rdbStore_ is nullptr");
215 return DP_GET_RDBSTORE_FAIL;
216 }
217 ret = rdbStore_->Update(changeRowCnt, ACCESS_CONTROL_TABLE, values, ACCESSCONTROLID_EQUAL_CONDITION,
218 std::vector<ValueObject>{ ValueObject(profile.GetAccessControlId()) });
219 if (ret != DP_SUCCESS) {
220 HILOGE("update accesscontrol_table failed");
221 return DP_UPDATE_ACL_PROFILE_FAIL;
222 }
223 }
224 this->NotifyCheck(profile, oldProfile);
225 HILOGI("UpdateAclProfile : %{public}s", profile.dump().c_str());
226 int32_t status = STATUS_INIT;
227 this->GetResultStatus(profile.GetTrustDeviceId(), status);
228 TrustDeviceProfile trustProfile;
229 ProfileUtils::ConvertToTrustDeviceProfile(profile, trustProfile);
230 trustProfile.SetStatus(status);
231 int32_t updateRet = this->UpdateTrustDeviceProfile(trustProfile);
232 if (updateRet != DP_SUCCESS) {
233 HILOGE("UpdateTrustDeviceProfile failed");
234 return DP_UPDATE_TRUST_DEVICE_PROFILE_FAIL;
235 }
236 HILOGI("end!");
237 return DP_SUCCESS;
238 }
239
GetTrustDeviceProfile(const std::string & deviceId,TrustDeviceProfile & profile)240 int32_t TrustProfileManager::GetTrustDeviceProfile(const std::string& deviceId, TrustDeviceProfile& profile)
241 {
242 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_TRUST_DEVICE_TABLE_WHERE_DEVICEID,
243 std::vector<ValueObject>{ ValueObject(deviceId) });
244 if (resultSet == nullptr) {
245 HILOGE("resultSet is nullptr");
246 return DP_GET_RESULTSET_FAIL;
247 }
248 int32_t rowCount = ROWCOUNT_INIT;
249 resultSet->GetRowCount(rowCount);
250 if (rowCount == 0) {
251 HILOGE("deviceId not find");
252 resultSet->Close();
253 return DP_NOT_FIND_DATA;
254 }
255 int32_t ret = resultSet->GoToFirstRow();
256 if (ret != DP_SUCCESS) {
257 HILOGE("not find trust device data");
258 resultSet->Close();
259 return DP_NOT_FIND_DATA;
260 }
261 ProfileUtils::ConvertToTrustDeviceProfile(resultSet, profile);
262 resultSet->Close();
263 HILOGI("end!");
264 return DP_SUCCESS;
265 }
266
GetAllTrustDeviceProfile(std::vector<TrustDeviceProfile> & profile)267 int32_t TrustProfileManager::GetAllTrustDeviceProfile(std::vector<TrustDeviceProfile>& profile)
268 {
269 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_TRUST_DEVICE_TABLE, std::vector<ValueObject> {});
270 if (resultSet == nullptr) {
271 HILOGE("resultSet is nullptr");
272 return DP_GET_RESULTSET_FAIL;
273 }
274 int32_t rowCount = ROWCOUNT_INIT;
275 resultSet->GetRowCount(rowCount);
276 if (rowCount == 0) {
277 HILOGE("trustdevice_table no data");
278 resultSet->Close();
279 return DP_NOT_FIND_DATA;
280 }
281 while (resultSet->GoToNextRow() == DP_SUCCESS) {
282 TrustDeviceProfile trustProfile;
283 ProfileUtils::ConvertToTrustDeviceProfile(resultSet, trustProfile);
284 profile.push_back(trustProfile);
285 }
286 resultSet->Close();
287 if (profile.empty()) {
288 return DP_NOT_FIND_DATA;
289 }
290 HILOGI("end!");
291 return DP_SUCCESS;
292 }
293
GetAccessControlProfile(int32_t userId,const std::string & bundleName,int32_t bindType,int32_t status,std::vector<AccessControlProfile> & profile)294 int32_t TrustProfileManager::GetAccessControlProfile(int32_t userId, const std::string& bundleName,
295 int32_t bindType, int32_t status, std::vector<AccessControlProfile>& profile)
296 {
297 if (bundleName.size() > MAX_STRING_LEN) {
298 HILOGE("bundleName is invalid");
299 return DP_INVALID_PARAMS;
300 }
301 HILOGI("Params, userId : %{public}s, bundleName : %{public}s, bindtype : %{public}d, status : %{public}d",
302 ProfileUtils::GetAnonyString(std::to_string(userId)).c_str(), bundleName.c_str(), bindType, status);
303 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_ACCESS_CONTROL_TABLE_WHERE_BINDTYPE_AND_STATUS,
304 std::vector<ValueObject>{ ValueObject(bindType), ValueObject(status) });
305 if (resultSet == nullptr) {
306 HILOGE("resultSet is nullptr");
307 return DP_GET_RESULTSET_FAIL;
308 }
309 int32_t rowCount = ROWCOUNT_INIT;
310 resultSet->GetRowCount(rowCount);
311 if (rowCount == 0) {
312 HILOGE("bindType and status not find");
313 resultSet->Close();
314 return DP_NOT_FIND_DATA;
315 }
316 int32_t ret = this->GetAclProfileByUserIdAndBundleName(resultSet, userId, bundleName, profile);
317 resultSet->Close();
318 if (ret != DP_SUCCESS) {
319 HILOGE("GetAclProfileByUserIdAndBundleName faild");
320 return DP_NOT_FIND_DATA;
321 }
322 RemoveLnnAcl(profile);
323 if (profile.empty()) {
324 HILOGE("by userId bundleName bindType status not find data");
325 return DP_NOT_FIND_DATA;
326 }
327 HILOGI("end!");
328 return DP_SUCCESS;
329 }
330
GetAccessControlProfile(int32_t userId,const std::string & bundleName,const std::string & trustDeviceId,int32_t status,std::vector<AccessControlProfile> & profile)331 int32_t TrustProfileManager::GetAccessControlProfile(int32_t userId, const std::string& bundleName,
332 const std::string& trustDeviceId, int32_t status, std::vector<AccessControlProfile>& profile)
333 {
334 if (bundleName.size() > MAX_STRING_LEN || trustDeviceId.size() > MAX_STRING_LEN) {
335 HILOGE("bundleName or trustDeviceId is invalid");
336 return DP_INVALID_PARAMS;
337 }
338 HILOGI("Params, userId : %{public}s, bundleName : %{public}s, trustDeviceId : %{public}s, status : %{public}d",
339 ProfileUtils::GetAnonyString(std::to_string(userId)).c_str(),
340 bundleName.c_str(), ProfileUtils::GetAnonyString(trustDeviceId).c_str(), status);
341 std::shared_ptr<ResultSet> resultSet =
342 GetResultSet(SELECT_ACCESS_CONTROL_TABLE_WHERE_TRUSTDEVICEID_AND_STATUS,
343 std::vector<ValueObject>{ ValueObject(trustDeviceId), ValueObject(status) });
344 if (resultSet == nullptr) {
345 HILOGE("resultSet is nullptr");
346 return DP_GET_RESULTSET_FAIL;
347 }
348 int32_t rowCount = ROWCOUNT_INIT;
349 resultSet->GetRowCount(rowCount);
350 if (rowCount == 0) {
351 HILOGE("trustDeviceId and status not find");
352 resultSet->Close();
353 return DP_NOT_FIND_DATA;
354 }
355 int32_t ret = this->GetAclProfileByUserIdAndBundleName(resultSet, userId, bundleName, profile);
356 resultSet->Close();
357 if (ret != DP_SUCCESS) {
358 HILOGE("GetAclProfileByUserIdAndBundleName faild");
359 return ret;
360 }
361 RemoveLnnAcl(profile);
362 if (profile.empty()) {
363 HILOGE("by userId bundleName trustDeviceId status not find data");
364 return DP_NOT_FIND_DATA;
365 }
366 HILOGI("end!");
367 return DP_SUCCESS;
368 }
369
GetAccessControlProfileByTokenId(int64_t tokenId,const std::string & trustDeviceId,int32_t status,std::vector<AccessControlProfile> & profile)370 int32_t TrustProfileManager::GetAccessControlProfileByTokenId(int64_t tokenId,
371 const std::string& trustDeviceId, int32_t status, std::vector<AccessControlProfile>& profile)
372 {
373 if (trustDeviceId.size() > MAX_STRING_LEN) {
374 HILOGE("trustDeviceId is invalid");
375 return DP_INVALID_PARAMS;
376 }
377 HILOGI("Params, tokenId : %{public}" PRId64 ", trustDeviceId : %{public}s, status : %{public}d",
378 tokenId, ProfileUtils::GetAnonyString(trustDeviceId).c_str(), status);
379 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_ACCESS_CONTROL_TABLE_WHERE_STATUS,
380 std::vector<ValueObject>{ ValueObject(status) });
381 if (resultSet == nullptr) {
382 HILOGE("resultSet is nullptr");
383 return DP_GET_RESULTSET_FAIL;
384 }
385 int32_t rowCount = ROWCOUNT_INIT;
386 resultSet->GetRowCount(rowCount);
387 if (rowCount == 0) {
388 HILOGE("trustDeviceId and status not find");
389 resultSet->Close();
390 return DP_NOT_FIND_DATA;
391 }
392 int32_t ret = this->GetAclProfileByTokenId(resultSet, trustDeviceId, tokenId, profile);
393 resultSet->Close();
394 if (ret != DP_SUCCESS) {
395 HILOGE("GetAclProfileByTokenId faild");
396 return ret;
397 }
398 RemoveLnnAcl(profile);
399 if (profile.empty()) {
400 HILOGE("tokenId not find data");
401 return DP_NOT_FIND_DATA;
402 }
403 HILOGI("end!");
404 return DP_SUCCESS;
405 }
406
GetAccessControlProfile(int32_t userId,const std::string & accountId,std::vector<AccessControlProfile> & profile)407 int32_t TrustProfileManager::GetAccessControlProfile(int32_t userId,
408 const std::string& accountId, std::vector<AccessControlProfile>& profile)
409 {
410 if (accountId.size() > MAX_STRING_LEN) {
411 HILOGE("accountId is invalid");
412 return DP_INVALID_PARAMS;
413 }
414 HILOGI("Params, userId : %{public}s, accountId : %{public}s",
415 ProfileUtils::GetAnonyString(std::to_string(userId)).c_str(),
416 ProfileUtils::GetAnonyString(accountId).c_str());
417 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_ACCESS_CONTROL_TABLE, std::vector<ValueObject> {});
418 if (resultSet == nullptr) {
419 HILOGE("resultSet is nullptr");
420 return DP_GET_RESULTSET_FAIL;
421 }
422 int32_t rowCount = ROWCOUNT_INIT;
423 resultSet->GetRowCount(rowCount);
424 if (rowCount == 0) {
425 HILOGE("accesscontrol_table no data");
426 resultSet->Close();
427 return DP_NOT_FIND_DATA;
428 }
429 while (resultSet->GoToNextRow() == DP_SUCCESS) {
430 int32_t columnIndex = COLUMNINDEX_INIT;
431 int64_t accesserId = ACCESSERID_INIT;
432 resultSet->GetColumnIndex(ACCESSER_ID, columnIndex);
433 resultSet->GetLong(columnIndex, accesserId);
434 int64_t accesseeId = ACCESSEEID_INIT;
435 resultSet->GetColumnIndex(ACCESSEE_ID, columnIndex);
436 resultSet->GetLong(columnIndex, accesseeId);
437 int32_t ret = this->GetAclProfileByUserIdAndAccountId(
438 resultSet, accesserId, accesseeId, userId, accountId, profile);
439 if (ret != DP_SUCCESS) {
440 HILOGE("GetAclProfileByUserIdAndAccountId faild");
441 return ret;
442 }
443 }
444 resultSet->Close();
445 RemoveLnnAcl(profile);
446 if (profile.empty()) {
447 HILOGE("by userId accountId not find data");
448 return DP_NOT_FIND_DATA;
449 }
450 HILOGD("end!");
451 return DP_SUCCESS;
452 }
453
GetAccessControlProfile(int32_t userId,std::vector<AccessControlProfile> & profile)454 int32_t TrustProfileManager::GetAccessControlProfile(int32_t userId, std::vector<AccessControlProfile> &profile)
455 {
456 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_ACCESS_CONTROL_TABLE, std::vector<ValueObject> {});
457 if (resultSet == nullptr) {
458 HILOGE("resultSet is nullptr");
459 return DP_GET_RESULTSET_FAIL;
460 }
461 int32_t rowCount = ROWCOUNT_INIT;
462 resultSet->GetRowCount(rowCount);
463 if (rowCount == 0) {
464 HILOGE("accesscontrol_table no data");
465 resultSet->Close();
466 return DP_NOT_FIND_DATA;
467 }
468 while (resultSet->GoToNextRow() == DP_SUCCESS) {
469 int32_t columnIndex = COLUMNINDEX_INIT;
470 int64_t accesserId = ACCESSERID_INIT;
471 resultSet->GetColumnIndex(ACCESSER_ID, columnIndex);
472 resultSet->GetLong(columnIndex, accesserId);
473 int64_t accesseeId = ACCESSEEID_INIT;
474 resultSet->GetColumnIndex(ACCESSEE_ID, columnIndex);
475 resultSet->GetLong(columnIndex, accesseeId);
476 int32_t ret = GetAccessControlProfiles(resultSet, accesserId, accesseeId, userId, profile);
477 if (ret != DP_SUCCESS) {
478 HILOGE("GetAccessControlProfile faild");
479 resultSet->Close();
480 return ret;
481 }
482 }
483 resultSet->Close();
484 RemoveLnnAcl(profile);
485 if (profile.empty()) {
486 HILOGE("by userId not find data, userId: %{public}s",
487 ProfileUtils::GetAnonyString(std::to_string(userId)).c_str());
488 return DP_NOT_FIND_DATA;
489 }
490 HILOGD("end!");
491 return DP_SUCCESS;
492 }
493
GetAccessControlProfile(const QueryType & queryType,const QueryProfile & queryProfile,std::vector<AccessControlProfile> & profile)494 int32_t TrustProfileManager::GetAccessControlProfile(const QueryType& queryType,
495 const QueryProfile& queryProfile, std::vector<AccessControlProfile>& profile)
496 {
497 std::vector<AccessControlProfile> aclProfiles;
498 int32_t ret = GetAllAccessControlProfiles(aclProfiles);
499 if (ret != DP_SUCCESS) {
500 HILOGE("GetAllAccessControlProfiles faild");
501 return ret;
502 }
503 if (queryType == QueryType::ACER_AND_ACEE_TOKENID) {
504 GetAclByAcerAndAceeTokenId(queryProfile, aclProfiles, profile);
505 }
506 if (queryType == QueryType::ACER_TOKENID) {
507 GetAclByAcerTokenId(queryProfile, aclProfiles, profile);
508 }
509 RemoveLnnAcl(profile);
510 if (profile.empty()) {
511 HILOGE("by userId and tokenId not find data, queryProfile : %{public}s", queryProfile.dump().c_str());
512 return DP_NOT_FIND_DATA;
513 }
514 HILOGI("profile size : %{public}zu", profile.size());
515 return DP_SUCCESS;
516 }
517
GetAclByAcerTokenId(const QueryProfile & queryProfile,const std::vector<AccessControlProfile> & aclProfiles,std::vector<AccessControlProfile> & profile)518 void TrustProfileManager::GetAclByAcerTokenId(const QueryProfile& queryProfile,
519 const std::vector<AccessControlProfile>& aclProfiles, std::vector<AccessControlProfile>& profile)
520 {
521 for (auto aclProfile : aclProfiles) {
522 if (aclProfile.GetTrustDeviceId() != queryProfile.GetAccesseeDeviceId() &&
523 aclProfile.GetTrustDeviceId() != queryProfile.GetAccesserDeviceId()) {
524 continue;
525 }
526 if (CheckForWardByAcer(queryProfile, aclProfile) || CheckReverseByAcer(queryProfile, aclProfile)) {
527 profile.emplace_back(aclProfile);
528 }
529 }
530 return;
531 }
532
GetAclByAcerAndAceeTokenId(const QueryProfile & queryProfile,const std::vector<AccessControlProfile> & aclProfiles,std::vector<AccessControlProfile> & profile)533 void TrustProfileManager::GetAclByAcerAndAceeTokenId(const QueryProfile& queryProfile,
534 const std::vector<AccessControlProfile>& aclProfiles, std::vector<AccessControlProfile>& profile)
535 {
536 for (auto aclProfile : aclProfiles) {
537 if (aclProfile.GetTrustDeviceId() != queryProfile.GetAccesseeDeviceId() &&
538 aclProfile.GetTrustDeviceId() != queryProfile.GetAccesserDeviceId()) {
539 continue;
540 }
541 if (CheckForWardByAcerAndAcee(queryProfile, aclProfile) ||
542 CheckReverseByAcerAndAcee(queryProfile, aclProfile)) {
543 profile.emplace_back(aclProfile);
544 }
545 }
546 return;
547 }
548
GetAllAccessControlProfile(std::vector<AccessControlProfile> & profiles)549 int32_t TrustProfileManager::GetAllAccessControlProfile(std::vector<AccessControlProfile>& profiles)
550 {
551 std::lock_guard<std::mutex> lock(aclMutex_);
552 int32_t ret = GetAllAccessControlProfiles(profiles);
553 if (ret != DP_SUCCESS) {
554 HILOGE("GetAllAccessControlProfile failed");
555 return ret;
556 }
557 RemoveLnnAcl(profiles);
558 if (profiles.empty()) {
559 HILOGE("not find data");
560 return DP_NOT_FIND_DATA;
561 }
562 return DP_SUCCESS;
563 }
564
GetAllAclIncludeLnnAcl(std::vector<AccessControlProfile> & profiles)565 int32_t TrustProfileManager::GetAllAclIncludeLnnAcl(std::vector<AccessControlProfile>& profiles)
566 {
567 std::lock_guard<std::mutex> lock(aclMutex_);
568 return GetAllAccessControlProfiles(profiles);
569 }
570
GetAccessControlProfile(const std::string & bundleName,int32_t bindType,int32_t status,std::vector<AccessControlProfile> & profile)571 int32_t TrustProfileManager::GetAccessControlProfile(const std::string& bundleName,
572 int32_t bindType, int32_t status, std::vector<AccessControlProfile>& profile)
573 {
574 if (bundleName.size() > MAX_STRING_LEN) {
575 HILOGE("bundleName is invalid");
576 return DP_INVALID_PARAMS;
577 }
578 HILOGI("Params, bundleName : %{public}s, bindType : %{public}d, status : %{public}d",
579 bundleName.c_str(), bindType, status);
580 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_ACCESS_CONTROL_TABLE_WHERE_BINDTYPE_AND_STATUS,
581 std::vector<ValueObject>{ ValueObject(bindType), ValueObject(status) });
582 if (resultSet == nullptr) {
583 HILOGE("resultSet is nullptr");
584 return DP_GET_RESULTSET_FAIL;
585 }
586 int32_t rowCount = ROWCOUNT_INIT;
587 resultSet->GetRowCount(rowCount);
588 if (rowCount == 0) {
589 HILOGE("bindType and status not find");
590 resultSet->Close();
591 return DP_NOT_FIND_DATA;
592 }
593 int32_t ret = this->GetAclProfileByBundleName(resultSet, bundleName, profile);
594 resultSet->Close();
595 if (ret != DP_SUCCESS) {
596 HILOGE("GetAclProfileByBundleName faild");
597 return ret;
598 }
599 RemoveLnnAcl(profile);
600 if (profile.empty()) {
601 HILOGE("by bundleName bindType status not find data");
602 return DP_NOT_FIND_DATA;
603 }
604 HILOGI("end!");
605 return DP_SUCCESS;
606 }
607
GetAccessControlProfile(const std::string & bundleName,const std::string & trustDeviceId,int32_t status,std::vector<AccessControlProfile> & profile)608 int32_t TrustProfileManager::GetAccessControlProfile(const std::string& bundleName,
609 const std::string& trustDeviceId, int32_t status, std::vector<AccessControlProfile>& profile)
610 {
611 if (bundleName.size() > MAX_STRING_LEN || trustDeviceId.size() > MAX_STRING_LEN) {
612 HILOGE("bundleName or trustDeviceId is invalid");
613 return DP_INVALID_PARAMS;
614 }
615 HILOGI("Params, bundleName : %{public}s, trustDeviceId : %{public}s, status : %{public}d",
616 bundleName.c_str(), ProfileUtils::GetAnonyString(trustDeviceId).c_str(), status);
617 std::shared_ptr<ResultSet> resultSet =
618 GetResultSet(SELECT_ACCESS_CONTROL_TABLE_WHERE_TRUSTDEVICEID_AND_STATUS,
619 std::vector<ValueObject>{ ValueObject(trustDeviceId), ValueObject(status) });
620 if (resultSet == nullptr) {
621 HILOGE("resultSet is nullptr");
622 return DP_GET_RESULTSET_FAIL;
623 }
624 int32_t rowCount = ROWCOUNT_INIT;
625 resultSet->GetRowCount(rowCount);
626 if (rowCount == 0) {
627 HILOGE("trustDeviceId and status not find");
628 resultSet->Close();
629 return DP_NOT_FIND_DATA;
630 }
631 int32_t ret = this->GetAclProfileByBundleName(resultSet, bundleName, profile);
632 resultSet->Close();
633 if (ret != DP_SUCCESS) {
634 HILOGE("GetAclProfileByBundleName faild");
635 return ret;
636 }
637 RemoveLnnAcl(profile);
638 if (profile.empty()) {
639 HILOGE("by bundleName trustDeviceId status not find data");
640 return DP_NOT_FIND_DATA;
641 }
642 HILOGI("end!");
643 return DP_SUCCESS;
644 }
645
GetAccessControlProfile(const std::map<std::string,std::string> & params,std::vector<AccessControlProfile> & profile)646 int32_t TrustProfileManager::GetAccessControlProfile(const std::map<std::string, std::string>& params,
647 std::vector<AccessControlProfile>& profile)
648 {
649 std::lock_guard<std::mutex> lock(aclMutex_);
650 if (params.find(TRUST_DEVICE_ID) != params.end() &&
651 ProfileUtils::IsPropertyValid(params, STATUS, INT32_MIN, INT32_MAX)) {
652 if (ProfileUtils::IsPropertyValid(params, USERID, INT32_MIN, INT32_MAX) &&
653 params.find(BUNDLENAME) != params.end()) {
654 return GetAccessControlProfile(std::atoi(params.at(USERID).c_str()),
655 params.at(BUNDLENAME), params.at(TRUST_DEVICE_ID), std::atoi(params.at(STATUS).c_str()), profile);
656 }
657 if (params.find(BUNDLENAME) != params.end()) {
658 return GetAccessControlProfile(params.at(BUNDLENAME),
659 params.at(TRUST_DEVICE_ID), std::atoi(params.at(STATUS).c_str()), profile);
660 }
661 if (ProfileUtils::IsPropertyValidInt64(params, TOKENID)) {
662 return GetAccessControlProfileByTokenId(std::atoi(params.at(TOKENID).c_str()),
663 params.at(TRUST_DEVICE_ID), std::atoi(params.at(STATUS).c_str()), profile);
664 }
665 }
666 if (ProfileUtils::IsPropertyValid(params, BIND_TYPE, INT32_MIN, INT32_MAX) &&
667 ProfileUtils::IsPropertyValid(params, STATUS, INT32_MIN, INT32_MAX)) {
668 if (ProfileUtils::IsPropertyValid(params, USERID, INT32_MIN, INT32_MAX) &&
669 params.find(BUNDLENAME) != params.end()) {
670 return GetAccessControlProfile(std::atoi(params.at(USERID).c_str()),
671 params.at(BUNDLENAME), std::atoi(params.at(BIND_TYPE).c_str()),
672 std::atoi(params.at(STATUS).c_str()), profile);
673 }
674 if (params.find(BUNDLENAME) != params.end()) {
675 return GetAccessControlProfile(params.at(BUNDLENAME), std::atoi(params.at(BIND_TYPE).c_str()),
676 std::atoi(params.at(STATUS).c_str()), profile);
677 }
678 }
679 if (ProfileUtils::IsPropertyValid(params, USERID, INT32_MIN, INT32_MAX)) {
680 if (params.find(ACCOUNTID) != params.end()) {
681 return GetAccessControlProfile(std::atoi(params.at(USERID).c_str()), params.at(ACCOUNTID), profile);
682 }
683 return GetAccessControlProfile(std::atoi(params.at(USERID).c_str()), profile);
684 }
685 QueryProfile queryProfile;
686 QueryType queryType;
687 if (GenerateQueryProfile(params, queryType, queryProfile)) {
688 return GetAccessControlProfile(queryType, queryProfile, profile);
689 }
690 HILOGE("params is error");
691 return DP_INVALID_PARAMS;
692 }
693
DeleteTrustDeviceProfile(const std::string & deviceId)694 int32_t TrustProfileManager::DeleteTrustDeviceProfile(const std::string& deviceId)
695 {
696 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_TRUST_DEVICE_TABLE_WHERE_DEVICEID,
697 std::vector<ValueObject>{ ValueObject(deviceId) });
698 if (resultSet == nullptr) {
699 HILOGE("resultSet is nullptr");
700 return DP_GET_RESULTSET_FAIL;
701 }
702 int32_t rowCount = ROWCOUNT_INIT;
703 resultSet->GetRowCount(rowCount);
704 if (rowCount == 0) {
705 HILOGE("no data");
706 resultSet->Close();
707 return DP_NOT_FIND_DATA;
708 }
709 resultSet->GoToFirstRow();
710 TrustDeviceProfile profile;
711 ProfileUtils::ConvertToTrustDeviceProfile(resultSet, profile);
712 resultSet->Close();
713 {
714 std::lock_guard<std::mutex> lock(rdbMutex_);
715 if (rdbStore_ == nullptr) {
716 HILOGE("rdbStore_ is nullptr");
717 return DP_GET_RDBSTORE_FAIL;
718 }
719 int32_t deleteRows = DELETEROWS_INIT;
720 int32_t ret = rdbStore_->Delete(deleteRows, TRUST_DEVICE_TABLE, DEVICEID_EQUAL_CONDITION,
721 std::vector<ValueObject>{ ValueObject(deviceId) });
722 if (ret != DP_SUCCESS) {
723 HILOGE("delete trustdevice_table data failed");
724 return DP_DELETE_TRUST_DEVICE_PROFILE_FAIL;
725 }
726 }
727 HILOGI("end!");
728 return DP_SUCCESS;
729 }
730
DeleteAccessControlProfile(int64_t accessControlId)731 int32_t TrustProfileManager::DeleteAccessControlProfile(int64_t accessControlId)
732 {
733 AccessControlProfile profile;
734 {
735 std::lock_guard<std::mutex> lock(aclMutex_);
736 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_ACCESS_CONTROL_TABLE_WHERE_ACCESSCONTROLID,
737 std::vector<ValueObject>{ ValueObject(accessControlId) });
738 if (resultSet == nullptr) {
739 HILOGE("resultSet is nullptr");
740 return DP_GET_RESULTSET_FAIL;
741 }
742 int32_t rowCount = ROWCOUNT_INIT;
743 resultSet->GetRowCount(rowCount);
744 if (rowCount == 0) {
745 HILOGE("by accessControlId not find data");
746 resultSet->Close();
747 return DP_NOT_FIND_DATA;
748 }
749 if (resultSet->GoToNextRow() != DP_SUCCESS) {
750 HILOGE("get AccessControlProfileResult failed");
751 resultSet->Close();
752 return DP_NOT_FIND_DATA;
753 }
754 ProfileUtils::ConvertToAccessControlProfile(resultSet, profile);
755 resultSet->Close();
756 int32_t ret = this->DeleteAccessControlProfileCheck(profile);
757 if (ret != DP_SUCCESS) {
758 HILOGE("DeleteAccessControlProfileCheck failed");
759 return ret;
760 }
761 }
762 int32_t delRet = this->DeleteTrustDeviceCheck(profile);
763 if (delRet != DP_SUCCESS) {
764 HILOGE("DeleteTrustDeviceCheck failed");
765 return delRet;
766 }
767 HILOGI("end!");
768 return DP_SUCCESS;
769 }
770
CreateTable()771 int32_t TrustProfileManager::CreateTable()
772 {
773 std::lock_guard<std::mutex> lock(rdbMutex_);
774 if (rdbStore_ == nullptr) {
775 HILOGE("rdbStore_ is nullptr");
776 return DP_GET_RDBSTORE_FAIL;
777 }
778 int32_t ret = rdbStore_->CreateTable(CREATE_TURST_DEVICE_TABLE_SQL);
779 if (ret != DP_SUCCESS) {
780 HILOGE("trustdevice_table create failed");
781 return DP_CREATE_TABLE_FAIL;
782 }
783 ret = rdbStore_->CreateTable(CREATE_ACCESS_CONTROL_TABLE_SQL);
784 if (ret != DP_SUCCESS) {
785 HILOGE("accesscontrol_table create failed");
786 return DP_CREATE_TABLE_FAIL;
787 }
788 ret = rdbStore_->CreateTable(CREATE_ACCESSER_TABLE_SQL);
789 if (ret != DP_SUCCESS) {
790 HILOGE("accessertable create failed");
791 return DP_CREATE_TABLE_FAIL;
792 }
793 ret = rdbStore_->CreateTable(CREATE_ACCESSEE_TABLE_SQL);
794 if (ret != DP_SUCCESS) {
795 HILOGE("accesseetable create failed");
796 return DP_CREATE_TABLE_FAIL;
797 }
798 return DP_SUCCESS;
799 }
800
CreateUniqueIndex()801 int32_t TrustProfileManager::CreateUniqueIndex()
802 {
803 std::lock_guard<std::mutex> lock(rdbMutex_);
804 if (rdbStore_ == nullptr) {
805 HILOGE("rdbStore_ is nullptr");
806 return DP_GET_RDBSTORE_FAIL;
807 }
808 int32_t ret = rdbStore_->CreateTable(CREATE_TURST_DEVICE_TABLE_UNIQUE_INDEX_SQL);
809 if (ret != DP_SUCCESS) {
810 HILOGE("trustdevice_table unique index create failed");
811 return DP_CREATE_UNIQUE_INDEX_FAIL;
812 }
813 ret = rdbStore_->CreateTable(CREATE_ACCESS_CONTROL_TABLE_UNIQUE_INDEX_SQL);
814 if (ret != DP_SUCCESS) {
815 HILOGE("accesscontrol_table unique index create failed");
816 return DP_CREATE_UNIQUE_INDEX_FAIL;
817 }
818 ret = rdbStore_->CreateTable(CREATE_ACCESSER_TABLE_UNIQUE_INDEX_SQL);
819 if (ret != DP_SUCCESS) {
820 HILOGE("accessertable unique index create failed");
821 return DP_CREATE_UNIQUE_INDEX_FAIL;
822 }
823 ret = rdbStore_->CreateTable(CREATE_ACCESSEE_TABLE_UNIQUE_INDEX_SQL);
824 if (ret != DP_SUCCESS) {
825 HILOGE("accesseetable unique index create failed");
826 return DP_CREATE_UNIQUE_INDEX_FAIL;
827 }
828 return DP_SUCCESS;
829 }
830
CheckForWardByAcerAndAcee(const QueryProfile & queryProfile,const AccessControlProfile & aclProfile)831 bool TrustProfileManager::CheckForWardByAcerAndAcee(const QueryProfile& queryProfile,
832 const AccessControlProfile& aclProfile)
833 {
834 if (aclProfile.GetAccesser().GetAccesserDeviceId() != queryProfile.GetAccesserDeviceId() ||
835 aclProfile.GetAccessee().GetAccesseeDeviceId() != queryProfile.GetAccesseeDeviceId() ||
836 aclProfile.GetStatus() != static_cast<int32_t>(Status::ACTIVE)) {
837 return false;
838 }
839 if ((aclProfile.GetAccesser().GetAccesserUserId() != queryProfile.GetAccesserUserId() &&
840 aclProfile.GetAccesser().GetAccesserUserId() != DEFAULT_USER_ID &&
841 aclProfile.GetAccesser().GetAccesserUserId() != DEFAULT_USER_ID_EXTRA) ||
842 (aclProfile.GetAccessee().GetAccesseeUserId() != queryProfile.GetAccesseeUserId() &&
843 aclProfile.GetAccessee().GetAccesseeUserId() != DEFAULT_USER_ID &&
844 aclProfile.GetAccessee().GetAccesseeUserId() != DEFAULT_USER_ID_EXTRA)) {
845 return false;
846 }
847 if ((aclProfile.GetBindType() == static_cast<uint32_t>(BindType::SAME_ACCOUNT) &&
848 aclProfile.GetAccesser().GetAccesserAccountId() == queryProfile.GetAccesserAccountId() &&
849 aclProfile.GetAccessee().GetAccesseeAccountId() == queryProfile.GetAccesseeAccountId()) ||
850 aclProfile.GetBindLevel() == static_cast<uint32_t>(BindLevel::USER)) {
851 return true;
852 }
853 if (aclProfile.GetAccesser().GetAccesserTokenId() == queryProfile.GetAccesserTokenId() &&
854 aclProfile.GetAccessee().GetAccesseeTokenId() == queryProfile.GetAccesseeTokenId()) {
855 return true;
856 }
857 return false;
858 }
859
CheckForWardByAcer(const QueryProfile & queryProfile,const AccessControlProfile & aclProfile)860 bool TrustProfileManager::CheckForWardByAcer(const QueryProfile& queryProfile,
861 const AccessControlProfile& aclProfile)
862 {
863 if (aclProfile.GetAccesser().GetAccesserDeviceId() != queryProfile.GetAccesserDeviceId() ||
864 aclProfile.GetAccessee().GetAccesseeDeviceId() != queryProfile.GetAccesseeDeviceId() ||
865 aclProfile.GetStatus() != static_cast<int32_t>(Status::ACTIVE)) {
866 return false;
867 }
868 if (aclProfile.GetAccesser().GetAccesserUserId() != queryProfile.GetAccesserUserId() &&
869 aclProfile.GetAccesser().GetAccesserUserId() != DEFAULT_USER_ID &&
870 aclProfile.GetAccesser().GetAccesserUserId() != DEFAULT_USER_ID_EXTRA) {
871 return false;
872 }
873 if ((aclProfile.GetBindType() == static_cast<uint32_t>(BindType::SAME_ACCOUNT) &&
874 aclProfile.GetAccesser().GetAccesserAccountId() == queryProfile.GetAccesserAccountId()) ||
875 aclProfile.GetBindLevel() == static_cast<uint32_t>(BindLevel::USER)) {
876 return true;
877 }
878 if (aclProfile.GetAccesser().GetAccesserTokenId() == queryProfile.GetAccesserTokenId()) {
879 return true;
880 }
881 return false;
882 }
883
CheckReverseByAcerAndAcee(const QueryProfile & queryProfile,const AccessControlProfile & aclProfile)884 bool TrustProfileManager::CheckReverseByAcerAndAcee(const QueryProfile& queryProfile,
885 const AccessControlProfile& aclProfile)
886 {
887 if (aclProfile.GetAccesser().GetAccesserDeviceId() != queryProfile.GetAccesseeDeviceId() ||
888 aclProfile.GetAccessee().GetAccesseeDeviceId() != queryProfile.GetAccesserDeviceId() ||
889 aclProfile.GetStatus() != static_cast<int32_t>(Status::ACTIVE)) {
890 return false;
891 }
892 if ((aclProfile.GetAccesser().GetAccesserUserId() != queryProfile.GetAccesseeUserId() &&
893 aclProfile.GetAccesser().GetAccesserUserId() != DEFAULT_USER_ID &&
894 aclProfile.GetAccesser().GetAccesserUserId() != DEFAULT_USER_ID_EXTRA) ||
895 (aclProfile.GetAccessee().GetAccesseeUserId() != queryProfile.GetAccesserUserId() &&
896 aclProfile.GetAccessee().GetAccesseeUserId() != DEFAULT_USER_ID &&
897 aclProfile.GetAccessee().GetAccesseeUserId() != DEFAULT_USER_ID_EXTRA)) {
898 return false;
899 }
900 if ((aclProfile.GetBindType() == static_cast<uint32_t>(BindType::SAME_ACCOUNT) &&
901 aclProfile.GetAccesser().GetAccesserAccountId() == queryProfile.GetAccesseeAccountId() &&
902 aclProfile.GetAccessee().GetAccesseeAccountId() == queryProfile.GetAccesserAccountId()) ||
903 aclProfile.GetBindLevel() == static_cast<uint32_t>(BindLevel::USER)) {
904 return true;
905 }
906 if (aclProfile.GetAccesser().GetAccesserTokenId() == queryProfile.GetAccesseeTokenId() &&
907 aclProfile.GetAccessee().GetAccesseeTokenId() == queryProfile.GetAccesserTokenId()) {
908 return true;
909 }
910 return false;
911 }
912
CheckReverseByAcer(const QueryProfile & queryProfile,const AccessControlProfile & aclProfile)913 bool TrustProfileManager::CheckReverseByAcer(const QueryProfile& queryProfile,
914 const AccessControlProfile& aclProfile)
915 {
916 if (aclProfile.GetAccesser().GetAccesserDeviceId() != queryProfile.GetAccesseeDeviceId() ||
917 aclProfile.GetAccessee().GetAccesseeDeviceId() != queryProfile.GetAccesserDeviceId() ||
918 aclProfile.GetStatus() != static_cast<int32_t>(Status::ACTIVE)) {
919 return false;
920 }
921 if (aclProfile.GetAccessee().GetAccesseeUserId() != queryProfile.GetAccesserUserId() &&
922 aclProfile.GetAccessee().GetAccesseeUserId() != DEFAULT_USER_ID &&
923 aclProfile.GetAccessee().GetAccesseeUserId() != DEFAULT_USER_ID_EXTRA) {
924 return false;
925 }
926 if ((aclProfile.GetBindType() == static_cast<uint32_t>(BindType::SAME_ACCOUNT) &&
927 aclProfile.GetAccessee().GetAccesseeAccountId() == queryProfile.GetAccesserAccountId()) ||
928 aclProfile.GetBindLevel() == static_cast<uint32_t>(BindLevel::USER)) {
929 return true;
930 }
931 if (aclProfile.GetAccessee().GetAccesseeTokenId() == queryProfile.GetAccesserTokenId()) {
932 return true;
933 }
934 return false;
935 }
936
GenerateQueryProfile(const std::map<std::string,std::string> & params,QueryType & queryType,QueryProfile & queryProfile)937 bool TrustProfileManager::GenerateQueryProfile(const std::map<std::string, std::string>& params,
938 QueryType& queryType, QueryProfile& queryProfile)
939 {
940 if (params.find(ACCESSER_DEVICE_ID) != params.end() && params.find(ACCESSEE_DEVICE_ID) != params.end() &&
941 params.find(ACCESSER_ACCOUNT_ID) != params.end() &&
942 ProfileUtils::IsPropertyValid(params, ACCESSER_USER_ID, INT32_MIN, INT32_MAX) &&
943 ProfileUtils::IsPropertyValidInt64(params, ACCESSER_TOKEN_ID)) {
944 if (params.find(ACCESSEE_ACCOUNT_ID) != params.end() &&
945 ProfileUtils::IsPropertyValid(params, ACCESSEE_USER_ID, INT32_MIN, INT32_MAX) &&
946 ProfileUtils::IsPropertyValidInt64(params, ACCESSEE_TOKEN_ID)) {
947 queryProfile.SetAccesserDeviceId(params.at(ACCESSER_DEVICE_ID));
948 queryProfile.SetAccesserUserId(std::atoi(params.at(ACCESSER_USER_ID).c_str()));
949 queryProfile.SetAccesserAccountId(params.at(ACCESSER_ACCOUNT_ID));
950 queryProfile.SetAccesserTokenId(std::atoi(params.at(ACCESSER_TOKEN_ID).c_str()));
951 queryProfile.SetAccesseeDeviceId(params.at(ACCESSEE_DEVICE_ID));
952 queryProfile.SetAccesseeUserId(std::atoi(params.at(ACCESSEE_USER_ID).c_str()));
953 queryProfile.SetAccesseeAccountId(params.at(ACCESSEE_ACCOUNT_ID));
954 queryProfile.SetAccesseeTokenId(std::atoi(params.at(ACCESSEE_TOKEN_ID).c_str()));
955 queryType = QueryType::ACER_AND_ACEE_TOKENID;
956 return true;
957 }
958 queryProfile.SetAccesserDeviceId(params.at(ACCESSER_DEVICE_ID));
959 queryProfile.SetAccesserTokenId(std::atoi(params.at(ACCESSER_TOKEN_ID).c_str()));
960 queryProfile.SetAccesserUserId(std::atoi(params.at(ACCESSER_USER_ID).c_str()));
961 queryProfile.SetAccesserAccountId(params.at(ACCESSER_ACCOUNT_ID));
962 queryProfile.SetAccesseeDeviceId(params.at(ACCESSEE_DEVICE_ID));
963 queryType = QueryType::ACER_TOKENID;
964 return true;
965 }
966 return false;
967 }
968
GetAllAccessControlProfiles(std::vector<AccessControlProfile> & profiles)969 int32_t TrustProfileManager::GetAllAccessControlProfiles(std::vector<AccessControlProfile>& profiles)
970 {
971 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_ACCESS_CONTROL_TABLE, std::vector<ValueObject> {});
972 if (resultSet == nullptr) {
973 HILOGE("resultSet is nullptr");
974 return DP_GET_RESULTSET_FAIL;
975 }
976 int32_t rowCount = ROWCOUNT_INIT;
977 resultSet->GetRowCount(rowCount);
978 if (rowCount == 0) {
979 HILOGE("accesscontrol_table no data");
980 resultSet->Close();
981 return DP_NOT_FIND_DATA;
982 }
983 while (resultSet->GoToNextRow() == DP_SUCCESS) {
984 int32_t columnIndex = COLUMNINDEX_INIT;
985 int64_t accesserId = ACCESSERID_INIT;
986 resultSet->GetColumnIndex(ACCESSER_ID, columnIndex);
987 resultSet->GetLong(columnIndex, accesserId);
988 int64_t accesseeId = ACCESSEEID_INIT;
989 resultSet->GetColumnIndex(ACCESSEE_ID, columnIndex);
990 resultSet->GetLong(columnIndex, accesseeId);
991 int32_t ret = this->GetAccessControlProfile(resultSet, accesserId, accesseeId, profiles);
992 if (ret != DP_SUCCESS) {
993 HILOGE("GetAccessControlProfile faild");
994 resultSet->Close();
995 return ret;
996 }
997 }
998 resultSet->Close();
999 if (profiles.empty()) {
1000 return DP_NOT_FIND_DATA;
1001 }
1002 HILOGI("end!");
1003 return DP_SUCCESS;
1004 }
1005
GetAclProfileByUserIdAndBundleName(std::shared_ptr<ResultSet> resultSet,int32_t userId,const std::string & bundleName,std::vector<AccessControlProfile> & profile)1006 int32_t TrustProfileManager::GetAclProfileByUserIdAndBundleName(std::shared_ptr<ResultSet> resultSet,
1007 int32_t userId, const std::string& bundleName, std::vector<AccessControlProfile>& profile)
1008 {
1009 if (resultSet == nullptr) {
1010 HILOGE("resultSet is nullptr");
1011 return DP_GET_RESULTSET_FAIL;
1012 }
1013 while (resultSet->GoToNextRow() == DP_SUCCESS) {
1014 int32_t columnIndex = COLUMNINDEX_INIT;
1015 int64_t accesserId = ACCESSERID_INIT;
1016 resultSet->GetColumnIndex(ACCESSER_ID, columnIndex);
1017 resultSet->GetLong(columnIndex, accesserId);
1018 int64_t accesseeId = ACCESSEEID_INIT;
1019 resultSet->GetColumnIndex(ACCESSEE_ID, columnIndex);
1020 resultSet->GetLong(columnIndex, accesseeId);
1021 int32_t bindType = BINDTYPE_INIT;
1022 resultSet->GetColumnIndex(BIND_TYPE, columnIndex);
1023 resultSet->GetInt(columnIndex, bindType);
1024 int32_t bindLevel = BINDLEVEL_INIT;
1025 resultSet->GetColumnIndex(BIND_LEVEL, columnIndex);
1026 resultSet->GetInt(columnIndex, bindLevel);
1027 if (bindType == static_cast<int32_t>(BindType::SAME_ACCOUNT) ||
1028 bindLevel == static_cast<int32_t>(BindLevel::USER)) {
1029 int32_t ret = this->GetAccessControlProfiles(resultSet, accesserId, accesseeId, userId, profile);
1030 if (ret != DP_SUCCESS) {
1031 HILOGE("GetAccessControlProfiles failed");
1032 return ret;
1033 }
1034 } else {
1035 int32_t ret = this->GetAccessControlProfiles(resultSet, accesserId,
1036 accesseeId, userId, bundleName, profile);
1037 if (ret != DP_SUCCESS) {
1038 HILOGE("GetAccessControlProfiles failed");
1039 return ret;
1040 }
1041 }
1042 }
1043 return DP_SUCCESS;
1044 }
1045
GetAclProfileByUserIdAndAccountId(std::shared_ptr<ResultSet> resultSet,int64_t accesserId,int64_t accesseeId,int32_t userId,const std::string & accountId,std::vector<AccessControlProfile> & profile)1046 int32_t TrustProfileManager::GetAclProfileByUserIdAndAccountId(std::shared_ptr<ResultSet> resultSet, int64_t accesserId,
1047 int64_t accesseeId, int32_t userId, const std::string& accountId, std::vector<AccessControlProfile>& profile)
1048 {
1049 std::shared_ptr<ResultSet> accesserResultSet =
1050 GetResultSet(SELECT_ACCESSER_TABLE_WHERE_ACCESSERID_AND_ACCESSERUSERID_ACCESSERACCOUNTID,
1051 std::vector<ValueObject>{ ValueObject(accesserId), ValueObject(userId), ValueObject(accountId) });
1052 if (accesserResultSet == nullptr) {
1053 HILOGE("accesserResultSet is nullptr");
1054 return DP_GET_RESULTSET_FAIL;
1055 }
1056 int32_t rowCount = ROWCOUNT_INIT;
1057 accesserResultSet->GetRowCount(rowCount);
1058 if (rowCount != 0) {
1059 std::shared_ptr<ResultSet> accesseeResultSet = GetResultSet(SELECT_ACCESSEE_TABLE_WHERE_ACCESSEEID,
1060 std::vector<ValueObject>{ ValueObject(accesseeId) });
1061 if (accesseeResultSet == nullptr) {
1062 HILOGE("accesseeResultSet is nullptr");
1063 return DP_GET_RESULTSET_FAIL;
1064 }
1065 ProfileUtils::ConvertToAccessControlProfiles(resultSet, accesserResultSet, accesseeResultSet, profile);
1066 accesserResultSet->Close();
1067 accesseeResultSet->Close();
1068 return DP_SUCCESS;
1069 }
1070 accesserResultSet->Close();
1071
1072 std::shared_ptr<ResultSet> accesseeResultSet =
1073 GetResultSet(SELECT_ACCESSEE_TABLE_WHERE_ACCESSEEID_AND_ACCESSEEUSEEID_ACCESSEEACCOUNTID,
1074 std::vector<ValueObject>{ ValueObject(accesseeId), ValueObject(userId), ValueObject(accountId) });
1075 if (accesseeResultSet == nullptr) {
1076 HILOGE("accesseeResultSet is nullptr");
1077 return DP_GET_RESULTSET_FAIL;
1078 }
1079 accesseeResultSet->GetRowCount(rowCount);
1080 if (rowCount != 0) {
1081 accesserResultSet = GetResultSet(SELECT_ACCESSER_TABLE_WHERE_ACCESSERID,
1082 std::vector<ValueObject>{ ValueObject(accesserId) });
1083 if (accesserResultSet == nullptr) {
1084 HILOGE("accesserResultSet is nullptr");
1085 return DP_GET_RESULTSET_FAIL;
1086 }
1087 ProfileUtils::ConvertToAccessControlProfiles(resultSet, accesserResultSet, accesseeResultSet, profile);
1088 accesserResultSet->Close();
1089 }
1090 accesseeResultSet->Close();
1091 return DP_SUCCESS;
1092 }
1093
GetAclProfileByTokenId(std::shared_ptr<ResultSet> resultSet,const std::string & trustDeviceId,int64_t tokenId,std::vector<AccessControlProfile> & profile)1094 int32_t TrustProfileManager::GetAclProfileByTokenId(std::shared_ptr<ResultSet> resultSet,
1095 const std::string& trustDeviceId, int64_t tokenId, std::vector<AccessControlProfile>& profile)
1096 {
1097 if (resultSet == nullptr) {
1098 HILOGE("resultSet is nullptr");
1099 return DP_GET_RESULTSET_FAIL;
1100 }
1101 while (resultSet->GoToNextRow() == DP_SUCCESS) {
1102 int32_t columnIndex = COLUMNINDEX_INIT;
1103 int64_t accesserId = ACCESSERID_INIT;
1104 resultSet->GetColumnIndex(ACCESSER_ID, columnIndex);
1105 resultSet->GetLong(columnIndex, accesserId);
1106 int64_t accesseeId = ACCESSEEID_INIT;
1107 resultSet->GetColumnIndex(ACCESSEE_ID, columnIndex);
1108 resultSet->GetLong(columnIndex, accesseeId);
1109 int32_t bindType = BINDTYPE_INIT;
1110 resultSet->GetColumnIndex(BIND_TYPE, columnIndex);
1111 resultSet->GetInt(columnIndex, bindType);
1112 int32_t bindLevel = BINDLEVEL_INIT;
1113 resultSet->GetColumnIndex(BIND_LEVEL, columnIndex);
1114 resultSet->GetInt(columnIndex, bindLevel);
1115 if (bindType == static_cast<int32_t> (BindType::SAME_ACCOUNT) ||
1116 bindLevel == static_cast<int32_t>(BindLevel::USER)) {
1117 int32_t ret = this->GetAccessControlProfilesByDeviceId(
1118 resultSet, accesserId, accesseeId, trustDeviceId, profile);
1119 if (ret != DP_SUCCESS) {
1120 HILOGE("GetAccessControlProfile failed");
1121 return ret;
1122 }
1123 } else {
1124 int32_t ret = this->GetAccessControlProfilesByTokenId(resultSet, accesserId,
1125 accesseeId, trustDeviceId, tokenId, profile);
1126 if (ret != DP_SUCCESS) {
1127 HILOGE("GetAccessControlProfilesByTokenId failed");
1128 return ret;
1129 }
1130 }
1131 }
1132 return DP_SUCCESS;
1133 }
1134
GetAclProfileByBundleName(std::shared_ptr<ResultSet> resultSet,const std::string & bundleName,std::vector<AccessControlProfile> & profile)1135 int32_t TrustProfileManager::GetAclProfileByBundleName(std::shared_ptr<ResultSet> resultSet,
1136 const std::string& bundleName, std::vector<AccessControlProfile>& profile)
1137 {
1138 if (resultSet == nullptr) {
1139 HILOGE("resultSet is nullptr");
1140 return DP_GET_RESULTSET_FAIL;
1141 }
1142 while (resultSet->GoToNextRow() == DP_SUCCESS) {
1143 int32_t columnIndex = COLUMNINDEX_INIT;
1144 int64_t accesserId = ACCESSERID_INIT;
1145 resultSet->GetColumnIndex(ACCESSER_ID, columnIndex);
1146 resultSet->GetLong(columnIndex, accesserId);
1147 int64_t accesseeId = ACCESSEEID_INIT;
1148 resultSet->GetColumnIndex(ACCESSEE_ID, columnIndex);
1149 resultSet->GetLong(columnIndex, accesseeId);
1150 int32_t bindType = BINDTYPE_INIT;
1151 resultSet->GetColumnIndex(BIND_TYPE, columnIndex);
1152 resultSet->GetInt(columnIndex, bindType);
1153 int32_t bindLevel = BINDLEVEL_INIT;
1154 resultSet->GetColumnIndex(BIND_LEVEL, columnIndex);
1155 resultSet->GetInt(columnIndex, bindLevel);
1156 if (bindType == static_cast<int32_t> (BindType::SAME_ACCOUNT) ||
1157 bindLevel == static_cast<int32_t>(BindLevel::USER)) {
1158 int32_t ret = this->GetAccessControlProfile(resultSet, accesserId, accesseeId, profile);
1159 if (ret != DP_SUCCESS) {
1160 HILOGE("GetAccessControlProfile failed");
1161 return ret;
1162 }
1163 } else {
1164 int32_t ret = this->GetAccessControlProfiles(resultSet, accesserId, accesseeId, bundleName, profile);
1165 if (ret != DP_SUCCESS) {
1166 HILOGE("GetAccessControlProfiles failed");
1167 return ret;
1168 }
1169 }
1170 }
1171 return DP_SUCCESS;
1172 }
1173
PutAccesserProfile(const AccessControlProfile & profile)1174 int32_t TrustProfileManager::PutAccesserProfile(const AccessControlProfile& profile)
1175 {
1176 Accesser accesser = profile.GetAccesser();
1177 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_ACCESSER_TABLE_WHERE_ALL, std::vector<ValueObject>{
1178 ValueObject(accesser.GetAccesserDeviceId()), ValueObject(accesser.GetAccesserUserId()),
1179 ValueObject(accesser.GetAccesserAccountId()), ValueObject(accesser.GetAccesserTokenId()),
1180 ValueObject(accesser.GetAccesserBundleName()), ValueObject(accesser.GetAccesserHapSignature()),
1181 ValueObject(static_cast<int32_t>(accesser.GetAccesserBindLevel())),
1182 ValueObject(accesser.GetAccesserDeviceName()), ValueObject(accesser.GetAccesserServiceName()),
1183 ValueObject(accesser.GetAccesserCredentialIdStr()),
1184 ValueObject(static_cast<int32_t>(accesser.GetAccesserStatus())),
1185 ValueObject(accesser.GetAccesserSessionKeyId())});
1186 if (resultSet == nullptr) {
1187 HILOGE("resultSet is nullptr");
1188 return DP_GET_RESULTSET_FAIL;
1189 }
1190 int32_t rowCount = ROWCOUNT_INIT;
1191 resultSet->GetRowCount(rowCount);
1192 if (rowCount > 0) {
1193 HILOGI("accesser is exists");
1194 resultSet->Close();
1195 return DP_SUCCESS;
1196 }
1197 resultSet->Close();
1198 ValuesBucket values;
1199 ProfileUtils::AccesserToEntries(profile, values);
1200 int64_t rowId = ROWID_INIT;
1201 {
1202 std::lock_guard<std::mutex> lock(rdbMutex_);
1203 if (rdbStore_ == nullptr) {
1204 HILOGE("rdbStore_ is nullptr");
1205 return DP_GET_RDBSTORE_FAIL;
1206 }
1207 int32_t ret = rdbStore_->Put(rowId, ACCESSER_TABLE, values);
1208 if (ret != DP_SUCCESS) {
1209 HILOGE("accessertable insert failed");
1210 return DP_PUT_ACCESSER_PROFILE_FAIL;
1211 }
1212 }
1213 HILOGI("PutAccesser : %{public}s", profile.GetAccesser().dump().c_str());
1214 return DP_SUCCESS;
1215 }
1216
PutAccesseeProfile(const AccessControlProfile & profile)1217 int32_t TrustProfileManager::PutAccesseeProfile(const AccessControlProfile& profile)
1218 {
1219 Accessee accessee = profile.GetAccessee();
1220 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_ACCESSEE_TABLE_WHERE_ALL, std::vector<ValueObject>{
1221 ValueObject(accessee.GetAccesseeDeviceId()), ValueObject(accessee.GetAccesseeUserId()),
1222 ValueObject(accessee.GetAccesseeAccountId()), ValueObject(accessee.GetAccesseeTokenId()),
1223 ValueObject(accessee.GetAccesseeBundleName()), ValueObject(accessee.GetAccesseeHapSignature()),
1224 ValueObject(static_cast<int32_t>(accessee.GetAccesseeBindLevel())),
1225 ValueObject(accessee.GetAccesseeDeviceName()), ValueObject(accessee.GetAccesseeServiceName()),
1226 ValueObject(accessee.GetAccesseeCredentialIdStr()),
1227 ValueObject(static_cast<int32_t>(accessee.GetAccesseeStatus())),
1228 ValueObject(accessee.GetAccesseeSessionKeyId())});
1229 if (resultSet == nullptr) {
1230 HILOGE("resultSet is nullptr");
1231 return DP_GET_RESULTSET_FAIL;
1232 }
1233 int32_t rowCount = ROWCOUNT_INIT;
1234 resultSet->GetRowCount(rowCount);
1235 if (rowCount > 0) {
1236 HILOGI("accessee is exists");
1237 resultSet->Close();
1238 return DP_SUCCESS;
1239 }
1240 resultSet->Close();
1241 ValuesBucket values;
1242 ProfileUtils::AccesseeToEntries(profile, values);
1243 int64_t rowId = ROWID_INIT;
1244 {
1245 std::lock_guard<std::mutex> lock(rdbMutex_);
1246 if (rdbStore_ == nullptr) {
1247 HILOGE("rdbStore_ is nullptr");
1248 return DP_GET_RDBSTORE_FAIL;
1249 }
1250 int32_t ret = rdbStore_->Put(rowId, ACCESSEE_TABLE, values);
1251 if (ret != DP_SUCCESS) {
1252 HILOGE("accesseetable insert failed");
1253 return DP_PUT_ACCESSEE_PROFILE_FAIL;
1254 }
1255 }
1256 HILOGI("PutAccessee : %{public}s", profile.GetAccessee().dump().c_str());
1257 return DP_SUCCESS;
1258 }
1259
SetAccessControlId(AccessControlProfile & profile)1260 int32_t TrustProfileManager::SetAccessControlId(AccessControlProfile& profile)
1261 {
1262 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_ACCESS_CONTROL_TABLE, std::vector<ValueObject> {});
1263 if (resultSet == nullptr) {
1264 HILOGE("resultSet is nullptr");
1265 return DP_GET_RESULTSET_FAIL;
1266 }
1267 int32_t rowCount = ROWCOUNT_INIT;
1268 resultSet->GetRowCount(rowCount);
1269 if (rowCount == 0) {
1270 profile.SetAccessControlId(1);
1271 resultSet->Close();
1272 return DP_SUCCESS;
1273 }
1274 int64_t accessControlId = ACCESSCONTROLID_INIT;
1275 int32_t columnIndex = COLUMNINDEX_INIT;
1276 resultSet->GoToLastRow();
1277 resultSet->GetColumnIndex(ACCESS_CONTROL_ID, columnIndex);
1278 resultSet->GetLong(columnIndex, accessControlId);
1279 resultSet->Close();
1280 profile.SetAccessControlId(accessControlId+1);
1281 return DP_SUCCESS;
1282 }
1283
SetAccesserId(AccessControlProfile & profile)1284 int32_t TrustProfileManager::SetAccesserId(AccessControlProfile& profile)
1285 {
1286 Accesser accesser = profile.GetAccesser();
1287 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_ACCESSER_TABLE_WHERE_ALL, std::vector<ValueObject>{
1288 ValueObject(accesser.GetAccesserDeviceId()), ValueObject(accesser.GetAccesserUserId()),
1289 ValueObject(accesser.GetAccesserAccountId()), ValueObject(accesser.GetAccesserTokenId()),
1290 ValueObject(accesser.GetAccesserBundleName()), ValueObject(accesser.GetAccesserHapSignature()),
1291 ValueObject(static_cast<int32_t>(accesser.GetAccesserBindLevel())),
1292 ValueObject(accesser.GetAccesserDeviceName()), ValueObject(accesser.GetAccesserServiceName()),
1293 ValueObject(accesser.GetAccesserCredentialIdStr()),
1294 ValueObject(static_cast<int32_t>(accesser.GetAccesserStatus())),
1295 ValueObject(accesser.GetAccesserSessionKeyId())});
1296 if (resultSet == nullptr) {
1297 HILOGE("resultSet is nullptr");
1298 return DP_GET_RESULTSET_FAIL;
1299 }
1300 int32_t rowCount = ROWCOUNT_INIT;
1301 int64_t accesserId = ACCESSERID_INIT;
1302 int32_t columnIndex = COLUMNINDEX_INIT;
1303 resultSet->GetRowCount(rowCount);
1304 if (rowCount > 0) {
1305 resultSet->GoToFirstRow();
1306 resultSet->GetColumnIndex(ACCESSER_ID, columnIndex);
1307 resultSet->GetLong(columnIndex, accesserId);
1308 profile.SetAccesserId(accesserId);
1309 resultSet->Close();
1310 return DP_SUCCESS;
1311 }
1312 resultSet->Close();
1313 resultSet = GetResultSet(SELECT_ACCESSER_TABLE, std::vector<ValueObject> {});
1314 if (resultSet == nullptr) {
1315 HILOGE("resultSet is nullptr");
1316 return DP_GET_RESULTSET_FAIL;
1317 }
1318 resultSet->GetRowCount(rowCount);
1319 if (rowCount == 0) {
1320 profile.GetAccesser().SetAccesserId(1);
1321 profile.SetAccesserId(1);
1322 resultSet->Close();
1323 return DP_SUCCESS;
1324 }
1325 resultSet->GoToLastRow();
1326 resultSet->GetColumnIndex(ACCESSER_ID, columnIndex);
1327 resultSet->GetLong(columnIndex, accesserId);
1328 resultSet->Close();
1329 accesserId = accesserId + 1;
1330 profile.SetAccesserId(accesserId);
1331 return DP_SUCCESS;
1332 }
1333
SetAccesseeId(AccessControlProfile & profile)1334 int32_t TrustProfileManager::SetAccesseeId(AccessControlProfile& profile)
1335 {
1336 Accessee accessee = profile.GetAccessee();
1337 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_ACCESSEE_TABLE_WHERE_ALL, std::vector<ValueObject>{
1338 ValueObject(accessee.GetAccesseeDeviceId()), ValueObject(accessee.GetAccesseeUserId()),
1339 ValueObject(accessee.GetAccesseeAccountId()), ValueObject(accessee.GetAccesseeTokenId()),
1340 ValueObject(accessee.GetAccesseeBundleName()), ValueObject(accessee.GetAccesseeHapSignature()),
1341 ValueObject(static_cast<int32_t>(accessee.GetAccesseeBindLevel())),
1342 ValueObject(accessee.GetAccesseeDeviceName()), ValueObject(accessee.GetAccesseeServiceName()),
1343 ValueObject(accessee.GetAccesseeCredentialIdStr()),
1344 ValueObject(static_cast<int32_t>(accessee.GetAccesseeStatus())),
1345 ValueObject(accessee.GetAccesseeSessionKeyId())});
1346 if (resultSet == nullptr) {
1347 HILOGE("resultSet is nullptr");
1348 return DP_GET_RESULTSET_FAIL;
1349 }
1350 int32_t rowCount = ROWCOUNT_INIT;
1351 int64_t accesseeId = ACCESSEEID_INIT;
1352 int32_t columnIndex = COLUMNINDEX_INIT;
1353 resultSet->GetRowCount(rowCount);
1354 if (rowCount > 0) {
1355 resultSet->GoToFirstRow();
1356 resultSet->GetColumnIndex(ACCESSEE_ID, columnIndex);
1357 resultSet->GetLong(columnIndex, accesseeId);
1358 profile.SetAccesseeId(accesseeId);
1359 resultSet->Close();
1360 return DP_SUCCESS;
1361 }
1362 resultSet->Close();
1363 resultSet = GetResultSet(SELECT_ACCESSEE_TABLE, std::vector<ValueObject> {});
1364 if (resultSet == nullptr) {
1365 HILOGE("resultSet is nullptr");
1366 return DP_GET_RESULTSET_FAIL;
1367 }
1368 resultSet->GetRowCount(rowCount);
1369 if (rowCount == 0) {
1370 profile.GetAccessee().SetAccesseeId(1);
1371 profile.SetAccesseeId(1);
1372 resultSet->Close();
1373 return DP_SUCCESS;
1374 }
1375 resultSet->GoToLastRow();
1376 resultSet->GetColumnIndex(ACCESSEE_ID, columnIndex);
1377 resultSet->GetLong(columnIndex, accesseeId);
1378 resultSet->Close();
1379 accesseeId = accesseeId + 1;
1380 profile.SetAccesseeId(accesseeId);
1381 return DP_SUCCESS;
1382 }
1383
UpdateAccesserProfile(const AccessControlProfile & profile)1384 int32_t TrustProfileManager::UpdateAccesserProfile(const AccessControlProfile& profile)
1385 {
1386 ValuesBucket values;
1387 ProfileUtils::AccesserToEntries(profile, values);
1388 int32_t changeRowCnt = CHANGEROWCNT_INIT;
1389 {
1390 std::lock_guard<std::mutex> lock(rdbMutex_);
1391 if (rdbStore_ == nullptr) {
1392 HILOGE("rdbStore_ is nullptr");
1393 return DP_GET_RDBSTORE_FAIL;
1394 }
1395 int32_t ret = rdbStore_->Update(changeRowCnt, ACCESSER_TABLE, values, ACCESSERID_EQUAL_CONDITION,
1396 std::vector<ValueObject> {ValueObject(profile.GetAccesserId())});
1397 if (ret != DP_SUCCESS) {
1398 HILOGE("accessertable update failed");
1399 return DP_UPDATE_ACCESSER_PROFILE_FAIL;
1400 }
1401 }
1402 HILOGI("UpdateAccesser : %{public}s", profile.GetAccesser().dump().c_str());
1403 return DP_SUCCESS;
1404 }
1405
UpdateAccesseeProfile(const AccessControlProfile & profile)1406 int32_t TrustProfileManager::UpdateAccesseeProfile(const AccessControlProfile& profile)
1407 {
1408 ValuesBucket values;
1409 ProfileUtils::AccesseeToEntries(profile, values);
1410 int32_t changeRowCnt = CHANGEROWCNT_INIT;
1411 {
1412 std::lock_guard<std::mutex> lock(rdbMutex_);
1413 if (rdbStore_ == nullptr) {
1414 HILOGE("rdbStore_ is nullptr");
1415 return DP_GET_RDBSTORE_FAIL;
1416 }
1417 int32_t ret = rdbStore_->Update(changeRowCnt, ACCESSEE_TABLE, values, ACCESSEEID_EQUAL_CONDITION,
1418 std::vector<ValueObject>{ ValueObject(profile.GetAccesseeId()) });
1419 if (ret != DP_SUCCESS) {
1420 HILOGE("accesseetable update failed");
1421 return DP_UPDATE_ACCESSEE_PROFILE_FAIL;
1422 }
1423 }
1424 HILOGI("UpdateAccessee : %{public}s", profile.GetAccessee().dump().c_str());
1425 return DP_SUCCESS;
1426 }
1427
UpdateTrustDeviceProfileNotify(const TrustDeviceProfile & oldProfile,const TrustDeviceProfile & newProfile)1428 int32_t TrustProfileManager::UpdateTrustDeviceProfileNotify(const TrustDeviceProfile& oldProfile,
1429 const TrustDeviceProfile &newProfile)
1430 {
1431 if (oldProfile.GetStatus() != newProfile.GetStatus()) {
1432 int32_t ret = SubscribeProfileManager::GetInstance().NotifyTrustDeviceProfileUpdate(oldProfile, newProfile);
1433 if (ret != DP_SUCCESS) {
1434 HILOGE("NotifyTrustDeviceProfileUpdate failed");
1435 return DP_NOTIFY_TRUST_DEVICE_FAIL;
1436 }
1437 }
1438 return DP_SUCCESS;
1439 }
1440
GetResultStatus(const std::string & trustDeviceId,int32_t & trustDeviceStatus)1441 int32_t TrustProfileManager::GetResultStatus(const std::string& trustDeviceId, int32_t& trustDeviceStatus)
1442 {
1443 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_ACCESS_CONTROL_TABLE_WHERE_TRUSTDEVICEID,
1444 std::vector<ValueObject>{ ValueObject(trustDeviceId) });
1445 if (resultSet == nullptr) {
1446 HILOGE("resultSet is nullptr");
1447 return DP_GET_RESULTSET_FAIL;
1448 }
1449 int32_t rowCount = ROWCOUNT_INIT;
1450 resultSet->GetRowCount(rowCount);
1451 if (rowCount == 0) {
1452 HILOGE("trustDeviceId not find");
1453 resultSet->Close();
1454 return DP_NOT_FIND_DATA;
1455 }
1456 int32_t columnIndex = COLUMNINDEX_INIT;
1457 trustDeviceStatus = 0;
1458 while (resultSet->GoToNextRow() == DP_SUCCESS) {
1459 int32_t status = STATUS_INIT;
1460 resultSet->GetColumnIndex(STATUS, columnIndex);
1461 resultSet->GetInt(columnIndex, status);
1462 if (status == 1) {
1463 trustDeviceStatus = 1;
1464 break;
1465 }
1466 }
1467 resultSet->Close();
1468 return DP_SUCCESS;
1469 }
1470
GetAccessControlProfile(std::shared_ptr<ResultSet> resultSet,int64_t accesserId,int64_t accesseeId,std::vector<AccessControlProfile> & profile)1471 int32_t TrustProfileManager::GetAccessControlProfile(std::shared_ptr<ResultSet> resultSet,
1472 int64_t accesserId, int64_t accesseeId, std::vector<AccessControlProfile>& profile)
1473 {
1474 std::shared_ptr<ResultSet> accesserResultSet = GetResultSet(SELECT_ACCESSER_TABLE_WHERE_ACCESSERID,
1475 std::vector<ValueObject>{ ValueObject(accesserId) });
1476 if (accesserResultSet == nullptr) {
1477 HILOGE("accesserResultSet is nullptr");
1478 return DP_GET_RESULTSET_FAIL;
1479 }
1480 int32_t rowCount = ROWCOUNT_INIT;
1481 accesserResultSet->GetRowCount(rowCount);
1482 if (rowCount == 0) {
1483 HILOGE("not find data");
1484 accesserResultSet->Close();
1485 return DP_NOT_FIND_DATA;
1486 }
1487 std::shared_ptr<ResultSet> accesseeResultSet = GetResultSet(SELECT_ACCESSEE_TABLE_WHERE_ACCESSEEID,
1488 std::vector<ValueObject>{ ValueObject(accesseeId) });
1489 if (accesseeResultSet == nullptr) {
1490 HILOGE("accesseeResultSet is nullptr");
1491 return DP_GET_RESULTSET_FAIL;
1492 }
1493 accesseeResultSet->GetRowCount(rowCount);
1494 if (rowCount == 0) {
1495 HILOGE("not find data");
1496 accesserResultSet->Close();
1497 accesseeResultSet->Close();
1498 return DP_NOT_FIND_DATA;
1499 }
1500 ProfileUtils::ConvertToAccessControlProfiles(resultSet, accesserResultSet, accesseeResultSet, profile);
1501 accesserResultSet->Close();
1502 accesseeResultSet->Close();
1503 return DP_SUCCESS;
1504 }
1505
GetAccessControlProfilesByDeviceId(std::shared_ptr<ResultSet> resultSet,int64_t accesserId,int64_t accesseeId,const std::string & trustDeviceId,std::vector<AccessControlProfile> & profile)1506 int32_t TrustProfileManager::GetAccessControlProfilesByDeviceId(
1507 std::shared_ptr<ResultSet> resultSet, int64_t accesserId, int64_t accesseeId,
1508 const std::string& trustDeviceId, std::vector<AccessControlProfile>& profile)
1509 {
1510 std::shared_ptr<ResultSet> accesserResultSet =
1511 GetResultSet(SELECT_ACCESSER_TABLE_WHERE_ACCESSERID_AND_ACCESSERDEVICEID,
1512 std::vector<ValueObject>{ ValueObject(accesserId), ValueObject(trustDeviceId) });
1513 if (accesserResultSet == nullptr) {
1514 HILOGE("accesserResultSet is nullptr");
1515 return DP_GET_RESULTSET_FAIL;
1516 }
1517 int32_t rowCount = ROWCOUNT_INIT;
1518 accesserResultSet->GetRowCount(rowCount);
1519 if (rowCount != 0) {
1520 std::shared_ptr<ResultSet> accesseeResultSet = GetResultSet(SELECT_ACCESSEE_TABLE_WHERE_ACCESSEEID,
1521 std::vector<ValueObject>{ ValueObject(accesseeId) });
1522 if (accesseeResultSet == nullptr) {
1523 HILOGE("accesseeResultSet is nullptr");
1524 return DP_GET_RESULTSET_FAIL;
1525 }
1526 ProfileUtils::ConvertToAccessControlProfiles(resultSet, accesserResultSet, accesseeResultSet, profile);
1527 accesserResultSet->Close();
1528 accesseeResultSet->Close();
1529 return DP_SUCCESS;
1530 }
1531 accesserResultSet->Close();
1532
1533 std::shared_ptr<ResultSet> accesseeResultSet =
1534 GetResultSet(SELECT_ACCESSEE_TABLE_WHERE_ACCESSEEID_AND_ACCESSEEDEVICEID,
1535 std::vector<ValueObject>{ ValueObject(accesseeId), ValueObject(trustDeviceId) });
1536 if (accesseeResultSet == nullptr) {
1537 HILOGE("accesseeResultSet is nullptr");
1538 return DP_GET_RESULTSET_FAIL;
1539 }
1540 accesseeResultSet->GetRowCount(rowCount);
1541 if (rowCount != 0) {
1542 accesserResultSet = GetResultSet(SELECT_ACCESSER_TABLE_WHERE_ACCESSERID,
1543 std::vector<ValueObject>{ ValueObject(accesserId) });
1544 if (accesserResultSet == nullptr) {
1545 HILOGE("accesserResultSet is nullptr");
1546 return DP_GET_RESULTSET_FAIL;
1547 }
1548 ProfileUtils::ConvertToAccessControlProfiles(resultSet, accesserResultSet, accesseeResultSet, profile);
1549 accesserResultSet->Close();
1550 }
1551 accesseeResultSet->Close();
1552 return DP_SUCCESS;
1553 }
1554
DeleteAccessControlProfileCheck(AccessControlProfile & profile)1555 int32_t TrustProfileManager::DeleteAccessControlProfileCheck(AccessControlProfile& profile)
1556 {
1557 Accessee accessee;
1558 int32_t ret = this->DeleteAccesseeCheck(profile.GetAccesseeId(), accessee);
1559 if (ret != DP_SUCCESS) {
1560 HILOGE("DeleteAccesseeCheck failed");
1561 return ret;
1562 }
1563 Accesser accesser;
1564 ret = this->DeleteAccesserCheck(profile.GetAccesserId(), accesser);
1565 if (ret != DP_SUCCESS) {
1566 HILOGE("DeleteAccesserCheck failed");
1567 return ret;
1568 }
1569 {
1570 std::lock_guard<std::mutex> lock(rdbMutex_);
1571 if (rdbStore_ == nullptr) {
1572 HILOGE("rdbStore_ is nullptr");
1573 return DP_GET_RDBSTORE_FAIL;
1574 }
1575 int32_t deleteRows = DELETEROWS_INIT;
1576 ret = rdbStore_->Delete(deleteRows, ACCESS_CONTROL_TABLE, ACCESSCONTROLID_EQUAL_CONDITION,
1577 std::vector<ValueObject>{ ValueObject(profile.GetAccessControlId()) });
1578 if (ret != DP_SUCCESS) {
1579 HILOGE("delete accesscontrol_table failed");
1580 return DP_DELETE_ACCESS_CONTROL_PROFILE_FAIL;
1581 }
1582 }
1583 HILOGI("DeleteAclProfile : %{public}s", profile.dump().c_str());
1584 AccessControlProfile resultProfile(profile);
1585 resultProfile.SetAccesser(accesser);
1586 resultProfile.SetAccessee(accessee);
1587 profile = resultProfile;
1588 return DP_SUCCESS;
1589 }
1590
GetResultSet(const std::string & sql,std::vector<ValueObject> condition)1591 std::shared_ptr<ResultSet> TrustProfileManager::GetResultSet(
1592 const std::string& sql, std::vector<ValueObject> condition)
1593 {
1594 if (sql.empty() || sql.length() > MAX_STRING_LEN) {
1595 HILOGE("sql is invalid");
1596 return nullptr;
1597 }
1598 if (condition.size() > MAX_PARAM_SIZE) {
1599 HILOGE("condition is invalid");
1600 return nullptr;
1601 }
1602 std::lock_guard<std::mutex> lock(rdbMutex_);
1603 if (rdbStore_ == nullptr) {
1604 HILOGE("rdbStore_ is nullptr");
1605 return nullptr;
1606 }
1607 return rdbStore_->Get(sql, condition);
1608 }
1609
SetAccessControlProfileId(AccessControlProfile & accessControlProfile)1610 int32_t TrustProfileManager::SetAccessControlProfileId(AccessControlProfile& accessControlProfile)
1611 {
1612 int32_t ret = this->SetAccessControlId(accessControlProfile);
1613 if (ret != DP_SUCCESS) {
1614 HILOGE("SetAccessControlId failed");
1615 return ret;
1616 }
1617 ret = this->SetAccesserId(accessControlProfile);
1618 if (ret != DP_SUCCESS) {
1619 HILOGE("SetAccesserId failed");
1620 return ret;
1621 }
1622 ret = this->SetAccesseeId(accessControlProfile);
1623 if (ret != DP_SUCCESS) {
1624 HILOGE("SetAccesseeId failed");
1625 return ret;
1626 }
1627 Accesser accesser(accessControlProfile.GetAccesser());
1628 accesser.SetAccesserId(accessControlProfile.GetAccesserId());
1629 accessControlProfile.SetAccesser(accesser);
1630 Accessee accessee(accessControlProfile.GetAccessee());
1631 accessee.SetAccesseeId(accessControlProfile.GetAccesseeId());
1632 accessControlProfile.SetAccessee(accessee);
1633 return DP_SUCCESS;
1634 }
1635
GetAccessControlProfiles(std::shared_ptr<ResultSet> resultSet,int64_t accesserId,int64_t accesseeId,int32_t userId,const std::string & bundleName,std::vector<AccessControlProfile> & profile)1636 int32_t TrustProfileManager::GetAccessControlProfiles(std::shared_ptr<ResultSet> resultSet,
1637 int64_t accesserId, int64_t accesseeId, int32_t userId, const std::string& bundleName,
1638 std::vector<AccessControlProfile>& profile)
1639 {
1640 std::shared_ptr<ResultSet> accesserResultSet =
1641 GetResultSet(SELECT_ACCESSER_TABLE_WHERE_ACCESSERID_AND_ACCESSERUSERID_ACCESSERBUNDLENAME,
1642 std::vector<ValueObject>{ ValueObject(accesserId), ValueObject(userId), ValueObject(bundleName) });
1643 if (accesserResultSet == nullptr) {
1644 HILOGE("accesserResultSet is nullptr");
1645 return DP_GET_RESULTSET_FAIL;
1646 }
1647 int32_t rowCount = ROWCOUNT_INIT;
1648 accesserResultSet->GetRowCount(rowCount);
1649 if (rowCount != 0) {
1650 std::shared_ptr<ResultSet> accesseeResultSet = GetResultSet(SELECT_ACCESSEE_TABLE_WHERE_ACCESSEEID,
1651 std::vector<ValueObject>{ ValueObject(accesseeId) });
1652 if (accesseeResultSet == nullptr) {
1653 HILOGE("accesseeResultSet is nullptr");
1654 return DP_GET_RESULTSET_FAIL;
1655 }
1656 ProfileUtils::ConvertToAccessControlProfiles(resultSet, accesserResultSet, accesseeResultSet, profile);
1657 accesserResultSet->Close();
1658 accesseeResultSet->Close();
1659 return DP_SUCCESS;
1660 }
1661 accesserResultSet->Close();
1662
1663 std::shared_ptr<ResultSet> accesseeResultSet =
1664 GetResultSet(SELECT_ACCESSEE_TABLE_WHERE_ACCESSEEID_AND_ACCESSEEUSEEID_ACCESSEEBUNDLENAME,
1665 std::vector<ValueObject>{ ValueObject(accesseeId), ValueObject(userId), ValueObject(bundleName) });
1666 if (accesseeResultSet == nullptr) {
1667 HILOGE("accesseeResultSet is nullptr");
1668 return DP_GET_RESULTSET_FAIL;
1669 }
1670 accesseeResultSet->GetRowCount(rowCount);
1671 if (rowCount != 0) {
1672 accesserResultSet = GetResultSet(SELECT_ACCESSER_TABLE_WHERE_ACCESSERID,
1673 std::vector<ValueObject>{ ValueObject(accesserId) });
1674 if (accesserResultSet == nullptr) {
1675 HILOGE("accesserResultSet is nullptr");
1676 return DP_GET_RESULTSET_FAIL;
1677 }
1678 ProfileUtils::ConvertToAccessControlProfiles(resultSet, accesserResultSet, accesseeResultSet, profile);
1679 accesserResultSet->Close();
1680 }
1681 accesseeResultSet->Close();
1682 return DP_SUCCESS;
1683 }
1684
GetAccessControlProfiles(std::shared_ptr<ResultSet> resultSet,int64_t accesserId,int64_t accesseeId,int32_t userId,std::vector<AccessControlProfile> & profile)1685 int32_t TrustProfileManager::GetAccessControlProfiles(std::shared_ptr<ResultSet> resultSet, int64_t accesserId,
1686 int64_t accesseeId, int32_t userId, std::vector<AccessControlProfile>& profile)
1687 {
1688 std::shared_ptr<ResultSet> accesserResultSet =
1689 GetResultSet(SELECT_ACCESSER_TABLE_WHERE_ACCESSERID_AND_ACCESSERUSERID,
1690 std::vector<ValueObject>{ ValueObject(accesserId), ValueObject(userId) });
1691 if (accesserResultSet == nullptr) {
1692 HILOGE("accesserResultSet is nullptr");
1693 return DP_GET_RESULTSET_FAIL;
1694 }
1695 int32_t rowCount = ROWCOUNT_INIT;
1696 accesserResultSet->GetRowCount(rowCount);
1697 if (rowCount != 0) {
1698 std::shared_ptr<ResultSet> accesseeResultSet = GetResultSet(SELECT_ACCESSEE_TABLE_WHERE_ACCESSEEID,
1699 std::vector<ValueObject>{ ValueObject(accesseeId) });
1700 if (accesseeResultSet == nullptr) {
1701 HILOGE("accesseeResultSet is nullptr");
1702 return DP_GET_RESULTSET_FAIL;
1703 }
1704 ProfileUtils::ConvertToAccessControlProfiles(resultSet, accesserResultSet, accesseeResultSet, profile);
1705 accesserResultSet->Close();
1706 accesseeResultSet->Close();
1707 return DP_SUCCESS;
1708 }
1709 accesserResultSet->Close();
1710
1711 std::shared_ptr<ResultSet> accesseeResultSet =
1712 GetResultSet(SELECT_ACCESSEE_TABLE_WHERE_ACCESSEEID_AND_ACCESSEEUSERID,
1713 std::vector<ValueObject>{ ValueObject(accesseeId), ValueObject(userId) });
1714 if (accesseeResultSet == nullptr) {
1715 HILOGE("accesseeResultSet is nullptr");
1716 return DP_GET_RESULTSET_FAIL;
1717 }
1718 accesseeResultSet->GetRowCount(rowCount);
1719 if (rowCount != 0) {
1720 accesserResultSet = GetResultSet(SELECT_ACCESSER_TABLE_WHERE_ACCESSERID,
1721 std::vector<ValueObject>{ ValueObject(accesserId) });
1722 if (accesserResultSet == nullptr) {
1723 HILOGE("accesserResultSet is nullptr");
1724 return DP_GET_RESULTSET_FAIL;
1725 }
1726 ProfileUtils::ConvertToAccessControlProfiles(resultSet, accesserResultSet, accesseeResultSet, profile);
1727 accesserResultSet->Close();
1728 }
1729 accesseeResultSet->Close();
1730 return DP_SUCCESS;
1731 }
1732
GetAccessControlProfiles(std::shared_ptr<ResultSet> resultSet,int64_t accesserId,int64_t accesseeId,const std::string & bundleName,std::vector<AccessControlProfile> & profile)1733 int32_t TrustProfileManager::GetAccessControlProfiles(std::shared_ptr<ResultSet> resultSet, int64_t accesserId,
1734 int64_t accesseeId, const std::string& bundleName, std::vector<AccessControlProfile>& profile)
1735 {
1736 std::shared_ptr<ResultSet> accesserResultSet =
1737 GetResultSet(SELECT_ACCESSER_TABLE_WHERE_ACCESSERID_AND_ACCESSERBUNDLENAME,
1738 std::vector<ValueObject>{ ValueObject(accesserId), ValueObject(bundleName) });
1739 if (accesserResultSet == nullptr) {
1740 HILOGE("accesserResultSet is nullptr");
1741 return DP_GET_RESULTSET_FAIL;
1742 }
1743 int32_t rowCount = ROWCOUNT_INIT;
1744 accesserResultSet->GetRowCount(rowCount);
1745 if (rowCount != 0) {
1746 std::shared_ptr<ResultSet> accesseeResultSet = GetResultSet(SELECT_ACCESSEE_TABLE_WHERE_ACCESSEEID,
1747 std::vector<ValueObject>{ ValueObject(accesseeId) });
1748 if (accesseeResultSet == nullptr) {
1749 HILOGE("accesseeResultSet is nullptr");
1750 return DP_GET_RESULTSET_FAIL;
1751 }
1752 ProfileUtils::ConvertToAccessControlProfiles(resultSet, accesserResultSet, accesseeResultSet, profile);
1753 accesserResultSet->Close();
1754 accesseeResultSet->Close();
1755 return DP_SUCCESS;
1756 }
1757 accesserResultSet->Close();
1758
1759 std::shared_ptr<ResultSet> accesseeResultSet =
1760 GetResultSet(SELECT_ACCESSEE_TABLE_WHERE_ACCESSEEID_AND_ACCESSEEBUNDLENAME,
1761 std::vector<ValueObject>{ ValueObject(accesseeId), ValueObject(bundleName) });
1762 if (accesseeResultSet == nullptr) {
1763 HILOGE("accesseeResultSet is nullptr");
1764 return DP_GET_RESULTSET_FAIL;
1765 }
1766 accesseeResultSet->GetRowCount(rowCount);
1767 if (rowCount != 0) {
1768 accesserResultSet = GetResultSet(SELECT_ACCESSER_TABLE_WHERE_ACCESSERID,
1769 std::vector<ValueObject>{ ValueObject(accesserId) });
1770 if (accesserResultSet == nullptr) {
1771 HILOGE("accesserResultSet is nullptr");
1772 return DP_GET_RESULTSET_FAIL;
1773 }
1774 ProfileUtils::ConvertToAccessControlProfiles(resultSet, accesserResultSet, accesseeResultSet, profile);
1775 accesserResultSet->Close();
1776 }
1777 accesseeResultSet->Close();
1778 return DP_SUCCESS;
1779 }
1780
GetAccessControlProfilesByTokenId(std::shared_ptr<ResultSet> resultSet,int64_t accesserId,int64_t accesseeId,const std::string & trustDeviceId,int64_t tokenId,std::vector<AccessControlProfile> & profile)1781 int32_t TrustProfileManager::GetAccessControlProfilesByTokenId(std::shared_ptr<ResultSet> resultSet,
1782 int64_t accesserId, int64_t accesseeId, const std::string& trustDeviceId,
1783 int64_t tokenId, std::vector<AccessControlProfile> &profile)
1784 {
1785 std::shared_ptr<ResultSet> accesserResultSet =
1786 GetResultSet(SELECT_ACCESSER_TABLE_WHERE_ACCESSERID_AND_ACCESSERDEVICEID,
1787 std::vector<ValueObject>{ ValueObject(accesserId), ValueObject(trustDeviceId) });
1788 if (accesserResultSet == nullptr) {
1789 HILOGE("accesserResultSet is nullptr");
1790 return DP_GET_RESULTSET_FAIL;
1791 }
1792 int32_t rowCount = ROWCOUNT_INIT;
1793 accesserResultSet->GetRowCount(rowCount);
1794 if (rowCount != 0) {
1795 std::shared_ptr<ResultSet> accesseeResultSet =
1796 GetResultSet(SELECT_ACCESSEE_TABLE_WHERE_ACCESSEEID_AND_ACCESSEETOKENID,
1797 std::vector<ValueObject>{ ValueObject(accesseeId), ValueObject(tokenId) });
1798 if (accesseeResultSet == nullptr) {
1799 HILOGE("accesseeResultSet is nullptr");
1800 return DP_GET_RESULTSET_FAIL;
1801 }
1802 ProfileUtils::ConvertToAccessControlProfiles(resultSet, accesserResultSet, accesseeResultSet, profile);
1803 accesserResultSet->Close();
1804 accesseeResultSet->Close();
1805 return DP_SUCCESS;
1806 }
1807 accesserResultSet->Close();
1808
1809 std::shared_ptr<ResultSet> accesseeResultSet =
1810 GetResultSet(SELECT_ACCESSEE_TABLE_WHERE_ACCESSEEID_AND_ACCESSEEDEVICEID,
1811 std::vector<ValueObject>{ ValueObject(accesseeId), ValueObject(trustDeviceId) });
1812 if (accesseeResultSet == nullptr) {
1813 HILOGE("accesseeResultSet is nullptr");
1814 return DP_GET_RESULTSET_FAIL;
1815 }
1816 accesseeResultSet->GetRowCount(rowCount);
1817 if (rowCount != 0) {
1818 accesserResultSet = GetResultSet(SELECT_ACCESSER_TABLE_WHERE_ACCESSERID_AND_ACCESSERTOKENID,
1819 std::vector<ValueObject>{ ValueObject(accesserId), ValueObject(tokenId) });
1820 if (accesserResultSet == nullptr) {
1821 HILOGE("accesserResultSet is nullptr");
1822 return DP_GET_RESULTSET_FAIL;
1823 }
1824 ProfileUtils::ConvertToAccessControlProfiles(resultSet, accesserResultSet, accesseeResultSet, profile);
1825 accesserResultSet->Close();
1826 }
1827 accesseeResultSet->Close();
1828 return DP_SUCCESS;
1829 }
1830
DeleteAccesserCheck(int64_t accesserId,Accesser & accesser)1831 int32_t TrustProfileManager::DeleteAccesserCheck(int64_t accesserId, Accesser& accesser)
1832 {
1833 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_ACCESS_CONTROL_TABLE_WHERE_ACCESSERID,
1834 std::vector<ValueObject>{ ValueObject(accesserId) });
1835 if (resultSet == nullptr) {
1836 HILOGE("resultSet is nullptr");
1837 return DP_GET_RESULTSET_FAIL;
1838 }
1839 int32_t rowCount = ROWCOUNT_INIT;
1840 resultSet->GetRowCount(rowCount);
1841 resultSet->Close();
1842 resultSet = GetResultSet(SELECT_ACCESSER_TABLE_WHERE_ACCESSERID,
1843 std::vector<ValueObject>{ ValueObject(accesserId) });
1844 if (resultSet == nullptr) {
1845 HILOGE("resultSet is nullptr");
1846 return DP_GET_RESULTSET_FAIL;
1847 }
1848 resultSet->GoToNextRow();
1849 ProfileUtils::ConvertToAccesser(resultSet, accesser);
1850 resultSet->Close();
1851 if (rowCount == DELETE_ACCESSER_CONDITION) {
1852 std::lock_guard<std::mutex> lock(rdbMutex_);
1853 if (rdbStore_ == nullptr) {
1854 HILOGE("rdbStore_ is nullptr");
1855 return DP_GET_RDBSTORE_FAIL;
1856 }
1857 int32_t deleteRows = DELETEROWS_INIT;
1858 int32_t ret = rdbStore_->Delete(deleteRows, ACCESSER_TABLE, ACCESSERID_EQUAL_CONDITION,
1859 std::vector<ValueObject>{ ValueObject(accesserId) });
1860 if (ret != DP_SUCCESS) {
1861 HILOGE("delete accessertable accesserId failed");
1862 return DP_DELETE_ACCESSER_PROFILE_FAIL;
1863 }
1864 HILOGI("DeleteAccesser : %{public}s", accesser.dump().c_str());
1865 }
1866 return DP_SUCCESS;
1867 }
1868
UpdateAclCheck(const AccessControlProfile & profile,AccessControlProfile & oldProfile)1869 int32_t TrustProfileManager::UpdateAclCheck(const AccessControlProfile& profile, AccessControlProfile& oldProfile)
1870 {
1871 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_ACCESS_CONTROL_TABLE_WHERE_ACCESSCONTROLID,
1872 std::vector<ValueObject>{ ValueObject(profile.GetAccessControlId()) });
1873 if (resultSet == nullptr) {
1874 HILOGE("resultSet is nullptr");
1875 return DP_GET_RESULTSET_FAIL;
1876 }
1877 int32_t rowCount = ROWCOUNT_INIT;
1878 resultSet->GetRowCount(rowCount);
1879 if (rowCount == 0) {
1880 HILOGE("accessControlId not find");
1881 resultSet->Close();
1882 return DP_NOT_FIND_DATA;
1883 }
1884 resultSet->GoToNextRow();
1885 ProfileUtils::ConvertToAccessControlProfile(resultSet, oldProfile);
1886 resultSet->Close();
1887 if (oldProfile.GetAccesseeId() != profile.GetAccessee().GetAccesseeId() ||
1888 oldProfile.GetAccesserId() != profile.GetAccesser().GetAccesserId() ||
1889 oldProfile.GetAccesserId() != profile.GetAccesserId() ||
1890 oldProfile.GetAccesseeId() != profile.GetAccesseeId()) {
1891 HILOGE("UpdateAclCheck:Can't Update not allowed attribute");
1892 return DP_UPDATE_ACL_NOT_ALLOW;
1893 }
1894 return DP_SUCCESS;
1895 }
1896
PutAclCheck(const AccessControlProfile & profile,bool peerDevInfoExists)1897 int32_t TrustProfileManager::PutAclCheck(const AccessControlProfile& profile, bool peerDevInfoExists)
1898 {
1899 TrustDeviceProfile trustProfile;
1900 ProfileUtils::ConvertToTrustDeviceProfile(profile, trustProfile);
1901 if (!peerDevInfoExists && !IsLnnAcl(profile)) {
1902 int32_t ret = SubscribeProfileManager::GetInstance().NotifyTrustDeviceProfileAdd(trustProfile);
1903 if (ret != DP_SUCCESS) {
1904 HILOGE("NotifyTrustDeviceProfileAdd failed");
1905 return DP_NOTIFY_TRUST_DEVICE_FAIL;
1906 }
1907 }
1908 std::string trustDeviceId = profile.GetTrustDeviceId();
1909 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_TRUST_DEVICE_TABLE_WHERE_DEVICEID,
1910 std::vector<ValueObject>{ ValueObject(trustDeviceId) });
1911 if (resultSet == nullptr) {
1912 HILOGE("resultSet is nullptr");
1913 return DP_GET_RESULTSET_FAIL;
1914 }
1915 int32_t rowCount = ROWCOUNT_INIT;
1916 resultSet->GetRowCount(rowCount);
1917 resultSet->Close();
1918 if (rowCount == 0) {
1919 this->PutTrustDeviceProfile(trustProfile);
1920 return DP_SUCCESS;
1921 }
1922 int32_t status = STATUS_INIT;
1923 if (this->GetResultStatus(trustDeviceId, status) != DP_SUCCESS) {
1924 HILOGE("GetResultStatus failed");
1925 return DP_GET_RESULTSET_FAIL;
1926 }
1927 trustProfile.SetStatus(status);
1928 if (this->UpdateTrustDeviceProfile(trustProfile) != DP_SUCCESS) {
1929 HILOGE("UpdateTrustDeviceProfile failed");
1930 return DP_UPDATE_TRUST_DEVICE_PROFILE_FAIL;
1931 }
1932 return DP_SUCCESS;
1933 }
1934
IsAclExists(const AccessControlProfile & profile)1935 int32_t TrustProfileManager::IsAclExists(const AccessControlProfile &profile)
1936 {
1937 std::shared_ptr<ResultSet> resultSet =
1938 GetResultSet(SELECT_ACCESS_CONTROL_TABLE_WHERE_ALL_EXCEPT_STATUS, std::vector<ValueObject>{
1939 ValueObject(profile.GetAccesserId()), ValueObject(profile.GetAccesseeId()),
1940 ValueObject(profile.GetTrustDeviceId()), ValueObject(profile.GetSessionKey()),
1941 ValueObject(static_cast<int32_t>(profile.GetBindType())),
1942 ValueObject(static_cast<int32_t>(profile.GetAuthenticationType())),
1943 ValueObject(static_cast<int32_t>(profile.GetDeviceIdType())),
1944 ValueObject(profile.GetDeviceIdHash()), ValueObject(profile.GetValidPeriod()),
1945 ValueObject(profile.GetLastAuthTime()), ValueObject(static_cast<int32_t>(profile.GetBindLevel()))});
1946 if (resultSet == nullptr) {
1947 HILOGE("resultSet is nullptr");
1948 return DP_GET_RESULTSET_FAIL;
1949 }
1950 int32_t rowCount = ROWCOUNT_INIT;
1951 resultSet->GetRowCount(rowCount);
1952 resultSet->Close();
1953 if (rowCount != 0) {
1954 HILOGI("accessControlProfile is exists");
1955 return DP_DATA_EXISTS;
1956 }
1957 return DP_SUCCESS;
1958 }
1959
CheckDeviceIdAndUserIdActive(const AccessControlProfile & profile,int32_t & resultCount)1960 int32_t TrustProfileManager::CheckDeviceIdAndUserIdActive(const AccessControlProfile& profile, int32_t& resultCount)
1961 {
1962 std::string peerDeviceId = profile.GetTrustDeviceId();
1963 std::string localDeviceId = profile.GetAccessee().GetAccesseeDeviceId();
1964 int32_t peerUserId = profile.GetAccesser().GetAccesserUserId();
1965 int32_t localUserId = profile.GetAccessee().GetAccesseeUserId();
1966 if (profile.GetAccessee().GetAccesseeDeviceId() == peerDeviceId) {
1967 peerUserId = profile.GetAccessee().GetAccesseeUserId();
1968 localUserId = profile.GetAccesser().GetAccesserUserId();
1969 localDeviceId = profile.GetAccesser().GetAccesserDeviceId();
1970 }
1971 std::vector<AccessControlProfile> aclProfiles;
1972 int32_t ret = GetAllAccessControlProfiles(aclProfiles);
1973 if (ret != DP_SUCCESS) {
1974 HILOGE("GetAllAccessControlProfiles failed");
1975 return ret;
1976 }
1977 RemoveLnnAcl(aclProfiles);
1978 for (auto aclProfile : aclProfiles) {
1979 if (peerDeviceId != aclProfile.GetTrustDeviceId() ||
1980 STATUS_ACTIVE != aclProfile.GetStatus()) {
1981 continue;
1982 }
1983 if ((localDeviceId == aclProfile.GetAccesser().GetAccesserDeviceId() &&
1984 localUserId == aclProfile.GetAccesser().GetAccesserUserId() &&
1985 peerDeviceId == aclProfile.GetAccessee().GetAccesseeDeviceId() &&
1986 peerUserId == aclProfile.GetAccessee().GetAccesseeUserId()) ||
1987 (peerDeviceId == aclProfile.GetAccesser().GetAccesserDeviceId() &&
1988 peerUserId == aclProfile.GetAccesser().GetAccesserUserId() &&
1989 localDeviceId == aclProfile.GetAccessee().GetAccesseeDeviceId() &&
1990 localUserId == aclProfile.GetAccessee().GetAccesseeUserId())) {
1991 resultCount++;
1992 HILOGE("localUserId and peerUserId have active acl exist");
1993 }
1994 }
1995 return DP_SUCCESS;
1996 }
1997
CheckDeviceIdAndUserIdExists(const AccessControlProfile & profile,bool & isExists)1998 int32_t TrustProfileManager::CheckDeviceIdAndUserIdExists(const AccessControlProfile& profile, bool& isExists)
1999 {
2000 std::string peerDeviceId = profile.GetTrustDeviceId();
2001 std::string localDeviceId = profile.GetAccessee().GetAccesseeDeviceId();
2002 int32_t peerUserId = profile.GetAccesser().GetAccesserUserId();
2003 int32_t localUserId = profile.GetAccessee().GetAccesseeUserId();
2004 if (profile.GetAccessee().GetAccesseeDeviceId() == peerDeviceId) {
2005 peerUserId = profile.GetAccessee().GetAccesseeUserId();
2006 localUserId = profile.GetAccesser().GetAccesserUserId();
2007 localDeviceId = profile.GetAccesser().GetAccesserDeviceId();
2008 }
2009 std::vector<AccessControlProfile> aclProfiles;
2010 int32_t ret = GetAllAccessControlProfiles(aclProfiles);
2011 if (ret != DP_SUCCESS) {
2012 HILOGE("GetAllAccessControlProfiles failed");
2013 return ret;
2014 }
2015 RemoveLnnAcl(aclProfiles);
2016 for (auto aclProfile : aclProfiles) {
2017 if (peerDeviceId != aclProfile.GetTrustDeviceId()) {
2018 continue;
2019 }
2020 if ((localDeviceId == aclProfile.GetAccesser().GetAccesserDeviceId() &&
2021 localUserId == aclProfile.GetAccesser().GetAccesserUserId() &&
2022 peerDeviceId == aclProfile.GetAccessee().GetAccesseeDeviceId() &&
2023 peerUserId == aclProfile.GetAccessee().GetAccesseeUserId()) ||
2024 (peerDeviceId == aclProfile.GetAccesser().GetAccesserDeviceId() &&
2025 peerUserId == aclProfile.GetAccesser().GetAccesserUserId() &&
2026 localDeviceId == aclProfile.GetAccessee().GetAccesseeDeviceId() &&
2027 localUserId == aclProfile.GetAccessee().GetAccesseeUserId())) {
2028 isExists = true;
2029 HILOGE("localUserId and peerUserId acl exist");
2030 break;
2031 }
2032 }
2033 return DP_SUCCESS;
2034 }
2035
NotifyCheck(const AccessControlProfile & profile,const AccessControlProfile & oldProfile)2036 int32_t TrustProfileManager::NotifyCheck(const AccessControlProfile& profile, const AccessControlProfile& oldProfile)
2037 {
2038 int32_t resultCount = 0;
2039 int32_t ret = CheckDeviceIdAndUserIdActive(profile, resultCount);
2040 if (ret != DP_SUCCESS) {
2041 HILOGE("CheckDeviceIdAndUserIdActive failed");
2042 return DP_NOTIFY_TRUST_DEVICE_FAIL;
2043 }
2044 HILOGI("resultCount : %{public}d", resultCount);
2045 TrustDeviceProfile trustProfile;
2046 ProfileUtils::ConvertToTrustDeviceProfile(profile, trustProfile);
2047 if (resultCount == 1 && profile.GetStatus() == STATUS_ACTIVE &&
2048 oldProfile.GetStatus() == STATUS_INACTIVE && !IsLnnAcl(profile)) {
2049 ret = SubscribeProfileManager::GetInstance().NotifyTrustDeviceProfileActive(trustProfile);
2050 if (ret != DP_SUCCESS) {
2051 HILOGE("NotifyTrustDeviceProfileActive failed");
2052 return DP_NOTIFY_TRUST_DEVICE_FAIL;
2053 }
2054 }
2055 if (resultCount == 0 && profile.GetStatus() == STATUS_INACTIVE &&
2056 oldProfile.GetStatus() == STATUS_ACTIVE && !IsLnnAcl(profile)) {
2057 int32_t ret = SubscribeProfileManager::GetInstance().NotifyTrustDeviceProfileInactive(trustProfile);
2058 if (ret != DP_SUCCESS) {
2059 HILOGE("NotifyTrustDeviceProfileInactive failed");
2060 return DP_NOTIFY_TRUST_DEVICE_FAIL;
2061 }
2062 }
2063 return DP_SUCCESS;
2064 }
2065
DeleteAccesseeCheck(int64_t accesseeId,Accessee & accessee)2066 int32_t TrustProfileManager::DeleteAccesseeCheck(int64_t accesseeId, Accessee& accessee)
2067 {
2068 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_ACCESS_CONTROL_TABLE_WHERE_ACCESSEEID,
2069 std::vector<ValueObject>{ ValueObject(accesseeId) });
2070 if (resultSet == nullptr) {
2071 HILOGE("resultSet is nullptr");
2072 return DP_GET_RESULTSET_FAIL;
2073 }
2074 int32_t rowCount = ROWCOUNT_INIT;
2075 resultSet->GetRowCount(rowCount);
2076 resultSet->Close();
2077 resultSet = GetResultSet(SELECT_ACCESSEE_TABLE_WHERE_ACCESSEEID,
2078 std::vector<ValueObject>{ ValueObject(accesseeId) });
2079 if (resultSet == nullptr) {
2080 HILOGE("resultSet is nullptr");
2081 return DP_GET_RESULTSET_FAIL;
2082 }
2083 resultSet->GoToNextRow();
2084 ProfileUtils::ConvertToAccessee(resultSet, accessee);
2085 resultSet->Close();
2086 if (rowCount == DELETE_ACCESSEE_CONDITION) {
2087 std::lock_guard<std::mutex> lock(rdbMutex_);
2088 if (rdbStore_ == nullptr) {
2089 HILOGE("rdbStore_ is nullptr");
2090 return DP_GET_RDBSTORE_FAIL;
2091 }
2092 int32_t deleteRows = DELETEROWS_INIT;
2093 int32_t ret = rdbStore_->Delete(deleteRows, ACCESSEE_TABLE, ACCESSEEID_EQUAL_CONDITION,
2094 std::vector<ValueObject> {ValueObject(accesseeId)});
2095 if (ret != DP_SUCCESS) {
2096 HILOGE("delete accesseetable accesseeId failed");
2097 return DP_DELETE_ACCESSEE_PROFILE_FAIL;
2098 }
2099 HILOGI("DeleteAccessee : %{public}s", accessee.dump().c_str());
2100 }
2101 return DP_SUCCESS;
2102 }
2103
DeleteTrustDeviceCheck(const AccessControlProfile & profile)2104 int32_t TrustProfileManager::DeleteTrustDeviceCheck(const AccessControlProfile& profile)
2105 {
2106 TrustDeviceProfile trustProfile;
2107 ProfileUtils::ConvertToTrustDeviceProfile(profile, trustProfile);
2108 bool isExists = false;
2109 CheckDeviceIdAndUserIdExists(profile, isExists);
2110 if (!isExists && !IsLnnAcl(profile)) {
2111 int32_t ret = SubscribeProfileManager::GetInstance().NotifyTrustDeviceProfileDelete(trustProfile);
2112 if (ret != DP_SUCCESS) {
2113 HILOGE("NotifyTrustDeviceProfileDelete failed");
2114 return DP_NOTIFY_TRUST_DEVICE_FAIL;
2115 }
2116 }
2117 std::shared_ptr<ResultSet> resultSet = GetResultSet(SELECT_ACCESS_CONTROL_TABLE_WHERE_TRUSTDEVICEID,
2118 std::vector<ValueObject>{ ValueObject(profile.GetTrustDeviceId()) });
2119 if (resultSet == nullptr) {
2120 HILOGE("resultSet is nullptr");
2121 return DP_GET_RESULTSET_FAIL;
2122 }
2123 int32_t rowCount = ROWCOUNT_INIT;
2124 resultSet->GetRowCount(rowCount);
2125 resultSet->Close();
2126 int32_t ret = RET_INIT;
2127 if (rowCount == DELETE_TRUST_CONDITION) {
2128 ret = this->DeleteTrustDeviceProfile(profile.GetTrustDeviceId());
2129 if (ret != DP_SUCCESS) {
2130 HILOGE("DeleteTrustDeviceProfile failed");
2131 return DP_DELETE_TRUST_DEVICE_PROFILE_FAIL;
2132 }
2133 } else {
2134 int32_t status = STATUS_INIT;
2135 this->GetResultStatus(profile.GetTrustDeviceId(), status);
2136 TrustDeviceProfile trustDeviceProfile(trustProfile);
2137 trustDeviceProfile.SetStatus(status);
2138 ret = this->UpdateTrustDeviceProfile(trustDeviceProfile);
2139 if (ret != DP_SUCCESS) {
2140 HILOGE("UpdateTrustDeviceProfile failed");
2141 return DP_UPDATE_TRUST_DEVICE_PROFILE_FAIL;
2142 }
2143 }
2144 return DP_SUCCESS;
2145 }
2146
RemoveLnnAcl(std::vector<AccessControlProfile> & profiles)2147 void TrustProfileManager::RemoveLnnAcl(std::vector<AccessControlProfile>& profiles)
2148 {
2149 std::vector<AccessControlProfile> aclProfiles(profiles);
2150 profiles.clear();
2151 for (auto aclProfile : aclProfiles) {
2152 if (IsLnnAcl(aclProfile)) {
2153 continue;
2154 }
2155 profiles.emplace_back(aclProfile);
2156 }
2157 return;
2158 }
2159
IsLnnAcl(const AccessControlProfile & aclProfile)2160 bool TrustProfileManager::IsLnnAcl(const AccessControlProfile& aclProfile)
2161 {
2162 if (aclProfile.GetExtraData().empty()) {
2163 return false;
2164 }
2165 cJSON* json = cJSON_Parse(aclProfile.GetExtraData().c_str());
2166 if (!cJSON_IsObject(json)) {
2167 HILOGW("cJSON_Parse extraData fail!");
2168 cJSON_Delete(json);
2169 return false;
2170 }
2171 std::string lnnAclValue;
2172 cJSON* item = cJSON_GetObjectItemCaseSensitive(json, IS_LNN_ACL.c_str());
2173 if (item != NULL && cJSON_IsString(item)) {
2174 lnnAclValue = item->valuestring;
2175 }
2176 if (lnnAclValue == LNN_ACL_TRUE) {
2177 item = NULL;
2178 cJSON_Delete(json);
2179 return true;
2180 }
2181 item = NULL;
2182 cJSON_Delete(json);
2183 return false;
2184 }
2185
IsAceeCreIdExistToAceeTable()2186 bool TrustProfileManager::IsAceeCreIdExistToAceeTable()
2187 {
2188 std::shared_ptr<ResultSet> resultSet = GetResultSet(PRAGMA_ACCESSEE_TABLE, std::vector<ValueObject>{});
2189 if (resultSet == nullptr) {
2190 HILOGE("resultSet is nullptr");
2191 return false;
2192 }
2193 while (resultSet->GoToNextRow() == DP_SUCCESS) {
2194 int32_t columnIndex = COLUMNINDEX_INIT;
2195 std::string columnName;
2196 resultSet->GetColumnIndex(NAME, columnIndex);
2197 resultSet->GetString(columnIndex, columnName);
2198 if (columnName == ACCESSEE_CREDENTIAL_ID) {
2199 HILOGE("aceeCreId exist to acee_table");
2200 return true;
2201 }
2202 }
2203 return false;
2204 }
2205
AddAceeCreIdColumnToAceeTable()2206 int32_t TrustProfileManager::AddAceeCreIdColumnToAceeTable()
2207 {
2208 std::lock_guard<std::mutex> lock(rdbMutex_);
2209 if (rdbStore_ == nullptr) {
2210 HILOGE("rdbStore_ is nullptr");
2211 return DP_GET_RDBSTORE_FAIL;
2212 }
2213 int32_t ret = rdbStore_->CreateTable(ALTER_TABLE_ACEE_ADD_COLUMN_ACEE_CREDENTIAL_ID);
2214 if (ret != DP_SUCCESS) {
2215 HILOGE("add column accesseeCredentialId to acee table failed");
2216 return DP_CREATE_TABLE_FAIL;
2217 }
2218 return DP_SUCCESS;
2219 }
2220 } // namespace DistributedDeviceProfile
2221 } // namespace OHOS