diff mbox series

[cifs-utils,RFC,10/12] upcall-helper: add string comparison

Message ID 20250510161609.2615639-11-sorenson@redhat.com
State New
Headers show
Series cifs.upcall helper script enabling complex key description matching | expand

Commit Message

Frank Sorenson May 10, 2025, 4:16 p.m. UTC
Use string comparison (as glob or regex) for 'host', 'user',
'sec', and 'upcall_target' matching.

Signed-off-by: Frank Sorenson <sorenson@redhat.com>
---
 contrib/upcall-helper/cifs-upcall-helper | 29 +++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/contrib/upcall-helper/cifs-upcall-helper b/contrib/upcall-helper/cifs-upcall-helper
index fdd0c9c..91243bd 100755
--- a/contrib/upcall-helper/cifs-upcall-helper
+++ b/contrib/upcall-helper/cifs-upcall-helper
@@ -49,6 +49,8 @@  my @descriptionv2_keys = ('keyuid', 'keygid', 'perms', 'host', 'ipv', 'ip', 'sec
 my $conf_split_re = qr/^([^\s]+)\s+(.+)/; # splitting each line of the conf file
 my $split_char = '[,;]'; # separator for match and options fields
 
+my $string_comparison_re = qr/^(host|user|sec|upcall_target)(=|==|!=|~|!~)(.+)/;
+
 sub log_msg {
 	my $msg_level = shift;
 
@@ -180,10 +182,35 @@  sub set_upcall_opts {
 	}
 }
 
+# matching logic
+sub check_string_match {
+	my $key_field_val = shift;
+	my $comparator = shift;
+	my $match_str = shift;
+
+	if ($comparator eq '=' or $comparator eq '==' or $comparator eq '!=') { # glob
+		$match_str =~ s/\./\\./g; # replace . with \.
+		$match_str =~ s/\*/\.\*/g; # replace * with .*
+	}
+
+	my $result = 1 if ($key_field_val =~ $match_str);
+	$result = $result ^ 1 if (substr($comparator, 0, 1) eq '!');
+
+	return $result;
+}
 sub match_criterion {
 	my $criterion = shift;
 
-	return 0;
+	my ($field, $comparator, $match_pattern);
+
+	return 1 if ($criterion eq '*');  # '*' is always true
+	if (($field, $comparator, $match_pattern) = $criterion =~ $string_comparison_re) {
+		return 0 if (! check_string_match($key_vars{$field}, $comparator, $match_pattern));
+	} else {
+		log_msg 0, "unrecognized match string: $criterion";
+		return 0;
+	}
+	return 1;
 }
 sub match_criteria {
 	my $criteria_str = shift;