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