cifs.upcall helper script enabling complex key description matching
Subsystems (2)
cover cifs.upcall helper script enabling complex key description matching
Background:
The request-key mechanism, used to execute upcalls, uses string matching of lines in the conf file to determine which program it should run to instantiate a key. This can usually be done based solely on the requested key type.
It also allows further selection of the executable and/or program arguments, based on string matches of the description and callout information fields of the upcall with lines of the request-key conf files. Each field (key type, key description, callout information) can also contain a single '*' wildcard to be used when doing the string matches, and the string must fully match in order to be selected.
When the kernel performs the upcall during a cifs mount in which kerberos is involved, the 'cifs.spnego' key type is requested. The key's description contains a number of key=value pairs, providing information such as the server's name and IP address, the username of the mount, and uid under which to search for the kerberos credentials.
An example key description:
ver=0x2;host=smbserver;ip4=192.168.122.11;sec=krb5;uid=0x0;creduid=0x0;user=user1;pid=0xbaf7d
request-key will attempt to match the key's description with the string in the description field of the config file, and executes cifs.upcall with the given arguments. However, the default configuration simply calls cifs.upcall for all 'cifs.spnego' key types, regardless of the key's description, and no additional arguments (other than the key id) are given to the upcall:
create cifs.spnego * * /usr/sbin/cifs.upcall %k
Specifying the string more explicitly to match the key's description more fully allows differentiation between servers, username, etc., and cifs.upcall will then execute with arguments specific to that server or user, such as (with catch-all for fallback to the default cifs.upcall execution):
create cifs.spnego ver=0x2;host=smbserver;ip4=192.168.122.11;sec=krb5;uid=0x0;creduid=0x501;user=user1;* * /usr/sbin/cifs.upcall -K /etc/smbserver_user1.keytab %k
create cifs.spnego * * /usr/sbin/cifs.upcall %k
Unfortunately, the key's description includes the pid of the requesting process, so in order to match the description string in the config file with the key's description, the '*' wildcard must be used to cover that portion of the description string. As a result, each description string in the config file must fully specify all of the other fields. This includes cases where a server name may resolve to multiple IP addresses; each combination must be individually specified. For example:
create cifs.spnego ver=0x2;host=smbserver;ip4=192.168.122.11;sec=krb5;uid=0x0;creduid=0x501;user=user1;* * /usr/sbin/cifs.upcall -K /etc/smbserver_user1.keytab %k
create cifs.spnego ver=0x2;host=smbserver;ip4=192.168.122.12;sec=krb5;uid=0x0;creduid=0x501;user=user1;* * /usr/sbin/cifs.upcall -K /etc/smbserver_user1.keytab %k
create cifs.spnego ver=0x2;host=smbserver;ip4=192.168.122.13;sec=krb5;uid=0x0;creduid=0x501;user=user1;* * /usr/sbin/cifs.upcall -K /etc/smbserver_user1.keytab %kThis is inflexible, and can quickly become unmanageable; the addition or change of an IP address to a server hostname would require updating the /etc/request-key.d/cifs.spnego.conf file on all clients, and adding a second user & keytab would require replicating the lines, with just minor variations:
create cifs.spnego ver=0x2;host=smbserver;ip4=192.168.122.11;sec=krb5;uid=0x0;creduid=0x502;user=user2* * /usr/sbin/cifs.upcall -K /etc/smbserver_user2.keytab %k
create cifs.spnego ver=0x2;host=smbserver;ip4=192.168.122.12;sec=krb5;uid=0x0;creduid=0x502;user=user2* * /usr/sbin/cifs.upcall -K /etc/smbserver_user2.keytab %k
create cifs.spnego ver=0x2;host=smbserver;ip4=192.168.122.13;sec=krb5;uid=0x0;creduid=0x502;user=user2* * /usr/sbin/cifs.upcall -K /etc/smbserver_user2.keytab %kIn addition, enabling use of gssproxy for credential retrieval is done by setting an environment variable, rather than an argument to cifs.upcall, changing the execution of cifs.upcall:
create cifs.spnego * * /usr/bin/env GSS_USE_PROXY=yes /usr/sbin/cifs.upcall %k
This script:
This helper script takes the place of cifs.upcall in the request-key.conf file, and is called directly for all 'cifs.spnego' types. It allows the use of more complex matching criteria of the fields of the key description, then executes the actual cifs.upcall with the appropriate arguments.
The matching is done by reading from the helper's config file (/etc/cifs-upcall-helper.conf), where each line consists of a set of match criteria and options to pass to cifs.upcall in the event of a match.
Each of the match criteria consists of a field name (parsed from the fields of the key's description - 'host', 'user', 'creduid', etc.), a comparison operator, and the value to compare, and multiple match criteria may be specified (all of which must pass in order to be selected).
Comparison operators include string matching and regexes for string fields (host and user), numeric comparisons (<, >, =, !=, etc.) for uid and creduid, and various IPv4 comparisons (ip ranges, network/netmask, network/prefix, etc.) (IPv6 still to be implemented).
When a match is made, the options list can specify flags to cifs.upcall; a keytab or krb5.conf file to use, a keyword to set the GSS_USE_PROXY environment variable, etc. Several % macros are also available, expanding to the values of the fields in the key's description.
For example, the following line can replace the 3 lines above which specify the hostname 'smbserver' and 'user1' username, and give a keytab to use:
host=smbserver,user=user1 keytab=/etc/smbserver_user1.keytab
or to cover multiple users, use %u macro for username:
host=smbserver keytab=/etc/smbserver_%u.keytab
The helper script then sets the appropriate arguments (and/or environment variables), and execs cifs.upcall.
In addition to matching, key descriptions, the config file is also used to increase debugging (to syslog) and to set the default arguments for future matches in the file:
# set the default arguments to cifs.upcall for subsequent matches
default use_proxy # increase helper's syslog verbosity to 'info' level or 'debug'
# level (from errors-only):
verbosity info
verbosity debugDetails of the implemented matching rules can be found in the README file, and additional example config file lines are in cifs-upcall-helper.conf.examples.
I welcome feedback on the helper script, as well as thoughts on its inclusion in cifs-utils.
Frank Sorenson (12):
contrib: add directory and documentation for cifs-upcall-helper
upcall-helper: add a sample config file
contrib: add cifs-upcall-helper script
upcall-helper: obtain and parse key description, assigning fields to
key_vars
upcall-helper: open and read each line of the helper's config file
upcall-helper: set upcall_opts on a match, or if 'default' is given
upcall-helper: set log level from config file
upcall-helper: build command-line and set environment variables for
cifs.upcall
upcall-helper: replace macros in upcall argument strings
upcall-helper: add string comparison
upcall-helper: add uid comparison
upcall-helper: add IP address comparisons
contrib/upcall-helper/README | 138 ++++++ contrib/upcall-helper/cifs-upcall-helper | 400 ++++++++++++++++++ .../cifs-upcall-helper.conf.examples | 66 +++ 3 files changed, 604 insertions(+) create mode 100644 contrib/upcall-helper/README create mode 100755 contrib/upcall-helper/cifs-upcall-helper create mode 100644 contrib/upcall-helper/cifs-upcall-helper.conf.examples
Signature · 1 line
2.49.0
RFC PATCH 1/12 contrib: add directory and documentation for cifs-upcall-helper
Add a directory in contrib for the cifs-upcall-helper script, with README that explains the script's use.
--- contrib/upcall-helper/README | 138 +++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 contrib/upcall-helper/README
contrib/upcall-helper/README new file +138 -0
@@ -0,0 +1,138 @@ +This helper script is used in /etc/request-key.d/cifs.spnego.conf to + enable complex matching of fields in the description of the key + when using krb5 for cifs mounts; when a match is found, cifs.upcall + is then executed with the specified options. + +This script will read each line of /etc/cifs-upcall-helper.conf, applying + match criteria in the first field to the key description; if all + criteria match, the options specified in the second field are used to + construct the command-line for calling the cifs.upcall program. + +If the cifs-upcall-helper.conf file is not present, or if no lines match, + the cifs.upcall is executed with the current default options (if any) + + +To use this helper script, install this script at + /usr/sbin/cifs-upcall-helper, and replace the following line in + /etc/request-key.d/cifs.spnego.conf: + + create cifs.spnego * * /usr/bin/cifs.upcall %k + + with + + create cifs.spnego * * /usr/sbin/cifs-upcall-helper %k + + +each line of the cifs-upcall-helper.conf file has the following format: + + <line> := + <selection_criteria><whitespace><options> + default<whitespace><options> + log_level<whitespace><log_level> + + <selection_criteria> := <criterion>[<delimiter><criterion>]* + + <options> := <option>[<delimiter><option>]* + + <delimiter> := [,;] + + <criterion> := + * + host<string_comparator><host_string> + user<string_comparator><user_string> + sec<string_comparator><sec_string> + + ip4<ip4_comparator><ip4_string> + ip6<string_comparator><ip6_string> + (TODO: enhance ip6_comparator) + + uid<numeric_comparator><uid_string> + creduid<numeric_comparator><uid_string> + + <string_comparator> := + '=' | '==' | '!=' | '~' | '!~' + '=', '==', and '!=' compare as 'globs' + ('*' is a wildcard which represents zero or more + characters; all other characters are literals) + '~' and '!~' compare the string as a 'regex' + + <host_string> := <character_string> + <user_string> := <character_string> + <sec_string> := krb5 | mskrb5 | iakerb + + <character_string> := [-a-zA-Z0-9_.] + + <ip4_comparator> := '=', '==', '!=' + + <ip4_string> := + <ip4_addr> | <ip4_range> | <ip4_net_netmask> | <ip4_net_prefix> + + <ip4_addr> := [0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3} + <ip4_range> := <ip4_addr>-<ip4_addr> + <ip4_net_netmask> := <ip4_addr>/<ip4_addr> + <ip4_net_prefix> := <ip4_addr>/<ip4_prefix> + <ip4_prefix> := ([0-9]|[12][0-9]|3[0-2]) + + (invalid netmask/prefix results in 'not a match', regardless of comparator) + + + <numeric_comparator> := '<' | '<=' | '=' | '==' | '>=' | '>' | '!=' + + <uid_string> := [0-9]+ | 0x[0-9a-fA-F]+ + + <option> := + * | - + keytab=<path> + krb5conf=<path> + krb5_trace=<path> + use-proxy|use_proxy + no-env-probe|no_env_probe + trust-dns|trust_dns + legacy-uid|legacy_uid + + with the exception of use_proxy and krb5_trace, options are passed to 'cifs.upcall' + with the relevant command-line argument; see the OPTIONS section of the manpage + for cifs.upcall(8) for further details + + use_proxy sets the GSS_USE_PROXY environment variable prior to calling 'cifs.upcall', + enabling the use of gssproxy; see the ENVIRONMENT VARIABLES section of the manpage + for cifs.upcall(8) for further details + + specifying 'krb5_trace' with path will set the KRB5_TRACE environment variable to the + path, prior to calling 'cifs.upcall'; setting this environment variable causes + kerberos-related tracing to be written to the file; see the ENVIRONMENT VARIABLES + section of the manpage for kerberos(7) for further details + + + in the <option> field, the <path> values for <keytab>, <krb5conf>, and <krb5_trace> + will also accept the following macros: + %h server hostname + %i server IP address + %s sec type + %U uid + %c creduid + %u username + + + when 'default' is specified as the selection criteria, all currently-specified + default options are cleared, and the <options> specified (if any) are + set as defaults + the line does not match, and processing continues with the next line + use option '*' or '-' as a placeholder; options will be cleared, but no + new options set + NOTE: default must be the only criteria + + + <log_level> := (0|1|2|errors|info|debug) + + when 'log_level' is specified as the selection criteria, + the logging level is set to the specified level. Options are: + 0|errors + only errors are logged to syslog, at level LOG_ERR + 1|info + more verbose information is logged to syslog, at level LOG_INFO + 2|debug + verbose debugging messages are output to syslog, at level LOG_INFO + any other values + verbosity is unchanged + NOTE: log_level must be the only criteria
Signature · 1 line
2.49.0
RFC PATCH 2/12 upcall-helper: add a sample config file
In reply to Frank SorensonAdd a file containing example config lines, demonstrating various ways to match hostname, user, IP, etc., and options to apply when executing cifs.upcall.
--- .../cifs-upcall-helper.conf.examples | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 contrib/upcall-helper/cifs-upcall-helper.conf.examples
contrib/upcall-helper/cifs-upcall-helper.conf.examples new file +66 -0
@@ -0,0 +1,66 @@ +# example cifs-upcall-helper.conf lines: + +# set a default option to be applied on any future matches +default use_proxy + +# set the log level to 1/info: +log_level info + +# match usernames; add the keytab option (to already configured default of +# 'use_proxy') +user=cifsclientvm23 keytab=/etc/cifsclientvm23.keytab +user=user2 keytab=/etc/cifsuser2.keytab + +# clear current default options; don't set any default options +default - + +# match usernames with regex; add the keytab options using % macro for username +user~cifsuser[1-5] keytab=/etc/cifs_keytabs/%u.keytab + +# match a cifs server hostname; no additional options applied +host=server1 - + +# all hostnames containing 'chicago', and use a single keytab for all hosts +host=*chicago* keytab=/etc/cifs-chicago.keytab + +# all hostnames containing 'london', and use a separate keytab for +# each host (%h macro for hostname): +host=*london* keytab=/etc/cifs-%h.keytab + +# match cifs server hostnames using regex; add keytab option +host~svr(chicago|london)[0-9]+ keytab=/etc/cifs_keytabs/chi_ldn.keytab + +# match cifs server hostnames using regex to select some, then exclude a specific +# hostname; add keytab +host~svrchicago[1-5],host!=svrchicago3 keytab=/etc/cifs_keytabs/chi_not3.keytab + +# for 'user1' (regardless of hostname, etc.), enable gssproxy and +# specify a krb5.conf file to use: +user=user1 krb5conf=/etc/krb5-cifs-user1.conf,use_proxy + +# match specified uid; add keytab +uid=501 keytab=/etc/cifsuser5.keytab + +# match uid greater than 500; add keytab +uid>500 keytab=/etc/cifsusers.keytab + +# all uids from 1000-2000, executing cifs.upcall with current default arguments +uid>=1000,uid<=2000 - + +# several ip4 formats; no additional options +ip4=192.168.122.73 - +ip4=192.168.122.70-192.168.122.80 - +ip4=192.168.123.0/255.255.255.0 - +ip4=192.168.123.0/24 - + +# any IP except this one; create/append to kerberos trace log +ip4!=192.168.122.73 krb5_trace=/tmp/krb5_trace.out + +# match both an ip AND a user (all criteria must be met to match); add keytab +ip4=10.2.3.4,user=user1 keytab=/etc/cifsuser1.keytab + + +# always match, don't add any more options; equivalent to reaching +# the end of the file without matching any lines; '*' can be used +# to bypass the remainder of the file +* -
Signature · 1 line
2.49.0
RFC PATCH 3/12 contrib: add cifs-upcall-helper script
In reply to Frank SorensonAdd a helper script for cifs.upcall, enabling complex matching of the key's description, and execution of cifs.upcall with specified options.
At this stage, the script does little more than call cifs.upcall.
--- contrib/upcall-helper/cifs-upcall-helper | 74 ++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100755 contrib/upcall-helper/cifs-upcall-helper
contrib/upcall-helper/cifs-upcall-helper new file +74 -0
@@ -0,0 +1,74 @@ +#!/usr/bin/perl -w + +# Copyright (C) Frank Sorenson ([email protected]) 2025 + +# helper script to replace cifs.upcall in /etc/request-key.d/cifs.spnego +# to enable complex matching of fields in the description field +# of the key when using krb5 for cifs mounts; cifs.upcall is then +# executed with specified arguments. + + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +use strict; +use warnings; +use Sys::Syslog; + +my $log_ident = 'cifs-upcall-helper'; +my $log_open = 0; +my $log_level = 0; +# 0 - only errors (LOG_ERR) +# 1 - relevant messages (LOG_INFO) +# 2 - verbose debugging (LOG_INFO) + +my $keyid; + +sub log_msg { + my $msg_level = shift; + + if ($log_level >= $msg_level) { + my $msg = shift; + my $syslog_level = 'info'; + + if (! $log_open) { + $log_open = 1 if openlog($log_ident, 'ndelay,pid', 'daemon'); + } + return if ! $log_open; + + $syslog_level = 'err' if $msg_level < 1; + + syslog($syslog_level, $msg); + } +} +sub exec_upcall { + my @upcall_args = ( '/usr/sbin/cifs.upcall' ); + push @upcall_args, $keyid; + + log_msg 1, sprintf("executing cifs.upcall: %s", join(' ', @upcall_args)); + exec { $upcall_args[0] } @upcall_args; +} + +if ($#ARGV ne 0) { + if (-t STDOUT) { + printf "usage: $0 <keyid>\n"; + } else { + log_msg 0, "usage $0 <keyid>"; + } + exit -1; +} +$keyid = $ARGV[0]; + +log_msg 1, "$log_ident - keyid: $keyid"; + +exec_upcall;
Signature · 1 line
2.49.0
RFC PATCH 4/12 upcall-helper: obtain and parse key description, assigning fields to key_vars
In reply to Frank SorensonExecute keyctl to get the key's description, and parse the description fields, assigning to key_vars for use during matching.
Still just executing the upcall with keyid.
--- contrib/upcall-helper/cifs-upcall-helper | 49 ++++++++++++++++++++++++ 1 file changed, 49 insertions(+)
contrib/upcall-helper/cifs-upcall-helper +49 -0
@@ -23,6 +23,7 @@ use strict; use warnings; +use POSIX; use Sys::Syslog; my $log_ident = 'cifs-upcall-helper';
@@ -33,6 +34,15 @@ my $log_level = 0; # 2 - verbose debugging (LOG_INFO) my $keyid; +my %key_vars; + +# keyctl rdescribe output format: +# cifs.spnego;0;0;39010000;ver=0x2;host=vm1;ip4=192.168.122.73;sec=krb5;uid=0x0;creduid=0x0;user=user1;pid=0xbaf7d;upcall_target=mount +# +# user=<username> is optional +# upcall_target=(mount|app) is optional +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'); sub log_msg { my $msg_level = shift;
@@ -58,6 +68,43 @@ sub exec_upcall { log_msg 1, sprintf("executing cifs.upcall: %s", join(' ', @upcall_args)); exec { $upcall_args[0] } @upcall_args; } +sub get_key_description { + my $keyid = shift; + + if (! open KEYCTL, "/usr/bin/keyctl rdescribe $keyid 2>&1 |") { + log_msg 0, "Error executing keyctl: $!"; + exec_upcall; + } + my $rdescribe = <KEYCTL>; + chomp $rdescribe; + close KEYCTL; + if ($?) { + log_msg 0, "Error executing keyctl: $rdescribe"; + exec_upcall; + } + + log_msg 2, "description for key $keyid: $rdescribe"; + return $rdescribe; +} +sub parse_key_description { + my $key_description_str = shift; + if ($key_description_str !~ $key_descriptionv2_re) { + log_msg 0, "could not match key description to known format: $key_description_str"; + log_msg 1, "executing upcall with default parameters"; + exec_upcall; + } + @key_vars{@descriptionv2_keys} = $key_description_str =~ $key_descriptionv2_re; + + $key_vars{'uid'} = scalar POSIX::strtol($key_vars{'uid'}, 16); + $key_vars{'creduid'} = scalar POSIX::strtol($key_vars{'creduid'}, 16); + $key_vars{'user'} = '' if !defined $key_vars{'user'}; + $key_vars{'upcall_target'} = '' if !defined $key_vars{'upcall_target'}; + log_msg 1, sprintf("description for key %d keyuid: %d, keygid: %d, perms: %d, host %s, ipv: %s, ip: %s, sec: %s, uid: %d, creduid: %d, user %s, upcall_target: %s", + $keyid, $key_vars{'keyuid'}, $key_vars{'keygid'}, $key_vars{'perms'}, + $key_vars{'host'}, $key_vars{'ipv'}, $key_vars{'ip'}, + $key_vars{'sec'}, $key_vars{'uid'}, $key_vars{'creduid'}, + $key_vars{'user'}, $key_vars{'upcall_target'}); +} if ($#ARGV ne 0) { if (-t STDOUT) {
@@ -71,4 +118,6 @@ $keyid = $ARGV[0]; log_msg 1, "$log_ident - keyid: $keyid"; +my $key_description_str = get_key_description $keyid; +parse_key_description $key_description_str; exec_upcall;
Signature · 1 line
2.49.0
RFC PATCH 5/12 upcall-helper: open and read each line of the helper's config file
In reply to Frank SorensonOpen 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.
--- contrib/upcall-helper/cifs-upcall-helper | 56 ++++++++++++++++++++++++ 1 file changed, 56 insertions(+)
contrib/upcall-helper/cifs-upcall-helper +56 -0
@@ -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;
Signature · 1 line
2.49.0
RFC PATCH 6/12 upcall-helper: set upcall_opts on a match, or if 'default' is given
In reply to Frank SorensonWhen all criteria match, or if 'default' is specified, set upcall_opts using the options field of the conf file line.
--- contrib/upcall-helper/cifs-upcall-helper | 34 ++++++++++++++++++++++++ 1 file changed, 34 insertions(+)
contrib/upcall-helper/cifs-upcall-helper +34 -0
@@ -34,6 +34,7 @@ my $log_level = 0; # 2 - verbose debugging (LOG_INFO) my $helper_conf = '/etc/cifs-upcall-helper.conf'; +my %upcall_opts = (); my $keyid; my %key_vars;
@@ -109,6 +110,32 @@ sub parse_key_description { $key_vars{'sec'}, $key_vars{'uid'}, $key_vars{'creduid'}, $key_vars{'user'}, $key_vars{'upcall_target'}); } +sub set_upcall_opts { + my $opts_str = shift; + + my @opts = split /$split_char/, $opts_str; + foreach my $opt (@opts) { + if ((my $field, my $val) = $opt =~ /^([^=]+)=(.+)$/) { + if ($field eq 'keytab' or $field eq 'krb5conf' or $field eq 'krb5_trace') { + $upcall_opts{$field} = $val; + } else { + log_msg 0, "unrecognized upcall option: $opt"; + } + } elsif ($opt eq 'use_proxy' or $opt eq 'use-proxy') { + $upcall_opts{'use_proxy'} = 1; + } elsif ($opt eq 'legacy_uid' or $opt eq 'legacy-uid') { + $upcall_opts{'legacy_uid'} = 1; + } elsif ($opt eq 'trust_dns' or $opt eq 'trust-dns') { + $upcall_opts{'trust_dns'} = 1; + } elsif ($opt eq 'no_env_probe' or $opt eq 'no-env-probe') { + $upcall_opts{'no_env_probe'} = 1; + } elsif ($opt eq '*' or $opt eq '-') { + } else { + log_msg 0, "unrecognized upcall option: $opt"; + } + } +} + sub match_criterion { my $criterion = shift;
@@ -132,8 +159,15 @@ sub parse_conf_line { } my ($criteria_str, $opts_str) = $line =~ $conf_split_re; + if ($criteria_str eq 'default' or $criteria_str eq 'defaults') { + %upcall_opts = (); # clear defaults + log_msg 1, "setting default options '$opts_str'"; + set_upcall_opts $opts_str; + return; + } if (match_criteria($criteria_str)) { log_msg 1, "matched '$criteria_str'; options '$opts_str'"; + set_upcall_opts $opts_str; # similar to defaults, but don't clear first exec_upcall; } }
Signature · 1 line
2.49.0
RFC PATCH 7/12 upcall-helper: set log level from config file
In reply to Frank SorensonIf 'log_level' is specified in the conf file, adjust the log level as specified.
--- contrib/upcall-helper/cifs-upcall-helper | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
contrib/upcall-helper/cifs-upcall-helper +18 -0
@@ -150,6 +150,19 @@ sub match_criteria { } return 1; } +sub set_log_level { + my $new_level_str = shift; + + if ($new_level_str eq 'errors' or $new_level_str eq '0') { + $log_level = 0; + } elsif ($new_level_str eq 'info' or $new_level_str eq '1') { + $log_level = 1; + } elsif ($new_level_str eq 'debug' or $new_level_str eq '2') { + $log_level = 2; + } else { # don't change log level + log_msg 0, "unrecognized log level: $new_level_str"; + } +} sub parse_conf_line { my $line = shift;
@@ -159,6 +172,11 @@ sub parse_conf_line { } my ($criteria_str, $opts_str) = $line =~ $conf_split_re; + if ($criteria_str eq 'log_level') { + log_msg 1, "setting log level '$opts_str'"; + set_log_level $opts_str; + return; + } if ($criteria_str eq 'default' or $criteria_str eq 'defaults') { %upcall_opts = (); # clear defaults log_msg 1, "setting default options '$opts_str'";
Signature · 1 line
2.49.0
RFC PATCH 8/12 upcall-helper: build command-line and set environment variables for cifs.upcall
In reply to Frank SorensonWhen executing cifs.upcall, use the values of upcall_opts to determine the command-line arguments, and set environment variables if specified.
--- contrib/upcall-helper/cifs-upcall-helper | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
contrib/upcall-helper/cifs-upcall-helper +17 -0
@@ -68,6 +68,23 @@ sub log_msg { } 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}); + } elsif ($opt eq 'krb5conf') { + push @upcall_args, ('-k', $upcall_opts{$opt}); + } elsif ($opt eq 'krb5_trace') { + $ENV{'KRB5_TRACE'} = $upcall_opts{$opt}; + } elsif ($opt eq 'use_proxy') { + $ENV{'GSS_USE_PROXY'} = 'yes'; + } elsif ($opt eq 'legacy_uid') { + push @upcall_args, '-l'; + } elsif ($opt eq 'trust_dns') { + push @upcall_args, '-t'; + } elsif ($opt eq 'no_env_probe') { + push @upcall_args, '-E'; + } + } push @upcall_args, $keyid; log_msg 1, sprintf("executing cifs.upcall: %s", join(' ', @upcall_args));
Signature · 1 line
2.49.0
RFC PATCH 9/12 upcall-helper: replace macros in upcall argument strings
In reply to Frank SorensonReplace any macros in the upcall argument strings with values of the key variables.
--- contrib/upcall-helper/cifs-upcall-helper | 33 +++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-)
contrib/upcall-helper/cifs-upcall-helper +30 -3
@@ -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') {
Signature · 1 line
2.49.0
RFC PATCH 10/12 upcall-helper: add string comparison
In reply to Frank SorensonUse string comparison (as glob or regex) for 'host', 'user', 'sec', and 'upcall_target' matching.
--- contrib/upcall-helper/cifs-upcall-helper | 29 +++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-)
contrib/upcall-helper/cifs-upcall-helper +28 -1
@@ -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;
Signature · 1 line
2.49.0
RFC PATCH 11/12 upcall-helper: add uid comparison
In reply to Frank SorensonUse numeric comparisons for 'uid' and 'creduid' matching.
--- contrib/upcall-helper/cifs-upcall-helper | 14 ++++++++++++++ 1 file changed, 14 insertions(+)
contrib/upcall-helper/cifs-upcall-helper +14 -0
@@ -50,6 +50,7 @@ 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)(=|==|!=|~|!~)(.+)/; +my $uid_comparison_re = qr/^(uid|creduid)(<|<=|=|==|>=|>|!=)(0x[0-9a-f]+|[0-9]+)$/; sub log_msg { my $msg_level = shift;
@@ -198,6 +199,17 @@ sub check_string_match { return $result; } +sub check_uid_match { + my $key_uid = shift; + my $comparison = shift; + my $comparison_uid = shift; + + $comparison_uid = scalar POSIX::strtol($comparison_uid, 16) if (substr($comparison_uid, 0, 2) eq '0x'); + + my $comparison_string = sprintf("%d %s %d", $key_uid, $comparison, $comparison_uid); + return 1 if (eval $comparison_string); + return 0; +} sub match_criterion { my $criterion = shift;
@@ -206,6 +218,8 @@ sub match_criterion { 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)); + } elsif (($field, $comparator, $match_pattern) = $criterion =~ $uid_comparison_re) { + return 0 if (! check_uid_match($key_vars{$field}, $comparator, $match_pattern)); } else { log_msg 0, "unrecognized match string: $criterion"; return 0;
Signature · 1 line
2.49.0
RFC PATCH 12/12 upcall-helper: add IP address comparisons
In reply to Frank SorensonAdd IPv4 comparisons for matching IP addresses, IP ranges, network/netmask, etc.:
<ip_address>
<ip_address>-<ip_address>
<network_address>/<netmask>
<network_address>/<prefix>For IPv6, use string matching; TODO: enhance ip6 matching.
--- contrib/upcall-helper/cifs-upcall-helper | 84 ++++++++++++++++++++++++ 1 file changed, 84 insertions(+)
contrib/upcall-helper/cifs-upcall-helper +84 -0
@@ -52,6 +52,14 @@ my $split_char = '[,;]'; # separator for match and options fields my $string_comparison_re = qr/^(host|user|sec|upcall_target)(=|==|!=|~|!~)(.+)/; my $uid_comparison_re = qr/^(uid|creduid)(<|<=|=|==|>=|>|!=)(0x[0-9a-f]+|[0-9]+)$/; +my $octet_re = qr/\d{1,2}|[01]\d{2}|2[0-4]\d|25[0-5]/; +my $ip4_re = qr/(?:$octet_re\.){3}$octet_re/; +my $ip4_range_re = qr/($ip4_re)-($ip4_re)/; +my $ip4_nm_re = qr/($ip4_re)\/($ip4_re)/; +my $ip4_cidr_re = qr/($ip4_re)\/(\d+)/; +my $ip4_comparison_re = qr/^ip4(=|==|!=)(\*|$ip4_re|$ip4_range_re|$ip4_nm_re|$ip4_cidr_re)$/; +my $ip6_comparison_re = qr/^ip6(=|==|!=|~|!~)(.+)/; # TODO: enhance ip6 comparator + sub log_msg { my $msg_level = shift;
@@ -210,6 +218,78 @@ sub check_uid_match { return 1 if (eval $comparison_string); return 0; } +sub ip4_to_decimal { + my $ip4 = shift; + + my @bytes = split /\./, $ip4; + return $bytes[0] * 2**24 + $bytes[1] * 2**16 + $bytes[2] * 2**8 + $bytes[3]; +} +sub check_ip4_match { + my $key_ip4 = shift; + my $comparator = shift; + my $match_str = shift; + + log_msg 2, sprintf("check_ip4_match(key_ip: %s, comparator: %s, match_str: %s)", $key_ip4, $comparator, $match_str); + + my $negate = ($comparator eq '!=') ? 1 : 0; + log_msg 2, "negating match in check_ip4_match" if $negate; + + my ($ip4_net_addr, $ret); + + if ($match_str eq '*') { + log_msg 2, "ip4 match is '*' (always true)%s", + ($negate ? " but negated" : ""); + return $negate ? 0 : 1; + } elsif ((my $ip4_addr) = $match_str =~ "^($ip4_re)\$") { + log_msg 2, "ip4 match is a simple ip"; + + $ret = ($key_ip4 eq $ip4_addr) ? 1 : 0; + return $ret ^ $negate; + } elsif ((my $ip4_range_low, my $ip4_range_high) = $match_str =~ "^($ip4_range_re)\$") { + log_msg 2, "ip4 match is an ip range"; + + $ip4_range_low = ip4_to_decimal $ip4_range_low; + $ip4_range_high = ip4_to_decimal $ip4_range_high; + $key_ip4 = ip4_to_decimal $key_ip4; + + $ret = ($key_ip4 >= $ip4_range_low and $key_ip4 <= $ip4_range_high) ? 1 : 0; + return $ret ^ $negate; + } elsif (($ip4_net_addr, my $ip4_netmask) = $match_str =~ "^($ip4_nm_re)\$") { + log_msg 2, "ip4 match is an ip/netmask"; + + # validate that netmask is valid: bits of 1s, followed by bits of zeros + $ip4_netmask = ip4_to_decimal $ip4_netmask; + my $ip4_netmask_str = sprintf("%032b", $ip4_netmask); + + # invalid netmask results in 'not a match', regardless of comparator + log_msg 0, "invalid netmask in ip4 match: '$ip4_netmask_str'" if ($ip4_netmask_str !~ /^1*0*$/); + return 0 if ($ip4_netmask_str !~ /^1*0*$/); + + $key_ip4 = ip4_to_decimal $key_ip4; + my $ip4_range_low = ip4_to_decimal($ip4_net_addr) & $ip4_netmask; + my $ip4_range_high = $ip4_range_low | ($ip4_netmask ^ 0xffffffff); + + $ret = ($key_ip4 >= $ip4_range_low and $key_ip4 <= $ip4_range_high) ? 1 : 0; + return $ret ^ $negate; + } elsif (($ip4_net_addr, my $prefix) = $match_str =~ "^($ip4_cidr_re)\$") { + log_msg 2, "ip4 match is ip/prefix (cidr)"; + + # invalid prefix results in 'not a match', regardless of comparator + log_msg 0, "invalid prefix in ip4 match: $prefix" if $prefix > 32; + return 0 if $prefix > 32; + + $key_ip4 = ip4_to_decimal $key_ip4; + my $ip4_range_low = ip4_to_decimal $ip4_net_addr; + my $bits_remaining = 32 - $prefix; + my $ip4_range_high = $ip4_range_low + 2**$bits_remaining - 1; + + $ret = ($key_ip4 >= $ip4_range_low and $key_ip4 <= $ip4_range_high) ? 1 : 0; + return $ret ^ $negate; + } + log_msg 0, "ip4 match didn't match any known format"; + + return 0; +} sub match_criterion { my $criterion = shift;
@@ -220,6 +300,10 @@ sub match_criterion { return 0 if (! check_string_match($key_vars{$field}, $comparator, $match_pattern)); } elsif (($field, $comparator, $match_pattern) = $criterion =~ $uid_comparison_re) { return 0 if (! check_uid_match($key_vars{$field}, $comparator, $match_pattern)); + } elsif (($comparator, $match_pattern) = $criterion =~ $ip4_comparison_re) { + return 0 if (! check_ip4_match($key_vars{'ip'}, $comparator, $match_pattern)); + } elsif (($field, $comparator, $match_pattern) = $criterion =~ $ip6_comparison_re) { # + return 0 if (! check_string_match($key_vars{'ip'}, $comparator, $match_pattern)); } else { log_msg 0, "unrecognized match string: $criterion"; return 0;
Signature · 1 line
2.49.0