From c666ea6c5b06a748ac4282feacc8b58202a09fc1 Mon Sep 17 00:00:00 2001 From: tutacat Date: Fri, 5 May 2023 02:54:14 +0100 Subject: [PATCH] Add doasudo.py, update README.md --- README.md | 2 ++ doasudo.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100755 doasudo.py diff --git a/README.md b/README.md index 5be470d..772533a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # doasudo sudo emulation for doas/opendoas + +Currently python only diff --git a/doasudo.py b/doasudo.py new file mode 100755 index 0000000..cd21f8a --- /dev/null +++ b/doasudo.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 +import subprocess +import argparse +has_args = False + +doas_exe = subprocess.getoutput('which doas') +if not doas_exe: + print("doasudo.py: doas: command not found.") + exit(128) + +def parse_args(has_args=False): + parser = argparse.ArgumentParser( + prog='doasudo.py', + description='Translate (most) of the sudo command to doas', + epilog='do as sudo', + ) + parser.add_argument('command', nargs="*") + parser.add_argument('-S', '--stdin', '-n', '--non-interactive', + help="Non interactive mode, fail if the matching rule doesn't have the nopass option.", + action='store_true', + ) + parser.add_argument('-s', '--shell', + help="Execute the shell from SHELL or /etc/passwd.", + action='store_true', + ) + parser.add_argument('-u', '--user', + help="Execute the command as user. The default is root.", + ) + parser.add_argument('-T', '--command-timeout', + help="No-op for compatability.", + action='store_true' + ) + parser.add_argument('-K', '--remove-timestamp', + help="Clear any persisted authentications from previous invocations, then immediately exit.", + action='store_const', + const=3, + dest='timestamp', + ) + parser.add_argument('-k', '--reset-timestamp', + help="Like -K, but a supplied command will be executed.", + action='store_const', + const=1, + dest='timestamp', + ) + return parser.parse_args() + +args = parse_args() + +doas_args = [] + +if args.user: + doas_args.extend(('-u',args.user)) +if args.shell: + doas_args.extend('-s') +if args.stdin: + doas_args.extend('-n') +args.timestamp = args.timestamp or 0 + +if not args.timestamp^2 and args.command != None: + subprocess.run((doas_exe,) + tuple(doas_args) + ('--',) + tuple(command) ) + +if args.timestamp^1: + os.system(doas_exe+' -L')