• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 <functional>
17 #include <iostream>
18 #include <map>
19 #include <string>
20 #include <vector>
21 
22 #include "client/storage_manager_client.h"
23 #include "storage_daemon_client.h"
24 #include "storage_service_log.h"
25 #include "utils/file_utils.h"
26 
27 #ifdef SDC_TEST_ENABLE
28 constexpr int32_t ARG_CNT_4 = 4;
29 constexpr int32_t ARG_CNT_5 = 5;
30 #endif
31 
InitGlobalKey(const std::vector<std::string> & args)32 static int32_t InitGlobalKey(const std::vector<std::string> &args)
33 {
34     (void)args;
35     return OHOS::StorageDaemon::StorageDaemonClient::InitGlobalKey();
36 }
37 
InitMainUser(const std::vector<std::string> & args)38 static int32_t InitMainUser(const std::vector<std::string> &args)
39 {
40     (void)args;
41     return OHOS::StorageDaemon::StorageDaemonClient::InitGlobalUserKeys();
42 }
43 
44 #ifdef SDC_TEST_ENABLE
GenerateUserKeys(const std::vector<std::string> & args)45 static int32_t GenerateUserKeys(const std::vector<std::string> &args)
46 {
47     if (args.size() < ARG_CNT_5) {
48         LOGE("Parameter nums is less than 5, please retry");
49         return -EINVAL;
50     }
51     uint32_t userId;
52     uint32_t flags;
53     // 3 means take the fourth argument of args, 4 means take the fifth argument of args
54     if ((OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) ||
55         (OHOS::StorageDaemon::StringToUint32(args[4], flags) == false)) {
56         LOGE("Parameter input error, please retry");
57         return -EINVAL;
58     }
59     return OHOS::StorageDaemon::StorageDaemonClient::GenerateUserKeys(userId, flags);
60 }
61 
PrepareUserSpace(const std::vector<std::string> & args)62 static int32_t PrepareUserSpace(const std::vector<std::string> &args)
63 {
64     if (args.size() < ARG_CNT_5) {
65         LOGE("Parameter nums is less than 5, please retry");
66         return -EINVAL;
67     }
68     uint32_t userId;
69     uint32_t flags;
70     // 3 means take the fourth argument of args, 4 means take the fifth argument of args
71     if ((OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) ||
72         (OHOS::StorageDaemon::StringToUint32(args[4], flags) == false)) {
73         LOGE("Parameter input error, please retry");
74         return -EINVAL;
75     }
76     return OHOS::StorageDaemon::StorageDaemonClient::PrepareUserDirs(userId, flags);
77 }
78 
DeleteUserKeys(const std::vector<std::string> & args)79 static int32_t DeleteUserKeys(const std::vector<std::string> &args)
80 {
81     if (args.size() < ARG_CNT_4) {
82         LOGE("Parameter nums is less than 4, please retry");
83         return -EINVAL;
84     }
85     uint32_t userId;
86     // 3 means take the fourth argument of args
87     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) {
88         LOGE("Parameter input error, please retry");
89         return -EINVAL;
90     }
91     return OHOS::StorageDaemon::StorageDaemonClient::DeleteUserKeys(userId);
92 }
93 
DestroyUserSpace(const std::vector<std::string> & args)94 static int32_t DestroyUserSpace(const std::vector<std::string> &args)
95 {
96     if (args.size() < ARG_CNT_5) {
97         LOGE("Parameter nums is less than 5, please retry");
98         return -EINVAL;
99     }
100     uint32_t userId;
101     uint32_t flags;
102     // 3 means take the fourth argument of args, 4 means take the fifth argument of args
103     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false ||
104         OHOS::StorageDaemon::StringToUint32(args[4], flags) == false) {
105         LOGE("Parameter input error, please retry");
106         return -EINVAL;
107     }
108     return OHOS::StorageDaemon::StorageDaemonClient::DestroyUserDirs(userId, flags);
109 }
110 
UpdateUserAuth(const std::vector<std::string> & args)111 static int32_t UpdateUserAuth(const std::vector<std::string> &args)
112 {
113     if (args.size() < ARG_CNT_5) {
114         LOGE("Parameter nums is less than 5, please retry");
115         return -EINVAL;
116     }
117     uint32_t userId;
118     // 3 means take the fourth argument of args
119     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) {
120         LOGE("Parameter input error, please retry");
121         return -EINVAL;
122     }
123 
124     // 4 means take the fifth argument of args, 5 means take the sixth argument of args
125     if (args.size() == 6) {
126         std::vector<uint8_t> oldSecret(args[4].begin(), args[4].end());
127         std::vector<uint8_t> newSecret(args[5].begin(), args[5].end());
128         return OHOS::StorageDaemon::StorageDaemonClient::UpdateUserAuth(userId, 0, {}, oldSecret, newSecret);
129     }
130     std::vector<uint8_t> newSecret(args[4].begin(), args[4].end());
131     return OHOS::StorageDaemon::StorageDaemonClient::UpdateUserAuth(userId, 0, {}, {}, newSecret);
132 }
133 
ActiveUserKey(const std::vector<std::string> & args)134 static int32_t ActiveUserKey(const std::vector<std::string> &args)
135 {
136     if (args.size() < ARG_CNT_4) {
137         LOGE("Parameter nums is less than 4, please retry");
138         return -EINVAL;
139     }
140     uint32_t userId;
141     // 3 means take the fourth argument of args
142     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) {
143         LOGE("Parameter input error, please retry");
144         return -EINVAL;
145     }
146     // 4 means take the fifth argument of args
147     if (args.size() == 5) {
148         std::vector<uint8_t> secret(args[4].begin(), args[4].end());
149         return OHOS::StorageDaemon::StorageDaemonClient::ActiveUserKey(userId, {}, secret);
150     }
151     return OHOS::StorageDaemon::StorageDaemonClient::ActiveUserKey(userId, {}, {});
152 }
153 
InactiveUserKey(const std::vector<std::string> & args)154 static int32_t InactiveUserKey(const std::vector<std::string> &args)
155 {
156     if (args.size() < ARG_CNT_4) {
157         LOGE("Parameter nums is less than 4, please retry");
158         return -EINVAL;
159     }
160     uint32_t userId;
161     // 3 means take the fourth argument of args
162     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) {
163         LOGE("Parameter input error, please retry");
164         return -EINVAL;
165     }
166     return OHOS::StorageDaemon::StorageDaemonClient::InactiveUserKey(userId);
167 }
168 
LockUserScreen(const std::vector<std::string> & args)169 static int32_t LockUserScreen(const std::vector<std::string> &args)
170 {
171     if (args.size() < ARG_CNT_4) {
172         LOGE("Parameter nums is less than 4, please retry");
173         return -EINVAL;
174     }
175     uint32_t userId;
176     // 3 means take the fourth argument of args
177     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) {
178         LOGE("Parameter input error, please retry");
179         return -EINVAL;
180     }
181     return OHOS::StorageDaemon::StorageDaemonClient::LockUserScreen(userId);
182 }
183 
UnlockUserScreen(const std::vector<std::string> & args)184 static int32_t UnlockUserScreen(const std::vector<std::string> &args)
185 {
186     if (args.size() < ARG_CNT_4) {
187         LOGE("Parameter nums is less than 4, please retry");
188         return -EINVAL;
189     }
190     uint32_t userId;
191     // 3 means take the fourth argument of args
192     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) {
193         LOGE("Parameter input error, please retry");
194         return -EINVAL;
195     }
196     return OHOS::StorageDaemon::StorageDaemonClient::UnlockUserScreen(userId);
197 }
198 
EnableFscrypt(const std::vector<std::string> & args)199 static int32_t EnableFscrypt(const std::vector<std::string> &args)
200 {
201     if (args.size() < ARG_CNT_4) {
202         LOGE("Parameter nums is less than 4, please retry");
203         return -EINVAL;
204     }
205     auto option = args[3]; // cmd no.3 param is the option
206     return OHOS::StorageDaemon::StorageDaemonClient::FscryptEnable(option);
207 }
208 
UpdateKeyContext(const std::vector<std::string> & args)209 static int32_t UpdateKeyContext(const std::vector<std::string> &args)
210 {
211     if (args.size() < ARG_CNT_4) {
212         LOGE("Parameter nums is less than 4, please retry");
213         return -EINVAL;
214     }
215     uint32_t userId;
216     // 3 means take the fourth argument of args
217     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) {
218         LOGE("Parameter input error, please retry");
219         return -EINVAL;
220     }
221     return OHOS::StorageDaemon::StorageDaemonClient::UpdateKeyContext(userId);
222 }
223 #endif
224 
225 static const auto g_fscryptCmdHandler = std::map<std::string,
226     std::function<int32_t(const std::vector<std::string> &)>> {
227     {"init_global_key", InitGlobalKey},
228     {"init_main_user", InitMainUser},
229 #ifdef SDC_TEST_ENABLE
230     {"generate_user_keys", GenerateUserKeys},
231     {"prepare_user_space", PrepareUserSpace},
232     {"delete_user_keys", DeleteUserKeys},
233     {"destroy_user_space", DestroyUserSpace},
234     {"update_user_auth", UpdateUserAuth},
235     {"active_user_key", ActiveUserKey},
236     {"inactive_user_key", InactiveUserKey},
237     {"enable", EnableFscrypt},
238     {"update_key_context", UpdateKeyContext},
239     {"lock_user_screen", LockUserScreen},
240     {"unlock_user_screen", UnlockUserScreen},
241 #endif
242 };
243 
HandleFileCrypt(const std::string & cmd,const std::vector<std::string> & args)244 static int HandleFileCrypt(const std::string &cmd, const std::vector<std::string> &args)
245 {
246     LOGI("fscrypt cmd: %{public}s", cmd.c_str());
247 
248     auto handler = g_fscryptCmdHandler.find(cmd);
249     if (handler == g_fscryptCmdHandler.end()) {
250         LOGE("Unknown fscrypt cmd: %{public}s", cmd.c_str());
251         return -EINVAL;
252     }
253     auto ret = handler->second(args);
254     if (ret != 0) {
255         LOGE("fscrypt cmd: %{public}s failed, ret: %{public}d", cmd.c_str(), ret);
256     } else {
257         LOGI("fscrypt cmd: %{public}s success", cmd.c_str());
258     }
259     return ret;
260 }
261 
main(int argc,char ** argv)262 int main(int argc, char **argv)
263 {
264     LOGI("sdc start");
265     std::vector<std::string> args(argv, argv + argc);
266 
267     if (argc < 2) {
268         LOGE("usage: sdc <subsystem> [cmd]");
269         return 0;
270     }
271 
272     int ret = 0;
273     // no.1 param is the cmd
274     if (args[1] == "filecrypt") {
275         ret = HandleFileCrypt(args[2], args); // no.2 param is the cmd
276     } else {
277         LOGE("Unknown subsystem: %{public}s", args[1].c_str()); // no.1 param is the cmd
278         ret = -EINVAL;
279     }
280 
281     LOGI("sdc end");
282     std::cout << "ret: " << ret << std::endl;
283     return ret;
284 }
285