cups.scm 43 KB

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