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 HAP_RESTORECON_H
17 #define HAP_RESTORECON_H
18 
19 #include <iosfwd>
20 #include <string>
21 #include <unordered_map>
22 #include <vector>
23 #include <selinux/context.h>
24 #include "sehap_contexts_trie.h"
25 
26 #define SELINUX_HAP_RESTORECON_RECURSE 1    // whether the data directory need recurse
27 #define SELINUX_HAP_RESTORECON_PREINSTALLED_APP 1   // whether it is a pre-built app
28 #define SELINUX_HAP_DEBUGGABLE 2 // whether it is a debuggable hap
29 #define SELINUX_HAP_DLP 4 // whether it is dlp hap
30 #define SELINUX_HAP_INPUT_ISOLATE 8 // whether it is input_isolate hap
31 
32 // parameters of each SehapInfo in file sehap_contexts
33 struct SehapInfo {
34     std::string apl = "";
35     std::string name = "";
36     std::string domain = "";
37     std::string type = "";
38     bool debuggable = false;
39     unsigned int extra = 0;
40 };
41 
42 struct HapFileInfo {
43     std::vector<std::string> pathNameOrig;
44     std::string apl;
45     std::string packageName;
46     unsigned int flags;
47     unsigned int hapFlags = 0;
48 };
49 
50 struct HapDomainInfo {
51     std::string apl;
52     std::string packageName;
53     unsigned int hapFlags = 0;
54 };
55 
56 class HapContext {
57 public:
58     HapContext();
59     ~HapContext();
60     int HapFileRestorecon(HapFileInfo& hapFileInfo);
61 
62     int HapDomainSetcontext(HapDomainInfo& hapDomainInfo);
63 
64 protected:
65     int HapFileRestorecon(const std::string &pathNameOrig, HapFileInfo& hapFileInfo);
66     int HapFileRecurseRestorecon(const std::string &realPath, HapFileInfo& hapFileInfo);
67     int RestoreconSb(const std::string &pathNameOrig, HapFileInfo& hapFileInfo);
68     int GetSecontext(HapFileInfo& hapFileInfo, const std::string &pathNameOrig,
69         char **newSecontext, char **oldSecontext);
70     int HapLabelLookup(const std::string &apl, const std::string &packageName,
71         char **secontextPtr, unsigned int hapFlags);
72 
73     int HapContextsLookup(bool isDomain, const std::string &apl, const std::string &packageName,
74         context_t con, unsigned int hapFlags);
75     int TypeSet(const std::string &type, context_t con);
76 };
77 
78 #endif // HAP_RESTORECON_H
79