cups.scm 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2016 Andy Wingo <wingo@pobox.com>
  3. ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org>
  4. ;;; Copyright © 2018 Ricardo Wurmus <rekado@elephly.net>
  5. ;;; Copyright © 2019 Alex Griffin <a@ajgrf.com>
  6. ;;; Copyright © 2019 Tobias Geerinckx-Rice <me@tobias.gr>
  7. ;;; Copyright © 2021 Maxime Devos <maximedevos@telenet.be>
  8. ;;; Copyright © 2023 muradm <mail@muradm.net>
  9. ;;;
  10. ;;; This file is part of GNU Guix.
  11. ;;;
  12. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  13. ;;; under the terms of the GNU General Public License as published by
  14. ;;; the Free Software Foundation; either version 3 of the License, or (at
  15. ;;; your option) any later version.
  16. ;;;
  17. ;;; GNU Guix is distributed in the hope that it will be useful, but
  18. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;;; GNU General Public License for more details.
  21. ;;;
  22. ;;; You should have received a copy of the GNU General Public License
  23. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  24. (define-module (gnu services cups)
  25. #:use-module (gnu services)
  26. #:use-module (gnu services shepherd)
  27. #:use-module (gnu services configuration)
  28. #:use-module (gnu system pam)
  29. #:use-module (gnu system shadow)
  30. #:use-module (gnu packages admin)
  31. #:use-module (gnu packages cups)
  32. #:use-module (gnu packages tls)
  33. #:use-module (guix packages)
  34. #:use-module (guix records)
  35. #:use-module (guix gexp)
  36. #:use-module (guix modules)
  37. #:use-module (ice-9 match)
  38. #:use-module ((srfi srfi-1) #:select (append-map find))
  39. #:export (cups-service-type
  40. cups-configuration
  41. opaque-cups-configuration
  42. files-configuration
  43. policy-configuration
  44. location-access-control
  45. operation-access-control
  46. method-access-control))
  47. ;;; Commentary:
  48. ;;;
  49. ;;; Service defininition for the CUPS printing system.
  50. ;;;
  51. ;;; Code:
  52. (define %cups-accounts
  53. (list (or
  54. ;; The "lp" group should already exist; try to reuse it.
  55. (find (lambda (group)
  56. (and (user-group? group)
  57. (string=? (user-group-name group) "lp")))
  58. %base-groups)
  59. (user-group (name "lp") (system? #t)))
  60. (user-group (name "lpadmin") (system? #t))
  61. (user-account
  62. (name "lp")
  63. (group "lp")
  64. (system? #t)
  65. (comment "System user for invoking printing helper programs")
  66. (home-directory "/var/empty")
  67. (shell (file-append shadow "/sbin/nologin")))))
  68. (define (uglify-field-name field-name)
  69. (let ((str (symbol->string field-name)))
  70. (string-concatenate
  71. (map string-titlecase
  72. (string-split (if (string-suffix? "?" str)
  73. (substring str 0 (1- (string-length str)))
  74. str)
  75. #\-)))))
  76. (define (serialize-field field-name val)
  77. (format #t "~a ~a\n" (uglify-field-name field-name) val))
  78. (define (serialize-string field-name val)
  79. (serialize-field field-name val))
  80. (define (multiline-string-list? val)
  81. (and (list? val)
  82. (and-map (lambda (x)
  83. (and (string? x) (not (string-index x #\space))))
  84. val)))
  85. (define (serialize-multiline-string-list field-name val)
  86. (for-each (lambda (str) (serialize-field field-name str)) val))
  87. (define (comma-separated-string-list? val)
  88. (and (list? val)
  89. (and-map (lambda (x)
  90. (and (string? x) (not (string-index x #\,))))
  91. val)))
  92. (define (serialize-comma-separated-string-list field-name val)
  93. (serialize-field field-name (string-join val ",")))
  94. ;; A special case of the above, where we don't want to emit anything at all
  95. ;; when #f, to allow CUPS to pick a default we cannot compute ourselves.
  96. ;; XXX Ideally, this could be a generic higher-order function, but it's used
  97. ;; only once so far: for ready-paper-sizes to handle "Auto" default-paper-size.
  98. (define (comma-separated-string-list-or-#f? val)
  99. (if val (comma-separated-string-list? val) #t))
  100. (define (serialize-comma-separated-string-list-or-#f field-name val)
  101. (if val (serialize-field field-name (string-join val ",")) #f))
  102. (define (space-separated-string-list? val)
  103. (and (list? val)
  104. (and-map (lambda (x)
  105. (and (string? x) (not (string-index x #\space))))
  106. val)))
  107. (define (serialize-space-separated-string-list field-name val)
  108. (serialize-field field-name (string-join val " ")))
  109. (define (space-separated-symbol-list? val)
  110. (and (list? val) (and-map symbol? val)))
  111. (define (serialize-space-separated-symbol-list field-name val)
  112. (serialize-field field-name (string-join (map symbol->string val) " ")))
  113. (define (file-name? val)
  114. (and (string? val)
  115. (string-prefix? "/" val)))
  116. (define (serialize-file-name field-name val)
  117. (serialize-string field-name val))
  118. (define (serialize-boolean field-name val)
  119. (serialize-string field-name (if val "yes" "no")))
  120. (define (non-negative-integer? val)
  121. (and (exact-integer? val) (not (negative? val))))
  122. (define (serialize-non-negative-integer field-name val)
  123. (serialize-field field-name val))
  124. (define-syntax define-enumerated-field-type
  125. (lambda (x)
  126. (define (id-append ctx . parts)
  127. (datum->syntax ctx (apply symbol-append (map syntax->datum parts))))
  128. (syntax-case x ()
  129. ((_ name (option ...))
  130. #`(begin
  131. (define (#,(id-append #'name #'name #'?) x)
  132. (memq x '(option ...)))
  133. (define (#,(id-append #'name #'serialize- #'name) field-name val)
  134. (serialize-field field-name val)))))))
  135. (define-enumerated-field-type access-log-level
  136. (config actions all))
  137. (define-enumerated-field-type browse-local-protocols
  138. (all dnssd none))
  139. (define-enumerated-field-type default-auth-type
  140. (Basic))
  141. (define-enumerated-field-type default-encryption
  142. (Never IfRequested Required))
  143. (define-enumerated-field-type error-policy
  144. (abort-job retry-job retry-current-job stop-printer))
  145. (define-enumerated-field-type log-level
  146. (none emerg alert crit error warn notice info debug debug2))
  147. (define-enumerated-field-type log-time-format
  148. (standard usecs))
  149. (define-enumerated-field-type server-tokens
  150. (None ProductOnly Major Minor Minimal OS Full))
  151. (define-enumerated-field-type method
  152. (DELETE GET HEAD OPTIONS POST PUT TRACE))
  153. (define-enumerated-field-type sandboxing
  154. (relaxed strict))
  155. (define (method-list? val)
  156. (and (list? val) (and-map method? val)))
  157. (define (serialize-method-list field-name val)
  158. (serialize-field field-name (string-join (map symbol->string val) " ")))
  159. (define (host-name-lookups? val)
  160. (memq val '(#f #t 'double)))
  161. (define (serialize-host-name-lookups field-name val)
  162. (serialize-field field-name
  163. (match val (#f "No") (#t "Yes") ('double "Double"))))
  164. (define (host-name-list-or-*? x)
  165. (or (eq? x '*)
  166. (and (list? x) (and-map string? x))))
  167. (define (serialize-host-name-list-or-* field-name val)
  168. (serialize-field field-name (match val
  169. ('* '*)
  170. (names (string-join names " ")))))
  171. (define (boolean-or-non-negative-integer? x)
  172. (or (boolean? x) (non-negative-integer? x)))
  173. (define (serialize-boolean-or-non-negative-integer field-name x)
  174. (if (boolean? x)
  175. (serialize-boolean field-name x)
  176. (serialize-non-negative-integer field-name x)))
  177. (define (ssl-options? x)
  178. (and (list? x)
  179. (and-map (lambda (elt) (memq elt '(AllowRC4
  180. AllowSSL3
  181. DenyCBC
  182. DenyTLS1.0))) x)))
  183. (define (serialize-ssl-options field-name val)
  184. (serialize-field field-name
  185. (match val
  186. (() "None")
  187. (opts (string-join (map symbol->string opts) " ")))))
  188. (define (serialize-access-control x)
  189. (display x)
  190. (newline))
  191. (define (serialize-access-control-list field-name val)
  192. (for-each serialize-access-control val))
  193. (define (access-control-list? val)
  194. (and (list? val) (and-map string? val)))
  195. (define-configuration operation-access-control
  196. (operations
  197. (space-separated-symbol-list '())
  198. "IPP operations to which this access control applies.")
  199. (access-controls
  200. (access-control-list '())
  201. "Access control directives, as a list of strings. Each string should be one directive, such as \"Order allow,deny\"."))
  202. (define-configuration method-access-control
  203. (reverse?
  204. (boolean #f)
  205. "If @code{#t}, apply access controls to all methods except the listed
  206. methods. Otherwise apply to only the listed methods.")
  207. (methods
  208. (method-list '())
  209. "Methods to which this access control applies.")
  210. (access-controls
  211. (access-control-list '())
  212. "Access control directives, as a list of strings. Each string should be one directive, such as \"Order allow,deny\"."))
  213. (define (serialize-operation-access-control x)
  214. (format #t "<Limit ~a>\n"
  215. (string-join (map symbol->string
  216. (operation-access-control-operations x)) " "))
  217. (serialize-configuration
  218. x
  219. (filter (lambda (field)
  220. (not (eq? (configuration-field-name field) 'operations)))
  221. operation-access-control-fields))
  222. (format #t "</Limit>\n"))
  223. (define (serialize-method-access-control x)
  224. (let ((limit (if (method-access-control-reverse? x) "LimitExcept" "Limit")))
  225. (format #t "<~a ~a>\n" limit
  226. (string-join (map symbol->string
  227. (method-access-control-methods x)) " "))
  228. (serialize-configuration
  229. x
  230. (filter (lambda (field)
  231. (case (configuration-field-name field)
  232. ((reverse? methods) #f)
  233. (else #t)))
  234. method-access-control-fields))
  235. (format #t "</~a>\n" limit)))
  236. (define (operation-access-control-list? val)
  237. (and (list? val) (and-map operation-access-control? val)))
  238. (define (serialize-operation-access-control-list field-name val)
  239. (for-each serialize-operation-access-control val))
  240. (define (method-access-control-list? val)
  241. (and (list? val) (and-map method-access-control? val)))
  242. (define (serialize-method-access-control-list field-name val)
  243. (for-each serialize-method-access-control val))
  244. (define-configuration location-access-control
  245. (path
  246. (file-name (configuration-missing-field 'location-access-control 'path))
  247. "Specifies the URI path to which the access control applies.")
  248. (access-controls
  249. (access-control-list '())
  250. "Access controls for all access to this path, in the same format as the
  251. @code{access-controls} of @code{operation-access-control}.")
  252. (method-access-controls
  253. (method-access-control-list '())
  254. "Access controls for method-specific access to this path."))
  255. (define (serialize-location-access-control x)
  256. (format #t "<Location ~a>\n" (location-access-control-path x))
  257. (serialize-configuration
  258. x
  259. (filter (lambda (field)
  260. (not (eq? (configuration-field-name field) 'path)))
  261. location-access-control-fields))
  262. (format #t "</Location>\n"))
  263. (define (location-access-control-list? val)
  264. (and (list? val) (and-map location-access-control? val)))
  265. (define (serialize-location-access-control-list field-name val)
  266. (for-each serialize-location-access-control val))
  267. (define-configuration policy-configuration
  268. (name
  269. (string (configuration-missing-field 'policy-configuration 'name))
  270. "Name of the policy.")
  271. (job-private-access
  272. (string "@OWNER @SYSTEM")
  273. "Specifies an access list for a job's private values.
  274. @code{@@ACL} maps to the printer's requesting-user-name-allowed or
  275. requesting-user-name-denied values. @code{@@OWNER} maps to the job's owner.
  276. @code{@@SYSTEM} maps to the groups listed for the @code{system-group} field of
  277. the @code{files-configuration}, which is reified into the
  278. @code{cups-files.conf(5)} file.
  279. Other possible elements of the access list include specific user names, and
  280. @code{@@@var{group}} to indicate members of a specific group. The access list
  281. may also be simply @code{all} or @code{default}.")
  282. (job-private-values
  283. (string (string-join '("job-name" "job-originating-host-name"
  284. "job-originating-user-name" "phone")))
  285. "Specifies the list of job values to make private, or @code{all},
  286. @code{default}, or @code{none}.")
  287. (subscription-private-access
  288. (string "@OWNER @SYSTEM")
  289. "Specifies an access list for a subscription's private values.
  290. @code{@@ACL} maps to the printer's requesting-user-name-allowed or
  291. requesting-user-name-denied values. @code{@@OWNER} maps to the job's owner.
  292. @code{@@SYSTEM} maps to the groups listed for the @code{system-group} field of
  293. the @code{files-configuration}, which is reified into the
  294. @code{cups-files.conf(5)} file.
  295. Other possible elements of the access list include specific user names, and
  296. @code{@@@var{group}} to indicate members of a specific group. The access list
  297. may also be simply @code{all} or @code{default}.")
  298. (subscription-private-values
  299. (string (string-join '("notify-events" "notify-pull-method"
  300. "notify-recipient-uri" "notify-subscriber-user-name"
  301. "notify-user-data")
  302. " "))
  303. "Specifies the list of job values to make private, or @code{all},
  304. @code{default}, or @code{none}.")
  305. (access-controls
  306. (operation-access-control-list '())
  307. "Access control by IPP operation."))
  308. (define (serialize-policy-configuration x)
  309. (format #t "<Policy ~a>\n" (policy-configuration-name x))
  310. (serialize-configuration
  311. x
  312. (filter (lambda (field)
  313. (not (eq? (configuration-field-name field) 'name)))
  314. policy-configuration-fields))
  315. (format #t "</Policy>\n"))
  316. (define (policy-configuration-list? x)
  317. (and (list? x) (and-map policy-configuration? x)))
  318. (define (serialize-policy-configuration-list field-name x)
  319. (for-each serialize-policy-configuration x))
  320. (define (log-location? x)
  321. (or (file-name? x)
  322. (eq? x 'stderr)
  323. (eq? x 'syslog)))
  324. (define (serialize-log-location field-name x)
  325. (if (string? x)
  326. (serialize-file-name field-name x)
  327. (serialize-field field-name x)))
  328. (define-configuration files-configuration
  329. (access-log
  330. (log-location "/var/log/cups/access_log")
  331. "Defines the access log filename. Specifying a blank filename disables
  332. access log generation. The value @code{stderr} causes log entries to be sent
  333. to the standard error file when the scheduler is running in the foreground, or
  334. to the system log daemon when run in the background. The value @code{syslog}
  335. causes log entries to be sent to the system log daemon. The server name may
  336. be included in filenames using the string @code{%s}, as in
  337. @code{/var/log/cups/%s-access_log}.")
  338. (cache-dir
  339. (file-name "/var/cache/cups")
  340. "Where CUPS should cache data.")
  341. (config-file-perm
  342. (string "0640")
  343. "Specifies the permissions for all configuration files that the scheduler
  344. writes.
  345. Note that the permissions for the printers.conf file are currently masked to
  346. only allow access from the scheduler user (typically root). This is done
  347. because printer device URIs sometimes contain sensitive authentication
  348. information that should not be generally known on the system. There is no way
  349. to disable this security feature.")
  350. ;; Not specifying data-dir and server-bin options as we handle these
  351. ;; manually. For document-root, the CUPS package has that path
  352. ;; preconfigured.
  353. (error-log
  354. (log-location "/var/log/cups/error_log")
  355. "Defines the error log filename. Specifying a blank filename disables
  356. access log generation. The value @code{stderr} causes log entries to be sent
  357. to the standard error file when the scheduler is running in the foreground, or
  358. to the system log daemon when run in the background. The value @code{syslog}
  359. causes log entries to be sent to the system log daemon. The server name may
  360. be included in filenames using the string @code{%s}, as in
  361. @code{/var/log/cups/%s-error_log}.")
  362. (fatal-errors
  363. (string "all -browse")
  364. "Specifies which errors are fatal, causing the scheduler to exit. The kind
  365. strings are:
  366. @table @code
  367. @item none
  368. No errors are fatal.
  369. @item all
  370. All of the errors below are fatal.
  371. @item browse
  372. Browsing initialization errors are fatal, for example failed connections to
  373. the DNS-SD daemon.
  374. @item config
  375. Configuration file syntax errors are fatal.
  376. @item listen
  377. Listen or Port errors are fatal, except for IPv6 failures on the loopback or
  378. @code{any} addresses.
  379. @item log
  380. Log file creation or write errors are fatal.
  381. @item permissions
  382. Bad startup file permissions are fatal, for example shared TLS certificate and
  383. key files with world-read permissions.
  384. @end table")
  385. (file-device?
  386. (boolean #f)
  387. "Specifies whether the file pseudo-device can be used for new printer
  388. queues. The URI @url{file:///dev/null} is always allowed.")
  389. (group
  390. (string "lp")
  391. "Specifies the group name or ID that will be used when executing external
  392. programs.")
  393. (log-file-group
  394. (string "lpadmin")
  395. "Specifies the group name or ID that will be used for log files.")
  396. (log-file-perm
  397. (string "0644")
  398. "Specifies the permissions for all log files that the scheduler writes.")
  399. (page-log
  400. (log-location "/var/log/cups/page_log")
  401. "Defines the page log filename. Specifying a blank filename disables
  402. access log generation. The value @code{stderr} causes log entries to be sent
  403. to the standard error file when the scheduler is running in the foreground, or
  404. to the system log daemon when run in the background. The value @code{syslog}
  405. causes log entries to be sent to the system log daemon. The server name may
  406. be included in filenames using the string @code{%s}, as in
  407. @code{/var/log/cups/%s-page_log}.")
  408. (remote-root
  409. (string "remroot")
  410. "Specifies the username that is associated with unauthenticated accesses by
  411. clients claiming to be the root user. The default is @code{remroot}.")
  412. (request-root
  413. (file-name "/var/spool/cups")
  414. "Specifies the directory that contains print jobs and other HTTP request
  415. data.")
  416. (sandboxing
  417. (sandboxing 'strict)
  418. "Specifies the level of security sandboxing that is applied to print
  419. filters, backends, and other child processes of the scheduler; either
  420. @code{relaxed} or @code{strict}. This directive is currently only
  421. used/supported on macOS.")
  422. (server-keychain
  423. (file-name "/etc/cups/ssl")
  424. "Specifies the location of TLS certificates and private keys. CUPS will
  425. look for public and private keys in this directory: a @code{.crt} files for
  426. PEM-encoded certificates and corresponding @code{.key} files for PEM-encoded
  427. private keys.")
  428. (server-root
  429. (file-name "/etc/cups")
  430. "Specifies the directory containing the server configuration files.")
  431. (sync-on-close?
  432. (boolean #f)
  433. "Specifies whether the scheduler calls fsync(2) after writing configuration
  434. or state files.")
  435. (system-group
  436. (space-separated-string-list '("lpadmin" "wheel" "root"))
  437. "Specifies the group(s) to use for @code{@@SYSTEM} group authentication.")
  438. (temp-dir
  439. (file-name "/var/spool/cups/tmp")
  440. "Specifies the directory where temporary files are stored.")
  441. (user
  442. (string "lp")
  443. "Specifies the user name or ID that is used when running external
  444. programs.")
  445. (set-env
  446. (string "variable value")
  447. "Set the specified environment variable to be passed to child processes."))
  448. (define (serialize-files-configuration field-name val)
  449. #f)
  450. (define (environment-variables? vars)
  451. (space-separated-string-list? vars))
  452. (define (serialize-environment-variables field-name vars)
  453. (unless (null? vars)
  454. (serialize-space-separated-string-list field-name vars)))
  455. (define (package-list? val)
  456. (and (list? val) (and-map file-like? val)))
  457. (define (serialize-package-list field-name val)
  458. #f)
  459. (define-configuration cups-configuration
  460. (cups
  461. (file-like cups)
  462. "The CUPS package.")
  463. (allow-empty-password?
  464. (boolean #f)
  465. "Specifies whether empty passwords will be allowed when authenticating via PAM.")
  466. (extensions
  467. (package-list (list brlaser cups-filters epson-inkjet-printer-escpr
  468. foomatic-filters hplip-minimal splix))
  469. "Drivers and other extensions to the CUPS package.")
  470. (files-configuration
  471. (files-configuration (files-configuration))
  472. "Configuration of where to write logs, what directories to use for print
  473. spools, and related privileged configuration parameters.")
  474. (access-log-level
  475. (access-log-level 'actions)
  476. "Specifies the logging level for the AccessLog file. The @code{config}
  477. level logs when printers and classes are added, deleted, or modified and when
  478. configuration files are accessed or updated. The @code{actions} level logs
  479. when print jobs are submitted, held, released, modified, or canceled, and any
  480. of the conditions for @code{config}. The @code{all} level logs all
  481. requests.")
  482. (auto-purge-jobs?
  483. (boolean #f)
  484. "Specifies whether to purge job history data automatically when it is no
  485. longer required for quotas.")
  486. (browse-dns-sd-sub-types
  487. (comma-separated-string-list (list "_cups" "_print" "_universal"))
  488. "Specifies a list of DNS-SD sub-types to advertise for each shared printer.
  489. The default @samp{(list \"_cups\" \"_print\" \"_universal\")} tells clients
  490. that CUPS sharing, IPP Everywhere, AirPrint, and Mopria are supported.")
  491. (browse-local-protocols
  492. (browse-local-protocols 'dnssd)
  493. "Specifies which protocols to use for local printer sharing.")
  494. (browse-web-if?
  495. (boolean #f)
  496. "Specifies whether the CUPS web interface is advertised.")
  497. (browsing?
  498. (boolean #f)
  499. "Specifies whether shared printers are advertised.")
  500. (default-auth-type
  501. (default-auth-type 'Basic)
  502. "Specifies the default type of authentication to use.")
  503. (default-encryption
  504. (default-encryption 'Required)
  505. "Specifies whether encryption will be used for authenticated requests.")
  506. (default-language
  507. (string "en")
  508. "Specifies the default language to use for text and web content.")
  509. (default-paper-size
  510. (string "Auto")
  511. "Specifies the default paper size for new print queues. @samp{\"Auto\"}
  512. uses a locale-specific default, while @samp{\"None\"} specifies there is no
  513. default paper size. Specific size names are typically @samp{\"Letter\"} or
  514. @samp{\"A4\"}.")
  515. (default-policy
  516. (string "default")
  517. "Specifies the default access policy to use.")
  518. (default-shared?
  519. (boolean #t)
  520. "Specifies whether local printers are shared by default.")
  521. (dirty-clean-interval
  522. (non-negative-integer 30)
  523. "Specifies the delay for updating of configuration and state files, in
  524. seconds. A value of 0 causes the update to happen as soon as possible,
  525. typically within a few milliseconds.")
  526. (error-policy
  527. (error-policy 'stop-printer)
  528. "Specifies what to do when an error occurs. Possible values are
  529. @code{abort-job}, which will discard the failed print job; @code{retry-job},
  530. which will retry the job at a later time; @code{retry-current-job}, which retries
  531. the failed job immediately; and @code{stop-printer}, which stops the
  532. printer.")
  533. (filter-limit
  534. (non-negative-integer 0)
  535. "Specifies the maximum cost of filters that are run concurrently, which can
  536. be used to minimize disk, memory, and CPU resource problems. A limit of 0
  537. disables filter limiting. An average print to a non-PostScript printer needs
  538. a filter limit of about 200. A PostScript printer needs about half
  539. that (100). Setting the limit below these thresholds will effectively limit
  540. the scheduler to printing a single job at any time.")
  541. (filter-nice
  542. (non-negative-integer 0)
  543. "Specifies the scheduling priority of filters that are run to print a job.
  544. The nice value ranges from 0, the highest priority, to 19, the lowest
  545. priority.")
  546. (host-name-lookups
  547. (host-name-lookups #f)
  548. "Specifies whether to do reverse lookups on connecting clients.
  549. The @code{double} setting causes @code{cupsd} to verify that the hostname
  550. resolved from the address matches one of the addresses returned for that
  551. hostname. Double lookups also prevent clients with unregistered addresses
  552. from connecting to your server. Only set this option to @code{#t} or
  553. @code{double} if absolutely required.")
  554. ;; Add this option if the package is built with socket activation support.
  555. ;; (idle-exit-timeout
  556. ;; (non-negative-integer 60)
  557. ;; "Specifies the length of time to wait before shutting down due to
  558. ;; inactivity. Note: Only applicable when @code{cupsd} is run on-demand
  559. ;; (e.g., with @code{-l}).")
  560. (job-kill-delay
  561. (non-negative-integer 30)
  562. "Specifies the number of seconds to wait before killing the filters and
  563. backend associated with a canceled or held job.")
  564. (job-retry-interval
  565. (non-negative-integer 30)
  566. "Specifies the interval between retries of jobs in seconds. This is
  567. typically used for fax queues but can also be used with normal print queues
  568. whose error policy is @code{retry-job} or @code{retry-current-job}.")
  569. (job-retry-limit
  570. (non-negative-integer 5)
  571. "Specifies the number of retries that are done for jobs. This is typically
  572. used for fax queues but can also be used with normal print queues whose error
  573. policy is @code{retry-job} or @code{retry-current-job}.")
  574. (keep-alive?
  575. (boolean #t)
  576. "Specifies whether to support HTTP keep-alive connections.")
  577. (limit-request-body
  578. (non-negative-integer 0)
  579. "Specifies the maximum size of print files, IPP requests, and HTML form
  580. data. A limit of 0 disables the limit check.")
  581. (listen
  582. (multiline-string-list '("localhost:631" "/var/run/cups/cups.sock"))
  583. "Listens on the specified interfaces for connections. Valid values are of
  584. the form @var{address}:@var{port}, where @var{address} is either an IPv6
  585. address enclosed in brackets, an IPv4 address, or @code{*} to indicate all
  586. addresses. Values can also be file names of local UNIX domain sockets. The
  587. Listen directive is similar to the Port directive but allows you to restrict
  588. access to specific interfaces or networks.")
  589. (location-access-controls
  590. (location-access-control-list
  591. (list (location-access-control
  592. (path "/")
  593. (access-controls '("Order allow,deny"
  594. "Allow localhost")))
  595. (location-access-control
  596. (path "/admin")
  597. (access-controls '("Order allow,deny"
  598. "Allow localhost")))
  599. (location-access-control
  600. (path "/admin/conf")
  601. (access-controls '("Order allow,deny"
  602. "AuthType Basic"
  603. "Require user @SYSTEM"
  604. "Allow localhost")))))
  605. "Specifies a set of additional access controls.")
  606. (log-debug-history
  607. (non-negative-integer 100)
  608. "Specifies the number of debugging messages that are retained for logging
  609. if an error occurs in a print job. Debug messages are logged regardless of
  610. the LogLevel setting.")
  611. (log-level
  612. (log-level 'info)
  613. "Specifies the level of logging for the ErrorLog file. The value
  614. @code{none} stops all logging while @code{debug2} logs everything.")
  615. (log-time-format
  616. (log-time-format 'standard)
  617. "Specifies the format of the date and time in the log files. The value
  618. @code{standard} logs whole seconds while @code{usecs} logs microseconds.")
  619. (max-clients
  620. (non-negative-integer 100)
  621. "Specifies the maximum number of simultaneous clients that are allowed by
  622. the scheduler.")
  623. (max-clients-per-host
  624. (non-negative-integer 100)
  625. "Specifies the maximum number of simultaneous clients that are allowed from
  626. a single address.")
  627. (max-copies
  628. (non-negative-integer 9999)
  629. "Specifies the maximum number of copies that a user can print of each
  630. job.")
  631. (max-hold-time
  632. (non-negative-integer 0)
  633. "Specifies the maximum time a job may remain in the @code{indefinite} hold
  634. state before it is canceled. A value of 0 disables cancellation of held
  635. jobs.")
  636. (max-jobs
  637. (non-negative-integer 500)
  638. "Specifies the maximum number of simultaneous jobs that are allowed. Set
  639. to 0 to allow an unlimited number of jobs.")
  640. (max-jobs-per-printer
  641. (non-negative-integer 0)
  642. "Specifies the maximum number of simultaneous jobs that are allowed per
  643. printer. A value of 0 allows up to MaxJobs jobs per printer.")
  644. (max-jobs-per-user
  645. (non-negative-integer 0)
  646. "Specifies the maximum number of simultaneous jobs that are allowed per
  647. user. A value of 0 allows up to @code{max-jobs} jobs per user.")
  648. (max-job-time
  649. (non-negative-integer 10800)
  650. "Specifies the maximum time a job may take to print before it is canceled,
  651. in seconds. Set to 0 to disable cancellation of \"stuck\" jobs.")
  652. (max-log-size
  653. (non-negative-integer 1048576)
  654. "Specifies the maximum size of the log files before they are rotated, in
  655. bytes. The value 0 disables log rotation.")
  656. (max-subscriptions
  657. (non-negative-integer 100)
  658. "Specifies the maximum number of simultaneous event subscriptions that are
  659. allowed. Set to @samp{0} to allow an unlimited number of subscriptions.")
  660. (max-subscriptions-per-job
  661. (non-negative-integer 0)
  662. "Specifies the maximum number of simultaneous event subscriptions that are
  663. allowed per job. A value of @samp{0} allows up to @code{max-subscriptions}
  664. per job.")
  665. (max-subscriptions-per-printer
  666. (non-negative-integer 0)
  667. "Specifies the maximum number of simultaneous event subscriptions that are
  668. allowed per printer. A value of @samp{0} allows up to @code{max-subscriptions}
  669. per printer.")
  670. (max-subscriptions-per-user
  671. (non-negative-integer 0)
  672. "Specifies the maximum number of simultaneous event subscriptions that are
  673. allowed per user. A value of @samp{0} allows up to @code{max-subscriptions}
  674. per user.")
  675. (multiple-operation-timeout
  676. (non-negative-integer 900)
  677. "Specifies the maximum amount of time to allow between files in a multiple
  678. file print job, in seconds.")
  679. (environment-variables
  680. (environment-variables '())
  681. "Passes the specified environment variable(s) to child processes; a list of
  682. strings.")
  683. (policies
  684. (policy-configuration-list
  685. (list (policy-configuration
  686. (name "default")
  687. (access-controls
  688. (list
  689. (operation-access-control
  690. (operations
  691. '(Send-Document
  692. Send-URI Hold-Job Release-Job Restart-Job Purge-Jobs
  693. Cancel-Job Close-Job Cancel-My-Jobs Set-Job-Attributes
  694. Create-Job-Subscription Renew-Subscription
  695. Cancel-Subscription Get-Notifications
  696. Reprocess-Job Cancel-Current-Job Suspend-Current-Job
  697. Resume-Job CUPS-Move-Job Validate-Job
  698. CUPS-Get-Document))
  699. (access-controls '("Require user @OWNER @SYSTEM"
  700. "Order deny,allow")))
  701. (operation-access-control
  702. (operations
  703. '(Pause-Printer
  704. Cancel-Jobs
  705. Resume-Printer Set-Printer-Attributes Enable-Printer
  706. Disable-Printer Pause-Printer-After-Current-Job
  707. Hold-New-Jobs Release-Held-New-Jobs Deactivate-Printer
  708. Activate-Printer Restart-Printer Shutdown-Printer
  709. Startup-Printer Promote-Job Schedule-Job-After
  710. CUPS-Authenticate-Job CUPS-Add-Printer
  711. CUPS-Delete-Printer CUPS-Add-Class CUPS-Delete-Class
  712. CUPS-Accept-Jobs CUPS-Reject-Jobs CUPS-Set-Default))
  713. (access-controls '("AuthType Basic"
  714. "Require user @SYSTEM"
  715. "Order deny,allow")))
  716. (operation-access-control
  717. (operations '(All))
  718. (access-controls '("Order deny,allow"))))))))
  719. "Specifies named access control policies.")
  720. #;
  721. (port
  722. (non-negative-integer 631)
  723. "Listens to the specified port number for connections.")
  724. (preserve-job-files
  725. (boolean-or-non-negative-integer 86400)
  726. "Specifies whether job files (documents) are preserved after a job is
  727. printed. If a numeric value is specified, job files are preserved for the
  728. indicated number of seconds after printing. Otherwise a boolean value applies
  729. indefinitely.")
  730. (preserve-job-history
  731. (boolean-or-non-negative-integer #t)
  732. "Specifies whether the job history is preserved after a job is printed.
  733. If a numeric value is specified, the job history is preserved for the
  734. indicated number of seconds after printing. If @code{#t}, the job history is
  735. preserved until the MaxJobs limit is reached.")
  736. (ready-paper-sizes
  737. (comma-separated-string-list-or-#f #f)
  738. "Specifies a list of potential paper sizes that are reported as ready,
  739. that is: loaded. The actual list will contain only the sizes that each
  740. printer supports. If @code{#f}, CUPS will assume
  741. @samp{(list \"Letter\" \"Legal\" \"Tabloid\" \"4x6\" \"Env10\")}
  742. if the default paper size is \"Letter\", and
  743. @samp{(list \"A3\" \"A4\" \"A5\" \"A6\" \"EnvDL\")} otherwise.")
  744. (reload-timeout
  745. (non-negative-integer 30)
  746. "Specifies the amount of time to wait for job completion before restarting
  747. the scheduler.")
  748. (server-admin
  749. (string "root@localhost.localdomain")
  750. "Specifies the email address of the server administrator.")
  751. (server-alias
  752. (host-name-list-or-* '*)
  753. "The ServerAlias directive is used for HTTP Host header validation when
  754. clients connect to the scheduler from external interfaces. Using the special
  755. name @code{*} can expose your system to known browser-based DNS rebinding
  756. attacks, even when accessing sites through a firewall. If the auto-discovery
  757. of alternate names does not work, we recommend listing each alternate name
  758. with a ServerAlias directive instead of using @code{*}.")
  759. (server-name
  760. (string "localhost")
  761. "Specifies the fully-qualified host name of the server.")
  762. (server-tokens
  763. (server-tokens 'Minimal)
  764. "Specifies what information is included in the Server header of HTTP
  765. responses. @code{None} disables the Server header. @code{ProductOnly}
  766. reports @code{CUPS}. @code{Major} reports @code{CUPS 2}. @code{Minor}
  767. reports @code{CUPS 2.0}. @code{Minimal} reports @code{CUPS 2.0.0}. @code{OS}
  768. reports @code{CUPS 2.0.0 (@var{uname})} where @var{uname} is the output of the
  769. @code{uname} command. @code{Full} reports @code{CUPS 2.0.0 (@var{uname})
  770. IPP/2.0}.")
  771. (ssl-listen
  772. (multiline-string-list '())
  773. "Listens on the specified interfaces for encrypted connections. Valid
  774. values are of the form @var{address}:@var{port}, where @var{address} is either
  775. an IPv6 address enclosed in brackets, an IPv4 address, or @code{*} to indicate
  776. all addresses.")
  777. (ssl-options
  778. (ssl-options '())
  779. "Sets encryption options. By default, CUPS only supports encryption
  780. using TLS v1.0 or higher using known secure cipher suites. Security is
  781. reduced when @code{Allow} options are used, and enhanced when @code{Deny}
  782. options are used. The @code{AllowRC4} option enables the 128-bit RC4 cipher
  783. suites, which are required for some older clients. The @code{AllowSSL3} option
  784. enables SSL v3.0, which is required for some older clients that do not support
  785. TLS v1.0. The @code{DenyCBC} option disables all CBC cipher suites. The
  786. @code{DenyTLS1.0} option disables TLS v1.0 support - this sets the minimum
  787. protocol version to TLS v1.1.")
  788. #;
  789. (ssl-port
  790. (non-negative-integer 631)
  791. "Listens on the specified port for encrypted connections.")
  792. (strict-conformance?
  793. (boolean #f)
  794. "Specifies whether the scheduler requires clients to strictly adhere to the
  795. IPP specifications.")
  796. (timeout
  797. (non-negative-integer 900)
  798. "Specifies the HTTP request timeout, in seconds.")
  799. (web-interface?
  800. (boolean #f)
  801. "Specifies whether the web interface is enabled."))
  802. (define-configuration opaque-cups-configuration
  803. (cups
  804. (package cups)
  805. "The CUPS package.")
  806. (allow-empty-password?
  807. (boolean #f)
  808. "Specifies whether empty passwords will be allowed when authenticating via PAM.")
  809. (extensions
  810. (package-list '())
  811. "Drivers and other extensions to the CUPS package.")
  812. (cupsd.conf
  813. (string (configuration-missing-field 'opaque-cups-configuration
  814. 'cupsd.conf))
  815. "The contents of the @code{cupsd.conf} to use.")
  816. (cups-files.conf
  817. (string (configuration-missing-field 'opaque-cups-configuration
  818. 'cups-files.conf))
  819. "The contents of the @code{cups-files.conf} to use."))
  820. (define %cups-activation
  821. ;; Activation gexp.
  822. (with-imported-modules (source-module-closure '((gnu build activation)
  823. (guix build utils)))
  824. #~(begin
  825. (use-modules (gnu build activation)
  826. (guix build utils))
  827. (define (build-subject parameters)
  828. (string-concatenate
  829. (map (lambda (pair)
  830. (let ((k (car pair)) (v (cdr pair)))
  831. (define (escape-char str chr)
  832. (string-join (string-split str chr) (string #\\ chr)))
  833. (string-append "/" k "="
  834. (escape-char (escape-char v #\=) #\/))))
  835. (filter (lambda (pair) (cdr pair)) parameters))))
  836. (define* (create-self-signed-certificate-if-absent
  837. #:key private-key public-key (owner (getpwnam "root"))
  838. (common-name (gethostname))
  839. (organization-name "Guix")
  840. (organization-unit-name "Default Self-Signed Certificate")
  841. (subject-parameters `(("CN" . ,common-name)
  842. ("O" . ,organization-name)
  843. ("OU" . ,organization-unit-name)))
  844. (subject (build-subject subject-parameters)))
  845. ;; Note that by default, OpenSSL outputs keys in PEM format. This
  846. ;; is what we want.
  847. (unless (file-exists? private-key)
  848. (cond
  849. ((zero? (system* (string-append #$openssl "/bin/openssl")
  850. "genrsa" "-out" private-key "2048"))
  851. (chown private-key (passwd:uid owner) (passwd:gid owner))
  852. (chmod private-key #o400))
  853. (else
  854. (format (current-error-port)
  855. "Failed to create private key at ~a.\n" private-key))))
  856. (unless (file-exists? public-key)
  857. (cond
  858. ((zero? (system* (string-append #$openssl "/bin/openssl")
  859. "req" "-new" "-x509" "-key" private-key
  860. "-out" public-key "-days" "3650"
  861. "-batch" "-subj" subject))
  862. (chown public-key (passwd:uid owner) (passwd:gid owner))
  863. (chmod public-key #o444))
  864. (else
  865. (format (current-error-port)
  866. "Failed to create public key at ~a.\n" public-key)))))
  867. (let ((user (getpwnam "lp")))
  868. (mkdir-p/perms "/var/run/cups" user #o755)
  869. (mkdir-p/perms "/var/spool/cups" user #o755)
  870. (mkdir-p/perms "/var/spool/cups/tmp" user #o755)
  871. (mkdir-p/perms "/var/log/cups" user #o755)
  872. (mkdir-p/perms "/var/cache/cups" user #o770)
  873. (mkdir-p/perms "/etc/cups" user #o755)
  874. (mkdir-p/perms "/etc/cups/ssl" user #o700)
  875. ;; This certificate is used for HTTPS connections to the CUPS web
  876. ;; interface.
  877. (create-self-signed-certificate-if-absent
  878. #:private-key "/etc/cups/ssl/localhost.key"
  879. #:public-key "/etc/cups/ssl/localhost.crt"
  880. #:owner (getpwnam "root")
  881. #:common-name (format #f "CUPS service on ~a" (gethostname)))))))
  882. (define (union-directory name packages paths)
  883. (computed-file
  884. name
  885. (with-imported-modules '((guix build utils))
  886. #~(begin
  887. (use-modules (guix build utils)
  888. (srfi srfi-1))
  889. (mkdir #$output)
  890. (for-each
  891. (lambda (package)
  892. (for-each
  893. (lambda (path)
  894. (for-each
  895. (lambda (src)
  896. (let* ((tail (substring src (string-length package)))
  897. (dst (string-append #$output tail)))
  898. (mkdir-p (dirname dst))
  899. ;; CUPS currently symlinks in some data from cups-filters
  900. ;; to its output dir. Probably we should stop doing this
  901. ;; and instead rely only on the CUPS service to union the
  902. ;; relevant set of CUPS packages.
  903. (if (file-exists? dst)
  904. (format (current-error-port) "warning: ~a exists\n" dst)
  905. (symlink src dst))))
  906. (find-files (string-append package path) #:stat stat)))
  907. (list #$@paths)))
  908. (list #$@packages))
  909. #t))))
  910. (define (cups-server-bin-directory extensions)
  911. "Return the CUPS ServerBin directory, containing binaries for CUPS and all
  912. extensions that it uses."
  913. (union-directory "cups-server-bin" extensions
  914. ;; /bin
  915. '("/lib/cups" "/share/ppd" "/share/cups")))
  916. (define (cups-shepherd-service config)
  917. "Return a list of <shepherd-service> for CONFIG."
  918. (let* ((cupsd.conf-str
  919. (cond
  920. ((opaque-cups-configuration? config)
  921. (opaque-cups-configuration-cupsd.conf config))
  922. (else
  923. (with-output-to-string
  924. (lambda ()
  925. (serialize-configuration config
  926. cups-configuration-fields))))))
  927. (cups-files.conf-str
  928. (cond
  929. ((opaque-cups-configuration? config)
  930. (opaque-cups-configuration-cups-files.conf config))
  931. (else
  932. (with-output-to-string
  933. (lambda ()
  934. (serialize-configuration
  935. (cups-configuration-files-configuration config)
  936. files-configuration-fields))))))
  937. (cups (if (opaque-cups-configuration? config)
  938. (opaque-cups-configuration-cups config)
  939. (cups-configuration-cups config)))
  940. (server-bin
  941. (cups-server-bin-directory
  942. (cons cups
  943. (cond
  944. ((opaque-cups-configuration? config)
  945. (opaque-cups-configuration-extensions config))
  946. (else
  947. (cups-configuration-extensions config))))))
  948. ;;"SetEnv PATH " server-bin "/bin" "\n"
  949. (cupsd.conf
  950. (plain-file "cupsd.conf" cupsd.conf-str))
  951. (cups-files.conf
  952. (mixed-text-file
  953. "cups-files.conf"
  954. cups-files.conf-str
  955. "CacheDir /var/cache/cups\n"
  956. "StateDir /var/run/cups\n"
  957. "DataDir " server-bin "/share/cups" "\n"
  958. "ServerBin " server-bin "/lib/cups" "\n")))
  959. (list (shepherd-service
  960. (documentation "Run the CUPS print server.")
  961. (provision '(cups))
  962. (requirement '(networking))
  963. (start #~(make-forkexec-constructor
  964. (list (string-append #$cups "/sbin/cupsd")
  965. "-f" "-c" #$cupsd.conf "-s" #$cups-files.conf)))
  966. (stop #~(make-kill-destructor))))))
  967. (define (cups-pam-service config)
  968. (let ((allow-empty-password?
  969. (if (opaque-cups-configuration? config)
  970. (opaque-cups-configuration-allow-empty-password? config)
  971. (cups-configuration-allow-empty-password? config))))
  972. (list (unix-pam-service "cups"
  973. #:allow-empty-passwords? allow-empty-password?))))
  974. (define cups-service-type
  975. (service-type (name 'cups)
  976. (extensions
  977. (list (service-extension shepherd-root-service-type
  978. cups-shepherd-service)
  979. (service-extension activation-service-type
  980. (const %cups-activation))
  981. (service-extension pam-root-service-type
  982. cups-pam-service)
  983. (service-extension account-service-type
  984. (const %cups-accounts))))
  985. ;; Extensions consist of lists of packages (representing CUPS
  986. ;; drivers, etc) that we just concatenate.
  987. (compose append)
  988. ;; Add extension packages by augmenting the cups-configuration
  989. ;; 'extensions' field.
  990. (extend
  991. (lambda (config extensions)
  992. (cond
  993. ((cups-configuration? config)
  994. (cups-configuration
  995. (inherit config)
  996. (extensions
  997. (append (cups-configuration-extensions config)
  998. extensions))))
  999. (else
  1000. (opaque-cups-configuration
  1001. (inherit config)
  1002. (extensions
  1003. (append (opaque-cups-configuration-extensions config)
  1004. extensions)))))))
  1005. (default-value (cups-configuration))
  1006. (description
  1007. "Run the CUPS print server.")))
  1008. ;; A little helper to make it easier to document all those fields.
  1009. (define (generate-cups-documentation)
  1010. (generate-documentation
  1011. `((cups-configuration
  1012. ,cups-configuration-fields
  1013. (files-configuration files-configuration)
  1014. (policies policy-configuration)
  1015. (location-access-controls location-access-controls))
  1016. (files-configuration ,files-configuration-fields)
  1017. (policy-configuration
  1018. ,policy-configuration-fields
  1019. (operation-access-controls operation-access-controls))
  1020. (location-access-controls
  1021. ,location-access-control-fields
  1022. (method-access-controls method-access-controls))
  1023. (operation-access-controls ,operation-access-control-fields)
  1024. (method-access-controls ,method-access-control-fields))
  1025. 'cups-configuration))