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, or (at your option)
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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; This is the auth-source.el package. It lets users tell Gnus how to
28 ;; authenticate in a single place. Simplicity is the goal. Instead
29 ;; of providing 5000 options, we'll stick to simple, easy to
30 ;; understand options.
33 ;; (require 'auth-source)
34 ;; (customize-variable 'auth-sources) ;; optional
36 ;; now, whatever sources you've defined for password have to be available
38 ;; if you want encrypted sources, which is strongly recommended, do
39 ;; (require 'epa-file)
42 ;; before you put some data in ~/.authinfo.gpg (the default place)
44 ;;; For url-auth authentication (HTTP/HTTPS), you need to use:
46 ;;; machine yourmachine.com:80 port http login testuser password testpass
48 ;;; This will match any realm and authentication method (basic or
49 ;;; digest). If you want finer controls, explore the url-auth source
50 ;;; code and variables.
52 ;;; (Tramp patch pending for this!)
53 ;;; For tramp authentication, use:
55 ;;; machine yourmachine.com port tramp login testuser password testpass
57 ;;; Note that the port can be scp or ssh, for example, to match only
58 ;;; those protocols. When you use port = tramp, you match any Tramp
65 (eval-when-compile (require 'cl))
66 (eval-when-compile (require 'netrc))
68 (defgroup auth-source nil
69 "Authentication sources."
70 :version "23.1" ;; No Gnus
73 (defcustom auth-source-protocols '((imap "imap" "imaps" "143" "993")
74 (pop3 "pop3" "pop" "pop3s" "110" "995")
78 "List of authentication protocols and their names"
81 :version "23.1" ;; No Gnus
82 :type '(repeat :tag "Authentication Protocols"
83 (cons :tag "Protocol Entry"
84 (symbol :tag "Protocol")
86 (string :tag "Name")))))
88 ;;; generate all the protocols in a format Customize can use
89 (defconst auth-source-protocols-customize
91 (let ((p (car-safe a)))
93 :tag (upcase (symbol-name p))
95 auth-source-protocols))
97 (defcustom auth-sources '((:source "~/.authinfo.gpg" :host t :protocol t))
98 "List of authentication sources.
100 Each entry is the authentication type with optional properties."
102 :version "23.1" ;; No Gnus
103 :type `(repeat :tag "Authentication Sources"
104 (list :tag "Source definition"
105 (const :format "" :value :source)
106 (string :tag "Authentication Source")
107 (const :format "" :value :host)
108 (choice :tag "Host (machine) choice"
110 (regexp :tag "Host (machine) regular expression (TODO)")
111 (const :tag "Fallback" nil))
112 (const :format "" :value :protocol)
113 (choice :tag "Protocol"
115 (const :tag "Fallback" nil)
116 ,@auth-source-protocols-customize))))
118 ;; temp for debugging
119 ;; (unintern 'auth-source-protocols)
120 ;; (unintern 'auth-sources)
121 ;; (customize-variable 'auth-sources)
122 ;; (setq auth-sources nil)
123 ;; (format "%S" auth-sources)
124 ;; (customize-variable 'auth-source-protocols)
125 ;; (setq auth-source-protocols nil)
126 ;; (format "%S" auth-source-protocols)
127 ;; (auth-source-pick "a" 'imap)
128 ;; (auth-source-user-or-password "login" "imap.myhost.com" 'imap)
129 ;; (auth-source-user-or-password "password" "imap.myhost.com" 'imap)
130 ;; (auth-source-user-or-password-imap "login" "imap.myhost.com")
131 ;; (auth-source-user-or-password-imap "password" "imap.myhost.com")
132 ;; (auth-source-protocol-defaults 'imap)
134 (defun auth-source-pick (host protocol &optional fallback)
135 "Parse `auth-sources' for HOST, and PROTOCOL matches.
137 Returns fallback choices (where PROTOCOL or HOST are nil) with FALLBACK t."
138 (interactive "sHost: \nsProtocol: \n") ;for testing
140 (dolist (choice auth-sources)
141 (let ((h (plist-get choice :host))
142 (p (plist-get choice :protocol)))
145 (and (stringp h) (string-match h host))
146 (and fallback (equal h nil)))
148 (and (symbolp p) (equal p protocol))
149 (and fallback (equal p nil))))
150 (push choice choices))))
154 (auth-source-pick host protocol t)))))
156 (defun auth-source-user-or-password (mode host protocol)
157 "Find user or password (from the string MODE) matching HOST and PROTOCOL."
159 "auth-source-user-or-password: get %s for %s (%s)"
162 (dolist (choice (auth-source-pick host protocol))
163 (setq found (netrc-machine-user-or-password
165 (plist-get choice :source)
167 (list (format "%s" protocol))
168 (auth-source-protocol-defaults protocol)))
171 "auth-source-user-or-password: found %s=%s for %s (%s)"
173 ;; don't show the password
174 (if (equal mode "password") "SECRET" found)
178 (defun auth-source-protocol-defaults (protocol)
179 "Return a list of default ports and names for PROTOCOL."
180 (cdr-safe (assoc protocol auth-source-protocols)))
182 (defun auth-source-user-or-password-imap (mode host)
183 (auth-source-user-or-password mode host 'imap))
185 (defun auth-source-user-or-password-pop3 (mode host)
186 (auth-source-user-or-password mode host 'pop3))
188 (defun auth-source-user-or-password-ssh (mode host)
189 (auth-source-user-or-password mode host 'ssh))
191 (defun auth-source-user-or-password-sftp (mode host)
192 (auth-source-user-or-password mode host 'sftp))
194 (defun auth-source-user-or-password-smtp (mode host)
195 (auth-source-user-or-password mode host 'smtp))
197 (provide 'auth-source)
199 ;; arch-tag: ff1afe78-06e9-42c2-b693-e9f922cbe4ab
200 ;;; auth-source.el ends here