* auth-source.el: Add more docs.
[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 ;; (setq epa-file-cache-passphrase-for-symmetric-encryption t) ; VERY important
40
41 ;; before you put some data in ~/.authinfo.gpg (the default place)
42
43 ;;; For url-auth authentication (HTTP/HTTPS), you need to use:
44
45 ;;; machine yourmachine.com:80 port http login testuser password testpass
46
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.
50
51 ;;; (Tramp patch pending for this!)
52 ;;; For tramp authentication, use:
53
54 ;;; machine yourmachine.com port tramp login testuser password testpass
55
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
58 ;;; protocol.
59
60 ;;; Code:
61
62 (require 'gnus-util)
63
64 (eval-when-compile (require 'cl))
65 (eval-when-compile (require 'netrc))
66
67 (defgroup auth-source nil
68   "Authentication sources."
69   :version "23.1" ;; No Gnus
70   :group 'gnus)
71
72 (defcustom auth-source-protocols '((imap "imap" "imaps" "143" "993")
73                                    (pop3 "pop3" "pop" "pop3s" "110" "995")
74                                    (ssh  "ssh" "22")
75                                    (sftp "sftp" "115")
76                                    (smtp "smtp" "25"))
77   "List of authentication protocols and their names"
78
79   :group 'auth-source
80   :version "23.1" ;; No Gnus
81   :type '(repeat :tag "Authentication Protocols"
82                  (cons :tag "Protocol Entry"
83                        (symbol :tag "Protocol")
84                        (repeat :tag "Names"
85                                (string :tag "Name")))))
86
87 ;;; generate all the protocols in a format Customize can use
88 (defconst auth-source-protocols-customize
89   (mapcar (lambda (a)
90             (let ((p (car-safe a)))
91               (list 'const 
92                     :tag (upcase (symbol-name p))
93                     p)))
94           auth-source-protocols))
95
96 (defcustom auth-sources '((:source "~/.authinfo.gpg" :host t :protocol t))
97   "List of authentication sources.
98
99 Each entry is the authentication type with optional properties."
100   :group 'auth-source
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"
108                                (const :tag "Any" t)
109                                (regexp :tag "Host (machine) regular expression (TODO)")
110                                (const :tag "Fallback" nil))
111                        (const :format "" :value :protocol)
112                        (choice :tag "Protocol"
113                                (const :tag "Any" t)
114                                (const :tag "Fallback" nil)
115                                ,@auth-source-protocols-customize))))
116
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)
132
133 (defun auth-source-pick (host protocol &optional fallback)
134   "Parse `auth-sources' for HOST, and PROTOCOL matches.
135
136 Returns fallback choices (where PROTOCOL or HOST are nil) with FALLBACK t."
137   (interactive "sHost: \nsProtocol: \n") ;for testing
138   (let (choices)
139     (dolist (choice auth-sources)
140       (let ((h (plist-get choice :host))
141             (p (plist-get choice :protocol)))
142         (when (and
143                (or (equal t h)
144                    (and (stringp h) (string-match h host))
145                    (and fallback (equal h nil)))
146                (or (equal t p)
147                    (and (symbolp p) (equal p protocol))
148                    (and fallback (equal p nil))))
149           (push choice choices))))
150     (if choices
151         choices
152       (unless fallback
153         (auth-source-pick host protocol t)))))
154
155 (defun auth-source-user-or-password (mode host protocol)
156   "Find user or password (from the string MODE) matching HOST and PROTOCOL."
157   (gnus-message 9 
158                 "auth-source-user-or-password: get %s for %s (%s)"
159                 mode host protocol)
160   (let (found)
161     (dolist (choice (auth-source-pick host protocol))
162       (setq found (netrc-machine-user-or-password 
163                    mode
164                    (plist-get choice :source)
165                    (list host)
166                    (list (format "%s" protocol))
167                    (auth-source-protocol-defaults protocol)))
168       (when found
169         (gnus-message 9 
170                       "auth-source-user-or-password: found %s=%s for %s (%s)"
171                       mode 
172                       ;; don't show the password
173                       (if (equal mode "password") "SECRET" found) 
174                       host protocol)
175         (return found)))))
176
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)))
180
181 (defun auth-source-user-or-password-imap (mode host)
182   (auth-source-user-or-password mode host 'imap))
183
184 (defun auth-source-user-or-password-pop3 (mode host)
185   (auth-source-user-or-password mode host 'pop3))
186
187 (defun auth-source-user-or-password-ssh (mode host)
188   (auth-source-user-or-password mode host 'ssh))
189
190 (defun auth-source-user-or-password-sftp (mode host)
191   (auth-source-user-or-password mode host 'sftp))
192
193 (defun auth-source-user-or-password-smtp (mode host)
194   (auth-source-user-or-password mode host 'smtp))
195
196 (provide 'auth-source)
197
198 ;; arch-tag: ff1afe78-06e9-42c2-b693-e9f922cbe4ab
199 ;;; auth-source.el ends here