Merge from emacs--devo--0
[gnus] / lisp / auth-source.el
1 ;;; auth-source.el --- authentication sources for Gnus and Emacs
2
3 ;; Copyright (C) 2008 Free Software Foundation, Inc.
4
5 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
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.
29
30 ;; Easy setup:
31 ;; (require 'auth-source)
32 ;; (customize-variable 'auth-sources) ;; optional
33
34 ;; now, whatever sources you've defined for password have to be available
35
36 ;; if you want encrypted sources, which is strongly recommended, do
37 ;; (require 'epa-file)
38 ;; (epa-file-mode)
39
40 ;; before you put some data in ~/.authinfo.gpg (the default place)
41
42 ;;; For url-auth authentication (HTTP/HTTPS), you need to use:
43
44 ;;; machine yourmachine.com:80 port http login testuser password testpass
45
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.
49
50 ;;; (Tramp patch pending for this!)
51 ;;; For tramp authentication, use:
52
53 ;;; machine yourmachine.com port tramp login testuser password testpass
54
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
57 ;;; protocol.
58
59 ;;; Code:
60
61 (require 'gnus-util)
62
63 (eval-when-compile (require 'cl))
64 (eval-when-compile (require 'netrc))
65
66 (defgroup auth-source nil
67   "Authentication sources."
68   :version "23.1" ;; No Gnus
69   :group 'gnus)
70
71 (defcustom auth-source-protocols '((imap "imap" "imaps" "143" "993")
72                                    (pop3 "pop3" "pop" "pop3s" "110" "995")
73                                    (ssh  "ssh" "22")
74                                    (sftp "sftp" "115")
75                                    (smtp "smtp" "25"))
76   "List of authentication protocols and their names"
77
78   :group 'auth-source
79   :version "23.1" ;; No Gnus
80   :type '(repeat :tag "Authentication Protocols"
81                  (cons :tag "Protocol Entry"
82                        (symbol :tag "Protocol")
83                        (repeat :tag "Names"
84                                (string :tag "Name")))))
85
86 ;;; generate all the protocols in a format Customize can use
87 (defconst auth-source-protocols-customize
88   (mapcar (lambda (a)
89             (let ((p (car-safe a)))
90               (list 'const 
91                     :tag (upcase (symbol-name p))
92                     p)))
93           auth-source-protocols))
94
95 (defcustom auth-sources '((:source "~/.authinfo.gpg" :host t :protocol t))
96   "List of authentication sources.
97
98 Each entry is the authentication type with optional properties."
99   :group 'auth-source
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"
107                                (const :tag "Any" t)
108                                (regexp :tag "Host (machine) regular expression (TODO)")
109                                (const :tag "Fallback" nil))
110                        (const :format "" :value :protocol)
111                        (choice :tag "Protocol"
112                                (const :tag "Any" t)
113                                (const :tag "Fallback" nil)
114                                ,@auth-source-protocols-customize))))
115
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)
131
132 (defun auth-source-pick (host protocol &optional fallback)
133   "Parse `auth-sources' for HOST, and PROTOCOL matches.
134
135 Returns fallback choices (where PROTOCOL or HOST are nil) with FALLBACK t."
136   (interactive "sHost: \nsProtocol: \n") ;for testing
137   (let (choices)
138     (dolist (choice auth-sources)
139       (let ((h (plist-get choice :host))
140             (p (plist-get choice :protocol)))
141         (when (and
142                (or (equal t h)
143                    (and (stringp h) (string-match h host))
144                    (and fallback (equal h nil)))
145                (or (equal t p)
146                    (and (symbolp p) (equal p protocol))
147                    (and fallback (equal p nil))))
148           (push choice choices))))
149     (if choices
150         choices
151       (unless fallback
152         (auth-source-pick host protocol t)))))
153
154 (defun auth-source-user-or-password (mode host protocol)
155   "Find user or password (from the string MODE) matching HOST and PROTOCOL."
156   (gnus-message 9 
157                 "auth-source-user-or-password: get %s for %s (%s)"
158                 mode host protocol)
159   (let (found)
160     (dolist (choice (auth-source-pick host protocol))
161       (setq found (netrc-machine-user-or-password 
162                    mode
163                    (plist-get choice :source)
164                    (list host)
165                    (list (format "%s" protocol))
166                    (auth-source-protocol-defaults protocol)))
167       (when found
168         (gnus-message 9 
169                       "auth-source-user-or-password: found %s=%s for %s (%s)"
170                       mode 
171                       ;; don't show the password
172                       (if (equal mode "password") "SECRET" found) 
173                       host protocol)
174         (return found)))))
175
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)))
179
180 (defun auth-source-user-or-password-imap (mode host)
181   (auth-source-user-or-password mode host 'imap))
182
183 (defun auth-source-user-or-password-pop3 (mode host)
184   (auth-source-user-or-password mode host 'pop3))
185
186 (defun auth-source-user-or-password-ssh (mode host)
187   (auth-source-user-or-password mode host 'ssh))
188
189 (defun auth-source-user-or-password-sftp (mode host)
190   (auth-source-user-or-password mode host 'sftp))
191
192 (defun auth-source-user-or-password-smtp (mode host)
193   (auth-source-user-or-password mode host 'smtp))
194
195 (provide 'auth-source)
196
197 ;; arch-tag: ff1afe78-06e9-42c2-b693-e9f922cbe4ab
198 ;;; auth-source.el ends here