Fix manual formatting errors
[bpt/emacs.git] / doc / misc / auth.texi
CommitLineData
9eb59592 1\input texinfo @c -*-texinfo-*-
7fbf7cae
TZ
2
3@include gnus-overrides.texi
4
db671917
AS
5@set VERSION 0.3
6
e280480a 7@setfilename ../../info/auth
5dc584b5 8@settitle Emacs auth-source Library @value{VERSION}
9eb59592 9
9eb59592
MB
10@copying
11This file describes the Emacs auth-source library.
12
ab422c4d 13Copyright @copyright{} 2008--2013 Free Software Foundation, Inc.
9eb59592
MB
14
15@quotation
16Permission is granted to copy, distribute and/or modify this document
17under the terms of the GNU Free Documentation License, Version 1.3 or
18any later version published by the Free Software Foundation; with no
19Invariant Sections, with the Front-Cover texts being ``A GNU Manual,''
20and with the Back-Cover Texts as in (a) below. A copy of the license
0b1af106 21is included in the section entitled ``GNU Free Documentation License''.
9eb59592
MB
22
23(a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
6bf430d1 24modify this GNU manual.''
9eb59592
MB
25@end quotation
26@end copying
27
0c973505 28@dircategory Emacs lisp libraries
5dc584b5 29@direntry
62e034c2 30* Auth-source: (auth). The Emacs auth-source library.
5dc584b5 31@end direntry
9eb59592
MB
32
33@titlepage
7fbf7cae
TZ
34@ifset WEBHACKDEVEL
35@title Emacs auth-source Library (DEVELOPMENT VERSION)
36@end ifset
37@ifclear WEBHACKDEVEL
9eb59592 38@title Emacs auth-source Library
7fbf7cae 39@end ifclear
9eb59592
MB
40@author by Ted Zlatanov
41@page
9eb59592
MB
42@vskip 0pt plus 1filll
43@insertcopying
44@end titlepage
9eb59592 45
5dc584b5 46@contents
9eb59592 47
5dc584b5 48@ifnottex
9eb59592
MB
49@node Top
50@top Emacs auth-source
51This manual describes the Emacs auth-source library.
52
53It is a way for multiple applications to share a single configuration
54(in Emacs and in files) for user convenience.
55
5dc584b5
KB
56@insertcopying
57
9eb59592
MB
58@menu
59* Overview:: Overview of the auth-source library.
fe3c5669
PE
60* Help for users::
61* Secret Service API::
62* Help for developers::
63* GnuPG and EasyPG Assistant Configuration::
0b1af106 64* GNU Free Documentation License:: The license for this documentation.
fe3c5669
PE
65* Index::
66* Function Index::
67* Variable Index::
9eb59592 68@end menu
5dc584b5 69@end ifnottex
9eb59592
MB
70
71@node Overview
72@chapter Overview
73
38dc51ba 74The auth-source library is simply a way for Emacs and Gnus, among
b8e0f0cd
G
75others, to answer the old burning question ``What are my user name and
76password?''
b0b63450 77
b8e0f0cd
G
78(This is different from the old question about burning ``Where is the
79fire extinguisher, please?''.)
80
81The auth-source library supports more than just the user name or the
82password (known as the secret).
83
84Similarly, the auth-source library supports multiple storage backend,
85currently either the classic ``netrc'' backend, examples of which you
1df7defd 86can see later in this document, or the Secret Service API@. This is
b8e0f0cd 87done with EIEIO-based backends and you can write your own if you want.
9eb59592
MB
88
89@node Help for users
90@chapter Help for users
91
b0b63450
MB
92``Netrc'' files are a de facto standard. They look like this:
93@example
38dc51ba 94machine @var{mymachine} login @var{myloginname} password @var{mypassword} port @var{myport}
b0b63450 95@end example
9eb59592 96
b8e0f0cd
G
97The @code{machine} is the server (either a DNS name or an IP address).
98It's known as @var{:host} in @code{auth-source-search} queries. You
99can also use @code{host}.
100
101The @code{port} is the connection port or protocol. It's known as
35123c04 102@var{:port} in @code{auth-source-search} queries.
b8e0f0cd
G
103
104The @code{user} is the user name. It's known as @var{:user} in
105@code{auth-source-search} queries. You can also use @code{login} and
106@code{account}.
107
108Spaces are always OK as far as auth-source is concerned (but other
109programs may not like them). Just put the data in quotes, escaping
3c93d00f 110quotes as you'd expect with @samp{\}.
b8e0f0cd
G
111
112All these are optional. You could just say (but we don't recommend
113it, we're just showing that it's possible)
38dc51ba 114
b8e0f0cd
G
115@example
116password @var{mypassword}
117@end example
38dc51ba 118
b8e0f0cd
G
119to use the same password everywhere. Again, @emph{DO NOT DO THIS} or
120you will be pwned as the kids say.
38dc51ba 121
3c93d00f
GM
122``Netrc'' files are usually called @file{.authinfo} or @file{.netrc};
123nowadays @file{.authinfo} seems to be more popular and the auth-source
4a3988d5
G
124library encourages this confusion by accepting both, as you'll see
125later.
38dc51ba 126
b8e0f0cd 127If you have problems with the search, set @code{auth-source-debug} to
733afdf4 128@code{'trivia} and see what host, port, and user the library is
3c93d00f 129checking in the @samp{*Messages*} buffer. Ditto for any other
733afdf4
TZ
130problems, your first step is always to see what's being checked. The
131second step, of course, is to write a blog entry about it and wait for
132the answer in the comments.
38dc51ba
KY
133
134You can customize the variable @code{auth-sources}. The following may
b0b63450
MB
135be needed if you are using an older version of Emacs or if the
136auth-source library is not loaded for some other reason.
9eb59592
MB
137
138@lisp
b0b63450 139(require 'auth-source) ;; probably not necessary
9eb59592
MB
140(customize-variable 'auth-sources) ;; optional, do it once
141@end lisp
142
143@defvar auth-sources
144
38dc51ba 145The @code{auth-sources} variable tells the auth-source library where
bd3e840f
KY
146your netrc files or Secret Service API collection items live for a
147particular host and protocol. While you can get fancy, the default
148and simplest configuration is:
9eb59592
MB
149
150@lisp
35123c04
TZ
151;;; old default: required :host and :port, not needed anymore
152(setq auth-sources '((:source "~/.authinfo.gpg" :host t :port t)))
bd3e840f
KY
153;;; mostly equivalent (see below about fallbacks) but shorter:
154(setq auth-sources '((:source "~/.authinfo.gpg")))
b8e0f0cd 155;;; even shorter and the @emph{default}:
4a3988d5 156(setq auth-sources '("~/.authinfo.gpg" "~/.authinfo" "~/.netrc"))
9fbc8f46
GM
157;;; use the Secrets API @var{Login} collection
158;;; (@pxref{Secret Service API})
5415d076 159(setq auth-sources '("secrets:Login"))
9eb59592
MB
160@end lisp
161
38dc51ba 162By adding multiple entries to @code{auth-sources} with a particular
b0b63450
MB
163host or protocol, you can have specific netrc files for that host or
164protocol. Usually this is unnecessary but may make sense if you have
165shared netrc files or some other unusual setup (90% of Emacs users
166have unusual setups and the remaining 10% are @emph{really} unusual).
9eb59592 167
b8e0f0cd 168Here's a mixed example using two sources:
bd3e840f
KY
169
170@lisp
9fbc8f46
GM
171(setq auth-sources '((:source (:secrets default)
172 :host "myserver" :user "joe")
b8e0f0cd 173 "~/.authinfo.gpg"))
bd3e840f
KY
174@end lisp
175
b0b63450 176@end defvar
9eb59592 177
38dc51ba 178If you don't customize @code{auth-sources}, you'll have to live with
723ea6b5
TZ
179the defaults: the unencrypted netrc file @file{~/.authinfo} will be
180used for any host and any port.
b8e0f0cd 181
723ea6b5
TZ
182If that fails, any host and any port are looked up in the netrc file
183@file{~/.authinfo.gpg}, which is a GnuPG encrypted file (@pxref{GnuPG
184and EasyPG Assistant Configuration}).
185
186Finally, the unencrypted netrc file @file{~/.netrc} will be used for
187any host and any port.
9eb59592 188
b8e0f0cd 189The typical netrc line example is without a port.
b0b63450
MB
190
191@example
192machine YOURMACHINE login YOU password YOURPASSWORD
193@end example
194
195This will match any authentication port. Simple, right? But what if
196there's a SMTP server on port 433 of that machine that needs a
197different password from the IMAP server?
198
199@example
200machine YOURMACHINE login YOU password SMTPPASSWORD port 433
201machine YOURMACHINE login YOU password GENERALPASSWORD
202@end example
203
9eb59592
MB
204For url-auth authentication (HTTP/HTTPS), you need to put this in your
205netrc file:
206
207@example
208machine yourmachine.com:80 port http login testuser password testpass
209@end example
210
b0b63450 211This will match any realm and authentication method (basic or digest)
1df7defd 212over HTTP@. HTTPS is set up similarly. If you want finer controls,
b0b63450 213explore the url-auth source code and variables.
9eb59592
MB
214
215For Tramp authentication, use:
216
217@example
218machine yourmachine.com port scp login testuser password testpass
219@end example
220
221Note that the port denotes the Tramp connection method. When you
222don't use a port entry, you match any Tramp method, as explained
b0b63450
MB
223earlier. Since Tramp has about 88 connection methods, this may be
224necessary if you have an unusual (see earlier comment on those) setup.
9eb59592 225
bd3e840f
KY
226@node Secret Service API
227@chapter Secret Service API
228
0317dd51
MA
229The @dfn{Secret Service API} is a standard from
230@uref{http://www.freedesktop.org/wiki/Specifications/secret-storage-spec,,freedesktop.org}
ca2c89b6
TZ
231to securely store passwords and other confidential information. This
232API is implemented by system daemons such as the GNOME Keyring and the
233KDE Wallet (these are GNOME and KDE packages respectively and should
234be available on most modern GNU/Linux systems).
0317dd51 235
ca2c89b6 236The auth-source library uses the @file{secrets.el} library to connect
1df7defd 237through the Secret Service API@. You can also use that library in
ca2c89b6 238other packages, it's not exclusive to auth-source.
0317dd51
MA
239
240@defvar secrets-enabled
241After loading @file{secrets.el}, a non-@code{nil} value of this
242variable indicates the existence of a daemon providing the Secret
243Service API.
244@end defvar
245
246@deffn Command secrets-show-secrets
ca2c89b6 247This command shows all collections, items, and their attributes.
0317dd51
MA
248@end deffn
249
ca2c89b6
TZ
250The atomic objects managed by the Secret Service API are @dfn{secret
251items}, which contain things an application wishes to store securely,
252like a password. Secret items have a label (a name), the @dfn{secret}
253(which is the string we want, like a password), and a set of lookup
254attributes. The attributes can be used to search and retrieve a
255secret item at a later date.
0317dd51
MA
256
257Secret items are grouped in @dfn{collections}. A collection is
ca2c89b6
TZ
258sometimes called a @samp{keyring} or @samp{wallet} in GNOME Keyring
259and KDE Wallet but it's the same thing, a group of secrets.
260Collections are personal and protected so only the owner can open them.
0317dd51 261
c8bf3227 262The most common collection is called @code{"login"}.
ca2c89b6 263
c8bf3227 264A collection can have an alias. The alias @code{"default"} is
ca2c89b6
TZ
265commonly used so the clients don't have to know the specific name of
266the collection they open. Other aliases are not supported yet.
c8bf3227 267Since aliases are globally accessible, set the @code{"default"} alias
ca2c89b6 268only when you're sure it's appropriate.
0317dd51
MA
269
270@defun secrets-list-collections
ca2c89b6 271This function returns all the collection names as a list.
0317dd51
MA
272@end defun
273
274@defun secrets-set-alias collection alias
275Set @var{alias} as alias of collection labeled @var{collection}.
c8bf3227 276Currently only the alias @code{"default"} is supported.
0317dd51
MA
277@end defun
278
279@defun secrets-get-alias alias
280Return the collection name @var{alias} is referencing to.
c8bf3227 281Currently only the alias @code{"default"} is supported.
0317dd51
MA
282@end defun
283
284Collections can be created and deleted by the functions
285@code{secrets-create-collection} and @code{secrets-delete-collection}.
ca2c89b6 286Usually, this is not done from within Emacs. Do not delete standard
c8bf3227 287collections such as @code{"login"}.
ca2c89b6 288
c8bf3227 289The special collection @code{"session"} exists for the lifetime of the
ca2c89b6
TZ
290corresponding client session (in our case, Emacs's lifetime). It is
291created automatically when Emacs uses the Secret Service interface and
292it is deleted when Emacs is killed. Therefore, it can be used to
c8bf3227 293store and retrieve secret items temporarily. The @code{"session"}
ca2c89b6
TZ
294collection is better than a persistent collection when the secret
295items should not live longer than Emacs. The session collection can
c8bf3227 296be specified either by the string @code{"session"}, or by @code{nil},
ca2c89b6 297whenever a collection parameter is needed in the following functions.
0317dd51
MA
298
299@defun secrets-list-items collection
ca2c89b6 300Returns all the item labels of @var{collection} as a list.
0317dd51
MA
301@end defun
302
303@defun secrets-create-item collection item password &rest attributes
304This function creates a new item in @var{collection} with label
aebd5f1a 305@var{item} and password @var{password}. @var{attributes} are
0317dd51
MA
306key-value pairs set for the created item. The keys are keyword
307symbols, starting with a colon. Example:
308
309@example
ca2c89b6
TZ
310;;; The session "session", the label is "my item"
311;;; and the secret (password) is "geheim"
0317dd51
MA
312(secrets-create-item "session" "my item" "geheim"
313 :method "sudo" :user "joe" :host "remote-host")
314@end example
315@end defun
316
317@defun secrets-get-secret collection item
318Return the secret of item labeled @var{item} in @var{collection}.
319If there is no such item, return @code{nil}.
320@end defun
321
322@defun secrets-delete-item collection item
323This function deletes item @var{item} in @var{collection}.
324@end defun
325
326The lookup attributes, which are specified during creation of a
327secret item, must be a key-value pair. Keys are keyword symbols,
328starting with a colon; values are strings. They can be retrieved
ca2c89b6 329from a given secret item and they can be used for searching of items.
0317dd51
MA
330
331@defun secrets-get-attribute collection item attribute
332Returns the value of key @var{attribute} of item labeled @var{item} in
333@var{collection}. If there is no such item, or the item doesn't own
334this key, the function returns @code{nil}.
335@end defun
336
337@defun secrets-get-attributes collection item
338Return the lookup attributes of item labeled @var{item} in
339@var{collection}. If there is no such item, or the item has no
340attributes, it returns @code{nil}. Example:
341
342@example
343(secrets-get-attributes "session" "my item")
344 @result{} ((:user . "joe") (:host ."remote-host"))
345@end example
346@end defun
347
348@defun secrets-search-items collection &rest attributes
ca2c89b6
TZ
349Search for the items in @var{collection} with matching
350@var{attributes}. The @var{attributes} are key-value pairs, as used
351in @code{secrets-create-item}. Example:
0317dd51
MA
352
353@example
354(secrets-search-items "session" :user "joe")
355 @result{} ("my item" "another item")
356@end example
357@end defun
bd3e840f 358
ca2c89b6
TZ
359The auth-source library uses the @file{secrets.el} library and thus
360the Secret Service API when you specify a source matching
c8bf3227
TZ
361@code{"secrets:COLLECTION"}. For instance, you could use
362@code{"secrets:session"} to use the @code{"session"} collection, open only
363for the lifetime of Emacs. Or you could use @code{"secrets:Login"} to
364open the @code{"Login"} collection. As a special case, you can use the
ca2c89b6 365symbol @code{default} in @code{auth-sources} (not a string, but a
c8bf3227 366symbol) to specify the @code{"default"} alias. Here is a contrived
ca2c89b6
TZ
367example that sets @code{auth-sources} to search three collections and
368then fall back to @file{~/.authinfo.gpg}.
369
370@example
371(setq auth-sources '(default
372 "secrets:session"
373 "secrets:Login"
374 "~/.authinfo.gpg"))
375@end example
376
9eb59592
MB
377@node Help for developers
378@chapter Help for developers
379
733afdf4
TZ
380The auth-source library lets you control logging output easily.
381
382@defvar auth-source-debug
3c93d00f
GM
383Set this variable to @code{'trivia} to see lots of output in
384@samp{*Messages*}, or set it to a function that behaves like
385@code{message} to do your own logging.
733afdf4
TZ
386@end defvar
387
b8e0f0cd 388The auth-source library only has a few functions for external use.
9eb59592 389
aebd5f1a
GM
390@defun auth-source-search &rest spec &key type max host user port secret require create delete &allow-other-keys
391This function searches (or modifies) authentication backends according
392to @var{spec}. See the function's doc-string for details.
393@c TODO more details.
b8e0f0cd 394@end defun
9eb59592 395
733afdf4 396Let's take a look at an example of using @code{auth-source-search}
aebd5f1a 397from Gnus's @code{nnimap.el}.
733afdf4
TZ
398
399@example
400(defun nnimap-credentials (address ports)
401 (let* ((auth-source-creation-prompts
402 '((user . "IMAP user at %h: ")
403 (secret . "IMAP password for %u@@%h: ")))
404 (found (nth 0 (auth-source-search :max 1
405 :host address
406 :port ports
407 :require '(:user :secret)
408 :create t))))
409 (if found
410 (list (plist-get found :user)
3107d2a5
KY
411 (let ((secret (plist-get found :secret)))
412 (if (functionp secret)
413 (funcall secret)
414 secret))
415 (plist-get found :save-function))
733afdf4
TZ
416 nil)))
417@end example
418
419This call requires the user and password (secret) to be in the
420results. It also requests that an entry be created if it doesn't
421exist already. While the created entry is being assembled, the shown
422prompts will be used to interact with the user. The caller can also
423pass data in @code{auth-source-creation-defaults} to supply defaults
424for any of the prompts.
425
426Note that the password needs to be evaluated if it's a function. It's
427wrapped in a function to provide some security.
428
6196cffe 429Later, after a successful login, @code{nnimap.el} calls the
733afdf4
TZ
430@code{:save-function} like so:
431
432@example
433(when (functionp (nth 2 credentials))
434 (funcall (nth 2 credentials)))
3107d2a5 435@end example
733afdf4 436
4248cca2 437This will work whether the @code{:save-function} was provided or not.
733afdf4
TZ
438@code{:save-function} will be provided only when a new entry was
439created, so this effectively says ``after a successful login, save the
440authentication information we just used, if it was newly created.''
441
4248cca2
TZ
442After the first time it's called, the @code{:save-function} will not
443run again (but it will log something if you have set
444@code{auth-source-debug} to @code{'trivia}). This is so it won't ask
445the same question again, which is annoying. This is so it won't ask
446the same question again, which is annoying. This is so it won't ask
447the same question again, which is annoying.
448
449So the responsibility of the API user that specified @code{:create t}
450is to call the @code{:save-function} if it's provided.
451
aebd5f1a
GM
452@defun auth-source-delete &rest spec &key delete &allow-other-keys
453This function deletes entries matching @var{spec} from the
454authentication backends. It returns the entries that were deleted.
455The backend may not actually delete the entries.
b8e0f0cd 456@end defun
bd3e840f 457
aebd5f1a
GM
458@defun auth-source-forget spec
459This function forgets any cached data that exactly matches @var{spec}.
460It returns @code{t} if it forget some data, and @code{nil} if no
461matching data was found.
b8e0f0cd
G
462@end defun
463
aebd5f1a
GM
464@defun auth-source-forget+ &rest spec &allow-other-keys
465This function forgets any cached data matching @var{spec}.
466It returns the number of items forgotten.
9eb59592
MB
467@end defun
468
36d3245f
G
469@node GnuPG and EasyPG Assistant Configuration
470@appendix GnuPG and EasyPG Assistant Configuration
471
98e2b864 472If you don't customize @code{auth-sources}, the auth-source library
3c93d00f
GM
473reads @file{~/.authinfo.gpg}, which is a GnuPG encrypted file. Then
474it will check @file{~/.authinfo} but it's not recommended to use such
7ba93e94 475an unencrypted file.
98e2b864 476
36d3245f 477In Emacs 23 or later there is an option @code{auto-encryption-mode} to
3c93d00f 478automatically decrypt @file{*.gpg} files. It is enabled by default.
a1d16a7b 479If you are using earlier versions of Emacs, you will need:
36d3245f
G
480
481@lisp
482(require 'epa-file)
483(epa-file-enable)
484@end lisp
485
98e2b864 486If you want your GnuPG passwords to be cached, set up @code{gpg-agent}
fe3c5669 487or EasyPG Assistant
c56dfd36 488(@pxref{Caching Passphrases, , Caching Passphrases, epa}).
36d3245f 489
98e2b864 490To quick start, here are some questions:
36d3245f
G
491
492@enumerate
a1d16a7b
G
493@item
494Do you use GnuPG version 2 instead of GnuPG version 1?
495@item
496Do you use symmetric encryption rather than public key encryption?
497@item
498Do you want to use gpg-agent?
36d3245f
G
499@end enumerate
500
501Here are configurations depending on your answers:
502
503@multitable {111} {222} {333} {configuration configuration configuration}
504@item @b{1} @tab @b{2} @tab @b{3} @tab Configuration
98e2b864 505@item Yes @tab Yes @tab Yes @tab Set up gpg-agent.
36d3245f 506@item Yes @tab Yes @tab No @tab You can't, without gpg-agent.
98e2b864 507@item Yes @tab No @tab Yes @tab Set up gpg-agent.
36d3245f
G
508@item Yes @tab No @tab No @tab You can't, without gpg-agent.
509@item No @tab Yes @tab Yes @tab Set up elisp passphrase cache.
510@item No @tab Yes @tab No @tab Set up elisp passphrase cache.
98e2b864 511@item No @tab No @tab Yes @tab Set up gpg-agent.
36d3245f
G
512@item No @tab No @tab No @tab You can't, without gpg-agent.
513@end multitable
514
a1d16a7b
G
515To set up gpg-agent, follow the instruction in GnuPG manual
516(@pxref{Invoking GPG-AGENT, , Invoking GPG-AGENT, gnupg}).
36d3245f
G
517
518To set up elisp passphrase cache, set
519@code{epa-file-cache-passphrase-for-symmetric-encryption}.
520
0b1af106
GM
521@node GNU Free Documentation License
522@appendix GNU Free Documentation License
523@include doclicense.texi
524
9eb59592 525@node Index
0b1af106 526@unnumbered Index
9eb59592
MB
527@printindex cp
528
529@node Function Index
0b1af106 530@unnumbered Function Index
9eb59592
MB
531@printindex fn
532
533@node Variable Index
0b1af106 534@unnumbered Variable Index
9eb59592
MB
535@printindex vr
536
9eb59592
MB
537@bye
538
539@c End: