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