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