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
|
/*
luksipc - Tool to convert block devices to LUKS in-place.
Copyright (C) 2011-2015 Johannes Bauer
This file is part of luksipc.
luksipc 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; this program is ONLY licensed under
version 3 of the License, later versions are explicitly excluded.
luksipc 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 luksipc; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Johannes Bauer <JohannesBauer@gmx.de>
*/
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "exec.h"
#include "luks.h"
#include "logging.h"
#include "globals.h"
#include "utils.h"
#include "random.h"
/* Checks is the given block device has already been formatted with LUKS. */
bool isLuks(const char *aBlockDevice) {
const char *arguments[] = {
"cryptsetup",
"isLuks",
aBlockDevice,
NULL
};
struct execResult_t execResult = execGetReturnCode(arguments);
return execResult.success && (execResult.returnCode == 0);
}
/* Returns if the given device mapper name is available (i.e. not active at the
* moment) */
bool isLuksMapperAvailable(const char *aMapperName) {
const char *arguments[] = {
"cryptsetup",
"status",
aMapperName,
NULL
};
logmsg(LLVL_DEBUG, "Performing dm-crypt status lookup on mapper name '%s'\n", aMapperName);
struct execResult_t execResult = execGetReturnCode(arguments);
bool mapperAvailable = execResult.success && (execResult.returnCode == 4);
logmsg(LLVL_DEBUG, "Device mapper name '%s' is %savailable (execution %s, returncode %d).\n", aMapperName, mapperAvailable ? "" : "NOT ", execResult.success ? "successful" : "failed", execResult.returnCode);
return mapperAvailable;
}
/* Formats a block device with LUKS using the given key file for slot 0 and
* passes some optional parameters (comma-separated) to cryptsetup */
bool luksFormat(const char *aBlkDevice, const char *aKeyFile, const char *aOptionalParams) {
int argcnt = -1;
char userSuppliedArguments[MAX_ARGLENGTH];
const char *arguments[MAX_ARG_CNT] = {
"cryptsetup",
"luksFormat",
"-q",
"--key-file",
aKeyFile,
NULL
};
if (aOptionalParams) {
if (!safestrcpy(userSuppliedArguments, aOptionalParams, MAX_ARGLENGTH)) {
logmsg(LLVL_ERROR, "Unable to copy user supplied argument, %d bytes max.\n", MAX_ARGLENGTH);
return false;
}
if (!argAppendParse(arguments, userSuppliedArguments, &argcnt, MAX_ARG_CNT)) {
logmsg(LLVL_ERROR, "Unable to copy user supplied argument, %d count max.\n", MAX_ARG_CNT);
return false;
}
}
if (!argAppend(arguments, aBlkDevice, &argcnt, MAX_ARG_CNT)) {
logmsg(LLVL_ERROR, "Unable to copy last user supplied argument, %d count max.\n", MAX_ARG_CNT);
return false;
}
logmsg(LLVL_DEBUG, "Performing luksFormat of block device %s using key file %s\n", aBlkDevice, aKeyFile);
struct execResult_t execResult = execGetReturnCode(arguments);
if ((!execResult.success) || (execResult.returnCode != 0)) {
logmsg(LLVL_ERROR, "luksFormat failed (execution %s, return code %d), aborting.\n", execResult.success ? "successful" : "failed", execResult.returnCode);
return false;
}
return true;
}
bool luksOpen(const char *aBlkDevice, const char *aKeyFile, const char *aHandle) {
const char *arguments[] = {
"cryptsetup",
"luksOpen",
"--key-file",
aKeyFile,
aBlkDevice,
aHandle,
NULL
};
logmsg(LLVL_DEBUG, "Performing luksOpen of block device %s using key file %s and device mapper handle %s\n", aBlkDevice, aKeyFile, aHandle);
struct execResult_t execResult = execGetReturnCode(arguments);
if ((!execResult.success) || (execResult.returnCode != 0)) {
logmsg(LLVL_ERROR, "luksOpen failed (execution %s, return code %d).\n", execResult.success ? "successful" : "failed", execResult.returnCode);
return false;
}
return true;
}
bool dmCreateAlias(const char *aSrcDevice, const char *aMapperHandle) {
uint64_t devSize = getDiskSizeOfPath(aSrcDevice);
if (devSize % 512) {
logmsg(LLVL_ERROR, "Device size of %s (%lu bytes) is not divisible by even 512 bytes sector size.\n", aSrcDevice, devSize);
return false;
}
char mapperTable[256];
snprintf(mapperTable, sizeof(mapperTable), "0 %lu linear %s 0", devSize / 512, aSrcDevice);
const char *arguments[] = {
"dmsetup",
"create",
aMapperHandle,
"--table",
mapperTable,
NULL
};
struct execResult_t execResult = execGetReturnCode(arguments);
if ((!execResult.success) || (execResult.returnCode != 0)) {
logmsg(LLVL_ERROR, "dmsetup alias creation failed (execution %s, returncode %d).\n", execResult.success ? "successful" : "failed", execResult.returnCode);
return false;
}
char aliasDeviceFilename[256];
snprintf(aliasDeviceFilename, sizeof(aliasDeviceFilename), "/dev/mapper/%s", aMapperHandle);
uint64_t aliasDevSize = getDiskSizeOfPath(aliasDeviceFilename);
if (devSize != aliasDevSize) {
logmsg(LLVL_ERROR, "Source device (%s) and its supposed alias device (%s) have different sizes (src = %lu and alias = %lu).\n", aSrcDevice, aliasDeviceFilename, devSize, aliasDevSize);
dmRemove(aMapperHandle);
return false;
}
logmsg(LLVL_DEBUG, "Created device mapper alias: %s -> %s\n", aliasDeviceFilename, aSrcDevice);
return true;
}
char *dmCreateDynamicAlias(const char *aSrcDevice, const char *aAliasPrefix) {
char alias[64];
if (aAliasPrefix && (strlen(aAliasPrefix) < 32)) {
snprintf(alias, sizeof(alias), "alias_%s_", aAliasPrefix);
} else {
strcpy(alias, "alias_");
}
if (!randomHexStrCat(alias, 4)) {
return NULL;
}
char *aliasPathname = malloc(strlen("/dev/mapper/") + strlen(alias) + 1);
if (!aliasPathname) {
logmsg(LLVL_ERROR, "malloc error for full filename of dynamic alias: %s\n", strerror(errno));
return NULL;
}
sprintf(aliasPathname, "/dev/mapper/%s", alias);
bool aliasSuccessful = dmCreateAlias(aSrcDevice, alias);
if (!aliasSuccessful) {
free(aliasPathname);
return NULL;
}
return aliasPathname;
}
bool dmRemove(const char *aMapperHandle) {
const char *arguments[] = {
"dmsetup",
"remove",
aMapperHandle,
NULL
};
/* Device cannot be closed if it is still open. udev will usually call
* blkid on the device after it is closed after been written to. Therefore
* it is possible that "dmsetup remove" fails immediately after closing the
* device (because blkid will have an open handle). We simply wait a bit
* and try again later if this happens. */
struct execResult_t execResult;
for (int try = 0; try < 10; try++) {
execResult = execGetReturnCode(arguments);
if (!execResult.success) {
return false;
}
if ((execResult.success) && (execResult.returnCode == 0)) {
break;
}
sleep(1);
}
bool success = (execResult.success) && (execResult.returnCode == 0) && isLuksMapperAvailable(aMapperHandle);
if (!success) {
logmsg(LLVL_ERROR, "Cannot remove device mapper handle %s (execution %s, return code %d)\n", aMapperHandle, execResult.success ? "successful" : "failed", execResult.returnCode);
}
return success;
}
|