• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 "ipc/storage_manager_stub.h"
17 #include "accesstoken_kit.h"
18 #include "ipc_skeleton.h"
19 #include "storage_manager_ipc_interface_code.h"
20 #include "storage_service_errno.h"
21 #include "storage_service_log.h"
22 
23 namespace OHOS {
24 namespace StorageManager {
25 using namespace std;
26 
27 constexpr pid_t ACCOUNT_UID = 3058;
28 constexpr pid_t BACKUP_SA_UID = 1089;
29 constexpr pid_t FOUNDATION_UID = 5523;
30 const std::string PERMISSION_STORAGE_MANAGER_CRYPT = "ohos.permission.STORAGE_MANAGER_CRYPT";
31 const std::string PERMISSION_STORAGE_MANAGER = "ohos.permission.STORAGE_MANAGER";
32 const std::string PERMISSION_MOUNT_MANAGER = "ohos.permission.MOUNT_UNMOUNT_MANAGER";
33 const std::string PERMISSION_FORMAT_MANAGER = "ohos.permission.MOUNT_FORMAT_MANAGER";
34 const std::string PROCESS_NAME_FOUNDATION = "foundation";
CheckClientPermission(const std::string & permissionStr)35 bool CheckClientPermission(const std::string& permissionStr)
36 {
37     Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
38     auto uid = IPCSkeleton::GetCallingUid();
39     auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenCaller);
40     int res = Security::AccessToken::PermissionState::PERMISSION_DENIED;
41     if (tokenType == Security::AccessToken::TOKEN_NATIVE && uid == ACCOUNT_UID) {
42         res = Security::AccessToken::PermissionState::PERMISSION_GRANTED;
43     } else {
44         res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permissionStr);
45     }
46 
47     if (res == Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
48         LOGI("StorageMangaer permissionCheck pass!");
49         return true;
50     }
51     LOGE("StorageManager permissionCheck error, need %{public}s", permissionStr.c_str());
52     return false;
53 }
54 
CheckClientPermissionForCrypt(const std::string & permissionStr)55 bool CheckClientPermissionForCrypt(const std::string& permissionStr)
56 {
57     Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
58     int res = Security::AccessToken::AccessTokenKit::VerifyAccessToken(tokenCaller, permissionStr);
59     if (res == Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
60         LOGI("StorageMangaer permissionCheck pass!");
61         return true;
62     }
63     LOGE("StorageManager permissionCheck error, need %{public}s", permissionStr.c_str());
64     return false;
65 }
66 
CheckClientPermissionForShareFile()67 bool CheckClientPermissionForShareFile()
68 {
69     Security::AccessToken::AccessTokenID tokenCaller = IPCSkeleton::GetCallingTokenID();
70     Security::AccessToken::NativeTokenInfo nativeInfo;
71     Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(tokenCaller, nativeInfo);
72 
73     auto uid = IPCSkeleton::GetCallingUid();
74     if (nativeInfo.processName != PROCESS_NAME_FOUNDATION || uid != FOUNDATION_UID) {
75         LOGE("CheckClientPermissionForShareFile error, processName is %{public}s, uid is %{public}d",
76             nativeInfo.processName.c_str(), uid);
77         return false;
78     }
79 
80     return true;
81 }
82 
StorageManagerStub()83 StorageManagerStub::StorageManagerStub()
84 {
85     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::PREPARE_ADD_USER)] =
86         &StorageManagerStub::HandlePrepareAddUser;
87     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::REMOVE_USER)] =
88         &StorageManagerStub::HandleRemoveUser;
89     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::PREPARE_START_USER)] =
90         &StorageManagerStub::HandlePrepareStartUser;
91     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::STOP_USER)] =
92         &StorageManagerStub::HandleStopUser;
93     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_TOTAL)] =
94         &StorageManagerStub::HandleGetTotal;
95     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_FREE)] =
96         &StorageManagerStub::HandleGetFree;
97     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_SYSTEM_SIZE)] =
98         &StorageManagerStub::HandleGetSystemSize;
99     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_TOTAL_SIZE)] =
100         &StorageManagerStub::HandleGetTotalSize;
101     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_FREE_SIZE)] =
102         &StorageManagerStub::HandleGetFreeSize;
103     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_CURR_USER_STATS)] =
104         &StorageManagerStub::HandleGetCurrUserStorageStats;
105     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_USER_STATS)] =
106         &StorageManagerStub::HandleGetUserStorageStats;
107     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_USER_STATS_BY_TYPE)] =
108         &StorageManagerStub::HandleGetUserStorageStatsByType;
109     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_CURR_BUNDLE_STATS)] =
110         &StorageManagerStub::HandleGetCurrentBundleStats;
111     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_BUNDLE_STATUS)] =
112         &StorageManagerStub::HandleGetBundleStatus;
113     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::NOTIFY_VOLUME_CREATED)] =
114         &StorageManagerStub::HandleNotifyVolumeCreated;
115     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::NOTIFY_VOLUME_MOUNTED)] =
116         &StorageManagerStub::HandleNotifyVolumeMounted;
117     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::NOTIFY_VOLUME_STATE_CHANGED)] =
118         &StorageManagerStub::HandleNotifyVolumeStateChanged;
119     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::MOUNT)] =
120         &StorageManagerStub::HandleMount;
121     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::UNMOUNT)] =
122         &StorageManagerStub::HandleUnmount;
123     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_ALL_VOLUMES)] =
124         &StorageManagerStub::HandleGetAllVolumes;
125     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::NOTIFY_DISK_CREATED)] =
126         &StorageManagerStub::HandleNotifyDiskCreated;
127     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::NOTIFY_DISK_DESTROYED)] =
128         &StorageManagerStub::HandleNotifyDiskDestroyed;
129     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::PARTITION)] =
130         &StorageManagerStub::HandlePartition;
131     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_ALL_DISKS)] =
132         &StorageManagerStub::HandleGetAllDisks;
133     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_VOL_BY_UUID)] =
134         &StorageManagerStub::HandleGetVolumeByUuid;
135     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_VOL_BY_ID)] =
136         &StorageManagerStub::HandleGetVolumeById;
137     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::SET_VOL_DESC)] =
138         &StorageManagerStub::HandleSetVolDesc;
139     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::FORMAT)] =
140         &StorageManagerStub::HandleFormat;
141     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::GET_DISK_BY_ID)] =
142         &StorageManagerStub::HandleGetDiskById;
143     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::CREATE_USER_KEYS)] =
144         &StorageManagerStub::HandleGenerateUserKeys;
145     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::DELETE_USER_KEYS)] =
146         &StorageManagerStub::HandleDeleteUserKeys;
147     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::UPDATE_USER_AUTH)] =
148         &StorageManagerStub::HandleUpdateUserAuth;
149     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::ACTIVE_USER_KEY)] =
150         &StorageManagerStub::HandleActiveUserKey;
151     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::INACTIVE_USER_KEY)] =
152         &StorageManagerStub::HandleInactiveUserKey;
153     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::LOCK_USER_SCREEN)] =
154         &StorageManagerStub::HandleLockUserScreen;
155     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::UNLOCK_USER_SCREEN)] =
156         &StorageManagerStub::HandleUnlockUserScreen;
157     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::LOCK_SCREEN_STATUS)] =
158         &StorageManagerStub::HandleGetLockScreenStatus;
159     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::UPDATE_KEY_CONTEXT)] =
160         &StorageManagerStub::HandleUpdateKeyContext;
161     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::CREATE_SHARE_FILE)] =
162         &StorageManagerStub::HandleCreateShareFile;
163     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::DELETE_SHARE_FILE)] =
164         &StorageManagerStub::HandleDeleteShareFile;
165     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::SET_BUNDLE_QUOTA)] =
166         &StorageManagerStub::HandleSetBundleQuota;
167     opToInterfaceMap_[static_cast<uint32_t>(StorageManagerInterfaceCode::UPDATE_MEM_PARA)] =
168         &StorageManagerStub::HandleUpdateMemoryPara;
169 }
170 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)171 int32_t StorageManagerStub::OnRemoteRequest(uint32_t code,
172     MessageParcel &data, MessageParcel &reply, MessageOption &option)
173 {
174     if (data.ReadInterfaceToken() != GetDescriptor()) {
175         return E_PERMISSION_DENIED;
176     }
177     auto interfaceIndex = opToInterfaceMap_.find(code);
178     if (interfaceIndex == opToInterfaceMap_.end() || !interfaceIndex->second) {
179         LOGE("Cannot response request %d: unknown tranction", code);
180         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
181     }
182     return (this->*(interfaceIndex->second))(data, reply);
183 }
184 
HandlePrepareAddUser(MessageParcel & data,MessageParcel & reply)185 int32_t StorageManagerStub::HandlePrepareAddUser(MessageParcel &data, MessageParcel &reply)
186 {
187     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT)) {
188         return E_PERMISSION_DENIED;
189     }
190     int32_t userId = data.ReadInt32();
191     uint32_t flags = data.ReadUint32();
192     LOGI("StorageManagerStub::HandlePrepareAddUser, userId:%{public}d", userId);
193     int err = PrepareAddUser(userId, flags);
194     if (!reply.WriteUint32(err)) {
195         LOGE("StorageManagerStub::HandlePrepareAddUser call PrepareAddUser failed");
196         return  E_WRITE_REPLY_ERR;
197     }
198     return E_OK;
199 }
200 
HandleRemoveUser(MessageParcel & data,MessageParcel & reply)201 int32_t StorageManagerStub::HandleRemoveUser(MessageParcel &data, MessageParcel &reply)
202 {
203     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT)) {
204         return E_PERMISSION_DENIED;
205     }
206     int32_t userId = data.ReadInt32();
207     uint32_t flags = data.ReadUint32();
208     LOGI("StorageManagerStub::HandleRemoveUser, userId:%{public}d", userId);
209     int err = RemoveUser(userId, flags);
210     if (!reply.WriteUint32(err)) {
211         LOGE("StorageManagerStub::HandleRemoveUser call RemoveUser failed");
212         return E_WRITE_REPLY_ERR;
213     }
214     return E_OK;
215 }
216 
HandlePrepareStartUser(MessageParcel & data,MessageParcel & reply)217 int32_t StorageManagerStub::HandlePrepareStartUser(MessageParcel &data, MessageParcel &reply)
218 {
219     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
220         return E_PERMISSION_DENIED;
221     }
222     int32_t userId = data.ReadInt32();
223     LOGI("StorageManagerStub::HandlePrepareStartUser, userId:%{public}d", userId);
224     int err = PrepareStartUser(userId);
225     if (!reply.WriteUint32(err)) {
226         LOGE("StorageManagerStub::HandlePrepareStartUser call PrepareStartUser failed");
227         return E_WRITE_REPLY_ERR;
228     }
229     return E_OK;
230 }
231 
HandleStopUser(MessageParcel & data,MessageParcel & reply)232 int32_t StorageManagerStub::HandleStopUser(MessageParcel &data, MessageParcel &reply)
233 {
234     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
235         return E_PERMISSION_DENIED;
236     }
237     int32_t userId = data.ReadInt32();
238     LOGI("StorageManagerStub::HandleStopUser, userId:%{public}d", userId);
239     int err = StopUser(userId);
240     if (!reply.WriteUint32(err)) {
241         LOGE("StorageManagerStub::HandleStopUser call StopUser failed");
242         return E_WRITE_REPLY_ERR;
243     }
244     return E_OK;
245 }
246 
HandleGetTotal(MessageParcel & data,MessageParcel & reply)247 int32_t StorageManagerStub::HandleGetTotal(MessageParcel &data, MessageParcel &reply)
248 {
249     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
250         return E_PERMISSION_DENIED;
251     }
252     LOGE("StorageManagerStub::HandleGetTotal Begin.");
253     std::string volumeId = data.ReadString();
254     int64_t totalSize;
255     int32_t err = GetTotalSizeOfVolume(volumeId, totalSize);
256     if (!reply.WriteInt32(err)) {
257         LOGE("StorageManagerStub::HandleGetTotal call OnUserDGetTotalSizeOfVolume failed");
258         return  E_WRITE_REPLY_ERR;
259     }
260     if (!reply.WriteInt64(totalSize)) {
261         return  E_WRITE_REPLY_ERR;
262     }
263     return E_OK;
264 }
265 
HandleGetFree(MessageParcel & data,MessageParcel & reply)266 int32_t StorageManagerStub::HandleGetFree(MessageParcel &data, MessageParcel &reply)
267 {
268     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
269         return E_PERMISSION_DENIED;
270     }
271     std::string volumeId = data.ReadString();
272     int64_t freeSize;
273     int32_t err = GetFreeSizeOfVolume(volumeId, freeSize);
274     if (!reply.WriteInt32(err)) {
275         LOGE("StorageManagerStub::HandleGetFree call GetFreeSizeOfVolume failed");
276         return  E_WRITE_REPLY_ERR;
277     }
278     if (!reply.WriteInt64(freeSize)) {
279         return  E_WRITE_REPLY_ERR;
280     }
281     return E_OK;
282 }
283 
HandleGetBundleStatus(MessageParcel & data,MessageParcel & reply)284 int32_t StorageManagerStub::HandleGetBundleStatus(MessageParcel &data, MessageParcel &reply)
285 {
286     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
287         return E_PERMISSION_DENIED;
288     }
289     std::string pkgName = data.ReadString();
290     BundleStats bundleStats;
291     int32_t err = GetBundleStats(pkgName, bundleStats);
292     if (!reply.WriteInt32(err)) {
293         return  E_WRITE_REPLY_ERR;
294     }
295     if (!bundleStats.Marshalling(reply)) {
296         return  E_WRITE_REPLY_ERR;
297     }
298     return E_OK;
299 }
300 
HandleGetSystemSize(MessageParcel & data,MessageParcel & reply)301 int32_t StorageManagerStub::HandleGetSystemSize(MessageParcel &data, MessageParcel &reply)
302 {
303     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
304         return E_PERMISSION_DENIED;
305     }
306     int64_t systemSize;
307     int32_t err = GetSystemSize(systemSize);
308     if (!reply.WriteInt32(err)) {
309         return  E_WRITE_REPLY_ERR;
310     }
311     if (!reply.WriteInt64(systemSize)) {
312         LOGE("StorageManagerStub::HandleGetFree call GetSystemSize failed");
313         return  E_WRITE_REPLY_ERR;
314     }
315     return E_OK;
316 }
317 
HandleGetTotalSize(MessageParcel & data,MessageParcel & reply)318 int32_t StorageManagerStub::HandleGetTotalSize(MessageParcel &data, MessageParcel &reply)
319 {
320     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
321         return E_PERMISSION_DENIED;
322     }
323     int64_t totalSize;
324     int32_t err = GetTotalSize(totalSize);
325     if (!reply.WriteInt32(err)) {
326         return  E_WRITE_REPLY_ERR;
327     }
328     if (!reply.WriteInt64(totalSize)) {
329         LOGE("StorageManagerStub::HandleGetFree call GetTotalSize failed");
330         return  E_WRITE_REPLY_ERR;
331     }
332     return E_OK;
333 }
334 
HandleGetFreeSize(MessageParcel & data,MessageParcel & reply)335 int32_t StorageManagerStub::HandleGetFreeSize(MessageParcel &data, MessageParcel &reply)
336 {
337     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
338         return E_PERMISSION_DENIED;
339     }
340     int64_t freeSize;
341     int32_t err = GetFreeSize(freeSize);
342     if (!reply.WriteInt32(err)) {
343         return  E_WRITE_REPLY_ERR;
344     }
345     if (!reply.WriteInt64(freeSize)) {
346         LOGE("StorageManagerStub::HandleGetFree call GetFreeSize failed");
347         return  E_WRITE_REPLY_ERR;
348     }
349     return E_OK;
350 }
351 
HandleGetCurrUserStorageStats(MessageParcel & data,MessageParcel & reply)352 int32_t StorageManagerStub::HandleGetCurrUserStorageStats(MessageParcel &data, MessageParcel &reply)
353 {
354     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
355         return E_PERMISSION_DENIED;
356     }
357     StorageStats storageStats;
358     int32_t err = GetUserStorageStats(storageStats);
359     if (!reply.WriteInt32(err)) {
360         return  E_WRITE_REPLY_ERR;
361     }
362     if (!storageStats.Marshalling(reply)) {
363         return  E_WRITE_REPLY_ERR;
364     }
365     return E_OK;
366 }
367 
HandleGetUserStorageStats(MessageParcel & data,MessageParcel & reply)368 int32_t StorageManagerStub::HandleGetUserStorageStats(MessageParcel &data, MessageParcel &reply)
369 {
370     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
371         return E_PERMISSION_DENIED;
372     }
373     int32_t userId = data.ReadInt32();
374     StorageStats storageStats;
375     int32_t err = GetUserStorageStats(userId, storageStats);
376     if (!reply.WriteInt32(err)) {
377         return  E_WRITE_REPLY_ERR;
378     }
379     if (!storageStats.Marshalling(reply)) {
380         return  E_WRITE_REPLY_ERR;
381     }
382     return E_OK;
383 }
384 
HandleGetCurrentBundleStats(MessageParcel & data,MessageParcel & reply)385 int32_t StorageManagerStub::HandleGetCurrentBundleStats(MessageParcel &data, MessageParcel &reply)
386 {
387     BundleStats bundleStats;
388     int32_t err = GetCurrentBundleStats(bundleStats);
389     if (!reply.WriteInt32(err)) {
390         return  E_WRITE_REPLY_ERR;
391     }
392     if (!bundleStats.Marshalling(reply)) {
393         return  E_WRITE_REPLY_ERR;
394     }
395     return E_OK;
396 }
397 
HandleGetAllVolumes(MessageParcel & data,MessageParcel & reply)398 int32_t StorageManagerStub::HandleGetAllVolumes(MessageParcel &data, MessageParcel &reply)
399 {
400     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
401         return E_PERMISSION_DENIED;
402     }
403     std::vector<VolumeExternal> ve;
404     int32_t err = GetAllVolumes(ve);
405     if (!reply.WriteInt32(err)) {
406         return  E_WRITE_REPLY_ERR;
407     }
408     uint size = ve.size();
409     if (size == 0) {
410         LOGE("StorageManagerStub::No volume.");
411         if (!reply.WriteUint32(0)) {
412             return  E_WRITE_REPLY_ERR;
413         }
414         return E_OK;
415     }
416     if (!reply.WriteUint32(ve.size())) {
417         return  E_WRITE_REPLY_ERR;
418     }
419     for (uint i = 0; i < size; i++) {
420         if (!ve[i].Marshalling(reply)) {
421             return  E_WRITE_REPLY_ERR;
422         }
423     }
424     return E_OK;
425 }
426 
HandleNotifyVolumeCreated(MessageParcel & data,MessageParcel & reply)427 int32_t StorageManagerStub::HandleNotifyVolumeCreated(MessageParcel &data, MessageParcel &reply)
428 {
429     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
430         return E_PERMISSION_DENIED;
431     }
432     std::unique_ptr<VolumeCore> vc = VolumeCore::Unmarshalling(data);
433     NotifyVolumeCreated(*vc);
434     LOGI("StorageManagerStub::HandleNotifyVolumeCreated");
435     return E_OK;
436 }
437 
HandleNotifyVolumeMounted(MessageParcel & data,MessageParcel & reply)438 int32_t StorageManagerStub::HandleNotifyVolumeMounted(MessageParcel &data, MessageParcel &reply)
439 {
440     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
441         return E_PERMISSION_DENIED;
442     }
443     std::string volumeId = data.ReadString();
444     int32_t fsType = data.ReadInt32();
445     std::string fsUuid = data.ReadString();
446     std::string path = data.ReadString();
447     std::string description = data.ReadString();
448     NotifyVolumeMounted(volumeId, fsType, fsUuid, path, description);
449     LOGI("StorageManagerStub::HandleNotifyVolumeMounted");
450     return E_OK;
451 }
452 
HandleNotifyVolumeStateChanged(MessageParcel & data,MessageParcel & reply)453 int32_t StorageManagerStub::HandleNotifyVolumeStateChanged(MessageParcel &data, MessageParcel &reply)
454 {
455     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
456         return E_PERMISSION_DENIED;
457     }
458     std::string volumeId = data.ReadString();
459     VolumeState state = VolumeState(data.ReadInt32());
460     NotifyVolumeStateChanged(volumeId, state);
461     LOGI("StorageManagerStub::HandleNotifyVolumeStateChanged");
462     return E_OK;
463 }
464 
HandleMount(MessageParcel & data,MessageParcel & reply)465 int32_t StorageManagerStub::HandleMount(MessageParcel &data, MessageParcel &reply)
466 {
467     if (!CheckClientPermission(PERMISSION_MOUNT_MANAGER)) {
468         return E_PERMISSION_DENIED;
469     }
470     std::string volumeId = data.ReadString();
471     int err = Mount(volumeId);
472     if (!reply.WriteUint32(err)) {
473         LOGE("StorageManagerStub::HandleMount call Mount failed");
474         return  E_WRITE_REPLY_ERR;
475     }
476     return E_OK;
477 }
478 
HandleUnmount(MessageParcel & data,MessageParcel & reply)479 int32_t StorageManagerStub::HandleUnmount(MessageParcel &data, MessageParcel &reply)
480 {
481     if (!CheckClientPermission(PERMISSION_MOUNT_MANAGER)) {
482         return E_PERMISSION_DENIED;
483     }
484     std::string volumeId = data.ReadString();
485     int err = Unmount(volumeId);
486     if (!reply.WriteUint32(err)) {
487         LOGE("StorageManagerStub::HandleUnmount call Mount failed");
488         return  E_WRITE_REPLY_ERR;
489     }
490     return E_OK;
491 }
492 
HandleNotifyDiskCreated(MessageParcel & data,MessageParcel & reply)493 int32_t StorageManagerStub::HandleNotifyDiskCreated(MessageParcel &data, MessageParcel &reply)
494 {
495     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
496         return E_PERMISSION_DENIED;
497     }
498     auto disk = Disk::Unmarshalling(data);
499     NotifyDiskCreated(*disk);
500     return E_OK;
501 }
502 
HandleNotifyDiskDestroyed(MessageParcel & data,MessageParcel & reply)503 int32_t StorageManagerStub::HandleNotifyDiskDestroyed(MessageParcel &data, MessageParcel &reply)
504 {
505     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
506         return E_PERMISSION_DENIED;
507     }
508     std::string diskId = data.ReadString();
509     NotifyDiskDestroyed(diskId);
510     return E_OK;
511 }
512 
HandlePartition(MessageParcel & data,MessageParcel & reply)513 int32_t StorageManagerStub::HandlePartition(MessageParcel &data, MessageParcel &reply)
514 {
515     if (!CheckClientPermission(PERMISSION_FORMAT_MANAGER)) {
516         return E_PERMISSION_DENIED;
517     }
518     std::string diskId = data.ReadString();
519     int32_t type = data.ReadInt32();
520     int err = Partition(diskId, type);
521     if (!reply.WriteUint32(err)) {
522         LOGE("StorageManagerStub::HandlePartition call Partition failed");
523         return E_WRITE_REPLY_ERR;
524     }
525     return E_OK;
526 }
527 
HandleGetAllDisks(MessageParcel & data,MessageParcel & reply)528 int32_t StorageManagerStub::HandleGetAllDisks(MessageParcel &data, MessageParcel &reply)
529 {
530     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
531         return E_PERMISSION_DENIED;
532     }
533     std::vector<Disk> disks;
534     int32_t err = GetAllDisks(disks);
535     if (!reply.WriteUint32(err)) {
536         return E_WRITE_REPLY_ERR;
537     }
538     uint size = disks.size();
539     if (size == 0) {
540         LOGE("StorageManagerStub::No Disk.");
541         if (!reply.WriteUint32(0)) {
542             return  E_WRITE_REPLY_ERR;
543         }
544         return E_OK;
545     }
546     if (!reply.WriteUint32(disks.size())) {
547         return  E_WRITE_REPLY_ERR;
548     }
549     for (uint i = 0; i < size; i++) {
550         if (!disks[i].Marshalling(reply)) {
551             return  E_WRITE_REPLY_ERR;
552         }
553     }
554     return E_OK;
555 }
556 
HandleGetVolumeByUuid(MessageParcel & data,MessageParcel & reply)557 int32_t StorageManagerStub::HandleGetVolumeByUuid(MessageParcel &data, MessageParcel &reply)
558 {
559     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
560         return E_PERMISSION_DENIED;
561     }
562     std::string fsUuid = data.ReadString();
563     VolumeExternal vc;
564     int err = GetVolumeByUuid(fsUuid, vc);
565     if (!vc.Marshalling(reply)) {
566         return E_WRITE_REPLY_ERR;
567     }
568     if (!reply.WriteUint32(err)) {
569         LOGE("StorageManagerStub::HandleGetVolumeByUuid call GetVolumeByUuid failed");
570         return E_WRITE_REPLY_ERR;
571     }
572     return E_OK;
573 }
574 
HandleGetVolumeById(MessageParcel & data,MessageParcel & reply)575 int32_t StorageManagerStub::HandleGetVolumeById(MessageParcel &data, MessageParcel &reply)
576 {
577     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
578         return E_PERMISSION_DENIED;
579     }
580     std::string volId = data.ReadString();
581     VolumeExternal vc;
582     int err = GetVolumeById(volId, vc);
583     if (!vc.Marshalling(reply)) {
584         return E_WRITE_REPLY_ERR;
585     }
586     if (!reply.WriteUint32(err)) {
587         LOGE("StorageManagerStub::HandleGetVolumeById call GetVolumeById failed");
588         return E_WRITE_REPLY_ERR;
589     }
590     return E_OK;
591 }
592 
HandleSetVolDesc(MessageParcel & data,MessageParcel & reply)593 int32_t StorageManagerStub::HandleSetVolDesc(MessageParcel &data, MessageParcel &reply)
594 {
595     if (!CheckClientPermission(PERMISSION_MOUNT_MANAGER)) {
596         return E_PERMISSION_DENIED;
597     }
598     std::string fsUuid = data.ReadString();
599     std::string desc = data.ReadString();
600     int err = SetVolumeDescription(fsUuid, desc);
601     if (!reply.WriteUint32(err)) {
602         LOGE("StorageManagerStub::HandleSetVolDesc call SetVolumeDescription failed");
603         return E_WRITE_REPLY_ERR;
604     }
605     return E_OK;
606 }
607 
HandleFormat(MessageParcel & data,MessageParcel & reply)608 int32_t StorageManagerStub::HandleFormat(MessageParcel &data, MessageParcel &reply)
609 {
610     if (!CheckClientPermission(PERMISSION_FORMAT_MANAGER)) {
611         return E_PERMISSION_DENIED;
612     }
613     std::string volId = data.ReadString();
614     std::string fsType = data.ReadString();
615     int err = Format(volId, fsType);
616     if (!reply.WriteUint32(err)) {
617         LOGE("StorageManagerStub::HandleFormat call Format failed");
618         return E_WRITE_REPLY_ERR;
619     }
620     return E_OK;
621 }
622 
HandleGetDiskById(MessageParcel & data,MessageParcel & reply)623 int32_t StorageManagerStub::HandleGetDiskById(MessageParcel &data, MessageParcel &reply)
624 {
625     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
626         return E_PERMISSION_DENIED;
627     }
628     std::string volId = data.ReadString();
629     Disk disk;
630     int err = GetDiskById(volId, disk);
631     if (!disk.Marshalling(reply)) {
632         return E_WRITE_REPLY_ERR;
633     }
634     if (!reply.WriteUint32(err)) {
635         LOGE("StorageManagerStub::HandleGetDiskById call GetDiskById failed");
636         return E_WRITE_REPLY_ERR;
637     }
638     return E_OK;
639 }
640 
HandleGenerateUserKeys(MessageParcel & data,MessageParcel & reply)641 int32_t StorageManagerStub::HandleGenerateUserKeys(MessageParcel &data, MessageParcel &reply)
642 {
643     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
644         return E_PERMISSION_DENIED;
645     }
646     uint32_t userId = data.ReadUint32();
647     uint32_t flags = data.ReadUint32();
648     int32_t err = GenerateUserKeys(userId, flags);
649     if (!reply.WriteInt32(err)) {
650         LOGE("Write reply error code failed");
651         return E_WRITE_REPLY_ERR;
652     }
653 
654     return E_OK;
655 }
656 
HandleDeleteUserKeys(MessageParcel & data,MessageParcel & reply)657 int32_t StorageManagerStub::HandleDeleteUserKeys(MessageParcel &data, MessageParcel &reply)
658 {
659     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
660         return E_PERMISSION_DENIED;
661     }
662     uint32_t userId = data.ReadUint32();
663     int32_t err = DeleteUserKeys(userId);
664     if (!reply.WriteInt32(err)) {
665         LOGE("Write reply error code failed");
666         return E_WRITE_REPLY_ERR;
667     }
668 
669     return E_OK;
670 }
671 
HandleUpdateUserAuth(MessageParcel & data,MessageParcel & reply)672 int32_t StorageManagerStub::HandleUpdateUserAuth(MessageParcel &data, MessageParcel &reply)
673 {
674     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT)) {
675         return E_PERMISSION_DENIED;
676     }
677     uint32_t userId = data.ReadUint32();
678     uint64_t secureUid = data.ReadUint64();
679 
680     std::vector<uint8_t> token;
681     std::vector<uint8_t> oldSecret;
682     std::vector<uint8_t> newSecret;
683     data.ReadUInt8Vector(&token);
684     data.ReadUInt8Vector(&oldSecret);
685     data.ReadUInt8Vector(&newSecret);
686 
687     int32_t err = UpdateUserAuth(userId, secureUid, token, oldSecret, newSecret);
688     if (!reply.WriteInt32(err)) {
689         LOGE("Write reply error code failed");
690         return E_WRITE_REPLY_ERR;
691     }
692     return E_OK;
693 }
694 
HandleActiveUserKey(MessageParcel & data,MessageParcel & reply)695 int32_t StorageManagerStub::HandleActiveUserKey(MessageParcel &data, MessageParcel &reply)
696 {
697     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT)) {
698         return E_PERMISSION_DENIED;
699     }
700     uint32_t userId = data.ReadUint32();
701 
702     std::vector<uint8_t> token;
703     std::vector<uint8_t> secret;
704     data.ReadUInt8Vector(&token);
705     data.ReadUInt8Vector(&secret);
706 
707     int32_t err = ActiveUserKey(userId, token, secret);
708     if (!reply.WriteInt32(err)) {
709         LOGE("Write reply error code failed");
710         return E_WRITE_REPLY_ERR;
711     }
712     return E_OK;
713 }
714 
HandleInactiveUserKey(MessageParcel & data,MessageParcel & reply)715 int32_t StorageManagerStub::HandleInactiveUserKey(MessageParcel &data, MessageParcel &reply)
716 {
717     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT)) {
718         return E_PERMISSION_DENIED;
719     }
720     uint32_t userId = data.ReadUint32();
721     int32_t err = InactiveUserKey(userId);
722     if (!reply.WriteInt32(err)) {
723         LOGE("Write reply error code failed");
724         return E_WRITE_REPLY_ERR;
725     }
726     return E_OK;
727 }
728 
HandleLockUserScreen(MessageParcel & data,MessageParcel & reply)729 int32_t StorageManagerStub::HandleLockUserScreen(MessageParcel &data, MessageParcel &reply)
730 {
731     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
732         return E_PERMISSION_DENIED;
733     }
734     uint32_t userId = data.ReadUint32();
735     int32_t err = LockUserScreen(userId);
736     if (!reply.WriteInt32(err)) {
737         LOGE("Write reply error code failed");
738         return E_WRITE_REPLY_ERR;
739     }
740 
741     return E_OK;
742 }
743 
HandleUnlockUserScreen(MessageParcel & data,MessageParcel & reply)744 int32_t StorageManagerStub::HandleUnlockUserScreen(MessageParcel &data, MessageParcel &reply)
745 {
746     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT)) {
747         return E_PERMISSION_DENIED;
748     }
749     uint32_t userId = data.ReadUint32();
750     int32_t err = UnlockUserScreen(userId);
751     if (!reply.WriteInt32(err)) {
752         LOGE("Write reply error code failed");
753         return E_WRITE_REPLY_ERR;
754     }
755     return E_OK;
756 }
757 
HandleGetLockScreenStatus(MessageParcel & data,MessageParcel & reply)758 int32_t StorageManagerStub::HandleGetLockScreenStatus(MessageParcel &data, MessageParcel &reply)
759 {
760     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER)) {
761         return E_PERMISSION_DENIED;
762     }
763     uint32_t userId = data.ReadUint32();
764     bool lockScreenStatus = false;
765     int32_t err = GetLockScreenStatus(userId, lockScreenStatus);
766     if (!reply.WriteBool(lockScreenStatus)) {
767         LOGE("Write reply lockScreenStatus failed");
768         return E_WRITE_REPLY_ERR;
769     }
770     if (!reply.WriteInt32(err)) {
771         LOGE("Write reply error code failed");
772         return E_WRITE_REPLY_ERR;
773     }
774     return E_OK;
775 }
776 
HandleUpdateKeyContext(MessageParcel & data,MessageParcel & reply)777 int32_t StorageManagerStub::HandleUpdateKeyContext(MessageParcel &data, MessageParcel &reply)
778 {
779     if (!CheckClientPermissionForCrypt(PERMISSION_STORAGE_MANAGER_CRYPT)) {
780         return E_PERMISSION_DENIED;
781     }
782     uint32_t userId = data.ReadUint32();
783     int32_t err = UpdateKeyContext(userId);
784     if (!reply.WriteInt32(err)) {
785         LOGE("Write reply error code failed");
786         return E_WRITE_REPLY_ERR;
787     }
788     return E_OK;
789 }
790 
HandleCreateShareFile(MessageParcel & data,MessageParcel & reply)791 int32_t StorageManagerStub::HandleCreateShareFile(MessageParcel &data, MessageParcel &reply)
792 {
793     if (!CheckClientPermissionForShareFile()) {
794         return E_PERMISSION_DENIED;
795     }
796 
797     std::vector<std::string> uriList;
798     if (!data.ReadStringVector(&uriList)) {
799         return E_WRITE_REPLY_ERR;
800     }
801     uint32_t tokenId = data.ReadUint32();
802     uint32_t flag = data.ReadUint32();
803     std::vector<int32_t> retList = CreateShareFile(uriList, tokenId, flag);
804     if (!reply.WriteInt32Vector(retList)) {
805         return E_WRITE_REPLY_ERR;
806     }
807     return E_OK;
808 }
809 
HandleDeleteShareFile(MessageParcel & data,MessageParcel & reply)810 int32_t StorageManagerStub::HandleDeleteShareFile(MessageParcel &data, MessageParcel &reply)
811 {
812     if (!CheckClientPermissionForShareFile()) {
813         return E_PERMISSION_DENIED;
814     }
815 
816     uint32_t tokenId = data.ReadUint32();
817     std::vector<std::string> uriList;
818     if (!data.ReadStringVector(&uriList)) {
819         return E_WRITE_REPLY_ERR;
820     }
821 
822     int err = DeleteShareFile(tokenId, uriList);
823     if (!reply.WriteInt32(err)) {
824         return E_WRITE_REPLY_ERR;
825     }
826     return E_OK;
827 }
828 
HandleSetBundleQuota(MessageParcel & data,MessageParcel & reply)829 int32_t StorageManagerStub::HandleSetBundleQuota(MessageParcel &data, MessageParcel &reply)
830 {
831     if (!CheckClientPermission(PERMISSION_STORAGE_MANAGER)) {
832         return E_PERMISSION_DENIED;
833     }
834 
835     std::string bundleName = data.ReadString();
836     int32_t uid = data.ReadInt32();
837     std::string bundleDataDirPath = data.ReadString();
838     int32_t limitSizeMb = data.ReadInt32();
839     int err = SetBundleQuota(bundleName, uid, bundleDataDirPath, limitSizeMb);
840     if (!reply.WriteInt32(err)) {
841         return E_WRITE_REPLY_ERR;
842     }
843     return E_OK;
844 }
845 
HandleGetUserStorageStatsByType(MessageParcel & data,MessageParcel & reply)846 int32_t StorageManagerStub::HandleGetUserStorageStatsByType(MessageParcel &data, MessageParcel &reply)
847 {
848     if (IPCSkeleton::GetCallingUid() != BACKUP_SA_UID) {
849         LOGE("StorageManager permissionCheck error, calling uid is invalid, need backup_sa uid.");
850         return E_PERMISSION_DENIED;
851     }
852 
853     int32_t userId = data.ReadInt32();
854     std::string type = data.ReadString();
855     StorageStats storageStats;
856     int32_t err = GetUserStorageStatsByType(userId, storageStats, type);
857     if (!reply.WriteInt32(err)) {
858         return E_WRITE_REPLY_ERR;
859     }
860     if (!storageStats.Marshalling(reply)) {
861         return  E_WRITE_REPLY_ERR;
862     }
863     return E_OK;
864 }
865 
HandleUpdateMemoryPara(MessageParcel & data,MessageParcel & reply)866 int32_t StorageManagerStub::HandleUpdateMemoryPara(MessageParcel &data, MessageParcel &reply)
867 {
868     if (IPCSkeleton::GetCallingUid() != BACKUP_SA_UID) {
869         LOGE("StorageManager permissionCheck error, calling uid is invalid, need backup_sa uid.");
870         return E_PERMISSION_DENIED;
871     }
872 
873     int32_t size = data.ReadInt32();
874     int32_t oldSize = 0;
875     int err = UpdateMemoryPara(size, oldSize);
876     if (!reply.WriteInt32(err)) {
877         return E_WRITE_REPLY_ERR;
878     }
879     if (!reply.WriteInt32(oldSize)) {
880         LOGE("StorageManagerStub::HandleUpdateMemoryPara call UpdateMemoryPara failed");
881         return E_WRITE_REPLY_ERR;
882     }
883     return E_OK;
884 }
885 } // StorageManager
886 } // OHOS
887