diff -r 000000000000 -r c9bf2537109a ait/os/bin/autoshadow/autoshadow.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ait/os/bin/autoshadow/autoshadow.py Tue Nov 12 11:31:34 2013 +0100 @@ -0,0 +1,152 @@ +#!/bin/env python +# -*- coding: utf-8 -*- + +# ------------------------------------------------------------ +# autoshadow.py +# +# Listen on DBus and mount any USB stick automatically +# and invoke shadowfuse for it +# +# Autor: Oliver Maurhart, +# +# Copyright (C) 2013 AIT Austrian Institute of Technology +# AIT Austrian Institute of Technology GmbH +# Donau-City-Strasse 1 | 1220 Vienna | Austria +# http://www.ait.ac.at +# +# 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 version 2. +# +# 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. +# ------------------------------------------------------------ + + +# ------------------------------------------------------------ +# imports + +import argparse +import dbus +import dbus.glib +import dbus.service +import gobject +import sys + + +# ------------------------------------------------------------ +# const + + +__version__ = "0.1" + + +# ------------------------------------------------------------ +# code + + +class AutoShadowService(dbus.service.Object): + + """The AutoShadowService is the DBus object which listens on UDisk2 events and decides to mount and shadow a device. + + This class incorporates a DBus service (at.ac.ait.opensecurity.AutoShadow) and binds + itself to the /AutoShadow object. + """ + + def __init__(self): + + bus = dbus.SystemBus() + bus_name = dbus.service.BusName('at.ac.ait.opensecurity.AutoShadow', bus) + dbus.service.Object.__init__(self, bus_name, '/AutoShadow') + + # get the UDisk2 system object + try: + udisk2 = bus.get_object('org.freedesktop.UDisks2', '/org/freedesktop/UDisks2') + except: + sys.stderr.write('Failed to aquire DBus Service org.freedesktop.UDisks2 object /org/freedesktop/UDisks2 on system DBus.\n') + sys.exit(1) + + # connect our signal + udisk2.connect_to_signal('InterfacesAdded', self.interface_added, sender_keyword='sender') + + + def interface_added(*args, **kwargs): + + """Entry point for signal for new interfaces""" + + # a new interface has been added + object_path = args[1] + interfaces_and_properties = args[2] + interface_keys = interfaces_and_properties.keys() + + if (interface_keys[0] == 'org.freedesktop.UDisks2.Drive'): + + # added a new drive + drive_values = interfaces_and_properties[interface_keys[0]] + drive_id = str(drive_values['Id']) + drive_vendor = str(drive_values['Vendor']) + drive_removeable = bool(drive_values['Removable']) + print('detected new drive: id=\'{0}\' vendor=\'{1}\' removeable={2}'.format(drive_id, drive_vendor, drive_removeable)) + + if (interface_keys[0] == 'org.freedesktop.UDisks2.Block'): + + # added a new device - filesystem? + if ('org.freedesktop.UDisks2.Filesystem' in interface_keys): + + # pick values of the device + device_values = interfaces_and_properties[interface_keys[0]] + device_path = bytearray(device_values['Device'][0:-1]).decode('latin-1') + print('detected new device: path=\'{0}\''.format(device_path)) + enforce_mount('/org/freedesktop/UDisks2/block_devices/' + device_path.split('/')[-1]) + + + def listen(self): + """Start listening on DBus""" + self.loop = gobject.MainLoop() + self.loop.run() + + + @dbus.service.method('at.ac.ait.opensecurity.AutoShadow') + def Quit(self): + """Terminate this service""" + self.loop.quit() + + + @dbus.service.method('at.ac.ait.opensecurity.AutoShadow', out_signature='s') + def Version(self): + """Give a version string""" + return __version__ + + +def enforce_mount(udisk_object): + + """This function does the real mounting of drives. + It also enforces the MirrorFuse on these mounts. + """ + + print("ENFORCING mount of " + udisk_object) + + +def main(): + + # parse command line + parser = argparse.ArgumentParser(description = 'Automount USB storage devices and invoke shadowfuse for it.') + args = parser.parse_args() + + # setup DBus event loop + autoshadow_service = AutoShadowService() + autoshadow_service.listen() + + +# start +if __name__ == "__main__": + main() + +