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