Skip to content

Commit

Permalink
fish: move disk decryption helpers in own file
Browse files Browse the repository at this point in the history
This way it is easier to use them outside the rest of the code in
guestfish for inspection & mount.

Just code motion, no behaviour changes.
  • Loading branch information
ptoscano committed Sep 19, 2016
1 parent 21a2010 commit 0920b80
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 69 deletions.
1 change: 1 addition & 0 deletions align/Makefile.am
Expand Up @@ -33,6 +33,7 @@ SHARED_SOURCE_FILES = \
../df/parallel.c \
../df/parallel.h \
../fish/config.c \
../fish/decrypt.c \
../fish/display-options.h \
../fish/display-options.c \
../fish/domain.c \
Expand Down
1 change: 1 addition & 0 deletions cat/Makefile.am
Expand Up @@ -31,6 +31,7 @@ EXTRA_DIST = \
bin_PROGRAMS = virt-cat virt-filesystems virt-log virt-ls

SHARED_SOURCE_FILES = \
../fish/decrypt.c \
../fish/display-options.h \
../fish/display-options.c \
../fish/domain.c \
Expand Down
1 change: 1 addition & 0 deletions df/Makefile.am
Expand Up @@ -28,6 +28,7 @@ bin_PROGRAMS = virt-df

SHARED_SOURCE_FILES = \
../fish/config.c \
../fish/decrypt.c \
../fish/display-options.h \
../fish/display-options.c \
../fish/domain.c \
Expand Down
1 change: 1 addition & 0 deletions diff/Makefile.am
Expand Up @@ -27,6 +27,7 @@ bin_PROGRAMS = virt-diff
SHARED_SOURCE_FILES = \
../cat/visit.h \
../cat/visit.c \
../fish/decrypt.c \
../fish/display-options.h \
../fish/display-options.c \
../fish/domain.c \
Expand Down
1 change: 1 addition & 0 deletions edit/Makefile.am
Expand Up @@ -26,6 +26,7 @@ bin_PROGRAMS = virt-edit

SHARED_SOURCE_FILES = \
../fish/config.c \
../fish/decrypt.c \
../fish/display-options.h \
../fish/display-options.c \
../fish/domain.c \
Expand Down
1 change: 1 addition & 0 deletions fish/Makefile.am
Expand Up @@ -73,6 +73,7 @@ EXTRA_DIST = \
# files must not include other guestfish files.
SHARED_SOURCE_FILES = \
config.c \
decrypt.c \
display-options.h \
display-options.c \
domain.c \
Expand Down
102 changes: 102 additions & 0 deletions fish/decrypt.c
@@ -0,0 +1,102 @@
/* libguestfs - shared disk decryption
* Copyright (C) 2010 Red Hat Inc.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

/**
* This file implements the decryption of disk images, usually done
* before mounting their partitions.
*/

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "c-ctype.h"

#include "guestfs.h"

#include "options.h"

/**
* Make a LUKS map name from the partition name,
* eg. C<"/dev/vda2" =E<gt> "luksvda2">
*/
static void
make_mapname (const char *device, char *mapname, size_t len)
{
size_t i = 0;

if (len < 5)
abort ();
strcpy (mapname, "luks");
mapname += 4;
len -= 4;

if (STRPREFIX (device, "/dev/"))
i = 5;

for (; device[i] != '\0' && len >= 1; ++i) {
if (c_isalnum (device[i])) {
*mapname++ = device[i];
len--;
}
}

*mapname = '\0';
}

/**
* Simple implementation of decryption: look for any C<crypto_LUKS>
* partitions and decrypt them, then rescan for VGs. This only works
* for Fedora whole-disk encryption. WIP to make this work for other
* encryption schemes.
*/
void
inspect_do_decrypt (guestfs_h *g)
{
CLEANUP_FREE_STRING_LIST char **partitions = guestfs_list_partitions (g);
if (partitions == NULL)
exit (EXIT_FAILURE);

int need_rescan = 0;
size_t i;
for (i = 0; partitions[i] != NULL; ++i) {
CLEANUP_FREE char *type = guestfs_vfs_type (g, partitions[i]);
if (type && STREQ (type, "crypto_LUKS")) {
char mapname[32];
make_mapname (partitions[i], mapname, sizeof mapname);

CLEANUP_FREE char *key = read_key (partitions[i]);
/* XXX Should we call guestfs_luks_open_ro if readonly flag
* is set? This might break 'mount_ro'.
*/
if (guestfs_luks_open (g, partitions[i], key, mapname) == -1)
exit (EXIT_FAILURE);

need_rescan = 1;
}
}

if (need_rescan) {
if (guestfs_vgscan (g) == -1)
exit (EXIT_FAILURE);
if (guestfs_vg_activate_all (g, 1) == -1)
exit (EXIT_FAILURE);
}
}
68 changes: 0 additions & 68 deletions fish/inspect.c
Expand Up @@ -202,71 +202,3 @@ print_inspect_prompt (void)
dev ? dev : mountpoints[i+1], mountpoints[i]);
}
}

