• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 SANDBOX_COMMON_H
17 #define SANDBOX_COMMON_H
18 
19 #include <string>
20 #include <vector>
21 #include <map>
22 #include <functional>
23 #include "sandbox_def.h"
24 #include "appspawn_msg.h"
25 #include "appspawn_server.h"
26 #include "appspawn_manager.h"
27 #include "sandbox_shared_mount.h"
28 #include "json_utils.h"
29 
30 namespace OHOS {
31 namespace AppSpawn {
32 
33 // 挂载选项
34 typedef struct SandboxMountConfig {
35     unsigned long mountFlags;
36     std::string optionsPoint;
37     std::string fsType;
38     std::string sandboxPath;
39     std::vector<std::string> decPaths;
40 } SandboxMountConfig;
41 
42 typedef struct MountPointProcessParams {
43     const AppSpawningCtx *appProperty;  // 引用属性
44     bool checkFlag;                     // 检查标志
45     std::string section;                // 分区名称
46     std::string sandboxRoot;            // 沙箱根路径
47     std::string bundleName;             // 包名
48 } MountPointProcessParams;
49 
50 using ArrayItemProcessor = std::function<int32_t(cJSON*)>;
51 
52 class SandboxCommon {
53 
54 public:
55     // 加载配置文件
56     static int LoadAppSandboxConfigCJson(AppSpawnMgr *content);
57     static int FreeAppSandboxConfigCJson(AppSpawnMgr *content);
58     static void StoreJsonConfig(cJSON *appSandboxConfig, SandboxCommonDef::SandboxConfigType type);
59     static std::vector<cJSON *> &GetCJsonConfig(SandboxCommonDef::SandboxConfigType type); // GetJsonConfig
60 
61     static int32_t HandleArrayForeach(cJSON *arrayJson, ArrayItemProcessor processor);
62 
63     // 获取应用信息
64     static std::string GetExtraInfoByType(const AppSpawningCtx *appProperty, const std::string &type);
65     static std::string GetSandboxRootPath(const AppSpawningCtx *appProperty, cJSON *config); // GetSbxPathByConfig
66 
67     // 文件操作
68     static int CreateDirRecursive(const std::string &path, mode_t mode); // MakeDirRecursive
69     static void CreateDirRecursiveWithClock(const std::string &path, mode_t mode); // MakeDirRecursiveWithClock
70     static void SetSandboxPathChmod(cJSON *jsonConfig, std::string &sandboxRoot); // DoSandboxChmod
71 
72     // 获取挂载配置参数信息
73     static uint32_t ConvertFlagStr(const std::string &flagStr);
74     static unsigned long GetMountFlags(cJSON *config); // GetSandboxMountFlags
75     static bool IsCreateSandboxPathEnabled(cJSON *json, std::string srcPath); // GetCreateSandboxPath
76     static bool IsTotalSandboxEnabled(const AppSpawningCtx *appProperty); // CheckTotalSandboxSwitchStatus
77     static bool IsAppSandboxEnabled(const AppSpawningCtx *appProperty); // CheckAppSandboxSwitchStatus
78     static void GetSandboxMountConfig(const AppSpawningCtx *appProperty, const std::string &section,
79                                       cJSON *mntPoint, SandboxMountConfig &mountConfig);
80 
81     // 校验操作
82     static bool HasPrivateInBundleName(const std::string &bundleName); // CheckBundleNameForPrivate
83     static bool IsMountSuccessful(cJSON *mntPoint); // GetCheckStatus
84     static int CheckBundleName(const std::string &bundleName);
85     static bool IsValidMountConfig(cJSON *mntPoint, const AppSpawningCtx *appProperty,
86                                    bool checkFlag); // CheckMountConfig
87     static bool IsPrivateSharedStatus(const std::string &bundleName,
88                                       AppSpawningCtx *appProperty); // GetSandboxPrivateSharedStatus
89     static int32_t CheckAppFullMountEnable();
90 
91     // 路径处理
92     static std::vector<std::string> SplitString(std::string &str, const std::string &delimiter); // split
93     static std::string ReplaceAllVariables(std::string str, const std::string& from,
94                                            const std::string& to); // replace_all
95     static std::string ConvertToRealPath(const AppSpawningCtx *appProperty, std::string path);
96     static std::string ConvertToRealPathWithPermission(const AppSpawningCtx *appProperty, std::string path);
97 
98     // 挂载操作
99     static int32_t DoAppSandboxMountOnce(const AppSpawningCtx *appProperty, const SharedMountArgs *arg);
100 
101 private:
102     // 加载配置文件
103     static uint32_t GetSandboxNsFlags(bool isNweb);
104     static bool AppSandboxPidNsIsSupport(void);
105     static void StoreCJsonConfig(cJSON *root, SandboxCommonDef::SandboxConfigType type);
106 
107     // 文件操作
108     static bool VerifyDirRecursive(const std::string &path); // CheckDirRecursive
109     static void CreateFileIfNotExist(const char *file); // CheckAndCreatFile
110 
111     // 获取挂载配置参数信息
112     static bool GetSwitchStatus(cJSON *config); // GetSbxSwitchStatusByConfig
113     static unsigned long GetMountFlagsFromConfig(const std::vector<std::string> &vec);
114     static bool IsDacOverrideEnabled(cJSON *config); // GetSandboxDacOverrideEnable
115     static std::string GetFsType(cJSON *config); // GetSandboxFsType
116     static std::string GetOptions(const AppSpawningCtx *appProperty, cJSON *config); // GetSandboxOptions
117     static std::vector<std::string> GetDecPath(const AppSpawningCtx *appProperty, cJSON *config); // GetSandboxDecPath
118 
119     // 校验操作
120     static bool IsNeededCheckPathStatus(const AppSpawningCtx *appProperty, const char *path);
121     static void CheckMountStatus(const std::string &path);
122 
123     // 路径处理
124     static std::string ReplaceVariablePackageName(const AppSpawningCtx *appProperty, const std::string &path);
125     static void MakeAtomicServiceDir(const AppSpawningCtx *appProperty, std::string path,
126                                      std::string variablePackageName);
127     static std::string ReplaceHostUserId(const AppSpawningCtx *appProperty, const std::string &path);
128     static std::string ReplaceClonePackageName(const AppSpawningCtx *appProperty, const std::string &path);
129     static const std::string &GetArkWebPackageName(void);
130     static const std::string &GetDevModel(void);
131 
132 private:
133     static int32_t deviceTypeEnable_;
134     static std::map<SandboxCommonDef::SandboxConfigType, std::vector<cJSON *>> appSandboxCJsonConfig_;
135     typedef enum {
136         SANDBOX_PACKAGENAME_DEFAULT = 0,
137         SANDBOX_PACKAGENAME_CLONE,
138         SANDBOX_PACKAGENAME_EXTENSION,
139         SANDBOX_PACKAGENAME_CLONE_AND_EXTENSION,
140         SANDBOX_PACKAGENAME_ATOMIC_SERVICE,
141     } SandboxVarPackageNameType;
142 };
143 
144 } // namespace AppSpawn
145 } // namespace OHOS
146 
147 #endif // SANDBOX_COMMON_H