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 #include "client/storage_manager_client.h"
16 
17 #include "iremote_object.h"
18 #include "iremote_proxy.h"
19 #include "iservice_registry.h"
20 #include "storage_service_log.h"
21 #include "system_ability_definition.h"
22 
23 namespace OHOS {
24 namespace StorageManager {
GetStorageManagerProxy(void)25 sptr<IStorageManager> StorageManagerClient::GetStorageManagerProxy(void)
26 {
27     auto samgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
28     if (samgr == nullptr) {
29         LOGE("samgr empty error");
30         return nullptr;
31     }
32 
33     sptr<IRemoteObject> object = samgr->GetSystemAbility(OHOS::STORAGE_MANAGER_MANAGER_ID);
34     if (object == nullptr) {
35         LOGE("storage manager client samgr ablity empty error");
36         return nullptr;
37     }
38 
39     return iface_cast<IStorageManager>(object);
40 }
41 
PrepareAddUser(uint32_t userId,uint32_t flags)42 int32_t StorageManagerClient::PrepareAddUser(uint32_t userId, uint32_t flags)
43 {
44     sptr<IStorageManager> client = GetStorageManagerProxy();
45     if (client == nullptr) {
46         LOGE("get storage manager service failed");
47         return -EFAULT;
48     }
49 
50     return client->PrepareAddUser(userId, flags);
51 }
52 
RemoveUser(uint32_t userId,uint32_t flags)53 int32_t StorageManagerClient::RemoveUser(uint32_t userId, uint32_t flags)
54 {
55     sptr<IStorageManager> client = GetStorageManagerProxy();
56     if (client == nullptr) {
57         LOGE("get storage manager service failed");
58         return -EFAULT;
59     }
60 
61     return client->RemoveUser(userId, flags);
62 }
63 
GenerateUserKeys(uint32_t userId,uint32_t flags)64 int32_t StorageManagerClient::GenerateUserKeys(uint32_t userId, uint32_t flags)
65 {
66     sptr<IStorageManager> client = GetStorageManagerProxy();
67     if (client == nullptr) {
68         LOGE("get storage manager service failed");
69         return -EFAULT;
70     }
71 
72     return client->GenerateUserKeys(userId, flags);
73 }
74 
DeleteUserKeys(uint32_t userId)75 int32_t StorageManagerClient::DeleteUserKeys(uint32_t userId)
76 {
77     sptr<IStorageManager> client = GetStorageManagerProxy();
78     if (client == nullptr) {
79         LOGE("get storage manager service failed");
80         return -EFAULT;
81     }
82 
83     return client->DeleteUserKeys(userId);
84 }
85 
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)86 int32_t StorageManagerClient::UpdateUserAuth(uint32_t userId, uint64_t secureUid,
87                                              const std::vector<uint8_t> &token,
88                                              const std::vector<uint8_t> &oldSecret,
89                                              const std::vector<uint8_t> &newSecret)
90 {
91     sptr<IStorageManager> client = GetStorageManagerProxy();
92     if (client == nullptr) {
93         LOGE("get storage manager service failed");
94         return -EFAULT;
95     }
96 
97     return client->UpdateUserAuth(userId, secureUid, token, oldSecret, newSecret);
98 }
99 
ActiveUserKey(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)100 int32_t StorageManagerClient::ActiveUserKey(uint32_t userId,
101                                             const std::vector<uint8_t> &token,
102                                             const std::vector<uint8_t> &secret)
103 {
104     sptr<IStorageManager> client = GetStorageManagerProxy();
105     if (client == nullptr) {
106         LOGE("get storage manager service failed");
107         return -EFAULT;
108     }
109 
110     return client->ActiveUserKey(userId, token, secret);
111 }
112 
InactiveUserKey(uint32_t userId)113 int32_t StorageManagerClient::InactiveUserKey(uint32_t userId)
114 {
115     sptr<IStorageManager> client = GetStorageManagerProxy();
116     if (client == nullptr) {
117         LOGE("get storage manager service failed");
118         return -EFAULT;
119     }
120 
121     return client->InactiveUserKey(userId);
122 }
123 
UpdateKeyContext(uint32_t userId)124 int32_t StorageManagerClient::UpdateKeyContext(uint32_t userId)
125 {
126     sptr<IStorageManager> client = GetStorageManagerProxy();
127     if (client == nullptr) {
128         LOGE("get storage manager service failed");
129         return -EFAULT;
130     }
131 
132     return client->UpdateKeyContext(userId);
133 }
134 
LockUserScreen(uint32_t userId)135 int32_t StorageManagerClient::LockUserScreen(uint32_t userId)
136 {
137     sptr<IStorageManager> client = GetStorageManagerProxy();
138     if (client == nullptr) {
139         LOGE("get storage manager service failed");
140         return -EFAULT;
141     }
142 
143     return client->LockUserScreen(userId);
144 }
145 
UnlockUserScreen(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)146 int32_t StorageManagerClient::UnlockUserScreen(uint32_t userId,
147                                                const std::vector<uint8_t> &token,
148                                                const std::vector<uint8_t> &secret)
149 {
150     sptr<IStorageManager> client = GetStorageManagerProxy();
151     if (client == nullptr) {
152         LOGE("get storage manager service failed");
153         return -EFAULT;
154     }
155 
156     return client->UnlockUserScreen(userId, token, secret);
157 }
158 
GetFileEncryptStatus(uint32_t userId,bool & isEncrypted,bool needCheckDirMount)159 int32_t StorageManagerClient::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount)
160 {
161     sptr<IStorageManager> client = GetStorageManagerProxy();
162     if (client == nullptr) {
163         LOGE("get storage manager service failed");
164         return -EFAULT;
165     }
166 
167     return client->GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount);
168 }
169 
GetLockScreenStatus(uint32_t userId,bool & lockScreenStatus)170 int32_t StorageManagerClient::GetLockScreenStatus(uint32_t userId, bool &lockScreenStatus)
171 {
172     sptr<IStorageManager> client = GetStorageManagerProxy();
173     if (client == nullptr) {
174         LOGE("get storage manager service failed");
175         return -EFAULT;
176     }
177 
178     return client->GetLockScreenStatus(userId, lockScreenStatus);
179 }
180 
MountDfsDocs(int32_t userId,const std::string & relativePath,const std::string & networkId,const std::string & deviceId)181 int32_t StorageManagerClient::MountDfsDocs(int32_t userId, const std::string &relativePath,
182     const std::string &networkId, const std::string &deviceId)
183 {
184     sptr<IStorageManager> client = GetStorageManagerProxy();
185     if (client == nullptr) {
186         LOGE("get storage manager service failed");
187         return -EFAULT;
188     }
189 
190     return client->MountDfsDocs(userId, relativePath, networkId, deviceId);
191 }
192 
UMountDfsDocs(int32_t userId,const std::string & relativePath,const std::string & networkId,const std::string & deviceId)193 int32_t StorageManagerClient::UMountDfsDocs(int32_t userId, const std::string &relativePath,
194     const std::string &networkId, const std::string &deviceId)
195 {
196     sptr<IStorageManager> client = GetStorageManagerProxy();
197     if (client == nullptr) {
198         LOGE("get storage manager service failed");
199         return -EFAULT;
200     }
201 
202     return client->UMountDfsDocs(userId, relativePath, networkId, deviceId);
203 }
204 }
205 }
206