• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "install_plugin.h"
17 
18 #include <fcntl.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <system_ability_definition.h>
22 #include <unistd.h>
23 
24 #include "bundle_mgr_interface.h"
25 #include "bundle_mgr_proxy.h"
26 #include "directory_ex.h"
27 #include "edm_ipc_interface_code.h"
28 #include "edm_sys_manager.h"
29 #include "installer_callback.h"
30 #include "iplugin_manager.h"
31 
32 namespace OHOS {
33 namespace EDM {
34 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(InstallPlugin::GetPlugin());
35 const std::string HAP_DIRECTORY = "/data/service/el1/public/edm/stream_install";
36 const std::string RELATIVE_PATH = "../";
37 const std::string CURRENT_PATH = "./";
38 const std::string SEPARATOR = "/";
39 constexpr int32_t EDM_UID = 3057;
40 constexpr int32_t EDM_GID = 3057;
41 
InitPlugin(std::shared_ptr<IPluginTemplate<InstallPlugin,InstallParam>> ptr)42 void InstallPlugin::InitPlugin(std::shared_ptr<IPluginTemplate<InstallPlugin, InstallParam>> ptr)
43 {
44     EDMLOGD("InstallPlugin InitPlugin...");
45     ptr->InitAttribute(EdmInterfaceCode::INSTALL, "install", "ohos.permission.ENTERPRISE_INSTALL_BUNDLE",
46         IPlugin::PermissionType::SUPER_DEVICE_ADMIN, false);
47     ptr->SetSerializer(InstallParamSerializer::GetInstance());
48     ptr->SetOnHandlePolicyListener(&InstallPlugin::OnSetPolicy, FuncOperateType::SET);
49 }
50 
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)51 ErrCode InstallPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply, int32_t userId)
52 {
53     std::string fileName = data.ReadString();
54     std::string bundlePath = HAP_DIRECTORY + "/" + fileName;
55 
56     if (bundlePath.length() > PATH_MAX) {
57         EDMLOGE("bundlePath length is error, the length is: [%{public}zu]", bundlePath.length());
58         reply.WriteInt32(EdmReturnErrCode::APPLICATION_INSTALL_FAILED);
59         reply.WriteString("invalid hapFilePath");
60         return EdmReturnErrCode::APPLICATION_INSTALL_FAILED;
61     }
62     if (fileName.find(RELATIVE_PATH) != std::string::npos || fileName.find(CURRENT_PATH) != std::string::npos ||
63         fileName.find(SEPARATOR) != std::string::npos) {
64         EDMLOGE("file path %{public}s invalid", bundlePath.c_str());
65         reply.WriteInt32(EdmReturnErrCode::APPLICATION_INSTALL_FAILED);
66         reply.WriteString("invalid hapFilePath");
67         return EdmReturnErrCode::APPLICATION_INSTALL_FAILED;
68     }
69     if (!CreateDirectory()) {
70         reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
71         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
72     }
73 
74     int32_t fd = open(bundlePath.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
75     if (fd < 0) {
76         EDMLOGE("open bundlePath %{public}s failed", bundlePath.c_str());
77         DeleteFiles();
78         reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
79         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
80     }
81     reply.WriteInt32(ERR_OK);
82     reply.WriteFileDescriptor(fd);
83     reply.WriteString(bundlePath);
84     return ERR_OK;
85 }
86 
CreateDirectory()87 bool InstallPlugin::CreateDirectory()
88 {
89     if (!OHOS::ForceCreateDirectory(HAP_DIRECTORY)) {
90         EDMLOGE("mkdir %{public}s failed", HAP_DIRECTORY.c_str());
91         return false;
92     }
93     if (chown(HAP_DIRECTORY.c_str(), EDM_UID, EDM_GID) != 0) {
94         EDMLOGE("fail to change %{public}s ownership", HAP_DIRECTORY.c_str());
95         return false;
96     }
97     mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
98     if (!OHOS::ChangeModeFile(HAP_DIRECTORY, mode)) {
99         EDMLOGE("change mode failed, temp install dir : %{public}s", HAP_DIRECTORY.c_str());
100         return false;
101     }
102     return true;
103 }
104 
DeleteFiles()105 bool InstallPlugin::DeleteFiles()
106 {
107     std::vector<std::string> files;
108     OHOS::GetDirFiles(HAP_DIRECTORY, files);
109 
110     for (auto const &file : files) {
111         if (!OHOS::RemoveFile(file)) {
112             EDMLOGE("can not remove file : %{public}s", file.c_str());
113             return false;
114         }
115     }
116     return true;
117 }
118 
OnSetPolicy(InstallParam & param,MessageParcel & reply)119 ErrCode InstallPlugin::OnSetPolicy(InstallParam &param, MessageParcel &reply)
120 {
121     EDMLOGI("InstallPlugin OnSetPolicy");
122     AppExecFwk::InstallParam installParam;
123     std::vector<std::string> realPaths;
124     ErrCode initRet = InstallParamInit(param, reply, installParam, realPaths);
125     if (initRet != ERR_OK) {
126         return initRet;
127     }
128 
129     auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
130     auto iBundleMgr = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
131     if (iBundleMgr == nullptr) {
132         EDMLOGE("can not get iBundleMgr");
133         DeleteFiles();
134         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
135     }
136     auto iBundleInstaller = iBundleMgr->GetBundleInstaller();
137     if ((iBundleInstaller == nullptr) || (iBundleInstaller->AsObject() == nullptr)) {
138         EDMLOGE("can not get iBundleInstaller");
139         DeleteFiles();
140         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
141     }
142     sptr<InstallerCallback> callback = new (std::nothrow) InstallerCallback();
143     if (callback == nullptr) {
144         DeleteFiles();
145         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
146     }
147 
148     ErrCode ret = iBundleInstaller->StreamInstall(realPaths, installParam, callback);
149     if (FAILED(ret)) {
150         if (!DeleteFiles()) {
151             return EdmReturnErrCode::SYSTEM_ABNORMALLY;
152         }
153         EDMLOGE("StreamInstall resultCode %{public}d", ret);
154         reply.WriteInt32(EdmReturnErrCode::APPLICATION_INSTALL_FAILED);
155         reply.WriteString("invalid hap file path");
156         return EdmReturnErrCode::APPLICATION_INSTALL_FAILED;
157     }
158     ret = callback->GetResultCode();
159     std::string errorMessage = callback->GetResultMsg();
160     EDMLOGE("StreamInstall resultCode %{public}d resultMsg %{public}s.", ret, errorMessage.c_str());
161     if (!DeleteFiles()) {
162         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
163     }
164     if (ret != ERR_OK) {
165         reply.WriteInt32(EdmReturnErrCode::APPLICATION_INSTALL_FAILED);
166         reply.WriteString(errorMessage);
167         return EdmReturnErrCode::APPLICATION_INSTALL_FAILED;
168     }
169     EDMLOGI("InstallPlugin OnSetPolicy end");
170     return ERR_OK;
171 }
172 
InstallParamInit(InstallParam & param,MessageParcel & reply,AppExecFwk::InstallParam & installParam,std::vector<std::string> & realPaths)173 ErrCode InstallPlugin::InstallParamInit(InstallParam &param, MessageParcel &reply,
174     AppExecFwk::InstallParam &installParam, std::vector<std::string> &realPaths)
175 {
176     installParam.userId = param.userId;
177     installParam.installFlag = static_cast<AppExecFwk::InstallFlag>(param.installFlag);
178     std::vector<std::string> hapFilePaths = param.hapFilePaths;
179 
180     for (auto const &hapFilePath : hapFilePaths) {
181         std::string realPath = "";
182         if (!PathToRealPath(hapFilePath, realPath)) {
183             EDMLOGE("invalid hap file path");
184             std::string errMsg = "invalid hap file path";
185             reply.WriteInt32(EdmReturnErrCode::APPLICATION_INSTALL_FAILED);
186             reply.WriteString(errMsg);
187             DeleteFiles();
188             return EdmReturnErrCode::APPLICATION_INSTALL_FAILED;
189         }
190         realPaths.emplace_back(realPath);
191     }
192     return ERR_OK;
193 }
194 } // namespace EDM
195 } // namespace OHOS
196