Skip to content

Commit 257767d

Browse files
authored
Do not ask for password if not needed. (#69)
* Do not ask for password if not needed. In certain circumstances we do not need to ask for a password because we already have previously authenticated to sudo and can just reuse the credentials. Since there is no way to check if a sudo timestamp has timed out we run a simple command and check the result. Using an existing sudo timestamp from within nvim seems to work only if the timestamp_type is set to 'global' in the sudo configuration file. The assesment of the risk of setting the security relevant setting timestamp_type to 'global' needs to be done by the user. * Explain sudoers settings and limitations in user documentation.
1 parent 8b0fc37 commit 257767d

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

autoload/suda.vim

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,24 @@ function! suda#system(cmd, ...) abort
99
if v:shell_error == 0
1010
return result
1111
endif
12-
try
13-
call inputsave()
14-
redraw | let password = inputsecret(g:suda#prompt)
15-
finally
16-
call inputrestore()
17-
endtry
18-
let cmd = printf('%s -p '''' -S %s', g:suda#executable, a:cmd)
12+
" Let's try running a command non-interactively. If it works, we have a sudo
13+
" timestamp that has not timed out yet. In this case there is no need to ask
14+
" for a password.
15+
" This only works if the timestamp_type is set to 'global' in the sudo
16+
" configuation file. It does not work with 'ppid', 'kernel' or 'tty'.
17+
let cmd = printf('%s -n true', g:suda#executable)
18+
let result = system(cmd)
19+
if v:shell_error == 0
20+
let cmd = printf('%s %s', g:suda#executable, a:cmd)
21+
else
22+
try
23+
call inputsave()
24+
redraw | let password = inputsecret(g:suda#prompt)
25+
finally
26+
call inputrestore()
27+
endtry
28+
let cmd = printf('%s -p '''' -S %s', g:suda#executable, a:cmd)
29+
endif
1930
return system(cmd, password . "\n" . (a:0 ? a:1 : ''))
2031
endfunction
2132

doc/suda.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ Make sure that the following shows 1.
3737
>
3838
: echo executable('sudo')
3939
<
40+
*suda* will ask for a password each time sudo is used for reading or writing.
41+
However, you can set global timestamps in your sudoers configuration, if you
42+
have sudo version 1.8.21 or higher. This will enable *suda* to reuse an
43+
existing sudo authentication token. In this case, it will not ask for a
44+
password if not needed. To enable, configure sudo with this option:
45+
>
46+
Defaults timestamp_type = global
47+
<
48+
The other types 'ppid', 'kernel' or 'tty' will not allow *suda* to use sudo
49+
credential caching. Please make sure this is in line with your security
50+
requirements.
4051

4152
=============================================================================
4253
USAGE *suda-usage*

0 commit comments

Comments
 (0)