Mercurial > emacs
annotate lisp/gnus/auth-source.el @ 95212:a6a4ada91449
* dispextern.h, xfaces.c (xstrcasecmp): Renamed from xstricmp.
* dosfns.c, fileio.c, font.c, fontset.c, image.c, macfns.c:
* macterm.c, process.c, w32.c, w32fns.c, w32proc.c, xfaces.c:
* xfns.c, xfont.c: Callers changed.
author | Jason Rumney <jasonr@gnu.org> |
---|---|
date | Thu, 22 May 2008 14:52:37 +0000 |
parents | d90ff358ee27 |
children | 20496e1c594a |
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 ;;; (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 | |
92694 | 60 ;;; Code: |
61 | |
94837 | 62 (require 'gnus-util) |
63 | |
92694 | 64 (eval-when-compile (require 'cl)) |
93386 | 65 (eval-when-compile (require 'netrc)) |
92694 | 66 |
67 (defgroup auth-source nil | |
68 "Authentication sources." | |
93386 | 69 :version "23.1" ;; No Gnus |
92694 | 70 :group 'gnus) |
71 | |
93386 | 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 | |
94980 | 96 (defcustom auth-sources '((:source "~/.authinfo.gpg" :host t :protocol t)) |
92694 | 97 "List of authentication sources. |
98 | |
99 Each entry is the authentication type with optional properties." | |
100 :group 'auth-source | |
93386 | 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) | |
94209 | 107 (choice :tag "Host (machine) choice" |
93386 | 108 (const :tag "Any" t) |
94209 | 109 (regexp :tag "Host (machine) regular expression (TODO)") |
93386 | 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)))) | |
92694 | 116 |
117 ;; temp for debugging | |
93386 | 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 | |
94369 | 133 (defun auth-source-pick (host protocol &optional fallback) |
134 "Parse `auth-sources' for HOST, and PROTOCOL matches. | |
93386 | 135 |
94369 | 136 Returns fallback choices (where PROTOCOL or HOST are nil) with FALLBACK t." |
93386 | 137 (interactive "sHost: \nsProtocol: \n") ;for testing |
138 (let (choices) | |
139 (dolist (choice auth-sources) | |
94369 | 140 (let ((h (plist-get choice :host)) |
93386 | 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 | |
94369 | 153 (auth-source-pick host protocol t))))) |
93386 | 154 |
94369 | 155 (defun auth-source-user-or-password (mode host protocol) |
156 "Find user or password (from the string MODE) matching HOST and PROTOCOL." | |
94837 | 157 (gnus-message 9 |
158 "auth-source-user-or-password: get %s for %s (%s)" | |
159 mode host protocol) | |
93386 | 160 (let (found) |
94369 | 161 (dolist (choice (auth-source-pick host protocol)) |
93386 | 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 | |
94837 | 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) | |
93386 | 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 | |
94369 | 181 (defun auth-source-user-or-password-imap (mode host) |
182 (auth-source-user-or-password mode host 'imap)) | |
93386 | 183 |
94369 | 184 (defun auth-source-user-or-password-pop3 (mode host) |
185 (auth-source-user-or-password mode host 'pop3)) | |
93386 | 186 |
94369 | 187 (defun auth-source-user-or-password-ssh (mode host) |
188 (auth-source-user-or-password mode host 'ssh)) | |
93386 | 189 |
94369 | 190 (defun auth-source-user-or-password-sftp (mode host) |
191 (auth-source-user-or-password mode host 'sftp)) | |
93386 | 192 |
94369 | 193 (defun auth-source-user-or-password-smtp (mode host) |
194 (auth-source-user-or-password mode host 'smtp)) | |
92694 | 195 |
196 (provide 'auth-source) | |
197 | |
198 ;; arch-tag: ff1afe78-06e9-42c2-b693-e9f922cbe4ab | |
199 ;;; auth-source.el ends here |