summaryrefslogtreecommitdiffstats
path: root/cryptodev.py
blob: 63dcd4a635b34efce558837a2371c635a68d591b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#
# cryptodev.py
#
# Copyright (C) 2007  Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Author(s): Dave Lehman <dlehman@redhat.com>
#

import os
import iutil

import logging
log = logging.getLogger("anaconda")

def isLuks(device):
    if not device.startswith("/"):
        device = "/dev/" + device
    rc = iutil.execWithRedirect("cryptsetup",
                                ["isLuks", device],
                                stdout = "/dev/null",
                                stderr = "/dev/null",
                                searchPath = 1)
    if rc:
        return False
    else:
        return True

def luksUUID(device):
    if not device.startswith("/"):
        device = "/dev/" + device

    if not isLuks(device):
        return None

    uuid = iutil.execWithCapture("cryptsetup", ["luksUUID", device])
    uuid = uuid.strip()
    return uuid

class LUKSDevice:
    """LUKSDevice represents an encrypted block device using LUKS/dm-crypt.
       It requires an underlying block device and a passphrase to become
       functional."""
    def __init__(self, device=None, passphrase=None, format=0):
        self._device = None
        self.__passphrase = ""
        self.name = ""
        self.uuid = None
        self.nameLocked = False
        self.format = format
        self.preexist = not format
        self.packages = ["cryptsetup-luks"]
        self.scheme = "LUKS"

        self.setDevice(device)
        self.setPassphrase(passphrase)
        if self.getUUID():
            name = "%s-%s" % (self.scheme.lower(), self.uuid)
            self.setName(name, lock=True)

    def getScheme(self):
        """Returns the name of the encryption scheme used by the device."""
        return self.scheme

    def setDevice(self, device):
        if self._device == device:
            return

        self._device = device
        if device is not None:
            # this will be temporary, but may be useful for debugging
            name = "%s-%s" % (self.scheme.lower(),
                              os.path.basename(device))
            self.setName(name)

    def getDevice(self, encrypted=0):
        if encrypted:
            dev = self._device
        else:
            dev = "mapper/%s" % (self.name,)

        return dev

    def getUUID(self):
        if self.format:
            # self.format means we're going to reformat but haven't yet
            # so we shouldn't act like there's anything worth seeing there
            return

        if not self.uuid:
            self.uuid = luksUUID(self.getDevice(encrypted=1))

        return self.uuid

    def setName(self, name, lock=False):
        """Set the name of the mapped device, eg: 'dmcrypt-sda3'"""
        if self.name == name:
            return

        if self.name and not self.getStatus():
            raise RuntimeError, "Cannot rename an active mapping."

        if self.nameLocked:
            log.debug("Failed to change locked mapping name: %s" % 
                      (self.name,))
            return

        self.name = name
        if lock and name:
            # don't allow anyone to lock the name as "" or None
            self.nameLocked = True

    def setPassphrase(self, passphrase):
        """Set the (plaintext) passphrase used to access the device."""
        self.__passphrase = passphrase

    def hasPassphrase(self):
        return self.__passphrase not in (None, "")

    def crypttab(self):
        """Return a crypttab formatted line describing this mapping."""
        format = "%-23s %-15s %s\n"
        line = format % (self.name,
                         "UUID=%s" % (self.getUUID(),),
                         "none")
        return line

    def getStatus(self):
        """0 means active, 1 means inactive (or non-existent)"""
        if not self.name:
            return 1

        rc = iutil.execWithRedirect("cryptsetup",
                                    ["status", self.name],
                                    stdout = "/dev/null",
                                    stderr = "/dev/null",
                                    searchPath = 1)
        return rc

    def formatDevice(self):
        """Write a LUKS header onto the device."""
        if not self.format:
            return

        if not self.getStatus():
            log.debug("refusing to format active mapping %s" % (self.name,))
            return 1

        if not self.hasPassphrase():
            raise RuntimeError, "Cannot create mapping without a passphrase."

        device = self.getDevice(encrypted=1)
        if not device:
            raise ValueError, "Cannot open mapping without a device."

        log.info("formatting %s as %s" % (device, self.getScheme()))
        p = os.pipe()
        os.write(p[1], "%s\n" % (self.__passphrase,))
        os.close(p[1])

        rc = iutil.execWithRedirect("cryptsetup",
                                    ["-q", "luksFormat",
                                     "/dev/%s" % (device,)],
                                    stdin = p[0],
                                    stdout = "/dev/null",
                                    stderr = "/dev/tty5",
                                    searchPath = 1)
        self.format = 0
        return rc

    def openDevice(self):
        if not self.getStatus():
            # already mapped
            return 0

        if not self.hasPassphrase():
            raise RuntimeError, "Cannot create mapping without a passphrase."

        device = self.getDevice(encrypted=1)
        if not device:
            raise ValueError, "Cannot open mapping without a device."

        uuid = self.getUUID()
        if not uuid:
            raise RuntimeError, "Device has no UUID."

        self.setName("%s-%s" % (self.scheme.lower(), uuid), lock=True)

        log.info("mapping %s device %s to %s" % (self.getScheme(),
                                                 device,
                                                 self.name))

        p = os.pipe()
        os.write(p[1], "%s\n" % (self.__passphrase,))
        os.close(p[1])

        rc = iutil.execWithRedirect("cryptsetup",
                                    ["luksOpen",
                                     "/dev/%s" % (device,),
                                     self.name],
                                    stdin = p[0],
                                    stdout = "/dev/null",
                                    stderr = "/dev/tty5",
                                    searchPath = 1)
        return rc

    def closeDevice(self):
        if self.getStatus():
            # not mapped
            return 0

        log.info("unmapping %s device %s" % (self.getScheme(), self.name))
        rc = iutil.execWithRedirect("cryptsetup",
                                    ["luksClose", self.name],
                                    stdout = "/dev/null",
                                    stderr = "/dev/tty5",
                                    searchPath = 1)
        return rc

    def addPassphrase(self, newpass):
        if not newpass:
            return 1

        if newpass == self.__passphrase:
            return 0

        p = os.pipe()
        os.write(p[1], "%s\n%s" % (self.__passphrase, newpass))
        os.close(p[1])

        device = self.getDevice(encrypted=1)
        log.info("adding new passphrase to %s device %s" % (self.getScheme(),
                                                            device))
        rc = iutil.execWithRedirect("cryptsetup",
                                    ["-q",
                                     "luksAddKey",
                                     "/dev/%s" % (device,)],
                                    stdin = p[0],
                                    stdout = "/dev/null",
                                    stderr = "/dev/tty5",
                                    searchPath = 1)

        return rc