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