epg-config.el 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. ;;; epg-config.el --- configuration of the EasyPG Library
  2. ;; Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. ;; Author: Daiki Ueno <ueno@unixuser.org>
  4. ;; Keywords: PGP, GnuPG
  5. ;; Package: epg
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Code:
  18. (defconst epg-package-name "epg"
  19. "Name of this package.")
  20. (defconst epg-version-number "1.0.0"
  21. "Version number of this package.")
  22. (defconst epg-bug-report-address "ueno@unixuser.org"
  23. "Report bugs to this address.")
  24. (defgroup epg ()
  25. "Interface to the GNU Privacy Guard (GnuPG)."
  26. :tag "EasyPG"
  27. :version "23.1"
  28. :group 'data
  29. :group 'external)
  30. (defcustom epg-gpg-program (cond ((executable-find "gpg") "gpg")
  31. ((executable-find "gpg2") "gpg2")
  32. (t "gpg"))
  33. "The `gpg' executable."
  34. :group 'epg
  35. :type 'string)
  36. (defcustom epg-gpgsm-program "gpgsm"
  37. "The `gpgsm' executable."
  38. :group 'epg
  39. :type 'string)
  40. (defcustom epg-gpgconf-program "gpgconf"
  41. "The `gpgconf' executable."
  42. :group 'epg
  43. :type 'string)
  44. (defcustom epg-gpg-home-directory nil
  45. "The directory which contains the configuration files of `epg-gpg-program'."
  46. :group 'epg
  47. :type '(choice (const :tag "Default" nil) directory))
  48. (defcustom epg-passphrase-coding-system nil
  49. "Coding system to use with messages from `epg-gpg-program'."
  50. :group 'epg
  51. :type 'symbol)
  52. (defcustom epg-debug nil
  53. "If non-nil, debug output goes to the \" *epg-debug*\" buffer.
  54. Note that the buffer name starts with a space."
  55. :group 'epg
  56. :type 'boolean)
  57. (defconst epg-gpg-minimum-version "1.4.3")
  58. ;;;###autoload
  59. (defun epg-configuration ()
  60. "Return a list of internal configuration parameters of `epg-gpg-program'."
  61. (let (config groups type args)
  62. (with-temp-buffer
  63. (apply #'call-process epg-gpg-program nil (list t nil) nil
  64. (append (if epg-gpg-home-directory
  65. (list "--homedir" epg-gpg-home-directory))
  66. '("--with-colons" "--list-config")))
  67. (goto-char (point-min))
  68. (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
  69. (setq type (intern (match-string 1))
  70. args (match-string 2))
  71. (cond
  72. ((eq type 'group)
  73. (if (string-match "\\`\\([^:]+\\):" args)
  74. (setq groups
  75. (cons (cons (downcase (match-string 1 args))
  76. (delete "" (split-string
  77. (substring args
  78. (match-end 0))
  79. ";")))
  80. groups))
  81. (if epg-debug
  82. (message "Invalid group configuration: %S" args))))
  83. ((memq type '(pubkey cipher digest compress))
  84. (if (string-match "\\`\\([0-9]+\\)\\(;[0-9]+\\)*" args)
  85. (setq config
  86. (cons (cons type
  87. (mapcar #'string-to-number
  88. (delete "" (split-string args ";"))))
  89. config))
  90. (if epg-debug
  91. (message "Invalid %S algorithm configuration: %S"
  92. type args))))
  93. (t
  94. (setq config (cons (cons type args) config))))))
  95. (if groups
  96. (cons (cons 'groups groups) config)
  97. config)))
  98. (defun epg-config--parse-version (string)
  99. (let ((index 0)
  100. version)
  101. (while (eq index (string-match "\\([0-9]+\\)\\.?" string index))
  102. (setq version (cons (string-to-number (match-string 1 string))
  103. version)
  104. index (match-end 0)))
  105. (nreverse version)))
  106. (defun epg-config--compare-version (v1 v2)
  107. (while (and v1 v2 (= (car v1) (car v2)))
  108. (setq v1 (cdr v1) v2 (cdr v2)))
  109. (- (or (car v1) 0) (or (car v2) 0)))
  110. ;;;###autoload
  111. (defun epg-check-configuration (config &optional minimum-version)
  112. "Verify that a sufficient version of GnuPG is installed."
  113. (let ((entry (assq 'version config))
  114. version)
  115. (unless (and entry
  116. (stringp (cdr entry)))
  117. (error "Undetermined version: %S" entry))
  118. (setq version (epg-config--parse-version (cdr entry))
  119. minimum-version (epg-config--parse-version
  120. (or minimum-version
  121. epg-gpg-minimum-version)))
  122. (unless (>= (epg-config--compare-version version minimum-version) 0)
  123. (error "Unsupported version: %s" (cdr entry)))))
  124. ;;;###autoload
  125. (defun epg-expand-group (config group)
  126. "Look at CONFIG and try to expand GROUP."
  127. (let ((entry (assq 'groups config)))
  128. (if (and entry
  129. (setq entry (assoc (downcase group) (cdr entry))))
  130. (cdr entry))))
  131. (provide 'epg-config)
  132. ;;; epg-config.el ends here