Skip to content

Commit

Permalink
Add doasudo.py, update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
tutacat committed May 5, 2023
1 parent b09a072 commit c666ea6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# doasudo
sudo emulation for doas/opendoas

Currently python only
63 changes: 63 additions & 0 deletions doasudo.py
Original file line number Diff line number Diff line change
@@ -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')

0 comments on commit c666ea6

Please sign in to comment.