Debian Codegraph @

src/scripts/client/access-portal.lua

raw
1 MEDIA_ROLE_NONE = 0
2 MEDIA_ROLE_CAMERA = 1 << 0
3
4 log = Log.open_topic ("s-client")
5
6 function hasPermission (permissions, app_id, lookup)
7 if permissions then
8 for key, values in pairs(permissions) do
9 if key == app_id then
10 for _, v in pairs(values) do
11 if v == lookup then
12 return true
13 end
14 end
15 end
16 end
17 end
18 return false
19 end
20
21 function parseMediaRoles (media_roles_str)
22 local media_roles = MEDIA_ROLE_NONE
23 for role in media_roles_str:gmatch('[^,%s]+') do
24 if role == "Camera" then
25 media_roles = media_roles | MEDIA_ROLE_CAMERA
26 end
27 end
28 return media_roles
29 end
30
31 function setPermissions (client, allow_client, allow_nodes)
32 local client_id = client["bound-id"]
33 log:info(client, "Granting ALL access to client " .. client_id)
34
35 -- Update permissions on client
36 client:update_permissions { [client_id] = allow_client and "all" or "-" }
37
38 -- Update permissions on camera source nodes
39 for node in nodes_om:iterate() do
40 local node_id = node["bound-id"]
41 client:update_permissions { [node_id] = allow_nodes and "all" or "-" }
42 end
43 end
44
45 function updateClientPermissions (client, permissions)
46 local client_id = client["bound-id"]
47 local str_prop = nil
48 local app_id = nil
49 local media_roles = nil
50 local allowed = false
51
52 -- Make sure the client is not the portal itself
53 str_prop = client.properties["pipewire.access.portal.is_portal"]
54 if str_prop == "yes" then
55 log:info (client, "client is the portal itself")
56 return
57 end
58
59 -- Make sure the client has a portal app Id
60 str_prop = client.properties["pipewire.access.portal.app_id"]
61 if str_prop == nil then
62 log:info (client, "Portal managed client did not set app_id")
63 return
64 end
65 if str_prop == "" then
66 log:info (client, "Ignoring portal check for non-sandboxed client")
67 setPermissions (client, true, true)
68 return
69 end
70 app_id = str_prop
71
72 -- Make sure the client has portal media roles
73 str_prop = client.properties["pipewire.access.portal.media_roles"]
74 if str_prop == nil then
75 log:info (client, "Portal managed client did not set media_roles")
76 return
77 end
78 media_roles = parseMediaRoles (str_prop)
79 if (media_roles & MEDIA_ROLE_CAMERA) == 0 then
80 log:info (client, "Ignoring portal check for clients without camera role")
81 return
82 end
83
84 -- Update permissions
85 allowed = hasPermission (permissions, app_id, "yes")
86
87 log:info (client, "setting permissions: " .. tostring(allowed))
88 setPermissions (client, allowed, allowed)
89 end
90
91 -- Create portal clients object manager
92 clients_om = ObjectManager {
93 Interest {
94 type = "client",
95 Constraint { "pipewire.access", "=", "portal" },
96 }
97 }
98
99 -- Set permissions to portal clients from the permission store if loaded
100 pps_plugin = Plugin.find("portal-permissionstore")
101 if pps_plugin then
102 nodes_om = ObjectManager {
103 Interest {
104 type = "node",
105 Constraint { "media.role", "=", "Camera" },
106 Constraint { "media.class", "=", "Video/Source" },
107 }
108 }
109 nodes_om:activate()
110
111 clients_om:connect("object-added", function (om, client)
112 local new_perms = pps_plugin:call("lookup", "devices", "camera");
113 updateClientPermissions (client, new_perms)
114 end)
115
116 nodes_om:connect("object-added", function (om, node)
117 local new_perms = pps_plugin:call("lookup", "devices", "camera");
118 for client in clients_om:iterate() do
119 updateClientPermissions (client, new_perms)
120 end
121 end)
122
123 pps_plugin:connect("changed", function (p, table, id, deleted, permissions)
124 if table == "devices" or id == "camera" then
125 for app_id, _ in pairs(permissions) do
126 for client in clients_om:iterate {
127 Constraint { "pipewire.access.portal.app_id", "=", app_id }
128 } do
129 updateClientPermissions (client, permissions)
130 end
131 end
132 end
133 end)
134 else
135 -- Otherwise, just set all permissions to all portal clients
136 clients_om:connect("object-added", function (om, client)
137 local id = client["bound-id"]
138 log:info(client, "Granting ALL access to client " .. id)
139 client:update_permissions { ["any"] = "all" }
140 end)
141 end
142
143 clients_om:activate()