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