diff mbox series

[cifs-utils,RFC,05/12] upcall-helper: open and read each line of the helper's config file

Message ID 20250510161609.2615639-6-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
Open the /etc/cifs-upcall-helper.conf file, read and split each
line, and attempt to match each criterion on the line.

Matching logic not in place yet, so no line matches.

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

Patch

diff --git a/contrib/upcall-helper/cifs-upcall-helper b/contrib/upcall-helper/cifs-upcall-helper
index ee62aff..d1d7436 100755
--- a/contrib/upcall-helper/cifs-upcall-helper
+++ b/contrib/upcall-helper/cifs-upcall-helper
@@ -33,6 +33,7 @@  my $log_level = 0;
 # 1 - relevant messages (LOG_INFO)
 # 2 - verbose debugging (LOG_INFO)
 
+my $helper_conf = '/etc/cifs-upcall-helper.conf';
 my $keyid;
 my %key_vars;
 
@@ -44,6 +45,9 @@  my %key_vars;
 my $key_descriptionv2_re = qr/^cifs.spnego;([0-9]+);([0-9]+);([0-9a-f]+);ver=0x2;host=([^;]+);(ip4|ip6)=([^;]+);sec=(krb5|mskrb5|iakerb);uid=(0x[0-9a-f]+);creduid=(0x[0-9a-f]+)(?:;user=([^;]+))?;pid=(0x[0-9a-f]+)(?:;upcall_target=(mount|app))?$/;
 my @descriptionv2_keys = ('keyuid', 'keygid', 'perms', 'host', 'ipv', 'ip', 'sec', 'uid', 'creduid', 'user', 'pid', 'upcall_target');
 
+my $conf_split_re = qr/^([^\s]+)\s+(.+)/; # splitting each line of the conf file
+my $split_char = '[,;]'; # separator for match and options fields
+
 sub log_msg {
 	my $msg_level = shift;
 
@@ -105,6 +109,51 @@  sub parse_key_description {
 		$key_vars{'sec'}, $key_vars{'uid'}, $key_vars{'creduid'},
 		$key_vars{'user'}, $key_vars{'upcall_target'});
 }
+sub match_criterion {
+	my $criterion = shift;
+
+	return 0;
+}
+sub match_criteria {
+	my $criteria_str = shift;
+
+	my @criterion_ary = split /$split_char/, $criteria_str;
+	foreach my $criterion (@criterion_ary) {
+		return 0 if (! match_criterion($criterion));
+	}
+	return 1;
+}
+sub parse_conf_line {
+	my $line = shift;
+
+	if ($line !~ $conf_split_re) {
+		log_msg 0, "unparseable line in $helper_conf: $line";
+		return;
+	}
+	my ($criteria_str, $opts_str) = $line =~ $conf_split_re;
+
+	if (match_criteria($criteria_str)) {
+		log_msg 1, "matched '$criteria_str'; options '$opts_str'";
+		exec_upcall;
+	}
+}
+sub parse_conf_file {
+	if (! open CONF, '<', $helper_conf) {
+		log_msg 0, "could not open $helper_conf: $1";
+		log_msg 1, "executing upcall with default parameters";
+		exec_upcall;
+	}
+	while (<CONF>) {
+		my $line = $_;
+		$line =~ s/#.+//;
+		chomp $line;
+
+		next if ($line eq '');
+
+		parse_conf_line $line;
+	}
+	close CONF;
+}
 
 if ($#ARGV ne 0) {
 	if (-t STDOUT) {
@@ -118,6 +167,13 @@  $keyid = $ARGV[0];
 
 log_msg 1, "$log_ident - keyid: $keyid";
 
+# no config file?  just execute with default options
+exec_upcall if (! -e $helper_conf);
+
 my $key_description_str = get_key_description $keyid;
 parse_key_description $key_description_str;
+
+parse_conf_file;
+
+log_msg 1, "no key description matched; executing upcall with default parameters";
 exec_upcall;