auth.texi 20 KB

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