agent.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. module SecureMailing::PGP::Tool::Exec::Agent
  3. extend ActiveSupport::Concern
  4. included do
  5. def kill_agent
  6. socket = agent_socket
  7. gpgconf(%w[--kill gpg-agent])
  8. # Wait for the gpg-agent to shut down and remove its socket file.
  9. time_slept = 0
  10. while File.exist?(socket)
  11. raise __("The 'gpg-agent' process could not be stopped.") if (time_slept += 0.1) > 10
  12. sleep 0.1
  13. end
  14. end
  15. private
  16. def agent_socket
  17. gpgconf(%w[--list-dir agent-socket]).strip
  18. end
  19. def gpgconf(cmdline)
  20. raise __("Use 'with_private_keyring' to create a private keyring or set @gnupg_home before calling gpg.") if !@gnupg_home
  21. bin = "#{File.dirname(binary_path)}/gpgconf"
  22. cmd = [bin] + cmdline
  23. env = { 'GNUPGHOME' => @gnupg_home }
  24. stdout, stderr, status = Open3.capture3(env, *cmd, binmode: true)
  25. Rails.logger.error { "PGP: #{cmd}: #{stderr}" } if !status.success?
  26. stdout
  27. end
  28. end
  29. end