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)
40 ;; before you put some data in ~/.authinfo.gpg (the default place)
42 ;;; For url-auth authentication (HTTP/HTTPS), you need to use:
44 ;;; machine yourmachine.com:80 port http login testuser password testpass
46 ;;; This will match any realm and authentication method (basic or
47 ;;; digest). If you want finer controls, explore the url-auth source
48 ;;; code and variables.
50 ;;; (Tramp patch pending for this!)
51 ;;; For tramp authentication, use:
53 ;;; machine yourmachine.com port tramp login testuser password testpass
55 ;;; Note that the port can be scp or ssh, for example, to match only
56 ;;; those protocols. When you use port = tramp, you match any Tramp
63 (eval-when-compile (require 'cl
))
64 (eval-when-compile (require 'netrc
))
66 (defgroup auth-source nil
67 "Authentication sources."
68 :version
"23.1" ;; No Gnus
71 (defcustom auth-source-protocols
'((imap "imap" "imaps" "143" "993")
72 (pop3 "pop3" "pop" "pop3s" "110" "995")
76 "List of authentication protocols and their names"
79 :version
"23.1" ;; No Gnus
80 :type
'(repeat :tag
"Authentication Protocols"
81 (cons :tag
"Protocol Entry"
82 (symbol :tag
"Protocol")
84 (string :tag
"Name")))))
86 ;;; generate all the protocols in a format Customize can use
87 (defconst auth-source-protocols-customize
89 (let ((p (car-safe a
)))
91 :tag
(upcase (symbol-name p
))
93 auth-source-protocols
))
95 (defcustom auth-sources
'((:source
"~/.authinfo.gpg" :host t
:protocol t
))
96 "List of authentication sources.
98 Each entry is the authentication type with optional properties."
100 :version
"23.1" ;; No Gnus
101 :type
`(repeat :tag
"Authentication Sources"
102 (list :tag
"Source definition"
103 (const :format
"" :value
:source
)
104 (string :tag
"Authentication Source")
105 (const :format
"" :value
:host
)
106 (choice :tag
"Host (machine) choice"
108 (regexp :tag
"Host (machine) regular expression (TODO)")
109 (const :tag
"Fallback" nil
))
110 (const :format
"" :value
:protocol
)
111 (choice :tag
"Protocol"
113 (const :tag
"Fallback" nil
)
114 ,@auth-source-protocols-customize
))))
116 ;; temp for debugging
117 ;; (unintern 'auth-source-protocols)
118 ;; (unintern 'auth-sources)
119 ;; (customize-variable 'auth-sources)
120 ;; (setq auth-sources nil)
121 ;; (format "%S" auth-sources)
122 ;; (customize-variable 'auth-source-protocols)
123 ;; (setq auth-source-protocols nil)
124 ;; (format "%S" auth-source-protocols)
125 ;; (auth-source-pick "a" 'imap)
126 ;; (auth-source-user-or-password "login" "imap.myhost.com" 'imap)
127 ;; (auth-source-user-or-password "password" "imap.myhost.com" 'imap)
128 ;; (auth-source-user-or-password-imap "login" "imap.myhost.com")
129 ;; (auth-source-user-or-password-imap "password" "imap.myhost.com")
130 ;; (auth-source-protocol-defaults 'imap)
132 (defun auth-source-pick (host protocol
&optional fallback
)
133 "Parse `auth-sources' for HOST, and PROTOCOL matches.
135 Returns fallback choices (where PROTOCOL or HOST are nil) with FALLBACK t."
136 (interactive "sHost: \nsProtocol: \n") ;for testing
138 (dolist (choice auth-sources
)
139 (let ((h (plist-get choice
:host
))
140 (p (plist-get choice
:protocol
)))
143 (and (stringp h
) (string-match h host
))
144 (and fallback
(equal h nil
)))
146 (and (symbolp p
) (equal p protocol
))
147 (and fallback
(equal p nil
))))
148 (push choice choices
))))
152 (auth-source-pick host protocol t
)))))
154 (defun auth-source-user-or-password (mode host protocol
)
155 "Find user or password (from the string MODE) matching HOST and PROTOCOL."
157 "auth-source-user-or-password: get %s for %s (%s)"
160 (dolist (choice (auth-source-pick host protocol
))
161 (setq found
(netrc-machine-user-or-password
163 (plist-get choice
:source
)
165 (list (format "%s" protocol
))
166 (auth-source-protocol-defaults protocol
)))
169 "auth-source-user-or-password: found %s=%s for %s (%s)"
171 ;; don't show the password
172 (if (equal mode
"password") "SECRET" found
)
176 (defun auth-source-protocol-defaults (protocol)
177 "Return a list of default ports and names for PROTOCOL."
178 (cdr-safe (assoc protocol auth-source-protocols
)))
180 (defun auth-source-user-or-password-imap (mode host
)
181 (auth-source-user-or-password mode host
'imap
))
183 (defun auth-source-user-or-password-pop3 (mode host
)
184 (auth-source-user-or-password mode host
'pop3
))
186 (defun auth-source-user-or-password-ssh (mode host
)
187 (auth-source-user-or-password mode host
'ssh
))
189 (defun auth-source-user-or-password-sftp (mode host
)
190 (auth-source-user-or-password mode host
'sftp
))
192 (defun auth-source-user-or-password-smtp (mode host
)
193 (auth-source-user-or-password mode host
'smtp
))
195 (provide 'auth-source
)
197 ;; arch-tag: ff1afe78-06e9-42c2-b693-e9f922cbe4ab
198 ;;; auth-source.el ends here