Class: PostfixAdmin::Doveadm

Inherits:
Object
  • Object
show all
Defined in:
lib/postfix_admin/doveadm.rb

Constant Summary collapse

CMD_DOVEADM_PW =
"doveadm pw"

Class Method Summary collapse

Class Method Details

.password(password, scheme, rounds: nil, user_name: nil, prefix: true) ⇒ Object

Generate a password hash using ‘doveadm pw` command



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/postfix_admin/doveadm.rb', line 16

def self.password(password, scheme, rounds: nil, user_name: nil,
                  prefix: true)
  escaped_password = Shellwords.escape(password)
  escaped_scheme   = Shellwords.escape(scheme)

  cmd = "#{CMD_DOVEADM_PW} -s #{escaped_scheme} -p #{escaped_password}"

  # DIGEST-MD5 requires -u option (user name)
  if scheme == "DIGEST-MD5"
    escaped_user_name = Shellwords.escape(user_name)
    cmd << " -u #{escaped_user_name}"
  end

  if rounds
    escaped_rounds   = Shellwords.escape(rounds.to_s)
    cmd << " -r #{rounds}"
  end

  output, error, status = Open3.capture3(cmd)

  if status.success?
    res = output.chomp
    if prefix
      res
    else
      # Remove the prefix
      res.gsub("{#{escaped_scheme}}", "")
    end
  else
    raise Error, "#{CMD_DOVEADM_PW}: #{error}"
  end
end

.schemesObject

List all supported password schemes



10
11
12
13
# File 'lib/postfix_admin/doveadm.rb', line 10

def self.schemes
  result = `#{CMD_DOVEADM_PW} -l`
  result.split
end