• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * Description: add permission utils
15  * Author: zhangge
16  * Create: 2023-05-11
17  */
18 #include "permission.h"
19 
20 #include <string>
21 #include <unistd.h>
22 
23 #include "cast_engine_log.h"
24 #include "ipc_skeleton.h"
25 #include "accesstoken_kit.h"
26 
27 using OHOS::Security::AccessToken::AccessTokenID;
28 using OHOS::Security::AccessToken::AccessTokenKit;
29 using OHOS::Security::AccessToken::PERMISSION_GRANTED;
30 
31 namespace OHOS {
32 namespace CastEngine {
33 namespace CastEngineService {
34 DEFINE_CAST_ENGINE_LABEL("Cast-Permission");
35 
36 namespace {
GetPermissionDescription(const std::string & permission)37 std::string GetPermissionDescription(const std::string &permission)
38 {
39     if (permission == "ohos.permission.ACCESS_CAST_ENGINE_MIRROR") {
40         return "Mirror permission";
41     }
42     if (permission == "ohos.permission.ACCESS_CAST_ENGINE_STREAM") {
43         return "Stream permission";
44     }
45     return "Unkown permission";
46 }
47 
CheckPermission(const std::string & permission)48 bool CheckPermission(const std::string &permission)
49 {
50     CLOGE("%{public}s succ", GetPermissionDescription(permission).c_str());
51     return true;
52 }
53 } // namespace
54 
55 std::mutex Permission::pidLock_;
56 std::vector<pid_t> Permission::pids_;
57 int32_t Permission::appUid_;
58 uint32_t Permission::appTokenId_;
59 int32_t Permission::appPid_;
60 
CheckMirrorPermission()61 bool Permission::CheckMirrorPermission()
62 {
63     return CheckPermission("ohos.permission.ACCESS_CAST_ENGINE_MIRROR");
64 }
65 
CheckStreamPermission()66 bool Permission::CheckStreamPermission()
67 {
68     return CheckPermission("ohos.permission.ACCESS_CAST_ENGINE_STREAM");
69 }
70 
SavePid(pid_t pid)71 void Permission::SavePid(pid_t pid)
72 {
73     std::lock_guard<std::mutex> lock(pidLock_);
74     CLOGD("save pid is %{public}d", pid);
75     if (std::find_if(pids_.begin(), pids_.end(), [pid](pid_t element) { return element == pid; }) == pids_.end()) {
76         pids_.push_back(pid);
77     }
78 }
79 
RemovePid(pid_t pid)80 void Permission::RemovePid(pid_t pid)
81 {
82     std::lock_guard<std::mutex> lock(pidLock_);
83     CLOGD("remove pid is %{public}d", pid);
84     auto iter = std::find_if(pids_.begin(), pids_.end(), [pid](pid_t element) { return element == pid; });
85     if (iter != pids_.end()) {
86         pids_.erase(iter);
87     }
88 }
89 
ClearPids()90 void Permission::ClearPids()
91 {
92     std::lock_guard<std::mutex> lock(pidLock_);
93     pids_.clear();
94 }
95 
CheckPidPermission()96 bool Permission::CheckPidPermission()
97 {
98     std::lock_guard<std::mutex> lock(pidLock_);
99     pid_t pid = IPCSkeleton::GetCallingPid();
100     pid_t myPid = getpid();
101     CLOGD("Calling pid is %{public}d, my pid is %{public}d", pid, myPid);
102     if (pid == myPid || pid == 0) { // 0 means role is proxy
103         return true;
104     }
105 
106     auto it = std::find_if(pids_.begin(), pids_.end(), [pid](pid_t element) { return element == pid; });
107     if (it == pids_.end()) {
108         CLOGE("pid(%{public}d) is illegal", pid);
109         return false;
110     }
111     return true;
112 }
113 
SaveMirrorAppInfo(std::tuple<int32_t,uint32_t,int32_t> appInfo)114 void Permission::SaveMirrorAppInfo(std::tuple<int32_t, uint32_t, int32_t> appInfo)
115 {
116     std::tie(appUid_, appTokenId_, appPid_) = appInfo;
117     CLOGD("appUid %{public}d, appTokenId %{public}u, appPid %{public}d", appUid_, appTokenId_, appPid_);
118 }
119 
GetMirrorAppInfo()120 std::tuple<int32_t, uint32_t, int32_t> Permission::GetMirrorAppInfo()
121 {
122     return {appUid_, appTokenId_, appPid_};
123 }
124 } // namespace CastEngineService
125 } // namespace CastEngine
126 } // namespace OHOS
127