#!/usr/bin/env python3 from os import (stat, utime, fdopen, supports_fd, O_CREAT, O_APPEND, open as osopen) from os.path import exists, isfile from argparse import ArgumentParser from sys import exit, stderr from email.mime.text import MIMEText from subprocess import Popen, PIPE from getpass import getuser from pwd import getpwuid from socket import gethostname from shlex import split as shell_split def eprint(*args, **kwargs): print(*args, file=stderr, **kwargs) def touch(fname, mode=0o666, dir_fd=None, **kwargs): flags = O_CREAT | O_APPEND with fdopen(osopen(fname, flags=flags, mode=mode, dir_fd=dir_fd)) as f: utime(f.fileno() if utime in supports_fd else fname, dir_fd=None if supports_fd else dir_fd, **kwargs) def ownership(filename): return getpwuid(stat(filename).st_uid).pw_name def mail(user, subject, body): domain = gethostname() message = MIMEText(body) message["From"] = "{}@{}".format(user, domain) message["To"] = "root@{}".format(domain) message["Subject"] = subject command = shell_split("/usr/sbin/sendmail -t -oi") proc = Popen(command, stdin=PIPE, universal_newlines=True) proc.communicate(message.as_string()) def share_list(config_file, splitter): scripts = None with open(config_file, "r") as cfgfile: scripts = cfgfile.read().split("\n") if len(scripts) == 1 and len(scripts[0]) == 0: print("There are currently no shared user scripts") exit(0) for script in scripts: name, path, desc, author = script.split(splitter) print("{}\t{} (made by {})".format(name, desc, author).expandtabs()) def share_submit(queue_file, splitter): script_path = input("Absolute path to script: ") if not isfile(script_path): eprint("ERROR: That script does not exist") exit(1) user = getuser() owner = ownership(script_path) if user != owner: eprint("ERROR: You do not own this script") exit(1) script_name = script_path.split("/")[-1] script_desc = input("Description of script: ") with open(queue_file, "a") as queue: queue.write("{0}{4}{1}{4}{2}{4}{3}".format(script_name, script_path, script_desc, user, splitter)) subject = "Submission of '{}' script".format(script_name) body = "\n".join([ "Path to script: {}".format(script_path), "Description: {}".format(script_desc), "Author: {}".format(user) ]) mail(user, subject, body) print("SUCCESS: Your script has been submitted for approval") config_file = "/center/etc/shared" splitter = "%%" if not exists(config_file): touch(config_file) queue_file = "{}.queue".format(config_file) if not exists(queue_file): touch(queue_file) description = "Allows users to share their scripts with the server" parser = ArgumentParser(prog="share", description=description) subparsers = parser.add_subparsers(title="commands", dest="commands") subparsers.required = False list_parser = subparsers.add_parser("list", help="list all shared scripts") submit_parser = subparsers.add_parser("submit", help="starts interactive prompt to share your script") arguments = parser.parse_args() if arguments.commands == "list": share_list(config_file, splitter) elif arguments.commands == "submit": share_submit(queue_file, splitter) else: parser.print_help()