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