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 "storage_daemon_client.h"
17 
18 #include <chrono>
19 #include <ctime>
20 #include <thread>
21 
22 #include "iremote_object.h"
23 #include "iremote_proxy.h"
24 #include "iservice_registry.h"
25 #include "libfscrypt/fscrypt_utils.h"
26 #include "storage_service_log.h"
27 #include "system_ability_definition.h"
28 
29 namespace {
30 constexpr uint32_t STORAGE_DAEMON_SFIFT = 1;
31 constexpr uint32_t CHECK_SERVICE_TIMES = 1000;
32 constexpr uint32_t SLEEP_TIME_PRE_CHECK = 10; // 10ms
33 constexpr uint32_t STORAGE_SERVICE_FLAG = (1 << STORAGE_DAEMON_SFIFT);
34 constexpr int32_t STORAGE_DAEMON_SAID = OHOS::STORAGE_MANAGER_DAEMON_ID;
35 }
36 
37 namespace OHOS {
38 namespace StorageDaemon {
GetStorageDaemonProxy(void)39 sptr<IStorageDaemon> StorageDaemonClient::GetStorageDaemonProxy(void)
40 {
41     auto samgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
42     if (samgr == nullptr) {
43         LOGE("samgr empty error");
44         return nullptr;
45     }
46 
47     sptr<IRemoteObject> object = samgr->GetSystemAbility(OHOS::STORAGE_MANAGER_DAEMON_ID);
48     if (object == nullptr) {
49         LOGE("storage daemon client samgr ablity empty error");
50         return nullptr;
51     }
52 
53     return iface_cast<IStorageDaemon>(object);
54 }
55 
CheckServiceStatus(uint32_t serviceFlags)56 bool StorageDaemonClient::CheckServiceStatus(uint32_t serviceFlags)
57 {
58     LOGW("CheckServiceStatus start");
59 
60     auto samgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
61     if (samgr == nullptr) {
62         LOGW("samgr is nullptr, retry");
63         for (uint32_t i = 0; i < CHECK_SERVICE_TIMES; i++) {
64             samgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
65             if (samgr != nullptr) {
66                 break;
67             }
68             LOGW("check samgr %{public}u times", i);
69             std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_PRE_CHECK));
70         }
71         if (samgr == nullptr) {
72             LOGE("samgr is nullptr, retry failed.");
73             return false;
74         }
75     }
76 
77     if (serviceFlags & STORAGE_SERVICE_FLAG) {
78         bool exist = false;
79         for (uint32_t i = 0; i < CHECK_SERVICE_TIMES; i++) {
80             auto object = samgr->CheckSystemAbility(STORAGE_DAEMON_SAID, exist);
81             if (object != nullptr) {
82                 break;
83             }
84             LOGW("check storage daemon status %{public}u times", i);
85             std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME_PRE_CHECK));
86         }
87         if (exist == false) {
88             LOGE("storage daemon service system ability error");
89             return false;
90         }
91     }
92     LOGW("CheckServiceStatus end, success");
93 
94     return true;
95 }
96 
PrepareUserDirs(int32_t userId,uint32_t flags)97 int32_t StorageDaemonClient::PrepareUserDirs(int32_t userId, uint32_t flags)
98 {
99     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
100         LOGE("service check failed");
101         return -EAGAIN;
102     }
103 
104     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
105     if (client == nullptr) {
106         LOGE("get storage daemon service failed");
107         return -EAGAIN;
108     }
109     return client->PrepareUserDirs(userId, flags);
110 }
111 
DestroyUserDirs(int32_t userId,uint32_t flags)112 int32_t StorageDaemonClient::DestroyUserDirs(int32_t userId, uint32_t flags)
113 {
114     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
115         LOGE("service check failed");
116         return -EAGAIN;
117     }
118 
119     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
120     if (client == nullptr) {
121         LOGE("get storage daemon service failed");
122         return -EAGAIN;
123     }
124     return client->DestroyUserDirs(userId, flags);
125 }
126 
StartUser(int32_t userId)127 int32_t StorageDaemonClient::StartUser(int32_t userId)
128 {
129     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
130         LOGE("service check failed");
131         return -EAGAIN;
132     }
133 
134     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
135     if (client == nullptr) {
136         LOGE("get storage daemon service failed");
137         return -EAGAIN;
138     }
139 
140     return client->StartUser(userId);
141 }
142 
StopUser(int32_t userId)143 int32_t StorageDaemonClient::StopUser(int32_t userId)
144 {
145     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
146         LOGE("service check failed");
147         return -EAGAIN;
148     }
149 
150     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
151     if (client == nullptr) {
152         LOGE("get storage daemon service failed");
153         return -EAGAIN;
154     }
155 
156     return client->StopUser(userId);
157 }
158 
PrepareUserSpace(uint32_t userId,const std::string & volumId,uint32_t flags)159 int32_t StorageDaemonClient::PrepareUserSpace(uint32_t userId, const std::string &volumId, uint32_t flags)
160 {
161     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
162         LOGE("service check failed");
163         return -EAGAIN;
164     }
165 
166     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
167     if (client == nullptr) {
168         LOGE("get storage daemon service failed");
169         return -EAGAIN;
170     }
171 
172     return client->PrepareUserDirs(userId, flags);
173 }
174 
DestroyUserSpace(uint32_t userId,const std::string & volumId,uint32_t flags)175 int32_t StorageDaemonClient::DestroyUserSpace(uint32_t userId, const std::string &volumId, uint32_t flags)
176 {
177     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
178         LOGE("service check failed");
179         return -EAGAIN;
180     }
181 
182     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
183     if (client == nullptr) {
184         LOGE("get storage daemon service failed");
185         return -EAGAIN;
186     }
187 
188     return client->DestroyUserDirs(userId, flags);
189 }
190 
InitGlobalKey(void)191 int32_t StorageDaemonClient::InitGlobalKey(void)
192 {
193     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
194         LOGE("service check failed");
195         return -EAGAIN;
196     }
197 
198     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
199     if (client == nullptr) {
200         LOGE("get storage daemon service failed");
201         return -EAGAIN;
202     }
203 
204     return client->InitGlobalKey();
205 }
206 
InitGlobalUserKeys(void)207 int32_t StorageDaemonClient::InitGlobalUserKeys(void)
208 {
209     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
210         LOGE("service check failed");
211         return -EAGAIN;
212     }
213 
214     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
215     if (client == nullptr) {
216         LOGE("get storage daemon service failed");
217         return -EAGAIN;
218     }
219 
220     return client->InitGlobalUserKeys();
221 }
222 
GenerateUserKeys(uint32_t userId,uint32_t flags)223 int32_t StorageDaemonClient::GenerateUserKeys(uint32_t userId, uint32_t flags)
224 {
225     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
226         LOGE("service check failed");
227         return -EAGAIN;
228     }
229 
230     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
231     if (client == nullptr) {
232         LOGE("get storage daemon service failed");
233         return -EAGAIN;
234     }
235 
236     return client->GenerateUserKeys(userId, flags);
237 }
238 
DeleteUserKeys(uint32_t userId)239 int32_t StorageDaemonClient::DeleteUserKeys(uint32_t userId)
240 {
241     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
242         LOGE("service check failed");
243         return -EAGAIN;
244     }
245 
246     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
247     if (client == nullptr) {
248         LOGE("get storage daemon service failed");
249         return -EAGAIN;
250     }
251 
252     return client->DeleteUserKeys(userId);
253 }
254 
UpdateUserAuth(uint32_t userId,uint64_t secureUid,const std::vector<uint8_t> & token,const std::vector<uint8_t> & oldSecret,const std::vector<uint8_t> & newSecret)255 int32_t StorageDaemonClient::UpdateUserAuth(uint32_t userId, uint64_t secureUid,
256                                             const std::vector<uint8_t> &token,
257                                             const std::vector<uint8_t> &oldSecret,
258                                             const std::vector<uint8_t> &newSecret)
259 {
260     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
261         LOGE("service check failed");
262         return -EAGAIN;
263     }
264 
265     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
266     if (client == nullptr) {
267         LOGE("get storage daemon service failed");
268         return -EAGAIN;
269     }
270 
271     return client->UpdateUserAuth(userId, secureUid, token, oldSecret, newSecret);
272 }
273 
ActiveUserKey(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)274 int32_t StorageDaemonClient::ActiveUserKey(uint32_t userId,
275                                            const std::vector<uint8_t> &token,
276                                            const std::vector<uint8_t> &secret)
277 {
278     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
279         LOGE("service check failed");
280         return -EAGAIN;
281     }
282 
283     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
284     if (client == nullptr) {
285         LOGE("get storage daemon service failed");
286         return -EAGAIN;
287     }
288 
289     return client->ActiveUserKey(userId, token, secret);
290 }
291 
InactiveUserKey(uint32_t userId)292 int32_t StorageDaemonClient::InactiveUserKey(uint32_t userId)
293 {
294     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
295         LOGE("service check failed");
296         return -EAGAIN;
297     }
298 
299     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
300     if (client == nullptr) {
301         LOGE("get storage daemon service failed");
302         return -EAGAIN;
303     }
304 
305     return client->InactiveUserKey(userId);
306 }
307 
LockUserScreen(uint32_t userId)308 int32_t StorageDaemonClient::LockUserScreen(uint32_t userId)
309 {
310     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
311         LOGE("service check failed");
312         return -EAGAIN;
313     }
314 
315     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
316     if (client == nullptr) {
317         LOGE("get storage daemon service failed");
318         return -EAGAIN;
319     }
320 
321     return client->LockUserScreen(userId);
322 }
323 
UnlockUserScreen(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)324 int32_t StorageDaemonClient::UnlockUserScreen(uint32_t userId, const std::vector<uint8_t> &token,
325                                               const std::vector<uint8_t> &secret)
326 {
327     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
328         LOGE("service check failed");
329         return -EAGAIN;
330     }
331 
332     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
333     if (client == nullptr) {
334         LOGE("get storage daemon service failed");
335         return -EAGAIN;
336     }
337 
338     return client->UnlockUserScreen(userId, token, secret);
339 }
340 
GetLockScreenStatus(uint32_t userId,bool & lockScreenStatus)341 int32_t StorageDaemonClient::GetLockScreenStatus(uint32_t userId, bool &lockScreenStatus)
342 {
343     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
344         LOGE("service check failed");
345         return -EAGAIN;
346     }
347 
348     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
349     if (client == nullptr) {
350         LOGE("get storage daemon service failed");
351         return -EAGAIN;
352     }
353 
354     return client->GetLockScreenStatus(userId, lockScreenStatus);
355 }
356 
UpdateKeyContext(uint32_t userId)357 int32_t StorageDaemonClient::UpdateKeyContext(uint32_t userId)
358 {
359     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
360         LOGE("service check failed");
361         return -EAGAIN;
362     }
363 
364     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
365     if (client == nullptr) {
366         LOGE("get storage daemon service failed");
367         return -EAGAIN;
368     }
369 
370     return client->UpdateKeyContext(userId);
371 }
372 
GenerateAppkey(uint32_t userId,uint32_t hashId,std::string & keyId)373 int32_t StorageDaemonClient::GenerateAppkey(uint32_t userId, uint32_t hashId, std::string &keyId)
374 {
375     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
376         LOGE("service check failed");
377         return -EAGAIN;
378     }
379 
380     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
381     if (client == nullptr) {
382         LOGE("get storage daemon service failed");
383         return -EAGAIN;
384     }
385 
386     return client->GenerateAppkey(userId, hashId, keyId);
387 }
388 
DeleteAppkey(uint32_t userId,const std::string keyId)389 int32_t StorageDaemonClient::DeleteAppkey(uint32_t userId, const std::string keyId)
390 {
391     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
392         LOGE("service check failed");
393         return -EAGAIN;
394     }
395 
396     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
397     if (client == nullptr) {
398         LOGE("get storage daemon service failed");
399         return -EAGAIN;
400     }
401 
402     return client->DeleteAppkey(userId, keyId);
403 }
404 
MountDfsDocs(int32_t userId,const std::string & relativePath,const std::string & networkId,const std::string & deviceId)405 int32_t StorageDaemonClient::MountDfsDocs(int32_t userId, const std::string &relativePath,
406     const std::string &networkId, const std::string &deviceId)
407 {
408     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
409         LOGE("Storage service flag check failed!");
410         return -EAGAIN;
411     }
412 
413     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
414     if (client == nullptr) {
415         LOGE("Get StorageDaemon service failed!");
416         return -EAGAIN;
417     }
418 
419     return client->MountDfsDocs(userId, relativePath, networkId, deviceId);
420 }
421 
UMountDfsDocs(int32_t userId,const std::string & relativePath,const std::string & networkId,const std::string & deviceId)422 int32_t StorageDaemonClient::UMountDfsDocs(int32_t userId, const std::string &relativePath,
423     const std::string &networkId, const std::string &deviceId)
424 {
425     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
426         LOGE("Storage service flag check failed!");
427         return -EAGAIN;
428     }
429 
430     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
431     if (client == nullptr) {
432         LOGE("Get StorageDaemon service failed!");
433         return -EAGAIN;
434     }
435 
436     return client->UMountDfsDocs(userId, relativePath, networkId, deviceId);
437 }
438 
FscryptEnable(const std::string & fscryptOptions)439 int32_t StorageDaemonClient::FscryptEnable(const std::string &fscryptOptions)
440 {
441 #ifdef USER_CRYPTO_MANAGER
442     int ret = SetFscryptSysparam(fscryptOptions.c_str());
443     if (ret) {
444         LOGE("Init fscrypt policy failed ret %{public}d", ret);
445         return ret;
446     }
447 #endif
448 
449     return 0;
450 }
451 
GetFileEncryptStatus(uint32_t userId,bool & isEncrypted,bool needCheckDirMount)452 int32_t StorageDaemonClient::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount)
453 {
454     if (!CheckServiceStatus(STORAGE_SERVICE_FLAG)) {
455         LOGE("Storage service flag check failed!");
456         return -EAGAIN;
457     }
458 
459     sptr<IStorageDaemon> client = GetStorageDaemonProxy();
460     if (client == nullptr) {
461         LOGE("Get StorageDaemon service failed!");
462         return -EAGAIN;
463     }
464 
465     return client->GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount);
466 }
467 } // namespace StorageDaemon
468 } // namespace OHOS
469