Skip to content

Commit 5edb66d

Browse files
sohomdatta1squell
authored andcommitted
Use more expressive error messages when failing out in visudo
Old error message: ``` visudo: Permission denied (os error 13) ``` Newer error message: ``` visudo: Failed to open existing sudoers file at "/etc/sudoers": Permission denied (os error 13) ``` (etc) Signed-off-by: Sohom <[email protected]>
1 parent 5c1922f commit 5edb66d

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/visudo/mod.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,42 @@ fn run(file_arg: Option<&str>, perms: bool, owner: bool) -> io::Result<()> {
143143
.unwrap_or_else(candidate_sudoers_file);
144144

145145
let (sudoers_file, existed) = if sudoers_path.exists() {
146-
let file = File::options().read(true).write(true).open(sudoers_path)?;
146+
let file = File::options()
147+
.read(true)
148+
.write(true)
149+
.open(sudoers_path)
150+
.map_err(|e| {
151+
io::Error::new(
152+
e.kind(),
153+
format!(
154+
"Failed to open existing sudoers file at {:?}: {}",
155+
sudoers_path, e
156+
),
157+
)
158+
})?;
147159

148160
(file, true)
149161
} else {
150162
// Create a sudoers file if it doesn't exist.
151-
let file = File::create(sudoers_path)?;
163+
let file = File::create(sudoers_path).map_err(|e| {
164+
io::Error::new(
165+
e.kind(),
166+
format!("Failed to create sudoers file at {:?}: {}", sudoers_path, e),
167+
)
168+
})?;
152169
// ogvisudo sets the permissions of the file so it can be read and written by the user and
153170
// read by the group if the `-f` argument was passed.
154171
if file_arg.is_some() {
155-
file.set_permissions(Permissions::from_mode(0o640))?;
172+
file.set_permissions(Permissions::from_mode(0o640))
173+
.map_err(|e| {
174+
io::Error::new(
175+
e.kind(),
176+
format!(
177+
"Failed to set permissions on new sudoers file at {:?}: {}",
178+
sudoers_path, e
179+
),
180+
)
181+
})?;
156182
}
157183
(file, false)
158184
};

0 commit comments

Comments
 (0)