cups.scm 44 KB

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