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