/**
* Make a LUKS map name from the partition name,
* eg. C<"/dev/vda2" =E<gt> "luksvda2">
*/
static void
make_mapname (const char *device, char *mapname, size_t len)
{
size_t i = 0;

if (len < 5)
abort ();
strcpy (mapname, "luks");
mapname += 4;
len -= 4;

if (STRPREFIX (device, "/dev/"))
i = 5;

for (; device[i] != '\0' && len >= 1; ++i) {
if (c_isalnum (device[i])) {
*mapname++ = device[i];
len--;
}
}

*mapname = '\0';
}

/**
* Simple implementation of decryption: look for any C<crypto_LUKS>
* partitions and decrypt them, then rescan for VGs. This only works
* for Fedora whole-disk encryption. WIP to make this work for other
* encryption schemes.
*/
void
inspect_do_decrypt (guestfs_h *g)
{
CLEANUP_FREE_STRING_LIST char **partitions = guestfs_list_partitions (g);
if (partitions == NULL)
exit (EXIT_FAILURE);

int need_rescan = 0;
size_t i;
for (i = 0; partitions[i] != NULL; ++i) {
CLEANUP_FREE char *type = guestfs_vfs_type (g, partitions[i]);
if (type && STREQ (type, "crypto_LUKS")) {
char mapname[32];
make_mapname (partitions[i], mapname, sizeof mapname);

CLEANUP_FREE char *key = read_key (partitions[i]);
/* XXX Should we call guestfs_luks_open_ro if readonly flag
* is set? This might break 'mount_ro'.
*/
if (guestfs_luks_open (g, partitions[i], key, mapname) == -1)
exit (EXIT_FAILURE);

need_rescan = 1;
}
}

if (need_rescan) {
if (guestfs_vgscan (g) == -1)
exit (EXIT_FAILURE);
if (guestfs_vg_activate_all (g, 1) == -1)
exit (EXIT_FAILURE);
}
}
4 changes: 3 additions & 1 deletion fish/options.h
Expand Up @@ -111,6 +111,9 @@ struct mp {
/* in config.c */
extern void parse_config (void);

/* in decrypt.c */
extern void inspect_do_decrypt (guestfs_h *g);

/* in domain.c */
extern int add_libvirt_drives (guestfs_h *g, const char *guest);

Expand All @@ -124,7 +127,6 @@ extern void print_inspect_prompt (void);

#if COMPILING_VIRT_INSPECTOR
/* (low-level inspection functions, used by virt-inspector only) */
extern void inspect_do_decrypt (guestfs_h *g);
extern void inspect_mount_root (guestfs_h *g, const char *root);
#endif

Expand Down
1 change: 1 addition & 0 deletions format/Makefile.am
Expand Up @@ -26,6 +26,7 @@ bin_PROGRAMS = virt-format

SHARED_SOURCE_FILES = \
../fish/config.c \
../fish/decrypt.c \
../fish/display-options.h \
../fish/display-options.c \
../fish/domain.c \
Expand Down
1 change: 1 addition & 0 deletions fuse/Makefile.am
Expand Up @@ -34,6 +34,7 @@ bin_PROGRAMS = \
# between guestfish and guestmount.
SHARED_SOURCE_FILES = \
../fish/config.c \
../fish/decrypt.c \
../fish/display-options.h \
../fish/display-options.c \
../fish/domain.c \
Expand Down
1 change: 1 addition & 0 deletions inspector/Makefile.am
Expand Up @@ -54,6 +54,7 @@ bin_PROGRAMS = virt-inspector

SHARED_SOURCE_FILES = \
../fish/config.c \
../fish/decrypt.c \
../fish/display-options.h \
../fish/display-options.c \
../fish/domain.c \
Expand Down
1 change: 1 addition & 0 deletions rescue/Makefile.am
Expand Up @@ -27,6 +27,7 @@ bin_PROGRAMS = virt-rescue

SHARED_SOURCE_FILES = \
../fish/config.c \
../fish/decrypt.c \
../fish/display-options.h \
../fish/display-options.c \
../fish/domain.c \
Expand Down

0 comments on commit 0920b80

Please sign in to comment.