Skip to content

Commit babaf13

Browse files
committed
move sudoedit editor suggestion to policy object
1 parent 67181fa commit babaf13

File tree

3 files changed

+54
-51
lines changed

3 files changed

+54
-51
lines changed

src/sudo/pipeline/edit.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,16 @@ pub fn run_edit(edit_opts: SudoEditOptions) -> Result<(), Error> {
3333
let command_exit_reason = {
3434
super::log_command_execution(&context);
3535

36+
let editor = policy.preferred_editor();
37+
3638
eprintln_ignore_io_error!(
37-
"this would launch sudoedit as requested, to edit the files: {:?}",
38-
context.files_to_edit.as_slice()
39+
"this would launch sudoedit as requested, to edit the files: {:?} using editor {}",
40+
context
41+
.files_to_edit
42+
.into_iter()
43+
.flatten()
44+
.collect::<Vec<_>>(),
45+
editor.display(),
3946
);
4047

4148
Ok::<_, std::io::Error>(ExitReason::Code(42))

src/sudoers/mod.rs

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -298,67 +298,58 @@ impl Sudoers {
298298
) -> PathBuf {
299299
self.specify_host_user_runas(on_host, am_user, Some(target_user));
300300

301-
let env_editor = self.settings.env_editor();
302-
self.select_editor(env_editor)
301+
select_editor(&self.settings, self.settings.env_editor())
303302
}
303+
}
304304

305-
#[cfg_attr(not(feature = "sudoedit"), allow(unused))]
306-
pub(crate) fn sudoedit_editor_path<User: UnixUser + PartialEq<User>>(
307-
&self,
308-
on_host: &system::Hostname,
309-
am_user: &User,
310-
target_user: &User,
311-
) -> PathBuf {
312-
self.select_editor(true)
313-
}
314-
315-
fn select_editor(&self, trusted_env: bool) -> PathBuf {
316-
let blessed_editors = self.settings.editor().expect("editor is always defined");
317-
318-
let is_whitelisted = |path: &Path| -> bool {
319-
trusted_env || blessed_editors.split(':').any(|x| Path::new(x) == path)
320-
};
321-
322-
// find editor in environment, if possible
305+
/// Retrieve the chosen editor from a settings object, filtering based on whether the
306+
/// environment is trusted (sudoedit) or maybe less so (visudo)
307+
fn select_editor(settings: &Settings, trusted_env: bool) -> PathBuf {
308+
let blessed_editors = settings.editor().expect("editor is always defined");
323309

324-
for key in ["SUDO_EDITOR", "VISUAL", "EDITOR"] {
325-
if let Some(editor) = std::env::var_os(key) {
326-
let editor = PathBuf::from(editor);
310+
let is_whitelisted = |path: &Path| -> bool {
311+
trusted_env || blessed_editors.split(':').any(|x| Path::new(x) == path)
312+
};
327313

328-
let editor = if can_execute(&editor) {
329-
editor
330-
} else if let Some(editor) = resolve_path(
331-
&editor,
332-
&std::env::var("PATH").unwrap_or(env!("SUDO_PATH_DEFAULT").to_string()),
333-
) {
334-
editor
335-
} else {
336-
continue;
337-
};
314+
// find editor in environment, if possible
315+
316+
for key in ["SUDO_EDITOR", "VISUAL", "EDITOR"] {
317+
if let Some(editor) = std::env::var_os(key) {
318+
let editor = PathBuf::from(editor);
319+
320+
let editor = if can_execute(&editor) {
321+
editor
322+
} else if let Some(editor) = resolve_path(
323+
&editor,
324+
&std::env::var("PATH").unwrap_or(env!("SUDO_PATH_DEFAULT").to_string()),
325+
) {
326+
editor
327+
} else {
328+
continue;
329+
};
338330

339-
if is_whitelisted(&editor) {
340-
return editor;
341-
}
331+
if is_whitelisted(&editor) {
332+
return editor;
342333
}
343334
}
335+
}
344336

345-
// no acceptable editor found in environment, fallback on config
337+
// no acceptable editor found in environment, fallback on config
346338

347-
for editor in blessed_editors.split(':') {
348-
let editor = Path::new(editor);
349-
if can_execute(editor) {
350-
return editor.to_owned();
351-
}
339+
for editor in blessed_editors.split(':') {
340+
let editor = Path::new(editor);
341+
if can_execute(editor) {
342+
return editor.to_owned();
352343
}
344+
}
353345

354-
// fallback on hardcoded path -- always provide something to the caller
346+
// fallback on hardcoded path -- always provide something to the caller
355347

356-
PathBuf::from(if cfg!(target_os = "linux") {
357-
"/usr/bin/editor"
358-
} else {
359-
"/usr/bin/vi"
360-
})
361-
}
348+
PathBuf::from(if cfg!(target_os = "linux") {
349+
"/usr/bin/editor"
350+
} else {
351+
"/usr/bin/vi"
352+
})
362353
}
363354

364355
// a `take_while` variant that does not consume the first non-matching item

src/sudoers/policy.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ impl Judgement {
142142
Authorization::Forbidden
143143
}
144144
}
145+
146+
#[cfg_attr(not(feature = "sudoedit"), allow(unused))]
147+
pub(crate) fn preferred_editor(&self) -> std::path::PathBuf {
148+
super::select_editor(&self.settings, true)
149+
}
145150
}
146151

147152
impl Sudoers {

0 commit comments

Comments
 (0)