2017-03-21 02:42:28 +01:00
|
|
|
# -*- encoding: utf8 -*-
|
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2017 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# 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.
|
2017-03-21 02:42:28 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2017-03-21 02:42:28 +01:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2017-10-12 00:11:50 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2017-03-21 02:42:28 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
|
2017-03-21 02:42:28 +01:00
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
import logging.handlers
|
2017-08-13 02:36:26 +02:00
|
|
|
import os
|
2017-03-21 02:42:28 +01:00
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import qubespolicy
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(description="Evaluate qrexec policy")
|
|
|
|
|
|
|
|
parser.add_argument("--assume-yes-for-ask", action="store_true",
|
|
|
|
dest="assume_yes_for_ask", default=False,
|
|
|
|
help="Allow run of service without confirmation if policy say 'ask'")
|
|
|
|
parser.add_argument("--just-evaluate", action="store_true",
|
|
|
|
dest="just_evaluate", default=False,
|
|
|
|
help="Do not run the service, only evaluate policy; "
|
|
|
|
"retcode=0 means 'allow'")
|
|
|
|
parser.add_argument('domain_id', metavar='src-domain-id',
|
|
|
|
help='Source domain ID (Xen ID or similar, not Qubes ID)')
|
|
|
|
parser.add_argument('domain', metavar='src-domain-name',
|
|
|
|
help='Source domain name')
|
|
|
|
parser.add_argument('target', metavar='dst-domain-name',
|
|
|
|
help='Target domain name')
|
|
|
|
parser.add_argument('service_name', metavar='service-name',
|
|
|
|
help='Service name')
|
|
|
|
parser.add_argument('process_ident', metavar='process-ident',
|
|
|
|
help='Qrexec process identifier - for connecting data channel')
|
|
|
|
|
|
|
|
|
2017-08-13 02:36:26 +02:00
|
|
|
def create_default_policy(service_name):
|
|
|
|
policy_file = os.path.join(qubespolicy.POLICY_DIR, service_name)
|
|
|
|
with open(policy_file, "w") as policy:
|
|
|
|
policy.write(
|
|
|
|
"## Policy file automatically created on first service call.\n")
|
|
|
|
policy.write(
|
|
|
|
"## Fill free to edit.\n")
|
|
|
|
policy.write("## Note that policy parsing stops at the first match\n")
|
|
|
|
policy.write("\n")
|
|
|
|
policy.write("## Please use a single # to start your custom comments\n")
|
|
|
|
policy.write("\n")
|
2018-02-16 05:17:20 +01:00
|
|
|
policy.write("@anyvm @anyvm ask\n")
|
2017-08-13 02:36:26 +02:00
|
|
|
|
|
|
|
|
2017-03-21 02:42:28 +01:00
|
|
|
def main(args=None):
|
|
|
|
args = parser.parse_args(args)
|
|
|
|
|
|
|
|
# Add source domain information, required by qrexec-client for establishing
|
|
|
|
# connection
|
|
|
|
caller_ident = args.process_ident + "," + args.domain + "," + args.domain_id
|
|
|
|
log = logging.getLogger('qubespolicy')
|
|
|
|
log.setLevel(logging.INFO)
|
2017-08-31 20:28:08 +02:00
|
|
|
if not log.handlers:
|
|
|
|
handler = logging.handlers.SysLogHandler(address='/dev/log')
|
|
|
|
log.addHandler(handler)
|
2017-03-21 02:42:28 +01:00
|
|
|
log_prefix = 'qrexec: {}: {} -> {}: '.format(
|
|
|
|
args.service_name, args.domain, args.target)
|
|
|
|
try:
|
|
|
|
system_info = qubespolicy.get_system_info()
|
|
|
|
except qubespolicy.QubesMgmtException as e:
|
|
|
|
log.error(log_prefix + 'error getting system info: ' + str(e))
|
|
|
|
return 1
|
|
|
|
try:
|
2017-08-13 02:36:26 +02:00
|
|
|
try:
|
|
|
|
policy = qubespolicy.Policy(args.service_name)
|
|
|
|
except qubespolicy.PolicyNotFound:
|
|
|
|
service_name = args.service_name.split('+')[0]
|
|
|
|
import pydbus
|
|
|
|
bus = pydbus.SystemBus()
|
|
|
|
proxy = bus.get('org.qubesos.PolicyAgent',
|
|
|
|
'/org/qubesos/PolicyAgent')
|
|
|
|
create_policy = proxy.ConfirmPolicyCreate(
|
|
|
|
args.domain, service_name)
|
|
|
|
if create_policy:
|
|
|
|
create_default_policy(service_name)
|
|
|
|
policy = qubespolicy.Policy(args.service_name)
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2017-03-21 02:42:28 +01:00
|
|
|
action = policy.evaluate(system_info, args.domain, args.target)
|
2017-08-07 02:02:01 +02:00
|
|
|
if args.assume_yes_for_ask and action.action == qubespolicy.Action.ask:
|
|
|
|
action.action = qubespolicy.Action.allow
|
|
|
|
if args.just_evaluate:
|
|
|
|
return {
|
|
|
|
qubespolicy.Action.allow: 0,
|
|
|
|
qubespolicy.Action.deny: 1,
|
|
|
|
qubespolicy.Action.ask: 1,
|
|
|
|
}[action.action]
|
2017-03-21 02:42:28 +01:00
|
|
|
if action.action == qubespolicy.Action.ask:
|
2017-03-24 16:27:02 +01:00
|
|
|
# late import to save on time for allow/deny actions
|
2017-04-06 15:39:31 +02:00
|
|
|
import pydbus
|
|
|
|
bus = pydbus.SystemBus()
|
|
|
|
proxy = bus.get('org.qubesos.PolicyAgent',
|
|
|
|
'/org/qubesos/PolicyAgent')
|
|
|
|
|
|
|
|
icons = {name: system_info['domains'][name]['icon']
|
|
|
|
for name in system_info['domains'].keys()}
|
2017-03-24 16:27:02 +01:00
|
|
|
for dispvm_base in system_info['domains']:
|
2017-09-03 03:11:48 +02:00
|
|
|
if not (system_info['domains'][dispvm_base]
|
|
|
|
['template_for_dispvms']):
|
2017-03-24 16:27:02 +01:00
|
|
|
continue
|
2018-02-16 05:17:20 +01:00
|
|
|
dispvm_api_name = '@dispvm:' + dispvm_base
|
2017-04-06 15:39:31 +02:00
|
|
|
icons[dispvm_api_name] = \
|
|
|
|
system_info['domains'][dispvm_base]['icon']
|
|
|
|
icons[dispvm_api_name] = \
|
|
|
|
icons[dispvm_api_name].replace('app', 'disp')
|
2017-03-24 16:27:02 +01:00
|
|
|
|
2017-04-06 15:39:31 +02:00
|
|
|
response = proxy.Ask(args.domain, args.service_name,
|
|
|
|
action.targets_for_ask, action.target or '', icons)
|
2017-03-24 16:27:02 +01:00
|
|
|
if response:
|
|
|
|
action.handle_user_response(True, response)
|
|
|
|
else:
|
|
|
|
action.handle_user_response(False)
|
2017-05-17 14:25:54 +02:00
|
|
|
log.info(log_prefix + 'allowed to {}'.format(action.target))
|
2017-03-21 02:42:28 +01:00
|
|
|
action.execute(caller_ident)
|
|
|
|
except qubespolicy.PolicySyntaxError as e:
|
|
|
|
log.error(log_prefix + 'error loading policy: ' + str(e))
|
|
|
|
return 1
|
|
|
|
except qubespolicy.AccessDenied as e:
|
|
|
|
log.info(log_prefix + 'denied: ' + str(e))
|
|
|
|
return 1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys.exit(main())
|