nss.scm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2015 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu system nss)
  19. #:use-module (rnrs enums)
  20. #:use-module (guix records)
  21. #:use-module (srfi srfi-9)
  22. #:use-module (ice-9 match)
  23. #:export (name-service-switch?
  24. name-service-switch
  25. name-service?
  26. name-service
  27. lookup-specification
  28. %default-nss
  29. %mdns-host-lookup-nss
  30. %files
  31. %compat
  32. %dns
  33. name-service-switch->string))
  34. ;;; Commentary:
  35. ;;;
  36. ;;; Bindings for libc's name service switch (NSS) configuration.
  37. ;;;
  38. ;;; Code:
  39. (define-record-type* <name-service> name-service
  40. make-name-service
  41. name-service?
  42. (name name-service-name)
  43. (reaction name-service-reaction
  44. (default (lookup-specification))))
  45. ;; Lookup specification (info "(libc) Actions in the NSS Configuration").
  46. (define-enumeration lookup-action
  47. (return continue)
  48. make-lookup-action)
  49. (define-enumeration lookup-status
  50. (success
  51. not-found
  52. unavailable
  53. try-again)
  54. make-lookup-status)
  55. (define-record-type <lookup-status-negation>
  56. (lookup-status-negation status)
  57. lookup-status-negation?
  58. (status lookup-status-negation-status))
  59. (define-record-type <lookup-reaction>
  60. (make-lookup-reaction status action)
  61. lookup-reaction?
  62. (status lookup-reaction-status)
  63. (action lookup-reaction-action))
  64. (define-syntax lookup-reaction
  65. (syntax-rules (not =>)
  66. ((_ ((not status) => action))
  67. (make-lookup-reaction (lookup-status-negation (lookup-status status))
  68. (lookup-action action)))
  69. ((_ (status => action))
  70. (make-lookup-reaction (lookup-status status)
  71. (lookup-action action)))))
  72. (define-syntax-rule (lookup-specification reaction ...)
  73. "Return an NSS lookup specification."
  74. (list (lookup-reaction reaction) ...))
  75. ;;;
  76. ;;; Common name services and default NSS configuration.
  77. ;;;
  78. (define %compat
  79. (name-service
  80. (name "compat")
  81. (reaction (lookup-specification (not-found => return)))))
  82. (define %files
  83. (name-service (name "files")))
  84. (define %dns
  85. ;; DNS is supposed to be authoritative, so unless it's unavailable, return
  86. ;; what it finds.
  87. (name-service
  88. (name "dns")
  89. (reaction (lookup-specification ((not unavailable) => return)))))
  90. ;; The NSS. We list all the databases here because that allows us to
  91. ;; statically ensure that the user's configuration refers to existing
  92. ;; databases. See libc/nss/databases.def for the list of databases. Default
  93. ;; values obtained by looking for "DEFAULT_CONFIG" in libc/nss/*.c.
  94. ;;
  95. ;; Although libc places 'dns' before 'files' in the default configurations of
  96. ;; the 'hosts' and 'networks' databases, we choose to put 'files' before 'dns'
  97. ;; by default, so that users can override host/address mappings in /etc/hosts
  98. ;; and bypass DNS to improve their privacy and escape NSA's MORECOWBELL.
  99. (define-record-type* <name-service-switch> name-service-switch
  100. make-name-service-switch
  101. name-service-switch?
  102. (aliases name-service-switch-aliases
  103. (default '()))
  104. (ethers name-service-switch-ethers
  105. (default '()))
  106. (group name-service-switch-group
  107. (default (list %compat %files)))
  108. (gshadow name-service-switch-gshadow
  109. (default '()))
  110. (hosts name-service-switch-hosts
  111. (default (list %files %dns)))
  112. (initgroups name-service-switch-initgroups
  113. (default '()))
  114. (netgroup name-service-switch-netgroup
  115. (default '()))
  116. (networks name-service-switch-networks
  117. (default (list %files %dns)))
  118. (password name-service-switch-password
  119. (default (list %compat %files)))
  120. (public-key name-service-switch-public-key
  121. (default '()))
  122. (rpc name-service-switch-rpc
  123. (default '()))
  124. (services name-service-switch-services
  125. (default '()))
  126. (shadow name-service-switch-shadow
  127. (default (list %compat %files))))
  128. (define %default-nss
  129. ;; Default NSS configuration.
  130. (name-service-switch))
  131. (define %mdns-host-lookup-nss
  132. (name-service-switch
  133. (hosts (list %files ;first, check /etc/hosts
  134. ;; If the above did not succeed, try with 'mdns_minimal'.
  135. (name-service
  136. (name "mdns_minimal")
  137. ;; 'mdns_minimal' is authoritative for '.local'. When it
  138. ;; returns "not found", no need to try the next methods.
  139. (reaction (lookup-specification
  140. (not-found => return))))
  141. ;; Then fall back to DNS.
  142. (name-service
  143. (name "dns"))
  144. ;; Finally, try with the "full" 'mdns'.
  145. (name-service
  146. (name "mdns"))))))
  147. ;;;
  148. ;;; Serialization.
  149. ;;;
  150. (define (lookup-status->string status)
  151. (match status
  152. ('success "SUCCESS")
  153. ('not-found "NOTFOUND")
  154. ('unavailable "UNAVAIL")
  155. ('try-again "TRYAGAIN")
  156. (($ <lookup-status-negation> status)
  157. (string-append "!" (lookup-status->string status)))))
  158. (define lookup-reaction->string
  159. (match-lambda
  160. (($ <lookup-reaction> status action)
  161. (string-append (lookup-status->string status) "="
  162. (symbol->string action)))))
  163. (define name-service->string
  164. (match-lambda
  165. (($ <name-service> name ())
  166. name)
  167. (($ <name-service> name reactions)
  168. (string-append name " ["
  169. (string-join (map lookup-reaction->string reactions))
  170. "]"))))
  171. (define (name-service-switch->string nss)
  172. "Return the 'nsswitch.conf' contents for NSS as a string. See \"NSS
  173. Configuration File\" in the libc manual."
  174. (let-syntax ((->string
  175. (syntax-rules ()
  176. ((_ name field)
  177. (match (field nss)
  178. (() ;keep the default config
  179. "")
  180. ((services (... ...))
  181. (string-append name ":\t"
  182. (string-join
  183. (map name-service->string services))
  184. "\n")))))))
  185. (string-append (->string "aliases" name-service-switch-aliases)
  186. (->string "ethers" name-service-switch-ethers)
  187. (->string "group" name-service-switch-group)
  188. (->string "gshadow" name-service-switch-gshadow)
  189. (->string "hosts" name-service-switch-hosts)
  190. (->string "initgroups" name-service-switch-initgroups)
  191. (->string "netgroup" name-service-switch-netgroup)
  192. (->string "networks" name-service-switch-networks)
  193. (->string "passwd" name-service-switch-password)
  194. (->string "publickey" name-service-switch-public-key)
  195. (->string "rpc" name-service-switch-rpc)
  196. (->string "services" name-service-switch-services)
  197. (->string "shadow" name-service-switch-shadow))))
  198. ;;; Local Variables:
  199. ;;; eval: (put 'name-service 'scheme-indent-function 0)
  200. ;;; eval: (put 'name-service-switch 'scheme-indent-function 0)
  201. ;;; End:
  202. ;;; nss.scm ends here