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 */
15
16 #define MLOG_TAG "PermissionHandlerCommon"
17
18 #include "permission_common.h"
19
20 #include <cstdlib>
21
22 #include "medialibrary_bundle_manager.h"
23 #include "medialibrary_operation.h"
24 #include "parameters.h"
25 #include "permission_utils.h"
26 #include "mediatool_uri.h"
27
28 using namespace std;
29
30 namespace OHOS::Media {
31
GetClientAppId()32 string GetClientAppId()
33 {
34 string bundleName = MediaLibraryBundleManager::GetInstance()->GetClientBundleName();
35 return PermissionUtils::GetAppIdByBundleName(bundleName);
36 }
37
IsMediatoolOperation(MediaLibraryCommand & cmd)38 bool IsMediatoolOperation(MediaLibraryCommand &cmd)
39 {
40 return cmd.GetOprnObject() == OperationObject::TOOL_PHOTO || cmd.GetOprnObject() == OperationObject::TOOL_AUDIO ||
41 cmd.GetOprnObject() == OperationObject::TOOL_ALBUM || cmd.GetOprnType() == Media::OperationType::DELETE_TOOL;
42 }
43
IsHdcShellMediatoolCommand(MediaLibraryCommand & cmd,const std::string & openFileMode)44 static bool IsHdcShellMediatoolCommand(MediaLibraryCommand &cmd, const std::string &openFileMode)
45 {
46 return cmd.GetOprnType() == Media::OperationType::TOOL_QUERY_BY_DISPLAY_NAME ||
47 cmd.GetOprnType() == Media::OperationType::DELETE_TOOL ||
48 cmd.GetOprnType() == Media::OperationType::UPDATE ||
49 cmd.GetOprnType() == Media::OperationType::ALBUM_DELETE_ASSETS ||
50 (cmd.GetOprnType() == Media::OperationType::DELETE &&
51 cmd.GetOprnObject() == OperationObject::FILESYSTEM_AUDIO) ||
52 (cmd.GetOprnType() == Media::OperationType::OPEN && openFileMode.find('w') == string::npos) ||
53 cmd.GetOprnType() == Media::OperationType::LS_MEDIA_FILES;
54 }
55
IsDeveloperMediaTool(MediaLibraryCommand & cmd,const std::string & openFileMode)56 bool IsDeveloperMediaTool(MediaLibraryCommand &cmd, const std::string &openFileMode)
57 {
58 if (!PermissionUtils::IsRootShell() &&
59 !(PermissionUtils::IsHdcShell() && IsHdcShellMediatoolCommand(cmd, openFileMode))) {
60 MEDIA_ERR_LOG("Mediatool permission check failed: target is not root");
61 return false;
62 }
63 if (!OHOS::system::GetBoolParameter("const.security.developermode.state", true)) {
64 MEDIA_ERR_LOG("Mediatool permission check failed: target is not in developer mode");
65 return false;
66 }
67 return true;
68 }
69
ConvertPermResult(bool isPermSuccess)70 int32_t ConvertPermResult(bool isPermSuccess)
71 {
72 return isPermSuccess ? E_SUCCESS : E_PERMISSION_DENIED;
73 }
74
75 } // namespace name
76
77