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