diff mbox series

[cifs-utils,RFC,09/12] upcall-helper: replace macros in upcall argument strings

Message ID 20250510161609.2615639-10-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
Replace any macros in the upcall argument strings with values
of the key variables.

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

Patch

diff --git a/contrib/upcall-helper/cifs-upcall-helper b/contrib/upcall-helper/cifs-upcall-helper
index 18e41df..fdd0c9c 100755
--- a/contrib/upcall-helper/cifs-upcall-helper
+++ b/contrib/upcall-helper/cifs-upcall-helper
@@ -66,15 +66,42 @@  sub log_msg {
 		syslog($syslog_level, $msg);
 	}
 }
+sub replace_macros {
+	my %macros = (
+		'%h' => 'host',
+		'%i' => 'ip',
+		'%s' => 'sec',
+		'%U' => 'uid',
+		'%c' => 'creduid',
+		'%u' => 'user',
+	);
+	my $str = shift;
+	my $result = '';
+	my $len = length $str;
+
+	for (my $i = 0 ; $i < $len ; $i++) {
+		my $chars = substr $str, $i, 2;
+		if (defined $macros{$chars}) {
+			$result .= $key_vars{$macros{$chars}};
+			$i += 1;
+		} elsif ($chars eq '%%') {
+			$result .= '%';
+			$i += 1;
+		} else {
+			$result .= substr $chars, 0, 1;
+		}
+	}
+	return $result;
+}
 sub exec_upcall {
 	my @upcall_args = ( '/usr/sbin/cifs.upcall' );
 	foreach my $opt (keys %upcall_opts) {
 		if ($opt eq 'keytab') {
-			push @upcall_args, ('-K', $upcall_opts{$opt});
+			push @upcall_args, ('-K', replace_macros($upcall_opts{$opt}));
 		} elsif ($opt eq 'krb5conf') {
-			push @upcall_args, ('-k', $upcall_opts{$opt});
+			push @upcall_args, ('-k', replace_macros($upcall_opts{$opt}));
 		} elsif ($opt eq 'krb5_trace') {
-			$ENV{'KRB5_TRACE'} = $upcall_opts{$opt};
+			$ENV{'KRB5_TRACE'} = replace_macros($upcall_opts{$opt});
 		} elsif ($opt eq 'use_proxy') {
 			$ENV{'GSS_USE_PROXY'} = 'yes';
 		} elsif ($opt eq 'legacy_uid') {