Implement master and slave mode
[platform/core/security/security-manager.git] / src / common / include / smack-rules.h
1 /*
2  *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Contact: Rafal Krypa <r.krypa@samsung.com>
5  *
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License
17  */
18 /**
19  * @file        smack-rules.h
20  * @author      Jacek Bukarewicz <j.bukarewicz@samsung.com>
21  * @version     1.0
22  * @brief       Header file of a class managing smack rules
23  *
24  */
25 #ifndef _SMACK_RULES_H_
26 #define _SMACK_RULES_H_
27
28 #include <vector>
29 #include <string>
30 #include <smack-exceptions.h>
31
32 struct smack_accesses;
33
34 namespace SecurityManager {
35
36 class SmackRules
37 {
38 public:
39     SmackRules();
40     virtual ~SmackRules();
41
42     void add(const std::string &subject, const std::string &object,
43             const std::string &permissions);
44     void addModify(const std::string &subject, const std::string &object,
45             const std::string &allowPermissions, const std::string &denyPermissions);
46     void loadFromFile(const std::string &path);
47     void addFromTemplate(const std::vector<std::string> &templateRules,
48         const std::string &appId, const std::string &pkgId, const std::string &zoneId);
49     void addFromTemplateFile(const std::string &appId, const std::string &pkgId,
50             const std::string &zoneId);
51
52     void apply() const;
53     void clear() const;
54     void saveToFile(const std::string &path) const;
55
56     /**
57      * Create cross dependencies for all applications in a package
58      *
59      * This is needed for all applications within a package to have
60      * correct permissions to shared data.
61      *
62      * @param[in] pkgContents - a list of all applications inside this package
63      * @param[in] zoneId - ID of zone which requested application install
64      */
65     void generatePackageCrossDeps(const std::vector<std::string> &pkgContents,
66             const std::string &zoneId);
67
68     /**
69      * Install package-specific smack rules.
70      *
71      * Function creates smack rules using predefined template. Rules are applied
72      * to the kernel and saved on persistent storage so they are loaded on system boot.
73      *
74      * @param[in] appId - application id that is beeing installed
75      * @param[in] pkgId - package id that the application is in
76      * @param[in] pkgContents - a list of all applications in the package
77      */
78     static void installApplicationRules(const std::string &appId, const std::string &pkgId,
79         const std::vector<std::string> &pkgContents);
80
81     /**
82      * Install package-specific smack rules.
83      *
84      * Function creates smack rules using predefined template. Rules are applied
85      * to the kernel and saved on persistent storage so they are loaded on system boot.
86      *
87      * @param[in] appId - application id that is beeing installed
88      * @param[in] pkgId - package id that the application is in
89      * @param[in] pkgContents - a list of all applications in the package
90      * @param[in] zoneId - ID of zone which requested application install
91      */
92     static void installApplicationRules(const std::string &appId, const std::string &pkgId,
93         const std::vector<std::string> &pkgContents, const std::string &zoneId);
94     /**
95      * Uninstall package-specific smack rules.
96      *
97      * Function loads package-specific smack rules, revokes them from the kernel
98      * and removes them from the persistent storage.
99      *
100      * @param[in] pkgId - package identifier
101      */
102     static void uninstallPackageRules(const std::string &pkgId);
103
104     /* FIXME: Remove this function if real pkgId instead of "User" label will be used
105      * in generateAppLabel(). */
106     static void addMissingRulesFix();
107
108     /**
109     * Uninstall application-specific smack rules.
110     *
111     * Function removes application specific rules from the kernel, and
112     * removes them for persistent storage.
113     *
114     * @param[in] appId - application id
115     * @param[in] pkgId - package id that the application belongs to
116     * @param[in] appsInPkg - a list of other applications in the same package id that the application belongs to
117     * @param[in] zoneId - ID of zone which requested application uninstall
118     */
119     static void uninstallApplicationRules(const std::string &appId, const std::string &pkgId,
120             std::vector<std::string> appsInPkg, const std::string &zoneId);
121
122     /**
123      * Update package specific rules
124      *
125      * This function regenerates all package rules that
126      * need to exist currently for all application in that
127      * package
128      *
129      * @param[in] pkgId - id of the package to update
130      * @param[in] pkgContents - a list of all applications in the package
131      * @param[in] zoneId - ID of zone which requested application uninstall
132      */
133     static void updatePackageRules(const std::string &pkgId,
134             const std::vector<std::string> &pkgContents, const std::string &zoneId);
135
136 private:
137     /**
138      * Create a path for package rules
139      *
140      */
141     static std::string getPackageRulesFilePath(const std::string &pkgId);
142
143     /**
144      * Create a path for application rules
145      */
146     static std::string getApplicationRulesFilePath(const std::string &appId);
147
148     /**
149      * Uninstall rules inside a specified file path
150      *
151      * This is a utility function that will clear all
152      * rules in the file specified by path
153      *
154      * @param[in] path - path to the file that contains the rules
155      */
156     static void uninstallRules (const std::string &path);
157
158     smack_accesses *m_handle;
159 };
160
161 } // namespace SecurityManager
162
163 #endif /* _SMACK_RULES_H_ */