2012-10-08 00:44:40 +02:00
|
|
|
#!/usr/bin/python2
|
2014-05-18 21:01:21 +02:00
|
|
|
# -*- encoding: utf8 -*-
|
2012-10-08 00:44:40 +02:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, http://www.qubes-os.org
|
|
|
|
#
|
|
|
|
# Copyright (C) 2010 Marek Marczykowski <marmarek@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program 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 General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
|
|
|
|
from qubes.qubes import QubesVmCollection, QubesException
|
2012-10-08 23:08:54 +02:00
|
|
|
from qubes.qubesutils import usb_list,usb_attach,usb_detach,usb_detach_all,usb_check_attached
|
2012-10-08 00:44:40 +02:00
|
|
|
from optparse import OptionParser
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
|
|
|
def main():
|
|
|
|
usage = "usage: %prog -l [options]\n"\
|
|
|
|
"usage: %prog -a [options] <vm-name> <device-vm-name>:<device>\n"\
|
|
|
|
"usage: %prog -d [options] <device-vm-name>:<device>\n"\
|
|
|
|
"List/set VM USB devices."
|
2012-10-12 23:34:18 +02:00
|
|
|
# "usage: %prog -d [options] <vm-name>\n"\
|
2012-10-08 00:44:40 +02:00
|
|
|
|
|
|
|
parser = OptionParser (usage)
|
|
|
|
parser.add_option ("-l", "--list", action="store_true", dest="do_list", default=False)
|
|
|
|
parser.add_option ("-a", "--attach", action="store_true", dest="do_attach", default=False)
|
|
|
|
parser.add_option ("-d", "--detach", action="store_true", dest="do_detach", default=False)
|
2012-10-12 22:12:40 +02:00
|
|
|
# parser.add_option ("-f", "--frontend", dest="frontend",
|
|
|
|
# help="Specify device id at destination VM [default: first unused]")
|
2012-10-08 23:08:54 +02:00
|
|
|
parser.add_option ("--no-auto-detach", dest="auto_detach", action="store_false", default=True,
|
|
|
|
help="Fail when device already connected to other VM")
|
2012-10-08 00:44:40 +02:00
|
|
|
parser.add_option ("--force-root", action="store_true", dest="force_root", default=False,
|
|
|
|
help="Force to run, even with root privileges")
|
|
|
|
|
|
|
|
(options, args) = parser.parse_args ()
|
|
|
|
|
2013-07-30 10:42:08 +02:00
|
|
|
if hasattr(os, "geteuid") and os.geteuid() == 0:
|
2012-10-08 00:44:40 +02:00
|
|
|
if not options.force_root:
|
|
|
|
print >> sys.stderr, "*** Running this tool as root is strongly discouraged, this will lead you in permissions problems."
|
|
|
|
print >> sys.stderr, "Retry as unprivileged user."
|
|
|
|
print >> sys.stderr, "... or use --force-root to continue anyway."
|
|
|
|
exit(1)
|
|
|
|
|
|
|
|
if options.do_list + options.do_attach + options.do_detach > 1:
|
|
|
|
print >> sys.stderr, "Only one of -l -a -d is allowed!"
|
|
|
|
exit (1)
|
|
|
|
|
2016-05-26 01:38:08 +02:00
|
|
|
qvm_collection = QubesVmCollection()
|
|
|
|
qvm_collection.lock_db_for_reading()
|
|
|
|
qvm_collection.load()
|
|
|
|
qvm_collection.unlock_db()
|
2012-10-08 00:44:40 +02:00
|
|
|
|
|
|
|
if options.do_attach:
|
2012-10-08 23:08:54 +02:00
|
|
|
if (len (args) != 2):
|
|
|
|
parser.error ("You must provide vm name and device!")
|
2012-10-08 00:44:40 +02:00
|
|
|
vm = qvm_collection.get_vm_by_name(args[0])
|
|
|
|
if vm is None:
|
|
|
|
parser.error ("Invalid VM name: %s" % args[0])
|
2012-10-12 23:34:18 +02:00
|
|
|
|
2016-05-26 01:38:08 +02:00
|
|
|
# FIXME: here we assume that device is always in form "domain:dev",
|
|
|
|
# which can be changed in the future
|
2012-10-08 00:44:40 +02:00
|
|
|
if args[1].find(":") < 0:
|
2016-05-26 01:38:08 +02:00
|
|
|
parser.error("Invalid device syntax: %s" % args[1])
|
|
|
|
backend_vm = qvm_collection.get_vm_by_name(args[1].split(":")[0])
|
|
|
|
if backend_vm is None:
|
|
|
|
parser.error("No such VM: {}".format(args[1].split(":")[0]))
|
2016-06-02 02:44:38 +02:00
|
|
|
dev_list = usb_list(qvm_collection, vm=backend_vm)
|
2012-10-08 00:44:40 +02:00
|
|
|
if not args[1] in dev_list.keys():
|
2016-05-26 01:38:08 +02:00
|
|
|
parser.error("Invalid device name: %s" % args[1])
|
2012-10-08 00:44:40 +02:00
|
|
|
dev = dev_list[args[1]]
|
|
|
|
assert backend_vm is not None
|
2012-10-12 23:34:18 +02:00
|
|
|
|
2012-10-08 00:44:40 +02:00
|
|
|
kwargs = {}
|
2012-10-12 22:12:40 +02:00
|
|
|
# if options.frontend:
|
|
|
|
# kwargs['frontend'] = options.frontend
|
2012-10-08 00:44:40 +02:00
|
|
|
kwargs['auto_detach'] = options.auto_detach
|
|
|
|
try:
|
2016-06-02 02:44:38 +02:00
|
|
|
usb_attach(qvm_collection, vm, dev, **kwargs)
|
2012-10-08 00:44:40 +02:00
|
|
|
except QubesException as e:
|
|
|
|
print >> sys.stderr, "ERROR: %s" % str(e)
|
|
|
|
sys.exit(1)
|
|
|
|
elif options.do_detach:
|
|
|
|
if (len (args) < 1):
|
|
|
|
parser.error ("You must provide device or vm name!")
|
2016-05-26 01:38:08 +02:00
|
|
|
if len(args) > 1:
|
2012-10-08 23:08:54 +02:00
|
|
|
parser.error ("Too many parameters")
|
2012-10-08 00:44:40 +02:00
|
|
|
# Check if provided name is VM
|
|
|
|
vm = qvm_collection.get_vm_by_name(args[0])
|
|
|
|
if vm is not None:
|
2012-10-12 23:34:18 +02:00
|
|
|
#kwargs = {}
|
|
|
|
#if options.frontend:
|
|
|
|
# kwargs['frontend'] = options.frontend
|
|
|
|
# usb_detach(vm, **kwargs)
|
|
|
|
#else:
|
2016-06-02 02:44:38 +02:00
|
|
|
usb_detach_all(qvm_collection, vm)
|
2012-10-08 00:44:40 +02:00
|
|
|
else:
|
2012-10-12 23:34:18 +02:00
|
|
|
# Maybe usbvm:device?
|
|
|
|
|
2016-05-26 01:38:08 +02:00
|
|
|
# FIXME: nasty copy-paste from attach code half a page above
|
|
|
|
# FIXME: here we assume that device is always in form "domain:dev",
|
|
|
|
# which can be changed in the future
|
2012-10-12 23:34:18 +02:00
|
|
|
if args[0].find(":") < 0:
|
2016-05-26 01:38:08 +02:00
|
|
|
parser.error("Invalid device syntax: %s" % args[0])
|
|
|
|
backend_vm = qvm_collection.get_vm_by_name(args[0].split(":")[0])
|
|
|
|
if backend_vm is None:
|
|
|
|
parser.error("No such VM: {}".format(args[0].split(":")[0]))
|
2016-06-02 02:44:38 +02:00
|
|
|
dev_list = usb_list(qvm_collection, vm=backend_vm)
|
2012-10-08 00:44:40 +02:00
|
|
|
if not args[0] in dev_list.keys():
|
2016-05-26 01:38:08 +02:00
|
|
|
parser.error("Invalid device name: %s" % args[0])
|
2012-10-08 00:44:40 +02:00
|
|
|
dev = dev_list[args[0]]
|
2016-06-02 02:44:38 +02:00
|
|
|
attached_to = usb_check_attached(qvm_collection, dev)
|
2012-10-08 00:44:40 +02:00
|
|
|
if attached_to is None:
|
|
|
|
print >> sys.stderr, "WARNING: Device not connected to any VM"
|
|
|
|
exit(0)
|
2016-06-02 02:44:38 +02:00
|
|
|
usb_detach(qvm_collection, attached_to, dev)
|
2012-10-08 00:44:40 +02:00
|
|
|
else:
|
2016-05-26 01:38:08 +02:00
|
|
|
if len(args) > 0:
|
|
|
|
parser.error("Too many parameters")
|
2012-10-08 00:44:40 +02:00
|
|
|
# do_list
|
2016-06-02 02:44:38 +02:00
|
|
|
for dev in usb_list(qvm_collection).values():
|
|
|
|
attached_to = dev['connected-to']
|
2012-10-08 00:44:40 +02:00
|
|
|
attached_to_str = ""
|
|
|
|
if attached_to:
|
2016-06-02 02:44:38 +02:00
|
|
|
attached_to_str = " (attached to %s)" % (attached_to.name)
|
2016-05-26 01:38:08 +02:00
|
|
|
print "%s\t%s%s" % (dev['name'], dev['desc'], attached_to_str)
|
2012-10-08 00:44:40 +02:00
|
|
|
exit (0)
|
|
|
|
|
|
|
|
main()
|