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().QueryOhosAccountInfoByUserId(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 
GetLocalAccountUid(std::string & accountUid)67 int32_t GetLocalAccountUid(std::string& accountUid)
68 {
69     OHOS::AccountSA::OhosAccountInfo accountInfo;
70     int32_t ret = OHOS::AccountSA::OhosAccountKits::GetInstance().GetOhosAccountInfoByUserId(GetCallingUserId(),
71         accountInfo);
72     if (ret != 0) {
73         return ret;
74     }
75     accountUid = accountInfo.GetRawUid();
76     return 0;
77 }
78 
GetUserIdFromUid(int32_t uid,int32_t * userId)79 int8_t GetUserIdFromUid(int32_t uid, int32_t* userId)
80 {
81     if (OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, *userId) != 0) {
82         DLP_LOG_INFO(LABEL, "get userId from uid failed, uid: %{public}d", uid);
83         return -1;
84     }
85     return 0;
86 }
87 
IsAccountLogIn(uint32_t osAccountId,AccountType accountType,const DlpBlob * accountId)88 bool IsAccountLogIn(uint32_t osAccountId, AccountType accountType, const DlpBlob* accountId)
89 {
90     if (accountId == nullptr) {
91         DLP_LOG_ERROR(LABEL, "Invalid input params.");
92         return DLP_ERR_INVALID_PARAMS;
93     }
94 
95     int32_t res;
96     if (accountType == CLOUD_ACCOUNT) {
97         OhosAccountInfo accountInfo;
98         res = OhosAccountKits::GetInstance().GetOhosAccountInfoByUserId(osAccountId, accountInfo);
99         if (res != DLP_SUCCESS) {
100             DLP_LOG_ERROR(LABEL, "GetOhosAccountInfoByUserId from OhosAccountKits failed, res:%{public}d.", res);
101             return false;
102         }
103         if (accountInfo.status_ == ACCOUNT_STATE_UNBOUND) {
104             DLP_LOG_ERROR(LABEL, "GetOhosAccountInfoByUserId from OhosAccountKits is not login.");
105             return false;
106         }
107         return true;
108     }
109 
110     if (accountType != DOMAIN_ACCOUNT) {
111         // app account status default value is true
112         return true;
113     }
114 
115     DomainAccountInfo info;
116     std::string account(reinterpret_cast<char*>(accountId->data), accountId->size);
117     info.accountId_ = account;
118     DLP_LOG_INFO(LABEL, "Get accountType:%{public}d", accountType);
119     DomainAccountStatus status;
120     res = DomainAccountClient::GetInstance().GetAccountStatus(info, status);
121     if (res != OHOS::ERR_OK) {
122         DLP_LOG_ERROR(LABEL, "GetAccountStatus from OsAccountKits failed, res:%{public}d.", res);
123         return false;
124     }
125     if (status != DomainAccountStatus::LOGIN) {
126         DLP_LOG_ERROR(LABEL, "Domain account status is not login. status=%{public}d.", status);
127         return false;
128     }
129     return true;
130 }
131 
GetDomainAccountName(char ** account)132 int32_t GetDomainAccountName(char** account)
133 {
134     int32_t userId;
135     if (!GetUserIdByForegroundAccount(&userId)) {
136         DLP_LOG_ERROR(LABEL, "GetUserIdByForegroundAccount error");
137         return DLP_PARSE_ERROR_ACCOUNT_INVALID;
138     }
139     OHOS::AccountSA::OsAccountInfo osAccountInfo;
140     if (OHOS::AccountSA::OsAccountManager::QueryOsAccountById(userId, osAccountInfo) != 0) {
141         DLP_LOG_ERROR(LABEL, "GetOsAccountLocalIdFromDomain return not 0");
142         return DLP_PARSE_ERROR_ACCOUNT_INVALID;
143     }
144     DomainAccountInfo domainInfo;
145     osAccountInfo.GetDomainInfo(domainInfo);
146     if (domainInfo.accountName_.empty()) {
147         DLP_LOG_ERROR(LABEL, "accountName_ empty");
148         return DLP_PARSE_ERROR_ACCOUNT_INVALID;
149     }
150     *account = strdup(domainInfo.accountName_.c_str());
151     return DLP_OK;
152 }