input: Fix minor issue while reporting disable cursor
[src/agl-compositor.git] / src / policy-deny.c
1 /*
2  * Copyright © 2020 Collabora, Ltd.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include "ivi-compositor.h"
27 #include "policy.h"
28
29 #ifdef HAVE_SMACK
30 #include <sys/smack.h>
31 #endif
32
33 #include <string.h>
34 #include "shared/helpers.h"
35
36 #ifdef HAVE_SMACK
37 static const char *const bind_agl_shell[] = {
38         "User::App::homescreen",
39         "User::App::cluster-gauges"     /* cluster-dashboard */
40 };
41
42 static const char *const bind_agl_shell_desktop[] = {
43         "User::App::launcher",
44         "User::App::alexa-viewer",
45         "User::App::tbtnavi",
46         "User::App::hvac",
47         "User::App::xdg-cluster-receiver",              /* cluster-receiver, native XDG app*/
48         "User::App::cluster-receiver"                   /* cluster-receiver, Qt app  */
49 };
50 #endif
51
52 static const char *const applications_permitted[] = {
53         "homescreen", "alexa-viewer", "launcher", "hvac",
54         "navigation", "mediaplayer"
55 };
56
57 /* helper start searches the applications_permitted for the
58  * app_id
59  */
60 static bool
61 ivi_policy_verify_permitted_app(const char *app_id)
62 {
63         for (size_t i = 0; i < ARRAY_LENGTH(applications_permitted); i++)
64                 if (strcmp(app_id, applications_permitted[i]) == 0)
65                         return true;
66
67         return false;
68 }
69
70 #ifdef HAVE_SMACK
71 /* helper to determine which applications are allowed to bind to the
72  * private extensions
73  */
74 static bool
75 ivi_policy_check_bind_agl_shell(const char *app_id)
76 {
77         for (size_t i = 0; i < ARRAY_LENGTH(bind_agl_shell); i++)
78                 if (strcmp(app_id, bind_agl_shell[i]) == 0)
79                         return true;
80
81         return false;
82 }
83
84 static bool
85 ivi_policy_check_bind_agl_shell_desktop(const char *app_id)
86 {
87         for (size_t i = 0; i < ARRAY_LENGTH(bind_agl_shell_desktop); i++)
88                 if (strcmp(app_id, bind_agl_shell_desktop[i]) == 0)
89                         return true;
90
91         return false;
92 }
93 #endif
94
95 static bool
96 ivi_policy_verify_ivi_surface(struct ivi_surface *surf)
97 {
98         const char *app_id = weston_desktop_surface_get_app_id(surf->dsurface);
99         return ivi_policy_verify_permitted_app(app_id);
100 }
101
102 /*
103  * deny-all policy implementation allows every action to be denied
104  *
105  * This is an example, that implements the API
106  *
107  * For injecting rules back in the compositor one should use ivi_policy_add()
108  * - policy_rule_allow_to_add is required in order to add further policy rules
109  * - policy_rule_try_event will be callback executed when handling the state
110  *   change
111  */
112 static bool
113 ivi_policy_default_surface_create(struct ivi_surface *surf, void *user_data)
114 {
115         return ivi_policy_verify_ivi_surface(surf);
116 }
117
118 static bool
119 ivi_policy_default_surface_commmited(struct ivi_surface *surf, void *user_data)
120 {
121         return ivi_policy_verify_ivi_surface(surf);
122 }
123
124 static bool
125 ivi_policy_default_surface_activate(struct ivi_surface *surf, void *user_data)
126 {
127         return ivi_policy_verify_ivi_surface(surf);
128 }
129
130 static bool
131 ivi_policy_default_surface_deactivate(struct ivi_surface *surf, void *user_data)
132 {
133         return ivi_policy_verify_ivi_surface(surf);
134 }
135
136 static bool
137 ivi_policy_default_surface_activate_default(struct ivi_surface *surf, void *user_data)
138 {
139         return ivi_policy_verify_ivi_surface(surf);
140 }
141
142 static bool
143 ivi_policy_default_surface_advertise_state_change(struct ivi_surface *surf, void *user_data)
144 {
145         return ivi_policy_verify_ivi_surface(surf);
146 }
147
148 #ifdef HAVE_SMACK
149 static bool
150 ivi_policy_default_shell_bind_interface(void *client, void *interface)
151 {
152         struct wl_interface *shell_interface = interface;
153         struct wl_client *conn_client = client;
154
155         pid_t pid, uid, gid;
156         int client_fd;
157         char *label;
158         bool ret = false;
159
160         wl_client_get_credentials(conn_client, &pid, &uid, &gid);
161
162         client_fd = wl_client_get_fd(conn_client);
163         if (smack_new_label_from_socket(client_fd, &label) < 0) {
164                 return ret;
165         }
166
167         if (strcmp(shell_interface->name, "agl_shell") == 0)
168                 ret = ivi_policy_check_bind_agl_shell(label);
169
170         if (strcmp(shell_interface->name, "agl_shell_desktop") == 0)
171                 ret = ivi_policy_check_bind_agl_shell_desktop(label);
172
173         if (ret)
174                 weston_log("Client with pid %d, uid %d, gid %d, allowed "
175                                 "to bind to %s for label %s\n", pid, uid, gid,
176                                 shell_interface->name, label);
177
178         /* client responsible for free'ing */
179         free(label);
180         return ret;
181 }
182 #else
183 static bool
184 ivi_policy_default_shell_bind_interface(void *client, void *interface)
185 {
186         return false;
187 }
188 #endif
189
190 static bool
191 ivi_policy_default_allow_to_add(void *user_data)
192 {
193         /* verify that policy rules can be added with ivi_policy_add() */
194         return true;
195 }
196
197 /*
198  * Policy rules added by ivi_policy_add() will be handled by this callback, and
199  * should be treated depending on the event. Note this is just an example.
200  */
201 static void
202 ivi_policy_default_try_event(struct ivi_a_policy *a_policy)
203 {
204         uint32_t event = a_policy->event;
205
206         switch (event) {
207         case AGL_SHELL_POLICY_EVENT_SHOW:
208                 ivi_layout_activate(a_policy->output, a_policy->app_id);
209                 break;
210         case AGL_SHELL_POLICY_EVENT_HIDE:
211                 ivi_layout_deactivate(a_policy->policy->ivi, a_policy->app_id);
212         default:
213                 break;
214         }
215 }
216
217 static const struct ivi_policy_api policy_api = {
218         .struct_size = sizeof(policy_api),
219         .surface_create = ivi_policy_default_surface_create,
220         .surface_commited = ivi_policy_default_surface_commmited,
221         .surface_activate = ivi_policy_default_surface_activate,
222         .surface_deactivate = ivi_policy_default_surface_deactivate,
223         .surface_activate_by_default = ivi_policy_default_surface_activate_default,
224         .surface_advertise_state_change = ivi_policy_default_surface_advertise_state_change,
225         .shell_bind_interface = ivi_policy_default_shell_bind_interface,
226         .policy_rule_allow_to_add = ivi_policy_default_allow_to_add,
227         .policy_rule_try_event = ivi_policy_default_try_event,
228 };
229
230 int
231 ivi_policy_init(struct ivi_compositor *ivi)
232 {
233         ivi->policy = ivi_policy_create(ivi, &policy_api, ivi);
234         if (!ivi->policy)
235                 return -1;
236
237         weston_log("Installing 'deny-all' policy engine\n");
238         return 0;
239 }