1 /*
2  * Copyright (c) 2021-2023 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 "account_proxy.h"
17 #include <ipc_types.h>
18 #include <string_ex.h>
19 #include "account_error_no.h"
20 #include "account_info_parcel.h"
21 #include "account_log_wrapper.h"
22 
23 namespace OHOS {
24 namespace AccountSA {
25 const size_t INTERCEPT_HEAD_PART_LEN_FOR_NAME = 1;
26 const std::string DEFAULT_ANON_STR = "**********";
27 
AnonymizeNameStr(const std::string & nameStr)28 static std::string AnonymizeNameStr(const std::string& nameStr)
29 {
30     if (nameStr.empty()) {
31         return nameStr;
32     }
33     return nameStr.substr(0, INTERCEPT_HEAD_PART_LEN_FOR_NAME) + DEFAULT_ANON_STR;
34 }
35 
SendRequest(AccountMgrInterfaceCode code,MessageParcel & data,MessageParcel & reply)36 ErrCode AccountProxy::SendRequest(AccountMgrInterfaceCode code, MessageParcel &data, MessageParcel &reply)
37 {
38     sptr<IRemoteObject> remote = Remote();
39     if (remote == nullptr) {
40         ACCOUNT_LOGE("remote is nullptr, code = %{public}d", code);
41         return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
42     }
43     MessageOption option(MessageOption::TF_SYNC);
44     int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
45     if (result != ERR_OK) {
46         ACCOUNT_LOGE("failed to send account request, code = %{public}d, result = %{public}d", code, result);
47     }
48     return result;
49 }
50 
UpdateOhosAccountInfo(const std::string & accountName,const std::string & uid,const std::string & eventStr)51 bool AccountProxy::UpdateOhosAccountInfo(
52     const std::string &accountName, const std::string &uid, const std::string &eventStr)
53 {
54     MessageParcel data;
55     if (!data.WriteInterfaceToken(GetDescriptor())) {
56         ACCOUNT_LOGE("Write descriptor failed!");
57         return false;
58     }
59     if (!data.WriteString16(Str8ToStr16(accountName))) {
60         ACCOUNT_LOGE("Write accountName failed!");
61         return false;
62     }
63     if (!data.WriteString16(Str8ToStr16(uid))) {
64         ACCOUNT_LOGE("Write uid failed!");
65         return false;
66     }
67     if (!data.WriteString16(Str8ToStr16(eventStr))) {
68         ACCOUNT_LOGE("Write eventStr failed!");
69         return false;
70     }
71     MessageParcel reply;
72     auto ret = SendRequest(AccountMgrInterfaceCode::UPDATE_OHOS_ACCOUNT_INFO, data, reply);
73     if (ret != ERR_NONE) {
74         return false;
75     }
76 
77     std::int32_t result = ERR_OK;
78     if (!reply.ReadInt32(result)) {
79         ACCOUNT_LOGE("reply ReadInt32 failed");
80         return false;
81     }
82 
83     if (result != ERR_OK) {
84         ACCOUNT_LOGE("UpdateOhosAccountInfo failed: %{public}d", result);
85         return false;
86     }
87 
88     return true;
89 }
90 
SetOhosAccountInfo(const OhosAccountInfo & ohosAccountInfo,const std::string & eventStr)91 std::int32_t AccountProxy::SetOhosAccountInfo(const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr)
92 {
93     MessageParcel data;
94     if (!data.WriteInterfaceToken(GetDescriptor())) {
95         ACCOUNT_LOGE("Write descriptor failed!");
96         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
97     }
98     if (!WriteOhosAccountInfo(data, ohosAccountInfo)) {
99         ACCOUNT_LOGE("Write ohosAccountInfo failed!");
100         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
101     }
102     if (!data.WriteString16(Str8ToStr16(eventStr))) {
103         ACCOUNT_LOGE("Write eventStr failed!");
104         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
105     }
106     MessageParcel reply;
107     auto ret = SendRequest(AccountMgrInterfaceCode::SET_OHOS_ACCOUNT_INFO, data, reply);
108     if (ret != ERR_NONE) {
109         return ret;
110     }
111 
112     std::int32_t result = ERR_OK;
113     if (!reply.ReadInt32(result)) {
114         ACCOUNT_LOGE("reply ReadInt32 failed");
115         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
116     }
117 
118     if (result != ERR_OK) {
119         ACCOUNT_LOGE("SetOhosAccountInfo failed: %{public}d", result);
120     }
121 
122     return result;
123 }
124 
SetOhosAccountInfoByUserId(const int32_t userId,const OhosAccountInfo & ohosAccountInfo,const std::string & eventStr)125 ErrCode AccountProxy::SetOhosAccountInfoByUserId(
126     const int32_t userId, const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr)
127 {
128     MessageParcel data;
129     if (!data.WriteInterfaceToken(GetDescriptor())) {
130         ACCOUNT_LOGE("Write descriptor failed!");
131         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
132     }
133     if (!data.WriteInt32(userId)) {
134         ACCOUNT_LOGE("failed to write userId failed");
135         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
136     }
137     if (!WriteOhosAccountInfo(data, ohosAccountInfo)) {
138         ACCOUNT_LOGE("Write ohosAccountInfo failed!");
139         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
140     }
141     if (!data.WriteString16(Str8ToStr16(eventStr))) {
142         ACCOUNT_LOGE("Write eventStr failed!");
143         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
144     }
145     MessageParcel reply;
146     auto ret = SendRequest(AccountMgrInterfaceCode::SET_OHOS_ACCOUNT_INFO_BY_USER_ID, data, reply);
147     if (ret != ERR_NONE) {
148         return ret;
149     }
150 
151     std::int32_t result = ERR_OK;
152     if (!reply.ReadInt32(result)) {
153         ACCOUNT_LOGE("reply ReadInt32 failed");
154         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
155     }
156     return result;
157 }
158 
QueryDistributedVirtualDeviceId(std::string & dvid)159 ErrCode AccountProxy::QueryDistributedVirtualDeviceId(std::string &dvid)
160 {
161     dvid = "";
162     MessageParcel data;
163 
164     if (!data.WriteInterfaceToken(GetDescriptor())) {
165         ACCOUNT_LOGE("Failed to write descriptor!");
166         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
167     }
168 
169     MessageParcel reply;
170     ErrCode code = SendRequest(AccountMgrInterfaceCode::QUERY_DISTRIBUTE_VIRTUAL_DEVICE_ID, data, reply);
171     if (code != ERR_OK) {
172         ACCOUNT_LOGE("Failed to send request, code %{public}d.", code);
173         return code;
174     }
175     int32_t result = ERR_OK;
176     if (!reply.ReadInt32(result)) {
177         ACCOUNT_LOGE("Failed to read result");
178         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
179     }
180     if (result != ERR_OK) {
181         ACCOUNT_LOGE("Failed to query dvid, result %{public}d.", result);
182         return result;
183     }
184     if (!reply.ReadString(dvid)) {
185         ACCOUNT_LOGE("Failed to read dvid");
186         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
187     }
188     return ERR_OK;
189 }
190 
QueryOhosAccountInfo(OhosAccountInfo & accountInfo)191 ErrCode AccountProxy::QueryOhosAccountInfo(OhosAccountInfo &accountInfo)
192 {
193     MessageParcel data;
194     if (!data.WriteInterfaceToken(GetDescriptor())) {
195         ACCOUNT_LOGE("Write descriptor failed");
196         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
197     }
198     MessageParcel reply;
199     auto ret = SendRequest(AccountMgrInterfaceCode::QUERY_OHOS_ACCOUNT_INFO, data, reply);
200     if (ret != ERR_NONE) {
201         return ret;
202     }
203 
204     std::u16string name;
205     std::u16string uid;
206     std::int32_t status;
207     if ((!reply.ReadString16(name)) || (!reply.ReadString16(uid)) || (!reply.ReadInt32(status))) {
208         ACCOUNT_LOGE("failed to read from parcel");
209         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
210     }
211     accountInfo.name_ = Str16ToStr8(name);
212     accountInfo.uid_ = Str16ToStr8(uid);
213     accountInfo.status_ = status;
214     return ERR_OK;
215 }
216 
GetOhosAccountInfo(OhosAccountInfo & ohosAccountInfo)217 ErrCode AccountProxy::GetOhosAccountInfo(OhosAccountInfo &ohosAccountInfo)
218 {
219     MessageParcel data;
220     if (!data.WriteInterfaceToken(GetDescriptor())) {
221         ACCOUNT_LOGE("Write descriptor failed");
222         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
223     }
224     MessageParcel reply;
225     auto ret = SendRequest(AccountMgrInterfaceCode::GET_OHOS_ACCOUNT_INFO, data, reply);
226     if (ret != ERR_NONE) {
227         return ret;
228     }
229     ret = ReadOhosAccountInfo(reply, ohosAccountInfo);
230     if (ret != ERR_OK) {
231         return ret;
232     }
233     ACCOUNT_LOGI("Get ohos account %{public}s.", AnonymizeNameStr(ohosAccountInfo.nickname_).c_str());
234     return ERR_OK;
235 }
236 
GetOhosAccountInfoByUserId(int32_t userId,OhosAccountInfo & ohosAccountInfo)237 ErrCode AccountProxy::GetOhosAccountInfoByUserId(int32_t userId, OhosAccountInfo &ohosAccountInfo)
238 {
239     MessageParcel data;
240     if (!data.WriteInterfaceToken(GetDescriptor())) {
241         ACCOUNT_LOGE("Write descriptor failed");
242         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
243     }
244     if (!data.WriteInt32(userId)) {
245         ACCOUNT_LOGE("failed to write int for userId %{public}d.", userId);
246         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
247     }
248     MessageParcel reply;
249     auto ret = SendRequest(AccountMgrInterfaceCode::GET_OHOS_ACCOUNT_INFO_BY_USER_ID, data, reply);
250     if (ret != ERR_NONE) {
251         return ret;
252     }
253     ret = ReadOhosAccountInfo(reply, ohosAccountInfo);
254     if (ret != ERR_OK) {
255         return ret;
256     }
257     ACCOUNT_LOGI("Get ohos account %{public}s.", AnonymizeNameStr(ohosAccountInfo.nickname_).c_str());
258     return ERR_OK;
259 }
260 
QueryOhosAccountInfoByUserId(std::int32_t userId,OhosAccountInfo & accountInfo)261 ErrCode AccountProxy::QueryOhosAccountInfoByUserId(std::int32_t userId, OhosAccountInfo &accountInfo)
262 {
263     MessageParcel data;
264     if (!data.WriteInterfaceToken(GetDescriptor())) {
265         ACCOUNT_LOGE("Write descriptor failed");
266         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
267     }
268 
269     if (!data.WriteInt32(userId)) {
270         ACCOUNT_LOGE("failed to write int for userId %{public}d.", userId);
271         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
272     }
273     MessageParcel reply;
274     auto ret = SendRequest(AccountMgrInterfaceCode::QUERY_OHOS_ACCOUNT_INFO_BY_USER_ID, data, reply);
275     if (ret != ERR_NONE) {
276         return ret;
277     }
278 
279     std::u16string name;
280     std::u16string uid;
281     std::int32_t status;
282     if ((!reply.ReadString16(name)) || (!reply.ReadString16(uid)) || (!reply.ReadInt32(status))) {
283         ACCOUNT_LOGE("failed to read from parcel");
284         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
285     }
286     accountInfo.name_ = Str16ToStr8(name);
287     accountInfo.uid_ = Str16ToStr8(uid);
288     accountInfo.status_ = status;
289     return ERR_OK;
290 }
291 
QueryDeviceAccountId(std::int32_t & accountId)292 std::int32_t AccountProxy::QueryDeviceAccountId(std::int32_t &accountId)
293 {
294     MessageParcel data;
295     if (!data.WriteInterfaceToken(GetDescriptor())) {
296         ACCOUNT_LOGE("Write descriptor failed");
297         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
298     }
299     MessageParcel reply;
300     auto ret = SendRequest(AccountMgrInterfaceCode::QUERY_DEVICE_ACCOUNT_ID, data, reply);
301     if (ret != ERR_NONE) {
302         return ret;
303     }
304     accountId = reply.ReadInt32();
305     return ERR_OK;
306 }
307 
SubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,const sptr<IRemoteObject> & eventListener)308 ErrCode AccountProxy::SubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
309     const sptr<IRemoteObject> &eventListener)
310 {
311     MessageParcel data;
312     MessageParcel reply;
313 
314     if (!data.WriteInterfaceToken(GetDescriptor())) {
315         ACCOUNT_LOGE("Write descriptor failed.");
316         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
317     }
318 
319     if (!data.WriteInt32(static_cast<int32_t>(type))) {
320         ACCOUNT_LOGE("Write type failed.");
321         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
322     }
323 
324     if (!data.WriteRemoteObject(eventListener)) {
325         ACCOUNT_LOGE("Write remote object for eventListener failed.");
326         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
327     }
328 
329     ErrCode result = SendRequest(AccountMgrInterfaceCode::SUBSCRIBE_DISTRIBUTED_ACCOUNT_EVENT, data, reply);
330     if (result != ERR_OK) {
331         ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
332         return result;
333     }
334 
335     if (!reply.ReadInt32(result)) {
336         ACCOUNT_LOGE("Read reply failed.");
337         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
338     }
339     if (result != ERR_OK) {
340         ACCOUNT_LOGE("Subscribe distributed account event failed, result=%{public}d.", result);
341         return result;
342     }
343     return ERR_OK;
344 }
345 
UnsubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,const sptr<IRemoteObject> & eventListener)346 ErrCode AccountProxy::UnsubscribeDistributedAccountEvent(const DISTRIBUTED_ACCOUNT_SUBSCRIBE_TYPE type,
347     const sptr<IRemoteObject> &eventListener)
348 {
349     MessageParcel data;
350     MessageParcel reply;
351 
352     if (!data.WriteInterfaceToken(GetDescriptor())) {
353         ACCOUNT_LOGE("Write descriptor failed.");
354         return ERR_ACCOUNT_COMMON_WRITE_DESCRIPTOR_ERROR;
355     }
356 
357     if (!data.WriteInt32(static_cast<int32_t>(type))) {
358         ACCOUNT_LOGE("Write type failed.");
359         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
360     }
361 
362     if (!data.WriteRemoteObject(eventListener)) {
363         ACCOUNT_LOGE("Write remote object for eventListener failed.");
364         return ERR_ACCOUNT_COMMON_WRITE_PARCEL_ERROR;
365     }
366 
367     ErrCode result = SendRequest(AccountMgrInterfaceCode::UNSUBSCRIBE_DISTRIBUTED_ACCOUNT_EVENT, data, reply);
368     if (result != ERR_OK) {
369         ACCOUNT_LOGE("SendRequest failed, result=%{public}d.", result);
370         return result;
371     }
372 
373     if (!reply.ReadInt32(result)) {
374         ACCOUNT_LOGE("Read reply failed.");
375         return ERR_ACCOUNT_COMMON_READ_PARCEL_ERROR;
376     }
377     if (result != ERR_OK) {
378         ACCOUNT_LOGE("Unsubscribe distributed account failed, result=%{public}d.", result);
379     }
380 
381     return result;
382 }
383 
GetAppAccountService()384 sptr<IRemoteObject> AccountProxy::GetAppAccountService()
385 {
386     MessageParcel data;
387     if (!data.WriteInterfaceToken(GetDescriptor())) {
388         ACCOUNT_LOGE("Write descriptor failed");
389         return nullptr;
390     }
391     MessageParcel reply;
392     auto ret = SendRequest(AccountMgrInterfaceCode::GET_APP_ACCOUNT_SERVICE, data, reply);
393     if (ret != ERR_NONE) {
394         return nullptr;
395     }
396     return reply.ReadRemoteObject();
397 }
398 
GetOsAccountService()399 sptr<IRemoteObject> AccountProxy::GetOsAccountService()
400 {
401     MessageParcel data;
402     if (!data.WriteInterfaceToken(GetDescriptor())) {
403         ACCOUNT_LOGE("Write descriptor failed");
404         return nullptr;
405     }
406     MessageParcel reply;
407     auto ret = SendRequest(AccountMgrInterfaceCode::GET_OS_ACCOUNT_SERVICE, data, reply);
408     if (ret != ERR_NONE) {
409         return nullptr;
410     }
411 
412     return reply.ReadRemoteObject();
413 }
414 
GetAccountIAMService()415 sptr<IRemoteObject> AccountProxy::GetAccountIAMService()
416 {
417     MessageParcel data;
418     if (!data.WriteInterfaceToken(GetDescriptor())) {
419         ACCOUNT_LOGE("Write descriptor failed");
420         return nullptr;
421     }
422     MessageParcel reply;
423     auto ret = SendRequest(AccountMgrInterfaceCode::GET_ACCOUNT_IAM_SERVICE, data, reply);
424     if (ret != ERR_NONE) {
425         return nullptr;
426     }
427 
428     return reply.ReadRemoteObject();
429 }
430 
GetDomainAccountService()431 sptr<IRemoteObject> AccountProxy::GetDomainAccountService()
432 {
433     MessageParcel data;
434     if (!data.WriteInterfaceToken(GetDescriptor())) {
435         ACCOUNT_LOGE("Write descriptor failed");
436         return nullptr;
437     }
438     MessageParcel reply;
439     auto ret = SendRequest(AccountMgrInterfaceCode::GET_DOMAIN_ACCOUNT_SERVICE, data, reply);
440     if (ret != ERR_NONE) {
441         return nullptr;
442     }
443     return reply.ReadRemoteObject();
444 }
445 }  // namespace AccountSA
446 }  // namespace OHOS
447