@@ -466,6 +466,118 @@ fn find_priority_threshold(rules: &[Rule]) -> u64 {
466466}
467467
468468impl Rule {
469+ /// Returns the "interface" value used in the final policy, replacing wildcards with empty
470+ /// strings
471+ /// See: https://github.com/bus1/dbus-broker/blob/e3324b3736fd40d95e7943fca6e485013d15d643/src/launch/policy.c#L97
472+ pub ( crate ) fn interface ( & self ) -> Result < String > {
473+ match self {
474+ Self :: Send { send_interface, .. } => {
475+ if send_interface == "*" {
476+ Ok ( "" . to_string ( ) )
477+ } else {
478+ Ok ( send_interface. to_owned ( ) )
479+ }
480+ }
481+ Self :: Receive {
482+ receive_interface, ..
483+ } => {
484+ if receive_interface == "*" {
485+ Ok ( "" . to_string ( ) )
486+ } else {
487+ Ok ( receive_interface. to_owned ( ) )
488+ }
489+ }
490+ _ => error:: InvalidPropertyForRuleSnafu {
491+ property : "interface" . to_string ( ) ,
492+ rule_type : format ! ( "{self:?}" ) ,
493+ }
494+ . fail ( ) ,
495+ }
496+ }
497+
498+ /// Returns the "name" value used in the final policy, replacing wildcards with empty
499+ /// strings
500+ /// See: https://github.com/bus1/dbus-broker/blob/e3324b3736fd40d95e7943fca6e485013d15d643/src/launch/policy.c#L97
501+ pub ( crate ) fn name ( & self ) -> Result < String > {
502+ match self {
503+ Self :: Send {
504+ send_destination, ..
505+ } => {
506+ if send_destination == "*" {
507+ Ok ( "" . to_string ( ) )
508+ } else {
509+ Ok ( send_destination. to_owned ( ) )
510+ }
511+ }
512+ Self :: Receive { receive_sender, .. } => {
513+ if receive_sender == "*" {
514+ Ok ( "" . to_string ( ) )
515+ } else {
516+ Ok ( receive_sender. to_owned ( ) )
517+ }
518+ }
519+ _ => error:: InvalidPropertyForRuleSnafu {
520+ property : "name" . to_string ( ) ,
521+ rule_type : format ! ( "{self:?}" ) ,
522+ }
523+ . fail ( ) ,
524+ }
525+ }
526+
527+ /// Returns the "member" value used in the final policy, replacing wildcards with empty
528+ /// strings
529+ /// See: https://github.com/bus1/dbus-broker/blob/e3324b3736fd40d95e7943fca6e485013d15d643/src/launch/policy.c#L97
530+ pub ( crate ) fn member ( & self ) -> Result < String > {
531+ match self {
532+ Self :: Send { send_member, .. } => {
533+ if send_member == "*" {
534+ Ok ( "" . to_string ( ) )
535+ } else {
536+ Ok ( send_member. to_owned ( ) )
537+ }
538+ }
539+ Self :: Receive { receive_member, .. } => {
540+ if receive_member == "*" {
541+ Ok ( "" . to_string ( ) )
542+ } else {
543+ Ok ( receive_member. to_owned ( ) )
544+ }
545+ }
546+ _ => error:: InvalidPropertyForRuleSnafu {
547+ property : "member" . to_string ( ) ,
548+ rule_type : format ! ( "{self:?}" ) ,
549+ }
550+ . fail ( ) ,
551+ }
552+ }
553+
554+ /// Returns the "path" value used in the final policy, replacing wildcards with empty
555+ /// strings
556+ /// See: https://github.com/bus1/dbus-broker/blob/e3324b3736fd40d95e7943fca6e485013d15d643/src/launch/policy.c#L97
557+ pub ( crate ) fn path ( & self ) -> Result < String > {
558+ match self {
559+ Self :: Send { send_path, .. } => {
560+ if send_path == "*" {
561+ Ok ( "" . to_string ( ) )
562+ } else {
563+ Ok ( send_path. to_owned ( ) )
564+ }
565+ }
566+ Self :: Receive { receive_path, .. } => {
567+ if receive_path == "*" {
568+ Ok ( "" . to_string ( ) )
569+ } else {
570+ Ok ( receive_path. to_owned ( ) )
571+ }
572+ }
573+ _ => error:: InvalidPropertyForRuleSnafu {
574+ property : "path" . to_string ( ) ,
575+ rule_type : format ! ( "{self:?}" ) ,
576+ }
577+ . fail ( ) ,
578+ }
579+ }
580+
469581 /// Sets the priority of the rule
470582 fn set_priority ( & mut self , new_priority : u64 ) {
471583 match self {
@@ -551,6 +663,7 @@ impl UsernameResolver for PasswdUsernameResolver {
551663#[ cfg( test) ]
552664mod tests {
553665 use super :: * ;
666+ use test_case:: test_case;
554667
555668 const ALICE_USER : u32 = 1 ;
556669 const BOB_USER : u32 = 2 ;
@@ -920,4 +1033,58 @@ mod tests {
9201033 clean_connect_rules ( 4 , & mut rules) ;
9211034 assert ! ( rules. is_empty( ) ) ;
9221035 }
1036+
1037+ #[ test_case( Rule :: Send {
1038+ allow: false ,
1039+ priority: u64 :: default ( ) ,
1040+ send_broadcast: u32 :: default ( ) ,
1041+ send_destination: "*" . to_string( ) ,
1042+ send_interface: "*" . to_string( ) ,
1043+ send_member: "*" . to_string( ) ,
1044+ send_path: "*" . to_string( ) ,
1045+ send_type: MessageType :: default ( ) ,
1046+ } ; "witt a send rule wildcards are replaced with empty strings" ) ]
1047+ #[ test_case( Rule :: Receive {
1048+ allow: false ,
1049+ priority: u64 :: default ( ) ,
1050+ receive_broadcast: u32 :: default ( ) ,
1051+ receive_interface: "*" . to_string( ) ,
1052+ receive_member: "*" . to_string( ) ,
1053+ receive_path: "*" . to_string( ) ,
1054+ receive_sender: "*" . to_string( ) ,
1055+ receive_type: MessageType :: default ( ) ,
1056+ } ; "with a receive rule wildcards are replaced with empty strings" ) ]
1057+ fn rules_with_wildcards ( rule : Rule ) {
1058+ assert_eq ! ( rule. name( ) . unwrap( ) , "" ) ;
1059+ assert_eq ! ( rule. interface( ) . unwrap( ) , "" ) ;
1060+ assert_eq ! ( rule. member( ) . unwrap( ) , "" ) ;
1061+ assert_eq ! ( rule. path( ) . unwrap( ) , "" ) ;
1062+ }
1063+
1064+ #[ test_case( Rule :: Send {
1065+ allow: false ,
1066+ priority: u64 :: default ( ) ,
1067+ send_broadcast: u32 :: default ( ) ,
1068+ send_destination: "name" . to_string( ) ,
1069+ send_interface: "interface" . to_string( ) ,
1070+ send_member: "member" . to_string( ) ,
1071+ send_path: "path" . to_string( ) ,
1072+ send_type: MessageType :: default ( ) ,
1073+ } ; "with a send rule the original value is returned" ) ]
1074+ #[ test_case( Rule :: Receive {
1075+ allow: false ,
1076+ priority: u64 :: default ( ) ,
1077+ receive_broadcast: u32 :: default ( ) ,
1078+ receive_interface: "interface" . to_string( ) ,
1079+ receive_member: "member" . to_string( ) ,
1080+ receive_path: "path" . to_string( ) ,
1081+ receive_sender: "name" . to_string( ) ,
1082+ receive_type: MessageType :: default ( ) ,
1083+ } ; "with a receive rule the original value is returned" ) ]
1084+ fn rules_without_wildcards ( rule : Rule ) {
1085+ assert_eq ! ( rule. name( ) . unwrap( ) , "name" ) ;
1086+ assert_eq ! ( rule. interface( ) . unwrap( ) , "interface" ) ;
1087+ assert_eq ! ( rule. member( ) . unwrap( ) , "member" ) ;
1088+ assert_eq ! ( rule. path( ) . unwrap( ) , "path" ) ;
1089+ }
9231090}
0 commit comments