| 1 |
-- Manage snap audio permissions |
| 2 |
-- |
| 3 |
-- Copyright © 2023 Canonical Ltd. |
| 4 |
-- @author Sergio Costas Rodriguez <[email protected]> |
| 5 |
-- |
| 6 |
-- SPDX-License-Identifier: MIT |
| 7 |
|
| 8 |
function removeClientPermissionsForOtherClients (client) |
| 9 |
-- Remove access to any other clients, but allow all the process of the |
| 10 |
-- same snap to access their elements |
| 11 |
local client_id = client.properties["pipewire.snap.id"] |
| 12 |
for snap_client in clients_snap:iterate() do |
| 13 |
local snap_client_id = snap_client.properties["pipewire.snap.id"] |
| 14 |
if snap_client_id ~= client_id then |
| 15 |
client:update_permissions { [snap_client["bound-id"]] = "-" } |
| 16 |
end |
| 17 |
end |
| 18 |
for no_snap_client in clients_no_snap:iterate() do |
| 19 |
client:update_permissions { [no_snap_client["bound-id"]] = "-" } |
| 20 |
end |
| 21 |
end |
| 22 |
|
| 23 |
function updateClientPermissions (client) |
| 24 |
-- Remove access to Audio/Sources and Audio/Sinks based on snap permissions |
| 25 |
for node in nodes_om:iterate() do |
| 26 |
local node_id = node["bound-id"] |
| 27 |
local property = "pipewire.snap.audio.playback" |
| 28 |
|
| 29 |
if node.properties["media.class"] == "Audio/Source" then |
| 30 |
property = "pipewire.snap.audio.record" |
| 31 |
end |
| 32 |
|
| 33 |
if client.properties[property] ~= "true" then |
| 34 |
client:update_permissions { [node_id] = "-" } |
| 35 |
end |
| 36 |
end |
| 37 |
end |
| 38 |
|
| 39 |
clients_snap = ObjectManager { |
| 40 |
Interest { |
| 41 |
type = "client", |
| 42 |
Constraint { "pipewire.snap.id", "+", type = "pw"}, |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
clients_no_snap = ObjectManager { |
| 47 |
Interest { |
| 48 |
type = "client", |
| 49 |
Constraint { "pipewire.snap.id", "-", type = "pw"}, |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
nodes_om = ObjectManager { |
| 54 |
Interest { |
| 55 |
type = "node", |
| 56 |
Constraint { "media.class", "matches", "Audio/*"} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
clients_snap:connect("object-added", function (om, client) |
| 61 |
-- If a new snap client is added, adjust its permissions |
| 62 |
updateClientPermissions (client) |
| 63 |
removeClientPermissionsForOtherClients (client) |
| 64 |
end) |
| 65 |
|
| 66 |
clients_no_snap:connect("object-added", function (om, client) |
| 67 |
-- If a new, non-snap client is added, |
| 68 |
-- remove access to it from other snaps |
| 69 |
client_id = client["bound-id"] |
| 70 |
for snap_client in clients_snap:iterate() do |
| 71 |
if client.properties["pipewire.snap.id"] ~= nil then |
| 72 |
snap_client:update_permissions { [client_id] = "-" } |
| 73 |
end |
| 74 |
end |
| 75 |
end) |
| 76 |
|
| 77 |
nodes_om:connect("object-added", function (om, node) |
| 78 |
-- If a new Audio/Sink or Audio/Source node is added, |
| 79 |
-- adjust the permissions in the snap clients |
| 80 |
for client in clients_snap:iterate() do |
| 81 |
updateClientPermissions (client) |
| 82 |
end |
| 83 |
end) |
| 84 |
|
| 85 |
clients_snap:activate() |
| 86 |
clients_no_snap:activate() |
| 87 |
nodes_om:activate() |