2 # Please note: This function is an implementation of a Ruby class and as such may not be entirely UTF8 compatible. To ensure compatibility please use this function with Ruby 2.4.0 or greater - https://bugs.ruby-lang.org/issues/10085.
4 Puppet::Parser::Functions::newfunction(
8 :doc => "Hashes a password using the crypt function. Provides a hash
9 usable on most POSIX systems.
11 The first argument to this function is the password to hash. If it is
12 undef or an empty string, this function returns undef.
14 The second argument to this function is which type of hash to use. It
15 will be converted into the appropriate crypt(3) hash specifier. Valid
18 |Hash type |Specifier|
19 |---------------------|---------|
22 |SHA-512 (recommended)|6 |
24 The third argument to this function is the salt to use.
26 Note: this uses the Puppet Master's implementation of crypt(3). If your
27 environment contains several different operating systems, ensure that they
28 are compatible before using this function.") do |args|
29 raise ArgumentError, "pw_hash(): wrong number of arguments (#{args.size} for 3)" if args.size != 3
31 if arg.is_a? Puppet::Pops::Types::PSensitiveType::Sensitive
37 raise ArgumentError, "pw_hash(): first argument must be a string" unless args[0].is_a? String or args[0].nil?
38 raise ArgumentError, "pw_hash(): second argument must be a string" unless args[1].is_a? String
39 hashes = { 'md5' => '1',
42 hash_type = hashes[args[1].downcase]
43 raise ArgumentError, "pw_hash(): #{args[1]} is not a valid hash type" if hash_type.nil?
44 raise ArgumentError, "pw_hash(): third argument must be a string" unless args[2].is_a? String
45 raise ArgumentError, "pw_hash(): third argument must not be empty" if args[2].empty?
46 raise ArgumentError, "pw_hash(): characters in salt must be in the set [a-zA-Z0-9./]" unless args[2].match(/\A[a-zA-Z0-9.\/]+\z/)
49 return nil if password.nil? or password.empty?
51 salt = "$#{hash_type}$#{args[2]}"
53 # handle weak implementations of String#crypt
54 if 'test'.crypt('$1$1') != '$1$1$Bp8CU9Oujr9SSEw53WV6G.'
56 if RUBY_PLATFORM == 'java'
57 # puppetserver bundles Apache Commons Codec
58 org.apache.commons.codec.digest.Crypt.crypt(password.to_java_bytes, salt)
60 # MS Windows and other systems that don't support enhanced salts
61 raise Puppet::ParseError, 'system does not support enhanced salts'