(allout-encrypt-string): Recognize epg failure to decrypt gpg2 armored text
[bpt/emacs.git] / lisp / gnus / auth-source.el
CommitLineData
8f7abae3
MB
1;;; auth-source.el --- authentication sources for Gnus and Emacs
2
73b0cd50 3;; Copyright (C) 2008-2011 Free Software Foundation, Inc.
8f7abae3
MB
4
5;; Author: Ted Zlatanov <tzz@lifelogs.com>
6;; Keywords: news
7
8;; This file is part of GNU Emacs.
9
5e809f55 10;; GNU Emacs is free software: you can redistribute it and/or modify
8f7abae3 11;; it under the terms of the GNU General Public License as published by
5e809f55
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
8f7abae3
MB
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
5e809f55 17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8f7abae3
MB
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
5e809f55 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
8f7abae3
MB
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.
d55fe5bb 29
554a69b8 30;; See the auth.info Info documentation for details.
4079589f 31
cbabe91f
TZ
32;; TODO:
33
34;; - never decode the backend file unless it's necessary
35;; - a more generic way to match backends and search backend contents
36;; - absorb netrc.el and simplify it
37;; - protect passwords better
38;; - allow creating and changing netrc lines (not files) e.g. change a password
39
8f7abae3
MB
40;;; Code:
41
b8e0f0cd 42(require 'password-cache)
d638ac9e 43(require 'mm-util)
e952b711 44(require 'gnus-util)
1821a7b4 45(require 'netrc)
b8e0f0cd 46(require 'assoc)
8f7abae3 47(eval-when-compile (require 'cl))
b8e0f0cd
G
48(require 'eieio)
49
0e4966fb
MA
50(autoload 'secrets-create-item "secrets")
51(autoload 'secrets-delete-item "secrets")
ec7995fa 52(autoload 'secrets-get-alias "secrets")
b8e0f0cd 53(autoload 'secrets-get-attributes "secrets")
fb178e4c 54(autoload 'secrets-get-secret "secrets")
0e4966fb
MA
55(autoload 'secrets-list-collections "secrets")
56(autoload 'secrets-search-items "secrets")
8f7abae3 57
b8e0f0cd
G
58(defvar secrets-enabled)
59
8f7abae3
MB
60(defgroup auth-source nil
61 "Authentication sources."
9b3ebcb6 62 :version "23.1" ;; No Gnus
8f7abae3
MB
63 :group 'gnus)
64
584c9d3f
G
65;;;###autoload
66(defcustom auth-source-cache-expiry 7200
67 "How many seconds passwords are cached, or nil to disable
68expiring. Overrides `password-cache-expiry' through a
69let-binding."
70 :group 'auth-source
71 :type '(choice (const :tag "Never" nil)
72 (const :tag "All Day" 86400)
73 (const :tag "2 Hours" 7200)
74 (const :tag "30 Minutes" 1800)
75 (integer :tag "Seconds")))
76
b8e0f0cd
G
77(defclass auth-source-backend ()
78 ((type :initarg :type
79 :initform 'netrc
80 :type symbol
81 :custom symbol
82 :documentation "The backend type.")
83 (source :initarg :source
84 :type string
85 :custom string
86 :documentation "The backend source.")
87 (host :initarg :host
88 :initform t
89 :type t
90 :custom string
91 :documentation "The backend host.")
92 (user :initarg :user
93 :initform t
94 :type t
95 :custom string
96 :documentation "The backend user.")
35123c04
TZ
97 (port :initarg :port
98 :initform t
99 :type t
100 :custom string
101 :documentation "The backend protocol.")
b8e0f0cd
G
102 (create-function :initarg :create-function
103 :initform ignore
104 :type function
105 :custom function
106 :documentation "The create function.")
107 (search-function :initarg :search-function
108 :initform ignore
109 :type function
110 :custom function
111 :documentation "The search function.")))
112
9b3ebcb6 113(defcustom auth-source-protocols '((imap "imap" "imaps" "143" "993")
cbabe91f
TZ
114 (pop3 "pop3" "pop" "pop3s" "110" "995")
115 (ssh "ssh" "22")
116 (sftp "sftp" "115")
117 (smtp "smtp" "25"))
9b3ebcb6
MB
118 "List of authentication protocols and their names"
119
120 :group 'auth-source
ec7995fa 121 :version "23.2" ;; No Gnus
9b3ebcb6 122 :type '(repeat :tag "Authentication Protocols"
cbabe91f
TZ
123 (cons :tag "Protocol Entry"
124 (symbol :tag "Protocol")
125 (repeat :tag "Names"
126 (string :tag "Name")))))
9b3ebcb6
MB
127
128;;; generate all the protocols in a format Customize can use
fb178e4c 129;;; TODO: generate on the fly from auth-source-protocols
9b3ebcb6
MB
130(defconst auth-source-protocols-customize
131 (mapcar (lambda (a)
cbabe91f
TZ
132 (let ((p (car-safe a)))
133 (list 'const
134 :tag (upcase (symbol-name p))
135 p)))
136 auth-source-protocols))
9b3ebcb6 137
b8e0f0cd
G
138(defvar auth-source-creation-defaults nil
139 "Defaults for creating token values. Usually let-bound.")
140
141(make-obsolete 'auth-source-hide-passwords nil "Emacs 24.1")
142
143(defvar auth-source-magic "auth-source-magic ")
ed778fad
MB
144
145(defcustom auth-source-do-cache t
b8e0f0cd 146 "Whether auth-source should cache information with `password-cache'."
ed778fad 147 :group 'auth-source
ec7995fa 148 :version "23.2" ;; No Gnus
ed778fad
MB
149 :type `boolean)
150
a202ff49 151(defcustom auth-source-debug nil
554a69b8 152 "Whether auth-source should log debug messages.
554a69b8
KY
153
154If the value is nil, debug messages are not logged.
ca6ddb88
TZ
155
156If the value is t, debug messages are logged with `message'. In
157that case, your authentication data will be in the clear (except
158for passwords).
159
554a69b8
KY
160If the value is a function, debug messages are logged by calling
161 that function using the same arguments as `message'."
162 :group 'auth-source
ec7995fa 163 :version "23.2" ;; No Gnus
cbabe91f
TZ
164 :type `(choice
165 :tag "auth-source debugging mode"
166 (const :tag "Log using `message' to the *Messages* buffer" t)
167 (function :tag "Function that takes arguments like `message'")
168 (const :tag "Don't log anything" nil)))
554a69b8 169
b8e0f0cd 170(defcustom auth-sources '("~/.authinfo.gpg" "~/.authinfo")
8f7abae3
MB
171 "List of authentication sources.
172
b8e0f0cd
G
173The default will get login and password information from
174\"~/.authinfo.gpg\", which you should set up with the EPA/EPG
175packages to be encrypted. If that file doesn't exist, it will
176try the unencrypted version \"~/.authinfo\".
177
178See the auth.info manual for details.
fb178e4c 179
ec7995fa
KY
180Each entry is the authentication type with optional properties.
181
182It's best to customize this with `M-x customize-variable' because the choices
183can get pretty complex."
8f7abae3 184 :group 'auth-source
b8e0f0cd 185 :version "24.1" ;; No Gnus
9b3ebcb6 186 :type `(repeat :tag "Authentication Sources"
b8e0f0cd
G
187 (choice
188 (string :tag "Just a file")
189 (const :tag "Default Secrets API Collection" 'default)
5415d076 190 (const :tag "Login Secrets API Collection" "secrets:Login")
b8e0f0cd
G
191 (const :tag "Temp Secrets API Collection" "secrets:session")
192 (list :tag "Source definition"
193 (const :format "" :value :source)
194 (choice :tag "Authentication backend choice"
195 (string :tag "Authentication Source (file)")
196 (list
197 :tag "Secret Service API/KWallet/GNOME Keyring"
198 (const :format "" :value :secrets)
199 (choice :tag "Collection to use"
200 (string :tag "Collection name")
201 (const :tag "Default" 'default)
5415d076 202 (const :tag "Login" "Login")
b8e0f0cd
G
203 (const
204 :tag "Temporary" "session"))))
205 (repeat :tag "Extra Parameters" :inline t
206 (choice :tag "Extra parameter"
207 (list
208 :tag "Host"
209 (const :format "" :value :host)
210 (choice :tag "Host (machine) choice"
211 (const :tag "Any" t)
212 (regexp
213 :tag "Regular expression")))
214 (list
215 :tag "Protocol"
35123c04 216 (const :format "" :value :port)
b8e0f0cd
G
217 (choice
218 :tag "Protocol"
219 (const :tag "Any" t)
220 ,@auth-source-protocols-customize))
221 (list :tag "User" :inline t
222 (const :format "" :value :user)
223 (choice :tag "Personality/Username"
224 (const :tag "Any" t)
225 (string :tag "Name")))))))))
8f7abae3 226
549c9aed
G
227(defcustom auth-source-gpg-encrypt-to t
228 "List of recipient keys that `authinfo.gpg' encrypted to.
229If the value is not a list, symmetric encryption will be used."
230 :group 'auth-source
b8e0f0cd 231 :version "24.1" ;; No Gnus
549c9aed 232 :type '(choice (const :tag "Symmetric encryption" t)
b8e0f0cd
G
233 (repeat :tag "Recipient public keys"
234 (string :tag "Recipient public key"))))
549c9aed 235
8f7abae3 236;; temp for debugging
9b3ebcb6
MB
237;; (unintern 'auth-source-protocols)
238;; (unintern 'auth-sources)
239;; (customize-variable 'auth-sources)
240;; (setq auth-sources nil)
241;; (format "%S" auth-sources)
242;; (customize-variable 'auth-source-protocols)
243;; (setq auth-source-protocols nil)
244;; (format "%S" auth-source-protocols)
fb178e4c 245;; (auth-source-pick nil :host "a" :port 'imap)
9b3ebcb6
MB
246;; (auth-source-user-or-password "login" "imap.myhost.com" 'imap)
247;; (auth-source-user-or-password "password" "imap.myhost.com" 'imap)
248;; (auth-source-user-or-password-imap "login" "imap.myhost.com")
249;; (auth-source-user-or-password-imap "password" "imap.myhost.com")
250;; (auth-source-protocol-defaults 'imap)
251
ca6ddb88
TZ
252;; (let ((auth-source-debug 'debug)) (auth-source-do-debug "hello"))
253;; (let ((auth-source-debug t)) (auth-source-do-debug "hello"))
254;; (let ((auth-source-debug nil)) (auth-source-do-debug "hello"))
554a69b8 255(defun auth-source-do-debug (&rest msg)
554a69b8 256 (when auth-source-debug
ca6ddb88
TZ
257 (apply 'auth-source-do-warn msg)))
258
259(defun auth-source-do-warn (&rest msg)
260 (apply
261 ;; set logger to either the function in auth-source-debug or 'message
262 ;; note that it will be 'message if auth-source-debug is nil
263 (if (functionp auth-source-debug)
264 auth-source-debug
265 'message)
266 msg))
267
554a69b8 268
35123c04
TZ
269;; (auth-source-pick nil :host "any" :port 'imap :user "joe")
270;; (auth-source-pick t :host "any" :port 'imap :user "joe")
271;; (setq auth-sources '((:source (:secrets default) :host t :port t :user "joe")
272;; (:source (:secrets "session") :host t :port t :user "joe")
273;; (:source (:secrets "Login") :host t :port t)
274;; (:source "~/.authinfo.gpg" :host t :port t)))
fb178e4c 275
35123c04
TZ
276;; (setq auth-sources '((:source (:secrets default) :host t :port t :user "joe")
277;; (:source (:secrets "session") :host t :port t :user "joe")
278;; (:source (:secrets "Login") :host t :port t)
cbabe91f 279;; ))
fb178e4c 280
35123c04 281;; (setq auth-sources '((:source "~/.authinfo.gpg" :host t :port t)))
fb178e4c 282
b8e0f0cd
G
283;; (auth-source-backend-parse "myfile.gpg")
284;; (auth-source-backend-parse 'default)
5415d076 285;; (auth-source-backend-parse "secrets:Login")
b8e0f0cd
G
286
287(defun auth-source-backend-parse (entry)
288 "Creates an auth-source-backend from an ENTRY in `auth-sources'."
289 (auth-source-backend-parse-parameters
290 entry
291 (cond
292 ;; take 'default and recurse to get it as a Secrets API default collection
293 ;; matching any user, host, and protocol
294 ((eq entry 'default)
295 (auth-source-backend-parse '(:source (:secrets default))))
296 ;; take secrets:XYZ and recurse to get it as Secrets API collection "XYZ"
297 ;; matching any user, host, and protocol
298 ((and (stringp entry) (string-match "^secrets:\\(.+\\)" entry))
299 (auth-source-backend-parse `(:source (:secrets ,(match-string 1 entry)))))
300 ;; take just a file name and recurse to get it as a netrc file
301 ;; matching any user, host, and protocol
302 ((stringp entry)
303 (auth-source-backend-parse `(:source ,entry)))
304
305 ;; a file name with parameters
306 ((stringp (plist-get entry :source))
307 (auth-source-backend
308 (plist-get entry :source)
309 :source (plist-get entry :source)
310 :type 'netrc
311 :search-function 'auth-source-netrc-search
312 :create-function 'auth-source-netrc-create))
313
314 ;; the Secrets API. We require the package, in order to have a
315 ;; defined value for `secrets-enabled'.
316 ((and
317 (not (null (plist-get entry :source))) ; the source must not be nil
318 (listp (plist-get entry :source)) ; and it must be a list
319 (require 'secrets nil t) ; and we must load the Secrets API
320 secrets-enabled) ; and that API must be enabled
321
322 ;; the source is either the :secrets key in ENTRY or
323 ;; if that's missing or nil, it's "session"
324 (let ((source (or (plist-get (plist-get entry :source) :secrets)
325 "session")))
326
327 ;; if the source is a symbol, we look for the alias named so,
5415d076 328 ;; and if that alias is missing, we use "Login"
b8e0f0cd
G
329 (when (symbolp source)
330 (setq source (or (secrets-get-alias (symbol-name source))
5415d076 331 "Login")))
b8e0f0cd 332
ca6ddb88
TZ
333 (if (featurep 'secrets)
334 (auth-source-backend
335 (format "Secrets API (%s)" source)
336 :source source
337 :type 'secrets
338 :search-function 'auth-source-secrets-search
339 :create-function 'auth-source-secrets-create)
340 (auth-source-do-warn
341 "auth-source-backend-parse: no Secrets API, ignoring spec: %S" entry)
342 (auth-source-backend
343 (format "Ignored Secrets API (%s)" source)
344 :source ""
345 :type 'ignore))))
b8e0f0cd
G
346
347 ;; none of them
348 (t
ca6ddb88 349 (auth-source-do-warn
b8e0f0cd
G
350 "auth-source-backend-parse: invalid backend spec: %S" entry)
351 (auth-source-backend
352 "Empty"
353 :source ""
354 :type 'ignore)))))
355
356(defun auth-source-backend-parse-parameters (entry backend)
357 "Fills in the extra auth-source-backend parameters of ENTRY.
35123c04
TZ
358Using the plist ENTRY, get the :host, :port, and :user search
359parameters."
e45de620
TZ
360 (let ((entry (if (stringp entry)
361 nil
362 entry))
363 val)
b8e0f0cd
G
364 (when (setq val (plist-get entry :host))
365 (oset backend host val))
366 (when (setq val (plist-get entry :user))
367 (oset backend user val))
35123c04
TZ
368 (when (setq val (plist-get entry :port))
369 (oset backend port val)))
b8e0f0cd
G
370 backend)
371
372;; (mapcar 'auth-source-backend-parse auth-sources)
373
374(defun* auth-source-search (&rest spec
35123c04 375 &key type max host user port secret
b8e0f0cd
G
376 create delete
377 &allow-other-keys)
378 "Search or modify authentication backends according to SPEC.
379
380This function parses `auth-sources' for matches of the SPEC
381plist. It can optionally create or update an authentication
382token if requested. A token is just a standard Emacs property
383list with a :secret property that can be a function; all the
384other properties will always hold scalar values.
385
386Typically the :secret property, if present, contains a password.
387
35123c04 388Common search keys are :max, :host, :port, and :user. In
b8e0f0cd
G
389addition, :create specifies how tokens will be or created.
390Finally, :type can specify which backend types you want to check.
391
392A string value is always matched literally. A symbol is matched
393as its string value, literally. All the SPEC values can be
394single values (symbol or string) or lists thereof (in which case
395any of the search terms matches).
396
397:create t means to create a token if possible.
398
399A new token will be created if no matching tokens were found.
400The new token will have only the keys the backend requires. For
401the netrc backend, for instance, that's the user, host, and
35123c04 402port keys.
b8e0f0cd
G
403
404Here's an example:
405
406\(let ((auth-source-creation-defaults '((user . \"defaultUser\")
407 (A . \"default A\"))))
408 (auth-source-search :host \"mine\" :type 'netrc :max 1
409 :P \"pppp\" :Q \"qqqq\"
410 :create t))
411
412which says:
413
414\"Search for any entry matching host 'mine' in backends of type
415 'netrc', maximum one result.
416
417 Create a new entry if you found none. The netrc backend will
35123c04 418 automatically require host, user, and port. The host will be
b8e0f0cd 419 'mine'. We prompt for the user with default 'defaultUser' and
35123c04 420 for the port without a default. We will not prompt for A, Q,
b8e0f0cd 421 or P. The resulting token will only have keys user, host, and
35123c04 422 port.\"
b8e0f0cd
G
423
424:create '(A B C) also means to create a token if possible.
425
426The behavior is like :create t but if the list contains any
427parameter, that parameter will be required in the resulting
428token. The value for that parameter will be obtained from the
429search parameters or from user input. If any queries are needed,
430the alist `auth-source-creation-defaults' will be checked for the
431default prompt.
432
433Here's an example:
434
435\(let ((auth-source-creation-defaults '((user . \"defaultUser\")
436 (A . \"default A\"))))
437 (auth-source-search :host '(\"nonesuch\" \"twosuch\") :type 'netrc :max 1
438 :P \"pppp\" :Q \"qqqq\"
439 :create '(A B Q)))
440
441which says:
442
443\"Search for any entry matching host 'nonesuch'
444 or 'twosuch' in backends of type 'netrc', maximum one result.
445
446 Create a new entry if you found none. The netrc backend will
35123c04 447 automatically require host, user, and port. The host will be
b8e0f0cd 448 'nonesuch' and Q will be 'qqqq'. We prompt for A with default
35123c04 449 'default A', for B and port with default nil, and for the
b8e0f0cd 450 user with default 'defaultUser'. We will not prompt for Q. The
35123c04 451 resulting token will have keys user, host, port, A, B, and Q.
b8e0f0cd
G
452 It will not have P with any value, even though P is used in the
453 search to find only entries that have P set to 'pppp'.\"
454
455When multiple values are specified in the search parameter, the
456first one is used for creation. So :host (X Y Z) would create a
457token for host X, for instance.
458
459This creation can fail if the search was not specific enough to
460create a new token (it's up to the backend to decide that). You
461should `catch' the backend-specific error as usual. Some
462backends (netrc, at least) will prompt the user rather than throw
463an error.
464
465:delete t means to delete any found entries. nil by default.
466Use `auth-source-delete' in ELisp code instead of calling
467`auth-source-search' directly with this parameter.
468
469:type (X Y Z) will check only those backend types. 'netrc and
470'secrets are the only ones supported right now.
471
472:max N means to try to return at most N items (defaults to 1).
473When 0 the function will return just t or nil to indicate if any
474matches were found. More than N items may be returned, depending
475on the search and the backend.
476
477:host (X Y Z) means to match only hosts X, Y, or Z according to
478the match rules above. Defaults to t.
479
480:user (X Y Z) means to match only users X, Y, or Z according to
481the match rules above. Defaults to t.
482
35123c04 483:port (P Q R) means to match only protocols P, Q, or R.
b8e0f0cd
G
484Defaults to t.
485
486:K (V1 V2 V3) for any other key K will match values V1, V2, or
487V3 (note the match rules above).
488
489The return value is a list with at most :max tokens. Each token
35123c04 490is a plist with keys :backend :host :port :user, plus any other
b8e0f0cd
G
491keys provided by the backend (notably :secret). But note the
492exception for :max 0, which see above.
493
494The token's :secret key can hold a function. In that case you
495must call it to obtain the actual value."
496 (let* ((backends (mapcar 'auth-source-backend-parse auth-sources))
497 (max (or max 1))
498 (ignored-keys '(:create :delete :max))
499 (keys (loop for i below (length spec) by 2
500 unless (memq (nth i spec) ignored-keys)
501 collect (nth i spec)))
502 (found (auth-source-recall spec))
503 filtered-backends accessor-key found-here goal)
504
505 (if (and found auth-source-do-cache)
506 (auth-source-do-debug
507 "auth-source-search: found %d CACHED results matching %S"
508 (length found) spec)
509
510 (assert
511 (or (eq t create) (listp create)) t
d5e9a4e9 512 "Invalid auth-source :create parameter (must be nil, t, or a list): %s %s")
b8e0f0cd 513
6ce6c742 514 (setq filtered-backends (copy-sequence backends))
b8e0f0cd
G
515 (dolist (backend backends)
516 (dolist (key keys)
517 ;; ignore invalid slots
518 (condition-case signal
519 (unless (eval `(auth-source-search-collection
520 (plist-get spec key)
521 (oref backend ,key)))
522 (setq filtered-backends (delq backend filtered-backends))
523 (return))
524 (invalid-slot-name))))
525
526 (auth-source-do-debug
527 "auth-source-search: found %d backends matching %S"
528 (length filtered-backends) spec)
529
530 ;; (debug spec "filtered" filtered-backends)
531 (setq goal max)
532 (dolist (backend filtered-backends)
533 (setq found-here (apply
534 (slot-value backend 'search-function)
535 :backend backend
536 :create create
537 :delete delete
538 spec))
539
540 ;; if max is 0, as soon as we find something, return it
541 (when (and (zerop max) (> 0 (length found-here)))
542 (return t))
543
544 ;; decrement the goal by the number of new results
545 (decf goal (length found-here))
546 ;; and append the new results to the full list
547 (setq found (append found found-here))
548
549 (auth-source-do-debug
550 "auth-source-search: found %d results (max %d/%d) in %S matching %S"
551 (length found-here) max goal backend spec)
552
553 ;; return full list if the goal is 0 or negative
554 (when (zerop (max 0 goal))
555 (return found))
556
557 ;; change the :max parameter in the spec to the goal
558 (setq spec (plist-put spec :max goal)))
559
560 (when (and found auth-source-do-cache)
561 (auth-source-remember spec found)))
562
563 found))
564
565;;; (auth-source-search :max 1)
566;;; (funcall (plist-get (nth 0 (auth-source-search :max 1)) :secret))
567;;; (auth-source-search :host "nonesuch" :type 'netrc :K 1)
568;;; (auth-source-search :host "nonesuch" :type 'secrets)
569
570(defun* auth-source-delete (&rest spec
571 &key delete
572 &allow-other-keys)
573 "Delete entries from the authentication backends according to SPEC.
574Calls `auth-source-search' with the :delete property in SPEC set to t.
575The backend may not actually delete the entries.
576
577Returns the deleted entries."
578 (auth-source-search (plist-put spec :delete t)))
579
580(defun auth-source-search-collection (collection value)
581 "Returns t is VALUE is t or COLLECTION is t or contains VALUE."
582 (when (and (atom collection) (not (eq t collection)))
583 (setq collection (list collection)))
584
585 ;; (debug :collection collection :value value)
586 (or (eq collection t)
587 (eq value t)
588 (equal collection value)
589 (member value collection)))
ed778fad 590
3b36c17e 591(defun auth-source-forget-all-cached ()
b8e0f0cd 592 "Forget all cached auth-source data."
3b36c17e 593 (interactive)
b8e0f0cd
G
594 (loop for sym being the symbols of password-data
595 ;; when the symbol name starts with auth-source-magic
596 when (string-match (concat "^" auth-source-magic)
597 (symbol-name sym))
598 ;; remove that key
599 do (password-cache-remove (symbol-name sym))))
600
601(defun auth-source-remember (spec found)
602 "Remember FOUND search results for SPEC."
584c9d3f
G
603 (let ((password-cache-expiry auth-source-cache-expiry))
604 (password-cache-add
605 (concat auth-source-magic (format "%S" spec)) found)))
b8e0f0cd
G
606
607(defun auth-source-recall (spec)
608 "Recall FOUND search results for SPEC."
609 (password-read-from-cache
610 (concat auth-source-magic (format "%S" spec))))
611
612(defun auth-source-forget (spec)
613 "Forget any cached data matching SPEC exactly.
614
615This is the same SPEC you passed to `auth-source-search'.
616Returns t or nil for forgotten or not found."
617 (password-cache-remove (concat auth-source-magic (format "%S" spec))))
618
619;;; (loop for sym being the symbols of password-data when (string-match (concat "^" auth-source-magic) (symbol-name sym)) collect (symbol-name sym))
620
621;;; (auth-source-remember '(:host "wedd") '(4 5 6))
622;;; (auth-source-remember '(:host "xedd") '(1 2 3))
623;;; (auth-source-recall '(:host "xedd"))
624;;; (auth-source-recall '(:host t))
625;;; (auth-source-forget+ :host t)
626
627(defun* auth-source-forget+ (&rest spec &allow-other-keys)
628 "Forget any cached data matching SPEC. Returns forgotten count.
629
630This is not a full `auth-source-search' spec but works similarly.
631For instance, \(:host \"myhost\" \"yourhost\") would find all the
632cached data that was found with a search for those two hosts,
633while \(:host t) would find all host entries."
634 (let ((count 0)
635 sname)
636 (loop for sym being the symbols of password-data
637 ;; when the symbol name matches with auth-source-magic
638 when (and (setq sname (symbol-name sym))
639 (string-match (concat "^" auth-source-magic "\\(.+\\)")
640 sname)
641 ;; and the spec matches what was stored in the cache
642 (auth-source-specmatchp spec (read (match-string 1 sname))))
643 ;; remove that key
644 do (progn
645 (password-cache-remove sname)
646 (incf count)))
647 count))
648
649(defun auth-source-specmatchp (spec stored)
650 (let ((keys (loop for i below (length spec) by 2
651 collect (nth i spec))))
652 (not (eq
653 (dolist (key keys)
654 (unless (auth-source-search-collection (plist-get stored key)
655 (plist-get spec key))
656 (return 'no)))
657 'no))))
658
659;;; Backend specific parsing: netrc/authinfo backend
660
661;;; (auth-source-netrc-parse "~/.authinfo.gpg")
662(defun* auth-source-netrc-parse (&rest
663 spec
35123c04 664 &key file max host user port delete
b8e0f0cd
G
665 &allow-other-keys)
666 "Parse FILE and return a list of all entries in the file.
667Note that the MAX parameter is used so we can exit the parse early."
668 (if (listp file)
669 ;; We got already parsed contents; just return it.
670 file
671 (when (file-exists-p file)
672 (with-temp-buffer
673 (let ((tokens '("machine" "host" "default" "login" "user"
674 "password" "account" "macdef" "force"
675 "port" "protocol"))
676 (max (or max 5000)) ; sanity check: default to stop at 5K
677 (modified 0)
678 alist elem result pair)
679 (insert-file-contents file)
680 (goto-char (point-min))
681 ;; Go through the file, line by line.
682 (while (and (not (eobp))
683 (> max 0))
684
685 (narrow-to-region (point) (point-at-eol))
686 ;; For each line, get the tokens and values.
687 (while (not (eobp))
688 (skip-chars-forward "\t ")
689 ;; Skip lines that begin with a "#".
690 (if (eq (char-after) ?#)
691 (goto-char (point-max))
692 (unless (eobp)
693 (setq elem
694 (if (= (following-char) ?\")
695 (read (current-buffer))
696 (buffer-substring
697 (point) (progn (skip-chars-forward "^\t ")
698 (point)))))
699 (cond
700 ((equal elem "macdef")
701 ;; We skip past the macro definition.
702 (widen)
703 (while (and (zerop (forward-line 1))
704 (looking-at "$")))
705 (narrow-to-region (point) (point)))
706 ((member elem tokens)
707 ;; Tokens that don't have a following value are ignored,
708 ;; except "default".
709 (when (and pair (or (cdr pair)
710 (equal (car pair) "default")))
711 (push pair alist))
712 (setq pair (list elem)))
713 (t
714 ;; Values that haven't got a preceding token are ignored.
715 (when pair
716 (setcdr pair elem)
717 (push pair alist)
718 (setq pair nil)))))))
719
720 (when (and alist
721 (> max 0)
722 (auth-source-search-collection
723 host
724 (or
725 (aget alist "machine")
35123c04
TZ
726 (aget alist "host")
727 t))
b8e0f0cd
G
728 (auth-source-search-collection
729 user
730 (or
731 (aget alist "login")
732 (aget alist "account")
35123c04
TZ
733 (aget alist "user")
734 t))
b8e0f0cd 735 (auth-source-search-collection
35123c04 736 port
b8e0f0cd
G
737 (or
738 (aget alist "port")
35123c04
TZ
739 (aget alist "protocol")
740 t)))
b8e0f0cd
G
741 (decf max)
742 (push (nreverse alist) result)
743 ;; to delete a line, we just comment it out
744 (when delete
745 (goto-char (point-min))
746 (insert "#")
747 (incf modified)))
748 (setq alist nil
749 pair nil)
750 (widen)
751 (forward-line 1))
752
753 (when (< 0 modified)
754 (when auth-source-gpg-encrypt-to
755 ;; (see bug#7487) making `epa-file-encrypt-to' local to
756 ;; this buffer lets epa-file skip the key selection query
757 ;; (see the `local-variable-p' check in
758 ;; `epa-file-write-region').
759 (unless (local-variable-p 'epa-file-encrypt-to (current-buffer))
760 (make-local-variable 'epa-file-encrypt-to))
761 (if (listp auth-source-gpg-encrypt-to)
762 (setq epa-file-encrypt-to auth-source-gpg-encrypt-to)))
763
764 ;; ask AFTER we've successfully opened the file
765 (when (y-or-n-p (format "Save file %s? (%d modifications)"
766 file modified))
767 (write-region (point-min) (point-max) file nil 'silent)
768 (auth-source-do-debug
769 "auth-source-netrc-parse: modified %d lines in %s"
770 modified file)))
771
772 (nreverse result))))))
773
774(defun auth-source-netrc-normalize (alist)
775 (mapcar (lambda (entry)
776 (let (ret item)
777 (while (setq item (pop entry))
778 (let ((k (car item))
779 (v (cdr item)))
780
781 ;; apply key aliases
782 (setq k (cond ((member k '("machine")) "host")
783 ((member k '("login" "account")) "user")
784 ((member k '("protocol")) "port")
785 ((member k '("password")) "secret")
786 (t k)))
787
788 ;; send back the secret in a function (lexical binding)
789 (when (equal k "secret")
790 (setq v (lexical-let ((v v))
791 (lambda () v))))
792
793 (setq ret (plist-put ret
794 (intern (concat ":" k))
795 v))
796 ))
797 ret))
798 alist))
799
800;;; (setq secret (plist-get (nth 0 (auth-source-search :host t :type 'netrc :K 1 :max 1)) :secret))
801;;; (funcall secret)
802
803(defun* auth-source-netrc-search (&rest
804 spec
805 &key backend create delete
35123c04 806 type max host user port
b8e0f0cd
G
807 &allow-other-keys)
808"Given a property list SPEC, return search matches from the :backend.
809See `auth-source-search' for details on SPEC."
810 ;; just in case, check that the type is correct (null or same as the backend)
811 (assert (or (null type) (eq type (oref backend type)))
d5e9a4e9 812 t "Invalid netrc search: %s %s")
b8e0f0cd
G
813
814 (let ((results (auth-source-netrc-normalize
815 (auth-source-netrc-parse
816 :max max
817 :delete delete
818 :file (oref backend source)
819 :host (or host t)
820 :user (or user t)
35123c04 821 :port (or port t)))))
b8e0f0cd
G
822
823 ;; if we need to create an entry AND none were found to match
824 (when (and create
825 (= 0 (length results)))
826
584c9d3f
G
827 ;; create based on the spec and record the value
828 (setq results (or
829 ;; if the user did not want to create the entry
830 ;; in the file, it will be returned
831 (apply (slot-value backend 'create-function) spec)
832 ;; if not, we do the search again without :create
833 ;; to get the updated data.
834
835 ;; the result will be returned, even if the search fails
836 (apply 'auth-source-netrc-search
837 (plist-put spec :create nil)))))
b8e0f0cd
G
838 results))
839
840;;; (auth-source-search :host "nonesuch" :type 'netrc :max 1 :create t)
841;;; (auth-source-search :host "nonesuch" :type 'netrc :max 1 :create t :create-extra-keys '((A "default A") (B)))
842
843(defun* auth-source-netrc-create (&rest spec
844 &key backend
35123c04 845 secret host user port create
b8e0f0cd 846 &allow-other-keys)
35123c04 847 (let* ((base-required '(host user port secret))
b8e0f0cd
G
848 ;; we know (because of an assertion in auth-source-search) that the
849 ;; :create parameter is either t or a list (which includes nil)
850 (create-extra (if (eq t create) nil create))
851 (required (append base-required create-extra))
852 (file (oref backend source))
853 (add "")
854 ;; `valist' is an alist
584c9d3f
G
855 valist
856 ;; `artificial' will be returned if no creation is needed
857 artificial)
b8e0f0cd
G
858
859 ;; only for base required elements (defined as function parameters):
860 ;; fill in the valist with whatever data we may have from the search
861 ;; we take the first value if it's a list, the whole value otherwise
862 (dolist (br base-required)
863 (when (symbol-value br)
864 (aput 'valist br (if (listp (symbol-value br))
865 (nth 0 (symbol-value br))
866 (symbol-value br)))))
867
868 ;; for extra required elements, see if the spec includes a value for them
869 (dolist (er create-extra)
870 (let ((name (concat ":" (symbol-name er)))
871 (keys (loop for i below (length spec) by 2
872 collect (nth i spec))))
873 (dolist (k keys)
874 (when (equal (symbol-name k) name)
875 (aput 'valist er (plist-get spec k))))))
876
877 ;; for each required element
878 (dolist (r required)
879 (let* ((data (aget valist r))
880 (given-default (aget auth-source-creation-defaults r))
881 ;; the defaults are simple
882 (default (cond
883 ((and (not given-default) (eq r 'user))
884 (user-login-name))
885 ;; note we need this empty string
35123c04 886 ((and (not given-default) (eq r 'port))
b8e0f0cd
G
887 "")
888 (t given-default)))
889 ;; the prompt's default string depends on the data so far
890 (default-string (if (and default (< 0 (length default)))
891 (format " (default %s)" default)
892 " (no default)"))
893 ;; the prompt should also show what's entered so far
894 (user-value (aget valist 'user))
895 (host-value (aget valist 'host))
35123c04 896 (port-value (aget valist 'port))
b8e0f0cd
G
897 (info-so-far (concat (if user-value
898 (format "%s@" user-value)
899 "[USER?]")
900 (if host-value
901 (format "%s" host-value)
902 "[HOST?]")
35123c04 903 (if port-value
b8e0f0cd 904 ;; this distinguishes protocol between
35123c04 905 (if (zerop (length port-value))
b8e0f0cd 906 "" ; 'entered as "no default"' vs.
35123c04 907 (format ":%s" port-value)) ; given
b8e0f0cd 908 ;; and this is when the protocol is unknown
35123c04 909 "[PORT?]"))))
3b36c17e 910
b8e0f0cd
G
911 ;; now prompt if the search SPEC did not include a required key;
912 ;; take the result and put it in `data' AND store it in `valist'
913 (aput 'valist r
914 (setq data
915 (cond
916 ((and (null data) (eq r 'secret))
917 ;; special case prompt for passwords
918 (read-passwd (format "Password for %s: " info-so-far)))
919 ((null data)
920 (read-string
921 (format "Enter %s for %s%s: "
922 r info-so-far default-string)
923 nil nil default))
924 (t data))))
925
584c9d3f
G
926 (when data
927 (setq artificial (plist-put artificial
928 (intern (concat ":" (symbol-name r)))
929 (if (eq r 'secret)
930 (lexical-let ((data data))
931 (lambda () data))
932 data))))
933
b8e0f0cd
G
934 ;; when r is not an empty string...
935 (when (and (stringp data)
936 (< 0 (length data)))
937 ;; append the key (the symbol name of r) and the value in r
938 (setq add (concat add
939 (format "%s%s %S"
940 ;; prepend a space
941 (if (zerop (length add)) "" " ")
942 ;; remap auth-source tokens to netrc
943 (case r
944 ('user "login")
945 ('host "machine")
946 ('secret "password")
35123c04 947 ('port "port") ; redundant but clearer
b8e0f0cd
G
948 (t (symbol-name r)))
949 ;; the value will be printed in %S format
950 data))))))
951
952 (with-temp-buffer
953 (when (file-exists-p file)
954 (insert-file-contents file))
955 (when auth-source-gpg-encrypt-to
956 ;; (see bug#7487) making `epa-file-encrypt-to' local to
957 ;; this buffer lets epa-file skip the key selection query
958 ;; (see the `local-variable-p' check in
959 ;; `epa-file-write-region').
960 (unless (local-variable-p 'epa-file-encrypt-to (current-buffer))
961 (make-local-variable 'epa-file-encrypt-to))
962 (if (listp auth-source-gpg-encrypt-to)
963 (setq epa-file-encrypt-to auth-source-gpg-encrypt-to)))
964 (goto-char (point-max))
965
966 ;; ask AFTER we've successfully opened the file
584c9d3f
G
967 (if (y-or-n-p (format "Add to file %s: line [%s]" file add))
968 (progn
969 (unless (bolp)
970 (insert "\n"))
971 (insert add "\n")
972 (write-region (point-min) (point-max) file nil 'silent)
973 (auth-source-do-debug
974 "auth-source-netrc-create: wrote 1 new line to %s"
975 file)
976 nil)
977 (list artificial)))))
b8e0f0cd
G
978
979;;; Backend specific parsing: Secrets API backend
980
981;;; (let ((auth-sources '(default))) (auth-source-search :max 1 :create t))
982;;; (let ((auth-sources '(default))) (auth-source-search :max 1 :delete t))
983;;; (let ((auth-sources '(default))) (auth-source-search :max 1))
984;;; (let ((auth-sources '(default))) (auth-source-search))
5415d076
G
985;;; (let ((auth-sources '("secrets:Login"))) (auth-source-search :max 1))
986;;; (let ((auth-sources '("secrets:Login"))) (auth-source-search :max 1 :signon_realm "https://git.gnus.org/Git"))
b8e0f0cd
G
987
988(defun* auth-source-secrets-search (&rest
989 spec
990 &key backend create delete label
35123c04 991 type max host user port
b8e0f0cd
G
992 &allow-other-keys)
993 "Search the Secrets API; spec is like `auth-source'.
994
995The :label key specifies the item's label. It is the only key
996that can specify a substring. Any :label value besides a string
997will allow any label.
998
999All other search keys must match exactly. If you need substring
1000matching, do a wider search and narrow it down yourself.
1001
1002You'll get back all the properties of the token as a plist.
1003
5415d076 1004Here's an example that looks for the first item in the 'Login'
b8e0f0cd
G
1005Secrets collection:
1006
5415d076 1007 \(let ((auth-sources '(\"secrets:Login\")))
b8e0f0cd
G
1008 (auth-source-search :max 1)
1009
5415d076 1010Here's another that looks for the first item in the 'Login'
b8e0f0cd
G
1011Secrets collection whose label contains 'gnus':
1012
5415d076 1013 \(let ((auth-sources '(\"secrets:Login\")))
b8e0f0cd
G
1014 (auth-source-search :max 1 :label \"gnus\")
1015
5415d076 1016And this one looks for the first item in the 'Login' Secrets
b8e0f0cd 1017collection that's a Google Chrome entry for the git.gnus.org site
5415d076 1018authentication tokens:
b8e0f0cd 1019
5415d076 1020 \(let ((auth-sources '(\"secrets:Login\")))
b8e0f0cd
G
1021 (auth-source-search :max 1 :signon_realm \"https://git.gnus.org/Git\"))
1022"
1023
1024 ;; TODO
1025 (assert (not create) nil
1026 "The Secrets API auth-source backend doesn't support creation yet")
1027 ;; TODO
1028 ;; (secrets-delete-item coll elt)
1029 (assert (not delete) nil
1030 "The Secrets API auth-source backend doesn't support deletion yet")
1031
1032 (let* ((coll (oref backend source))
1033 (max (or max 5000)) ; sanity check: default to stop at 5K
1034 (ignored-keys '(:create :delete :max :backend :label))
1035 (search-keys (loop for i below (length spec) by 2
1036 unless (memq (nth i spec) ignored-keys)
1037 collect (nth i spec)))
1038 ;; build a search spec without the ignored keys
1039 ;; if a search key is nil or t (match anything), we skip it
5415d076
G
1040 (search-spec (apply 'append (mapcar
1041 (lambda (k)
1042 (if (or (null (plist-get spec k))
1043 (eq t (plist-get spec k)))
1044 nil
1045 (list k (plist-get spec k))))
1046 search-keys)))
35123c04 1047 ;; needed keys (always including host, login, port, and secret)
d638ac9e 1048 (returned-keys (mm-delete-duplicates (append
35123c04 1049 '(:host :login :port :secret)
d638ac9e 1050 search-keys)))
b8e0f0cd
G
1051 (items (loop for item in (apply 'secrets-search-items coll search-spec)
1052 unless (and (stringp label)
1053 (not (string-match label item)))
1054 collect item))
1055 ;; TODO: respect max in `secrets-search-items', not after the fact
5415d076 1056 (items (butlast items (- (length items) max)))
b8e0f0cd
G
1057 ;; convert the item name to a full plist
1058 (items (mapcar (lambda (item)
1059 (append
1060 ;; make an entry for the secret (password) element
1061 (list
1062 :secret
1063 (lexical-let ((v (secrets-get-secret coll item)))
1064 (lambda () v)))
1065 ;; rewrite the entry from ((k1 v1) (k2 v2)) to plist
5415d076
G
1066 (apply 'append
1067 (mapcar (lambda (entry)
1068 (list (car entry) (cdr entry)))
1069 (secrets-get-attributes coll item)))))
b8e0f0cd
G
1070 items))
1071 ;; ensure each item has each key in `returned-keys'
1072 (items (mapcar (lambda (plist)
1073 (append
5415d076
G
1074 (apply 'append
1075 (mapcar (lambda (req)
1076 (if (plist-get plist req)
1077 nil
1078 (list req nil)))
1079 returned-keys))
b8e0f0cd
G
1080 plist))
1081 items)))
1082 items))
1083
1084(defun* auth-source-secrets-create (&rest
1085 spec
35123c04 1086 &key backend type max host user port
b8e0f0cd
G
1087 &allow-other-keys)
1088 ;; TODO
1089 ;; (apply 'secrets-create-item (auth-get-source entry) name passwd spec)
1090 (debug spec))
1091
1092;;; older API
1093
1094;;; (auth-source-user-or-password '("login" "password") "imap.myhost.com" t "tzz")
1095
1096;; deprecate the old interface
1097(make-obsolete 'auth-source-user-or-password
1098 'auth-source-search "Emacs 24.1")
1099(make-obsolete 'auth-source-forget-user-or-password
1100 'auth-source-forget "Emacs 24.1")
fb178e4c 1101
0e4966fb 1102(defun auth-source-user-or-password
35123c04
TZ
1103 (mode host port &optional username create-missing delete-existing)
1104 "Find MODE (string or list of strings) matching HOST and PORT.
fb178e4c 1105
b8e0f0cd
G
1106DEPRECATED in favor of `auth-source-search'!
1107
fb178e4c
KY
1108USERNAME is optional and will be used as \"login\" in a search
1109across the Secret Service API (see secrets.el) if the resulting
1110items don't have a username. This means that if you search for
1111username \"joe\" and it matches an item but the item doesn't have
1112a :user attribute, the username \"joe\" will be returned.
1113
0e4966fb
MA
1114A non nil DELETE-EXISTING means deleting any matching password
1115entry in the respective sources. This is useful only when
1116CREATE-MISSING is non nil as well; the intended use case is to
1117remove wrong password entries.
1118
1119If no matching entry is found, and CREATE-MISSING is non nil,
1120the password will be retrieved interactively, and it will be
1121stored in the password database which matches best (see
1122`auth-sources').
1123
1124MODE can be \"login\" or \"password\"."
554a69b8 1125 (auth-source-do-debug
b8e0f0cd 1126 "auth-source-user-or-password: DEPRECATED get %s for %s (%s) + user=%s"
35123c04 1127 mode host port username)
b8e0f0cd 1128
3b36c17e 1129 (let* ((listy (listp mode))
cbabe91f
TZ
1130 (mode (if listy mode (list mode)))
1131 (cname (if username
35123c04
TZ
1132 (format "%s %s:%s %s" mode host port username)
1133 (format "%s %s:%s" mode host port)))
1134 (search (list :host host :port port))
cbabe91f 1135 (search (if username (append search (list :user username)) search))
b8e0f0cd
G
1136 (search (if create-missing
1137 (append search (list :create t))
1138 search))
1139 (search (if delete-existing
1140 (append search (list :delete t))
1141 search))
1142 ;; (found (if (not delete-existing)
1143 ;; (gethash cname auth-source-cache)
1144 ;; (remhash cname auth-source-cache)
1145 ;; nil)))
1146 (found nil))
ed778fad 1147 (if found
cbabe91f
TZ
1148 (progn
1149 (auth-source-do-debug
b8e0f0cd 1150 "auth-source-user-or-password: DEPRECATED cached %s=%s for %s (%s) + %s"
cbabe91f
TZ
1151 mode
1152 ;; don't show the password
b8e0f0cd 1153 (if (and (member "password" mode) t)
cbabe91f
TZ
1154 "SECRET"
1155 found)
35123c04 1156 host port username)
cbabe91f 1157 found) ; return the found data
b8e0f0cd
G
1158 ;; else, if not found, search with a max of 1
1159 (let ((choice (nth 0 (apply 'auth-source-search
1160 (append '(:max 1) search)))))
1161 (when choice
1162 (dolist (m mode)
1163 (cond
1164 ((equal "password" m)
1165 (push (if (plist-get choice :secret)
1166 (funcall (plist-get choice :secret))
1167 nil) found))
1168 ((equal "login" m)
1169 (push (plist-get choice :user) found)))))
1170 (setq found (nreverse found))
1171 (setq found (if listy found (car-safe found)))))
9b3ebcb6 1172
b8e0f0cd 1173 found))
8f7abae3
MB
1174
1175(provide 'auth-source)
1176
8f7abae3 1177;;; auth-source.el ends here