Add conditional statements for krate not supported
[platform/core/security/dpm-zone.git] / plugin / zone.cpp
1 /*
2  *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <sys/inotify.h>
19
20 #include <regex>
21 #include <algorithm>
22 #include <string>
23
24 #include <tzplatform_config.h>
25 #include <klay/auth/user.h>
26
27 #include <dpm/pil/policy-context.h>
28 #include <dpm/pil/policy-model.h>
29 #include <dpm/pil/policy-storage.h>
30
31 #ifndef KRATE_SUPPORTED
32 #include <dpm/pil/app-bundle.h>
33 #include <dpm/pil/launchpad.h>
34
35 #include <krate/krate.h>
36
37 #define NAME_PATTERN "^[A-Za-z_][A-Za-z0-9_.-]*"
38
39 namespace {
40
41 std::regex krateNamePattern(NAME_PATTERN);
42
43 bool foreachKrateCallback(const char* name, void* user_data)
44 {
45         auto pList = (std::vector<std::string>*)user_data;
46         pList->push_back(name);
47         return true;
48 }
49
50 bool isAllowedName(const std::string& name)
51 {
52         if (!std::regex_match(name, krateNamePattern)) {
53                 return false;
54         }
55
56         bool exists;
57         try {
58                 runtime::User user(name);
59                 exists = true;
60         } catch (runtime::Exception& e) {
61                 exists = false;
62         }
63
64         return !exists;
65 }
66
67 } // namespace
68 #endif
69
70 class Zone : public AbstractPolicyProvider {
71 public:
72         int create(const std::string& name, const std::string& setupWizAppid);
73         int remove(const std::string& name);
74         int getState(const std::string& name);
75         std::vector<std::string> enumerate(int state);
76 };
77
78 int Zone::create(const std::string& name, const std::string& setupWizAppid)
79 {
80 #ifndef KRATE_SUPPORTED
81         if (!isAllowedName(name)) {
82                 return TIZEN_ERROR_NOT_PERMITTED;
83         }
84
85         try {
86                 Bundle bundle;
87                 bundle.add("id", "krate-create");
88                 bundle.add("user-data", name);
89
90                 Launchpad launchpad(rmi::Service::getPeerUid());
91                 launchpad.launch("org.tizen.dpm-syspopup", bundle);
92         } catch (runtime::Exception& e) {
93                 ERROR(e.what());
94                 return -1;
95         }
96
97         return 0;
98 #else
99         return TIZEN_ERROR_NOT_PERMITTED;
100 #endif
101 }
102
103 int Zone::remove(const std::string& name)
104 {
105         if (getState(name) == 0) {
106                 return TIZEN_ERROR_NOT_PERMITTED;
107         }
108
109 #ifndef KRATE_SUPPORTED
110         try {
111                 Bundle bundle;
112                 bundle.add("id", "krate-remove");
113                 bundle.add("user-data", name);
114
115                 Launchpad launchpad(rmi::Service::getPeerUid());
116                 launchpad.launch("org.tizen.dpm-syspopup", bundle);
117         } catch (runtime::Exception& e) {
118                 ERROR(e.what());
119                 return -1;
120         }
121
122         return 0;
123 #else
124         return TIZEN_ERROR_NOT_PERMITTED;
125 #endif
126 }
127
128 int Zone::getState(const std::string& name)
129 {
130 #ifndef KRATE_SUPPORTED
131         krate_state_e state = (krate_state_e)0;
132         krate_manager_h krate_manager;
133
134         krate_manager_create(&krate_manager);
135         krate_manager_get_krate_state(krate_manager, name.c_str(), &state);
136         krate_manager_destroy(krate_manager);
137 #else
138         int state = 0;
139 #endif
140
141         return (int)state;
142 }
143
144 std::vector<std::string> Zone::enumerate(int state)
145 {
146         std::vector<std::string> list;
147 #ifndef KRATE_SUPPORTED
148         krate_manager_h krate_manager;
149
150         krate_manager_create(&krate_manager);
151         krate_manager_foreach_name(krate_manager, (krate_state_e)state, foreachKrateCallback, &list);
152         krate_manager_destroy(krate_manager);
153 #endif
154
155         return list;
156 }
157
158 extern "C" {
159
160 #define PRIVILEGE "http://tizen.org/privilege/dpm.zone"
161
162 AbstractPolicyProvider *PolicyFactory(PolicyControlContext& context)
163 {
164         Zone *policy = new Zone();
165
166         context.expose(policy, PRIVILEGE, (int)(Zone::create)(std::string, std::string));
167         context.expose(policy, PRIVILEGE, (int)(Zone::remove)(std::string));
168         context.expose(policy, "",        (int)(Zone::getState)(std::string));
169         context.expose(policy, "",        (std::vector<std::string>)(Zone::enumerate)(int));
170
171         return policy;
172 }
173
174 } // extern "C"