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