#! /usr/bin/python3 # Copyright (c) 2022 Joey Schulze # # 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA. import os import sys import pwd import stat import syslog import signal import pyinotify from argparse import ArgumentParser, RawTextHelpFormatter, FileType from configparser import ConfigParser config = None class DeviceHandler(pyinotify.ProcessEvent): def __init__(self): super().__init__() def process_IN_CREATE(self, event): uuid = os.path.basename(event.pathname) syslog.syslog(syslog.LOG_INFO, "New UUID available: {}".format(uuid)) if config.has_section(uuid): syslog.syslog(syslog.LOG_INFO, "Destination {}".format(config.get(uuid, 'dest'))) self.mount(uuid) def mount(self, uuid): dest = config.get(uuid, 'dest') user = config.get(uuid, 'user') if config.has_option(uuid, 'user') else 'root' group = config.get(uuid, 'group') if config.has_option(uuid, 'group') else user options = '' if config.has_option(uuid, 'options'): options += (',' if len(options) else '') + config.get(uuid, 'options') if 'shortname=' in options or 'fmask=' in options: uid = pwd.getpwnam(user).pw_uid gid = pwd.getpwnam(group).pw_gid options += (',' if len(options) else '') + 'uid={},gid={}'.format(self.uid, self.gid) if not os.path.isdir(dest): os.system("install -d -m 755 -o {user} -g {group} {dest}".format( user=user, group=group, dest=dest)) cmd = "mount{opts} UUID={uuid} {dest}".format( uuid=uuid, dest=dest, opts=(' -o ' + options) if len(options) else '') syslog.syslog(syslog.LOG_DEBUG, cmd) os.system(cmd) self.assert_sudoers(uuid) def assert_sudoers(self, uuid): user = config.get(uuid, 'user') if config.has_option(uuid, 'user') else 'root' if user == 'root': return if config.has_option(uuid, 'sudoers') and config.get(uuid, 'sudoers').lower() == 'true': dest = os.path.join('/etc/sudoers.d', user + '-umount-' + os.path.basename(config.get(uuid, 'dest'))) if not os.path.exists(dest): syslog.syslog(syslog.LOG_INFO, "Write new sudoers configuration {}".format(os.path.basename(dest))) with open(dest, 'w') as f: print("{user} ALL=NOPASSWD: /bin/umount {dest}, /bin/umount {dest}/".format( user=user, dest=config.get(uuid, 'dest')), file=f) def parse_arguments(): global config epilog = [] parser = ArgumentParser(description='Infodrom Automounter', formatter_class=RawTextHelpFormatter, epilog='\r\n'.join(epilog)) parser.add_argument('--config', '-C', dest='config', action='store', default='/etc/automount.conf', help='Read configuration') parser.add_argument('--with-exception', dest='exception', default=False, action='store_true', help='display full exception') args = parser.parse_args() config = ConfigParser() config.read(args.config, encoding='UTF-8') return args def watch_uuid(path): wm = pyinotify.WatchManager() wm.add_watch(path, pyinotify.IN_CREATE) notifier = pyinotify.Notifier(wm, DeviceHandler()) notifier.loop() def main(args): syslog.openlog(os.path.basename(sys.argv[0]), syslog.LOG_PID, syslog.LOG_DAEMON) watch_uuid('/dev/disk/by-uuid') # print(config.sections()) if __name__ == '__main__': args = parse_arguments() try: main(args) except KeyboardInterrupt: pass except Exception as e: if args.exception: from traceback import format_exc print(e, file=sys.stderr) print(format_exc(), file=sys.stderr) else: print("An error occurred:", e)