1 ;;; auth-source.el --- authentication sources for Gnus and Emacs
3 ;; Copyright (C) 2008 Free Software Foundation, Inc.
5 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This is the auth-source.el package. It lets users tell Gnus how to
26 ;; authenticate in a single place. Simplicity is the goal. Instead
27 ;; of providing 5000 options, we'll stick to simple, easy to
28 ;; understand options.
31 ;; (require 'auth-source)
32 ;; (customize-variable 'auth-sources) ;; optional
34 ;; now, whatever sources you've defined for password have to be available
36 ;; if you want encrypted sources, which is strongly recommended, do
37 ;; (require 'epa-file)
39 ;; (setq epa-file-cache-passphrase-for-symmetric-encryption t) ; VERY important
41 ;; before you put some data in ~/.authinfo.gpg (the default place)
43 ;;; For url-auth authentication (HTTP/HTTPS), you need to use:
45 ;;; machine yourmachine.com:80 port http login testuser password testpass
47 ;;; This will match any realm and authentication method (basic or
48 ;;; digest). If you want finer controls, explore the url-auth source
49 ;;; code and variables.
51 ;;; (Tramp patch pending for this!)
52 ;;; For tramp authentication, use:
54 ;;; machine yourmachine.com port tramp login testuser password testpass
56 ;;; Note that the port can be scp or ssh, for example, to match only
57 ;;; those protocols. When you use port = tramp, you match any Tramp
64 (eval-when-compile (require 'cl))
65 (eval-when-compile (require 'netrc))
67 (defgroup auth-source nil
68 "Authentication sources."
69 :version "23.1" ;; No Gnus
72 (defcustom auth-source-protocols '((imap "imap" "imaps" "143" "993")
73 (pop3 "pop3" "pop" "pop3s" "110" "995")
77 "List of authentication protocols and their names"
80 :version "23.1" ;; No Gnus
81 :type '(repeat :tag "Authentication Protocols"
82 (cons :tag "Protocol Entry"
83 (symbol :tag "Protocol")
85 (string :tag "Name")))))
87 ;;; generate all the protocols in a format Customize can use
88 (defconst auth-source-protocols-customize
90 (let ((p (car-safe a)))
92 :tag (upcase (symbol-name p))
94 auth-source-protocols))
96 (defcustom auth-sources '((:source "~/.authinfo.gpg" :host t :protocol t))
97 "List of authentication sources.
99 Each entry is the authentication type with optional properties."
101 :version "23.1" ;; No Gnus
102 :type `(repeat :tag "Authentication Sources"
103 (list :tag "Source definition"
104 (const :format "" :value :source)
105 (string :tag "Authentication Source")
106 (const :format "" :value :host)
107 (choice :tag "Host (machine) choice"
109 (regexp :tag "Host (machine) regular expression (TODO)")
110 (const :tag "Fallback" nil))
111 (const :format "" :value :protocol)
112 (choice :tag "Protocol"
114 (const :tag "Fallback" nil)
115 ,@auth-source-protocols-customize))))
117 ;; temp for debugging
118 ;; (unintern 'auth-source-protocols)
119 ;; (unintern 'auth-sources)
120 ;; (customize-variable 'auth-sources)
121 ;; (setq auth-sources nil)
122 ;; (format "%S" auth-sources)
123 ;; (customize-variable 'auth-source-protocols)
124 ;; (setq auth-source-protocols nil)
125 ;; (format "%S" auth-source-protocols)
126 ;; (auth-source-pick "a" 'imap)
127 ;; (auth-source-user-or-password "login" "imap.myhost.com" 'imap)
128 ;; (auth-source-user-or-password "password" "imap.myhost.com" 'imap)
129 ;; (auth-source-user-or-password-imap "login" "imap.myhost.com")
130 ;; (auth-source-user-or-password-imap "password" "imap.myhost.com")
131 ;; (auth-source-protocol-defaults 'imap)
133 (defun auth-source-pick (host protocol &optional fallback)
134 "Parse `auth-sources' for HOST, and PROTOCOL matches.
136 Returns fallback choices (where PROTOCOL or HOST are nil) with FALLBACK t."
137 (interactive "sHost: \nsProtocol: \n") ;for testing
139 (dolist (choice auth-sources)
140 (let ((h (plist-get choice :host))
141 (p (plist-get choice :protocol)))
144 (and (stringp h) (string-match h host))
145 (and fallback (equal h nil)))
147 (and (symbolp p) (equal p protocol))
148 (and fallback (equal p nil))))
149 (push choice choices))))
153 (auth-source-pick host protocol t)))))
155 (defun auth-source-user-or-password (mode host protocol)
156 "Find user or password (from the string MODE) matching HOST and PROTOCOL."
158 "auth-source-user-or-password: get %s for %s (%s)"
161 (dolist (choice (auth-source-pick host protocol))
162 (setq found (netrc-machine-user-or-password
164 (plist-get choice :source)
166 (list (format "%s" protocol))
167 (auth-source-protocol-defaults protocol)))
170 "auth-source-user-or-password: found %s=%s for %s (%s)"
172 ;; don't show the password
173 (if (equal mode "password") "SECRET" found)
177 (defun auth-source-protocol-defaults (protocol)
178 "Return a list of default ports and names for PROTOCOL."
179 (cdr-safe (assoc protocol auth-source-protocols)))
181 (defun auth-source-user-or-password-imap (mode host)
182 (auth-source-user-or-password mode host 'imap))
184 (defun auth-source-user-or-password-pop3 (mode host)
185 (auth-source-user-or-password mode host 'pop3))
187 (defun auth-source-user-or-password-ssh (mode host)
188 (auth-source-user-or-password mode host 'ssh))
190 (defun auth-source-user-or-password-sftp (mode host)
191 (auth-source-user-or-password mode host 'sftp))
193 (defun auth-source-user-or-password-smtp (mode host)
194 (auth-source-user-or-password mode host 'smtp))
196 (provide 'auth-source)
198 ;; arch-tag: ff1afe78-06e9-42c2-b693-e9f922cbe4ab
199 ;;; auth-source.el ends here