• 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 "user/user_manager.h"
17 #include <cstdlib>
18 #ifdef USER_CRYPTO_MANAGER
19 #include "crypto/key_manager.h"
20 #endif
21 #include "ipc/istorage_daemon.h"
22 #include "storage_service_constant.h"
23 #include "storage_service_errno.h"
24 #include "storage_service_log.h"
25 #include "utils/string_utils.h"
26 
27 using namespace std;
28 
29 namespace OHOS {
30 namespace StorageDaemon {
31 std::shared_ptr<UserManager> UserManager::instance_ = nullptr;
UserManager()32 UserManager::UserManager()
33     : rootDirVec_{{"/data/app/%s/%d", 0711, OID_ROOT, OID_ROOT},
34                   {"/data/service/%s/%d", 0711, OID_ROOT, OID_ROOT},
35                   {"/data/chipset/%s/%d", 0711, OID_ROOT, OID_ROOT}},
36       subDirVec_{{"/data/app/%s/%d/base", 0711, OID_ROOT, OID_ROOT},
37                  {"/data/app/%s/%d/database", 0711, OID_ROOT, OID_ROOT}},
38       backupDirVec_{{"/data/service/el2/%d/backup", 02771, OID_BACKUP, OID_BACKUP},
39                     {"/data/service/el2/%d/backup/backup_sa", 0711, OID_BACKUP, OID_BACKUP}}
40 {
41 }
42 
GetInstance()43 std::shared_ptr<UserManager> UserManager::GetInstance()
44 {
45     static std::once_flag onceFlag;
46     std::call_once(onceFlag, [&]() { instance_ = std::make_shared<UserManager>(); });
47 
48     return instance_;
49 }
50 
StartUser(int32_t userId)51 int32_t UserManager::StartUser(int32_t userId)
52 {
53     LOGI("start user %{public}d", userId);
54     std::lock_guard<std::mutex> lock(mutex_);
55     return MountManager::GetInstance()->MountByUser(userId);
56 }
57 
StopUser(int32_t userId)58 int32_t UserManager::StopUser(int32_t userId)
59 {
60     LOGI("stop user %{public}d", userId);
61     std::lock_guard<std::mutex> lock(mutex_);
62     return MountManager::GetInstance()->UmountByUser(userId);
63 }
64 
PrepareUserDirs(int32_t userId,uint32_t flags)65 int32_t UserManager::PrepareUserDirs(int32_t userId, uint32_t flags)
66 {
67     LOGI("prepare user dirs for %{public}d, flags %{public}u", userId, flags);
68     std::lock_guard<std::mutex> lock(mutex_);
69     int32_t err = E_OK;
70 
71     if (flags & IStorageDaemon::CRYPTO_FLAG_EL1) {
72         err = PrepareDirsFromIdAndLevel(userId, EL1);
73         if (err != E_OK) {
74             return err;
75         }
76 
77         err = PrepareEl1BundleDir(userId);
78         if (err != E_OK) {
79             return err;
80         }
81     }
82 
83     if (flags & IStorageDaemon::CRYPTO_FLAG_EL2) {
84         err = PrepareDirsFromIdAndLevel(userId, EL2);
85         if (err != E_OK) {
86             return err;
87         }
88 
89         err = MountManager::GetInstance()->PrepareHmdfsDirs(userId);
90         if (err != E_OK) {
91             LOGE("Prepare hmdfs dir error");
92             return err;
93         }
94 
95         err = MountManager::GetInstance()->PrepareFileManagerDirs(userId);
96         if (err != E_OK) {
97             LOGE("Prepare fileManager dir error");
98             return err;
99         }
100 
101         err = PrepareEl2BackupDir(userId);
102         if (err != E_OK) {
103             return err;
104         }
105     }
106 
107     return E_OK;
108 }
109 
DestroyUserDirs(int32_t userId,uint32_t flags)110 int32_t UserManager::DestroyUserDirs(int32_t userId, uint32_t flags)
111 {
112     LOGI("destroy user dirs for %{public}d, flags %{public}u", userId, flags);
113     std::lock_guard<std::mutex> lock(mutex_);
114     int32_t ret = E_OK;
115     int32_t err;
116 
117     if (flags & IStorageDaemon::CRYPTO_FLAG_EL1) {
118         err = DestroyDirsFromIdAndLevel(userId, EL1);
119         ret = (err != E_OK) ? err : ret;
120 
121         err = DestroyEl1BundleDir(userId);
122         ret = (err != E_OK) ? err : ret;
123     }
124 
125     if (flags & IStorageDaemon::CRYPTO_FLAG_EL2) {
126         err = DestroyDirsFromIdAndLevel(userId, EL2);
127         ret = (err != E_OK) ? err : ret;
128 
129         err = MountManager::GetInstance()->DestroyFileManagerDirs(userId);
130         ret = (err != E_OK) ? err : ret;
131 
132         err = MountManager::GetInstance()->DestroyHmdfsDirs(userId);
133         ret = (err != E_OK) ? err : ret;
134 
135         err = DestroyEl2BackupDir(userId);
136         ret = (err != E_OK) ? err : ret;
137     }
138 
139     return ret;
140 }
141 
PrepareDirsFromVec(int32_t userId,const std::string & level,const std::vector<DirInfo> & vec)142 inline bool PrepareDirsFromVec(int32_t userId, const std::string &level, const std::vector<DirInfo> &vec)
143 {
144     for (const DirInfo &dir : vec) {
145         if (!PrepareDir(StringPrintf(dir.path.c_str(), level.c_str(), userId), dir.mode, dir.uid, dir.gid)) {
146             return false;
147         }
148     }
149 
150     return true;
151 }
152 
DestroyDirsFromVec(int32_t userId,const std::string & level,const std::vector<DirInfo> & vec)153 inline bool DestroyDirsFromVec(int32_t userId, const std::string &level, const std::vector<DirInfo> &vec)
154 {
155     bool err = true;
156 
157     for (const DirInfo &dir : vec) {
158         if (IsEndWith(dir.path.c_str(), "%d")) {
159             err = RmDirRecurse(StringPrintf(dir.path.c_str(), level.c_str(), userId));
160         }
161     }
162 
163     return err;
164 }
165 
PrepareDirsFromIdAndLevel(int32_t userId,const std::string & level)166 int32_t UserManager::PrepareDirsFromIdAndLevel(int32_t userId, const std::string &level)
167 {
168     if (!PrepareDirsFromVec(userId, level, rootDirVec_)) {
169         LOGE("failed to prepare %{public}s root dirs for userid %{public}d", level.c_str(), userId);
170         return E_PREPARE_DIR;
171     }
172 
173     // set policy here
174     std::vector<FileList> list;
175     for (auto item : rootDirVec_) {
176         FileList temp;
177         temp.userId = static_cast<uint32_t>(userId);
178         temp.path = StringPrintf(item.path.c_str(), level.c_str(), userId);
179         list.push_back(temp);
180     }
181     int ret = SetElDirFscryptPolicy(userId, level, list);
182     if (ret != E_OK) {
183         LOGE("Set el poilcy failed");
184         return ret;
185     }
186 
187     if (!PrepareDirsFromVec(userId, level, subDirVec_)) {
188         LOGE("failed to prepare %{public}s sub dirs for userid %{public}d", level.c_str(), userId);
189         return E_PREPARE_DIR;
190     }
191 
192     return E_OK;
193 }
194 
DestroyDirsFromIdAndLevel(int32_t userId,const std::string & level)195 int32_t UserManager::DestroyDirsFromIdAndLevel(int32_t userId, const std::string &level)
196 {
197     if (!DestroyDirsFromVec(userId, level, rootDirVec_)) {
198         LOGE("failed to destroy %{public}s dirs for userid %{public}d", level.c_str(), userId);
199         return E_DESTROY_DIR;
200     }
201 
202     return E_OK;
203 }
204 
PrepareEl1BundleDir(int32_t userId)205 int32_t UserManager::PrepareEl1BundleDir(int32_t userId)
206 {
207     if (!PrepareDir(StringPrintf(bundle_.c_str(), userId), 0711, OID_ROOT, OID_ROOT)) {
208         return E_PREPARE_DIR;
209     }
210 
211     // set policy here
212     std::vector<FileList> list;
213     FileList temp;
214     temp.userId = static_cast<uint32_t>(userId);
215     temp.path = StringPrintf(bundle_.c_str(), userId);
216     list.push_back(temp);
217     int ret = SetElDirFscryptPolicy(userId, EL1, list);
218     if (ret != E_OK) {
219         LOGE("Set el1 poilcy failed");
220         return ret;
221     }
222 
223     return E_OK;
224 }
225 
DestroyEl1BundleDir(int32_t userId)226 int32_t UserManager::DestroyEl1BundleDir(int32_t userId)
227 {
228     if (!RmDirRecurse(StringPrintf(bundle_.c_str(), userId))) {
229         return E_DESTROY_DIR;
230     }
231 
232     return E_OK;
233 }
234 
SetElDirFscryptPolicy(int32_t userId,const std::string & level,const std::vector<FileList> & list)235 int32_t UserManager::SetElDirFscryptPolicy(int32_t userId, const std::string &level,
236                                            const std::vector<FileList> &list)
237 {
238 #ifdef USER_CRYPTO_MANAGER
239     if (EL_DIR_MAP.find(level) == EL_DIR_MAP.end()) {
240         LOGE("el type error");
241         return E_SET_POLICY;
242     }
243     if (KeyManager::GetInstance()->SetDirectoryElPolicy(userId, EL_DIR_MAP[level], list)) {
244         LOGE("Set user dir el1 policy error");
245         return E_SET_POLICY;
246     }
247 #endif
248 
249     return E_OK;
250 }
251 
PrepareEl2BackupDir(int32_t userId)252 int32_t UserManager::PrepareEl2BackupDir(int32_t userId)
253 {
254     for (const DirInfo &dir : backupDirVec_) {
255         if (!PrepareDir(StringPrintf(dir.path.c_str(), userId), dir.mode, dir.uid, dir.gid)) {
256             return E_PREPARE_DIR;
257         }
258     }
259 
260     return E_OK;
261 }
262 
DestroyEl2BackupDir(int32_t userId)263 int32_t UserManager::DestroyEl2BackupDir(int32_t userId)
264 {
265     for (const DirInfo &dir :  backupDirVec_) {
266         if (!RmDirRecurse(StringPrintf(dir.path.c_str(), userId))) {
267             return E_DESTROY_DIR;
268         }
269     }
270 
271     return E_OK;
272 }
273 } // namespace StorageDaemon
274 } // namespace OHOS
275