• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_OPERATOR_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_OPERATOR_H
18 
19 #include <mutex>
20 #include <string>
21 #include <vector>
22 
23 #include "aot/aot_args.h"
24 #include "appexecfwk_errors.h"
25 #include "bundle_extractor.h"
26 #include "code_sign_helper.h"
27 #include "installd/installd_constants.h"
28 #include "ipc/check_encryption_param.h"
29 #include "ipc/code_signature_param.h"
30 #include "ipc/extract_param.h"
31 #include "nocopyable.h"
32 
33 namespace OHOS {
34 namespace AppExecFwk {
35 using EnforceMetadataProcessForApp = int32_t (*)(const std::unordered_map<std::string, std::string> &,
36     uint32_t, bool &, const int32_t, const bool &);
37 
38 class InstalldOperator {
39 public:
40     /**
41      * @brief Check link file and unlink.
42      * @param path Indicates the file path to be checked.
43      * @return Returns true if the file is link and unlink succeed; returns false otherwise.
44      */
45     static bool CheckAndDeleteLinkFile(const std::string &path);
46     /**
47      * @brief Check whether a file exist.
48      * @param path Indicates the file path to be checked.
49      * @return Returns true if the file exist; returns false otherwise.
50      */
51     static bool IsExistFile(const std::string &path);
52     /**
53      * @brief Check whether an AP file exists in the current directory of the file.
54      * @param path Indicates the file path to be checked.
55      * @return Returns true if the file exist; returns false otherwise.
56      */
57     static bool IsExistApFile(const std::string &path);
58     /**
59      * @brief Check whether a directory exist.
60      * @param path Indicates the directory path to be checked.
61      * @return Returns true if the directory exist; returns false otherwise.
62      */
63     static bool IsExistDir(const std::string &path);
64     /**
65      * @brief Check whether a directory is empty.
66      * @param dir Indicates the directory path to be checked.
67      * @return Returns true if the directory is empty; returns false otherwise.
68      */
69     static bool IsDirEmpty(const std::string &dir);
70     /**
71      * @brief Make a new directory including the parent path if not exist.
72      * @param path Indicates the directory path to be checked.
73      * @param isReadByOthers Indicates the directory whether read by other users.
74      * @return Returns true if the directory make successfully; returns false otherwise.
75      */
76     static bool MkRecursiveDir(const std::string &path, bool isReadByOthers);
77     /**
78      * @brief Delete a directory.
79      * @param path Indicates the directory path to be deleted.
80      * @return Returns true if the directory deleted successfully; returns false otherwise.
81      */
82     static bool DeleteDir(const std::string &path);
83 
84     static bool DeleteDirFast(const std::string &path);
85     /**
86      * @brief Extract the files of a compressed package to a specific directory.
87      * @param srcModulePath Indicates the package file path.
88      * @param targetSoPath so files decompression path.
89      * @param cpuAbi cpuAbi.
90      * @return Returns true if the package extracted successfully; returns false otherwise.
91      */
92     static bool ExtractFiles(const std::string &sourcePath, const std::string &targetSoPath,
93         const std::string &cpuAbi);
94 
95     static bool IsNativeSo(const std::string &entryName, const std::string &cpuAbi);
96 
97     static bool ExtractFiles(const ExtractParam &extractParam);
98     static bool ExtractFiles(const std::string hnpPackageInfo, const ExtractParam &extractParam);
99     static void ExtractTargetFile(
100         const BundleExtractor &extractor,
101         const std::string &entryName,
102         const std::string &targetPath,
103         const std::string &cpuAbi,
104         const ExtractFileType &extractFileType = ExtractFileType::SO);
105     static void ExtractTargetHnpFile(
106         const BundleExtractor &extractor,
107         const std::string &entryName,
108         const std::string &targetPath,
109         const ExtractFileType &extractFileType = ExtractFileType::SO);
110     static bool ProcessBundleInstallNative(
111         const std::string &userId,
112         const std::string &hnpRootPath,
113         const std::string &hapPath,
114         const std::string &cpuAbi,
115         const std::string &packageName);
116     static bool ProcessBundleUnInstallNative(const std::string &userId, const std::string &bundleName);
117 
118     static bool DeterminePrefix(const ExtractFileType &extractFileType, const std::string &cpuAbi,
119         std::string &prefix);
120 
121     static bool DetermineSuffix(const ExtractFileType &extractFileType, std::vector<std::string> &suffixes);
122 
123     static bool IsNativeFile(
124         const std::string &entryName, const ExtractParam &extractParam);
125 
126     /**
127      * @brief Rename a directory from old path to new path.
128      * @param oldPath Indicates the old path name.
129      * @param newPath Indicates the new path name.
130      * @return Returns true if the directory renamed successfully; returns false otherwise.
131      */
132     static bool RenameDir(const std::string &oldPath, const std::string &newPath);
133     /**
134      * @brief Change the owner and group ID of a file or directory.
135      * @param filePath Indicates the file or directory path.
136      * @param uid Indicates the uid.
137      * @param uid Indicates the gid.
138      * @return Returns true if changed successfully; returns false otherwise.
139      */
140     static bool ChangeFileAttr(const std::string &filePath, const int uid, const int gid);
141     /**
142      * @brief Rename a file from old path to new path.
143      * @param oldPath Indicates the old path name.
144      * @param newPath Indicates the new path name.
145      * @return Returns true if the file renamed successfully; returns false otherwise.
146      */
147     static bool RenameFile(const std::string &oldPath, const std::string &newPath);
148     /**
149      * @brief Check whether a path is valid under a root path.
150      * @param rootDir Indicates the root path name.
151      * @param path Indicates the path to be checked.
152      * @return Returns true if the path is valid successfully; returns false otherwise.
153      */
154     static bool IsValidPath(const std::string &rootDir, const std::string &path);
155     /**
156      * @brief Check whether a path is valid code path.
157      * @param codePath Indicates the path to be checked.
158      * @return Returns true if the file renamed successfully; returns false otherwise.
159      */
160     static bool IsValidCodePath(const std::string &codePath);
161     /**
162      * @brief Get the parent directory path of a file.
163      * @param codePath Indicates the file path.
164      * @return Returns the parent directory if get successfully; returns empty string otherwise.
165      */
166     static std::string GetPathDir(const std::string &path);
167     /**
168      * @brief Delete files in a directory.
169      * @param path Indicates the directory path of the files to be deleted.
170      * @return Returns true if the files deleted successfully; returns false otherwise.
171      */
172     static bool DeleteFiles(const std::string &dataPath);
173     /**
174      * @brief Delete files in a directory except the directories to be kept.
175      * @param dataPath Indicates the directory path of the files to be deleted.
176      * @param dirsToKeep Indicates the directories to be kept.
177      * @return Returns true if the files deleted successfully; returns false otherwise
178      */
179     static bool DeleteFilesExceptDirs(const std::string &dataPath, const std::vector<std::string> &dirsToKeep);
180     /**
181      * @brief Make a directory and change the owner and group ID of it.
182      * @param path Indicates the directory path to be made.
183      * @param isReadByOthers Indicates the directory whether read by other users.
184      * @param uid Indicates the uid.
185      * @param uid Indicates the gid.
186      * @return Returns true if directory made successfully; returns false otherwise.
187      */
188     static bool MkOwnerDir(const std::string &path, bool isReadByOthers, const int uid, const int gid);
189     /**
190      * @brief Make a directory and change the owner and group ID of it.
191      * @param path Indicates the directory path to be made.
192      * @param mode Indicates the directory mode.
193      * @param uid Indicates the uid.
194      * @param uid Indicates the gid.
195      * @return Returns true if directory made successfully; returns false otherwise.
196      */
197     static bool MkOwnerDir(const std::string &path,  int mode, const int uid, const int gid);
198     /**
199      * @brief Get disk usage for dir.
200      * @param dir Indicates the directory.
201      * @param size Indicates the disk size.
202      * @return Returns true if successfully; returns false otherwise.
203      */
204     static int64_t GetDiskUsage(const std::string &dir, bool isRealPath = false);
205     /**
206      * @brief Traverse all cache directories.
207      * @param currentPath Indicates the current path.
208      * @param cacheDirs Indicates the cache directories.
209      * @return Returns true if successfully; returns false otherwise.
210      */
211     static void TraverseCacheDirectory(const std::string &currentPath, std::vector<std::string> &cacheDirs);
212     /**
213      * @brief Get disk usage from path.
214      * @param path Indicates the current path.
215      * @return Returns disk size.
216      */
217     static int64_t GetDiskUsageFromPath(const std::vector<std::string> &path);
218 
219     static bool InitialiseQuotaMounts();
220 
221     static int64_t GetDiskUsageFromQuota(const int32_t uid);
222 
223     static bool ScanDir(
224         const std::string &dirPath, ScanMode scanMode, ResultMode resultMode, std::vector<std::string> &paths);
225 
226     static bool ScanSoFiles(const std::string &newSoPath, const std::string &originPath,
227         const std::string &currentPath, std::vector<std::string> &paths);
228 
229     static bool CopyFile(const std::string &sourceFile, const std::string &destinationFile);
230 
231     static bool CopyFileFast(const std::string &sourcePath, const std::string &destPath);
232 
233     static bool ChangeDirOwnerRecursively(const std::string &path, const int uid, const int gid);
234 
235     static bool IsDiffFiles(const std::string &entryName,
236         const std::string &targetPath, const std::string &cpuAbi);
237 
238     static bool ExtractDiffFiles(const std::string &filePath, const std::string &targetPath,
239         const std::string &cpuAbi);
240 
241     static bool ApplyDiffPatch(const std::string &oldSoPath, const std::string &diffFilePath,
242         const std::string &newSoPath, int32_t uid);
243 
244     static bool ObtainQuickFixFileDir(const std::string &dir, std::vector<std::string> &fileVec);
245 
246     static bool CopyFiles(const std::string &sourceDir, const std::string &destinationDir);
247 
248     static bool GetNativeLibraryFileNames(const std::string &filePath, const std::string &cpuAbi,
249         std::vector<std::string> &fileNames);
250 
251     static bool GetAtomicServiceBundleDataDir(const std::string &bundleName,
252         const int32_t userId, std::vector<std::string> &allPathNames);
253 
254 #if defined(CODE_SIGNATURE_ENABLE)
255     static bool PrepareEntryMap(const CodeSignatureParam &codeSignatureParam,
256         const std::vector<std::string> &soEntryFiles, Security::CodeSign::EntryMap &entryMap);
257     static ErrCode PerformCodeSignatureCheck(const CodeSignatureParam &codeSignatureParam,
258         const Security::CodeSign::EntryMap &entryMap);
259 #endif
260 
261     static bool VerifyCodeSignature(const CodeSignatureParam &codeSignatureParam);
262 
263 #if defined(CODE_ENCRYPTION_ENABLE)
264     static bool EnforceEncryption(std::unordered_map<std::string, std::string> &entryMap, int32_t bundleId,
265         bool &isEncryption, InstallBundleType installBundleType, bool isCompressNativeLibrary);
266 #endif
267 
268     static bool CheckEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption);
269 
270     static bool CheckHapEncryption(const CheckEncryptionParam &checkEncryptionParam, bool &isEncryption);
271 
272     static bool MoveFiles(const std::string &srcDir, const std::string &desDir, bool isDesDirNeedCreated = false);
273 
274     static bool MoveFileOrDir(const std::string &srcPath, const std::string &destPath, mode_t mode);
275 
276     static bool MoveFile(const std::string &srcPath, const std::string &destPath);
277 
278     static bool ExtractDriverSoFiles(const std::string &srcPath,
279         const std::unordered_multimap<std::string, std::string> &dirMap);
280 
281     static bool CopyDriverSoFiles(const BundleExtractor &extractor, const std::string &originalDir,
282         const std::string &destinedDir);
283 
284 #if defined(CODE_ENCRYPTION_ENABLE)
285     static ErrCode ExtractSoFilesToTmpHapPath(const std::string &hapPath, const std::string &cpuAbi,
286         const std::string &tmpSoPath, int32_t uid);
287 
288     static ErrCode ExtractSoFilesToTmpSoPath(const std::string &hapPath, const std::string &realSoFilesPath,
289         const std::string &cpuAbi, const std::string &tmpSoPath, int32_t uid);
290 
291     static ErrCode DecryptSoFile(const std::string &hapPath, const std::string &tmpHapPath, int32_t uid,
292         uint32_t fileSize, uint32_t offset);
293 
294     static ErrCode RemoveEncryptedKey(int32_t uid, const std::vector<std::string> &soList);
295 
296     static int32_t CallIoctl(int32_t flag, int32_t associatedFlag, int32_t uid, int32_t &fd);
297 #endif
298 
299     static bool GenerateKeyIdAndSetPolicy(int32_t uid, const std::string &bundleName,
300         const int32_t userId, std::string &keyId);
301 
302     static bool DeleteKeyId(const std::string &bundleName, const int32_t userId);
303 
304 private:
305     static bool ObtainNativeSoFile(const BundleExtractor &extractor, const std::string &cpuAbi,
306         std::vector<std::string> &soEntryFiles);
307 
308     static bool ProcessApplyDiffPatchPath(const std::string &oldSoPath, const std::string &diffFilePath,
309         const std::string &newSoPath, std::vector<std::string> &oldSoFileNames,
310         std::vector<std::string> &diffFileNames);
311     static bool ExtractResourceFiles(const ExtractParam &extractParam, const BundleExtractor &extractor);
312     static bool CheckPathIsSame(const std::string &path, int32_t mode, const int32_t uid, const int32_t gid,
313         bool &isPathExist);
314 #if defined(CODE_ENCRYPTION_ENABLE)
315     static std::mutex encryptionMutex_;
316     static void *encryptionHandle_;
317     static EnforceMetadataProcessForApp enforceMetadataProcessForApp_;
318     static bool OpenEncryptionHandle();
319 #endif
320     static void FsyncFile(const std::string &path);
321     static std::string GetSameLevelTmpPath(const std::string &path);
322 };
323 }  // namespace AppExecFwk
324 }  // namespace OHOS
325 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_INSTALLD_OPERATOR_H
326