1 /*
2  * Copyright (C) 2021 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 "ipc_skeleton.h"
17 
18 #include <cstdint>
19 #include <sys/types.h>
20 
21 #include "access_token_adapter.h"
22 #include "accesstoken_kit.h"
23 #include "iosfwd"
24 #include "ipc_process_skeleton.h"
25 #include "ipc_thread_skeleton.h"
26 #include "ipc_types.h"
27 #include "iremote_invoker.h"
28 #include "iremote_object.h"
29 #include "refbase.h"
30 #include "string"
31 #include "tokenInfo.h"
32 #include "unistd.h"
33 #include "utils_log.h"
34 
35 int32_t TokenInfo::tokenid;
36 
37 namespace OHOS {
38 using namespace Security::AccessToken;
39 #ifdef CONFIG_IPC_SINGLE
40 using namespace IPC_SINGLE;
41 #endif
42 
GetCallingUid()43 pid_t IPCSkeleton::GetCallingUid()
44 {
45     return 0;
46 }
47 
GetCallingTokenID()48 uint32_t IPCSkeleton::GetCallingTokenID()
49 {
50     int ret = 0;
51     switch (TokenInfo::GetTokenId()) {
52         case TOKEN_HAP: {
53             ret = TOKEN_HAP;
54             break;
55         }
56         case TOKEN_NATIVE: {
57             ret = TOKEN_NATIVE;
58             break;
59         }
60         case TOKEN_SHELL: {
61             ret = TOKEN_NATIVE;
62             break;
63         }
64         default: {
65             ret = -1;
66             break;
67         }
68     }
69     return ret;
70 }
71 
GetCallingFullTokenID()72 uint64_t IPCSkeleton::GetCallingFullTokenID()
73 {
74     IRemoteInvoker *invoker = IPCThreadSkeleton::GetActiveInvoker();
75     if (invoker != nullptr) {
76         return invoker->GetCallerTokenID();
77     }
78     return GetSelfTokenID();
79 }
80 
GetSelfTokenID()81 uint64_t IPCSkeleton::GetSelfTokenID()
82 {
83     IRemoteInvoker *invoker = IPCThreadSkeleton::GetDefaultInvoker();
84     if (invoker != nullptr) {
85         return invoker->GetSelfTokenID();
86     }
87     return 0;
88 }
89 } // namespace OHOS
90