@@ -18,7 +18,6 @@ use crate::{
1818 sudo:: { candidate_sudoers_file, diagnostic} ,
1919 sudoers:: { self , Sudoers } ,
2020 system:: {
21- can_execute,
2221 file:: { create_temporary_dir, Chown , FileLock } ,
2322 interface:: { GroupId , UserId } ,
2423 signal:: { consts:: * , register_handlers, SignalStream } ,
@@ -248,7 +247,6 @@ fn edit_sudoers_file(
248247) -> io:: Result < ( ) > {
249248 let mut stderr = io:: stderr ( ) ;
250249
251- let mut editor_path = None ;
252250 let mut sudoers_contents = Vec :: new ( ) ;
253251
254252 // Since visudo is meant to run as root, resolve shouldn't fail
@@ -262,31 +260,36 @@ fn edit_sudoers_file(
262260
263261 let host_name = Hostname :: resolve ( ) ;
264262
265- if existed {
263+ let editor_path = if existed {
266264 // If the sudoers file existed, read its contents and write them into the temporary file.
267265 sudoers_file. read_to_end ( & mut sudoers_contents) ?;
268266 // Rewind the sudoers file so it can be written later.
269267 sudoers_file. rewind ( ) ?;
270268 // Write to the temporary file.
271269 tmp_file. write_all ( & sudoers_contents) ?;
272270
273- let ( sudoers, errors ) = Sudoers :: read ( sudoers_contents. as_slice ( ) , sudoers_path) ?;
271+ let ( sudoers, _errors ) = Sudoers :: read ( sudoers_contents. as_slice ( ) , sudoers_path) ?;
274272
275- if errors. is_empty ( ) {
276- editor_path = sudoers. solve_editor_path ( & host_name, & current_user, & current_user) ;
277- }
278- }
279-
280- let editor_path = match editor_path {
281- Some ( path) => path,
282- None => editor_path_fallback ( ) ?,
273+ sudoers. visudo_editor_path ( & host_name, & current_user, & current_user)
274+ } else {
275+ // there is no /etc/sudoers config yet, so use a system default
276+ PathBuf :: from ( crate :: defaults:: SYSTEM_EDITOR )
283277 } ;
284278
285279 loop {
286280 Command :: new ( & editor_path)
287281 . arg ( "--" )
288282 . arg ( tmp_path)
289- . spawn ( ) ?
283+ . spawn ( )
284+ . map_err ( |_| {
285+ io:: Error :: new (
286+ io:: ErrorKind :: NotFound ,
287+ format ! (
288+ "specified editor ({}) could not be used" ,
289+ editor_path. display( )
290+ ) ,
291+ )
292+ } ) ?
290293 . wait_with_output ( ) ?;
291294
292295 let ( sudoers, errors) = File :: open ( tmp_path)
@@ -354,27 +357,6 @@ fn edit_sudoers_file(
354357 Ok ( ( ) )
355358}
356359
357- fn editor_path_fallback ( ) -> io:: Result < PathBuf > {
358- let editors = if cfg ! ( target_os = "linux" ) {
359- & [ "/usr/bin/editor" ] [ ..]
360- } else if cfg ! ( target_os = "freebsd" ) {
361- & [ "/usr/bin/vi" ]
362- } else {
363- unimplemented ! ( "unknown default editor for target" )
364- } ;
365- for & editor in editors {
366- let path = Path :: new ( editor) ;
367- if can_execute ( path) {
368- return Ok ( path. to_owned ( ) ) ;
369- }
370- }
371-
372- Err ( io:: Error :: new (
373- io:: ErrorKind :: NotFound ,
374- "cannot find text editor" ,
375- ) )
376- }
377-
378360// To detect potential lock-outs if the user called "sudo visudo".
379361// Note that SUDO_USER will normally be set by sudo.
380362//
0 commit comments