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 
IsMediatoolOperationType(MediaLibraryCommand &cmd, const std::string &openFileNode)43 static bool IsMediatoolOperationType(MediaLibraryCommand &cmd, const std::string &openFileNode)
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::OPEN && openFileNode.find('w') == string::npos);
50 }
51 
IsDeveloperMediaTool(MediaLibraryCommand &cmd, const std::string openFileNode)52 bool IsDeveloperMediaTool(MediaLibraryCommand &cmd, const std::string openFileNode)
53 {
54     if (!PermissionUtils::IsRootShell() &&
55         !(PermissionUtils::IsHdcShell() && IsMediatoolOperationType(cmd, openFileNode))) {
56         MEDIA_ERR_LOG("Mediatool permission check failed: target is not root");
57         return false;
58     }
59     if (!OHOS::system::GetBoolParameter("const.security.developermode.state", true)) {
60         MEDIA_ERR_LOG("Mediatool permission check failed: target is not in developer mode");
61         return false;
62     }
63     return true;
64 }
65 
ConvertPermResult(bool isPermSuccess)66 int32_t ConvertPermResult(bool isPermSuccess)
67 {
68     return isPermSuccess ? E_SUCCESS : E_PERMISSION_DENIED;
69 }
70 
71 } // namespace name
72 
73