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 #include "security_policy.h" 17 18 #include "logger.h" 19 #include "rdb_errno.h" 20 #include "security_label.h" 21 22 namespace OHOS { 23 namespace NativeRdb { SetFileSecurityLevel(const std::string & filePath,const std::string & securityLevel)24int SecurityPolicy::SetFileSecurityLevel(const std::string &filePath, const std::string &securityLevel) 25 { 26 bool result = DistributedFS::ModuleSecurityLabel::SecurityLabel::SetSecurityLabel(filePath, securityLevel); 27 return result ? E_OK : E_ERROR; 28 } 29 GetSecurityLevelValue(SecurityLevel securityLevel)30std::string SecurityPolicy::GetSecurityLevelValue(SecurityLevel securityLevel) 31 { 32 switch (securityLevel) { 33 case SecurityLevel::S1: 34 return "s1"; 35 case SecurityLevel::S2: 36 return "s2"; 37 case SecurityLevel::S3: 38 return "s3"; 39 case SecurityLevel::S4: 40 return "s4"; 41 default: 42 return ""; 43 } 44 } 45 GetFileSecurityLevel(const std::string & filePath)46std::string SecurityPolicy::GetFileSecurityLevel(const std::string &filePath) 47 { 48 return DistributedFS::ModuleSecurityLabel::SecurityLabel::GetSecurityLabel(filePath); 49 } 50 SetSecurityLabel(const RdbStoreConfig & config)51int SecurityPolicy::SetSecurityLabel(const RdbStoreConfig &config) 52 { 53 if (config.GetStorageMode() != StorageMode::MODE_MEMORY && config.GetSecurityLevel() != SecurityLevel::LAST) { 54 std::string currentLevel = GetFileSecurityLevel(config.GetPath()); 55 std::string toSetLevel = GetSecurityLevelValue(config.GetSecurityLevel()); 56 if (currentLevel.empty()) { 57 return SetFileSecurityLevel(config.GetPath(), toSetLevel); 58 } 59 return currentLevel == toSetLevel ? E_OK : E_ERROR; 60 } 61 return E_OK; 62 } 63 } // namespace NativeRdb 64 } // namespace OHOS