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