1 /*
2  * Copyright (c) 2022-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 #include "account_hisysevent_adapter.h"
16 #include "account_log_wrapper.h"
17 #ifdef HAS_HISYSEVENT_PART
18 #include "hisysevent.h"
19 #endif // HAS_HISYSEVENT_PART
20 
21 namespace OHOS {
22 namespace AccountSA {
23 namespace {
24 #ifdef HAS_HISYSEVENT_PART
25 using namespace OHOS::HiviewDFX;
26 #endif // HAS_HISYSEVENT_PART
27 }
28 
ReportServiceStartFail(int32_t errCode,const std::string & errMsg)29 void ReportServiceStartFail(int32_t errCode, const std::string& errMsg)
30 {
31 #ifdef HAS_HISYSEVENT_PART
32     int ret = HiSysEventWrite(HiSysEvent::Domain::ACCOUNT, "SERVICE_START_FAILED",
33         HiSysEvent::EventType::FAULT,
34         "ERROR_TYPE", errCode,
35         "ERROR_MSG", errMsg);
36     if (ret != 0) {
37         ACCOUNT_LOGE("hisysevent write failed! ret %{public}d. errCode %{public}d", ret, errCode);
38     }
39 #else // HAS_HISYSEVENT_PART
40     (void)errCode;
41 #endif // HAS_HISYSEVENT_PART
42 }
43 
ReportPermissionFail(int32_t callerUid,int32_t callerPid,const std::string & permName)44 void ReportPermissionFail(int32_t callerUid, int32_t callerPid, const std::string& permName)
45 {
46 #ifdef HAS_HISYSEVENT_PART
47     int ret = HiSysEventWrite(HiSysEvent::Domain::ACCOUNT, "PERMISSION_EXCEPTION",
48         HiSysEvent::EventType::SECURITY,
49         "CALLER_UID", callerUid,
50         "CALLER_PID", callerPid,
51         "PERMISSION_NAME", permName);
52     if (ret != 0) {
53         ACCOUNT_LOGE("hisysevent write failed! ret %{public}d. uid %{public}d, pid %{public}d permName %{public}s.",
54             ret, callerUid, callerPid, permName.c_str());
55     }
56 #else // HAS_HISYSEVENT_PART
57     (void)callerUid;
58     (void)callerPid;
59     (void)permName;
60 #endif // HAS_HISYSEVENT_PART
61 }
62 
ReportOsAccountOperationFail(int32_t id,const std::string & operationStr,int32_t errCode,const std::string & errMsg)63 void ReportOsAccountOperationFail(
64     int32_t id, const std::string& operationStr, int32_t errCode, const std::string& errMsg)
65 {
66 #ifdef HAS_HISYSEVENT_PART
67     int ret = HiSysEventWrite(HiSysEvent::Domain::ACCOUNT, "OS_ACCOUNT_FAILED",
68         HiSysEvent::EventType::FAULT,
69         "ID", id,
70         "OPERATE_TYPE", operationStr,
71         "ERROR_TYPE", errCode,
72         "ERROR_MSG", errMsg);
73     if (ret != 0) {
74         ACCOUNT_LOGE("ret %{public}d, id %{public}d, opStr %{public}s, errCode %{public}d errMsg %{public}s.",
75             ret, id, operationStr.c_str(), errCode, errMsg.c_str());
76     }
77 #else // HAS_HISYSEVENT_PART
78     (void)id;
79     (void)errCode;
80     (void)operationStr;
81     (void)errMsg;
82 #endif // HAS_HISYSEVENT_PART
83 }
84 
ReportOhosAccountOperationFail(int32_t userId,const std::string & operationStr,int32_t errCode,const std::string & errMsg)85 void ReportOhosAccountOperationFail(
86     int32_t userId, const std::string& operationStr, int32_t errCode, const std::string& errMsg)
87 {
88 #ifdef HAS_HISYSEVENT_PART
89     int ret = HiSysEventWrite(HiSysEvent::Domain::ACCOUNT, "DISTRIBUTED_ACCOUNT_FAILED",
90         HiSysEvent::EventType::FAULT,
91         "USER_ID", userId,
92         "OPERATE_TYPE", operationStr,
93         "ERROR_TYPE", errCode,
94         "ERROR_MSG", errMsg);
95     if (ret != 0) {
96         ACCOUNT_LOGE("ret %{public}d, userId %{public}d, opStr %{public}s, errCode %{public}d errMsg %{public}s.",
97             ret, userId, operationStr.c_str(), errCode, errMsg.c_str());
98     }
99 #else // HAS_HISYSEVENT_PART
100     (void)userId;
101     (void)operationStr;
102     (void)errCode;
103     (void)errMsg;
104 #endif // HAS_HISYSEVENT_PART
105 }
106 
ReportAppAccountOperationFail(const std::string & name,const std::string & owner,const std::string & operationStr,int32_t errCode,const std::string & errMsg)107 void ReportAppAccountOperationFail(const std::string &name, const std::string &owner, const std::string& operationStr,
108     int32_t errCode, const std::string& errMsg)
109 {
110 #ifdef HAS_HISYSEVENT_PART
111     int ret = HiSysEventWrite(HiSysEvent::Domain::ACCOUNT, "APP_ACCOUNT_FAILED",
112         HiSysEvent::EventType::FAULT,
113         "NAME", name,
114         "OWNER", owner,
115         "OPERATE_TYPE", operationStr,
116         "ERROR_TYPE", errCode,
117         "ERROR_MSG", errMsg);
118     if (ret != 0) {
119         ACCOUNT_LOGE(
120             "ret %{public}d, name %{public}s, owner %{public}s, opStr %{public}s, "
121             "errCode %{public}d, errMsg %{public}s.",
122             ret, name.c_str(), owner.c_str(), operationStr.c_str(), errCode, errMsg.c_str());
123     }
124 #else // HAS_HISYSEVENT_PART
125     (void)name;
126     (void)owner;
127     (void)errCode;
128     (void)operationStr;
129     (void)errMsg;
130 #endif // HAS_HISYSEVENT_PART
131 }
132 
ReportOsAccountLifeCycle(int32_t id,const std::string & operationStr)133 void ReportOsAccountLifeCycle(int32_t id, const std::string& operationStr)
134 {
135 #ifdef HAS_HISYSEVENT_PART
136     int ret = HiSysEventWrite(HiSysEvent::Domain::ACCOUNT, "OS_ACCOUNT_LIFE_CYCLE",
137         HiSysEvent::EventType::BEHAVIOR,
138         "ID", id,
139         "OPERATE_TYPE", operationStr);
140     if (ret != 0) {
141         ACCOUNT_LOGE("ret %{public}d, operationStr %{public}s, id %{public}d.",
142             ret, operationStr.c_str(), id);
143     }
144 #else // HAS_HISYSEVENT_PART
145     (void)id;
146     (void)operationStr;
147 #endif // HAS_HISYSEVENT_PART
148 }
149 
ReportOsAccountSwitch(int32_t currentId,int32_t oldId)150 void ReportOsAccountSwitch(int32_t currentId, int32_t oldId)
151 {
152 #ifdef HAS_HISYSEVENT_PART
153     int ret = HiSysEventWrite(HiSysEvent::Domain::ACCOUNT, "OS_ACCOUNT_SWITCH",
154         HiSysEvent::EventType::BEHAVIOR,
155         "CURRENT_ID", currentId,
156         "OLD_ID", oldId);
157     if (ret != 0) {
158         ACCOUNT_LOGE("ret %{public}d, currentId %{public}d, oldId %{public}d.",
159             ret, currentId, oldId);
160     }
161 #else // HAS_HISYSEVENT_PART
162     (void)currentId;
163     (void)oldId;
164 #endif // HAS_HISYSEVENT_PART
165 }
166 
ReportOhosAccountStateChange(int32_t userId,int32_t operateType,int32_t oldStat,int32_t newStat)167 void ReportOhosAccountStateChange(int32_t userId, int32_t operateType, int32_t oldStat, int32_t newStat)
168 {
169 #ifdef HAS_HISYSEVENT_PART
170     int ret = HiSysEventWrite(HiSysEvent::Domain::ACCOUNT, "DISTRIBUTED_ACCOUNT_CHANGE",
171         HiSysEvent::EventType::BEHAVIOR,
172         "USER_ID", userId,
173         "OPERATION_TYPE", operateType,
174         "OLD_STATE", oldStat,
175         "NEW_STATE", newStat);
176     if (ret != 0) {
177         ACCOUNT_LOGE("ret %{public}d, [%{public}d, %{public}d, %{public}d, %{public}d]",
178             ret, userId, operateType, oldStat, newStat);
179     }
180 #else // HAS_HISYSEVENT_PART
181     (void)userId;
182     (void)operateType;
183     (void)oldStat;
184     (void)newStat;
185 #endif // HAS_HISYSEVENT_PART
186 }
187 
ReportOsAccountDataTampered(int32_t id,const std::string & dataPath,const std::string & dataLabel)188 void ReportOsAccountDataTampered(int32_t id, const std::string& dataPath, const std::string& dataLabel)
189 {
190 #ifdef HAS_HISYSEVENT_PART
191     int ret = HiSysEventWrite(HiSysEvent::Domain::ACCOUNT, "DATA_TAMPERED",
192         HiSysEvent::EventType::SECURITY,
193         "ID", id,
194         "DATA_PATH", dataPath,
195         "DATA_LABEL", dataLabel);
196     if (ret != 0) {
197         ACCOUNT_LOGE("sysevent write failed, ret %{public}d, id %{public}d, dataPath %{public}s, dataLabel %{public}s.",
198             ret, id, dataPath.c_str(), dataLabel.c_str());
199     }
200 #else // HAS_HISYSEVENT_PART
201     (void)id;
202     (void)dataPath;
203     (void)dataLabel;
204 #endif // HAS_HISYSEVENT_PART
205 }
206 } // AccountSA
207 } // OHOS