1 /*
2  * Copyright (c) 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 "session_ipc_adapter.h"
17 
18 #include <string>
19 
20 #include "accesstoken_kit.h"
21 #include "ipc_skeleton.h"
22 #include "tokenid_kit.h"
23 #include "trans_log.h"
24 
CheckIsSystemService(void)25 bool CheckIsSystemService(void)
26 {
27     uint32_t tokenId = OHOS::IPCSkeleton::GetSelfTokenID();
28     auto type = OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
29     TRANS_LOGD(TRANS_SDK, "access token type=%{public}d", type);
30     if (type == OHOS::Security::AccessToken::TOKEN_NATIVE) {
31         return true;
32     }
33     return false;
34 }
35 
CheckIsNormalApp(const char * sessionName)36 bool CheckIsNormalApp(const char *sessionName)
37 {
38     #define DBINDER_BUS_NAME_PREFIX "DBinder"
39     //The authorization of dbind is granted through Samgr, and there is no control here
40     if (strncmp(sessionName, DBINDER_BUS_NAME_PREFIX, strlen(DBINDER_BUS_NAME_PREFIX)) == 0) {
41         return false;
42     }
43     uint64_t selfToken = OHOS::IPCSkeleton::GetSelfTokenID();
44     auto tokenType = OHOS::Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(static_cast<uint32_t>(selfToken));
45     if (tokenType == OHOS::Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE) {
46         return false;
47     } else if (tokenType == OHOS::Security::AccessToken::ATokenTypeEnum::TOKEN_HAP) {
48         bool isSystemApp = OHOS::Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(selfToken);
49         if (isSystemApp) {
50             return false;
51         }
52     }
53     TRANS_LOGI(TRANS_SDK, "is normal app");
54     return true;
55 }