1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "account_adapt.h"
17 #include "dlp_permission.h"
18 #include "dlp_permission_log.h"
19 #include "domain_account_client.h"
20 #include "ipc_skeleton.h"
21 #include "ohos_account_kits.h"
22 #include "os_account_manager.h"
23
24 namespace {
25 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "AccountAdapt"};
26 constexpr static int UID_TRANSFORM_DIVISOR = 200000;
27 }
28 using OHOS::Security::DlpPermission::DLP_PARSE_ERROR_ACCOUNT_INVALID;
29 using OHOS::Security::DlpPermission::DLP_OK;
30 using OHOS::AccountSA::OhosAccountInfo;
31 using OHOS::AccountSA::OhosAccountKits;
32 using OHOS::AccountSA::ACCOUNT_STATE_UNBOUND;
33 using OHOS::AccountSA::DomainAccountClient;
34 using OHOS::AccountSA::DomainAccountStatus;
35 using OHOS::AccountSA::DomainAccountInfo;
36
GetCallingUserId(void)37 int32_t GetCallingUserId(void)
38 {
39 int32_t callingUid = OHOS::IPCSkeleton::GetCallingUid();
40 return (callingUid / UID_TRANSFORM_DIVISOR);
41 }
42
GetLocalAccountName(char ** account,uint32_t userId)43 int8_t GetLocalAccountName(char** account, uint32_t userId)
44 {
45 if (account == nullptr) {
46 return -1;
47 }
48 std::pair<bool, OHOS::AccountSA::OhosAccountInfo> accountInfo =
49 OHOS::AccountSA::OhosAccountKits::GetInstance().QueryOsAccountDistributedInfo(userId);
50 if (accountInfo.first) {
51 *account = strdup(accountInfo.second.name_.c_str());
52 return 0;
53 }
54 return -1;
55 }
56
GetUserIdByForegroundAccount(int32_t * userId)57 bool GetUserIdByForegroundAccount(int32_t* userId)
58 {
59 int32_t res = OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(*userId);
60 if (res != 0) {
61 DLP_LOG_ERROR(LABEL, "GetForegroundOsAccountLocalId failed %{public}d", res);
62 return false;
63 }
64 return true;
65 }
66
GetUserIdFromUid(int32_t uid,int32_t * userId)67 int8_t GetUserIdFromUid(int32_t uid, int32_t* userId)
68 {
69 if (OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, *userId) != 0) {
70 DLP_LOG_INFO(LABEL, "get userId from uid failed, uid: %{public}d", uid);
71 return -1;
72 }
73 return 0;
74 }
75
IsAccountLogIn(uint32_t osAccountId,AccountType accountType,const DlpBlob * accountId)76 bool IsAccountLogIn(uint32_t osAccountId, AccountType accountType, const DlpBlob* accountId)
77 {
78 if (accountId == nullptr) {
79 DLP_LOG_ERROR(LABEL, "Invalid input params.");
80 return false;
81 }
82
83 int32_t res;
84 if (accountType == CLOUD_ACCOUNT) {
85 OhosAccountInfo accountInfo;
86 res = OhosAccountKits::GetInstance().GetOsAccountDistributedInfo(osAccountId, accountInfo);
87 if (res != DLP_SUCCESS) {
88 DLP_LOG_ERROR(LABEL, "GetOsAccountDistributedInfo from OhosAccountKits failed, res:%{public}d.", res);
89 return false;
90 }
91 if (accountInfo.status_ == ACCOUNT_STATE_UNBOUND) {
92 DLP_LOG_ERROR(LABEL, "GetOsAccountDistributedInfo from OhosAccountKits is not login.");
93 return false;
94 }
95 return true;
96 }
97
98 if (accountType != DOMAIN_ACCOUNT) {
99 // app account status default value is true
100 return true;
101 }
102
103 DomainAccountInfo info;
104 std::string account(reinterpret_cast<char*>(accountId->data), accountId->size);
105 info.accountId_ = account;
106 DLP_LOG_INFO(LABEL, "Get accountType:%{public}d", accountType);
107 DomainAccountStatus status;
108 res = DomainAccountClient::GetInstance().GetAccountStatus(info, status);
109 if (res != OHOS::ERR_OK) {
110 DLP_LOG_ERROR(LABEL, "GetAccountStatus from OsAccountKits failed, res:%{public}d.", res);
111 return false;
112 }
113 if (status != DomainAccountStatus::LOGIN) {
114 DLP_LOG_ERROR(LABEL, "Domain account status is not login. status=%{public}d.", status);
115 return false;
116 }
117 return true;
118 }
119
GetDomainAccountName(char ** account)120 int32_t GetDomainAccountName(char** account)
121 {
122 int32_t userId;
123 if (!GetUserIdByForegroundAccount(&userId)) {
124 DLP_LOG_ERROR(LABEL, "GetUserIdByForegroundAccount error");
125 return DLP_PARSE_ERROR_ACCOUNT_INVALID;
126 }
127 OHOS::AccountSA::OsAccountInfo osAccountInfo;
128 if (OHOS::AccountSA::OsAccountManager::QueryOsAccountById(userId, osAccountInfo) != 0) {
129 DLP_LOG_ERROR(LABEL, "GetOsAccountLocalIdFromDomain return not 0");
130 return DLP_PARSE_ERROR_ACCOUNT_INVALID;
131 }
132 DomainAccountInfo domainInfo;
133 osAccountInfo.GetDomainInfo(domainInfo);
134 if (domainInfo.accountName_.empty()) {
135 DLP_LOG_ERROR(LABEL, "accountName_ empty");
136 return DLP_PARSE_ERROR_ACCOUNT_INVALID;
137 }
138 *account = strdup(domainInfo.accountName_.c_str());
139 return DLP_OK;
140 }