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 "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 DM_IMPLEMENT_SINGLE_INSTANCE(AppManager);
36
GetAppId()37 const std::string AppManager::GetAppId()
38 {
39 std::string appId = "";
40 AccessTokenID tokenId = IPCSkeleton::GetCallingTokenID();
41 if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) {
42 LOGD("The caller is not token_hap.");
43 return appId;
44 }
45 sptr<AppExecFwk::IBundleMgr> bundleManager = nullptr;
46 if (!GetBundleManagerProxy(bundleManager)) {
47 LOGE("get bundleManager failed.");
48 return appId;
49 }
50 if (bundleManager == nullptr) {
51 LOGE("bundleManager is nullptr.");
52 return appId;
53 }
54 int32_t userId = -1;
55 if (AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId) != ERR_OK) {
56 LOGE("GetAppIdByCallingUid QueryActiveOsAccountIds failed.");
57 return appId;
58 }
59 std::string BundleName = "";
60 bundleManager->GetNameForUid(IPCSkeleton::GetCallingUid(), BundleName);
61 AppExecFwk::BundleInfo bundleInfo;
62 int32_t ret = bundleManager->GetBundleInfoV9(
63 BundleName, static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_SIGNATURE_INFO),
64 bundleInfo, userId);
65 if (ret != 0) {
66 LOGE(" GetBundleInfoV9 failed %{public}d.", ret);
67 return appId;
68 }
69 appId = bundleInfo.appId;
70 return appId;
71 }
72
RegisterCallerAppId(const std::string & pkgName)73 void AppManager::RegisterCallerAppId(const std::string &pkgName)
74 {
75 if (pkgName.empty()) {
76 LOGE("Invalid parameter, pkgName is empty.");
77 return;
78 }
79 std::string appId = GetAppId();
80 if (appId.empty()) {
81 LOGE("PkgName %{public}s get appid failed.", pkgName.c_str());
82 return;
83 }
84 LOGI("PkgName %{public}s, appId %{public}s.", pkgName.c_str(), GetAnonyString(appId).c_str());
85 std::lock_guard<std::mutex> lock(appIdMapLock_);
86 appIdMap_[pkgName] = appId;
87 }
88
UnRegisterCallerAppId(const std::string & pkgName)89 void AppManager::UnRegisterCallerAppId(const std::string &pkgName)
90 {
91 if (pkgName.empty()) {
92 LOGE("Invalid parameter, pkgName is empty.");
93 return;
94 }
95 LOGI("PkgName %{public}s.", pkgName.c_str());
96 std::lock_guard<std::mutex> lock(appIdMapLock_);
97 if (appIdMap_.find(pkgName) == appIdMap_.end()) {
98 LOGE("AppIdMap not find pkgName.");
99 return;
100 }
101 appIdMap_.erase(pkgName);
102 }
103
GetAppIdByPkgName(const std::string & pkgName,std::string & appId)104 int32_t AppManager::GetAppIdByPkgName(const std::string &pkgName, std::string &appId)
105 {
106 if (pkgName.empty()) {
107 LOGE("Invalid parameter, pkgName is empty.");
108 return ERR_DM_INPUT_PARA_INVALID;
109 }
110 LOGD("PkgName %{public}s.", pkgName.c_str());
111 std::lock_guard<std::mutex> lock(appIdMapLock_);
112 if (appIdMap_.find(pkgName) == appIdMap_.end()) {
113 LOGD("AppIdMap not find pkgName.");
114 return ERR_DM_FAILED;
115 }
116 appId = appIdMap_[pkgName];
117 return DM_OK;
118 }
119
GetBundleManagerProxy(sptr<AppExecFwk::IBundleMgr> & bundleManager)120 bool AppManager::GetBundleManagerProxy(sptr<AppExecFwk::IBundleMgr> &bundleManager)
121 {
122 sptr<ISystemAbilityManager> systemAbilityManager =
123 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
124 if (systemAbilityManager == nullptr) {
125 LOGE("GetBundleManagerProxy Failed to get system ability mgr.");
126 return false;
127 }
128 sptr<IRemoteObject> remoteObject =
129 systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
130 if (remoteObject == nullptr) {
131 LOGE("GetBundleManagerProxy Failed to get bundle manager service.");
132 return false;
133 }
134 bundleManager = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
135 if (bundleManager == nullptr) {
136 LOGE("bundleManager is nullptr");
137 return false;
138 }
139 return true;
140 }
141
IsSystemSA()142 bool AppManager::IsSystemSA()
143 {
144 AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
145 if (tokenCaller == 0) {
146 LOGE("IsSystemSA GetCallingTokenID error.");
147 return false;
148 }
149 ATokenTypeEnum tokenTypeFlag = AccessTokenKit::GetTokenTypeFlag(tokenCaller);
150 if (tokenTypeFlag == ATokenTypeEnum::TOKEN_NATIVE) {
151 return true;
152 }
153 return false;
154 }
155
GetCallerName(bool isSystemSA,std::string & callerName)156 int32_t AppManager::GetCallerName(bool isSystemSA, std::string &callerName)
157 {
158 AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
159 if (tokenCaller == 0) {
160 LOGE("GetCallingTokenID error.");
161 return ERR_DM_FAILED;
162 }
163 LOGI("tokenCaller ID == %{public}s", GetAnonyInt32(tokenCaller).c_str());
164 ATokenTypeEnum tokenTypeFlag = AccessTokenKit::GetTokenTypeFlag(tokenCaller);
165 if (tokenTypeFlag == ATokenTypeEnum::TOKEN_HAP) {
166 isSystemSA = false;
167 HapTokenInfo tokenInfo;
168 if (AccessTokenKit::GetHapTokenInfo(tokenCaller, tokenInfo) != EOK) {
169 LOGE("GetHapTokenInfo failed.");
170 return ERR_DM_FAILED;
171 }
172 callerName = std::move(tokenInfo.bundleName);
173 } else if (tokenTypeFlag == ATokenTypeEnum::TOKEN_NATIVE) {
174 isSystemSA = true;
175 NativeTokenInfo tokenInfo;
176 if (AccessTokenKit::GetNativeTokenInfo(tokenCaller, tokenInfo) != EOK) {
177 LOGE("GetNativeTokenInfo failed.");
178 return ERR_DM_FAILED;
179 }
180 callerName = std::move(tokenInfo.processName);
181 } else {
182 LOGE("failed, unsupported process.");
183 return ERR_DM_FAILED;
184 }
185 return DM_OK;
186 }
187
GetNativeTokenIdByName(std::string & processName,int64_t & tokenId)188 int32_t AppManager::GetNativeTokenIdByName(std::string &processName, int64_t &tokenId)
189 {
190 AccessTokenID nativeTokenId = AccessTokenKit::GetNativeTokenId(processName);
191 if (nativeTokenId == INVALID_TOKENID) {
192 LOGE("GetNativeTokenId failed.");
193 return ERR_DM_FAILED;
194 }
195 tokenId = static_cast<int64_t>(nativeTokenId);
196 return DM_OK;
197 }
198
GetHapTokenIdByName(int32_t userId,std::string & bundleName,int32_t instIndex,int64_t & tokenId)199 int32_t AppManager::GetHapTokenIdByName(int32_t userId, std::string &bundleName, int32_t instIndex, int64_t &tokenId)
200 {
201 auto hapTokenId = AccessTokenKit::GetHapTokenID(userId, bundleName, instIndex);
202 if (hapTokenId == 0) {
203 LOGE("GetHapTokenId failed.");
204 return ERR_DM_FAILED;
205 }
206 tokenId = static_cast<int64_t>(hapTokenId);
207 return DM_OK;
208 }
209
GetCallerProcessName(std::string & processName)210 int32_t AppManager::GetCallerProcessName(std::string &processName)
211 {
212 AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
213 if (tokenCaller == 0) {
214 LOGE("GetCallerProcessName GetCallingTokenID error.");
215 return ERR_DM_FAILED;
216 }
217 LOGI("GetCallerProcessName::tokenCaller ID == %{public}s", GetAnonyInt32(tokenCaller).c_str());
218 ATokenTypeEnum tokenTypeFlag = AccessTokenKit::GetTokenTypeFlag(tokenCaller);
219 if (tokenTypeFlag == ATokenTypeEnum::TOKEN_HAP) {
220 HapTokenInfo tokenInfo;
221 if (AccessTokenKit::GetHapTokenInfo(tokenCaller, tokenInfo) != EOK) {
222 LOGE("GetHapTokenInfo failed.");
223 return ERR_DM_FAILED;
224 }
225 processName = std::move(tokenInfo.bundleName);
226 uint64_t fullTokenId = IPCSkeleton::GetCallingFullTokenID();
227 if (!OHOS::Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId)) {
228 LOGE("GetCallerProcessName %{public}s not system hap.", processName.c_str());
229 return ERR_DM_FAILED;
230 }
231 } else if (tokenTypeFlag == ATokenTypeEnum::TOKEN_NATIVE) {
232 NativeTokenInfo tokenInfo;
233 if (AccessTokenKit::GetNativeTokenInfo(tokenCaller, tokenInfo) != EOK) {
234 LOGE("GetNativeTokenInfo failed.");
235 return ERR_DM_FAILED;
236 }
237 processName = std::move(tokenInfo.processName);
238 } else {
239 LOGE("GetCallerProcessName failed, unsupported process.");
240 return ERR_DM_FAILED;
241 }
242
243 LOGI("Get process name: %{public}s success.", processName.c_str());
244 return DM_OK;
245 }
246 } // namespace DistributedHardware
247 } // namespace OHOS
248