services: pcscd: Cleanup socket when started.
[jackhill/guix/guix.git] / gnu / services / security-token.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018 Arun Isaac <arunisaac@systemreboot.net>
3 ;;; Copyright © 2020 Tobias Geerinckx-Rice <me@tobias.gr>
4 ;;; Copyright © 2021 Brice Waegeneire <brice@waegenei.re>
5 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 (define-module (gnu services security-token)
22 #:use-module (gnu services)
23 #:use-module (gnu services shepherd)
24 #:use-module (gnu packages admin)
25 #:use-module (gnu packages base)
26 #:use-module (gnu packages security-token)
27 #:use-module (gnu system shadow)
28 #:use-module (guix gexp)
29 #:use-module (guix modules)
30 #:use-module (guix records)
31 #:use-module (ice-9 match)
32 #:use-module (srfi srfi-26)
33 #:export (pcscd-configuration
34 pcscd-configuration?
35 pcscd-configuration-pcsc-lite
36 pcscd-configuration-usb-drivers
37 pcscd-service-type))
38
39 ;;;
40 ;;; PC/SC Smart Card Daemon
41 ;;;
42
43 (define-record-type* <pcscd-configuration>
44 pcscd-configuration make-pcscd-configuration pcscd-configuration?
45 (pcsc-lite pcscd-configuration-pcsc-lite
46 (default pcsc-lite))
47 (usb-drivers pcscd-configuration-usb-drivers
48 (default (list ccid))))
49
50 (define pcscd-shepherd-service
51 (match-lambda
52 (($ <pcscd-configuration> pcsc-lite)
53 (with-imported-modules (source-module-closure
54 '((gnu build shepherd)))
55 (shepherd-service
56 (documentation "PC/SC Smart Card Daemon")
57 (provision '(pcscd))
58 (requirement '(syslogd))
59 (modules '((gnu build shepherd)))
60 (start #~(lambda _
61 (let ((socket "/run/pcscd/pcscd.comm"))
62 (when (file-exists? socket)
63 (delete-file socket)))
64 (invoke #$(file-append pcsc-lite "/sbin/pcscd"))
65 (call-with-input-file "/run/pcscd/pcscd.pid" read)))
66 (stop #~(make-kill-destructor)))))))
67
68 (define pcscd-activation
69 (match-lambda
70 (($ <pcscd-configuration> pcsc-lite usb-drivers)
71 (with-imported-modules (source-module-closure
72 '((guix build utils)))
73 #~(begin
74 (use-modules (guix build utils))
75 ;; XXX: We can't use (guix utils) because it requires a
76 ;; dynamically-linked Guile, hence the duplicate switch-symlinks.
77 (define (switch-symlinks link target)
78 (let ((pivot (string-append link ".new")))
79 (symlink target pivot)
80 (rename-file pivot link)))
81 (mkdir-p "/var/lib")
82 (switch-symlinks "/var/lib/pcsc"
83 #$(directory-union
84 "pcsc"
85 (map (cut file-append <> "/pcsc")
86 usb-drivers))))))))
87
88 (define pcscd-service-type
89 (service-type
90 (name 'pcscd)
91 (description
92 "Run @command{pcscd}, the PC/SC smart card daemon.")
93 (extensions
94 (list (service-extension shepherd-root-service-type
95 (compose list pcscd-shepherd-service))
96 (service-extension activation-service-type
97 pcscd-activation)))
98 (default-value (pcscd-configuration))))