1/*
2 * Copyright (c) 2023-2024 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 "app_manager.h"
17
18#include "accesstoken_kit.h"
19#include "if_system_ability_manager.h"
20#include "ipc_skeleton.h"
21#include "iservice_registry.h"
22#include "os_account_manager.h"
23#include "system_ability_definition.h"
24
25#include "dm_anonymous.h"
26#include "dm_constants.h"
27#include "dm_log.h"
28
29using namespace OHOS::Security::AccessToken;
30
31namespace OHOS {
32namespace DistributedHardware {
33DM_IMPLEMENT_SINGLE_INSTANCE(AppManager);
34
35const std::string AppManager::GetAppId()
36{
37    std::string appId = "";
38    AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID();
39    if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) {
40        LOGI("The caller is not token_hap.");
41        return appId;
42    }
43    sptr<AppExecFwk::IBundleMgr> bundleManager = nullptr;
44    if (!GetBundleManagerProxy(bundleManager)) {
45        LOGE("get bundleManager failed.");
46        return appId;
47    }
48    if (bundleManager == nullptr) {
49        LOGE("bundleManager is nullptr.");
50        return appId;
51    }
52    int userId;
53    ErrCode result = AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId);
54    if (result != ERR_OK) {
55        LOGE("GetAppIdByCallingUid QueryActiveOsAccountIds failed.");
56        return appId;
57    }
58    std::string BundleName;
59    bundleManager->GetNameForUid(IPCSkeleton::GetCallingUid(), BundleName);
60    AppExecFwk::BundleInfo bundleInfo;
61    int ret = bundleManager->GetBundleInfoV9(
62        BundleName, static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO),
63        bundleInfo, userId);
64    if (ret != 0) {
65        LOGE(" GetBundleInfoV9 failed %{public}d.", ret);
66        return appId;
67    }
68    appId = bundleInfo.appId;
69    return appId;
70}
71
72void AppManager::RegisterCallerAppId(const std::string &pkgName)
73{
74    if (pkgName.empty()) {
75        LOGE("Invalid parameter, pkgName is empty.");
76        return;
77    }
78    std::string appId = GetAppId();
79    if (appId.empty()) {
80        LOGE("PkgName %{public}s get appid failed.", pkgName.c_str());
81        return;
82    }
83    LOGI("PkgName %{public}s, appId %{public}s.", pkgName.c_str(), GetAnonyString(appId).c_str());
84    std::lock_guard<std::mutex> lock(appIdMapLock_);
85    appIdMap_[pkgName] = appId;
86}
87
88void AppManager::UnRegisterCallerAppId(const std::string &pkgName)
89{
90    if (pkgName.empty()) {
91        LOGE("Invalid parameter, pkgName is empty.");
92        return;
93    }
94    LOGI("PkgName %{public}s.", pkgName.c_str());
95    std::lock_guard<std::mutex> lock(appIdMapLock_);
96    if (appIdMap_.find(pkgName) == appIdMap_.end()) {
97        LOGE("AppIdMap not find pkgName.");
98        return;
99    }
100    appIdMap_.erase(pkgName);
101}
102
103int32_t AppManager::GetAppIdByPkgName(const std::string &pkgName, std::string &appId)
104{
105    if (pkgName.empty()) {
106        LOGE("Invalid parameter, pkgName is empty.");
107        return ERR_DM_INPUT_PARA_INVALID;
108    }
109    LOGI("PkgName %{public}s.", pkgName.c_str());
110    std::lock_guard<std::mutex> lock(appIdMapLock_);
111    if (appIdMap_.find(pkgName) == appIdMap_.end()) {
112        LOGE("AppIdMap not find pkgName.");
113        return ERR_DM_FAILED;
114    }
115    appId = appIdMap_[pkgName];
116    return DM_OK;
117}
118
119bool AppManager::GetBundleManagerProxy(sptr<AppExecFwk::IBundleMgr> &bundleManager)
120{
121    sptr<ISystemAbilityManager> systemAbilityManager =
122        SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
123    if (systemAbilityManager == nullptr) {
124        LOGE("GetBundleManagerProxy Failed to get system ability mgr.");
125        return false;
126    }
127    sptr<IRemoteObject> remoteObject =
128        systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
129    if (remoteObject == nullptr) {
130        LOGE("GetBundleManagerProxy Failed to get bundle manager service.");
131        return false;
132    }
133    bundleManager = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
134    if (bundleManager == nullptr) {
135        LOGE("bundleManager is nullptr");
136        return false;
137    }
138    return true;
139}
140
141bool AppManager::IsSystemSA()
142{
143    AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
144    if (tokenCaller == 0) {
145        LOGE("IsSystemSA GetCallingTokenID error.");
146        return false;
147    }
148    ATokenTypeEnum tokenTypeFlag = AccessTokenKit::GetTokenTypeFlag(tokenCaller);
149    if (tokenTypeFlag == ATokenTypeEnum::TOKEN_NATIVE) {
150        return true;
151    }
152    return false;
153}
154} // namespace DistributedHardware
155} // namespace OHOS
156