Debian Codegraph @

src/common/resolve.rs

raw symbols SCIP
1 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::systemrust-analyzer cargo sudo-rs 0.2.14 system/::interfacerust-analyzer cargo sudo-rs 0.2.14 system/interface/::UserIdrust-analyzer cargo sudo-rs 0.2.14 system/interface/UserId#;
2 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::systemrust-analyzer cargo sudo-rs 0.2.14 system/::{Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#, Userrust-analyzer cargo sudo-rs 0.2.14 system/User#};
3 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/;
4 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{
5 envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/,
6 ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#,
7 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/, iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/,
8 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/::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#,
9 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#},
10 strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#,
11 };
12
13 use superrust-analyzer cargo sudo-rs 0.2.14 common/::SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#;
14 use superrust-analyzer cargo sudo-rs 0.2.14 common/::{Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#, contextrust-analyzer cargo sudo-rs 0.2.14 common/context/::LaunchTyperust-analyzer cargo sudo-rs 0.2.14 common/context/LaunchType#};
15
16 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
17 enum NameOrIdrust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#<'a, T: FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#> {
18 Namerust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#Name#(&'a SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#),
19 Idrust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#Id#(T),
20 }
21
22 impl<'a, T: FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#> NameOrIdrust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#<'a, T> {
23 pub fn parserust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[`NameOrId<'a, T>`]parse().(input: &'a SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[`NameOrId<'a, T>`]> {
24 if input.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() {
25 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#
26 } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(stripped) = input.strip_prefixrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]strip_prefix().('#') {
27 stripped.parserust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]parse().::<T>().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|id| Selfrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[`NameOrId<'a, T>`]::Idrust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#Id#(id))
28 } else {
29 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Selfrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[`NameOrId<'a, T>`]::Namerust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#Name#(input))
30 }
31 }
32 }
33
34 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
35 pub struct CurrentUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser# {
36 innerrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#inner.: Userrust-analyzer cargo sudo-rs 0.2.14 system/User#,
37 }
38
39 impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<CurrentUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#> for Userrust-analyzer cargo sudo-rs 0.2.14 system/User# {
40 fn fromrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[User][`From<CurrentUser>`]from().(value: CurrentUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#) -> Selfrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[User][`From<CurrentUser>`] {
41 value.innerrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#inner.
42 }
43 }
44
45 impl fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for CurrentUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser# {
46 fn fmtrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[CurrentUser][Debug]fmt().(&self, f: &mut fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
47 f.debug_tuplerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]debug_tuple().("CurrentUser").fieldrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugTuple<'a, 'b>`]field().(&self.innerrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#inner.).finishrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/builders/impl#[`DebugTuple<'a, 'b>`]finish().()
48 }
49 }
50
51 impl opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref# for CurrentUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser# {
52 type Targetrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[CurrentUser][Deref]Target# = Userrust-analyzer cargo sudo-rs 0.2.14 system/User#;
53
54 fn derefrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[CurrentUser][Deref]deref().(&self) -> &Selfrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[CurrentUser][Deref]::Targetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#Target# {
55 &self.innerrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#inner.
56 }
57 }
58
59 impl CurrentUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser# {
60 #[cfg(test)]
61 pub fn fakerust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[CurrentUser]fake().(user: Userrust-analyzer cargo sudo-rs 0.2.14 system/User#) -> Selfrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[CurrentUser] {
62 Selfrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[CurrentUser] { innerrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#inner.: user }
63 }
64
65 pub fn resolverust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[CurrentUser]resolve().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[CurrentUser], Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#> {
66 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[CurrentUser] {
67 innerrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#inner.: Userrust-analyzer cargo sudo-rs 0.2.14 system/User#::realrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]real().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#::UserNotFoundrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#UserNotFound#("current user".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
68 })
69 }
70 }
71
72 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
73 pub struct AuthUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/AuthUser#(Userrust-analyzer cargo sudo-rs 0.2.14 system/User#);
74
75 impl AuthUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/AuthUser# {
76 pub fn from_current_userrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[AuthUser]from_current_user().(user: CurrentUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#) -> Selfrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[AuthUser] {
77 Selfrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[AuthUser](user.innerrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#inner.)
78 }
79
80 pub fn resolve_root_for_rootpwrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[AuthUser]resolve_root_for_rootpw().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[AuthUser], Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#> {
81 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[AuthUser](
82 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.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..ok_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or().(Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#::UserNotFoundrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#UserNotFound#("root".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
83 ))
84 }
85
86 pub fn from_user_for_targetpwrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[AuthUser]from_user_for_targetpw().(user: Userrust-analyzer cargo sudo-rs 0.2.14 system/User#) -> Selfrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[AuthUser] {
87 Selfrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[AuthUser](user)
88 }
89 }
90
91 impl opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref# for AuthUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/AuthUser# {
92 type Targetrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[AuthUser][Deref]Target# = Userrust-analyzer cargo sudo-rs 0.2.14 system/User#;
93
94 fn derefrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[AuthUser][Deref]deref().(&self) -> &Selfrust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[AuthUser][Deref]::Targetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#Target# {
95 &self.0rust-analyzer cargo sudo-rs 0.2.14 common/resolve/AuthUser#0.
96 }
97 }
98
99 type Shellrust-analyzer cargo sudo-rs 0.2.14 common/resolve/Shell# = Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#>;
100
101 pub(superrust-analyzer cargo sudo-rs 0.2.14 common/) fn resolve_shellrust-analyzer cargo sudo-rs 0.2.14 common/resolve/resolve_shell().(
102 launch_type: LaunchTyperust-analyzer cargo sudo-rs 0.2.14 common/context/LaunchType#,
103 current_user: &Userrust-analyzer cargo sudo-rs 0.2.14 system/User#,
104 target_user: &Userrust-analyzer cargo sudo-rs 0.2.14 system/User#,
105 ) -> Shellrust-analyzer cargo sudo-rs 0.2.14 common/resolve/Shell# {
106 match launch_type {
107 LaunchTyperust-analyzer cargo sudo-rs 0.2.14 common/context/LaunchType#::Loginrust-analyzer cargo sudo-rs 0.2.14 common/context/LaunchType#Login# => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(target_user.shellrust-analyzer cargo sudo-rs 0.2.14 system/User#shell..clonerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][Clone]clone().()),
108
109 LaunchTyperust-analyzer cargo sudo-rs 0.2.14 common/context/LaunchType#::Shellrust-analyzer cargo sudo-rs 0.2.14 common/context/LaunchType#Shell# => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(
110 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().("SHELL")
111 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|s| s.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())
112 .unwrap_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or_else().(|_| current_user.shellrust-analyzer cargo sudo-rs 0.2.14 system/User#shell..clonerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][Clone]clone().()),
113 ),
114
115 LaunchTyperust-analyzer cargo sudo-rs 0.2.14 common/context/LaunchType#::Directrust-analyzer cargo sudo-rs 0.2.14 common/context/LaunchType#Direct# => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
116 }
117 }
118
119 pub(craterust-analyzer cargo sudo-rs 0.2.14 crate/) fn resolve_target_user_and_grouprust-analyzer cargo sudo-rs 0.2.14 common/resolve/resolve_target_user_and_group().(
120 target_user_name_or_id: &Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#>,
121 target_group_name_or_id: &Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#>,
122 current_user: &CurrentUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#,
123 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(Userrust-analyzer cargo sudo-rs 0.2.14 system/User#, Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#), Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#> {
124 // resolve user name or #<id> to a user
125 let mut target_user =
126 resolve_from_name_or_idrust-analyzer cargo sudo-rs 0.2.14 common/resolve/resolve_from_name_or_id().(target_user_name_or_id, Userrust-analyzer cargo sudo-rs 0.2.14 system/User#::from_namerust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]from_name()., Userrust-analyzer cargo sudo-rs 0.2.14 system/User#::from_uidrust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]from_uid().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
127
128 // resolve group name or #<id> to a group
129 let mut target_group =
130 resolve_from_name_or_idrust-analyzer cargo sudo-rs 0.2.14 common/resolve/resolve_from_name_or_id().(target_group_name_or_id, Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#::from_namerust-analyzer cargo sudo-rs 0.2.14 system/impl#[Group]from_name()., Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#::from_gidrust-analyzer cargo sudo-rs 0.2.14 system/impl#[Group]from_gid().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
131
132 match (&target_user_name_or_id, &target_group_name_or_id) {
133 // when -g is specified, but -u is not specified default -u to the current user
134 (Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(_)) => {
135 target_user = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(current_user.clonerust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[CurrentUser][Clone]clone().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
136 }
137 // when -u is specified but -g is not specified, default -g to the primary group of the specified user
138 (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(_), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) => {
139 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(user) = &target_user {
140 target_group = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(user.primary_grouprust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]primary_group().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().);
141 }
142 }
143 // when no -u or -g is specified, default to root:root
144 (Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) => {
145 target_user = Userrust-analyzer cargo sudo-rs 0.2.14 system/User#::from_namerust-analyzer cargo sudo-rs 0.2.14 system/impl#[User]from_name().(c"root")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
146 target_group = Grouprust-analyzer cargo sudo-rs 0.2.14 system/Group#::from_namerust-analyzer cargo sudo-rs 0.2.14 system/impl#[Group]from_name().(if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_os = "linux") {
147 c"root"
148 } else {
149 c"wheel"
150 })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
151 }
152 _ => {}
153 }
154
155 match (target_user, target_group) {
156 (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(user), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(group)) => {
157 // resolve success
158 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((user, group))
159 }
160 // group name or id not found
161 (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(_), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#::GroupNotFoundrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#GroupNotFound#(
162 target_group_name_or_id
163 .as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().()
164 .unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_default().()
165 .to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
166 )),
167 // user (and maybe group) name or id not found
168 _ => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#::UserNotFoundrust-analyzer cargo sudo-rs 0.2.14 common/error/Error#UserNotFound#(
169 target_user_name_or_id
170 .as_derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_deref().()
171 .unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_default().()
172 .to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
173 )),
174 }
175 }
176
177 fn resolve_from_name_or_idrust-analyzer cargo sudo-rs 0.2.14 common/resolve/resolve_from_name_or_id().<T, I, E>(
178 input: &Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<SudoStringrust-analyzer cargo sudo-rs 0.2.14 common/string/SudoString#>,
179 from_name: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(&CStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ffi/c_str/CStr#) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<T>, E>,
180 from_id: impl FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(I) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<T>, E>,
181 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<T>, E>
182 where
183 I: FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#,
184 {
185 match input.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().().and_thenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]and_then().(NameOrIdrust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#::parserust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[`NameOrId<'a, T>`]parse().) {
186 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(NameOrIdrust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#::Namerust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#Name#(name)) => from_name(name.as_cstrrust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString]as_cstr().()),
187 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(NameOrIdrust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#::Idrust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#Id#(id)) => from_id(id),
188 Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#),
189 }
190 }
191
192 /// Check whether a path points to a regular file and any executable flag is set
193 pub(craterust-analyzer cargo sudo-rs 0.2.14 crate/) fn is_valid_executablerust-analyzer cargo sudo-rs 0.2.14 common/resolve/is_valid_executable().(path: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#) -> bool {
194 if path.is_filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_file().() {
195 match fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::metadatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/metadata().(path) {
196 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(meta) => meta.moderust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/fs/impl#[`fs::Metadata`][MetadataExt]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().0o111 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().0,
197 _ => false,
198 }
199 } else {
200 false
201 }
202 }
203
204 /// Resolve a executable name based in the PATH environment variable
205 /// When resolving a path, this code checks whether the target file is
206 /// a regular file and has any executable bits set. It does not specifically
207 /// check for user, group, or others' executable bit.
208 pub(craterust-analyzer cargo sudo-rs 0.2.14 crate/) fn resolve_pathrust-analyzer cargo sudo-rs 0.2.14 common/resolve/resolve_path().(command: &Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, path: &str) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> {
209 // Depending on the security policy, the user's PATH environment variable may be modified,
210 // replaced, or passed unchanged to the program that sudo executes.
211
212 path.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().(':')
213 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(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().)
214 // ignore all relative paths ("", "." or "./")
215 .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|path| path.is_absoluterust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_absolute().())
216 // construct a possible executable absolute path candidate
217 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|path| path.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(command))
218 // check whether the candidate is a regular file and any executable flag is set
219 .findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#find().(|arg| is_valid_executablerust-analyzer cargo sudo-rs 0.2.14 common/resolve/is_valid_executable().(arg))
220 }
221
222 #[cfg(test)]
223 mod testsrust-analyzer cargo sudo-rs 0.2.14 common/resolve/tests/ {
224 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::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#;
225
226 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::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#;
227 use craterust-analyzer cargo sudo-rs 0.2.14 crate/::systemrust-analyzer cargo sudo-rs 0.2.14 system/::ROOT_GROUP_NAMErust-analyzer cargo sudo-rs 0.2.14 system/ROOT_GROUP_NAME.;
228
229 use superrust-analyzer cargo sudo-rs 0.2.14 common/resolve/::{NameOrIdrust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#, is_valid_executablerust-analyzer cargo sudo-rs 0.2.14 common/resolve/is_valid_executable()., resolve_pathrust-analyzer cargo sudo-rs 0.2.14 common/resolve/resolve_path()., resolve_target_user_and_grouprust-analyzer cargo sudo-rs 0.2.14 common/resolve/resolve_target_user_and_group().};
230
231 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
232 fn test_resolve_pathrust-analyzer cargo sudo-rs 0.2.14 common/resolve/tests/test_resolve_path().() {
233 // Assume any linux distro has utilities in this PATH
234 let path = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin";
235
236 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(is_valid_executablerust-analyzer cargo sudo-rs 0.2.14 common/resolve/is_valid_executable().(
237 &resolve_pathrust-analyzer cargo sudo-rs 0.2.14 common/resolve/resolve_path().(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().("yes"), path).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()
238 ));
239
240 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(is_valid_executablerust-analyzer cargo sudo-rs 0.2.14 common/resolve/is_valid_executable().(
241 &resolve_pathrust-analyzer cargo sudo-rs 0.2.14 common/resolve/resolve_path().(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().("whoami"), path).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()
242 ));
243
244 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(is_valid_executablerust-analyzer cargo sudo-rs 0.2.14 common/resolve/is_valid_executable().(
245 &resolve_pathrust-analyzer cargo sudo-rs 0.2.14 common/resolve/resolve_path().(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().("env"), path).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()
246 ));
247 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(resolve_pathrust-analyzer cargo sudo-rs 0.2.14 common/resolve/resolve_path().(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().("thisisnotonyourfs"), path), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
248 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(resolve_pathrust-analyzer cargo sudo-rs 0.2.14 common/resolve/resolve_path().(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().("thisisnotonyourfs"), "."), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
249 }
250
251 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
252 fn test_name_or_idrust-analyzer cargo sudo-rs 0.2.14 common/resolve/tests/test_name_or_id().() {
253 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(NameOrIdrust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#::<u32>::parserust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[`NameOrId<'a, T>`]parse().(&"".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
254 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
255 NameOrIdrust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#::<u32>::parserust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[`NameOrId<'a, T>`]parse().(&"mies".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()),
256 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(NameOrIdrust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#::Namerust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#Name#(&"mies".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()))
257 );
258 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
259 NameOrIdrust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#::<u32>::parserust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[`NameOrId<'a, T>`]parse().(&"1337".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()),
260 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(NameOrIdrust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#::Namerust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#Name#(&"1337".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()))
261 );
262 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
263 NameOrIdrust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#::<u32>::parserust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[`NameOrId<'a, T>`]parse().(&"#1337".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()),
264 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(NameOrIdrust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#::Idrust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#Id#(1337))
265 );
266 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(NameOrIdrust-analyzer cargo sudo-rs 0.2.14 common/resolve/NameOrId#::<u32>::parserust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[`NameOrId<'a, T>`]parse().(&"#-1".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#);
267 }
268
269 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
270 fn test_resolve_target_user_and_grouprust-analyzer cargo sudo-rs 0.2.14 common/resolve/tests/test_resolve_target_user_and_group().() {
271 let current_user = CurrentUserrust-analyzer cargo sudo-rs 0.2.14 common/resolve/CurrentUser#::resolverust-analyzer cargo sudo-rs 0.2.14 common/resolve/impl#[CurrentUser]resolve().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
272
273 // fallback to root
274 let (user, group) = resolve_target_user_and_grouprust-analyzer cargo sudo-rs 0.2.14 common/resolve/resolve_target_user_and_group().(&Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, &Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, &current_user).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
275 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(user.namerust-analyzer cargo sudo-rs 0.2.14 system/User#name., "root");
276 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(group.namerust-analyzer cargo sudo-rs 0.2.14 system/Group#name..unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), ROOT_GROUP_NAMErust-analyzer cargo sudo-rs 0.2.14 system/ROOT_GROUP_NAME.);
277
278 // unknown user
279 let result =
280 resolve_target_user_and_grouprust-analyzer cargo sudo-rs 0.2.14 common/resolve/resolve_target_user_and_group().(&Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("non_existing_ghost".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), &Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, &current_user);
281 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(result.is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
282
283 // unknown user
284 let result =
285 resolve_target_user_and_grouprust-analyzer cargo sudo-rs 0.2.14 common/resolve/resolve_target_user_and_group().(&Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, &Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#("non_existing_ghost".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), &current_user);
286 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(result.is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
287
288 // fallback to current user when different group specified
289 let (user, group) =
290 resolve_target_user_and_grouprust-analyzer cargo sudo-rs 0.2.14 common/resolve/resolve_target_user_and_group().(&Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, &Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ROOT_GROUP_NAMErust-analyzer cargo sudo-rs 0.2.14 system/ROOT_GROUP_NAME..intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()), &current_user)
291 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
292 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(user.namerust-analyzer cargo sudo-rs 0.2.14 system/User#name., current_user.namerust-analyzer cargo sudo-rs 0.2.14 system/User#name.);
293 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(group.namerust-analyzer cargo sudo-rs 0.2.14 system/Group#name..unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(), ROOT_GROUP_NAMErust-analyzer cargo sudo-rs 0.2.14 system/ROOT_GROUP_NAME.);
294
295 // fallback to current users group when no group specified
296 let (user, group) =
297 resolve_target_user_and_grouprust-analyzer cargo sudo-rs 0.2.14 common/resolve/resolve_target_user_and_group().(&Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(current_user.namerust-analyzer cargo sudo-rs 0.2.14 system/User#name..clonerust-analyzer cargo sudo-rs 0.2.14 common/string/impl#[SudoString][Clone]clone().()), &Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, &current_user)
298 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
299 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(user.namerust-analyzer cargo sudo-rs 0.2.14 system/User#name., current_user.namerust-analyzer cargo sudo-rs 0.2.14 system/User#name.);
300 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(group.gidrust-analyzer cargo sudo-rs 0.2.14 system/Group#gid., current_user.gidrust-analyzer cargo sudo-rs 0.2.14 system/User#gid.);
301 }
302 }
303
304 /// Resolve symlinks in all the directories leading up to a file, but
305 /// not the file itself; this allows sudo to specify a precise policy with
306 /// tools like busybox or pgrep (which is a symlink to pgrep on systems)
307 /// This function will check for existence.
308 pub fn canonicalizerust-analyzer cargo sudo-rs 0.2.14 common/resolve/canonicalize().<P: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>>(path: P) -> 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#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> {
309 let reconstructed_path = canonicalize_newfilerust-analyzer cargo sudo-rs 0.2.14 common/resolve/canonicalize_newfile().(path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
310
311 // access the object to generate the regular error if it does not exist
312 let _ = fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::metadatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/metadata().(&reconstructed_path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
313
314 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(reconstructed_path)
315 }
316
317 /// Resolve symlinks in all the directories leading up to a file, but
318 /// not the file itself; this allows us to keep symlinks as is, and will
319 /// also work on non-existing files.
320 pub fn canonicalize_newfilerust-analyzer cargo sudo-rs 0.2.14 common/resolve/canonicalize_newfile().<P: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>>(path: P) -> 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#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> {
321 let path = path.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().();
322 let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(parent) = path.parentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]parent().() else {
323 // path is "/" or a prefix
324 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(path.to_path_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_path_buf().());
325 };
326
327 let canon_path = fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::canonicalizerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/canonicalize().(parent)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
328
329 let reconstructed_path = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(file_name) = path.file_namerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]file_name().() {
330 canon_path.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(file_name)
331 } else {
332 canon_path
333 };
334
335 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(reconstructed_path)
336 }
337
338 #[cfg(test)]
339 mod testrust-analyzer cargo sudo-rs 0.2.14 common/resolve/test/ {
340 use superrust-analyzer cargo sudo-rs 0.2.14 common/resolve/::canonicalizerust-analyzer cargo sudo-rs 0.2.14 common/resolve/canonicalize().;
341 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::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#;
342
343 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
344 fn canonicalizationrust-analyzer cargo sudo-rs 0.2.14 common/resolve/test/canonicalization().() {
345 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(canonicalizerust-analyzer cargo sudo-rs 0.2.14 common/resolve/canonicalize().("/").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 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().("/"));
346 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(canonicalizerust-analyzer cargo sudo-rs 0.2.14 common/resolve/canonicalize().("").is_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]is_err().());
347 if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(any(target_os = "linux")) {
348 // this test REQUIRES /usr/bin/unxz to be a symlink for /usr/bin/xz or /usr/bin/busybox
349 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(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().("/bin").is_symlinkrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_symlink().());
350 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(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().("/usr/bin/unxz").is_symlinkrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_symlink().());
351 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
352 canonicalizerust-analyzer cargo sudo-rs 0.2.14 common/resolve/canonicalize().("/usr/bin/unxz").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
353 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().("/usr/bin/unxz")
354 );
355 // this assumes /bin is a symlink on /usr/bin, like it is on modern Debian/Ubuntu
356 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
357 canonicalizerust-analyzer cargo sudo-rs 0.2.14 common/resolve/canonicalize().("/bin/unxz").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
358 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().("/usr/bin/unxz")
359 );
360 } else if cfgrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/cfg!!(target_os = "freebsd") {
361 // this test REQUIRES /usr/bin/pkill to be a symlink
362 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(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().("/usr/bin/pkill").is_symlinkrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_symlink().());
363 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
364 canonicalizerust-analyzer cargo sudo-rs 0.2.14 common/resolve/canonicalize().("/usr/bin/pkill").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(),
365 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().("/usr/bin/pkill")
366 );
367 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(canonicalizerust-analyzer cargo sudo-rs 0.2.14 common/resolve/canonicalize().("/bin/pkill").unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 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().("/bin/pkill"));
368 } else {
369 panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!(
370 "canonicalization test not yet adapted for {}",
371 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::constsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/consts/::OSrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/consts/OS.
372 );
373 }
374 }
375 }