@@ -11,9 +11,7 @@ use crate::log::{auth_info, auth_warn};
1111use crate :: pam:: PamContext ;
1212use crate :: sudo:: env:: environment;
1313use crate :: sudo:: pam:: { attempt_authenticate, init_pam, pre_exec, InitPamArgs } ;
14- use crate :: sudoers:: {
15- AuthenticatingUser , Authentication , Authorization , DirChange , Judgement , Restrictions , Sudoers ,
16- } ;
14+ use crate :: sudoers:: { AuthenticatingUser , Authentication , Authorization , Judgement , Sudoers } ;
1715use crate :: system:: term:: current_tty_name;
1816use crate :: system:: timestamp:: { RecordScope , SessionRecordFile , TouchResult } ;
1917use crate :: system:: { escape_os_str_lossy, Process } ;
@@ -61,15 +59,14 @@ pub fn run(mut cmd_opts: SudoRunOptions) -> Result<(), Error> {
6159
6260 let user_requested_env_vars = std:: mem:: take ( & mut cmd_opts. env_var_list ) ;
6361
64- let mut context = Context :: from_run_opts ( cmd_opts, & mut policy) ?;
62+ let context = Context :: from_run_opts ( cmd_opts, & mut policy) ?;
6563
6664 let policy = judge ( policy, & context) ?;
6765
6866 let Authorization :: Allowed ( auth, controls) = policy. authorization ( ) else {
6967 return Err ( Error :: Authorization ( context. current_user . name . to_string ( ) ) ) ;
7068 } ;
7169
72- apply_policy_to_context ( & mut context, & controls) ?;
7370 let mut pam_context = auth_and_update_record_file ( & context, auth) ?;
7471
7572 // build environment
@@ -96,25 +93,19 @@ pub fn run(mut cmd_opts: SudoRunOptions) -> Result<(), Error> {
9693
9794 // prepare switch of apparmor profile
9895 #[ cfg( feature = "apparmor" ) ]
99- if let Some ( profile) = controls. apparmor_profile {
100- crate :: apparmor:: set_profile_for_next_exec ( & profile)
101- . map_err ( |err| Error :: AppArmor ( profile, err) ) ?;
96+ if let Some ( profile) = & controls. apparmor_profile {
97+ crate :: apparmor:: set_profile_for_next_exec ( profile)
98+ . map_err ( |err| Error :: AppArmor ( profile. clone ( ) , err) ) ?;
10299 }
103100
101+ let options = context. try_as_run_options ( & controls) ?;
102+
103+ // Log after try_as_run_options to avoid logging if the command is not resolved
104+ log_command_execution ( & context) ;
105+
104106 // run command and return corresponding exit code
105- let command_exit_reason = if context. command . resolved {
106- log_command_execution ( & context) ;
107-
108- crate :: exec:: run_command (
109- context
110- . try_as_run_options ( )
111- . map_err ( |io_error| Error :: Io ( Some ( context. command . command . clone ( ) ) , io_error) ) ?,
112- target_env,
113- )
114- . map_err ( |io_error| Error :: Io ( Some ( context. command . command ) , io_error) )
115- } else {
116- Err ( Error :: CommandNotFound ( context. command . command ) )
117- } ;
107+ let command_exit_reason = crate :: exec:: run_command ( options, target_env)
108+ . map_err ( |io_error| Error :: Io ( Some ( context. command . command ) , io_error) ) ;
118109
119110 pam_context. close_session ( ) ;
120111
@@ -154,6 +145,7 @@ fn auth_and_update_record_file(
154145 password_timeout,
155146 ref credential,
156147 pwfeedback,
148+ noninteractive_auth,
157149 } : Authentication ,
158150) -> Result < PamContext , Error > {
159151 let auth_user = match credential {
@@ -190,7 +182,7 @@ fn auth_and_update_record_file(
190182 hostname : & context. hostname ,
191183 } ) ?;
192184 if auth_status. must_authenticate {
193- if context. non_interactive && !context . noninteractive_auth {
185+ if context. non_interactive && !noninteractive_auth {
194186 return Err ( Error :: InteractionRequired ) ;
195187 }
196188
@@ -213,40 +205,6 @@ fn auth_and_update_record_file(
213205 Ok ( pam_context)
214206}
215207
216- fn apply_policy_to_context (
217- context : & mut Context ,
218- controls : & Restrictions ,
219- ) -> Result < ( ) , crate :: common:: Error > {
220- // see if the chdir flag is permitted
221- match controls. chdir {
222- DirChange :: Any => { }
223- DirChange :: Strict ( optdir) => {
224- if let Some ( chdir) = & context. chdir {
225- return Err ( Error :: ChDirNotAllowed {
226- chdir : chdir. clone ( ) ,
227- command : context. command . command . clone ( ) ,
228- } ) ;
229- } else {
230- context. chdir = optdir. cloned ( ) ;
231- }
232- }
233- }
234-
235- // expand tildes in the path with the users home directory
236- if let Some ( dir) = context. chdir . take ( ) {
237- context. chdir = Some ( dir. expand_tilde_in_path ( & context. target_user . name ) ?)
238- }
239-
240- // in case the user could set these from the commandline, something more fancy
241- // could be needed, but here we copy these -- perhaps we should split up the Context type
242- context. use_pty = controls. use_pty ;
243- context. noexec = controls. noexec ;
244- context. umask = controls. umask ;
245- context. noninteractive_auth = controls. noninteractive_auth ;
246-
247- Ok ( ( ) )
248- }
249-
250208/// This should determine what the authentication status for the given record
251209/// match limit and origin/target user from the context is.
252210fn determine_auth_status (
0 commit comments