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 "simple_timer_info.h"
17 #include "time_common.h"
18 #include "time_xcollie.h"
19 #include "time_service_stub.h"
20 #include "ntp_update_time.h"
21 #include "ntp_trusted_time.h"
22 
23 namespace OHOS {
24 namespace MiscServices {
25 using namespace OHOS::HiviewDFX;
26 namespace {
27 static const uint32_t MAX_EXEMPTION_SIZE = 1000;
28 static const int MAX_PID_LIST_SIZE = 1024;
29 }
30 
31 
TimeServiceStub()32 TimeServiceStub::TimeServiceStub()
33 {
34     memberFuncMap_ = {
35         { TimeServiceIpcInterfaceCode::SET_TIME,
36             [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnSetTime(data, reply); } },
37         { TimeServiceIpcInterfaceCode::SET_TIME_ZONE,
38             [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnSetTimeZone(data, reply); } },
39         { TimeServiceIpcInterfaceCode::GET_TIME_ZONE,
40             [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetTimeZone(data, reply); } },
41         { TimeServiceIpcInterfaceCode::GET_THREAD_TIME_MILLI,
42             [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetThreadTimeMs(data, reply); } },
43         { TimeServiceIpcInterfaceCode::GET_THREAD_TIME_NANO,
44             [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetThreadTimeNs(data, reply); } },
45         { TimeServiceIpcInterfaceCode::CREATE_TIMER,
46             [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnCreateTimer(data, reply); } },
47         { TimeServiceIpcInterfaceCode::START_TIMER,
48             [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnStartTimer(data, reply); } },
49         { TimeServiceIpcInterfaceCode::STOP_TIMER,
50             [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnStopTimer(data, reply); } },
51         { TimeServiceIpcInterfaceCode::DESTROY_TIMER,
52             [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnDestroyTimer(data, reply); } },
53         { TimeServiceIpcInterfaceCode::PROXY_TIMER,
54             [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnTimerProxy(data, reply); } },
55         { TimeServiceIpcInterfaceCode::PID_PROXY_TIMER,
56             [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnPidTimerProxy(data, reply); } },
57         { TimeServiceIpcInterfaceCode::ADJUST_TIMER,
58             [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnAdjustTimer(data, reply); } },
59         { TimeServiceIpcInterfaceCode::SET_TIMER_EXEMPTION,
60             [this] (MessageParcel &data, MessageParcel &reply) -> int32_t {
61                 return OnSetTimerExemption(data, reply); } },
62         { TimeServiceIpcInterfaceCode::RESET_ALL_PROXY,
63             [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnAllProxyReset(data, reply); } },
64         { TimeServiceIpcInterfaceCode::GET_NTP_TIME_MILLI,
65             [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetNtpTimeMs(data, reply); } },
66         { TimeServiceIpcInterfaceCode::GET_REAL_TIME_MILLI,
67             [this] (MessageParcel &data, MessageParcel &reply) -> int32_t { return OnGetRealTimeMs(data, reply); } },
68     };
69 }
70 
~TimeServiceStub()71 TimeServiceStub::~TimeServiceStub()
72 {
73     memberFuncMap_.clear();
74 }
75 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)76 int32_t TimeServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
77     MessageOption &option)
78 {
79     TIME_HILOGD(TIME_MODULE_SERVICE, " start##code = %{public}u", code);
80     std::u16string descriptor = TimeServiceStub::GetDescriptor();
81     std::u16string remoteDescriptor = data.ReadInterfaceToken();
82     if (descriptor != remoteDescriptor) {
83         TIME_HILOGE(TIME_MODULE_SERVICE, " end##descriptor checked fail");
84         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
85     }
86     pid_t p = IPCSkeleton::GetCallingPid();
87     pid_t p1 = IPCSkeleton::GetCallingUid();
88     TIME_HILOGD(TIME_MODULE_SERVICE, "CallingPid = %{public}d, CallingUid = %{public}d, code = %{public}u", p, p1,
89                 code);
90     if (code >= static_cast<uint32_t>(TimeServiceIpcInterfaceCode::SET_TIME) &&
91         code <= static_cast<uint32_t>(TimeServiceIpcInterfaceCode::GET_REAL_TIME_MILLI)) {
92         auto itFunc = memberFuncMap_.find(static_cast<TimeServiceIpcInterfaceCode>(code));
93         if (itFunc != memberFuncMap_.end()) {
94             auto memberFunc = itFunc->second;
95             if (memberFunc != nullptr) {
96                 return memberFunc(data, reply);
97             }
98         }
99     }
100     int ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
101     TIME_HILOGD(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
102     return ret;
103 }
104 
OnSetTime(MessageParcel & data,MessageParcel & reply)105 int32_t TimeServiceStub::OnSetTime(MessageParcel &data, MessageParcel &reply)
106 {
107     TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
108     TimeXCollie timeXCollie("TimeService::SetTime");
109     int64_t time = data.ReadInt64();
110     auto apiVersion = data.ReadInt8();
111     if (apiVersion == APIVersion::API_VERSION_9) {
112         if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
113             TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
114             return E_TIME_NOT_SYSTEM_APP;
115         }
116     }
117     if (!TimePermission::CheckCallingPermission(TimePermission::setTime)) {
118         TIME_HILOGE(TIME_MODULE_SERVICE, "permission check setTime failed");
119         return E_TIME_NO_PERMISSION;
120     }
121     int32_t ret = SetTime(time);
122     TIME_HILOGD(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
123     return ret;
124 }
125 
OnSetTimeZone(MessageParcel & data,MessageParcel & reply)126 int32_t TimeServiceStub::OnSetTimeZone(MessageParcel &data, MessageParcel &reply)
127 {
128     TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
129     TimeXCollie timeXCollie("TimeService::SetTimeZone");
130     std::string timeZoneId = data.ReadString();
131     auto apiVersion = data.ReadInt8();
132     if (apiVersion == APIVersion::API_VERSION_9) {
133         if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
134             TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
135             return E_TIME_NOT_SYSTEM_APP;
136         }
137     }
138     if (!TimePermission::CheckCallingPermission(TimePermission::setTimeZone)) {
139         TIME_HILOGE(TIME_MODULE_SERVICE, "permission check setTime failed");
140         return E_TIME_NO_PERMISSION;
141     }
142     int32_t ret = SetTimeZone(timeZoneId);
143     TIME_HILOGD(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
144     return ret;
145 }
146 
OnGetTimeZone(MessageParcel & data,MessageParcel & reply)147 int32_t TimeServiceStub::OnGetTimeZone(MessageParcel &data, MessageParcel &reply)
148 {
149     TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
150     TimeXCollie timeXCollie("TimeService::GetTimeZone");
151     std::string timeZoneId;
152     int32_t ret = GetTimeZone(timeZoneId);
153     if (ret != ERR_OK) {
154         TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
155         return ret;
156     }
157     reply.WriteString(timeZoneId);
158     TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
159     return ret;
160 }
161 
OnGetThreadTimeMs(MessageParcel & data,MessageParcel & reply)162 int32_t TimeServiceStub::OnGetThreadTimeMs(MessageParcel &data, MessageParcel &reply)
163 {
164     TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
165     TimeXCollie timeXCollie("TimeService::GetThreadTimeMs");
166     int64_t time;
167     int32_t ret = GetThreadTimeMs(time);
168     if (ret != ERR_OK) {
169         TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
170         return ret;
171     }
172     reply.WriteInt64(time);
173     TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
174     return ret;
175 }
176 
OnGetThreadTimeNs(MessageParcel & data,MessageParcel & reply)177 int32_t TimeServiceStub::OnGetThreadTimeNs(MessageParcel &data, MessageParcel &reply)
178 {
179     TIME_HILOGD(TIME_MODULE_SERVICE, " start.");
180     TimeXCollie timeXCollie("TimeService::GetThreadTimeNs");
181     int64_t time;
182     int32_t ret = GetThreadTimeNs(time);
183     if (ret != ERR_OK) {
184         TIME_HILOGE(TIME_MODULE_SERVICE, " end##ret = %{public}d", ret);
185         return ret;
186     }
187     reply.WriteInt64(time);
188     TIME_HILOGD(TIME_MODULE_SERVICE, " end.");
189     return ret;
190 }
191 
OnCreateTimer(MessageParcel & data,MessageParcel & reply)192 int32_t TimeServiceStub::OnCreateTimer(MessageParcel &data, MessageParcel &reply)
193 {
194     TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
195     TimeXCollie timeXCollie("TimeService::CreateTimer");
196     if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
197         TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
198         return E_TIME_NOT_SYSTEM_APP;
199     }
200     std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent{ nullptr };
201     auto type = data.ReadInt32();
202     auto repeat = data.ReadBool();
203     auto disposable = data.ReadBool();
204     auto interval = data.ReadUint64();
205     if (data.ReadBool()) {
206         wantAgent = std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>(
207             data.ReadParcelable<OHOS::AbilityRuntime::WantAgent::WantAgent>());
208         if (!wantAgent) {
209             TIME_HILOGE(TIME_MODULE_SERVICE, "Input wantagent nullptr");
210             return E_TIME_NULLPTR;
211         }
212     }
213     sptr<IRemoteObject> obj = data.ReadRemoteObject();
214     if (obj == nullptr) {
215         TIME_HILOGE(TIME_MODULE_SERVICE, "Input nullptr");
216         return E_TIME_NULLPTR;
217     }
218     auto timerOptions = std::make_shared<SimpleTimerInfo>();
219     timerOptions->type = type;
220     timerOptions->repeat = repeat;
221     timerOptions->interval = interval;
222     timerOptions->disposable = disposable;
223     timerOptions->wantAgent = wantAgent;
224     uint64_t timerId = data.ReadUint64();
225     auto errCode = CreateTimer(timerOptions, obj, timerId);
226     if (errCode != E_TIME_OK) {
227         TIME_HILOGE(TIME_MODULE_SERVICE, "Create timer failed");
228         return E_TIME_DEAL_FAILED;
229     }
230     if (!reply.WriteUint64(timerId)) {
231         TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to write timerId");
232         return E_TIME_WRITE_PARCEL_ERROR;
233     }
234     TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
235     return ERR_OK;
236 }
237 
OnStartTimer(MessageParcel & data,MessageParcel & reply)238 int32_t TimeServiceStub::OnStartTimer(MessageParcel &data, MessageParcel &reply)
239 {
240     TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
241     TimeXCollie timeXCollie("TimeService::StartTimer");
242     if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
243         TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
244         return E_TIME_NOT_SYSTEM_APP;
245     }
246     auto timerId = data.ReadUint64();
247     auto triggerTime = data.ReadUint64();
248     if (StartTimer(timerId, triggerTime) != E_TIME_OK) {
249         TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to start timer");
250         return E_TIME_DEAL_FAILED;
251     }
252     TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
253     return ERR_OK;
254 }
255 
OnStopTimer(MessageParcel & data,MessageParcel & reply)256 int32_t TimeServiceStub::OnStopTimer(MessageParcel &data, MessageParcel &reply)
257 {
258     TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
259     TimeXCollie timeXCollie("TimeService::StopTimer");
260     if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
261         TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
262         return E_TIME_NOT_SYSTEM_APP;
263     }
264     auto timerId = data.ReadUint64();
265     if (StopTimer(timerId) != E_TIME_OK) {
266         TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to stop timer");
267         return E_TIME_DEAL_FAILED;
268     }
269     TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
270     return ERR_OK;
271 }
272 
OnDestroyTimer(MessageParcel & data,MessageParcel & reply)273 int32_t TimeServiceStub::OnDestroyTimer(MessageParcel &data, MessageParcel &reply)
274 {
275     TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
276     TimeXCollie timeXCollie("TimeService::DestroyTimer");
277     if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
278         TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
279         return E_TIME_NOT_SYSTEM_APP;
280     }
281     auto timerId = data.ReadUint64();
282     if (DestroyTimer(timerId, false) != E_TIME_OK) {
283         TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to destory timer");
284         return E_TIME_DEAL_FAILED;
285     }
286     TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
287     return ERR_OK;
288 }
289 
OnTimerProxy(MessageParcel & data,MessageParcel & reply)290 int32_t TimeServiceStub::OnTimerProxy(MessageParcel &data, MessageParcel &reply)
291 {
292     TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
293     TimeXCollie timeXCollie("TimeService::TimerProxy");
294     auto uid = data.ReadInt32();
295     if (uid == 0) {
296         TIME_HILOGE(TIME_MODULE_SERVICE, "Error param uid.");
297         return E_TIME_READ_PARCEL_ERROR;
298     }
299     auto isProxy = data.ReadBool();
300     auto needRetrigger = data.ReadBool();
301     if (!ProxyTimer(uid, isProxy, needRetrigger)) {
302         return E_TIME_DEAL_FAILED;
303     }
304     TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
305     return ERR_OK;
306 }
307 
OnPidTimerProxy(MessageParcel & data,MessageParcel & reply)308 int32_t TimeServiceStub::OnPidTimerProxy(MessageParcel &data, MessageParcel &reply)
309 {
310     TimeXCollie timeXCollie("TimeService::PidTimerProxy");
311     auto uid = data.ReadInt32();
312     auto pidListSize = data.ReadInt32();
313     std::set<int> pidList;
314     if (pidListSize == 0 || pidListSize > MAX_PID_LIST_SIZE) {
315         TIME_HILOGE(TIME_MODULE_SERVICE, "Error pid list size.");
316         return E_TIME_READ_PARCEL_ERROR;
317     }
318     for (int i = 0; i < pidListSize; i++) {
319         auto pid = data.ReadInt32();
320         if (pid != 0) {
321             pidList.insert(pid);
322         }
323     }
324     if (pidList.size() == 0) {
325         TIME_HILOGE(TIME_MODULE_SERVICE, "Error pidList.");
326         return E_TIME_READ_PARCEL_ERROR;
327     }
328     auto isProxy = data.ReadBool();
329     auto needRetrigger = data.ReadBool();
330     if (!ProxyTimer(uid, pidList, isProxy, needRetrigger)) {
331         return E_TIME_DEAL_FAILED;
332     }
333     return ERR_OK;
334 }
335 
OnAdjustTimer(MessageParcel & data,MessageParcel & reply)336 int32_t TimeServiceStub::OnAdjustTimer(MessageParcel &data, MessageParcel &reply)
337 {
338     TIME_HILOGD(TIME_MODULE_SERVICE, "on timer adjust start.");
339     TimeXCollie timeXCollie("TimeService::AdjustTimer");
340     if (!TimePermission::CheckProxyCallingPermission()) {
341         TIME_HILOGE(TIME_MODULE_SERVICE, "Adjust Timer permission check failed");
342         return E_TIME_NO_PERMISSION;
343     }
344     bool isAdjust = false;
345     uint32_t interval = 0;
346     if (!data.ReadBool(isAdjust)) {
347         return E_TIME_READ_PARCEL_ERROR;
348     }
349     if (!data.ReadUint32(interval)) {
350         return E_TIME_READ_PARCEL_ERROR;
351     }
352     if (isAdjust && interval == 0) {
353         TIME_HILOGE(TIME_MODULE_SERVICE, "invalid parameter: interval");
354         return E_TIME_READ_PARCEL_ERROR;
355     }
356     if (AdjustTimer(isAdjust, interval) != E_TIME_OK) {
357         TIME_HILOGE(TIME_MODULE_SERVICE, "Error adjust timer.");
358         return E_TIME_DEAL_FAILED;
359     }
360     TIME_HILOGD(TIME_MODULE_SERVICE, "on timer adjust end.");
361     return ERR_OK;
362 }
363 
OnSetTimerExemption(MessageParcel & data,MessageParcel & reply)364 int32_t TimeServiceStub::OnSetTimerExemption(MessageParcel &data, MessageParcel &reply)
365 {
366     TIME_HILOGD(TIME_MODULE_SERVICE, "set timer exemption start.");
367     TimeXCollie timeXCollie("TimeService::SetTimerExemption");
368     if (!TimePermission::CheckProxyCallingPermission()) {
369         TIME_HILOGE(TIME_MODULE_SERVICE, "Set Timer Exemption permission check failed");
370         return E_TIME_NO_PERMISSION;
371     }
372     std::unordered_set<std::string> nameArr;
373     uint32_t size;
374     bool isExemption;
375     if (!data.ReadUint32(size)) {
376         return E_TIME_READ_PARCEL_ERROR;
377     }
378     if (size > MAX_EXEMPTION_SIZE) {
379         return E_TIME_PARAMETERS_INVALID;
380     }
381     for (uint32_t i = 0; i < size; ++i) {
382         std::string name;
383         if (!data.ReadString(name)) {
384             return E_TIME_READ_PARCEL_ERROR;
385         }
386         nameArr.insert(name);
387     }
388 
389     if (!data.ReadBool(isExemption)) {
390         return E_TIME_READ_PARCEL_ERROR;
391     }
392     SetTimerExemption(nameArr, isExemption);
393     TIME_HILOGD(TIME_MODULE_SERVICE, "set timer exemption end.");
394     return ERR_OK;
395 }
396 
OnAllProxyReset(MessageParcel & data,MessageParcel & reply)397 int32_t TimeServiceStub::OnAllProxyReset(MessageParcel &data, MessageParcel &reply)
398 {
399     TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
400     TimeXCollie timeXCollie("TimeService::AllProxyReset");
401     if (!ResetAllProxy()) {
402         return E_TIME_DEAL_FAILED;
403     }
404     TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
405     return ERR_OK;
406 }
407 
OnGetNtpTimeMs(MessageParcel & data,MessageParcel & reply)408 int32_t TimeServiceStub::OnGetNtpTimeMs(MessageParcel &data, MessageParcel &reply)
409 {
410     TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
411     if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
412         TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
413         return E_TIME_NOT_SYSTEM_APP;
414     }
415     int64_t time = 0;
416     auto ret = GetNtpTimeMs(time);
417     if (ret != E_TIME_OK) {
418         return ret;
419     }
420     if (!reply.WriteUint64(time)) {
421         TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to write NTP time");
422         return E_TIME_WRITE_PARCEL_ERROR;
423     }
424     TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
425     return ERR_OK;
426 }
427 
OnGetRealTimeMs(MessageParcel & data,MessageParcel & reply)428 int32_t TimeServiceStub::OnGetRealTimeMs(MessageParcel &data, MessageParcel &reply)
429 {
430     TIME_HILOGD(TIME_MODULE_SERVICE, "start.");
431     if (!TimePermission::CheckSystemUidCallingPermission(IPCSkeleton::GetCallingFullTokenID())) {
432         TIME_HILOGE(TIME_MODULE_SERVICE, "not system applications");
433         return E_TIME_NOT_SYSTEM_APP;
434     }
435     int64_t time = 0;
436     auto ret = GetRealTimeMs(time);
437     if (ret != E_TIME_OK) {
438         return ret;
439     }
440     if (!reply.WriteUint64(time)) {
441         TIME_HILOGE(TIME_MODULE_SERVICE, "Failed to write NTP time");
442         return E_TIME_WRITE_PARCEL_ERROR;
443     }
444     TIME_HILOGD(TIME_MODULE_SERVICE, "end.");
445     return ERR_OK;
446 }
447 } // namespace MiscServices
448 } // namespace OHOS