• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "app_manager.h"
17 
18 #include "accesstoken_kit.h"
19 #include "access_token.h"
20 #include "if_system_ability_manager.h"
21 #include "ipc_skeleton.h"
22 #include "iservice_registry.h"
23 #include "os_account_manager.h"
24 #include "system_ability_definition.h"
25 #include "tokenid_kit.h"
26 
27 #include "dm_anonymous.h"
28 #include "dm_error_type.h"
29 #include "dm_log.h"
30 
31 using namespace OHOS::Security::AccessToken;
32 
33 namespace OHOS {
34 namespace DistributedHardware {
35 namespace {
36 const uint32_t MAX_CONTAINER_SIZE = 1000;
37 }
38 DM_IMPLEMENT_SINGLE_INSTANCE(AppManager);
39 
GetAppId()40 const std::string AppManager::GetAppId()
41 {
42     std::string appId = "";
43     AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID();
44     if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) {
45         LOGD("The caller is not token_hap.");
46         return appId;
47     }
48     sptr<AppExecFwk::IBundleMgr> bundleManager = nullptr;
49     if (!GetBundleManagerProxy(bundleManager)) {
50         LOGE("get bundleManager failed.");
51         return appId;
52     }
53     if (bundleManager == nullptr) {
54         LOGE("bundleManager is nullptr.");
55         return appId;
56     }
57     int32_t userId = -1;
58     if (AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId) != ERR_OK) {
59         LOGE("GetAppIdByCallingUid QueryActiveOsAccountIds failed.");
60         return appId;
61     }
62     std::string BundleName = "";
63     bundleManager->GetNameForUid(IPCSkeleton::GetCallingUid(), BundleName);
64     AppExecFwk::BundleInfo bundleInfo;
65     int32_t ret = bundleManager->GetBundleInfoV9(
66         BundleName, static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO),
67         bundleInfo, userId);
68     if (ret != 0) {
69         LOGE(" GetBundleInfoV9 failed %{public}d.", ret);
70         return appId;
71     }
72     appId = bundleInfo.appId;
73     return appId;
74 }
75 
RegisterCallerAppId(const std::string & pkgName)76 DM_EXPORT void AppManager::RegisterCallerAppId(const std::string &pkgName)
77 {
78     if (pkgName.empty()) {
79         LOGE("Invalid parameter, pkgName is empty.");
80         return;
81     }
82     std::string appId = GetAppId();
83     if (appId.empty()) {
84         LOGD("PkgName %{public}s get appid failed.", pkgName.c_str());
85         return;
86     }
87     LOGI("PkgName %{public}s, appId %{public}s.", pkgName.c_str(), GetAnonyString(appId).c_str());
88     std::lock_guard<std::mutex> lock(appIdMapLock_);
89     CHECK_SIZE_VOID(appIdMap_);
90     appIdMap_[pkgName] = appId;
91 }
92 
UnRegisterCallerAppId(const std::string & pkgName)93 DM_EXPORT void AppManager::UnRegisterCallerAppId(const std::string &pkgName)
94 {
95     if (pkgName.empty()) {
96         LOGE("Invalid parameter, pkgName is empty.");
97         return;
98     }
99     LOGI("PkgName %{public}s.", pkgName.c_str());
100     std::lock_guard<std::mutex> lock(appIdMapLock_);
101     if (appIdMap_.find(pkgName) == appIdMap_.end()) {
102         return;
103     }
104     appIdMap_.erase(pkgName);
105 }
106 
GetAppIdByPkgName(const std::string & pkgName,std::string & appId)107 int32_t AppManager::GetAppIdByPkgName(const std::string &pkgName, std::string &appId)
108 {
109     if (pkgName.empty()) {
110         LOGE("Invalid parameter, pkgName is empty.");
111         return ERR_DM_INPUT_PARA_INVALID;
112     }
113     LOGD("PkgName %{public}s.", pkgName.c_str());
114     std::lock_guard<std::mutex> lock(appIdMapLock_);
115     if (appIdMap_.find(pkgName) == appIdMap_.end()) {
116         LOGD("AppIdMap not find pkgName.");
117         return ERR_DM_FAILED;
118     }
119     appId = appIdMap_[pkgName];
120     return DM_OK;
121 }
122 
GetBundleManagerProxy(sptr<AppExecFwk::IBundleMgr> & bundleManager)123 bool AppManager::GetBundleManagerProxy(sptr<AppExecFwk::IBundleMgr> &bundleManager)
124 {
125     sptr<ISystemAbilityManager> systemAbilityManager =
126         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
127     if (systemAbilityManager == nullptr) {
128         LOGE("GetBundleManagerProxy Failed to get system ability mgr.");
129         return false;
130     }
131     sptr<IRemoteObject> remoteObject =
132         systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
133     if (remoteObject == nullptr) {
134         LOGE("GetBundleManagerProxy Failed to get bundle manager service.");
135         return false;
136     }
137     bundleManager = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
138     if (bundleManager == nullptr) {
139         LOGE("bundleManager is nullptr");
140         return false;
141     }
142     return true;
143 }
144 
IsSystemSA()145 bool AppManager::IsSystemSA()
146 {
147     AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
148     if (tokenCaller == 0) {
149         LOGE("IsSystemSA GetCallingTokenID error.");
150         return false;
151     }
152     ATokenTypeEnum tokenTypeFlag = AccessTokenKit::GetTokenTypeFlag(tokenCaller);
153     if (tokenTypeFlag == ATokenTypeEnum::TOKEN_NATIVE) {
154         return true;
155     }
156     return false;
157 }
158 
IsSystemApp()159 DM_EXPORT bool AppManager::IsSystemApp()
160 {
161     uint64_t fullTokenId = IPCSkeleton::GetCallingFullTokenID();
162     return OHOS::Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId);
163 }
164 
GetCallerName(bool isSystemSA,std::string & callerName)165 DM_EXPORT int32_t AppManager::GetCallerName(bool isSystemSA, std::string &callerName)
166 {
167     AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
168     if (tokenCaller == 0) {
169         LOGE("GetCallingTokenID error.");
170         return ERR_DM_FAILED;
171     }
172     LOGI("tokenCaller ID == %{public}s", GetAnonyInt32(tokenCaller).c_str());
173     ATokenTypeEnum tokenTypeFlag = AccessTokenKit::GetTokenTypeFlag(tokenCaller);
174     if (tokenTypeFlag == ATokenTypeEnum::TOKEN_HAP) {
175         isSystemSA = false;
176         HapTokenInfo tokenInfo;
177         if (AccessTokenKit::GetHapTokenInfo(tokenCaller, tokenInfo) != EOK) {
178             LOGE("GetHapTokenInfo failed.");
179             return ERR_DM_FAILED;
180         }
181         callerName = std::move(tokenInfo.bundleName);
182     } else if (tokenTypeFlag == ATokenTypeEnum::TOKEN_NATIVE) {
183         isSystemSA = true;
184         NativeTokenInfo tokenInfo;
185         if (AccessTokenKit::GetNativeTokenInfo(tokenCaller, tokenInfo) != EOK) {
186             LOGE("GetNativeTokenInfo failed.");
187             return ERR_DM_FAILED;
188         }
189         callerName = std::move(tokenInfo.processName);
190     } else {
191         LOGE("failed, unsupported process.");
192         return ERR_DM_FAILED;
193     }
194     return DM_OK;
195 }
196 
GetBundleNameByTokenId(int64_t tokenId,std::string & bundleName)197 DM_EXPORT int32_t AppManager::GetBundleNameByTokenId(int64_t tokenId, std::string &bundleName)
198 {
199     if (tokenId < 0) {
200         LOGE("GetBundleNameByTokenId error.");
201         return ERR_DM_FAILED;
202     }
203     AccessTokenID tokenIdTemp = static_cast<AccessTokenID>(tokenId);
204     ATokenTypeEnum tokenTypeFlag = AccessTokenKit::GetTokenTypeFlag(tokenIdTemp);
205     if (tokenTypeFlag == ATokenTypeEnum::TOKEN_HAP) {
206         HapTokenInfo tokenInfo;
207         if (AccessTokenKit::GetHapTokenInfo(tokenIdTemp, tokenInfo) != EOK) {
208             LOGE("GetHapTokenInfo failed.");
209             return ERR_DM_FAILED;
210         }
211         bundleName = std::move(tokenInfo.bundleName);
212     } else if (tokenTypeFlag == ATokenTypeEnum::TOKEN_NATIVE) {
213         NativeTokenInfo tokenInfo;
214         if (AccessTokenKit::GetNativeTokenInfo(tokenIdTemp, tokenInfo) != EOK) {
215             LOGE("GetNativeTokenInfo failed.");
216             return ERR_DM_FAILED;
217         }
218         bundleName = std::move(tokenInfo.processName);
219     } else {
220         LOGE("failed, unsupported process.");
221         return ERR_DM_FAILED;
222     }
223     return DM_OK;
224 }
225 
GetNativeTokenIdByName(std::string & processName,int64_t & tokenId)226 DM_EXPORT int32_t AppManager::GetNativeTokenIdByName(std::string &processName,
227     int64_t &tokenId)
228 {
229     AccessTokenID nativeTokenId = AccessTokenKit::GetNativeTokenId(processName);
230     if (nativeTokenId == INVALID_TOKENID) {
231         LOGE("GetNativeTokenId failed.");
232         return ERR_DM_FAILED;
233     }
234     tokenId = static_cast<int64_t>(nativeTokenId);
235     return DM_OK;
236 }
237 
GetHapTokenIdByName(int32_t userId,std::string & bundleName,int32_t instIndex,int64_t & tokenId)238 DM_EXPORT int32_t AppManager::GetHapTokenIdByName(int32_t userId,
239     std::string &bundleName, int32_t instIndex, int64_t &tokenId)
240 {
241     auto hapTokenId = AccessTokenKit::GetHapTokenID(userId, bundleName, instIndex);
242     if (hapTokenId == 0) {
243         LOGE("GetHapTokenId failed.");
244         return ERR_DM_FAILED;
245     }
246     tokenId = static_cast<int64_t>(hapTokenId);
247     return DM_OK;
248 }
249 
GetCallerProcessName(std::string & processName)250 DM_EXPORT int32_t AppManager::GetCallerProcessName(std::string &processName)
251 {
252     AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
253     if (tokenCaller == 0) {
254         LOGE("GetCallerProcessName GetCallingTokenID error.");
255         return ERR_DM_FAILED;
256     }
257     ATokenTypeEnum tokenTypeFlag = AccessTokenKit::GetTokenTypeFlag(tokenCaller);
258     if (tokenTypeFlag == ATokenTypeEnum::TOKEN_HAP) {
259         HapTokenInfo tokenInfo;
260         if (AccessTokenKit::GetHapTokenInfo(tokenCaller, tokenInfo) != EOK) {
261             LOGE("GetHapTokenInfo failed.");
262             return ERR_DM_FAILED;
263         }
264         processName = std::move(tokenInfo.bundleName);
265         uint64_t fullTokenId = IPCSkeleton::GetCallingFullTokenID();
266         if (!OHOS::Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId)) {
267             LOGE("GetCallerProcessName %{public}s not system hap.", processName.c_str());
268             return ERR_DM_FAILED;
269         }
270     } else if (tokenTypeFlag == ATokenTypeEnum::TOKEN_NATIVE) {
271         NativeTokenInfo tokenInfo;
272         if (AccessTokenKit::GetNativeTokenInfo(tokenCaller, tokenInfo) != EOK) {
273             LOGE("GetNativeTokenInfo failed.");
274             return ERR_DM_FAILED;
275         }
276         processName = std::move(tokenInfo.processName);
277     } else {
278         LOGE("GetCallerProcessName failed, unsupported process.");
279         return ERR_DM_FAILED;
280     }
281 
282     LOGI("Get process name: %{public}s success.", processName.c_str());
283     return DM_OK;
284 }
285 
GetTokenIdByBundleName(int32_t userId,std::string & bundleName,int64_t & tokenId)286 int32_t AppManager::GetTokenIdByBundleName(int32_t userId, std::string &bundleName, int64_t &tokenId)
287 {
288     int32_t ret = GetNativeTokenIdByName(bundleName, tokenId);
289     if (ret == DM_OK) {
290         return DM_OK;
291     }
292     ret = GetHapTokenIdByName(userId, bundleName, 0, tokenId);
293     if (ret != DM_OK) {
294         LOGE("get tokenId by bundleName failed %{public}s", GetAnonyString(bundleName).c_str());
295     }
296     return ret;
297 }
298 
GetBundleNameForSelf(std::string & bundleName)299 int32_t AppManager::GetBundleNameForSelf(std::string &bundleName)
300 {
301     sptr<AppExecFwk::IBundleMgr> bundleManager = nullptr;
302     if (!GetBundleManagerProxy(bundleManager)) {
303         LOGE("get bundleManager failed.");
304         return ERR_DM_GET_BMS_FAILED;
305     }
306     if (bundleManager == nullptr) {
307         LOGE("bundleManager is nullptr.");
308         return ERR_DM_GET_BMS_FAILED;
309     }
310     AppExecFwk::BundleInfo bundleInfo;
311     int32_t flags = static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION);
312     int32_t ret = static_cast<int32_t>(bundleManager->GetBundleInfoForSelf(flags, bundleInfo));
313     if (ret != ERR_OK) {
314         LOGE("failed, ret=%{public}d.", ret);
315         return ERR_DM_GET_BUNDLE_NAME_FAILED;
316     }
317     bundleName = bundleInfo.name;
318     return DM_OK;
319 }
320 } // namespace DistributedHardware
321 } // namespace OHOS
322