Skip to content

Commit 124dfb1

Browse files
committed
Detect "system editor" at runtime on Linux
On Linux, there is no clearly established "default", but "nano" and "vi" are obvious choices. On FreeBSD, "vi" comes with the base system so is the clear default
1 parent 29946bf commit 124dfb1

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

docs/man/sudoers.5.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ sudo's behavior can be modified by Default_Entry lines, as explained earlier. A
430430

431431
* editor
432432

433-
A colon (‘:’) separated list of editor path names used by **sudoedit** and **visudo**. For **sudoedit**, this list is used to find an editor when none of the SUDO_EDITOR, VISUAL or EDITOR environment variables are set to an editor that exists and is executable. For **visudo**, it is used as a white list of allowed editors; **visudo** will choose the editor that matches the user's SUDO_EDITOR, VISUAL or EDITOR environment variable if possible, or the first editor in the list that exists and is executable if not. Unless invoked as **sudoedit**, sudo does not preserve the SUDO_EDITOR, VISUAL or EDITOR environment variables unless they are present in the **env_keep** list. The default on Linux is _/usr/bin/editor_, on FreeBSD _/usr/vim/vi_.
433+
A colon (‘:’) separated list of editor path names used by **sudoedit** and **visudo**. For **sudoedit**, this list is used to find an editor when none of the SUDO_EDITOR, VISUAL or EDITOR environment variables are set to an editor that exists and is executable. For **visudo**, it is used as a white list of allowed editors; **visudo** will choose the editor that matches the user's SUDO_EDITOR, VISUAL or EDITOR environment variable if possible, or the first editor in the list that exists and is executable if not. Unless invoked as **sudoedit**, sudo does not preserve the SUDO_EDITOR, VISUAL or EDITOR environment variables unless they are present in the **env_keep** list. The default on Linux is _/usr/bin/editor:/usr/bin/nano:/usr/bin/vi_. On FreeBSD the default is _/usr/bin/vi_.
434434

435435
## Strings that can be used in a boolean context:
436436

src/defaults/mod.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ use settings_dsl::{
2121
result_of, storage_of,
2222
};
2323

24-
pub const SYSTEM_EDITOR: &str = if cfg!(target_os = "linux") {
25-
"/usr/bin/editor"
26-
} else {
24+
pub fn system_editor() -> &'static str {
25+
#[cfg(target_os = "linux")]
26+
for editor in ["/usr/bin/editor", "/usr/bin/nano"] {
27+
if std::path::Path::new(editor).exists() {
28+
return editor;
29+
}
30+
}
31+
2732
"/usr/bin/vi"
28-
};
33+
}
2934

3035
defaults! {
3136
always_query_group_plugin = false #ignored
@@ -61,7 +66,7 @@ defaults! {
6166
passwd_timeout = (5*60) (!= 0) {fractional_minutes}
6267
timestamp_timeout = (15*60) (!= 0) {fractional_minutes}
6368

64-
editor = SYSTEM_EDITOR
69+
editor = { system_editor() }
6570
env_editor = true
6671

6772
env_keep = ["COLORS", "DISPLAY", "HOSTNAME", "KRB5CCNAME", "LS_COLORS", "PATH",

src/sudoers/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ fn select_editor(settings: &Settings, trusted_env: bool) -> PathBuf {
345345

346346
// fallback on hardcoded path -- always provide something to the caller
347347

348-
PathBuf::from(defaults::SYSTEM_EDITOR)
348+
PathBuf::from(defaults::system_editor())
349349
}
350350

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

src/visudo/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ fn edit_sudoers_file(
273273
sudoers.visudo_editor_path(&host_name, &current_user, &current_user)
274274
} else {
275275
// there is no /etc/sudoers config yet, so use a system default
276-
PathBuf::from(crate::defaults::SYSTEM_EDITOR)
276+
PathBuf::from(crate::defaults::system_editor())
277277
};
278278

279279
loop {

0 commit comments

Comments
 (0)