• 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_daemon_stub.h"
17 
18 #include "ipc/storage_daemon_ipc_interface_code.h"
19 #include "storage_service_errno.h"
20 #include "storage_service_log.h"
21 #include "string_ex.h"
22 
23 namespace OHOS {
24 namespace StorageDaemon {
25 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)26 int32_t StorageDaemonStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
27                                            MessageParcel &reply, MessageOption &option)
28 {
29     auto remoteDescriptor = data.ReadInterfaceToken();
30     if (GetDescriptor() != remoteDescriptor) {
31         return E_PERMISSION_DENIED;
32     }
33 
34     LOGI("recv remote request code %{public}u", code);
35     int err = E_OK;
36     switch (code) {
37         case static_cast<int32_t>(StorageDaemonInterfaceCode::SHUTDOWN):
38             err = HandleShutdown();
39             break;
40         case static_cast<int32_t>(StorageDaemonInterfaceCode::CHECK):
41             err = HandleCheck(data, reply);
42             break;
43         case static_cast<int32_t>(StorageDaemonInterfaceCode::MOUNT):
44             err = HandleMount(data, reply);
45             break;
46         case static_cast<int32_t>(StorageDaemonInterfaceCode::UMOUNT):
47             err =HandleUMount(data, reply);
48             break;
49         case static_cast<int32_t>(StorageDaemonInterfaceCode::PARTITION):
50             err = HandlePartition(data, reply);
51             break;
52         case static_cast<int32_t>(StorageDaemonInterfaceCode::FORMAT):
53             err = HandleFormat(data, reply);
54             break;
55         case static_cast<int32_t>(StorageDaemonInterfaceCode::SET_VOL_DESC):
56             err = HandleSetVolDesc(data, reply);
57             break;
58         case static_cast<int32_t>(StorageDaemonInterfaceCode::PREPARE_USER_DIRS):
59             err = HandlePrepareUserDirs(data, reply);
60             break;
61         case static_cast<int32_t>(StorageDaemonInterfaceCode::DESTROY_USER_DIRS):
62             err = HandleDestroyUserDirs(data, reply);
63             break;
64         case static_cast<int32_t>(StorageDaemonInterfaceCode::START_USER):
65             err = HandleStartUser(data,  reply);
66             break;
67         case static_cast<int32_t>(StorageDaemonInterfaceCode::STOP_USER):
68             err = HandleStopUser(data, reply);
69             break;
70         case static_cast<int32_t>(StorageDaemonInterfaceCode::INIT_GLOBAL_KEY):
71             err = HandleInitGlobalKey(data, reply);
72             break;
73         case static_cast<int32_t>(StorageDaemonInterfaceCode::INIT_GLOBAL_USER_KEYS):
74             err = HandleInitGlobalUserKeys(data, reply);
75             break;
76         case static_cast<int32_t>(StorageDaemonInterfaceCode::CREATE_USER_KEYS):
77             err = HandleGenerateUserKeys(data, reply);
78             break;
79         case static_cast<int32_t>(StorageDaemonInterfaceCode::DELETE_USER_KEYS):
80             err = HandleDeleteUserKeys(data, reply);
81             break;
82         case static_cast<int32_t>(StorageDaemonInterfaceCode::UPDATE_USER_AUTH):
83             err = HandleUpdateUserAuth(data, reply);
84             break;
85         case static_cast<int32_t>(StorageDaemonInterfaceCode::ACTIVE_USER_KEY):
86             err = HandleActiveUserKey(data, reply);
87             break;
88         case static_cast<int32_t>(StorageDaemonInterfaceCode::INACTIVE_USER_KEY):
89             err = HandleInactiveUserKey(data, reply);
90             break;
91         case static_cast<int32_t>(StorageDaemonInterfaceCode::UPDATE_KEY_CONTEXT):
92             err = HandleUpdateKeyContext(data, reply);
93             break;
94         case static_cast<int32_t>(StorageDaemonInterfaceCode::CREATE_SHARE_FILE):
95             err = HandleCreateShareFile(data, reply);
96             break;
97         case static_cast<int32_t>(StorageDaemonInterfaceCode::DELETE_SHARE_FILE):
98             err = HandleDeleteShareFile(data, reply);
99             break;
100         case static_cast<int32_t>(StorageDaemonInterfaceCode::SET_BUNDLE_QUOTA):
101             err = HandleSetBundleQuota(data, reply);
102             break;
103         default: {
104             LOGI(" use IPCObjectStub default OnRemoteRequest");
105             err = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
106             break;
107         }
108     }
109 
110     return err;
111 }
112 
HandleShutdown()113 int32_t StorageDaemonStub::HandleShutdown()
114 {
115     Shutdown();
116     return E_OK;
117 }
118 
HandleMount(MessageParcel & data,MessageParcel & reply)119 int32_t StorageDaemonStub::HandleMount(MessageParcel &data, MessageParcel &reply)
120 {
121     std::string volId = data.ReadString();
122     uint32_t flags = data.ReadUint32();
123 
124     int err = Mount(volId, flags);
125     if (!reply.WriteInt32(err)) {
126         return  E_WRITE_REPLY_ERR;
127     }
128 
129     return E_OK;
130 }
131 
HandleUMount(MessageParcel & data,MessageParcel & reply)132 int32_t StorageDaemonStub::HandleUMount(MessageParcel &data, MessageParcel &reply)
133 {
134     std::string volId = data.ReadString();
135 
136     int err = UMount(volId);
137     if (!reply.WriteInt32(err)) {
138         return  E_WRITE_REPLY_ERR;
139     }
140 
141     return E_OK;
142 }
143 
HandleCheck(MessageParcel & data,MessageParcel & reply)144 int32_t StorageDaemonStub::HandleCheck(MessageParcel &data, MessageParcel &reply)
145 {
146     std::string volId = data.ReadString();
147 
148     int err = Check(volId);
149     if (!reply.WriteInt32(err)) {
150         return  E_WRITE_REPLY_ERR;
151     }
152 
153     return E_OK;
154 }
155 
HandleFormat(MessageParcel & data,MessageParcel & reply)156 int32_t StorageDaemonStub::HandleFormat(MessageParcel &data, MessageParcel &reply)
157 {
158     std::string volId = data.ReadString();
159     std::string fsType = data.ReadString();
160 
161     int err = Format(volId, fsType);
162     if (!reply.WriteInt32(err)) {
163         return  E_WRITE_REPLY_ERR;
164     }
165 
166     return E_OK;
167 }
168 
HandlePartition(MessageParcel & data,MessageParcel & reply)169 int32_t StorageDaemonStub::HandlePartition(MessageParcel &data, MessageParcel &reply)
170 {
171     std::string volId = data.ReadString();
172     int32_t type = data.ReadInt32();
173 
174     int err = Partition(volId, type);
175     if (!reply.WriteInt32(err)) {
176         return  E_WRITE_REPLY_ERR;
177     }
178 
179     return E_OK;
180 }
181 
HandleSetVolDesc(MessageParcel & data,MessageParcel & reply)182 int32_t StorageDaemonStub::HandleSetVolDesc(MessageParcel &data, MessageParcel &reply)
183 {
184     std::string volId = data.ReadString();
185     std::string description = data.ReadString();
186 
187     int err = SetVolumeDescription(volId, description);
188     if (!reply.WriteInt32(err)) {
189         return E_WRITE_REPLY_ERR;
190     }
191 
192     return E_OK;
193 }
194 
HandlePrepareUserDirs(MessageParcel & data,MessageParcel & reply)195 int32_t StorageDaemonStub::HandlePrepareUserDirs(MessageParcel &data, MessageParcel &reply)
196 {
197     int32_t userId = data.ReadInt32();
198     uint32_t flags = data.ReadUint32();
199 
200     int err = PrepareUserDirs(userId, flags);
201     if (!reply.WriteInt32(err)) {
202         return  E_WRITE_REPLY_ERR;
203     }
204 
205     return E_OK;
206 }
207 
HandleDestroyUserDirs(MessageParcel & data,MessageParcel & reply)208 int32_t StorageDaemonStub::HandleDestroyUserDirs(MessageParcel &data, MessageParcel &reply)
209 {
210     int32_t userId = data.ReadInt32();
211     uint32_t flags = data.ReadUint32();
212 
213     int err = DestroyUserDirs(userId, flags);
214     if (!reply.WriteInt32(err)) {
215         return  E_WRITE_REPLY_ERR;
216     }
217 
218     return E_OK;
219 }
220 
HandleStartUser(MessageParcel & data,MessageParcel & reply)221 int32_t StorageDaemonStub::HandleStartUser(MessageParcel &data, MessageParcel &reply)
222 {
223     int32_t userId = data.ReadInt32();
224 
225     int32_t err = StartUser(userId);
226     if (!reply.WriteInt32(err)) {
227         return E_WRITE_REPLY_ERR;
228     }
229 
230     return E_OK;
231 }
232 
HandleStopUser(MessageParcel & data,MessageParcel & reply)233 int32_t StorageDaemonStub::HandleStopUser(MessageParcel &data, MessageParcel &reply)
234 {
235     int32_t userId = data.ReadInt32();
236 
237     int32_t err = StopUser(userId);
238     if (!reply.WriteInt32(err)) {
239         return E_WRITE_REPLY_ERR;
240     }
241 
242     return E_OK;
243 }
244 
HandleInitGlobalKey(MessageParcel & data,MessageParcel & reply)245 int32_t StorageDaemonStub::HandleInitGlobalKey(MessageParcel &data, MessageParcel &reply)
246 {
247     int err = InitGlobalKey();
248     if (!reply.WriteInt32(err)) {
249         return E_WRITE_REPLY_ERR;
250     }
251 
252     return E_OK;
253 }
254 
HandleInitGlobalUserKeys(MessageParcel & data,MessageParcel & reply)255 int32_t StorageDaemonStub::HandleInitGlobalUserKeys(MessageParcel &data, MessageParcel &reply)
256 {
257     int err = InitGlobalUserKeys();
258     if (!reply.WriteInt32(err)) {
259         return E_WRITE_REPLY_ERR;
260     }
261 
262     return E_OK;
263 }
264 
HandleGenerateUserKeys(MessageParcel & data,MessageParcel & reply)265 int32_t StorageDaemonStub::HandleGenerateUserKeys(MessageParcel &data, MessageParcel &reply)
266 {
267     uint32_t userId = data.ReadUint32();
268     uint32_t flags = data.ReadUint32();
269 
270     int err = GenerateUserKeys(userId, flags);
271     if (!reply.WriteInt32(err)) {
272         return E_WRITE_REPLY_ERR;
273     }
274 
275     return E_OK;
276 }
277 
HandleDeleteUserKeys(MessageParcel & data,MessageParcel & reply)278 int32_t StorageDaemonStub::HandleDeleteUserKeys(MessageParcel &data, MessageParcel &reply)
279 {
280     uint32_t userId = data.ReadUint32();
281 
282     int err = DeleteUserKeys(userId);
283     if (!reply.WriteInt32(err)) {
284         return E_WRITE_REPLY_ERR;
285     }
286 
287     return E_OK;
288 }
289 
HandleUpdateUserAuth(MessageParcel & data,MessageParcel & reply)290 int32_t StorageDaemonStub::HandleUpdateUserAuth(MessageParcel &data, MessageParcel &reply)
291 {
292     uint32_t userId = data.ReadUint32();
293     uint64_t secureUid = data.ReadUint64();
294 
295     std::vector<uint8_t> token;
296     std::vector<uint8_t> oldSecret;
297     std::vector<uint8_t> newSecret;
298     data.ReadUInt8Vector(&token);
299     data.ReadUInt8Vector(&oldSecret);
300     data.ReadUInt8Vector(&newSecret);
301 
302     int err = UpdateUserAuth(userId, secureUid, token, oldSecret, newSecret);
303     if (!reply.WriteInt32(err)) {
304         return E_WRITE_REPLY_ERR;
305     }
306 
307     return E_OK;
308 }
309 
HandleActiveUserKey(MessageParcel & data,MessageParcel & reply)310 int32_t StorageDaemonStub::HandleActiveUserKey(MessageParcel &data, MessageParcel &reply)
311 {
312     uint32_t userId = data.ReadUint32();
313 
314     std::vector<uint8_t> token;
315     std::vector<uint8_t> secret;
316     data.ReadUInt8Vector(&token);
317     data.ReadUInt8Vector(&secret);
318 
319     int err = ActiveUserKey(userId, token, secret);
320     if (!reply.WriteInt32(err)) {
321         return E_WRITE_REPLY_ERR;
322     }
323 
324     return E_OK;
325 }
326 
HandleInactiveUserKey(MessageParcel & data,MessageParcel & reply)327 int32_t StorageDaemonStub::HandleInactiveUserKey(MessageParcel &data, MessageParcel &reply)
328 {
329     uint32_t userId = data.ReadUint32();
330 
331     int err = InactiveUserKey(userId);
332     if (!reply.WriteInt32(err)) {
333         return E_WRITE_REPLY_ERR;
334     }
335 
336     return E_OK;
337 }
338 
HandleUpdateKeyContext(MessageParcel & data,MessageParcel & reply)339 int32_t StorageDaemonStub::HandleUpdateKeyContext(MessageParcel &data, MessageParcel &reply)
340 {
341     uint32_t userId = data.ReadUint32();
342     int err = UpdateKeyContext(userId);
343     if (!reply.WriteInt32(err)) {
344         return E_WRITE_REPLY_ERR;
345     }
346 
347     return E_OK;
348 }
349 
HandleCreateShareFile(MessageParcel & data,MessageParcel & reply)350 int32_t StorageDaemonStub::HandleCreateShareFile(MessageParcel &data, MessageParcel &reply)
351 {
352     std::string uri = data.ReadString();
353     uint32_t tokenId = data.ReadUint32();
354     uint32_t flag = data.ReadUint32();
355     int err = CreateShareFile(uri, tokenId, flag);
356     if (!reply.WriteInt32(err)) {
357         return E_WRITE_REPLY_ERR;
358     }
359     return E_OK;
360 }
361 
HandleDeleteShareFile(MessageParcel & data,MessageParcel & reply)362 int32_t StorageDaemonStub::HandleDeleteShareFile(MessageParcel &data, MessageParcel &reply)
363 {
364     uint32_t tokenId = data.ReadUint32();
365     std::vector<std::string> sharePathList;
366     if (!data.ReadStringVector(&sharePathList)) {
367         return E_WRITE_REPLY_ERR;
368     }
369     int err = DeleteShareFile(tokenId, sharePathList);
370     if (!reply.WriteInt32(err)) {
371         return E_WRITE_REPLY_ERR;
372     }
373     return E_OK;
374 }
375 
HandleSetBundleQuota(MessageParcel & data,MessageParcel & reply)376 int32_t StorageDaemonStub::HandleSetBundleQuota(MessageParcel &data, MessageParcel &reply)
377 {
378     std::string bundleName = data.ReadString();
379     int32_t uid = data.ReadInt32();
380     std::string bundleDataDirPath = data.ReadString();
381     int32_t limitSizeMb = data.ReadInt32();
382     int err = SetBundleQuota(bundleName, uid, bundleDataDirPath, limitSizeMb);
383     if (!reply.WriteInt32(err)) {
384         return E_WRITE_REPLY_ERR;
385     }
386     return E_OK;
387 }
388 
389 } // StorageDaemon
390 } // OHOS
391