1 /*
2 * Copyright (c) 2021-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 "securitylabel_n_exporter.h"
17
18 #include <singleton.h>
19 #include <tuple>
20
21 #include "filemgmt_libhilog.h"
22 #include "security_label.h"
23
24 namespace OHOS {
25 namespace FileManagement {
26 namespace ModuleSecurityLabel {
27 using namespace OHOS::FileManagement::LibN;
28 using namespace std;
29
SetSecurityLabel(napi_env env,napi_callback_info info)30 napi_value SetSecurityLabel(napi_env env, napi_callback_info info)
31 {
32 NFuncArg funcArg(env, info);
33 if (!funcArg.InitArgs(static_cast<int>(NARG_CNT::TWO), static_cast<int>(NARG_CNT::THREE))) {
34 HILOGE("Number of arguments unmatched");
35 NError(EINVAL).ThrowErr(env);
36 return nullptr;
37 }
38
39 bool succ = false;
40 std::unique_ptr<char []> path;
41 std::unique_ptr<char []> dataLevel;
42 tie(succ, path, std::ignore) = NVal(env, funcArg[static_cast<int>(NARG_POS::FIRST)]).ToUTF8StringPath();
43 if (!succ) {
44 HILOGE("Invalid path");
45 NError(EINVAL).ThrowErr(env);
46 return nullptr;
47 }
48 tie(succ, dataLevel, std::ignore) = NVal(env, funcArg[static_cast<int>(NARG_POS::SECOND)]).ToUTF8String();
49 if (!succ) {
50 HILOGE("Invalid dataLevel");
51 NError(EINVAL).ThrowErr(env);
52 return nullptr;
53 }
54 std::string dataLevelString(dataLevel.get());
55 if (DATA_LEVEL.find(dataLevelString) == DATA_LEVEL.end()) {
56 HILOGE("Invalid Argument of dataLevelEnum");
57 NError(EINVAL).ThrowErr(env);
58 return nullptr;
59 }
60 auto cbExec = [pathString = string(path.get()), dataLevelString]() -> NError {
61 bool ret = SecurityLabel::SetSecurityLabel(pathString, dataLevelString);
62 if (!ret) {
63 return NError(errno);
64 }
65 return NError(ERRNO_NOERR);
66 };
67 auto cbComplete = [](napi_env env, NError err) -> NVal {
68 if (err) {
69 return { env, err.GetNapiErr(env) };
70 }
71 return NVal::CreateUndefined(env);
72 };
73 static const std::string PROCEDURE_NAME = "SetSecurityLabel";
74 NVal thisVar(env, funcArg.GetThisVar());
75 if (funcArg.GetArgc() == static_cast<size_t>(NARG_CNT::TWO)) {
76 return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_;
77 } else {
78 NVal cb(env, funcArg[static_cast<int>(NARG_POS::THIRD)]);
79 return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_;
80 }
81 }
82
SetSecurityLabelSync(napi_env env,napi_callback_info info)83 napi_value SetSecurityLabelSync(napi_env env, napi_callback_info info)
84 {
85 NFuncArg funcArg(env, info);
86 if (!funcArg.InitArgs(static_cast<int>(NARG_CNT::TWO), static_cast<int>(NARG_CNT::THREE))) {
87 HILOGE("Number of arguments unmatched");
88 NError(EINVAL).ThrowErr(env);
89 return nullptr;
90 }
91
92 bool succ = false;
93 std::unique_ptr<char []> path;
94 std::unique_ptr<char []> dataLevel;
95 tie(succ, path, std::ignore) = NVal(env, funcArg[static_cast<int>(NARG_POS::FIRST)]).ToUTF8StringPath();
96 if (!succ) {
97 HILOGE("Invalid path");
98 NError(EINVAL).ThrowErr(env);
99 return nullptr;
100 }
101
102 tie(succ, dataLevel, std::ignore) = NVal(env, funcArg[static_cast<int>(NARG_POS::SECOND)]).ToUTF8String();
103 if (!succ) {
104 HILOGE("Invalid dataLevel");
105 NError(EINVAL).ThrowErr(env);
106 return nullptr;
107 }
108
109 if (DATA_LEVEL.find(dataLevel.get()) == DATA_LEVEL.end()) {
110 HILOGE("Invalid Argument of dataLevelEnum");
111 NError(EINVAL).ThrowErr(env);
112 return nullptr;
113 }
114
115 bool ret = SecurityLabel::SetSecurityLabel(path.get(), dataLevel.get());
116 if (!ret) {
117 NError(errno).ThrowErr(env);
118 return nullptr;
119 }
120
121 return NVal::CreateUndefined(env).val_;
122 }
123
GetSecurityLabel(napi_env env,napi_callback_info info)124 napi_value GetSecurityLabel(napi_env env, napi_callback_info info)
125 {
126 NFuncArg funcArg(env, info);
127 if (!funcArg.InitArgs(static_cast<int>(NARG_CNT::ONE), static_cast<int>(NARG_CNT::TWO))) {
128 HILOGE("Number of arguments unmatched");
129 NError(EINVAL).ThrowErr(env);
130 return nullptr;
131 }
132
133 bool succ = false;
134 std::unique_ptr<char []> path;
135 tie(succ, path, std::ignore) = NVal(env, funcArg[static_cast<int>(NARG_POS::FIRST)]).ToUTF8StringPath();
136 if (!succ) {
137 HILOGE("Invalid path");
138 NError(EINVAL).ThrowErr(env);
139 return nullptr;
140 }
141 auto result = std::make_shared<string>();
142 std::string pathString(path.get());
143 auto cbExec = [pathString = move(pathString), result]() -> NError {
144 *result = SecurityLabel::GetSecurityLabel(pathString);
145 return NError(ERRNO_NOERR);
146 };
147 auto cbComplete = [result](napi_env env, NError err) -> NVal {
148 if (err) {
149 return { env, err.GetNapiErr(env) };
150 }
151 return { NVal::CreateUTF8String(env, *result) };
152 };
153
154 static const std::string PROCEDURE_NAME = "GetSecurityLabel";
155 NVal thisVar(env, funcArg.GetThisVar());
156 if (funcArg.GetArgc() == static_cast<size_t>(NARG_CNT::ONE)) {
157 return NAsyncWorkPromise(env, thisVar).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_;
158 } else {
159 NVal cb(env, funcArg[static_cast<int>(NARG_POS::SECOND)]);
160 if (cb.TypeIs(napi_function)) {
161 return NAsyncWorkCallback(env, thisVar, cb).Schedule(PROCEDURE_NAME, cbExec, cbComplete).val_;
162 }
163 }
164 return NVal::CreateUndefined(env).val_;
165 }
166
GetSecurityLabelSync(napi_env env,napi_callback_info info)167 napi_value GetSecurityLabelSync(napi_env env, napi_callback_info info)
168 {
169 NFuncArg funcArg(env, info);
170 if (!funcArg.InitArgs(static_cast<int>(NARG_CNT::ONE), static_cast<int>(NARG_CNT::TWO))) {
171 HILOGE("Number of arguments unmatched");
172 NError(EINVAL).ThrowErr(env);
173 return nullptr;
174 }
175
176 bool succ = false;
177 std::unique_ptr<char []> path;
178 tie(succ, path, std::ignore) = NVal(env, funcArg[static_cast<int>(NARG_POS::FIRST)]).ToUTF8StringPath();
179 if (!succ) {
180 HILOGE("Invalid path");
181 NError(EINVAL).ThrowErr(env);
182 return nullptr;
183 }
184
185 std::string result = SecurityLabel::GetSecurityLabel(path.get());
186 return NVal::CreateUTF8String(env, result).val_;
187 }
188 } // namespace ModuleSecurityLabel
189 } // namespace FileManagement
190 } // namespace OHOS