• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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_UTILS_H
17 #define SANDBOX_UTILS_H
18 
19 #include <string>
20 #include <vector>
21 #include <sys/types.h>
22 #include "nlohmann/json.hpp"
23 #include "client_socket.h"
24 
25 namespace OHOS {
26 namespace AppSpawn {
27 class SandboxUtils {
28 public:
29     static void StoreNamespaceJsonConfig(nlohmann::json &appNamespaceConfig);
30     static nlohmann::json GetNamespaceJsonConfig(void);
31     static void StoreJsonConfig(nlohmann::json &appSandboxConfig);
32     static nlohmann::json GetJsonConfig();
33     static void StoreProductJsonConfig(nlohmann::json &productSandboxConfig);
34     static nlohmann::json GetProductJsonConfig();
35     static int32_t SetAppSandboxProperty(const ClientSocket::AppProperty *appProperty);
36     static uint32_t GetNamespaceFlagsFromConfig(const char *bundleName);
37 
38 private:
39     static int32_t DoAppSandboxMountOnce(const char *originPath, const char *destinationPath,
40                                          const char *fsType, unsigned long mountFlags,
41                                          const char *options);
42     static int32_t DoSandboxFileCommonBind(const ClientSocket::AppProperty *appProperty, nlohmann::json &wholeConfig);
43     static int32_t DoSandboxFileCommonSymlink(const ClientSocket::AppProperty *appProperty,
44                                               nlohmann::json &wholeConfig);
45     static int32_t DoSandboxFilePrivateBind(const ClientSocket::AppProperty *appProperty, nlohmann::json &wholeConfig);
46     static int32_t DoSandboxFilePrivateSymlink(const ClientSocket::AppProperty *appProperty,
47                                                nlohmann::json &wholeConfig);
48     static int32_t DoSandboxFilePrivateFlagsPointHandle(const ClientSocket::AppProperty *appProperty,
49                                                         nlohmann::json &wholeConfig);
50     static int32_t DoSandboxFileCommonFlagsPointHandle(const ClientSocket::AppProperty *appProperty,
51                                                        nlohmann::json &wholeConfig);
52     static int32_t HandleFlagsPoint(const ClientSocket::AppProperty *appProperty,
53                                            nlohmann::json &wholeConfig);
54     static int32_t SetPrivateAppSandboxProperty(const ClientSocket::AppProperty *appProperty);
55     static int32_t SetCommonAppSandboxProperty(const ClientSocket::AppProperty *appProperty,
56                                                       std::string &sandboxPackagePath);
57     static int32_t DoSandboxRootFolderCreateAdapt(std::string &sandboxPackagePath);
58     static int32_t DoSandboxRootFolderCreate(const ClientSocket::AppProperty *appProperty,
59                                              std::string &sandboxPackagePath);
60     static void DoSandboxChmod(nlohmann::json jsonConfig, std::string &sandboxRoot);
61     static int DoAllMntPointsMount(const ClientSocket::AppProperty *appProperty, nlohmann::json &appConfig);
62     static int DoAllSymlinkPointslink(const ClientSocket::AppProperty *appProperty, nlohmann::json &appConfig);
63     static std::string ConvertToRealPath(const ClientSocket::AppProperty *appProperty, std::string sandboxRoot);
64     static std::string GetSbxPathByConfig(const ClientSocket::AppProperty *appProperty, nlohmann::json &config);
65     static void CheckAndPrepareSrcPath(const ClientSocket::AppProperty *appProperty, const std::string &srcPath);
66     static bool CheckTotalSandboxSwitchStatus(const ClientSocket::AppProperty *appProperty);
67     static bool CheckAppSandboxSwitchStatus(const ClientSocket::AppProperty *appProperty);
68     static bool CheckBundleNameForPrivate(const std::string &bundleName);
69     static bool GetSbxSwitchStatusByConfig(nlohmann::json &config);
70     static unsigned long GetMountFlagsFromConfig(const std::vector<std::string> &vec);
71     static int32_t SetCommonAppSandboxProperty_(const ClientSocket::AppProperty *appProperty,
72                                          nlohmann::json &config);
73     static int32_t SetPrivateAppSandboxProperty_(const ClientSocket::AppProperty *appProperty,
74                                           nlohmann::json &config);
75     static int32_t SetRenderSandboxProperty(const ClientSocket::AppProperty *appProperty,
76                                             std::string &sandboxPackagePath);
77 
78 private:
79     static nlohmann::json appNamespaceConfig_;
80     static nlohmann::json appSandboxConfig_;
81     static nlohmann::json productSandboxConfig_;
82 };
83 } // namespace AppSpawn
84 } // namespace OHOS
85 #endif  // SANDBOX_UTILS_H
86