epg-config.el 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ;;; epg-config.el --- configuration of the EasyPG Library
  2. ;; Copyright (C) 2006-2012 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 (or (executable-find "gpg")
  31. (executable-find "gpg2")
  32. "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-gpg-home-directory nil
  41. "The directory which contains the configuration files of `epg-gpg-program'."
  42. :group 'epg
  43. :type '(choice (const :tag "Default" nil) directory))
  44. (defcustom epg-passphrase-coding-system nil
  45. "Coding system to use with messages from `epg-gpg-program'."
  46. :group 'epg
  47. :type 'symbol)
  48. (defcustom epg-debug nil
  49. "If non-nil, debug output goes to the \" *epg-debug*\" buffer.
  50. Note that the buffer name starts with a space."
  51. :group 'epg
  52. :type 'boolean)
  53. (defconst epg-gpg-minimum-version "1.4.3")
  54. ;;;###autoload
  55. (defun epg-configuration ()
  56. "Return a list of internal configuration parameters of `epg-gpg-program'."
  57. (let (config groups type args)
  58. (with-temp-buffer
  59. (apply #'call-process epg-gpg-program nil (list t nil) nil
  60. (append (if epg-gpg-home-directory
  61. (list "--homedir" epg-gpg-home-directory))
  62. '("--with-colons" "--list-config")))
  63. (goto-char (point-min))
  64. (while (re-search-forward "^cfg:\\([^:]+\\):\\(.*\\)" nil t)
  65. (setq type (intern (match-string 1))
  66. args (match-string 2))
  67. (cond
  68. ((eq type 'group)
  69. (if (string-match "\\`\\([^:]+\\):" args)
  70. (setq groups
  71. (cons (cons (downcase (match-string 1 args))
  72. (delete "" (split-string
  73. (substring args
  74. (match-end 0))
  75. ";")))
  76. groups))
  77. (if epg-debug
  78. (message "Invalid group configuration: %S" args))))
  79. ((memq type '(pubkey cipher digest compress))
  80. (if (string-match "\\`\\([0-9]+\\)\\(;[0-9]+\\)*" args)
  81. (setq config
  82. (cons (cons type
  83. (mapcar #'string-to-number
  84. (delete "" (split-string args ";"))))
  85. config))
  86. (if epg-debug
  87. (message "Invalid %S algorithm configuration: %S"
  88. type args))))
  89. (t
  90. (setq config (cons (cons type args) config))))))
  91. (if groups
  92. (cons (cons 'groups groups) config)
  93. config)))
  94. (defun epg-config--parse-version (string)
  95. (let ((index 0)
  96. version)
  97. (while (eq index (string-match "\\([0-9]+\\)\\.?" string index))
  98. (setq version (cons (string-to-number (match-string 1 string))
  99. version)
  100. index (match-end 0)))
  101. (nreverse version)))
  102. (defun epg-config--compare-version (v1 v2)
  103. (while (and v1 v2 (= (car v1) (car v2)))
  104. (setq v1 (cdr v1) v2 (cdr v2)))
  105. (- (or (car v1) 0) (or (car v2) 0)))
  106. ;;;###autoload
  107. (defun epg-check-configuration (config &optional minimum-version)
  108. "Verify that a sufficient version of GnuPG is installed."
  109. (let ((entry (assq 'version config))
  110. version)
  111. (unless (and entry
  112. (stringp (cdr entry)))
  113. (error "Undetermined version: %S" entry))
  114. (setq version (epg-config--parse-version (cdr entry))
  115. minimum-version (epg-config--parse-version
  116. (or minimum-version
  117. epg-gpg-minimum-version)))
  118. (unless (>= (epg-config--compare-version version minimum-version) 0)
  119. (error "Unsupported version: %s" (cdr entry)))))
  120. ;;;###autoload
  121. (defun epg-expand-group (config group)
  122. "Look at CONFIG and try to expand GROUP."
  123. (let ((entry (assq 'groups config)))
  124. (if (and entry
  125. (setq entry (assoc (downcase group) (cdr entry))))
  126. (cdr entry))))
  127. (provide 'epg-config)
  128. ;;; epg-config.el ends here