@@ -5,7 +5,7 @@ use crate::exec::{RunOptions, Umask};
5
5
#[ cfg_attr( not( feature = "sudoedit" ) , allow( unused_imports) ) ]
6
6
use crate :: sudo:: { SudoEditOptions , SudoListOptions , SudoRunOptions , SudoValidateOptions } ;
7
7
use crate :: sudoers:: Sudoers ;
8
- use crate :: system:: { Group , Hostname , Process , User } ;
8
+ use crate :: system:: { audit :: sudo_call , Group , Hostname , Process , User } ;
9
9
10
10
use super :: {
11
11
command:: CommandAndArguments ,
@@ -81,7 +81,9 @@ impl Context {
81
81
system_path. as_ref ( )
82
82
} ;
83
83
84
- CommandAndArguments :: build_from_args ( shell, sudo_options. positional_args , path)
84
+ sudo_call ( & target_user, & target_group, || {
85
+ CommandAndArguments :: build_from_args ( shell, sudo_options. positional_args , path)
86
+ } ) ?
85
87
} ;
86
88
87
89
Ok ( Context {
@@ -115,18 +117,20 @@ impl Context {
115
117
resolve_target_user_and_group ( & sudo_options. user , & sudo_options. group , & current_user) ?;
116
118
117
119
// resolve file arguments; if something can't be resolved, don't add it to the "edit" list
118
- let resolved_args = sudo_options. positional_args . iter ( ) . map ( |arg| {
119
- let path = Path :: new ( arg) ;
120
- let absolute_path;
121
- crate :: common:: resolve:: canonicalize_newfile ( if path. is_absolute ( ) {
122
- path
123
- } else {
124
- absolute_path = Path :: new ( "." ) . join ( path) ;
125
- & absolute_path
120
+ let resolved_args = sudo_call ( & target_user, & target_group, || {
121
+ sudo_options. positional_args . iter ( ) . map ( |arg| {
122
+ let path = Path :: new ( arg) ;
123
+ let absolute_path;
124
+ crate :: common:: resolve:: canonicalize_newfile ( if path. is_absolute ( ) {
125
+ path
126
+ } else {
127
+ absolute_path = Path :: new ( "." ) . join ( path) ;
128
+ & absolute_path
129
+ } )
130
+ . map_err ( |_| arg)
131
+ . and_then ( |path| path. into_os_string ( ) . into_string ( ) . map_err ( |_| arg) )
126
132
} )
127
- . map_err ( |_| arg)
128
- . and_then ( |path| path. into_os_string ( ) . into_string ( ) . map_err ( |_| arg) )
129
- } ) ;
133
+ } ) ?;
130
134
131
135
let files_to_edit = resolved_args
132
136
. clone ( )
@@ -223,7 +227,9 @@ impl Context {
223
227
system_path. as_ref ( )
224
228
} ;
225
229
226
- CommandAndArguments :: build_from_args ( None , sudo_options. positional_args , path)
230
+ sudo_call ( & target_user, & target_group, || {
231
+ CommandAndArguments :: build_from_args ( None , sudo_options. positional_args , path)
232
+ } ) ?
227
233
} ;
228
234
229
235
Ok ( Context {
@@ -270,21 +276,21 @@ impl Context {
270
276
271
277
#[ cfg( test) ]
272
278
mod tests {
273
- use crate :: {
274
- sudo:: SudoAction ,
275
- system:: { interface:: UserId , Hostname } ,
276
- } ;
279
+ use crate :: { common:: resolve:: CurrentUser , sudo:: SudoAction , system:: Hostname } ;
277
280
278
281
use super :: Context ;
279
282
280
283
#[ test]
281
284
fn test_build_run_context ( ) {
282
- let options = SudoAction :: try_parse_from ( [ "sudo" , "echo" , "hello" ] )
285
+ let mut options = SudoAction :: try_parse_from ( [ "sudo" , "echo" , "hello" ] )
283
286
. unwrap ( )
284
287
. try_into_run ( )
285
288
. ok ( )
286
289
. unwrap ( ) ;
287
290
291
+ let current_user = CurrentUser :: resolve ( ) . unwrap ( ) ;
292
+ options. user = Some ( current_user. name . clone ( ) ) ;
293
+
288
294
let context = Context :: from_run_opts ( options, & mut Default :: default ( ) ) . unwrap ( ) ;
289
295
290
296
if cfg ! ( target_os = "linux" ) {
@@ -295,6 +301,6 @@ mod tests {
295
301
}
296
302
assert_eq ! ( context. command. arguments, [ "hello" ] ) ;
297
303
assert_eq ! ( context. hostname, Hostname :: resolve( ) ) ;
298
- assert_eq ! ( context. target_user. uid, UserId :: ROOT ) ;
304
+ assert_eq ! ( context. target_user. uid, current_user . uid ) ;
299
305
}
300
306
}
0 commit comments