• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "sys_installer_stub.h"
17 
18 #include <string_ex.h>
19 
20 #include "accesstoken_kit.h"
21 #include "hap_token_info.h"
22 #include "ipc_skeleton.h"
23 #include "log/log.h"
24 #include "securec.h"
25 #include "sys_installer_sa_ipc_interface_code.h"
26 #include "utils.h"
27 
28 namespace OHOS {
29 namespace SysInstaller {
30 using namespace std;
31 using namespace Updater;
32 using namespace std::placeholders;
33 
SysInstallerStub()34 SysInstallerStub::SysInstallerStub()
35 {
36     requestFuncMap_.emplace(SysInstallerInterfaceCode::SYS_INSTALLER_INIT,
37         bind(&SysInstallerStub::SysInstallerInitStub, this, _1, _2, _3, _4));
38     requestFuncMap_.emplace(SysInstallerInterfaceCode::UPDATE_PACKAGE,
39         bind(&SysInstallerStub::StartUpdatePackageZipStub, this, _1, _2, _3, _4));
40     requestFuncMap_.emplace(SysInstallerInterfaceCode::SET_UPDATE_CALLBACK,
41         bind(&SysInstallerStub::SetUpdateCallbackStub, this, _1, _2, _3, _4));
42     requestFuncMap_.emplace(SysInstallerInterfaceCode::GET_UPDATE_STATUS,
43         bind(&SysInstallerStub::GetUpdateStatusStub, this, _1, _2, _3, _4));
44     requestFuncMap_.emplace(SysInstallerInterfaceCode::UPDATE_PARA_PACKAGE,
45         bind(&SysInstallerStub::StartUpdateParaZipStub, this, _1, _2, _3, _4));
46     requestFuncMap_.emplace(SysInstallerInterfaceCode::DELETE_PARA_PACKAGE,
47         bind(&SysInstallerStub::StartDeleteParaZipStub, this, _1, _2, _3, _4));
48 }
49 
~SysInstallerStub()50 SysInstallerStub::~SysInstallerStub()
51 {
52     requestFuncMap_.clear();
53 }
54 
SysInstallerInitStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)55 int32_t SysInstallerStub::SysInstallerInitStub(SysInstallerStub *service,
56     MessageParcel &data, MessageParcel &reply, MessageOption &option)
57 {
58     if (service == nullptr) {
59         LOG(ERROR) << "Invalid param";
60         return -1;
61     }
62     int ret = service->SysInstallerInit();
63     reply.WriteInt32(ret);
64     return 0;
65 }
66 
StartUpdatePackageZipStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)67 int32_t SysInstallerStub::StartUpdatePackageZipStub(SysInstallerStub *service,
68     MessageParcel &data, MessageParcel &reply, MessageOption &option)
69 {
70     if (service == nullptr) {
71         LOG(ERROR) << "Invalid param";
72         return -1;
73     }
74     string pkgPath = Str16ToStr8(data.ReadString16());
75     int ret = service->StartUpdatePackageZip(pkgPath);
76     reply.WriteInt32(ret);
77     return 0;
78 }
79 
SetUpdateCallbackStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)80 int32_t SysInstallerStub::SetUpdateCallbackStub(SysInstallerStub *service,
81     MessageParcel &data, MessageParcel &reply, MessageOption &option)
82 {
83     if (service == nullptr) {
84         LOG(ERROR) << "Invalid param";
85         return -1;
86     }
87     auto remote = data.ReadRemoteObject();
88     if (remote == nullptr) {
89         LOG(ERROR) << "Failed to read remote obj";
90         return -1;
91     }
92     int ret = service->SetUpdateCallback(iface_cast<ISysInstallerCallback>(remote));
93     reply.WriteInt32(ret);
94     return 0;
95 }
96 
GetUpdateStatusStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)97 int32_t SysInstallerStub::GetUpdateStatusStub(SysInstallerStub *service,
98     MessageParcel &data, MessageParcel &reply, MessageOption &option)
99 {
100     if (service == nullptr) {
101         LOG(ERROR) << "Invalid param";
102         return -1;
103     }
104     int32_t ret = service->GetUpdateStatus();
105     reply.WriteInt32(ret);
106     return 0;
107 }
108 
StartUpdateParaZipStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)109 int32_t SysInstallerStub::StartUpdateParaZipStub(SysInstallerStub *service,
110     MessageParcel &data, MessageParcel &reply, MessageOption &option)
111 {
112     if (service == nullptr) {
113         LOG(ERROR) << "Invalid param";
114         return -1;
115     }
116     string pkgPath = Str16ToStr8(data.ReadString16());
117     string location = Str16ToStr8(data.ReadString16());
118     string cfgDir = Str16ToStr8(data.ReadString16());
119     LOG(INFO) << "StartUpdateParaZipStub path:" << pkgPath << " location:" << location << " cfgDir:" << cfgDir;
120 
121     int32_t ret = service->StartUpdateParaZip(pkgPath, location, cfgDir);
122     reply.WriteInt32(ret);
123     return 0;
124 }
125 
StartDeleteParaZipStub(SysInstallerStub * service,MessageParcel & data,MessageParcel & reply,MessageOption & option)126 int32_t SysInstallerStub::StartDeleteParaZipStub(SysInstallerStub *service,
127     MessageParcel &data, MessageParcel &reply, MessageOption &option)
128 {
129     if (service == nullptr) {
130         LOG(ERROR) << "Invalid param";
131         return -1;
132     }
133     string location = Str16ToStr8(data.ReadString16());
134     string cfgDir = Str16ToStr8(data.ReadString16());
135     LOG(INFO) << "StartDeleteParaZipStub location:" << location << " cfgDir:" << cfgDir;
136 
137     int32_t ret = service->StartDeleteParaZip(location, cfgDir);
138     reply.WriteInt32(ret);
139     return 0;
140 }
141 
IsPermissionGranted(void)142 bool SysInstallerStub::IsPermissionGranted(void)
143 {
144     Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
145     std::string permission = "ohos.permission.UPDATE_SYSTEM";
146 
147     int verifyResult = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, permission);
148     bool isPermissionGranted = (verifyResult == Security::AccessToken::PERMISSION_GRANTED);
149     if (!isPermissionGranted) {
150         LOG(ERROR) << "not granted " << permission.c_str();
151     }
152     return isPermissionGranted;
153 }
154 
CheckCallingPerm(void)155 bool SysInstallerStub::CheckCallingPerm(void)
156 {
157     int32_t callingUid = OHOS::IPCSkeleton::GetCallingUid();
158     LOG(INFO) << "CheckCallingPerm callingUid:" << callingUid;
159     if (callingUid == 0) {
160         return true;
161     }
162     return callingUid == Updater::Utils::USER_UPDATE_AUTHORITY && IsPermissionGranted();
163 }
164 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)165 int32_t SysInstallerStub::OnRemoteRequest(uint32_t code,
166     MessageParcel &data, MessageParcel &reply, MessageOption &option)
167 {
168     if (data.ReadInterfaceToken() != GetDescriptor()) {
169         LOG(ERROR) << "SysInstallerStub ReadInterfaceToken fail";
170         return -1;
171     }
172 
173     LOG(INFO) << "OnRemoteRequest func code " << code;
174     auto inter = requestFuncMap_.find(code);
175     if (inter != requestFuncMap_.end()) {
176         if (!CheckCallingPerm()) {
177             LOG(ERROR) << "SysInstallerStub CheckCallingPerm fail";
178             return -1;
179         }
180         return inter->second(this, data, reply, option);
181     }
182 
183     LOG(INFO) << "UpdateServiceStub OnRemoteRequest code " << code << "not found";
184     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
185 }
186 } // namespace SysInstaller
187 } // namespace OHOS