| 1 |
/* exported FlatpakPermissionsModel getDefault */ |
| 2 |
|
| 3 |
/* permissions.js |
| 4 |
* |
| 5 |
* Copyright 2020 Martin Abente Lahaye |
| 6 |
* |
| 7 |
* This program is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation, either version 3 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
const {Gio, GObject, GLib} = imports.gi; |
| 22 |
|
| 23 |
const {FlatpakUnsupportedModel} = imports.models.unsupported; |
| 24 |
const {FlatpakDevicesModel} = imports.models.devices; |
| 25 |
const {FlatpakSharedModel} = imports.models.shared; |
| 26 |
const {FlatpakSocketsModel} = imports.models.sockets; |
| 27 |
const {FlatpakFeaturesModel} = imports.models.features; |
| 28 |
const {FlatpakFilesystemsOtherModel} = imports.models.filesystemsOther; |
| 29 |
const {FlatpakVariablesModel} = imports.models.variables; |
| 30 |
const {FlatpakSessionBusModel} = imports.models.sessionBus; |
| 31 |
const {FlatpakSystemBusModel} = imports.models.systemBus; |
| 32 |
const {FlatsealOverrideStatus} = imports.models.overrideStatus; |
| 33 |
const {isGlobalOverride} = imports.models.globalModel; |
| 34 |
const {applications, filesystems, persistent, portals} = imports.models; |
| 35 |
|
| 36 |
const FLAGS = GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT; |
| 37 |
|
| 38 |
const MODELS = { |
| 39 |
shared: new FlatpakSharedModel(), |
| 40 |
sockets: new FlatpakSocketsModel(), |
| 41 |
devices: new FlatpakDevicesModel(), |
| 42 |
features: new FlatpakFeaturesModel(), |
| 43 |
filesystems: filesystems.getDefault(), |
| 44 |
filesystemsOther: new FlatpakFilesystemsOtherModel(), |
| 45 |
persistent: persistent.getDefault(), |
| 46 |
variables: new FlatpakVariablesModel(), |
| 47 |
system: new FlatpakSystemBusModel(), |
| 48 |
session: new FlatpakSessionBusModel(), |
| 49 |
portals: portals.getDefault(), |
| 50 |
unsupported: new FlatpakUnsupportedModel(), |
| 51 |
}; |
| 52 |
|
| 53 |
function generate_index() { |
| 54 |
const index = {}; |
| 55 |
|
| 56 |
Object.values(MODELS).forEach(model => { |
| 57 |
const group = model.constructor.getGroup(); |
| 58 |
const key = model.constructor.getKey(); |
| 59 |
const options = model.getOptions(); |
| 60 |
|
| 61 |
if (key === null) { |
| 62 |
index[`${group}`] = model; |
| 63 |
} else if (options === null) { |
| 64 |
index[`${group}_${key}`] = model; |
| 65 |
} else { |
| 66 |
options.forEach(option => { |
| 67 |
index[`${group}_${key}_${option}`] = model; |
| 68 |
}); |
| 69 |
} |
| 70 |
}); |
| 71 |
|
| 72 |
return index; |
| 73 |
} |
| 74 |
|
| 75 |
const INDEX = generate_index(); |
| 76 |
|
| 77 |
function generate() { |
| 78 |
const properties = {}; |
| 79 |
|
| 80 |
Object.values(MODELS).forEach(model => { |
| 81 |
Object.entries(model.getPermissions()).forEach(([property]) => { |
| 82 |
const value = model.constructor.getDefault(); |
| 83 |
|
| 84 |
if (typeof value === 'boolean') { |
| 85 |
properties[property] = GObject.ParamSpec.boolean( |
| 86 |
property, property, property, FLAGS, value); |
| 87 |
} else if (typeof value === 'string') { |
| 88 |
properties[property] = GObject.ParamSpec.string( |
| 89 |
property, property, property, FLAGS, value); |
| 90 |
} else if (typeof value === 'number') { |
| 91 |
const max = model.constructor.getMax(); |
| 92 |
const min = model.constructor.getMin(); |
| 93 |
properties[property] = GObject.ParamSpec.int( |
| 94 |
property, property, property, FLAGS, min, max, value); |
| 95 |
} else { |
| 96 |
logError(`No support for ${property}:${typeof value}`); |
| 97 |
} |
| 98 |
|
| 99 |
/* overrides status */ |
| 100 |
const statusProperty = `${property}-status`; |
| 101 |
properties[statusProperty] = GObject.ParamSpec.string( |
| 102 |
statusProperty, statusProperty, statusProperty, FLAGS, FlatsealOverrideStatus.ORIGINAL); |
| 103 |
}); |
| 104 |
}); |
| 105 |
|
| 106 |
return properties; |
| 107 |
} |
| 108 |
|
| 109 |
var DELAY = 500; |
| 110 |
|
| 111 |
var FlatpakPermissionsModel = GObject.registerClass({ |
| 112 |
GTypeName: 'FlatpakPermissionsModel', |
| 113 |
Properties: generate(), |
| 114 |
Signals: { |
| 115 |
changed: { |
| 116 |
param_types: [GObject.TYPE_BOOLEAN, GObject.TYPE_BOOLEAN], |
| 117 |
}, |
| 118 |
reset: {}, |
| 119 |
failed: {}, |
| 120 |
}, |
| 121 |
}, class FlatpakPermissionsModel extends GObject.Object { |
| 122 |
_init() { |
| 123 |
super._init({}); |
| 124 |
this._appId = ''; |
| 125 |
this._delayedHandlerId = 0; |
| 126 |
this._monitors = []; |
| 127 |
this._monitorsDelayedHandlerId = 0; |
| 128 |
this._changesByUser = 0; |
| 129 |
this._applications = applications.getDefault(); |
| 130 |
this._notifyHandlerId = this.connect('notify', this._delayedUpdate.bind(this)); |
| 131 |
this._ensureBaseOverridesPath(); |
| 132 |
} |
| 133 |
|
| 134 |
_ensureBaseOverridesPath() { |
| 135 |
const path = this._getBaseOverridesPath(); |
| 136 |
if (GLib.access(path, 0) !== 0) |
| 137 |
GLib.mkdir_with_parents(path, 0o0755); |
| 138 |
} |
| 139 |
|
| 140 |
_getBaseOverridesPath() { |
| 141 |
return GLib.build_filenamev([this._applications.userPath, 'overrides']); |
| 142 |
} |
| 143 |
|
| 144 |
_getMetadataPath() { |
| 145 |
return this._applications.getMetadataPathForAppId(this._appId); |
| 146 |
} |
| 147 |
|
| 148 |
_getGlobalOverridesPath() { |
| 149 |
return GLib.build_filenamev([this._getBaseOverridesPath(), 'global']); |
| 150 |
} |
| 151 |
|
| 152 |
_getOverridesPath() { |
| 153 |
return GLib.build_filenamev([this._getBaseOverridesPath(), this._appId]); |
| 154 |
} |
| 155 |
|
| 156 |
_loadPermissionsForPath(path, overrides, global) { |
| 157 |
if (GLib.access(path, 0) !== 0) |
| 158 |
return; |
| 159 |
|
| 160 |
const keyFile = new GLib.KeyFile(); |
| 161 |
|
| 162 |
try { |
| 163 |
keyFile.load_from_file(path, 0); |
| 164 |
} catch (err) { |
| 165 |
logError(err, `Could not load ${path}`); |
| 166 |
this.emit('failed'); |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
const [groups] = keyFile.get_groups(); |
| 171 |
|
| 172 |
groups.forEach(group => { |
| 173 |
const [keys] = keyFile.get_keys(group); |
| 174 |
|
| 175 |
keys.forEach(key => { |
| 176 |
const value = keyFile.get_value(group, key); |
| 177 |
|
| 178 |
/* First for models that process the value as a whole */ |
| 179 |
let model = this.constructor._find(`${group}`); |
| 180 |
|
| 181 |
if (model !== null) { |
| 182 |
model.loadFromKeyFile(group, key, value, overrides, global); |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
/* Then check models that process the value as individually */ |
| 187 |
const values = value |
| 188 |
.replace(/;+$/, '') |
| 189 |
.split(';'); |
| 190 |
|
| 191 |
values.forEach(option => { |
| 192 |
model = this.constructor._find(`${group}_${key}_${option.replace('!', '')}`); |
| 193 |
|
| 194 |
if (model === null) |
| 195 |
model = this.constructor._find(`${group}_${key}`); |
| 196 |
|
| 197 |
if (model === null && overrides && !global) |
| 198 |
model = MODELS.unsupported; |
| 199 |
|
| 200 |
if (model !== null) |
| 201 |
model.loadFromKeyFile(group, key, option, overrides, global); |
| 202 |
}); |
| 203 |
}); |
| 204 |
}); |
| 205 |
} |
| 206 |
|
| 207 |
_loadPermissions() { |
| 208 |
if (isGlobalOverride(this._appId)) |
| 209 |
return; |
| 210 |
|
| 211 |
this._loadPermissionsForPath(this._getMetadataPath(), false, false); |
| 212 |
} |
| 213 |
|
| 214 |
_loadGlobalOverrides() { |
| 215 |
if (isGlobalOverride(this._appId)) |
| 216 |
return; |
| 217 |
|
| 218 |
this._loadPermissionsForPath( |
| 219 |
this._getGlobalOverridesPath(), true, true); |
| 220 |
} |
| 221 |
|
| 222 |
_loadOverrides() { |
| 223 |
this._loadPermissionsForPath( |
| 224 |
this._getOverridesPath(), true, false); |
| 225 |
} |
| 226 |
|
| 227 |
_checkIfChanged() { |
| 228 |
const overrideExists = GLib.access(this._getOverridesPath(), 0) === 0; |
| 229 |
const portalsChanged = MODELS.portals.changed(); |
| 230 |
const changed = overrideExists || portalsChanged; |
| 231 |
const unsupported = !MODELS.unsupported.isEmpty(); |
| 232 |
this.emit('changed', changed, unsupported); |
| 233 |
} |
| 234 |
|
| 235 |
_saveOverrides() { |
| 236 |
const keyFile = new GLib.KeyFile(); |
| 237 |
|
| 238 |
Object.values(MODELS).forEach(model => model.saveToKeyFile(keyFile)); |
| 239 |
|
| 240 |
const [, length] = keyFile.to_data(); |
| 241 |
const path = this._getOverridesPath(); |
| 242 |
|
| 243 |
if (length === 0) |
| 244 |
GLib.unlink(path); |
| 245 |
else |
| 246 |
keyFile.save_to_file(path); |
| 247 |
|
| 248 |
this._checkIfChanged(); |
| 249 |
} |
| 250 |
|
| 251 |
_updateProperties() { |
| 252 |
GObject.signal_handler_block(this, this._notifyHandlerId); |
| 253 |
|
| 254 |
Object.values(MODELS).forEach(model => model.updateProxyProperty(this)); |
| 255 |
|
| 256 |
this._checkIfChanged(); |
| 257 |
|
| 258 |
GObject.signal_handler_unblock(this, this._notifyHandlerId); |
| 259 |
} |
| 260 |
|
| 261 |
_updateStatusProperties() { |
| 262 |
GObject.signal_handler_block(this, this._notifyHandlerId); |
| 263 |
|
| 264 |
Object.values(MODELS).forEach(model => model.updateStatusProperty(this)); |
| 265 |
|
| 266 |
GObject.signal_handler_unblock(this, this._notifyHandlerId); |
| 267 |
} |
| 268 |
|
| 269 |
_delayedUpdate() { |
| 270 |
if (this._delayedHandlerId !== 0) |
| 271 |
GLib.Source.remove(this._delayedHandlerId); |
| 272 |
|
| 273 |
this._delayedHandlerId = GLib.timeout_add( |
| 274 |
GLib.PRIORITY_HIGH, DELAY, this._updateModels.bind(this)); |
| 275 |
} |
| 276 |
|
| 277 |
_processPendingUpdates() { |
| 278 |
if (this._appId === '') |
| 279 |
return; |
| 280 |
|
| 281 |
if (this._delayedHandlerId === 0) |
| 282 |
return; |
| 283 |
|
| 284 |
GLib.Source.remove(this._delayedHandlerId); |
| 285 |
this._updateModels(); |
| 286 |
} |
| 287 |
|
| 288 |
_updateModels() { |
| 289 |
Object.values(MODELS).forEach(model => { |
| 290 |
Object.entries(model.getPermissions()).forEach(([property]) => { |
| 291 |
model.updateFromProxyProperty( |
| 292 |
property, |
| 293 |
this[property.replace(/-/g, '_')]); |
| 294 |
}); |
| 295 |
}); |
| 296 |
|
| 297 |
this._changesByUser += 1; |
| 298 |
this._saveOverrides(); |
| 299 |
this._updateStatusProperties(); |
| 300 |
|
| 301 |
this._delayedHandlerId = 0; |
| 302 |
return GLib.SOURCE_REMOVE; |
| 303 |
} |
| 304 |
|
| 305 |
_setup() { |
| 306 |
if (this._appId === '') |
| 307 |
return; |
| 308 |
|
| 309 |
Object.values(MODELS).forEach(model => model.reset()); |
| 310 |
this._loadPermissions(); |
| 311 |
this._loadGlobalOverrides(); |
| 312 |
this._loadOverrides(); |
| 313 |
this._updateProperties(); |
| 314 |
this._updateStatusProperties(); |
| 315 |
} |
| 316 |
|
| 317 |
_setupMonitors() { |
| 318 |
if (this._appId === '') |
| 319 |
return; |
| 320 |
|
| 321 |
let paths = [this._getGlobalOverridesPath()]; |
| 322 |
|
| 323 |
if (!isGlobalOverride(this._appId)) |
| 324 |
paths = [...paths, this._getMetadataPath(), this._getOverridesPath()]; |
| 325 |
|
| 326 |
paths.forEach(path => { |
| 327 |
const file = Gio.File.new_for_path(path); |
| 328 |
|
| 329 |
try { |
| 330 |
const monitor = file.monitor_file(Gio.FileMonitorFlags.WATCH_MOVES, null); |
| 331 |
monitor.connect('changed', this._delayMonitorsChanged.bind(this)); |
| 332 |
this._monitors.push(monitor); |
| 333 |
} catch (err) { |
| 334 |
logError(err); |
| 335 |
} |
| 336 |
}); |
| 337 |
} |
| 338 |
|
| 339 |
_cancelMonitors() { |
| 340 |
if (this._monitorsDelayedHandlerId !== 0) |
| 341 |
GLib.Source.remove(this._monitorsDelayedHandlerId); |
| 342 |
|
| 343 |
this._monitors.forEach(monitor => { |
| 344 |
monitor.cancel(); |
| 345 |
}); |
| 346 |
|
| 347 |
this._monitors = []; |
| 348 |
this._monitorsDelayedHandlerId = 0; |
| 349 |
this._changesByUser = 0; |
| 350 |
} |
| 351 |
|
| 352 |
_delayMonitorsChanged() { |
| 353 |
if (this._monitorsDelayedHandlerId !== 0) |
| 354 |
GLib.Source.remove(this._monitorsDelayedHandlerId); |
| 355 |
|
| 356 |
this._monitorsDelayedHandlerId = GLib.timeout_add( |
| 357 |
GLib.PRIORITY_DEFAULT, DELAY, this._updateFromMonitors.bind(this)); |
| 358 |
} |
| 359 |
|
| 360 |
_updateFromMonitors() { |
| 361 |
if (this._changesByUser === 0) |
| 362 |
this._setup(); |
| 363 |
else |
| 364 |
this._changesByUser -= 1; |
| 365 |
|
| 366 |
this._monitorsDelayedHandlerId = 0; |
| 367 |
return GLib.SOURCE_REMOVE; |
| 368 |
} |
| 369 |
|
| 370 |
getAll() { |
| 371 |
const list = []; |
| 372 |
|
| 373 |
Object.values(MODELS).forEach(model => { |
| 374 |
Object.entries(model.getPermissions()).forEach(([property, permission]) => { |
| 375 |
const entry = {}; |
| 376 |
|
| 377 |
entry['property'] = property; |
| 378 |
entry['description'] = permission.description; |
| 379 |
entry['value'] = this[property.replace(/-/g, '_')]; |
| 380 |
entry['type'] = model.constructor.getType(); |
| 381 |
entry['permission'] = permission.example; |
| 382 |
entry['supported'] = permission.supported; |
| 383 |
entry['portalTable'] = permission.table; |
| 384 |
entry['portalId'] = permission.id; |
| 385 |
entry['groupTitle'] = model.constructor.getTitle(); |
| 386 |
entry['groupStyle'] = model.constructor.getStyle(); |
| 387 |
entry['groupDescription'] = model.constructor.getDescription(); |
| 388 |
entry['statusProperty'] = `${property}-status`; |
| 389 |
entry['serializeFunc'] = model.constructor.serialize; |
| 390 |
entry['deserializeFunc'] = model.constructor.deserialize; |
| 391 |
|
| 392 |
list.push(entry); |
| 393 |
}); |
| 394 |
}); |
| 395 |
|
| 396 |
return list; |
| 397 |
} |
| 398 |
|
| 399 |
undo() { |
| 400 |
this._changesByUser += 1; |
| 401 |
const path = this._getOverridesPath(); |
| 402 |
this._backup.save_to_file(path); |
| 403 |
MODELS.portals.restore(); |
| 404 |
this._setup(); |
| 405 |
} |
| 406 |
|
| 407 |
backup() { |
| 408 |
this._backup = new GLib.KeyFile(); |
| 409 |
Object.values(MODELS).forEach(model => model.saveToKeyFile(this._backup)); |
| 410 |
MODELS.portals.backup(); |
| 411 |
} |
| 412 |
|
| 413 |
reset() { |
| 414 |
this._changesByUser += 1; |
| 415 |
this.backup(); |
| 416 |
const path = this._getOverridesPath(); |
| 417 |
GLib.unlink(path); |
| 418 |
MODELS.portals.forget(); |
| 419 |
this._setup(); |
| 420 |
this.emit('reset'); |
| 421 |
} |
| 422 |
|
| 423 |
shutdown() { |
| 424 |
this._processPendingUpdates(); |
| 425 |
this._cancelMonitors(); |
| 426 |
} |
| 427 |
|
| 428 |
reload() { |
| 429 |
this.shutdown(); |
| 430 |
this._setup(); |
| 431 |
this._setupMonitors(); |
| 432 |
} |
| 433 |
|
| 434 |
set appId(appId) { |
| 435 |
this._backup = null; |
| 436 |
this.shutdown(); |
| 437 |
this._appId = appId; |
| 438 |
MODELS.portals.appId = appId; |
| 439 |
this._setup(); |
| 440 |
this._setupMonitors(); |
| 441 |
} |
| 442 |
|
| 443 |
get appId() { |
| 444 |
return this._appId; |
| 445 |
} |
| 446 |
|
| 447 |
static _find(key) { |
| 448 |
return Object.hasOwn(INDEX, key) ? INDEX[key] : null; |
| 449 |
} |
| 450 |
|
| 451 |
/* testing */ |
| 452 |
|
| 453 |
static getGroupForProperty(property) { |
| 454 |
const [group] = property.split('-'); |
| 455 |
return MODELS[group].constructor.getGroup(); |
| 456 |
} |
| 457 |
}); |
| 458 |
|
| 459 |
|
| 460 |
var getDefault = (function() { |
| 461 |
let instance; |
| 462 |
return function() { |
| 463 |
if (typeof instance === 'undefined') |
| 464 |
instance = new FlatpakPermissionsModel(); |
| 465 |
return instance; |
| 466 |
}; |
| 467 |
}()); |