policy-default: Use libsmack to check client label
[src/agl-compositor.git] / src / policy-default.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
35 /*
36  * default policy implementation allows every action to be possible
37  *
38  * This is an example, that implements the API
39  *
40  * For injecting rules back in the compositor one should use ivi_policy_add()
41  * - policy_rule_allow_to_add is required in order to add further policy rules
42  * - policy_rule_try_event will be callback executed when handling the state
43  *   change
44  */
45 static bool
46 ivi_policy_default_surface_create(struct ivi_surface *surf, void *user_data)
47 {
48         /* verify that the surface should be created */
49         return true;
50 }
51
52 static bool
53 ivi_policy_default_surface_commmited(struct ivi_surface *surf, void *user_data)
54 {
55         /* verify that the surface should be committed */
56         return true;
57 }
58
59 static bool
60 ivi_policy_default_surface_activate(struct ivi_surface *surf, void *user_data)
61 {
62         /* verify that the surface should be switched to */
63         return true;
64 }
65
66 static bool
67 ivi_policy_default_surface_deactivate(struct ivi_surface *surf, void *user_data)
68 {
69         /* verify that the surface should be de-activated to */
70         return true;
71 }
72
73 static bool
74 ivi_policy_default_surface_activate_default(struct ivi_surface *surf, void *user_data)
75 {
76         /* verify that the surface should be switched to */
77         return true;
78 }
79
80 static bool
81 ivi_policy_default_surface_advertise_state_change(struct ivi_surface *surf, void *user_data)
82 {
83         /* verify that the surface should sent as notification */
84         return true;
85 }
86
87 #ifdef HAVE_SMACK
88 static bool
89 ivi_policy_default_shell_bind_interface(void *client, void *interface)
90 {
91         struct wl_interface *shell_interface = interface;
92         struct wl_client *conn_client = client;
93
94         pid_t pid, uid, gid;
95         int client_fd;
96         char *label;
97         bool ret = false;
98
99         wl_client_get_credentials(conn_client, &pid, &uid, &gid);
100
101         client_fd = wl_client_get_fd(conn_client);
102         if (smack_new_label_from_socket(client_fd, &label) < 0) {
103                 return ret;
104         }
105
106         if (strcmp(shell_interface->name, "agl_shell") == 0)
107                 if (strcmp(label, "User::App::homescreen") == 0)
108                         ret = true;
109
110         if (strcmp(shell_interface->name, "agl_shell_desktop") == 0)
111                 if (strcmp(label, "User::App::launcher") == 0 ||
112                     strcmp(label, "User::App::alexa-viewer") == 0 ||
113                     strcmp(label, "User::App::tbtnavi") == 0 ||
114                     strcmp(label, "User::App::hvac") == 0)
115                         ret = true;
116
117         if (ret)
118                 weston_log("Client with pid %d, uid %d, gid %d, allowed "
119                                 "to bind to %s for label %s\n", pid, uid, gid,
120                                 shell_interface->name, label);
121
122         /* client responsible for free'ing */
123         free(label);
124         return ret;
125 }
126 #else
127 static bool
128 ivi_policy_default_shell_bind_interface(void *client, void *interface)
129 {
130         return true;
131 }
132 #endif
133
134 static bool
135 ivi_policy_default_allow_to_add(void *user_data)
136 {
137         /* verify that policy rules can be added with ivi_policy_add() */
138         return true;
139 }
140
141 /*
142  * Policy rules added by ivi_policy_add() will be handled by this callback, and
143  * should be treated depending on the event. Note this is just an example.
144  */
145 static void
146 ivi_policy_default_try_event(struct ivi_a_policy *a_policy)
147 {
148         uint32_t event = a_policy->event;
149
150         switch (event) {
151         case AGL_SHELL_POLICY_EVENT_SHOW:
152                 ivi_layout_activate(a_policy->output, a_policy->app_id);
153                 break;
154         case AGL_SHELL_POLICY_EVENT_HIDE:
155                 ivi_layout_deactivate(a_policy->policy->ivi, a_policy->app_id);
156         default:
157                 break;
158         }
159 }
160
161 static const struct ivi_policy_api policy_api = {
162         .struct_size = sizeof(policy_api),
163         .surface_create = ivi_policy_default_surface_create,
164         .surface_commited = ivi_policy_default_surface_commmited,
165         .surface_activate = ivi_policy_default_surface_activate,
166         .surface_deactivate = ivi_policy_default_surface_deactivate,
167         .surface_activate_by_default = ivi_policy_default_surface_activate_default,
168         .surface_advertise_state_change = ivi_policy_default_surface_advertise_state_change,
169         .shell_bind_interface = ivi_policy_default_shell_bind_interface,
170         .policy_rule_allow_to_add = ivi_policy_default_allow_to_add,
171         .policy_rule_try_event = ivi_policy_default_try_event,
172 };
173
174 int
175 ivi_policy_init(struct ivi_compositor *ivi)
176 {
177         ivi->policy = ivi_policy_create(ivi, &policy_api, ivi);
178         if (!ivi->policy)
179                 return -1;
180
181         weston_log("Installing 'allow-all' policy engine\n");
182         return 0;
183 }