Debian Codegraph @

src/visudo/mod.rs

raw symbols SCIP
1 #![forbid(unsafe_code)]
2
3 mod clirust-analyzer cargo sudo-rs 0.2.14 visudo/cli/;
4 mod helprust-analyzer cargo sudo-rs 0.2.14 visudo/help/;
5
6 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{
7 envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/, ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/,
8 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::{Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#, Permissionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/Permissions#},
9 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#, Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#},
10 osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::{
11 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/::fchownrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/fchown().,
12 preluderust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/prelude/::{MetadataExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/MetadataExt#, PermissionsExtrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/PermissionsExt#},
13 },
14 pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::{Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#},
15 processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#,
16 strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/,
17 };
18
19 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::{
20 commonrust-analyzer cargo sudo-rs 0.2.14 common/::resolverust-analyzer cargo sudo-rs 0.2.14 common/resolve/::CurrentUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#,
21 sudorust-analyzer cargo sudo-rs 0.2.14 sudo/::{candidate_sudoers_filerust-analyzer cargo sudo-rs 0.2.14 sudo/candidate_sudoers_file()., diagnosticrust-analyzer cargo sudo-rs 0.2.14 sudo/diagnostic/},
22 sudoersrust-analyzer cargo sudo-rs 0.2.14 sudoers/::{selfrust-analyzer cargo sudo-rs 0.2.14 sudoers/, Sudoersrust-analyzer cargo sudo-rs 0.2.14 sudoers/Sudoers#},
23 systemrust-analyzer cargo sudo-rs 0.2.14 system/::{
24 Hostnamerust-analyzer cargo sudo-rs 0.2.14 system/Hostname#, Userrust-analyzer cargo sudo-rs 0.2.14 system/User#,
25 filerust-analyzer cargo sudo-rs 0.2.14 system/file/::{FileLockrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/FileLock#, create_temporary_dirrust-analyzer cargo sudo-rs 0.2.14 system/file/tmpdir/create_temporary_dir().},
26 interfacerust-analyzer cargo sudo-rs 0.2.14 system/interface/::UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#,
27 signalrust-analyzer cargo sudo-rs 0.2.14 system/signal/::{SignalStreamrust-analyzer cargo sudo-rs 0.2.14 system/signal/stream/SignalStream#, SignalsStaterust-analyzer cargo sudo-rs 0.2.14 system/signal/state/SignalsState#, constsrust-analyzer cargo sudo-rs 0.2.14 system/signal/consts/::*, register_handlersrust-analyzer cargo sudo-rs 0.2.14 system/signal/stream/register_handlers().},
28 },
29 };
30
31 use selfrust-analyzer cargo sudo-rs 0.2.14 visudo/::clirust-analyzer cargo sudo-rs 0.2.14 visudo/cli/::{VisudoActionrust-analyzer cargo sudo-rs 0.2.14 visudo/cli/VisudoAction#, VisudoOptionsrust-analyzer cargo sudo-rs 0.2.14 visudo/cli/VisudoOptions#};
32 use selfrust-analyzer cargo sudo-rs 0.2.14 visudo/::helprust-analyzer cargo sudo-rs 0.2.14 visudo/help/::{USAGE_MSGrust-analyzer cargo sudo-rs 0.2.14 visudo/help/USAGE_MSG., long_help_messagerust-analyzer cargo sudo-rs 0.2.14 visudo/help/long_help_message().};
33
34 const VERSIONrust-analyzer cargo sudo-rs 0.2.14 visudo/VERSION.: &str = envrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/env!!("CARGO_PKG_VERSION");
35
36 macro_rules! io_msgrust-analyzer cargo sudo-rs 0.2.14 visudo/io_msg! {
37 ($err:expr, $($tt:tt)*) => {
38 io::Error::new($err.kind(), format!("{}: {}", format_args!($($tt)*), $err))
39 };
40 }
41
42 pub fn mainrust-analyzer cargo sudo-rs 0.2.14 visudo/main().() {
43 if Userrust-analyzer cargo sudo-rs 0.2.14 system/User#::effective_uidrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]effective_uid().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().Userrust-analyzer cargo sudo-rs 0.2.14 system/User#::real_uidrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]real_uid().() || Userrust-analyzer cargo sudo-rs 0.2.14 system/User#::effective_gidrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]effective_gid().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().Userrust-analyzer cargo sudo-rs 0.2.14 system/User#::real_gidrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]real_gid().() {
44 println_ignore_io_errorrust-analyzer cargo sudo-rs 0.2.14 macros/println_ignore_io_error!!(
45 "Visudo must not be installed as setuid binary.\n\
46 Please notify your packager about this misconfiguration.\n\
47 To prevent privilege escalation visudo will now abort.
48 "
49 );
50 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(1);
51 }
52
53 let options = match VisudoOptionsrust-analyzer cargo sudo-rs 0.2.14 visudo/cli/VisudoOptions#::from_envrust-analyzer cargo sudo-rs 0.2.14 visudo/cli/impl#[VisudoOptions]from_env().() {
54 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(options) => options,
55 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(error) => {
56 println_ignore_io_errorrust-analyzer cargo sudo-rs 0.2.14 macros/println_ignore_io_error!!("visudo: {error}\n{USAGE_MSG}");
57 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(1);
58 }
59 };
60
61 let cmd = match options.actionrust-analyzer cargo sudo-rs 0.2.14 visudo/cli/VisudoOptions#action. {
62 VisudoActionrust-analyzer cargo sudo-rs 0.2.14 visudo/cli/VisudoAction#::Helprust-analyzer cargo sudo-rs 0.2.14 visudo/cli/VisudoAction#Help# => {
63 println_ignore_io_errorrust-analyzer cargo sudo-rs 0.2.14 macros/println_ignore_io_error!!("{}", long_help_messagerust-analyzer cargo sudo-rs 0.2.14 visudo/help/long_help_message().());
64 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(0);
65 }
66 VisudoActionrust-analyzer cargo sudo-rs 0.2.14 visudo/cli/VisudoAction#::Versionrust-analyzer cargo sudo-rs 0.2.14 visudo/cli/VisudoAction#Version# => {
67 println_ignore_io_errorrust-analyzer cargo sudo-rs 0.2.14 macros/println_ignore_io_error!!("visudo-rs {VERSION}");
68 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(0);
69 }
70 VisudoActionrust-analyzer cargo sudo-rs 0.2.14 visudo/cli/VisudoAction#::Checkrust-analyzer cargo sudo-rs 0.2.14 visudo/cli/VisudoAction#Check# => checkrust-analyzer cargo sudo-rs 0.2.14 visudo/check().,
71 VisudoActionrust-analyzer cargo sudo-rs 0.2.14 visudo/cli/VisudoAction#::Runrust-analyzer cargo sudo-rs 0.2.14 visudo/cli/VisudoAction#Run# => runrust-analyzer cargo sudo-rs 0.2.14 visudo/run().,
72 };
73
74 match cmd(options.filerust-analyzer cargo sudo-rs 0.2.14 visudo/cli/VisudoOptions#file..as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().(), options.permsrust-analyzer cargo sudo-rs 0.2.14 visudo/cli/VisudoOptions#perms., options.ownerrust-analyzer cargo sudo-rs 0.2.14 visudo/cli/VisudoOptions#owner.) {
75 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) => {}
76 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(error) => {
77 eprintln_ignore_io_errorrust-analyzer cargo sudo-rs 0.2.14 macros/eprintln_ignore_io_error!!("visudo: {error}");
78 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(1);
79 }
80 }
81 }
82
83 fn checkrust-analyzer cargo sudo-rs 0.2.14 visudo/check().(file_arg: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str>, perms: bool, owner: bool) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> {
84 let mut sudoers_path = file_arg
85 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`From<&T>`]from().)
86 .unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_else().(candidate_sudoers_filerust-analyzer cargo sudo-rs 0.2.14 sudo/candidate_sudoers_file().);
87
88 let sudoers_file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(if sudoers_path rust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`PartialEq<&Path>`]eq().==rust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`PartialEq<&Path>`]eq(). rust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`PartialEq<&Path>`]eq().Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().("-") {
89 // portability: /dev/stdin 'almost POSIX' and exists on BSD and Linux systems
90 sudoers_path = PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`From<&T>`]from().("stdin");
91 Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().("/dev/stdin")
92 } else {
93 &sudoers_path
94 })
95 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| io_msgrust-analyzer cargo sudo-rs 0.2.14 visudo/io_msg!!(err, "unable to open {}", sudoers_path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
96
97 let metadata = sudoers_file.metadatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]metadata().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
98
99 if file_arg.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() || perms {
100 // For some reason, the MSB of the mode is on so we need to mask it.
101 let mode = metadata.permissionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[Metadata]permissions().().moderust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/impl#[Permissions][PermissionsExt]mode().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().&rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[u32][`BitAnd<Self>`]bitand().0o777;
102
103 if mode rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]ne().0o440 {
104 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]other().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
105 "{}: bad permissions, should be mode 0440, but found {mode:04o}",
106 sudoers_path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().()
107 )));
108 }
109 }
110
111 if file_arg.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() || owner {
112 let owner = (metadata.uidrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/impl#[`fs::Metadata`][MetadataExt]uid().(), metadata.gidrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/impl#[`fs::Metadata`][MetadataExt]gid().());
113
114 if owner rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core tuple/impl#[`(U, T)`][`PartialEq<Self>`]ne().(0, 0) {
115 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]other().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
116 "{}: wrong owner (uid, gid) should be (0, 0), but found {owner:?}",
117 sudoers_path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().()
118 )));
119 }
120 }
121
122 let (_sudoers, errors) = Sudoersrust-analyzer cargo sudo-rs 0.2.14 sudoers/Sudoers#::readrust-analyzer cargo sudo-rs 0.2.14 sudoers/impl#[Sudoers]read().(&sudoers_file, &sudoers_path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
123
124 if errors.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() {
125 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdout().(), "{}: parsed OK", sudoers_path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
126 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(());
127 }
128
129 for craterust-analyzer cargo sudo-rs 0.2.14 crate/::sudoersrust-analyzer cargo sudo-rs 0.2.14 sudoers/::Errorrust-analyzer cargo sudo-rs 0.2.14 sudoers/Error# {
130 messagerust-analyzer cargo sudo-rs 0.2.14 sudoers/Error#message.,
131 sourcerust-analyzer cargo sudo-rs 0.2.14 sudoers/Error#source.,
132 locationrust-analyzer cargo sudo-rs 0.2.14 sudoers/Error#location.,
133 } in errors
134 {
135 let path = source.as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(&sudoers_path);
136 diagnosticrust-analyzer cargo sudo-rs 0.2.14 sudo/diagnostic/::diagnosticrust-analyzer cargo sudo-rs 0.2.14 sudo/diagnostic/diagnostic!!("syntax error: {message}", path @ location);
137 }
138
139 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]other().("invalid sudoers file"))
140 }
141
142 fn runrust-analyzer cargo sudo-rs 0.2.14 visudo/run().(file_arg: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<&str>, perms: bool, owner: bool) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> {
143 let sudoers_path = &file_arg
144 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`From<&T>`]from().)
145 .unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_else().(candidate_sudoers_filerust-analyzer cargo sudo-rs 0.2.14 sudo/candidate_sudoers_file().);
146
147 let (sudoers_file, existed) = if sudoers_path.existsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]exists().() {
148 let file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::optionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]options().()
149 .readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]read().(true)
150 .writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]write().(true)
151 .openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]open().(sudoers_path)
152 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| {
153 io_msgrust-analyzer cargo sudo-rs 0.2.14 visudo/io_msg!!(
154 err,
155 "Failed to open existing sudoers file at {sudoers_path:?}"
156 )
157 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
158
159 (file, true)
160 } else {
161 // Create a sudoers file if it doesn't exist.
162 let file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]create().(sudoers_path)
163 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| io_msgrust-analyzer cargo sudo-rs 0.2.14 visudo/io_msg!!(err, "Failed to create sudoers file at {sudoers_path:?}"))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
164
165 // ogvisudo sets the permissions of the file so it can be read and written by the user and
166 // read by the group if the `-f` argument was passed.
167 if file_arg.is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() {
168 file.set_permissionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]set_permissions().(Permissionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/Permissions#::from_moderust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/impl#[Permissions][PermissionsExt]from_mode().(0o640))
169 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| {
170 io_msgrust-analyzer cargo sudo-rs 0.2.14 visudo/io_msg!!(
171 err,
172 "Failed to set permissions on new sudoers file at {sudoers_path:?}"
173 )
174 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
175 }
176 (file, false)
177 };
178
179 let lock = FileLockrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/FileLock#::exclusiverust-analyzer cargo sudo-rs 0.2.14 system/file/lock/impl#[FileLock]exclusive().(&sudoers_file, true).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| {
180 if err.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().() rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[ErrorKind][`PartialEq<Self>`]eq().==rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[ErrorKind][`PartialEq<Self>`]eq(). rust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[ErrorKind][`PartialEq<Self>`]eq().iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::WouldBlockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#WouldBlock# {
181 io_msgrust-analyzer cargo sudo-rs 0.2.14 visudo/io_msg!!(err, "{} busy, try again later", sudoers_path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().())
182 } else {
183 err
184 }
185 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
186
187 if perms || file_arg.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() {
188 sudoers_file.set_permissionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]set_permissions().(Permissionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/Permissions#::from_moderust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/impl#[Permissions][PermissionsExt]from_mode().(0o440))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
189 }
190
191 if owner || file_arg.is_nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_none().() {
192 fchownrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/fchown().(&sudoers_file, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(0), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(0))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
193 }
194
195 let signal_stream = SignalStreamrust-analyzer cargo sudo-rs 0.2.14 system/signal/stream/SignalStream#::initrust-analyzer cargo sudo-rs 0.2.14 system/signal/stream/impl#[SignalStream]init().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
196
197 let handlers = register_handlersrust-analyzer cargo sudo-rs 0.2.14 system/signal/stream/register_handlers().(
198 [SIGTERMrust-analyzer cargo libc 0.2.186 unix/linux_like/SIGTERM., SIGHUPrust-analyzer cargo libc 0.2.186 unix/linux_like/SIGHUP., SIGINTrust-analyzer cargo libc 0.2.186 unix/linux_like/SIGINT., SIGQUITrust-analyzer cargo libc 0.2.186 unix/linux_like/SIGQUIT.],
199 &mut SignalsStaterust-analyzer cargo sudo-rs 0.2.14 system/signal/state/SignalsState#::saverust-analyzer cargo sudo-rs 0.2.14 system/signal/state/impl#[SignalsState]save().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
200 )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
201
202 let tmp_dir = create_temporary_dirrust-analyzer cargo sudo-rs 0.2.14 system/file/tmpdir/create_temporary_dir().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
203 let tmp_path = tmp_dir.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().("sudoers");
204
205 {
206 let tmp_dir = tmp_dir.clonerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][Clone]clone().();
207 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::threadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/::spawnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/functions/spawn().(|| -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> {
208 signal_stream.recvrust-analyzer cargo sudo-rs 0.2.14 system/signal/stream/impl#[SignalStream]recv().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
209
210 let _ = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::remove_dir_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/remove_dir_all().(tmp_dir);
211
212 droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(handlers);
213
214 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::exitrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/exit().(1)
215 });
216 }
217
218 let tmp_file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::optionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]options().()
219 .readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]read().(true)
220 .writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]write().(true)
221 .createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]create().(true)
222 .truncaterust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]truncate().(true)
223 .openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[OpenOptions]open().(&tmp_path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
224
225 tmp_file.set_permissionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]set_permissions().(Permissionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/Permissions#::from_moderust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/impl#[Permissions][PermissionsExt]from_mode().(0o600))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
226
227 let result = edit_sudoers_filerust-analyzer cargo sudo-rs 0.2.14 visudo/edit_sudoers_file().(
228 existed,
229 sudoers_file,
230 sudoers_path,
231 lock,
232 tmp_file,
233 &tmp_path,
234 );
235
236 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::remove_dir_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/remove_dir_all().(tmp_dir)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
237
238 result
239 }
240
241 fn edit_sudoers_filerust-analyzer cargo sudo-rs 0.2.14 visudo/edit_sudoers_file().(
242 existed: bool,
243 mut sudoers_file: Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#,
244 sudoers_path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#,
245 lock: FileLockrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/FileLock#,
246 mut tmp_file: Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#,
247 tmp_path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#,
248 ) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> {
249 let mut stderr = iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stderrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stderr().();
250
251 let mut sudoers_contents = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().();
252
253 // Since visudo is meant to run as root, resolve shouldn't fail
254 let current_user: Userrust-analyzer cargo sudo-rs 0.2.14 system/User# = match CurrentUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#::resolverust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[CurrentUser]resolve().() {
255 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(user) => user.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
256 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => {
257 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(stderr, "visudo: cannot resolve : {err}")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
258 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(());
259 }
260 };
261
262 let host_name = Hostnamerust-analyzer cargo sudo-rs 0.2.14 system/Hostname#::resolverust-analyzer cargo sudo-rs 0.2.14 system/impl#[Hostname]resolve().();
263
264 if existed {
265 // If the sudoers file existed, read its contents and write them into the temporary file.
266 sudoers_file.read_to_endrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Read]read_to_end().(&mut sudoers_contents)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
267 // Rewind the sudoers file so it can be written later.
268 sudoers_file.rewindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#rewind().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
269 // Write to the temporary file.
270 tmp_file.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(&sudoers_contents)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
271 }
272
273 let editor_path = Sudoersrust-analyzer cargo sudo-rs 0.2.14 sudoers/Sudoers#::readrust-analyzer cargo sudo-rs 0.2.14 sudoers/impl#[Sudoers]read().(sudoers_contents.as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().(), sudoers_path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
274 .0
275 .visudo_editor_pathrust-analyzer cargo sudo-rs 0.2.14 sudoers/impl#[Sudoers]visudo_editor_path().(&host_name, &current_user, &current_user)
276 .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| {
277 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::NotFoundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#NotFound#, "no usable editor could be found")
278 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
279
280 loop {
281 Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]new().(&editor_path.0)
282 .argsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]args().(&editor_path.1)
283 .argrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]arg().("--")
284 .argrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]arg().(tmp_path)
285 .spawnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]spawn().()
286 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| {
287 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(
288 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::NotFoundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#NotFound#,
289 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!(
290 "specified editor ({}) could not be used",
291 editor_path.0.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().()
292 ),
293 )
294 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().
295 .wait_with_outputrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Child]wait_with_output().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
296
297 let (sudoers, errors) = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().(tmp_path)
298 .and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]and_then().(|reader| Sudoersrust-analyzer cargo sudo-rs 0.2.14 sudoers/Sudoers#::readrust-analyzer cargo sudo-rs 0.2.14 sudoers/impl#[Sudoers]read().(reader, sudoers_path))
299 .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| {
300 io_msgrust-analyzer cargo sudo-rs 0.2.14 visudo/io_msg!!(
301 err,
302 "unable to re-open temporary file ({}), {} unchanged",
303 tmp_path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().(),
304 sudoers_path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().()
305 )
306 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
307
308 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().errors.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() {
309 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(
310 stderr,
311 "The provided sudoers file format is not recognized or contains syntax errors. Please review:\n"
312 )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
313
314 for craterust-analyzer cargo sudo-rs 0.2.14 crate/::sudoersrust-analyzer cargo sudo-rs 0.2.14 sudoers/::Errorrust-analyzer cargo sudo-rs 0.2.14 sudoers/Error# {
315 messagerust-analyzer cargo sudo-rs 0.2.14 sudoers/Error#message.,
316 sourcerust-analyzer cargo sudo-rs 0.2.14 sudoers/Error#source.,
317 locationrust-analyzer cargo sudo-rs 0.2.14 sudoers/Error#location.,
318 } in errors
319 {
320 let path = source.as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or().(sudoers_path);
321 diagnosticrust-analyzer cargo sudo-rs 0.2.14 sudo/diagnostic/::diagnosticrust-analyzer cargo sudo-rs 0.2.14 sudo/diagnostic/diagnostic!!("syntax error: {message}", path @ location);
322 }
323
324 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(stderr)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
325
326 match ask_responserust-analyzer cargo sudo-rs 0.2.14 visudo/ask_response().(
327 "What now? e(x)it without saving / (e)dit again: ",
328 "xe",
329 'x',
330 )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). {
331 'x' => return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()),
332 _ => continue,
333 }
334 } else {
335 if sudoers_path rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]new().("/etc/sudoers")
336 && sudo_visudo_is_allowedrust-analyzer cargo sudo-rs 0.2.14 visudo/sudo_visudo_is_allowed().(sudoers, &host_name) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][`PartialEq<Self>`]eq().Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(false)
337 {
338 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(
339 stderr,
340 "It looks like you have removed your ability to run 'sudo visudo' again.\n"
341 )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
342 match ask_responserust-analyzer cargo sudo-rs 0.2.14 visudo/ask_response().(
343 "What now? e(x)it without saving / (e)dit again / lock me out and (S)ave: ",
344 "xeS",
345 'x',
346 )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). {
347 'x' => return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()),
348 'S' => {}
349 _ => continue,
350 }
351 }
352
353 break;
354 }
355 }
356
357 let tmp_contents = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read().(tmp_path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
358 // Only write to the sudoers file if the contents changed.
359 if tmp_contents rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/partial_eq/impl#[`Vec<T, A1>`][`PartialEq<Vec<U, A2>>`]eq().==rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/partial_eq/impl#[`Vec<T, A1>`][`PartialEq<Vec<U, A2>>`]eq(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/partial_eq/impl#[`Vec<T, A1>`][`PartialEq<Vec<U, A2>>`]eq().sudoers_contents {
360 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(stderr, "visudo: {} unchanged", tmp_path.displayrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]display().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
361 } else {
362 sudoers_file.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(&tmp_contents)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
363 let new_size = sudoers_file.stream_positionrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File][Seek]stream_position().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
364 sudoers_file.set_lenrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]set_len().(new_size)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
365 }
366
367 lock.unlockrust-analyzer cargo sudo-rs 0.2.14 system/file/lock/impl#[FileLock]unlock().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
368
369 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
370 }
371
372 // To detect potential lock-outs if the user called "sudo visudo".
373 // Note that SUDO_USER will normally be set by sudo.
374 //
375 // This returns Some(false) if visudo is forbidden under the given config;
376 // Some(true) if it is allowed; and None if it cannot be determined, which
377 // will be the case if e.g. visudo was simply run as root.
378 fn sudo_visudo_is_allowedrust-analyzer cargo sudo-rs 0.2.14 visudo/sudo_visudo_is_allowed().(mut sudoers: Sudoersrust-analyzer cargo sudo-rs 0.2.14 sudoers/Sudoers#, host_name: &Hostnamerust-analyzer cargo sudo-rs 0.2.14 system/Hostname#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<bool> {
379 let sudo_user =
380 Userrust-analyzer cargo sudo-rs 0.2.14 system/User#::from_namerust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]from_name().(&ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/CString#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/impl#[CString]new().(envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::varrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var().("SUDO_USER").okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
381
382 let super_user = Userrust-analyzer cargo sudo-rs 0.2.14 system/User#::from_uidrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]from_uid().(UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#::ROOTrust-analyzer cargo sudo-rs 0.2.14 system/interface/impl#[UserId]ROOT.).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().;
383
384 let request = sudoersrust-analyzer cargo sudo-rs 0.2.14 sudoers/::Requestrust-analyzer cargo sudo-rs 0.2.14 sudoers/Request# {
385 userrust-analyzer cargo sudo-rs 0.2.14 sudoers/Request#user.: &super_user,
386 grouprust-analyzer cargo sudo-rs 0.2.14 sudoers/Request#group.: &super_user.primary_grouprust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]primary_group().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().,
387 commandrust-analyzer cargo sudo-rs 0.2.14 sudoers/Request#command.: &envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::current_exerust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/current_exe().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().,
388 argumentsrust-analyzer cargo sudo-rs 0.2.14 sudoers/Request#arguments.: &[],
389 };
390
391 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(
392 sudoers
393 .checkrust-analyzer cargo sudo-rs 0.2.14 sudoers/impl#[Sudoers]check().(&sudo_user, host_name, request)
394 .authorizationrust-analyzer cargo sudo-rs 0.2.14 sudoers/policy/impl#[Judgement]authorization().(),
395 sudoersrust-analyzer cargo sudo-rs 0.2.14 sudoers/::Authorizationrust-analyzer cargo sudo-rs 0.2.14 sudoers/policy/Authorization#::Allowedrust-analyzer cargo sudo-rs 0.2.14 sudoers/policy/Authorization#Allowed# { .. }
396 ))
397 }
398
399 // This will panic if valid_responses is empty.
400 pub(craterust-analyzer cargo sudo-rs 0.2.14 crate/) fn ask_responserust-analyzer cargo sudo-rs 0.2.14 visudo/ask_response().(
401 prompt: &str,
402 valid_responses: &str,
403 safe_choice: char,
404 ) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<char> {
405 let stdin = iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stdinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdin().();
406 let stdout = iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stdout().();
407 let mut stderr = iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::stderrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/stderr().();
408
409 let stdin_handle = stdin.lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[Stdin]lock().();
410 let mut stdout_handle = stdout.lockrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[Stdout]lock().();
411
412 let mut lines = stdin_handle.linesrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#lines().();
413
414 loop {
415 stdout_handle.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[`StdoutLock<'_>`][Write]write_all().(prompt.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
416 stdout_handle.flushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/stdio/impl#[`StdoutLock<'_>`][Write]flush().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
417
418 match lines.nextrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/impl#[`Lines<B>`][Iterator]next().() {
419 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(answer))
420 if answer
421 .charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().()
422 .nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().()
423 .is_some_andrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some_and().(|input| valid_responses.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().(input)) =>
424 {
425 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(answer.charsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]chars().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`Chars<'a>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().());
426 }
427 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(answer)) => writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(stderr, "Invalid option: '{answer}'\n",)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
428 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err)) => writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(stderr, "Invalid response: {err}\n",)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
429 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => {
430 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(stderr, "visudo: cannot read user input")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
431 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(safe_choice);
432 }
433 }
434 }
435 }