cups.scm 43 KB

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