diff --git a/build/rubygem-simp-cli.spec b/build/rubygem-simp-cli.spec index f39047fb..175057c5 100644 --- a/build/rubygem-simp-cli.spec +++ b/build/rubygem-simp-cli.spec @@ -233,6 +233,13 @@ EOM options, e.g. `LDAP: {}`) instead of the deprecated `sssd::domains` Array, and omits the key when there are no domains. Requires simp/sssd 11.0.0 or later; the item class `SssdDomains` is now `SssdDomainsEntries` +- `simp config` validates LDAP DNs as whole DNs with case-insensitive + attribute types. `simp_options::ldap::base_dn` accepts any valid DN (e.g. + `DC=example,DC=com`, the form `simplib::ldap::domain_to_dn()` produces), + except that the SIMP LDAP server's base DN must still start with `dc=`. + The bind, sync and root DNs accept `CN=` as well as `cn=`. Malformed DNs, + such as an empty value, a `;` or missing comma between RDNs, or stray + whitespace or line breaks, are rejected * Fri May 20 2022 Trevor Vaughan - 7.0.1 - Always set the hostname to the one specified during `simp config` in the apply diff --git a/lib/simp/cli/config/items/data/simp_options_ldap_base_dn.rb b/lib/simp/cli/config/items/data/simp_options_ldap_base_dn.rb index 0a1d5b42..007f5210 100644 --- a/lib/simp/cli/config/items/data/simp_options_ldap_base_dn.rb +++ b/lib/simp/cli/config/items/data/simp_options_ldap_base_dn.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative '../item' +require_relative '../../utils' require_relative 'cli_is_simp_ldap_server' require_relative 'cli_network_hostname' @@ -27,12 +28,27 @@ def get_recommended_value end end + # simp_openldap creates the suffix entry as a `domain` object named by + # its first RDN, so when this host is the SIMP LDAP server, the base DN + # must start with `dc=`. def validate(x) - (x.to_s =~ %r{^dc=}) ? true : false + return false unless Simp::Cli::Config::Utils.validate_ldap_dn(x) + return true unless simp_ldap_server? + + x.match?(%r{\Adc=}i) end def not_valid_message - "Valid LDAP Base Distinguished Name must begin with 'dc='" + msg = "Invalid LDAP Base Distinguished Name. Use a DN such as 'dc=example,dc=com'" + msg += "; the SIMP LDAP server's base DN must start with 'dc='" if simp_ldap_server? + msg + end + + private + + def simp_ldap_server? + @config_items.key?('cli::is_simp_ldap_server') && + @config_items.fetch('cli::is_simp_ldap_server').value end end end diff --git a/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn.rb b/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn.rb index cc12979e..1906cd89 100644 --- a/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn.rb +++ b/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative '../item' +require_relative '../../utils' require_relative 'cli_is_simp_ldap_server' module Simp; end @@ -15,11 +16,11 @@ def initialize(puppet_env_info = DEFAULT_PUPPET_ENV_INFO) end def validate(x) - (x.to_s =~ %r{^cn=}) ? true : false + Simp::Cli::Config::Utils.validate_ldap_cn_dn(x) end def not_valid_message - "Valid LDAP Bind Distinguished Name must begin with 'cn='" + "Valid LDAP Bind Distinguished Name must be a DN that begins with a 'cn=' RDN" end def get_recommended_value diff --git a/lib/simp/cli/config/items/data/simp_options_ldap_root_dn.rb b/lib/simp/cli/config/items/data/simp_options_ldap_root_dn.rb index e1ea0ccc..58bd5789 100644 --- a/lib/simp/cli/config/items/data/simp_options_ldap_root_dn.rb +++ b/lib/simp/cli/config/items/data/simp_options_ldap_root_dn.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative '../item' +require_relative '../../utils' require_relative 'cli_is_simp_ldap_server' module Simp; end @@ -25,11 +26,11 @@ def get_recommended_value end def not_valid_message - "Valid LDAP Root Distinguished Name must begin with 'cn='" + "Valid LDAP Root Distinguished Name must be a DN that begins with a 'cn=' RDN" end def validate(x) - (x.to_s =~ %r{^cn=}) ? true : false + Simp::Cli::Config::Utils.validate_ldap_cn_dn(x) end end end diff --git a/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn.rb b/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn.rb index 70519164..bf7ee376 100644 --- a/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn.rb +++ b/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative '../item' +require_relative '../../utils' require_relative 'cli_is_simp_ldap_server' module Simp; end @@ -15,11 +16,11 @@ def initialize(puppet_env_info = DEFAULT_PUPPET_ENV_INFO) end def validate(x) - (x.to_s =~ %r{^cn=}) ? true : false + Simp::Cli::Config::Utils.validate_ldap_cn_dn(x) end def not_valid_message - "Valid LDAP Sync Distinguished Name must begin with 'cn='" + "Valid LDAP Sync Distinguished Name must be a DN that begins with a 'cn=' RDN" end def get_recommended_value diff --git a/lib/simp/cli/config/utils.rb b/lib/simp/cli/config/utils.rb index cf80ec00..5ba4e66d 100644 --- a/lib/simp/cli/config/utils.rb +++ b/lib/simp/cli/config/utils.rb @@ -14,6 +14,26 @@ class Simp::Cli::Config::Utils SHA512_CRYPT_UNSUPPORTED_MSG = "This system's crypt(3) does not support SHA-512 password hashes" + # LDAP Distinguished Name grammar (RFC 4514), used by validate_ldap_dn. + # + # * Attribute types are names (case-insensitive) or numeric OIDs. + # * A value is non-empty. `"`, `+`, `,`, `;`, `<`, `=`, `>` and `\` must be + # escaped with a backslash (or written as `\XX` hex pairs), as must a + # leading `#` or space and a trailing space. `=` isn't special in RFC 4514, + # but an unescaped one almost always means a missing comma. + # * Control characters (including CR, LF and tab) are never allowed. + # * Multi-valued RDNs are joined with `+`; spaces around the `,` between + # RDNs are accepted. + LDAP_DN_ATTR = %r{(?:[A-Za-z][A-Za-z0-9-]*|\d+(?:\.\d+)*)}.freeze + LDAP_DN_ESCAPE = %r{\\(?:[ "\#+,;<=>\\]|\h\h)}.freeze + LDAP_DN_PLAIN = %r{[^\p{Cntrl} "+,;<=>\\]}.freeze + LDAP_DN_VALUE = %r{(?:#{LDAP_DN_ESCAPE}|(?!\#)#{LDAP_DN_PLAIN})(?:(?:#{LDAP_DN_ESCAPE}|#{LDAP_DN_PLAIN}| )*(?:#{LDAP_DN_ESCAPE}|#{LDAP_DN_PLAIN}))?}.freeze + LDAP_DN_RDN = %r{#{LDAP_DN_ATTR}=#{LDAP_DN_VALUE}(?:\+#{LDAP_DN_ATTR}=#{LDAP_DN_VALUE})*}.freeze + LDAP_DN_REGEX = %r{\A#{LDAP_DN_RDN}(?: *, *#{LDAP_DN_RDN})*\z}.freeze + + # A Hiera interpolation such as %{hiera('simp_options::ldap::base_dn')} + HIERA_INTERPOLATION_REGEX = %r{%\{[^{}\p{Cntrl}]+\}}.freeze + class << self def validate_domain(domain) # From Simplib::Domain custom type from pupmod-simp-simplib @@ -41,6 +61,24 @@ def validate_fqdn(fqdn) ((fqdn =~ regex) ? true : false) end + # Validate an LDAP Distinguished Name against LDAP_DN_REGEX. + def validate_ldap_dn(dn) + dn.is_a?(String) && LDAP_DN_REGEX.match?(dn) + end + + # Validate an LDAP DN whose first RDN is `cn=...` (e.g. a bind DN). + # + # A whole RDN may be a Hiera interpolation, as in SIMP's recommended + # values (`cn=hostAuth,ou=Hosts,%{hiera('simp_options::ldap::base_dn')}`). + # Each interpolation is replaced with a placeholder RDN before the DN + # is validated, so it can't hide a malformed DN around it. + def validate_ldap_cn_dn(dn) + return false unless dn.is_a?(String) + + resolved = dn.gsub(HIERA_INTERPOLATION_REGEX, 'dc=interpolated') + resolved.match?(%r{\Acn=}i) && validate_ldap_dn(resolved) + end + def validate_ip(ip) return false unless ip.respond_to?(:=~) diff --git a/spec/lib/simp/cli/config/items/data/simp_options_ldap_base_dn_spec.rb b/spec/lib/simp/cli/config/items/data/simp_options_ldap_base_dn_spec.rb index 73a1cf4a..16fd7583 100644 --- a/spec/lib/simp/cli/config/items/data/simp_options_ldap_base_dn_spec.rb +++ b/spec/lib/simp/cli/config/items/data/simp_options_ldap_base_dn_spec.rb @@ -42,12 +42,32 @@ end describe '#validate' do + context 'when this host is the SIMP LDAP server' do + before :each do + item = Simp::Cli::Config::Item::CliIsSimpLdapServer.new + item.value = true + @ci.config_items[item.key] = item + end + + it 'requires a leading dc= RDN' do + expect(@ci.validate('DC=tasty,DC=bacon')).to be true + expect(@ci.validate('o=Bacon')).to be false + expect(@ci.validate('cn=hostAuth,ou=Hosts,dc=tasty,dc=bacon')).to be false + expect(@ci.not_valid_message).to include("must start with 'dc='") + end + end + it 'validates ldap_base_dns' do expect(@ci.validate('dc=tasty,dc=bacon')).to be true + expect(@ci.validate('DC=tasty,DC=bacon')).to be true + expect(@ci.validate('o=Bacon')).to be true + expect(@ci.validate('ou=Hosts,dc=tasty,dc=bacon')).to be true end it "doesn't validate bad ldap_base_dns" do - expect(@ci.validate('cn=hostAuth,ou=Hosts,dc=tasty,dc=bacon')).to be false + expect(@ci.validate('dc=')).to be false + expect(@ci.validate("x\ndc=tasty,dc=bacon")).to be false + expect(@ci.validate('tasty.bacon')).to be false expect(@ci.validate(nil)).to be false expect(@ci.validate(false)).to be false end diff --git a/spec/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn_spec.rb b/spec/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn_spec.rb index 55b4cd03..4a3ddede 100644 --- a/spec/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn_spec.rb +++ b/spec/lib/simp/cli/config/items/data/simp_options_ldap_bind_dn_spec.rb @@ -34,8 +34,19 @@ expect(@ci.validate('cn=hostAuth,ou=Hosts,dc=tasty,dc=bacon')).to be true end + it 'validates an uppercase CN' do + expect(@ci.validate('CN=hostAuth,OU=Hosts,DC=tasty,DC=bacon')).to be true + end + + it 'validates the hiera-interpolated recommended value' do + expect(@ci.validate("cn=hostAuth,ou=Hosts,%{hiera('simp_options::ldap::base_dn')}")).to be true + end + it "doesn't validate bad ldap_bind_dns" do expect(@ci.validate('dc=tasty,dc=bacon')).to be false + expect(@ci.validate('cn=')).to be false + expect(@ci.validate('cn=a,garbage')).to be false + expect(@ci.validate("x\ncn=a,dc=tasty")).to be false expect(@ci.validate(nil)).to be false expect(@ci.validate(false)).to be false end diff --git a/spec/lib/simp/cli/config/items/data/simp_options_ldap_root_dn_spec.rb b/spec/lib/simp/cli/config/items/data/simp_options_ldap_root_dn_spec.rb index e9e89c81..2495803f 100644 --- a/spec/lib/simp/cli/config/items/data/simp_options_ldap_root_dn_spec.rb +++ b/spec/lib/simp/cli/config/items/data/simp_options_ldap_root_dn_spec.rb @@ -34,8 +34,19 @@ expect(@ci.validate('cn=hostAuth,ou=Hosts,dc=tasty,dc=bacon')).to be true end + it 'validates an uppercase CN' do + expect(@ci.validate('CN=LDAPAdmin,OU=Hosts,DC=tasty,DC=bacon')).to be true + end + + it 'validates the hiera-interpolated recommended value' do + expect(@ci.validate("cn=LDAPAdmin,ou=People,%{hiera('simp_options::ldap::base_dn')}")).to be true + end + it "doesn't validate bad ldap_root_dns" do expect(@ci.validate('dc=tasty,dc=bacon')).to be false + expect(@ci.validate('cn=')).to be false + expect(@ci.validate('cn=a,garbage')).to be false + expect(@ci.validate("x\ncn=a,dc=tasty")).to be false expect(@ci.validate(nil)).to be false expect(@ci.validate(false)).to be false end diff --git a/spec/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn_spec.rb b/spec/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn_spec.rb index bf8c7d33..ea77f0e6 100644 --- a/spec/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn_spec.rb +++ b/spec/lib/simp/cli/config/items/data/simp_options_ldap_sync_dn_spec.rb @@ -34,7 +34,18 @@ expect(@ci.validate('cn=LDAPSync,ou=Hosts,dc=tasty,dc=bacon')).to be true end + it 'validates an uppercase CN' do + expect(@ci.validate('CN=LDAPSync,OU=Hosts,DC=tasty,DC=bacon')).to be true + end + + it 'validates the hiera-interpolated recommended value' do + expect(@ci.validate("cn=LDAPSync,ou=Hosts,%{hiera('simp_options::ldap::base_dn')}")).to be true + end + it "doesn't validate bad ldap_sync_dns" do + expect(@ci.validate('cn=')).to be false + expect(@ci.validate('cn=a,garbage')).to be false + expect(@ci.validate("x\ncn=a,dc=tasty")).to be false expect(@ci.validate(nil)).to be false expect(@ci.validate(false)).to be false end diff --git a/spec/lib/simp/cli/config/utils_spec.rb b/spec/lib/simp/cli/config/utils_spec.rb index 8ec51af1..e3fe5103 100644 --- a/spec/lib/simp/cli/config/utils_spec.rb +++ b/spec/lib/simp/cli/config/utils_spec.rb @@ -37,6 +37,74 @@ end end + describe '.validate_ldap_dn' do + it 'validates DNs regardless of attribute case or type' do + [ + 'dc=example,dc=com', + 'DC=example,DC=com', + 'o=Example', + 'ou=People, dc=example,dc=com', + 'cn=Doe\\, John,ou=People,dc=example,dc=com', + 'cn=a+uid=b,dc=example,dc=com', + '2.5.4.3=a,dc=example', + 'cn=Some Name,dc=example', + 'cn=\\#1,dc=example', + 'cn=\\3Cx\\3E,dc=example', + ].each do |dn| + expect(described_class.validate_ldap_dn(dn)).to be(true), dn + end + end + + it "doesn't validate malformed DNs" do + [ + 'dc=', + '=example', + 'dc=example,', + 'dc=example,,dc=com', + 'example.com', + "x\ndc=example", + "dc=example\n", + "dc=example\r", + 'dc=example,dc=com ', + 'dc= example', + 'dc=example;dc=com', + 'dc=example dc=com', + 'dc=#zz,dc=com', + 'cn=a