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