1 /*
2  * Copyright (c) 2020 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 OHOS_BUNDLE_INSTALLER_H
17 #define OHOS_BUNDLE_INSTALLER_H
18 
19 #include "bundle_common.h"
20 #include "bundle_info.h"
21 #include "bundle_info_utils.h"
22 #include "install_param.h"
23 #include "hap_sign_verify.h"
24 #include "stdint.h"
25 
26 #include <string>
27 #include <vector>
28 
29 namespace OHOS {
30 class BundleInstaller {
31 public:
32     BundleInstaller(const std::string &codeDirPath, const std::string &dataDirPath);
33     ~BundleInstaller();
34 
35     uint8_t Install(const char *path, const InstallParam &installParam);
36     uint8_t Uninstall(const char *bundleName, const InstallParam &installParam);
37     std::string GetCodeDirPath() const;
38     std::string GetDataDirPath() const;
39 private:
40     uint8_t ProcessBundleInstall(const std::string &path, const char *randStr, InstallRecord &installRecord,
41         uint8_t hapType);
42     uint8_t HandleFileAndBackUpRecord(const char *codePath, const char *randStr, InstallRecord &record,
43         bool isUpdate, uint8_t hapType);
44     uint8_t UpdateBundleInfo(const char *appId, const BundleRes &bundleRes, BundleInfo *bundleInfo, bool isUpdate,
45         uint8_t hapType);
46     uint8_t ReshapeAppId(const char *bundleName, std::string &appId);
47     uint8_t CheckProvisionInfoIsValid(const SignatureInfo &signatureInfo, const Permissions &permissions,
48         const char *bundleName);
49     bool MatchPermissions(const std::vector<std::string> & restrictedPermissions,
50         PermissionTrans *permissionsTrans, int32_t permTransNum);
51     bool MatchBundleName(const char *bundleName, const char *matchedBundleName);
52     uint8_t CheckInstallFileIsValid(const char *path);
53     void RecordThirdSystemBundle(const char *bundleName, const char *path);
54     uint8_t StorePermissions(const char *bundleName, PermissionTrans *permissions, int32_t permNum, bool isUpdate);
55     uint8_t CheckVersionAndSignature(const char *bundleName, BundleInfo *bundleInfo);
56     bool RenameJsonFile(const char *fileName, const char *randStr);
57     bool CheckIsThirdSystemBundle(const char *bundleName);
58     bool BackUpInstallRecord(const InstallRecord &record, const char *jsonPath);
59     void InitThirdSystemBundleRecord(const char *bundleName, const char *path);
60     bool BackUpUidAndGidInfo(const InstallRecord &record, const char *jsonPath);
61     void ModifyInstallDirByHapType(const InstallParam &installParam, uint8_t hapType);
62     uint8_t GetHapType(const char *path);
63     void RestoreInstallEnv(const InstallParam &installParam);
64     uint8_t CheckDeviceCapIsValid(BundleInfo *bundleInfo);
65     bool CheckAbilityCapIsValid(AbilityInfo *abilityInfo, char sysCaps[][MAX_SYSCAP_NAME_LEN], int32_t sysNum);
66 
67     std::string codeDirPath_;
68     std::string dataDirPath_;
69 };
70 
71 #define CHECK_PRO_RESULT(errcode, bundleInfo, permissions, abilityRes)       \
72     do {                                                                     \
73         if ((errcode) != ERR_OK) {                                             \
74             BundleInfoUtils::FreeBundleInfo(bundleInfo);                     \
75             AdapterFree((permissions).permissionTrans);                        \
76             AdapterFree(abilityRes);                                         \
77             return errcode;                                                  \
78         }                                                                    \
79     } while (0)
80 
81 #define CLEAR_INSTALL_ENV(bundleInfo)                                        \
82     do {                                                                     \
83         if ((bundleInfo) != nullptr) {                                         \
84             BundleDaemonClient::GetInstance().RemoveInstallDirectory(        \
85                 (bundleInfo)->codePath, (bundleInfo)->dataPath, false);          \
86             BundleInfoUtils::FreeBundleInfo(bundleInfo);                     \
87         }                                                                    \
88     } while (0)
89 
90 #define CHECK_PRO_PART_ROLLBACK(errcode, path, permissions, bundleInfo, abilityRes)      \
91     do {                                                                                 \
92         if ((errcode) != ERR_OK && (bundleInfo) != nullptr) {                                \
93             BundleDaemonClient::GetInstance().RemoveFile((path).c_str());                  \
94             BundleInfoUtils::FreeBundleInfo(bundleInfo);                                 \
95             AdapterFree((permissions).permissionTrans);                                    \
96             AdapterFree(abilityRes);                                                     \
97             return errcode;                                                              \
98         }                                                                                \
99     } while (0)
100 
101 #define CHECK_PRO_ROLLBACK(errcode, permissions, bundleInfo, abilityRes, randStr)        \
102     do {                                                                                 \
103         if ((errcode) != ERR_OK && (bundleInfo) != nullptr) {                                \
104             AdapterFree((permissions).permissionTrans);                                    \
105             AdapterFree(abilityRes);                                                     \
106             ManagerService::GetInstance().RemoveBundleInfo((bundleInfo)->bundleName);      \
107             BundleUtil::DeleteJsonFile((bundleInfo)->bundleName, randStr);                 \
108             CLEAR_INSTALL_ENV(bundleInfo);                                               \
109             return errcode;                                                              \
110         }                                                                                \
111     } while (0)
112 } // namespace OHOS
113 #endif // OHOS_BUNDLE_INSTALLER_H
114