@@ -62,9 +62,9 @@ macro_rules! cstr2cow {
6262}
6363
6464fn get_context_help_text ( ) -> String {
65- #[ cfg( not( feature = "selinux" ) ) ]
65+ #[ cfg( not( any ( feature = "selinux" , feature = "smack" ) ) ) ]
6666 return translate ! ( "id-context-help-disabled" ) ;
67- #[ cfg( feature = "selinux" ) ]
67+ #[ cfg( any ( feature = "selinux" , feature = "smack" ) ) ]
6868 return translate ! ( "id-context-help-enabled" ) ;
6969}
7070
@@ -98,7 +98,10 @@ struct State {
9898 rflag : bool , // --real
9999 zflag : bool , // --zero
100100 cflag : bool , // --context
101+ #[ cfg( feature = "selinux" ) ]
101102 selinux_supported : bool ,
103+ #[ cfg( feature = "smack" ) ]
104+ smack_supported : bool ,
102105 ids : Option < Ids > ,
103106 // The behavior for calling GNU's `id` and calling GNU's `id $USER` is similar but different.
104107 // * The SELinux context is only displayed without a specified user.
@@ -136,16 +139,10 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
136139 zflag : matches. get_flag ( options:: OPT_ZERO ) ,
137140 cflag : matches. get_flag ( options:: OPT_CONTEXT ) ,
138141
139- selinux_supported : {
140- #[ cfg( feature = "selinux" ) ]
141- {
142- uucore:: selinux:: is_selinux_enabled ( )
143- }
144- #[ cfg( not( feature = "selinux" ) ) ]
145- {
146- false
147- }
148- } ,
142+ #[ cfg( feature = "selinux" ) ]
143+ selinux_supported : uucore:: selinux:: is_selinux_enabled ( ) ,
144+ #[ cfg( feature = "smack" ) ]
145+ smack_supported : uucore:: smack:: is_smack_enabled ( ) ,
149146 user_specified : !users. is_empty ( ) ,
150147 ids : None ,
151148 } ;
@@ -179,26 +176,43 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
179176 let line_ending = LineEnding :: from_zero_flag ( state. zflag ) ;
180177
181178 if state. cflag {
182- return if state . selinux_supported {
183- // print SElinux context and exit
184- # [ cfg ( all ( any ( target_os = "linux" , target_os = "android" ) , feature = "selinux" ) ) ]
179+ // SELinux context
180+ # [ cfg ( feature = "selinux" ) ]
181+ if state . selinux_supported {
185182 if let Ok ( context) = selinux:: SecurityContext :: current ( false ) {
186183 let bytes = context. as_bytes ( ) ;
187184 print ! ( "{}{line_ending}" , String :: from_utf8_lossy( bytes) ) ;
185+ return Ok ( ( ) ) ;
188186 } else {
189- // print error because `cflag` was explicitly requested
190187 return Err ( USimpleError :: new (
191188 1 ,
192189 translate ! ( "id-error-cannot-get-context" ) ,
193190 ) ) ;
194191 }
195- Ok ( ( ) )
196- } else {
197- Err ( USimpleError :: new (
198- 1 ,
199- translate ! ( "id-error-context-selinux-only" ) ,
200- ) )
201- } ;
192+ }
193+
194+ // SMACK label
195+ #[ cfg( feature = "smack" ) ]
196+ if state. smack_supported {
197+ match uucore:: smack:: get_smack_label_for_self ( ) {
198+ Ok ( label) => {
199+ print ! ( "{label}{line_ending}" ) ;
200+ return Ok ( ( ) ) ;
201+ }
202+ Err ( _) => {
203+ return Err ( USimpleError :: new (
204+ 1 ,
205+ translate ! ( "id-error-cannot-get-context" ) ,
206+ ) ) ;
207+ }
208+ }
209+ }
210+
211+ // Neither SELinux nor SMACK supported
212+ return Err ( USimpleError :: new (
213+ 1 ,
214+ translate ! ( "id-error-context-selinux-only" ) ,
215+ ) ) ;
202216 }
203217
204218 for i in 0 ..=users. len ( ) {
@@ -666,7 +680,7 @@ fn id_print(state: &State, groups: &[u32]) {
666680 . join( "," )
667681 ) ;
668682
669- #[ cfg( all ( any ( target_os = "linux" , target_os = "android" ) , feature = "selinux" ) ) ]
683+ #[ cfg( feature = "selinux" ) ]
670684 if state. selinux_supported
671685 && !state. user_specified
672686 && std:: env:: var_os ( "POSIXLY_CORRECT" ) . is_none ( )
@@ -677,6 +691,17 @@ fn id_print(state: &State, groups: &[u32]) {
677691 print ! ( " context={}" , String :: from_utf8_lossy( bytes) ) ;
678692 }
679693 }
694+
695+ #[ cfg( feature = "smack" ) ]
696+ if state. smack_supported
697+ && !state. user_specified
698+ && std:: env:: var_os ( "POSIXLY_CORRECT" ) . is_none ( )
699+ {
700+ // print SMACK label (does not depend on "-Z")
701+ if let Ok ( label) = uucore:: smack:: get_smack_label_for_self ( ) {
702+ print ! ( " context={label}" ) ;
703+ }
704+ }
680705}
681706
682707#[ cfg( not( any( target_os = "linux" , target_os = "android" , target_os = "openbsd" ) ) ) ]
0 commit comments