aboutsummaryrefslogtreecommitdiff
path: root/.local/bin/doas_askpass
blob: 49a56ef4ec108ad57725ed91a49a0aedaf232ed3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/expect --

# askpass implementation for doas
# example usage: DOAS_ASKPASS="rofi -dmenu -P -p password:" doas_askpass echo working

# don't mind the man behind the curtain
log_user 0

# no command, then nothing to do
if { $argc == 0 } { exit 0 }

# treat all arguments as command input
set cmd [lrange $argv 0 end];

# read askpass from env or fallback to dmenu_pass ()
if {[info exists ::env(DOAS_ASKPASS)]} {
	set askpass "$::env(DOAS_ASKPASS)"
} else {
	set askpass "dmenu_pass password:"
}

# read password from user
set pwd [exec {*}$askpass]

# spawn doas operation
spawn doas {*}$cmd

# send password and execute command
expect "doas*password:" {
	send -- "$pwd\r"
	expect \r
	log_user 1
	expect eof
}

# get the exit status of the spawned doas command
set status [wait]

# check exit code (4th element of wait result)
set exit_code [lindex $status 3]

# exit with 1 if doas failed, else 0
if { $exit_code != 0 } {
	exit 1
} else {
	exit 0
}