6a8ec8cbdd0e4766d890d80362c0cba1f712c059
[gnus] / lisp / auth-source.el
1 ;;; auth-source.el --- authentication sources for Gnus and Emacs
2
3 ;; Copyright (C) 2008-2015 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 ;; See the auth.info Info documentation for details.
31
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
40 ;;; Code:
41
42 (require 'password-cache)
43 (require 'mm-util)
44 (require 'gnus-util)
45
46 (eval-when-compile (require 'cl))
47 (eval-and-compile
48   (or (ignore-errors (require 'eieio))
49       ;; gnus-fallback-lib/ from gnus/lisp/gnus-fallback-lib
50       (ignore-errors
51         (let ((load-path (cons (expand-file-name
52                                 "gnus-fallback-lib/eieio"
53                                 (file-name-directory (locate-library "gnus")))
54                                load-path)))
55           (require 'eieio)))
56       (error
57        "eieio not found in `load-path' or gnus-fallback-lib/ directory.")))
58
59 (autoload 'secrets-create-item "secrets")
60 (autoload 'secrets-delete-item "secrets")
61 (autoload 'secrets-get-alias "secrets")
62 (autoload 'secrets-get-attributes "secrets")
63 (autoload 'secrets-get-secret "secrets")
64 (autoload 'secrets-list-collections "secrets")
65 (autoload 'secrets-search-items "secrets")
66
67 (autoload 'rfc2104-hash "rfc2104")
68
69 (autoload 'plstore-open "plstore")
70 (autoload 'plstore-find "plstore")
71 (autoload 'plstore-put "plstore")
72 (autoload 'plstore-delete "plstore")
73 (autoload 'plstore-save "plstore")
74 (autoload 'plstore-get-file "plstore")
75
76 (eval-and-compile
77   (autoload 'epg-make-context "epg")
78   (autoload 'epg-context-set-passphrase-callback "epg")
79   (autoload 'epg-decrypt-string "epg")
80   (autoload 'epg-encrypt-string "epg")
81   (autoload 'epg-context-set-armor "epg"))
82
83 (autoload 'help-mode "help-mode" nil t)
84
85 (defvar secrets-enabled)
86
87 (defgroup auth-source nil
88   "Authentication sources."
89   :version "23.1" ;; No Gnus
90   :group 'gnus)
91
92 ;;;###autoload
93 (defcustom auth-source-cache-expiry 7200
94   "How many seconds passwords are cached, or nil to disable
95 expiring.  Overrides `password-cache-expiry' through a
96 let-binding."
97   :version "24.1"
98   :group 'auth-source
99   :type '(choice (const :tag "Never" nil)
100                  (const :tag "All Day" 86400)
101                  (const :tag "2 Hours" 7200)
102                  (const :tag "30 Minutes" 1800)
103                  (integer :tag "Seconds")))
104
105 ;; The slots below correspond with the `auth-source-search' spec,
106 ;; so a backend with :host set, for instance, would match only
107 ;; searches for that host.  Normally they are nil.
108 (defclass auth-source-backend ()
109   ((type :initarg :type
110          :initform 'netrc
111          :type symbol
112          :custom symbol
113          :documentation "The backend type.")
114    (source :initarg :source
115            :type string
116            :custom string
117            :documentation "The backend source.")
118    (host :initarg :host
119          :initform t
120          :type t
121          :custom string
122          :documentation "The backend host.")
123    (user :initarg :user
124          :initform t
125          :type t
126          :custom string
127          :documentation "The backend user.")
128    (port :initarg :port
129          :initform t
130          :type t
131          :custom string
132          :documentation "The backend protocol.")
133    (data :initarg :data
134          :initform nil
135          :documentation "Internal backend data.")
136    (create-function :initarg :create-function
137                     :initform ignore
138                     :type function
139                     :custom function
140                     :documentation "The create function.")
141    (search-function :initarg :search-function
142                     :initform ignore
143                     :type function
144                     :custom function
145                     :documentation "The search function.")))
146
147 (defcustom auth-source-protocols '((imap "imap" "imaps" "143" "993")
148                                    (pop3 "pop3" "pop" "pop3s" "110" "995")
149                                    (ssh  "ssh" "22")
150                                    (sftp "sftp" "115")
151                                    (smtp "smtp" "25"))
152   "List of authentication protocols and their names"
153
154   :group 'auth-source
155   :version "23.2" ;; No Gnus
156   :type '(repeat :tag "Authentication Protocols"
157                  (cons :tag "Protocol Entry"
158                        (symbol :tag "Protocol")
159                        (repeat :tag "Names"
160                                (string :tag "Name")))))
161
162 ;; Generate all the protocols in a format Customize can use.
163 ;; TODO: generate on the fly from auth-source-protocols
164 (defconst auth-source-protocols-customize
165   (mapcar (lambda (a)
166             (let ((p (car-safe a)))
167               (list 'const
168                     :tag (upcase (symbol-name p))
169                     p)))
170           auth-source-protocols))
171
172 (defvar auth-source-creation-defaults nil
173   "Defaults for creating token values.  Usually let-bound.")
174
175 (defvar auth-source-creation-prompts nil
176   "Default prompts for token values.  Usually let-bound.")
177
178 (make-obsolete 'auth-source-hide-passwords nil "Emacs 24.1")
179
180 (defcustom auth-source-save-behavior 'ask
181   "If set, auth-source will respect it for save behavior."
182   :group 'auth-source
183   :version "23.2" ;; No Gnus
184   :type `(choice
185           :tag "auth-source new token save behavior"
186           (const :tag "Always save" t)
187           (const :tag "Never save" nil)
188           (const :tag "Ask" ask)))
189
190 ;; TODO: make the default (setq auth-source-netrc-use-gpg-tokens `((,(if (boundp 'epa-file-auto-mode-alist-entry) (car (symbol-value 'epa-file-auto-mode-alist-entry)) "\\.gpg\\'") never) (t gpg)))
191 ;; TODO: or maybe leave as (setq auth-source-netrc-use-gpg-tokens 'never)
192
193 (defcustom auth-source-netrc-use-gpg-tokens 'never
194   "Set this to tell auth-source when to create GPG password
195 tokens in netrc files.  It's either an alist or `never'.
196 Note that if EPA/EPG is not available, this should NOT be used."
197   :group 'auth-source
198   :version "23.2" ;; No Gnus
199   :type `(choice
200           (const :tag "Always use GPG password tokens" (t gpg))
201           (const :tag "Never use GPG password tokens" never)
202           (repeat :tag "Use a lookup list"
203                   (list
204                    (choice :tag "Matcher"
205                            (const :tag "Match anything" t)
206                            (const :tag "The EPA encrypted file extensions"
207                                   ,(if (boundp 'epa-file-auto-mode-alist-entry)
208                                        (car (symbol-value
209                                              'epa-file-auto-mode-alist-entry))
210                                      "\\.gpg\\'"))
211                            (regexp :tag "Regular expression"))
212                    (choice :tag "What to do"
213                            (const :tag "Save GPG-encrypted password tokens" gpg)
214                            (const :tag "Don't encrypt tokens" never))))))
215
216 (defvar auth-source-magic "auth-source-magic ")
217
218 (defcustom auth-source-do-cache t
219   "Whether auth-source should cache information with `password-cache'."
220   :group 'auth-source
221   :version "23.2" ;; No Gnus
222   :type `boolean)
223
224 (defcustom auth-source-debug nil
225   "Whether auth-source should log debug messages.
226
227 If the value is nil, debug messages are not logged.
228
229 If the value is t, debug messages are logged with `message'.  In
230 that case, your authentication data will be in the clear (except
231 for passwords).
232
233 If the value is a function, debug messages are logged by calling
234  that function using the same arguments as `message'."
235   :group 'auth-source
236   :version "23.2" ;; No Gnus
237   :type `(choice
238           :tag "auth-source debugging mode"
239           (const :tag "Log using `message' to the *Messages* buffer" t)
240           (const :tag "Log all trivia with `message' to the *Messages* buffer"
241                  trivia)
242           (function :tag "Function that takes arguments like `message'")
243           (const :tag "Don't log anything" nil)))
244
245 (defcustom auth-sources '("~/.authinfo" "~/.authinfo.gpg" "~/.netrc")
246   "List of authentication sources.
247 Each entry is the authentication type with optional properties.
248 Entries are tried in the order in which they appear.
249 See Info node `(auth)Help for users' for details.
250
251 If an entry names a file with the \".gpg\" extension and you have
252 EPA/EPG set up, the file will be encrypted and decrypted
253 automatically.  See Info node `(epa)Encrypting/decrypting gpg files'
254 for details.
255
256 It's best to customize this with `\\[customize-variable]' because the choices
257 can get pretty complex."
258   :group 'auth-source
259   :version "24.1" ;; No Gnus
260   :type `(repeat :tag "Authentication Sources"
261                  (choice
262                   (string :tag "Just a file")
263                   (const :tag "Default Secrets API Collection" default)
264                   (const :tag "Login Secrets API Collection" "secrets:Login")
265                   (const :tag "Temp Secrets API Collection" "secrets:session")
266
267                   (const :tag "Default internet Mac OS Keychain"
268                          macos-keychain-internet)
269
270                   (const :tag "Default generic Mac OS Keychain"
271                          macos-keychain-generic)
272
273                   (list :tag "Source definition"
274                         (const :format "" :value :source)
275                         (choice :tag "Authentication backend choice"
276                                 (string :tag "Authentication Source (file)")
277                                 (list
278                                  :tag "Secret Service API/KWallet/GNOME Keyring"
279                                  (const :format "" :value :secrets)
280                                  (choice :tag "Collection to use"
281                                          (string :tag "Collection name")
282                                          (const :tag "Default" default)
283                                          (const :tag "Login" "Login")
284                                          (const
285                                           :tag "Temporary" "session")))
286                                 (list
287                                  :tag "Mac OS internet Keychain"
288                                  (const :format ""
289                                         :value :macos-keychain-internet)
290                                  (choice :tag "Collection to use"
291                                          (string :tag "internet Keychain path")
292                                          (const :tag "default" default)))
293                                 (list
294                                  :tag "Mac OS generic Keychain"
295                                  (const :format ""
296                                         :value :macos-keychain-generic)
297                                  (choice :tag "Collection to use"
298                                          (string :tag "generic Keychain path")
299                                          (const :tag "default" default))))
300                         (repeat :tag "Extra Parameters" :inline t
301                                 (choice :tag "Extra parameter"
302                                         (list
303                                          :tag "Host"
304                                          (const :format "" :value :host)
305                                          (choice :tag "Host (machine) choice"
306                                                  (const :tag "Any" t)
307                                                  (regexp
308                                                   :tag "Regular expression")))
309                                         (list
310                                          :tag "Protocol"
311                                          (const :format "" :value :port)
312                                          (choice
313                                           :tag "Protocol"
314                                           (const :tag "Any" t)
315                                           ,@auth-source-protocols-customize))
316                                         (list :tag "User" :inline t
317                                               (const :format "" :value :user)
318                                               (choice
319                                                :tag "Personality/Username"
320                                                (const :tag "Any" t)
321                                                (string
322                                                 :tag "Name")))))))))
323
324 (defcustom auth-source-gpg-encrypt-to t
325   "List of recipient keys that `authinfo.gpg' encrypted to.
326 If the value is not a list, symmetric encryption will be used."
327   :group 'auth-source
328   :version "24.1" ;; No Gnus
329   :type '(choice (const :tag "Symmetric encryption" t)
330                  (repeat :tag "Recipient public keys"
331                          (string :tag "Recipient public key"))))
332
333 ;; temp for debugging
334 ;; (unintern 'auth-source-protocols)
335 ;; (unintern 'auth-sources)
336 ;; (customize-variable 'auth-sources)
337 ;; (setq auth-sources nil)
338 ;; (format "%S" auth-sources)
339 ;; (customize-variable 'auth-source-protocols)
340 ;; (setq auth-source-protocols nil)
341 ;; (format "%S" auth-source-protocols)
342 ;; (auth-source-pick nil :host "a" :port 'imap)
343 ;; (auth-source-user-or-password "login" "imap.myhost.com" 'imap)
344 ;; (auth-source-user-or-password "password" "imap.myhost.com" 'imap)
345 ;; (auth-source-user-or-password-imap "login" "imap.myhost.com")
346 ;; (auth-source-user-or-password-imap "password" "imap.myhost.com")
347 ;; (auth-source-protocol-defaults 'imap)
348
349 ;; (let ((auth-source-debug 'debug)) (auth-source-do-debug "hello"))
350 ;; (let ((auth-source-debug t)) (auth-source-do-debug "hello"))
351 ;; (let ((auth-source-debug nil)) (auth-source-do-debug "hello"))
352 (defun auth-source-do-debug (&rest msg)
353   (when auth-source-debug
354     (apply #'auth-source-do-warn msg)))
355
356 (defun auth-source-do-trivia (&rest msg)
357   (when (or (eq auth-source-debug 'trivia)
358             (functionp auth-source-debug))
359     (apply #'auth-source-do-warn msg)))
360
361 (defun auth-source-do-warn (&rest msg)
362   (apply
363    ;; set logger to either the function in auth-source-debug or 'message
364    ;; note that it will be 'message if auth-source-debug is nil
365    (if (functionp auth-source-debug)
366        auth-source-debug
367      'message)
368    msg))
369
370
371 ;; (auth-source-read-char-choice "enter choice? " '(?a ?b ?q))
372 (defun auth-source-read-char-choice (prompt choices)
373   "Read one of CHOICES by `read-char-choice', or `read-char'.
374 `dropdown-list' support is disabled because it doesn't work reliably.
375 Only one of CHOICES will be returned.  The PROMPT is augmented
376 with \"[a/b/c] \" if CHOICES is \(?a ?b ?c)."
377   (when choices
378     (let* ((prompt-choices
379             (apply #'concat (loop for c in choices
380                                  collect (format "%c/" c))))
381            (prompt-choices (concat "[" (substring prompt-choices 0 -1) "] "))
382            (full-prompt (concat prompt prompt-choices))
383            k)
384
385       (while (not (memq k choices))
386         (setq k (cond
387                  ((fboundp 'read-char-choice)
388                   (read-char-choice full-prompt choices))
389                  (t (message "%s" full-prompt)
390                     (setq k (read-char))))))
391       k)))
392
393 ;; (auth-source-pick nil :host "any" :port 'imap :user "joe")
394 ;; (auth-source-pick t :host "any" :port 'imap :user "joe")
395 ;; (setq auth-sources '((:source (:secrets default) :host t :port t :user "joe")
396 ;;                   (:source (:secrets "session") :host t :port t :user "joe")
397 ;;                   (:source (:secrets "Login") :host t :port t)
398 ;;                   (:source "~/.authinfo.gpg" :host t :port t)))
399
400 ;; (setq auth-sources '((:source (:secrets default) :host t :port t :user "joe")
401 ;;                   (:source (:secrets "session") :host t :port t :user "joe")
402 ;;                   (:source (:secrets "Login") :host t :port t)
403 ;;                   ))
404
405 ;; (setq auth-sources '((:source "~/.authinfo.gpg" :host t :port t)))
406
407 ;; (auth-source-backend-parse "myfile.gpg")
408 ;; (auth-source-backend-parse 'default)
409 ;; (auth-source-backend-parse "secrets:Login")
410 ;; (auth-source-backend-parse 'macos-keychain-internet)
411 ;; (auth-source-backend-parse 'macos-keychain-generic)
412 ;; (auth-source-backend-parse "macos-keychain-internet:/path/here.keychain")
413 ;; (auth-source-backend-parse "macos-keychain-generic:/path/here.keychain")
414
415 (defun auth-source-backend-parse (entry)
416   "Creates an auth-source-backend from an ENTRY in `auth-sources'."
417   (auth-source-backend-parse-parameters
418    entry
419    (cond
420     ;; take 'default and recurse to get it as a Secrets API default collection
421     ;; matching any user, host, and protocol
422     ((eq entry 'default)
423      (auth-source-backend-parse '(:source (:secrets default))))
424     ;; take secrets:XYZ and recurse to get it as Secrets API collection "XYZ"
425     ;; matching any user, host, and protocol
426     ((and (stringp entry) (string-match "^secrets:\\(.+\\)" entry))
427      (auth-source-backend-parse `(:source (:secrets ,(match-string 1 entry)))))
428
429     ;; take 'macos-keychain-internet and recurse to get it as a Mac OS
430     ;; Keychain collection matching any user, host, and protocol
431     ((eq entry 'macos-keychain-internet)
432      (auth-source-backend-parse '(:source (:macos-keychain-internet default))))
433     ;; take 'macos-keychain-generic and recurse to get it as a Mac OS
434     ;; Keychain collection matching any user, host, and protocol
435     ((eq entry 'macos-keychain-generic)
436      (auth-source-backend-parse '(:source (:macos-keychain-generic default))))
437     ;; take macos-keychain-internet:XYZ and recurse to get it as MacOS
438     ;; Keychain "XYZ" matching any user, host, and protocol
439     ((and (stringp entry) (string-match "^macos-keychain-internet:\\(.+\\)"
440                                         entry))
441      (auth-source-backend-parse `(:source (:macos-keychain-internet
442                                            ,(match-string 1 entry)))))
443     ;; take macos-keychain-generic:XYZ and recurse to get it as MacOS
444     ;; Keychain "XYZ" matching any user, host, and protocol
445     ((and (stringp entry) (string-match "^macos-keychain-generic:\\(.+\\)"
446                                         entry))
447      (auth-source-backend-parse `(:source (:macos-keychain-generic
448                                            ,(match-string 1 entry)))))
449
450     ;; take just a file name and recurse to get it as a netrc file
451     ;; matching any user, host, and protocol
452     ((stringp entry)
453      (auth-source-backend-parse `(:source ,entry)))
454
455     ;; a file name with parameters
456     ((stringp (plist-get entry :source))
457      (if (equal (file-name-extension (plist-get entry :source)) "plist")
458          (auth-source-backend
459           (plist-get entry :source)
460           :source (plist-get entry :source)
461           :type 'plstore
462           :search-function 'auth-source-plstore-search
463           :create-function 'auth-source-plstore-create
464           :data (plstore-open (plist-get entry :source)))
465        (auth-source-backend
466         (plist-get entry :source)
467         :source (plist-get entry :source)
468         :type 'netrc
469         :search-function 'auth-source-netrc-search
470         :create-function 'auth-source-netrc-create)))
471
472     ;; the MacOS Keychain
473     ((and
474       (not (null (plist-get entry :source))) ; the source must not be nil
475       (listp (plist-get entry :source))      ; and it must be a list
476       (or
477        (plist-get (plist-get entry :source) :macos-keychain-generic)
478        (plist-get (plist-get entry :source) :macos-keychain-internet)))
479
480      (let* ((source-spec (plist-get entry :source))
481             (keychain-generic (plist-get source-spec :macos-keychain-generic))
482             (keychain-type (if keychain-generic
483                                'macos-keychain-generic
484                              'macos-keychain-internet))
485             (source (plist-get source-spec (if keychain-generic
486                                                :macos-keychain-generic
487                                              :macos-keychain-internet))))
488
489        (when (symbolp source)
490          (setq source (symbol-name source)))
491
492        (auth-source-backend
493         (format "Mac OS Keychain (%s)" source)
494         :source source
495         :type keychain-type
496         :search-function 'auth-source-macos-keychain-search
497         :create-function 'auth-source-macos-keychain-create)))
498
499     ;; the Secrets API.  We require the package, in order to have a
500     ;; defined value for `secrets-enabled'.
501     ((and
502       (not (null (plist-get entry :source))) ; the source must not be nil
503       (listp (plist-get entry :source))      ; and it must be a list
504       (require 'secrets nil t)               ; and we must load the Secrets API
505       secrets-enabled)                       ; and that API must be enabled
506
507      ;; the source is either the :secrets key in ENTRY or
508      ;; if that's missing or nil, it's "session"
509      (let ((source (or (plist-get (plist-get entry :source) :secrets)
510                        "session")))
511
512        ;; if the source is a symbol, we look for the alias named so,
513        ;; and if that alias is missing, we use "Login"
514        (when (symbolp source)
515          (setq source (or (secrets-get-alias (symbol-name source))
516                           "Login")))
517
518        (if (featurep 'secrets)
519            (auth-source-backend
520             (format "Secrets API (%s)" source)
521             :source source
522             :type 'secrets
523             :search-function 'auth-source-secrets-search
524             :create-function 'auth-source-secrets-create)
525          (auth-source-do-warn
526           "auth-source-backend-parse: no Secrets API, ignoring spec: %S" entry)
527          (auth-source-backend
528           (format "Ignored Secrets API (%s)" source)
529           :source ""
530           :type 'ignore))))
531
532     ;; none of them
533     (t
534      (auth-source-do-warn
535       "auth-source-backend-parse: invalid backend spec: %S" entry)
536      (auth-source-backend
537       "Empty"
538       :source ""
539       :type 'ignore)))))
540
541 (defun auth-source-backend-parse-parameters (entry backend)
542   "Fills in the extra auth-source-backend parameters of ENTRY.
543 Using the plist ENTRY, get the :host, :port, and :user search
544 parameters."
545   (let ((entry (if (stringp entry)
546                    nil
547                  entry))
548         val)
549     (when (setq val (plist-get entry :host))
550       (oset backend host val))
551     (when (setq val (plist-get entry :user))
552       (oset backend user val))
553     (when (setq val (plist-get entry :port))
554       (oset backend port val)))
555   backend)
556
557 ;; (mapcar 'auth-source-backend-parse auth-sources)
558
559 (defun* auth-source-search (&rest spec
560                                   &key type max host user port secret
561                                   require create delete
562                                   &allow-other-keys)
563   "Search or modify authentication backends according to SPEC.
564
565 This function parses `auth-sources' for matches of the SPEC
566 plist.  It can optionally create or update an authentication
567 token if requested.  A token is just a standard Emacs property
568 list with a :secret property that can be a function; all the
569 other properties will always hold scalar values.
570
571 Typically the :secret property, if present, contains a password.
572
573 Common search keys are :max, :host, :port, and :user.  In
574 addition, :create specifies if and how tokens will be created.
575 Finally, :type can specify which backend types you want to check.
576
577 A string value is always matched literally.  A symbol is matched
578 as its string value, literally.  All the SPEC values can be
579 single values (symbol or string) or lists thereof (in which case
580 any of the search terms matches).
581
582 :create t means to create a token if possible.
583
584 A new token will be created if no matching tokens were found.
585 The new token will have only the keys the backend requires.  For
586 the netrc backend, for instance, that's the user, host, and
587 port keys.
588
589 Here's an example:
590
591 \(let ((auth-source-creation-defaults \\='((user . \"defaultUser\")
592                                         (A    . \"default A\"))))
593   (auth-source-search :host \"mine\" :type \\='netrc :max 1
594                       :P \"pppp\" :Q \"qqqq\"
595                       :create t))
596
597 which says:
598
599 \"Search for any entry matching host `mine' in backends of type
600  `netrc', maximum one result.
601
602  Create a new entry if you found none.  The netrc backend will
603  automatically require host, user, and port.  The host will be
604  `mine'.  We prompt for the user with default `defaultUser' and
605  for the port without a default.  We will not prompt for A, Q,
606  or P.  The resulting token will only have keys user, host, and
607  port.\"
608
609 :create \\='(A B C) also means to create a token if possible.
610
611 The behavior is like :create t but if the list contains any
612 parameter, that parameter will be required in the resulting
613 token.  The value for that parameter will be obtained from the
614 search parameters or from user input.  If any queries are needed,
615 the alist `auth-source-creation-defaults' will be checked for the
616 default value.  If the user, host, or port are missing, the alist
617 `auth-source-creation-prompts' will be used to look up the
618 prompts IN THAT ORDER (so the `user' prompt will be queried first,
619 then `host', then `port', and finally `secret').  Each prompt string
620 can use %u, %h, and %p to show the user, host, and port.
621
622 Here's an example:
623
624 \(let ((auth-source-creation-defaults \\='((user . \"defaultUser\")
625                                         (A    . \"default A\")))
626        (auth-source-creation-prompts
627         \\='((password . \"Enter IMAP password for %h:%p: \"))))
628   (auth-source-search :host \\='(\"nonesuch\" \"twosuch\") :type \\='netrc :max 1
629                       :P \"pppp\" :Q \"qqqq\"
630                       :create \\='(A B Q)))
631
632 which says:
633
634 \"Search for any entry matching host `nonesuch'
635  or `twosuch' in backends of type `netrc', maximum one result.
636
637  Create a new entry if you found none.  The netrc backend will
638  automatically require host, user, and port.  The host will be
639  `nonesuch' and Q will be `qqqq'.  We prompt for the password
640  with the shown prompt.  We will not prompt for Q.  The resulting
641  token will have keys user, host, port, A, B, and Q.  It will not
642  have P with any value, even though P is used in the search to
643  find only entries that have P set to `pppp'.\"
644
645 When multiple values are specified in the search parameter, the
646 user is prompted for which one.  So :host (X Y Z) would ask the
647 user to choose between X, Y, and Z.
648
649 This creation can fail if the search was not specific enough to
650 create a new token (it's up to the backend to decide that).  You
651 should `catch' the backend-specific error as usual.  Some
652 backends (netrc, at least) will prompt the user rather than throw
653 an error.
654
655 :require (A B C) means that only results that contain those
656 tokens will be returned.  Thus for instance requiring :secret
657 will ensure that any results will actually have a :secret
658 property.
659
660 :delete t means to delete any found entries.  nil by default.
661 Use `auth-source-delete' in ELisp code instead of calling
662 `auth-source-search' directly with this parameter.
663
664 :type (X Y Z) will check only those backend types.  `netrc' and
665 `secrets' are the only ones supported right now.
666
667 :max N means to try to return at most N items (defaults to 1).
668 More than N items may be returned, depending on the search and
669 the backend.
670
671 When :max is 0 the function will return just t or nil to indicate
672 if any matches were found.
673
674 :host (X Y Z) means to match only hosts X, Y, or Z according to
675 the match rules above.  Defaults to t.
676
677 :user (X Y Z) means to match only users X, Y, or Z according to
678 the match rules above.  Defaults to t.
679
680 :port (P Q R) means to match only protocols P, Q, or R.
681 Defaults to t.
682
683 :K (V1 V2 V3) for any other key K will match values V1, V2, or
684 V3 (note the match rules above).
685
686 The return value is a list with at most :max tokens.  Each token
687 is a plist with keys :backend :host :port :user, plus any other
688 keys provided by the backend (notably :secret).  But note the
689 exception for :max 0, which see above.
690
691 The token can hold a :save-function key.  If you call that, the
692 user will be prompted to save the data to the backend.  You can't
693 request that this should happen right after creation, because
694 `auth-source-search' has no way of knowing if the token is
695 actually useful.  So the caller must arrange to call this function.
696
697 The token's :secret key can hold a function.  In that case you
698 must call it to obtain the actual value."
699   (let* ((backends (mapcar #'auth-source-backend-parse auth-sources))
700          (max (or max 1))
701          (ignored-keys '(:require :create :delete :max))
702          (keys (loop for i below (length spec) by 2
703                      unless (memq (nth i spec) ignored-keys)
704                      collect (nth i spec)))
705          (cached (auth-source-remembered-p spec))
706          ;; note that we may have cached results but found is still nil
707          ;; (there were no results from the search)
708          (found (auth-source-recall spec))
709          filtered-backends accessor-key backend)
710
711     (if (and cached auth-source-do-cache)
712         (auth-source-do-debug
713          "auth-source-search: found %d CACHED results matching %S"
714          (length found) spec)
715
716       (assert
717        (or (eq t create) (listp create)) t
718        "Invalid auth-source :create parameter (must be t or a list): %s %s")
719
720       (assert
721        (listp require) t
722        "Invalid auth-source :require parameter (must be a list): %s")
723
724       (setq filtered-backends (copy-sequence backends))
725       (dolist (backend backends)
726         (dolist (key keys)
727           ;; ignore invalid slots
728           (condition-case signal
729               (unless (eval `(auth-source-search-collection
730                               (plist-get spec key)
731                               (oref backend ,key)))
732                 (setq filtered-backends (delq backend filtered-backends))
733                 (return))
734             (invalid-slot-name))))
735
736       (auth-source-do-trivia
737        "auth-source-search: found %d backends matching %S"
738        (length filtered-backends) spec)
739
740       ;; (debug spec "filtered" filtered-backends)
741       ;; First go through all the backends without :create, so we can
742       ;; query them all.
743       (setq found (auth-source-search-backends filtered-backends
744                                                spec
745                                                ;; to exit early
746                                                max
747                                                ;; create is always nil here
748                                                nil delete
749                                                require))
750
751       (auth-source-do-debug
752        "auth-source-search: found %d results (max %d) matching %S"
753        (length found) max spec)
754
755       ;; If we didn't find anything, then we allow the backend(s) to
756       ;; create the entries.
757       (when (and create
758                  (not found))
759         (setq found (auth-source-search-backends filtered-backends
760                                                  spec
761                                                  ;; to exit early
762                                                  max
763                                                  create delete
764                                                  require))
765         (auth-source-do-debug
766          "auth-source-search: CREATED %d results (max %d) matching %S"
767          (length found) max spec))
768
769       ;; note we remember the lack of result too, if it's applicable
770       (when auth-source-do-cache
771         (auth-source-remember spec found)))
772
773     (if (zerop max)
774         (not (null found))
775       found)))
776
777 (defun auth-source-search-backends (backends spec max create delete require)
778   (let ((max (if (zerop max) 1 max)) ; stop with 1 match if we're asked for zero
779         matches)
780     (dolist (backend backends)
781       (when (> max (length matches)) ; if we need more matches...
782         (let* ((bmatches (apply
783                           (slot-value backend 'search-function)
784                           :backend backend
785                           :type (slot-value backend :type)
786                           ;; note we're overriding whatever the spec
787                           ;; has for :max, :require, :create, and :delete
788                           :max max
789                           :require require
790                           :create create
791                           :delete delete
792                           spec)))
793           (when bmatches
794             (auth-source-do-trivia
795              "auth-source-search-backend: got %d (max %d) in %s:%s matching %S"
796              (length bmatches) max
797              (slot-value backend :type)
798              (slot-value backend :source)
799              spec)
800             (setq matches (append matches bmatches))))))
801     matches))
802
803 ;; (auth-source-search :max 0)
804 ;; (auth-source-search :max 1)
805 ;; (funcall (plist-get (nth 0 (auth-source-search :max 1)) :secret))
806 ;; (auth-source-search :host "nonesuch" :type 'netrc :K 1)
807 ;; (auth-source-search :host "nonesuch" :type 'secrets)
808
809 (defun* auth-source-delete (&rest spec
810                                   &key delete
811                                   &allow-other-keys)
812   "Delete entries from the authentication backends according to SPEC.
813 Calls `auth-source-search' with the :delete property in SPEC set to t.
814 The backend may not actually delete the entries.
815
816 Returns the deleted entries."
817   (auth-source-search (plist-put spec :delete t)))
818
819 (defun auth-source-search-collection (collection value)
820   "Returns t is VALUE is t or COLLECTION is t or COLLECTION contains VALUE."
821   (when (and (atom collection) (not (eq t collection)))
822     (setq collection (list collection)))
823
824   ;; (debug :collection collection :value value)
825   (or (eq collection t)
826       (eq value t)
827       (equal collection value)
828       (member value collection)))
829
830 (defvar auth-source-netrc-cache nil)
831
832 (defun auth-source-forget-all-cached ()
833   "Forget all cached auth-source data."
834   (interactive)
835   (loop for sym being the symbols of password-data
836         ;; when the symbol name starts with auth-source-magic
837         when (string-match (concat "^" auth-source-magic)
838                            (symbol-name sym))
839         ;; remove that key
840         do (password-cache-remove (symbol-name sym)))
841   (setq auth-source-netrc-cache nil))
842
843 (defun auth-source-format-cache-entry (spec)
844   "Format SPEC entry to put it in the password cache."
845   (concat auth-source-magic (format "%S" spec)))
846
847 (defun auth-source-remember (spec found)
848   "Remember FOUND search results for SPEC."
849   (let ((password-cache-expiry auth-source-cache-expiry))
850     (password-cache-add
851      (auth-source-format-cache-entry spec) found)))
852
853 (defun auth-source-recall (spec)
854   "Recall FOUND search results for SPEC."
855   (password-read-from-cache (auth-source-format-cache-entry spec)))
856
857 (defun auth-source-remembered-p (spec)
858   "Check if SPEC is remembered."
859   (password-in-cache-p
860    (auth-source-format-cache-entry spec)))
861
862 (defun auth-source-forget (spec)
863   "Forget any cached data matching SPEC exactly.
864
865 This is the same SPEC you passed to `auth-source-search'.
866 Returns t or nil for forgotten or not found."
867   (password-cache-remove (auth-source-format-cache-entry spec)))
868
869 ;; (loop for sym being the symbols of password-data when (string-match (concat "^" auth-source-magic) (symbol-name sym)) collect (symbol-name sym))
870
871 ;; (auth-source-remember '(:host "wedd") '(4 5 6))
872 ;; (auth-source-remembered-p '(:host "wedd"))
873 ;; (auth-source-remember '(:host "xedd") '(1 2 3))
874 ;; (auth-source-remembered-p '(:host "xedd"))
875 ;; (auth-source-remembered-p '(:host "zedd"))
876 ;; (auth-source-recall '(:host "xedd"))
877 ;; (auth-source-recall '(:host t))
878 ;; (auth-source-forget+ :host t)
879
880 (defun* auth-source-forget+ (&rest spec &allow-other-keys)
881   "Forget any cached data matching SPEC.  Returns forgotten count.
882
883 This is not a full `auth-source-search' spec but works similarly.
884 For instance, \(:host \"myhost\" \"yourhost\") would find all the
885 cached data that was found with a search for those two hosts,
886 while \(:host t) would find all host entries."
887   (let ((count 0)
888         sname)
889     (loop for sym being the symbols of password-data
890           ;; when the symbol name matches with auth-source-magic
891           when (and (setq sname (symbol-name sym))
892                     (string-match (concat "^" auth-source-magic "\\(.+\\)")
893                                   sname)
894                     ;; and the spec matches what was stored in the cache
895                     (auth-source-specmatchp spec (read (match-string 1 sname))))
896           ;; remove that key
897           do (progn
898                (password-cache-remove sname)
899                (incf count)))
900     count))
901
902 (defun auth-source-specmatchp (spec stored)
903   (let ((keys (loop for i below (length spec) by 2
904                     collect (nth i spec))))
905     (not (eq
906           (dolist (key keys)
907             (unless (auth-source-search-collection (plist-get stored key)
908                                                    (plist-get spec key))
909               (return 'no)))
910           'no))))
911
912 ;; (auth-source-pick-first-password :host "z.lifelogs.com")
913 ;; (auth-source-pick-first-password :port "imap")
914 (defun auth-source-pick-first-password (&rest spec)
915   "Pick the first secret found from applying SPEC to `auth-source-search'."
916   (let* ((result (nth 0 (apply #'auth-source-search (plist-put spec :max 1))))
917          (secret (plist-get result :secret)))
918
919     (if (functionp secret)
920         (funcall secret)
921       secret)))
922
923 ;; (auth-source-format-prompt "test %u %h %p" '((?u "user") (?h "host")))
924 (defun auth-source-format-prompt (prompt alist)
925   "Format PROMPT using %x (for any character x) specifiers in ALIST."
926   (dolist (cell alist)
927     (let ((c (nth 0 cell))
928           (v (nth 1 cell)))
929       (when (and c v)
930         (setq prompt (replace-regexp-in-string (format "%%%c" c)
931                                                (format "%s" v)
932                                                prompt nil t)))))
933   prompt)
934
935 (defun auth-source-ensure-strings (values)
936   (unless (listp values)
937     (setq values (list values)))
938   (mapcar (lambda (value)
939             (if (numberp value)
940                 (format "%s" value)
941               value))
942           values))
943
944 ;;; Backend specific parsing: netrc/authinfo backend
945
946 (defun auth-source--aput-1 (alist key val)
947   (let ((seen ())
948         (rest alist))
949     (while (and (consp rest) (not (equal key (caar rest))))
950       (push (pop rest) seen))
951     (cons (cons key val)
952           (if (null rest) alist
953             (nconc (nreverse seen)
954                    (if (equal key (caar rest)) (cdr rest) rest))))))
955 (defmacro auth-source--aput (var key val)
956   `(setq ,var (auth-source--aput-1 ,var ,key ,val)))
957
958 (defun auth-source--aget (alist key)
959   (cdr (assoc key alist)))
960
961 ;; (auth-source-netrc-parse :file "~/.authinfo.gpg")
962 (defun* auth-source-netrc-parse (&rest
963                                  spec
964                                  &key file max host user port delete require
965                                  &allow-other-keys)
966   "Parse FILE and return a list of all entries in the file.
967 Note that the MAX parameter is used so we can exit the parse early."
968   (if (listp file)
969       ;; We got already parsed contents; just return it.
970       file
971     (when (file-exists-p file)
972       (setq port (auth-source-ensure-strings port))
973       (with-temp-buffer
974         (let* ((max (or max 5000))       ; sanity check: default to stop at 5K
975                (modified 0)
976                (cached (cdr-safe (assoc file auth-source-netrc-cache)))
977                (cached-mtime (plist-get cached :mtime))
978                (cached-secrets (plist-get cached :secret))
979                (check (lambda(alist)
980                         (and alist
981                              (auth-source-search-collection
982                               host
983                               (or
984                                (auth-source--aget alist "machine")
985                                (auth-source--aget alist "host")
986                                t))
987                              (auth-source-search-collection
988                               user
989                               (or
990                                (auth-source--aget alist "login")
991                                (auth-source--aget alist "account")
992                                (auth-source--aget alist "user")
993                                t))
994                              (auth-source-search-collection
995                               port
996                               (or
997                                (auth-source--aget alist "port")
998                                (auth-source--aget alist "protocol")
999                                t))
1000                              (or
1001                               ;; the required list of keys is nil, or
1002                               (null require)
1003                               ;; every element of require is in n(ormalized)
1004                               (let ((n (nth 0 (auth-source-netrc-normalize
1005                                                (list alist) file))))
1006                                 (loop for req in require
1007                                       always (plist-get n req)))))))
1008                result)
1009
1010           (if (and (functionp cached-secrets)
1011                    (equal cached-mtime
1012                           (nth 5 (file-attributes file))))
1013               (progn
1014                 (auth-source-do-trivia
1015                  "auth-source-netrc-parse: using CACHED file data for %s"
1016                  file)
1017                 (insert (funcall cached-secrets)))
1018             (insert-file-contents file)
1019             ;; cache all netrc files (used to be just .gpg files)
1020             ;; Store the contents of the file heavily encrypted in memory.
1021             ;; (note for the irony-impaired: they are just obfuscated)
1022             (auth-source--aput
1023              auth-source-netrc-cache file
1024              (list :mtime (nth 5 (file-attributes file))
1025                    :secret (lexical-let ((v (mapcar #'1+ (buffer-string))))
1026                              (lambda () (apply #'string (mapcar #'1- v)))))))
1027           (goto-char (point-min))
1028           (let ((entries (auth-source-netrc-parse-entries check max))
1029                 alist)
1030             (while (setq alist (pop entries))
1031                 (push (nreverse alist) result)))
1032
1033           (when (< 0 modified)
1034             (when auth-source-gpg-encrypt-to
1035               ;; (see bug#7487) making `epa-file-encrypt-to' local to
1036               ;; this buffer lets epa-file skip the key selection query
1037               ;; (see the `local-variable-p' check in
1038               ;; `epa-file-write-region').
1039               (unless (local-variable-p 'epa-file-encrypt-to (current-buffer))
1040                 (make-local-variable 'epa-file-encrypt-to))
1041               (if (listp auth-source-gpg-encrypt-to)
1042                   (setq epa-file-encrypt-to auth-source-gpg-encrypt-to)))
1043
1044             ;; ask AFTER we've successfully opened the file
1045             (when (y-or-n-p (format "Save file %s? (%d deletions)"
1046                                     file modified))
1047               (write-region (point-min) (point-max) file nil 'silent)
1048               (auth-source-do-debug
1049                "auth-source-netrc-parse: modified %d lines in %s"
1050                modified file)))
1051
1052           (nreverse result))))))
1053
1054 (defun auth-source-netrc-parse-next-interesting ()
1055   "Advance to the next interesting position in the current buffer."
1056   ;; If we're looking at a comment or are at the end of the line, move forward
1057   (while (or (looking-at "#")
1058              (and (eolp)
1059                   (not (eobp))))
1060     (forward-line 1))
1061   (skip-chars-forward "\t "))
1062
1063 (defun auth-source-netrc-parse-one ()
1064   "Read one thing from the current buffer."
1065   (auth-source-netrc-parse-next-interesting)
1066
1067   (when (or (looking-at "'\\([^']*\\)'")
1068             (looking-at "\"\\([^\"]*\\)\"")
1069             (looking-at "\\([^ \t\n]+\\)"))
1070     (forward-char (length (match-string 0)))
1071     (auth-source-netrc-parse-next-interesting)
1072     (match-string-no-properties 1)))
1073
1074 ;; with thanks to org-mode
1075 (defsubst auth-source-current-line (&optional pos)
1076   (save-excursion
1077     (and pos (goto-char pos))
1078     ;; works also in narrowed buffer, because we start at 1, not point-min
1079     (+ (if (bolp) 1 0) (count-lines 1 (point)))))
1080
1081 (defun auth-source-netrc-parse-entries(check max)
1082   "Parse up to MAX netrc entries, passed by CHECK, from the current buffer."
1083   (let ((adder (lambda(check alist all)
1084                  (when (and
1085                         alist
1086                         (> max (length all))
1087                         (funcall check alist))
1088                    (push alist all))
1089                  all))
1090         item item2 all alist default)
1091     (while (setq item (auth-source-netrc-parse-one))
1092       (setq default (equal item "default"))
1093       ;; We're starting a new machine.  Save the old one.
1094       (when (and alist
1095                  (or default
1096                      (equal item "machine")))
1097         ;; (auth-source-do-trivia
1098         ;;  "auth-source-netrc-parse-entries: got entry %S" alist)
1099         (setq all (funcall adder check alist all)
1100               alist nil))
1101       ;; In default entries, we don't have a next token.
1102       ;; We store them as ("machine" . t)
1103       (if default
1104           (push (cons "machine" t) alist)
1105         ;; Not a default entry.  Grab the next item.
1106         (when (setq item2 (auth-source-netrc-parse-one))
1107           ;; Did we get a "machine" value?
1108           (if (equal item2 "machine")
1109               (progn
1110                 (gnus-error 1
1111                  "%s: Unexpected `machine' token at line %d"
1112                  "auth-source-netrc-parse-entries"
1113                  (auth-source-current-line))
1114                 (forward-line 1))
1115             (push (cons item item2) alist)))))
1116
1117     ;; Clean up: if there's an entry left over, use it.
1118     (when alist
1119       (setq all (funcall adder check alist all))
1120       ;; (auth-source-do-trivia
1121       ;;  "auth-source-netrc-parse-entries: got2 entry %S" alist)
1122       )
1123     (nreverse all)))
1124
1125 (defvar auth-source-passphrase-alist nil)
1126
1127 (defun auth-source-token-passphrase-callback-function (context key-id file)
1128   (let* ((file (file-truename file))
1129          (entry (assoc file auth-source-passphrase-alist))
1130          passphrase)
1131     ;; return the saved passphrase, calling a function if needed
1132     (or (copy-sequence (if (functionp (cdr entry))
1133                            (funcall (cdr entry))
1134                          (cdr entry)))
1135         (progn
1136           (unless entry
1137             (setq entry (list file))
1138             (push entry auth-source-passphrase-alist))
1139           (setq passphrase
1140                 (read-passwd
1141                  (format "Passphrase for %s tokens: " file)
1142                  t))
1143           (setcdr entry (lexical-let ((p (copy-sequence passphrase)))
1144                           (lambda () p)))
1145           passphrase))))
1146
1147 ;; (auth-source-epa-extract-gpg-token "gpg:LS0tLS1CRUdJTiBQR1AgTUVTU0FHRS0tLS0tClZlcnNpb246IEdudVBHIHYxLjQuMTEgKEdOVS9MaW51eCkKCmpBMEVBd01DT25qMjB1ak9rZnRneVI3K21iNm9aZWhuLzRad3cySkdlbnVaKzRpeEswWDY5di9icDI1U1dsQT0KPS9yc2wKLS0tLS1FTkQgUEdQIE1FU1NBR0UtLS0tLQo=" "~/.netrc")
1148 (defun auth-source-epa-extract-gpg-token (secret file)
1149   "Pass either the decoded SECRET or the gpg:BASE64DATA version.
1150 FILE is the file from which we obtained this token."
1151   (when (string-match "^gpg:\\(.+\\)" secret)
1152     (setq secret (base64-decode-string (match-string 1 secret))))
1153   (let ((context (epg-make-context 'OpenPGP))
1154         plain)
1155     (epg-context-set-passphrase-callback
1156      context
1157      (cons #'auth-source-token-passphrase-callback-function
1158            file))
1159     (epg-decrypt-string context secret)))
1160
1161 ;; (insert (auth-source-epa-make-gpg-token "mysecret" "~/.netrc"))
1162 (defun auth-source-epa-make-gpg-token (secret file)
1163   (let ((context (epg-make-context 'OpenPGP))
1164         (pp-escape-newlines nil)
1165         cipher)
1166     (epg-context-set-armor context t)
1167     (epg-context-set-passphrase-callback
1168      context
1169      (cons #'auth-source-token-passphrase-callback-function
1170            file))
1171     (setq cipher (epg-encrypt-string context secret nil))
1172     (with-temp-buffer
1173       (insert cipher)
1174       (base64-encode-region (point-min) (point-max) t)
1175       (concat "gpg:" (buffer-substring-no-properties
1176                       (point-min)
1177                       (point-max))))))
1178
1179 (defun auth-source-netrc-normalize (alist filename)
1180   (mapcar (lambda (entry)
1181             (let (ret item)
1182               (while (setq item (pop entry))
1183                 (let ((k (car item))
1184                       (v (cdr item)))
1185
1186                   ;; apply key aliases
1187                   (setq k (cond ((member k '("machine")) "host")
1188                                 ((member k '("login" "account")) "user")
1189                                 ((member k '("protocol")) "port")
1190                                 ((member k '("password")) "secret")
1191                                 (t k)))
1192
1193                   ;; send back the secret in a function (lexical binding)
1194                   (when (equal k "secret")
1195                     (setq v (lexical-let ((lexv v)
1196                                           (token-decoder nil))
1197                               (when (string-match "^gpg:" lexv)
1198                                 ;; it's a GPG token: create a token decoder
1199                                 ;; which unsets itself once
1200                                 (setq token-decoder
1201                                       (lambda (val)
1202                                         (prog1
1203                                             (auth-source-epa-extract-gpg-token
1204                                              val
1205                                              filename)
1206                                           (setq token-decoder nil)))))
1207                               (lambda ()
1208                                 (when token-decoder
1209                                   (setq lexv (funcall token-decoder lexv)))
1210                                 lexv))))
1211                   (setq ret (plist-put ret
1212                                        (intern (concat ":" k))
1213                                        v))))
1214               ret))
1215           alist))
1216
1217 ;; (setq secret (plist-get (nth 0 (auth-source-search :host t :type 'netrc :K 1 :max 1)) :secret))
1218 ;; (funcall secret)
1219
1220 (defun* auth-source-netrc-search (&rest
1221                                   spec
1222                                   &key backend require create delete
1223                                   type max host user port
1224                                   &allow-other-keys)
1225   "Given a property list SPEC, return search matches from the :backend.
1226 See `auth-source-search' for details on SPEC."
1227   ;; just in case, check that the type is correct (null or same as the backend)
1228   (assert (or (null type) (eq type (oref backend type)))
1229           t "Invalid netrc search: %s %s")
1230
1231   (let ((results (auth-source-netrc-normalize
1232                   (auth-source-netrc-parse
1233                    :max max
1234                    :require require
1235                    :delete delete
1236                    :file (oref backend source)
1237                    :host (or host t)
1238                    :user (or user t)
1239                    :port (or port t))
1240                   (oref backend source))))
1241
1242     ;; if we need to create an entry AND none were found to match
1243     (when (and create
1244                (not results))
1245
1246       ;; create based on the spec and record the value
1247       (setq results (or
1248                      ;; if the user did not want to create the entry
1249                      ;; in the file, it will be returned
1250                      (apply (slot-value backend 'create-function) spec)
1251                      ;; if not, we do the search again without :create
1252                      ;; to get the updated data.
1253
1254                      ;; the result will be returned, even if the search fails
1255                      (apply #'auth-source-netrc-search
1256                             (plist-put spec :create nil)))))
1257     results))
1258
1259 (defun auth-source-netrc-element-or-first (v)
1260   (if (listp v)
1261       (nth 0 v)
1262     v))
1263
1264 ;; (auth-source-search :host "nonesuch" :type 'netrc :max 1 :create t)
1265 ;; (auth-source-search :host "nonesuch" :type 'netrc :max 1 :create t :create-extra-keys '((A "default A") (B)))
1266
1267 (defun* auth-source-netrc-create (&rest spec
1268                                         &key backend
1269                                         secret host user port create
1270                                         &allow-other-keys)
1271   (let* ((base-required '(host user port secret))
1272          ;; we know (because of an assertion in auth-source-search) that the
1273          ;; :create parameter is either t or a list (which includes nil)
1274          (create-extra (if (eq t create) nil create))
1275          (current-data (car (auth-source-search :max 1
1276                                                 :host host
1277                                                 :port port)))
1278          (required (append base-required create-extra))
1279          (file (oref backend source))
1280          (add "")
1281          ;; `valist' is an alist
1282          valist
1283          ;; `artificial' will be returned if no creation is needed
1284          artificial)
1285
1286     ;; only for base required elements (defined as function parameters):
1287     ;; fill in the valist with whatever data we may have from the search
1288     ;; we complete the first value if it's a list and use the value otherwise
1289     (dolist (br base-required)
1290       (when (symbol-value br)
1291         (let ((br-choice (cond
1292                           ;; all-accepting choice (predicate is t)
1293                           ((eq t (symbol-value br)) nil)
1294                           ;; just the value otherwise
1295                           (t (symbol-value br)))))
1296           (when br-choice
1297             (auth-source--aput valist br br-choice)))))
1298
1299     ;; for extra required elements, see if the spec includes a value for them
1300     (dolist (er create-extra)
1301       (let ((name (concat ":" (symbol-name er)))
1302             (keys (loop for i below (length spec) by 2
1303                         collect (nth i spec))))
1304         (dolist (k keys)
1305           (when (equal (symbol-name k) name)
1306             (auth-source--aput valist er (plist-get spec k))))))
1307
1308     ;; for each required element
1309     (dolist (r required)
1310       (let* ((data (auth-source--aget valist r))
1311              ;; take the first element if the data is a list
1312              (data (or (auth-source-netrc-element-or-first data)
1313                        (plist-get current-data
1314                                   (intern (format ":%s" r) obarray))))
1315              ;; this is the default to be offered
1316              (given-default (auth-source--aget
1317                              auth-source-creation-defaults r))
1318              ;; the default supplementals are simple:
1319              ;; for the user, try `given-default' and then (user-login-name);
1320              ;; otherwise take `given-default'
1321              (default (cond
1322                        ((and (not given-default) (eq r 'user))
1323                         (user-login-name))
1324                        (t given-default)))
1325              (printable-defaults (list
1326                                   (cons 'user
1327                                         (or
1328                                          (auth-source-netrc-element-or-first
1329                                           (auth-source--aget valist 'user))
1330                                          (plist-get artificial :user)
1331                                          "[any user]"))
1332                                   (cons 'host
1333                                         (or
1334                                          (auth-source-netrc-element-or-first
1335                                           (auth-source--aget valist 'host))
1336                                          (plist-get artificial :host)
1337                                          "[any host]"))
1338                                   (cons 'port
1339                                         (or
1340                                          (auth-source-netrc-element-or-first
1341                                           (auth-source--aget valist 'port))
1342                                          (plist-get artificial :port)
1343                                          "[any port]"))))
1344              (prompt (or (auth-source--aget auth-source-creation-prompts r)
1345                          (case r
1346                            (secret "%p password for %u@%h: ")
1347                            (user "%p user name for %h: ")
1348                            (host "%p host name for user %u: ")
1349                            (port "%p port for %u@%h: "))
1350                          (format "Enter %s (%%u@%%h:%%p): " r)))
1351              (prompt (auth-source-format-prompt
1352                       prompt
1353                       `((?u ,(auth-source--aget printable-defaults 'user))
1354                         (?h ,(auth-source--aget printable-defaults 'host))
1355                         (?p ,(auth-source--aget printable-defaults 'port))))))
1356
1357         ;; Store the data, prompting for the password if needed.
1358         (setq data (or data
1359                        (if (eq r 'secret)
1360                            ;; Special case prompt for passwords.
1361                            ;; TODO: make the default (setq auth-source-netrc-use-gpg-tokens `((,(if (boundp 'epa-file-auto-mode-alist-entry) (car (symbol-value 'epa-file-auto-mode-alist-entry)) "\\.gpg\\'") nil) (t gpg)))
1362                            ;; TODO: or maybe leave as (setq auth-source-netrc-use-gpg-tokens 'never)
1363                            (let* ((ep (format "Use GPG password tokens in %s?" file))
1364                                   (gpg-encrypt
1365                                    (cond
1366                                     ((eq auth-source-netrc-use-gpg-tokens 'never)
1367                                      'never)
1368                                     ((listp auth-source-netrc-use-gpg-tokens)
1369                                      (let ((check (copy-sequence
1370                                                    auth-source-netrc-use-gpg-tokens))
1371                                            item ret)
1372                                        (while check
1373                                          (setq item (pop check))
1374                                          (when (or (eq (car item) t)
1375                                                    (string-match (car item) file))
1376                                            (setq ret (cdr item))
1377                                            (setq check nil)))))
1378                                     (t 'never)))
1379                                   (plain (or (eval default) (read-passwd prompt))))
1380                              ;; ask if we don't know what to do (in which case
1381                              ;; auth-source-netrc-use-gpg-tokens must be a list)
1382                              (unless gpg-encrypt
1383                                (setq gpg-encrypt (if (y-or-n-p ep) 'gpg 'never))
1384                                ;; TODO: save the defcustom now? or ask?
1385                                (setq auth-source-netrc-use-gpg-tokens
1386                                      (cons `(,file ,gpg-encrypt)
1387                                            auth-source-netrc-use-gpg-tokens)))
1388                              (if (eq gpg-encrypt 'gpg)
1389                                  (auth-source-epa-make-gpg-token plain file)
1390                                plain))
1391                          (if (stringp default)
1392                              (read-string (if (string-match ": *\\'" prompt)
1393                                               (concat (substring prompt 0 (match-beginning 0))
1394                                                       " (default " default "): ")
1395                                             (concat prompt "(default " default ") "))
1396                                           nil nil default)
1397                            (eval default)))))
1398
1399         (when data
1400           (setq artificial (plist-put artificial
1401                                       (intern (concat ":" (symbol-name r)))
1402                                       (if (eq r 'secret)
1403                                           (lexical-let ((data data))
1404                                             (lambda () data))
1405                                         data))))
1406
1407         ;; When r is not an empty string...
1408         (when (and (stringp data)
1409                    (< 0 (length data)))
1410           ;; this function is not strictly necessary but I think it
1411           ;; makes the code clearer -tzz
1412           (let ((printer (lambda ()
1413                            ;; append the key (the symbol name of r)
1414                            ;; and the value in r
1415                            (format "%s%s %s"
1416                                    ;; prepend a space
1417                                    (if (zerop (length add)) "" " ")
1418                                    ;; remap auth-source tokens to netrc
1419                                    (case r
1420                                      (user   "login")
1421                                      (host   "machine")
1422                                      (secret "password")
1423                                      (port   "port") ; redundant but clearer
1424                                      (t (symbol-name r)))
1425                                    (if (string-match "[\"# ]" data)
1426                                        (format "%S" data)
1427                                      data)))))
1428             (setq add (concat add (funcall printer)))))))
1429
1430     (plist-put
1431      artificial
1432      :save-function
1433      (lexical-let ((file file)
1434                    (add add))
1435        (lambda () (auth-source-netrc-saver file add))))
1436
1437     (list artificial)))
1438
1439 ;;(funcall (plist-get (nth 0 (auth-source-search :host '("nonesuch2") :user "tzz" :port "imap" :create t :max 1)) :save-function))
1440 (defun auth-source-netrc-saver (file add)
1441   "Save a line ADD in FILE, prompting along the way.
1442 Respects `auth-source-save-behavior'.  Uses
1443 `auth-source-netrc-cache' to avoid prompting more than once."
1444   (let* ((key (format "%s %s" file (rfc2104-hash 'md5 64 16 file add)))
1445          (cached (assoc key auth-source-netrc-cache)))
1446
1447     (if cached
1448         (auth-source-do-trivia
1449          "auth-source-netrc-saver: found previous run for key %s, returning"
1450          key)
1451       (with-temp-buffer
1452         (when (file-exists-p file)
1453           (insert-file-contents file))
1454         (when auth-source-gpg-encrypt-to
1455           ;; (see bug#7487) making `epa-file-encrypt-to' local to
1456           ;; this buffer lets epa-file skip the key selection query
1457           ;; (see the `local-variable-p' check in
1458           ;; `epa-file-write-region').
1459           (unless (local-variable-p 'epa-file-encrypt-to (current-buffer))
1460             (make-local-variable 'epa-file-encrypt-to))
1461           (if (listp auth-source-gpg-encrypt-to)
1462               (setq epa-file-encrypt-to auth-source-gpg-encrypt-to)))
1463         ;; we want the new data to be found first, so insert at beginning
1464         (goto-char (point-min))
1465
1466         ;; Ask AFTER we've successfully opened the file.
1467         (let ((prompt (format "Save auth info to file %s? " file))
1468               (done (not (eq auth-source-save-behavior 'ask)))
1469               (bufname "*auth-source Help*")
1470               k)
1471           (while (not done)
1472             (setq k (auth-source-read-char-choice prompt '(?y ?n ?N ?e ??)))
1473             (case k
1474               (?y (setq done t))
1475               (?? (save-excursion
1476                     (with-output-to-temp-buffer bufname
1477                       (princ
1478                        (concat "(y)es, save\n"
1479                                "(n)o but use the info\n"
1480                                "(N)o and don't ask to save again\n"
1481                                "(e)dit the line\n"
1482                                "(?) for help as you can see.\n"))
1483                       ;; Why?  Doesn't with-output-to-temp-buffer already do
1484                       ;; the exact same thing anyway?  --Stef
1485                       (set-buffer standard-output)
1486                       (help-mode))))
1487               (?n (setq add ""
1488                         done t))
1489               (?N
1490                (setq add ""
1491                      done t)
1492                (customize-save-variable 'auth-source-save-behavior nil))
1493               (?e (setq add (read-string "Line to add: " add)))
1494               (t nil)))
1495
1496           (when (get-buffer-window bufname)
1497             (delete-window (get-buffer-window bufname)))
1498
1499           ;; Make sure the info is not saved.
1500           (when (null auth-source-save-behavior)
1501             (setq add ""))
1502
1503           (when (< 0 (length add))
1504             (progn
1505               (unless (bolp)
1506                 (insert "\n"))
1507               (insert add "\n")
1508               (write-region (point-min) (point-max) file nil 'silent)
1509               ;; Make the .authinfo file non-world-readable.
1510               (set-file-modes file #o600)
1511               (auth-source-do-debug
1512                "auth-source-netrc-create: wrote 1 new line to %s"
1513                file)
1514               (message "Saved new authentication information to %s" file)
1515               nil))))
1516       (auth-source--aput auth-source-netrc-cache key "ran"))))
1517
1518 ;;; Backend specific parsing: Secrets API backend
1519
1520 ;; (let ((auth-sources '(default))) (auth-source-search :max 1 :create t))
1521 ;; (let ((auth-sources '(default))) (auth-source-search :max 1 :delete t))
1522 ;; (let ((auth-sources '(default))) (auth-source-search :max 1))
1523 ;; (let ((auth-sources '(default))) (auth-source-search))
1524 ;; (let ((auth-sources '("secrets:Login"))) (auth-source-search :max 1))
1525 ;; (let ((auth-sources '("secrets:Login"))) (auth-source-search :max 1 :signon_realm "https://git.gnus.org/Git"))
1526
1527 (defun auth-source-secrets-listify-pattern (pattern)
1528   "Convert a pattern with lists to a list of string patterns.
1529
1530 auth-source patterns can have values of the form :foo (\"bar\"
1531 \"qux\"), which means to match any secret with :foo equal to
1532 \"bar\" or :foo equal to \"qux\".  The secrets backend supports
1533 only string values for patterns, so this routine returns a list
1534 of patterns that is equivalent to the single original pattern
1535 when interpreted such that if a secret matches any pattern in the
1536 list, it matches the original pattern."
1537   (if (null pattern)
1538       '(nil)
1539     (let* ((key (pop pattern))
1540            (value (pop pattern))
1541            (tails (auth-source-secrets-listify-pattern pattern))
1542            (heads (if (stringp value)
1543                       (list (list key value))
1544                     (mapcar (lambda (v) (list key v)) value))))
1545       (loop
1546          for h in heads
1547          nconc
1548            (loop
1549               for tl in tails
1550               collect (append h tl))))))
1551
1552 (defun* auth-source-secrets-search (&rest
1553                                     spec
1554                                     &key backend create delete label
1555                                     type max host user port
1556                                     &allow-other-keys)
1557   "Search the Secrets API; spec is like `auth-source'.
1558
1559 The :label key specifies the item's label.  It is the only key
1560 that can specify a substring.  Any :label value besides a string
1561 will allow any label.
1562
1563 All other search keys must match exactly.  If you need substring
1564 matching, do a wider search and narrow it down yourself.
1565
1566 You'll get back all the properties of the token as a plist.
1567
1568 Here's an example that looks for the first item in the `Login'
1569 Secrets collection:
1570
1571  (let ((auth-sources \\='(\"secrets:Login\")))
1572     (auth-source-search :max 1)
1573
1574 Here's another that looks for the first item in the `Login'
1575 Secrets collection whose label contains `gnus':
1576
1577  (let ((auth-sources \\='(\"secrets:Login\")))
1578     (auth-source-search :max 1 :label \"gnus\")
1579
1580 And this one looks for the first item in the `Login' Secrets
1581 collection that's a Google Chrome entry for the git.gnus.org site
1582 authentication tokens:
1583
1584  (let ((auth-sources \\='(\"secrets:Login\")))
1585     (auth-source-search :max 1 :signon_realm \"https://git.gnus.org/Git\"))
1586 "
1587
1588   ;; TODO
1589   (assert (not create) nil
1590           "The Secrets API auth-source backend doesn't support creation yet")
1591   ;; TODO
1592   ;; (secrets-delete-item coll elt)
1593   (assert (not delete) nil
1594           "The Secrets API auth-source backend doesn't support deletion yet")
1595
1596   (let* ((coll (oref backend source))
1597          (max (or max 5000))     ; sanity check: default to stop at 5K
1598          (ignored-keys '(:create :delete :max :backend :label :require :type))
1599          (search-keys (loop for i below (length spec) by 2
1600                             unless (memq (nth i spec) ignored-keys)
1601                             collect (nth i spec)))
1602          ;; build a search spec without the ignored keys
1603          ;; if a search key is nil or t (match anything), we skip it
1604          (search-specs (auth-source-secrets-listify-pattern
1605                         (apply #'append (mapcar
1606                                       (lambda (k)
1607                                         (if (or (null (plist-get spec k))
1608                                                 (eq t (plist-get spec k)))
1609                                             nil
1610                                           (list k (plist-get spec k))))
1611                                       search-keys))))
1612          ;; needed keys (always including host, login, port, and secret)
1613          (returned-keys (mm-delete-duplicates (append
1614                                                '(:host :login :port :secret)
1615                                                search-keys)))
1616          (items
1617           (loop for search-spec in search-specs
1618                nconc
1619                (loop for item in (apply #'secrets-search-items coll search-spec)
1620                   unless (and (stringp label)
1621                               (not (string-match label item)))
1622                   collect item)))
1623          ;; TODO: respect max in `secrets-search-items', not after the fact
1624          (items (butlast items (- (length items) max)))
1625          ;; convert the item name to a full plist
1626          (items (mapcar (lambda (item)
1627                           (append
1628                            ;; make an entry for the secret (password) element
1629                            (list
1630                             :secret
1631                             (lexical-let ((v (secrets-get-secret coll item)))
1632                               (lambda () v)))
1633                            ;; rewrite the entry from ((k1 v1) (k2 v2)) to plist
1634                            (apply #'append
1635                                   (mapcar (lambda (entry)
1636                                             (list (car entry) (cdr entry)))
1637                                           (secrets-get-attributes coll item)))))
1638                         items))
1639          ;; ensure each item has each key in `returned-keys'
1640          (items (mapcar (lambda (plist)
1641                           (append
1642                            (apply #'append
1643                                   (mapcar (lambda (req)
1644                                             (if (plist-get plist req)
1645                                                 nil
1646                                               (list req nil)))
1647                                           returned-keys))
1648                            plist))
1649                         items)))
1650     items))
1651
1652 (defun* auth-source-secrets-create (&rest
1653                                     spec
1654                                     &key backend type max host user port
1655                                     &allow-other-keys)
1656   ;; TODO
1657   ;; (apply 'secrets-create-item (auth-get-source entry) name passwd spec)
1658   (debug spec))
1659
1660 ;;; Backend specific parsing: Mac OS Keychain (using /usr/bin/security) backend
1661
1662 ;; (let ((auth-sources '(macos-keychain-internet))) (auth-source-search :max 1 :create t))
1663 ;; (let ((auth-sources '(macos-keychain-internet))) (auth-source-search :max 1 :delete t))
1664 ;; (let ((auth-sources '(macos-keychain-internet))) (auth-source-search :max 1))
1665 ;; (let ((auth-sources '(macos-keychain-internet))) (auth-source-search))
1666
1667 ;; (let ((auth-sources '(macos-keychain-generic))) (auth-source-search :max 1 :create t))
1668 ;; (let ((auth-sources '(macos-keychain-generic))) (auth-source-search :max 1 :delete t))
1669 ;; (let ((auth-sources '(macos-keychain-generic))) (auth-source-search :max 1))
1670 ;; (let ((auth-sources '(macos-keychain-generic))) (auth-source-search))
1671
1672 ;; (let ((auth-sources '("macos-keychain-internet:/Users/tzz/Library/Keychains/login.keychain"))) (auth-source-search :max 1))
1673 ;; (let ((auth-sources '("macos-keychain-generic:Login"))) (auth-source-search :max 1 :host "git.gnus.org"))
1674 ;; (let ((auth-sources '("macos-keychain-generic:Login"))) (auth-source-search :max 1))
1675
1676 (defun* auth-source-macos-keychain-search (&rest
1677                                     spec
1678                                     &key backend create delete label
1679                                     type max host user port
1680                                     &allow-other-keys)
1681   "Search the MacOS Keychain; spec is like `auth-source'.
1682
1683 All search keys must match exactly.  If you need substring
1684 matching, do a wider search and narrow it down yourself.
1685
1686 You'll get back all the properties of the token as a plist.
1687
1688 The :type key is either `macos-keychain-internet' or
1689 `macos-keychain-generic'.
1690
1691 For the internet keychain type, the :label key searches the
1692 item's labels (\"-l LABEL\" passed to \"/usr/bin/security\").
1693 Similarly, :host maps to \"-s HOST\", :user maps to \"-a USER\",
1694 and :port maps to \"-P PORT\" or \"-r PROT\"
1695 (note PROT has to be a 4-character string).
1696
1697 For the generic keychain type, the :label key searches the item's
1698 labels (\"-l LABEL\" passed to \"/usr/bin/security\").
1699 Similarly, :host maps to \"-c HOST\" (the \"creator\" keychain
1700 field), :user maps to \"-a USER\", and :port maps to \"-s PORT\".
1701
1702 Here's an example that looks for the first item in the default
1703 generic MacOS Keychain:
1704
1705  (let ((auth-sources \\='(macos-keychain-generic)))
1706     (auth-source-search :max 1)
1707
1708 Here's another that looks for the first item in the internet
1709 MacOS Keychain collection whose label is `gnus':
1710
1711  (let ((auth-sources \\='(macos-keychain-internet)))
1712     (auth-source-search :max 1 :label \"gnus\")
1713
1714 And this one looks for the first item in the internet keychain
1715 entries for git.gnus.org:
1716
1717  (let ((auth-sources \\='(macos-keychain-internet\")))
1718     (auth-source-search :max 1 :host \"git.gnus.org\"))
1719 "
1720   ;; TODO
1721   (assert (not create) nil
1722           "The MacOS Keychain auth-source backend doesn't support creation yet")
1723   ;; TODO
1724   ;; (macos-keychain-delete-item coll elt)
1725   (assert (not delete) nil
1726           "The MacOS Keychain auth-source backend doesn't support deletion yet")
1727
1728   (let* ((coll (oref backend source))
1729          (max (or max 5000))     ; sanity check: default to stop at 5K
1730          (ignored-keys '(:create :delete :max :backend :label))
1731          (search-keys (loop for i below (length spec) by 2
1732                             unless (memq (nth i spec) ignored-keys)
1733                             collect (nth i spec)))
1734          ;; build a search spec without the ignored keys
1735          ;; if a search key is nil or t (match anything), we skip it
1736          (search-spec (apply #'append (mapcar
1737                                       (lambda (k)
1738                                         (if (or (null (plist-get spec k))
1739                                                 (eq t (plist-get spec k)))
1740                                             nil
1741                                           (list k (plist-get spec k))))
1742                                       search-keys)))
1743          ;; needed keys (always including host, login, port, and secret)
1744          (returned-keys (mm-delete-duplicates (append
1745                                                '(:host :login :port :secret)
1746                                                search-keys)))
1747          (items (apply #'auth-source-macos-keychain-search-items
1748                        coll
1749                        type
1750                        max
1751                        search-spec))
1752
1753          ;; ensure each item has each key in `returned-keys'
1754          (items (mapcar (lambda (plist)
1755                           (append
1756                            (apply #'append
1757                                   (mapcar (lambda (req)
1758                                             (if (plist-get plist req)
1759                                                 nil
1760                                               (list req nil)))
1761                                           returned-keys))
1762                            plist))
1763                         items)))
1764     items))
1765
1766 (defun* auth-source-macos-keychain-search-items (coll type max
1767                                                       &rest spec
1768                                                       &key label type
1769                                                       host user port
1770                                                       &allow-other-keys)
1771
1772   (let* ((keychain-generic (eq type 'macos-keychain-generic))
1773          (args `(,(if keychain-generic
1774                       "find-generic-password"
1775                     "find-internet-password")
1776                  "-g"))
1777          (ret (list :type type)))
1778     (when label
1779       (setq args (append args (list "-l" label))))
1780     (when host
1781       (setq args (append args (list (if keychain-generic "-c" "-s") host))))
1782     (when user
1783       (setq args (append args (list "-a" user))))
1784
1785     (when port
1786       (if keychain-generic
1787           (setq args (append args (list "-s" port)))
1788         (setq args (append args (list
1789                                  (if (string-match "[0-9]+" port) "-P" "-r")
1790                                  port)))))
1791
1792       (unless (equal coll "default")
1793         (setq args (append args (list coll))))
1794
1795       (with-temp-buffer
1796         (apply #'call-process "/usr/bin/security" nil t nil args)
1797         (goto-char (point-min))
1798         (while (not (eobp))
1799           (cond
1800            ((looking-at "^password: \"\\(.+\\)\"$")
1801             (setq ret (auth-source-macos-keychain-result-append
1802                        ret
1803                        keychain-generic
1804                        "secret"
1805                        (lexical-let ((v (match-string 1)))
1806                          (lambda () v)))))
1807            ;; TODO: check if this is really the label
1808            ;; match 0x00000007 <blob>="AppleID"
1809            ((looking-at "^[ ]+0x00000007 <blob>=\"\\(.+\\)\"")
1810             (setq ret (auth-source-macos-keychain-result-append
1811                        ret
1812                        keychain-generic
1813                        "label"
1814                        (match-string 1))))
1815            ;; match "crtr"<uint32>="aapl"
1816            ;; match "svce"<blob>="AppleID"
1817            ((looking-at "^[ ]+\"\\([a-z]+\\)\"[^=]+=\"\\(.+\\)\"")
1818             (setq ret (auth-source-macos-keychain-result-append
1819                        ret
1820                        keychain-generic
1821                        (match-string 1)
1822                        (match-string 2)))))
1823           (forward-line)))
1824       ;; return `ret' iff it has the :secret key
1825       (and (plist-get ret :secret) (list ret))))
1826
1827 (defun auth-source-macos-keychain-result-append (result generic k v)
1828   (push v result)
1829   (setq k (cond
1830            ((equal k "acct") "user")
1831            ;; for generic keychains, creator is host, service is port
1832            ((and generic (equal k "crtr")) "host")
1833            ((and generic (equal k "svce")) "port")
1834            ;; for internet keychains, protocol is port, server is host
1835            ((and (not generic) (equal k "ptcl")) "port")
1836            ((and (not generic) (equal k "srvr")) "host")
1837            (t k)))
1838
1839   (push (intern (format ":%s" k)) result))
1840
1841 (defun* auth-source-macos-keychain-create (&rest
1842                                            spec
1843                                            &key backend type max host user port
1844                                            &allow-other-keys)
1845   ;; TODO
1846   (debug spec))
1847
1848 ;;; Backend specific parsing: PLSTORE backend
1849
1850 (defun* auth-source-plstore-search (&rest
1851                                     spec
1852                                     &key backend create delete label
1853                                     type max host user port
1854                                     &allow-other-keys)
1855   "Search the PLSTORE; spec is like `auth-source'."
1856   (let* ((store (oref backend data))
1857          (max (or max 5000))     ; sanity check: default to stop at 5K
1858          (ignored-keys '(:create :delete :max :backend :label :require :type))
1859          (search-keys (loop for i below (length spec) by 2
1860                             unless (memq (nth i spec) ignored-keys)
1861                             collect (nth i spec)))
1862          ;; build a search spec without the ignored keys
1863          ;; if a search key is nil or t (match anything), we skip it
1864          (search-spec (apply #'append (mapcar
1865                                       (lambda (k)
1866                                         (let ((v (plist-get spec k)))
1867                                           (if (or (null v)
1868                                                   (eq t v))
1869                                               nil
1870                                             (if (stringp v)
1871                                                 (setq v (list v)))
1872                                             (list k v))))
1873                                       search-keys)))
1874          ;; needed keys (always including host, login, port, and secret)
1875          (returned-keys (mm-delete-duplicates (append
1876                                                '(:host :login :port :secret)
1877                                                search-keys)))
1878          (items (plstore-find store search-spec))
1879          (item-names (mapcar #'car items))
1880          (items (butlast items (- (length items) max)))
1881          ;; convert the item to a full plist
1882          (items (mapcar (lambda (item)
1883                           (let* ((plist (copy-tree (cdr item)))
1884                                  (secret (plist-member plist :secret)))
1885                             (if secret
1886                                 (setcar
1887                                  (cdr secret)
1888                                  (lexical-let ((v (car (cdr secret))))
1889                                    (lambda () v))))
1890                             plist))
1891                         items))
1892          ;; ensure each item has each key in `returned-keys'
1893          (items (mapcar (lambda (plist)
1894                           (append
1895                            (apply #'append
1896                                   (mapcar (lambda (req)
1897                                             (if (plist-get plist req)
1898                                                 nil
1899                                               (list req nil)))
1900                                           returned-keys))
1901                            plist))
1902                         items)))
1903     (cond
1904      ;; if we need to create an entry AND none were found to match
1905      ((and create
1906            (not items))
1907
1908       ;; create based on the spec and record the value
1909       (setq items (or
1910                    ;; if the user did not want to create the entry
1911                    ;; in the file, it will be returned
1912                    (apply (slot-value backend 'create-function) spec)
1913                    ;; if not, we do the search again without :create
1914                    ;; to get the updated data.
1915
1916                    ;; the result will be returned, even if the search fails
1917                    (apply #'auth-source-plstore-search
1918                           (plist-put spec :create nil)))))
1919      ((and delete
1920            item-names)
1921       (dolist (item-name item-names)
1922         (plstore-delete store item-name))
1923       (plstore-save store)))
1924     items))
1925
1926 (defun* auth-source-plstore-create (&rest spec
1927                                           &key backend
1928                                           secret host user port create
1929                                           &allow-other-keys)
1930   (let* ((base-required '(host user port secret))
1931          (base-secret '(secret))
1932          ;; we know (because of an assertion in auth-source-search) that the
1933          ;; :create parameter is either t or a list (which includes nil)
1934          (create-extra (if (eq t create) nil create))
1935          (current-data (car (auth-source-search :max 1
1936                                                 :host host
1937                                                 :port port)))
1938          (required (append base-required create-extra))
1939          (file (oref backend source))
1940          (add "")
1941          ;; `valist' is an alist
1942          valist
1943          ;; `artificial' will be returned if no creation is needed
1944          artificial
1945          secret-artificial)
1946
1947     ;; only for base required elements (defined as function parameters):
1948     ;; fill in the valist with whatever data we may have from the search
1949     ;; we complete the first value if it's a list and use the value otherwise
1950     (dolist (br base-required)
1951       (when (symbol-value br)
1952         (let ((br-choice (cond
1953                           ;; all-accepting choice (predicate is t)
1954                           ((eq t (symbol-value br)) nil)
1955                           ;; just the value otherwise
1956                           (t (symbol-value br)))))
1957           (when br-choice
1958             (auth-source--aput valist br br-choice)))))
1959
1960     ;; for extra required elements, see if the spec includes a value for them
1961     (dolist (er create-extra)
1962       (let ((name (concat ":" (symbol-name er)))
1963             (keys (loop for i below (length spec) by 2
1964                         collect (nth i spec))))
1965         (dolist (k keys)
1966           (when (equal (symbol-name k) name)
1967             (auth-source--aput valist er (plist-get spec k))))))
1968
1969     ;; for each required element
1970     (dolist (r required)
1971       (let* ((data (auth-source--aget valist r))
1972              ;; take the first element if the data is a list
1973              (data (or (auth-source-netrc-element-or-first data)
1974                        (plist-get current-data
1975                                   (intern (format ":%s" r) obarray))))
1976              ;; this is the default to be offered
1977              (given-default (auth-source--aget
1978                              auth-source-creation-defaults r))
1979              ;; the default supplementals are simple:
1980              ;; for the user, try `given-default' and then (user-login-name);
1981              ;; otherwise take `given-default'
1982              (default (cond
1983                        ((and (not given-default) (eq r 'user))
1984                         (user-login-name))
1985                        (t given-default)))
1986              (printable-defaults (list
1987                                   (cons 'user
1988                                         (or
1989                                          (auth-source-netrc-element-or-first
1990                                           (auth-source--aget valist 'user))
1991                                          (plist-get artificial :user)
1992                                          "[any user]"))
1993                                   (cons 'host
1994                                         (or
1995                                          (auth-source-netrc-element-or-first
1996                                           (auth-source--aget valist 'host))
1997                                          (plist-get artificial :host)
1998                                          "[any host]"))
1999                                   (cons 'port
2000                                         (or
2001                                          (auth-source-netrc-element-or-first
2002                                           (auth-source--aget valist 'port))
2003                                          (plist-get artificial :port)
2004                                          "[any port]"))))
2005              (prompt (or (auth-source--aget auth-source-creation-prompts r)
2006                          (case r
2007                            (secret "%p password for %u@%h: ")
2008                            (user "%p user name for %h: ")
2009                            (host "%p host name for user %u: ")
2010                            (port "%p port for %u@%h: "))
2011                          (format "Enter %s (%%u@%%h:%%p): " r)))
2012              (prompt (auth-source-format-prompt
2013                       prompt
2014                       `((?u ,(auth-source--aget printable-defaults 'user))
2015                         (?h ,(auth-source--aget printable-defaults 'host))
2016                         (?p ,(auth-source--aget printable-defaults 'port))))))
2017
2018         ;; Store the data, prompting for the password if needed.
2019         (setq data (or data
2020                        (if (eq r 'secret)
2021                            (or (eval default) (read-passwd prompt))
2022                          (if (stringp default)
2023                              (read-string
2024                               (if (string-match ": *\\'" prompt)
2025                                   (concat (substring prompt 0 (match-beginning 0))
2026                                           " (default " default "): ")
2027                                 (concat prompt "(default " default ") "))
2028                               nil nil default)
2029                            (eval default)))))
2030
2031         (when data
2032           (if (member r base-secret)
2033               (setq secret-artificial
2034                     (plist-put secret-artificial
2035                                (intern (concat ":" (symbol-name r)))
2036                                data))
2037             (setq artificial (plist-put artificial
2038                                         (intern (concat ":" (symbol-name r)))
2039                                         data))))))
2040     (plstore-put (oref backend data)
2041                  (sha1 (format "%s@%s:%s"
2042                                (plist-get artificial :user)
2043                                (plist-get artificial :host)
2044                                (plist-get artificial :port)))
2045                  artificial secret-artificial)
2046     (if (y-or-n-p (format "Save auth info to file %s? "
2047                           (plstore-get-file (oref backend data))))
2048         (plstore-save (oref backend data)))))
2049
2050 ;;; older API
2051
2052 ;; (auth-source-user-or-password '("login" "password") "imap.myhost.com" t "tzz")
2053
2054 ;; deprecate the old interface
2055 (make-obsolete 'auth-source-user-or-password
2056                'auth-source-search "Emacs 24.1")
2057 (make-obsolete 'auth-source-forget-user-or-password
2058                'auth-source-forget "Emacs 24.1")
2059
2060 (defun auth-source-user-or-password
2061   (mode host port &optional username create-missing delete-existing)
2062   "Find MODE (string or list of strings) matching HOST and PORT.
2063
2064 DEPRECATED in favor of `auth-source-search'!
2065
2066 USERNAME is optional and will be used as \"login\" in a search
2067 across the Secret Service API (see secrets.el) if the resulting
2068 items don't have a username.  This means that if you search for
2069 username \"joe\" and it matches an item but the item doesn't have
2070 a :user attribute, the username \"joe\" will be returned.
2071
2072 A non nil DELETE-EXISTING means deleting any matching password
2073 entry in the respective sources.  This is useful only when
2074 CREATE-MISSING is non nil as well; the intended use case is to
2075 remove wrong password entries.
2076
2077 If no matching entry is found, and CREATE-MISSING is non nil,
2078 the password will be retrieved interactively, and it will be
2079 stored in the password database which matches best (see
2080 `auth-sources').
2081
2082 MODE can be \"login\" or \"password\"."
2083   (auth-source-do-debug
2084    "auth-source-user-or-password: DEPRECATED get %s for %s (%s) + user=%s"
2085    mode host port username)
2086
2087   (let* ((listy (listp mode))
2088          (mode (if listy mode (list mode)))
2089          (cname (if username
2090                     (format "%s %s:%s %s" mode host port username)
2091                   (format "%s %s:%s" mode host port)))
2092          (search (list :host host :port port))
2093          (search (if username (append search (list :user username)) search))
2094          (search (if create-missing
2095                      (append search (list :create t))
2096                    search))
2097          (search (if delete-existing
2098                      (append search (list :delete t))
2099                    search))
2100          ;; (found (if (not delete-existing)
2101          ;;            (gethash cname auth-source-cache)
2102          ;;          (remhash cname auth-source-cache)
2103          ;;          nil)))
2104          (found nil))
2105     (if found
2106         (progn
2107           (auth-source-do-debug
2108            "auth-source-user-or-password: DEPRECATED cached %s=%s for %s (%s) + %s"
2109            mode
2110            ;; don't show the password
2111            (if (and (member "password" mode) t)
2112                "SECRET"
2113              found)
2114            host port username)
2115           found)                        ; return the found data
2116       ;; else, if not found, search with a max of 1
2117       (let ((choice (nth 0 (apply #'auth-source-search
2118                                   (append '(:max 1) search)))))
2119         (when choice
2120           (dolist (m mode)
2121             (cond
2122              ((equal "password" m)
2123               (push (if (plist-get choice :secret)
2124                         (funcall (plist-get choice :secret))
2125                       nil) found))
2126              ((equal "login" m)
2127               (push (plist-get choice :user) found)))))
2128         (setq found (nreverse found))
2129         (setq found (if listy found (car-safe found)))))
2130
2131     found))
2132
2133 (defun auth-source-user-and-password (host &optional user)
2134   (let* ((auth-info (car
2135                      (if user
2136                          (auth-source-search
2137                           :host host
2138                           :user "yourusername"
2139                           :max 1
2140                           :require '(:user :secret)
2141                           :create nil)
2142                        (auth-source-search
2143                         :host host
2144                         :max 1
2145                         :require '(:user :secret)
2146                         :create nil))))
2147          (user (plist-get auth-info :user))
2148          (password (plist-get auth-info :secret)))
2149     (when (functionp password)
2150       (setq password (funcall password)))
2151     (list user password auth-info)))
2152
2153 (provide 'auth-source)
2154
2155 ;;; auth-source.el ends here