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