@@ -301,6 +301,9 @@ pub enum ServEvent<'g, 'a> {
301301 ///
302302 /// TODO details
303303 SessionPty ( ServPtyRequest < ' g , ' a > ) ,
304+ /// Server has received one environment variable.
305+ /// Note: input strings are not sanitised.
306+ SessionEnv ( ServEnvironmentRequest < ' g , ' a > ) ,
304307
305308 /// The SSH session is no longer running
306309 #[ allow( unused) ]
@@ -326,6 +329,7 @@ impl Debug for ServEvent<'_, '_> {
326329 Self :: SessionExec ( _) => "SessionExec" ,
327330 Self :: SessionSubsystem ( _) => "SessionSubsystem" ,
328331 Self :: SessionPty ( _) => "SessionPty" ,
332+ Self :: SessionEnv ( _) => "Environment" ,
329333 Self :: Defunct => "Defunct" ,
330334 Self :: PollAgain => "PollAgain" ,
331335 } ;
@@ -745,7 +749,7 @@ impl<'g, 'a> ServExecRequest<'g, 'a> {
745749 self . raw_command ( ) ?. as_str ( )
746750 }
747751
748- pub fn raw_command ( & self ) -> Result < TextString < ' _ > > {
752+ fn raw_command ( & self ) -> Result < TextString < ' _ > > {
749753 self . runner . fetch_servcommand ( )
750754 }
751755
@@ -847,6 +851,69 @@ impl Drop for ServPtyRequest<'_, '_> {
847851 }
848852}
849853
854+ /// An environment variable request
855+ ///
856+ pub struct ServEnvironmentRequest < ' g , ' a > {
857+ runner : & ' g mut Runner < ' a , Server > ,
858+ num : ChanNum ,
859+ done : bool ,
860+ }
861+
862+ impl < ' g , ' a > ServEnvironmentRequest < ' g , ' a > {
863+ fn new ( runner : & ' g mut Runner < ' a , Server > , num : ChanNum ) -> Self {
864+ Self { runner, num, done : false }
865+ }
866+
867+ /// Indicate that the request succeeded.
868+ ///
869+ /// Note that if the peer didn't request a reply, this call
870+ /// will not do anything.
871+ pub fn succeed ( mut self ) -> Result < ( ) > {
872+ self . done = true ;
873+ self . runner . resume_chanreq ( true )
874+ }
875+
876+ /// Indicate that the request failed.
877+ ///
878+ /// Note that if the peer didn't request a reply, this call
879+ /// will not do anything.
880+ /// Does not need to be called explicitly, also occurs on drop without `accept()`
881+ pub fn fail ( mut self ) -> Result < ( ) > {
882+ self . done = true ;
883+ self . runner . resume_chanreq ( false )
884+ }
885+
886+ /// Return the associated channel number.
887+ ///
888+ /// This will correspond to a `ChanHandle::num()`
889+ /// from a previous [`ServOpenSession`] event.
890+ pub fn channel ( & self ) -> ChanNum {
891+ self . num
892+ }
893+
894+ /// Retrieve the name of the environment variable (from NAME=VALUE pair).
895+ pub fn name ( & self ) -> Result < & str > {
896+ self . raw_name ( ) ?. as_str ( )
897+ }
898+
899+ /// Retrieve the raw name of the environment variable.
900+ fn raw_name ( & self ) -> Result < TextString < ' _ > > {
901+ self . runner . fetch_env_name ( )
902+ }
903+
904+ /// Retrieve the value of the environment variable (from NAME=VALUE pair).
905+ pub fn value ( & self ) -> Result < & str > {
906+ self . raw_value ( ) ?. as_str ( )
907+ }
908+
909+ /// Retrieve the raw value of the environment variable.
910+ fn raw_value ( & self ) -> Result < TextString < ' _ > > {
911+ self . runner . fetch_env_value ( )
912+ }
913+
914+ // TODO: does the app care about wantreply?
915+ }
916+
850917// Only small values should be stored inline.
851918// Larger state is retrieved from the current packet via Runner::fetch_*()
852919#[ derive( Debug , Clone ) ]
@@ -872,6 +939,9 @@ pub(crate) enum ServEventId {
872939 SessionPty {
873940 num : ChanNum ,
874941 } ,
942+ Environment {
943+ num : ChanNum ,
944+ } ,
875945 #[ allow( unused) ]
876946 Defunct ,
877947 // TODO:
@@ -925,6 +995,10 @@ impl ServEventId {
925995 debug_assert ! ( matches!( p, Some ( Packet :: ChannelRequest ( _) ) ) ) ;
926996 Ok ( ServEvent :: SessionPty ( ServPtyRequest :: new ( runner, num) ) )
927997 }
998+ Self :: Environment { num } => {
999+ debug_assert ! ( matches!( p, Some ( Packet :: ChannelRequest ( _) ) ) ) ;
1000+ Ok ( ServEvent :: SessionEnv ( ServEnvironmentRequest :: new ( runner, num) ) )
1001+ }
9281002 Self :: Defunct => Ok ( ServEvent :: Defunct ) ,
9291003 }
9301004 }
@@ -942,6 +1016,7 @@ impl ServEventId {
9421016 | Self :: SessionShell { .. }
9431017 | Self :: SessionExec { .. }
9441018 | Self :: SessionSubsystem { .. }
1019+ | Self :: Environment { .. }
9451020 | Self :: SessionPty { .. } => true ,
9461021 }
9471022 }
0 commit comments