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