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 "file_manager_service_stub.h"
17
18 #include "accesstoken_kit.h"
19 #include "file_manager_service_def.h"
20 #include "file_manager_service_errno.h"
21 #include "file_manager_service.h"
22 #include "ipc_singleton.h"
23 #include "ipc_skeleton.h"
24 #include "log.h"
25 #include "media_file_utils.h"
26 #include "oper_factory.h"
27 #include "sa_mgr_client.h"
28 #include "string_ex.h"
29 #include "system_ability_definition.h"
30 using namespace std;
31 namespace OHOS {
32 namespace FileManagerService {
GetEquipmentCode(uint32_t code)33 static int GetEquipmentCode(uint32_t code)
34 {
35 return (code >> EQUIPMENT_SHIFT) & CODE_MASK;
36 }
37
GetOperCode(uint32_t code)38 static int GetOperCode(uint32_t code)
39 {
40 return code & CODE_MASK;
41 }
42
OperProcess(uint32_t code,MessageParcel & data,MessageParcel & reply)43 int FileManagerServiceStub::OperProcess(uint32_t code, MessageParcel &data,
44 MessageParcel &reply)
45 {
46 int equipmentId = GetEquipmentCode(code);
47 int operCode = GetOperCode(code);
48 OperFactory factory = OperFactory();
49 auto fp = factory.GetFileOper(equipmentId);
50 if (fp == nullptr) {
51 ERR_LOG("OnRemoteRequest inner error %{public}d", code);
52 return FAIL;
53 }
54 int errCode = fp->OperProcess(operCode, data, reply);
55 return errCode;
56 }
57
CheckClientPermission(const std::string & permissionStr)58 bool CheckClientPermission(const std::string& permissionStr)
59 {
60 Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
61 int res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller,
62 permissionStr);
63 if (res != Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
64 ERR_LOG("Have no media permission");
65 return false;
66 }
67 ERR_LOG("permission check success");
68 return true;
69 }
70
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)71 int FileManagerServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
72 MessageParcel &reply, MessageOption &option)
73 {
74 // check whether request from fms proxy
75 if (data.ReadInterfaceToken() != GetDescriptor()) {
76 ERR_LOG("reject error remote request");
77 reply.WriteInt32(FAIL);
78 return FAIL;
79 }
80 // change permission string after finishing accessToken
81 string permission = "ohos.permission.READ_MEDIA";
82 if (!CheckClientPermission(permission)) {
83 ERR_LOG("checkpermission error FAIL");
84 reply.WriteInt32(FAIL);
85 return FAIL;
86 }
87 if (!MediaFileUtils::InitHelper(AsObject())) {
88 ERR_LOG("Init MediaLibraryDataAbility Helper error");
89 reply.WriteInt32(FAIL);
90 return FAIL;
91 }
92 // do request process
93 int32_t errCode = OperProcess(code, data, reply);
94 reply.WriteInt32(errCode);
95 return errCode;
96 }
97 } // namespace FileManagerService
98 } // namespace OHOS
99