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