• 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 <iostream>
17 #include <string>
18 #include <vector>
19 #include <map>
20 #include <functional>
21 
22 #include "storage_daemon_client.h"
23 #include "storage_service_log.h"
24 #include "utils/file_utils.h"
25 #include "client/storage_manager_client.h"
26 
InitGlobalKey(const std::vector<std::string> & args)27 static int32_t InitGlobalKey(const std::vector<std::string> &args)
28 {
29     (void)args;
30     return OHOS::StorageDaemon::StorageDaemonClient::InitGlobalKey();
31 }
32 
InitMainUser(const std::vector<std::string> & args)33 static int32_t InitMainUser(const std::vector<std::string> &args)
34 {
35     (void)args;
36     return OHOS::StorageDaemon::StorageDaemonClient::InitGlobalUserKeys();
37 }
38 
GenerateUserKeys(const std::vector<std::string> & args)39 static int32_t GenerateUserKeys(const std::vector<std::string> &args)
40 {
41     if (args.size() < 5) {
42         LOGE("Parameter nums is less than 5, please retry");
43         return -EINVAL;
44     }
45     uint32_t userId, flags;
46     if ((OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) ||
47         (OHOS::StorageDaemon::StringToUint32(args[4], flags) == false)) {
48         LOGE("Parameter input error, please retry");
49         return -EINVAL;
50     }
51     return OHOS::StorageManager::StorageManagerClient::GenerateUserKeys(userId, flags);
52 }
53 
PrepareUserSpace(const std::vector<std::string> & args)54 static int32_t PrepareUserSpace(const std::vector<std::string> &args)
55 {
56     if (args.size() < 5) {
57         LOGE("Parameter nums is less than 5, please retry");
58         return -EINVAL;
59     }
60     uint32_t userId, flags;
61     if ((OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) ||
62         (OHOS::StorageDaemon::StringToUint32(args[4], flags) == false)) {
63         LOGE("Parameter input error, please retry");
64         return -EINVAL;
65     }
66     std::string volumId = "";
67     return OHOS::StorageManager::StorageManagerClient::PrepareAddUser(userId, volumId, flags);
68 }
69 
DeleteUserKeys(const std::vector<std::string> & args)70 static int32_t DeleteUserKeys(const std::vector<std::string> &args)
71 {
72     if (args.size() < 4) {
73         LOGE("Parameter nums is less than 4, please retry");
74         return -EINVAL;
75     }
76     uint32_t userId;
77     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) {
78         LOGE("Parameter input error, please retry");
79         return -EINVAL;
80     }
81     return OHOS::StorageManager::StorageManagerClient::DeleteUserKeys(userId);
82 }
83 
DestroyUserSpace(const std::vector<std::string> & args)84 static int32_t DestroyUserSpace(const std::vector<std::string> &args)
85 {
86     if (args.size() < 5) {
87         LOGE("Parameter nums is less than 5, please retry");
88         return -EINVAL;
89     }
90     uint32_t userId, flags;
91     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false ||
92         OHOS::StorageDaemon::StringToUint32(args[4], flags) == false) {
93         LOGE("Parameter input error, please retry");
94         return -EINVAL;
95     }
96     std::string volumId = "";
97     return OHOS::StorageManager::StorageManagerClient::RemoveUser(userId, volumId, flags);
98 }
99 
UpdateUserAuth(const std::vector<std::string> & args)100 static int32_t UpdateUserAuth(const std::vector<std::string> &args)
101 {
102     if (args.size() < 6) {
103         LOGE("Parameter nums is less than 6, please retry");
104         return -EINVAL;
105     }
106     uint32_t userId;
107     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) {
108         LOGE("Parameter input error, please retry");
109         return -EINVAL;
110     }
111     std::string token = args[4];
112     std::string secret = args[5];
113     return OHOS::StorageManager::StorageManagerClient::UpdateUserAuth(userId, token, secret);
114 }
115 
ActiveUserKey(const std::vector<std::string> & args)116 static int32_t ActiveUserKey(const std::vector<std::string> &args)
117 {
118     if (args.size() < 6) {
119         LOGE("Parameter nums is less than 6, please retry");
120         return -EINVAL;
121     }
122     uint32_t userId;
123     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) {
124         LOGE("Parameter input error, please retry");
125         return -EINVAL;
126     }
127     std::string token = args[4];
128     std::string secret = args[5];
129     return OHOS::StorageManager::StorageManagerClient::ActiveUserKey(userId, token, secret);
130 }
131 
InactiveUserKey(const std::vector<std::string> & args)132 static int32_t InactiveUserKey(const std::vector<std::string> &args)
133 {
134     if (args.size() < 4) {
135         LOGE("Parameter nums is less than 4, please retry");
136         return -EINVAL;
137     }
138     uint32_t userId;
139     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) {
140         LOGE("Parameter input error, please retry");
141         return -EINVAL;
142     }
143     return OHOS::StorageManager::StorageManagerClient::InactiveUserKey(userId);
144 }
145 
EnableFscrypt(const std::vector<std::string> & args)146 static int32_t EnableFscrypt(const std::vector<std::string> &args)
147 {
148     if (args.size() < 4) {
149         LOGE("Parameter nums is less than 4, please retry");
150         return -EINVAL;
151     }
152     auto option = args[3]; // cmd no.3 param is the option
153     return OHOS::StorageDaemon::StorageDaemonClient::FscryptEnable(option);
154 }
155 
UpdateKeyContext(const std::vector<std::string> & args)156 static int32_t UpdateKeyContext(const std::vector<std::string> &args)
157 {
158     if (args.size() < 4) {
159         LOGE("Parameter nums is less than 4, please retry");
160         return -EINVAL;
161     }
162     uint32_t userId;
163     if (OHOS::StorageDaemon::StringToUint32(args[3], userId) == false) {
164         LOGE("Parameter input error, please retry");
165         return -EINVAL;
166     }
167     return OHOS::StorageManager::StorageManagerClient::UpdateKeyContext(userId);
168 }
169 
170 static const auto g_fscryptCmdHandler = std::map<std::string,
171     std::function<int32_t(const std::vector<std::string> &)>> {
172     {"init_global_key", InitGlobalKey},
173     {"init_main_user", InitMainUser},
174     {"generate_user_keys", GenerateUserKeys},
175     {"prepare_user_space", PrepareUserSpace},
176     {"delete_user_keys", DeleteUserKeys},
177     {"destroy_user_space", DestroyUserSpace},
178     {"update_user_auth", UpdateUserAuth},
179     {"active_user_key", ActiveUserKey},
180     {"inactive_user_key", InactiveUserKey},
181     {"enable", EnableFscrypt},
182     {"update_key_context", UpdateKeyContext},
183 };
184 
HandleFileCrypt(const std::string & cmd,const std::vector<std::string> & args)185 static int HandleFileCrypt(const std::string &cmd, const std::vector<std::string> &args)
186 {
187     LOGI("fscrypt cmd: %{public}s", cmd.c_str());
188 
189     auto handler = g_fscryptCmdHandler.find(cmd);
190     if (handler == g_fscryptCmdHandler.end()) {
191         LOGE("Unknown fscrypt cmd: %{public}s", cmd.c_str());
192         return -EINVAL;
193     }
194     auto ret = handler->second(args);
195     if (ret != 0) {
196         LOGE("fscrypt cmd: %{public}s failed, ret: %{public}d", cmd.c_str(), ret);
197     } else {
198         LOGI("fscrypt cmd: %{public}s success", cmd.c_str());
199     }
200     return ret;
201 }
202 
main(int argc,char ** argv)203 int main(int argc, char **argv)
204 {
205     LOGI("sdc start");
206     std::vector<std::string> args(argv, argv + argc);
207 
208     if (argc < 2) {
209         LOGE("usage: sdc <subsystem> [cmd]");
210         return 0;
211     }
212 
213     int ret = 0;
214     if (args[1] == "filecrypt") {
215         ret = HandleFileCrypt(args[2], args); // no.2 param is the cmd
216     } else {
217         LOGE("Unknown subsystem: %{public}s", args[1].c_str());
218         ret = -EINVAL;
219     }
220 
221     LOGI("sdc end");
222     return ret;
223 }
224