2018-10-02 18:03:33 +02:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
# Copyright © 2018 Endless Mobile, Inc.
|
|
|
|
|
#
|
|
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
|
# along with this library; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
|
import os
|
|
|
|
|
import pwd
|
|
|
|
|
import sys
|
|
|
|
|
import gi
|
2019-02-26 18:43:56 +01:00
|
|
|
|
gi.require_version('Malcontent', '0') # noqa
|
|
|
|
|
from gi.repository import Malcontent, GLib
|
2018-10-02 18:03:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Exit codes, which are a documented part of the API.
|
|
|
|
|
EXIT_SUCCESS = 0
|
|
|
|
|
EXIT_INVALID_OPTION = 1
|
|
|
|
|
EXIT_PERMISSION_DENIED = 2
|
|
|
|
|
EXIT_PATH_NOT_ALLOWED = 3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __get_app_filter(user_id, interactive):
|
|
|
|
|
"""Get the app filter for `user_id` off the bus.
|
|
|
|
|
|
|
|
|
|
If `interactive` is `True`, interactive polkit authorisation dialogues will
|
|
|
|
|
be allowed. An exception will be raised on failure."""
|
2019-03-19 17:55:11 +01:00
|
|
|
|
if interactive:
|
|
|
|
|
flags = Malcontent.GetAppFilterFlags.INTERACTIVE
|
|
|
|
|
else:
|
|
|
|
|
flags = Malcontent.GetAppFilterFlags.NONE
|
|
|
|
|
|
2019-02-26 18:43:56 +01:00
|
|
|
|
return Malcontent.get_app_filter(
|
2018-10-02 18:03:33 +02:00
|
|
|
|
connection=None, user_id=user_id,
|
2019-03-19 17:55:11 +01:00
|
|
|
|
flags=flags, cancellable=None)
|
2018-10-02 18:03:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __get_app_filter_or_error(user_id, interactive):
|
|
|
|
|
"""Wrapper around __get_app_filter() which prints an error and raises
|
|
|
|
|
SystemExit, rather than an internal exception."""
|
|
|
|
|
try:
|
|
|
|
|
return __get_app_filter(user_id, interactive)
|
|
|
|
|
except GLib.Error as e:
|
|
|
|
|
print('Error getting app filter for user {}: {}'.format(
|
|
|
|
|
user_id, e.message), file=sys.stderr)
|
|
|
|
|
raise SystemExit(EXIT_PERMISSION_DENIED)
|
|
|
|
|
|
|
|
|
|
|
2018-10-12 05:48:29 +02:00
|
|
|
|
def __set_app_filter(user_id, app_filter, interactive):
|
|
|
|
|
"""Set the app filter for `user_id` off the bus.
|
|
|
|
|
|
|
|
|
|
If `interactive` is `True`, interactive polkit authorisation dialogues will
|
|
|
|
|
be allowed. An exception will be raised on failure."""
|
2019-03-19 17:55:11 +01:00
|
|
|
|
if interactive:
|
|
|
|
|
flags = Malcontent.GetAppFilterFlags.INTERACTIVE
|
|
|
|
|
else:
|
|
|
|
|
flags = Malcontent.GetAppFilterFlags.NONE
|
|
|
|
|
|
2019-02-26 18:43:56 +01:00
|
|
|
|
Malcontent.set_app_filter(
|
2018-10-12 05:48:29 +02:00
|
|
|
|
connection=None, user_id=user_id, app_filter=app_filter,
|
2019-03-19 17:55:11 +01:00
|
|
|
|
flags=flags, cancellable=None)
|
2018-10-12 05:48:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __set_app_filter_or_error(user_id, app_filter, interactive):
|
|
|
|
|
"""Wrapper around __set_app_filter() which prints an error and raises
|
|
|
|
|
SystemExit, rather than an internal exception."""
|
|
|
|
|
try:
|
|
|
|
|
__set_app_filter(user_id, app_filter, interactive)
|
|
|
|
|
except GLib.Error as e:
|
|
|
|
|
print('Error setting app filter for user {}: {}'.format(
|
|
|
|
|
user_id, e.message), file=sys.stderr)
|
|
|
|
|
raise SystemExit(EXIT_PERMISSION_DENIED)
|
|
|
|
|
|
|
|
|
|
|
2018-10-02 18:03:33 +02:00
|
|
|
|
def __lookup_user_id(user):
|
|
|
|
|
"""Convert a command-line specified username or ID into a user ID. If
|
|
|
|
|
`user` is empty, use the current user ID.
|
|
|
|
|
|
|
|
|
|
Raise KeyError if lookup fails."""
|
|
|
|
|
if user == '':
|
|
|
|
|
return os.getuid()
|
|
|
|
|
elif user.isdigit():
|
|
|
|
|
return int(user)
|
|
|
|
|
else:
|
|
|
|
|
return pwd.getpwnam(user).pw_uid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __lookup_user_id_or_error(user):
|
|
|
|
|
"""Wrapper around __lookup_user_id() which prints an error and raises
|
|
|
|
|
SystemExit, rather than an internal exception."""
|
|
|
|
|
try:
|
|
|
|
|
return __lookup_user_id(user)
|
|
|
|
|
except KeyError:
|
|
|
|
|
print('Error getting ID for username {}'.format(user), file=sys.stderr)
|
|
|
|
|
raise SystemExit(EXIT_INVALID_OPTION)
|
|
|
|
|
|
|
|
|
|
|
2018-10-12 05:48:29 +02:00
|
|
|
|
oars_value_mapping = {
|
2019-02-26 18:43:56 +01:00
|
|
|
|
Malcontent.AppFilterOarsValue.UNKNOWN: "unknown",
|
|
|
|
|
Malcontent.AppFilterOarsValue.NONE: "none",
|
|
|
|
|
Malcontent.AppFilterOarsValue.MILD: "mild",
|
|
|
|
|
Malcontent.AppFilterOarsValue.MODERATE: "moderate",
|
|
|
|
|
Malcontent.AppFilterOarsValue.INTENSE: "intense",
|
2018-10-12 05:48:29 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2018-10-09 12:25:39 +02:00
|
|
|
|
def __oars_value_to_string(value):
|
2019-02-26 18:43:56 +01:00
|
|
|
|
"""Convert an Malcontent.AppFilterOarsValue to a human-readable
|
2018-10-09 12:25:39 +02:00
|
|
|
|
string."""
|
|
|
|
|
try:
|
2018-10-12 05:48:29 +02:00
|
|
|
|
return oars_value_mapping[value]
|
2018-10-09 12:25:39 +02:00
|
|
|
|
except KeyError:
|
|
|
|
|
return "invalid (OARS value {})".format(value)
|
|
|
|
|
|
|
|
|
|
|
2018-10-12 05:48:29 +02:00
|
|
|
|
def __oars_value_from_string(value_str):
|
|
|
|
|
"""Convert a human-readable string to an
|
2019-02-26 18:43:56 +01:00
|
|
|
|
Malcontent.AppFilterOarsValue."""
|
2018-10-12 05:48:29 +02:00
|
|
|
|
for k, v in oars_value_mapping.items():
|
|
|
|
|
if v == value_str:
|
|
|
|
|
return k
|
|
|
|
|
raise KeyError('Unknown OARS value ‘{}’'.format(value_str))
|
|
|
|
|
|
|
|
|
|
|
2018-10-02 18:03:33 +02:00
|
|
|
|
def command_get(user, quiet=False, interactive=True):
|
|
|
|
|
"""Get the app filter for the given user."""
|
|
|
|
|
user_id = __lookup_user_id_or_error(user)
|
2018-11-13 12:46:24 +01:00
|
|
|
|
app_filter = __get_app_filter_or_error(user_id, interactive)
|
|
|
|
|
|
|
|
|
|
print('App filter for user {} retrieved:'.format(user_id))
|
2018-10-02 18:03:33 +02:00
|
|
|
|
|
2018-11-13 12:46:24 +01:00
|
|
|
|
sections = app_filter.get_oars_sections()
|
|
|
|
|
for section in sections:
|
|
|
|
|
value = app_filter.get_oars_value(section)
|
|
|
|
|
print(' {}: {}'.format(section, oars_value_mapping[value]))
|
|
|
|
|
if not sections:
|
|
|
|
|
print(' (No OARS values)')
|
|
|
|
|
|
2018-11-29 22:09:57 +01:00
|
|
|
|
if app_filter.is_user_installation_allowed():
|
|
|
|
|
print('App installation is allowed to user repository')
|
|
|
|
|
else:
|
|
|
|
|
print('App installation is disallowed to user repository')
|
|
|
|
|
|
2018-11-28 17:39:39 +01:00
|
|
|
|
if app_filter.is_system_installation_allowed():
|
|
|
|
|
print('App installation is allowed to system repository')
|
2018-11-13 12:46:24 +01:00
|
|
|
|
else:
|
2018-11-28 17:39:39 +01:00
|
|
|
|
print('App installation is disallowed to system repository')
|
2018-10-02 18:03:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def command_check(user, path, quiet=False, interactive=True):
|
2018-10-12 06:59:04 +02:00
|
|
|
|
"""Check the given path or flatpak ref is runnable by the given user,
|
|
|
|
|
according to their app filter."""
|
2018-10-02 18:03:33 +02:00
|
|
|
|
user_id = __lookup_user_id_or_error(user)
|
|
|
|
|
app_filter = __get_app_filter_or_error(user_id, interactive)
|
|
|
|
|
|
2018-11-14 16:17:18 +01:00
|
|
|
|
if path.startswith('app/') and path.count('/') < 3:
|
|
|
|
|
# Flatpak app ID
|
|
|
|
|
path = path[4:]
|
|
|
|
|
is_allowed = app_filter.is_flatpak_app_allowed(path)
|
|
|
|
|
noun = 'Flatpak app ID'
|
|
|
|
|
elif path.startswith('app/') or path.startswith('runtime/'):
|
2018-10-12 06:59:04 +02:00
|
|
|
|
# Flatpak ref
|
|
|
|
|
is_allowed = app_filter.is_flatpak_ref_allowed(path)
|
|
|
|
|
noun = 'Flatpak ref'
|
|
|
|
|
else:
|
|
|
|
|
# File system path
|
|
|
|
|
path = os.path.abspath(path)
|
|
|
|
|
is_allowed = app_filter.is_path_allowed(path)
|
|
|
|
|
noun = 'Path'
|
|
|
|
|
|
|
|
|
|
if is_allowed:
|
|
|
|
|
print('{} {} is allowed by app filter for user {}'.format(
|
|
|
|
|
noun, path, user_id))
|
2018-10-02 18:03:33 +02:00
|
|
|
|
return
|
|
|
|
|
else:
|
2018-10-12 06:59:04 +02:00
|
|
|
|
print('{} {} is not allowed by app filter for user {}'.format(
|
|
|
|
|
noun, path, user_id))
|
2018-10-02 18:03:33 +02:00
|
|
|
|
raise SystemExit(EXIT_PATH_NOT_ALLOWED)
|
|
|
|
|
|
|
|
|
|
|
2018-10-09 12:25:39 +02:00
|
|
|
|
def command_oars_section(user, section, quiet=False, interactive=True):
|
|
|
|
|
"""Get the value of the given OARS section for the given user, according
|
|
|
|
|
to their OARS filter."""
|
|
|
|
|
user_id = __lookup_user_id_or_error(user)
|
|
|
|
|
app_filter = __get_app_filter_or_error(user_id, interactive)
|
|
|
|
|
|
|
|
|
|
value = app_filter.get_oars_value(section)
|
|
|
|
|
print('OARS section ‘{}’ for user {} has value ‘{}’'.format(
|
|
|
|
|
section, user_id, __oars_value_to_string(value)))
|
|
|
|
|
|
|
|
|
|
|
2018-11-29 22:09:57 +01:00
|
|
|
|
def command_set(user, allow_user_installation=True,
|
|
|
|
|
allow_system_installation=False, app_filter_args=None,
|
2018-11-13 12:45:41 +01:00
|
|
|
|
quiet=False, interactive=True):
|
2018-10-12 05:48:29 +02:00
|
|
|
|
"""Set the app filter for the given user."""
|
|
|
|
|
user_id = __lookup_user_id_or_error(user)
|
2019-02-26 18:43:56 +01:00
|
|
|
|
builder = Malcontent.AppFilterBuilder.new()
|
2018-11-29 22:09:57 +01:00
|
|
|
|
builder.set_allow_user_installation(allow_user_installation)
|
2018-11-28 17:39:39 +01:00
|
|
|
|
builder.set_allow_system_installation(allow_system_installation)
|
2018-10-12 05:48:29 +02:00
|
|
|
|
|
|
|
|
|
for arg in app_filter_args:
|
|
|
|
|
if '=' in arg:
|
|
|
|
|
[section, value_str] = arg.split('=', 2)
|
|
|
|
|
try:
|
|
|
|
|
value = __oars_value_from_string(value_str)
|
|
|
|
|
except KeyError:
|
|
|
|
|
print('Unknown OARS value ‘{}’'.format(value_str),
|
|
|
|
|
file=sys.stderr)
|
|
|
|
|
raise SystemExit(EXIT_INVALID_OPTION)
|
|
|
|
|
builder.set_oars_value(section, value)
|
2018-10-12 06:59:04 +02:00
|
|
|
|
elif arg.startswith('app/') or arg.startswith('runtime/'):
|
|
|
|
|
builder.blacklist_flatpak_ref(arg)
|
2018-10-12 05:48:29 +02:00
|
|
|
|
else:
|
|
|
|
|
builder.blacklist_path(arg)
|
|
|
|
|
app_filter = builder.end()
|
|
|
|
|
|
|
|
|
|
__set_app_filter_or_error(user_id, app_filter, interactive)
|
|
|
|
|
|
|
|
|
|
print('App filter for user {} set'.format(user_id))
|
|
|
|
|
|
|
|
|
|
|
2018-10-02 18:03:33 +02:00
|
|
|
|
def main():
|
|
|
|
|
# Parse command line arguments
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
|
description='Query and update parental controls.')
|
|
|
|
|
subparsers = parser.add_subparsers(metavar='command',
|
|
|
|
|
help='command to run (default: ‘get’)')
|
|
|
|
|
parser.set_defaults(function=command_get)
|
|
|
|
|
parser.add_argument('-q', '--quiet', action='store_true',
|
|
|
|
|
help='output no informational messages')
|
|
|
|
|
parser.set_defaults(quiet=False)
|
|
|
|
|
|
|
|
|
|
# Common options for the subcommands which might need authorisation.
|
|
|
|
|
common_parser = argparse.ArgumentParser(add_help=False)
|
|
|
|
|
group = common_parser.add_mutually_exclusive_group()
|
|
|
|
|
group.add_argument('-n', '--no-interactive', dest='interactive',
|
|
|
|
|
action='store_false',
|
|
|
|
|
help='do not allow interactive polkit authorization '
|
|
|
|
|
'dialogues')
|
|
|
|
|
group.add_argument('--interactive', dest='interactive',
|
|
|
|
|
action='store_true',
|
|
|
|
|
help='opposite of --no-interactive')
|
|
|
|
|
common_parser.set_defaults(interactive=True)
|
|
|
|
|
|
|
|
|
|
# ‘get’ command
|
|
|
|
|
parser_get = subparsers.add_parser('get', parents=[common_parser],
|
|
|
|
|
help='get current parental controls '
|
|
|
|
|
'settings')
|
|
|
|
|
parser_get.set_defaults(function=command_get)
|
|
|
|
|
parser_get.add_argument('user', default='', nargs='?',
|
|
|
|
|
help='user ID or username to get the app filter '
|
|
|
|
|
'for (default: current user)')
|
|
|
|
|
|
|
|
|
|
# ‘check’ command
|
|
|
|
|
parser_check = subparsers.add_parser('check', parents=[common_parser],
|
|
|
|
|
help='check whether a path is '
|
|
|
|
|
'allowed by app filter')
|
|
|
|
|
parser_check.set_defaults(function=command_check)
|
|
|
|
|
parser_check.add_argument('user', default='', nargs='?',
|
|
|
|
|
help='user ID or username to get the app filter '
|
|
|
|
|
'for (default: current user)')
|
|
|
|
|
parser_check.add_argument('path',
|
|
|
|
|
help='path to a program to check')
|
|
|
|
|
|
2018-10-09 12:25:39 +02:00
|
|
|
|
# ‘oars-section’ command
|
|
|
|
|
parser_oars_section = subparsers.add_parser('oars-section',
|
|
|
|
|
parents=[common_parser],
|
|
|
|
|
help='get the value of a '
|
|
|
|
|
'given OARS section')
|
|
|
|
|
parser_oars_section.set_defaults(function=command_oars_section)
|
|
|
|
|
parser_oars_section.add_argument('user', default='', nargs='?',
|
|
|
|
|
help='user ID or username to get the '
|
|
|
|
|
'OARS filter for (default: current '
|
|
|
|
|
'user)')
|
|
|
|
|
parser_oars_section.add_argument('section', help='OARS section to get')
|
|
|
|
|
|
2018-10-12 05:48:29 +02:00
|
|
|
|
# ‘set’ command
|
|
|
|
|
parser_set = subparsers.add_parser('set', parents=[common_parser],
|
|
|
|
|
help='set current parental controls '
|
|
|
|
|
'settings')
|
|
|
|
|
parser_set.set_defaults(function=command_set)
|
|
|
|
|
parser_set.add_argument('user', default='', nargs='?',
|
|
|
|
|
help='user ID or username to get the app filter '
|
|
|
|
|
'for (default: current user)')
|
2018-11-29 22:09:57 +01:00
|
|
|
|
parser_set.add_argument('--allow-user-installation',
|
|
|
|
|
dest='allow_user_installation',
|
|
|
|
|
action='store_true',
|
|
|
|
|
help='allow installation to the user flatpak '
|
|
|
|
|
'repo in general')
|
|
|
|
|
parser_set.add_argument('--disallow-user-installation',
|
|
|
|
|
dest='allow_user_installation',
|
|
|
|
|
action='store_false',
|
|
|
|
|
help='unconditionally disallow installation to '
|
|
|
|
|
'the user flatpak repo')
|
2018-11-28 17:39:39 +01:00
|
|
|
|
parser_set.add_argument('--allow-system-installation',
|
|
|
|
|
dest='allow_system_installation',
|
|
|
|
|
action='store_true',
|
|
|
|
|
help='allow installation to the system flatpak '
|
|
|
|
|
'repo in general')
|
|
|
|
|
parser_set.add_argument('--disallow-system-installation',
|
|
|
|
|
dest='allow_system_installation',
|
2018-11-13 12:45:41 +01:00
|
|
|
|
action='store_false',
|
2018-11-28 17:39:39 +01:00
|
|
|
|
help='unconditionally disallow installation to '
|
|
|
|
|
'the system flatpak repo')
|
2018-10-12 05:48:29 +02:00
|
|
|
|
parser_set.add_argument('app_filter_args', nargs='*',
|
|
|
|
|
help='paths to blacklist and OARS section=value '
|
|
|
|
|
'pairs to store')
|
2018-11-29 22:09:57 +01:00
|
|
|
|
parser_set.set_defaults(allow_user_installation=True,
|
|
|
|
|
allow_system_installation=False)
|
2018-10-12 05:48:29 +02:00
|
|
|
|
|
2018-10-02 18:03:33 +02:00
|
|
|
|
# Parse the command line arguments and run the subcommand.
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
args_dict = dict((k, v) for k, v in vars(args).items() if k != 'function')
|
|
|
|
|
args.function(**args_dict)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|