1 /*
2  * Copyright (c) 2022 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 "time_permission.h"
17 
18 #include "accesstoken_kit.h"
19 #include "ipc_skeleton.h"
20 #include "tokenid_kit.h"
21 
22 namespace OHOS {
23 namespace MiscServices {
24 const std::string TimePermission::setTime = "ohos.permission.SET_TIME";
25 const std::string TimePermission::setTimeZone = "ohos.permission.SET_TIME_ZONE";
CheckCallingPermission(const std::string & permissionName)26 bool TimePermission::CheckCallingPermission(const std::string &permissionName)
27 {
28     if (permissionName.empty()) {
29         TIME_HILOGE(TIME_MODULE_COMMON, "permission check failed, permission name is empty.");
30         return false;
31     }
32     auto callerToken = IPCSkeleton::GetCallingTokenID();
33     int result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, permissionName);
34     if (result != Security::AccessToken::PERMISSION_GRANTED) {
35         TIME_HILOGE(TIME_MODULE_COMMON, "permission check failed, result:%{public}d, permission:%{public}s",
36             result, permissionName.c_str());
37         return false;
38     }
39     return true;
40 }
41 
CheckProxyCallingPermission()42 bool TimePermission::CheckProxyCallingPermission()
43 {
44     auto callerToken = IPCSkeleton::GetCallingTokenID();
45     auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callerToken);
46     return (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE ||
47             tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL);
48 }
49 
CheckSystemUidCallingPermission(uint64_t tokenId)50 bool TimePermission::CheckSystemUidCallingPermission(uint64_t tokenId)
51 {
52     if (CheckProxyCallingPermission()) {
53         return true;
54     }
55     return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(tokenId);
56 }
57 } // namespace MiscServices
58 } // namespace OHOS