Add zone policy
[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
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <sys/inotify.h>
20
21 #include <regex>
22 #include <algorithm>
23 #include <string>
24
25 #include <tzplatform_config.h>
26 #include <klay/auth/user.h>
27 #include <krate/krate.h>
28
29 #include <dpm/pil/policy-context.h>
30 #include <dpm/pil/policy-model.h>
31 #include <dpm/pil/policy-storage.h>
32 #include <dpm/pil/app-bundle.h>
33 #include <dpm/pil/launchpad.h>
34
35 #define NAME_PATTERN "^[A-Za-z_][A-Za-z0-9_.-]*"
36
37 namespace {
38
39 std::regex krateNamePattern(NAME_PATTERN);
40
41 bool foreachKrateCallback(const char* name, void* user_data)
42 {
43         auto pList = (std::vector<std::string>*)user_data;
44         pList->push_back(name);
45         return true;
46 }
47
48 bool isAllowedName(const std::string& name)
49 {
50         if (!std::regex_match(name, krateNamePattern)) {
51                 return false;
52         }
53
54         bool exists;
55         try {
56                 runtime::User user(name);
57                 exists = true;
58         } catch (runtime::Exception& e) {
59                 exists = false;
60         }
61
62         return !exists;
63 }
64
65 } // namespace
66
67
68 class Zone : public AbstractPolicyProvider {
69 public:
70         int create(const std::string& name, const std::string& setupWizAppid);
71         int remove(const std::string& name);
72         int getState(const std::string& name);
73         std::vector<std::string> enumerate(int state);
74 };
75
76 int Zone::create(const std::string& name, const std::string& setupWizAppid)
77 {
78         if (!isAllowedName(name)) {
79                 return -1;
80         }
81
82         try {
83                 Bundle bundle;
84                 bundle.add("id", "krate-create");
85                 bundle.add("user-data", name);
86
87                 Launchpad launchpad(rmi::Service::getPeerUid());
88                 launchpad.launch("org.tizen.dpm-syspopup", bundle);
89         } catch (runtime::Exception& e) {
90                 ERROR(e.what());
91                 return -1;
92         }
93
94         return 0;
95 }
96
97 int Zone::remove(const std::string& name)
98 {
99         if (getState(name) == 0) {
100                 return -1;
101         }
102
103         try {
104                 Bundle bundle;
105                 bundle.add("id", "krate-remove");
106                 bundle.add("user-data", name);
107
108                 Launchpad launchpad(rmi::Service::getPeerUid());
109                 launchpad.launch("org.tizen.dpm-syspopup", bundle);
110         } catch (runtime::Exception& e) {
111                 ERROR(e.what());
112                 return -1;
113         }
114
115         return 0;
116 }
117
118 int Zone::getState(const std::string& name)
119 {
120         krate_state_e state = (krate_state_e)0;
121         krate_manager_h krate_manager;
122
123         krate_manager_create(&krate_manager);
124         krate_manager_get_krate_state(krate_manager, name.c_str(), &state);
125         krate_manager_destroy(krate_manager);
126
127         return (int)state;
128 }
129
130 std::vector<std::string> Zone::enumerate(int state)
131 {
132         std::vector<std::string> list;
133         krate_manager_h krate_manager;
134
135         krate_manager_create(&krate_manager);
136         krate_manager_foreach_name(krate_manager, (krate_state_e)state, foreachKrateCallback, &list);
137         krate_manager_destroy(krate_manager);
138
139         return list;
140 }
141
142 extern "C" {
143
144 #define PRIVILEGE "http://tizen.org/privilege/dpm.zone"
145
146 AbstractPolicyProvider *PolicyFactory(PolicyControlContext& context)
147 {
148         Zone *policy = new Zone();
149
150         context.expose(policy, PRIVILEGE, (int)(Zone::create)(std::string, std::string));
151         context.expose(policy, PRIVILEGE, (int)(Zone::remove)(std::string));
152         context.expose(policy, "",        (int)(Zone::getState)(std::string));
153         context.expose(policy, "",        (std::vector<std::string>)(Zone::enumerate)(int));
154
155         return policy;
156 }
157
158 } // extern "C"