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 #ifndef IPC_COMMON_H 17 #define IPC_COMMON_H 18 19 #include <cinttypes> 20 #include <iremote_stub.h> 21 #include <optional> 22 #include <string> 23 24 #include "iam_common_defines.h" 25 #include "nocopyable.h" 26 27 namespace OHOS { 28 namespace UserIam { 29 namespace UserAuth { 30 enum Permission { 31 MANAGE_USER_IDM_PERMISSION, 32 USE_USER_IDM_PERMISSION, 33 ACCESS_USER_AUTH_INTERNAL_PERMISSION, 34 ACCESS_BIOMETRIC_PERMISSION, 35 ACCESS_AUTH_RESPOOL, 36 ENFORCE_USER_IDM, 37 SUPPORT_USER_AUTH, 38 IS_SYSTEM_APP, 39 CLEAR_REDUNDANCY_PERMISSION, 40 USE_USER_ACCESS_MANAGER, 41 USER_AUTH_FROM_BACKGROUND, 42 ENTERPRISE_DEVICE_MGR, 43 }; 44 45 class IpcCommon final : public NoCopyable { 46 public: 47 using Recipient = std::function<void()>; 48 static int32_t GetCallingUserId(IPCObjectStub &stub, int32_t &userId); 49 static int32_t GetActiveUserId(std::optional<int32_t> &userId); 50 static int32_t GetAllUserId(std::vector<int32_t> &userIds); 51 static int32_t GetUserTypeByUserId(int32_t userId, int32_t &userType); 52 static bool CheckPermission(IPCObjectStub &stub, Permission permission); 53 static uint32_t GetAccessTokenId(IPCObjectStub &stub); 54 static uint32_t GetTokenId(IPCObjectStub &stub); 55 static bool GetCallerName(IPCObjectStub &stub, std::string &callerName, int32_t &callerType); 56 static bool GetCallingAppID(IPCObjectStub &stub, std::string &callingAppID); 57 static bool CheckForegroundApplication(const std::string &bundleName); 58 static bool IsOsAccountVerified(int32_t userId); 59 static int32_t GetDirectCallerType(IPCObjectStub &stub); 60 class PeerDeathRecipient final : public IPCObjectProxy::DeathRecipient { 61 public: PeerDeathRecipient(Recipient && recipient)62 explicit PeerDeathRecipient(Recipient &&recipient) : recipient_(std::forward<Recipient>(recipient)) 63 { 64 } 65 ~PeerDeathRecipient() override = default; OnRemoteDied(const wptr<IRemoteObject> & object)66 void OnRemoteDied(const wptr<IRemoteObject> &object) override 67 { 68 if (auto remote = object.promote(); !remote) { 69 return; 70 } 71 if (recipient_) { 72 recipient_(); 73 } 74 }; 75 76 private: 77 Recipient recipient_; 78 }; 79 80 private: 81 static bool CheckNativeCallingProcessWhiteList(IPCObjectStub &stub, Permission permission); 82 static bool CheckDirectCallerAndFirstCallerIfSet(IPCObjectStub &stub, const std::string &permission); 83 static bool CheckDirectCaller(IPCObjectStub &stub, const std::string &permission); 84 static bool CheckCallerIsSystemApp(IPCObjectStub &stub); 85 static std::vector<std::pair<int32_t, std::string>> GetWhiteLists(Permission permission); 86 }; 87 } // namespace UserAuth 88 } // namespace UserIam 89 } // namespace OHOS 90 #endif // IPC_COMMON_H