From 4bb911aa10be843b2968565fdf7e117bc34bd972 Mon Sep 17 00:00:00 2001 From: allfro Date: Tue, 19 Mar 2013 20:52:47 -0400 Subject: [PATCH] Additional fixes to pysudo --- src/canari/commands/run_transform.py | 5 ++--- src/scripts/pysudo | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/canari/commands/run_transform.py b/src/canari/commands/run_transform.py index 8696311..d063b37 100644 --- a/src/canari/commands/run_transform.py +++ b/src/canari/commands/run_transform.py @@ -2,12 +2,12 @@ import os import sys +from traceback import format_exc from argparse import ArgumentParser -from traceback import format_exc from canari.maltego.message import MaltegoException, MaltegoTransformResponseMessage, UIMessage -from common import cmd_name, import_transform, fix_binpath, get_bin_dir, sudo +from common import cmd_name, import_transform, fix_binpath, sudo from canari.maltego.utils import onterminate, parseargs, croak, message from canari.config import config @@ -66,7 +66,6 @@ def run(args): [transform, params, value, fields] = parseargs(['canari %s' % cmd_name(__name__)] + args) m = None - pysudo = os.path.join(get_bin_dir(), 'pysudo') fix_binpath(config['default/path']) try: diff --git a/src/scripts/pysudo b/src/scripts/pysudo index 3954341..7185a7e 100755 --- a/src/scripts/pysudo +++ b/src/scripts/pysudo @@ -24,25 +24,42 @@ def main(): print 'usage: %s ' % sys.argv[0] exit(-1) - + # Let's try and run it right away and see what happens p = subprocess.Popen(['sudo', '-S'] + sys.argv[1:], stdin=subprocess.PIPE, stderr=subprocess.PIPE) p.communicate() + # It ran! if not p.returncode: exit(0) + # It didn't :( - let's lock this region now to avoid having multiple password boxes pop-up l = fmutex('pysudo.%s.lock' % getpass.getuser()) + + # Try running it again (maybe another process authenticated... why ask for a password again?) + p = subprocess.Popen(['sudo', '-S'] + sys.argv[1:], stdin=subprocess.PIPE, stderr=subprocess.PIPE) + p.communicate() + + if not p.returncode: + l.unlock() + exit(0) + + # No we really need to ask for a password :( for i in range(0, 3): password = passwordbox('Please enter your password.', 'sudo', '') if password is None: exit(1) + + # Try it out with a password now! p = subprocess.Popen(['sudo', '-S', 'true'], stdin=subprocess.PIPE, stderr=subprocess.PIPE) p.communicate(input='%s\n' % password) + + # Did it work? Yes: let's do it! if not p.returncode: l.unlock() p = subprocess.Popen(['sudo', '-S'] + sys.argv[1:]) p.communicate(input='%s\n' % password) exit(p.returncode) + exit(2) if __name__ == '__main__': -- 2.45.1