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
[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:
print 'usage: %s <command>' % 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__':