Improve responsiveness while in 'replace-buffer-contents'
[emacs.git] / doc / misc / auth.texi
blobf1667c49f1a5c59e50772b6615330d008be7dea2
1 \input texinfo                  @c -*-texinfo-*-
3 @include gnus-overrides.texi
5 @set VERSION 0.3
7 @setfilename ../../info/auth.info
8 @settitle Emacs auth-source Library @value{VERSION}
9 @include docstyle.texi
11 @copying
12 This file describes the Emacs auth-source library.
14 Copyright @copyright{} 2008--2018 Free Software Foundation, Inc.
16 @quotation
17 Permission is granted to copy, distribute and/or modify this document
18 under the terms of the GNU Free Documentation License, Version 1.3 or
19 any later version published by the Free Software Foundation; with no
20 Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
21 and with the Back-Cover Texts as in (a) below.  A copy of the license
22 is included in the section entitled ``GNU Free Documentation License''.
24 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
25 modify this GNU manual.''
26 @end quotation
27 @end copying
29 @dircategory Emacs lisp libraries
30 @direntry
31 * Auth-source: (auth).          The Emacs auth-source library.
32 @end direntry
34 @titlepage
35 @ifset WEBHACKDEVEL
36 @title Emacs auth-source Library (DEVELOPMENT VERSION)
37 @end ifset
38 @ifclear WEBHACKDEVEL
39 @title Emacs auth-source Library
40 @end ifclear
41 @author by Ted Zlatanov
42 @page
43 @vskip 0pt plus 1filll
44 @insertcopying
45 @end titlepage
47 @contents
49 @ifnottex
50 @node Top
51 @top Emacs auth-source
52 This manual describes the Emacs auth-source library.
54 It is a way for multiple applications to share a single configuration
55 (in Emacs and in files) for user convenience.
57 @insertcopying
59 @menu
60 * Overview::                    Overview of the auth-source library.
61 * Help for users::
62 * Multiple GMail accounts with Gnus::
63 * Secret Service API::
64 * The Unix password store::
65 * Help for developers::
66 * GnuPG and EasyPG Assistant Configuration::
67 * GNU Free Documentation License::  The license for this documentation.
68 * Index::
69 * Function Index::
70 * Variable Index::
71 @end menu
72 @end ifnottex
74 @node Overview
75 @chapter Overview
77 The auth-source library is simply a way for Emacs and Gnus, among
78 others, to answer the old burning question ``What are my user name and
79 password?''
81 (This is different from the old question about burning ``Where is the
82 fire extinguisher, please?''.)
84 The auth-source library supports more than just the user name or the
85 password (known as the secret).
87 Similarly, the auth-source library supports multiple storage backend,
88 currently either the classic ``netrc'' backend, examples of which you
89 can see later in this document, the Secret Service API, and pass, the
90 standard unix password manager.  This is done with EIEIO-based
91 backends and you can write your own if you want.
93 @node Help for users
94 @chapter Help for users
96 ``Netrc'' files are a de facto standard.  They look like this:
97 @example
98 machine @var{mymachine} login @var{myloginname} password @var{mypassword} port @var{myport}
99 @end example
101 The @code{machine} is the server (either a DNS name or an IP address).
102 It's known as @var{:host} in @code{auth-source-search} queries.  You
103 can also use @code{host}.
105 The @code{port} is the connection port or protocol.  It's known as
106 @var{:port} in @code{auth-source-search} queries.
108 The @code{user} is the user name.  It's known as @var{:user} in
109 @code{auth-source-search} queries.  You can also use @code{login} and
110 @code{account}.
112 You can use spaces inside a password or other token by surrounding the
113 token with either single or double quotes.
115 You can use apostrophes inside a password or other token by
116 surrounding it with double quotes, e.g., @code{"he'llo"}. Similarly you
117 can use double quotes inside a password or other token by surrounding
118 it with apostrophes, e.g., @code{'he"llo'}. You can't mix both (so a
119 password or other token can't have both apostrophes and double quotes).
121 All this is optional. You could just say (but we don't recommend it,
122 we're just showing that it's possible)
124 @example
125 password @var{mypassword}
126 @end example
128 to use the same password everywhere.  Again, @emph{DO NOT DO THIS} or
129 you will be pwned as the kids say.
131 ``Netrc'' files are usually called @file{.authinfo} or @file{.netrc};
132 nowadays @file{.authinfo} seems to be more popular and the auth-source
133 library encourages this confusion by accepting both, as you'll see
134 later.
136 If you have problems with the search, set @code{auth-source-debug} to
137 @code{'trivia} and see what host, port, and user the library is
138 checking in the @file{*Messages*} buffer.  Ditto for any other
139 problems, your first step is always to see what's being checked.  The
140 second step, of course, is to write a blog entry about it and wait for
141 the answer in the comments.
143 You can customize the variable @code{auth-sources}.  The following may
144 be needed if you are using an older version of Emacs or if the
145 auth-source library is not loaded for some other reason.
147 @lisp
148 (require 'auth-source)             ;; probably not necessary
149 (customize-variable 'auth-sources) ;; optional, do it once
150 @end lisp
152 @defvar auth-sources
154 The @code{auth-sources} variable tells the auth-source library where
155 your netrc files, Secret Service API collection items, or your
156 password store live for a particular host and protocol.  While you can
157 get fancy, the default and simplest configuration is:
159 @lisp
160 ;;; old default: required :host and :port, not needed anymore
161 (setq auth-sources '((:source "~/.authinfo.gpg" :host t :port t)))
162 ;;; mostly equivalent (see below about fallbacks) but shorter:
163 (setq auth-sources '((:source "~/.authinfo.gpg")))
164 ;;; even shorter and the @emph{default}:
165 (setq auth-sources '("~/.authinfo.gpg" "~/.authinfo" "~/.netrc"))
166 ;;; use the Secrets API @var{Login} collection
167 ;;; (@pxref{Secret Service API})
168 (setq auth-sources '("secrets:Login"))
169 ;;; use pass (@file{~/.password-store})
170 ;;; (@pxref{The Unix password store})
171 (setq auth-sources '(password-store))
172 @end lisp
174 By adding multiple entries to @code{auth-sources} with a particular
175 host or protocol, you can have specific netrc files for that host or
176 protocol.  Usually this is unnecessary but may make sense if you have
177 shared netrc files or some other unusual setup (90% of Emacs users
178 have unusual setups and the remaining 10% are @emph{really} unusual).
180 Here's a mixed example using two sources:
182 @lisp
183 (setq auth-sources '((:source (:secrets default)
184                       :host "myserver" :user "joe")
185                      "~/.authinfo.gpg"))
186 @end lisp
188 @end defvar
190 If you don't customize @code{auth-sources}, you'll have to live with
191 the defaults: the unencrypted netrc file @file{~/.authinfo} will be
192 used for any host and any port.
194 If that fails, any host and any port are looked up in the netrc file
195 @file{~/.authinfo.gpg}, which is a GnuPG encrypted file (@pxref{GnuPG
196 and EasyPG Assistant Configuration}).
198 Finally, the unencrypted netrc file @file{~/.netrc} will be used for
199 any host and any port.
201 The typical netrc line example is without a port.
203 @example
204 machine YOURMACHINE login YOU password YOURPASSWORD
205 @end example
207 This will match any authentication port.  Simple, right?  But what if
208 there's a SMTP server on port 433 of that machine that needs a
209 different password from the IMAP server?
211 @example
212 machine YOURMACHINE login YOU password SMTPPASSWORD port 433
213 machine YOURMACHINE login YOU password GENERALPASSWORD
214 @end example
216 For url-auth authentication (HTTP/HTTPS), you need to put this in your
217 netrc file:
219 @example
220 machine yourmachine.com:80 port http login testuser password testpass
221 @end example
223 This will match any realm and authentication method (basic or digest)
224 over HTTP@.  HTTPS is set up similarly.  If you want finer controls,
225 explore the url-auth source code and variables.
227 For Tramp authentication, use:
229 @example
230 machine yourmachine.com port scp login testuser password testpass
231 @end example
233 Note that the port denotes the Tramp connection method.  When you
234 don't use a port entry, you match any Tramp method, as explained
235 earlier.  Since Tramp has about 88 connection methods, this may be
236 necessary if you have an unusual (see earlier comment on those) setup.
238 @node Multiple GMail accounts with Gnus
239 @chapter Multiple GMail accounts with Gnus
241 For multiple GMail accounts with Gnus, you have to make two nnimap
242 entries in your @code{gnus-secondary-select-methods} with distinct
243 names:
245 @example
246 (setq gnus-secondary-select-methods '((nnimap "gmail"
247                                          (nnimap-address "imap.gmail.com"))
248                                       (nnimap "gmail2"
249                                          (nnimap-address "imap.gmail.com"))))
250 @end example
252 Your netrc entries will then be:
254 @example
255 machine gmail login account@@gmail.com password "account password" port imap
256 machine gmail2 login account2@@gmail.com password "account2 password" port imap
257 @end example
259 @node Secret Service API
260 @chapter Secret Service API
262 The @dfn{Secret Service API} is a standard from
263 @uref{http://www.freedesktop.org/wiki/Specifications/secret-storage-spec,,freedesktop.org}
264 to securely store passwords and other confidential information.  This
265 API is implemented by system daemons such as the GNOME Keyring and the
266 KDE Wallet (these are GNOME and KDE packages respectively and should
267 be available on most modern GNU/Linux systems).
269 The auth-source library uses the @file{secrets.el} library to connect
270 through the Secret Service API@.  You can also use that library in
271 other packages, it's not exclusive to auth-source.
273 @defvar secrets-enabled
274 After loading @file{secrets.el}, a non-@code{nil} value of this
275 variable indicates the existence of a daemon providing the Secret
276 Service API.
277 @end defvar
279 @deffn Command secrets-show-secrets
280 This command shows all collections, items, and their attributes.
281 @end deffn
283 The atomic objects managed by the Secret Service API are @dfn{secret
284 items}, which contain things an application wishes to store securely,
285 like a password.  Secret items have a label (a name), the @dfn{secret}
286 (which is the string we want, like a password), and a set of lookup
287 attributes.  The attributes can be used to search and retrieve a
288 secret item at a later date.
290 Secret items are grouped in @dfn{collections}.  A collection is
291 sometimes called a @samp{keyring} or @samp{wallet} in GNOME Keyring
292 and KDE Wallet but it's the same thing, a group of secrets.
293 Collections are personal and protected so only the owner can open them.
295 The most common collection is called @code{"login"}.
297 A collection can have an alias.  The alias @code{"default"} is
298 commonly used so the clients don't have to know the specific name of
299 the collection they open.  Other aliases are not supported yet.
300 Since aliases are globally accessible, set the @code{"default"} alias
301 only when you're sure it's appropriate.
303 @defun secrets-list-collections
304 This function returns all the collection names as a list.
305 @end defun
307 @defun secrets-set-alias collection alias
308 Set @var{alias} as alias of collection labeled @var{collection}.
309 Currently only the alias @code{"default"} is supported.
310 @end defun
312 @defun secrets-get-alias alias
313 Return the collection name @var{alias} is referencing to.
314 Currently only the alias @code{"default"} is supported.
315 @end defun
317 Collections can be created and deleted by the functions
318 @code{secrets-create-collection} and @code{secrets-delete-collection}.
319 Usually, this is not done from within Emacs.  Do not delete standard
320 collections such as @code{"login"}.
322 The special collection @code{"session"} exists for the lifetime of the
323 corresponding client session (in our case, Emacs's lifetime).  It is
324 created automatically when Emacs uses the Secret Service interface and
325 it is deleted when Emacs is killed.  Therefore, it can be used to
326 store and retrieve secret items temporarily.  The @code{"session"}
327 collection is better than a persistent collection when the secret
328 items should not live longer than Emacs.  The session collection can
329 be specified either by the string @code{"session"}, or by @code{nil},
330 whenever a collection parameter is needed in the following functions.
332 @defun secrets-list-items collection
333 Returns all the item labels of @var{collection} as a list.
334 @end defun
336 @defun secrets-create-item collection item password &rest attributes
337 This function creates a new item in @var{collection} with label
338 @var{item} and password @var{password}.  @var{attributes} are
339 key-value pairs set for the created item.  The keys are keyword
340 symbols, starting with a colon.  Example:
342 @example
343 ;;; The session "session", the label is "my item"
344 ;;; and the secret (password) is "geheim"
345 (secrets-create-item "session" "my item" "geheim"
346  :method "sudo" :user "joe" :host "remote-host")
347 @end example
348 @end defun
350 @defun secrets-get-secret collection item
351 Return the secret of item labeled @var{item} in @var{collection}.
352 If there is no such item, return @code{nil}.
353 @end defun
355 @defun secrets-delete-item collection item
356 This function deletes item @var{item} in @var{collection}.
357 @end defun
359 The lookup attributes, which are specified during creation of a
360 secret item, must be a key-value pair.  Keys are keyword symbols,
361 starting with a colon; values are strings.  They can be retrieved
362 from a given secret item and they can be used for searching of items.
364 @defun secrets-get-attribute collection item attribute
365 Returns the value of key @var{attribute} of item labeled @var{item} in
366 @var{collection}.  If there is no such item, or the item doesn't own
367 this key, the function returns @code{nil}.
368 @end defun
370 @defun secrets-get-attributes collection item
371 Return the lookup attributes of item labeled @var{item} in
372 @var{collection}.  If there is no such item, or the item has no
373 attributes, it returns @code{nil}.  Example:
375 @example
376 (secrets-get-attributes "session" "my item")
377      @result{} ((:user . "joe") (:host ."remote-host"))
378 @end example
379 @end defun
381 @defun secrets-search-items collection &rest attributes
382 Search for the items in @var{collection} with matching
383 @var{attributes}.  The @var{attributes} are key-value pairs, as used
384 in @code{secrets-create-item}.  Example:
386 @example
387 (secrets-search-items "session" :user "joe")
388      @result{} ("my item" "another item")
389 @end example
390 @end defun
392 The auth-source library uses the @file{secrets.el} library and thus
393 the Secret Service API when you specify a source matching
394 @code{"secrets:COLLECTION"}.  For instance, you could use
395 @code{"secrets:session"} to use the @code{"session"} collection, open only
396 for the lifetime of Emacs.  Or you could use @code{"secrets:Login"} to
397 open the @code{"Login"} collection.  As a special case, you can use the
398 symbol @code{default} in @code{auth-sources} (not a string, but a
399 symbol) to specify the @code{"default"} alias.  Here is a contrived
400 example that sets @code{auth-sources} to search three collections and
401 then fall back to @file{~/.authinfo.gpg}.
403 @example
404 (setq auth-sources '(default
405                      "secrets:session"
406                      "secrets:Login"
407                      "~/.authinfo.gpg"))
408 @end example
410 @node The Unix password store
411 @chapter The Unix password store
413 @uref{http://www.passwordstore.org,,The standard unix password
414 manager} (or just @code{pass}) stores your passwords in
415 @code{gpg}-protected files following the Unix philosophy.
417 Emacs integration of @code{pass} follows the first approach suggested
418 by the pass project itself for data organization to find data. This
419 means that the filename of the file containing the password for a user
420 on a particular host must contain the host name.  The file itself must
421 contain the password on the first line, as well as a @code{username}
422 field containing the username on a subsequent line. A @code{port}
423 field can be used to differentiate the authentication data for several
424 services with the same username on the same host.
426 Users of @code{pass} may also be interested in functionality provided
427 by other Emacs packages dealing with pass:
429 @itemize
430 @item
431 @uref{https://git.zx2c4.com/password-store/tree/contrib/emacs/password-store.el,,password-store}: library wrapping @code{pass};
432 @item
433 @uref{https://github.com/NicolasPetton/pass,,pass}: major mode to manipulate the store and edit entries;
434 @item
435 @uref{https://github.com/jabranham/helm-pass,,helm-pass}: helm interface for pass.
436 @end itemize
438 @node Help for developers
439 @chapter Help for developers
441 The auth-source library lets you control logging output easily.
443 @defvar auth-source-debug
444 Set this variable to @code{'trivia} to see lots of output in
445 @file{*Messages*}, or set it to a function that behaves like
446 @code{message} to do your own logging.
447 @end defvar
449 The auth-source library only has a few functions for external use.
451 @defun auth-source-search &rest spec &key type max host user port secret require create delete &allow-other-keys
452 This function searches (or modifies) authentication backends according
453 to @var{spec}.  See the function's doc-string for details.
454 @c TODO more details.
455 @end defun
457 Let's take a look at an example of using @code{auth-source-search}
458 from Gnus's @code{nnimap.el}.
460 @example
461 (defun nnimap-credentials (address ports)
462   (let* ((auth-source-creation-prompts
463           '((user  . "IMAP user at %h: ")
464             (secret . "IMAP password for %u@@%h: ")))
465          (found (nth 0 (auth-source-search :max 1
466                                            :host address
467                                            :port ports
468                                            :require '(:user :secret)
469                                            :create t))))
470     (if found
471         (list (plist-get found :user)
472               (let ((secret (plist-get found :secret)))
473                 (if (functionp secret)
474                     (funcall secret)
475                   secret))
476               (plist-get found :save-function))
477       nil)))
478 @end example
480 This call requires the user and password (secret) to be in the
481 results.  It also requests that an entry be created if it doesn't
482 exist already.  While the created entry is being assembled, the shown
483 prompts will be used to interact with the user.  The caller can also
484 pass data in @code{auth-source-creation-defaults} to supply defaults
485 for any of the prompts.
487 Note that the password needs to be evaluated if it's a function.  It's
488 wrapped in a function to provide some security.
490 Later, after a successful login, @code{nnimap.el} calls the
491 @code{:save-function} like so:
493 @example
494 (when (functionp (nth 2 credentials))
495    (funcall (nth 2 credentials)))
496 @end example
498 This will work whether the @code{:save-function} was provided or not.
499 @code{:save-function} will be provided only when a new entry was
500 created, so this effectively says ``after a successful login, save the
501 authentication information we just used, if it was newly created.''
503 After the first time it's called, the @code{:save-function} will not
504 run again (but it will log something if you have set
505 @code{auth-source-debug} to @code{'trivia}).  This is so it won't ask
506 the same question again, which is annoying.  This is so it won't ask
507 the same question again, which is annoying.  This is so it won't ask
508 the same question again, which is annoying.
510 So the responsibility of the API user that specified @code{:create t}
511 is to call the @code{:save-function} if it's provided.
513 @defun auth-source-delete &rest spec &key delete &allow-other-keys
514 This function deletes entries matching @var{spec} from the
515 authentication backends.  It returns the entries that were deleted.
516 The backend may not actually delete the entries.
517 @end defun
519 @defun auth-source-forget spec
520 This function forgets any cached data that exactly matches @var{spec}.
521 It returns @code{t} if it forget some data, and @code{nil} if no
522 matching data was found.
523 @end defun
525 @defun auth-source-forget+ &rest spec &allow-other-keys
526 This function forgets any cached data matching @var{spec}.
527 It returns the number of items forgotten.
528 @end defun
530 @node GnuPG and EasyPG Assistant Configuration
531 @appendix GnuPG and EasyPG Assistant Configuration
533 If the @code{auth-sources} variable contains @file{~/.authinfo.gpg}
534 before @file{~/.authinfo}, the auth-source library will try to
535 read the GnuPG encrypted @file{.gpg} file first, before
536 the unencrypted file.
538 In Emacs 23 or later there is an option @code{auto-encryption-mode} to
539 automatically decrypt @file{*.gpg} files.  It is enabled by default.
540 If you are using earlier versions of Emacs, you will need:
542 @lisp
543 (require 'epa-file)
544 (epa-file-enable)
545 @end lisp
547 If you want your GnuPG passwords to be cached, set up @code{gpg-agent}
548 or EasyPG Assistant
549 (@pxref{Caching Passphrases, , Caching Passphrases, epa}).
551 To quick start, here are some questions:
553 @itemize
554 @item
555 Do you use GnuPG version 2 instead of GnuPG version 1?
556 @item
557 Do you use symmetric encryption rather than public key encryption?
558 @item
559 Do you want to use gpg-agent?
560 @end itemize
562 Here are configurations depending on your answers:
564 @multitable {111} {222} {333} {configuration configuration configuration}
565 @item @b{1} @tab @b{2} @tab @b{3} @tab Configuration
566 @item Yes @tab Yes @tab Yes @tab Set up gpg-agent.
567 @item Yes @tab Yes @tab No @tab You can't, without gpg-agent.
568 @item Yes @tab No @tab Yes @tab Set up gpg-agent.
569 @item Yes @tab No @tab No @tab You can't, without gpg-agent.
570 @item No @tab Yes @tab Yes @tab Set up elisp passphrase cache.
571 @item No @tab Yes @tab No @tab Set up elisp passphrase cache.
572 @item No @tab No @tab Yes @tab Set up gpg-agent.
573 @item No @tab No @tab No @tab You can't, without gpg-agent.
574 @end multitable
576 To set up gpg-agent, follow the instruction in GnuPG manual
577 (@pxref{Invoking GPG-AGENT, , Invoking GPG-AGENT, gnupg}).
579 To set up elisp passphrase cache, set
580 @code{epa-file-cache-passphrase-for-symmetric-encryption}.
582 @node GNU Free Documentation License
583 @appendix GNU Free Documentation License
584 @include doclicense.texi
586 @node Index
587 @unnumbered Index
588 @printindex cp
590 @node Function Index
591 @unnumbered Function Index
592 @printindex fn
594 @node Variable Index
595 @unnumbered Variable Index
596 @printindex vr
598 @bye
600 @c End: