Mercurial > emacs
annotate lisp/gnus/auth-source.el @ 99187:f552fe2942ff
*** empty log message ***
author | Martin Rudalics <rudalics@gmx.at> |
---|---|
date | Tue, 28 Oct 2008 17:36:34 +0000 |
parents | 20496e1c594a |
children | d0dc678bbb96 |
rev | line source |
---|---|
92694 | 1 ;;; auth-source.el --- authentication sources for Gnus and Emacs |
2 | |
93386 | 3 ;; Copyright (C) 2008 Free Software Foundation, Inc. |
92694 | 4 |
5 ;; Author: Ted Zlatanov <tzz@lifelogs.com> | |
6 ;; Keywords: news | |
7 | |
8 ;; This file is part of GNU Emacs. | |
9 | |
94662
f42ef85caf91
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94369
diff
changeset
|
10 ;; GNU Emacs is free software: you can redistribute it and/or modify |
92694 | 11 ;; it under the terms of the GNU General Public License as published by |
94662
f42ef85caf91
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94369
diff
changeset
|
12 ;; the Free Software Foundation, either version 3 of the License, or |
f42ef85caf91
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94369
diff
changeset
|
13 ;; (at your option) any later version. |
92694 | 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 | |
94662
f42ef85caf91
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94369
diff
changeset
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
92694 | 18 ;; GNU General Public License for more details. |
19 | |
20 ;; You should have received a copy of the GNU General Public License | |
94662
f42ef85caf91
Switch to recommended form of GPLv3 permissions notice.
Glenn Morris <rgm@gnu.org>
parents:
94369
diff
changeset
|
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
92694 | 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. | |
94209 | 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) | |
95193 | 39 ;; (setq epa-file-cache-passphrase-for-symmetric-encryption t) ; VERY important |
94209 | 40 |
41 ;; before you put some data in ~/.authinfo.gpg (the default place) | |
42 | |
94837 | 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 | |
94980 | 51 ;;; For tramp authentication, use: |
52 | |
95665
20496e1c594a
* auth-source.el: Precise Tramp doc.
Michael Albinus <michael.albinus@gmx.de>
parents:
95193
diff
changeset
|
53 ;;; machine yourmachine.com port scp login testuser password testpass |
94980 | 54 |
95665
20496e1c594a
* auth-source.el: Precise Tramp doc.
Michael Albinus <michael.albinus@gmx.de>
parents:
95193
diff
changeset
|
55 ;;; Note that the port denotes the Tramp connection method. When you |
20496e1c594a
* auth-source.el: Precise Tramp doc.
Michael Albinus <michael.albinus@gmx.de>
parents:
95193
diff
changeset
|
56 ;;; don't use a port entry, you match any Tramp method. |
94980 | 57 |
92694 | 58 ;;; Code: |
59 | |
94837 | 60 (require 'gnus-util) |
61 | |
92694 | 62 (eval-when-compile (require 'cl)) |
93386 | 63 (eval-when-compile (require 'netrc)) |
92694 | 64 |
65 (defgroup auth-source nil | |
66 "Authentication sources." | |
93386 | 67 :version "23.1" ;; No Gnus |
92694 | 68 :group 'gnus) |
69 | |
93386 | 70 (defcustom auth-source-protocols '((imap "imap" "imaps" "143" "993") |
71 (pop3 "pop3" "pop" "pop3s" "110" "995") | |
72 (ssh "ssh" "22") | |
73 (sftp "sftp" "115") | |
74 (smtp "smtp" "25")) | |
75 "List of authentication protocols and their names" | |
76 | |
77 :group 'auth-source | |
78 :version "23.1" ;; No Gnus | |
79 :type '(repeat :tag "Authentication Protocols" | |
80 (cons :tag "Protocol Entry" | |
81 (symbol :tag "Protocol") | |
82 (repeat :tag "Names" | |
83 (string :tag "Name"))))) | |
84 | |
85 ;;; generate all the protocols in a format Customize can use | |
86 (defconst auth-source-protocols-customize | |
87 (mapcar (lambda (a) | |
88 (let ((p (car-safe a))) | |
95665
20496e1c594a
* auth-source.el: Precise Tramp doc.
Michael Albinus <michael.albinus@gmx.de>
parents:
95193
diff
changeset
|
89 (list 'const |
93386 | 90 :tag (upcase (symbol-name p)) |
91 p))) | |
92 auth-source-protocols)) | |
93 | |
94980 | 94 (defcustom auth-sources '((:source "~/.authinfo.gpg" :host t :protocol t)) |
92694 | 95 "List of authentication sources. |
96 | |
97 Each entry is the authentication type with optional properties." | |
98 :group 'auth-source | |
93386 | 99 :version "23.1" ;; No Gnus |
100 :type `(repeat :tag "Authentication Sources" | |
101 (list :tag "Source definition" | |
102 (const :format "" :value :source) | |
103 (string :tag "Authentication Source") | |
104 (const :format "" :value :host) | |
94209 | 105 (choice :tag "Host (machine) choice" |
93386 | 106 (const :tag "Any" t) |
94209 | 107 (regexp :tag "Host (machine) regular expression (TODO)") |
93386 | 108 (const :tag "Fallback" nil)) |
109 (const :format "" :value :protocol) | |
110 (choice :tag "Protocol" | |
111 (const :tag "Any" t) | |
112 (const :tag "Fallback" nil) | |
113 ,@auth-source-protocols-customize)))) | |
92694 | 114 |
115 ;; temp for debugging | |
93386 | 116 ;; (unintern 'auth-source-protocols) |
117 ;; (unintern 'auth-sources) | |
118 ;; (customize-variable 'auth-sources) | |
119 ;; (setq auth-sources nil) | |
120 ;; (format "%S" auth-sources) | |
121 ;; (customize-variable 'auth-source-protocols) | |
122 ;; (setq auth-source-protocols nil) | |
123 ;; (format "%S" auth-source-protocols) | |
124 ;; (auth-source-pick "a" 'imap) | |
125 ;; (auth-source-user-or-password "login" "imap.myhost.com" 'imap) | |
126 ;; (auth-source-user-or-password "password" "imap.myhost.com" 'imap) | |
127 ;; (auth-source-user-or-password-imap "login" "imap.myhost.com") | |
128 ;; (auth-source-user-or-password-imap "password" "imap.myhost.com") | |
129 ;; (auth-source-protocol-defaults 'imap) | |
130 | |
94369 | 131 (defun auth-source-pick (host protocol &optional fallback) |
132 "Parse `auth-sources' for HOST, and PROTOCOL matches. | |
93386 | 133 |
94369 | 134 Returns fallback choices (where PROTOCOL or HOST are nil) with FALLBACK t." |
93386 | 135 (interactive "sHost: \nsProtocol: \n") ;for testing |
136 (let (choices) | |
137 (dolist (choice auth-sources) | |
94369 | 138 (let ((h (plist-get choice :host)) |
93386 | 139 (p (plist-get choice :protocol))) |
140 (when (and | |
141 (or (equal t h) | |
142 (and (stringp h) (string-match h host)) | |
143 (and fallback (equal h nil))) | |
144 (or (equal t p) | |
145 (and (symbolp p) (equal p protocol)) | |
146 (and fallback (equal p nil)))) | |
147 (push choice choices)))) | |
148 (if choices | |
149 choices | |
150 (unless fallback | |
94369 | 151 (auth-source-pick host protocol t))))) |
93386 | 152 |
94369 | 153 (defun auth-source-user-or-password (mode host protocol) |
154 "Find user or password (from the string MODE) matching HOST and PROTOCOL." | |
95665
20496e1c594a
* auth-source.el: Precise Tramp doc.
Michael Albinus <michael.albinus@gmx.de>
parents:
95193
diff
changeset
|
155 (gnus-message 9 |
94837 | 156 "auth-source-user-or-password: get %s for %s (%s)" |
157 mode host protocol) | |
93386 | 158 (let (found) |
94369 | 159 (dolist (choice (auth-source-pick host protocol)) |
95665
20496e1c594a
* auth-source.el: Precise Tramp doc.
Michael Albinus <michael.albinus@gmx.de>
parents:
95193
diff
changeset
|
160 (setq found (netrc-machine-user-or-password |
93386 | 161 mode |
162 (plist-get choice :source) | |
163 (list host) | |
164 (list (format "%s" protocol)) | |
165 (auth-source-protocol-defaults protocol))) | |
166 (when found | |
95665
20496e1c594a
* auth-source.el: Precise Tramp doc.
Michael Albinus <michael.albinus@gmx.de>
parents:
95193
diff
changeset
|
167 (gnus-message 9 |
94837 | 168 "auth-source-user-or-password: found %s=%s for %s (%s)" |
95665
20496e1c594a
* auth-source.el: Precise Tramp doc.
Michael Albinus <michael.albinus@gmx.de>
parents:
95193
diff
changeset
|
169 mode |
94837 | 170 ;; don't show the password |
95665
20496e1c594a
* auth-source.el: Precise Tramp doc.
Michael Albinus <michael.albinus@gmx.de>
parents:
95193
diff
changeset
|
171 (if (equal mode "password") "SECRET" found) |
94837 | 172 host protocol) |
93386 | 173 (return found))))) |
174 | |
175 (defun auth-source-protocol-defaults (protocol) | |
176 "Return a list of default ports and names for PROTOCOL." | |
177 (cdr-safe (assoc protocol auth-source-protocols))) | |
178 | |
94369 | 179 (defun auth-source-user-or-password-imap (mode host) |
180 (auth-source-user-or-password mode host 'imap)) | |
93386 | 181 |
94369 | 182 (defun auth-source-user-or-password-pop3 (mode host) |
183 (auth-source-user-or-password mode host 'pop3)) | |
93386 | 184 |
94369 | 185 (defun auth-source-user-or-password-ssh (mode host) |
186 (auth-source-user-or-password mode host 'ssh)) | |
93386 | 187 |
94369 | 188 (defun auth-source-user-or-password-sftp (mode host) |
189 (auth-source-user-or-password mode host 'sftp)) | |
93386 | 190 |
94369 | 191 (defun auth-source-user-or-password-smtp (mode host) |
192 (auth-source-user-or-password mode host 'smtp)) | |
92694 | 193 |
194 (provide 'auth-source) | |
195 | |
196 ;; arch-tag: ff1afe78-06e9-42c2-b693-e9f922cbe4ab | |
197 ;;; auth-source.el ends here |