1 /*
2  * Copyright (c) 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 "bundle_manager_proxy.h"
17 
18 #include <fcntl.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 
23 #include "directory_ex.h"
24 #include "edm_constants.h"
25 #include "edm_log.h"
26 #include "func_code.h"
27 #include "message_parcel_utils.h"
28 #include "policy_type.h"
29 
30 namespace OHOS {
31 namespace EDM {
32 std::shared_ptr<BundleManagerProxy> BundleManagerProxy::instance_ = nullptr;
33 std::mutex BundleManagerProxy::mutexLock_;
34 const std::u16string DESCRIPTOR = u"ohos.edm.IEnterpriseDeviceMgr";
35 const std::string HAP_DIRECTORY = "/data/service/el1/public/edm/stream_install";
36 const std::string SEPARATOR = "/";
37 const int32_t DEFAULT_BUFFER_SIZE = 65536;
38 
BundleManagerProxy()39 BundleManagerProxy::BundleManagerProxy()
40 {
41     AddPolicyTypeMap();
42     EDMLOGD("BundleManagerProxy()");
43 }
44 
~BundleManagerProxy()45 BundleManagerProxy::~BundleManagerProxy()
46 {
47     EDMLOGD("~BundleManagerProxy()");
48 }
49 
AddPolicyTypeMap()50 void BundleManagerProxy::AddPolicyTypeMap()
51 {
52     policyTypeMap_[static_cast<int32_t>(PolicyType::ALLOW_INSTALL)] = EdmInterfaceCode::ALLOWED_INSTALL_BUNDLES;
53     policyTypeMap_[static_cast<int32_t>(PolicyType::DISALLOW_INSTALL)] = EdmInterfaceCode::DISALLOWED_INSTALL_BUNDLES;
54     policyTypeMap_[static_cast<int32_t>(PolicyType::DISALLOW_UNINSTALL)] =
55         EdmInterfaceCode::DISALLOWED_UNINSTALL_BUNDLES;
56 }
57 
GetBundleManagerProxy()58 std::shared_ptr<BundleManagerProxy> BundleManagerProxy::GetBundleManagerProxy()
59 {
60     if (instance_ == nullptr) {
61         std::lock_guard<std::mutex> lock(mutexLock_);
62         if (instance_ == nullptr) {
63             std::shared_ptr<BundleManagerProxy> temp = std::make_shared<BundleManagerProxy>();
64             instance_ = temp;
65         }
66     }
67     return instance_;
68 }
69 
Uninstall(AppExecFwk::ElementName & admin,std::string bundleName,int32_t userId,bool isKeepData,std::string & retMessage)70 int32_t BundleManagerProxy::Uninstall(AppExecFwk::ElementName &admin, std::string bundleName, int32_t userId,
71     bool isKeepData, std::string &retMessage)
72 {
73     EDMLOGD("BundleManagerProxy::Uninstall");
74     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
75     MessageParcel data;
76     MessageParcel reply;
77     data.WriteInterfaceToken(DESCRIPTOR);
78     data.WriteInt32(WITHOUT_USERID);
79     data.WriteParcelable(&admin);
80     data.WriteString(WITHOUT_PERMISSION_TAG);
81     data.WriteString(bundleName);
82     data.WriteInt32(userId);
83     data.WriteBool(isKeepData);
84     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::UNINSTALL);
85     ErrCode ret = proxy->HandleDevicePolicy(funcCode, data, reply);
86     if (ret == EdmReturnErrCode::PARAM_ERROR) {
87         retMessage = reply.ReadString();
88     }
89     return ret;
90 }
91 
AddBundlesByPolicyType(AppExecFwk::ElementName & admin,std::vector<std::string> & bundles,int32_t userId,int32_t policyType)92 int32_t BundleManagerProxy::AddBundlesByPolicyType(AppExecFwk::ElementName &admin, std::vector<std::string> &bundles,
93     int32_t userId, int32_t policyType)
94 {
95     EDMLOGD("BundleManagerProxy::AddBundlesByPolicyType policyType =%{public}d", policyType);
96     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
97     MessageParcel data;
98     std::uint32_t funcCode = 0;
99     if (policyTypeMap_.count(policyType) > 0) {
100         funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, policyTypeMap_[policyType]);
101     } else {
102         EDMLOGE("can not get policy type");
103         return EdmReturnErrCode::PARAM_ERROR;
104     }
105     data.WriteInterfaceToken(DESCRIPTOR);
106     data.WriteInt32(HAS_USERID);
107     data.WriteInt32(userId);
108     data.WriteParcelable(&admin);
109     data.WriteString(WITHOUT_PERMISSION_TAG);
110     data.WriteStringVector(bundles);
111     return proxy->HandleDevicePolicy(funcCode, data);
112 }
113 
RemoveBundlesByPolicyType(AppExecFwk::ElementName & admin,std::vector<std::string> & bundles,int32_t userId,int32_t policyType)114 int32_t BundleManagerProxy::RemoveBundlesByPolicyType(AppExecFwk::ElementName &admin, std::vector<std::string> &bundles,
115     int32_t userId, int32_t policyType)
116 {
117     EDMLOGD("BundleManagerProxy::RemoveBundlesByPolicyType policyType =%{public}d", policyType);
118     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
119     MessageParcel data;
120     std::uint32_t funcCode = 0;
121     if (policyTypeMap_.count(policyType) > 0) {
122         funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::REMOVE, policyTypeMap_[policyType]);
123     } else {
124         EDMLOGE("can not get policy type");
125         return EdmReturnErrCode::PARAM_ERROR;
126     }
127     data.WriteInterfaceToken(DESCRIPTOR);
128     data.WriteInt32(HAS_USERID);
129     data.WriteInt32(userId);
130     data.WriteParcelable(&admin);
131     data.WriteString(WITHOUT_PERMISSION_TAG);
132     data.WriteStringVector(bundles);
133     return proxy->HandleDevicePolicy(funcCode, data);
134 }
135 
GetBundlesByPolicyType(AppExecFwk::ElementName & admin,int32_t userId,std::vector<std::string> & bundles,int32_t policyType)136 int32_t BundleManagerProxy::GetBundlesByPolicyType(AppExecFwk::ElementName &admin, int32_t userId,
137     std::vector<std::string> &bundles, int32_t policyType)
138 {
139     EDMLOGD("BundleManagerProxy::GetAllowedOrDisallowedInstallBundles policyType =%{public}d", policyType);
140     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
141     MessageParcel data;
142     MessageParcel reply;
143     data.WriteInterfaceToken(DESCRIPTOR);
144     data.WriteInt32(HAS_USERID);
145     data.WriteInt32(userId);
146     data.WriteString(WITHOUT_PERMISSION_TAG);
147     data.WriteInt32(HAS_ADMIN);
148     data.WriteParcelable(&admin);
149     if (policyTypeMap_.count(policyType) > 0) {
150         proxy->GetPolicy(policyTypeMap_[policyType], data, reply);
151     } else {
152         EDMLOGE("can not get policy type");
153         return EdmReturnErrCode::PARAM_ERROR;
154     }
155     int32_t ret = ERR_INVALID_VALUE;
156     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
157     if (!blRes) {
158         EDMLOGW("EnterpriseDeviceMgrProxy:GetPolicy fail. %{public}d", ret);
159         return ret;
160     }
161     int32_t size = reply.ReadInt32();
162     if (size > EdmConstants::APPID_MAX_SIZE) {
163         EDMLOGE("bundles size=[%{public}d] is too large", size);
164         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
165     }
166     reply.ReadStringVector(&bundles);
167     return ERR_OK;
168 }
169 
Install(AppExecFwk::ElementName & admin,std::vector<std::string> & hapFilePaths,AppExecFwk::InstallParam & installParam,std::string & errMessage)170 int32_t BundleManagerProxy::Install(AppExecFwk::ElementName &admin, std::vector<std::string> &hapFilePaths,
171     AppExecFwk::InstallParam &installParam, std::string &errMessage)
172 {
173     EDMLOGD("BundleManagerProxy::install");
174 
175     if (hapFilePaths.empty()) {
176         EDMLOGE("install failed due to empty hapFilePaths");
177         return EdmReturnErrCode::PARAM_ERROR;
178     }
179     std::vector<std::string> realPaths;
180     for (auto const &hapFilePath : hapFilePaths) {
181         ErrCode res = WriteFileToStream(admin, hapFilePath, realPaths, errMessage);
182         if (res != ERR_OK) {
183             EDMLOGE("WriteFileToStream failed");
184             return res;
185         }
186     }
187 
188     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
189     MessageParcel data;
190     MessageParcel reply;
191     data.WriteInterfaceToken(DESCRIPTOR);
192     data.WriteInt32(WITHOUT_USERID);
193     data.WriteParcelable(&admin);
194     data.WriteString(WITHOUT_PERMISSION_TAG);
195     data.WriteStringVector(realPaths);
196     MessageParcelUtils::WriteInstallParam(installParam, data);
197     std::uint32_t funcCode = POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::SET, EdmInterfaceCode::INSTALL);
198     ErrCode ret = proxy->HandleDevicePolicy(funcCode, data, reply);
199     if (ret == EdmReturnErrCode::APPLICATION_INSTALL_FAILED) {
200         errMessage = reply.ReadString();
201         EDMLOGE("Install failed");
202     }
203     return ret;
204 }
205 
WriteFileToInner(MessageParcel & reply,const std::string & realPath,std::vector<std::string> & servicePaths,std::string & errMessage)206 ErrCode BundleManagerProxy::WriteFileToInner(MessageParcel &reply, const std::string &realPath,
207     std::vector<std::string> &servicePaths, std::string &errMessage)
208 {
209     int32_t sharedFd = reply.ReadFileDescriptor();
210     servicePaths.emplace_back(reply.ReadString());
211     if (sharedFd < 0) {
212         EDMLOGE("write file to stream failed due to invalid file descriptor");
213         errMessage = "write file to stream failed due to invalid file descriptor";
214         return EdmReturnErrCode::APPLICATION_INSTALL_FAILED;
215     }
216     int32_t outputFd = dup(sharedFd);
217     close(sharedFd);
218 
219     int32_t inputFd = open(realPath.c_str(), O_RDONLY);
220     if (inputFd < 0) {
221         close(outputFd);
222         EDMLOGE("write file to stream failed due to open the hap file");
223         errMessage = "write file to stream failed due to open the hap file";
224         return EdmReturnErrCode::APPLICATION_INSTALL_FAILED;
225     }
226     char buffer[DEFAULT_BUFFER_SIZE] = {0};
227     int offset = -1;
228     while ((offset = read(inputFd, buffer, sizeof(buffer))) > 0) {
229         if (write(outputFd, buffer, offset) < 0) {
230             close(inputFd);
231             close(outputFd);
232             EDMLOGE("write file to the temp dir failed");
233             errMessage = "write file to the temp dir failed";
234             return EdmReturnErrCode::APPLICATION_INSTALL_FAILED;
235         }
236     }
237     close(inputFd);
238     fsync(outputFd);
239     close(outputFd);
240     return ERR_OK;
241 }
242 
WriteFileToStream(AppExecFwk::ElementName & admin,const std::string & hapFilePath,std::vector<std::string> & servicePaths,std::string & errMessage)243 ErrCode BundleManagerProxy::WriteFileToStream(AppExecFwk::ElementName &admin, const std::string &hapFilePath,
244     std::vector<std::string> &servicePaths, std::string &errMessage)
245 {
246     std::string fileName;
247     std::string realPath;
248     ErrCode checkRet = checkHapFilePath(hapFilePath, fileName, realPath, errMessage);
249     if (checkRet != ERR_OK) {
250         return checkRet;
251     }
252 
253     auto proxy = EnterpriseDeviceMgrProxy::GetInstance();
254     MessageParcel data;
255     MessageParcel reply;
256     data.WriteInterfaceToken(DESCRIPTOR);
257     data.WriteInt32(WITHOUT_USERID);
258     data.WriteString(WITHOUT_PERMISSION_TAG);
259     data.WriteInt32(HAS_ADMIN);
260     data.WriteParcelable(&admin);
261     data.WriteString(fileName);
262     proxy->GetPolicy(POLICY_FUNC_CODE((std::uint32_t)FuncOperateType::GET, EdmInterfaceCode::INSTALL), data, reply);
263     int32_t ret = ERR_INVALID_VALUE;
264     bool blRes = reply.ReadInt32(ret) && (ret == ERR_OK);
265     if (!blRes) {
266         EDMLOGW("BundleManagerProxy:WriteFileToStream fail. %{public}d", ret);
267         errMessage = reply.ReadString();
268         return ret;
269     }
270     if (WriteFileToInner(reply, realPath, servicePaths, errMessage) != ERR_OK) {
271         EDMLOGE("write file to stream failed");
272         return EdmReturnErrCode::APPLICATION_INSTALL_FAILED;
273     }
274     return ERR_OK;
275 }
276 
checkHapFilePath(const std::string & hapFilePath,std::string & fileName,std::string & realPath,std::string & errMessage)277 ErrCode BundleManagerProxy::checkHapFilePath(const std::string &hapFilePath, std::string &fileName,
278     std::string &realPath, std::string &errMessage)
279 {
280     if (!PathToRealPath(hapFilePath, realPath)) {
281         EDMLOGE("install failed due to invalid hapFilePaths");
282         errMessage = "install failed due to invalid hapFilePaths";
283         return EdmReturnErrCode::APPLICATION_INSTALL_FAILED;
284     }
285 
286     // find hap file name
287     size_t pos = realPath.find_last_of(SEPARATOR);
288     if (pos == std::string::npos || pos == realPath.size() - 1) {
289         EDMLOGE("write file to stream failed due to invalid file path");
290         errMessage = "write file to stream failed due to invalid file path";
291         return EdmReturnErrCode::APPLICATION_INSTALL_FAILED;
292     }
293     fileName = realPath.substr(pos + 1);
294     if (fileName.empty()) {
295         EDMLOGE("write file to stream failed due to invalid file path");
296         errMessage = "write file to stream failed due to invalid file path";
297         return EdmReturnErrCode::APPLICATION_INSTALL_FAILED;
298     }
299     std::string innerFilePath = HAP_DIRECTORY + SEPARATOR + fileName;
300     if ((innerFilePath.length() > PATH_MAX)) {
301         errMessage = "invalid hap file path";
302         return EdmReturnErrCode::APPLICATION_INSTALL_FAILED;
303     }
304     return ERR_OK;
305 }
306 } // namespace EDM
307 } // namespace OHOS
308